diff --git a/Cargo.lock b/Cargo.lock index bbb4113..66040fe 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -140,9 +140,9 @@ checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" [[package]] name = "cc" -version = "1.4.0" +version = "1.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5add81bb678e6cb321aff7fa0dc7689ad82b112dbc032cea19f91d6b8e3582b9" +checksum = "5d262e149917187838d5b42777c8253bcb64500067342904e7d429499a6f277e" dependencies = [ "find-msvc-tools", "shlex", @@ -246,9 +246,9 @@ checksum = "780955b8b195a21ab8e4ac6b60dd1dbdcec1dc6c51c0617964b08c81785e12c9" [[package]] name = "find-msvc-tools" -version = "0.1.9" +version = "0.1.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5baebc0774151f905a1a2cc41989300b1e6fbb29aff0ceffa1064fdd3088d582" +checksum = "26b73573e6edcd2af0cdf47bd6cb58f0b3839491263c314eaad1ccf24430e1de" [[package]] name = "flate2" @@ -422,9 +422,9 @@ checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682" [[package]] name = "js-sys" -version = "0.3.103" +version = "0.3.104" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "53b44bfcdb3f8d5837a46dae1ca9660a837176eee74a28b229bc626816589102" +checksum = "0e0c1080212aad755ea003d18543e8768dd432c48819efd73a7bf1e39b7a5a3a" dependencies = [ "cfg-if", "futures-util", @@ -463,6 +463,7 @@ dependencies = [ "clap", "idna_adapter", "include_dir", + "libc", "libloading", "predicates", "pulldown-cmark", @@ -949,9 +950,9 @@ checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b" [[package]] name = "wasm-bindgen" -version = "0.2.126" +version = "0.2.127" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4b067c0c11094aef6b7a801c1e34a26affafdf3d051dba08456b868789aaf9a4" +checksum = "1b70935747edd64d89de3efa29d73789b806c15798f8e7dca4d8ac356b50ce70" dependencies = [ "cfg-if", "once_cell", @@ -962,9 +963,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro" -version = "0.2.126" +version = "0.2.127" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "167ce5e579f6bcf889c4f7175a8a5a585de84e8ff93976ce393efa5f2837aab1" +checksum = "77775f8f3f7217702089053b94958f8f54061a3f663417df76e19cbdcca29bc1" dependencies = [ "quote", "wasm-bindgen-macro-support", @@ -972,9 +973,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-macro-support" -version = "0.2.126" +version = "0.2.127" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3997c7839262f4ef12cf90b818d6340c18e80f263f1a94bf157d0ec4420380e" +checksum = "e11d33f857dc2fb11b8bc75aee111aa9cbeb12cd9f25efd3d4c2a3dd4e235284" dependencies = [ "bumpalo", "proc-macro2", @@ -985,9 +986,9 @@ dependencies = [ [[package]] name = "wasm-bindgen-shared" -version = "0.2.126" +version = "0.2.127" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "dc1b4cb0cc549fcf58d7dfc081778139b3d283a081644e833e84682ad71cea24" +checksum = "7ef64dbcc55df09c7e5a46182d181c2cfa3e925f3da937ea764728b4bbb9dcbf" dependencies = [ "unicode-ident", ] diff --git a/Cargo.toml b/Cargo.toml index e0f6dcc..dbf5ea5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ idna_adapter = "=1.1.0" zeroize = "=1.8.1" libloading = "=0.8.6" include_dir = "0.7" +libc = "0.2" [workspace] members = ["plugins/demo", "plugins/agent"] diff --git a/doc/design/ext-agent-plan.md b/doc/design/ext-agent-plan.md index f68b9b0..b410036 100644 --- a/doc/design/ext-agent-plan.md +++ b/doc/design/ext-agent-plan.md @@ -58,7 +58,7 @@ - **大模型配置与父智能体一致**(见 §5) - 正文:总目标叙述 + 子智能体骨架 / 或父模型生成的首版步骤 ▼ -③ (可选)父模型「分解」:改写工作簿为更细的步骤 / 多智能体 +③ 父模型「分解」(非 OKF 命中路径必跑):`phase=decompose`,尚未 spawn;可 `DECISION: RUN` / `CONTINUE`+补丁 / 固化后 `DONE` ▼ ④ lib/subtask:spawn path=<工作簿绝对或相对路径> ▼ diff --git a/doc/design/ext-agent.md b/doc/design/ext-agent.md index 6ad17d5..8f0de83 100644 --- a/doc/design/ext-agent.md +++ b/doc/design/ext-agent.md @@ -111,7 +111,7 @@ Marqdo 的宪法是 **代码即文档、文档即知识库**: | | | |---|---| | **输入** | 复杂目标 | -| **过程** | 父智能体**创建(或续跑)一份工作簿 `.mq.md`** → 子文件大模型配置与父一致 → **`lib/subtask` `spawn path=`** 运行该文件 → 读结果与写回 → **改代码 / 改写回后再执行**(有轮次上限) | +| **过程** | 父智能体**创建(或续跑)工作簿** →(非命中)**父先分解** → **`lib/subtask` spawn** 跑子文件 → 观察写回 → **父修订补丁后再执行**(有轮次上限) | | **输出** | 汇总 map(含 `result`)+ 磁盘上的工作簿(代码与 `marqdo-out` 均可演化) | | **适用** | 需分解、多情形、可固化步骤、可复用子成果的任务 | @@ -346,4 +346,4 @@ lib/subtask:spawn fn=<名> → wait **智能体框架必须让编排看起来像文档、跑起来像程序、留痕像知识库。** 单步解决原子问题(默认写回);多步让父智能体编写并修订子 Marqdo 工作簿、经子任务执行、把确定步骤固化为代码——详见 [ext-agent-plan.md](ext-agent-plan.md)。 -流式:`## step` / `## plan` 支持 `stream=True` / `echo=True`(SSE 增量;返回 map 不变;`plan` 另附 `events`,可选 `trace` 写回)。见 [agent-streaming.md](../roadmap/agent-streaming.md)。 +流式:`## step` / `## plan` 支持 `stream=True` / `echo=True`(SSE 增量;返回 map 不变;`plan` 另附 `events`,可选 `trace` 写回)。非命中时先流式父分解再 spawn 子任务。见 [agent-streaming.md](../roadmap/agent-streaming.md)。 diff --git a/doc/design/ext-llm.md b/doc/design/ext-llm.md index 26e6a36..fd77ca1 100644 --- a/doc/design/ext-llm.md +++ b/doc/design/ext-llm.md @@ -20,7 +20,7 @@ Resolution: `MARQDO_EXT/ai/…`, repo `ext/ai/…`, cwd `ext/ai/…` (see [ext-c Other official ext: [ext-agent.md](ext-agent.md) / [ext-agent-plan.md](ext-agent-plan.md) (**document-driven agent** — step with default writeback / plan workbook; thin `TOOL:` loop superseded). Installer: [ext-cli.md](ext-cli.md). Native plugins: [ext-abi.md](ext-abi.md). -Streaming: `complete stream=True` returns an event list (`delta` / `done` / `error`); see [agent-streaming.md](../roadmap/agent-streaming.md). Default (`stream=False`) is unchanged (answer string). +Streaming: `complete stream=True` returns an event list (`reasoning` / `delta` / `done` / `error`); `echo=True` prints thinking (`reasoning_content`) and answer tokens as they arrive. See [agent-streaming.md](../roadmap/agent-streaming.md). Default (`stream=False`) is unchanged (answer string). **Note:** Prefer `marqdo ext add llm`. Import `> ext/ai/llm.mq.md` from repo or install root. diff --git a/doc/roadmap/agent-streaming.md b/doc/roadmap/agent-streaming.md index 481495a..d88c07b 100644 --- a/doc/roadmap/agent-streaming.md +++ b/doc/roadmap/agent-streaming.md @@ -62,11 +62,12 @@ ext/ai/agent step/plan 订阅事件 → 可选透传或写回「过程槽」 | `type` | 含义 | 主要字段 | |--------|------|----------| -| `delta` | 模型增量文本 | `text` | +| `reasoning` | 模型思考增量(如 DeepSeek `reasoning_content`) | `text` | +| `delta` | 模型答案增量文本 | `text` | | `tool_start` / `tool_end` | 工具轮 | `name`, `result?` | | `round` | plan 轮次边界 | `round`, `workbook`, `exit_code?` | -| `decision` | DONE / CONTINUE | `decision`, `summary?` | -| `done` | 本调用结束 | `result`(与今日返回值对齐) | +| `decision` | DONE / CONTINUE / RUN | `decision`, `summary?` | +| `done` | 本调用结束 | `result`(答案正文;不含 thinking) | | `error` | 失败 | `message` | 中英键名可在 L1 包装层镜像;插件/wire 先用英文 `type`。 @@ -148,7 +149,8 @@ S3 view 订阅 (待做) 1. `plan` / `多步` 增加 `stream` / `echo` / `trace`(中文 `流式` / `打印增量` / `轨迹`)。 2. 事件挂在返回 map 的 `events`;父 `complete` 只冒泡 `delta`/`error`(嵌套 `done` 不进列表)。 -3. `trace=True` → 写回槽 `trace`(与 TTY `echo` 可分立)。 +3. `trace=True` → 写回槽 `trace`(与 TTY `echo` 可分立)。 +4. **体验(父先子后)**:非命中路径先 `plan:decompose`(父 pre-run 分解,可流式),再 `plan:await`(子 quiet)。修订轮仍是观察后父决策。SSE 本身用 `tests/ext/llm-stream-live.mq.md`。 验收: diff --git a/ext/ai/agent.mq.md b/ext/ai/agent.mq.md index 2ac1697..f09df4e 100644 --- a/ext/ai/agent.mq.md +++ b/ext/ai/agent.mq.md @@ -197,7 +197,7 @@ Spawn a workbook file (quiet by default), wait for `{code,value}`, and inspect. ## extract_plan_decision + `reply` -Parse `DECISION: DONE` / `DECISION: CONTINUE` (Chinese: `决定:完成` / `决定:继续`). +Parse `DECISION: DONE` / `CONTINUE` / `RUN` (Chinese: `决定:完成` / `继续` / `运行`). *`esc` = > json.parse text={"nl":"\n","fw":":"} * *`nl` = > json.get value=`esc` key=nl * @@ -242,11 +242,15 @@ Parse `DECISION: DONE` / `DECISION: CONTINUE` (Chinese: `决定:完成` / `决 **DONE** 2. `found` == CONTINUE **CONTINUE** -3. `found` == 完成 +3. `found` == RUN + **RUN** +4. `found` == 完成 **DONE** -4. `found` == 继续 +5. `found` == 继续 **CONTINUE** -5. * +6. `found` == 运行 + **RUN** +7. * **`found`** --- @@ -308,8 +312,9 @@ First `SUMMARY:` / `汇总:` / `汇总:` line body, else trimmed reply. + `observation` + `explore_attempt`=None + `explore_n`=None + + `phase`=revise -Parent developer-agent prompt: observe → decide DONE or CONTINUE with exact FIND/REPLACE patches (never rewrite the whole workbook). +Parent developer-agent prompt. `phase=decompose`: pre-run (design step ③) before any spawn. `phase=revise` (default): observe → DONE or CONTINUE with patches. *`standing` = > json.get value=`agent` key=standing * 1. `standing` @@ -320,14 +325,18 @@ Parent developer-agent prompt: observe → decide DONE or CONTINUE with exact FI *`skill` = > agent_marqdo_skill * *`obs_s` = > json.stringify value=`observation` * *`goal_s` = > json.stringify value=`goal` * -*`esc` = > json.parse text={"a":"\n\n--- standing ---\n","b":"\n\n--- goal ---\n","c":"\n\n--- workbook observation ---\n","d":"\n\n--- marqdo skill ---\n","e":"\n\n--- how to act ---\nYou are a Marqdo agent-development master.\nPriority order:\n1) Code first: if the goal is a fixed answer, solidify to a **return** (e.g. **pong**) or ordinary ## — no further LLM worker.step. Do not rely on > print; file subtasks are quiet and the parent consumes wait.value.\n2) If exit_code is 0 AND the workbook source still contains worker.step (or .单步), you MUST CONTINUE with a FIND/REPLACE that solidifies to a returned answer. Never DONE while .step remains.\n3) If exit_code is 0 AND already solidified (no worker.step) with a clear main return, DECISION: DONE with a short SUMMARY.\n4) If exploring an alternate path, try a meaningfully DIFFERENT structure than prior attempts (e.g. dual agents, narrower standing, or pure code). Do not merely re-run the same step.\n5) Prefer multiple narrow agents over one giant standing when roles differ.\nNever rewrite the whole .mq.md file.\nReply with EXACTLY one protocol:\nDECISION: DONE\nSUMMARY: \nOR\nDECISION: CONTINUE\nPATCH:\n<<<\nFIND\n\n===\nREPLACE\n\n>>>\n(You may repeat <<< blocks.)\n","f":"\n\n--- explore attempt ---\n"} * +*`esc` = > json.parse text={"a":"\n\n--- standing ---\n","b":"\n\n--- goal ---\n","c":"\n\n--- workbook observation ---\n","d":"\n\n--- marqdo skill ---\n","e_rev":"\n\n--- how to act ---\nYou are a Marqdo agent-development master.\nPriority order:\n1) Code first: if the goal is a fixed answer, solidify to a **return** (e.g. **pong**) or ordinary ## — no further LLM worker.step. Do not rely on > print; file subtasks are quiet and the parent consumes wait.value.\n2) If exit_code is 0 AND the workbook source still contains worker.step (or .单步), you MUST CONTINUE with a FIND/REPLACE that solidifies to a returned answer. Never DONE while .step remains.\n3) If exit_code is 0 AND already solidified (no worker.step) with a clear main return, DECISION: DONE with a short SUMMARY.\n4) If exploring an alternate path, try a meaningfully DIFFERENT structure than prior attempts (e.g. dual agents, narrower standing, or pure code). Do not merely re-run the same step.\n5) Prefer multiple narrow agents over one giant standing when roles differ.\nNever rewrite the whole .mq.md file.\nReply with EXACTLY one protocol:\nDECISION: DONE\nSUMMARY: \nOR\nDECISION: CONTINUE\nPATCH:\n<<<\nFIND\n\n===\nREPLACE\n\n>>>\n(You may repeat <<< blocks.)\n","e_dec":"\n\n--- how to act ---\nYou are a Marqdo agent-development master. PRE-RUN DECOMPOSE: the workbook has NOT been executed yet.\nPriority order:\n1) Code first: if the goal is a fixed answer, PATCH to a **return** (no worker.step / .单步), then DECISION: RUN (runtime will spawn once) or DECISION: DONE with SUMMARY after solidify.\n2) If the skeleton is fine to execute as-is, reply DECISION: RUN (patches optional; zero patches are OK).\n3) To reshape before the first run, DECISION: CONTINUE with FIND/REPLACE patches; runtime applies them then spawns.\n4) Prefer multiple narrow agents when roles differ. Never rewrite the whole .mq.md file.\nReply with EXACTLY one protocol:\nDECISION: RUN\nOR\nDECISION: DONE\nSUMMARY: \nOR\nDECISION: CONTINUE\nPATCH:\n<<<\nFIND\n\n===\nREPLACE\n\n>>>\n(You may repeat <<< blocks.)\n","f":"\n\n--- explore attempt ---\n"} * *`a` = > json.get value=`esc` key=a * *`b` = > json.get value=`esc` key=b * *`c` = > json.get value=`esc` key=c * *`d` = > json.get value=`esc` key=d * -*`e` = > json.get value=`esc` key=e * *`f` = > json.get value=`esc` key=f * -*`p` = You develop and revise Marqdo workbooks with surgical patches. * +1. `phase` == decompose + *`e` = > json.get value=`esc` key=e_dec * + *`p` = You decompose the workbook before the first run. * +2. * + *`e` = > json.get value=`esc` key=e_rev * + *`p` = You develop and revise Marqdo workbooks with surgical patches. * *`p` = `p` + `a` + `up` + `b` + `goal_s` + `c` + `obs_s` + `d` + `skill` + `e` * 1. `explore_attempt` *`p` = `p` + `f` * @@ -342,6 +351,59 @@ Parent developer-agent prompt: observe → decide DONE or CONTINUE with exact FI --- +## plan_echo_decompose + + `workbook` + + `stream`=False + + `echo`=False + +TTY marker for pre-run parent decompose (before any child spawn). + +1. `stream` + 1. `echo` + > print text=plan:decompose + 2. * + *`_` = 1* +2. * + *`_` = 1* + +**** + +## plan_echo_await + + `round` + + `workbook` + + `stream`=False + + `echo`=False + +TTY-only marker before `await_workbook` so `stream+echo` is not silent during the (often long) child run. + +1. `stream` + 1. `echo` + > print text=plan:await + > print text=`round` + 2. * + *`_` = 1* +2. * + *`_` = 1* + +**** + +## workbook_has_worker_step + + `path` + +True when source still contains `worker.step` or `.单步`. + +*`src` = > fs.read_text path=`path` * +*`a` = > split value=`src` sep=worker.step * +*`na` = > len value=`a` * +*`b` = > split value=`src` sep=.单步 * +*`nb` = > len value=`b` * +1. `na` > 1 + **1** +2. `nb` > 1 + **1** +3. * + **0** + ## plan_append_round + `events` + `round` @@ -391,16 +453,18 @@ Append a `round` event when `stream=True`. Optional `echo` prints a short TTY ma + `from` + `stream`=False -Bubble parent `complete stream=True` deltas (and errors) into the plan event list. Skip nested `done` so plan owns the final `done`. +Bubble parent `complete stream=True` reasoning/deltas (and errors) into the plan event list. Skip nested `done` so plan owns the final `done`. 1. `stream` - [`ev`](`from`) *`t` = > json.get value=`ev` key=type * 1. `t` == delta *`events` = > json.append list=`events` item=`ev` * - 2. `t` == error + 2. `t` == reasoning *`events` = > json.append list=`events` item=`ev` * - 3. * + 3. `t` == error + *`events` = > json.append list=`events` item=`ev` * + 4. * *`_` = 1* 2. * *`_` = 1* @@ -570,7 +634,7 @@ With `stream=True`, the model call uses SSE; `echo=True` prints delta text to st Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/.mq.md`. While task file count `< explore_n` and skill is not llm_free, force a new explore variant under `kb_dir/explore//`. Code-first: llm_free hits skip parent LLM. File children return via `# main`; `plan` exposes that as `result`. -With `stream=True`, emit `round` / parent `delta` / `decision` / `done` on the returned map as `events` (final `result` unchanged). `echo=True` prints round markers and parent deltas. `trace=True` writes events to writeback slot `trace`. Quiet child subtasks stay quiet. +Non-hit path runs parent **decompose** before the first child spawn (`DECISION: RUN` / `CONTINUE`+patch / solidified `DONE`). Then `await` → revise loop. With `stream=True`, emit parent `delta` / `decision` / `round` / `done` on `events`. `echo=True` prints `plan:decompose` / `plan:await` / deltas. `trace=True` writes events to writeback slot `trace`. Quiet child subtasks stay quiet. *`tools` = > json.get value=`self` key=tools * *`cache` = miss* @@ -751,9 +815,58 @@ With `stream=True`, emit `round` / parent `delta` / `decision` / `done` on the r *`summary` = None* *`status` = error* *`err` = max_rounds exhausted * -1. `improve` - *`left` = 1* +*`skip_loop` = None* + +> plan_echo_decompose workbook=`path` stream=`stream` echo=`echo` +*`last_obs` = > inspect_workbook path=`path` * +*`ctx` = > build_plan_context agent=`self` goal=`goal` observation=`last_obs` explore_attempt=`explore_attempt` explore_n=`explore_n` phase=decompose * +1. `stream` + *`evs` = > `model`.complete prompt=`ctx` stream=True echo=`echo` * + *`last_reply` = > llm.stream_result events=`evs` * + *`events` = > plan_merge_deltas events=`events` from=`evs` stream=`stream` * 2. * + *`last_reply` = > `model`.complete prompt=`ctx` * +*`last_reply` = > trim value=`last_reply` * +*`dec` = > extract_plan_decision reply=`last_reply` * +*`events` = > plan_append_decision events=`events` decision=`dec` stream=`stream` * + +1. `dec` == CONTINUE + *`n` = > fs.apply_patch_blocks path=`path` text=`last_reply` * + *`_` = 1* +2. `dec` == RUN + *`_` = 1* +3. `dec` == DONE + *`has_step` = > workbook_has_worker_step path=`path` * + 1. `has_step` + *`_` = 1* + 2. * + > plan_echo_await round=1 workbook=`path` stream=`stream` echo=`echo` + *`aw` = > await_workbook path=`path` * + *`code` = > json.get value=`aw` key=code * + *`child_val` = > json.get value=`aw` key=value * + *`last_obs` = > json.get value=`aw` key=observation * + *`round` = 1* + *`events` = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` stream=`stream` echo=`echo` * + 1. `code` == 0 + *`done` = 1* + *`status` = ok* + *`summary` = > extract_plan_summary reply=`last_reply` * + *`err` = None* + *`skip_loop` = 1* + 2. * + *`_` = 1* +4. * + *`done` = 1* + *`status` = error* + *`err` = unrecognized plan decision * + *`summary` = `last_reply`* + *`skip_loop` = 1* + +1. `skip_loop` + *`left` = 0* +2. `improve` + *`left` = 1* +3. * *`left` = `max_rounds`* - `left` > 0 @@ -761,12 +874,13 @@ With `stream=True`, emit `round` / parent `delta` / `decision` / `done` on the r *`left` = 0* 2. * *`round` = `round` + 1* + > plan_echo_await round=`round` workbook=`path` stream=`stream` echo=`echo` *`aw` = > await_workbook path=`path` * *`code` = > json.get value=`aw` key=code * *`child_val` = > json.get value=`aw` key=value * *`last_obs` = > json.get value=`aw` key=observation * *`events` = > plan_append_round events=`events` round=`round` workbook=`path` exit_code=`code` stream=`stream` echo=`echo` * - *`ctx` = > build_plan_context agent=`self` goal=`goal` observation=`last_obs` explore_attempt=`explore_attempt` explore_n=`explore_n` * + *`ctx` = > build_plan_context agent=`self` goal=`goal` observation=`last_obs` explore_attempt=`explore_attempt` explore_n=`explore_n` phase=revise * 1. `stream` *`evs` = > `model`.complete prompt=`ctx` stream=True echo=`echo` * *`last_reply` = > llm.stream_result events=`evs` * @@ -793,7 +907,9 @@ With `stream=True`, emit `round` / parent `delta` / `decision` / `done` on the r *`err` = no patches applied * *`summary` = > extract_plan_summary reply=`last_reply` * *`left` = 0* - 3. * + 3. `dec` == RUN + *`left` = `left` - 1* + 4. * *`done` = 1* *`status` = error* *`err` = unrecognized plan decision * diff --git a/man-test/ext/agent-plan.mq.md b/man-test/ext/agent-plan.mq.md index c968f84..802b8f0 100644 --- a/man-test/ext/agent-plan.mq.md +++ b/man-test/ext/agent-plan.mq.md @@ -17,6 +17,9 @@ description: Full plan loop — spawn workbook, inspect; simple pong goal. *`tools` = > json.parse text=[] * *`助手` = > agent.agent model=`model` tools=`tools` standing=You are a Marqdo agent-development master. Prefer DONE when the workbook already satisfies the goal. * -*`out` = > `助手`.plan goal=`goal` max_rounds=3 writeback=False * +流式开在 plan 上:非命中先 plan:decompose(父分解增量),再 plan:await(子 quiet),然后父修订轮。 -> print `out` +*`out` = > `助手`.plan goal=`goal` max_rounds=3 writeback=False stream=True echo=True force=True * + +*`result` = > json.get value=`out` key=result * +> print text=`result` diff --git a/src/bytecode/vm.rs b/src/bytecode/vm.rs index c579865..82f270d 100644 --- a/src/bytecode/vm.rs +++ b/src/bytecode/vm.rs @@ -237,8 +237,18 @@ impl Vm { Op::Input => { let prompt = pop(&mut stack).map_err(|m| self.err_at(span, m))?; let text = prompt.as_display(); - self.emit_prompt(&text); - let line = self.input.read_line().map_err(|e| { + let line = if self.capture { + self.emit_prompt(&text); + self.input.read_line() + } else if crate::input_feed::stdin_is_tty() { + self.input + .read_line_with_prompt(&text) + .map(|(line, _)| line) + } else { + self.emit_prompt(&text); + self.input.read_line() + } + .map_err(|e| { let msg = e.to_string(); if msg.contains("input needs a line") { self.err_at(span, msg) diff --git a/src/host/net.rs b/src/host/net.rs index 7bec35d..1cdf89a 100644 --- a/src/host/net.rs +++ b/src/host/net.rs @@ -215,12 +215,76 @@ fn event_map(pairs: Vec<(&str, Value)>) -> Value { ) } +/// Push stream events from one OpenAI-compatible SSE JSON object. +/// DeepSeek (and similar) may stream chain-of-thought in `delta.reasoning_content` +/// while the final answer arrives later in `delta.content`. +/// Returns `Err` only on JSON issues handled by caller; `Ok(true)` means stop (error frame). +fn push_openai_delta_events( + v: &serde_json::Value, + events: &mut Vec, + result: &mut String, + echoed: &mut bool, + saw_reasoning: &mut bool, + echo: bool, +) -> bool { + if let Some(msg) = v.get("error").and_then(|e| { + e.get("message") + .and_then(|m| m.as_str()) + .or_else(|| e.as_str()) + }) { + events.push(event_map(vec![ + ("type", Value::Text("error".into())), + ("message", Value::Text(msg.into())), + ])); + return true; + } + let reasoning = v + .pointer("/choices/0/delta/reasoning_content") + .and_then(|c| c.as_str()) + .unwrap_or(""); + if !reasoning.is_empty() { + *saw_reasoning = true; + if echo { + print!("{reasoning}"); + let _ = std::io::stdout().flush(); + *echoed = true; + } + events.push(event_map(vec![ + ("type", Value::Text("reasoning".into())), + ("text", Value::Text(reasoning.into())), + ])); + } + let content = v + .pointer("/choices/0/delta/content") + .and_then(|c| c.as_str()) + .unwrap_or(""); + if !content.is_empty() { + if echo && *saw_reasoning && result.is_empty() { + println!(); + *echoed = true; + } + result.push_str(content); + if echo { + print!("{content}"); + let _ = std::io::stdout().flush(); + *echoed = true; + } + events.push(event_map(vec![ + ("type", Value::Text("delta".into())), + ("text", Value::Text(content.into())), + ])); + } + false +} + /// Map OpenAI-compatible chat SSE payloads → Marqdo stream events -/// (`delta` / `done` / `error`). Optional `echo` prints delta text to stdout as it arrives. +/// (`reasoning` / `delta` / `done` / `error`). Optional `echo` prints text to stdout as it arrives. pub fn openai_chat_sse_events(text: &str, echo: bool) -> Result, String> { let mut events = Vec::new(); let mut result = String::new(); let mut saw_done = false; + let mut echoed = false; + let mut saw_reasoning = false; for data in sse_data_payloads(text) { let data = data.trim(); if data.is_empty() { @@ -232,34 +296,18 @@ pub fn openai_chat_sse_events(text: &str, echo: bool) -> Result, Stri } let v: serde_json::Value = serde_json::from_str(data) .map_err(|e| format!("openai sse json: {e}"))?; - if let Some(msg) = v.get("error").and_then(|e| { - e.get("message") - .and_then(|m| m.as_str()) - .or_else(|| e.as_str()) - }) { - events.push(event_map(vec![ - ("type", Value::Text("error".into())), - ("message", Value::Text(msg.into())), - ])); + if push_openai_delta_events( + &v, + &mut events, + &mut result, + &mut echoed, + &mut saw_reasoning, + echo, + ) { return Ok(events); } - let content = v - .pointer("/choices/0/delta/content") - .and_then(|c| c.as_str()) - .unwrap_or(""); - if !content.is_empty() { - result.push_str(content); - if echo { - print!("{content}"); - let _ = std::io::stdout().flush(); - } - events.push(event_map(vec![ - ("type", Value::Text("delta".into())), - ("text", Value::Text(content.into())), - ])); - } } - if echo && !result.is_empty() { + if echo && echoed { println!(); } if !saw_done && events.is_empty() && result.is_empty() { @@ -296,10 +344,14 @@ fn read_sse_body_to_events( let mut events = Vec::new(); let mut result = String::new(); let mut data_buf: Vec = Vec::new(); + let mut echoed = false; + let mut saw_reasoning = false; let flush_data = |data_buf: &mut Vec, events: &mut Vec, result: &mut String, + echoed: &mut bool, + saw_reasoning: &mut bool, echo: bool| -> Result { if data_buf.is_empty() { @@ -316,33 +368,14 @@ fn read_sse_body_to_events( } let v: serde_json::Value = serde_json::from_str(data).map_err(|e| format!("openai sse json: {e}"))?; - if let Some(msg) = v.get("error").and_then(|e| { - e.get("message") - .and_then(|m| m.as_str()) - .or_else(|| e.as_str()) - }) { - events.push(event_map(vec![ - ("type", Value::Text("error".into())), - ("message", Value::Text(msg.into())), - ])); - return Ok(true); - } - let content = v - .pointer("/choices/0/delta/content") - .and_then(|c| c.as_str()) - .unwrap_or(""); - if !content.is_empty() { - result.push_str(content); - if echo { - print!("{content}"); - let _ = std::io::stdout().flush(); - } - events.push(event_map(vec![ - ("type", Value::Text("delta".into())), - ("text", Value::Text(content.into())), - ])); - } - Ok(false) + Ok(push_openai_delta_events( + &v, + events, + result, + echoed, + saw_reasoning, + echo, + )) }; loop { @@ -360,7 +393,14 @@ fn read_sse_body_to_events( let line = buf.strip_suffix('\n').unwrap_or(&buf); let line = line.strip_suffix('\r').unwrap_or(line); if line.is_empty() { - if flush_data(&mut data_buf, &mut events, &mut result, echo)? { + if flush_data( + &mut data_buf, + &mut events, + &mut result, + &mut echoed, + &mut saw_reasoning, + echo, + )? { break; } continue; @@ -370,8 +410,15 @@ fn read_sse_body_to_events( data_buf.push(payload.to_string()); } } - let _ = flush_data(&mut data_buf, &mut events, &mut result, echo)?; - if echo && !result.is_empty() { + let _ = flush_data( + &mut data_buf, + &mut events, + &mut result, + &mut echoed, + &mut saw_reasoning, + echo, + )?; + if echo && echoed { println!(); } if events.iter().any(|e| matches!(e, Value::Map(m) if m.iter().any(|(k,v)| k == "type" && matches!(v, Value::Text(t) if t == "error")))) { @@ -486,4 +533,31 @@ data: [DONE]\n\ _ => panic!("expected done map"), } } + + #[test] + fn openai_sse_reasoning_then_content() { + let fixture = "\ +data: {\"choices\":[{\"delta\":{\"reasoning_content\":\"think\"}}]}\n\ +\n\ +data: {\"choices\":[{\"delta\":{\"content\":\"Hi\"}}]}\n\ +\n\ +data: [DONE]\n\ +"; + let ev = openai_chat_sse_events(fixture, false).unwrap(); + assert_eq!(ev.len(), 3); + match &ev[0] { + Value::Map(m) => { + assert!(m.iter().any(|(k, v)| k == "type" && matches!(v, Value::Text(t) if t == "reasoning"))); + assert!(m.iter().any(|(k, v)| k == "text" && matches!(v, Value::Text(t) if t == "think"))); + } + _ => panic!("expected reasoning map"), + } + match &ev[2] { + Value::Map(m) => { + assert!(m.iter().any(|(k, v)| k == "type" && matches!(v, Value::Text(t) if t == "done"))); + assert!(m.iter().any(|(k, v)| k == "result" && matches!(v, Value::Text(t) if t == "Hi"))); + } + _ => panic!("expected done map"), + } + } } diff --git a/src/input_feed.rs b/src/input_feed.rs index 895e270..544f172 100644 --- a/src/input_feed.rs +++ b/src/input_feed.rs @@ -1,8 +1,9 @@ //! Preset stdin lines for `input` (CLI `--stdin-file`, view form, frontmatter, tests). +//! Interactive TTY path uses Unicode-aware line editing (char backspace + redraw). use std::collections::VecDeque; use std::fs; -use std::io::{self, BufRead}; +use std::io::{self, BufRead, Read, Write}; use std::path::Path; use anyhow::{Context, Result}; @@ -144,6 +145,159 @@ fn strip_block_indent(line: &str) -> String { s.trim_end_matches('\r').to_string() } +pub fn stdin_is_tty() -> bool { + #[cfg(unix)] + { + unsafe { libc::isatty(libc::STDIN_FILENO) == 1 } + } + #[cfg(not(unix))] + { + false + } +} + +/// Piped / non-TTY: read raw bytes to newline (avoids mid-frame UTF-8 panic), lossy decode. +fn read_stdin_line_piped() -> Result { + let mut buf = Vec::new(); + io::stdin().lock().read_until(b'\n', &mut buf)?; + if buf.last() == Some(&b'\n') { + buf.pop(); + } + if buf.last() == Some(&b'\r') { + buf.pop(); + } + Ok(String::from_utf8_lossy(&buf).into_owned()) +} + +/// Ensure prompt has a trailing space so typed text does not glue to the last glyph. +fn normalize_prompt(prompt: &str) -> String { + if prompt.is_empty() { + return String::new(); + } + if prompt.ends_with(|c: char| c.is_whitespace()) { + prompt.to_string() + } else { + format!("{prompt} ") + } +} + +#[cfg(unix)] +struct TermGuard { + fd: i32, + original: libc::termios, +} + +#[cfg(unix)] +impl TermGuard { + fn enter_cbreak() -> Result { + let fd = libc::STDIN_FILENO; + let mut original = unsafe { std::mem::zeroed::() }; + if unsafe { libc::tcgetattr(fd, &mut original) } != 0 { + return Err(io::Error::last_os_error()).context("tcgetattr"); + } + let mut raw = original; + raw.c_lflag &= !(libc::ICANON | libc::ECHO); + raw.c_cc[libc::VMIN] = 1; + raw.c_cc[libc::VTIME] = 0; + if unsafe { libc::tcsetattr(fd, libc::TCSANOW, &raw) } != 0 { + return Err(io::Error::last_os_error()).context("tcsetattr"); + } + Ok(Self { fd, original }) + } +} + +#[cfg(unix)] +impl Drop for TermGuard { + fn drop(&mut self) { + unsafe { + libc::tcsetattr(self.fd, libc::TCSANOW, &self.original); + } + } +} + +fn redraw_line(prompt: &str, buf: &str) -> Result<()> { + let mut out = io::stdout().lock(); + // CR + clear whole line, then reprint (fixes CJK backspace display desync). + write!(out, "\r\x1b[2K{prompt}{buf}")?; + out.flush()?; + Ok(()) +} + +/// Interactive line editor: UTF-8 char backspace + full-line redraw. +#[cfg(unix)] +fn read_stdin_line_interactive(prompt: &str) -> Result { + let prompt = normalize_prompt(prompt); + let _guard = TermGuard::enter_cbreak()?; + redraw_line(&prompt, "")?; + + let mut buf = String::new(); + let mut pending = Vec::new(); + let mut stdin = io::stdin().lock(); + let mut byte = [0u8; 1]; + + loop { + let n = stdin.read(&mut byte)?; + if n == 0 { + // EOF + writeln!(io::stdout())?; + return Ok(buf); + } + let b = byte[0]; + match b { + b'\n' | b'\r' => { + writeln!(io::stdout())?; + return Ok(buf); + } + 0x7f | 0x08 => { + // Backspace / BS — delete one Unicode scalar, not one byte. + if buf.pop().is_some() { + redraw_line(&prompt, &buf)?; + } + } + 0x03 => { + // Ctrl-C + writeln!(io::stdout())?; + anyhow::bail!("interrupted"); + } + 0x04 => { + // Ctrl-D + if buf.is_empty() && pending.is_empty() { + writeln!(io::stdout())?; + return Ok(buf); + } + } + b if b < 0x20 => { + // ignore other controls + } + b => { + pending.push(b); + match std::str::from_utf8(&pending) { + Ok(s) => { + buf.push_str(s); + pending.clear(); + redraw_line(&prompt, &buf)?; + } + Err(e) if e.error_len().is_none() => { + // incomplete UTF-8 sequence — wait for more bytes + } + Err(_) => { + // invalid — drop and continue + pending.clear(); + } + } + } + } + } +} + +#[cfg(not(unix))] +fn read_stdin_line_interactive(prompt: &str) -> Result { + let prompt = normalize_prompt(prompt); + print!("{prompt}"); + io::stdout().flush()?; + read_stdin_line_piped() +} + #[derive(Debug, Clone, Default)] pub struct InputFeed { queue: VecDeque, @@ -159,6 +313,7 @@ impl InputFeed { } /// Next line from the preset queue, or real stdin when not capturing. + /// Prefer [`Self::read_line_with_prompt`] for interactive TTY (Unicode backspace). pub fn read_line(&mut self) -> Result { if let Some(line) = self.queue.pop_front() { return Ok(line); @@ -168,15 +323,23 @@ impl InputFeed { "input needs a line (frontmatter stdin:/输入:, view Preset input, or --stdin-file)" ); } - let mut line = String::new(); - io::stdin().lock().read_line(&mut line)?; - if line.ends_with('\n') { - line.pop(); - if line.ends_with('\r') { - line.pop(); - } + read_stdin_line_piped() + } + + /// Read a line; on a TTY, run Unicode-aware editing and draw `prompt` itself. + /// Returns `Ok((line, painted_prompt))` where `painted_prompt` is true when this + /// function already printed the prompt (caller must not `emit_prompt` again). + pub fn read_line_with_prompt(&mut self, prompt: &str) -> Result<(String, bool)> { + if let Some(line) = self.queue.pop_front() { + return Ok((line, false)); } - Ok(line) + if self.capture { + anyhow::bail!( + "input needs a line (frontmatter stdin:/输入:, view Preset input, or --stdin-file)" + ); + } + let line = read_stdin_line_interactive(prompt)?; + Ok((line, true)) } } @@ -232,4 +395,19 @@ mod tests { vec!["a".to_string(), String::new(), "b".to_string()] ); } + + #[test] + fn normalize_prompt_adds_space() { + assert_eq!(normalize_prompt("问?"), "问? "); + assert_eq!(normalize_prompt("问? "), "问? "); + assert_eq!(normalize_prompt(""), ""); + } + + #[test] + fn utf8_pop_removes_full_char() { + let mut s = String::from("帮我规划"); + assert_eq!(s.pop(), Some('划')); + assert_eq!(s.pop(), Some('规')); + assert_eq!(s, "帮我"); + } } diff --git a/src/interp.rs b/src/interp.rs index fe7e920..9f2e687 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -503,8 +503,18 @@ impl Interpreter { .get("prompt") .map(|v| v.as_display()) .unwrap_or_default(); - self.emit_prompt(&prompt); - let line = self.input.read_line().map_err(|e| { + let line = if self.capture { + self.emit_prompt(&prompt); + self.input.read_line() + } else if crate::input_feed::stdin_is_tty() { + self.input + .read_line_with_prompt(&prompt) + .map(|(line, _)| line) + } else { + self.emit_prompt(&prompt); + self.input.read_line() + } + .map_err(|e| { let msg = e.to_string(); if msg.contains("input needs a line") { self.err(msg) diff --git a/tests/ext/agent-plan-decision.mq.md b/tests/ext/agent-plan-decision.mq.md index afec892..dc9bb29 100644 --- a/tests/ext/agent-plan-decision.mq.md +++ b/tests/ext/agent-plan-decision.mq.md @@ -8,7 +8,7 @@ description: extract_plan_decision/summary; dual skeleton contains research+writ # main -*`samples` = > json.parse text={"done":"DECISION: DONE\nSUMMARY: all good\n","cont":"DECISION: CONTINUE\nPATCH:\n<<<\nFIND\nold\n===\nREPLACE\nnew\n>>>\n","zh":"决定:完成\n汇总:好了\n"} * +*`samples` = > json.parse text={"done":"DECISION: DONE\nSUMMARY: all good\n","cont":"DECISION: CONTINUE\nPATCH:\n<<<\nFIND\nold\n===\nREPLACE\nnew\n>>>\n","zh":"决定:完成\n汇总:好了\n","run":"DECISION: RUN\n","zh_run":"决定:运行\n"} * *`d1` = > json.get value=`samples` key=done * *`r1` = > agent.extract_plan_decision reply=`d1` * > print text=`r1` @@ -23,6 +23,14 @@ description: extract_plan_decision/summary; dual skeleton contains research+writ *`r3` = > agent.extract_plan_decision reply=`d3` * > print text=`r3` +*`d4` = > json.get value=`samples` key=run * +*`r4` = > agent.extract_plan_decision reply=`d4` * +> print text=`r4` + +*`d5` = > json.get value=`samples` key=zh_run * +*`r5` = > agent.extract_plan_decision reply=`d5` * +> print text=`r5` + *`dual` = > agent.render_workbook_skeleton goal=demo skeleton=dual * *`has_r` = > split value=`dual` sep=research * *`n_r` = > len value=`has_r` * diff --git a/tests/ext/llm-stream-live.mq.md b/tests/ext/llm-stream-live.mq.md new file mode 100644 index 0000000..10aeede --- /dev/null +++ b/tests/ext/llm-stream-live.mq.md @@ -0,0 +1,19 @@ +--- +title: ext llm DeepSeek stream live +description: Live SSE complete with echo; credentials from tests/ext/.env. +> ext/ai/llm.mq.md +> lib/json.mq.md +--- + +# main + +> llm.load_env path=.env + +*`model` = > llm.llm * + +*`events` = > `model`.complete prompt=Reply with exactly one English word: pong stream=True echo=True * +*`n` = > len value=`events` * +> print text=`n` + +*`answer` = > llm.stream_result events=`events` * +> print text=`answer` diff --git a/tests/gold.rs b/tests/gold.rs index e8664e6..a478d3d 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -1241,7 +1241,7 @@ fn ext_agent_plan_confirm() { fn ext_agent_plan_decision() { assert_out( "tests/ext/agent-plan-decision.mq.md", - "DONE\nall good\nCONTINUE\nDONE\ndual-ok\nsolidify-ok\n1\nkeep new keep", + "DONE\nall good\nCONTINUE\nDONE\nRUN\nRUN\ndual-ok\nsolidify-ok\n1\nkeep new keep", ); } @@ -1262,6 +1262,33 @@ fn ext_llm_complete_live() { ); } +#[test] +fn ext_llm_stream_live() { + let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) + .args(["run", "tests/ext/llm-stream-live.mq.md"]) + .current_dir(env!("CARGO_MANIFEST_DIR")) + .output() + .expect("run llm-stream-live"); + let code = output.status.code().unwrap_or(1); + let stdout = String::from_utf8_lossy(&output.stdout); + let stderr = String::from_utf8_lossy(&output.stderr); + assert_eq!(code, 0, "llm-stream-live stderr={stderr}"); + let lines: Vec<&str> = stdout.trim_end().lines().collect(); + assert!( + lines.len() >= 2, + "llm-stream-live expected echo line + count + answer, got {stdout:?}" + ); + let answer = lines.last().unwrap().to_lowercase(); + assert!( + answer.contains("pong"), + "expected pong in streamed answer, got {stdout:?}" + ); + let n: usize = lines[lines.len() - 2] + .parse() + .unwrap_or(0); + assert!(n >= 2, "expected at least delta+done events, n={n} stdout={stdout:?}"); +} + #[test] fn ext_agent_run_live() { let status = Command::new("cargo") @@ -1373,6 +1400,20 @@ Hello", ); } +#[test] +fn lib_net_openai_sse_reasoning() { + assert_out( + "tests/lib/net-openai-sse-reasoning.mq.md", + "3 +reasoning +think +delta +Hi +done +Hi", + ); +} + #[test] fn ext_llm_stream_offline() { assert_out("tests/ext/llm-stream-offline.mq.md", "Hi!"); diff --git a/tests/lib/net-openai-sse-reasoning.mq.md b/tests/lib/net-openai-sse-reasoning.mq.md new file mode 100644 index 0000000..735ee0c --- /dev/null +++ b/tests/lib/net-openai-sse-reasoning.mq.md @@ -0,0 +1,32 @@ +--- +title: openai SSE reasoning_content offline +description: DeepSeek-style thinking streams as type=reasoning; answer stays delta/done.result. +> lib/net.mq.md +> lib/json.mq.md +--- + +# main + +*`fixture` = "data: {\"choices\":[{\"delta\":{\"reasoning_content\":\"think\"}}]}\n\ndata: {\"choices\":[{\"delta\":{\"content\":\"Hi\"}}]}\n\ndata: [DONE]\n" * + +*`events` = > net.openai_sse_parse text=`fixture` * +*`n` = > len value=`events` * +> print text=`n` + +*`e0` = > at value=`events` index=0 * +*`t0` = > json.get value=`e0` key=type * +*`x0` = > json.get value=`e0` key=text * +> print text=`t0` +> print text=`x0` + +*`e1` = > at value=`events` index=1 * +*`t1` = > json.get value=`e1` key=type * +*`x1` = > json.get value=`e1` key=text * +> print text=`t1` +> print text=`x1` + +*`e2` = > at value=`events` index=2 * +*`t2` = > json.get value=`e2` key=type * +*`r2` = > json.get value=`e2` key=result * +> print text=`t2` +> print text=`r2`