Implement streaming S0-S2: SSE complete, step/plan events.

Add net/llm SSE primitives and optional stream/echo/trace on step and plan so callers can watch deltas and rounds without changing final return values.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-10 11:03:04 +08:00
1 parent 5456d9ff83
commit b6bb42e460
18 files changed
+809 -64

No files matched your search

+1 -1
View File
@@ -52,7 +52,7 @@
| [roadmap/interpreter.md](roadmap/interpreter.md) | 正经解释器路线图 |
| [roadmap/next-phase.md](roadmap/next-phase.md) | **下一阶段:input / 诊断 / 标准库 / view 调试** |
| [roadmap/okf-near-match.md](roadmap/okf-near-match.md) | **规划:OKF 相近任务命中**(精确 sig 之外的可选软复用) |
| [roadmap/agent-streaming.md](roadmap/agent-streaming.md) | **规划:智能体 / LLM 流式输出**(事件模型 + step/plan 透传) |
| [roadmap/agent-streaming.md](roadmap/agent-streaming.md) | **流式输出**(SSE + `complete`/`step` stream;S0–S1 已落地) |
| [roadmap/object-inheritance.md](roadmap/object-inheritance.md) | **对象继承**(`# 子类 = > 父类`;I0–I2 已落地) |
| [roadmap/tables-maps-footnotes.md](roadmap/tables-maps-footnotes.md) | **表格语义**(竖表 / 横表字典 / 脚注取元 / 行向记录;T0–T4 已落地) |
| [roadmap/ext-web.md](roadmap/ext-web.md) | **规划:`ext/web` 动态网页**(env/DB init·迁移·样式脚手架·ABI) |
+1 -1
View File
@@ -346,4 +346,4 @@ lib/subtask:spawn fn=<名> → wait
**智能体框架必须让编排看起来像文档、跑起来像程序、留痕像知识库。**
单步解决原子问题(默认写回);多步让父智能体编写并修订子 Marqdo 工作簿、经子任务执行、把确定步骤固化为代码——详见 [ext-agent-plan.md](ext-agent-plan.md)。
流式呈现思考与中间输出(不替代返回值 / 写回)见规划 [agent-streaming.md](../roadmap/agent-streaming.md)。
流式:`## step` / `## plan` 支持 `stream=True` / `echo=True`(SSE 增量;返回 map 不变;`plan` 另附 `events`,可选 `trace` 写回)。见 [agent-streaming.md](../roadmap/agent-streaming.md)。
+13 -2
View File
@@ -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` / agent process events) is **planned**, not shipped — see [agent-streaming.md](../roadmap/agent-streaming.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).
**Note:** Prefer `marqdo ext add llm`. Import `> ext/ai/llm.mq.md` from repo or install root.
@@ -37,8 +37,9 @@ Streaming (`complete` / agent process events) is **planned**, not shipped — se
| English | Chinese | Role |
|---------|---------|------|
| `## load_env` | `## 加载环境` | Free function: load `.env` |
| `## stream_result` | `## 流式结果` | Collapse event list → final text |
| `# llm` | `# 大模型` | Object ctor → handle map |
| `## complete` / `## chat` | `## 运行` / `## 聊天` | Methods on the handle |
| `## complete` / `## chat` | `## 运行` / `## 聊天` | Methods; optional `stream` / `流式`, `echo` / `打印增量` |
```markdown
---
@@ -52,6 +53,15 @@ Streaming (`complete` / agent process events) is **planned**, not shipped — se
*`model` = > llm *
*`reply` = > `model`.complete prompt=Say hi in one word *
> print text=`reply`
*`events` = > `model`.complete prompt=Say hi stream=True *
- [`ev`](`events`)
*`t` = > json.get value=`ev` key=type *
1. `t` == "delta"
*`chunk` = > json.get value=`ev` key=text *
> print text=`chunk`
2. *
*`_` = 1*
```
Environment (create a **project-local** `.env`; do not commit secrets):
@@ -67,6 +77,7 @@ Fallbacks: `MARQDO_LLM_API_KEY`, `MARQDO_LLM_BASE_URL`, `MARQDO_LLM_MODEL`.
## Tests
- Import smoke: `tests/ext/llm-import.mq.md`
- Offline stream: `tests/ext/llm-stream-offline.mq.md` + `tests/lib/net-openai-sse.mq.md`
- Live complete (local): `tests/ext/llm-complete.mq.md` + `tests/ext/.env`
- Live agent `## step`: `tests/ext/agent-run-live.mq.md` (DeepSeek; gold: `ext_agent_run_live`)
- Agent smoke (real `llm` handle): `tests/ext/agent-smoke.mq.md` (gold: `ext_agent_framework_smoke`)
+25 -11
View File
@@ -2,8 +2,8 @@
| | |
|---|---|
| 状态 | **规划 / 未开工** |
| 日期 | 2026-08-09 |
| 状态 | **S0–S2 已落地**(S3 view 仍待做) |
| 日期 | 2026-08-10 |
| 相关 | [ext-llm.md](../design/ext-llm.md) · [ext-agent.md](../design/ext-agent.md) · [ext-agent-plan.md](../design/ext-agent-plan.md) · [stdlib-subtask.md](../design/stdlib-subtask.md) · [view.md](../design/view.md) |
| 痛点 | `complete` / `step` / `plan` 均为整段返回;用户看不到思考与中间输出 |
@@ -132,12 +132,24 @@ plan 特有:
## 5. 推荐落地顺序
```text
S0 net/llm:SSE 读 + complete stream=True + 事件 foreach (基础)
S1 step stream=True 透传 delta / tool_* (单步体验)
S2 plan stream=True:round + 父 delta;trace 写回可选 (多步体验)
S3 view 订阅 (可视化)
S0 net/llm:SSE 读 + complete stream=True + 事件 foreach ✅
S1 step stream=True(echo 增量;返回 map 不变) ✅
S2 plan stream=True:round + 父 delta;trace 写回可选 ✅
S3 view 订阅 (待做)
```
### 开放点决议(S0)
1. **Eager 事件列表**(非惰性句柄):`complete stream=True` 返回 `List` of maps,可用今日 foreach。
2. **默认不自动 print**:`echo=False`;需要边读边打时传 `echo=True` / `打印增量=真`。
3. 中文:`## 运行` 用 `流式=` / `打印增量=`。
### 开放点决议(S2)
1. `plan` / `多步` 增加 `stream` / `echo` / `trace`(中文 `流式` / `打印增量` / `轨迹`)。
2. 事件挂在返回 map 的 `events`;父 `complete` 只冒泡 `delta`/`error`(嵌套 `done` 不进列表)。
3. `trace=True` → 写回槽 `trace`(与 TTY `echo` 可分立)。
验收:
1. 未传 `stream` 的金样零 diff。
@@ -147,10 +159,12 @@ S3 view 订阅 (可视化)
---
## 6. 开放点(开工前锁定)
## 6. 开放点
1. 事件集合是「Eager 列表」还是「惰性迭代句柄」(影响内存与语法)。
2. `delta` 默认是否自动 `print`(便利 vs 双份输出)。
3. 中文面方法名:`## 运行` 是否共用 `stream=` / `流式=`。
1. ~~Eager vs 惰性~~ → S0 选 eager;惰性句柄可后补。
2. ~~delta 默认 print~~ → 默认关;`echo=True` 可选。
3. ~~中文面~~ → `流式=` / `打印增量=`。
4. ~~S2~~ → `plan stream=True`:`round` / 父 delta / `decision` / `done`;可选 `trace`。
5. **S3**:`marqdo view` 订阅同一事件模型。
本文不排期;S0 可与 [okf-near-match.md](okf-near-match.md) 并行,互不阻塞。
S0 可与 [okf-near-match.md](okf-near-match.md) 并行,互不阻塞。
+133 -4
View File
@@ -342,6 +342,101 @@ Parent developer-agent prompt: observe → decide DONE or CONTINUE with exact FI
---
## plan_append_round
+ `events`
+ `round`
+ `workbook`
+ `exit_code`
+ `stream`=False
+ `echo`=False
Append a `round` event when `stream=True`. Optional `echo` prints a short TTY marker (not the full workbook path).
1. `stream`
*`ev` = > json.parse text={"type":"round"} *
*`ev` = > json.set map=`ev` key=round value=`round` *
*`ev` = > json.set map=`ev` key=workbook value=`workbook` *
*`ev` = > json.set map=`ev` key=exit_code value=`exit_code` *
*`events` = > json.append list=`events` item=`ev` *
1. `echo`
> print text=plan:round
2. *
*`_` = 1*
2. *
*`_` = 1*
**`events`**
## plan_append_decision
+ `events`
+ `decision`
+ `stream`=False
+ `summary`=None
1. `stream`
*`ev` = > json.parse text={"type":"decision"} *
*`ev` = > json.set map=`ev` key=decision value=`decision` *
1. `summary`
*`ev` = > json.set map=`ev` key=summary value=`summary` *
2. *
*`_` = 1*
*`events` = > json.append list=`events` item=`ev` *
2. *
*`_` = 1*
**`events`**
## plan_merge_deltas
+ `events`
+ `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`.
1. `stream`
- [`ev`](`from`)
*`t` = > json.get value=`ev` key=type *
1. `t` == delta
*`events` = > json.append list=`events` item=`ev` *
2. `t` == error
*`events` = > json.append list=`events` item=`ev` *
3. *
*`_` = 1*
2. *
*`_` = 1*
**`events`**
## plan_finish_stream
+ `out`
+ `events`
+ `stream`=False
+ `trace`=False
+ `result`=None
When `stream=True`, append `done` and attach `events` on the result map. Optional `trace=True` writes the event list to writeback slot `trace`.
1. `stream`
*`ev` = > json.parse text={"type":"done"} *
1. `result`
*`ev` = > json.set map=`ev` key=result value=`result` *
2. *
*`_` = 1*
*`events` = > json.append list=`events` item=`ev` *
*`out` = > json.set map=`out` key=events value=`events` *
2. *
*`_` = 1*
1. `trace`
*`body` = > json.stringify value=`events` *
> writeback.record value=`body` key=trace
2. *
*`_` = 1*
**`out`**
---
# agent
+ `model`
+ `tools`
@@ -380,9 +475,13 @@ Clear the plugin session bag for this agent id.
## step
+ `task`
+ `writeback`=True
+ `stream`=False
+ `echo`=False
One atomic turn: context → LLM → optional tool via subtask. Returns a **map** (`status`, `task`, `decision`, and on success `result` plus optional `tool` / `tool_result`; on failure `error`). By default (`writeback=True`) persists that map under named slots `ok` / `error` at the call site; pass `writeback=False` to skip.
With `stream=True`, the model call uses SSE; `echo=True` prints delta text to stdout as it arrives. The returned map is unchanged (final `result` string).
*`ctx` = > build_step_context agent=`self` task=`task` *
*`id` = > json.get value=`self` key=id *
*`model` = > json.get value=`self` key=model *
@@ -392,7 +491,12 @@ One atomic turn: context → LLM → optional tool via subtask. Returns a **map*
*`user_turn` = > json.set map=`user_turn` key=content value=`task` *
> agent_history_append id=`id` item=`user_turn`
*`reply` = > `model`.complete prompt=`ctx` *
1. `stream`
*`evs` = > `model`.complete prompt=`ctx` stream=True echo=`echo` *
*`reply` = > llm.stream_result events=`evs` *
2. *
*`reply` = > `model`.complete prompt=`ctx` *
*`reply` = > trim value=`reply` *
*`decision` = `reply`*
*`tool_name` = > extract_tool_name reply=`reply` *
@@ -418,7 +522,11 @@ One atomic turn: context → LLM → optional tool via subtask. Returns a **map*
*`fp` = `fp` + ran via subtask and returned: *
*`fp` = `fp` + `tool_s` *
*`fp` = `fp` + . Reply to the user briefly. *
*`reply` = > `model`.complete prompt=`fp` *
1. `stream`
*`evs2` = > `model`.complete prompt=`fp` stream=True echo=`echo` *
*`reply` = > llm.stream_result events=`evs2` *
2. *
*`reply` = > `model`.complete prompt=`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` *
@@ -456,9 +564,14 @@ One atomic turn: context → LLM → optional tool via subtask. Returns a **map*
+ `kb_dir`=.marqdo/agent-kb
+ `improve_every`=3
+ `explore_n`=3
+ `stream`=False
+ `echo`=False
+ `trace`=False
Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/<slug>.mq.md`. While task file count `< explore_n` and skill is not llm_free, force a new explore variant under `kb_dir/explore/<slug>/`. 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.
*`tools` = > json.get value=`self` key=tools *
*`cache` = miss*
*`path` = None*
@@ -466,6 +579,7 @@ Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/<slug>.mq.md
*`explore` = None*
*`explore_attempt` = None*
*`skel_kind` = `skeleton`*
*`events` = > json.parse text=[] *
*`tf` = > agent_kb_task_files kb_dir=`kb_dir` goal=`goal` tools=`tools` *
*`nfiles` = > json.get value=`tf` key=count *
@@ -501,11 +615,13 @@ Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/<slug>.mq.md
*`out` = > json.set map=`out` key=summary value=`sum` *
*`out` = > json.set map=`out` key=observation value=`last_obs` *
*`out` = > json.set map=`out` key=result value=`child_val` *
*`events` = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` stream=`stream` echo=`echo` *
1. `writeback`
*`body` = > json.stringify value=`out` *
> writeback.record value=`body` key=ok
2. *
*`_` = 1*
*`out` = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`child_val` *
**`out`**
2. *
*`path` = None*
@@ -546,11 +662,13 @@ Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/<slug>.mq.md
*`out` = > json.set map=`out` key=summary value=`sum` *
*`out` = > json.set map=`out` key=observation value=`last_obs` *
*`out` = > json.set map=`out` key=result value=`child_val` *
*`events` = > plan_append_round events=`events` round=1 workbook=`path` exit_code=`code` stream=`stream` echo=`echo` *
1. `writeback`
*`body` = > json.stringify value=`out` *
> writeback.record value=`body` key=ok
2. *
*`_` = 1*
*`out` = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`child_val` *
**`out`**
2. *
*`path` = None*
@@ -619,6 +737,7 @@ Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/<slug>.mq.md
> writeback.record value=`body` key=ok
2. *
*`_` = 1*
*`out` = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` *
**`out`**
2. *
*`_` = 1*
@@ -646,10 +765,17 @@ Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/<slug>.mq.md
*`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` *
*`last_reply` = > `model`.complete prompt=`ctx` *
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` == DONE
> agent_workbook_solidify path=`path` observation=`last_obs`
*`done` = 1*
@@ -706,10 +832,12 @@ Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/<slug>.mq.md
1. `child_val`
*`out` = > json.set map=`out` key=result value=`child_val` *
*`plan_result` = `child_val`*
2. `summary`
*`out` = > json.set map=`out` key=result value=`summary` *
*`plan_result` = `summary`*
3. *
*`_` = 1*
*`plan_result` = None*
1. `writeback`
*`body` = > json.stringify value=`out` *
@@ -720,4 +848,5 @@ Multi-step with OKF agent-kb. Default workbook is `kb_dir/resources/<slug>.mq.md
2. *
*`_` = 1*
*`out` = > plan_finish_stream out=`out` events=`events` stream=`stream` trace=`trace` result=`plan_result` *
**`out`**
+53 -16
View File
@@ -13,6 +13,24 @@ Load `.env` from cwd (optional named arg `path=`). Does not override existing va
**> sys.load_dotenv path=`path`**
## stream_result
+ `events`
Reduce a `complete stream=True` event list to the final `done.result` text (or `error.message`).
*`answer` = "" *
- [`ev`](`events`)
*`t` = > json.get value=`ev` key=type *
1. `t` == "done"
*`answer` = > json.get value=`ev` key=result *
2. `t` == "error"
*`answer` = > json.get value=`ev` key=message *
3. *
*`_` = 1*
**`answer`**
# llm
Construct an LLM handle from `OPENAI_*` / `MARQDO_LLM_*` (default base OpenAI v1, model `gpt-4o-mini`).
@@ -83,9 +101,13 @@ Construct an LLM handle from `OPENAI_*` / `MARQDO_LLM_*` (default base OpenAI v1
## complete
+ `prompt`
+ `stream`=False
+ `echo`=False
Chat completion using `self` / `自` handle fields.
With `stream=True`, returns a list of event maps (`delta` / `done` / `error`) for foreach; default remains a single answer string. Optional `echo=True` prints deltas to stdout while the SSE body is read.
*`api_key` = > json.get value=`self` key=api_key *
*`base_url` = > json.get value=`self` key=base_url *
*`model` = > json.get value=`self` key=model *
@@ -103,29 +125,44 @@ Chat completion using `self` / `自` handle fields.
*`headers` = > json.parse text=`hdr_raw` *
*`q_model` = > json.quote text=`model` *
*`q_prompt` = > json.quote text=`prompt` *
*`body` = `p1` + `q_model` + `p2` + `q_prompt` + `p3` *
*`resp` = > net.http_post url=`url` body=`body` headers=`headers` *
*`status` = > json.get value=`resp` key=status *
*`raw` = > json.get value=`resp` key=body *
1. `status` == 200
*`data` = > json.parse text=`raw` *
*`choices` = > json.get value=`data` key=choices *
*`first` = > at value=`choices` index=0 *
*`message` = > json.get value=`first` key=message *
*`content` = > json.get value=`message` key=content *
**`content`**
1. `stream`
*`p3s` = "}],\"stream\":true}" *
*`body` = `p1` + `q_model` + `p2` + `q_prompt` + `p3s` *
*`resp` = > net.http_post_sse url=`url` body=`body` headers=`headers` echo=`echo` *
*`status` = > json.get value=`resp` key=status *
*`events` = > json.get value=`resp` key=events *
1. `status` == 200
**`events`**
2. *
> print text=ext/ai/llm: HTTP error (stream)
> print text=`status`
> sys.exit code=1
2. *
> print text=ext/ai/llm: HTTP error
> print text=`status`
> print text=`raw`
> sys.exit code=1
*`body` = `p1` + `q_model` + `p2` + `q_prompt` + `p3` *
*`resp` = > net.http_post url=`url` body=`body` headers=`headers` *
*`status` = > json.get value=`resp` key=status *
*`raw` = > json.get value=`resp` key=body *
1. `status` == 200
*`data` = > json.parse text=`raw` *
*`choices` = > json.get value=`data` key=choices *
*`first` = > at value=`choices` index=0 *
*`message` = > json.get value=`first` key=message *
*`content` = > json.get value=`message` key=content *
**`content`**
2. *
> print text=ext/ai/llm: HTTP error
> print text=`status`
> print text=`raw`
> sys.exit code=1
---
## chat
+ `prompt`
+ `stream`=False
+ `echo`=False
Alias for `complete`.
**> `self`.complete prompt=`prompt`**
**> `self`.complete prompt=`prompt` stream=`stream` echo=`echo`**
+53 -16
View File
@@ -13,6 +13,24 @@ description: OpenAI 兼容大模型对象(中文)。导入 ext/ai/大模型.
**> 系统.加载环境 路径=`路径`**
## 流式结果
+ `事件`
把 `运行 流式=真` 的事件列表收成最终 `done.result`(或 `error.message`)。
*`answer` = "" *
- [`ev`](`事件`)
*`t` = > json.get value=`ev` key=type *
1. `t` == "done"
*`answer` = > json.get value=`ev` key=result *
2. `t` == "error"
*`answer` = > json.get value=`ev` key=message *
3. *
*`_` = 1*
**`answer`**
# 大模型
从 `OPENAI_*` / `MARQDO_LLM_*` 构造句柄(默认 OpenAI v1 + `gpt-4o-mini`)。
@@ -83,9 +101,13 @@ description: OpenAI 兼容大模型对象(中文)。导入 ext/ai/大模型.
## 运行
+ `提示`
+ `流式`=False
+ `打印增量`=False
使用 `自` / `self` 句柄字段做聊天补全。
`流式=真` 时返回事件列表(`delta` / `done` / `error`),默认同今日返回整段文本。`打印增量=真` 边读边打印。
*`api_key` = > json.get value=`自` key=api_key *
*`base_url` = > json.get value=`自` key=base_url *
*`model` = > json.get value=`自` key=model *
@@ -103,29 +125,44 @@ description: OpenAI 兼容大模型对象(中文)。导入 ext/ai/大模型.
*`headers` = > json.parse text=`hdr_raw` *
*`q_model` = > json.quote text=`model` *
*`q_prompt` = > json.quote text=`提示` *
*`body` = `p1` + `q_model` + `p2` + `q_prompt` + `p3` *
*`resp` = > 网络.提交 地址=`url` 内容=`body` headers=`headers` *
*`status` = > json.get value=`resp` key=status *
*`raw` = > json.get value=`resp` key=body *
1. `status` == 200
*`data` = > json.parse text=`raw` *
*`choices` = > json.get value=`data` key=choices *
*`first` = > at value=`choices` index=0 *
*`message` = > json.get value=`first` key=message *
*`content` = > json.get value=`message` key=content *
**`content`**
1. `流式`
*`p3s` = "}],\"stream\":true}" *
*`body` = `p1` + `q_model` + `p2` + `q_prompt` + `p3s` *
*`resp` = > 网络.提交流式 地址=`url` 内容=`body` headers=`headers` 打印增量=`打印增量` *
*`status` = > json.get value=`resp` key=status *
*`events` = > json.get value=`resp` key=events *
1. `status` == 200
**`events`**
2. *
> 打印 内容=ext/ai/大模型: HTTP 错误(流式)
> 打印 内容=`status`
> 系统.退出 码=1
2. *
> 打印 内容=ext/ai/大模型: HTTP 错误
> 打印 内容=`status`
> 打印 内容=`raw`
> 系统.退出 码=1
*`body` = `p1` + `q_model` + `p2` + `q_prompt` + `p3` *
*`resp` = > 网络.提交 地址=`url` 内容=`body` headers=`headers` *
*`status` = > json.get value=`resp` key=status *
*`raw` = > json.get value=`resp` key=body *
1. `status` == 200
*`data` = > json.parse text=`raw` *
*`choices` = > json.get value=`data` key=choices *
*`first` = > at value=`choices` index=0 *
*`message` = > json.get value=`first` key=message *
*`content` = > json.get value=`message` key=content *
**`content`**
2. *
> 打印 内容=ext/ai/大模型: HTTP 错误
> 打印 内容=`status`
> 打印 内容=`raw`
> 系统.退出 码=1
---
## 聊天
+ `提示`
+ `流式`=False
+ `打印增量`=False
`运行` 的别名。
**> `自`.运行 提示=`提示`**
**> `自`.运行 提示=`提示` 流式=`流式` 打印增量=`打印增量`**
+20 -4
View File
@@ -130,9 +130,13 @@ description: 文档驱动智能体 — 单步 / 多步;工具经 lib/子任务
## 单步
+ `任务`
+ `写回`=真
+ `流式`=假
+ `打印增量`=假
原子一轮:上下文 → 大模型 → 可选子任务调工具。返回 **map**(`status`、`task`、`decision`;成功含 `result`,可选 `tool` / `tool_result`;失败含 `error`)。默认(`写回=真`)将结果写入调用行下的命名槽 `ok` / `error`;传 `写回=假` 可关闭。
`流式=真` 时走 SSE;`打印增量=真` 边读边打印。返回 map 仍是最终 `result` 文本。
*`ctx` = > 构建单步上下文 智能体=`自` 任务=`任务` *
*`id` = > json.get value=`自` key=id *
*`model` = > json.get value=`自` key=model *
@@ -142,7 +146,12 @@ description: 文档驱动智能体 — 单步 / 多步;工具经 lib/子任务
*`user_turn` = > json.set map=`user_turn` key=content value=`任务` *
> agent_history_append id=`id` item=`user_turn`
*`reply` = > `model`.运行 提示=`ctx` *
1. `流式`
*`evs` = > `model`.运行 提示=`ctx` 流式=真 打印增量=`打印增量` *
*`reply` = > 大模型.流式结果 事件=`evs` *
2. *
*`reply` = > `model`.运行 提示=`ctx` *
*`reply` = > trim value=`reply` *
*`decision` = `reply`*
*`tool_name` = > 提取工具名 回复=`reply` *
@@ -168,7 +177,11 @@ description: 文档驱动智能体 — 单步 / 多步;工具经 lib/子任务
*`fp` = `fp` + ran via subtask and returned: *
*`fp` = `fp` + `tool_s` *
*`fp` = `fp` + . Reply to the user briefly. *
*`reply` = > `model`.运行 提示=`fp` *
1. `流式`
*`evs2` = > `model`.运行 提示=`fp` 流式=真 打印增量=`打印增量` *
*`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` *
@@ -206,11 +219,14 @@ description: 文档驱动智能体 — 单步 / 多步;工具经 lib/子任务
+ `知识库目录`=.marqdo/agent-kb
+ `改进周期`=3
+ `探索次数`=3
+ `流式`=False
+ `打印增量`=False
+ `轨迹`=False
多步工作簿默认写入 OKF `知识库目录/resources/<slug>.mq.md`;仅当传入 `工作簿目录` 时才用临时 runs。转发至英文 `agent.plan`。
多步工作簿默认写入 OKF `知识库目录/resources/<slug>.mq.md`;仅当传入 `工作簿目录` 时才用临时 runs。转发至英文 `agent.plan`。`流式` / `打印增量` / `轨迹` 对应 `stream` / `echo` / `trace`。
*`model` = > json.get value=`self` key=model *
*`tools` = > json.get value=`self` key=tools *
*`standing` = > json.get value=`self` key=standing *
*`en` = > agent.agent model=`model` tools=`tools` standing=`standing` *
**> `en`.plan goal=`目标` workbook_dir=`工作簿目录` workbook=`工作簿` max_rounds=`最大轮次` confirm=`确认分解` writeback=`写回` skeleton=`骨架` reuse=`复用` optimize=`优化` force=`强制` promote=`晋升` kb_dir=`知识库目录` improve_every=`改进周期` explore_n=`探索次数`**
**> `en`.plan goal=`目标` workbook_dir=`工作簿目录` workbook=`工作簿` max_rounds=`最大轮次` confirm=`确认分解` writeback=`写回` skeleton=`骨架` reuse=`复用` optimize=`优化` force=`强制` promote=`晋升` kb_dir=`知识库目录` improve_every=`改进周期` explore_n=`探索次数` stream=`流式` echo=`打印增量` trace=`轨迹`**
+19
View File
@@ -21,6 +21,25 @@ Default content type is JSON. Optional `content_type=` / `headers=` (map).
**> host_http_post url=`url` body=`body` content_type=`content_type` headers=`headers`**
## http_post_sse
+ `url`
+ `body`
+ `content_type`=None
+ `headers`=None
+ `echo`=False
POST and consume OpenAI-compatible SSE. Returns `{status, events}` where `events` is a list of `{type, …}` maps (`delta` / `done` / `error`). Optional `echo=True` prints delta text to stdout as chunks arrive.
**> host_http_post_sse url=`url` body=`body` content_type=`content_type` headers=`headers` echo=`echo`**
## openai_sse_parse
+ `text`
+ `echo`=False
Offline: parse an OpenAI chat SSE body into the same event list (no network).
**> host_openai_sse_parse text=`text` echo=`echo`**
## http_request
+ `method`
+ `url`
+19
View File
@@ -21,6 +21,25 @@ description: 中文 HTTP(S) 包装。可选 headers / content_type 经宿主别
**> host_http_post url=`地址` body=`内容` content_type=`content_type` headers=`headers`**
## 提交流式
+ `地址`
+ `内容`
+ `content_type`=None
+ `headers`=None
+ `打印增量`=False
POST 并消费 OpenAI 兼容 SSE。返回 `{status, events}`(`delta` / `done` / `error`)。`打印增量=真` 时边读边打印增量文本。
**> host_http_post_sse url=`地址` body=`内容` content_type=`content_type` headers=`headers` echo=`打印增量`**
## 解析流式
+ `内容`
+ `打印增量`=False
离线解析 OpenAI 聊天 SSE 正文为事件列表(不联网)。
**> host_openai_sse_parse text=`内容` echo=`打印增量`**
## 请求
+ `方法`
+ `地址`
+1 -1
View File
@@ -6,7 +6,7 @@ description: Minimal HTTP(S) client and URL encode
# main
Import lib/net.mq.md. Functions: http_get(url), http_post(url, body), http_request(method, url), url_encode(text). HTTPS and optional headers= map are supported. Default post content-type is JSON.
Import lib/net.mq.md. Functions: http_get, http_post, http_post_sse, openai_sse_parse, http_request, url_encode. HTTPS and optional headers= map are supported. Default post content-type is JSON. `http_post_sse` / `openai_sse_parse` return OpenAI chat stream event lists (`delta` / `done`).
*`e` = > net.url_encode text=hello world *
+22 -8
View File
@@ -88,19 +88,33 @@ pub fn canonical_param(builtin: &str, param: &str) -> String {
("host_exec" | "exec", "cmd" | "命令") => "cmd".into(),
("host_exec" | "exec", "args" | "参数") => "args".into(),
(
"host_http_get" | "http_get" | "host_http_post" | "http_post" | "host_http_request"
| "http_request",
"host_http_get" | "http_get" | "host_http_post" | "http_post" | "host_http_post_sse"
| "http_post_sse" | "host_http_request" | "http_request",
"headers" | "头",
) => "headers".into(),
(
"host_http_post" | "http_post" | "host_http_request" | "http_request",
"host_http_post" | "http_post" | "host_http_post_sse" | "http_post_sse"
| "host_http_request" | "http_request",
"content_type" | "类型",
) => "content_type".into(),
("host_http_get" | "http_get" | "host_http_post" | "http_post" | "host_http_request"
| "http_request", "url" | "地址") => "url".into(),
("host_http_post" | "http_post" | "host_http_request" | "http_request", "body" | "内容") => {
"body".into()
}
(
"host_http_get" | "http_get" | "host_http_post" | "http_post" | "host_http_post_sse"
| "http_post_sse" | "host_http_request" | "http_request",
"url" | "地址",
) => "url".into(),
(
"host_http_post" | "http_post" | "host_http_post_sse" | "http_post_sse"
| "host_http_request" | "http_request",
"body" | "内容",
) => "body".into(),
(
"host_http_post_sse" | "http_post_sse" | "host_openai_sse_parse" | "openai_sse_parse",
"echo" | "打印增量" | "print_deltas",
) => "echo".into(),
(
"host_openai_sse_parse" | "openai_sse_parse",
"text" | "内容",
) => "text".into(),
("host_json_stringify" | "stringify", "indent" | "缩进") => "indent".into(),
("host_plot", "steps" | "步数") => "steps".into(),
("host_plot", "derivative" | "导数") => "derivative".into(),
+23
View File
@@ -97,6 +97,8 @@ pub enum HostFn {
TextPatch = 95,
ApplyPatchBlocks = 96,
WritebackScanPath = 97,
HttpPostSse = 98,
OpenAiSseParse = 99,
}
impl HostFn {
@@ -190,6 +192,8 @@ impl HostFn {
95 => Self::TextPatch,
96 => Self::ApplyPatchBlocks,
97 => Self::WritebackScanPath,
98 => Self::HttpPostSse,
99 => Self::OpenAiSseParse,
_ => return None,
})
}
@@ -221,6 +225,8 @@ impl HostFn {
// Aliases so optional `headers` / `content_type` bind like plot.
"host_http_get" | "http_get" => Self::HttpGet,
"host_http_post" | "http_post" => Self::HttpPost,
"host_http_post_sse" | "http_post_sse" => Self::HttpPostSse,
"host_openai_sse_parse" | "openai_sse_parse" => Self::OpenAiSseParse,
"host_url_encode" => Self::UrlEncode,
"host_pi" => Self::Pi,
"host_e" => Self::EConst,
@@ -321,6 +327,8 @@ impl HostFn {
Self::JsonKeys => "host_json_keys",
Self::HttpGet => "host_http_get",
Self::HttpPost => "host_http_post",
Self::HttpPostSse => "host_http_post_sse",
Self::OpenAiSseParse => "host_openai_sse_parse",
Self::UrlEncode => "host_url_encode",
Self::Pi => "host_pi",
Self::EConst => "host_e",
@@ -407,6 +415,8 @@ impl HostFn {
Self::JsonGet => &["value", "key"],
Self::HttpGet => &["url"],
Self::HttpPost => &["url", "body"],
Self::HttpPostSse => &["url", "body"],
Self::OpenAiSseParse => &["text"],
Self::HttpRequest => &["method", "url"],
Self::DotenvLoad => &[],
Self::Pi | Self::EConst | Self::Random => &[],
@@ -451,6 +461,8 @@ impl HostFn {
Self::JsonStringify => &["indent"],
Self::HttpGet => &["headers"],
Self::HttpPost => &["content_type", "headers"],
Self::HttpPostSse => &["content_type", "headers", "echo"],
Self::OpenAiSseParse => &["echo"],
Self::HttpRequest => &["body", "content_type", "headers"],
Self::DotenvLoad => &["path"],
Self::Exec => &["args"],
@@ -524,6 +536,17 @@ pub fn call_host(
bound.get("content_type"),
bound.get("headers"),
),
HostFn::HttpPostSse => net::http_post_sse(
ctx,
require(bound, "url")?,
require(bound, "body")?,
bound.get("content_type"),
bound.get("headers"),
bound.get("echo"),
),
HostFn::OpenAiSseParse => {
net::openai_sse_parse(require(bound, "text")?, bound.get("echo"))
}
HostFn::HttpRequest => net::http_request(
ctx,
require(bound, "method")?,
+297
View File
@@ -1,5 +1,6 @@
//! HTTP host primitives (HTTP and HTTPS via ureq).
use std::io::{BufRead, BufReader, Write};
use std::time::Duration;
use crate::host::HostContext;
@@ -173,6 +174,275 @@ pub fn url_encode(text: &Value) -> Result<Value, String> {
))
}
fn truthy_flag(v: Option<&Value>) -> bool {
match v {
None | Some(Value::None) => false,
Some(v) => v.truthy(),
}
}
/// Extract `data:` payloads from an SSE body (ignores comments / event: / id:).
pub fn sse_data_payloads(text: &str) -> Vec<String> {
let mut out = Vec::new();
let mut cur: Vec<String> = Vec::new();
for line in text.split('\n') {
let line = line.strip_suffix('\r').unwrap_or(line);
if line.is_empty() {
if !cur.is_empty() {
out.push(cur.join("\n"));
cur.clear();
}
continue;
}
if let Some(rest) = line.strip_prefix("data:") {
let payload = rest.strip_prefix(' ').unwrap_or(rest);
cur.push(payload.to_string());
}
// Other SSE fields ignored for chat-completions wire format.
}
if !cur.is_empty() {
out.push(cur.join("\n"));
}
out
}
fn event_map(pairs: Vec<(&str, Value)>) -> Value {
Value::Map(
pairs
.into_iter()
.map(|(k, v)| (k.to_string(), v))
.collect(),
)
}
/// Map OpenAI-compatible chat SSE payloads → Marqdo stream events
/// (`delta` / `done` / `error`). Optional `echo` prints delta text to stdout as it arrives.
pub fn openai_chat_sse_events(text: &str, echo: bool) -> Result<Vec<Value>, String> {
let mut events = Vec::new();
let mut result = String::new();
let mut saw_done = false;
for data in sse_data_payloads(text) {
let data = data.trim();
if data.is_empty() {
continue;
}
if data == "[DONE]" {
saw_done = true;
break;
}
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(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() {
println!();
}
if !saw_done && events.is_empty() && result.is_empty() {
// Non-SSE / empty: treat as error for clearer gold diagnostics.
events.push(event_map(vec![
("type", Value::Text("error".into())),
(
"message",
Value::Text("openai sse: no data frames".into()),
),
]));
return Ok(events);
}
events.push(event_map(vec![
("type", Value::Text("done".into())),
("result", Value::Text(result)),
]));
Ok(events)
}
/// Offline: parse OpenAI chat SSE text → event list (no network).
pub fn openai_sse_parse(text: &Value, echo: Option<&Value>) -> Result<Value, String> {
let text = as_text(text, "text")?;
let echo = truthy_flag(echo);
Ok(Value::List(openai_chat_sse_events(text, echo)?))
}
fn read_sse_body_to_events<R: BufRead>(
mut reader: R,
echo: bool,
) -> Result<Vec<Value>, String> {
let mut buf = String::new();
let mut raw = String::new();
let mut events = Vec::new();
let mut result = String::new();
let mut data_buf: Vec<String> = Vec::new();
let flush_data = |data_buf: &mut Vec<String>,
events: &mut Vec<Value>,
result: &mut String,
echo: bool|
-> Result<bool, String> {
if data_buf.is_empty() {
return Ok(false);
}
let data = data_buf.join("\n");
data_buf.clear();
let data = data.trim();
if data.is_empty() {
return Ok(false);
}
if data == "[DONE]" {
return Ok(true);
}
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)
};
loop {
buf.clear();
let n = reader
.read_line(&mut buf)
.map_err(|e| format!("http sse read: {e}"))?;
if n == 0 {
break;
}
raw.push_str(&buf);
if raw.len() > 8 * 1024 * 1024 {
return Err("http sse response exceeds 8MiB limit".into());
}
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)? {
break;
}
continue;
}
if let Some(rest) = line.strip_prefix("data:") {
let payload = rest.strip_prefix(' ').unwrap_or(rest);
data_buf.push(payload.to_string());
}
}
let _ = flush_data(&mut data_buf, &mut events, &mut result, echo)?;
if echo && !result.is_empty() {
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")))) {
return Ok(events);
}
events.push(event_map(vec![
("type", Value::Text("done".into())),
("result", Value::Text(result)),
]));
Ok(events)
}
/// POST and consume OpenAI-compatible SSE; return `{status, events}` list of stream maps.
pub fn http_post_sse(
ctx: &HostContext,
url: &Value,
body: &Value,
content_type: Option<&Value>,
headers: Option<&Value>,
echo: Option<&Value>,
) -> Result<Value, String> {
if !ctx.allow_net() {
return Err("http_post_sse denied by host policy".into());
}
let url = as_text(url, "url")?;
let body = as_text(body, "body")?;
let ct = match content_type {
None | Some(Value::None) => "application/json; charset=utf-8",
Some(v) => as_text(v, "content_type")?,
};
let hdrs = headers_from_value(headers)?;
let echo = truthy_flag(echo);
if !(url.starts_with("http://") || url.starts_with("https://")) {
return Err("url must start with http:// or https://".into());
}
let agent = ureq::AgentBuilder::new()
.timeout_connect(Duration::from_secs(15))
.timeout_read(Duration::from_secs(120))
.build();
let mut req = agent.post(url).set("User-Agent", "marqdo").set("Content-Type", ct);
req = req.set("Accept", "text/event-stream");
for (k, v) in &hdrs {
req = req.set(k, v);
}
match req.send_string(body) {
Ok(r) => {
let status = r.status();
let reader = BufReader::new(r.into_reader());
let events = read_sse_body_to_events(reader, echo)?;
Ok(Value::Map(vec![
("status".into(), Value::Int(status as i64)),
("events".into(), Value::List(events)),
]))
}
Err(ureq::Error::Status(code, r)) => {
let text = r.into_string().unwrap_or_default();
Ok(Value::Map(vec![
("status".into(), Value::Int(code as i64)),
(
"events".into(),
Value::List(vec![event_map(vec![
("type", Value::Text("error".into())),
("message", Value::Text(text)),
])]),
),
]))
}
Err(e) => Err(format!("http POST sse {url}: {e}")),
}
}
#[cfg(test)]
mod tests {
use super::*;
@@ -189,4 +459,31 @@ mod tests {
.iter()
.any(|(k, v)| k == "Authorization" && v == "Bearer x"));
}
#[test]
fn openai_sse_fixture_to_events() {
let fixture = "\
data: {\"choices\":[{\"delta\":{\"content\":\"Hel\"}}]}\n\
\n\
data: {\"choices\":[{\"delta\":{\"content\":\"lo\"}}]}\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 == "delta")));
assert!(m.iter().any(|(k, v)| k == "text" && matches!(v, Value::Text(t) if t == "Hel")));
}
_ => panic!("expected 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 == "Hello")));
}
_ => panic!("expected done map"),
}
}
}
+61
View File
@@ -0,0 +1,61 @@
---
title: agent plan stream helpers + confirm (offline)
description: plan_append_*/plan_finish_stream; confirm=True with stream attaches done event.
> ext/ai/llm.mq.md
> ext/ai/agent.mq.md
> lib/json.mq.md
> lib/fs.mq.md
---
# main
*`events` = > json.parse text=[] *
*`events` = > agent.plan_append_round events=`events` round=1 workbook=wb.mq.md exit_code=0 stream=True *
*`from` = > json.parse text=[{"type":"delta","text":"Hi"},{"type":"done","result":"Hi"}] *
*`events` = > agent.plan_merge_deltas events=`events` from=`from` stream=True *
*`events` = > agent.plan_append_decision events=`events` decision=DONE stream=True summary=ok *
*`out` = > json.parse text={"status":"ok"} *
*`out` = > agent.plan_finish_stream out=`out` events=`events` stream=True result=child *
*`evs` = > json.get value=`out` key=events *
*`n` = > len value=`evs` *
> print text=`n`
*`e0` = > at value=`evs` index=0 *
*`t0` = > json.get value=`e0` key=type *
> print text=`t0`
*`e1` = > at value=`evs` 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=`evs` index=2 *
*`t2` = > json.get value=`e2` key=type *
> print text=`t2`
*`e3` = > at value=`evs` index=3 *
*`t3` = > json.get value=`e3` key=type *
*`r3` = > json.get value=`e3` key=result *
> print text=`t3`
> print text=`r3`
> llm.load_env path=.env
*`model` = > llm.llm *
*`tools` = > json.parse text=[] *
*`助手` = > agent.agent model=`model` tools=`tools` standing=offline plan stream *
*`pout` = > `助手`.plan goal=say hi confirm=True workbook_dir=".marqdo/agent-runs" writeback=False stream=True *
*`st` = > json.get value=`pout` key=status *
> print text=`st`
*`pevs` = > json.get value=`pout` key=events *
*`pn` = > len value=`pevs` *
> print text=`pn`
*`plast` = > at value=`pevs` index=0 *
*`pt` = > json.get value=`plast` key=type *
> print text=`pt`
+14
View File
@@ -0,0 +1,14 @@
---
title: llm stream_result offline
description: No network — SSE fixture via net.openai_sse_parse + llm.stream_result
> ext/ai/llm.mq.md
> lib/net.mq.md
---
# main
*`fixture` = "data: {\"choices\":[{\"delta\":{\"content\":\"Hi\"}}]}\n\ndata: {\"choices\":[{\"delta\":{\"content\":\"!\"}}]}\n\ndata: [DONE]\n" *
*`events` = > net.openai_sse_parse text=`fixture` *
*`answer` = > llm.stream_result events=`events` *
> print text=`answer`
+32
View File
@@ -1363,6 +1363,38 @@ fn lib_net_encode() {
assert_out("tests/lib/net-encode.mq.md", "a+b");
}
#[test]
fn lib_net_openai_sse() {
assert_out(
"tests/lib/net-openai-sse.mq.md",
"Hel
lo
Hello",
);
}
#[test]
fn ext_llm_stream_offline() {
assert_out("tests/ext/llm-stream-offline.mq.md", "Hi!");
}
#[test]
fn ext_agent_plan_stream_offline() {
assert_out(
"tests/ext/agent-plan-stream-offline.mq.md",
"4
round
delta
Hi
decision
done
child
pending
1
done",
);
}
#[test]
fn lib_math_formula() {
assert_out(
+22
View File
@@ -0,0 +1,22 @@
---
title: openai SSE parse offline
> lib/net.mq.md
> lib/json.mq.md
---
# main
*`fixture` = "data: {\"choices\":[{\"delta\":{\"content\":\"Hel\"}}]}\n\ndata: {\"choices\":[{\"delta\":{\"content\":\"lo\"}}]}\n\ndata: [DONE]\n" *
*`events` = > net.openai_sse_parse text=`fixture` *
- [`ev`](`events`)
*`t` = > json.get value=`ev` key=type *
1. `t` == "delta"
*`chunk` = > json.get value=`ev` key=text *
> print text=`chunk`
2. `t` == "done"
*`answer` = > json.get value=`ev` key=result *
> print text=`answer`
3. *
> print text=bad