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 <cursoragent@cursor.com>
This commit is contained in:
cflmyandCursor committed 2026-08-10 11:58:26 +08:00
1 parent d1ddb74235
commit 2a9fceb51c
26 files changed
+918 -54

No files matched your search

+1 -1
View File
@@ -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). |
+10 -1
View File
@@ -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)
+1 -1
View File
@@ -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** |
### 非目标
+1 -1
View File
@@ -83,7 +83,7 @@
| 语法 | `# Child = > Parent`(右侧必须是 `>` + 简单类型名;无路径) |
| `_type` | 构造后仍为**最具体子类名** |
| 方法查找 | 先子类 `##`,未命中再沿基类链;同名则子类覆盖 |
| 构造 | v1 **不**隐式调用父构造;需要时在子类正文显式 `` `self` = > Parent … `` |
| 构造 | **不**隐式调用父构造(无 auto-super)。需要父字段时在子类正文首行显式 `` `self` = > Parent … ``,再改字段并 `**`self`**`;返回 map 的 `_type` 仍为子类名 |
| 多继承 | 不支持 |
| 诊断 | 未知基类、循环继承、非法 RHS(如 `# Foo = 1`) |
+8 -2
View File
@@ -342,7 +342,13 @@ Prefer spawning `resource` over re-planning.
- `concepts/skills/<slug>.md`
- `resources/<slug>.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,避免两套真理。
+4 -1
View File
@@ -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)。
---
+3 -3
View File
@@ -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) 并行,互不阻塞。
+4 -5
View File
@@ -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 增强。
+1 -1
View File
@@ -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 |
+12 -4
View File
@@ -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 *
+7
View File
@@ -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`**
+7
View File
@@ -39,3 +39,10 @@ description: 中文系统包装(加载环境 可选 路径=)
+ `参数`=None
**> host_exec cmd=`命令` args=`参数`**
## 发布流事件
+ `事件`
向进程内 EventBus 发布流事件(供 `marqdo view` SSE)。无订阅者时为空操作。
**> host_stream_publish event=`事件`**
+158 -5
View File
@@ -164,6 +164,76 @@ fn extract_fm_field(source: &str, key: &str) -> Option<String> {
None
}
/// Parse Task FM `aliases:` as inline list `[a, b]`, comma string, or YAML `- item` block.
fn extract_fm_aliases(source: &str) -> Vec<String> {
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<String> = 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<Value, String> {
pub fn kb_lookup(args: &Value) -> Result<Value, String> {
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<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 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<Value, String> {
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<Value, String> {
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 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<Value, String> {
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<String> = 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,
+1 -1
View File
@@ -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 *
+4
View File
@@ -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",
+9
View File
@@ -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")?,
+54
View File
@@ -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<EventBus> = OnceLock::new();
pub struct EventBus {
subs: Mutex<Vec<Sender<String>>>,
}
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<String> {
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);
}
+1
View File
@@ -6,6 +6,7 @@
pub mod agent_rt;
mod dispatch;
pub mod event_bus;
pub mod foreign;
mod fs;
pub(crate) mod json;
+20 -10
View File
@@ -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<Vec<Value>, 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<R: BufRead>(
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)
}
+4 -3
View File
@@ -788,9 +788,10 @@ fn instance_type_name(v: &Value) -> std::result::Result<String, String> {
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()))]),
+258 -1
View File
@@ -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#"<div class="stream-panel">
<div class="stream-head">
<strong>Stream</strong>
<button type="button" id="stream-run">Run</button>
<span class="stream-status" id="stream-status">idle</span>
</div>
<pre class="stream-log" id="stream-log"></pre>
</div>"#
.to_string(),
LinkMode::Static { .. } => String::new(),
};
let main = format!(
r#"
<script>
@@ -1100,6 +1286,8 @@ window.MARQDO_FILE_PATH = {path_js};
<section class="block">
<h2>Execution</h2>
{stdin_panel}
{stream_panel}
{plan_card}
<div class="out {status}">{out}</div>
{plots}
</section>
@@ -1116,6 +1304,8 @@ window.MARQDO_FILE_PATH = {path_js};
rel = escape(rel),
structure_block = structure_block,
stdin_panel = stdin_panel,
stream_panel = stream_panel,
plan_card = plan_card,
out = out_text,
plots = plots_html,
source = escape(&vm.source),
@@ -1123,6 +1313,73 @@ window.MARQDO_FILE_PATH = {path_js};
layout(rel, &nav, &main)
}
/// Plan / agent result map → card with workbook link and cache/rounds.
fn plan_result_card(stdout: &str, links: &LinkMode) -> String {
let trimmed = stdout.trim();
if trimmed.is_empty() {
return String::new();
}
let candidate = trimmed
.lines()
.rev()
.find(|l| l.trim_start().starts_with('{'))
.unwrap_or(trimmed);
let Ok(v) = serde_json::from_str::<serde_json::Value>(candidate) else {
return String::new();
};
let workbook = v.get("workbook").and_then(|x| x.as_str());
let cache = v.get("cache").and_then(|x| x.as_str());
let rounds = v
.get("rounds")
.and_then(|x| x.as_u64())
.or_else(|| v.get("round").and_then(|x| x.as_u64()));
let status = v.get("status").and_then(|x| x.as_str());
if workbook.is_none() && cache.is_none() && rounds.is_none() {
return String::new();
}
let mut meta = Vec::new();
if let Some(s) = status {
meta.push(format!("status={}", escape(s)));
}
if let Some(c) = cache {
meta.push(format!("cache={}", escape(c)));
}
if let Some(r) = rounds {
meta.push(format!("rounds={r}"));
}
let link = match (workbook, links) {
(Some(wb), LinkMode::Live) => {
let href = format!("/file?path={}", urlencoding_path(wb));
format!(
r#"<a href="{href}">Open workbook</a> <code>{}</code>"#,
escape(wb)
)
}
(Some(wb), LinkMode::Static { .. }) => {
format!(r#"workbook <code>{}</code>"#, escape(wb))
}
_ => String::new(),
};
format!(
r#"<div class="plan-card"><strong>Plan result</strong><div class="plan-meta">{}</div>{}</div>"#,
meta.join(" · "),
link
)
}
fn urlencoding_path(s: &str) -> String {
let mut out = String::new();
for b in s.as_bytes() {
match *b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' | b'/' => {
out.push(*b as char);
}
_ => out.push_str(&format!("%{b:02X}")),
}
}
out
}
fn escape_js_string(s: &str) -> String {
let mut out = String::from('"');
for c in s.chars() {
+158 -10
View File
@@ -10,9 +10,12 @@ mod render;
pub use output::{write_static, OutputOptions};
use std::fs;
use std::io::{self, Read};
use std::net::SocketAddr;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::mpsc::{Receiver, RecvTimeoutError};
use std::time::Duration;
use anyhow::{bail, Context, Result};
use tiny_http::{Header, Method, Request, Response, Server, StatusCode};
@@ -38,6 +41,7 @@ pub struct DebugOptions {
pub open_browser: bool,
}
#[derive(Clone)]
pub(crate) struct ViewRoot {
pub root: PathBuf,
pub only_file: Option<PathBuf>,
@@ -60,10 +64,14 @@ pub fn serve(opts: ViewOptions) -> Result<()> {
let _ = open_url(&url);
}
// Per-request threads so SSE long-poll and `/api/run` do not block each other.
for request in server.incoming_requests() {
if let Err(err) = handle(&root_info, request) {
eprintln!("view request error: {err:#}");
}
let root = root_info.clone();
std::thread::spawn(move || {
if let Err(err) = handle(&root, request) {
eprintln!("view request error: {err:#}");
}
});
}
Ok(())
}
@@ -156,12 +164,16 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
if method == Method::Post {
let (path_part, _) = split_url(&url);
if path_part == "/api/foreign-run" {
return api_foreign_run(root, request);
match path_part {
"/api/foreign-run" => return api_foreign_run(root, request),
"/api/run" => return api_run(root, request),
_ => {
let resp =
Response::from_string("method not allowed").with_status_code(StatusCode(405));
let _ = request.respond(resp);
return Ok(());
}
}
let resp = Response::from_string("method not allowed").with_status_code(StatusCode(405));
let _ = request.respond(resp);
return Ok(());
}
if method != Method::Get {
@@ -197,6 +209,7 @@ fn handle(root: &ViewRoot, request: Request) -> Result<()> {
let json = files_json(&root.files);
respond_json(request, json)
}
"/api/events" => api_events(request),
_ => {
let resp = Response::from_string("not found").with_status_code(StatusCode(404));
let _ = request.respond(resp);
@@ -291,9 +304,9 @@ pub(crate) fn build_file_view(
stdin_lines: &[String],
live: bool,
) -> Result<FileViewModel> {
let source = fs::read_to_string(abs).with_context(|| format!("read {}", abs.display()))?;
let mut source = fs::read_to_string(abs).with_context(|| format!("read {}", abs.display()))?;
// Structure shows this file only — do not merge imported lib bodies into the tree.
let (structure, outline, input_prompts) = match crate::parse::parse_source(&source) {
let (mut structure, mut outline, input_prompts) = match crate::parse::parse_source(&source) {
Ok(module) => (
render_module_structure(&module, &source),
crate::view::render::render_function_outline(&module),
@@ -334,6 +347,17 @@ pub(crate) fn build_file_view(
),
}
};
// Re-read after run so writeback slots / workbook edits show in Structure.
if !awaiting_input {
if let Ok(next) = fs::read_to_string(abs) {
source = next;
if let Ok(module) = crate::parse::parse_source(&source) {
structure = render_module_structure(&module, &source);
outline = crate::view::render::render_function_outline(&module);
}
}
}
outline.push_str(&crate::view::render::render_writeback_outline(&source));
Ok(FileViewModel {
rel_path: rel.to_string(),
source,
@@ -524,6 +548,130 @@ fn api_foreign_run(root: &ViewRoot, mut request: Request) -> Result<()> {
respond_json(request, json)
}
/// SSE: long-lived `text/event-stream` of EventBus JSON maps (`data: …\n\n`).
fn api_events(request: Request) -> Result<()> {
let rx = crate::host::event_bus::EventBus::global().subscribe();
let body = SseBody::new(rx);
let ct = Header::from_bytes(&b"Content-Type"[..], &b"text/event-stream; charset=utf-8"[..])
.map_err(|_| anyhow::anyhow!("header"))?;
let cc = Header::from_bytes(&b"Cache-Control"[..], &b"no-cache"[..])
.map_err(|_| anyhow::anyhow!("header"))?;
let conn = Header::from_bytes(&b"Connection"[..], &b"keep-alive"[..])
.map_err(|_| anyhow::anyhow!("header"))?;
let resp = Response::empty(StatusCode(200))
.with_header(ct)
.with_header(cc)
.with_header(conn)
.with_data(body, None)
.with_chunked_threshold(0);
request.respond(resp).context("respond sse")?;
Ok(())
}
/// Background `run_file` for the live Stream panel; events go to the EventBus.
fn api_run(root: &ViewRoot, mut request: Request) -> Result<()> {
let mut body = Vec::new();
std::io::Read::read_to_end(&mut request.as_reader(), &mut body)?;
let body = String::from_utf8_lossy(&body);
let v: serde_json::Value = serde_json::from_str(&body).unwrap_or(serde_json::Value::Null);
let rel = v.get("path").and_then(|x| x.as_str()).unwrap_or("").to_string();
let stdin_raw = v.get("stdin").and_then(|x| x.as_str()).unwrap_or("");
let stdin_lines = split_stdin_text(stdin_raw);
if rel.is_empty() {
return respond_json(
request,
serde_json::json!({ "ok": false, "error": "missing path" }).to_string(),
);
}
let abs = match resolve_rel(root, &rel) {
Ok(p) => p,
Err(e) => {
return respond_json(
request,
serde_json::json!({ "ok": false, "error": format!("{e:#}") }).to_string(),
);
}
};
let root_path = root.root.clone();
let rel_clone = rel.clone();
std::thread::spawn(move || {
let start = serde_json::json!({
"type": "run_start",
"path": rel_clone,
});
crate::host::event_bus::EventBus::global().publish_json(&start.to_string());
let source = fs::read_to_string(&abs).unwrap_or_default();
let effective = effective_stdin(&source, &stdin_lines);
let mut opts = RunOptions::default();
opts.stdin_lines = effective;
opts.sleep_limit_ms = Some(30_000);
opts.fs_root = Some(root_path);
match run_file_capture(&abs, &opts) {
Ok(cap) => {
let done = serde_json::json!({
"type": "done",
"path": rel_clone,
"ok": true,
"result": cap.stdout,
});
crate::host::event_bus::EventBus::global().publish_json(&done.to_string());
}
Err(e) => {
let err = serde_json::json!({
"type": "error",
"path": rel_clone,
"message": format!("{e:#}"),
});
crate::host::event_bus::EventBus::global().publish_json(&err.to_string());
}
}
});
respond_json(
request,
serde_json::json!({ "ok": true, "started": true, "path": rel }).to_string(),
)
}
struct SseBody {
rx: Receiver<String>,
buf: Vec<u8>,
pos: usize,
}
impl SseBody {
fn new(rx: Receiver<String>) -> Self {
Self {
rx,
buf: Vec::new(),
pos: 0,
}
}
}
impl Read for SseBody {
fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
loop {
if self.pos < self.buf.len() {
let n = (self.buf.len() - self.pos).min(out.len());
out[..n].copy_from_slice(&self.buf[self.pos..self.pos + n]);
self.pos += n;
return Ok(n);
}
match self.rx.recv_timeout(Duration::from_secs(15)) {
Ok(json) => {
self.buf = format!("data: {json}\n\n").into_bytes();
self.pos = 0;
}
Err(RecvTimeoutError::Timeout) => {
self.buf = b": ping\n\n".to_vec();
self.pos = 0;
}
Err(RecvTimeoutError::Disconnected) => return Ok(0),
}
}
}
}
fn respond_html(request: Request, body: String) -> Result<()> {
let header = Header::from_bytes(&b"Content-Type"[..], &b"text/html; charset=utf-8"[..])
.map_err(|_| anyhow::anyhow!("header"))?;
+71 -4
View File
@@ -445,14 +445,81 @@ fn stmt_end_line(stmt: &Stmt) -> u32 {
fn output_card(line: u32, writebacks: &HashMap<u32, String>) -> String {
match writebacks.get(&line) {
Some(body) => format!(
r#"<div class="card output-card"><span class="badge">output</span><pre class="output-body">{}</pre></div>"#,
escape(body)
),
Some(body) => {
let badges = writeback_badges(body);
format!(
r#"<div class="card output-card" id="wb-{line}"><div class="wb-badges">{badges}</div><pre class="output-body">{}</pre></div>"#,
escape(body)
)
}
None => String::new(),
}
}
fn writeback_badges(body: &str) -> String {
let mut keys = Vec::new();
for line in body.lines() {
if let Some(rest) = line.strip_prefix('[') {
if let Some(end) = rest.find(']') {
let k = &rest[..end];
if !k.is_empty() && !keys.iter().any(|x| x == k) {
keys.push(k.to_string());
}
}
}
}
if keys.is_empty() {
return r#"<span class="badge wb-badge">output</span>"#.into();
}
keys.into_iter()
.map(|k| {
let cls = match k.as_str() {
"ok" => "wb-ok",
"error" => "wb-error",
"trace" => "wb-trace",
_ => "wb-other",
};
format!(r#"<span class="badge wb-badge {cls}">{k}</span>"#, k = escape(&k))
})
.collect::<Vec<_>>()
.join(" ")
}
/// Outline links to writeback cards (`#wb-{line}`).
pub fn render_writeback_outline(source: &str) -> String {
let map = writeback::writeback_map(source);
if map.is_empty() {
return String::new();
}
let mut lines: Vec<u32> = map.keys().copied().filter(|l| *l > 0).collect();
lines.sort_unstable();
if lines.is_empty() {
return String::new();
}
let mut out = String::from(
r#"<div class="wb-outline" id="wb-outline"><div class="wb-outline-title">Writebacks</div><ul class="outline-tree">"#,
);
for line in lines {
let body = map.get(&line).map(|s| s.as_str()).unwrap_or("");
let label = if body.contains("[error]") {
"error"
} else if body.contains("[ok]") {
"ok"
} else if body.contains("[trace]") {
"trace"
} else {
"output"
};
out.push_str(&format!(
r##"<li class="outline-item"><a href="#wb-{line}">L{line} · {label}</a></li>"##,
line = line,
label = label,
));
}
out.push_str("</ul></div>");
out
}
fn render_stmt(
stmt: &Stmt,
lines: &[ClassifiedLine],
+64
View File
@@ -0,0 +1,64 @@
---
title: OKF agent-kb alias lookup (offline)
description: Promote resource; patch Task aliases; variant goal hits same resource.
> ext/ai/agent.mq.md
> lib/fs.mq.md
> lib/json.mq.md
> lib/plugin.mq.md
> lib/sys.mq.md
---
# main
*`p` = > plugin.native_path name=agent *
1. `p`
> plugin.load path=`p`
2. *
> print text=no-agent-plugin
> sys.exit code=1
*`kb` = .marqdo/agent-kb-alias-test *
*`wb` = .marqdo/agent-runs/alias-pong.mq.md *
*`goal` = Reply with exactly the word pong and nothing else. *
*`alias` = say pong only *
*`body` = "---\ntitle: pong skill\n---\n\n# main\n\n**pong**\n" *
> fs.write_text path=`wb` text=`body`
*`prom` = > agent_kb_promote kb_dir=`kb` goal=`goal` workbook=`wb` aliases=`alias` *
*`okp` = > json.get value=`prom` key=promoted *
1. `okp`
> print text=promoted
2. *
> print text=promote-fail
*`slug` = > json.get value=`prom` key=slug *
*`parts` = > json.parse text={"a":"/concepts/tasks/","b":".md"} *
*`a` = > json.get value=`parts` key=a *
*`b` = > json.get value=`parts` key=b *
*`task` = `kb` + `a` + `slug` + `b` *
*`src` = > fs.read_text path=`task` *
*`has` = > split value=`src` sep=say pong only *
*`hn` = > len value=`has` *
1. `hn` > 1
> print text=alias-written
2. *
> print text=alias-missing
*`hit` = > agent_kb_lookup kb_dir=`kb` goal=`alias` *
1. `hit`
> print text=alias-ok
2. *
> print text=alias-miss
*`mk` = > json.get value=`hit` key=match *
> print text=`mk`
*`slug_hit` = > json.get value=`hit` key=slug *
1. `slug` == `slug_hit`
> print text=same-slug
2. *
> print text=slug-drift
*`exact` = > agent_kb_lookup kb_dir=`kb` goal=`goal` *
*`mk2` = > json.get value=`exact` key=match *
> print text=`mk2`
+29
View File
@@ -85,6 +85,16 @@ HELLO, world!",
);
}
#[test]
fn structure_inherit_explicit_super() {
assert_out(
"tests/structure/inherit-explicit-super.mq.md",
"Child
child
Ada",
);
}
#[test]
fn structure_nested_call() {
assert_out("tests/structure/nested-call.mq.md", "Hello World!");
@@ -1385,6 +1395,25 @@ fn ext_agent_kb_reuse() {
assert_out("tests/ext/agent-kb-reuse.mq.md", &expect);
}
#[test]
fn ext_agent_kb_alias() {
let status = Command::new("cargo")
.args(["build", "-p", "marqdo_plugin_agent"])
.current_dir(env!("CARGO_MANIFEST_DIR"))
.status()
.expect("build agent plugin");
assert!(status.success(), "failed to build marqdo_plugin_agent");
assert_out(
"tests/ext/agent-kb-alias.mq.md",
"promoted
alias-written
alias-ok
alias
same-slug
exact",
);
}
#[test]
fn lib_net_encode() {
assert_out("tests/lib/net-encode.mq.md", "a+b");
@@ -0,0 +1,28 @@
---
title: inherit explicit parent constructor
> lib/json.mq.md
---
# Parent
+ `name`
*`out` = > json.parse text={"role":"parent"} *
*`out` = > json.set map=`out` key=name value=`name` *
**`out`**
# Child = > Parent
+ `name`
*`self` = > Parent name=`name` *
*`self` = > json.set map=`self` key=role value=child *
**`self`**
# main
*`c` = > Child name=Ada *
*`ty` = > json.get value=`c` key=_type *
*`role` = > json.get value=`c` key=role *
*`name` = > json.get value=`c` key=name *
> print text=`ty`
> print text=`role`
> print text=`name`