From 2a9fceb51cfbd780f49d2a04e378a14a48ac6be7 Mon Sep 17 00:00:00 2001 From: cflmy Date: Mon, 10 Aug 2026 11:58:26 +0800 Subject: [PATCH] Land OKF aliases, view SSE, workbook nav, and explicit parent ctor. Complete the core agent roadmap: soft KB hits via Task aliases, EventBus-backed view streaming, D4 writeback/workbook navigation, and I3 explicit-super without implicit auto-super. Co-authored-by: Cursor --- .cursor/skills/marqdo/SKILL.md | 2 +- .cursor/skills/marqdo/examples.md | 11 +- doc/design/ext-agent.md | 2 +- doc/design/objects.md | 2 +- doc/design/okf.md | 10 +- doc/design/view.md | 5 +- doc/roadmap/agent-streaming.md | 6 +- doc/roadmap/object-inheritance.md | 9 +- doc/roadmap/okf-near-match.md | 2 +- ext/ai/agent.mq.md | 16 +- lib/sys.mq.md | 7 + lib/系统.mq.md | 7 + plugins/agent/src/kb.rs | 163 +++++++++++- public/stdlib/sys.mq.md | 2 +- src/aliases.rs | 4 + src/host/dispatch.rs | 9 + src/host/event_bus.rs | 54 ++++ src/host/mod.rs | 1 + src/host/net.rs | 30 ++- src/interp.rs | 7 +- src/view/html.rs | 259 ++++++++++++++++++- src/view/mod.rs | 168 +++++++++++- src/view/render.rs | 75 +++++- tests/ext/agent-kb-alias.mq.md | 64 +++++ tests/gold.rs | 29 +++ tests/structure/inherit-explicit-super.mq.md | 28 ++ 26 files changed, 918 insertions(+), 54 deletions(-) create mode 100644 src/host/event_bus.rs create mode 100644 tests/ext/agent-kb-alias.mq.md create mode 100644 tests/structure/inherit-explicit-super.mq.md diff --git a/.cursor/skills/marqdo/SKILL.md b/.cursor/skills/marqdo/SKILL.md index 19b5da9..04031bc 100644 --- a/.cursor/skills/marqdo/SKILL.md +++ b/.cursor/skills/marqdo/SKILL.md @@ -30,7 +30,7 @@ Canonical design (repo): `doc/design/markdown-mapping.md`, `doc/design/keywords. | Marker | Meaning | |--------|---------| -| `#` | **Object / type** (constructor body). `# main` = entry object. `# Child = > Parent` = inherit. | +| `#` | **Object / type** (constructor body). `# main` = entry object. `# Child = > Parent` = inherit (**no** implicit parent constructor; call `` `self` = > Parent … `` explicitly when needed). | | `##` … `######` | **Function / method** (nesting by heading depth). | | `` + `name` `` under heading | Parameter (optional `` `name`=default ``) | | `1.` `2.` … in body | Branch arm (condition or `N. *` else). **Same-indent restart at `1.` = new branch statement** (not more arms of the previous list). | diff --git a/.cursor/skills/marqdo/examples.md b/.cursor/skills/marqdo/examples.md index 6b55321..92750a3 100644 --- a/.cursor/skills/marqdo/examples.md +++ b/.cursor/skills/marqdo/examples.md @@ -175,7 +175,16 @@ See `tests/structure/object-handle.mq.md` and `ext/llm.mq.md`: `# Type` construc **`msg`** ``` -`_type` stays the most specific name (`Loud`). Methods walk the base chain; same-name `##` on the child overrides. No implicit super in v1. +`_type` stays the most specific name (`Loud`). Methods walk the base chain; same-name `##` on the child overrides. **No implicit super** — if the child needs parent fields, call the parent explicitly: + +```markdown +# Child = > Parent + + `name` + +*`self` = > Parent name=`name` * +*`self` = > json.set map=`self` key=extra value=1 * +**`self`** +``` ## 11. Agent layout (ABI) diff --git a/doc/design/ext-agent.md b/doc/design/ext-agent.md index 8f0de83..14e37b0 100644 --- a/doc/design/ext-agent.md +++ b/doc/design/ext-agent.md @@ -329,7 +329,7 @@ lib/subtask:spawn fn=<名> → wait | D1 | 重写 `ext/ai/agent.mq.md` / `智能体.mq.md`:`单步` + 源码/位置注入 + **子任务调工具** + **结构化返回值**;运行时经 **ABI v2 agent 插件**(禁 ext 直调 host) | **done** | | D2 | `多步`:按 [ext-agent-plan.md](ext-agent-plan.md)(观察→精确补丁;禁止整文件重写);**D2a–D2f 主路径已落地** | **done** | | D3 | 金样例:离线单步 / 写回;**live 单步**(`tests/ext/agent-run-live.mq.md`);多步骨架(可不打网) | **live done** | -| D4 | `view` / `debug` 对工作簿与写回块的导航体验 | TODO | +| D4 | `view` / `debug` 对工作簿与写回块的导航体验 | done(workbook 链到 `/file?path=`;写回槽 `ok`/`error`/`trace` 徽章 + `#wb-{line}`;plan 结果卡) | | O2 | OKF 任务知识包:`plan` reuse/promote([okf.md §7](okf.md)) | **done** | ### 非目标 diff --git a/doc/design/objects.md b/doc/design/objects.md index 06e1a3a..f7c5167 100644 --- a/doc/design/objects.md +++ b/doc/design/objects.md @@ -83,7 +83,7 @@ | 语法 | `# Child = > Parent`(右侧必须是 `>` + 简单类型名;无路径) | | `_type` | 构造后仍为**最具体子类名** | | 方法查找 | 先子类 `##`,未命中再沿基类链;同名则子类覆盖 | -| 构造 | v1 **不**隐式调用父构造;需要时在子类正文显式 `` `self` = > Parent … `` | +| 构造 | **不**隐式调用父构造(无 auto-super)。需要父字段时在子类正文首行显式 `` `self` = > Parent … ``,再改字段并 `**`self`**`;返回 map 的 `_type` 仍为子类名 | | 多继承 | 不支持 | | 诊断 | 未知基类、循环继承、非法 RHS(如 `# Foo = 1`) | diff --git a/doc/design/okf.md b/doc/design/okf.md index dab46e1..beab816 100644 --- a/doc/design/okf.md +++ b/doc/design/okf.md @@ -342,7 +342,13 @@ Prefer spawning `resource` over re-planning. - `concepts/skills/.md` - `resources/.mq.md` -匹配:先按 slug 试探并核对 frontmatter `sig`,再扫目录按 `sig:`,最后回退旧 hex 文件名。v1 **精确命中**;不做 embedding。相近措辞自动复用属未来规划,见 [okf-near-match.md](../roadmap/okf-near-match.md)。 +匹配顺序: + +1. **精确**:slug 试探 + frontmatter `sig` → 扫目录 `sig:` → 旧 hex 文件名。 +2. **别名(Accepted)**:精确 miss 后,扫 Task FM `aliases:`(YAML 列表或 `[a, b]`);`normalize_goal(query)` 与某条 alias **精确相等**则命中同一 Skill/resource。lookup 返回 `match: exact|alias`;`plan` 对 alias 写 `cache=soft-hit`。 +3. **不做**默认 embedding / 编辑距离主路径。父裁决 soft_match 仍见 [okf-near-match.md](../roadmap/okf-near-match.md)。 + +`agent_kb_promote` 可选 `aliases=`(字符串或列表)写入 Task;再晋升时合并既有 aliases。 ### 7.4 与 `## plan` 的契约 @@ -426,7 +432,7 @@ agent-kb 运行时实现归属 **agent 插件**(`agent_goal_sig` / `agent_kb_l | **O2** | 本文锁定的任务知识包:`Marqdo Task` / `Agent Skill`;`plan` reuse/promote | **done**(host kb_* + plan 快路径) | | **O3** | catalog 增强:`verified`/`sources` 可选;模块页可点击依赖;扫入 `agent-kb` 概念供人浏览 | **done** | | **O4** | `log.md` 策展、可选 `kb query`、Attested-style「再现跑通」收据(非 BigQuery) | 远期 | -| **O5** | 相近任务可选软命中(规范句 / aliases / 父裁决;非默认向量库) | 规划见 [okf-near-match.md](../roadmap/okf-near-match.md) | +| **O5** | 相近任务软命中:Task `aliases` 第二趟已落地;规范句 / 父裁决 / 向量仍见 [okf-near-match.md](../roadmap/okf-near-match.md) | **aliases done** | O2 设计落盘后,智能体侧实现文档可薄化为指向本文 §7,避免两套真理。 diff --git a/doc/design/view.md b/doc/design/view.md index eb1af26..d359795 100644 --- a/doc/design/view.md +++ b/doc/design/view.md @@ -102,8 +102,11 @@ marqdo view output tests/structure/hello.mq.md -o /tmp/hello-doc | `GET` | `/` | 默认打开第一个 `.mq.md`(无文件时为空提示) | | `GET` | `/file?path=…` | 单文件页 | | `GET` | `/api/tree` | JSON 树(可选) | +| `GET` | `/api/events?path=…` | SSE(`text/event-stream`):订阅进程内 EventBus(`reasoning` / `delta` / `round` / `decision` / `done` / `error`) | +| `POST` | `/api/run` | JSON `{path, stdin?}`:后台 `run_file`;事件进 bus;结束补 `done`/`error`(不替代页内整页 stdout) | +| `POST` | `/api/foreign-run` | 外联代码块试跑 | -根目录锁死;拒绝 `..` 逃逸。 +根目录锁死;拒绝 `..` 逃逸。默认页加载仍整页执行;Stream 面板为增强路径(`EventSource` + Run)。 --- diff --git a/doc/roadmap/agent-streaming.md b/doc/roadmap/agent-streaming.md index d88c07b..1350f74 100644 --- a/doc/roadmap/agent-streaming.md +++ b/doc/roadmap/agent-streaming.md @@ -2,7 +2,7 @@ | | | |---|---| -| 状态 | **S0–S2 已落地**(S3 view 仍待做) | +| 状态 | **S0–S3 已落地**(view SSE) | | 日期 | 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` 均为整段返回;用户看不到思考与中间输出 | @@ -136,7 +136,7 @@ plan 特有: S0 net/llm:SSE 读 + complete stream=True + 事件 foreach ✅ S1 step stream=True(echo 增量;返回 map 不变) ✅ S2 plan stream=True:round + 父 delta;trace 写回可选 ✅ -S3 view 订阅 (待做) +S3 view 订阅(EventBus + GET /api/events + POST /api/run) ✅ ``` ### 开放点决议(S0) @@ -167,6 +167,6 @@ S3 view 订阅 (待做) 2. ~~delta 默认 print~~ → 默认关;`echo=True` 可选。 3. ~~中文面~~ → `流式=` / `打印增量=`。 4. ~~S2~~ → `plan stream=True`:`round` / 父 delta / `decision` / `done`;可选 `trace`。 -5. **S3**:`marqdo view` 订阅同一事件模型。 +5. ~~**S3**~~ → `marqdo view`:进程内 EventBus;`GET /api/events`(SSE);`POST /api/run` 后台执行;Execution 区 EventSource 面板。CLI 默认不订阅。 S0 可与 [okf-near-match.md](okf-near-match.md) 并行,互不阻塞。 diff --git a/doc/roadmap/object-inheritance.md b/doc/roadmap/object-inheritance.md index 1b33cdd..83bfab0 100644 --- a/doc/roadmap/object-inheritance.md +++ b/doc/roadmap/object-inheritance.md @@ -2,7 +2,7 @@ | | | |---|---| -| 状态 | **I0–I2 已落地**(I3 隐式 super 仍可选) | +| 状态 | **I0–I3 已落地**(显式父构造约定;**不做**隐式 auto-super) | | 日期 | 2026-08-10 | | 相关 | [objects.md](../design/objects.md) · [markdown-mapping.md](../design/markdown-mapping.md) · [keywords.md](../design/keywords.md) | | 提案语法 | `# 子类 = > 父类` | @@ -139,7 +139,7 @@ I0 语法 + AST + 金样(解析/循环继承错误) ✅ I1 方法解析沿基类链;构造 _type=子类 ✅ I2 覆盖方法;文档更新 objects.md ✅ -I3 (可选)子构造约定:显式 > 父类 … 或隐式 super +I3 显式父构造约定(**不做**隐式 auto-super) ✅ ``` 验收: @@ -147,14 +147,13 @@ I3 (可选)子构造约定:显式 > 父类 … 或隐式 super 1. 无继承文件行为不变。 2. 子类实例能调父类 `##`;覆盖后只走子类。 3. `` # A = > B `` / `` # B = > A `` 诊断清晰。 -4. Skill / 金样:`tests/structure/inherit-*.mq.md`。 +4. Skill / 金样:`tests/structure/inherit-*.mq.md`(含显式 `` `self` = > Parent ``)。 --- ## 7. 开放点 -1. **隐式 super**:子类构造体执行前是否自动跑父构造体? - - 建议 v1:**不隐式**;需要时正文写 `` `self` = > 父类 … `` 再改字段,或提供 `` > 父类.构造 … `` 约定。 +1. ~~**隐式 super**~~ → **不做**:子类构造体不自动跑父构造。需要父初始化时正文写 `` `self` = > 父类 … `` 再改字段后 `**`self`**`;构造完成时 `_type` 仍为最具体子类名。 2. 中英基类别名:`> agent` 与 `> 智能体` 是否同一对象(取决于模块导出)。 3. 继承是否写入 OKF Module 页(catalog 展示 `extends`)——可随 O3/O4 增强。 diff --git a/doc/roadmap/okf-near-match.md b/doc/roadmap/okf-near-match.md index a64d496..afacfd1 100644 --- a/doc/roadmap/okf-near-match.md +++ b/doc/roadmap/okf-near-match.md @@ -2,7 +2,7 @@ | | | |---|---| -| 状态 | **规划 / 未开工** | +| 状态 | **B aliases 已落地**(A 规范句 / C 父裁决 / E 向量仍待) | | 日期 | 2026-08-09 | | 相关 | [okf.md](../design/okf.md) §7 · [ext-agent-plan.md](../design/ext-agent-plan.md) §4.2 · [ext-agent.md](../design/ext-agent.md) | | 触发观察 | `man-test`:相近 goal 生成两份独立 resource | diff --git a/ext/ai/agent.mq.md b/ext/ai/agent.mq.md index f09df4e..d4f286f 100644 --- a/ext/ai/agent.mq.md +++ b/ext/ai/agent.mq.md @@ -420,6 +420,7 @@ Append a `round` event when `stream=True`. Optional `echo` prints a short TTY ma *`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` * + > sys.stream_publish event=`ev` 1. `echo` > print text=plan:round 2. * @@ -443,6 +444,7 @@ Append a `round` event when `stream=True`. Optional `echo` prints a short TTY ma 2. * *`_` = 1* *`events` = > json.append list=`events` item=`ev` * + > sys.stream_publish event=`ev` 2. * *`_` = 1* @@ -487,6 +489,7 @@ When `stream=True`, append `done` and attach `events` on the result map. Optiona 2. * *`_` = 1* *`events` = > json.append list=`events` item=`ev` * + > sys.stream_publish event=`ev` *`out` = > json.set map=`out` key=events value=`events` * 2. * *`_` = 1* @@ -655,6 +658,11 @@ Non-hit path runs parent **decompose** before the first child spawn (`DECISION: 3. `reuse` *`hit` = > agent_kb_lookup kb_dir=`kb_dir` goal=`goal` tools=`tools` * 1. `hit` + *`match_kind` = > json.get value=`hit` key=match * + 1. `match_kind` == alias + *`cache_label` = soft-hit* + 2. * + *`cache_label` = hit* *`lf` = > json.get value=`hit` key=llm_free * 1. `lf` *`path` = > json.get value=`hit` key=resource * @@ -664,13 +672,13 @@ Non-hit path runs parent **decompose** before the first child spawn (`DECISION: *`last_obs` = > json.get value=`aw` key=observation * 1. `code` == 0 > agent_kb_record_hit kb_dir=`kb_dir` goal=`goal` tools=`tools` improve_every=`improve_every` - *`cache` = hit* + *`cache` = `cache_label`* *`out` = > json.parse text={"status":"ok"} * *`out` = > json.set map=`out` key=status value=ok * *`out` = > json.set map=`out` key=goal value=`goal` * *`out` = > json.set map=`out` key=workbook value=`path` * *`out` = > json.set map=`out` key=rounds value=1 * - *`out` = > json.set map=`out` key=cache value=hit * + *`out` = > json.set map=`out` key=cache value=`cache_label` * *`sk` = > json.get value=`hit` key=skill * *`out` = > json.set map=`out` key=skill value=`sk` * *`st` = > json.get value=`hit` key=status * @@ -711,13 +719,13 @@ Non-hit path runs parent **decompose** before the first child spawn (`DECISION: *`improve` = 1* *`cache` = improve* 2. * - *`cache` = hit* + *`cache` = `cache_label`* *`out` = > json.parse text={"status":"ok"} * *`out` = > json.set map=`out` key=status value=ok * *`out` = > json.set map=`out` key=goal value=`goal` * *`out` = > json.set map=`out` key=workbook value=`path` * *`out` = > json.set map=`out` key=rounds value=1 * - *`out` = > json.set map=`out` key=cache value=hit * + *`out` = > json.set map=`out` key=cache value=`cache_label` * *`sk` = > json.get value=`hit` key=skill * *`out` = > json.set map=`out` key=skill value=`sk` * *`st` = > json.get value=`hit` key=status * diff --git a/lib/sys.mq.md b/lib/sys.mq.md index 18b0fdf..da48739 100644 --- a/lib/sys.mq.md +++ b/lib/sys.mq.md @@ -39,3 +39,10 @@ Load `.env` (optional named arg `path=`). Existing process env is not overridden + `args`=None **> host_exec cmd=`cmd` args=`args`** + +## stream_publish + + `event` + +Publish a stream event map onto the process EventBus (`marqdo view` SSE). No-op when nothing is subscribed. + +**> host_stream_publish event=`event`** diff --git a/lib/系统.mq.md b/lib/系统.mq.md index a69b729..3abd13a 100644 --- a/lib/系统.mq.md +++ b/lib/系统.mq.md @@ -39,3 +39,10 @@ description: 中文系统包装(加载环境 可选 路径=) + `参数`=None **> host_exec cmd=`命令` args=`参数`** + +## 发布流事件 + + `事件` + +向进程内 EventBus 发布流事件(供 `marqdo view` SSE)。无订阅者时为空操作。 + +**> host_stream_publish event=`事件`** diff --git a/plugins/agent/src/kb.rs b/plugins/agent/src/kb.rs index e0bb610..5742af7 100644 --- a/plugins/agent/src/kb.rs +++ b/plugins/agent/src/kb.rs @@ -164,6 +164,76 @@ fn extract_fm_field(source: &str, key: &str) -> Option { None } +/// Parse Task FM `aliases:` as inline list `[a, b]`, comma string, or YAML `- item` block. +fn extract_fm_aliases(source: &str) -> Vec { + let source = source.strip_prefix('\u{feff}').unwrap_or(source); + let lines: Vec<&str> = source.lines().collect(); + if lines.first().map(|l| l.trim()) != Some("---") { + return Vec::new(); + } + let mut out = Vec::new(); + let mut i = 1usize; + while i < lines.len() { + let t = lines[i].trim(); + if t == "---" { + break; + } + if let Some(rest) = t.strip_prefix("aliases:") { + let rest = rest.trim(); + if rest.is_empty() { + i += 1; + while i < lines.len() { + let body = lines[i]; + let bt = body.trim(); + if bt == "---" { + break; + } + if let Some(item) = bt.strip_prefix("- ") { + let a = normalize_goal(item.trim().trim_matches('"')); + if !a.is_empty() { + out.push(a); + } + i += 1; + continue; + } + if bt == "-" { + i += 1; + continue; + } + if !body.starts_with(' ') && !body.starts_with('\t') && !bt.is_empty() { + break; + } + i += 1; + } + break; + } + let inline = rest.trim_matches(|c| c == '[' || c == ']'); + for part in inline.split(',') { + let a = normalize_goal(part.trim().trim_matches('"')); + if !a.is_empty() { + out.push(a); + } + } + break; + } + i += 1; + } + out +} + +fn format_aliases_yaml(aliases: &[String]) -> String { + if aliases.is_empty() { + return String::new(); + } + let mut s = String::from("aliases:\n"); + for a in aliases { + s.push_str(" - "); + s.push_str(&yaml_escape(a)); + s.push('\n'); + } + s +} + fn set_fm_field(source: &str, key: &str, value: &str) -> String { let source = source.strip_prefix('\u{feff}').unwrap_or(source); let lines: Vec<&str> = source.lines().collect(); @@ -258,6 +328,43 @@ fn resolve_skill_paths( None } +/// Second-pass exact match: `normalize_goal(query)` equals a Task FM alias. +fn resolve_skill_paths_by_alias( + root: &Path, + goal_norm: &str, +) -> Option<(PathBuf, PathBuf, PathBuf, String)> { + if goal_norm.is_empty() { + return None; + } + let tasks_dir = root.join("concepts/tasks"); + let skills_dir = root.join("concepts/skills"); + let res_dir = root.join("resources"); + if !tasks_dir.is_dir() { + return None; + } + let rd = fs::read_dir(&tasks_dir).ok()?; + for ent in rd.flatten() { + let p = ent.path(); + if p.extension().and_then(|e| e.to_str()) != Some("md") { + continue; + } + let Ok(src) = fs::read_to_string(&p) else { + continue; + }; + let aliases = extract_fm_aliases(&src); + if !aliases.iter().any(|a| a == goal_norm) { + continue; + } + let stem = p.file_stem()?.to_string_lossy().into_owned(); + let skill = skills_dir.join(format!("{stem}.md")); + let res = res_dir.join(format!("{stem}.mq.md")); + if skill.is_file() && res.is_file() { + return Some((p, skill, res, stem)); + } + } + None +} + fn unique_slug(root: &Path, base: &str, sig: &str) -> String { let tasks = root.join("concepts/tasks"); let candidate = tasks.join(format!("{base}.md")); @@ -296,11 +403,32 @@ fn write_task_concept( slug: &str, goal: &str, now: &str, + aliases: &[String], ) -> Result<(), String> { if let Some(p) = path.parent() { fs::create_dir_all(p).map_err(|e| format!("mkdir: {e}"))?; } let title: String = goal.chars().take(48).collect(); + let mut merged: Vec = Vec::new(); + if path.is_file() { + if let Ok(old) = fs::read_to_string(path) { + for a in extract_fm_aliases(&old) { + if !merged.iter().any(|x| x == &a) { + merged.push(a); + } + } + } + } + for a in aliases { + let a = normalize_goal(a); + if a.is_empty() || a == goal { + continue; + } + if !merged.iter().any(|x| x == &a) { + merged.push(a); + } + } + let alias_block = format_aliases_yaml(&merged); let body = format!( r#"--- type: Marqdo Task @@ -310,7 +438,7 @@ sig: {sig} status: stable tags: [agent-task] skill: ../skills/{slug}.md -generated: +{alias_block}generated: by: marqdo-agent/plan at: {now} verified: @@ -326,6 +454,7 @@ See [skill](../skills/{slug}.md). goal_e = yaml_escape(goal), sig = sig, slug = slug, + alias_block = alias_block, now = now, ); fs::write(path, body).map_err(|e| format!("write task: {e}")) @@ -435,10 +564,16 @@ pub fn goal_slug(args: &Value) -> Result { pub fn kb_lookup(args: &Value) -> Result { let kb = arg_text(args, "kb_dir")?; let goal = arg_text(args, "goal")?; - let sig = goal_sig_hex(&material_for(goal, opt_tools(args))); + let goal_n = normalize_goal(goal); + let query_sig = goal_sig_hex(&material_for(goal, opt_tools(args))); let slug_hint = goal_slug_str(goal); let root = resolve_path(kb); - let Some((_task, skill_path, _res, slug)) = resolve_skill_paths(&root, &sig, &slug_hint) else { + let mut match_kind = "exact"; + let resolved = resolve_skill_paths(&root, &query_sig, &slug_hint).or_else(|| { + match_kind = "alias"; + resolve_skill_paths_by_alias(&root, &goal_n) + }); + let Some((_task, skill_path, _res, slug)) = resolved else { return Ok(Value::Null); }; @@ -457,6 +592,7 @@ pub fn kb_lookup(args: &Value) -> Result { let hits = extract_fm_field(&skill_src, "hits") .and_then(|s| s.parse::().ok()) .unwrap_or(0); + let skill_sig = extract_fm_field(&skill_src, "sig").unwrap_or_else(|| query_sig.clone()); let res_path = root.join("resources").join(format!("{slug}.mq.md")); if res_path.is_file() { @@ -474,7 +610,9 @@ pub fn kb_lookup(args: &Value) -> Result { Ok(json!({ "hit": true, - "sig": sig, + "match": match_kind, + "sig": skill_sig, + "query_sig": query_sig, "slug": slug, "status": status, "llm_free": llm_free, @@ -489,10 +627,13 @@ pub fn kb_lookup(args: &Value) -> Result { pub fn kb_record_hit(args: &Value) -> Result { let kb = arg_text(args, "kb_dir")?; let goal = arg_text(args, "goal")?; + let goal_n = normalize_goal(goal); let sig = goal_sig_hex(&material_for(goal, opt_tools(args))); let slug_hint = goal_slug_str(goal); let root = resolve_path(kb); - let Some((_t, skill_path, _r, slug)) = resolve_skill_paths(&root, &sig, &slug_hint) else { + let Some((_t, skill_path, _r, slug)) = resolve_skill_paths(&root, &sig, &slug_hint) + .or_else(|| resolve_skill_paths_by_alias(&root, &goal_n)) + else { return Ok(Value::Null); }; let src = fs::read_to_string(&skill_path).map_err(|e| format!("read skill: {e}"))?; @@ -571,12 +712,24 @@ pub fn kb_promote(args: &Value) -> Result { fs::write(&res_path, &src).map_err(|e| format!("write resource: {e}"))?; let now = now_rfc3339(); + // Optional extra aliases from caller (JSON array or single string). + let mut extra_aliases: Vec = Vec::new(); + if let Some(Value::Array(items)) = args.get("aliases") { + for v in items { + if let Some(s) = v.as_str() { + extra_aliases.push(s.to_string()); + } + } + } else if let Some(Value::String(s)) = args.get("aliases") { + extra_aliases.push(s.clone()); + } write_task_concept( &root.join("concepts/tasks").join(format!("{slug}.md")), &sig, &slug, &goal_n, &now, + &extra_aliases, )?; write_skill_concept( &skill_path, diff --git a/public/stdlib/sys.mq.md b/public/stdlib/sys.mq.md index 2e28057..80e4455 100644 --- a/public/stdlib/sys.mq.md +++ b/public/stdlib/sys.mq.md @@ -6,7 +6,7 @@ description: Env, dotenv, cwd, args, exit, exec # main -Import lib/sys.mq.md. Functions: env_get, env_set, load_dotenv(optional path), args, cwd, exit(code), exec(cmd). load_dotenv does not override existing variables. Under view, exit soft-fails instead of killing the process. +Import lib/sys.mq.md. Functions: env_get, env_set, load_dotenv(optional path), args, cwd, exit(code), exec(cmd), stream_publish(event). load_dotenv does not override existing variables. Under view, exit soft-fails instead of killing the process. `stream_publish` feeds the view SSE EventBus. *`d` = > cwd * diff --git a/src/aliases.rs b/src/aliases.rs index cc5e880..cf72387 100644 --- a/src/aliases.rs +++ b/src/aliases.rs @@ -87,6 +87,10 @@ pub fn canonical_param(builtin: &str, param: &str) -> String { ("host_dotenv_load" | "load_dotenv", "path" | "路径") => "path".into(), ("host_exec" | "exec", "cmd" | "命令") => "cmd".into(), ("host_exec" | "exec", "args" | "参数") => "args".into(), + ( + "host_stream_publish" | "stream_publish", + "event" | "事件", + ) => "event".into(), ( "host_http_get" | "http_get" | "host_http_post" | "http_post" | "host_http_post_sse" | "http_post_sse" | "host_http_request" | "http_request", diff --git a/src/host/dispatch.rs b/src/host/dispatch.rs index 81c6192..e7cfff6 100644 --- a/src/host/dispatch.rs +++ b/src/host/dispatch.rs @@ -99,6 +99,7 @@ pub enum HostFn { WritebackScanPath = 97, HttpPostSse = 98, OpenAiSseParse = 99, + StreamPublish = 100, } impl HostFn { @@ -194,6 +195,7 @@ impl HostFn { 97 => Self::WritebackScanPath, 98 => Self::HttpPostSse, 99 => Self::OpenAiSseParse, + 100 => Self::StreamPublish, _ => return None, }) } @@ -227,6 +229,7 @@ impl HostFn { "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_stream_publish" | "stream_publish" => Self::StreamPublish, "host_url_encode" => Self::UrlEncode, "host_pi" => Self::Pi, "host_e" => Self::EConst, @@ -329,6 +332,7 @@ impl HostFn { Self::HttpPost => "host_http_post", Self::HttpPostSse => "host_http_post_sse", Self::OpenAiSseParse => "host_openai_sse_parse", + Self::StreamPublish => "host_stream_publish", Self::UrlEncode => "host_url_encode", Self::Pi => "host_pi", Self::EConst => "host_e", @@ -417,6 +421,7 @@ impl HostFn { Self::HttpPost => &["url", "body"], Self::HttpPostSse => &["url", "body"], Self::OpenAiSseParse => &["text"], + Self::StreamPublish => &["event"], Self::HttpRequest => &["method", "url"], Self::DotenvLoad => &[], Self::Pi | Self::EConst | Self::Random => &[], @@ -547,6 +552,10 @@ pub fn call_host( HostFn::OpenAiSseParse => { net::openai_sse_parse(require(bound, "text")?, bound.get("echo")) } + HostFn::StreamPublish => { + crate::host::event_bus::publish(require(bound, "event")?); + Ok(Value::Bool(true)) + } HostFn::HttpRequest => net::http_request( ctx, require(bound, "method")?, diff --git a/src/host/event_bus.rs b/src/host/event_bus.rs new file mode 100644 index 0000000..fd66e20 --- /dev/null +++ b/src/host/event_bus.rs @@ -0,0 +1,54 @@ +//! Process-wide stream event bus for `marqdo view` SSE (S3). +//! Publishers: LLM SSE parse, optional `host_stream_publish`. +//! Subscribers: view `GET /api/events`. + +use std::sync::mpsc::{self, Receiver, Sender}; +use std::sync::{Mutex, OnceLock}; + +use crate::host::json::value_to_json; +use crate::value::Value; + +static BUS: OnceLock = OnceLock::new(); + +pub struct EventBus { + subs: Mutex>>, +} + +impl EventBus { + fn new() -> Self { + Self { + subs: Mutex::new(Vec::new()), + } + } + + pub fn global() -> &'static EventBus { + BUS.get_or_init(EventBus::new) + } + + /// Subscribe to JSON event lines (already serialized maps). + pub fn subscribe(&self) -> Receiver { + let (tx, rx) = mpsc::channel(); + if let Ok(mut g) = self.subs.lock() { + g.push(tx); + } + rx + } + + pub fn publish_json(&self, json_line: &str) { + let Ok(mut g) = self.subs.lock() else { + return; + }; + g.retain(|tx| tx.send(json_line.to_string()).is_ok()); + } + + pub fn publish_value(&self, v: &Value) { + if let Ok(j) = value_to_json(v) { + self.publish_json(&j.to_string()); + } + } +} + +/// Publish a Marqdo map (or any value) onto the global bus. +pub fn publish(v: &Value) { + EventBus::global().publish_value(v); +} diff --git a/src/host/mod.rs b/src/host/mod.rs index a291755..07fe964 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -6,6 +6,7 @@ pub mod agent_rt; mod dispatch; +pub mod event_bus; pub mod foreign; mod fs; pub(crate) mod json; diff --git a/src/host/net.rs b/src/host/net.rs index 1cdf89a..e1ae7f1 100644 --- a/src/host/net.rs +++ b/src/host/net.rs @@ -232,10 +232,12 @@ fn push_openai_delta_events( .and_then(|m| m.as_str()) .or_else(|| e.as_str()) }) { - events.push(event_map(vec![ + let ev = event_map(vec![ ("type", Value::Text("error".into())), ("message", Value::Text(msg.into())), - ])); + ]); + crate::host::event_bus::publish(&ev); + events.push(ev); return true; } let reasoning = v @@ -249,10 +251,12 @@ fn push_openai_delta_events( let _ = std::io::stdout().flush(); *echoed = true; } - events.push(event_map(vec![ + let ev = event_map(vec![ ("type", Value::Text("reasoning".into())), ("text", Value::Text(reasoning.into())), - ])); + ]); + crate::host::event_bus::publish(&ev); + events.push(ev); } let content = v .pointer("/choices/0/delta/content") @@ -269,10 +273,12 @@ fn push_openai_delta_events( let _ = std::io::stdout().flush(); *echoed = true; } - events.push(event_map(vec![ + let ev = event_map(vec![ ("type", Value::Text("delta".into())), ("text", Value::Text(content.into())), - ])); + ]); + crate::host::event_bus::publish(&ev); + events.push(ev); } false } @@ -321,10 +327,12 @@ pub fn openai_chat_sse_events(text: &str, echo: bool) -> Result, Stri ])); return Ok(events); } - events.push(event_map(vec![ + let done = event_map(vec![ ("type", Value::Text("done".into())), ("result", Value::Text(result)), - ])); + ]); + crate::host::event_bus::publish(&done); + events.push(done); Ok(events) } @@ -424,10 +432,12 @@ fn read_sse_body_to_events( 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![ + let done = event_map(vec![ ("type", Value::Text("done".into())), ("result", Value::Text(result)), - ])); + ]); + crate::host::event_bus::publish(&done); + events.push(done); Ok(events) } diff --git a/src/interp.rs b/src/interp.rs index 9f2e687..13ee34c 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -788,9 +788,10 @@ fn instance_type_name(v: &Value) -> std::result::Result { fn tag_instance(type_name: &str, value: Value) -> Value { match value { Value::Map(mut entries) => { - if !entries.iter().any(|(k, _)| k == "_type") { - entries.insert(0, ("_type".into(), Value::Text(type_name.into()))); - } + // Always stamp the constructed type so explicit `` `self` = > Parent `` + // in a child constructor still yields the most specific `_type`. + entries.retain(|(k, _)| k != "_type"); + entries.insert(0, ("_type".into(), Value::Text(type_name.into()))); Value::Map(entries) } Value::None => Value::Map(vec![("_type".into(), Value::Text(type_name.into()))]), diff --git a/src/view/html.rs b/src/view/html.rs index 0bd5fd4..dac3549 100644 --- a/src/view/html.rs +++ b/src/view/html.rs @@ -474,6 +474,14 @@ section.block > h2 { border-left: 3px solid #2563eb; background: #f8fafc; } +.output-card .wb-badges { + display: flex; + flex-wrap: wrap; + gap: 0.35rem; +} +.wb-badge.wb-ok { background: #dcfce7; color: #166534; } +.wb-badge.wb-error { background: #fee2e2; color: #991b1b; } +.wb-badge.wb-trace { background: #e0e7ff; color: #3730a3; } .output-card .output-body { margin: 0.35rem 0 0; padding: 0.5rem 0.65rem; @@ -481,6 +489,89 @@ section.block > h2 { white-space: pre-wrap; color: #0f172a; } +.wb-outline { + margin-top: 0.75rem; + padding-top: 0.75rem; + border-top: 1px solid var(--line); +} +.wb-outline-title { + font: 0.72rem/1.2 var(--sans); + letter-spacing: 0.04em; + text-transform: uppercase; + color: var(--muted); + margin-bottom: 0.35rem; +} +.plan-card { + margin: 0 0 0.85rem; + padding: 0.75rem 0.9rem; + border: 1px solid var(--line); + border-radius: var(--radius); + background: #f8fafc; + font: 0.88rem/1.45 var(--sans); +} +.plan-card .plan-meta { + display: flex; + flex-wrap: wrap; + gap: 0.5rem 1rem; + margin: 0.35rem 0 0.55rem; + color: var(--muted); + font: 0.8rem/1.35 var(--mono); +} +.plan-card a { color: #1d4ed8; } +.stream-panel { + margin: 0 0 0.85rem; + border: 1px solid var(--line); + border-radius: var(--radius); + background: #0f172a; + color: #e2e8f0; + overflow: hidden; +} +.stream-head { + display: flex; + align-items: center; + gap: 0.65rem; + padding: 0.55rem 0.75rem; + background: #1e293b; + border-bottom: 1px solid #334155; + font: 0.8rem/1 var(--sans); +} +.stream-head button { + font: 0.78rem/1 var(--sans); + padding: 0.35rem 0.7rem; + border-radius: 6px; + border: 1px solid #475569; + background: #334155; + color: #f8fafc; + cursor: pointer; +} +.stream-head button:disabled { + opacity: 0.5; + cursor: not-allowed; +} +.stream-status { + margin-left: auto; + color: #94a3b8; + font: 0.72rem/1 var(--mono); +} +.stream-log { + margin: 0; + padding: 0.75rem 0.85rem; + min-height: 6rem; + max-height: 18rem; + overflow: auto; + font: 0.78rem/1.45 var(--mono); + white-space: pre-wrap; + word-break: break-word; +} +.stream-log .ev-mark { + color: #93c5fd; +} +.stream-log .ev-reason { + color: #c4b5fd; +} +.stream-log .ev-err { + color: #fca5a5; +} .code-head { display: flex; flex-wrap: wrap; @@ -763,14 +854,96 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String { }}); }}); }} + function initStreamPanel() {{ + if (!window.MARQDO_VIEW_LIVE) return; + var log = document.getElementById("stream-log"); + var status = document.getElementById("stream-status"); + var runBtn = document.getElementById("stream-run"); + if (!log || !window.EventSource) return; + var es = new EventSource("/api/events?path=" + encodeURIComponent(window.MARQDO_FILE_PATH || "")); + function appendLine(cls, text) {{ + var span = document.createElement("span"); + if (cls) span.className = cls; + span.textContent = text; + log.appendChild(span); + log.appendChild(document.createTextNode("\\n")); + log.scrollTop = log.scrollHeight; + }} + es.onopen = function () {{ + if (status) status.textContent = "connected"; + }}; + es.onerror = function () {{ + if (status) status.textContent = "reconnecting…"; + }}; + es.onmessage = function (msg) {{ + try {{ + var ev = JSON.parse(msg.data); + var t = ev.type || "?"; + if (t === "reasoning") {{ + appendLine("ev-reason", ev.text || ""); + }} else if (t === "delta") {{ + appendLine(null, ev.text || ""); + }} else if (t === "error") {{ + appendLine("ev-err", "error: " + (ev.message || "")); + }} else if (t === "round" || t === "decision" || t === "run_start" || t === "done") {{ + var mark = t; + if (t === "round") mark += " #" + (ev.round != null ? ev.round : ""); + if (t === "decision") mark += " " + (ev.decision || ""); + if (t === "done" && ev.path) mark += " " + ev.path; + appendLine("ev-mark", "— " + mark + " —"); + if (t === "done" && typeof ev.result === "string" && ev.result) {{ + appendLine(null, ev.result.slice(0, 2000)); + }} + if (ev.workbook) {{ + appendLine("ev-mark", "workbook: " + ev.workbook); + }} + }} else {{ + appendLine("ev-mark", JSON.stringify(ev)); + }} + }} catch (e) {{ + appendLine("ev-err", String(msg.data || e)); + }} + }}; + if (runBtn) {{ + runBtn.addEventListener("click", function () {{ + runBtn.disabled = true; + if (status) status.textContent = "running…"; + log.textContent = ""; + var stdinEl = document.getElementById("stdin"); + fetch("/api/run", {{ + method: "POST", + headers: {{ "Content-Type": "application/json" }}, + body: JSON.stringify({{ + path: window.MARQDO_FILE_PATH || "", + stdin: stdinEl ? stdinEl.value : "" + }}) + }}) + .then(function (r) {{ return r.json(); }}) + .then(function (data) {{ + if (!data.ok) {{ + appendLine("ev-err", data.error || "run failed"); + if (status) status.textContent = "error"; + }} + }}) + .catch(function (e) {{ + appendLine("ev-err", String(e)); + if (status) status.textContent = "error"; + }}) + .finally(function () {{ runBtn.disabled = false; }}); + }}); + }} + }} + if (document.readyState === "loading") {{ document.addEventListener("DOMContentLoaded", function () {{ initForeignCards(); initOutlineSearch(); + initStreamPanel(); }}); }} else {{ initForeignCards(); initOutlineSearch(); + initStreamPanel(); }} function initOutlineSearch() {{ @@ -798,7 +971,7 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String { }}); }} input.addEventListener("input", applyFilter); - tree.querySelectorAll("a[href^='#fn-']").forEach(function (a) {{ + document.querySelectorAll("a[href^='#fn-'], a[href^='#wb-']").forEach(function (a) {{ a.addEventListener("click", function (ev) {{ var id = (a.getAttribute("href") || "").slice(1); var el = id ? document.getElementById(id) : null; @@ -1085,6 +1258,19 @@ pub fn page_file(files: &[PathBuf], rel: &str, vm: &FileViewModel, links: &LinkM outline = outline_block, ) }; + let plan_card = plan_result_card(&vm.stdout, links); + let stream_panel = match links { + LinkMode::Live => r#"
+
+ Stream + + idle +
+

+
"# + .to_string(), + LinkMode::Static { .. } => String::new(), + }; let main = format!( r#"