Add OKF soft match, CALL tool streaming, and lib whitelist dispatch.
Canonicalize/alias lookup and optional soft_match parent REUSE cut kb fragmentation; stream tool_start/end for Plan-and-Move visibility; allow safe CALL:lib.* without FC. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
dbb2765553
commit
ff84c43c10
16 files changed
+771
-89
No files matched your search
@@ -83,7 +83,7 @@ cargo build -p marqdo_plugin_agent
|
||||
```
|
||||
|
||||
- Demo (ABI v1): `demo_add`, `demo_echo` — [`tests/lib/plugin-demo.mq.md`](../../tests/lib/plugin-demo.mq.md).
|
||||
- Agent (ABI v2): layout + session bag + context queries + **OKF agent-kb** (`agent_goal_sig`, `agent_goal_slug`, `agent_kb_lookup`, `agent_kb_promote`, `agent_kb_record_hit`, `agent_workbook_solidify`, `agent_kb_task_files`) — [`plugins/agent`](../../plugins/agent); used by [`ext/ai/agent.mq.md`](../../ext/ai/agent.mq.md). Lookup: `plugin.native_path name=agent` or [ext-cli.md](ext-cli.md) install / `MARQDO_AGENT_PLUGIN`.
|
||||
- Agent (ABI v2): layout + session bag + context queries + **OKF agent-kb** (`agent_goal_sig`, `agent_goal_slug`, `agent_kb_lookup`, `agent_kb_canonicalize`, `agent_kb_list_tasks`, `agent_kb_add_alias`, `agent_kb_promote`, `agent_kb_record_hit`, `agent_workbook_solidify`, `agent_kb_task_files`) — [`plugins/agent`](../../plugins/agent); used by [`ext/ai/agent.mq.md`](../../ext/ai/agent.mq.md). Lookup: `plugin.native_path name=agent` or [ext-cli.md](ext-cli.md) install / `MARQDO_AGENT_PLUGIN`.
|
||||
|
||||
## Layering
|
||||
|
||||
|
||||
@@ -103,9 +103,11 @@
|
||||
|
||||
### 6.2 标准库与造工具
|
||||
|
||||
- **标准库**:父通过 `lib_catalog` 知晓能力;把 `> lib/….mq.md` 与 `##` 写进工作簿(PATCH)或 scratch 工具文件后即可被子任务 / 后续 `CALL` 使用。
|
||||
- **标准库直调(白名单)**:`CALL:lib.fs.read_text`(及 `exists` / `list_dir` / `json.parse` / `json.stringify` / `sys.cwd`)+ 可选 `ARGS:{…}` JSON 行;拒绝 `net.*` / `sys.exec` / 写删类。
|
||||
- **目录**:`lib_catalog` 列出可 CALL 的 dotted 名。
|
||||
- **造工具**:优先 `CONTINUE` + 短 PATCH 在工作簿内新增 `##`;或 `CALL:scratch_tool_write` 沉淀独立工具文件。
|
||||
- **非目标(本阶段)**:把全部 `lib/*` 自动反射成 JSON tools schema;原生 function calling。
|
||||
- **流式**:父 CALL/READ 发 `tool_start` / `tool_end`(view Tool 卡)。
|
||||
- **非目标**:全量 `lib/*` JSON schema 反射;OpenAI function calling。
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted;D2a 已落地;D2b–D2d(观察→补丁式多步)实现中** |
|
||||
| 日期 | 2026-08-07 |
|
||||
| 状态 | **Accepted;D2a–D2f 已落地;父 Plan-and-Move / OKF 软命中见配套文档** |
|
||||
| 日期 | 2026-08-11 |
|
||||
| 父文档 | [ext-agent.md](ext-agent.md) |
|
||||
| 相关 | [**ext-agent-parent.md**](ext-agent-parent.md)(**父 Plan-and-Move 行动面**) · [stdlib-writeback.md](stdlib-writeback.md) · [stdlib-subtask.md](stdlib-subtask.md) · [module-namespace.md](module-namespace.md) · [ai-skill.md](ai-skill.md) · [objects.md](objects.md) |
|
||||
|
||||
|
||||
+5
-2
@@ -346,7 +346,9 @@ Prefer spawning `resource` over re-planning.
|
||||
|
||||
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)。
|
||||
3. **规范句(Accepted)**:仍 miss 时对 goal 做轻量 `agent_kb_canonicalize`(剥站立前缀、去尾标点),再精确/别名各试一次;命中返回 `match: canonical`,`cache=soft-hit`。
|
||||
4. **父裁决 soft_match(Accepted,默认关)**:`plan soft_match=True` 且仍 miss 时,把 `agent_kb_list_tasks` 候选交给父模型:`DECISION: REUSE` + `SLUG:` 或 `DECISION: NEW`;REUSE 后可 `agent_kb_add_alias` 把当前 goal 写入别名。
|
||||
5. **不做**默认 embedding / 编辑距离主路径。向量层见 [okf-near-match.md](../roadmap/okf-near-match.md) E。
|
||||
|
||||
`agent_kb_promote` 可选 `aliases=`(字符串或列表)写入 Task;再晋升时合并既有 aliases。
|
||||
|
||||
@@ -357,6 +359,7 @@ Prefer spawning `resource` over re-planning.
|
||||
| `reuse` / `复用` | `True` | 允许知识包快路径 |
|
||||
| `optimize` / `优化` | `False` | 跳过知识包,强制再规划 |
|
||||
| `force` / `强制` | `False` | 忽略知识包 |
|
||||
| `soft_match` / `软命中` | `False` | miss 后父裁决 REUSE/NEW(须显式打开) |
|
||||
| `promote` / `晋升` | `True` | DONE 后固化并写入/更新概念与 resource |
|
||||
| `kb_dir` / `知识库目录` | `.marqdo/agent-kb` | bundle 根 |
|
||||
| `workbook_dir` / `工作簿目录` | `None` | 默认直接写 `kb_dir/resources/<slug>.mq.md`;显式传入则用该目录下的临时 `workbook-<slug>-<ts>.mq.md` |
|
||||
@@ -432,7 +435,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** | 相近任务软命中:Task `aliases` 第二趟已落地;规范句 / 父裁决 / 向量仍见 [okf-near-match.md](../roadmap/okf-near-match.md) | **aliases done** |
|
||||
| **O5** | 相近任务软命中:aliases + canonicalize + `soft_match` 父裁决已落地;向量仍见 [okf-near-match.md](../roadmap/okf-near-match.md) E | **A/B/C done** |
|
||||
|
||||
O2 设计落盘后,智能体侧实现文档可薄化为指向本文 §7,避免两套真理。
|
||||
|
||||
|
||||
+2
-1
@@ -116,7 +116,8 @@ marqdo view output tests/structure/hello.mq.md -o /tmp/hello-doc
|
||||
|------|------|------|
|
||||
| **Thinking** | `reasoning` | 可折叠;流式追加到同一块;默认展开直至本轮 `delta`/`done` 开始,之后可收起 |
|
||||
| **Answer** | `delta` | **主文**;token 连续拼进同一段落(禁止每 token 换行);主色正文 |
|
||||
| **Decision** | `decision` | 时间线条目标记(`RUN` / `CONTINUE` / `DONE` + 可选 summary) |
|
||||
| **Decision** | `decision` | 时间线条目标记(`RUN` / `CONTINUE` / `DONE` / `REUSE` / `NEW` + 可选 summary) |
|
||||
| **Tool** | `tool_start` / `tool_end` | 父 Plan-and-Move 的 CALL/READ:名称 + kind;`tool_end` 可带截断 `result` |
|
||||
| **Child / Workbook** | `round` | **独立卡片**:轮次、退出码、workbook 链(`/file?path=`)、可选 `result` 摘要;与父思考/答案视觉分离 |
|
||||
| **Status** | `run_start` / `done` / `error` | 顶栏状态 + 错误行;`done.result` 仅在无 Answer 块时作摘要,不与 reasoning 混排 |
|
||||
|
||||
|
||||
@@ -64,7 +64,7 @@ ext/ai/agent step/plan 订阅事件 → 可选透传或写回「过程槽」
|
||||
|--------|------|----------|
|
||||
| `reasoning` | 模型思考增量(如 DeepSeek `reasoning_content`) | `text` |
|
||||
| `delta` | 模型答案增量文本 | `text` |
|
||||
| `tool_start` / `tool_end` | 工具轮 | `name`, `result?` |
|
||||
| `tool_start` / `tool_end` | 工具轮(父 plan CALL/READ 已发;view Tool 卡) | `name`, `kind?`, `result?` |
|
||||
| `round` | plan 轮次边界(子工作簿跑完) | `round`, `workbook`, `exit_code?`, `result?`(子返回值摘要) |
|
||||
| `decision` | DONE / CONTINUE / RUN | `decision`, `summary?` |
|
||||
| `done` | 本调用结束 | `result`(答案正文;不含 thinking) |
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **B aliases 已落地**(A 规范句 / C 父裁决 / E 向量仍待) |
|
||||
| 日期 | 2026-08-09 |
|
||||
| 状态 | **A 规范句 + B aliases + C 父裁决(soft_match)已落地**;E 向量仍待 |
|
||||
| 日期 | 2026-08-11 |
|
||||
| 相关 | [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 |
|
||||
|
||||
@@ -11,23 +11,20 @@
|
||||
|
||||
## 1. 问题
|
||||
|
||||
今日 agent-kb **只做精确命中**:
|
||||
v1 agent-kb **默认只做精确命中**(仍是默认快路径):
|
||||
|
||||
1. `normalize_goal`:去首尾空白、合并连续空白(**不做**近义改写)。
|
||||
2. `sig` = 规范化字符串的稳定短哈希;`slug` = 可读路径名。
|
||||
3. `agent_kb_lookup`:按 `sig` / slug 精确对上才复用;否则新建 Task / Skill / `resources/<slug>.mq.md`。
|
||||
3. `agent_kb_lookup`:精确 → aliases → **canonicalize 后再精确/别名**;仍 miss 且 `plan soft_match=True` 时父裁决 REUSE/NEW。
|
||||
|
||||
因此字面不同、含义相近的两次 `plan` 会被当成**两个任务**。实测例子:
|
||||
历史痛点(已由 A/B/C 缓解):
|
||||
|
||||
| goal(调用方输入) | 产物 slug(示意) |
|
||||
|--------------------|-------------------|
|
||||
| `你是一个智能体,帮我规划明天的行程` | `你是一个智能体-帮我规划明天的行程` |
|
||||
| `帮我规划明行程` | `帮我规划明行程` |
|
||||
| goal(调用方输入) | 旧行为 |
|
||||
|--------------------|--------|
|
||||
| `你是一个智能体,帮我规划明天的行程` | 独立 slug,与下方不命中 |
|
||||
| `帮我规划明天的行程` | 另一份 resource |
|
||||
|
||||
调用方直觉:「这不是同一件事吗?」
|
||||
框架行为:「指纹不同 → 不命中 → 再探索 / 再固化。」
|
||||
|
||||
这在 v1 **是合理且有意的**(见 §2),但缺少一层「相近含义」通道时,知识包会碎片化,复用率下降。
|
||||
调用方直觉:「这不是同一件事吗?」——现可用 canonicalize(剥站立前缀)或显式 `soft_match` 父裁决。
|
||||
|
||||
---
|
||||
|
||||
@@ -44,14 +41,14 @@
|
||||
|
||||
---
|
||||
|
||||
## 3. 目标(未来能力)
|
||||
## 3. 目标
|
||||
|
||||
在 **不破坏** 精确 `sig` 契约的前提下,可选地:
|
||||
在 **不破坏** 精确 `sig` 契约的前提下:
|
||||
|
||||
1. 识别「同一任务意图的不同措辞」→ 复用已有 Skill / resource;或
|
||||
2. 至少提示调用方 / 父智能体:「可能命中已有任务 X,确认后复用」。
|
||||
1. **已落地**:A 规范句、B aliases、C `soft_match` 父裁决(默认关)。
|
||||
2. **仍待**:E 向量(仅有证据再开)。
|
||||
|
||||
非目标(本规划明确不做为默认):
|
||||
非目标(仍不做为默认):
|
||||
|
||||
- 把 agent-kb 做成通用向量知识库 / RAG 产品。
|
||||
- 用不可审计的静默相似度覆盖精确 `sig`。
|
||||
@@ -63,32 +60,26 @@
|
||||
|
||||
相近命中 **不等于** 必须上向量检索。按与 Marqdo 契合度排序:
|
||||
|
||||
### A. 规范化后再精确命中(首选探索)
|
||||
### A. 规范化后再精确命中 — **已落地**
|
||||
|
||||
| | |
|
||||
|--|--|
|
||||
| 做法 | lookup 前把 goal 收成**规范句**(规则表,或一次小模型 / 父 LLM 调用),再算现有 `sig` |
|
||||
| 优点 | 索引与今日相同;无 ANN;结果可写回 Task(`canonical` / `aliases`) |
|
||||
| 风险 | 规范器不稳会导致漂移;需冻结「规范句」写入 task 页,避免每次改写 |
|
||||
| 落地形态 | `agent_kb_canonicalize`(插件)或 `plan` 内可选 `canonicalize=True` |
|
||||
| 做法 | `agent_kb_canonicalize`:剥站立前缀、去尾 `?`/`?`/`。`,再 `normalize_goal`;lookup 第三趟 |
|
||||
| 落地 | 插件 + `match: canonical`;`cache=soft-hit` |
|
||||
|
||||
### B. 别名 / 标签表(文档原生)
|
||||
### B. 别名 / 标签表 — **已落地**
|
||||
|
||||
| | |
|
||||
|--|--|
|
||||
| 做法 | Task frontmatter 增加 `aliases: […]` / `tags: […]`;lookup:先 `sig`,再扫别名精确匹配 |
|
||||
| 优点 | 完全可读可 git;promote / 人工策展可补别名 |
|
||||
| 风险 | 冷启动靠人;自动抽别名仍可能要模型 |
|
||||
| 落地形态 | 扩 `concepts/tasks/*.md`;`kb_lookup` 第二趟扫 aliases |
|
||||
| 做法 | Task FM `aliases:`;lookup 第二趟精确匹配 |
|
||||
| 落地 | `match: alias`;promote / `agent_kb_add_alias` |
|
||||
|
||||
### C. 父智能体软裁决(miss 后)
|
||||
### C. 父智能体软裁决(miss 后)— **已落地(默认关)**
|
||||
|
||||
| | |
|
||||
|--|--|
|
||||
| 做法 | 精确 miss 时,把 `index.md`(或 task 标题列表)交给父模型:`REUSE:<slug>` / `NEW` |
|
||||
| 优点 | 无新基础设施;与「父是开发大师」一致;决策可写回 |
|
||||
| 风险 | 多一次 LLM;需严格协议防乱指;`llm_free` 快路径仍走精确命中 |
|
||||
| 落地形态 | `plan` 在 reuse miss 且 `soft_match=True` 时分支 |
|
||||
| 做法 | `plan soft_match=True`:`agent_kb_list_tasks` → 父 `DECISION: REUSE` + `SLUG:` 或 `DECISION: NEW` |
|
||||
| 落地 | REUSE → 现有 hit 分支;可选 append alias |
|
||||
|
||||
### D. 字面近似(弱,仅拼写容错)
|
||||
|
||||
@@ -110,29 +101,30 @@
|
||||
|
||||
---
|
||||
|
||||
## 5. 推荐演进顺序
|
||||
## 5. 演进顺序
|
||||
|
||||
```text
|
||||
精确 sig(现状,永不删除)
|
||||
→ A 规范句 或 B aliases(文档层,可金样)
|
||||
→ C soft_match 父裁决(可选,打网)
|
||||
精确 sig(永不删除)
|
||||
→ A 规范句 + B aliases(已落地)
|
||||
→ C soft_match 父裁决(已落地,默认关)
|
||||
→ E 向量(仅有证据再开)
|
||||
```
|
||||
|
||||
验收原则:
|
||||
验收原则(仍适用):
|
||||
|
||||
1. 默认行为与今日一致(未开开关 = 仅精确命中)。
|
||||
2. 软命中必须留下可读痕迹(写回 `cache=soft-hit`、选用的 slug、可选置信说明)。
|
||||
1. 默认行为与精确路径一致(`soft_match` 默认 False;canonicalize 无模型、低风险)。
|
||||
2. 软命中留下 `cache=soft-hit` / `match` 痕迹。
|
||||
3. 假阳性可被 `force=True` / 显式 `workbook=` 覆盖。
|
||||
4. 实现仍在 **agent 插件 + `ext/ai`**,不进核心 `src/host`。
|
||||
|
||||
---
|
||||
|
||||
## 6. 调用方近期规避(无需等实现)
|
||||
## 6. 调用方建议
|
||||
|
||||
- 固定规范 goal 字符串(应用层常量),不要把「你是一个智能体」等站立语拼进 goal。
|
||||
- 固定规范 goal 字符串;站立语可由 canonicalize 剥掉,但仍宜少拼。
|
||||
- 续跑同一主题时传 `workbook=` 或已知 resource 路径。
|
||||
- 策展:合并重复 task 页,把次要措辞写入将来的 `aliases`(今日可先记在 task 正文)。
|
||||
- 近义但字面差大时显式 `soft_match=True`(多一次父 LLM)。
|
||||
- 策展:合并重复 task,次要措辞写入 `aliases`。
|
||||
|
||||
---
|
||||
|
||||
@@ -140,8 +132,6 @@
|
||||
|
||||
| 条目 | 关系 |
|
||||
|------|------|
|
||||
| [okf.md](../design/okf.md) O2 | 任务包精确 reuse **已完成**;本文是 O2 之上的软命中延伸 |
|
||||
| [okf.md](../design/okf.md) O4 | `kb query` / 策展可与 B(aliases)同批考虑 |
|
||||
| [ext-agent-plan.md](../design/ext-agent-plan.md) | 多轮仍只走子文件接力;软命中只影响「选哪个子文件」,不改变模式 C |
|
||||
|
||||
本文不锁定实现工期;开工前应先选 A/B/C 之一写一小段 Accepted 补丁进 `okf.md` §7.3,再改插件与金样。
|
||||
| [okf.md](../design/okf.md) O2 / O5 | 精确 reuse + A/B/C 软命中;E 向量仍待 |
|
||||
| [okf.md](../design/okf.md) O4 | `kb query` / 策展可继续扩 aliases |
|
||||
| [ext-agent-plan.md](../design/ext-agent-plan.md) | 多轮仍只走子文件接力;软命中只影响「选哪个子文件」 |
|
||||
+293
-12
@@ -344,13 +344,131 @@ Drop `<!-- … -->` blocks (including marqdo-out bodies) so parent excerpts stay
|
||||
|
||||
## lib_catalog
|
||||
|
||||
Short catalog of English stdlib modules the parent may wire into workbooks or scratch tools.
|
||||
Callable dotted names (whitelist for `CALL:lib…`) plus static module file list for workbook imports.
|
||||
|
||||
*`out` = > json.parse text={"note":"Import via frontmatter > lib/<file>.mq.md then call ## members. Prefer PATCH workbook or CALL:scratch_tool_write over inventing prose.","files":["fs.mq.md","json.mq.md","subtask.mq.md","sys.mq.md","writeback.mq.md","text.mq.md","time.mq.md","math.mq.md","net.mq.md","table.mq.md","plugin.mq.md","foreign.mq.md"]} *
|
||||
*`out` = > json.parse text={"note":"Direct CALL:lib.<mod>.<fn> + optional ARGS:{json} for whitelist only. Or import via frontmatter > lib/<file>.mq.md then PATCH / scratch_tool_write.","files":["fs.mq.md","json.mq.md","subtask.mq.md","sys.mq.md","writeback.mq.md","text.mq.md","time.mq.md","math.mq.md","net.mq.md","table.mq.md","plugin.mq.md","foreign.mq.md"],"callable":[{"name":"lib.fs.read_text","desc":"Read UTF-8 text file"},{"name":"lib.fs.exists","desc":"Path exists?"},{"name":"lib.fs.list_dir","desc":"List directory entries"},{"name":"lib.json.parse","desc":"Parse JSON text"},{"name":"lib.json.stringify","desc":"Serialize JSON"},{"name":"lib.sys.cwd","desc":"Process working directory"}]} *
|
||||
**`out`**
|
||||
|
||||
---
|
||||
|
||||
## extract_call_args
|
||||
+ `reply`=None
|
||||
|
||||
Parse `ARGS:{…}` JSON line, or a bare `{…}` line after CALL.
|
||||
|
||||
*`esc` = > json.parse text={"nl":"\n","mark":"ARGS:","brace":"{"} *
|
||||
*`nl` = > json.get value=`esc` key=nl *
|
||||
*`mark` = > json.get value=`esc` key=mark *
|
||||
*`brace` = > json.get value=`esc` key=brace *
|
||||
*`empty` = > json.parse text={} *
|
||||
1. `reply`
|
||||
*`text` = > trim value=`reply` *
|
||||
*`lines` = > split value=`text` sep=`nl` *
|
||||
*`found` = None*
|
||||
- [`line`](`lines`)
|
||||
1. `found`
|
||||
*`_` = 1*
|
||||
2. *
|
||||
*`t` = > trim value=`line` *
|
||||
*`chunks` = > split value=`t` sep=`mark` *
|
||||
*`cn` = > len value=`chunks` *
|
||||
1. `cn` > 1
|
||||
*`rest` = > at value=`chunks` index=1 *
|
||||
*`found` = > trim value=`rest` *
|
||||
2. *
|
||||
*`chunks` = > split value=`t` sep=`brace` *
|
||||
*`cn` = > len value=`chunks` *
|
||||
*`prefix` = > at value=`chunks` index=0 *
|
||||
1. `cn` > 1
|
||||
1. `prefix`
|
||||
*`_` = 1*
|
||||
2. *
|
||||
*`found` = `t`*
|
||||
2. *
|
||||
*`_` = 1*
|
||||
1. `found`
|
||||
**> json.parse text=`found`**
|
||||
2. *
|
||||
**`empty`**
|
||||
2. *
|
||||
**`empty`**
|
||||
|
||||
---
|
||||
|
||||
## tool_result_brief
|
||||
+ `value`=None
|
||||
+ `max_chars`=1024
|
||||
|
||||
Truncate a tool result for `tool_end` events (~1KiB).
|
||||
|
||||
1. `value`
|
||||
*`s` = > json.stringify value=`value` *
|
||||
**> text_head_lines text=`s` max_lines=40 max_chars=`max_chars`**
|
||||
2. *
|
||||
****
|
||||
|
||||
---
|
||||
|
||||
## plan_append_tool
|
||||
+ `events`
|
||||
+ `type`
|
||||
+ `name`
|
||||
+ `kind`
|
||||
+ `result`=None
|
||||
+ `stream`=False
|
||||
|
||||
Publish `tool_start` / `tool_end` when `stream=True`.
|
||||
|
||||
1. `stream`
|
||||
*`ev` = > json.parse text={} *
|
||||
*`ev` = > json.set map=`ev` key=type value=`type` *
|
||||
*`ev` = > json.set map=`ev` key=name value=`name` *
|
||||
*`ev` = > json.set map=`ev` key=kind value=`kind` *
|
||||
1. `result`
|
||||
*`ev` = > json.set map=`ev` key=result value=`result` *
|
||||
2. *
|
||||
*`_` = 1*
|
||||
*`events` = > json.append list=`events` item=`ev` *
|
||||
> sys.stream_publish event=`ev`
|
||||
2. *
|
||||
*`_` = 1*
|
||||
|
||||
**`events`**
|
||||
|
||||
---
|
||||
|
||||
## run_lib_call
|
||||
+ `name`
|
||||
+ `reply`=None
|
||||
|
||||
Whitelist `CALL:lib.<mod>.<fn>` (+ ARGS). Rejects net/exec/write/delete.
|
||||
|
||||
*`args` = > extract_call_args reply=`reply` *
|
||||
*`errs` = > json.parse text={"deny":"Lib CALL not allowed: "} *
|
||||
|
||||
1. `name` == lib.fs.exists
|
||||
*`path` = > json.get value=`args` key=path *
|
||||
**> fs.exists path=`path`**
|
||||
2. `name` == lib.fs.read_text
|
||||
*`path` = > json.get value=`args` key=path *
|
||||
**> fs.read_text path=`path`**
|
||||
3. `name` == lib.fs.list_dir
|
||||
*`path` = > json.get value=`args` key=path *
|
||||
**> fs.list_dir path=`path`**
|
||||
4. `name` == lib.json.parse
|
||||
*`text` = > json.get value=`args` key=text *
|
||||
**> json.parse text=`text`**
|
||||
5. `name` == lib.json.stringify
|
||||
*`value` = > json.get value=`args` key=value *
|
||||
**> json.stringify value=`value`**
|
||||
6. `name` == lib.sys.cwd
|
||||
**> sys.cwd**
|
||||
7. *
|
||||
*`deny` = > json.get value=`errs` key=deny *
|
||||
**`deny` + `name`**
|
||||
|
||||
---
|
||||
|
||||
## scratch_tool_write
|
||||
+ `name`
|
||||
+ `text`
|
||||
@@ -510,7 +628,7 @@ Parse NAME: id plus a triple-angle body after CALL:scratch_tool_write.
|
||||
+ `observation`=None
|
||||
+ `reply`=None
|
||||
|
||||
Parent Plan-and-Move tools (stdlib-aware helpers in this module).
|
||||
Parent Plan-and-Move tools (helpers + whitelist `CALL:lib…`).
|
||||
|
||||
1. `name` == workbook_read
|
||||
**> workbook_read path=`path` depth=deep**
|
||||
@@ -531,8 +649,13 @@ Parent Plan-and-Move tools (stdlib-aware helpers in this module).
|
||||
2. *
|
||||
**> json.get value=`errs` key=b**
|
||||
5. *
|
||||
*`msg` = Parent tool not allowed: *
|
||||
**`msg` + `name`**
|
||||
*`dots` = > split value=`name` sep=. *
|
||||
*`head` = > at value=`dots` index=0 *
|
||||
1. `head` == lib
|
||||
**> run_lib_call name=`name` reply=`reply`**
|
||||
2. *
|
||||
*`msg` = Parent tool not allowed: *
|
||||
**`msg` + `name`**
|
||||
|
||||
---
|
||||
|
||||
@@ -742,17 +865,97 @@ Parse `DECISION: DONE` / `CONTINUE` / `RUN` (Chinese: `决定:完成` / `继
|
||||
**CONTINUE**
|
||||
3. `found` == RUN
|
||||
**RUN**
|
||||
4. `found` == 完成
|
||||
4. `found` == REUSE
|
||||
**REUSE**
|
||||
5. `found` == NEW
|
||||
**NEW**
|
||||
6. `found` == 完成
|
||||
**DONE**
|
||||
5. `found` == 继续
|
||||
7. `found` == 继续
|
||||
**CONTINUE**
|
||||
6. `found` == 运行
|
||||
8. `found` == 运行
|
||||
**RUN**
|
||||
7. *
|
||||
9. `found` == 复用
|
||||
**REUSE**
|
||||
10. `found` == 新建
|
||||
**NEW**
|
||||
11. *
|
||||
**`found`**
|
||||
|
||||
---
|
||||
|
||||
## extract_soft_match_slug
|
||||
+ `reply`
|
||||
|
||||
Parse `SLUG: …` / `标识:…` after a soft-match REUSE decision.
|
||||
|
||||
*`esc` = > json.parse text={"nl":"\n","fw":":"} *
|
||||
*`nl` = > json.get value=`esc` key=nl *
|
||||
*`fw` = > json.get value=`esc` key=fw *
|
||||
*`text` = > trim value=`reply` *
|
||||
*`lines` = > split value=`text` sep=`nl` *
|
||||
*`found` = None*
|
||||
|
||||
- [`line`](`lines`)
|
||||
*`t` = > trim value=`line` *
|
||||
*`parts` = > split value=`t` sep=: *
|
||||
*`n` = > len value=`parts` *
|
||||
1. `n` > 1
|
||||
*`head` = > at value=`parts` index=0 *
|
||||
*`head` = > trim value=`head` *
|
||||
*`rest` = > at value=`parts` index=1 *
|
||||
*`rest` = > trim value=`rest` *
|
||||
1. `head` == SLUG
|
||||
*`found` = `rest`*
|
||||
2. `head` == 标识
|
||||
*`found` = `rest`*
|
||||
3. *
|
||||
*`_` = 1*
|
||||
2. *
|
||||
*`parts` = > split value=`t` sep=`fw` *
|
||||
*`n` = > len value=`parts` *
|
||||
1. `n` > 1
|
||||
*`head` = > at value=`parts` index=0 *
|
||||
*`head` = > trim value=`head` *
|
||||
*`rest` = > at value=`parts` index=1 *
|
||||
*`rest` = > trim value=`rest` *
|
||||
1. `head` == 标识
|
||||
*`found` = `rest`*
|
||||
2. `head` == SLUG
|
||||
*`found` = `rest`*
|
||||
3. *
|
||||
*`_` = 1*
|
||||
2. *
|
||||
*`_` = 1*
|
||||
|
||||
**`found`**
|
||||
|
||||
---
|
||||
|
||||
## build_soft_match_prompt
|
||||
+ `goal`
|
||||
+ `tasks`
|
||||
|
||||
Short parent prompt: REUSE+SLUG or NEW only.
|
||||
|
||||
*`esc` = > json.parse text={"a":"\n\n--- goal ---\n","b":"\n\n--- candidate tasks (slug | title) ---\n","c":"\n\n--- how to act ---\nExact reuse miss. Reply with ONE protocol only:\nDECISION: REUSE\nSLUG: <exact-slug-from-list>\nor\nDECISION: NEW\nNo other prose.\n","nl":"\n","sep":" | "} *
|
||||
*`a` = > json.get value=`esc` key=a *
|
||||
*`b` = > json.get value=`esc` key=b *
|
||||
*`c` = > json.get value=`esc` key=c *
|
||||
*`nl` = > json.get value=`esc` key=nl *
|
||||
*`sep` = > json.get value=`esc` key=sep *
|
||||
*`lines` = > json.parse text=[] *
|
||||
- [`t`](`tasks`)
|
||||
*`slug` = > json.get value=`t` key=slug *
|
||||
*`title` = > json.get value=`t` key=title *
|
||||
*`row` = `slug` + `sep` + `title` *
|
||||
*`lines` = > json.append list=`lines` item=`row` *
|
||||
*`catalog` = > join value=`lines` sep=`nl` *
|
||||
*`p` = Soft-match: same task intent? *
|
||||
**`p` + `a` + `goal` + `b` + `catalog` + `c`**
|
||||
|
||||
---
|
||||
|
||||
## extract_plan_summary
|
||||
+ `reply`
|
||||
|
||||
@@ -824,7 +1027,7 @@ Parent Plan-and-Move prompt. Short protocol only — no long monologues.
|
||||
*`compact` = > compact_plan_observation observation=`observation` *
|
||||
*`obs_s` = > json.stringify value=`compact` *
|
||||
*`goal_s` = > json.stringify value=`goal` *
|
||||
*`esc` = > json.parse text={"a":"\n\n--- standing ---\n","b":"\n\n--- goal ---\n","c":"\n\n--- observation ---\n","d":"\n\n--- skill brief ---\n","tools":"\n\n--- parent tools ---\nCALL:workbook_read | workbook_excerpt | lib_catalog | scratch_tool_write\nREAD:source | stderr | stdout | slots\nCreate tools: CONTINUE PATCH add ##, or CALL:scratch_tool_write with NAME line and fenced body.\n","e_rev":"\n\n--- how to act ---\nPlan-and-Move parent. Reply with ONE protocol only (no long reasoning).\n1) exit_code=0 and has_value → DECISION: DONE + one-line SUMMARY. solidify_on_done. Do not CONTINUE just because has_worker_step.\n2) Need more evidence → READ:kind or CALL:tool (then you will be re-prompted).\n3) Failure / wrong value → DECISION: CONTINUE + short PATCH (<20 lines). Never paste user prose into REPLACE.\nProtocols:\nCALL:name\nREAD:kind\nDECISION: DONE\nSUMMARY: one line\nDECISION: CONTINUE\nPATCH with FIND/REPLACE fences\n","e_dec":"\n\n--- how to act ---\nPRE-RUN decompose. Reply with ONE protocol only (no long reasoning).\n1) Skeleton OK → DECISION: RUN.\n2) Need evidence → READ:source or CALL:workbook_read / lib_catalog.\n3) Reshape → DECISION: CONTINUE + short PATCH (<20 lines).\n4) Fixed answer without LLM → PATCH to return, then RUN or DONE.\nProtocols:\nCALL:name\nREAD:kind\nDECISION: RUN\nDECISION: DONE\nSUMMARY: one line\nDECISION: CONTINUE\nPATCH with FIND/REPLACE fences\n","f":"\n\n--- explore ---\n"} *
|
||||
*`esc` = > json.parse text={"a":"\n\n--- standing ---\n","b":"\n\n--- goal ---\n","c":"\n\n--- observation ---\n","d":"\n\n--- skill brief ---\n","tools":"\n\n--- parent tools ---\nCALL:workbook_read | workbook_excerpt | lib_catalog | scratch_tool_write\nCALL:lib.fs.read_text|exists|list_dir | lib.json.parse|stringify | lib.sys.cwd (+ ARGS:{json})\nREAD:source | stderr | stdout | slots\nCreate tools: CONTINUE PATCH add ##, or CALL:scratch_tool_write with NAME line and fenced body.\n","e_rev":"\n\n--- how to act ---\nPlan-and-Move parent. Reply with ONE protocol only (no long reasoning).\n1) exit_code=0 and has_value → DECISION: DONE + one-line SUMMARY. solidify_on_done. Do not CONTINUE just because has_worker_step.\n2) Need more evidence → READ:kind or CALL:tool (then you will be re-prompted).\n3) Failure / wrong value → DECISION: CONTINUE + short PATCH (<20 lines). Never paste user prose into REPLACE.\nProtocols:\nCALL:name\nREAD:kind\nDECISION: DONE\nSUMMARY: one line\nDECISION: CONTINUE\nPATCH with FIND/REPLACE fences\n","e_dec":"\n\n--- how to act ---\nPRE-RUN decompose. Reply with ONE protocol only (no long reasoning).\n1) Skeleton OK → DECISION: RUN.\n2) Need evidence → READ:source or CALL:workbook_read / lib_catalog.\n3) Reshape → DECISION: CONTINUE + short PATCH (<20 lines).\n4) Fixed answer without LLM → PATCH to return, then RUN or DONE.\nProtocols:\nCALL:name\nREAD:kind\nDECISION: RUN\nDECISION: DONE\nSUMMARY: one line\nDECISION: CONTINUE\nPATCH with FIND/REPLACE fences\n","f":"\n\n--- explore ---\n"} *
|
||||
*`a` = > json.get value=`esc` key=a *
|
||||
*`b` = > json.get value=`esc` key=b *
|
||||
*`c` = > json.get value=`esc` key=c *
|
||||
@@ -889,11 +1092,17 @@ LLM Plan → CALL/READ/DECISION loop. Returns `{decision,reply,observation,event
|
||||
*`kind` = > json.get value=`act` key=kind *
|
||||
*`name` = > json.get value=`act` key=name *
|
||||
1. `kind` == call
|
||||
*`evs_all` = > plan_append_tool events=`evs_all` type=tool_start name=`name` kind=call stream=`stream` *
|
||||
*`tool_out` = > run_parent_tool name=`name` path=`path` observation=`last_obs` reply=`last_reply` *
|
||||
*`brief` = > tool_result_brief value=`tool_out` *
|
||||
*`evs_all` = > plan_append_tool events=`evs_all` type=tool_end name=`name` kind=call result=`brief` stream=`stream` *
|
||||
*`last_obs` = > json.set map=`last_obs` key=tool_result value=`tool_out` *
|
||||
*`left` = `left` - 1*
|
||||
2. `kind` == read
|
||||
*`evs_all` = > plan_append_tool events=`evs_all` type=tool_start name=`name` kind=read stream=`stream` *
|
||||
*`last_obs` = > plan_read_deepen observation=`last_obs` kind=`name` path=`path` *
|
||||
*`brief` = > tool_result_brief value=`last_obs` *
|
||||
*`evs_all` = > plan_append_tool events=`evs_all` type=tool_end name=`name` kind=read result=`brief` stream=`stream` *
|
||||
*`left` = `left` - 1*
|
||||
3. `kind` == decision
|
||||
*`decision` = `name`*
|
||||
@@ -1193,6 +1402,7 @@ With `stream=True`, the model call uses SSE; `echo=True` prints delta text to st
|
||||
+ `reuse`=True
|
||||
+ `optimize`=False
|
||||
+ `force`=False
|
||||
+ `soft_match`=False
|
||||
+ `promote`=True
|
||||
+ `kb_dir`=.marqdo/agent-kb
|
||||
+ `improve_every`=3
|
||||
@@ -1203,7 +1413,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/<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`.
|
||||
|
||||
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.
|
||||
Non-hit path: optional `soft_match=True` parent REUSE/NEW; else **decompose** before first child spawn (`DECISION: RUN` / `CONTINUE`+patch / solidified `DONE`). Then `await` → revise loop. With `stream=True`, emit parent `delta` / `decision` / `tool_start`/`tool_end` / `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*
|
||||
@@ -1227,7 +1437,13 @@ Non-hit path runs parent **decompose** before the first child spawn (`DECISION:
|
||||
*`match_kind` = > json.get value=`hit` key=match *
|
||||
1. `match_kind` == alias
|
||||
*`cache_label` = soft-hit*
|
||||
2. *
|
||||
2. `match_kind` == canonical
|
||||
*`cache_label` = soft-hit*
|
||||
*`hit_slug` = > json.get value=`hit` key=slug *
|
||||
> agent_kb_add_alias kb_dir=`kb_dir` slug=`hit_slug` alias=`goal`
|
||||
3. `match_kind` == soft
|
||||
*`cache_label` = soft-hit*
|
||||
4. *
|
||||
*`cache_label` = hit*
|
||||
*`lf` = > json.get value=`hit` key=llm_free *
|
||||
1. `lf`
|
||||
@@ -1311,6 +1527,71 @@ Non-hit path runs parent **decompose** before the first child spawn (`DECISION:
|
||||
2. *
|
||||
*`path` = None*
|
||||
2. *
|
||||
1. `soft_match`
|
||||
*`listed` = > agent_kb_list_tasks kb_dir=`kb_dir` *
|
||||
*`tasks` = > json.get value=`listed` key=tasks *
|
||||
*`tn` = > len value=`tasks` *
|
||||
1. `tn` > 0
|
||||
*`soft_prompt` = > build_soft_match_prompt goal=`goal` tasks=`tasks` *
|
||||
*`model` = > json.get value=`self` key=model *
|
||||
1. `stream`
|
||||
*`evs` = > `model`.complete prompt=`soft_prompt` stream=True echo=`echo` *
|
||||
*`soft_reply` = > llm.stream_result events=`evs` *
|
||||
*`events` = > plan_merge_deltas events=`events` from=`evs` stream=`stream` *
|
||||
2. *
|
||||
*`soft_reply` = > `model`.complete prompt=`soft_prompt` *
|
||||
*`soft_reply` = > trim value=`soft_reply` *
|
||||
*`soft_dec` = > extract_plan_decision reply=`soft_reply` *
|
||||
*`events` = > plan_append_decision events=`events` decision=`soft_dec` stream=`stream` *
|
||||
1. `soft_dec` == REUSE
|
||||
*`reuse_slug` = > extract_soft_match_slug reply=`soft_reply` *
|
||||
1. `reuse_slug`
|
||||
*`hit` = > agent_kb_lookup kb_dir=`kb_dir` slug=`reuse_slug` goal=`goal` tools=`tools` *
|
||||
1. `hit`
|
||||
> agent_kb_add_alias kb_dir=`kb_dir` slug=`reuse_slug` alias=`goal`
|
||||
*`cache_label` = soft-hit*
|
||||
*`path` = > json.get value=`hit` key=resource *
|
||||
*`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 *
|
||||
1. `code` == 0
|
||||
> agent_kb_record_hit kb_dir=`kb_dir` goal=`goal` tools=`tools` improve_every=`improve_every`
|
||||
*`cache` = soft-hit*
|
||||
*`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=soft-hit *
|
||||
*`sk` = > json.get value=`hit` key=skill *
|
||||
*`out` = > json.set map=`out` key=skill value=`sk` *
|
||||
*`st` = > json.get value=`hit` key=status *
|
||||
*`out` = > json.set map=`out` key=skill_status value=`st` *
|
||||
*`sum` = OKF soft_match REUSE; spawned resource *
|
||||
*`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` result=`child_val` 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*
|
||||
2. *
|
||||
*`_` = 1*
|
||||
2. *
|
||||
*`_` = 1*
|
||||
2. *
|
||||
*`_` = 1*
|
||||
2. *
|
||||
*`_` = 1*
|
||||
2. *
|
||||
*`_` = 1*
|
||||
1. `nfiles` < `explore_n`
|
||||
*`explore` = 1*
|
||||
*`cache` = explore*
|
||||
|
||||
+3
-2
@@ -215,6 +215,7 @@ description: 文档驱动智能体 — 单步 / 多步;工具经 lib/子任务
|
||||
+ `复用`=True
|
||||
+ `优化`=False
|
||||
+ `强制`=False
|
||||
+ `软命中`=False
|
||||
+ `晋升`=True
|
||||
+ `知识库目录`=.marqdo/agent-kb
|
||||
+ `改进周期`=3
|
||||
@@ -223,10 +224,10 @@ description: 文档驱动智能体 — 单步 / 多步;工具经 lib/子任务
|
||||
+ `打印增量`=False
|
||||
+ `轨迹`=False
|
||||
|
||||
多步工作簿默认写入 OKF `知识库目录/resources/<slug>.mq.md`;仅当传入 `工作簿目录` 时才用临时 runs。转发至英文 `agent.plan`。`流式` / `打印增量` / `轨迹` 对应 `stream` / `echo` / `trace`。
|
||||
多步工作簿默认写入 OKF `知识库目录/resources/<slug>.mq.md`;仅当传入 `工作簿目录` 时才用临时 runs。转发至英文 `agent.plan`。`流式` / `打印增量` / `轨迹` 对应 `stream` / `echo` / `trace`;`软命中` → `soft_match`。
|
||||
|
||||
*`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=`探索次数` stream=`流式` echo=`打印增量` trace=`轨迹`**
|
||||
**> `en`.plan goal=`目标` workbook_dir=`工作簿目录` workbook=`工作簿` max_rounds=`最大轮次` confirm=`确认分解` writeback=`写回` skeleton=`骨架` reuse=`复用` optimize=`优化` force=`强制` soft_match=`软命中` promote=`晋升` kb_dir=`知识库目录` improve_every=`改进周期` explore_n=`探索次数` stream=`流式` echo=`打印增量` trace=`轨迹`**
|
||||
+209
-17
@@ -23,6 +23,46 @@ pub fn normalize_goal(raw: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// Light rule-based canonicalize (no model): strip standing prefixes, trailing `?`/`?`/`。`.
|
||||
pub fn canonicalize_goal_str(raw: &str) -> String {
|
||||
let mut s = raw.trim().to_string();
|
||||
const PREFIXES: &[&str] = &[
|
||||
"你是一个智能体,",
|
||||
"你是一个智能体,",
|
||||
"你是一个智能体:",
|
||||
"你是一个智能体:",
|
||||
"你是一个智能体",
|
||||
"作为一个智能体,",
|
||||
"作为一个智能体,",
|
||||
"作为一个智能体:",
|
||||
"作为一个智能体:",
|
||||
"作为一个智能体",
|
||||
"You are an agent, ",
|
||||
"You are an agent,",
|
||||
"You are an agent. ",
|
||||
"You are an agent.",
|
||||
"You are an agent ",
|
||||
"You are an agent",
|
||||
"As an agent, ",
|
||||
"As an agent,",
|
||||
"As an agent ",
|
||||
"As an agent",
|
||||
];
|
||||
for p in PREFIXES {
|
||||
if let Some(rest) = s.strip_prefix(p) {
|
||||
s = rest.trim_start().to_string();
|
||||
break;
|
||||
}
|
||||
}
|
||||
while s.ends_with('?') || s.ends_with('?') || s.ends_with('。') {
|
||||
s.pop();
|
||||
while s.ends_with(|c: char| c.is_whitespace()) {
|
||||
s.pop();
|
||||
}
|
||||
}
|
||||
normalize_goal(&s)
|
||||
}
|
||||
|
||||
pub fn goal_sig_hex(material: &str) -> String {
|
||||
let mut h: u64 = 5381;
|
||||
for b in material.as_bytes() {
|
||||
@@ -561,24 +601,21 @@ pub fn goal_slug(args: &Value) -> Result<Value, String> {
|
||||
Ok(Value::String(goal_slug_str(goal)))
|
||||
}
|
||||
|
||||
pub fn kb_lookup(args: &Value) -> Result<Value, String> {
|
||||
let kb = arg_text(args, "kb_dir")?;
|
||||
pub fn kb_canonicalize(args: &Value) -> Result<Value, String> {
|
||||
let goal = arg_text(args, "goal")?;
|
||||
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 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);
|
||||
};
|
||||
Ok(Value::String(canonicalize_goal_str(goal)))
|
||||
}
|
||||
|
||||
fn lookup_hit_payload(
|
||||
kb: &str,
|
||||
root: &Path,
|
||||
skill_path: &Path,
|
||||
slug: &str,
|
||||
match_kind: &str,
|
||||
query_sig: &str,
|
||||
) -> Result<Value, String> {
|
||||
let skill_src =
|
||||
fs::read_to_string(&skill_path).map_err(|e| format!("kb_lookup read skill: {e}"))?;
|
||||
fs::read_to_string(skill_path).map_err(|e| format!("kb_lookup read skill: {e}"))?;
|
||||
let mut status = extract_fm_field(&skill_src, "status").unwrap_or_else(|| "stable".into());
|
||||
if status == "deprecated" || status == "draft" {
|
||||
return Ok(Value::Null);
|
||||
@@ -592,7 +629,7 @@ pub fn kb_lookup(args: &Value) -> Result<Value, String> {
|
||||
let hits = extract_fm_field(&skill_src, "hits")
|
||||
.and_then(|s| s.parse::<i64>().ok())
|
||||
.unwrap_or(0);
|
||||
let skill_sig = extract_fm_field(&skill_src, "sig").unwrap_or_else(|| query_sig.clone());
|
||||
let skill_sig = extract_fm_field(&skill_src, "sig").unwrap_or_else(|| query_sig.to_string());
|
||||
|
||||
let res_path = root.join("resources").join(format!("{slug}.mq.md"));
|
||||
if res_path.is_file() {
|
||||
@@ -624,15 +661,162 @@ pub fn kb_lookup(args: &Value) -> Result<Value, String> {
|
||||
}))
|
||||
}
|
||||
|
||||
fn resolve_skill_paths_by_slug(
|
||||
root: &Path,
|
||||
slug: &str,
|
||||
) -> Option<(PathBuf, PathBuf, PathBuf, String)> {
|
||||
let slug = slug.trim();
|
||||
if slug.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let task = root.join("concepts/tasks").join(format!("{slug}.md"));
|
||||
let skill = root.join("concepts/skills").join(format!("{slug}.md"));
|
||||
let res = root.join("resources").join(format!("{slug}.mq.md"));
|
||||
if task.is_file() && skill.is_file() && res.is_file() {
|
||||
Some((task, skill, res, slug.to_string()))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
pub fn kb_lookup(args: &Value) -> Result<Value, String> {
|
||||
let kb = arg_text(args, "kb_dir")?;
|
||||
let root = resolve_path(kb);
|
||||
let tools = opt_tools(args);
|
||||
|
||||
// Soft-match REUSE: resolve by explicit slug.
|
||||
if let Some(slug) = args
|
||||
.get("slug")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(str::trim)
|
||||
.filter(|s| !s.is_empty())
|
||||
{
|
||||
let Some((_task, skill_path, _res, slug)) = resolve_skill_paths_by_slug(&root, slug) else {
|
||||
return Ok(Value::Null);
|
||||
};
|
||||
let query_sig = args
|
||||
.get("goal")
|
||||
.and_then(|v| v.as_str())
|
||||
.map(|g| goal_sig_hex(&material_for(g, tools)))
|
||||
.unwrap_or_default();
|
||||
return lookup_hit_payload(kb, &root, &skill_path, &slug, "soft", &query_sig);
|
||||
}
|
||||
|
||||
let goal = arg_text(args, "goal")?;
|
||||
let goal_n = normalize_goal(goal);
|
||||
let query_sig = goal_sig_hex(&material_for(goal, tools));
|
||||
let slug_hint = goal_slug_str(goal);
|
||||
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)
|
||||
})
|
||||
.or_else(|| {
|
||||
let canon = canonicalize_goal_str(goal);
|
||||
if canon.is_empty() || canon == goal_n {
|
||||
return None;
|
||||
}
|
||||
match_kind = "canonical";
|
||||
let sig2 = goal_sig_hex(&material_for(&canon, tools));
|
||||
let slug2 = goal_slug_str(&canon);
|
||||
resolve_skill_paths(&root, &sig2, &slug2)
|
||||
.or_else(|| resolve_skill_paths_by_alias(&root, &canon))
|
||||
});
|
||||
let Some((_task, skill_path, _res, slug)) = resolved else {
|
||||
return Ok(Value::Null);
|
||||
};
|
||||
|
||||
lookup_hit_payload(kb, &root, &skill_path, &slug, match_kind, &query_sig)
|
||||
}
|
||||
|
||||
pub fn kb_list_tasks(args: &Value) -> Result<Value, String> {
|
||||
let kb = arg_text(args, "kb_dir")?;
|
||||
let limit = match args.get("limit") {
|
||||
None | Some(Value::Null) => 40usize,
|
||||
Some(Value::Number(n)) => n.as_u64().unwrap_or(40) as usize,
|
||||
Some(Value::String(s)) => s.parse().unwrap_or(40),
|
||||
_ => 40,
|
||||
}
|
||||
.clamp(1, 200);
|
||||
let root = resolve_path(kb);
|
||||
let tasks_dir = root.join("concepts/tasks");
|
||||
let mut tasks: Vec<Value> = Vec::new();
|
||||
if tasks_dir.is_dir() {
|
||||
let mut ents: Vec<_> = fs::read_dir(&tasks_dir)
|
||||
.map_err(|e| format!("list tasks: {e}"))?
|
||||
.flatten()
|
||||
.collect();
|
||||
ents.sort_by_key(|e| e.file_name());
|
||||
for ent in ents {
|
||||
if tasks.len() >= limit {
|
||||
break;
|
||||
}
|
||||
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 slug = p
|
||||
.file_stem()
|
||||
.map(|s| s.to_string_lossy().into_owned())
|
||||
.unwrap_or_default();
|
||||
if slug.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let title = extract_fm_field(&src, "title").unwrap_or_else(|| slug.clone());
|
||||
tasks.push(json!({ "slug": slug, "title": title }));
|
||||
}
|
||||
}
|
||||
let count = tasks.len() as i64;
|
||||
Ok(json!({ "tasks": tasks, "count": count }))
|
||||
}
|
||||
|
||||
pub fn kb_add_alias(args: &Value) -> Result<Value, String> {
|
||||
let kb = arg_text(args, "kb_dir")?;
|
||||
let slug = arg_text(args, "slug")?;
|
||||
let alias = arg_text(args, "alias")?;
|
||||
let alias_n = normalize_goal(alias);
|
||||
if alias_n.is_empty() {
|
||||
return Ok(json!({ "ok": false, "reason": "empty_alias" }));
|
||||
}
|
||||
let root = resolve_path(kb);
|
||||
let path = root.join("concepts/tasks").join(format!("{slug}.md"));
|
||||
if !path.is_file() {
|
||||
return Ok(json!({ "ok": false, "reason": "missing_task" }));
|
||||
}
|
||||
let src = fs::read_to_string(&path).map_err(|e| format!("read task: {e}"))?;
|
||||
let sig = extract_fm_field(&src, "sig").unwrap_or_default();
|
||||
let goal = extract_fm_field(&src, "description")
|
||||
.or_else(|| extract_fm_field(&src, "title"))
|
||||
.unwrap_or_default();
|
||||
let now = now_rfc3339();
|
||||
write_task_concept(&path, &sig, slug, &goal, &now, &[alias_n.clone()])?;
|
||||
Ok(json!({ "ok": true, "slug": slug, "alias": alias_n }))
|
||||
}
|
||||
|
||||
pub fn kb_record_hit(args: &Value) -> Result<Value, String> {
|
||||
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 tools = opt_tools(args);
|
||||
let sig = goal_sig_hex(&material_for(goal, tools));
|
||||
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)
|
||||
.or_else(|| resolve_skill_paths_by_alias(&root, &goal_n))
|
||||
.or_else(|| {
|
||||
let canon = canonicalize_goal_str(goal);
|
||||
if canon.is_empty() || canon == goal_n {
|
||||
return None;
|
||||
}
|
||||
let sig2 = goal_sig_hex(&material_for(&canon, tools));
|
||||
let slug2 = goal_slug_str(&canon);
|
||||
resolve_skill_paths(&root, &sig2, &slug2)
|
||||
.or_else(|| resolve_skill_paths_by_alias(&root, &canon))
|
||||
})
|
||||
else {
|
||||
return Ok(Value::Null);
|
||||
};
|
||||
@@ -910,4 +1094,12 @@ mod tests {
|
||||
assert_eq!(a, goal_sig_hex("hello"));
|
||||
assert_eq!(a.len(), 12);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canonicalize_strips_standing_prefix() {
|
||||
let c = canonicalize_goal_str("你是一个智能体,帮我规划明天的行程");
|
||||
assert_eq!(c, "帮我规划明天的行程");
|
||||
let c2 = canonicalize_goal_str("你是一个智能体,帮我规划明天的行程?");
|
||||
assert_eq!(c2, "帮我规划明天的行程");
|
||||
}
|
||||
}
|
||||
@@ -877,7 +877,10 @@ macro_rules! kb_ffi {
|
||||
|
||||
kb_ffi!(agent_goal_sig, kb::goal_sig, "agent_goal_sig");
|
||||
kb_ffi!(agent_goal_slug, kb::goal_slug, "agent_goal_slug");
|
||||
kb_ffi!(agent_kb_canonicalize, kb::kb_canonicalize, "agent_kb_canonicalize");
|
||||
kb_ffi!(agent_kb_lookup, kb::kb_lookup, "agent_kb_lookup");
|
||||
kb_ffi!(agent_kb_list_tasks, kb::kb_list_tasks, "agent_kb_list_tasks");
|
||||
kb_ffi!(agent_kb_add_alias, kb::kb_add_alias, "agent_kb_add_alias");
|
||||
kb_ffi!(agent_kb_promote, kb::kb_promote, "agent_kb_promote");
|
||||
kb_ffi!(agent_kb_record_hit, kb::kb_record_hit, "agent_kb_record_hit");
|
||||
kb_ffi!(
|
||||
@@ -962,9 +965,18 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
|
||||
if register(host, "agent_goal_slug", "goal", agent_goal_slug) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_canonicalize", "goal", agent_kb_canonicalize) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_lookup", "kb_dir,goal", agent_kb_lookup) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_list_tasks", "kb_dir", agent_kb_list_tasks) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_add_alias", "kb_dir,slug,alias", agent_kb_add_alias) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_kb_promote",
|
||||
|
||||
@@ -644,6 +644,37 @@ section.block > h2 {
|
||||
color: #1d1d1f;
|
||||
letter-spacing: 0.02em;
|
||||
}
|
||||
.stream-tool {
|
||||
border: 1px solid var(--line);
|
||||
border-left: 3px solid #94a3b8;
|
||||
border-radius: 8px;
|
||||
background: #f1f5f9;
|
||||
padding: 0.45rem 0.7rem;
|
||||
margin: 0.25rem 0;
|
||||
}
|
||||
.stream-tool-head {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: baseline;
|
||||
gap: 0.35rem 0.65rem;
|
||||
font: 0.78rem/1.3 var(--sans);
|
||||
color: var(--muted);
|
||||
}
|
||||
.stream-tool-head strong {
|
||||
color: #334155;
|
||||
font-weight: 600;
|
||||
}
|
||||
.stream-tool-meta {
|
||||
font: 0.72rem/1.3 var(--mono);
|
||||
color: #64748b;
|
||||
}
|
||||
.stream-tool-body {
|
||||
margin-top: 0.3rem;
|
||||
font: 0.78rem/1.4 var(--mono);
|
||||
color: #475569;
|
||||
white-space: pre-wrap;
|
||||
word-break: break-word;
|
||||
}
|
||||
.stream-child {
|
||||
border: 1px solid var(--line);
|
||||
border-left: 3px solid #2563eb;
|
||||
@@ -1156,6 +1187,35 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String {
|
||||
resetTurnRefs();
|
||||
scrollIfPinned();
|
||||
}}
|
||||
function addToolCard(ev) {{
|
||||
flushPendingAll();
|
||||
ensureTurn();
|
||||
var card = document.createElement("div");
|
||||
card.className = "stream-tool";
|
||||
var head = document.createElement("div");
|
||||
head.className = "stream-tool-head";
|
||||
var title = document.createElement("strong");
|
||||
var phase = (ev.type === "tool_end") ? "Tool end" : "Tool start";
|
||||
title.textContent = phase;
|
||||
head.appendChild(title);
|
||||
var meta = document.createElement("span");
|
||||
meta.className = "stream-tool-meta";
|
||||
var bits = [];
|
||||
if (ev.name) bits.push(ev.name);
|
||||
if (ev.kind) bits.push(ev.kind);
|
||||
meta.textContent = bits.join(" · ");
|
||||
head.appendChild(meta);
|
||||
card.appendChild(head);
|
||||
if (ev.type === "tool_end" && ev.result != null && ev.result !== "") {{
|
||||
var body = document.createElement("div");
|
||||
body.className = "stream-tool-body";
|
||||
var res = formatResult(ev.result);
|
||||
body.textContent = res.length > 1024 ? res.slice(0, 1024) + "…" : res;
|
||||
card.appendChild(body);
|
||||
}}
|
||||
turn.appendChild(card);
|
||||
scrollIfPinned();
|
||||
}}
|
||||
function addChildCard(ev) {{
|
||||
flushPendingAll();
|
||||
var card = document.createElement("div");
|
||||
@@ -1209,6 +1269,8 @@ pub fn layout(title: &str, nav: &str, main: &str) -> String {
|
||||
if (status) status.textContent = "error";
|
||||
}} else if (t === "decision") {{
|
||||
addDecision(ev);
|
||||
}} else if (t === "tool_start" || t === "tool_end") {{
|
||||
addToolCard(ev);
|
||||
}} else if (t === "round") {{
|
||||
addChildCard(ev);
|
||||
}} else if (t === "run_start") {{
|
||||
|
||||
@@ -62,3 +62,32 @@ description: Promote resource; patch Task aliases; variant goal hits same resour
|
||||
*`exact` = > agent_kb_lookup kb_dir=`kb` goal=`goal` *
|
||||
*`mk2` = > json.get value=`exact` key=match *
|
||||
> print text=`mk2`
|
||||
|
||||
*`g_trip` = 帮我规划明天的行程 *
|
||||
*`wb2` = .marqdo/agent-runs/alias-trip.mq.md *
|
||||
*`body2` = "---\ntitle: trip\n---\n\n# main\n\n**trip-ok**\n" *
|
||||
> fs.write_text path=`wb2` text=`body2`
|
||||
*`prom2` = > agent_kb_promote kb_dir=`kb` goal=`g_trip` workbook=`wb2` *
|
||||
*`ok2` = > json.get value=`prom2` key=promoted *
|
||||
1. `ok2`
|
||||
> print text=trip-promoted
|
||||
2. *
|
||||
> print text=trip-promote-fail
|
||||
|
||||
*`g_canon` = 你是一个智能体,帮我规划明天的行程 *
|
||||
*`c` = > agent_kb_canonicalize goal=`g_canon` *
|
||||
> print text=`c`
|
||||
*`hit_c` = > agent_kb_lookup kb_dir=`kb` goal=`g_canon` *
|
||||
1. `hit_c`
|
||||
> print text=canonical-ok
|
||||
2. *
|
||||
> print text=canonical-miss
|
||||
*`mk3` = > json.get value=`hit_c` key=match *
|
||||
> print text=`mk3`
|
||||
|
||||
*`listed` = > agent_kb_list_tasks kb_dir=`kb` *
|
||||
*`tc` = > json.get value=`listed` key=count *
|
||||
1. `tc` > 1
|
||||
> print text=list-ok
|
||||
2. *
|
||||
> print text=list-bad
|
||||
@@ -0,0 +1,45 @@
|
||||
---
|
||||
title: Parent CALL:lib whitelist (offline)
|
||||
description: lib.fs.exists + ARGS; reject lib.net; catalog callable.
|
||||
> ext/ai/agent.mq.md
|
||||
> lib/json.mq.md
|
||||
> lib/fs.mq.md
|
||||
> lib/sys.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*`parts` = > json.parse text={"a":"CALL:lib.fs.exists","b":"ARGS:{\"path\":\".\"}","nl":"\n"} *
|
||||
*`a` = > json.get value=`parts` key=a *
|
||||
*`b` = > json.get value=`parts` key=b *
|
||||
*`nl` = > json.get value=`parts` key=nl *
|
||||
*`t_ok` = `a` + `nl` + `b` + `nl` *
|
||||
*`act` = > agent.extract_plan_act reply=`t_ok` *
|
||||
*`kind` = > json.get value=`act` key=kind *
|
||||
*`name` = > json.get value=`act` key=name *
|
||||
> print text=`kind`
|
||||
> print text=`name`
|
||||
|
||||
*`out` = > agent.run_parent_tool name=`name` path=. reply=`t_ok` *
|
||||
1. `out`
|
||||
> print text=exists-ok
|
||||
2. *
|
||||
> print text=exists-bad
|
||||
|
||||
*`deny` = > agent.run_parent_tool name=lib.net.get path=. reply=CALL:lib.net.get *
|
||||
*`sep` = > json.parse text={"s":"not allowed"} *
|
||||
*`s` = > json.get value=`sep` key=s *
|
||||
*`dparts` = > split value=`deny` sep=`s` *
|
||||
*`pn` = > len value=`dparts` *
|
||||
1. `pn` > 1
|
||||
> print text=deny-ok
|
||||
2. *
|
||||
> print text=deny-bad
|
||||
|
||||
*`cat` = > agent.lib_catalog *
|
||||
*`callable` = > json.get value=`cat` key=callable *
|
||||
*`cn` = > len value=`callable` *
|
||||
1. `cn` > 3
|
||||
> print text=callable-ok
|
||||
2. *
|
||||
> print text=callable-bad
|
||||
@@ -0,0 +1,34 @@
|
||||
---
|
||||
title: OKF soft_match protocol parse (offline)
|
||||
description: REUSE/NEW + SLUG extractors; no network.
|
||||
> ext/ai/agent.mq.md
|
||||
> lib/json.mq.md
|
||||
---
|
||||
|
||||
# main
|
||||
|
||||
*`samples` = > json.parse text={"reuse":"DECISION: REUSE\nSLUG: trip-plan\n","new":"DECISION: NEW\n","zh":"决定:复用\n标识:行程-a\n"} *
|
||||
*`r1` = > json.get value=`samples` key=reuse *
|
||||
*`d1` = > agent.extract_plan_decision reply=`r1` *
|
||||
> print text=`d1`
|
||||
*`s1` = > agent.extract_soft_match_slug reply=`r1` *
|
||||
> print text=`s1`
|
||||
|
||||
*`r2` = > json.get value=`samples` key=new *
|
||||
*`d2` = > agent.extract_plan_decision reply=`r2` *
|
||||
> print text=`d2`
|
||||
|
||||
*`r3` = > json.get value=`samples` key=zh *
|
||||
*`d3` = > agent.extract_plan_decision reply=`r3` *
|
||||
> print text=`d3`
|
||||
*`s3` = > agent.extract_soft_match_slug reply=`r3` *
|
||||
> print text=`s3`
|
||||
|
||||
*`tasks` = > json.parse text=[{"slug":"a","title":"Alpha"},{"slug":"b","title":"Beta"}] *
|
||||
*`prompt` = > agent.build_soft_match_prompt goal=demo tasks=`tasks` *
|
||||
*`has` = > split value=`prompt` sep=DECISION: REUSE *
|
||||
*`n` = > len value=`has` *
|
||||
1. `n` > 1
|
||||
> print text=prompt-ok
|
||||
2. *
|
||||
> print text=prompt-bad
|
||||
+31
-1
@@ -1423,7 +1423,37 @@ alias-written
|
||||
alias-ok
|
||||
alias
|
||||
same-slug
|
||||
exact",
|
||||
exact
|
||||
trip-promoted
|
||||
帮我规划明天的行程
|
||||
canonical-ok
|
||||
canonical
|
||||
list-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_agent_soft_match() {
|
||||
assert_out(
|
||||
"tests/ext/agent-soft-match.mq.md",
|
||||
"REUSE
|
||||
trip-plan
|
||||
NEW
|
||||
REUSE
|
||||
行程-a
|
||||
prompt-ok",
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ext_agent_lib_call() {
|
||||
assert_out(
|
||||
"tests/ext/agent-lib-call.mq.md",
|
||||
"call
|
||||
lib.fs.exists
|
||||
exists-ok
|
||||
deny-ok
|
||||
callable-ok",
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user