Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
bfd468d5e3 | ||
|
|
8353ba38fe | ||
|
|
b43a08b2e2 | ||
|
|
1908bf1a53 | ||
|
|
c44ea38ae2 | ||
|
|
d489f7c088 | ||
|
|
a993cfd76a |
No files matched your search
@@ -38,7 +38,7 @@ A `.mq.md` file must stay readable as Markdown. If a human cannot skim the data
|
||||
|
||||
Cells are expressions (`doc/design/table-cell-expressions.md`): bare words = text, `` `var` `` = variable, quote paths/`/` ratios.
|
||||
|
||||
### 2. Good patterns
|
||||
### 2. Good patterns (copy these — not `json.set`)
|
||||
|
||||
**Wire / steps / commands** — `@` record table:
|
||||
|
||||
@@ -60,7 +60,53 @@ Cells are expressions (`doc/design/table-cell-expressions.md`): bare words = tex
|
||||
| `wire` | `set_text` |
|
||||
```
|
||||
|
||||
**Single dynamic key** — one `table.put` (or a helper), not a json chain:
|
||||
**Agent / LLM result map** — one readable row, not a `json.parse` + `json.set` chain:
|
||||
|
||||
```markdown
|
||||
`out` =
|
||||
|
||||
| status | task | result | llm_calls | tokens |
|
||||
|--------|------|--------|-----------|--------|
|
||||
| ok | `task` | `reply` | `llm_calls` | `tokens` |
|
||||
|
||||
*out*
|
||||
```
|
||||
|
||||
**Soft error pack** (transport / gate failure) — still a table humans can skim:
|
||||
|
||||
```markdown
|
||||
`pack` =
|
||||
|
||||
| finish | text | error | usage | tool_calls |
|
||||
|--------|------|-------|-------|------------|
|
||||
| error | "" | `msg` | `usage0` | `empty_tc` |
|
||||
|
||||
*pack*
|
||||
```
|
||||
|
||||
**Optional / conditional key** — sparse `table.put` after the static table (one dynamic key only):
|
||||
|
||||
```markdown
|
||||
`out` =
|
||||
|
||||
| status | task |
|
||||
|--------|------|
|
||||
| error | `task` |
|
||||
|
||||
1. `err`
|
||||
**out = > table.put in=`out` at="error" value=`err`**
|
||||
2. *
|
||||
**_ = 1**
|
||||
```
|
||||
|
||||
**Empty list / empty map** — prefer stdlib seeds over `json.parse text=[]` / `{}`:
|
||||
|
||||
```markdown
|
||||
**empty_tc = > table.empty_list**
|
||||
**empty_map = > table.empty_map**
|
||||
```
|
||||
|
||||
**Single dynamic key from scratch** — one `table.put` (or a helper), not a json chain:
|
||||
|
||||
```markdown
|
||||
**`set_text` = table.put in=None at="#log" value=msg**
|
||||
@@ -73,14 +119,20 @@ Better still — call a named helper:
|
||||
**browser.set_text sel="#log" text=msg**
|
||||
```
|
||||
|
||||
### 3. Bad pattern (do not write)
|
||||
**When `json.*` is OK:** `json.parse` / `json.stringify` / `json.quote` on **wire text**; `json.get` to read an opaque map. Never use `json.set` / `json.append` to *build* author-facing structures.
|
||||
|
||||
### 3. Bad pattern (do not write — and do not copy from old `ext/ai`)
|
||||
|
||||
```markdown
|
||||
**`ret` = json.set map=None key="set_text" value=…**
|
||||
**`ret` = json.set map=ret key="canvas" value=…**
|
||||
**`cmds` = json.append list=cmds item=c0**
|
||||
**`out` = json.parse text={"status":"ok"}**
|
||||
**`out` = json.set map=out key="task" value=task**
|
||||
```
|
||||
|
||||
Matching surrounding **legacy** `json.set` in `ext/ai/*.mq.md` is **not** compliance. Refactor the call site to tables / `table.put` instead of extending the glue.
|
||||
|
||||
## Writing application `.mq.md`
|
||||
|
||||
1. Frontmatter: title + imports (`table`, domain libs). **Do not import `json` unless parsing strings.**
|
||||
|
||||
@@ -32,7 +32,7 @@ Canonical design (repo): `doc/design/markdown-mapping.md`, `doc/design/keywords.
|
||||
8. Prefer ending side-effect-only function bodies with `*None*` / `*无*` / whole-line `**` / `****` when later lines must belong to an outer function. Lone `---` / `***` are Markdown thematic breaks (skipped), not function end.
|
||||
9. **`ext/**` never calls `host_*`.** Agent/OKF helpers are plugin names (`agent_kb_*`, …) after `plugin.load`. Do **not** add agent/OKF domain code to `src/host/` (core bloat). See `doc/design/ext-agent.md` §4.
|
||||
10. **Browser (route C/D/E/F):** client logic is Marqdo on WASM; official bridge is host glue and **may** implement lists, routing, storage, WebSocket, canvas, file read, observers — authors **must not hand-write business JS**. Use `web.client_embed` / `data-mq-source-url` auto-mount. Client effects: prefer **GFM tables + `lib/browser`** ([marqdo-dev](../marqdo-dev/SKILL.md)).
|
||||
11. **Code-as-documentation / no bag glue:** Prefer **GFM tables** for maps, lists, wire, commands. Prefer **`table.put` / named helpers** over `json.set` / `json.append` chains. `lib/json` is for parse/stringify/quote only — not a dict builder. Unreadable json pipelines are a style bug. Read collections with **`[key](map)`**, not footnote indexes for new code.
|
||||
11. **Code-as-documentation / no bag glue:** Prefer **GFM tables** for maps, lists, wire, commands. Prefer **`table.put` / named helpers** over `json.set` / `json.append` chains. `lib/json` is for parse/stringify/quote only — not a dict builder. Unreadable json pipelines are a style bug. Read collections with **`[key](map)`**, not footnote indexes for new code. **Positive copy-paste shapes** (result map, soft-error pack, sparse `table.put`): [marqdo-dev](../marqdo-dev/SKILL.md) §2 · [examples.md](examples.md) §6e. Do **not** extend legacy `json.set` in `ext/ai` — refactor to tables.
|
||||
12. **One language per file:** Do not mix EN and ZH API names in the same `.mq.md` (imports, calls, params). Pick English *or* Chinese mirrors for the whole program.
|
||||
13. **Sandbox trial habit (Agent / generated `.mq.md`):** Prefer `agent.trial` / `run sandbox=True` / `sandbox.open`→`run`→`diff`→`commit` before promoting to the host tree. Host files must stay unchanged until a successful commit. Caps live in `sandbox.profile` GFM tables — do not invent a second dialect.
|
||||
|
||||
|
||||
@@ -162,6 +162,56 @@ import browser:lib/browser.mq.md
|
||||
|
||||
Do **not** build effect maps with `json.set` chains. See [marqdo-dev](../marqdo-dev/SKILL.md).
|
||||
|
||||
## 6e. Agent / LLM result maps — GFM first (positive)
|
||||
|
||||
Return and soft-error packs are **tables**, not `json.set` pipelines. Full style contract: [marqdo-dev](../marqdo-dev/SKILL.md) §2.
|
||||
|
||||
```markdown
|
||||
---
|
||||
import table:lib/table.mq.md
|
||||
---
|
||||
|
||||
## step_ok
|
||||
+ `task`
|
||||
+ `reply`
|
||||
|
||||
`out` =
|
||||
|
||||
| status | task | result |
|
||||
|--------|------|--------|
|
||||
| ok | `task` | `reply` |
|
||||
|
||||
*out*
|
||||
|
||||
## step_err
|
||||
+ `task`
|
||||
+ `err`
|
||||
|
||||
`out` =
|
||||
|
||||
| status | task | error |
|
||||
|--------|------|-------|
|
||||
| error | `task` | `err` |
|
||||
|
||||
*out*
|
||||
|
||||
## soft_http_pack
|
||||
+ `msg`
|
||||
+ `usage0`
|
||||
|
||||
**empty_tc = > table.empty_list**
|
||||
|
||||
`pack` =
|
||||
|
||||
| finish | text | error | usage | tool_calls |
|
||||
|--------|------|-------|-------|------------|
|
||||
| error | "" | `msg` | `usage0` | `empty_tc` |
|
||||
|
||||
*pack*
|
||||
```
|
||||
|
||||
Do **not** mirror legacy `ext/ai/agent.mq.md` `json.parse`+`json.set` chains when adding new arms — convert the shape to a table.
|
||||
|
||||
## 7. Import text stdlib
|
||||
|
||||
```markdown
|
||||
|
||||
@@ -126,6 +126,8 @@ jobs:
|
||||
## Marqdo ${{ github.ref_name }}
|
||||
|
||||
### 亮点 / Highlights
|
||||
- **v1.4.5**:Streamable HTTP MCP + web invoke Bearer/401 · openai/agent json.get 硬化
|
||||
- **v1.4.5**: Streamable HTTP MCP + web invoke Bearer/401; openai/agent json.get hardening
|
||||
- **v1.4.2**:Agent/LLM 优化 O1–O5——sandbox trial · promote proof · step_route/EFI/cost · lint · quarantine
|
||||
- **v1.4.2**: Agent/LLM optimize O1–O5 — sandbox trial, promote proof, step_route/EFI/cost, lint, quarantine
|
||||
- **v1.4.1**:view 标注连线——SVG mid→mid(underline 底中 → chip 顶中)· 帧 chip id 对齐 · L→R 密度
|
||||
|
||||
@@ -8,6 +8,82 @@
|
||||
|
||||
### Changed
|
||||
|
||||
## v1.4.5 — 2026-10-16
|
||||
|
||||
### Highlights
|
||||
|
||||
**Streamable HTTP MCP + web `/v1` 鉴权硬化**:`agent_mcp_serve` 支持 `transport=http`(MCP Streamable HTTP 2025-03-26);web invoke 注入 `Authorization` / `X-Api-Key`,`ok=false`+`unauthorized` → HTTP 401;`ext/ai` openai/agent 字段访问改 `json.get`,消除 footnote 硬崩。CLI · Ext · wasm **1.4.5**。
|
||||
|
||||
```bash
|
||||
git checkout v1.4.5 && cargo build --release
|
||||
cargo build --release -p marqdo_plugin_agent
|
||||
# HTTP MCP:
|
||||
# QDAGENT_MCP_TRANSPORT=http marqdo run cli/求道-mcp.mq.md
|
||||
marqdo ext add agent # CDN → 1.4.5
|
||||
```
|
||||
|
||||
**核心表面 Δ(Core surface Δ)**:**新增** MCP Streamable HTTP 传输(`transport=http|streamable-http`);web invoke 契约扩展(header 注入 · 401);**无** MQ 关键词 / 表几何变更。
|
||||
|
||||
### Added
|
||||
|
||||
- **`agent_mcp_serve` Streamable HTTP**:`transport=http` · `host`/`port`/`path`;POST JSON-RPC + 可选 GET SSE;`Mcp-Session-Id`。facade:`ext/ai/agent` `## serve`。
|
||||
|
||||
### Fixed
|
||||
|
||||
- **web invoke**:`Authorization` / `X-Api-Key` 注入 payload;`jsonWrapInvokeValue` 对 `unauthorized` 返回 401;`{sse}` 自动 event-stream。
|
||||
- **`ext/ai/llm/openai` · `agent` · `llm`**:字段访问改 `json.get` / `at`,避免 `[field](x)` 在非 map 上硬崩。
|
||||
- **explore workbook**:子任务 `stream=False`(网关 SSE 524 更稳)。
|
||||
|
||||
### Changed
|
||||
|
||||
- CLI / Ext / wasm SemVer **1.4.5**。
|
||||
- 设计:[doc/design/ext-web-artifact.md](doc/design/ext-web-artifact.md)(sse / bearer)。
|
||||
|
||||
## v1.4.4 — 2026-10-16
|
||||
|
||||
### Highlights
|
||||
|
||||
**LLM 传输软失败 + `ext/ai` 表优先重构**:上游连接拒绝/超时/HTTP≠200 时,Runtime 返回结构化 `finish|status=error`(不再硬中断 VM);`agent` / `智能体` / `llm/openai` 去掉 `json.set`/`json.append` 袋胶水。含 CLI host · `plugins/llm` · Ext pack **1.4.4**。
|
||||
|
||||
```bash
|
||||
git checkout v1.4.4 && cargo build --release
|
||||
cargo build --release -p marqdo_plugin_llm
|
||||
marqdo run tests/ext/agent-llm-transport-soft.mq.md
|
||||
marqdo ext add agent # CDN → 1.4.4
|
||||
```
|
||||
|
||||
**核心表面 Δ(Core surface Δ)**:**无增删**——HTTP 宿主软失败契约 + Extension 风格重构;不改 MQ 关键词 / 表几何。
|
||||
|
||||
### Fixed
|
||||
|
||||
- **LLM / Agent 传输软失败**:`host_http_post` / SSE · `plugins/llm` · `ext/ai/llm/openai` 结构化 `finish=error`;`agent.step` / `plan` / `run` 收成 `status=error`。金样 `tests/ext/agent-llm-transport-soft.mq.md`。说明:[doc/research/agent-llm-transport-soft-2026-10.md](doc/research/agent-llm-transport-soft-2026-10.md)。
|
||||
- **`agent.run`**:`cache == "soft-hit"` 引号比较(裸 `soft-hit` 曾被解析为减法)。
|
||||
|
||||
### Changed
|
||||
|
||||
- **`ext/ai` 去 json 胶水**:结果 map / 事件用 GFM 表 + `table.put` / `table.append` / `table.empty_*`。SKILL 正面示例:`.cursor/skills/marqdo-dev` §2 · `marqdo/examples.md` §6e。
|
||||
- CLI / Ext / wasm SemVer **1.4.4**。
|
||||
|
||||
## Ext pack v1.4.3 — 2026-10-16
|
||||
|
||||
### Highlights
|
||||
|
||||
**Agent cold EFI / resolve 硬崩修复(CLI 仍为 v1.4.2)**:qdagent 接通 `agent.run` 时空 OKF 触发 `unknown function preflight` 与 `missing map key llm_free`。本包修复 `ext/ai/agent` facade 与 `agent` native;金样 `tests/ext/agent-run-cold-efi.mq.md`。说明:[`doc/research/agent-run-efi-resolve-bugs-2026-10.md`](doc/research/agent-run-efi-resolve-bugs-2026-10.md)。
|
||||
|
||||
```bash
|
||||
marqdo ext add agent # CDN latest → 1.4.3
|
||||
# 或:MARQDO_EXT_VERSION=1.4.3 marqdo ext add agent
|
||||
```
|
||||
|
||||
**核心表面 Δ(Core surface Δ)**:**无增删**——仅 Extension / Agent Runtime 契约修补。
|
||||
|
||||
### Fixed
|
||||
- **`agent.run`**:`efi=True` 改为 `` `self`.preflight ``(不再裸调全局 `preflight`)。
|
||||
- **`agent_resolve` miss**:返回字段对齐命中路径,含 `llm_free: false`;facade 对 `llm_free` / route 字段改用 `json.get`。
|
||||
|
||||
### Changed
|
||||
- 扩展包 SemVer **1.4.3**(`ext/VERSION`);与 CLI **v1.4.2** 可独立安装。
|
||||
|
||||
## v1.4.2 — 2026-10-16
|
||||
|
||||
### Highlights
|
||||
|
||||
Generated
+2
-2
@@ -572,7 +572,7 @@ checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
|
||||
|
||||
[[package]]
|
||||
name = "marqdo"
|
||||
version = "1.4.1"
|
||||
version = "1.4.5"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"assert_cmd",
|
||||
@@ -603,7 +603,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "marqdo-wasm"
|
||||
version = "1.4.1"
|
||||
version = "1.4.5"
|
||||
dependencies = [
|
||||
"marqdo",
|
||||
"serde_json",
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "marqdo"
|
||||
version = "1.4.2"
|
||||
version = "1.4.5"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
description = "Marqdo — Markdown markers as an interpreted programming language"
|
||||
|
||||
@@ -149,8 +149,11 @@ marqdo catalog public -o .marqdo
|
||||
|
||||
---
|
||||
|
||||
## 现状(v1.4.2)
|
||||
## 现状(v1.4.5)
|
||||
|
||||
- **Streamable HTTP MCP + web invoke 鉴权(v1.4.5)**:`agent_mcp_serve transport=http` · Bearer/`X-Api-Key` 注入 · `unauthorized`→401 · openai/agent `json.get` 硬化
|
||||
- **LLM 传输软失败 + `ext/ai` 表优先(v1.4.4)**:上游不可达 → `status=error`(不 abort);`agent`/`llm` 去 `json.set` 胶水 · [agent-llm-transport-soft-2026-10.md](doc/research/agent-llm-transport-soft-2026-10.md)
|
||||
- **Ext pack 1.4.3**:修复 `agent.run` cold EFI(`` `self`.preflight `` + miss resolve `llm_free`);已并入 **≥1.4.4** · [agent-run-efi-resolve-bugs-2026-10.md](doc/research/agent-run-efi-resolve-bugs-2026-10.md)
|
||||
- **Agent/LLM 优化 O1–O5(v1.4.2)**:`ext/ai/sandbox` CoW · `agent.trial` / `sandbox=` · promote sandbox proof · `step_route` / EFI / cost · style lint · quarantine · [ext-ai-optimize.md](doc/roadmap/ext-ai-optimize.md)
|
||||
- **view 标注连线(v1.4.1)**:underline→chip SVG mid→mid(底边中点→轨道→短竖线→顶边中点)· 帧 chip 共用 id · L→R 密度 · [view-prose-annot-gaps.md](doc/design/view-prose-annot-gaps.md)
|
||||
- **叙述统一 / 公式 meta / 图片自写回(v1.4.0)**:`% marqdo: var=` 块公式绑定 · 臂/循环体叙述与 foreach 中英糖 · `` 图片自写回 · view 推断形参实值 · [fence-meta-and-control-prose.md](doc/design/fence-meta-and-control-prose.md) · [stdlib-writeback.md](doc/design/stdlib-writeback.md)
|
||||
@@ -171,7 +174,7 @@ marqdo catalog public -o .marqdo
|
||||
- **`web`**:**Artifact 模型(ADR [0007](doc/adr/0007-web-document-endpoint.md))** — `type: web|endpoint` Document/Endpoint;facade `page`/`route`/`serve`/`render`/`inspect`/`use`;Resource 在 `ext/data` · `ext/security` · `ext/net`;设计 [ext-web-artifact.md](doc/design/ext-web-artifact.md);原生 **Go `libweb`**([ADR 0004](doc/adr/0004-web-plugin-go.md));示例 [marqdo-blog](examples/marqdo-blog/) · [web-client-site](examples/web-client-site/) · [anlian-mq](examples/anlian-mq/)
|
||||
- **`quantum`**:Q0–Q7 + Q8a/b 主题 SVG;示例 [quantum-entanglement](examples/quantum-entanglement/)
|
||||
- **`linalg`**:L0–L6 + 公式文档面(中缀 / `declare` / 展示化简);示例 [linalg-transpose](examples/linalg-transpose/) · [linalg-svd](examples/linalg-svd/) · [linalg-least-squares](examples/linalg-least-squares/)
|
||||
- **`agent`**:A1–A4 + **MCP Server/Client stdio** + Skill Compilation + Adaptive Routing + resume + **Engineering preflight**(`agent.preflight` / `eng_preflight`);宿主缺口见 [ext-hosting.md](doc/roadmap/ext-hosting.md) · [agent-framework-gaps-after-a4.md](doc/research/agent-framework-gaps-after-a4.md) · [engineering-knowledge.md](doc/design/engineering-knowledge.md)
|
||||
- **`agent`**:A1–A4 + **MCP Server/Client**(stdio + **Streamable HTTP**)+ Skill Compilation + Adaptive Routing + resume + **Engineering preflight**(`agent.preflight` / `eng_preflight`);宿主缺口见 [ext-hosting.md](doc/roadmap/ext-hosting.md) · [agent-framework-gaps-after-a4.md](doc/research/agent-framework-gaps-after-a4.md) · [engineering-knowledge.md](doc/design/engineering-knowledge.md)
|
||||
- **`llm`**:语义 `ask`/`stream`/`collect`(OpenAI/Ollama;可选原生 `plugins/llm`)
|
||||
- 安装:`marqdo ext list` / `add …` / `remove`([ext-cli.md](doc/design/ext-cli.md);默认 `~/.marqdo/ext`)。原生插件先 `cargo build -p marqdo_plugin_*` 再 `ext add`
|
||||
- **原生插件 ABI**:[`include/marqdo_abi.h`](include/marqdo_abi.h) · [ext-abi.md](doc/design/ext-abi.md);`plugins/{demo,agent,web,quantum,linalg,llm}`(`web` 为 Go;其余为 Rust)
|
||||
@@ -180,7 +183,7 @@ marqdo catalog public -o .marqdo
|
||||
- **浏览器 Marqdo(WASM)**:`marqdo wasm build` + 官方 bridge 自启(作者零业务 JS;桥内可含列表/路由/storage/ws/文件/Canvas/音频/Observer/拖放);`lib/browser` + GFM;`web.client_embed`;示例 [browser-hello](examples/browser-hello/) · [browser-app](examples/browser-app/) · [browser-media](examples/browser-media/) · [web-client-site](examples/web-client-site/)([ADR 0002](doc/adr/0002-browser-marqdo-wasm.md) · [D](doc/roadmap/browser-wasm-d.md) · [E](doc/roadmap/browser-wasm-e.md) · [F](doc/roadmap/browser-wasm-f.md))
|
||||
- 选型:[ADR 0001 — Rust](doc/adr/0001-implementation-language.md) · [ADR 0002 — 浏览器 WASM](doc/adr/0002-browser-marqdo-wasm.md)(C0–C5 完结,见 [roadmap/browser-wasm.md](doc/roadmap/browser-wasm.md))· [ADR 0003 — 异步效应](doc/adr/0003-browser-async-effects.md)
|
||||
|
||||
### 如何使用最新 Marqdo(v1.4.2)
|
||||
### 如何使用最新 Marqdo(v1.4.5)
|
||||
|
||||
**安装解释器(任选其一)**
|
||||
|
||||
@@ -195,7 +198,7 @@ marqdo catalog public -o .marqdo
|
||||
```bash
|
||||
# 1) 源码安装(跟 tag 或 main)
|
||||
git clone https://github.com/cflmy/marqdo.git && cd marqdo
|
||||
git checkout v1.4.2 # 或留在 main
|
||||
git checkout v1.4.5 # 或留在 main
|
||||
cargo build --release
|
||||
export PATH="$PWD/target/release:$PATH"
|
||||
marqdo version
|
||||
@@ -234,7 +237,7 @@ marqdo wasm build
|
||||
# → dist/wasm/ … 见 examples/browser-hello/
|
||||
|
||||
# 6) Releases:Windows exe/zip/vsix;Linux CLI zip + native `.so` zip
|
||||
# https://github.com/cflmy/marqdo/releases/tag/v1.4.2
|
||||
# https://github.com/cflmy/marqdo/releases/tag/v1.4.5
|
||||
```
|
||||
|
||||
开发期也可用 `cargo run -- …` 代替已安装的 `marqdo`:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "marqdo-wasm"
|
||||
version = "1.4.2"
|
||||
version = "1.4.5"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
description = "Marqdo interpreter as WebAssembly (browser host bridge)"
|
||||
|
||||
Vendored
+15
@@ -1,3 +1,18 @@
|
||||
marqdo (1.4.5-1ppa1) noble; urgency=medium
|
||||
|
||||
* Marqdo 1.4.5: Streamable HTTP MCP (agent_mcp_serve transport=http);
|
||||
web invoke Bearer/X-Api-Key inject + unauthorized→401; openai/agent
|
||||
field access via json.get (no footnote hard-fail).
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Fri, 16 Oct 2026 17:40:00 +0800
|
||||
|
||||
marqdo (1.4.4-1ppa1) noble; urgency=medium
|
||||
|
||||
* Marqdo 1.4.4: Soft LLM transport errors (structured status=error);
|
||||
ext/ai table-first refactor (no json.set glue).
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Fri, 16 Oct 2026 14:40:00 +0800
|
||||
|
||||
marqdo (1.4.2-1ppa1) noble; urgency=medium
|
||||
|
||||
* Marqdo 1.4.2: Agent/LLM optimize O1–O5 — sandbox trial opt-in,
|
||||
|
||||
@@ -120,8 +120,8 @@ import data:ext/data/db.mq.md
|
||||
| `path` | `路径` | URL |
|
||||
| `method` | `方法` | GET/POST/… |
|
||||
| `request` | `请求` | `json` / `form` / `none` |
|
||||
| `response` | `响应` | `json` / `text` / `html` |
|
||||
| `auth` | `鉴权` | 角色名或 `none` |
|
||||
| `response` | `响应` | `json` / `text` / `html` / `sse`(`text/event-stream`) |
|
||||
| `auth` | `鉴权` | 角色名或 `none`;`bearer` / `api_key` = 不要求 cookie,把 `Authorization` / `X-Api-Key` 注入 payload 由 `# main` 校验 |
|
||||
|
||||
正文在请求时执行;返回值按 `response` 序列化。Result 协议见 §8。
|
||||
|
||||
|
||||
@@ -0,0 +1,58 @@
|
||||
# 缺陷记录:LLM 传输失败硬中断 Agent Runtime
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Fixed(2026-10-16)** — 见 §3;金样 `tests/ext/agent-llm-transport-soft.mq.md` |
|
||||
| 日期 | 2026-10-16 |
|
||||
| 发现方 | qdagent(接通 `agent.run` live 路径时) |
|
||||
| 影响面 | `host/net` · `plugins/llm` · `ext/ai/llm` · `ext/ai/llm/openai` · `ext/ai/agent` |
|
||||
| 相关 | [agent-run-efi-resolve-bugs-2026-10.md](agent-run-efi-resolve-bugs-2026-10.md) · [ext-agent.md](../design/ext-agent.md) |
|
||||
|
||||
---
|
||||
|
||||
## 0. 一句话
|
||||
|
||||
上游 LLM **连接拒绝 / 超时 / 非 200** 时,Runtime 曾以 **硬错误** 终止整个 `marqdo run`;产品(qdagent ASK/BUILD)无法落到结构化 `status=error`。
|
||||
|
||||
---
|
||||
|
||||
## 1. 复现
|
||||
|
||||
```bash
|
||||
OPENAI_API_KEY=offline OPENAI_BASE_URL=http://127.0.0.1:9 \
|
||||
marqdo run tests/ext/agent-llm-transport-soft.mq.md
|
||||
# 修复前:exit≠0,error: http POST …
|
||||
# 修复后:exit 0,ask/step/run 均带 finish|status=error + error 文本
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 2. 根因
|
||||
|
||||
| 层 | 行为 |
|
||||
|----|------|
|
||||
| `host_http_post` / SSE | 传输失败 `Err(...)` → VM abort |
|
||||
| `plugins/llm` `llm_chat_completions` | 同上 `Err` |
|
||||
| `ext/ai/llm/openai` | HTTP≠200 → `sys.exit 1` |
|
||||
| `agent.model_turn` / `step` / `plan` | 无软失败臂 |
|
||||
|
||||
---
|
||||
|
||||
## 3. 修复清单
|
||||
|
||||
| # | 改动 | 文件 |
|
||||
|---|------|------|
|
||||
| 1 | 传输失败 → `{status:0, body}` / SSE error event | `src/host/net.rs` |
|
||||
| 2 | 插件返回 `{finish:error, error, …}` 而非 `Err` | `plugins/llm/src/lib.rs` |
|
||||
| 3 | openai 非 200 → 结构化 pack,禁止 `sys.exit` | `ext/ai/llm/openai.mq.md` |
|
||||
| 4 | `llm.ask` 透传 `error` / `finish` | `ext/ai/llm.mq.md` |
|
||||
| 5 | `model_turn` / `step` / `plan` 收成 `status=error` | `ext/ai/agent.mq.md` |
|
||||
| 6 | 金样 | `tests/ext/agent-llm-transport-soft.mq.md` |
|
||||
|
||||
**契约**:Agent Runtime 对 LLM 传输失败必须可捕获;`status=error` + `error` 字符串;进程退出码 0(程序正常返回错误图)。
|
||||
|
||||
---
|
||||
|
||||
## 4. 对 qdagent
|
||||
|
||||
`ensure_llm` 有 key 但上游不可达时,`ask`/`build` 可走既有 `mode=error` 臂写 workbook,不再整 CLI 崩。
|
||||
@@ -0,0 +1,117 @@
|
||||
# 缺陷记录:`agent.run` EFI / resolve 在消费方崩溃(qdagent 复现)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Fixed(2026-10-16)** — 见 §3;金样 `tests/ext/agent-run-cold-efi.mq.md` |
|
||||
| 日期 | 2026-10-16 |
|
||||
| 发现方 | qdagent(`~/work/qdagent`)接通 Marqdo ≥1.4.2 Agent EFI 全路径时 |
|
||||
| 影响面 | `ext/ai/agent` · `## run` · `plugins/agent` `agent_resolve` |
|
||||
| 相关 | [ext-agent.md](../design/ext-agent.md) · [engineering-knowledge.md](../design/engineering-knowledge.md) · [ai-agent-llm-optimize-2026-10.md](ai-agent-llm-optimize-2026-10.md) |
|
||||
|
||||
---
|
||||
|
||||
## 0. 一句话
|
||||
|
||||
`agent.run` 在 **空 OKF / 无 skill 命中** 或 **`efi=True`** 时,会以硬错误退出,而不是降级到 `plan` 探索。
|
||||
这是 **Agent Runtime 契约不完整**,不是 qdagent 业务写错。
|
||||
|
||||
---
|
||||
|
||||
## 1. 复现(qdagent)
|
||||
|
||||
环境:工作区 Marqdo `1.4.2` + `MARQDO_EXT=~/work/marqdo/ext`;qdagent 调用:
|
||||
|
||||
```text
|
||||
助手 = > agent.agent …
|
||||
`助手`.run task=… efi=True # 或 efi=False 后仍进 resolve
|
||||
```
|
||||
|
||||
本仓几乎没有 `.marqdo/agent-kb` 编译 skill。
|
||||
|
||||
### Bug A — `efi=True` → `unknown function preflight`
|
||||
|
||||
**位置**:[`ext/ai/agent.mq.md`](../../ext/ai/agent.mq.md) `## run`
|
||||
|
||||
```markdown
|
||||
1. `efi`
|
||||
**efi_pf = preflight task=`task`**
|
||||
```
|
||||
|
||||
同文件其它同对象方法一律写 `` `self`.route `` / `` `self`.resolve ``。
|
||||
裸标识 `preflight` 在导入消费方(如 `cli/求道-构建.mq.md`)里被解析成**全局函数**,找不到 → 硬错。
|
||||
|
||||
**期望**:`` **efi_pf = > `self`.preflight task=`task`** ``(或等价 method call)。
|
||||
|
||||
### Bug B — miss resolve → `missing map key llm_free`
|
||||
|
||||
**位置**:
|
||||
|
||||
1. Plugin:[`plugins/agent/src/memory.rs`](../../plugins/agent/src/memory.rs) `resolve_task`
|
||||
当 `kb_lookup` 返回 null:
|
||||
|
||||
```rust
|
||||
Ok(json!({
|
||||
"mode": "unknown",
|
||||
"skill": null,
|
||||
"resource": null,
|
||||
"confidence": 0.0,
|
||||
// 缺 llm_free
|
||||
}))
|
||||
```
|
||||
|
||||
2. Facade:`## run` 在 resolve 之后:
|
||||
|
||||
```markdown
|
||||
**llm_free = [llm_free](resolved)**
|
||||
```
|
||||
|
||||
脚注式取键在缺键时 **硬失败**,无法落到后面的 `plan` 探索臂。
|
||||
|
||||
**期望**:
|
||||
|
||||
- miss 结果始终带 `"llm_free": false`(与命中路径字段集对齐)
|
||||
- facade 侧对可选键用 `json.get`(缺省 `False`),防御其它调用方
|
||||
|
||||
---
|
||||
|
||||
## 2. 为何金样没拦住
|
||||
|
||||
| 金样 | 为何漏网 |
|
||||
|------|----------|
|
||||
| `tests/ext/agent-zero-llm.mq.md` | 先编译 skill / policy,再 `run`;resolve **命中** |
|
||||
| `tests/ext/eng-preflight.mq.md` | 只测 `agent_eng_preflight`,**不**走 `agent.run efi=` |
|
||||
| 多数 `agent-*-live` | 自带 kb 或 `efi=False` / 不经过 cold miss |
|
||||
|
||||
qdagent 是第一个「冷库 + 产品 EFI + `run`」消费方,因此打穿。
|
||||
|
||||
---
|
||||
|
||||
## 3. 修复清单(本轮)
|
||||
|
||||
| # | 改动 | 文件 |
|
||||
|---|------|------|
|
||||
| 1 | miss resolve 补 `llm_free: false` | `plugins/agent/src/memory.rs` |
|
||||
| 2 | `` `self`.preflight ``;`llm_free`/`mode` 用 `json.get` | `ext/ai/agent.mq.md` |
|
||||
| 3 | 回归金样:冷 kb + `run efi=True` 不崩溃,落到 explore/plan | `tests/ext/agent-run-cold-efi.mq.md` |
|
||||
| 4 | 重建 `marqdo_plugin_agent` 并跑金样 | `cargo build -p marqdo_plugin_agent --release` |
|
||||
|
||||
---
|
||||
|
||||
## 4. 对 qdagent 的含义
|
||||
|
||||
修复后 qdagent 可恢复:
|
||||
|
||||
```text
|
||||
product preflight → latch → agent.run (efi=True|False) / plan
|
||||
```
|
||||
|
||||
而不必长期绕开 `agent.run`。
|
||||
在插件未装入新 so 前,qdagent 侧 EFI latch + `step`/`plan` 仍是合法降级。
|
||||
|
||||
---
|
||||
|
||||
## 5. 非目标
|
||||
|
||||
- 不改 EKC / `agent_eng_preflight` 语义
|
||||
- 不引入第二套 Agent loop
|
||||
- 不把向量检索升为权威真相
|
||||
+1
-1
@@ -1 +1 @@
|
||||
1.4.2
|
||||
1.4.5
|
||||
+457
-324
File diff suppressed because it is too large.
Load diff
+42
-15
@@ -5,6 +5,7 @@ description: >-
|
||||
OpenAI-compatible and Ollama HTTP stay behind the semantic surface.
|
||||
import sys:lib/sys.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import table:lib/table.mq.md
|
||||
import fs:lib/fs.mq.md
|
||||
import text:lib/text.mq.md
|
||||
import openai:ext/ai/llm/openai.mq.md
|
||||
@@ -25,14 +26,14 @@ Collapse a stream event list to final answer text (`done.result` or `error.messa
|
||||
**answer = ""**
|
||||
|
||||
- [ev](events)
|
||||
**t = [type](ev)**
|
||||
**t = > json.get value=`ev` key="type"**
|
||||
1. `t` == "done"
|
||||
**answer = [result](ev)**
|
||||
**answer = > json.get value=`ev` key="result"**
|
||||
2. `t` == "error"
|
||||
**answer = [message](ev)**
|
||||
**answer = > json.get value=`ev` key="message"**
|
||||
3. `t` == "delta"
|
||||
4. `t` == "text"
|
||||
**chunk = [text](ev)**
|
||||
**chunk = > json.get value=`ev` key="text"**
|
||||
1. `chunk`
|
||||
**answer = answer + chunk**
|
||||
2. *
|
||||
@@ -167,7 +168,8 @@ Module convenience: create a default handle and return **answer text**. Pass `pa
|
||||
|
||||
**m = > create model=`model` base_url=`base_url` api_key=`api_key`**
|
||||
**r = > m.ask prompt=`p`**
|
||||
*[text](r)*
|
||||
**_ret = > json.get value=`r` key="text"**
|
||||
*_ret*
|
||||
|
||||
# llm
|
||||
+ `model`=None
|
||||
@@ -283,9 +285,21 @@ Semantic ask → **LLMResult** (`text`, `model`, `finish`, `backend`, `usage`, `
|
||||
**text = > json.get value=`pack` key="text"**
|
||||
**usage = > json.get value=`pack` key="usage"**
|
||||
**finish = > json.get value=`pack` key="finish"**
|
||||
**err = > json.get value=`pack` key="error"**
|
||||
**tc = > json.get value=`pack` key="tool_calls"**
|
||||
1. not `tc`
|
||||
**tc = > json.parse text=[]**
|
||||
**tc = > table.empty_list**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. not `text`
|
||||
**text = ""**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. not `finish`
|
||||
1. `err`
|
||||
**finish = "error"**
|
||||
2. *
|
||||
**finish = "stop"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
@@ -293,16 +307,27 @@ Hoist receiver fields before the result table — table cells can shadow `self`
|
||||
column names (`model`, `usage`), so `[model](self)` / `[total_tokens](usage)`
|
||||
inside the table is unsafe.
|
||||
|
||||
**mdl = [model](self)**
|
||||
**b = [backend](self)**
|
||||
**nm = [name](self)**
|
||||
**tt = [total_tokens](usage)**
|
||||
**mdl = > json.get value=`self` key="model"**
|
||||
**b = > json.get value=`self` key="backend"**
|
||||
**nm = > json.get value=`self` key="name"**
|
||||
1. `usage`
|
||||
**tt = > json.get value=`usage` key="total_tokens"**
|
||||
2. *
|
||||
**tt = 0**
|
||||
1. not `tt`
|
||||
**tt = 0**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. `err`
|
||||
**calls = 0**
|
||||
2. *
|
||||
**calls = 1**
|
||||
|
||||
`result` =
|
||||
|
||||
| text | model | finish | backend | usage | name | llm_calls | tokens | tool_calls |
|
||||
|------|-------|--------|---------|-------|------|-----------|--------|------------|
|
||||
| `text` | `mdl` | `finish` | `b` | `usage` | `nm` | 1 | `tt` | `tc` |
|
||||
| text | model | finish | backend | usage | name | llm_calls | tokens | tool_calls | error |
|
||||
|------|-------|--------|---------|-------|------|-----------|--------|------------|-------|
|
||||
| `text` | `mdl` | `finish` | `b` | `usage` | `nm` | `calls` | `tt` | `tc` | `err` |
|
||||
|
||||
*result*
|
||||
|
||||
@@ -345,10 +370,12 @@ Internal / compatibility primitive. Prefer `ask` / `stream` in new code.
|
||||
|
||||
1. `stream`
|
||||
**pack = > self.complete_result prompt=`prompt` stream=True echo=`echo`**
|
||||
*[events](pack)*
|
||||
**_ret = > json.get value=`pack` key="events"**
|
||||
*_ret*
|
||||
2. *
|
||||
**pack = > self.complete_result prompt=`prompt` stream=False echo=`echo`**
|
||||
*[text](pack)*
|
||||
**_ret = > json.get value=`pack` key="text"**
|
||||
*_ret*
|
||||
|
||||
## chat
|
||||
+ `prompt`
|
||||
|
||||
+65
-26
@@ -4,6 +4,7 @@ description: >-
|
||||
OpenAI-compatible chat completions transport (HTTP + SSE).
|
||||
Prefers optional plugins/llm ABI when present; falls back to lib/net.
|
||||
Used by ext/ai/llm; authors should prefer llm.ask / llm.stream.
|
||||
Result packs are GFM tables / table.put (code-as-documentation).
|
||||
import net:lib/net.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import table:lib/table.mq.md
|
||||
@@ -53,21 +54,18 @@ Extract OpenAI-shaped `choices[0].message.tool_calls` as a list (or empty list).
|
||||
|
||||
**choices = > json.get value=`data` key="choices"**
|
||||
1. not `choices`
|
||||
**empty = > json.parse text=[]**
|
||||
*empty*
|
||||
*> table.empty_list*
|
||||
2. *
|
||||
**c0 = [1](choices)**
|
||||
**msg = > json.get value=`c0` key="message"**
|
||||
1. not `msg`
|
||||
**empty = > json.parse text=[]**
|
||||
*empty*
|
||||
*> table.empty_list*
|
||||
2. *
|
||||
**tc = > json.get value=`msg` key="tool_calls"**
|
||||
1. `tc`
|
||||
*tc*
|
||||
2. *
|
||||
**empty = > json.parse text=[]**
|
||||
*empty*
|
||||
*> table.empty_list*
|
||||
|
||||
## chat_completions
|
||||
+ `base_url`
|
||||
@@ -79,7 +77,7 @@ Extract OpenAI-shaped `choices[0].message.tool_calls` as a list (or empty list).
|
||||
+ `suffix`=/chat/completions
|
||||
+ `bearer`="Bearer "
|
||||
|
||||
POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_completions`) when the native plugin is resolvable; otherwise `lib/net` HTTP/SSE. Non-stream → `{text,usage,finish,tool_calls}`; stream → `{events,finish}`.
|
||||
POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_completions`) when the native plugin is resolvable; otherwise `lib/net` HTTP/SSE. Non-stream → `{text,usage,finish,tool_calls}`; stream → `{events,finish}`. Soft transport / HTTP failures return `finish=error` packs (never abort the VM).
|
||||
|
||||
**p = > plugin.native_path name="llm"**
|
||||
1. `p`
|
||||
@@ -106,40 +104,81 @@ POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_c
|
||||
**req = > table.put in=`req` at="stream" value=True**
|
||||
**body = > json.stringify value=`req`**
|
||||
**resp = > net.http_post_sse url=`url` body=`body` headers=`headers` echo=`echo`**
|
||||
1. [status](resp) == 200
|
||||
**pack = > json.parse text={"finish":"stop"}**
|
||||
**pack = > json.set map=`pack` key="events" value=[events](resp)**
|
||||
**st = > json.get value=`resp` key="status"**
|
||||
1. `st` == 200
|
||||
**evs = > json.get value=`resp` key="events"**
|
||||
|
||||
`pack` =
|
||||
|
||||
| finish | events |
|
||||
|--------|--------|
|
||||
| stop | `evs` |
|
||||
|
||||
*pack*
|
||||
2. *
|
||||
> print text=ext/ai/llm/openai: HTTP error (stream)
|
||||
> print text=[status](resp)
|
||||
> sys.exit code=1
|
||||
Soft transport / HTTP failure — structured pack, never abort the VM.
|
||||
**msg = "HTTP " + `st`**
|
||||
**evs = > json.get value=`resp` key="events"**
|
||||
1. `evs`
|
||||
**e0 = > at value=`evs` index=0**
|
||||
**m2 = > json.get value=`e0` key="message"**
|
||||
1. `m2`
|
||||
**msg = `m2`**
|
||||
`pack` =
|
||||
|
||||
| finish | text | error | events |
|
||||
|--------|------|-------|--------|
|
||||
| error | "" | `msg` | `evs` |
|
||||
|
||||
*pack*
|
||||
2. *
|
||||
**body = > json.stringify value=`req`**
|
||||
**resp = > net.http_post url=`url` body=`body` headers=`headers`**
|
||||
1. [status](resp) == 200
|
||||
**data = > json.parse text=[body](resp)**
|
||||
**msg = [message]([1]([choices](data)))**
|
||||
**st = > json.get value=`resp` key="status"**
|
||||
1. `st` == 200
|
||||
**raw = > json.get value=`resp` key="body"**
|
||||
**data = > json.parse text=`raw`**
|
||||
**choices = > json.get value=`data` key="choices"**
|
||||
**choice0 = > at value=`choices` index=0**
|
||||
**msg = > json.get value=`choice0` key="message"**
|
||||
**text = > json.get value=`msg` key="content"**
|
||||
1. not `text`
|
||||
**text = ""**
|
||||
2. *
|
||||
**_ = 1**
|
||||
**usage = > usage_from data=`data`**
|
||||
**fr = > json.get value=[1]([choices](data)) key="finish_reason"**
|
||||
**fr = > json.get value=`choice0` key="finish_reason"**
|
||||
1. not `fr`
|
||||
**fr = "stop"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
**tc = > tool_calls_from data=`data`**
|
||||
**pack = > json.parse text={"finish":"stop"}**
|
||||
**pack = > json.set map=`pack` key="text" value=`text`**
|
||||
**pack = > json.set map=`pack` key="usage" value=`usage`**
|
||||
**pack = > json.set map=`pack` key="finish" value=`fr`**
|
||||
**pack = > json.set map=`pack` key="tool_calls" value=`tc`**
|
||||
|
||||
`pack` =
|
||||
|
||||
| finish | text | usage | tool_calls |
|
||||
|--------|------|-------|------------|
|
||||
| `fr` | `text` | `usage` | `tc` |
|
||||
|
||||
*pack*
|
||||
2. *
|
||||
> print text=ext/ai/llm/openai: HTTP error
|
||||
> print text=[status](resp)
|
||||
> print text=[body](resp)
|
||||
> sys.exit code=1
|
||||
Soft transport / HTTP failure — structured pack, never abort the VM.
|
||||
**raw = > json.get value=`resp` key="body"**
|
||||
**msg = "HTTP " + `st`**
|
||||
1. `raw`
|
||||
**msg = `msg` + ": " + `raw`**
|
||||
`usage0` =
|
||||
|
||||
| prompt_tokens | completion_tokens | total_tokens |
|
||||
|---------------|-------------------|--------------|
|
||||
| 0 | 0 | 0 |
|
||||
|
||||
**empty_tc = > table.empty_list**
|
||||
|
||||
`pack` =
|
||||
|
||||
| finish | text | error | usage | tool_calls |
|
||||
|--------|------|-------|-------|------------|
|
||||
| error | "" | `msg` | `usage0` | `empty_tc` |
|
||||
|
||||
*pack*
|
||||
+28
-18
@@ -88,13 +88,14 @@ import json:lib/json.mq.md
|
||||
> 系统.退出 码=1
|
||||
|
||||
**id = > agent_alloc**
|
||||
**h = > json.parse text={"_type":"智能体"}**
|
||||
**h = > json.set map=`h` key="id" value=`id`**
|
||||
**h = > json.set map=`h` key="model" value=`大模型`**
|
||||
**h = > json.set map=`h` key="tools" value=`工具`**
|
||||
`h` =
|
||||
|
||||
| _type | id | model | tools |
|
||||
|-------|-----|-------|-------|
|
||||
| 智能体 | `id` | `大模型` | `工具` |
|
||||
|
||||
1. `站立提示`
|
||||
**h = > json.set map=`h` key="standing" value=`站立提示`**
|
||||
**h = table.put in=`h` at="standing" value=`站立提示`**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
@@ -138,8 +139,12 @@ O1 可选沙盒试跑。转发 `agent.trial`(表风格返回;宿主树在成
|
||||
**model = > json.get value=`自` key="model"**
|
||||
**tools = > json.get value=`自` key="tools"**
|
||||
|
||||
**user_turn = > json.parse text={"role":"user"}**
|
||||
**user_turn = > json.set map=`user_turn` key="content" value=`任务`**
|
||||
`user_turn` =
|
||||
|
||||
| role | content |
|
||||
|------|---------|
|
||||
| user | `任务` |
|
||||
|
||||
> agent_history_append id=`id` item=`user_turn`
|
||||
|
||||
1. `流式`
|
||||
@@ -151,9 +156,11 @@ O1 可选沙盒试跑。转发 `agent.trial`(表风格返回;宿主树在成
|
||||
**reply = > trim value=`reply`**
|
||||
**decision = reply**
|
||||
**tool_name = > 提取工具名 回复=`reply`**
|
||||
**out = > json.parse text={"status":"ok"}**
|
||||
**out = > json.set map=`out` key="task" value=`任务`**
|
||||
**out = > json.set map=`out` key="decision" value=`decision`**
|
||||
`out` =
|
||||
|
||||
| status | task | decision |
|
||||
|--------|------|----------|
|
||||
| ok | `任务` | `decision` |
|
||||
|
||||
1. `tool_name`
|
||||
**tool_out = > 运行工具 工具=`tools` 名=`tool_name`**
|
||||
@@ -161,8 +168,8 @@ O1 可选沙盒试跑。转发 `agent.trial`(表风格返回;宿主树在成
|
||||
**deny = > split value=`tool_s` sep="Tool not allowed"**
|
||||
**deny_n = > len value=`deny`**
|
||||
1. `deny_n` > 1
|
||||
**out = > json.set map=`out` key="status" value="error"**
|
||||
**out = > json.set map=`out` key="error" value=`tool_s`**
|
||||
**out = table.put in=`out` at="status" value="error"**
|
||||
**out = table.put in=`out` at="error" value=`tool_s`**
|
||||
**reply = tool_s**
|
||||
2. *
|
||||
**task_s = > json.stringify value=`任务`**
|
||||
@@ -178,14 +185,17 @@ O1 可选沙盒试跑。转发 `agent.trial`(表风格返回;宿主树在成
|
||||
**reply = > 大模型.流式结果 事件=`evs2`**
|
||||
2. *
|
||||
**reply = > model.运行 提示=`fp`**
|
||||
**out = > json.set map=`out` key="tool" value=`tool_name`**
|
||||
**out = > json.set map=`out` key="tool_result" value=`tool_s`**
|
||||
**out = > json.set map=`out` key="result" value=`reply`**
|
||||
**out = table.put in=`out` at="tool" value=`tool_name`**
|
||||
**out = table.put in=`out` at="tool_result" value=`tool_s`**
|
||||
**out = table.put in=`out` at="result" value=`reply`**
|
||||
2. *
|
||||
**out = > json.set map=`out` key="result" value=`reply`**
|
||||
**out = table.put in=`out` at="result" value=`reply`**
|
||||
|
||||
**as_turn = > json.parse text={"role":"assistant"}**
|
||||
**as_turn = > json.set map=`as_turn` key="content" value=`reply`**
|
||||
`as_turn` =
|
||||
|
||||
| role | content |
|
||||
|------|---------|
|
||||
| assistant | `reply` |
|
||||
> agent_history_append id=`id` item=`as_turn`
|
||||
|
||||
1. `沙盒`
|
||||
|
||||
+336
-41
@@ -1,9 +1,14 @@
|
||||
//! Minimal MCP Server (stdio JSON-RPC) — tools map to `lib.member` via host `call_lib_path`.
|
||||
//! Minimal MCP Server — stdio JSON-RPC + Streamable HTTP (spec 2025-03-26).
|
||||
|
||||
use serde_json::{json, Map, Value};
|
||||
use std::ffi::CStr;
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::io::{BufRead, BufReader, Read, Write};
|
||||
use std::net::{TcpListener, TcpStream};
|
||||
use std::os::raw::{c_char, c_int};
|
||||
use std::sync::atomic::{AtomicU64, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use std::time::{SystemTime, UNIX_EPOCH};
|
||||
|
||||
use crate::{host_query_json_args, parse_args, set_err, set_out};
|
||||
|
||||
@@ -98,6 +103,40 @@ fn call_tool(tools: &[Value], name: &str, arguments: &Value) -> Result<Value, St
|
||||
}))
|
||||
}
|
||||
|
||||
/// Handle one JSON-RPC message. Returns `None` for notifications (no response body).
|
||||
fn dispatch_rpc(name: &str, tools: &[Value], msg: &Value) -> Option<Value> {
|
||||
let id = msg.get("id").cloned().unwrap_or(Value::Null);
|
||||
let method = msg.get("method").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let params = msg.get("params").cloned().unwrap_or(json!({}));
|
||||
|
||||
if matches!(id, Value::Null) && method.starts_with("notifications/") {
|
||||
return None;
|
||||
}
|
||||
|
||||
Some(match method {
|
||||
"initialize" => rpc_result(
|
||||
&id,
|
||||
json!({
|
||||
"protocolVersion": "2025-03-26",
|
||||
"capabilities": { "tools": {} },
|
||||
"serverInfo": { "name": name, "version": "0.1.0" }
|
||||
}),
|
||||
),
|
||||
"tools/list" | "list_tools" => rpc_result(&id, tools_list(tools)),
|
||||
"tools/call" | "call_tool" => {
|
||||
let tname = params.get("name").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let args = params.get("arguments").cloned().unwrap_or(json!({}));
|
||||
match call_tool(tools, tname, &args) {
|
||||
Ok(r) => rpc_result(&id, r),
|
||||
Err(e) => rpc_error(&id, -32000, &e),
|
||||
}
|
||||
}
|
||||
"ping" => rpc_result(&id, json!({})),
|
||||
"" => rpc_error(&id, -32600, "missing method"),
|
||||
other => rpc_error(&id, -32601, &format!("method not found: {other}")),
|
||||
})
|
||||
}
|
||||
|
||||
/// Blocking stdio MCP loop. `tools` is a JSON array of `{name, fn, description}`.
|
||||
pub fn serve_stdio(name: &str, tools: Vec<Value>) -> Result<Value, String> {
|
||||
let stdin = std::io::stdin();
|
||||
@@ -106,41 +145,274 @@ pub fn serve_stdio(name: &str, tools: Vec<Value>) -> Result<Value, String> {
|
||||
let Some(msg) = read_line_message(&mut reader)? else {
|
||||
break;
|
||||
};
|
||||
let id = msg.get("id").cloned().unwrap_or(Value::Null);
|
||||
let method = msg.get("method").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let params = msg.get("params").cloned().unwrap_or(json!({}));
|
||||
|
||||
if matches!(id, Value::Null) && method.starts_with("notifications/") {
|
||||
continue;
|
||||
if let Some(reply) = dispatch_rpc(name, &tools, &msg) {
|
||||
write_message(&reply)?;
|
||||
}
|
||||
|
||||
let reply_msg = match method {
|
||||
"initialize" => rpc_result(
|
||||
&id,
|
||||
json!({
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": { "tools": {} },
|
||||
"serverInfo": { "name": name, "version": "0.1.0" }
|
||||
}),
|
||||
),
|
||||
"tools/list" | "list_tools" => rpc_result(&id, tools_list(&tools)),
|
||||
"tools/call" | "call_tool" => {
|
||||
let tname = params.get("name").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let args = params.get("arguments").cloned().unwrap_or(json!({}));
|
||||
match call_tool(&tools, tname, &args) {
|
||||
Ok(r) => rpc_result(&id, r),
|
||||
Err(e) => rpc_error(&id, -32000, &e),
|
||||
}
|
||||
}
|
||||
"ping" => rpc_result(&id, json!({})),
|
||||
"" => rpc_error(&id, -32600, "missing method"),
|
||||
other => rpc_error(&id, -32601, &format!("method not found: {other}")),
|
||||
};
|
||||
write_message(&reply_msg)?;
|
||||
}
|
||||
Ok(json!({ "ok": true, "transport": "stdio" }))
|
||||
}
|
||||
|
||||
fn new_session_id() -> String {
|
||||
static SEQ: AtomicU64 = AtomicU64::new(1);
|
||||
let n = SEQ.fetch_add(1, Ordering::Relaxed);
|
||||
let ms = SystemTime::now()
|
||||
.duration_since(UNIX_EPOCH)
|
||||
.map(|d| d.as_millis())
|
||||
.unwrap_or(0);
|
||||
format!("mcp-{ms}-{n}")
|
||||
}
|
||||
|
||||
fn read_http_request(
|
||||
reader: &mut BufReader<TcpStream>,
|
||||
) -> Result<(String, String, Vec<(String, String)>, Vec<u8>), String> {
|
||||
let mut request_line = String::new();
|
||||
reader
|
||||
.read_line(&mut request_line)
|
||||
.map_err(|e| format!("read request: {e}"))?;
|
||||
let parts: Vec<&str> = request_line.trim().split_whitespace().collect();
|
||||
if parts.len() < 2 {
|
||||
return Err("bad request line".into());
|
||||
}
|
||||
let method = parts[0].to_string();
|
||||
let path = parts[1].to_string();
|
||||
let mut headers = Vec::new();
|
||||
let mut content_length = 0usize;
|
||||
loop {
|
||||
let mut line = String::new();
|
||||
reader
|
||||
.read_line(&mut line)
|
||||
.map_err(|e| format!("read header: {e}"))?;
|
||||
let line = line.trim_end_matches(['\r', '\n']);
|
||||
if line.is_empty() {
|
||||
break;
|
||||
}
|
||||
if let Some((k, v)) = line.split_once(':') {
|
||||
let key = k.trim().to_string();
|
||||
let val = v.trim().to_string();
|
||||
if key.eq_ignore_ascii_case("content-length") {
|
||||
content_length = val.parse().unwrap_or(0);
|
||||
}
|
||||
headers.push((key, val));
|
||||
}
|
||||
}
|
||||
let mut body = vec![0u8; content_length];
|
||||
if content_length > 0 {
|
||||
reader
|
||||
.read_exact(&mut body)
|
||||
.map_err(|e| format!("read body: {e}"))?;
|
||||
}
|
||||
Ok((method, path, headers, body))
|
||||
}
|
||||
|
||||
fn header_get<'a>(headers: &'a [(String, String)], name: &str) -> Option<&'a str> {
|
||||
headers
|
||||
.iter()
|
||||
.find(|(k, _)| k.eq_ignore_ascii_case(name))
|
||||
.map(|(_, v)| v.as_str())
|
||||
}
|
||||
|
||||
fn write_http(
|
||||
stream: &mut TcpStream,
|
||||
status: u16,
|
||||
reason: &str,
|
||||
content_type: &str,
|
||||
body: &[u8],
|
||||
extra: &[(&str, &str)],
|
||||
) -> Result<(), String> {
|
||||
let mut head = format!(
|
||||
"HTTP/1.1 {status} {reason}\r\nContent-Type: {content_type}\r\nContent-Length: {}\r\nConnection: close\r\n",
|
||||
body.len()
|
||||
);
|
||||
for (k, v) in extra {
|
||||
head.push_str(k);
|
||||
head.push_str(": ");
|
||||
head.push_str(v);
|
||||
head.push_str("\r\n");
|
||||
}
|
||||
head.push_str("\r\n");
|
||||
stream
|
||||
.write_all(head.as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
stream.write_all(body).map_err(|e| e.to_string())?;
|
||||
stream.flush().map_err(|e| e.to_string())?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn path_matches(req_path: &str, endpoint: &str) -> bool {
|
||||
let base = req_path.split('?').next().unwrap_or(req_path);
|
||||
base == endpoint || base.trim_end_matches('/') == endpoint.trim_end_matches('/')
|
||||
}
|
||||
|
||||
fn handle_http_conn(
|
||||
stream: TcpStream,
|
||||
name: &str,
|
||||
tools: &[Value],
|
||||
endpoint: &str,
|
||||
) -> Result<(), String> {
|
||||
let mut reader = BufReader::new(stream);
|
||||
let (method, path, headers, body) = read_http_request(&mut reader)?;
|
||||
let mut stream = reader.into_inner();
|
||||
if !path_matches(&path, endpoint) {
|
||||
write_http(
|
||||
&mut stream,
|
||||
404,
|
||||
"Not Found",
|
||||
"application/json",
|
||||
br#"{"error":"not_found"}"#,
|
||||
&[],
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if method.eq_ignore_ascii_case("GET") {
|
||||
// Optional SSE stream — empty keepalive then close (clients may 405).
|
||||
let accept = header_get(&headers, "Accept").unwrap_or("");
|
||||
if accept.contains("text/event-stream") {
|
||||
let body = b": mcp streamable-http ready\n\n";
|
||||
write_http(
|
||||
&mut stream,
|
||||
200,
|
||||
"OK",
|
||||
"text/event-stream",
|
||||
body,
|
||||
&[("Cache-Control", "no-cache")],
|
||||
)?;
|
||||
} else {
|
||||
write_http(
|
||||
&mut stream,
|
||||
405,
|
||||
"Method Not Allowed",
|
||||
"application/json",
|
||||
br#"{"error":"use_post_for_jsonrpc"}"#,
|
||||
&[("Allow", "POST, GET")],
|
||||
)?;
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if method.eq_ignore_ascii_case("DELETE") {
|
||||
write_http(&mut stream, 200, "OK", "application/json", b"{}", &[])?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if !method.eq_ignore_ascii_case("POST") {
|
||||
write_http(
|
||||
&mut stream,
|
||||
405,
|
||||
"Method Not Allowed",
|
||||
"application/json",
|
||||
br#"{"error":"method_not_allowed"}"#,
|
||||
&[("Allow", "POST, GET, DELETE")],
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let text = String::from_utf8_lossy(&body);
|
||||
let msg: Value = serde_json::from_str(text.trim()).map_err(|e| format!("json-rpc: {e}"))?;
|
||||
let is_init = msg.get("method").and_then(|v| v.as_str()) == Some("initialize");
|
||||
let existing_sid = header_get(&headers, "Mcp-Session-Id")
|
||||
.or_else(|| header_get(&headers, "MCP-Session-Id"))
|
||||
.map(|s| s.to_string());
|
||||
|
||||
// Notifications → 202 Accepted, empty body.
|
||||
if msg.get("id").is_none() || msg.get("id") == Some(&Value::Null) {
|
||||
if let Some(m) = msg.get("method").and_then(|v| v.as_str()) {
|
||||
if m.starts_with("notifications/") {
|
||||
write_http(
|
||||
&mut stream,
|
||||
202,
|
||||
"Accepted",
|
||||
"application/json",
|
||||
b"",
|
||||
&[],
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let reply = match dispatch_rpc(name, tools, &msg) {
|
||||
Some(r) => r,
|
||||
None => {
|
||||
write_http(
|
||||
&mut stream,
|
||||
202,
|
||||
"Accepted",
|
||||
"application/json",
|
||||
b"",
|
||||
&[],
|
||||
)?;
|
||||
return Ok(());
|
||||
}
|
||||
};
|
||||
|
||||
let body = serde_json::to_vec(&reply).map_err(|e| e.to_string())?;
|
||||
let mut extras: Vec<(&str, String)> = Vec::new();
|
||||
if is_init {
|
||||
let sid = existing_sid.unwrap_or_else(new_session_id);
|
||||
extras.push(("Mcp-Session-Id", sid));
|
||||
} else if let Some(sid) = existing_sid {
|
||||
extras.push(("Mcp-Session-Id", sid));
|
||||
}
|
||||
let extra_refs: Vec<(&str, &str)> = extras.iter().map(|(k, v)| (*k, v.as_str())).collect();
|
||||
write_http(
|
||||
&mut stream,
|
||||
200,
|
||||
"OK",
|
||||
"application/json",
|
||||
&body,
|
||||
&extra_refs,
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Streamable HTTP MCP (POST JSON-RPC + optional GET SSE). Spec 2025-03-26.
|
||||
pub fn serve_http(
|
||||
name: &str,
|
||||
tools: Vec<Value>,
|
||||
host: &str,
|
||||
port: u16,
|
||||
path: &str,
|
||||
) -> Result<Value, String> {
|
||||
let addr = format!("{host}:{port}");
|
||||
let listener = TcpListener::bind(&addr).map_err(|e| format!("bind {addr}: {e}"))?;
|
||||
let name = Arc::new(name.to_string());
|
||||
let tools = Arc::new(tools);
|
||||
let endpoint = Arc::new(if path.is_empty() {
|
||||
"/mcp".to_string()
|
||||
} else if path.starts_with('/') {
|
||||
path.to_string()
|
||||
} else {
|
||||
format!("/{path}")
|
||||
});
|
||||
eprintln!(
|
||||
"mcp streamable-http listening on http://{addr}{} (POST JSON-RPC)",
|
||||
&*endpoint
|
||||
);
|
||||
for conn in listener.incoming() {
|
||||
match conn {
|
||||
Ok(stream) => {
|
||||
let name = Arc::clone(&name);
|
||||
let tools = Arc::clone(&tools);
|
||||
let endpoint = Arc::clone(&endpoint);
|
||||
thread::spawn(move || {
|
||||
if let Err(e) = handle_http_conn(stream, &name, &tools, &endpoint) {
|
||||
eprintln!("mcp http conn: {e}");
|
||||
}
|
||||
});
|
||||
}
|
||||
Err(e) => eprintln!("mcp http accept: {e}"),
|
||||
}
|
||||
}
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"transport": "http",
|
||||
"host": host,
|
||||
"port": port,
|
||||
"path": *endpoint
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn tools_from_value(v: &Value) -> Vec<Value> {
|
||||
if let Some(arr) = v.as_array() {
|
||||
return arr.clone();
|
||||
@@ -180,13 +452,6 @@ pub unsafe extern "C" fn agent_mcp_serve(
|
||||
.get("transport")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("stdio");
|
||||
if transport != "stdio" {
|
||||
set_err(
|
||||
err_msg,
|
||||
&format!("mcp serve: transport `{transport}` not implemented yet (use stdio)"),
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
let name = args
|
||||
.get("name")
|
||||
.and_then(|v| v.as_str())
|
||||
@@ -195,7 +460,38 @@ pub unsafe extern "C" fn agent_mcp_serve(
|
||||
.get("tools")
|
||||
.map(tools_from_value)
|
||||
.unwrap_or_default();
|
||||
match serve_stdio(name, tools) {
|
||||
|
||||
let result = match transport {
|
||||
"stdio" => serve_stdio(name, tools),
|
||||
"http" | "streamable-http" | "sse" => {
|
||||
let host = args
|
||||
.get("host")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("127.0.0.1");
|
||||
let port = args
|
||||
.get("port")
|
||||
.and_then(|v| {
|
||||
v.as_u64()
|
||||
.or_else(|| v.as_i64().map(|n| n as u64))
|
||||
.or_else(|| v.as_str().and_then(|s| s.parse().ok()))
|
||||
})
|
||||
.unwrap_or(7434) as u16;
|
||||
let path = args
|
||||
.get("path")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("/mcp");
|
||||
serve_http(name, tools, host, port, path)
|
||||
}
|
||||
other => {
|
||||
set_err(
|
||||
err_msg,
|
||||
&format!("mcp serve: transport `{other}` not supported (use stdio|http)"),
|
||||
);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
|
||||
match result {
|
||||
Ok(v) => {
|
||||
set_out(out_json, &v.to_string());
|
||||
0
|
||||
@@ -207,6 +503,5 @@ pub unsafe extern "C" fn agent_mcp_serve(
|
||||
}
|
||||
}
|
||||
|
||||
// Silence unused import if CStr unused in this module.
|
||||
#[allow(dead_code)]
|
||||
fn _unused(_: &CStr) {}
|
||||
@@ -1038,10 +1038,17 @@ pub fn resolve_task(args: &Value) -> Result<Value, String> {
|
||||
"tools": args.get("tools").cloned().unwrap_or(Value::Null),
|
||||
}))?;
|
||||
if hit.is_null() {
|
||||
// Keep field set aligned with hit path so facades can safely read llm_free
|
||||
// (qdagent / agent.run crashed on missing key when OKF is cold).
|
||||
return Ok(json!({
|
||||
"mode": "unknown",
|
||||
"match": null,
|
||||
"skill": null,
|
||||
"resource": null,
|
||||
"slug": null,
|
||||
"status": null,
|
||||
"llm_free": false,
|
||||
"score": null,
|
||||
"confidence": 0.0,
|
||||
}));
|
||||
}
|
||||
@@ -1078,6 +1085,25 @@ pub fn resolve_task(args: &Value) -> Result<Value, String> {
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn resolve_miss_includes_llm_free() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
"marqdo-resolve-miss-{}",
|
||||
std::process::id()
|
||||
));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let out = resolve_task(&json!({
|
||||
"task": "no-such-skill-cold-miss",
|
||||
"kb_dir": dir.to_string_lossy(),
|
||||
"near_match": false,
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(out.get("mode").and_then(|v| v.as_str()), Some("unknown"));
|
||||
assert_eq!(out.get("llm_free").and_then(|v| v.as_bool()), Some(false));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn record_and_cluster_and_compile() {
|
||||
let dir = std::env::temp_dir().join(format!(
|
||||
|
||||
+42
-6
@@ -245,7 +245,12 @@ fn chat_completions(args: &Value) -> Result<Value, String> {
|
||||
let status = r.status();
|
||||
if status != 200 {
|
||||
let text = read_limited(r.into_reader())?;
|
||||
return Err(format!("HTTP {status}: {text}"));
|
||||
return Ok(json!({
|
||||
"events": [{"type": "error", "message": format!("HTTP {status}: {text}")}],
|
||||
"finish": "error",
|
||||
"error": format!("HTTP {status}: {text}"),
|
||||
"transport": "plugin",
|
||||
}));
|
||||
}
|
||||
let events = parse_sse_events(BufReader::new(r.into_reader()), echo)?;
|
||||
Ok(json!({
|
||||
@@ -256,9 +261,19 @@ fn chat_completions(args: &Value) -> Result<Value, String> {
|
||||
}
|
||||
Err(ureq::Error::Status(code, r)) => {
|
||||
let text = read_limited(r.into_reader()).unwrap_or_default();
|
||||
Err(format!("HTTP {code}: {text}"))
|
||||
Ok(json!({
|
||||
"events": [{"type": "error", "message": format!("HTTP {code}: {text}")}],
|
||||
"finish": "error",
|
||||
"error": format!("HTTP {code}: {text}"),
|
||||
"transport": "plugin",
|
||||
}))
|
||||
}
|
||||
Err(e) => Err(format!("http POST sse {url}: {e}")),
|
||||
Err(e) => Ok(json!({
|
||||
"events": [{"type": "error", "message": format!("http POST sse {url}: {e}")}],
|
||||
"finish": "error",
|
||||
"error": format!("http POST sse {url}: {e}"),
|
||||
"transport": "plugin",
|
||||
})),
|
||||
}
|
||||
} else {
|
||||
match req.send_string(&body) {
|
||||
@@ -266,7 +281,14 @@ fn chat_completions(args: &Value) -> Result<Value, String> {
|
||||
let status = r.status();
|
||||
let text = read_limited(r.into_reader())?;
|
||||
if status != 200 {
|
||||
return Err(format!("HTTP {status}: {text}"));
|
||||
return Ok(json!({
|
||||
"text": "",
|
||||
"usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
|
||||
"finish": "error",
|
||||
"error": format!("HTTP {status}: {text}"),
|
||||
"tool_calls": [],
|
||||
"transport": "plugin",
|
||||
}));
|
||||
}
|
||||
let data: Value = serde_json::from_str(&text).map_err(|e| e.to_string())?;
|
||||
let msg = data
|
||||
@@ -299,9 +321,23 @@ fn chat_completions(args: &Value) -> Result<Value, String> {
|
||||
}
|
||||
Err(ureq::Error::Status(code, r)) => {
|
||||
let text = read_limited(r.into_reader()).unwrap_or_default();
|
||||
Err(format!("HTTP {code}: {text}"))
|
||||
Ok(json!({
|
||||
"text": "",
|
||||
"usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
|
||||
"finish": "error",
|
||||
"error": format!("HTTP {code}: {text}"),
|
||||
"tool_calls": [],
|
||||
"transport": "plugin",
|
||||
}))
|
||||
}
|
||||
Err(e) => Err(format!("http POST {url}: {e}")),
|
||||
Err(e) => Ok(json!({
|
||||
"text": "",
|
||||
"usage": {"prompt_tokens": 0, "completion_tokens": 0, "total_tokens": 0},
|
||||
"finish": "error",
|
||||
"error": format!("http POST {url}: {e}"),
|
||||
"tool_calls": [],
|
||||
"transport": "plugin",
|
||||
})),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ package httpx
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
@@ -118,12 +119,74 @@ func jsonWrapInvokeValue(value any) (int, map[string]any, error) {
|
||||
}
|
||||
switch t := value.(type) {
|
||||
case map[string]any:
|
||||
return http.StatusOK, t, nil
|
||||
code := http.StatusOK
|
||||
if ok, has := t["ok"]; has {
|
||||
if b, isBool := ok.(bool); isBool && !b {
|
||||
if errMsg, _ := t["error"].(string); strings.EqualFold(errMsg, "unauthorized") ||
|
||||
strings.Contains(strings.ToLower(errMsg), "unauthorized") {
|
||||
code = http.StatusUnauthorized
|
||||
}
|
||||
}
|
||||
}
|
||||
return code, t, nil
|
||||
default:
|
||||
return http.StatusOK, map[string]any{"ok": true, "result": value}, nil
|
||||
}
|
||||
}
|
||||
|
||||
// writeSSE writes OpenAI-compatible (or generic) Server-Sent Events.
|
||||
// value may be a string body, or a map with key "sse" / "body" / "chunks".
|
||||
func writeSSE(w http.ResponseWriter, value any) {
|
||||
w.Header().Set("Content-Type", "text/event-stream; charset=utf-8")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
flusher, _ := w.(http.Flusher)
|
||||
|
||||
writeChunk := func(s string) {
|
||||
if s == "" {
|
||||
return
|
||||
}
|
||||
if !strings.HasSuffix(s, "\n") {
|
||||
s += "\n"
|
||||
}
|
||||
_, _ = io.WriteString(w, s)
|
||||
if flusher != nil {
|
||||
flusher.Flush()
|
||||
}
|
||||
}
|
||||
|
||||
switch t := value.(type) {
|
||||
case string:
|
||||
writeChunk(t)
|
||||
case map[string]any:
|
||||
if s, ok := t["sse"].(string); ok {
|
||||
writeChunk(s)
|
||||
return
|
||||
}
|
||||
if s, ok := t["body"].(string); ok {
|
||||
writeChunk(s)
|
||||
return
|
||||
}
|
||||
if chunks, ok := t["chunks"].([]any); ok {
|
||||
for _, c := range chunks {
|
||||
writeChunk(fmt.Sprint(c))
|
||||
}
|
||||
return
|
||||
}
|
||||
b, err := json.Marshal(t)
|
||||
if err != nil {
|
||||
writeChunk("data: {\"error\":\"sse_encode\"}\n\n")
|
||||
return
|
||||
}
|
||||
writeChunk("data: " + string(b) + "\n\n")
|
||||
writeChunk("data: [DONE]\n\n")
|
||||
default:
|
||||
writeChunk("data: " + fmt.Sprint(value) + "\n\n")
|
||||
writeChunk("data: [DONE]\n\n")
|
||||
}
|
||||
}
|
||||
|
||||
func invokeArgsFromPayload(payload map[string]any) map[string]any {
|
||||
args := map[string]any{}
|
||||
for k, v := range payload {
|
||||
|
||||
@@ -17,8 +17,8 @@ type artifactRoute struct {
|
||||
Path string
|
||||
File string
|
||||
Request string // json | form | none | query
|
||||
Response string // json | text | html
|
||||
Auth string // role name or "" / "none"
|
||||
Response string // json | text | html | sse
|
||||
Auth string // role name or "" / "none" | "bearer"
|
||||
}
|
||||
|
||||
func parseArtifactRoute(v any) artifactRoute {
|
||||
@@ -57,16 +57,21 @@ func (st *state) mountArtifactRoutes(mux *http.ServeMux) {
|
||||
|
||||
func (st *state) handleArtifact(w http.ResponseWriter, r *http.Request, route artifactRoute) {
|
||||
if route.Auth != "" && !strings.EqualFold(route.Auth, "none") {
|
||||
cookie := r.Header.Get("Cookie")
|
||||
user := session.UsernameFromCookie(cookie)
|
||||
if user == "" {
|
||||
jsonErr(w, http.StatusUnauthorized, "auth required")
|
||||
return
|
||||
}
|
||||
role := session.RoleFromCookie(cookie)
|
||||
if role != "" && role != route.Auth && role != "admin" && role != "superadmin" {
|
||||
jsonErr(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
if strings.EqualFold(route.Auth, "bearer") || strings.EqualFold(route.Auth, "api_key") {
|
||||
// Bearer / API-key auth is enforced inside the Endpoint `# main`
|
||||
// (payload carries authorization / api_key). Do not require cookies.
|
||||
} else {
|
||||
cookie := r.Header.Get("Cookie")
|
||||
user := session.UsernameFromCookie(cookie)
|
||||
if user == "" {
|
||||
jsonErr(w, http.StatusUnauthorized, "auth required")
|
||||
return
|
||||
}
|
||||
role := session.RoleFromCookie(cookie)
|
||||
if role != "" && role != route.Auth && role != "admin" && role != "superadmin" {
|
||||
jsonErr(w, http.StatusForbidden, "forbidden")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -115,6 +120,16 @@ func (st *state) handleArtifact(w http.ResponseWriter, r *http.Request, route ar
|
||||
payload[name] = v
|
||||
}
|
||||
}
|
||||
if auth := strings.TrimSpace(r.Header.Get("Authorization")); auth != "" {
|
||||
if _, exists := payload["authorization"]; !exists {
|
||||
payload["authorization"] = auth
|
||||
}
|
||||
}
|
||||
if key := strings.TrimSpace(r.Header.Get("X-Api-Key")); key != "" {
|
||||
if _, exists := payload["api_key"]; !exists {
|
||||
payload["api_key"] = key
|
||||
}
|
||||
}
|
||||
|
||||
value, err := plugin.RunArtifact(route.File, payload)
|
||||
if err != nil {
|
||||
@@ -131,6 +146,8 @@ func (st *state) handleArtifact(w http.ResponseWriter, r *http.Request, route ar
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = io.WriteString(w, fmt.Sprint(value))
|
||||
case "sse", "event-stream", "text/event-stream":
|
||||
writeSSE(w, value)
|
||||
default:
|
||||
code, body, err := jsonWrapInvokeValue(value)
|
||||
if err != nil {
|
||||
|
||||
@@ -102,6 +102,18 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo
|
||||
}
|
||||
}
|
||||
|
||||
// Bind common auth / client headers for OpenAI-compatible and API-key surfaces.
|
||||
if auth := strings.TrimSpace(r.Header.Get("Authorization")); auth != "" {
|
||||
if _, exists := payload["authorization"]; !exists {
|
||||
payload["authorization"] = auth
|
||||
}
|
||||
}
|
||||
if key := strings.TrimSpace(r.Header.Get("X-Api-Key")); key != "" {
|
||||
if _, exists := payload["api_key"]; !exists {
|
||||
payload["api_key"] = key
|
||||
}
|
||||
}
|
||||
|
||||
args := invokeArgsFromPayload(payload)
|
||||
value, err := plugin.CallLibPath(route.FnPath, args)
|
||||
if err != nil {
|
||||
@@ -114,10 +126,22 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo
|
||||
strings.Contains(msg, "no active host") {
|
||||
code = http.StatusBadRequest
|
||||
}
|
||||
if strings.Contains(msg, "unauthorized") || strings.Contains(msg, "Unauthorized") {
|
||||
code = http.StatusUnauthorized
|
||||
}
|
||||
jsonErr(w, code, msg)
|
||||
return
|
||||
}
|
||||
|
||||
// OpenAI-compatible handlers may return `{sse: "..."}` to force event-stream
|
||||
// even when the route's declared return mode is json.
|
||||
if m, ok := value.(map[string]any); ok {
|
||||
if s, ok := m["sse"].(string); ok && strings.TrimSpace(s) != "" {
|
||||
writeSSE(w, m)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
switch route.ReturnMode {
|
||||
case "text", "html", "text/html":
|
||||
if value == nil {
|
||||
@@ -135,6 +159,8 @@ func (st *state) handleInvoke(w http.ResponseWriter, r *http.Request, route invo
|
||||
w.Header().Set("Content-Type", ct)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
_, _ = io.WriteString(w, text)
|
||||
case "sse", "event-stream", "text/event-stream":
|
||||
writeSSE(w, value)
|
||||
case "status":
|
||||
if value == nil {
|
||||
jsonErr(w, http.StatusInternalServerError, "null result")
|
||||
|
||||
+17
-3
@@ -152,8 +152,13 @@ pub fn http_post(
|
||||
Some(v) => as_text(v, "content_type")?,
|
||||
};
|
||||
let hdrs = headers_from_value(headers)?;
|
||||
let (status, resp) = http_exchange("POST", url, Some(body), Some(ct), &hdrs)?;
|
||||
Ok(response_map(status, resp))
|
||||
// Transport failures → status 0 + body message (soft). Callers that need
|
||||
// hard errors can still treat status!=200 as failure; Agent/LLM surfaces
|
||||
// map this into structured `{finish:error}` instead of aborting the VM.
|
||||
match http_exchange("POST", url, Some(body), Some(ct), &hdrs) {
|
||||
Ok((status, resp)) => Ok(response_map(status, resp)),
|
||||
Err(e) => Ok(response_map(0, e)),
|
||||
}
|
||||
}
|
||||
|
||||
/// Generic request: method + url + optional body / content_type / headers.
|
||||
@@ -847,7 +852,16 @@ pub fn http_post_sse(
|
||||
),
|
||||
]))
|
||||
}
|
||||
Err(e) => Err(format!("http POST sse {url}: {e}")),
|
||||
Err(e) => Ok(Value::Map(vec![
|
||||
("status".into(), Value::Int(0)),
|
||||
(
|
||||
"events".into(),
|
||||
Value::List(vec![event_map(vec![
|
||||
("type", Value::Text("error".into())),
|
||||
("message", Value::Text(format!("http POST sse {url}: {e}"))),
|
||||
])]),
|
||||
),
|
||||
])),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
---
|
||||
title: agent / llm soft transport errors
|
||||
description: >-
|
||||
Regression (2026-10): unreachable OpenAI upstream must not abort the VM.
|
||||
`llm.ask` / `agent.step` / `agent.run` return structured status=error.
|
||||
import agent:ext/ai/agent.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import llm:ext/ai/llm.mq.md
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
> sys.env_set name="OPENAI_API_KEY" value="offline-transport-soft"
|
||||
> sys.env_set name="OPENAI_BASE_URL" value="http://127.0.0.1:9"
|
||||
|
||||
**p = > plugin.native_path name="agent"**
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
2. *
|
||||
> print text=no-agent-plugin
|
||||
> sys.exit code=1
|
||||
|
||||
**model = > llm.llm**
|
||||
**ask = > model.ask prompt="ping soft transport"**
|
||||
**fin = > json.get value=`ask` key="finish"**
|
||||
**aerr = > json.get value=`ask` key="error"**
|
||||
1. `fin` == "error"
|
||||
1. `aerr`
|
||||
> print text=llm-ask-soft-ok
|
||||
2. *
|
||||
> print text=llm-ask-missing-error
|
||||
> print text=`ask`
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=llm-ask-hard-or-ok-unexpected
|
||||
> print text=`ask`
|
||||
> sys.exit code=1
|
||||
|
||||
`tools` =
|
||||
|
||||
| 工具 |
|
||||
|------|
|
||||
|
||||
**ag = > agent.agent model=`model` tools=`tools` standing="transport soft probe"**
|
||||
**step = > `ag`.step task="unreachable upstream probe" writeback=False max_reads=1**
|
||||
**sst = > json.get value=`step` key="status"**
|
||||
**serr = > json.get value=`step` key="error"**
|
||||
1. `sst` == "error"
|
||||
1. `serr`
|
||||
> print text=agent-step-soft-ok
|
||||
2. *
|
||||
> print text=agent-step-missing-error
|
||||
> print text=`step`
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=agent-step-unexpected
|
||||
> print text=`step`
|
||||
> sys.exit code=1
|
||||
|
||||
**out = > `ag`.run task="unreachable upstream probe" efi=False learn=False verify=False writeback=False reuse=False near_match=False max_rounds=1**
|
||||
**rst = > json.get value=`out` key="status"**
|
||||
**rerr = > json.get value=`out` key="error"**
|
||||
1. `rst` == "error"
|
||||
1. `rerr`
|
||||
> print text=agent-run-soft-ok
|
||||
2. *
|
||||
> print text=agent-run-missing-error
|
||||
> print text=`out`
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
> print text=agent-run-unexpected
|
||||
> print text=`out`
|
||||
> sys.exit code=1
|
||||
|
||||
*"agent-llm-transport-soft-ok"*
|
||||
@@ -0,0 +1,76 @@
|
||||
---
|
||||
title: agent.run cold OKF — resolve llm_free + self.preflight
|
||||
description: >-
|
||||
Regression for qdagent-facing bugs (2026-10):
|
||||
(A) miss resolve must expose llm_free=false;
|
||||
(B) `self`.preflight must resolve when agent is imported by a consumer.
|
||||
Offline — dummy API key; no live network required for assertions A/B.
|
||||
import agent:ext/ai/agent.mq.md
|
||||
import fs:lib/fs.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import llm:ext/ai/llm.mq.md
|
||||
import plugin:lib/plugin.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
> sys.env_set name="OPENAI_API_KEY" value="offline-cold-efi-dummy"
|
||||
|
||||
**p = > plugin.native_path name="agent"**
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
2. *
|
||||
> print text=no-agent-plugin
|
||||
> sys.exit code=1
|
||||
|
||||
**kb = ".marqdo/agent-kb-cold-efi"**
|
||||
**ex = > fs.exists path=`kb`**
|
||||
1. `ex`
|
||||
> fs.remove_tree path=`kb`
|
||||
2. *
|
||||
**_ = 1**
|
||||
> fs.make_dir path=`kb`
|
||||
|
||||
**resolved = > agent_resolve task="qdagent-cold-unknown-task-xyz-20261016" kb_dir=`kb`**
|
||||
**lf = > json.get value=`resolved` key="llm_free"**
|
||||
**mode = > json.get value=`resolved` key="mode"**
|
||||
1. `lf` == False
|
||||
> print text=resolve-miss-llm_free-ok
|
||||
2. *
|
||||
> print text=resolve-miss-llm_free-MISSING
|
||||
> print text=`resolved`
|
||||
> sys.exit code=1
|
||||
|
||||
1. `mode` == "unknown"
|
||||
> print text=resolve-miss-mode-ok
|
||||
2. *
|
||||
> print text=resolve-miss-mode-unexpected
|
||||
> sys.exit code=1
|
||||
|
||||
**model = > llm.llm**
|
||||
`tools` =
|
||||
|
||||
| 工具 |
|
||||
|------|
|
||||
|
||||
**ag = > agent.agent model=`model` tools=`tools` standing="cold efi preflight probe"**
|
||||
**pf = > `ag`.preflight task="qdagent-cold-unknown-task-xyz-20261016" marqdo_dir=".marqdo"**
|
||||
**st = > json.get value=`pf` key="status"**
|
||||
1. `st`
|
||||
> print text=self-preflight-ok
|
||||
> print text=`st`
|
||||
2. *
|
||||
> print text=self-preflight-failed
|
||||
> print text=`pf`
|
||||
> sys.exit code=1
|
||||
|
||||
**via = > `ag`.resolve task="qdagent-cold-unknown-task-xyz-20261016" kb_dir=`kb`**
|
||||
**via_lf = > json.get value=`via` key="llm_free"**
|
||||
1. `via_lf` == False
|
||||
> print text=method-resolve-llm_free-ok
|
||||
2. *
|
||||
> print text=method-resolve-llm_free-MISSING
|
||||
> sys.exit code=1
|
||||
|
||||
*"agent-run-cold-efi-ok"*
|
||||
Reference in new issue
Block a user