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

+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=`轨迹`**