Compare commits
53
Commits
ext-v1.0.4
...
v1.1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
33f8b94bb0 | ||
|
|
0ed57b6d54 | ||
|
|
c29d48f38c | ||
|
|
f1e82ce4e5 | ||
|
|
2d412e3bba | ||
|
|
46d78c6e15 | ||
|
|
81a14f9b38 | ||
|
|
88dc6d5a54 | ||
|
|
4a2d89406b | ||
|
|
d8d435fead | ||
|
|
1f8e61f3bf | ||
|
|
a9ee858f53 | ||
|
|
8489d90926 | ||
|
|
5272acaea0 | ||
|
|
cba51238b6 | ||
|
|
5fffb800f9 | ||
|
|
1a729eb9ec | ||
|
|
00c763300e | ||
|
|
224b3d4a66 | ||
|
|
81d26a265a | ||
|
|
5b599125d1 | ||
|
|
18d89d6cf2 | ||
|
|
199b5c532f | ||
|
|
db5f0e830b | ||
|
|
7a449c40b1 | ||
|
|
97fda7aa10 | ||
|
|
a49592a3ba | ||
|
|
ed9ac03c0c | ||
|
|
cb46ad7abe | ||
|
|
586975090b | ||
|
|
544805266b | ||
|
|
f3051fb536 | ||
|
|
86b183e2d7 | ||
|
|
8e23a5395a | ||
|
|
cab609ee91 | ||
|
|
b752cd49b7 | ||
|
|
2c91b5cc7b | ||
|
|
453321c8c2 | ||
|
|
981d754133 | ||
|
|
410f36f719 | ||
|
|
bd06aedb90 | ||
|
|
42ddeb14e7 | ||
|
|
6bd127a313 | ||
|
|
4b7643d833 | ||
|
|
3f96592c99 | ||
|
|
98018fc63b | ||
|
|
aeac4d6d20 | ||
|
|
9e9a80df31 | ||
|
|
5f07b1c3b1 | ||
|
|
bfea2204eb | ||
|
|
fb30cd3db3 | ||
|
|
584cb9ac9d | ||
|
|
428ee141e1 |
No files matched your search
@@ -150,3 +150,18 @@ When editing a file full of `json.set`:
|
||||
- Tables: `doc/design/stdlib-table.md` · `doc/design/table-cell-expressions.md`
|
||||
- Browser effects: `doc/roadmap/browser-wasm-e.md` · `doc/roadmap/browser-wasm-f.md`
|
||||
- Release: [marqdo-release](../marqdo-release/SKILL.md)
|
||||
|
||||
## 渐进式契约 + MLSP(Phase 2/3 落地后的开发戒律)
|
||||
|
||||
- **契约实现**:`src/contract.rs`(类型词汇 / 三形状契约表解析 / 合并 / 四边界校验诊断);
|
||||
`src/parse/mod.rs::extract_function_contract`(位置 + 形状双重判定,**在升参推断之前**执行;
|
||||
绑定表与普通文档表绝不吃 ⇒ 无契约文件零回归)。契约表从 body 移除——元数据永不执行。
|
||||
- **静态互查**:`src/check.rs`(`marqdo check [--json]`)——契约行 vs 升参推断形参双向漂移、
|
||||
未知类型名、错位契约;Error ⇒ 非零退出。
|
||||
- **AI 工具面**:`src/mlsp.rs`(`marqdo mlsp`,行分隔 JSON)——`locate` / `syntax` / `validate` /
|
||||
`repair_targets` / `schema`。语法唯一事实来源 = `parse::CORE_CONSTRUCTS`(CI 守卫与
|
||||
`doc/design/core-surface.md` 同步);MLSP 只加检索别名,**不定义语法**。
|
||||
- **准入戒律**:新特性先过 `doc/design/layers.md` 三层归属(Language 层改动须 ADR + core-surface);
|
||||
新契约形状/类型词汇 = 文档层扩展,进 `src/contract.rs`,不进词法/语法。
|
||||
- **错误出口唯一**:`src/load.rs::label_error` 已示范——装载层/任何新代码都**不许**把
|
||||
`Diagnostic` 拍平成字符串;错误链里必须能 `Diagnostic::find` 找回结构化诊断(AI/MLSP 面)。
|
||||
@@ -85,7 +85,7 @@ Checks: clean tree; `main` ff-only; smoke tests; CHANGELOG Unreleased; no secret
|
||||
|------|------|-----|-----|
|
||||
| `Cargo.toml` + `crates/marqdo-wasm/Cargo.toml` | bump | bump | — |
|
||||
| `ext/VERSION` | bump | — | bump |
|
||||
| `CHANGELOG.md` | `## vVER` | `## vVER` | `## Ext pack vVER` |
|
||||
| `CHANGELOG.md` | `## vVER`(含**核心表面 Δ**,见 `doc/design/layers.md`) | same | `## Ext pack vVER` |
|
||||
| `README.md` / `public/**` install + download table | update | update | update ext notes |
|
||||
| `.cursor/skills/marqdo/SKILL.md` | if surface changed | same | rare |
|
||||
| `.github/workflows/release.yml` Highlights stub | yes | yes | — |
|
||||
|
||||
@@ -236,7 +236,25 @@ git push https://proxy.cflmy.top/github.com/cflmy/marqdo.git vVER
|
||||
|
||||
Short GETs via the proxy often work; **long `git push` may still time out**. If push hangs or drops, skip Clash retries and go to **HK SSH jump** below.
|
||||
|
||||
`gh` against `api.github.com` may still need a direct path or HK jump (the reverse proxy is for `github.com` git, not always the API).
|
||||
`gh` / Release API can use the same path reverse-proxy:
|
||||
|
||||
| Path prefix | Upstream |
|
||||
|-------------|----------|
|
||||
| `/github.com/` | `https://github.com/` |
|
||||
| `/api.github.com/` | `https://api.github.com/` |
|
||||
| `/uploads.github.com/` | `https://uploads.github.com/` (Release asset POST; OpenResty on HK) |
|
||||
|
||||
```bash
|
||||
# Upload a Release asset through proxy.cflmy.top (Content-Type: application/octet-stream)
|
||||
curl -X POST \
|
||||
-H "Authorization: Bearer $GH_TOKEN" \
|
||||
-H "Accept: application/vnd.github+json" \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary @dist/FILE.zip \
|
||||
"https://proxy.cflmy.top/uploads.github.com/repos/cflmy/marqdo/releases/RELEASE_ID/assets?name=FILE.zip"
|
||||
```
|
||||
|
||||
Do **not** rewrite `Host: uploads.github.com` against `proxy.cflmy.top` without the `/uploads.github.com/` path prefix — that returns 404.
|
||||
|
||||
### Diagnose
|
||||
|
||||
|
||||
@@ -36,6 +36,62 @@ Canonical design (repo): [`markdown-mapping-v0.3.md`](../../doc/design/markdown-
|
||||
11. **Code-as-documentation / no bag glue:** Prefer **GFM tables** for maps, lists, wire, commands. Prefer **`table.put` / named helpers** over `json.set` / `json.append` chains. `lib/json` is for parse/stringify/quote only — not a dict builder. Unreadable json pipelines are a style bug.
|
||||
12. **Bracket-marked calls:** Function name may be marked with `[函数名]`; bare words before `[` are boolean modifiers (`礼貌 [问候] x` → `礼貌=True`). `]` immediately followed by `(` is **index** `[key](coll)`, not a call. Prefer `[k](c)` over footnote index in new code. See [bracket-call-modifiers.md](../../doc/design/bracket-call-modifiers.md).
|
||||
|
||||
## 记不住语法细节?查 MLSP(AI 必读)
|
||||
|
||||
**不必背 Marqdo 语法细节**——语法可查、程序可验、修复有界。写码前后走**查询式**工作流:
|
||||
|
||||
```bash
|
||||
printf '%s\n' '{"id":1,"method":"syntax","params":{"query":"返回"}}' | marqdo mlsp
|
||||
printf '%s\n' '{"id":2,"method":"locate","params":{"path":"a.mq.md","symbol":"加一"}}' | marqdo mlsp
|
||||
printf '%s\n' '{"id":3,"method":"validate","params":{"path":"a.mq.md"}}' | marqdo mlsp
|
||||
printf '%s\n' '{"id":4,"method":"schema","params":{"path":"a.mq.md","unit":"加一"}}' | marqdo mlsp
|
||||
printf '%s\n' '{"id":5,"method":"repair_targets","params":{"code":"contract.arg_mismatch","doc_anchor":"a.mq.md#L9-L15"}}' | marqdo mlsp
|
||||
```
|
||||
|
||||
| method | 何时用 |
|
||||
|--------|--------|
|
||||
| `syntax` | 不确定某个语法怎么写(关键字/别名 → 构造卡片:「返回」「表」「调用」「循环」…) |
|
||||
| `locate` | 找符号定义位置 + 契约;或反查构造卡片 |
|
||||
| `validate` | 提交前校验(结构化诊断数组;`ok:false` 就修) |
|
||||
| `schema` | 查单元契约(形参/返回/字段) |
|
||||
| `repair_targets` | 拿错误的**有界**修复靶点:只改锚点范围内行,越界必弃权(abstain) |
|
||||
|
||||
行分隔 JSON:每行请求 `{"id","method","params"}` → 每行响应 `{"id","ok","result"|"error"}`。
|
||||
语法唯一事实来源 = `doc/design/core-surface.md`(CI 守卫同步);卡片带 `manual` 指回文档行。
|
||||
所有错误诊断都是结构化 JSON:`code` / `severity` / `span` / `suggestion` / `doc_anchor` / `doc_quote` / `contract_ref`。
|
||||
|
||||
### 渐进式契约(可选标注;schema 就长在文档表里)
|
||||
|
||||
契约是**未绑定文档表**(绑定表是数据,永远不是契约)。三种形状、严格位置:
|
||||
|
||||
| 形状(须含 `类型` 列) | 允许位置 | 附着 |
|
||||
|--------------------------|----------|------|
|
||||
| `参数`/`类型`/`说明` 表 + `返回`/`类型`/`说明` 表 | 函数体首个可执行行之前 | 函数 |
|
||||
| `字段`/`类型`/`可空`/`说明` 表 | 紧邻 `` `名` = `` 绑定之前 | 集合(变量) |
|
||||
| `字段`/`类型`/`可空`/`说明` 表 | 对象(`#`)体首成员之前 | 对象类型 |
|
||||
|
||||
````text
|
||||
## 加一
|
||||
|
||||
对于输入变量`n`,期望是数字。
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| n | number | 输入值 |
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| | number | 加一结果 |
|
||||
|
||||
*n+1*
|
||||
````
|
||||
|
||||
- 类型词汇:`text` / `number` / `bool` / `list` / `map` / `any` / `#` 对象名(拼错 ⇒ `contract.unknown_type` + 最近建议)。
|
||||
- 契约表**永不执行**、不污染升参推断;无契约文件行为逐字节不变(动态默认,零摩擦)。
|
||||
- 运行时只在四个边界校验:调用实参 / 字段访问 `[键](集合)` / 表绑定 / 返回。
|
||||
- `marqdo check [FILE] [--json]` 静态互查:契约行 vs 升参推断形参**双向**漂移(错行=Error;新升参未覆盖=Warning)、未知类型、错位契约;有 Error 非零退出。
|
||||
- 契约诊断带 `doc_anchor` + `doc_quote`(表原文)——修复只照锚点局部改。
|
||||
|
||||
## Markup → meaning (v0.3)
|
||||
|
||||
| Marker | Meaning |
|
||||
@@ -145,7 +201,7 @@ import clock:lib/time.mq.md
|
||||
- Instance methods stay `` > `obj`.method `` (backticks on the receiver only).
|
||||
- `lib/…` resolves via `MARQDO_LIB`, cwd `lib/`, or `lib/` next to the `marqdo` binary.
|
||||
- Design: [module-namespace.md](../../doc/design/module-namespace.md).
|
||||
- Official optional extensions (`ext/`, not stdlib): install with `marqdo ext add llm|agent|web|quantum|linalg` (see `doc/design/ext-cli.md`). `ext/llm` — chat; **`ext/agent`** — document-driven agents (see **ext/agent** below); **`ext/web`** — dynamic sites (see below); **`ext/quantum`** — circuits + Q7/Q8 (see below); **`ext/linalg`** — formula-first linear algebra (see below).
|
||||
- Official optional extensions (`ext/`, not stdlib): install with `marqdo ext add llm|agent|web|quantum|linalg` (see `doc/design/ext-cli.md`). **`ext/llm`** — intelligence primitive (`ask` / `stream` / `collect`; `complete` is compat); **`ext/agent`** — document-driven agents (see **ext/agent** below); **`ext/web`** — dynamic sites (see below); **`ext/quantum`** — circuits + Q7/Q8 (see below); **`ext/linalg`** — formula-first linear algebra (see below).
|
||||
- Native plugins: `lib/plugin` — `## load` / `unload` / `list`. C ABI: `include/marqdo_abi.h`.
|
||||
- Prefer `table.put` / `表.改` for list/map element updates; keep `json` for parse/stringify/quote (see `doc/design/stdlib-table.md`).
|
||||
- Builtins (no import): `print`/`打印`, `input`/`输入`, `len`/`长度`, `str`/`文本`, `int`/`整数`; literals `True`/`真`, `False`/`假`, `None`/`空`; logic `and`/`且`, `or`/`或`, `not`/`非`.
|
||||
@@ -161,10 +217,12 @@ Install: `marqdo ext add agent` (ZH: `智能体`). Native: `cargo build --releas
|
||||
1. **Code is documentation** — the runbook `.mq.md` is the model’s ground truth, not a hidden system prompt bag.
|
||||
2. **Call site is first-class** — `build_step_context` injects path/function/line so the model knows where it is in the program.
|
||||
3. **Document is the knowledge base (OKF)** — writeback → promote/solidify → reuse / `llm_free`; MCP/corpus are evidence only.
|
||||
4. **Skill Compilation (v2)** — successful runs become `.mq.md` episodes; after enough evidence, `maybe_learn` compiles an executable skill (`llm_free`). Prefer `## run` over bare `## plan` when you want learning.
|
||||
5. **Adaptive Routing (P4)** — router is abstract (`marqdo` / `small-llm` / `llm` / `auto`); `jev` is a compat alias of `small-llm`. Decision via `router_model` (any llm handle). Policy Compilation writes `policies/router.mq.md`.
|
||||
|
||||
APIs: `# agent` / `# 智能体` → `## step` / `## plan`. Context: standing + task + **call site** + tools + budgeted source/skill + act protocol (`CALL:` / `READ:`). Inspect without LLM: `dump_step_context`. Inside `writeback=` / `写回=` branches use `host_writeback_record` (param name shadows the writeback import).
|
||||
APIs: `# agent` / `# 智能体` → `## run` / `## route` / `## step` / `## plan` (+ `resolve` / `record_episode` / `maybe_learn` / `compile` / `compile_policy` / `metrics`). Context: standing + task + **call site** + tools + budgeted source/skill + act protocol (`CALL:` / `READ:`). Inspect without LLM: `dump_step_context`. Inside `writeback=` / `写回=` branches use `host_writeback_record` (param name shadows the writeback import).
|
||||
|
||||
Examples: [agent-pong](../../examples/agent-pong/) · [agent-okf-flywheel](../../examples/agent-okf-flywheel/). Harness: `scripts/agent-harness.sh`. Design: [ext-agent.md](../../doc/design/ext-agent.md) · Wave B: [agent-framework-2026-09.md](../../doc/research/agent-framework-2026-09.md). Dev LLM: copy [.env.example](../../.env.example) → `.env` (gitignored).
|
||||
Examples: [agent-pong](../../examples/agent-pong/) · [agent-okf-flywheel](../../examples/agent-okf-flywheel/). Harness: `scripts/agent-harness.sh`. Design: [ext-agent.md](../../doc/design/ext-agent.md) · Wave B: [agent-framework-2026-09.md](../../doc/research/agent-framework-2026-09.md) · v2: [doc/next/003.md](../../doc/next/003.md) · P4: [doc/next/004.md](../../doc/next/004.md). Dev LLM: copy [.env.example](../../.env.example) → `.env` (gitignored).
|
||||
|
||||
## Official extension: `ext/web` (dynamic sites)
|
||||
|
||||
@@ -244,11 +302,14 @@ Examples: [linalg-svd](../../examples/linalg-svd/) · [linalg-least-squares](../
|
||||
|
||||
## AI authoring workflow
|
||||
|
||||
1. Decide language surface (English builtins + `lib/text.mq.md`, or Chinese + `lib/文本.mq.md`).
|
||||
2. Write `.mq.md` with `# main`, blank lines around prose, correct markers.
|
||||
3. Run: `marqdo run path/to/file.mq.md` (cwd = project root so `lib/` resolves).
|
||||
4. On `path:line:col: message`, fix that span; re-run until exit 0.
|
||||
5. Optional: `marqdo view .` / `marqdo debug .` for structure and breakpoints.
|
||||
1. **查再写**:不确定语法就 `marqdo mlsp`(`syntax` / `locate`)——不必背语法细节。
|
||||
2. Decide language surface (English builtins + `lib/text.mq.md`, or Chinese + `lib/文本.mq.md`).
|
||||
3. Write `.mq.md` with `# main`, blank lines around prose, correct markers.
|
||||
4. 可选:给函数/集合写**渐进式契约**(上节三种表格形状;写全或不写,别写一半)。
|
||||
5. Run: `marqdo run path/to/file.mq.md` (cwd = project root so `lib/` resolves).
|
||||
6. On errors:结构化诊断(含 `doc_anchor` / `suggestion`)→ `repair_targets` 取有界靶点,**只改锚点范围内**的行。
|
||||
7. 提交前:`marqdo check path/to/file.mq.md`(或 `mlsp validate`)清到零 Error。
|
||||
8. Optional: `marqdo view .` / `marqdo debug .` for structure and breakpoints.
|
||||
|
||||
## Anti-patterns (common model mistakes)
|
||||
|
||||
@@ -287,3 +348,5 @@ Examples: [linalg-svd](../../examples/linalg-svd/) · [linalg-least-squares](../
|
||||
- [ ] Browser client: `lib/browser` + tables when returning effects
|
||||
- [ ] Quantum draw: quote `kind=` / `theme=`; rebuild plugin → `ext add quantum` before view
|
||||
- [ ] `marqdo run` succeeds (or errors addressed)
|
||||
- [ ] 语法拿不准的地方已查 MLSP(`syntax`/`locate`),而不是凭记忆发明
|
||||
- [ ] 契约(如有)与升参一致:`marqdo check` 零 Error
|
||||
@@ -0,0 +1,15 @@
|
||||
## 层归属(必填,见 doc/design/layers.md)
|
||||
|
||||
- [ ] Language(`src/lex` `src/parse` `src/ast` `src/interp` `src/diagnostics` / 契约)
|
||||
- [ ] Runtime(`lib/` `plugins/` `crates/` catalog / view / debug / builtin)
|
||||
- [ ] Document(`*.mq.md` `skills/` `ext/` `public/` `examples/`)
|
||||
|
||||
## 核心表面(见 doc/design/core-surface.md)
|
||||
|
||||
- [ ] 不涉及核心标记
|
||||
- [ ] 涉及核心标记:ADR 编号 ______;core-surface.md 与 `CORE_CONSTRUCTS` 已同步(CI 守卫)
|
||||
|
||||
## 验收
|
||||
|
||||
- [ ] 相关验收(机器可验证)全绿
|
||||
- [ ] 全量回归绿(`cargo test` / `go test ./...` 视触及面)
|
||||
@@ -38,7 +38,7 @@ jobs:
|
||||
run: |
|
||||
export PATH="/c/ProgramData/mingw64/mingw64/bin:/c/ProgramData/chocolatey/lib/mingw/tools/install/mingw64/bin:$PATH"
|
||||
cargo build --release
|
||||
cargo build --release -p marqdo_plugin_agent -p marqdo_plugin_quantum -p marqdo_plugin_linalg
|
||||
cargo build --release -p marqdo_plugin_agent -p marqdo_plugin_quantum -p marqdo_plugin_linalg -p marqdo_plugin_llm
|
||||
bash ./scripts/build-web-plugin.sh
|
||||
|
||||
- name: Checkout extension sources (branch vscode-extension only)
|
||||
@@ -87,6 +87,7 @@ jobs:
|
||||
Copy-Item target/release/agent.dll "$stageNative/native/agent.dll"
|
||||
Copy-Item target/release/quantum.dll "$stageNative/native/quantum.dll"
|
||||
Copy-Item target/release/linalg.dll "$stageNative/native/linalg.dll"
|
||||
Copy-Item target/release/llm.dll "$stageNative/native/llm.dll"
|
||||
if (Test-Path "dist/$nativeZip") { Remove-Item "dist/$nativeZip" -Force }
|
||||
Compress-Archive -Path "$stageNative/*" -DestinationPath "dist/$nativeZip" -Force
|
||||
$stage = "dist/stage-bundle"
|
||||
@@ -100,6 +101,7 @@ jobs:
|
||||
Copy-Item target/release/agent.dll "$stage/ext/native/agent.dll"
|
||||
Copy-Item target/release/quantum.dll "$stage/ext/native/quantum.dll"
|
||||
Copy-Item target/release/linalg.dll "$stage/ext/native/linalg.dll"
|
||||
Copy-Item target/release/llm.dll "$stage/ext/native/llm.dll"
|
||||
if (Test-Path "dist/$zip") { Remove-Item "dist/$zip" -Force }
|
||||
Compress-Archive -Path "$stage/*" -DestinationPath "dist/$zip" -Force
|
||||
$stageLib = "dist/stage-stdlib"
|
||||
@@ -124,9 +126,12 @@ jobs:
|
||||
## Marqdo ${{ github.ref_name }}
|
||||
|
||||
### 亮点 / Highlights
|
||||
- **v1.0.3**:Ubuntu PPA + 扩展 CDN(ext.marqdo.com);CLI/扩展可分发;noble 捆绑 Rust 工具链
|
||||
- **v1.0.3**: Ubuntu PPA + extension CDN; independent CLI/ext packs; bundled Rust for noble
|
||||
- **v1.0.1**:修复 Go `libweb` 动态路由 `{param}` 查询代入、详情 Markdown、CSS 数字误报
|
||||
- **v1.1.1**:AI 栈收口——llm `ask`/`stream`/`collect` · Metadata Binding · Skill Compilation + Adaptive Routing · MCP 客户端 + resume · 可选 `plugins/llm`
|
||||
- **v1.1.1**: AI stack — llm ask/stream/collect, Metadata Binding, Skill Compilation + Adaptive Routing, MCP client + resume, optional plugins/llm
|
||||
- **v1.1.0**:AI 原生闭环三件套——核心表面守卫 · 渐进式契约 + `marqdo check` · **MLSP for AI** · `run --json`
|
||||
- **v1.1.0**: AI-native loop — core-surface guard, progressive contracts + `marqdo check`, **MLSP for AI**, `run --json`
|
||||
- **v1.0.7**:WS/网络边界加固;`url_encode` `%20`
|
||||
- **v1.0.3**:Ubuntu PPA + 扩展 CDN(ext.marqdo.com)
|
||||
- **Linux 包必出**:`*-native-x86_64-unknown-linux-gnu.zip` + Linux CLI bundle
|
||||
- Details: [CHANGELOG](https://github.com/cflmy/marqdo/blob/main/CHANGELOG.md) · [mapping v0.3](https://github.com/cflmy/marqdo/blob/main/doc/design/markdown-mapping-v0.3.md)
|
||||
|
||||
@@ -136,7 +141,7 @@ jobs:
|
||||
|-------|----------|
|
||||
| `marqdo-*-windows-msvc.exe` | CLI with **embedded** official stdlib |
|
||||
| `marqdo-*-windows-msvc.zip` | `marqdo.exe` + `lib/` + `ext/`(含 native DLL) |
|
||||
| `marqdo-*-native-*.zip` | 预编译 `web`/`agent`/`quantum`/`linalg`(Windows DLL + **Linux .so 必出**) |
|
||||
| `marqdo-*-native-*.zip` | 预编译 `web`/`agent`/`quantum`/`linalg`/`llm`(Windows DLL + **Linux .so 必出**) |
|
||||
| `marqdo-*-unknown-linux-gnu.zip` | Linux CLI + `lib/` + `ext/` + native `.so` |
|
||||
| `marqdo-*-stdlib.zip` | `lib/` only |
|
||||
| `marqdo-*-ext.zip` | Official `ext/` L1 sources |
|
||||
@@ -181,7 +186,7 @@ jobs:
|
||||
run: |
|
||||
set -euo pipefail
|
||||
cargo build --release
|
||||
cargo build --release -p marqdo_plugin_agent -p marqdo_plugin_quantum -p marqdo_plugin_linalg
|
||||
cargo build --release -p marqdo_plugin_agent -p marqdo_plugin_quantum -p marqdo_plugin_linalg -p marqdo_plugin_llm
|
||||
bash ./scripts/build-web-plugin.sh
|
||||
|
||||
- name: Package Linux native zip + CLI bundle
|
||||
@@ -190,7 +195,7 @@ jobs:
|
||||
VER="${GITHUB_REF_NAME#v}"
|
||||
TARGET="x86_64-unknown-linux-gnu"
|
||||
mkdir -p dist/stage-native/native
|
||||
for lib in libweb.so libagent.so libquantum.so liblinalg.so; do
|
||||
for lib in libweb.so libagent.so libquantum.so liblinalg.so libllm.so; do
|
||||
src="target/release/${lib}"
|
||||
if [[ ! -f "$src" && "$lib" == libweb.so ]]; then
|
||||
src="plugins/web/build/libweb.so"
|
||||
|
||||
@@ -84,3 +84,6 @@ plugins/*/tmp/
|
||||
|
||||
# ephemeral sqlite / test artifacts
|
||||
tests/ext/:memory:
|
||||
|
||||
# local secrets
|
||||
.env
|
||||
+157
@@ -8,6 +8,163 @@
|
||||
|
||||
### Changed
|
||||
|
||||
## v1.1.1 — 2026-09-24
|
||||
|
||||
### Highlights
|
||||
|
||||
**AI 栈收口(agent + llm)**:语义 `ask`/`stream`/`collect`、Artifact Metadata Binding、Skill Compilation + Adaptive Routing、MCP 客户端与 resume 检查点、可选 `plugins/llm` Transport ABI。
|
||||
本版 CLI 与扩展包 **同步为 1.1.1**。
|
||||
|
||||
- **LLM 原语**:`ext/ai/llm` 从 OpenAI HTTP 客户端提升为 `ask`/`stream`/`collect` 智能原语(`complete` 保留兼容);`LLMResult.tool_calls`;可选原生 `llm_chat_completions`(无 `.so` 时回退 `lib/net`)。
|
||||
- **Metadata Binding**:frontmatter `${env|arg|sys|secret}` 加载期解析;Phase 2 将绑定键注入入口 env(正文禁止 `${…}`);`sys.meta` / `--bind`;llm ctor 优先 meta。
|
||||
- **Agent**:Skill Compilation(episode → llm_free skills)+ Adaptive Routing(`marqdo` / `small-llm` / `llm` / `auto`;`jev` 为 `small-llm` 兼容别名);MCP **stdio 客户端**;resume 检查点;零 LLM 金样路径。
|
||||
- **度量与后端**:`model_turn` usage、named handles(fast/reasoning)、`type:prompt` 加载、openai/ollama 分流。
|
||||
|
||||
```bash
|
||||
# Ubuntu PPA / 源码
|
||||
sudo add-apt-repository ppa:cflmy/marqdo && sudo apt update && sudo apt install marqdo
|
||||
git checkout v1.1.1 && cargo build --release
|
||||
marqdo ext add llm && marqdo ext add agent
|
||||
```
|
||||
|
||||
**核心表面 Δ(Core surface Δ)**:**无增删**——19 个核心构造不变。本版为 **Runtime**(Metadata Binding、LLM/agent 插件与扩展)变更,不触碰语言标记。
|
||||
|
||||
### Added
|
||||
- **`ext/ai/llm` 语义表面**:`ask` / `stream` / `collect`(`complete` 兼容);`LLMResult.tool_calls`(OpenAI-shaped `choices[0].message.tool_calls`);`sys.module_source` + 入口文档 `prompt_body` 离线路径。
|
||||
- **`plugins/llm` Transport ABI**(`llm_chat_completions`):`ext/ai/llm/openai` 优先原生插件,否则 `lib/net` HTTP/SSE。
|
||||
- **Artifact Metadata Binding**:frontmatter `${env|arg|sys|secret}` → `Module.metadata`;Secret 掩码;`sys.meta` / `--bind`;Phase 2 入口 env 注入(正文拒绝 `${…}`;字节码 prologue 同源)。
|
||||
- **Agent Skill Compilation + Adaptive Routing**:episode → `maybe_learn` → llm_free skills;路由后端 `marqdo` / `small-llm` / `llm` / `auto`(`jev` = `small-llm` 兼容别名,不再探测 `MARQDO_JEV`)。
|
||||
- **Agent MCP stdio 客户端**(`agent_mcp_client` / `mcp_connect` / `mcp_client_list` / `mcp_client_call`)+ Python mock 离线金样。
|
||||
- **Agent resume 检查点**(`.marqdo/agent-resume/<id>.json` — save/load/clear/list;`run` 可设 `resume_id=`)。
|
||||
- **AI 栈度量与后端**:`model_turn` usage、named handles(fast/reasoning)、`type:prompt` 加载、openai/ollama 分流、agent-zero-llm 金样。
|
||||
|
||||
### Fixed
|
||||
- **plan force 不再冲掉已有 `resources` workbook**(保留 llm_free 资源;金样 `agent-plan-preserve`)。
|
||||
- **compiled-skill Marqdo 语法**:llm_free await 不再误落到 plan+HTTP。
|
||||
|
||||
### Changed
|
||||
- Adaptive Routing:决策面为 `router_model`(任意 llm handle);`jev` 降为 `small-llm` 文档/兼容别名。
|
||||
|
||||
## v1.1.0 — 2026-09-23
|
||||
|
||||
### Highlights
|
||||
|
||||
**AI 原生闭环三件套落地**:核心表面守卫(问题一)· 渐进式契约(问题二)· MLSP for AI(问题三)。
|
||||
`marqdo check` 静态契约防漂移、`marqdo mlsp` 让 AI **按需查询**语法而非背诵、`run --json` 统一结构化诊断(错误即数据,供 AI 直接消费)。
|
||||
本版 CLI 与扩展包 **同步为 1.1.0**。
|
||||
|
||||
- **核心要小(问题一)**:`doc/design/core-surface.md` 列出 19 个核心构造 + `CORE_CONSTRUCTS` 守卫测试——任一侧(清单/文档/金样)单边改动 CI 必红;三层架构(Core / Runtime / Document)入 `doc/design/layers.md`。
|
||||
- **语义校验(问题二)**:渐进式契约表(`参数`/`返回`/`字段`/集合)嵌入文档叙述;四边界校验(调用实参 / 返回值 / 表绑定 / 键访问);`marqdo check` 静态防漂移(类型 typo / 参数名漂移 / 重复契约必报)。
|
||||
- **AI 可靠反馈 + 不必学语法(问题三)**:`marqdo mlsp`(MLSP,行分隔 JSON over stdio)提供 `locate` / `syntax` / `validate` / `repair_targets` / `repair_apply` / `schema`;`syntax` 卡片机械携带戒律原文 / 样例 / 文档锚点,AI 无需预学语法。修复护栏:有界行编辑、越界必拒(abstain)。
|
||||
- **验证(P4)**:锚点消融 anchor 一轮修复率 **100%**(双模型,plain 85%/90%);契约消融拦截 100% / 误报 0%;防漂移必报 9/9;核心守卫演练 3/3 必红。详见 [perf-validation-report-2026-09-23.md](doc/roadmap/perf-validation-report-2026-09-23.md)。
|
||||
|
||||
```bash
|
||||
# Ubuntu PPA / 源码
|
||||
sudo add-apt-repository ppa:cflmy/marqdo && sudo apt update && sudo apt install marqdo
|
||||
git checkout v1.1.0 && cargo build --release
|
||||
marqdo ext add web
|
||||
```
|
||||
|
||||
**核心表面 Δ(Core surface Δ)**:**无增删**——19 个核心构造(`heading` / `bold-code` / `italic-return` / `param-list` / `call-quote` / `branch-list` / `loop-list` / `table-bind` / `footnote-index` / `link-index` / …)与 v1.0.7 完全一致,未新增标记。本版为 **Runtime 层(契约校验 / MLSP / 诊断 JSON)** 与 **Document 层(core-surface / layers)** 变更,不触碰语言核心。
|
||||
|
||||
### Added
|
||||
- **MLSP(`marqdo mlsp`)**:面向 AI 的语言服务协议(行分隔 JSON over stdio)——`locate` / `syntax` / `validate` / `repair_targets` / `repair_apply` / `schema`;`syntax` 卡片含 `semantics` / `rule` / `sample` / `doc_quote`(自持,机器可读戒律原文)。
|
||||
- **`marqdo check`**:静态契约漂移检查(未知类型词 / 参数名漂移 / 返回越约 / 重复契约表 / 错位契约表)。
|
||||
- **渐进式契约(Progressive contracts)**:文档叙述内嵌契约表(`参数`/`返回`/`字段` + 集合 `字段`),四边界校验(调用实参 / 返回值 / 表绑定 / 键访问);被绑定的 `字段|类型` 表是**数据**、永不误判为契约。
|
||||
- **`marqdo run --json`**:统一结构化诊断(`code` / `severity` / `suggestion` / `doc_anchor` / `doc_quote` / `contract_ref`),错误即数据。
|
||||
- **修复护栏**:`mlsp repair_apply` 有界行编辑——只允许落在 `doc_anchor` 指定范围内的编辑,越界整体拒绝(all-or-nothing / abstain)。
|
||||
- **守卫与文档**:`doc/design/core-surface.md`(19 构造)、`doc/design/layers.md`(三层架构)、`tests/core_surface.rs`(守卫生效验证)。
|
||||
|
||||
### Fixed
|
||||
- **错误链不再扁平化**:`src/load.rs` 以 `label_error` 保留结构化 `Diagnostic`("Error Exit Only" 原则),修复契约类错误被降级为字符串的回归。
|
||||
- **契约 parse 边界**:`interp.rs` 键访问优先给出 `index_diag`(索引/键诊断先于取值),空字段 vs 缺字段语义消歧。
|
||||
|
||||
### Changed
|
||||
- **`.cursor/skills/marqdo`**:由「背诵式压缩操作手册」改为**查询式工作流**——写码前 `mlsp syntax` / `locate`,提交前 `mlsp validate` + `marqdo check`,修复照 `doc_anchor` 局部改。
|
||||
- **契约表一经出现即为完整声明**(新推断的升参未覆盖 ⇒ `contract.param_drift` 警告,不静默)。
|
||||
- 取元**主推** `[键](集合)`(`link-index`);`` `集合`[^键] ``(`footnote-index`)保留兼容。
|
||||
|
||||
### 已知未决(诚实标注)
|
||||
- **P4 实验 (b) 盲测未达标**:「查询式 vs 全文档背诵」四判据未全立(`deepseek-flash`:docs 4/6 vs query 0/3;端点 infra 削样本)。查询机制本身已验证无缺陷,瓶颈在**模型自查纪律**(不知道自己不知道 ⇒ 不查询)——协议改进(写码前强制自检 / 陌生构造强制查询)列入下一阶段。见 [perf-validation-report-2026-09-23.md](doc/roadmap/perf-validation-report-2026-09-23.md) §3。
|
||||
|
||||
## Unreleased
|
||||
|
||||
## v1.0.7 — 2026-09-22
|
||||
|
||||
### Highlights
|
||||
|
||||
**WS / 网络边界加固**:WebSocket 握手同源校验、broadcast/room 默认需登录(`require_auth`/`需要登录` 可显式关闭)、消息 1 MiB × 256 条上限、空 room 回收;HTTP 响应 8 MiB 在读流时截断;`url_encode` 空格 `%20`。本版 CLI 与扩展包 **同步为 1.0.7**。
|
||||
|
||||
- **安全默认**:浏览器 WS 握手仅同源;有状态 fan-out 端点未登录 401 / 公开直播流显式 `require_auth=False`
|
||||
- **资源上限**:WS 单条 1 MiB、单连接 256 条;HTTP/SSE 8 MiB 读流截断——对端无法借大响应耗尽内存
|
||||
- **测试**:P3 金样对齐 quiet-admin 门禁语义;wasm smoke 以 ESM 加载官方 bridge
|
||||
|
||||
```bash
|
||||
# Ubuntu PPA / 源码
|
||||
sudo add-apt-repository ppa:cflmy/marqdo && sudo apt update && sudo apt install marqdo
|
||||
git checkout v1.0.7 && cargo build --release
|
||||
marqdo ext add web
|
||||
```
|
||||
|
||||
### Fixed
|
||||
- **WS 安全默认**:握手改为同源校验(不再放行任意 Origin);`route_ws` 新增 `require_auth`/`需要登录`,broadcast/room 默认要求登录会话,公开直播流需显式设假,未认证握手 401。
|
||||
- **WS 资源上限**:单条消息 1 MiB、单连接最多收集 256 条回复;传输错误/超时返回 `{ok:false, error}`,不再伪装空成功;空 room 自动回收(防 URL 派生 room 名耗尽内存)。
|
||||
- **HTTP 响应上限**:8 MiB 上限在读流时截断(含错误路径与 SSE 错误路径),上游无法借超大响应耗尽内存。
|
||||
|
||||
### Changed
|
||||
- `net.url_encode`:空格编码为 `%20`(原 `+`),URL 段语义下与字面 `+` 消歧义。
|
||||
- `web`/`网页` 文档同步:`route_ws` 的 `require_auth`/`需要登录` 与 `ws.connect` 的收集上限语义。
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Added
|
||||
|
||||
### Fixed
|
||||
|
||||
### Changed
|
||||
|
||||
## Ext pack v1.0.6 — 2026-09-18
|
||||
|
||||
### Highlights
|
||||
|
||||
**ext/web OIDC、表单删除与安静管理台(CLI 仍为 v1.0.3)**:OIDC 登录/登出回首页、表单 `删除` 动作、默认关闭内置 admin/RBAC desk、独立 Redis 会话 URL、Postgres CRUD。安装:
|
||||
|
||||
```bash
|
||||
marqdo ext add web # CDN latest → 1.0.6
|
||||
```
|
||||
|
||||
### Added
|
||||
- **OIDC**:`auth.oidc` / 网页鉴权 OIDC;登录入口与 `next` 回跳。
|
||||
- **表单删除**:`form` 动作 `删除`/`delete`(通用 CRUD,无站点特判)。
|
||||
- **列表/路由**:请求上下文、列表装配延续;注销默认回 `/` 避免 IdP 再登录环。
|
||||
|
||||
### Fixed
|
||||
- 内置 admin / RBAC desk 默认安静,直至显式 `admin` / `enable_rbac`。
|
||||
- 静态资源缓存与 WASM 启动延迟;会话 CSRF 与 Postgres 方言。
|
||||
|
||||
### Changed
|
||||
- 扩展包 SemVer **1.0.6**(`ext/VERSION`);与 CLI **v1.0.3** 可独立安装。
|
||||
|
||||
## Ext pack v1.0.5 — 2026-09-18
|
||||
|
||||
### Highlights
|
||||
|
||||
**ext/web 请求上下文与表单来源(CLI 仍为 v1.0.3)**:字段 `来源`/`source`(session / route / now)、server-only 盖戳、页面 `列表装配`/`compose_list`;禁止按业务表特判。安装:
|
||||
|
||||
```bash
|
||||
marqdo ext add web # CDN latest → 1.0.5
|
||||
```
|
||||
|
||||
### Added
|
||||
- **Request context**:表单提交携带 `_mq_params` / `_mq_return`;`ApplySources` 从 session/route/now 写入。
|
||||
- **字段来源列**:`client`(默认)/ `session.username` / `route.NAME` / `now`;非 client 不渲染且覆盖客户端伪造。
|
||||
- **列表装配**:`compose_list` / `列表装配`(副绑定 + 条件/排序/插槽),详情页可同页渲染子列表。
|
||||
- 设计文:[ext-web-request-context.md](doc/design/ext-web-request-context.md);金样 `ext_web_form_source_live`。
|
||||
|
||||
### Changed
|
||||
- 扩展包 SemVer **1.0.5**(`ext/VERSION`);与 CLI **v1.0.3** 可独立安装。
|
||||
- 移除评论表名特判(`enrichCommentSubmit`)。
|
||||
|
||||
## Ext pack v1.0.4 — 2026-09-17
|
||||
|
||||
### Highlights
|
||||
|
||||
Generated
+12
-2
@@ -572,7 +572,7 @@ checksum = "0ceec5bc11778974d1bcb055b18002eba7f4b3518b6a0081b3af5f21666da9ad"
|
||||
|
||||
[[package]]
|
||||
name = "marqdo"
|
||||
version = "1.0.3"
|
||||
version = "1.1.1"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"assert_cmd",
|
||||
@@ -603,7 +603,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "marqdo-wasm"
|
||||
version = "1.0.3"
|
||||
version = "1.1.1"
|
||||
dependencies = [
|
||||
"marqdo",
|
||||
"serde_json",
|
||||
@@ -632,6 +632,16 @@ dependencies = [
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "marqdo_plugin_llm"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"idna_adapter",
|
||||
"serde_json",
|
||||
"ureq",
|
||||
"zeroize",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "marqdo_plugin_quantum"
|
||||
version = "0.1.0"
|
||||
|
||||
+4
-2
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "marqdo"
|
||||
version = "1.0.3"
|
||||
version = "1.1.1"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
description = "Marqdo — Markdown markers as an interpreted programming language"
|
||||
@@ -50,13 +50,15 @@ include_dir = "0.7"
|
||||
libc = { version = "0.2", optional = true }
|
||||
|
||||
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||
# No `wasmbind`: keeps cdylib free of wasm-bindgen imports (raw mq_* ABI).
|
||||
# Custom entropy — no `js`/wasm-bindgen imports (raw mq_* ABI + marqdo-bridge.js).
|
||||
chrono = { version = "0.4", default-features = false, features = ["alloc", "std"] }
|
||||
getrandom = { version = "=0.2.17", default-features = false, features = ["custom"] }
|
||||
|
||||
[workspace]
|
||||
members = [
|
||||
"plugins/demo",
|
||||
"plugins/agent",
|
||||
"plugins/llm",
|
||||
"plugins/quantum",
|
||||
"plugins/linalg",
|
||||
"crates/marqdo-wasm",
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
# Marqdo
|
||||
|
||||
**代码即文档。文档即代码。文档即知识库。**
|
||||
**代码即文档。文档即知识库。知识即可执行。**
|
||||
|
||||
Marqdo 把 Markdown **标记当作编程语法**:同一个 `.mq.md` 文件,既是给人读的文稿,也是给机器跑的程序。写文档的过程就是在写可执行逻辑——大型项目里,说明、示例与实现不再分叉成三份真相。
|
||||
Marqdo 把 Markdown **标记当作编程语法**:同一个 `.mq.md` 文件,既是给人读的文稿,也是给机器跑的程序。写文档的过程就是在写可执行逻辑——大型项目里,说明、示例与实现不再分叉成三份真相。它不是「又一个 Markdown 编程语言」,而是**文档原生的编程运行时(Document-native runtime)**——一门关于**可执行知识(Executable Knowledge)**的语言。
|
||||
|
||||
欢迎访问 [marqdo 官方网站](https://www.marqdo.com/) 阅读更多特性与可执行文档。
|
||||
|
||||
@@ -10,6 +10,47 @@ Marqdo 把 Markdown **标记当作编程语法**:同一个 `.mq.md` 文件,
|
||||
|
||||
---
|
||||
|
||||
## 核心优势:可执行知识(Executable Knowledge)
|
||||
|
||||
> **Knowledge That Can Run —— 知识即可执行。**
|
||||
> Marqdo 不是「用 Markdown 写程序」这么简单:它让一个 `.mq.md` 文档**同时**是文稿、知识与程序——同一份结构,同时服务于人、解释器、工具与 AI。
|
||||
|
||||
```text
|
||||
.mq.md(同一份文件)
|
||||
┌─────────┼─────────┐
|
||||
文稿 知识 程序
|
||||
└─────────┼─────────┘
|
||||
▼
|
||||
Marqdo 运行时
|
||||
▼
|
||||
可执行知识(Executable Knowledge)
|
||||
```
|
||||
|
||||
| 真正的核心优势 | 说明 |
|
||||
|------|------|
|
||||
| **知识-代码一致性**<br>Knowledge-Code Coherence | 说明、数据、示例与实现**同一份源文件**:文档里的表格就是程序的数据,文档里的调用就是真实执行。「文档过期」不再是悄悄漂移,而是可被机器发现的**知识不一致**——知识陈述本身可以被测试。 |
|
||||
| **默认安全,AI 友好** | 无标记文字默认是叙述(注释),只有显式标记才进入执行。AI 自由发挥**不会污染**文档与代码的边界——这比「自然语言编程」安全得多,是 AI 原生语言的重要前提。 |
|
||||
| **multi-view artifact**<br>`.mq.md` 双后缀即开放接口 | `.mq` 告诉解释器「可执行」,`.md` 告诉整个 Markdown 生态「仍是文档」。GitHub 直接读、VS Code 直接改、搜索引擎直接索引、LLM 直接理解、Marqdo 直接运行。**Marqdo 不占有知识,只是给 Markdown 增加执行语义。** |
|
||||
| **表格即数据结构** | 程序的数据结构直接就是文档的数据结构:单列竖表 → `List`;横表(1 数据行)→ `Map`;横表(多数据行)→ `Map` of `List`(列向聚合);首列写 `@` / `行` / `row` → 行向记录 `List` of `Map`。取元 `[键](集合)` 也是 Markdown 链接形。这是 literate programming 的下一步。 |
|
||||
| **语义干净** | **返回是架构,打印只是函数**:`*…*` 改变控制流,`print` 只是副作用函数;分支与循环长在 Markdown 结构上(`1.` `2.` 递增 = 分支,`-` = 循环),不是所有列表都被粗暴当成 if。 |
|
||||
| **AI 的可靠上下文与长期记忆** | 一个 `.mq.md` 同时是 documentation + schema + code + examples + knowledge + executable behavior;AI 不必再拼装 README + OpenAPI + JSON Schema + prompt。整个工程天然就是 AI 的**世界模型**——比 RAG 更接近 Executable RAG。 |
|
||||
| **完整工程,不是玩具** | Rust 正经解释器(词法 / 语法 / 语义 / 树遍历 + 字节码后端)、stdlib、模块与对象、debugger、viewer、catalog、WASM、原生插件 ABI、VS Code 扩展、agent / LLM / MCP。 |
|
||||
|
||||
---
|
||||
|
||||
## 愿景与期许
|
||||
|
||||
不把「AI 能写 Marqdo」当终点,而是 **「AI 能可靠地生成可验证的 Marqdo」**:
|
||||
|
||||
1. **AI 可验证语言**:`LLM → Marqdo AST → 语义检查 → 结构化诊断 → LLM 修复 → 合格程序`。让类型与语义检查成为 AI 编程闭环里的强约束,堵住「语义看似合理、类型实则不对」的生成。
|
||||
2. **Marqdo Schema / Language Server for AI**:机器可读的语言 Schema(`get_syntax` / `validate` / `repair` …),AI 不必读千行文档猜语法,而是 discover → construct → validate → execute → inspect。
|
||||
3. **程序即 AI 长期记忆**:`.mq.md` 工程 = 可执行知识库 = AI 的世界模型(Executable Knowledge Representation)。
|
||||
4. **可执行知识图**:`.mq.md` 之间的 Markdown 链接天然长成文档图 + 知识图 + 程序图(Executable Knowledge Graph)。
|
||||
5. **知识可测试**:文档中的示例表自动执行;知识与执行结果不一致会被机器报告——Knowledge-Code Coherence 从理念变为日常。
|
||||
6. **可靠性基准**:让同一个 AI 分别用 Python / TypeScript / Marqdo 完成真实任务,测量生成长度、语法 / 语义错误率、修复轮次、token 消耗与人类阅读 / 修改成本——用实验数据证明「文档型语法降低 LLM 生成熵」。
|
||||
|
||||
---
|
||||
|
||||
## 为什么重要
|
||||
|
||||
| 痛点 | Marqdo 的答案 |
|
||||
@@ -38,7 +79,7 @@ Marqdo 把 Markdown **标记当作编程语法**:同一个 `.mq.md` 文件,
|
||||
| 叙述 `` `名` `` / `` `名`=默认 `` | 形参由体推断(`` + `名` `` 仍可用) |
|
||||
| `1.` … | 体内 `1.` `2.` … = 分支(`N. *` = else) |
|
||||
| `-` | 循环(`` - `条件` `` 或 `` - [项](集合) ``) |
|
||||
| 表格 | 集合;取元优先 `[键](集合)` |
|
||||
| 表格 | 集合(几何即类型,见下);取元优先 `[键](集合)` |
|
||||
| 单独一行 `---` / `***` | **叙述分隔**(跳过;不是函数收束) |
|
||||
|
||||
```markdown
|
||||
@@ -58,6 +99,8 @@ Marqdo 把 Markdown **标记当作编程语法**:同一个 `.mq.md` 文件,
|
||||
对于输入变量`n`,执行**n=n+1**接着返回*n*。
|
||||
```
|
||||
|
||||
**表格几何即类型**:单列竖表 → `List`;横表(1 数据行)→ `Map`;横表(多数据行)→ `Map` of `List`(列向聚合);首列写 `@` / `行` / `row` → 行向记录 `List` of `Map`。详见 [tables-maps-footnotes.md](doc/roadmap/tables-maps-footnotes.md)。
|
||||
|
||||
完整约定:[markdown-mapping-v0.3.md](doc/design/markdown-mapping-v0.3.md)。
|
||||
|
||||
---
|
||||
@@ -68,7 +111,8 @@ Marqdo 把 Markdown **标记当作编程语法**:同一个 `.mq.md` 文件,
|
||||
2. **返回是架构;打印只是函数。**
|
||||
3. **代码即文档即知识库**:文稿可执行,执行可回看结构,结构可导航与调试。
|
||||
4. **对齐 [OKF](https://github.com/GoogleCloudPlatform/knowledge-catalog/blob/main/okf/SPEC.md)(Open Knowledge Format)方向**:清单由工具从 `.mq.md` **自动生成**,不是手填配置——见下节与 [catalog-cli.md](doc/design/catalog-cli.md)。
|
||||
5. **正经解释器(Rust)**:词法/行分类 → 语法 → 语义 → 树遍历(及字节码);**不用 Flex/Bison**。见 [路线图](doc/roadmap/interpreter.md)。
|
||||
5. **正经解释器(Rust)**:词法/行分类 → 语法 → 语义 → 树遍历(及字节码);**不用 Flex/Bison**。见 [路线图](doc/roadmap/interpreter.md)。
|
||||
6. **语言核心保持小**:Language(标记映射 / AST / 语义 / 类型)、Runtime(stdlib / 插件 ABI / WASM / agent)、Document(知识 + 代码 + UI + AI)三层分明——生态功能不淹没语言身份。
|
||||
|
||||
---
|
||||
|
||||
@@ -98,8 +142,10 @@ marqdo catalog public -o .marqdo
|
||||
|
||||
---
|
||||
|
||||
## 现状(v1.0.3)
|
||||
## 现状(v1.1.1)
|
||||
|
||||
- **AI 栈(v1.1.1)**:`ext/ai/llm` 语义 `ask`/`stream`/`collect` · Artifact Metadata Binding(`${env|arg|sys|secret}`)· Agent Skill Compilation + Adaptive Routing · MCP stdio 客户端 + resume 检查点 · 可选 `plugins/llm` Transport ABI
|
||||
- **AI 原生闭环(v1.1.0)**:核心表面守卫(19 构造,单边改动 CI 必红)· **渐进式契约**(文档内嵌 `参数`/`返回`/`字段` 表 + 四边界校验 + `marqdo check` 防漂移)· **MLSP for AI**(`marqdo mlsp`:`locate`/`syntax`/`validate`/`repair_targets`/`repair_apply`/`schema`,AI 按需查语法而非背诵)· **结构化诊断**(`run --json`,错误即数据);验证见 [perf-validation-report-2026-09-23.md](doc/roadmap/perf-validation-report-2026-09-23.md)
|
||||
- 映射与解释器:**v0.3 语法宪法**(`**` 代码 / `*` 返回 / 叙述声明);Phase I 树遍历 + 字节码后端;金样例在 `tests/` 与 `tests/markup-v03/`
|
||||
- **对象**:`#` = 类型/构造,`##`+ = 函数/方法;见 [objects.md](doc/design/objects.md)
|
||||
- **`marqdo view`**:文档浏览器(Structure + 函数大纲/搜索 + Execution + Variables 浮窗)
|
||||
@@ -111,16 +157,16 @@ marqdo catalog public -o .marqdo
|
||||
- **`web`**:W0–W7 + P3 + **W8**;**定制 C0–C4**;**宿主 H1–H2**(`proxy`/`invoke`,见 [ext-hosting.md](doc/roadmap/ext-hosting.md));**表驱动引言**(`compose_intro` / `引言装配`);原生层默认 **Go `libweb`**([ADR 0004](doc/adr/0004-web-plugin-go.md),`scripts/build-web-plugin.sh`);示例 [web-site](examples/web-site/) · [web-site-zh](examples/web-site-zh/) · [marqdo-blog](examples/marqdo-blog/) · [anlian-mq](examples/anlian-mq/);生产路径见 [web-asgi-servers-and-marqdo.md](doc/design/web-asgi-servers-and-marqdo.md)
|
||||
- **`quantum`**:Q0–Q7 + Q8a/b 主题 SVG;示例 [quantum-entanglement](examples/quantum-entanglement/)
|
||||
- **`linalg`**:L0–L6 + 公式文档面(中缀 / `declare` / 展示化简);示例 [linalg-transpose](examples/linalg-transpose/) · [linalg-svd](examples/linalg-svd/) · [linalg-least-squares](examples/linalg-least-squares/)
|
||||
- **`agent`**:A1–A4 + **MCP Server stdio**(`mcp_server`);宿主缺口见 [ext-hosting.md](doc/roadmap/ext-hosting.md) · [agent-framework-gaps-after-a4.md](doc/research/agent-framework-gaps-after-a4.md)
|
||||
- **`llm`**:OpenAI 兼容对话
|
||||
- **`agent`**:A1–A4 + **MCP Server/Client stdio** + Skill Compilation + Adaptive Routing + resume;宿主缺口见 [ext-hosting.md](doc/roadmap/ext-hosting.md) · [agent-framework-gaps-after-a4.md](doc/research/agent-framework-gaps-after-a4.md)
|
||||
- **`llm`**:语义 `ask`/`stream`/`collect`(OpenAI/Ollama;可选原生 `plugins/llm`)
|
||||
- 安装:`marqdo ext list` / `add …` / `remove`([ext-cli.md](doc/design/ext-cli.md);默认 `~/.marqdo/ext`)。原生插件先 `cargo build -p marqdo_plugin_*` 再 `ext add`
|
||||
- **原生插件 ABI**:[`include/marqdo_abi.h`](include/marqdo_abi.h) · [ext-abi.md](doc/design/ext-abi.md);`plugins/{demo,agent,web,quantum,linalg}`(`web` 为 Go;其余为 Rust)
|
||||
- **原生插件 ABI**:[`include/marqdo_abi.h`](include/marqdo_abi.h) · [ext-abi.md](doc/design/ext-abi.md);`plugins/{demo,agent,web,quantum,linalg,llm}`(`web` 为 Go;其余为 Rust)
|
||||
- **用户静态站**:`public/` → `view output` → CI 发布 [gh-pages](https://cflmy.github.io/marqdo/)
|
||||
- **VS Code 扩展**:分支 **`vscode-extension`**(`main` 不跟踪源码;见 [vscode-extension-commit.md](doc/design/vscode-extension-commit.md));Release 附带 `.vsix`
|
||||
- **浏览器 Marqdo(WASM)**:`marqdo wasm build` + 官方 bridge 自启(作者零业务 JS;桥内可含列表/路由/storage/ws/文件/Canvas/音频/Observer/拖放);`lib/browser` + GFM;`web.client_embed`;示例 [browser-hello](examples/browser-hello/) · [browser-app](examples/browser-app/) · [browser-media](examples/browser-media/) · [web-client-site](examples/web-client-site/)([ADR 0002](doc/adr/0002-browser-marqdo-wasm.md) · [D](doc/roadmap/browser-wasm-d.md) · [E](doc/roadmap/browser-wasm-e.md) · [F](doc/roadmap/browser-wasm-f.md))
|
||||
- 选型:[ADR 0001 — Rust](doc/adr/0001-implementation-language.md) · [ADR 0002 — 浏览器 WASM](doc/adr/0002-browser-marqdo-wasm.md)(C0–C5 完结,见 [roadmap/browser-wasm.md](doc/roadmap/browser-wasm.md))· [ADR 0003 — 异步效应](doc/adr/0003-browser-async-effects.md)
|
||||
|
||||
### 如何使用最新 Marqdo(v1.0.3)
|
||||
### 如何使用最新 Marqdo(v1.1.1)
|
||||
|
||||
**安装解释器(任选其一)**
|
||||
|
||||
@@ -135,7 +181,7 @@ marqdo catalog public -o .marqdo
|
||||
```bash
|
||||
# 1) 源码安装(跟 tag 或 main)
|
||||
git clone https://github.com/cflmy/marqdo.git && cd marqdo
|
||||
git checkout v1.0.3 # 或留在 main
|
||||
git checkout v1.1.1 # 或留在 main
|
||||
cargo build --release
|
||||
export PATH="$PWD/target/release:$PATH"
|
||||
marqdo version
|
||||
@@ -148,13 +194,18 @@ marqdo view public --no-open
|
||||
marqdo debug public --no-open
|
||||
marqdo catalog public -o .marqdo
|
||||
|
||||
# 2b) AI 原生闭环(v1.1.0+):契约检查 / 结构化诊断 / MLSP 查询
|
||||
marqdo check tests/contracts/ok_contract.mq.md # 静态契约防漂移
|
||||
marqdo run tests/structure/hello.mq.md --json # 结构化诊断(错误即数据)
|
||||
printf '{"id":1,"method":"syntax","params":{"query":"返回"}}\n' | marqdo mlsp
|
||||
|
||||
# 3) 安装官方扩展(Release / CDN 用户:无需本机 Rust/Go)
|
||||
marqdo ext add web # 或:网页 — 自动下载 L1 + 预编译 native(Go libweb)
|
||||
marqdo ext add agent # 或:智能体
|
||||
marqdo ext add quantum # 或:量子
|
||||
marqdo ext add linalg # 或:线性代数
|
||||
marqdo ext add llm # 或:大模型(纯 .mq.md)
|
||||
# 开发者:cargo build --release -p marqdo_plugin_{agent,quantum,linalg}
|
||||
marqdo ext add llm # 或:大模型(.mq.md + 可选 native llm)
|
||||
# 开发者:cargo build --release -p marqdo_plugin_{agent,quantum,linalg,llm}
|
||||
# bash ./scripts/build-web-plugin.sh # 需 Go + cgo → libweb
|
||||
# 离线:解压 Windows zip(已含 ext/native)或手动放下 native zip;MARQDO_EXT_NO_DOWNLOAD=1
|
||||
|
||||
@@ -169,7 +220,7 @@ marqdo wasm build
|
||||
# → dist/wasm/ … 见 examples/browser-hello/
|
||||
|
||||
# 6) Releases:Windows exe/zip/vsix;Linux CLI zip + native `.so` zip
|
||||
# https://github.com/cflmy/marqdo/releases/tag/v1.0.3
|
||||
# https://github.com/cflmy/marqdo/releases/tag/v1.1.1
|
||||
```
|
||||
|
||||
开发期也可用 `cargo run -- …` 代替已安装的 `marqdo`:
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "marqdo-wasm"
|
||||
version = "1.0.3"
|
||||
version = "1.1.1"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
description = "Marqdo interpreter as WebAssembly (browser host bridge)"
|
||||
|
||||
@@ -197,6 +197,20 @@ export function eventArgsFromDom(ev, valueFromSel) {
|
||||
args.fields = fields;
|
||||
}
|
||||
}
|
||||
if (ev && (ev.type === "scroll" || ev.type === "resize")) {
|
||||
try {
|
||||
const doc = typeof document !== "undefined" ? document.documentElement : null;
|
||||
const body = typeof document !== "undefined" ? document.body : null;
|
||||
const scrollTop = (doc && (doc.scrollTop || 0)) || (body && body.scrollTop) || 0;
|
||||
const height = doc ? (doc.scrollHeight - doc.clientHeight) || 1 : 1;
|
||||
const ratio = Math.max(0, Math.min(1, scrollTop / height));
|
||||
args.scroll_top = scrollTop;
|
||||
args.scroll_max = height;
|
||||
args.scroll_ratio = ratio;
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
return args;
|
||||
}
|
||||
|
||||
@@ -721,75 +735,88 @@ function runObserveEffect(exports, spec, onError) {
|
||||
const kind = String(spec.kind || spec.type || "intersect").toLowerCase();
|
||||
const sel = String(spec.sel || "");
|
||||
const thenFn = spec.then;
|
||||
const id = String(spec.id || `obs${++observeSeq}`);
|
||||
const el = qsOne(sel);
|
||||
if (!el || !thenFn) return;
|
||||
const baseId = String(spec.id || `obs${++observeSeq}`);
|
||||
const nodes = qsAll(sel);
|
||||
if (!nodes.length || (!thenFn && !spec.add_class)) return;
|
||||
|
||||
const prev = observeHandles.get(id);
|
||||
if (prev) {
|
||||
try {
|
||||
prev.disconnect();
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
}
|
||||
|
||||
const fire = (payload) => {
|
||||
try {
|
||||
const result = call(exports, thenFn, payload);
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
return;
|
||||
nodes.forEach((el, idx) => {
|
||||
const id = nodes.length === 1 ? baseId : `${baseId}_${idx}`;
|
||||
const prev = observeHandles.get(id);
|
||||
if (prev) {
|
||||
try {
|
||||
prev.disconnect();
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
applyEffects(exports, result.value, { onError });
|
||||
} catch (e) {
|
||||
if (onError) onError(String(e));
|
||||
}
|
||||
};
|
||||
|
||||
if (kind === "resize" && typeof ResizeObserver !== "undefined") {
|
||||
const ro = new ResizeObserver((entries) => {
|
||||
const en = entries[0];
|
||||
const cr = en?.contentRect;
|
||||
fire({
|
||||
ok: true,
|
||||
kind: "resize",
|
||||
id,
|
||||
width: cr ? cr.width : 0,
|
||||
height: cr ? cr.height : 0,
|
||||
});
|
||||
});
|
||||
ro.observe(el);
|
||||
observeHandles.set(id, ro);
|
||||
return;
|
||||
}
|
||||
const fire = (payload) => {
|
||||
try {
|
||||
const result = call(exports, thenFn, payload);
|
||||
if (!result.ok) {
|
||||
if (onError) onError(result.error);
|
||||
return;
|
||||
}
|
||||
applyEffects(exports, result.value, { onError });
|
||||
} catch (e) {
|
||||
if (onError) onError(String(e));
|
||||
}
|
||||
};
|
||||
|
||||
if (typeof IntersectionObserver !== "undefined") {
|
||||
const io = new IntersectionObserver(
|
||||
(entries) => {
|
||||
if (kind === "resize" && typeof ResizeObserver !== "undefined") {
|
||||
const ro = new ResizeObserver((entries) => {
|
||||
const en = entries[0];
|
||||
if (!en) return;
|
||||
const cr = en?.contentRect;
|
||||
fire({
|
||||
ok: true,
|
||||
kind: "intersect",
|
||||
kind: "resize",
|
||||
id,
|
||||
intersecting: !!en.isIntersecting,
|
||||
ratio: en.intersectionRatio,
|
||||
width: cr ? cr.width : 0,
|
||||
height: cr ? cr.height : 0,
|
||||
});
|
||||
if (spec.once && en.isIntersecting) {
|
||||
try {
|
||||
io.disconnect();
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
});
|
||||
ro.observe(el);
|
||||
observeHandles.set(id, ro);
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof IntersectionObserver !== "undefined") {
|
||||
const opts = { threshold: spec.threshold != null ? Number(spec.threshold) : 0.1 };
|
||||
if (spec.rootMargin != null) opts.rootMargin = String(spec.rootMargin);
|
||||
const io = new IntersectionObserver(
|
||||
(entries) => {
|
||||
const en = entries[0];
|
||||
if (!en) return;
|
||||
if (en.isIntersecting && spec.add_class) {
|
||||
for (const c of String(spec.add_class).split(/\s+/).filter(Boolean)) {
|
||||
el.classList.add(c);
|
||||
}
|
||||
}
|
||||
observeHandles.delete(id);
|
||||
}
|
||||
},
|
||||
{ threshold: spec.threshold != null ? Number(spec.threshold) : 0.1 },
|
||||
);
|
||||
io.observe(el);
|
||||
observeHandles.set(id, io);
|
||||
}
|
||||
if (thenFn) {
|
||||
fire({
|
||||
ok: true,
|
||||
kind: "intersect",
|
||||
id,
|
||||
intersecting: !!en.isIntersecting,
|
||||
ratio: en.intersectionRatio,
|
||||
target_id: el.id || "",
|
||||
});
|
||||
}
|
||||
if (spec.once && en.isIntersecting) {
|
||||
try {
|
||||
io.disconnect();
|
||||
} catch (_) {
|
||||
/* ignore */
|
||||
}
|
||||
observeHandles.delete(id);
|
||||
}
|
||||
},
|
||||
opts,
|
||||
);
|
||||
io.observe(el);
|
||||
observeHandles.set(id, io);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function buildFetchInit(spec) {
|
||||
@@ -1386,12 +1413,24 @@ export async function autoMount(doc) {
|
||||
|
||||
const g = typeof globalThis !== "undefined" ? globalThis : undefined;
|
||||
if (g && g.document) {
|
||||
// Defer WASM boot until after first paint / load so LCP images are not
|
||||
// contending with a ~1.6MB wasm download on the critical path.
|
||||
const start = () => {
|
||||
autoMount(g.document).catch((e) => console.error("marqdo autoMount:", e));
|
||||
const run = () => {
|
||||
autoMount(g.document).catch((e) => console.error("marqdo autoMount:", e));
|
||||
};
|
||||
const schedule = () => {
|
||||
if (typeof g.requestIdleCallback === "function") {
|
||||
g.requestIdleCallback(() => run(), { timeout: 1500 });
|
||||
} else {
|
||||
g.setTimeout(run, 0);
|
||||
}
|
||||
};
|
||||
if (g.document.readyState === "complete") {
|
||||
schedule();
|
||||
} else {
|
||||
g.addEventListener("load", schedule, { once: true });
|
||||
}
|
||||
};
|
||||
if (g.document.readyState === "loading") {
|
||||
g.document.addEventListener("DOMContentLoaded", start);
|
||||
} else {
|
||||
queueMicrotask(start);
|
||||
}
|
||||
start();
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "marqdo-bridge",
|
||||
"private": true,
|
||||
"type": "module"
|
||||
}
|
||||
@@ -185,7 +185,38 @@ fn pack_json(v: &serde_json::Value) -> *mut u8 {
|
||||
ptr::copy_nonoverlapping(len.to_le_bytes().as_ptr(), out, 4);
|
||||
ptr::copy_nonoverlapping(bytes.as_ptr(), out.add(4), bytes.len());
|
||||
}
|
||||
out
|
||||
out
|
||||
}
|
||||
|
||||
/// getrandom `custom` backend for wasm32 — no wasm-bindgen / JS glue imports.
|
||||
/// Preferable to the `js` feature which pulls `__wbindgen_*` and breaks raw mq_* ABI.
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
#[no_mangle]
|
||||
unsafe extern "Rust" fn __getrandom_custom(dest: *mut u8, len: usize) -> u32 {
|
||||
// xorshift64* seeded once; fine for uuid/non-crypto browser host helpers.
|
||||
use std::cell::Cell;
|
||||
thread_local! {
|
||||
static STATE: Cell<u64> = const { Cell::new(0x4d51_5241_4e44_4f21) };
|
||||
}
|
||||
if dest.is_null() || len == 0 {
|
||||
return 0;
|
||||
}
|
||||
let mut s = STATE.with(|c| {
|
||||
let mut v = c.get();
|
||||
if v == 0 {
|
||||
v = 0x4d51_5241_4e44_4f21;
|
||||
}
|
||||
v
|
||||
});
|
||||
let buf = unsafe { std::slice::from_raw_parts_mut(dest, len) };
|
||||
for b in buf.iter_mut() {
|
||||
s ^= s << 13;
|
||||
s ^= s >> 7;
|
||||
s ^= s << 17;
|
||||
*b = (s >> 8) as u8;
|
||||
}
|
||||
STATE.with(|c| c.set(s));
|
||||
0
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
Vendored
+34
@@ -1,3 +1,37 @@
|
||||
marqdo (1.1.1-1ppa1) noble; urgency=medium
|
||||
|
||||
* Marqdo 1.1.1: AI stack closure — llm ask/stream/collect, Metadata Binding,
|
||||
Skill Compilation + Adaptive Routing, MCP stdio client + resume checkpoints,
|
||||
optional plugins/llm Transport ABI (fallback lib/net).
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Thu, 24 Sep 2026 20:32:00 +0800
|
||||
|
||||
marqdo (1.1.0-1ppa1) noble; urgency=medium
|
||||
|
||||
* Marqdo 1.1.0: core surface guard + progressive contracts + MLSP for AI.
|
||||
New `marqdo check` (static contract drift) and `marqdo mlsp` (language
|
||||
server for AI: locate/syntax/validate/repair_targets/schema/repair_apply);
|
||||
`run --json` unified structured diagnostics; contract tables
|
||||
(参数/返回/字段/集合) with four validation boundaries; anchored repair
|
||||
guardrails (bounded line edits, out-of-scope refused).
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Wed, 23 Sep 2026 13:30:00 +0800
|
||||
|
||||
marqdo (1.0.7-1ppa1) noble; urgency=medium
|
||||
|
||||
* Marqdo 1.0.7: WS/net boundary hardening (same-origin handshake,
|
||||
require_auth/需要登录, message caps, empty-room GC); HTTP 8 MiB cap
|
||||
enforced while reading; url_encode space -> %20.
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Tue, 22 Sep 2026 15:40:00 +0800
|
||||
|
||||
marqdo (1.0.3-1ppa2) noble; urgency=medium
|
||||
|
||||
* Fix Launchpad build: dh_clean -Xvendor/ so cargo vendor checksums keep
|
||||
Cargo.toml.orig (debian-only; reuse published 1.0.3.orig.tar.xz).
|
||||
|
||||
-- cflmy <pingan@cflmy.cn> Thu, 17 Sep 2026 21:28:00 +0800
|
||||
|
||||
marqdo (1.0.3-1ppa1) noble; urgency=medium
|
||||
|
||||
* Marqdo 1.0.3: PPA + ext CDN; unbound GFM tables; bundled Rust for noble.
|
||||
|
||||
Vendored
+5
@@ -46,6 +46,11 @@ override_dh_auto_install:
|
||||
install -m 0755 $(CURDIR)/target/release/marqdo \
|
||||
$(CURDIR)/debian/marqdo/usr/bin/marqdo
|
||||
|
||||
# dh_clean deletes *.orig by default; cargo vendor checksums require
|
||||
# vendor/**/Cargo.toml.orig. Exclude the vendored tree from that purge.
|
||||
override_dh_clean:
|
||||
dh_clean -Xvendor/
|
||||
|
||||
override_dh_auto_clean:
|
||||
dh_auto_clean
|
||||
rm -rf debian/cargo_home
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
# ADR 0006:Artifact Metadata Binding(`${…}` 进核心 · Metadata-only)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| Status | **Accepted** |
|
||||
| Date | 2026-09-24 |
|
||||
| Related | [binding.md](../design/binding.md) · [core-surface.md](../design/core-surface.md) · [layers.md](../design/layers.md) · [doc/next/006.md](../next/006.md) |
|
||||
|
||||
## Context
|
||||
|
||||
- Executable Document 需要声明「依赖哪些外部运行时资源」(模型名、API 密钥、base URL),而不是在 `ext/*` 里各自 `sys.env_get`。
|
||||
- [doc/next/006.md](../next/006.md) 提出 **Marqdo Binding**(`${namespace.name}`)与 Artifact Metadata 统一解析。
|
||||
- [layers.md](../design/layers.md):Language 层变更须 ADR;核心构造计数只许持平或下降;默认进 ext。
|
||||
- 正文插值会与 `$$…$$` 公式面冲突,且会新增内联构造压力。
|
||||
|
||||
## Decision
|
||||
|
||||
1. 采纳 **Artifact Metadata Binding** 为 Language 层能力;正式规范见 [binding.md](../design/binding.md)。
|
||||
2. **`${…}` 仅**在文件头 `---` Metadata 中生效;正文 / 粗体 / 调用 / 表 / 叙述 **永不**写 Binding Expression(否决 [006 §17](../next/006.md))。
|
||||
3. **不**把 Binding 记入 [core-surface.md](../design/core-surface.md) 的 19 构造守卫表——与 `import` 同属 §4「其它语言面」。
|
||||
4. 核心只做声明与解析;扩展(如 `ext/ai/llm`)消费已绑定的 `Module.metadata` / `sys.meta*` / 入口提升变量。
|
||||
5. 第一版 namespace:`env` · `arg` · `sys` · `secret`。`config` / `file` 推迟。
|
||||
6. 加载期一次 resolve → Bound Artifact;`Value::Secret` 遮掩展示;`sys.env_get` 保留为逃逸口。
|
||||
7. **Phase 2**:入口执行前把 metadata 键提升为普通变量(形参/赋值可覆盖);正文用 `` `model` ``,不再 `${env…}`。
|
||||
|
||||
## Consequences
|
||||
|
||||
### Positive
|
||||
|
||||
- 配置成为文档的一部分;LLM / agent / 其它 ext 共用同一套 Binding,避免各写一套 getenv。
|
||||
- 缺失 `!` 在加载期失败,而不是运行到 HTTP 401。
|
||||
- 不膨胀 CORE_CONSTRUCTS;与公式 `$$` 无冲突。
|
||||
- 正文保持普通变量语义,不引入第二套环境变量语法。
|
||||
|
||||
### Negative / Risks
|
||||
|
||||
- Secret 在 `Text + Secret` 拼接时会揭示明文(供 Authorization);须约束日志面。
|
||||
- metadata 键与业务变量同名时依赖「形参/赋值覆盖」规则,作者需注意命名。
|
||||
|
||||
### Neutral
|
||||
|
||||
- `#` / `##` / `**` / `*` 等核心标记不变;frontmatter `import` 句法不变。
|
||||
@@ -0,0 +1,186 @@
|
||||
# Marqdo Binding · Artifact Metadata
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · Phase 1 + Phase 2(scope lift)** |
|
||||
| 日期 | 2026-09-24 |
|
||||
| ADR | [0006-artifact-metadata-binding.md](../adr/0006-artifact-metadata-binding.md) |
|
||||
| 讨论稿 | [doc/next/006.md](../next/006.md) |
|
||||
| 实现 | `src/binding/` · `Module.metadata` · entry env lift · `sys.meta` / `sys.meta_get` · `marqdo run --bind` |
|
||||
|
||||
> **边界**:核心负责「声明与解析配置」;扩展负责「解释配置的语义」。
|
||||
> **`${…}` 只出现在文件头 Artifact Metadata**——正文 / 粗体 / 调用 / 表 **永不**写 `${env…}`(否决讨论稿 §17)。正文使用已绑定的普通名字。
|
||||
|
||||
---
|
||||
|
||||
## 1. Artifact Metadata
|
||||
|
||||
文件开头成对 `---` 之间为 **Artifact Metadata**(声明式元信息环境),不是「随便贴的 YAML」。
|
||||
|
||||
```text
|
||||
.mq.md
|
||||
├── Metadata ← Identity / Configuration / Dependencies / Bindings
|
||||
└── Executable Body
|
||||
```
|
||||
|
||||
- `import bind:…` / `导入` 行:模块依赖(既有语义不变)。
|
||||
- 其它 `key: value` 行:元信息;值可为字面量或 **Binding Expression**。
|
||||
- Phase 1 仅支持 **flat** 键(同级 `key: value`);块标量 `>` / `|` 收成 Text。
|
||||
|
||||
例:
|
||||
|
||||
```yaml
|
||||
---
|
||||
title: demo
|
||||
type: llm
|
||||
model: ${env.OPENAI_MODEL ?? "gpt-4o-mini"}
|
||||
api_key: ${secret.OPENAI_API_KEY!}
|
||||
base_url: ${env.OPENAI_BASE_URL ?? "https://api.openai.com/v1"}
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
```
|
||||
|
||||
加载后内部为已绑定的 `Module.metadata`(确定性快照),执行期不再对同一键反复 `getenv`。
|
||||
|
||||
---
|
||||
|
||||
## 2. Binding Expression
|
||||
|
||||
外形:
|
||||
|
||||
```text
|
||||
${expression}
|
||||
```
|
||||
|
||||
### 2.1 路径
|
||||
|
||||
```text
|
||||
${namespace.name}
|
||||
```
|
||||
|
||||
| Namespace | 意义 | Phase 1 |
|
||||
|-----------|------|---------|
|
||||
| `env` | 进程环境变量 | ✅ |
|
||||
| `arg` | CLI / Runtime 绑定(`marqdo run --bind KEY=VALUE`) | ✅ |
|
||||
| `sys` | 系统信息:`cwd` · `platform` | ✅ |
|
||||
| `secret` | 敏感环境变量 → `Value::Secret` | ✅ |
|
||||
| `config` | 配置 Artifact | 推迟 |
|
||||
| `file` | 外部文件内容 | 推迟 |
|
||||
|
||||
### 2.2 默认与必需
|
||||
|
||||
```text
|
||||
${env.OPENAI_MODEL ?? "gpt-4o-mini"}
|
||||
${secret.OPENAI_API_KEY!}
|
||||
```
|
||||
|
||||
- `A ?? B`:A 缺失(未设置 / 空)则用 B。
|
||||
- `A!`:A 必须存在,否则加载失败(`config.required_binding_missing`)。
|
||||
|
||||
### 2.3 类型转换
|
||||
|
||||
```text
|
||||
${int(env.MAX_TOKENS ?? "4096")}
|
||||
${float(env.TEMPERATURE ?? "0.2")}
|
||||
${bool(env.STREAM ?? "false")}
|
||||
```
|
||||
|
||||
### 2.4 独占 value vs 插值
|
||||
|
||||
| 形态 | 语义 |
|
||||
|------|------|
|
||||
| `model: ${env.OPENAI_MODEL}` | **Value binding** — 保留解析后的类型(含 Secret) |
|
||||
| `title: "Hello ${env.USER}"` | **Interpolation** — 结果为 Text |
|
||||
| `model: gpt-4o-mini` | **Literal** — 字面量;**不会**被环境变量静默覆盖 |
|
||||
|
||||
---
|
||||
|
||||
## 3. Resolution Policy
|
||||
|
||||
对每个 metadata 键:
|
||||
|
||||
1. 解析该键的字面量 / Binding / 插值(字面量定稿后不受 env 覆盖)。
|
||||
2. 若 CLI 提供了同名 `--bind KEY=VALUE`,**覆盖**该键的最终值(Text)。
|
||||
3. Binding 内部查找顺序(仅当表达式写出时):`arg` / `env` / `secret` / `sys` 按表达式 namespace;`??` 链从左到右。
|
||||
|
||||
推荐作者意图:
|
||||
|
||||
```text
|
||||
CLI --bind
|
||||
→ Artifact 字面量 / 已写死的 Binding 结果
|
||||
→ Binding 源(env / secret / …)
|
||||
→ ?? 默认
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## 4. Secret
|
||||
|
||||
`${secret.NAME}` → `Value::Secret`。
|
||||
|
||||
- `print` / `str` / Display → `<secret>`(不泄露)。
|
||||
- `type` → `secret`。
|
||||
- 与 Text 拼接(如 `bearer + api_key`)时**揭示**明文,供 HTTP Authorization。
|
||||
- 不得进入普通 episode / 错误信息明文(扩展作者责任;核心 Display 已遮掩)。
|
||||
|
||||
---
|
||||
|
||||
## 5. Runtime API
|
||||
|
||||
| API | 语义 |
|
||||
|-----|------|
|
||||
| `sys.meta` / `系统.元信息` | 入口模块已绑定 metadata(map) |
|
||||
| `sys.meta_get` / `系统.取元信息` | `key=` → 值或 `None` |
|
||||
| `marqdo run FILE --bind KEY=VALUE` | 可重复;写入 `arg` 并覆盖同名 metadata 键 |
|
||||
| `sys.env_get` | **保留**逃逸口;新代码优先 Metadata Binding |
|
||||
|
||||
---
|
||||
|
||||
## 6. 错误
|
||||
|
||||
| code | 何时 |
|
||||
|------|------|
|
||||
| `config.required_binding_missing` | `${ns.name!}` 且源缺失 |
|
||||
| `config.unknown_binding_ns` | 未知 namespace |
|
||||
| `config.bad_binding` | 表达式非法 / cast 失败 |
|
||||
|
||||
诊断尽量带 frontmatter 行号。
|
||||
|
||||
---
|
||||
|
||||
## 7. 与扩展的分工
|
||||
|
||||
- **核心**:解析 Metadata、resolve Binding、暴露 `Module.metadata` / `sys.meta*`,并在入口把 metadata **提升为普通变量**。
|
||||
- **`ext/ai/llm`**:构造次序为 **显式实参 → 入口作用域同名变量 / `sys.meta_get` → `sys.env_get` 回退 → 默认**。
|
||||
|
||||
---
|
||||
|
||||
## 8. Phase 2 · Metadata scope lift(Accepted)
|
||||
|
||||
加载期绑定完成后,入口执行(`# main` 或唯一无参顶层对象)前,把 `Module.metadata` 的每个键注入 entry 环境,成为**普通变量**:
|
||||
|
||||
```yaml
|
||||
---
|
||||
model: ${env.ModelName ?? "gpt-4o-mini"}
|
||||
---
|
||||
```
|
||||
|
||||
```marqdo
|
||||
# main
|
||||
|
||||
> print text=`model`
|
||||
```
|
||||
|
||||
### 规则
|
||||
|
||||
1. 注入时机:进入入口函数之前。
|
||||
2. 冲突:形参 / 调用实参 **覆盖** 同名 metadata;程序内赋值亦然。metadata **不**覆盖已存在的绑定。
|
||||
3. `sys.meta` / `sys.meta_get` 仍可用(显式通道)。
|
||||
4. **拒绝** 正文 / 粗体 / 表 / 叙述中的 `${…}`(讨论稿 006 §17 否决)。普通语言也不在语句里 `${ENV}`。
|
||||
|
||||
### 仍推迟(P3+)
|
||||
|
||||
- `config` / `file` namespace
|
||||
- 嵌套 YAML map
|
||||
- 显式 `override:` 糖
|
||||
- 计入 `CORE_CONSTRUCTS`
|
||||
@@ -0,0 +1,47 @@
|
||||
# 核心表面清单(core-surface)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · 核心语法封闭集合(CI 守卫锁定)** |
|
||||
| 日期 | 2026-09-22 |
|
||||
| 守卫 | `tests/core_surface.rs` ⇄ `src/parse/mod.rs` `CORE_CONSTRUCTS`(改动任一侧不同步 → CI 红) |
|
||||
| 相关 | [markdown-mapping-v0.3.md](markdown-mapping-v0.3.md)(语义宪法)· [layers.md](layers.md)(准入戒律)· [three-problems.md](../roadmap/three-problems.md) 问题一 · [three-problems-plan.md](../roadmap/three-problems-plan.md) T1.1/T1.3 |
|
||||
|
||||
> **戒律总纲**:核心标记是**封闭集合**(约 19 个构造)。新增任何一行必须走 ADR(见 [layers.md](layers.md) 准入戒律)并回答「为什么现有标记不够」;生态需求的默认答案是**进 ext**。本表 id 列与守卫测试强一致:改表不同步 `CORE_CONSTRUCTS`(或反之)、样例文件缺失,一律 CI 红。
|
||||
|
||||
## 1. 行构造
|
||||
|
||||
| id | 外形 | 语义 | 戒律 | 样例 |
|
||||
|----|------|------|------|------|
|
||||
| `narrative` | 无标记文字 | 叙述(注释) | 段落内延续到空行;**默认安全**——永远不存在「叙述被执行」的路径 | `tests/markup-v03/soft-emphasis.mq.md` |
|
||||
| `blank` | 空行 | 段落边界 | 叙述分段的唯一手段;不得被赋予其它语义 | `tests/markup-v03/inc-prose.mq.md` |
|
||||
| `heading-unit` | `#` / `##` | 对象 / 函数单元 | 标题即单元边界,按深度嵌套;正文禁止用 `##` 做版式(会变成函数) | `tests/markup-v03/decl-ident-only.mq.md` |
|
||||
| `call-line` | `>` | 调用行 | 输出等副作用 = **普通函数**,不是架构标记 | `tests/markup-v03/dual-call.mq.md` |
|
||||
| `branch-list` | 体内 `1.` 递增 | 分支 | 自 `1.` 起序号递增构成一个判断;重出 `1.` 开新判断;`N. *` = else;**不是所有有序列表都是 if** | `tests/markup-v03/greet-default.mq.md` |
|
||||
| `loop-list` | `-` | 循环 | 条件形 `` - `条件` `` 或遍历形 `- [项](集合)` | `tests/structure/loop.mq.md` |
|
||||
| `param` | `` + `名` `` | 形参(兼容形) | 首选叙述 `` `名` `` 升参(宪法 §8);`+` 形仅兼容保留 | `tests/markup-v03/greet-default.mq.md` |
|
||||
| `table` | GFM 表 | 集合 | 几何即类型([tables-maps-footnotes.md](../roadmap/tables-maps-footnotes.md));**绑定 = 数据,未绑定 = 文档表** | `tests/structure/collection.mq.md` |
|
||||
| `fence` | ` ```lang ` | 外联 | 语言块原样外联,不进 Marqdo 语义 | `tests/lib/foreign-python.mq.md` |
|
||||
| `hr` | 单行 `---` 或 `***` | 叙述分隔 | **不收束函数**(返回才收束) | `tests/markup-v03/module-hr.mq.md` |
|
||||
| `writeback` | `<!-- marqdo-out … -->` | 持久化输出 | 只读回放,不执行 | `tests/lib/writeback-get.mq.md` |
|
||||
|
||||
## 2. 内联标记
|
||||
|
||||
| id | 外形 | 语义 | 戒律 | 样例 |
|
||||
|----|------|------|------|------|
|
||||
| `ident` | `` `名` `` | 声明 / 引用 | 首次未赋值 = 声明;叙述与程序共用同一视觉习惯 | `tests/markup-v03/decl-ident-only.mq.md` |
|
||||
| `bold-code` | `**…**` | 代码(赋值 / 调用 / 表达式) | 只有粗体进入执行;判别序:赋值 → 调用 → 表达式 | `tests/markup-v03/multi-bold.mq.md` |
|
||||
| `italic-return` | `*…*` | 返回 | 改变控制流(**返回是架构**);返回即结束函数体 | `tests/markup-v03/inc-prose.mq.md` |
|
||||
| `empty-return` | `*None*` / `****` | 空返回 | 返回 `None` 并结束函数体 | `tests/markup-v03/empty-return-none.mq.md` |
|
||||
| `link-index` | `[键](集合)` | 取元 / foreach | **主取元语法**;与 bracket-call 靠 `]` 后是否紧邻 `(` 消歧 | `tests/lib/table-collections.mq.md` |
|
||||
| `bracket-call` | `修饰… [函数] 实参…` | 方括号标记调用 | 前置修饰 → `名=True`;与 link-index 消歧同上 | `tests/structure/bracket-call.mq.md` |
|
||||
| `footnote-index` | `集合[^键]` | 过渡取元 | **仅兼容**;新代码一律写 link-index | `tests/structure/footnote-index.mq.md` |
|
||||
| `soft-emphasis` | `*仅*` / `**说明**` | 忽略 | 非代码形强调**不执行**——默认安全的关键消歧规则 | `tests/markup-v03/soft-emphasis.mq.md` |
|
||||
|
||||
## 3. 词法大类(LineKind,`src/lex/mod.rs`)
|
||||
|
||||
行级入口只有四个大类:`Blank` / `Comment`(叙述段落,含软强调与行内 `` `名` ``)/ `Writeback` / `Code`(`# * > + - | ~ $` 或数字开头)。本清单第 1 节的行构造是在 `Code`(及 `Comment`/`Writeback`)内部的**语义细分**;第 2 节内联标记由 `src/parse/prose.rs` 行内扫描产出。两层合起来 = 核心表面全集。
|
||||
|
||||
## 4. 范围外(进 Language 层,但不在本守卫)
|
||||
|
||||
frontmatter / Artifact Metadata(元信息)、`import` 行(模块装载)、以及 Metadata 内的 **Binding Expression**(`${ns.name}`,见 [binding.md](binding.md) · [ADR 0006](../adr/0006-artifact-metadata-binding.md))属于 Language 层的其它语言面,随 [layers.md](layers.md) 准入流程登记,**暂不进本清单守卫**(不增 CORE_CONSTRUCTS 计数)。Phase 2 将已绑定 metadata **提升为入口普通变量**(仍非新标记)。如需一并守卫,走 ADR 扩表。
|
||||
@@ -180,11 +180,26 @@ ext/
|
||||
| 中文 | 英文 | 角色 |
|
||||
|------|------|------|
|
||||
| `# 智能体` | `# agent` | 构造:`大模型`/`model`,`工具`/`tools`,可选 `站立提示`/`standing` |
|
||||
| `## 运行` | `## run` | **v2 主入口**:Adaptive `route` → execute → record episode → maybe_learn(技能 + 策略) |
|
||||
| `## 单步` | `## step` | 原子执行;注入源码 + 位置;可选工具;返回结构化 map;**默认自动写回**(`写回=` / `writeback=` 可关) |
|
||||
| `## 多步` | `## plan` | 建/续工作簿 → subtask 跑文件 → 修订循环 → 汇总(见 [ext-agent-plan.md](ext-agent-plan.md)) |
|
||||
| `## 解析` | `## resolve` | 技能解析:exact → alias → canonical → near |
|
||||
| `## 路由` | `## route` | **P4 Adaptive Routing**:`marqdo` \| `small-llm` \| `llm` \| `auto`(`jev` 为 `small-llm` 兼容别名) |
|
||||
| `## 应用路由` | `## route_apply` | 将小模型/大模型路由回复(`SKILL:<slug>` / `EXPLORE`)落到 kb 技能 |
|
||||
| `## 编译策略` | `## compile_policy` | Policy Compilation → `policies/router.mq.md` |
|
||||
| `## 记录情景` | `## record_episode` | 每次执行沉淀为 `.mq.md` episode(`.marqdo/agent-memory/`) |
|
||||
| `## 尝试学习` | `## maybe_learn` | 情景足够时归纳 → 合成 → 编译技能,并尝试编译策略 |
|
||||
| `## 编译` | `## compile` | 候选技能 → 版本化 `skills/<slug>/vN.mq.md` + 可选 OKF promote |
|
||||
| `## 指标` | `## metrics` | Reasoning amortization(`avg_llm_calls` / `compiled_runs`) |
|
||||
| `## 分解` | `## decompose` |(可公开或内部)目标 → 子任务表 |
|
||||
| `## 清空历史` | `## clear_history` | 若仍保留会话式辅助状态则清空;**不以隐藏 chat 袋为真相源**——真相在文档写回 |
|
||||
|
||||
**Skill Compilation(v2)**:Agent 的记忆不是向量袋,而是可执行 `.mq.md`。路径:`episodes/` → `experiences/` → `candidates/` → `skills/vN.mq.md` → OKF `agent-kb`(`llm_free`)。编译技能正文为合法 Marqdo(`**result = …**` + 斜体返回);金样 `tests/ext/agent-zero-llm.mq.md` 验收 policy+skill 全路径 `execution.llm_calls=0`。`plan` 对已存在的 `resources/<slug>.mq.md` **不覆盖**(`agent-plan-preserve`)。
|
||||
|
||||
**LLM 成本**:`model_turn` 走 `model.ask` / `stream`+`collect`,把 `LLMResult.tokens` 写入 episode / `execution`。
|
||||
|
||||
**Adaptive Routing(P4)**:Router 是抽象能力,backend = `marqdo`(已编译 `policies/router.mq.md`)/ `small-llm`(`router_model` 任意 llm 句柄)/ `llm` / `auto` 级联。`jev` 仅为 `small-llm` 兼容别名,**不进入框架专属设计**。热路径在 **`plugins/agent`**(`route.rs`);**禁止**向 `src/host/` 加领域原语。设计:[doc/next/003.md](../next/003.md) · [doc/next/004.md](../next/004.md)。
|
||||
|
||||
构造参数(v1):
|
||||
|
||||
| 参数 | 说明 |
|
||||
|
||||
+74
-51
@@ -2,13 +2,21 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| Status | Accepted (v0.1.x) |
|
||||
| Date | 2026-08-06 |
|
||||
| Related | [objects.md](objects.md) · [ext-abi.md](ext-abi.md) · [ext-agent.md](ext-agent.md) · [ext-cli.md](ext-cli.md) |
|
||||
| Status | **Accepted · v0.3 semantic**(005:Intelligence Primitive) |
|
||||
| Date | 2026-09-24 |
|
||||
| Related | [objects.md](objects.md) · [ext-agent.md](ext-agent.md) · [ext-cli.md](ext-cli.md) · [ai-stack.md](../roadmap/ai-stack.md) · [doc/next/005.md](../next/005.md) |
|
||||
|
||||
## Scope
|
||||
|
||||
`ext/ai/` holds **official LLM + agent** extensions. Import paths:
|
||||
`ext/ai/llm` is Marqdo’s **intelligence primitive**, not an OpenAI SDK wrapper.
|
||||
|
||||
```text
|
||||
Author surface: ask / stream / collect / create / fast / reasoning / prompt_load
|
||||
Transport: ext/ai/llm/openai.mq.md (+ ollama defaults)
|
||||
Compat: complete / chat
|
||||
```
|
||||
|
||||
Import:
|
||||
|
||||
```markdown
|
||||
---
|
||||
@@ -16,70 +24,85 @@ import llm:ext/ai/llm.mq.md
|
||||
---
|
||||
```
|
||||
|
||||
Resolution: `MARQDO_EXT/ai/…`, repo `ext/ai/…`, cwd `ext/ai/…` (see [ext-cli.md](ext-cli.md)).
|
||||
|
||||
Other official ext: [ext-agent.md](ext-agent.md) / [ext-agent-plan.md](ext-agent-plan.md) (**document-driven agent** — step with default writeback / plan workbook; thin `TOOL:` loop superseded). Installer: [ext-cli.md](ext-cli.md). Native plugins: [ext-abi.md](ext-abi.md).
|
||||
|
||||
Streaming: `complete stream=True` returns an event list (`reasoning` / `delta` / `done` / `error`); `echo=True` prints thinking (`reasoning_content`) and answer tokens as they arrive. See [agent-streaming.md](../roadmap/agent-streaming.md). Default (`stream=False`) is unchanged (answer string).
|
||||
|
||||
**Note:** Prefer `marqdo ext add llm`. Import `import llm:ext/ai/llm.mq.md` from repo or install root.
|
||||
|
||||
## Platform prerequisites
|
||||
|
||||
| Capability | Surface |
|
||||
|------------|---------|
|
||||
| HTTPS + headers | `lib/net` → `http_post` / `headers=` map |
|
||||
| Dotenv | `## load_env` in ext (or `load_dotenv` / `lib/sys`) |
|
||||
| JSON quote | `lib/json` → `quote` |
|
||||
|
||||
## `ext/ai/llm` / `ext/ai/大模型` (object handles)
|
||||
## Public API
|
||||
|
||||
| English | Chinese | Role |
|
||||
|---------|---------|------|
|
||||
| `## load_env` | `## 加载环境` | Free function: load `.env` |
|
||||
| `## stream_result` | `## 流式结果` | Collapse event list → final text |
|
||||
| `# llm` | `# 大模型` | Object ctor → handle map |
|
||||
| `## complete` / `## chat` | `## 运行` / `## 聊天` | Methods; optional `stream` / `流式`, `echo` / `打印增量` |
|
||||
| `## load_env` | `## 加载环境` | Load `.env` |
|
||||
| `## create` | `## 创建` | Factory → LLM handle |
|
||||
| `## fast` / `## reasoning` | `## 快速` / `## 推理` | Named handles |
|
||||
| `## prompt_load` / `## prompt` / `## prompt_body` | `## 加载提示` / `## 提示` / `## 提示正文` | Prompt artifacts (`type: prompt` → body) |
|
||||
| `## ask` (module) | `## 提问` | Convenience → **text** (`prompt=` or `path=`) |
|
||||
| `## collect` | `## 收集` | Events → text |
|
||||
| `## stream_result` | `## 流式结果` | Alias of `collect` |
|
||||
| `# llm` | `# 大模型` | Handle ctor (`backend=` openai-compatible \| ollama) |
|
||||
| `## ask` (method) | `## 提问` | → **LLMResult** map |
|
||||
| `## stream` | `## 流式` | → event list |
|
||||
| `## complete` / `## chat` | `## 运行` / `## 聊天` | Compat / transport primitive |
|
||||
|
||||
### Preferred
|
||||
|
||||
```markdown
|
||||
---
|
||||
import llm:ext/ai/llm.mq.md
|
||||
---
|
||||
**answer = > llm.ask prompt="What is Marqdo?"**
|
||||
*answer*
|
||||
|
||||
# main
|
||||
**model = > llm.create**
|
||||
**result = > `model`.ask prompt=`prompt`**
|
||||
*[text](result)*
|
||||
|
||||
> load_env
|
||||
**fast = > llm.fast**
|
||||
**events = > `fast`.stream prompt=`prompt`**
|
||||
**text = > llm.collect events=`events`**
|
||||
|
||||
*`model` = > llm *
|
||||
*`reply` = > `model`.complete prompt=Say hi in one word *
|
||||
> print text=`reply`
|
||||
|
||||
*`events` = > `model`.complete prompt=Say hi stream=True *
|
||||
- [`ev`](`events`)
|
||||
*`t` = > json.get value=`ev` key=type *
|
||||
1. `t` == "delta"
|
||||
*`chunk` = > json.get value=`ev` key=text *
|
||||
> print text=`chunk`
|
||||
2. *
|
||||
*`_` = 1*
|
||||
**body = > llm.prompt path="fixtures/prompt-marqdo.md"**
|
||||
**body2 = > llm.prompt text=`raw`**
|
||||
```
|
||||
|
||||
Environment (create a **project-local** `.env`; do not commit secrets):
|
||||
### LLMResult
|
||||
|
||||
| Field | Meaning |
|
||||
|-------|---------|
|
||||
| `text` | Answer string |
|
||||
| `model` | Model id |
|
||||
| `finish` | Finish reason (`stop`, …) |
|
||||
| `backend` | e.g. `openai-compatible` / `ollama` |
|
||||
| `usage` | `{prompt_tokens,completion_tokens,total_tokens}` |
|
||||
| `tokens` | Alias of `usage.total_tokens` (agent metrics) |
|
||||
| `name` | Optional handle tag (`fast` / `reasoning`) |
|
||||
| `llm_calls` | `1` per ask |
|
||||
| `tool_calls` | List of OpenAI-shaped tool call objects (empty when none) |
|
||||
|
||||
### Compat
|
||||
|
||||
`complete` / `chat` still return a bare string (or event list when `stream=True`) so `ext/ai/agent` and older runbooks keep working. **New code should use `ask` / `stream` / `collect`.**
|
||||
|
||||
## Environment
|
||||
|
||||
```env
|
||||
OPENAI_API_KEY=sk-...
|
||||
OPENAI_BASE_URL=https://api.openai.com/v1
|
||||
OPENAI_MODEL=gpt-4o-mini
|
||||
MARQDO_LLM_FAST_MODEL=...
|
||||
MARQDO_LLM_REASONING_MODEL=...
|
||||
OLLAMA_HOST=http://127.0.0.1:11434/v1
|
||||
```
|
||||
|
||||
Fallbacks: `MARQDO_LLM_API_KEY`, `MARQDO_LLM_BASE_URL`, `MARQDO_LLM_MODEL`.
|
||||
Fallbacks: `MARQDO_LLM_API_KEY`, `MARQDO_LLM_BASE_URL`, `MARQDO_LLM_MODEL`. Ctor also accepts `api_key=` / `base_url=` / `model=` / `backend=`.
|
||||
|
||||
## Backends
|
||||
|
||||
| `backend=` | Module | Notes |
|
||||
|------------|--------|-------|
|
||||
| `openai-compatible` (default) | `ext/ai/llm/openai.mq.md` | Chat Completions HTTP + SSE |
|
||||
| `ollama` | defaults in ctor + same transport | Key defaults to `ollama`; host from `OLLAMA_HOST` |
|
||||
|
||||
## Tests
|
||||
|
||||
- Import smoke: `tests/ext/llm-import.mq.md`
|
||||
- Offline stream: `tests/ext/llm-stream-offline.mq.md` + `tests/lib/net-openai-sse.mq.md`
|
||||
- Live complete (local): `tests/ext/llm-complete.mq.md` + `tests/ext/.env`
|
||||
- Live agent `## step`: `tests/ext/agent-run-live.mq.md` (DeepSeek; gold: `ext_agent_run_live`)
|
||||
- Agent smoke (real `llm` handle): `tests/ext/agent-smoke.mq.md` (gold: `ext_agent_framework_smoke`)
|
||||
- Live complete: `tests/ext/llm-complete.mq.md` (gold: `ext_llm_complete_live`)
|
||||
|
||||
- `tests/ext/llm-import.mq.md`
|
||||
- `tests/ext/llm-ctor-offline.mq.md`
|
||||
- `tests/ext/llm-ask-offline.mq.md` (create + collect)
|
||||
- `tests/ext/llm-stream-offline.mq.md`
|
||||
- `tests/ext/llm-named-offline.mq.md` (fast/reasoning + prompt_load)
|
||||
- `tests/ext/llm-tool-calls-offline.mq.md` (`tool_calls_from` / Result field)
|
||||
- `tests/ext/llm-meta-offline.mq.md` · `llm-entry-prompt-offline.mq.md`
|
||||
- Live: `llm-complete.mq.md` · `llm-stream-live.mq.md`
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 验收 | qdqc:注册登录、评论、desk 内建角色/赋权;`tests/ext/web-rbac-*-smoke.mq.md` |
|
||||
| 状态 | **Accepted · 已落地核心**(HTTP register + `/_rbac/*` + gate permissions;样站 qdqc 已接线) |
|
||||
| 验收 | qdqc:门禁 `desk:access` + 评论权限;`tests/ext/web-rbac-*-smoke.mq.md` |
|
||||
| 状态 | **Accepted · 已落地核心**(gate permissions;`/_rbac/*` 管理台 **opt-in** `管理台=真`) |
|
||||
|
||||
---
|
||||
|
||||
@@ -43,7 +43,7 @@
|
||||
|
||||
| 方法 | ABI | 说明 |
|
||||
|------|-----|------|
|
||||
| `app.rbac` / `权限` | `web_app_rbac` | ensure schema + optional seed catalog table |
|
||||
| `app.rbac` / `启用权限` | `web_app_rbac` | ensure schema;可选 `目录`;`管理台=真` 才挂 `/_rbac/*` |
|
||||
| `app.gate` | `web_app_gate` | 新增 `permissions`/`权限`(CSV);与 `roles` 可并存:有 permissions 时按权限判定 |
|
||||
| `rbac.can` | `web_rbac_can` | `{ok, allowed}` |
|
||||
| `rbac.assign_role` | `web_rbac_assign_role` | 用户←角色 |
|
||||
@@ -56,9 +56,10 @@
|
||||
## 4. 运行时
|
||||
|
||||
1. `listen`:若 `auth.rbac=true` 或调用过 `app.rbac`,对 `db` URL `EnsureSchema` + 种子。
|
||||
2. 登录成功:解析角色 → DB `user_roles` ∪ GFM 角色展开 → session `permissions`(CSV)+ 兼容 `role`。
|
||||
3. `withRBAC`:gate 含 `permissions` 则 `PermissionAllowed`;否则旧 `RoleAllowed`。
|
||||
4. 注册:`POST /register` 写入 `web_users`,默认赋 `member` 角色。
|
||||
2. 仅当 `auth.rbac_desk=true`(`启用权限 管理台=真`)时挂载 `/_rbac/*` API 与 desk。
|
||||
3. 登录成功:解析角色 → DB `user_roles` ∪ GFM 角色展开 → session `permissions`(CSV)+ 兼容 `role`。
|
||||
4. `withRBAC`:gate 含 `permissions` 则 `PermissionAllowed`;否则旧 `RoleAllowed`。
|
||||
5. 注册:`POST /register` 写入 `web_users`,默认赋 `member` 角色。
|
||||
|
||||
---
|
||||
|
||||
|
||||
@@ -0,0 +1,91 @@
|
||||
# ext/web:请求上下文与表单字段来源
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted** |
|
||||
| 日期 | 2026-09-18 |
|
||||
| 相关 | [ext-web.md](ext-web.md) §5.5 · 对照 Django `request.user` / URL kwargs |
|
||||
| 动机 | 禁止扩展库按业务表名(如 `comments`)特判填字段;身份与路由键属于请求上下文 |
|
||||
|
||||
---
|
||||
|
||||
## 0. 原则
|
||||
|
||||
1. **扩展库提供机制,站点表达领域** — 不得出现 `if table == "comments"`。
|
||||
2. **身份只从 session 来,且服务端盖戳** — 客户端提交的 `author` 等不可信。
|
||||
3. **资源键从路由参数来** — 页面已在 `/post/{slug}` 时,表单继承该页 `params`,不解析 Referer 猜 slug。
|
||||
4. **非 `client` 来源的字段默认不渲染** — 服务端在校验前写入;若 POST 带同名键,一律覆盖丢弃。
|
||||
|
||||
---
|
||||
|
||||
## 1. 请求上下文(Request)
|
||||
|
||||
每次处理 HTTP 请求时,框架逻辑上持有:
|
||||
|
||||
| 键 | 含义 |
|
||||
|----|------|
|
||||
| `user` / `username` | 登录名;访客为空 |
|
||||
| `params` | 动态路由捕获(如 `{slug}`) |
|
||||
| `path` | 请求路径 |
|
||||
| `method` | GET / POST / … |
|
||||
| `query` | URL 查询(可选) |
|
||||
|
||||
与 Django 对应:`request.user`、`request.resolver_match.kwargs`、`request.path`。
|
||||
|
||||
页面渲染时已有 `page.params`(`injectParams`)。表单提交时通过隐藏字段 `_mq_params`(JSON)与 `_mq_return`(回跳路径)把**渲染该表单时的页上下文**带回 POST,避免依赖 Referer。
|
||||
|
||||
---
|
||||
|
||||
## 2. 字段表「来源」列
|
||||
|
||||
字段表增加可选列 `来源` / `source`(缺省 = `client`):
|
||||
|
||||
```markdown
|
||||
| 字段 | 标签 | 类型 | 必填 | 默认 | 来源 |
|
||||
|------|------|------|------|------|------|
|
||||
| body | 写下你的想法 | textarea | true | | client |
|
||||
| author | | | true | | session.username |
|
||||
| post_slug | | | true | | route.slug |
|
||||
| created_at | | | false | | now |
|
||||
```
|
||||
|
||||
| 来源值 | 含义 | 渲染 |
|
||||
|--------|------|------|
|
||||
| `client` / 空 | 用户填写 | 正常控件 |
|
||||
| `session.username` / `session.user` / `user` | 当前登录用户名 | 不渲染 |
|
||||
| `route.NAME` / `param.NAME` / `params.NAME` | `params[NAME]` | 不渲染 |
|
||||
| `now` / `utc.now` / `server.now` | UTC `YYYY-MM-DD HH:MM:SS` | 不渲染 |
|
||||
|
||||
`类型=hidden` 仍可显式使用;与 `来源≠client` 叠加时仍不依赖客户端值(服务端覆盖)。
|
||||
|
||||
---
|
||||
|
||||
## 3. 提交管线
|
||||
|
||||
```
|
||||
ParseForm → 去掉来源≠client 的 POST 键
|
||||
→ 按字段来源从 Request 写入
|
||||
→ validate → insert/update
|
||||
→ 回跳:优先 _mq_return(同站路径),否则 form.redirect
|
||||
```
|
||||
|
||||
伪造 `author` / `post_slug` 无效。
|
||||
|
||||
---
|
||||
|
||||
## 4. 页面列表装配(副绑定)
|
||||
|
||||
详情页可附加与主主体**不同查询条件**的列表绑定(如评论):
|
||||
|
||||
```markdown
|
||||
**post = > post.列表装配 主体=`评论卡片` 条件=`评论条件` 排序="-created_at"**
|
||||
```
|
||||
|
||||
条件值同样支持 `{slug}`。渲染在主文章/卡片区之后,无需为子资源单独写 JSON API + 前端过滤。
|
||||
|
||||
---
|
||||
|
||||
## 5. 非目标
|
||||
|
||||
- 不在扩展库内实现「评论」领域类型。
|
||||
- 不把 Referer 解析当作正式 params 来源(仅兼容期可作 `_mq_params` 缺失时的弱回退,金样不依赖)。
|
||||
@@ -3,8 +3,8 @@
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · W0–W7 + P3 + W8(站点图标/Head/图片装配)完结**(SQLite/Postgres 作者面:页面装配 / db CRUD+`where` / form / 嵌入主区 / `app.route` / 路由 `/_part` / admin UI / RBAC / 上传相册 / SEO·RSS·sitemap / **favicon·头资源·图片装配**;Postgres 等见 §9) |
|
||||
| 日期 | 2026-08-11(W8:2026-08-29) |
|
||||
| 相关 | [markdown-mapping.md](markdown-mapping.md) · [module-namespace.md](module-namespace.md) · [objects.md](objects.md) · [ext-abi.md](ext-abi.md) · [ext-cli.md](ext-cli.md) · [stdlib-i18n.md](stdlib-i18n.md) · [web-assets-and-images.md](web-assets-and-images.md) |
|
||||
| 日期 | 2026-08-11(W8:2026-08-29;请求上下文:2026-09-18) |
|
||||
| 相关 | [markdown-mapping.md](markdown-mapping.md) · [module-namespace.md](module-namespace.md) · [objects.md](objects.md) · [ext-abi.md](ext-abi.md) · [ext-cli.md](ext-cli.md) · [stdlib-i18n.md](stdlib-i18n.md) · [web-assets-and-images.md](web-assets-and-images.md) · **[ext-web-request-context.md](ext-web-request-context.md)** |
|
||||
| 安装(目标) | `marqdo ext add web`(中英:`web` / `网页`) |
|
||||
| 本文目的 | 锁定**作者面**与**类 API**;指导实现 |
|
||||
|
||||
@@ -247,6 +247,7 @@ import db:db/index.mq.md
|
||||
|------|------|
|
||||
| compose_components / 组件装配 | 解析组件·样式路径 → 填 nav/sidebar/footer → CSS → 登记 part |
|
||||
| compose_main / 主体装配 | 解析库字段与样式路径 → 填 main → 登记 part(如 id=`index`) |
|
||||
| compose_list / 列表装配 | 副绑定(独立 query/order);可选 target 注入引言元素;见 [ext-web-request-context.md](ext-web-request-context.md) |
|
||||
|
||||
**槽位推断:** 由组件导出名决定(`nav`→顶栏,`side`→侧栏,`foot`→底栏;其余→main)。
|
||||
|
||||
@@ -423,6 +424,7 @@ import db:db/index.mq.md
|
||||
| `类型` | 控件/值类型:`text` `textarea` `number` `email` `url` `checkbox` `select` … |
|
||||
| `必填` | 是否必填 |
|
||||
| `默认` | 可选默认值 |
|
||||
| `来源` / `source` | 可选;见 [ext-web-request-context.md](ext-web-request-context.md):`client`(默认)/ `session.username` / `route.NAME` / `now`;非 client 不渲染且服务端盖戳 |
|
||||
|
||||
#### 5.5.2 校验表(作者面)
|
||||
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
# 三层归属与准入戒律(layers)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · 准入制度(问题一落地)** |
|
||||
| 日期 | 2026-09-22 |
|
||||
| 相关 | [core-surface.md](core-surface.md)(核心表面守卫)· [three-problems.md](../roadmap/three-problems.md) 问题一 · [three-problems-plan.md](../roadmap/three-problems-plan.md) T1.2 |
|
||||
|
||||
> **目的**:语言核心保持小——**Language / Runtime / Document 三层分明,生态功能不淹没语言身份**。本制度是机制不是宣言:违反即 CI 红 / PR 不评审。
|
||||
|
||||
## 1. 三层归属表
|
||||
|
||||
| 层 | 内容 | 仓库位置 |
|
||||
|----|------|----------|
|
||||
| **Language** | 标记映射、词法/行分类、AST、语义、升参、契约检查器、诊断 | `src/lex/` `src/parse/` `src/ast.rs` `src/interp.rs` `src/diagnostics.rs`(及 `contract`/`check`)· 宪法 [markdown-mapping-v0.3.md](markdown-mapping-v0.3.md) · [core-surface.md](core-surface.md) |
|
||||
| **Runtime** | stdlib、插件 ABI、WASM、debugger、view、catalog / schema / mcp CLI、字节码后端 | `lib/` `plugins/` `crates/` `src/catalog.rs` `src/view/` `src/debug/` `src/builtin.rs` `src/bytecode/` |
|
||||
| **Document** | 知识单元、AI skills、示例、官方扩展、用户站 | `*.mq.md` `skills/` `ext/` `public/` `examples/` `doc/` |
|
||||
|
||||
## 2. 准入戒律
|
||||
|
||||
1. **先填层归属**:任何新特性提案(issue / PR / 设计文档)必须先声明进哪层;未填 = 不评审。
|
||||
2. **Language 层变更 = ADR**:新增核心标记必须有 `doc/adr/` 编号 + 「为什么现有标记不够」+ [core-surface.md](core-surface.md) 增行 + 样例 + `CORE_CONSTRUCTS` 同步(缺任一 → CI 守卫红)。
|
||||
3. **核心计数只许持平或下降**:每个 release 的核心标记计数不得增长;确需增长即触发 Language 层复盘(ADR 里写明不可替代理由)。
|
||||
4. **默认进 ext**:能用扩展、stdlib、插件解决的,一律不进核心。
|
||||
5. **CHANGELOG 单列「核心表面 Δ」**:每个 release 写明增删(无 Δ 写「无」);发版 checklist(`.cursor/skills/marqdo-release/SKILL.md`)含此步。
|
||||
|
||||
## 3. PR 模板
|
||||
|
||||
`.github/PULL_REQUEST_TEMPLATE.md` 含「层归属」必填项与「核心表面」勾选——与本制度一一对应。
|
||||
@@ -616,3 +616,4 @@ FnDef.params:
|
||||
| 2026-09-13 | 决议空返回(§7.2)与提及性反引号(§4.4);形参改为「升参」与绑定图分离(§8.2) |
|
||||
| 2026-09-14 | §7.2:`---` 不再收束;返回语句结束函数体解析;空返回首选 `*None*` |
|
||||
| 2026-09-14 | 取元主语法改为 `[键](集合)`;脚注取元过渡保留 |
|
||||
| 2026-09-24 | Artifact Metadata Binding(`${…}` Phase 1,仅 frontmatter):见 [binding.md](binding.md) · ADR 0006 |
|
||||
@@ -130,7 +130,7 @@ Markdown **标记即语法**。输出**不是**语言架构原语,而是内置
|
||||
|
||||
## 5. Frontmatter 与跨文件
|
||||
|
||||
文件头 `---` 对为元信息。其中 `import bind:path.mq.md` / `导入` 为**文件导入**;`import bind:lib.member` 为**短名**(废除独立 `use` 与 frontmatter `>` 导入)。调用经裸点号路径(`time.parse`、`agent.agent`);实例方法在独立 `>` 调用须 `` > `var`.method ``(`*…*` 段内接收者裸名)。详见 [module-namespace.md](module-namespace.md)。
|
||||
文件头 `---` 对为 **Artifact Metadata**。其中 `import bind:path.mq.md` / `导入` 为**文件导入**;`import bind:lib.member` 为**短名**(废除独立 `use` 与 frontmatter `>` 导入)。其它 `key: value` 可为字面量或 **Binding**(`${env.NAME}` 等,加载期解析);见 [binding.md](binding.md)。调用经裸点号路径(`time.parse`、`agent.agent`);实例方法在独立 `>` 调用须 `` > `var`.method ``(`*…*` 段内接收者裸名)。详见 [module-namespace.md](module-namespace.md)。
|
||||
|
||||
**函数体内的** `---` / `***` 不解释为 frontmatter(见 §11)。
|
||||
|
||||
|
||||
@@ -74,6 +74,8 @@ Watch: https://launchpad.net/~cflmy/+archive/ubuntu/marqdo/+builds
|
||||
| Symptom | Fix |
|
||||
|---------|-----|
|
||||
| `lock file version 4 requires -Znext-lockfile-bump` | Rebuild with current `ppa-build-source.sh` (bundles Rust); bump `1ppaN` |
|
||||
| `failed to calculate checksum of: …/vendor/…/Cargo.toml.orig` | `dh_clean` purged `*.orig`; fixed via `dh_clean -Xvendor/` (+ strip `.orig` only when **creating a new** orig); bump `1ppaN` with **`-sd`** |
|
||||
| `orig.tar.xz already exists … different contents` | Never rewrite an uploaded upstream `.orig`; reuse PPA file (`ppa-build-source.sh` downloads it) and `debuild -S -sd` |
|
||||
| Same version rejected by Launchpad | Bump `debian/changelog` (`1ppa2`, …) |
|
||||
| Wrong series | Enable series on PPA; rebuild with that series name |
|
||||
|
||||
|
||||
+1373
File diff suppressed because it is too large.
Load diff
+933
@@ -0,0 +1,933 @@
|
||||
这是marqdo的智能体框架,我一直都不满意,我认为marqdo的智能体框架应该更好才对,因为marqdo本身就是一门极好的AI原生语言,针对这个智能体框架,我的构想是,每次执行,智能体都会沉淀mq.md文档,超过三次的调用就会自动总结优化出一份类似与记忆的mq.md文档,把每一步都拆解的非常好,类似与人类的某些学习过程,当完全学习到了一些内容,实际上后续就可以不再每次都聚精会神,而是用一种较低能耗运行,就像是我们沉淀下来的mq.md代码,其中可以使用非常少的大模型思考,或者使用类似Jev的决策模型,或者干脆只使用分支控制,之后调用即可,你觉得呢?
|
||||
|
||||
对,而且我现在觉得你这个构想**很可能比目前的 Agent 框架本身更接近 Marqdo 真正应该成为的东西**。
|
||||
|
||||
我仔细看了你上传的 `ext/ai/agent`。其实你已经无意中铺好了不少基础:现在的 `plan` 已经有 `reuse`、`near_match`、`soft_match`、`promote`、`improve_every=3` 和 `kb_dir=.marqdo/agent-kb`;命中已有 skill 后甚至可以走 `llm_free` 路径。 而且现有逻辑已经会把成功的 workbook promote 到 agent KB。
|
||||
|
||||
**所以我觉得你现在缺的不是“memory”,而是一个更深的概念:Learning → Compilation。**
|
||||
|
||||
---
|
||||
|
||||
# 1. 我会把你想做的东西定义成:Agent 的“认知编译”
|
||||
|
||||
你描述的人类学习过程非常准确:
|
||||
|
||||
```text
|
||||
第一次:
|
||||
仔细思考
|
||||
↓
|
||||
尝试
|
||||
↓
|
||||
犯错
|
||||
↓
|
||||
修正
|
||||
|
||||
第二次:
|
||||
仍然需要思考
|
||||
↓
|
||||
但已经有经验
|
||||
|
||||
第三次:
|
||||
开始形成套路
|
||||
|
||||
多次之后:
|
||||
“我知道怎么做了”
|
||||
↓
|
||||
不再重新思考
|
||||
↓
|
||||
直接执行
|
||||
```
|
||||
|
||||
传统 Agent:
|
||||
|
||||
```text
|
||||
User
|
||||
↓
|
||||
LLM
|
||||
↓
|
||||
Tool
|
||||
↓
|
||||
LLM
|
||||
↓
|
||||
Tool
|
||||
↓
|
||||
LLM
|
||||
```
|
||||
|
||||
每次任务都重新付出推理成本。
|
||||
|
||||
你想让 Marqdo 变成:
|
||||
|
||||
```text
|
||||
新任务
|
||||
│
|
||||
▼
|
||||
是否已经学会?
|
||||
/ \
|
||||
是 否
|
||||
│ │
|
||||
▼ ▼
|
||||
已编译 mq.md LLM 推理
|
||||
│ │
|
||||
▼ ▼
|
||||
低成本执行 执行 + 观察
|
||||
│
|
||||
▼
|
||||
沉淀 mq.md
|
||||
│
|
||||
▼
|
||||
验证 / 优化
|
||||
│
|
||||
▼
|
||||
编译成 Skill
|
||||
```
|
||||
|
||||
我认为这个想法**非常漂亮**。
|
||||
|
||||
---
|
||||
|
||||
# 2. 而且这和 Marqdo 的语言设计天然吻合
|
||||
|
||||
这可能是你整个项目最重要的一次“闭环”。
|
||||
|
||||
你前面定义:
|
||||
|
||||
> `.mq.md` = Markdown + 可执行程序 + 知识。
|
||||
|
||||
现在 Agent 又产生:
|
||||
|
||||
> `.mq.md` = Agent 学会的经验。
|
||||
|
||||
于是:
|
||||
|
||||
```text
|
||||
Agent
|
||||
│
|
||||
│ 学习
|
||||
▼
|
||||
.mq.md
|
||||
│
|
||||
┌───────┼────────┐
|
||||
↓ ↓ ↓
|
||||
知识 策略 程序
|
||||
│ │ │
|
||||
└───────┼────────┘
|
||||
▼
|
||||
Agent Skill
|
||||
│
|
||||
▼
|
||||
低成本执行
|
||||
```
|
||||
|
||||
这就非常有意思了。
|
||||
|
||||
**Agent 的 memory 不再是一段向量 embedding,也不只是聊天历史。**
|
||||
|
||||
它是:
|
||||
|
||||
> **可阅读、可修改、可验证、可执行的知识程序。**
|
||||
|
||||
这才是真正的 **Marqdo-native Agent**。
|
||||
|
||||
---
|
||||
|
||||
# 3. 你现在的 Agent 已经有这个雏形,但还没有真正“学习”
|
||||
|
||||
从你现在的代码看,已经存在:
|
||||
|
||||
```text
|
||||
reuse
|
||||
near_match
|
||||
soft_match
|
||||
promote
|
||||
improve_every = 3
|
||||
llm_free
|
||||
```
|
||||
|
||||
这些机制实际上已经在做:
|
||||
|
||||
> **经验复用。**
|
||||
|
||||
例如当前逻辑会查找已有 KB,如果命中 `llm_free` skill,就可以直接运行 resource,而不是再让父 Agent 做一轮 LLM 推理。
|
||||
|
||||
这是很好的基础。
|
||||
|
||||
但我认为:
|
||||
|
||||
> **Reuse ≠ Learning。**
|
||||
|
||||
现在更像:
|
||||
|
||||
```text
|
||||
成功任务
|
||||
↓
|
||||
保存
|
||||
↓
|
||||
以后查找
|
||||
↓
|
||||
复用
|
||||
```
|
||||
|
||||
而你现在构想的是:
|
||||
|
||||
```text
|
||||
成功任务
|
||||
↓
|
||||
观察多次执行
|
||||
↓
|
||||
抽象共同结构
|
||||
↓
|
||||
去掉偶然信息
|
||||
↓
|
||||
形成一般策略
|
||||
↓
|
||||
验证
|
||||
↓
|
||||
编译
|
||||
↓
|
||||
以后低成本执行
|
||||
```
|
||||
|
||||
这两者差距非常大。
|
||||
|
||||
---
|
||||
|
||||
# 4. 我甚至建议你不要叫它 Memory
|
||||
|
||||
我会给它一个更有 Marqdo 味道的名字:
|
||||
|
||||
## **Skill Compilation**
|
||||
|
||||
或者:
|
||||
|
||||
## **Experience Compilation**
|
||||
|
||||
甚至:
|
||||
|
||||
## **Cognitive Compilation**
|
||||
|
||||
整个过程:
|
||||
|
||||
```text
|
||||
Experience
|
||||
↓
|
||||
Episodic Trace
|
||||
↓
|
||||
Pattern Extraction
|
||||
↓
|
||||
Generalization
|
||||
↓
|
||||
Marqdo Skill
|
||||
↓
|
||||
Validation
|
||||
↓
|
||||
Compiled Skill
|
||||
```
|
||||
|
||||
然后 Agent 不再只是:
|
||||
|
||||
> “记住我以前做过什么。”
|
||||
|
||||
而是:
|
||||
|
||||
> **“把以前的思考编译成以后不需要思考的程序。”**
|
||||
|
||||
我觉得这是一个非常强的概念。
|
||||
|
||||
---
|
||||
|
||||
# 5. 例如真的可以这样工作
|
||||
|
||||
第一次:
|
||||
|
||||
用户:
|
||||
|
||||
> 把这个 CSV 中年龄大于 18 的用户筛出来,并按照年龄排序。
|
||||
|
||||
Agent:
|
||||
|
||||
```text
|
||||
LLM reasoning
|
||||
↓
|
||||
读取 CSV
|
||||
↓
|
||||
分析结构
|
||||
↓
|
||||
过滤
|
||||
↓
|
||||
排序
|
||||
↓
|
||||
输出
|
||||
```
|
||||
|
||||
产生:
|
||||
|
||||
```text
|
||||
.marqdo/agent-runs/xxx.mq.md
|
||||
```
|
||||
|
||||
第二次用户:
|
||||
|
||||
> 把另一个 CSV 中年龄大于 18 的用户筛出来,并按照年龄排序。
|
||||
|
||||
Agent 发现:
|
||||
|
||||
```text
|
||||
intent similarity = 0.94
|
||||
```
|
||||
|
||||
于是:
|
||||
|
||||
```text
|
||||
REUSE
|
||||
```
|
||||
|
||||
直接调用之前的程序。
|
||||
|
||||
---
|
||||
|
||||
第三、第四、第五次:
|
||||
|
||||
Agent 发现:
|
||||
|
||||
```text
|
||||
same structure
|
||||
different file
|
||||
different column names
|
||||
```
|
||||
|
||||
这时候不应该继续保存五个:
|
||||
|
||||
```text
|
||||
task1.mq.md
|
||||
task2.mq.md
|
||||
task3.mq.md
|
||||
task4.mq.md
|
||||
task5.mq.md
|
||||
```
|
||||
|
||||
而应该让 LLM 总结:
|
||||
|
||||
```markdown
|
||||
# Filter and Sort CSV Users
|
||||
|
||||
## Preconditions
|
||||
|
||||
- Input contains user records.
|
||||
- An age field exists.
|
||||
|
||||
## Procedure
|
||||
|
||||
1. Read the input table.
|
||||
2. Keep rows where `age > 18`.
|
||||
3. Sort by `age` ascending.
|
||||
|
||||
## Variables
|
||||
|
||||
- `input`
|
||||
- `age_field`
|
||||
- `output`
|
||||
|
||||
## Exceptions
|
||||
|
||||
- Missing age field
|
||||
- Invalid age
|
||||
|
||||
## Examples
|
||||
|
||||
| Input | Output |
|
||||
|---|---|
|
||||
| ... | ... |
|
||||
```
|
||||
|
||||
然后:
|
||||
|
||||
```text
|
||||
skill_status = compiled
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# 6. 更关键的是:这个 mq.md 可以逐渐“去 LLM 化”
|
||||
|
||||
这是你刚才说的:
|
||||
|
||||
> “其中可以使用非常少的大模型思考,或者使用类似 Jev 的决策模型,或者干脆只使用分支控制。”
|
||||
|
||||
**我非常赞同。**
|
||||
|
||||
我甚至认为这应该成为 Marqdo Agent 的核心架构。
|
||||
|
||||
可以设计成一个:
|
||||
|
||||
# Cognitive Cost Ladder
|
||||
|
||||
```text
|
||||
Level 0
|
||||
Deterministic Marqdo
|
||||
│
|
||||
▼
|
||||
Level 1
|
||||
Rule / Branch
|
||||
│
|
||||
▼
|
||||
Level 2
|
||||
Small decision model
|
||||
│
|
||||
▼
|
||||
Level 3
|
||||
Small LLM
|
||||
│
|
||||
▼
|
||||
Level 4
|
||||
Full LLM reasoning
|
||||
│
|
||||
▼
|
||||
Level 5
|
||||
Multi-agent exploration
|
||||
```
|
||||
|
||||
新问题:
|
||||
|
||||
```text
|
||||
Full LLM
|
||||
↓
|
||||
learn
|
||||
↓
|
||||
Marqdo Skill
|
||||
↓
|
||||
validation
|
||||
↓
|
||||
Level 2/1
|
||||
```
|
||||
|
||||
于是 Agent 会越来越“便宜”。
|
||||
|
||||
---
|
||||
|
||||
# 7. 这甚至给你提供了一个极漂亮的学习曲线
|
||||
|
||||
假设一个任务第一次需要:
|
||||
|
||||
```text
|
||||
8 次 LLM calls
|
||||
```
|
||||
|
||||
第二次:
|
||||
|
||||
```text
|
||||
5 次
|
||||
```
|
||||
|
||||
第三次:
|
||||
|
||||
```text
|
||||
3 次
|
||||
```
|
||||
|
||||
学习完成:
|
||||
|
||||
```text
|
||||
0 次
|
||||
```
|
||||
|
||||
或者:
|
||||
|
||||
```text
|
||||
1 次 small model
|
||||
```
|
||||
|
||||
这时候你就有一个非常漂亮的指标:
|
||||
|
||||
## Reasoning Amortization
|
||||
|
||||
定义:
|
||||
|
||||
$$
|
||||
C_n =
|
||||
\frac{
|
||||
\text{LLM inference cost over first } n \text{ executions}
|
||||
}{n}
|
||||
$$
|
||||
|
||||
随着执行次数增加:
|
||||
|
||||
```text
|
||||
Cost
|
||||
│\
|
||||
│ \
|
||||
│ \
|
||||
│ \
|
||||
│ \____
|
||||
│ ─────────
|
||||
└───────────────────
|
||||
executions
|
||||
```
|
||||
|
||||
**这甚至解决了你之前担心的 Marqdo 性能难以证明的问题。**
|
||||
|
||||
你之前不知道怎么证明:
|
||||
|
||||
> Marqdo 比 Python 更适合 AI。
|
||||
|
||||
但现在你可以研究:
|
||||
|
||||
> **Marqdo Agent 能否把高成本 LLM reasoning 逐渐编译成低成本 executable knowledge?**
|
||||
|
||||
这个是一个非常明确、可测量、而且很有研究价值的问题。
|
||||
|
||||
---
|
||||
|
||||
# 8. 但是这里有一个非常重要的危险
|
||||
|
||||
你说:
|
||||
|
||||
> “超过三次调用就自动总结。”
|
||||
|
||||
我建议:
|
||||
|
||||
**不要真的这么简单。**
|
||||
|
||||
三次可以作为:
|
||||
|
||||
> **触发学习候选**
|
||||
|
||||
但不能作为:
|
||||
|
||||
> **自动认为已经学会。**
|
||||
|
||||
否则会发生:
|
||||
|
||||
```text
|
||||
第一次:特殊情况
|
||||
第二次:特殊情况
|
||||
第三次:特殊情况
|
||||
```
|
||||
|
||||
LLM 总结:
|
||||
|
||||
```text
|
||||
General rule:
|
||||
...
|
||||
```
|
||||
|
||||
然后把偶然性错误“编译”进 Skill。
|
||||
|
||||
这会非常危险。
|
||||
|
||||
---
|
||||
|
||||
# 9. 我建议建立“经验 → 技能”的四阶段
|
||||
|
||||
## Stage 1 — Episode
|
||||
|
||||
每次任务产生:
|
||||
|
||||
```text
|
||||
task
|
||||
context
|
||||
actions
|
||||
tool calls
|
||||
observations
|
||||
errors
|
||||
result
|
||||
cost
|
||||
```
|
||||
|
||||
例如:
|
||||
|
||||
```text
|
||||
episode-001.mq.md
|
||||
episode-002.mq.md
|
||||
episode-003.mq.md
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Stage 2 — Candidate Skill
|
||||
|
||||
达到阈值之后:
|
||||
|
||||
```text
|
||||
3+ similar episodes
|
||||
```
|
||||
|
||||
让 LLM 总结:
|
||||
|
||||
```text
|
||||
candidate.mq.md
|
||||
```
|
||||
|
||||
但它还是:
|
||||
|
||||
> **candidate**
|
||||
|
||||
而不是正式 skill。
|
||||
|
||||
---
|
||||
|
||||
## Stage 3 — Verification
|
||||
|
||||
用历史任务重新运行:
|
||||
|
||||
```text
|
||||
episode 1
|
||||
episode 2
|
||||
episode 3
|
||||
```
|
||||
|
||||
测试:
|
||||
|
||||
```text
|
||||
candidate skill
|
||||
```
|
||||
|
||||
如果:
|
||||
|
||||
```text
|
||||
3 / 3 success
|
||||
```
|
||||
|
||||
再加新的 held-out task:
|
||||
|
||||
```text
|
||||
episode 4
|
||||
```
|
||||
|
||||
如果:
|
||||
|
||||
```text
|
||||
4 / 4
|
||||
```
|
||||
|
||||
才提高 confidence。
|
||||
|
||||
---
|
||||
|
||||
## Stage 4 — Compilation
|
||||
|
||||
最后:
|
||||
|
||||
```text
|
||||
candidate
|
||||
↓
|
||||
verified
|
||||
↓
|
||||
compiled
|
||||
```
|
||||
|
||||
此时才允许:
|
||||
|
||||
```text
|
||||
llm_free = true
|
||||
```
|
||||
|
||||
你目前的系统已经有 `llm_free` 命中后直接运行 resource 的机制。
|
||||
|
||||
我认为未来这个 flag **不应该是人工/静态指定的属性,而应该成为学习系统的产物。**
|
||||
|
||||
---
|
||||
|
||||
# 10. 甚至可以让 mq.md 自己记录“学习程度”
|
||||
|
||||
比如:
|
||||
|
||||
```yaml
|
||||
---
|
||||
type: skill
|
||||
status: compiled
|
||||
confidence: 0.97
|
||||
evidence: 17
|
||||
success_rate: 0.941
|
||||
llm_free: true
|
||||
created_from: 6c8...
|
||||
last_validated: 2026-09-23
|
||||
avg_reasoning_cost: 0.2
|
||||
---
|
||||
```
|
||||
|
||||
然后正文:
|
||||
|
||||
```markdown
|
||||
# CSV 成人用户筛选
|
||||
|
||||
## Intent
|
||||
|
||||
筛选年龄大于 18 岁的用户。
|
||||
|
||||
## Procedure
|
||||
|
||||
- 读取 CSV
|
||||
- 判断 age
|
||||
- 过滤
|
||||
- 排序
|
||||
|
||||
## Fallback
|
||||
|
||||
如果无法确定 age 字段:
|
||||
|
||||
> ask_llm
|
||||
|
||||
## Verification
|
||||
|
||||
| Case | Result |
|
||||
|---|---|
|
||||
| case-001 | ✓ |
|
||||
| case-002 | ✓ |
|
||||
| case-003 | ✓ |
|
||||
| case-004 | ✓ |
|
||||
```
|
||||
|
||||
注意这个设计里非常重要的一点:
|
||||
|
||||
> **Skill 本身可以包含一个 fallback 到 LLM 的路径。**
|
||||
|
||||
所以不是:
|
||||
|
||||
```text
|
||||
学会 / 不会
|
||||
```
|
||||
|
||||
而是:
|
||||
|
||||
```text
|
||||
确定部分 → deterministic
|
||||
不确定部分 → small model
|
||||
真正未知 → full LLM
|
||||
```
|
||||
|
||||
这就非常接近人类的实际认知过程。
|
||||
|
||||
---
|
||||
|
||||
# 11. 我甚至会把 Agent 的运行模型改成这样
|
||||
|
||||
```text
|
||||
User Task
|
||||
│
|
||||
▼
|
||||
┌─────────────────┐
|
||||
│ Skill Resolver │
|
||||
└────────┬────────┘
|
||||
│
|
||||
┌────────────┼────────────┐
|
||||
▼ ▼ ▼
|
||||
Exact Near Unknown
|
||||
Hit Hit
|
||||
│ │ │
|
||||
▼ ▼ ▼
|
||||
mq.md execute small model Full LLM
|
||||
│ │ │
|
||||
└────────────┼────────────┘
|
||||
▼
|
||||
Result
|
||||
│
|
||||
▼
|
||||
Episode
|
||||
│
|
||||
▼
|
||||
Experience Store
|
||||
│
|
||||
┌────────┴────────┐
|
||||
│ │
|
||||
repeated? novel?
|
||||
│
|
||||
▼
|
||||
Skill synthesis
|
||||
│
|
||||
▼
|
||||
Verification
|
||||
│
|
||||
▼
|
||||
Compilation
|
||||
│
|
||||
▼
|
||||
mq.md Skill
|
||||
```
|
||||
|
||||
**这就已经不是普通 Agent framework 了。**
|
||||
|
||||
它是一个:
|
||||
|
||||
> **Self-compiling Agent**
|
||||
|
||||
---
|
||||
|
||||
# 12. 而 Marqdo 在这里有一个其他 Agent framework 很难天然拥有的优势
|
||||
|
||||
因为“学习结果”不是:
|
||||
|
||||
```json
|
||||
{
|
||||
"memory": "..."
|
||||
}
|
||||
```
|
||||
|
||||
也不是:
|
||||
|
||||
```text
|
||||
embedding vector
|
||||
```
|
||||
|
||||
而是:
|
||||
|
||||
```text
|
||||
learned_skill.mq.md
|
||||
```
|
||||
|
||||
这意味着用户可以:
|
||||
|
||||
```text
|
||||
打开
|
||||
阅读
|
||||
修改
|
||||
运行
|
||||
测试
|
||||
分享
|
||||
Git commit
|
||||
diff
|
||||
fork
|
||||
```
|
||||
|
||||
甚至:
|
||||
|
||||
```text
|
||||
Agent A
|
||||
↓
|
||||
learned_skill.mq.md
|
||||
↓
|
||||
Agent B
|
||||
```
|
||||
|
||||
**Agent 的经验第一次成为一种真正可移植的软件 artifact。**
|
||||
|
||||
我认为这是你整个 Marqdo 项目里非常值得深入挖掘的东西。
|
||||
|
||||
---
|
||||
|
||||
# 13. 而且它和你前面说的“Executable Knowledge”完全闭环
|
||||
|
||||
你之前提出:
|
||||
|
||||
```text
|
||||
Executable Document
|
||||
↓
|
||||
Executable Knowledge Base
|
||||
```
|
||||
|
||||
现在再往前一步:
|
||||
|
||||
```text
|
||||
Executable Knowledge
|
||||
↓
|
||||
Agent Experience
|
||||
↓
|
||||
Agent Learning
|
||||
↓
|
||||
Compiled Knowledge
|
||||
↓
|
||||
Executable Skill
|
||||
```
|
||||
|
||||
于是整个 Marqdo 可以形成:
|
||||
|
||||
```text
|
||||
┌───────────────┐
|
||||
│ Human │
|
||||
└───────┬───────┘
|
||||
│
|
||||
▼
|
||||
Natural Task
|
||||
│
|
||||
▼
|
||||
AI Reasoning
|
||||
│
|
||||
▼
|
||||
Executable Work
|
||||
│
|
||||
▼
|
||||
.mq.md
|
||||
│
|
||||
┌───────┴───────┐
|
||||
▼ ▼
|
||||
Knowledge Skill
|
||||
│ │
|
||||
└───────┬───────┘
|
||||
▼
|
||||
Future Agent
|
||||
│
|
||||
▼
|
||||
Less Reasoning
|
||||
│
|
||||
▼
|
||||
More Execution
|
||||
```
|
||||
|
||||
这就是我现在最喜欢 Marqdo 的地方:
|
||||
|
||||
> **Agent 不只是使用知识库;Agent 会把自己的经验沉淀成知识库,然后知识库反过来改变 Agent 的计算方式。**
|
||||
|
||||
这是一个真正的闭环。
|
||||
|
||||
---
|
||||
|
||||
## 最后,我会建议你对现有 Agent 做一个非常明确的架构升级
|
||||
|
||||
你现在已经有:
|
||||
|
||||
```text
|
||||
reuse
|
||||
near_match
|
||||
soft_match
|
||||
promote
|
||||
improve_every=3
|
||||
llm_free
|
||||
```
|
||||
|
||||
所以**完全没必要推倒重来**。
|
||||
|
||||
我会把它演化成:
|
||||
|
||||
```text
|
||||
Current Agent
|
||||
│
|
||||
┌────────┴────────┐
|
||||
│ │
|
||||
Execution Experience
|
||||
│ │
|
||||
│ ▼
|
||||
│ Episode Store
|
||||
│ │
|
||||
│ ≥ N similar
|
||||
│ │
|
||||
│ ▼
|
||||
│ Skill Synthesis
|
||||
│ │
|
||||
│ ▼
|
||||
│ Skill Validation
|
||||
│ │
|
||||
│ ▼
|
||||
│ Skill Compilation
|
||||
│ │
|
||||
└─────────────┬───┘
|
||||
▼
|
||||
.mq.md Skill
|
||||
│
|
||||
┌────────┼────────┐
|
||||
▼ ▼ ▼
|
||||
Branch Small LLM Full LLM
|
||||
```
|
||||
|
||||
而且你当前已经有一个非常好的“终点”:**当 child 返回 value 时,可以 deterministic 地 solidify,而不是再次让父 LLM 发明 patch。** 当前代码已经明确这么处理。
|
||||
|
||||
所以我会把你的下一版 Agent 的核心 slogan 直接定成:
|
||||
|
||||
> **Think → Execute → Learn → Compile → Reuse**
|
||||
|
||||
或者更 Marqdo 一点:
|
||||
|
||||
> **Every successful thought can become executable knowledge.**
|
||||
|
||||
这比“Marqdo 有一个 Agent framework”强得多。
|
||||
|
||||
而且这件事**非常适合做你前面说的论文实验**:你甚至可以测一个 Agent 在重复任务上的 **LLM calls / tokens / latency / success rate 随经验次数变化的曲线**,直接验证“推理成本被 mq.md 技能逐渐摊销”的假设。这样 Marqdo 的 Agent、Executable Knowledge 和你之前苦恼的“性能怎么证明”三个问题,反而被同一个实验串起来了。
|
||||
+2626
File diff suppressed because it is too large.
Load diff
+541
@@ -0,0 +1,541 @@
|
||||
有时候可能用户不太好接入Jev,所以阶段4应该要允许用户使用小参数的模型代替,你认为呢?
|
||||
|
||||
我非常赞同,而且我甚至建议**不要把 Jev 作为阶段 4 的必选组件**。
|
||||
|
||||
更准确的设计应该是:
|
||||
|
||||
> **Jev 是一种可选的决策器(Decision Engine),小参数模型是另一种可选实现;Marqdo Agent 本身只规定“需要一个 Router/Policy”,而不规定 Router 必须由什么实现。**
|
||||
|
||||
这样会让整个框架开放很多。
|
||||
|
||||
### 1. 阶段 4 应该改成“Adaptive Routing”
|
||||
|
||||
原来的:
|
||||
|
||||
```text
|
||||
Phase 4
|
||||
↓
|
||||
Jev Router
|
||||
```
|
||||
|
||||
我建议改成:
|
||||
|
||||
```text
|
||||
Phase 4: Adaptive Routing
|
||||
│
|
||||
┌─────────┼─────────┐
|
||||
↓ ↓ ↓
|
||||
Marqdo Rule Jev Small LLM
|
||||
│ │ │
|
||||
└─────────┼─────────┘
|
||||
↓
|
||||
Route Policy
|
||||
```
|
||||
|
||||
甚至还可以允许:
|
||||
|
||||
```text
|
||||
Rule
|
||||
Small Model
|
||||
Jev
|
||||
Full LLM
|
||||
```
|
||||
|
||||
混合使用。
|
||||
|
||||
---
|
||||
|
||||
# 2. 为什么这个设计更合理
|
||||
|
||||
因为用户真正需要的不是:
|
||||
|
||||
> “我要接入 Jev。”
|
||||
|
||||
而是:
|
||||
|
||||
> “我希望 Agent 学会以后,不要每次都调用大模型。”
|
||||
|
||||
这两个概念应该分开。
|
||||
|
||||
所以 Marqdo 只定义接口:
|
||||
|
||||
```text
|
||||
agent.route(task, candidates)
|
||||
```
|
||||
|
||||
至于内部怎么实现,可以是:
|
||||
|
||||
### A. 纯 Marqdo
|
||||
|
||||
```text
|
||||
if task.type == "json":
|
||||
return json-repair
|
||||
|
||||
if task.type == "search":
|
||||
return corpus-search
|
||||
```
|
||||
|
||||
完全不需要模型。
|
||||
|
||||
---
|
||||
|
||||
### B. 小参数模型
|
||||
|
||||
比如用户已经有:
|
||||
|
||||
```text
|
||||
Qwen 0.5B
|
||||
Qwen 1.5B
|
||||
Llama 1B
|
||||
某个本地模型
|
||||
```
|
||||
|
||||
就可以:
|
||||
|
||||
```text
|
||||
task
|
||||
↓
|
||||
small model
|
||||
↓
|
||||
"json-repair"
|
||||
```
|
||||
|
||||
甚至可以让用户直接配置:
|
||||
|
||||
```yaml
|
||||
router:
|
||||
type: llm
|
||||
|
||||
provider: openai-compatible
|
||||
base_url: ...
|
||||
api_key: ...
|
||||
model: ...
|
||||
```
|
||||
|
||||
这和你之前设计 AI 斗蛐蛐时提出的 **OpenAI-compatible URL + Key** 思路其实也很契合。
|
||||
|
||||
---
|
||||
|
||||
### C. Jev
|
||||
|
||||
如果用户安装了 Jev:
|
||||
|
||||
```text
|
||||
task
|
||||
↓
|
||||
Jev
|
||||
↓
|
||||
policy
|
||||
```
|
||||
|
||||
那么就用 Jev。
|
||||
|
||||
---
|
||||
|
||||
### D. 大模型
|
||||
|
||||
甚至最开始没有任何 Router:
|
||||
|
||||
```text
|
||||
task
|
||||
↓
|
||||
large LLM
|
||||
↓
|
||||
decide
|
||||
```
|
||||
|
||||
也是合法的。
|
||||
|
||||
所以:
|
||||
|
||||
> **Router 是抽象能力,不是具体模型。**
|
||||
|
||||
---
|
||||
|
||||
# 3. 更重要的是:Router 本身也可以被学习
|
||||
|
||||
这里我觉得还能进一步提升你的设计。
|
||||
|
||||
一开始:
|
||||
|
||||
```text
|
||||
Task
|
||||
↓
|
||||
Large LLM
|
||||
↓
|
||||
选择 skill
|
||||
```
|
||||
|
||||
系统观察:
|
||||
|
||||
```text
|
||||
task A → json-repair
|
||||
task B → json-repair
|
||||
task C → json-repair
|
||||
task D → corpus-search
|
||||
```
|
||||
|
||||
之后可以自动生成:
|
||||
|
||||
```text
|
||||
router.mq.md
|
||||
```
|
||||
|
||||
例如:
|
||||
|
||||
```markdown
|
||||
---
|
||||
type: policy
|
||||
status: compiled
|
||||
---
|
||||
|
||||
## route
|
||||
|
||||
1. `task.type` == "json"
|
||||
*"json-repair"*
|
||||
|
||||
2. `task.type` == "search"
|
||||
*"corpus-search"*
|
||||
|
||||
3. *
|
||||
*"llm"*
|
||||
```
|
||||
|
||||
那么你甚至不需要 Jev。
|
||||
|
||||
**Marqdo 自己的控制流就是 Router。**
|
||||
|
||||
---
|
||||
|
||||
# 4. 所以我会重新设计 Cost Ladder
|
||||
|
||||
之前的:
|
||||
|
||||
```text
|
||||
L0 deterministic
|
||||
L1 rule
|
||||
L2 small model
|
||||
L3 small LLM
|
||||
L4 large LLM
|
||||
L5 exploration
|
||||
```
|
||||
|
||||
我建议改成:
|
||||
|
||||
```text
|
||||
┌────────────────────────┐
|
||||
│ Adaptive Router │
|
||||
└───────────┬────────────┘
|
||||
│
|
||||
┌─────────────────┼─────────────────┐
|
||||
↓ ↓ ↓
|
||||
Deterministic Small Model Large LLM
|
||||
│ │ │
|
||||
↓ ↓ ↓
|
||||
L0/L1 L2/L3 L4
|
||||
│
|
||||
↓
|
||||
Exploration L5
|
||||
```
|
||||
|
||||
这里最关键的是:
|
||||
|
||||
> **L2/L3 不应该规定具体模型。**
|
||||
|
||||
可以是:
|
||||
|
||||
```text
|
||||
Marqdo rules
|
||||
OR
|
||||
Jev
|
||||
OR
|
||||
0.5B model
|
||||
OR
|
||||
1B model
|
||||
OR
|
||||
3B model
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# 5. 我甚至建议用户可以配置多个 Router
|
||||
|
||||
例如:
|
||||
|
||||
```yaml
|
||||
router:
|
||||
candidates:
|
||||
|
||||
- name: rules
|
||||
type: marqdo
|
||||
|
||||
- name: local
|
||||
type: llm
|
||||
model: qwen-0.5b
|
||||
|
||||
- name: jev
|
||||
type: jev
|
||||
|
||||
- name: cloud
|
||||
type: llm
|
||||
model: ...
|
||||
```
|
||||
|
||||
然后 Agent 自己选择:
|
||||
|
||||
```text
|
||||
Task
|
||||
↓
|
||||
Router Resolver
|
||||
↓
|
||||
rules?
|
||||
↓
|
||||
confidence insufficient
|
||||
↓
|
||||
small model?
|
||||
↓
|
||||
confidence insufficient
|
||||
↓
|
||||
Jev?
|
||||
↓
|
||||
confidence insufficient
|
||||
↓
|
||||
large LLM
|
||||
```
|
||||
|
||||
这就形成一个**分级决策系统**。
|
||||
|
||||
---
|
||||
|
||||
# 6. 最漂亮的地方:Router 也可以被编译
|
||||
|
||||
最终甚至可以:
|
||||
|
||||
```text
|
||||
第一次:
|
||||
|
||||
LLM
|
||||
↓
|
||||
决定调用 Skill A
|
||||
|
||||
第二次:
|
||||
|
||||
LLM
|
||||
↓
|
||||
决定调用 Skill A
|
||||
|
||||
第三次:
|
||||
|
||||
LLM
|
||||
↓
|
||||
决定调用 Skill A
|
||||
|
||||
Learning
|
||||
↓
|
||||
发现稳定模式
|
||||
|
||||
Compile
|
||||
↓
|
||||
router.mq.md
|
||||
```
|
||||
|
||||
以后:
|
||||
|
||||
```text
|
||||
Task
|
||||
↓
|
||||
router.mq.md
|
||||
↓
|
||||
skill A
|
||||
↓
|
||||
execute
|
||||
```
|
||||
|
||||
**0 LLM。**
|
||||
|
||||
于是整个 Agent 的学习就不只是:
|
||||
|
||||
```text
|
||||
学习如何解决任务
|
||||
```
|
||||
|
||||
而是同时学习:
|
||||
|
||||
```text
|
||||
1. 学习如何解决任务
|
||||
2. 学习什么时候使用什么技能
|
||||
3. 学习什么时候需要大模型
|
||||
```
|
||||
|
||||
这三个层次组合起来,我认为比单纯引入 Jev 更有意思。
|
||||
|
||||
---
|
||||
|
||||
# 7. 最终可以形成三级编译
|
||||
|
||||
我现在反而觉得你的 Agent Framework 可以把整个“学习”定义成三个阶段:
|
||||
|
||||
```text
|
||||
Experience
|
||||
↓
|
||||
Skill Compilation
|
||||
↓
|
||||
Policy Compilation
|
||||
↓
|
||||
Execution Compilation
|
||||
```
|
||||
|
||||
### 第一层:Skill Compilation
|
||||
|
||||
```text
|
||||
LLM experience
|
||||
↓
|
||||
skill.mq.md
|
||||
```
|
||||
|
||||
解决:
|
||||
|
||||
> **怎么做?**
|
||||
|
||||
### 第二层:Policy Compilation
|
||||
|
||||
```text
|
||||
many task → skill mappings
|
||||
↓
|
||||
policy.mq.md
|
||||
```
|
||||
|
||||
解决:
|
||||
|
||||
> **什么时候做什么?**
|
||||
|
||||
### 第三层:Execution Compilation
|
||||
|
||||
```text
|
||||
skill + policy
|
||||
↓
|
||||
pure Marqdo execution
|
||||
```
|
||||
|
||||
解决:
|
||||
|
||||
> **能不能完全不思考直接执行?**
|
||||
|
||||
这时候整个 Agent 就变成:
|
||||
|
||||
```text
|
||||
Large LLM
|
||||
│
|
||||
Exploration
|
||||
↓
|
||||
Experience
|
||||
↓
|
||||
┌───────────────┐
|
||||
│ Skill Compile │
|
||||
└───────┬───────┘
|
||||
↓
|
||||
Skill MQ
|
||||
│
|
||||
┌───────┴───────┐
|
||||
↓ ↓
|
||||
Policy Compile Direct Execute
|
||||
↓
|
||||
Router MQ
|
||||
│
|
||||
↓
|
||||
Skill Selection
|
||||
│
|
||||
↓
|
||||
Execute
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
# 8. 因此 Phase 4 我建议正式改名
|
||||
|
||||
不要叫:
|
||||
|
||||
> Jev Router
|
||||
|
||||
而叫:
|
||||
|
||||
> **Adaptive Policy / Routing Layer**
|
||||
|
||||
然后提供四种 backend:
|
||||
|
||||
```text
|
||||
router.backend = marqdo
|
||||
router.backend = small-llm
|
||||
router.backend = jev
|
||||
router.backend = llm
|
||||
```
|
||||
|
||||
甚至:
|
||||
|
||||
```text
|
||||
router.backend = auto
|
||||
```
|
||||
|
||||
让 Agent 根据可用环境自动选择。
|
||||
|
||||
---
|
||||
|
||||
## 9. 用户体验也会好很多
|
||||
|
||||
用户第一次安装 Marqdo:
|
||||
|
||||
```text
|
||||
marqdo agent run ...
|
||||
```
|
||||
|
||||
没有 Jev,也没有小模型。
|
||||
|
||||
完全没问题:
|
||||
|
||||
```text
|
||||
→ 使用配置的大模型
|
||||
```
|
||||
|
||||
用户后来配置:
|
||||
|
||||
```yaml
|
||||
router:
|
||||
backend: small-llm
|
||||
provider: openai-compatible
|
||||
model: qwen-0.5b
|
||||
```
|
||||
|
||||
Agent 就自动变成:
|
||||
|
||||
```text
|
||||
大模型:负责复杂推理 / 学习
|
||||
小模型:负责日常路由
|
||||
Marqdo:负责确定性执行
|
||||
```
|
||||
|
||||
如果用户安装 Jev:
|
||||
|
||||
```yaml
|
||||
router:
|
||||
backend: jev
|
||||
```
|
||||
|
||||
则:
|
||||
|
||||
```text
|
||||
Jev:负责路由
|
||||
大模型:负责复杂任务
|
||||
Marqdo:负责执行
|
||||
```
|
||||
|
||||
**用户完全不需要改变 Agent 的上层 API。**
|
||||
|
||||
---
|
||||
|
||||
我认为这个调整非常重要,因为它让 **Marqdo Agent 的核心创新从“依赖某个新模型”变成“模型无关的认知编译架构”**。
|
||||
|
||||
最终可以浓缩成一句话:
|
||||
|
||||
> **大模型负责探索,小模型/Jev 负责决策,Marqdo 负责执行;随着经验积累,探索和决策逐渐被编译成可执行的 `.mq.md`。**
|
||||
|
||||
这其实比“接入 Jev”本身更符合你最开始提出的 **Think → Execute → Learn → Compile → Reuse** 路线。
|
||||
+1173
File diff suppressed because it is too large.
Load diff
+1335
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,77 @@
|
||||
# Marqdo AI 栈统一开发方案(llm + agent)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| Status | **Active** |
|
||||
| Date | 2026-09-24 |
|
||||
| Source | [doc/next/002–005](../next/) · [ext-llm.md](../design/ext-llm.md) · [ext-agent.md](../design/ext-agent.md) |
|
||||
|
||||
## 1. 一句话
|
||||
|
||||
> **Document → Prompt → LLM → Agent → Skill → Policy → Executable Knowledge**
|
||||
|
||||
大模型负责探索,小模型(`router_model` / 任意 llm 句柄)负责决策,Marqdo 负责执行;经验编译进 `.mq.md` 后推理成本下降。
|
||||
|
||||
## 2. 分层(勿混)
|
||||
|
||||
| 层 | 职责 | 落点 |
|
||||
|----|------|------|
|
||||
| **Intelligence primitive** | `ask` / `stream` / `collect`;结果对象 | `ext/ai/llm` |
|
||||
| **Agent runtime** | `run` / `step` / `plan` / `route` | `ext/ai/agent` |
|
||||
| **Memory / compile** | episode → skill → policy | `plugins/agent`(ABI;**不进** `src/host`) |
|
||||
| **Transport** | HTTP / SSE / provider | `ext/ai/llm/openai` · `ollama`;用户不可见 |
|
||||
|
||||
硬规则:`ext/**` 不调 `host_*`;agent/OKF/路由热路径只进 `plugins/agent`。
|
||||
|
||||
## 3. LLM(005)— 已/待
|
||||
|
||||
| 项 | 状态 |
|
||||
|----|------|
|
||||
| 语义 API:`ask` / `stream` / `collect`(`complete` 降为兼容原语) | **done** |
|
||||
| `create` 工厂;handle 带 `backend` | **done** |
|
||||
| LLMResult:`text` / `model` / `finish` / `backend` / `usage` / `tokens` | **done** |
|
||||
| Named models(`llm.fast` / `llm.reasoning`) | **done** |
|
||||
| Prompt document:`prompt` / `prompt_load` + `type: prompt` 正文 | **done**(无参 `ask`/`stream` → `sys.module_source` + `prompt_body`) |
|
||||
| 拆 `ext/ai/llm/openai.mq.md` + `ollama` 后端 | **done** |
|
||||
| tool_calls 完整 Result | **done**(`openai.tool_calls_from` + `LLMResult.tool_calls`;`llm-tool-calls-offline`) |
|
||||
| Transport → 可选 ABI 插件 (`plugins/llm`) | **done** |
|
||||
| Artifact Metadata Binding(`${env.*}` / `secret` · `sys.meta`) | **done**(Phase 1 Metadata-only + Phase 2 scope lift;[binding.md](../design/binding.md)) |
|
||||
|
||||
作者面优先:
|
||||
|
||||
```marqdo
|
||||
**answer = > llm.ask prompt="What is Marqdo?"**
|
||||
**events = > model.stream prompt=`p`**
|
||||
**text = > llm.collect events=`events`**
|
||||
**fast = > llm.fast**
|
||||
**prompt = > llm.prompt path="prompts/task.md"**
|
||||
```
|
||||
|
||||
## 4. Agent(002–004)— 已/待
|
||||
|
||||
| 项 | 状态 |
|
||||
|----|------|
|
||||
| Skill Compilation:episode → maybe_learn → `llm_free` | **done** |
|
||||
| Adaptive Routing:`marqdo` \| `small-llm` \| `llm` \| `auto`(`jev`→`small-llm` 别名) | **done** |
|
||||
| Policy Compilation → `policies/router.mq.md` | **done** |
|
||||
| `run` 串联 route → execute → record → learn | **done** |
|
||||
| Agent 默认走 `model.ask` + Result.usage 记成本 | **done**(`model_turn`) |
|
||||
| Prompt artifact / document-as-prompt | **done**(经 llm `path=` / `prompt`) |
|
||||
| 零 LLM 执行(policy+skill → `llm_calls=0`) | **done** |
|
||||
| `plan` 不覆盖已有 `llm_free` resource | **done** |
|
||||
| 真 MCP client(stdio) / resume checkpoint / 小模型路由请求面 | **done**(live 路由仍可选 skip) |
|
||||
|
||||
## 5. 联调顺序
|
||||
|
||||
1. **LLM Result → Agent metrics** — **done**
|
||||
2. **Named handles** — **done**
|
||||
3. **Prompt document** — **done**(`type: prompt` 剥 frontmatter;无参 `ask`/`stream` 经 `sys.module_source`)
|
||||
4. **Backend split** — **done**
|
||||
5. **Execution compilation** — **done**(`agent-zero-llm` 金样 + harness)
|
||||
6. **Metadata Binding** — **done**(Phase 1 + Phase 2 scope lift;`llm-meta-offline` · `meta-lift`)
|
||||
|
||||
## 6. 验收金样
|
||||
|
||||
- Agent:`tests/ext/agent-v2-learn.mq.md` · `agent-p4-route.mq.md` · `agent-zero-llm.mq.md` · `agent-plan-preserve.mq.md` · `agent-mcp-client.mq.md` · `agent-resume.mq.md` · `agent-small-llm-route.mq.md` · `scripts/agent-harness.sh`
|
||||
- LLM:`tests/ext/llm-ask-offline.mq.md` · `llm-stream-offline.mq.md` · `llm-ctor-offline.mq.md` · `llm-named-offline.mq.md` · `llm-meta-offline.mq.md` · `llm-entry-prompt-offline.mq.md` · `llm-tool-calls-offline.mq.md`
|
||||
- Binding:`tests/structure/meta-binding.mq.md` · `meta-lift.mq.md`
|
||||
@@ -0,0 +1,34 @@
|
||||
# 盲测实验(§5.2(b) 查询式 vs 全文档背诵)—— 2026-09-23
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 模型 | `deepseek-flash` @ `https://llm.cflmy.cn/v1` |
|
||||
| 样本 | 6 任务(业务 3 + QuixBugs 改写 3),oracle = stdout 精确匹配 |
|
||||
| 协议 | 系统提示词 = 硬规则 12 条 + 契约表三形状(两臂同文);**唯一变量 = 语法来源**:`query` 臂 MLSP 查询通道(≤3 次)vs `docs` 臂完整 markup 参考(背诵) |
|
||||
| 修复 | validate 诊断 + stderr 回喂,≤2 次修复;temperature=0,max_tokens=8000 |
|
||||
|
||||
## 汇总(判据:Q 通过率不劣于 D;首轮语法错误率 Q≤D;token Q≤70%%D;轮次不升)
|
||||
|
||||
| 臂 | 通过(一轮) | 通过(最终≤3) | 首轮语法错误 | 首轮契约诊断 | 平均修复轮(过例) | token 合计(prompt+completion) | 弃权/基础设施 |
|
||||
|---|---|---|---|---|---|---|---|
|
||||
| query | 0/3 | 0/3 | 2/3 | 1/3 | 0.00 | 69771(42478+27293) | 3 |
|
||||
| docs | 3/6 | 4/6 | 2/6 | 0/6 | 0.50 | 57276(33346+23930) | 0 |
|
||||
|
||||
## 逐例
|
||||
|
||||
| 例 | 臂 | 结果 | 修复轮 | 首轮语法错 | 首轮契约诊断 | token(p+c) | 查询 | 备注 |
|
||||
|---|---|---|---|---|---|---|---|---|
|
||||
| claims_reserve_settle | query | load-fail | 0 | 是 | 否 | 11987+8177 | 3 | {"error":{"code":"mlsp.parse_error","message":"<memory>: trailing input in expression: \"(claim - deductible, 0)\""},"id":1,"ok":false} \| stderr=error: /tmp/marqdo_blind_1694593_query_claims_reserve_settle.mq.md: trailing input in expression: "(claim - deductible, 0)" |
|
||||
| contract_milestone_pay | query | infra | 0 | 否 | 否 | 3970+4718 | 3 | 非 JSON / 空体: |
|
||||
| vendor_po_gate | query | load-fail | 0 | 是 | 否 | 9552+3686 | 3 | 回复中无程序 \| 开头: QUERY: 条件分支 写法 |
|
||||
| qb_gcd | query | run-fail | 0 | 否 | 是 | 9700+8488 | 3 | \| stderr=error: /tmp/marqdo_blind_1694593_query_qb_gcd.mq.md:26:1: unknown function `gcd(48` |
|
||||
| qb_max_sublist_sum | query | infra | 0 | 否 | 否 | 1253+134 | 3 | 非 JSON / 空体: |
|
||||
| qb_get_factors | query | infra | 0 | 否 | 否 | 6016+2090 | 3 | 非 JSON / 空体: |
|
||||
| claims_reserve_settle | docs | pass | 0 | 否 | 否 | 2699+2987 | 0 | queries=0 |
|
||||
| contract_milestone_pay | docs | pass | 0 | 否 | 否 | 2800+3202 | 0 | queries=0 |
|
||||
| vendor_po_gate | docs | pass | 0 | 否 | 否 | 2690+1264 | 0 | queries=0 |
|
||||
| qb_gcd | docs | pass | 2 | 否 | 否 | 8147+6970 | 0 | queries=0 |
|
||||
| qb_max_sublist_sum | docs | run-fail | 0 | 是 | 否 | 8518+3609 | 0 | \| stderr=error: /tmp/marqdo_blind_1694593_docs_qb_max_sublist_sum.mq.md:3:1: unknown function `[-2,` |
|
||||
| qb_get_factors | docs | run-fail | 0 | 是 | 否 | 8492+5898 | 0 | \| stderr=error: /tmp/marqdo_blind_1694593_docs_qb_get_factors.mq.md:20:1: missing argument for parameter `value` |
|
||||
|
||||
报告由 `tests/blind_experiment.rs` 生成;材料冻结于 `tests/blind/`(任务书 + docs-bundle),判据见 `doc/roadmap/perf-validation.md` §2。
|
||||
@@ -0,0 +1,34 @@
|
||||
# 盲测实验(§5.2(b) 查询式 vs 全文档背诵)—— 2026-09-23
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 模型 | `mimo-v2.6-pro` @ `https://llm.cflmy.cn/v1` |
|
||||
| 样本 | 6 任务(业务 3 + QuixBugs 改写 3),oracle = stdout 精确匹配 |
|
||||
| 协议 | 系统提示词 = 硬规则 12 条 + 契约表三形状(两臂同文);**唯一变量 = 语法来源**:`query` 臂 MLSP 查询通道(≤3 次)vs `docs` 臂完整 markup 参考(背诵) |
|
||||
| 修复 | validate 诊断 + stderr 回喂,≤2 次修复;temperature=0,max_tokens=8000 |
|
||||
|
||||
## 汇总(判据:Q 通过率不劣于 D;首轮语法错误率 Q≤D;token Q≤70%%D;轮次不升)
|
||||
|
||||
| 臂 | 通过(一轮) | 通过(最终≤3) | 首轮语法错误 | 首轮契约诊断 | 平均修复轮(过例) | token 合计(prompt+completion) | 弃权/基础设施 |
|
||||
|---|---|---|---|---|---|---|---|
|
||||
| query | 0/1 | 0/1 | 1/1 | 0/1 | 0.00 | 12428(10255+2173) | 5 |
|
||||
| docs | 0/0 | 0/0 | 0/0 | 0/0 | 0.00 | 0(0+0) | 6 |
|
||||
|
||||
## 逐例
|
||||
|
||||
| 例 | 臂 | 结果 | 修复轮 | 首轮语法错 | 首轮契约诊断 | token(p+c) | 查询 | 备注 |
|
||||
|---|---|---|---|---|---|---|---|---|
|
||||
| claims_reserve_settle | query | infra | 0 | 否 | 否 | 1424+324 | 3 | 非 JSON / 空体: |
|
||||
| contract_milestone_pay | query | infra | 0 | 否 | 否 | 0+0 | 0 | 非 JSON / 空体: |
|
||||
| vendor_po_gate | query | infra | 0 | 否 | 否 | 1417+213 | 3 | 非 JSON / 空体: |
|
||||
| qb_gcd | query | load-fail | 0 | 是 | 否 | 5011+1265 | 0 | {"error":{"code":"mlsp.parse_error","message":"<memory>: unexpected expression input: \"**y**\""},"id":1,"ok":false} \| stderr=error: /tmp/marqdo_blind_1710203_query_qb_gcd.mq.md: unexpected expression input: "**y**" |
|
||||
| qb_max_sublist_sum | query | infra | 0 | 否 | 否 | 1216+172 | 3 | 非 JSON / 空体: |
|
||||
| qb_get_factors | query | infra | 0 | 否 | 否 | 1187+199 | 3 | 非 JSON / 空体: |
|
||||
| claims_reserve_settle | docs | infra | 0 | 否 | 否 | 0+0 | 0 | 非 JSON / 空体: |
|
||||
| contract_milestone_pay | docs | infra | 0 | 否 | 否 | 0+0 | 0 | 非 JSON / 空体: |
|
||||
| vendor_po_gate | docs | infra | 0 | 否 | 否 | 0+0 | 0 | 非 JSON / 空体: |
|
||||
| qb_gcd | docs | infra | 0 | 否 | 否 | 0+0 | 0 | 非 JSON / 空体: |
|
||||
| qb_max_sublist_sum | docs | infra | 0 | 否 | 否 | 0+0 | 0 | 非 JSON / 空体: |
|
||||
| qb_get_factors | docs | infra | 0 | 否 | 否 | 0+0 | 0 | 非 JSON / 空体: {"error":{"message":"upstream connection failed","type":"server_error","param":n… |
|
||||
|
||||
报告由 `tests/blind_experiment.rs` 生成;材料冻结于 `tests/blind/`(任务书 + docs-bundle),判据见 `doc/roadmap/perf-validation.md` §2。
|
||||
@@ -39,6 +39,12 @@
|
||||
| **B0** | 官方示例演示 call_site/源码/OKF;Skill/文档;上下文管道可读 | **done** |
|
||||
| **B0.5** | 本机 live(`.env` → 开发模型)验证「模型用上了文档」 | **done**(`AGENT_LIVE` / `AGENT_HARNESS_LIVE`) |
|
||||
| **B1** | 评测 harness 锁差异化指标(constitution / dump / writeback 遮蔽) | **Active** |
|
||||
| **B2–B5** | 真 MCP / resume+HITL / stats / 并行 | 暂缓 |
|
||||
| **B1.5** | **Skill Compilation v2(Phase 1–3)**:episode → maybe_learn → compile → `llm_free`;`agent.run` / `resolve` / `metrics`;ABI 在 `plugins/agent`(`memory.rs`);金样 `agent-v2-learn` | **done** |
|
||||
| **B1.6 / P4** | **Adaptive Routing**(原 Jev Router 更名):`route` backends = `marqdo` \| `small-llm` \| `jev?` \| `llm` \| `auto`;Policy Compilation → `policies/router.mq.md`;金样 `agent-p4-route`;设计 [004.md](../next/004.md) | **done** |
|
||||
| **L0** | **LLM semantic surface(005)**:`ask` / `stream` / `collect` / `create`;`complete` 降为兼容;统一方案 [ai-stack.md](ai-stack.md) | **done** |
|
||||
| **B2** | 真 MCP stdio client(`agent_mcp_client` + fixture 仍作离线证据) | **done** |
|
||||
| **B3** | resume checkpoint(`.marqdo/agent-resume/`;HITL view 仍暂缓) | **done**(HITL 暂缓) |
|
||||
| **B4–B5** | stats / 并行;小模型 live 路由金样(无 key skip) | 部分 |
|
||||
| — | LLM named models / prompt-document | **done** |
|
||||
|
||||
示例:`examples/agent-pong/` · `examples/agent-okf-flywheel/`。
|
||||
示例:`examples/agent-pong/` · `examples/agent-okf-flywheel/`。v2 设计:[doc/next/002.md](../next/002.md) · [doc/next/003.md](../next/003.md) · P4:[doc/next/004.md](../next/004.md) · LLM:[doc/next/005.md](../next/005.md)。
|
||||
@@ -0,0 +1,118 @@
|
||||
# P4 验证汇总报告 — 2026-09-23
|
||||
|
||||
> 执行 [perf-validation.md](perf-validation.md)(自设计性能验证协议)的结果汇总。
|
||||
> 覆盖 three-problems.md 三大问题的**验证矩阵**(§5.2 a–e)。逐例明细见各实验原始报告(文末索引)。
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 协议 | [perf-validation.md](perf-validation.md)(实验 (a)–(e)) |
|
||||
| 判据源 | [three-problems.md](three-problems.md) §3.7 / 各问题 §2.4 |
|
||||
| 构建 | marqdo v1.0.7(本仓库工作树;金 20 + 全量回归通过后采样) |
|
||||
| 端点 | `llm.cflmy.cn/v1`(OpenAI 兼容;temperature=0) |
|
||||
| 模型 | 修复实验:`cflmy`、`mimo-v2.6-flash`;盲测:`deepseek-flash`、`mimo-v2.6-pro`(按用户指定) |
|
||||
| 回归 | 全量干净回归(无 API key 基线)**零失败** |
|
||||
|
||||
## 1. 五实验结果与判据对照
|
||||
|
||||
| 实验 | 判据 | 结果 | 判定 |
|
||||
|---|---|---|---|
|
||||
| **(a) 锚点消融**(T3.3,双模型×20例×双臂,三轮) | anchor 臂一轮修复率 ≥ 80%;护栏 0 次越界应用 | cflmy anchor **20/20=100%**(plain 17/20=85%);mimo-v2.6-flash anchor **20/20=100%**(plain 18/20=90%;容错加固中的第二轮 anchor 18/20=90% 亦 ≥80%);越界拒/越界应用 0/0 | ✅ 过 |
|
||||
| **(b) 盲测**(查询式 vs 全文档背诵,6任务×2臂) | 四判据:最终通过率 Q 不劣于 D(允许 −0 例);首轮语法错误率 Q ≤ D;平均修复轮次 Q ≤ D;token ≤ 70% of D(首轮契约诊断率为观测项) | deepseek-flash:docs 4/6 vs query 0/3(3 例端点 infra 剔除);首轮语法错 2/3 vs 2/6;token 69,771 vs 57,276。mimo-v2.6-pro:两跑均端点 infra(12/12、11/12),**不可评估** | ❌ 四判据未全立 ⇒ 按 §3.7 继续改协议 |
|
||||
| **(c) 契约消融**(四边界×三情形) | 带契约错值拦截 = 100%;带契约对值误报 = 0%;无契约路径零回归 | 拦截 4/4=**100%**;误报 0/4=**0%**;无契约 4/4 动态语义保持(含 `missing map key` 基线) | ✅ 过 |
|
||||
| **(d) 防漂移演练**(9 漂移 + 2 负对照) | 故意写错必报 = 100%;对照组误报 = 0% | 必报 9/9=**100%**(`unknown_type`×2、`param_drift`×2、`return_mismatch`、`arg_mismatch`、`unknown_key`、`contract.duplicate`、`contract.unknown_table`);负对照误报 0/2 | ✅ 过 |
|
||||
| **(e) 核心守卫演练**(3 种变异注入) | 单边改动必红;还原必绿 | 3/3 必红(`CORE_CONSTRUCTS` 私增、`core-surface.md` 私增、金样删除);还原全绿 | ✅ 过 |
|
||||
|
||||
**可复制结论(a/c/d/e 全过)**:锚点臂一轮修复率双模型 100%(判据 ≥80%,增量 +15pp/+10pp——易锚定样本近天花板,区分性留难样本扩展);契约系统"只在声明处拦截、零误报、不碰动态路径"成立;契约/实现互查防漂移 100% 必报;核心面单边膨胀被 CI 守卫机械阻断。
|
||||
|
||||
## 2. 实验 (c)(d)(e) 明细
|
||||
|
||||
### (c) 契约消融(tests/contract_ablation.rs,确定性)
|
||||
|
||||
四边界(调用实参 / 返回值 / 表绑定 / 键访问)各测三情形:
|
||||
|
||||
| 情形 | 口径 | 结果 |
|
||||
|---|---|---|
|
||||
| ① 带契约 + 错值 | 必须拦截(`contract.arg_mismatch` / `contract.return_mismatch` / `contract.field_mismatch` / `contract.unknown_key`) | 4/4 |
|
||||
| ② 带契约 + 对值 | 不得报契约错(同路径同形状) | 0/4 误报 |
|
||||
| ③ 无契约 + 错值 | 保持动态语义(不得出现 `contract.*`;走经典动态错误如 `missing map key`) | 4/4 |
|
||||
|
||||
取元一律用主推的 `[]()` 形(`[键](`集合`)`)验证;`集合[^键]` 过渡形保留兼容金样覆盖。
|
||||
|
||||
### (d) 防漂移演练(tests/drift_drill.rs,确定性)
|
||||
|
||||
9 个"故意写错"全部必报:类型词拼错、参数名漂移、升参未覆盖、返回越约、实参越约、未知键、重复契约表、错列表头契约表、运行时值越约。负对照 2 例(被绑定的数据表、纯动态程序)零误报。同时验证"契约表一经出现即为完整声明"(新升参未覆盖 ⇒ `contract.param_drift` warning)被按设计触发。
|
||||
|
||||
### (e) 核心守卫演练(人工变异,tests/core_surface.rs 必红验证)
|
||||
|
||||
| 变异 | 注入点 | 结果 |
|
||||
|---|---|---|
|
||||
| 私增 `ghost-construct` | `src/parse/mod.rs` `CORE_CONSTRUCTS` | 🔴 必红 |
|
||||
| 私增 `ghost-construct` 行 | `doc/design/core-surface.md` | 🔴 必红 |
|
||||
| 删除金样文件 | `tests/markup-v03/soft-emphasis.mq.md` | 🔴 必红 |
|
||||
|
||||
三次还原后 🟢 全绿。CI 守卫强制"清单与文档双改 + 全部金样存在"。
|
||||
|
||||
## 3. 实验 (b) 盲测:详况与诚实结论
|
||||
|
||||
### 协议
|
||||
|
||||
两臂系统提示词同文 = 硬规则 12 条 + 契约表三形状;**唯一变量 = 语法知识获取**:
|
||||
|
||||
- `query` 臂:MLSP 查询通道(`QUERY: <关键字>` ⇒ `mlsp syntax` 卡片,含戒律原文/样例/文档锚点;≤3 次)
|
||||
- `docs` 臂:完整 markup 参考背诵(`tests/blind/docs-bundle.md`,背诵式全语法表 + 中文内建面)
|
||||
|
||||
任务 6 例(`tests/blind/tasks/`):业务 3(保险理赔准备金 / 工程进度款 / 采购付款门槛,均出自律所实测任务集)+ QuixBugs 改写 3(gcd / 最大子段和 / 质因数分解);oracle = stdout 精确匹配(任务书自带期望输出,把推理难度控制掉,失败归因于**语言能力**)。修复回路 ≤2 次(validate 结构化诊断 + stderr 回喂)。
|
||||
|
||||
### deepseek-flash(完成)
|
||||
|
||||
| 臂 | 通过(一轮) | 通过(最终) | 首轮语法错 | 平均修复轮(过例) | token(p+c) | infra 剔除 |
|
||||
|---|---|---|---|---|---|---|
|
||||
| query | 0/3 | 0/3 | 2/3 | – | 69,771(42,478+27,293) | 3/6 |
|
||||
| docs | 3/6 | **4/6** | 2/6 | 0.50 | 57,276(33,346+23,930) | 0/6 |
|
||||
|
||||
- **docs 臂**:业务题 3/3 全部一轮通过(各 ~3k token);`qb_gcd` 修复 2 轮过;`qb_max_sublist_sum`、`qb_get_factors` 暴露真语法滑倒(列表字面量被当调用 `unknown function [-2,`;参数名漂移 `missing argument for parameter value`)。
|
||||
- **query 臂**:查询通道**被积极使用**(5/6 例用满 3 次,多词查询如 `条件分支 写法` 也能正确命中卡片——已单独验证查询匹配无缺陷);但 3 例被端点空体吃掉,其余 3 例败于真语法错(Python 式元组 `(a, b)` ⇒ `trailing input in expression`;Python 式调用 `gcd(48, 18)` ⇒ `unknown function gcd(48`)与 1 例查询后未交程序。
|
||||
|
||||
### mimo-v2.6-pro(两跑均不可评估)
|
||||
|
||||
首跑 **12/12** 全部"非 JSON / 空体"(与 deepseek 并行时段)。三向探针(小提示词×12000/4000、大提示词×12000)证实**网关随机空体**、与 `max_tokens`/提示词体量无稳定关系。加重试(8→12 次、退避 10→15 秒)并**单独**重跑后仍为 **11/12** 端点 infra(含两臂首聊即 `tok=(0,0)` 的空体);唯一执行到的 `qb_gcd` 例暴露真语法错(`**y**` 作语句 ⇒ `unexpected expression input`)。**结论:`mimo-v2.6-pro` 经本网关不可评估**,(b) 判据以 `deepseek-flash` 数据为准。原始报告:[blind-experiment-2026-09-23-mimo-v2.6-pro.md](blind-experiment-2026-09-23-mimo-v2.6-pro.md)。
|
||||
|
||||
### 判据对照(截至 deepseek-flash)
|
||||
|
||||
| 判据(协议 §2.4 原文口径) | 目标 | 实测(deepseek-flash,非 infra 口径) | 判定 |
|
||||
|---|---|---|---|
|
||||
| 最终通过率(≤3 轮)Q 不劣于 D(允许 −0 例) | ≥ D | 0/3 vs 4/6 | ❌ |
|
||||
| 首轮语法错误率 Q ≤ D | ≤ | 2/3 vs 2/6 | ❌ |
|
||||
| 平均修复轮次 Q ≤ D | ≤ | query 臂无通过例,不可比 | ⚠️ |
|
||||
| token 用量 Q ≤ 70% of D | ≤ 40,093 | 69,771(D 的 121.8%) | ❌ |
|
||||
| (观测项)首轮契约诊断率 Q ≤ D | – | 1/3 vs 0/6 | ➖ 观测 |
|
||||
|
||||
**诚实结论:在 deepseek-flash 上,"查询式"没有跑赢"全背诵"**——三项可判量化判据未达,按协议 §3.7 判定:**四判据未全成立 ⇒「AI 不必学语法」本轮未获实证,继续改协议**(不是证伪——infra 吞掉 query 臂一半样本,非 infra n=3)。要点:
|
||||
|
||||
1. **统计力被端点削弱**:query 臂 6 例中 3 例被空体吃掉(协议无从执行),非 infra 样本 n=3;mimo-v2.6-pro 整组不可用。判据是"有方向的强声明",当前证据**不足以支持也未证伪**其普适性,只能定格为"deepseek-flash × 本任务集 × 本协议版本"下的一次未达标观测。
|
||||
2. **失败模式是真发现**:查询臂的败例是**跨语言习惯泄漏**(元组表达式、Python 调用形)——查询卡片能回答"条件分支怎么写",但模型不查询"这里能不能用元组"。说明"AI 不必学语法"的瓶颈不在**查询机制**,而在**模型的自查纪律**(不知道自己不知道)。
|
||||
3. **token 判据方向存疑**:查询式省的是**系统提示词常驻**(背诵包),花的是**多轮往返**。6 例短程序任务里往返开销 > 常驻节省;该判据更适用于**长会话多文件**场景(一次背诵包 × N 轮 vs 每轮均摊查询),本轮任务形态低估了查询式的收益面。
|
||||
4. **docs 臂的 4/6 说明任务集有区分度**(不是全过/全不过的退化测量)。
|
||||
|
||||
## 4. 测量端基础设施记录(方法学附注)
|
||||
|
||||
- **LLM 端点不稳定**是本轮最大的干扰源:随机空体、reasoning 模型 `finish=length` 截断(思考吃满额度、正文为空)。对策:空体重试 5→12 次 + 退避 5→15 秒、截断自适应(`max_tokens` 加倍重试,封顶 32k)、infra 例从判据比率中剔除并单列。
|
||||
- **harness 缺陷两处**(已修,未污染最终数据):查询额度用满后的续篇被当最终回复(⇒ 假"无程序");程序提取失败不进修复回路(⇒ 少给模型一次标准修复机会)。修复后的协议对两臂完全一致。
|
||||
- **模型口径变化留痕**:原定 `cflmy`/`mimo-v2.6-flash` 组合按用户指示弃用,改 `mimo-v2.6-pro`/`deepseek-flash`;修复实验 (a) 的双模型数据仍是原组合(金 20 协议已完成,不重跑)。
|
||||
|
||||
## 5. 材料与复现
|
||||
|
||||
```bash
|
||||
# (a) 锚点消融(需要 API key)
|
||||
cargo test --test repair_experiment -- --ignored --nocapture # anchor/plain 双臂×20例
|
||||
# (b) 盲测(每模型一次;建议单独跑避免并发)
|
||||
OPENAI_MODEL=deepseek-flash MARQDO_EXP_DATE=2026-09-23 cargo test --test blind_experiment -- --ignored --nocapture
|
||||
# (c)(d) 确定性
|
||||
cargo test --test contract_ablation --test drift_drill
|
||||
# (e) 核心守卫(变异注入→必红→还原必绿,按 perf-validation.md §5 手法)
|
||||
cargo test --test core_surface
|
||||
```
|
||||
|
||||
- 任务书 / 背诵包(冻结,勿改):`tests/blind/`
|
||||
- 原始报告:[blind-experiment-2026-09-23-deepseek-flash.md](blind-experiment-2026-09-23-deepseek-flash.md) · [blind-experiment-2026-09-23-mimo-v2.6-pro.md](blind-experiment-2026-09-23-mimo-v2.6-pro.md)(首跑全 infra)· [repair-experiment-2026-09-22.md](repair-experiment-2026-09-22.md) · [repair-experiment-2026-09-22-mimo-v2.6-flash.md](repair-experiment-2026-09-22-mimo-v2.6-flash.md)
|
||||
- 判据协议:[perf-validation.md](perf-validation.md)(版本化于本仓库,实验前冻结)
|
||||
@@ -0,0 +1,125 @@
|
||||
# P4 性能验证设计(实验协议与判据)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **Accepted · P4 执行依据** |
|
||||
| 日期 | 2026-09-23 |
|
||||
| 授权 | 用户授权**自行设计**本性能验证设计(P4 不再等待外部文档导入;`doc/next/002.md` 与性能验证无关) |
|
||||
| 上位判据 | [three-problems.md](three-problems.md) §5.1 判据矩阵 + §5.2 实验 (a)–(e);「判据矩阵全绿 + 实验数字可复现之前,三大问题一律视为未解决」 |
|
||||
| 工程任务 | [three-problems-plan.md](three-problems-plan.md) Phase 1–3(已完成);本文只负责**验证它们** |
|
||||
|
||||
> **本文定位**:把 three-problems.md §5.2 的五个实验操作化——每个实验给出**材料、协议、指标、判据**,全部机器可执行、数字可复现。实验产物 = 报告 [perf-validation-report-2026-09-23.md](perf-validation-report-2026-09-23.md)。**只认行为与数字,不认宣言。**
|
||||
|
||||
## 0. 总览
|
||||
|
||||
| 实验 | 验证对象 | 判据(全过 = 该问题的实证成立) | 执行方式 | 状态 |
|
||||
|------|----------|-------------------------------|----------|------|
|
||||
| (a) 锚点修复消融 | 问题三 · 可靠反馈 | 一轮修复率 ≥ 80%(anchor 臂),护栏 0 越界 | 模型在环 `tests/repair_experiment.rs` | ✅ 已完成(三轮双模型) |
|
||||
| (b) 盲测 | 问题三 · 不必学语法 | 查询组错误率 ≤ 全文档组;token 显著更少;修复轮次不升 | 模型在环 `tests/blind_experiment.rs` | 见报告 |
|
||||
| (c) 契约消融 | 问题二 · 语义校验 | 拦截率 = 100%;误报率 = 0%;无契约路径零回归 | 确定性 `tests/contract_ablation.rs` | 见报告 |
|
||||
| (d) 防漂移演练 | 问题二 · 契约可信 | 故意写错契约必报率 = 100% | 确定性 `tests/drift_drill.rs` | 见报告 |
|
||||
| (e) 核心守卫演练 | 问题一 · 核心要小 | 三种变异注入各必红 → 还原必绿 | 变异脚本(见 §4) | 见报告 |
|
||||
|
||||
判定口径([three-problems.md](three-problems.md) §5.1):**任何一项判据不成立,对应问题即「未解决」**——不许用文档宣言充数。
|
||||
|
||||
---
|
||||
|
||||
## 1. 实验 (a):锚点修复消融(问题三 · 可靠反馈)
|
||||
|
||||
**假设**:诊断带 `doc_anchor`/`doc_quote`/`suggestion`(锚点诊断)比裸诊断显著提高一轮修复率。
|
||||
|
||||
- **对照**:`anchor` 臂(全量诊断字段)vs `plain` 臂(剥掉 doc_anchor/doc_quote/suggestion)。
|
||||
- **材料**:golden 20 例(10 骨架 × 2 变体:arg/ret/field/key/typo/drift/dup),易锚定错误。
|
||||
- **协议**:`validate` → `repair_targets` → 模型行编辑 → `repair_apply`(越界必拒/abstain)→ 复验;≤2 轮。
|
||||
- **指标**:一轮修复率、最终修复率、平均尝试、越界拒、弃权。
|
||||
- **判据**:anchor 臂一轮修复率 ≥ 80%;护栏 0 次越界应用(越界必拒由 `tests/repair_loop.rs` 硬断言兜底)。
|
||||
- **执行**:`OPENAI_MODEL=… cargo test --test repair_experiment -- --ignored --nocapture`。
|
||||
- **结果**:三轮双模型全过(cflmy anchor 100% / mimo 90%→100%,plain 85%–100%,护栏 0 越界)——
|
||||
详见 [repair-experiment-2026-09-22.md](repair-experiment-2026-09-22.md) 与
|
||||
[repair-experiment-2026-09-22-mimo-v2.6-flash.md](repair-experiment-2026-09-22-mimo-v2.6-flash.md)。**✅ PASS**。
|
||||
- **诚实注记**:样本为易锚定错误(两臂近天花板),锚点增量的区分性证据留待难样本扩展;不影响本判据达标。
|
||||
|
||||
## 2. 实验 (b):盲测——查询式 vs 全文档背诵(问题三 · 超级核心)
|
||||
|
||||
**假设**:「零语法文档 + MLSP 查询」组(Q)成绩**不劣于**「全文档背诵」组(D),且 prompt token 显著更少、修复轮次不升([three-problems.md](three-problems.md) §3.7 最终判据)。
|
||||
|
||||
### 2.1 对照(唯一变量 = 语法知识的获取方式)
|
||||
|
||||
| 臂 | 系统提示词内容 | 语法知识来源 |
|
||||
|----|----------------|--------------|
|
||||
| **Q 查询组** | 硬规则(12 条)+ 契约表三形状 + MLSP 查询通道说明 | 按需 `QUERY: <关键字>`(≤3 次,harness 代答 `mlsp syntax` 卡片) |
|
||||
| **D 全文档组** | 硬规则(12 条)+ 契约表三形状 + **完整 markup 参考**(`tests/blind/docs-bundle.md`:v0.3 全语法表 + 中文内建面) | 上下文预载(背诵) |
|
||||
|
||||
两臂共同点:硬规则与契约表形状**同文**(受控变量,不测契约知识差异);修复回路相同(`mlsp validate` 诊断回喂,≤2 轮);模型 / 温度 / 任务 / 提示词模板相同。
|
||||
|
||||
### 2.2 材料:任务集(6 例,业务 3 + 算法 3)
|
||||
|
||||
| id | 类型 | 任务 | 功能 oracle(stdout 精确匹配) |
|
||||
|----|------|------|-------------------------------|
|
||||
| `claims_reserve_settle` | 业务 | 保险准备金(免赔/共保/分项限额/追偿/地板) | `28000` |
|
||||
| `contract_milestone_pay` | 业务 | 工程里程碑应付款(保留金/逾期罚/变更/税/地板) | `37354` |
|
||||
| `vendor_po_gate` | 业务 | 采购 PO 准付闸(折扣/加急/预算帽/合规费/地板) | `814` |
|
||||
| `qb_gcd` | 算法 | 最大公约数;打印 gcd(48,18) gcd(1071,462) gcd(0,5) gcd(17,5) 各一行 | `6` `21` `5` `1` |
|
||||
| `qb_max_sublist_sum` | 算法 | 最大子段和;三组输入各打印一行 | `6` `6` `-1` |
|
||||
| `qb_get_factors` | 算法 | 质因数分解,因数以 `*` 连接;12 / 13 / 60 各一行 | `2*2*3` `13` `2*2*3*5` |
|
||||
|
||||
业务任务书复用 MarqdoThesis `formal/exp3-bizreq/data/tasks/`(权威规则文本原样进入提示词);算法任务书为 QuixBugs 改写(从「修 bug」改「实现 + 固定输出」,消除参考源码变量)。
|
||||
|
||||
### 2.3 协议(每例)
|
||||
|
||||
1. 提示词 = 任务书 + 「输出完整 `.mq.md` 源码于单个 ```markdown 围栏」;
|
||||
2. **查询阶段**(仅 Q 臂):模型可输出 `QUERY: <关键字>` 行,harness 以 `mlsp syntax` 卡片作答并继续;累计 ≤3 次;
|
||||
3. **执行阶段**:`marqdo run` → stdout == oracle 即通过;失败则回喂 `mlsp validate` 结构化诊断 + stderr,允许 ≤2 次修复;
|
||||
4. 记录每轮 token(API `usage.prompt_tokens + completion_tokens` 累计)。
|
||||
|
||||
### 2.4 指标与判据
|
||||
|
||||
| 指标 | 口径 | 判据 |
|
||||
|------|------|------|
|
||||
| 功能通过率(一轮 / 最终≤3 轮) | stdout 与 oracle 精确匹配 | Q 最终通过率**不劣于** D(允许 −0 例) |
|
||||
| 首轮语法错误率 | 首轮 `validate`/run 出现 parse 类失败的例数占比 | Q ≤ D |
|
||||
| 首轮契约诊断率 | 首轮出现 `contract.*` 诊断的例数占比 | Q ≤ D(观测项) |
|
||||
| 平均修复轮次 | 通过前的修复次数(含首轮生成记 0) | Q ≤ D |
|
||||
| token 用量 | 逐例累计 prompt+completion | Q **显著更少**(≤ 70% of D) |
|
||||
|
||||
**判定**:四条判据全成立 ⇒ 「AI 不必学语法」实证成立;否则按 §3.7「继续改协议」,不得以改进文档充数。
|
||||
- **执行**:`OPENAI_MODEL=… cargo test --test blind_experiment -- --ignored --nocapture`(每模型一轮)。
|
||||
|
||||
## 3. 实验 (c):契约消融(问题二 · 语义校验)
|
||||
|
||||
**假设**:契约只在**有契约处**拦截错误值(P1:无契约 = 全动态),且不误报。
|
||||
|
||||
- **材料**:`tests/contract_ablation.rs` 内联四边界用例(调用实参 / 返回 / 表绑定 / 字段访问)×三形态:
|
||||
① 带契约 + 错值 → **必拦**(`contract.*` 诊断);② 带契约 + 对值 → **零误报**(正常产出);③ 同程序**删契约表** + 错值 → **零契约诊断**(动态语义原样)。
|
||||
- **指标与判据**:
|
||||
- 拦截率 = ①被拦 / ①总数 = **100%**;
|
||||
- 误报率 = ②误报 / ②总数 = **0%**;
|
||||
- 无契约零回归 = ③零 `contract.*` 诊断且输出 = 动态语义基线;且全量 `cargo test` 绿(441+ 既有测试是无契约路径的主回归面)。
|
||||
- **执行**:`cargo test --test contract_ablation`。
|
||||
|
||||
## 4. 实验 (d):防漂移演练(问题二 · 契约可信)
|
||||
|
||||
**假设**:契约写错**必**被机器抓(不静默信任契约)——three-problems.md §2.8「故意写错契约的金样必红」。
|
||||
|
||||
- **材料**:`tests/drift_drill.rs`——故意写错契约的 N 例(类型 typo `nubmer`、未知对象类型、契约参数名 ≠ 升参名 ×2、返回契约明显不符、重复契约表、错位契约表)+ **负对照**(被绑定的 `字段|类型` 数据表 = web_db_migration 式,必须**不**被当契约吃掉)。
|
||||
- **指标与判据**:
|
||||
- 必报率 = 漂移例被诊断(`check` 报错/警告或运行时 `contract.*`)/ 漂移例总数 = **100%**;
|
||||
- 负对照误报 = **0**。
|
||||
- **执行**:`cargo test --test drift_drill`。
|
||||
|
||||
## 5. 实验 (e):核心守卫演练(问题一 · 核心要小)
|
||||
|
||||
**假设**:核心表面任何**单侧**变更(代码/文档/金样不同步)→ CI 必红——three-problems.md §1.5「新增标记不更新清单时 CI 必须红」。
|
||||
|
||||
- **材料**:`tests/core_surface.rs` 守卫 + 三种变异注入(脚本化,`git checkout` 还原):
|
||||
1. `src/parse/mod.rs` `CORE_CONSTRUCTS` 增一行、不改文档 → **必红**;
|
||||
2. `doc/design/core-surface.md` 表增一行、不改代码 → **必红**;
|
||||
3. 金样文件改名(路径失效)→ **必红**。
|
||||
- **指标与判据**:3/3 变异注入后守卫测试红;还原后全绿。**必红率 = 100%**。
|
||||
- **执行**:报告内嵌演练记录(命令 + 测试输出摘录)。
|
||||
|
||||
## 6. 报告与复现
|
||||
|
||||
- 报告:[perf-validation-report-2026-09-23.md](perf-validation-report-2026-09-23.md)((a) 引用既有两份实验报告;(b)–(e) 本次产出)。
|
||||
- 复现命令全部写入报告各节;模型在环实验记录模型名与端点,判据数字允许 ±1 例波动(模型非确定性),**判据线本身不放宽**。
|
||||
- 汇总结论回填 [three-problems-plan.md](three-problems-plan.md) 落地记录;§5.1 判据矩阵逐行标注 ✅/❌。
|
||||
@@ -0,0 +1,64 @@
|
||||
# 修复实验(§5.2(a) 锚点消融)—— 2026-09-22
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 模型 | `mimo-v2.6-flash` @ `https://llm.cflmy.cn/v1` |
|
||||
| 样本 | golden 20 例(易锚定错误) |
|
||||
| 协议 | `validate` → `repair_targets` → 模型行编辑 → `repair_apply`(越界必拒/abstain)→ 复验;≤2 轮 |
|
||||
| 对照 | `anchor` = 诊断带 `doc_anchor`/`doc_quote`/`suggestion`;`plain` = 全部剥掉 |
|
||||
|
||||
## 汇总(设计目标:一轮修复率 ≥ 80%)
|
||||
|
||||
| 臂 | 一轮修复率 | 最终修复率(≤2轮) | 平均尝试 | 越界拒(护栏) | 弃权 |
|
||||
|---|---|---|---|---|---|
|
||||
| anchor | 20/20(100%) | 20/20(100%) | 1.00 | 0 | 0 |
|
||||
| plain | 18/20(90%) | 19/20(95%) | 1.05 | 0 | 1 |
|
||||
|
||||
## 逐例
|
||||
|
||||
| 例 | 臂 | 结果 | 轮次 | 越界拒 | 备注 |
|
||||
|---|---|---|---|---|---|
|
||||
| arg-num-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| arg-num-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| arg-num-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| arg-num-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| arg-text-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| arg-text-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| arg-text-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| arg-text-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| drift-cover-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| drift-cover-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| drift-cover-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| drift-cover-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| drift-rename-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| drift-rename-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| drift-rename-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| drift-rename-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| dup-delete-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| dup-delete-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| dup-delete-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| dup-delete-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| field-nullable-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| field-nullable-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| field-nullable-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| field-nullable-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| field-type-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| field-type-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| field-type-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| field-type-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| key-fix-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| key-fix-a | plain | 未修复 | 1 | 0 | 模型弃权/输出不可解析: [{"op":"replace","line":13","text":"*`用户`[^name]*"}] |
|
||||
| key-fix-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| key-fix-b | plain | 二轮修复 | 2 | 0 | 修复后仍有诊断: {"code":"marqdo.error","contract_ref":null,"doc_anchor":null,"doc_quote":null,"message":"missing map key `phone`","severity":"error","span":{"col":1,"file":"/tmp/marqdo_repair_1666049_plain_key-fix-b.mq.md","line":14},"suggestion":null} |
|
||||
| ret-type-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| ret-type-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| ret-type-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| ret-type-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| type-typo-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| type-typo-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| type-typo-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| type-typo-b | plain | 一轮修复 | 1 | 0 | |
|
||||
|
||||
## 护栏核对
|
||||
|
||||
- 每次越界编辑都以 `mlsp.repair_out_of_scope` + `abstain` 被拒,且 `repair_apply` 全有或全无(源零字节不改)——由 `tests/repair_loop.rs` 的确定性断言覆盖;本实验计数仅作记录。
|
||||
@@ -0,0 +1,64 @@
|
||||
# 修复实验(§5.2(a) 锚点消融)—— 2026-09-22
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 模型 | `cflmy` @ `https://llm.cflmy.cn/v1` |
|
||||
| 样本 | golden 20 例(易锚定错误) |
|
||||
| 协议 | `validate` → `repair_targets` → 模型行编辑 → `repair_apply`(越界必拒/abstain)→ 复验;≤2 轮 |
|
||||
| 对照 | `anchor` = 诊断带 `doc_anchor`/`doc_quote`/`suggestion`;`plain` = 全部剥掉 |
|
||||
|
||||
## 汇总(设计目标:一轮修复率 ≥ 80%)
|
||||
|
||||
| 臂 | 一轮修复率 | 最终修复率(≤2轮) | 平均尝试 | 越界拒(护栏) | 弃权 |
|
||||
|---|---|---|---|---|---|
|
||||
| anchor | 20/20(100%) | 20/20(100%) | 1.00 | 0 | 0 |
|
||||
| plain | 17/20(85%) | 17/20(85%) | 0.85 | 0 | 0 |
|
||||
|
||||
## 逐例
|
||||
|
||||
| 例 | 臂 | 结果 | 轮次 | 越界拒 | 备注 |
|
||||
|---|---|---|---|---|---|
|
||||
| arg-num-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| arg-num-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| arg-num-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| arg-num-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| arg-text-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| arg-text-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| arg-text-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| arg-text-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| drift-cover-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| drift-cover-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| drift-cover-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| drift-cover-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| drift-rename-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| drift-rename-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| drift-rename-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| drift-rename-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| dup-delete-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| dup-delete-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| dup-delete-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| dup-delete-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| field-nullable-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| field-nullable-a | plain | 未修复 | 0 | 0 | LLM 调用失败(重试后): 非 JSON / 空体: |
|
||||
| field-nullable-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| field-nullable-b | plain | 未修复 | 0 | 0 | LLM 调用失败(重试后): 非 JSON / 空体: |
|
||||
| field-type-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| field-type-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| field-type-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| field-type-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| key-fix-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| key-fix-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| key-fix-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| key-fix-b | plain | 未修复 | 0 | 0 | LLM 调用失败(重试后): 非 JSON / 空体: |
|
||||
| ret-type-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| ret-type-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| ret-type-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| ret-type-b | plain | 一轮修复 | 1 | 0 | |
|
||||
| type-typo-a | anchor | 一轮修复 | 1 | 0 | |
|
||||
| type-typo-a | plain | 一轮修复 | 1 | 0 | |
|
||||
| type-typo-b | anchor | 一轮修复 | 1 | 0 | |
|
||||
| type-typo-b | plain | 一轮修复 | 1 | 0 | |
|
||||
|
||||
## 护栏核对
|
||||
|
||||
- 每次越界编辑都以 `mlsp.repair_out_of_scope` + `abstain` 被拒,且 `repair_apply` 全有或全无(源零字节不改)——由 `tests/repair_loop.rs` 的确定性断言覆盖;本实验计数仅作记录。
|
||||
@@ -0,0 +1,225 @@
|
||||
# 三大问题落地计划(core 收口 · 渐进式契约 · MLSP)
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **本期开发依据 · 执行中** |
|
||||
| 日期 | 2026-09-22 |
|
||||
| 设计 | [three-problems.md](three-problems.md)(解决方向与验证设计;判据矩阵 §5.1) |
|
||||
| 范围 | **工程任务 = Phase 1–3**;验证实验(three-problems.md §5.2)协议见 [perf-validation.md](perf-validation.md)(2026-09-23 用户授权自行设计并执行) |
|
||||
| 约定 | 每任务带机器可验证验收;**任务完成 = 验收全绿 + 全量回归绿**;只改宣言不改行为 = 未完成 |
|
||||
|
||||
## 0. 本期范围与依赖
|
||||
|
||||
| 阶段 | 定位 | 本期 |
|
||||
|------|------|------|
|
||||
| **Phase 1** 核心收口 + 诊断地基 | 一切的地基 | ✅ 启动 |
|
||||
| **Phase 2** 渐进式契约层 | 校验层(schema 源头 = 表格/段落) | ✅ 本期主目标 |
|
||||
| **Phase 3** MLSP(AI 语言服务) | **下一步的超级核心**(AI 不必学语法 + 可靠反馈闭环) | ⏭ 下期启动,本期只留接口 |
|
||||
| **Phase 4** 验证实验 | 性能验证(非开发任务) | ❌ 不认领 |
|
||||
|
||||
依赖链(**跳步会倒逼「另造一套错误 / 另写一套 schema」的遮盖式捷径**):
|
||||
|
||||
```text
|
||||
T1.4 Diagnostic ──是──> T2.x 契约诊断的唯一出口
|
||||
T2.x 契约 ───────是──> T3.1 schema 注册表的主数据源
|
||||
T1.1 core-surface ─是─> T3.1 语言面 + T1.3 守卫
|
||||
```
|
||||
|
||||
---
|
||||
|
||||
## Phase 1 · 核心收口 + 诊断地基(本期启动)
|
||||
|
||||
### T1.1 核心表面清单
|
||||
|
||||
- **产出**:`doc/design/core-surface.md`
|
||||
- **实现要点**:
|
||||
- 两张表:**行构造**(heading-unit `#`/`##`、call-line `>`、branch-list `1.`、loop-list `-`、param `+ 名`、table、fence、hr、writeback、narrative、blank)与**内联标记**(ident `` `名` ``、bold-code `**…**`、italic-return `*…*`、empty-return、link-index `[键](集合)`、bracket-call `[函数]`、footnote-index `[^键]` 过渡);
|
||||
- 每行含:**id**(英文 kebab,守卫用)、Markdown 外形、语义、**戒律**(边界 + 为什么不能是普通 Markdown)、**金样**(`tests/markup-v03/…` 路径);
|
||||
- 与 [markdown-mapping-v0.3.md](../design/markdown-mapping-v0.3.md) §5 / §10 对齐;词法大类(`src/lex/mod.rs` `LineKind`)另列一节说明与构造表的关系。
|
||||
- **验收**:每行金样文件真实存在(T1.3 断言);id 全集 = T1.3 `CORE_CONSTRUCTS` 全集。
|
||||
|
||||
### T1.2 三层归属与准入戒律
|
||||
|
||||
- **产出**:`doc/design/layers.md` + `.github/PULL_REQUEST_TEMPLATE.md`(无则新建)
|
||||
- **实现要点**:
|
||||
- 归属表(含仓库路径):Language = `src/lex` `src/parse` `src/ast` `src/interp` `src/diagnostics` + 契约检查器;Runtime = `lib/` `plugins/` `crates/` `src/catalog.rs` `src/view` `src/debug` + 未来的 check/schema/mcp;Document = `*.mq.md` `skills/` `ext/` `public/` `examples/`;
|
||||
- 准入戒律:新特性先填层归属;进 Language 必答「为什么现有标记不够」;默认进 ext;核心标记计数每 release 只许持平或下降;
|
||||
- 发布 checklist:CHANGELOG 增「核心表面 Δ」栏(写入 `skills/marqdo-release/SKILL.md` 一步)。
|
||||
- **验收**:PR 模板含「层归属」必填项;layers.md 与 core-surface.md 互相引用闭合。
|
||||
|
||||
### T1.3 核心表面守卫(CI)
|
||||
|
||||
- **产出**:`tests/core_surface.rs` + `src/parse/mod.rs` 增 `pub const CORE_CONSTRUCTS: &[(&str, &str)]`(id、外形;**紧邻分类代码维护**)
|
||||
- **实现要点**:`include_str!` 解析 core-surface.md 表格 id 列 ↔ `CORE_CONSTRUCTS` **双向集合相等**;每行「金样」路径 `Path::exists()` 断言。
|
||||
- **验收(反遮盖核心)**:
|
||||
- 新增 `CORE_CONSTRUCTS` 条目不改文档 → **红**;
|
||||
- 文档加行不改代码 → **红**;
|
||||
- 删除/改名金样文件 → **红**。
|
||||
|
||||
### T1.4 诊断对象扩展(Phase 2/3 的唯一出口)
|
||||
|
||||
- **产出**:`src/diagnostics.rs` 扩展 + `src/main.rs` `run --json`
|
||||
- **实现要点**:
|
||||
- `Diagnostic` 增 `code`(如 `parse.*` / `contract.*` / `runtime.*`)、`severity`、`suggestion`、`doc_anchor`、`doc_quote`、`contract_ref`(Phase 2 起填值);`::new/at` 签名不变(全仓调用零改动),新增 builder 方法;
|
||||
- `serde::Serialize` + `to_json()`;**Display 保持 `path:line:col: message`**(人类输出零回归);
|
||||
- `run --json`:顶层错误路径把 Diagnostic 序列化输出(stderr JSON 行)。
|
||||
- **验收**:现有错误金样文本输出**零变化**;新金样断言 JSON `code`/`span` 非空;`cargo test` 全绿。
|
||||
|
||||
---
|
||||
|
||||
## Phase 2 · 渐进式契约层(本期主目标)
|
||||
|
||||
### T2.1 契约 AST + 提取器
|
||||
|
||||
- **产出**:`src/contract.rs`(`Contract` / `ParamContract` / `FieldContract` + 类型词汇 `text/number/bool/list/map/any/对象名`);`src/parse/`(提取);`src/ast.rs`(挂载,带 span)
|
||||
- **实现要点**:按 [three-problems.md](three-problems.md) §2.3/§2.4——**未绑定文档表** + 列头关键字(`参数/返回/字段/类型/说明`)+ 允许位置(函数体首个可执行行前 / 绑定前 / 对象体首成员前);**被绑定的表永不视为契约表**。
|
||||
- **验收**:三附着面金样 + 「绑定表不吃」反例金样(web_db_migration 式 `字段|类型|可空` 绑定数据表必须原样是数据);未知类型名(`nubmer`)必产诊断。
|
||||
|
||||
### T2.2 四边界运行时校验
|
||||
|
||||
- **产出**:`src/interp.rs` 接入四边界——调用实参 / 字段访问(`[键](集合)`、`.field`)/ 表绑定 / `*…*` 返回
|
||||
- **实现要点**:只在**该边界有契约**时校验(无契约 = 全动态,P1);诊断一律走 T1.4 出口(`code=contract.*`)。
|
||||
- **验收**:有契约 + 错值 → 必产诊断;**无契约路径全量回归零变化**;three-problems.md §5.2(c) 消融材料可产。
|
||||
|
||||
### T2.3 `marqdo check` + `--json`
|
||||
|
||||
- **产出**:`src/check.rs` + `src/main.rs` `Commands::Check`
|
||||
- **实现要点**:静态**按需**校验(只查有契约处);不做全局推导。
|
||||
- **验收**:契约样例工程 `check` 退出码非 0 且 JSON 合协议;干净工程 0 退出零输出。
|
||||
|
||||
### T2.4 锚点诊断
|
||||
|
||||
- **产出**:契约诊断填 `doc_anchor`(契约表行段 `file#Lx-Ly`)+ `doc_quote`(表行原文)
|
||||
- **验收**:金样断言锚点非空且行号回读命中契约表;three-problems.md §5.2(a) 材料就绪。
|
||||
|
||||
### T2.5 契约 vs 实现互查(防漂移)
|
||||
|
||||
- **产出**:check + 运行时提示——契约参数名 ≠ 升参名 / 未知类型 / 返回契约明显不符 → 诊断
|
||||
- **验收**:故意写错契约的金样**必红**(three-problems.md §5.2(d) 材料就绪)。
|
||||
|
||||
### T2.6 运行时内省 `get_schema`(可延后,P3)
|
||||
|
||||
- **产出**:`src/builtin.rs` 内省 builtin(契约是数据,P3)
|
||||
- **验收**:demo 运行中取回自身函数契约并打印。
|
||||
|
||||
---
|
||||
|
||||
## Phase 3 · MLSP(下一步的超级核心;本期只留接口)
|
||||
|
||||
### T3.1 `marqdo schema`(schema 注册表,三来源零手写)
|
||||
|
||||
- **产出**:`src/schema.rs` + `Commands::Schema`——语言面(core-surface 机械转 JSON)+ 函数/类型面(**契约** + stdlib/ext 签名)+ 项目面(`catalog.yaml`)
|
||||
- **验收**:两次生成 byte-identical;stdlib 主函数覆盖率 ≥ 90%。
|
||||
|
||||
### T3.2 `marqdo mcp`(AI 工具面)
|
||||
|
||||
- **产出**:MCP server(参考 `plugins/agent/src/mcp_server.rs`)——`syntax_list` / `syntax_get` / `fn_schema` / `type_schema` / `validate` / `repair` / `run`
|
||||
- **验收**:MCP 客户端冒烟:`fn_schema` → `validate` → `repair` 全链路。
|
||||
|
||||
### T3.3 查询式 AI Skill
|
||||
|
||||
- **产出**:`skills/marqdo/` 改写——十条硬规则 + 查询工作流(construct → validate → 修 → run);细节一律走 `syntax_get` / `fn_schema`
|
||||
- **验收**:skill 体积减半以上;金样全绿。
|
||||
|
||||
### T3.4 修复回路
|
||||
|
||||
- **产出**:repair 规则库 + 语句级 patch(AST span 对齐;预算 N=3;防循环)
|
||||
- **验收**:注入 bug 金样一轮修复率可测(three-problems.md §5.2(a) 执行就绪)。
|
||||
|
||||
---
|
||||
|
||||
## Phase 4 · 验证实验(**协议 = [perf-validation.md](perf-validation.md)**)
|
||||
|
||||
- 2026-09-23 用户授权**自行设计**性能验证设计并执行验证;`doc/next/002.md` 与性能验证无关(此前「待导入」表述已纠正)。
|
||||
- five 实验 (a)–(e) 的**材料由 Phase 1–3 的验收金样直接供给**;协议 / 指标 / 判据见 [perf-validation.md](perf-validation.md) §1–§5;
|
||||
实验产物 = [perf-validation-report-2026-09-23.md](perf-validation-report-2026-09-23.md)。
|
||||
|
||||
---
|
||||
|
||||
## 完成定义
|
||||
|
||||
| 阶段 | 完成定义 |
|
||||
|------|----------|
|
||||
| Phase 1 | T1.1–T1.4 验收全绿 |
|
||||
| Phase 2 | T2.1–T2.5 验收全绿(T2.6 可延后) |
|
||||
| Phase 3 | T3.1–T3.4 验收全绿 |
|
||||
| 三大问题「已解决」 | [three-problems.md](three-problems.md) §5.1 判据矩阵全绿 + 实验数字可复现(Phase 4) |
|
||||
|
||||
## 落地记录
|
||||
|
||||
### Phase 2 · 渐进式契约层(2026-09-22 完成 T2.1–T2.5;T2.6 按计划延后至 Phase 3)
|
||||
|
||||
- **T2.1** `src/contract.rs`(类型词汇/三形状契约表解析/合并/四边界校验诊断)+
|
||||
`src/parse/mod.rs::extract_function_contract`(位置 + 形状双重判定,**在升参推断之前**执行——
|
||||
契约表的单元不会被误升参;契约表从 body 移除,元数据永不执行)。
|
||||
反例守住:**绑定**的 `字段|类型` 数据表(web_db_migration 风格)绝不视为契约。
|
||||
- **T2.2** 四边界全接:调用实参(普通/库路径/方法三条调用路径)、返回、表绑定、字段访问
|
||||
(键存在性**先于**取值,契约诊断优先于 `missing map key`)。列语义按「几何即类型」:标量或全匹配列向量。
|
||||
- **T2.3/T2.5** `marqdo check [FILE] [--json]`:契约行 vs 升参推断形参双向互查
|
||||
(错行 ⇒ Error;新升参未覆盖 ⇒ Warning)、未知类型名(附最近建议)、错位契约;Error ⇒ 非零退出。
|
||||
- **T2.4** 全部契约诊断带 `doc_anchor`(`file.mq.md#L7-L13`)+ `doc_quote`(契约表原文)+
|
||||
`contract_ref`(`declared` = 契约声明摘要);`run --json` / `check --json` 同源输出。
|
||||
- **附带修复(错误出口唯一)**:`src/load.rs` 原来把 parse 错误 `anyhow!("{label}: {e}")` 拍平成字符串,
|
||||
结构化诊断在 import 装载链路丢失;改为 `label_error` 保留 Diagnostic(补文件标签),任何装载层都不许拍平错误。
|
||||
- 验收:`tests/contracts.rs` 12 例(提取 4 + 四边界 4 + check 3 + 正向对照 1)全绿;
|
||||
全量回归零失败(含 262 gold)。
|
||||
- **T2.6(契约导出 `get_schema`)**:按计划延后至 Phase 3 与 MLSP `schema` 一并实现。
|
||||
|
||||
### Phase 3 · MLSP(2026-09-22 完成 T3.1–T3.4 全部工程项 + T2.6)
|
||||
|
||||
- **T3.1** `marqdo mlsp` 子命令:行分隔 JSON over stdio,请求 `{"id","method","params"}` →
|
||||
响应 `{"id","ok","result"|"error"}`(永不 panic,错误也是结构化 JSON)。
|
||||
- **T3.2** 五个 handler 全部实现(`src/mlsp.rs`):
|
||||
`locate`(构造卡片 / 符号定位 + 契约)、`syntax`(关键字/别名 → 卡片;语法唯一事实来源 =
|
||||
`parse::CORE_CONSTRUCTS`,MLSP 只加检索别名不定义语法)、`validate`(parse + 契约 check → 结构化诊断数组)、
|
||||
`repair_targets`(有界修复靶点:`strategy=anchored-local`、`on_violation=abstain`——越界必拒护栏先行)、
|
||||
`schema`(**T2.6 完成**:单元名 → 形参/返回/字段契约 + 升参清单 + 锚点)。
|
||||
- 验收:`tests/mlsp.rs` 7 例全绿(语法问答/未知查询/符号定位/漂移校验/schema 导出/有界修复/结构化错误);
|
||||
stdio 冒烟通过;全量回归零失败。
|
||||
- **T3.4(修复回路)**(2026-09-22 完成工程项)——`src/repair.rs` **机械护栏**(有界行编辑
|
||||
replace/delete/insert:触碰行必须落在靶点范围内,越界 ⇒ 整体拒绝 + 越界清单 + abstain,源零字节不改;
|
||||
表尾追加允许 end+1)+ `mlsp repair_apply` 出口(护栏的 MLSP 面)+ **golden 20 例**
|
||||
(10 骨架 × 2 变体:arg/ret/field/key/typo/drift/dup;金修复经完整修复循环复现——诊断 → 靶点 →
|
||||
行编辑自动推导 → 范围自检(「易锚定」的机器验证)→ 应用 → 复验)+ 护栏负例(越界必拒/部分越界全拒)。
|
||||
**模型实验完成(2026-09-22 · `tests/repair_experiment.rs` · 双模型三轮 @ llm.cflmy.cn)**:
|
||||
§5.2(a) 锚点消融——`anchor` 臂(诊断带 `doc_anchor`/`doc_quote`/`suggestion`)**一轮修复率最高 20/20 = 100%**(目标 ≥80% ✅);
|
||||
`plain` 臂(去锚)85%–100%。三轮汇总:
|
||||
|
||||
| 轮 | 模型 | anchor 一轮 | anchor 最终 | plain 一轮 | plain 最终 | 护栏越界 |
|
||||
|---|---|---|---|---|---|---|
|
||||
| 1 | `cflmy`(Qwen3.8-Flash-Next) | **20/20(100%)** | 100% | 17/20(85%) | 17/20 | 0 |
|
||||
| 2 | `mimo-v2.6-flash`(容错加固中) | 18/20(90%) | 19/20(95%) | 19/20(95%) | 19/20 | 0 |
|
||||
| 3 | `mimo-v2.6-flash`(容错补齐后) | **20/20(100%)** | 100% | 18/20(90%) | 19/20(95%) | 0 |
|
||||
|
||||
**"≥80% 一轮修复率"三轮双臂全部达标(最低 85%)**;护栏 0 越界(越界必拒/abstain 由确定性测试
|
||||
`tests/repair_loop.rs` 硬断言覆盖)。实验同时是 `validate`→`repair_targets`→`repair_apply` 功能面的真实消费者。
|
||||
**"不稳定"定性为三类测量端问题,均已加固**(与 Marqdo 语义无关):① 网关空体抖动 → curl `--retry` + 5s 退避;
|
||||
② 推理模型 token 预算被 `reasoning_content` 吃光 → `max_tokens` 2000→8000;③ 模型输出格式畸形 → 解析容错
|
||||
(数组/单对象/NDJSON 多对象流/字符串化元素/字符串型行号/围栏/数字后多余引号的确定性修补)。
|
||||
锚点增量观察:易锚定样本两臂都高(天花板效应),cflmy 上 anchor +20pp 明显、mimo 上持平——
|
||||
**锚点价值的区分性验证留 Phase 4**(难样本/信息受限消融)。
|
||||
报告:[repair-experiment-2026-09-22.md](repair-experiment-2026-09-22.md)(cflmy)、
|
||||
[repair-experiment-2026-09-22-mimo-v2.6-flash.md](repair-experiment-2026-09-22-mimo-v2.6-flash.md)(mimo 终测)。
|
||||
- **T3.3(AI Skill 换骨:背语法 → 查 MLSP)**(2026-09-22 完成)——`marqdo` / `marqdo-dev` 两件套改为
|
||||
查询式工作流:写码前 `mlsp syntax/locate`、提交前 `mlsp validate` + `marqdo check`;
|
||||
渐进式契约三种表格形状入语法面;错误修复照 `doc_anchor` 局部改(越界必弃权)。
|
||||
|
||||
## Phase 4 · 验证执行记录(2026-09-23,协议 [perf-validation.md](perf-validation.md))
|
||||
|
||||
- **(a) 锚点修复消融** ✅ PASS——判据「anchor 一轮 ≥80% + 护栏 0 越界」:cflmy anchor 20/20=100%(plain 85%)、
|
||||
mimo-v2.6-flash anchor 20/20=100%(plain 90%),越界拒/越界应用 0/0。易锚定样本近天花板,
|
||||
增量区分性留难样本扩展(协议 §1 诚实注记)。
|
||||
- **(b) 盲测(查询式 vs 全文档背诵)** ❌ 四判据未全立 ⇒ 按 three-problems.md §3.7 **继续改协议**——
|
||||
deepseek-flash:docs 4/6 vs query 0/3(端点 infra 剔除 3/6);首轮语法错 2/3 vs 2/6;token 69,771 vs 57,276。
|
||||
mimo-v2.6-pro 两跑均端点 infra(12/12、11/12,**不可评估**;结果回填 [perf-validation-report-2026-09-23.md](perf-validation-report-2026-09-23.md) §3)。
|
||||
**真发现**:查询机制本身无缺陷(多词查询亦正确命中卡片),败例是**跨语言习惯泄漏**(元组表达式/Python 调用形)
|
||||
——模型"不知道自己不知道"而不去查。协议改进主攻方向:**自查触发机制**(validate 前置自检、
|
||||
陌生构造强制查询),而非继续改进卡片。
|
||||
- **(c) 契约消融** ✅ PASS——拦截率 4/4=100%、误报率 0/4=0%、无契约零回归 4/4(四边界:调用实参/返回/表绑定/键访问)。
|
||||
- **(d) 防漂移演练** ✅ PASS——必报率 9/9=100%(9 类故意写错全数诊断)、负对照误报 0/2。
|
||||
- **(e) 核心守卫演练** ✅ PASS——3/3 变异注入必红(清单私增/文档私增/金样删除)、还原必绿。
|
||||
- **判据矩阵回填**:three-problems.md §5.1 逐行标注——问题 1 ✅、问题 2 ✅、**问题 3 ❌(未解决)**。
|
||||
- **测量端记录**:LLM 网关随机空体为最大干扰(重试 5→12 次、退避 5→15 秒、截断自适应 max_tokens);
|
||||
harness 修复两处协议性 bug(查询额度用满误判、提取失败不进修复回路)后采样;
|
||||
模型口径按用户指示由 `cflmy`/`mimo-v2.6-flash` 改 `mimo-v2.6-pro`/`deepseek-flash`(实验 (a) 数据为原组合,不重跑)。
|
||||
- **全量干净回归**(无 API key 基线)零失败。
|
||||
@@ -0,0 +1,393 @@
|
||||
# Marqdo 三大问题:解决方向与验证设计
|
||||
|
||||
——证明「被解决」,而不是「被遮盖」
|
||||
|
||||
| | |
|
||||
|---|---|
|
||||
| 状态 | **设计 · 已导出本期开发任务:[three-problems-plan.md](three-problems-plan.md);验证实验不属于开发任务** |
|
||||
| 日期 | 2026-09-22 |
|
||||
| 来源 | [001.md](../next/001.md) 评审讨论提炼的三大问题;用户决议:**问题二走「渐进式契约 / 标注类型」**(拒绝照搬静态类型,也拒绝放弃语义校验);**问题三(AI 可靠反馈 + AI 不必学语法)是下一步的超级核心** |
|
||||
| 相关 | [markdown-mapping-v0.3.md](../design/markdown-mapping-v0.3.md)(语法宪法)· [tables-maps-footnotes.md](tables-maps-footnotes.md)(表格几何)· [catalog-cli.md](../design/catalog-cli.md) · [ai-skill.md](../design/ai-skill.md) · [objects.md](../design/objects.md) · [interpreter.md](interpreter.md) |
|
||||
|
||||
> **本文定位:三大问题的解决方向与验证设计。** 给三大问题定义「什么算被解决」(机器可验证判据,见 §5.1)与验证实验(修复率 / 盲测 / 消融 / 防漂移演练,见 §5.2),把「真正解决」与「遮盖过去」区分开。文中的解决步骤(各节 S 表)是**设计方向**;本期开发任务、排期与验收以 [three-problems-plan.md](three-problems-plan.md) 为准。**验证实验(§5.2)协议见 [perf-validation.md](perf-validation.md)**(2026-09-23 用户授权自行设计并执行)。
|
||||
>
|
||||
> **一句话:语言核心收口是地基;渐进式契约是校验层,schema 直接长在表格与说明段落里;AI 语言服务消费这两者形成可靠闭环。** 三个问题必须一起解——契约是 AI 接口的数据源,核心收口是契约语法不膨胀的前提。
|
||||
|
||||
**贯穿戒律(防止「遮盖式解决」):每个阶段的验收必须是机器可验证的行为变化。只产出宣言文档、不改变工具行为的,一律视为未完成。**
|
||||
|
||||
---
|
||||
|
||||
## 0. 问题清单与依赖
|
||||
|
||||
| # | 问题 | 定位 | 一句话方案 |
|
||||
|---|------|------|-----------|
|
||||
| 1 | 语言核心正在被生态功能淹没,identity 不清 | **地基** | 三层归属表 + 准入戒律 + 核心表面守卫(CI 断言) |
|
||||
| 2 | 「静态类型列为非目标」被误读为「不需要语义校验」;但照搬 Java/TS 静态类型会毁掉低摩擦 | **校验层** | **渐进式契约**:schema 写在表格 / 说明段落里;按需契约校验;诊断带文档锚点 |
|
||||
| 3 | AI 反馈不可靠(只有给人看的文本);AI 还在背千行语法文档 | **超级核心** | **Marqdo Language Server for AI(MLSP)**:结构化诊断协议 + 可查询 schema + 修复回路 |
|
||||
|
||||
依赖链:
|
||||
|
||||
```text
|
||||
问题 1(核心收口:契约语法不许越界进核心)
|
||||
└─→ 问题 2(契约层:schema 源头 = 表格 / 说明段落)
|
||||
└─→ 问题 3(schema 注册表 = 契约 + 核心表面 + catalog)
|
||||
└─→ 实证(修复实验 + 消融基准 → README 愿景 6)
|
||||
```
|
||||
|
||||
共享地基先行项:**统一诊断协议(Diagnostic)**——问题 2 的契约校验与问题 3 的 AI 反馈都以它为出口,因此它排在最前(见 §3.3 与 §4)。
|
||||
|
||||
---
|
||||
|
||||
## 1. 问题一:语言核心必须小
|
||||
|
||||
### 1.1 诊断
|
||||
|
||||
Marqdo 现在同时是:Markdown 语言 + literate programming + 知识库 + catalog + web + WASM + 浏览器 + LLM + agent + MCP + 量子 + 线代 + 插件 ABI。工程上这很酷;语言设计上的危险是:
|
||||
|
||||
> **核心语言的 identity 被生态功能淹没;新功能随手进核心 → 语法面暗中膨胀。**
|
||||
|
||||
真正缺的不是「收敛宣言」,而是**三样机制**:
|
||||
|
||||
1. 一份穷举的**核心表面清单**——现在「什么算核心」没有唯一明文;
|
||||
2. 一张**三层归属表**——新提案无处登记,默认哪儿都塞;
|
||||
3. 一条**越界即可见的守卫**——核心语法暗增长没有任何机器报警。
|
||||
|
||||
### 1.2 目标 / 非目标
|
||||
|
||||
**目标**:Language / Runtime / Document 三层分离;核心标记维持**约 15 个**的封闭集合,生态增长不再增加语言面。
|
||||
|
||||
**非目标**:删掉 web/quantum/agent 等功能(它们不是错,层次混放才是错);重写解释器;给核心加模块可见性等系统编程特性。
|
||||
|
||||
### 1.3 三层归属表(S1 产出物,此处先锁定语义)
|
||||
|
||||
| 层 | 内容 | 仓库位置 |
|
||||
|----|------|----------|
|
||||
| **Language** | 标记映射、词法/行分类、AST、语义、升参、**契约检查器**、诊断 | `src/`(解释器核心)· `doc/design/markdown-mapping-v0.3.md` |
|
||||
| **Runtime** | stdlib、插件 ABI、WASM、debugger、view、catalog / schema CLI、MCP | `lib/` · `plugins/` · `crates/` · `src/bin` |
|
||||
| **Document** | 知识单元、skills、示例、public 站、agent 工作流、官方扩展 | `*.mq.md` · `skills/` · `ext/` · `public/` · `examples/` |
|
||||
|
||||
核心表面清单(S1 的枚举基线,来自宪法 §5,逐项进 `core-surface.md`):
|
||||
|
||||
叙述(含 `` `名` `` 声明/引用)· `**…**` 代码 · `*…*` 返回 · 空返回形(`*None*` 等)· `>` 调用 · `#` / `##` 标题单元 · 体内 `1.` 分支 · `-` 循环 · GFM 表格 = 集合 · `[键](集合)` 取元 · `修饰… [函数名]` 方括号调用 · 脚注取元(过渡)· ` ```lang ` 外联 · `---` 叙述分隔 · `` + `名` `` 形参(兼容形)。
|
||||
|
||||
### 1.4 解决方向(设计 · 任务化见 plan)
|
||||
|
||||
| 步 | 动作 | 产出 | 验收(机器可验证) |
|
||||
|----|------|------|--------------------|
|
||||
| S1 | 写 `doc/design/core-surface.md`:穷举核心标记,每个标记一行「戒律」(语义、边界、**为什么不能是普通 Markdown**) | 核心表面唯一清单 | 清单项 ↔ `tests/markup-v03/` 金样逐项有例;缺项即测试红 |
|
||||
| S2 | 写 `doc/design/layers.md`:§1.3 归属表 + **准入戒律**——任何新特性提案必须先填「进哪层」;进 Language 层必须答「为什么现有标记不够」,默认答案是「进 ext」 | 归属与准入制度 | 仓库 PR 模板加「层归属」必填项 |
|
||||
| S3 | 核心表面守卫:Rust 测试断言「词法分类枚举 ↔ core-surface.md 表格」一致(脚本解析 markdown 表格比对枚举名) | `tests/core_surface.rs` | 故意加一个新标记不更新清单 → CI 红 |
|
||||
| S4 | 核心变更强制走 ADR:新增核心标记必须有 `doc/adr/` 编号 + 反例说明;每个 release 的 CHANGELOG 单列「核心表面 Δ」 | ADR 戒律 | 发版 checklist 含「核心表面 Δ」栏 |
|
||||
| S5 | 文档收口:README/doc 目录按三层重排;`doc/design/` 只收 Language 层决策,Runtime 进 `doc/roadmap/`,生态面进各自 ext 文档 | 重排后的文档树 | README 目录树与三层表一一对应 |
|
||||
|
||||
**观测指标**(不是 KPI,是体检):核心标记计数每个 release 只许持平或下降;连续 3 个版本增长即触发 Language 层复盘。
|
||||
|
||||
### 1.5 遮盖 vs 解决
|
||||
|
||||
| 遮盖式做法(禁止) | 本方案的做法 | 判据 |
|
||||
|--------------------|--------------|------|
|
||||
| 写一篇「语言核心很重要」的宣言 | 清单 + 归属表 + **CI 守卫** | 新增标记不更新清单时 CI 必须红 |
|
||||
| 「以后尽量不加语法」的口头约定 | ADR 强制 + CHANGELOG 核心表面 Δ | 没有 ADR 编号的核心变更不允许合并 |
|
||||
| 把 ext 功能砍掉显得小 | 不砍功能,只**归层** | web/quantum/agent 全部保留且测试通过 |
|
||||
|
||||
---
|
||||
|
||||
## 2. 问题二:渐进式契约 / 标注类型
|
||||
|
||||
### 2.1 分歧点澄清(本方案的立场)
|
||||
|
||||
GPT 一侧强调「AI 可验证语言需要强语义校验」,容易被听成「上静态类型」。**这两件事不一样**:
|
||||
|
||||
> 「放弃全程序静态编译类型」**≠** 「放弃语义检查、字段解析、契约校验」。
|
||||
|
||||
照搬 Java/TS 式静态类型会破坏「自然叙述优先、低摩擦」的体验——Marqdo 会变得笨重,丢掉对业务人员 / agent 即兴脚本的友好性。**目标不是变成静态语言,而是一套附着在文档单元上的可查询契约系统,作为 AI 闭环里的校验层。**
|
||||
|
||||
渐进式契约的四条特征:
|
||||
|
||||
1. **动态默认**——不强制任何标注,保持动态,适合快速原型与 agent 即兴脚本;
|
||||
2. **契约即文档**——schema 可选地写在**表格、文档段落**里,不引入第二套标注语言;
|
||||
3. **按需校验**——不做全局类型推导;在调用边界 / 字段访问时校验「当前值 vs 契约」;
|
||||
4. **诊断即文档链接**——错误不只报类型错,还定位到文档里对应的说明段落,给 AI 的诊断自带文档上下文。
|
||||
|
||||
**独有优势(TS/Python 做不到)**:契约的 schema 源头就是表格与函数说明段落本身——不是额外写一套 JSON Schema。知识、契约、数据、代码在同一份 `.mq.md` 里,**契约即知识**。这是 Knowledge-Code Coherence 的第一个可落地实证:契约说 `int` 实际来了 `text` 时,被机器抓住的不是「文档过期」,而是**知识不一致**。
|
||||
|
||||
### 2.2 设计原则
|
||||
|
||||
| # | 原则 | 含义 |
|
||||
|---|------|------|
|
||||
| P1 | 无契约 = 全动态 | 没写契约的单元行为**一字不变**(零回归是硬验收) |
|
||||
| P2 | 不新增核心标记 | 契约语法全部骑在现有 Markdown 外形上(问题一戒律直接约束本节) |
|
||||
| P3 | 契约是数据 | 契约表本身就是 Marqdo 集合,运行时可内省(`get_schema` 一类的 lib 函数免费获得) |
|
||||
| P4 | 双向防漂移 | 不只「值 vs 契约」,还查「契约 vs 实现」(§2.7-S5) |
|
||||
| P5 | 诊断带锚点 | 凡契约诊断必含:契约表所在文件 + 行段 + 原文引用 |
|
||||
|
||||
### 2.3 契约的三个附着面
|
||||
|
||||
**A. 函数契约** —— `##` 函数体内、首个可执行行之前的**未绑定文档表**:
|
||||
|
||||
```markdown
|
||||
## 加一
|
||||
|
||||
加一函数:把输入加一。
|
||||
|
||||
| 参数 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| `n` | number | 输入值 |
|
||||
|
||||
| 返回 | 类型 | 说明 |
|
||||
|------|------|------|
|
||||
| — | number | n+1 |
|
||||
|
||||
对于输入变量`n`,执行**n=n+1**接着返回*n*。
|
||||
```
|
||||
|
||||
参数仍由 v0.3 升参推断(宪法 §8),契约**不改变**形参机制,只在存在时校验——零摩擦兼容存量代码。
|
||||
|
||||
**B. 集合 / 表格契约** —— 紧邻 `` `名` = `` 绑定之前的未绑定文档表:
|
||||
|
||||
```markdown
|
||||
| 字段 | 类型 | 可空 |
|
||||
|------|------|------|
|
||||
| id | number | 否 |
|
||||
| name | text | 否 |
|
||||
|
||||
`用户` =
|
||||
|
||||
| 行 | id | name |
|
||||
|----|----|------|
|
||||
| 1 | 1 | 甲 |
|
||||
```
|
||||
|
||||
**C. 对象契约** —— `# 对象` 体内首个成员之前的 `| 字段 | 类型 |` 表;同时被 catalog 提取为条目 schema(见 §3.5 项目面)。
|
||||
|
||||
**先例**:schema 长在表格里今天已经发生——`web_db_migration` 的 `| 字段 | 类型 | 可空 |` 列就是以数据形式传递的 DDL schema。契约系统把这个模式升格为**通用约定**。
|
||||
|
||||
### 2.4 形态选型(为什么是「契约表」)
|
||||
|
||||
| 候选 | 形态 | 判决 |
|
||||
|------|------|------|
|
||||
| **P-A 契约表**(选定) | 列头关键字(`参数`/`返回`/`字段`/`类型`/`说明`)的未绑定文档表 | ✅ 与「GFM 表格即数据」哲学一致;catalog 可提取;人读无负担 |
|
||||
| P-B 叙述内标注 | `` `n`: int `` 写在散文里 | ❌ **否决**:改动行内 `` `名` `` 词法 = 核心标记变更,违反问题一戒律与 P2 |
|
||||
| P-C frontmatter | YAML 声明契约 | ⚠️ 仅作文件级汇总的可选补充(serde_yaml 已在解析元信息);函数粒度不够,不作主形态 |
|
||||
|
||||
**消歧规则(防「契约表」误吃数据表)**:
|
||||
|
||||
1. **被绑定的表永不视为契约表**——`名` = 后的表是数据(宪法 §5:未绑定才「求值后丢弃」,即文档表)。`web_db_migration` 的列头恰好叫 `字段|类型|可空` 但它是绑定数据,不受影响;
|
||||
2. 契约表必须是**未绑定文档表** + 列头命中关键字集,且出现在允许位置(§2.3 A/B/C);
|
||||
3. 同一位置出现多张契约表:参数表与返回表各认各的列头;其余重复 → 诊断(防两份契约打架)。
|
||||
|
||||
### 2.5 类型词汇(刻意做小)
|
||||
|
||||
`text` · `number` · `bool` · `list` · `map` · `any` · 对象名(与 `# 对象` / `_type` 对齐)。
|
||||
|
||||
- 词汇表**只有这一行**;扩展类型词汇 = Language 层变更,走 ADR(问题一 S4)。
|
||||
- 类型名拼错(如 `nubmer`)→ **必须报诊断**(§2.7 验收),否则契约本身成为新的漂移源。
|
||||
|
||||
### 2.6 校验时机(按需四边界,不做全局推导)
|
||||
|
||||
| 边界 | 校验什么 | 时机 |
|
||||
|------|----------|------|
|
||||
| 调用实参 | 实参值 vs 参数契约(类型 + 可选 required) | 运行时调用瞬间;`check` 静态可查处一并查 |
|
||||
| 字段访问 | `[键](集合)` / `obj.field` vs 字段契约(键存在性 + 类型) | 访问瞬间 |
|
||||
| 表绑定 | `` `名` = `` 表的几何/单元格 vs 集合契约 | 绑定瞬间 |
|
||||
| 返回 | `*…*` 值 vs 返回契约 | 返回瞬间 |
|
||||
|
||||
无契约 → 该边界直接跳过(P1)。校验器**不是**编译器:不做跨函数全局推导、不要求全程序一致。
|
||||
|
||||
### 2.7 解决方向(设计 · 任务化见 plan)
|
||||
|
||||
| 步 | 动作 | 产出 | 验收(机器可验证) |
|
||||
|----|------|------|--------------------|
|
||||
| S1 | 契约提取器:按 §2.3/§2.4 规则解析未绑定文档表 → `Contract` 对象,挂入 AST 单元(`Param` 已有 `span`,契约同级带 `span`) | AST + 提取器 | 金样:三附着面各一份提取断言(含「绑定表不吃」反例) |
|
||||
| S2 | 运行时边界校验:§2.6 四边界接入 `Contract` | 运行时校验 | 有契约 + 错值 → **必**产出 §3.3 诊断;无契约金样**全绿零回归** |
|
||||
| S3 | `marqdo check`:静态按需校验(只查有契约处),`--json` 输出诊断 | CLI | 契约样例工程 `check` 退出码非 0 且诊断 JSON 合协议 |
|
||||
| S4 | 诊断锚点:契约错误必带 `doc_anchor` + `doc_quote`(契约表原文行) | 诊断增强 | 金样断言诊断 JSON 里锚点字段非空、行号可回读原文 |
|
||||
| S5 | **契约 vs 实现互查**(P4):契约参数名 ≠ 升参推断名、契约类型名未知、契约返回 vs 语句结构明显不符 → 诊断 | 防漂移检查 | 故意写错契约的金样**必须**红(而不是静默信任契约) |
|
||||
| S6 | 运行时内省:`lib` 提供 `get_schema 单元=…`(P3:契约是数据) | lib 函数 | demo:运行中取回自身函数契约表并打印 |
|
||||
|
||||
### 2.8 遮盖 vs 解决
|
||||
|
||||
| 遮盖式做法(禁止) | 本方案的做法 | 判据 |
|
||||
|--------------------|--------------|------|
|
||||
| 把「静态类型」继续列为非目标就收工 | 明确「≠ 放弃语义校验」,落**边界校验器** | 运行时行为变化可测(§2.7-S2) |
|
||||
| 照搬 Java/TS 全程序静态类型 | 渐进式:无标注零摩擦,按需四边界 | 无契约金样零回归 |
|
||||
| 另写一套 JSON Schema 文件 | schema = 表格 / 说明段落本身 | 仓库里**不允许**出现手写的第二份 schema;契约只能从 `.mq.md` 提取 |
|
||||
| 契约写了就永远正确 | 契约 vs 升参互查 + 未知类型名必报 | 故意写错契约的金样必红 |
|
||||
| 错误只报 `type mismatch` 文本 | 诊断带文档锚点 + 原文引用 | 金样断言锚点字段 |
|
||||
|
||||
---
|
||||
|
||||
## 3. 问题三(超级核心):AI 可靠反馈 + AI 不必学语法细节
|
||||
|
||||
### 3.1 两个子问题
|
||||
|
||||
**A. 可靠反馈**:今天解释器诊断是给人看的文本(`path:line:col` + message,见 ai-skill「按诊断迭代」)。AI 拿到它要「猜」哪里错、对应文档哪句话、怎么改。GPT 设想的闭环——
|
||||
|
||||
```text
|
||||
LLM → Marqdo → 语义校验 → 结构化 Error → LLM 修复 → 合格程序
|
||||
```
|
||||
|
||||
——的第一块砖就是**结构化诊断**,而且诊断必须自带文档上下文(问题二 §2.1-4 的锚点在这里被 AI 消费)。
|
||||
|
||||
**B. 不必学语法**:今天 `skills/marqdo` 是「面向模型的压缩操作手册」= **背诵式**。正确形态是**查询式**:
|
||||
|
||||
```text
|
||||
AI ── discover ─→ construct ─→ validate ─→ execute ─→ inspect
|
||||
```
|
||||
|
||||
AI 不阅读一千行文档然后猜,而是按需调 `get_syntax("branch")`、`fn_schema("print")`、`validate(program)`。**这才是 Marqdo 与普通 DSL 拉开距离的地方。**
|
||||
|
||||
### 3.2 总形态:Marqdo Language Server for AI(MLSP)
|
||||
|
||||
一套机器可读的语言服务协议。工具面(首版最小集):
|
||||
|
||||
| 工具 | 入参 | 返回 | 数据源 |
|
||||
|------|------|------|--------|
|
||||
| `syntax_list` / `syntax_get` | — / 构造名 | 核心构造表 / 单构造戒律 | core-surface(问题一 S1,机械转 JSON) |
|
||||
| `fn_schema` | 函数名 | 参数/返回契约 + 说明 + 锚点 | 契约(问题二)+ stdlib/ext 签名 |
|
||||
| `type_schema` | 对象名 | 字段契约 + 方法表 | `# 对象` 契约 + catalog |
|
||||
| `validate` | 程序文本/文件 | `Diagnostic[]` | 契约校验器 + 语义检查 |
|
||||
| `repair` | 程序 + 诊断 | 建议 patch(限定行级) | 诊断规则库 |
|
||||
| `run` | 入口 | 结果或结构化错误 | 运行时 |
|
||||
|
||||
交付通道(按序):**`marqdo mcp`**(MCP server,复用 agent ext 的 MCP 经验)→ CLI `--json`(任何宿主可用)→ `skills/marqdo` 改造成「教 AI 查询」(§3.6)。
|
||||
|
||||
### 3.3 先决:统一诊断协议(可靠反馈的地基)
|
||||
|
||||
所有层(解释器、check、契约、插件错误)统一产出:
|
||||
|
||||
```json
|
||||
{
|
||||
"code": "contract.arg_mismatch",
|
||||
"severity": "error",
|
||||
"message": "参数 `n` 期望 number,实际 text",
|
||||
"span": { "file": "demo.mq.md", "line": 12, "col": 3, "heading": ["加一"] },
|
||||
"doc_anchor": "demo.mq.md#L4-L7",
|
||||
"doc_quote": "| `n` | number | 输入值 |",
|
||||
"contract_ref": { "unit": "加一", "kind": "param", "name": "n", "declared": "number" },
|
||||
"suggestion": "把实参改为数字;若契约有误,改契约表的类型列"
|
||||
}
|
||||
```
|
||||
|
||||
规则:
|
||||
|
||||
1. 凡可定位必带 `span`(AST 已有 span,宪法 §10.2);
|
||||
2. 凡契约错误必带 `doc_anchor` + `doc_quote`——**错误直接定位到文档里对应的说明段落**;
|
||||
3. 凡可修复建议必带 `suggestion`(给修复回路的原料);
|
||||
4. 人类文本输出保持现状,`--json` 并行提供——人机同读同写,诊断也一样。
|
||||
|
||||
### 3.4 修复回路(repair loop)
|
||||
|
||||
```text
|
||||
validate(p) → diagnose → patch(p, diag) → validate(p') → … (预算 N=3 轮)
|
||||
```
|
||||
|
||||
- **限定行级 patch**,不允许整文件重写(防止 AI「修复」时破坏叙述);
|
||||
- 尝试预算与重复诊断去重,防循环;
|
||||
- 实验指标:**一轮修复率**(带锚点 vs 不带锚点两组对比)——这是「可靠反馈」的直接证据。
|
||||
|
||||
### 3.5 Schema 注册表(三来源,零手写第二份)
|
||||
|
||||
| 面 | 来源 | 说明 |
|
||||
|----|------|------|
|
||||
| 语言面 | `core-surface.md` 机械转 JSON | 构造戒律原文进 `description` |
|
||||
| 函数 / 类型面 | **契约**(问题二产物)+ stdlib/ext 签名 | schema 源头 = 表格与说明段落(§2.1 独有优势) |
|
||||
| 项目面 | `catalog.yaml`(catalog 自动生成) | 知识单元、链接关系(知识图的边) |
|
||||
|
||||
格式 `marqdo-schema.json`(示意):
|
||||
|
||||
```json
|
||||
{
|
||||
"language": "marqdo",
|
||||
"version": "0.3",
|
||||
"constructs": [{ "name": "branch", "syntax": "1. … 2. … N. *", "rule": "自 1. 递增;重出 1. 开新判断" }],
|
||||
"functions": [{ "name": "加一", "params": [{ "name": "n", "type": "number", "desc": "输入值", "anchor": "#L4" }], "returns": { "type": "number" } }]
|
||||
}
|
||||
```
|
||||
|
||||
命令:`marqdo schema [PATH]`(catalog 的兄弟命令,同样**只生成不手改**)。
|
||||
|
||||
### 3.6 AI Skill:从「背诵」改「查询」
|
||||
|
||||
`skills/marqdo` 改造原则:
|
||||
|
||||
- `SKILL.md` 只留**十条不可违反的硬规则**(叙述默认安全、`**` 代码 `*` 返回、表格几何、命名/双语约定等)+ MLSP 工具用法;
|
||||
- 语法细节、API 明细**一律走 `syntax_get` / `fn_schema`**,不再进上下文;
|
||||
- 工作流改为:construct(凭硬规则写)→ `validate` → 按诊断修 → `run`。
|
||||
|
||||
### 3.7 解决方向(设计 · 任务化见 plan)
|
||||
|
||||
| 步 | 动作 | 验收(机器可验证) |
|
||||
|----|------|--------------------|
|
||||
| S1 | Diagnostic 结构落地(Rust)+ 解释器诊断改造 + `run --json` / `check --json` | 现有错误金样转 JSON 后 `code`/`span` 全非空 |
|
||||
| S2 | 契约诊断接入(消费 §2.7-S4 的锚点) | §2.8 判据全部成立 |
|
||||
| S3 | `marqdo schema` 生成器(三来源汇聚) | 生成物 diff 稳定(两次生成 byte-identical);stdlib 主函数覆盖率 ≥ 90% |
|
||||
| S4 | `marqdo mcp`:暴露 §3.2 工具面 | 用 MCP 客户端冒烟:`fn_schema` → `validate` → `repair` 全链路 |
|
||||
| S5 | `skills/marqdo` 改造为查询式(保留硬规则十条) | skill 体积减半以上;既有 `marqdo run` 金样仍全绿 |
|
||||
| S6 | 修复回路 + 实验执行(实验设计见 §5.2) | 实验报告(进 MarqdoThesis,对接 README 愿景 6) |
|
||||
|
||||
**「解决而不是遮盖」的最终判据(问题三)**:盲测组(零语法文档 + 工具)的成绩**不劣于**全文档背诵组,且 token 显著更少、修复轮次不升——达不到就继续改协议,**不许**用「再写一篇更好的文档」充数。
|
||||
|
||||
### 3.8 遮盖 vs 解决
|
||||
|
||||
| 遮盖式做法(禁止) | 本方案的做法 | 判据 |
|
||||
|--------------------|--------------|------|
|
||||
| 再写一份更全的 AI 语法文档 | 查询协议 + schema 注册表 | 盲测实验成立 |
|
||||
| 让 AI「多读 reference.md」 | `syntax_get` / `fn_schema` 按需取 | skill 体积减半且成绩不降 |
|
||||
| 报错文本润色一下 | 统一 Diagnostic JSON + 文档锚点 | 诊断金样断言锚点字段 |
|
||||
| 主观感觉「AI 好用了」 | 一轮修复率 / 盲测 / token 三组数字 | 实验报告可复现(MarqdoThesis) |
|
||||
|
||||
---
|
||||
|
||||
## 4. 路线顺序(依赖序 · 排期见 [three-problems-plan.md](three-problems-plan.md))
|
||||
|
||||
> 本表是**依赖顺序**,不是排期表——任务化与本期范围见 [three-problems-plan.md](three-problems-plan.md)。
|
||||
|
||||
| 顺序 | 内容 | 产出 | 依赖 |
|
||||
|------|------|------|------|
|
||||
| **M0 核心收口** | 问题一 S1–S5 | core-surface.md · layers.md · CI 守卫 | — |
|
||||
| **M1 诊断地基** | 问题三 S1(Diagnostic + `--json`) | 统一诊断协议 | M0 |
|
||||
| **M2 契约层** | 问题二 S1–S6 | 提取器 · 四边界校验 · `check` · 锚点 · 防漂移 | M1 |
|
||||
| **M3 AI 语言服务** | 问题三 S2–S5 | schema 生成 · `marqdo mcp` · 查询式 skill · 修复回路 | M2 |
|
||||
| **M4 实证** | 问题三 S6 | 修复实验 + 盲测 + 消融(对接 README 愿景 6) | M3 |
|
||||
|
||||
顺序理由:Diagnostic 先于契约(契约诊断是它的第一个消费者);契约先于 MLSP(契约是 schema 注册表的主数据源)——**任何一个跳步都会倒逼「另写一套 schema / 另造一套错误」的遮盖式捷径**。
|
||||
|
||||
---
|
||||
|
||||
## 5. 验证矩阵与实验设计(本文核心)
|
||||
|
||||
### 5.1 判据矩阵:什么算「已解决」
|
||||
|
||||
| 问题 | 解决的定义(机器可验证) | 遮盖式收场(一句话就能写完的,禁止) | 验证手段 | 判定(2026-09-23) |
|
||||
|------|--------------------------|--------------------------------------|----------|--------------------|
|
||||
| 1 核心要小 | 新标记不更新 core-surface → **CI 红** | 「我们重视语言核心」 | CI 守卫演练:故意加标记不更新清单,必须红 | ✅ (e) 3/3 必红→还原必绿 |
|
||||
| 2 语义校验 | 四边界校验器上线;契约写错**必**被机器抓;无契约路径零回归 | 「静态类型是非目标」/照搬 TS 类型/另写 JSON Schema | 契约金样 + 防漂移金样 + 全量回归 | ✅ (c) 拦截 100%/误报 0%/零回归;(d) 必报 9/9 |
|
||||
| 3 AI 反馈 / 语法 | 盲测组错误率 ≤ 全文档组且 token 显著更少;一轮修复率可复现 | 「再写更好的文档」 | §5.2 实验 (a)(b)(d) | ❌ (a) 过(双模型 100%);**(b) 四判据未全立** ⇒ 按 §3.7 继续改协议 |
|
||||
|
||||
> 2026-09-23 实测数字与判据对照:[perf-validation-report-2026-09-23.md](perf-validation-report-2026-09-23.md)。
|
||||
> 按本矩阵口径:**问题 1、2 实证成立;问题 3 未解决**(查询式盲测未跑赢全背诵——详见报告 §3 的四条要点,
|
||||
> 其中「模型不查自己不知道的语法」是协议改进的主攻方向)。
|
||||
|
||||
### 5.2 实验设计(性能 / 可靠性验证)
|
||||
|
||||
| 实验 | 对照组 | 指标 | 材料 |
|
||||
|------|--------|------|------|
|
||||
| (a) 锚点修复 | 诊断带 `doc_anchor` vs 不带 | 一轮修复率、平均修复轮次 | 注入已知 bug 的金样集 |
|
||||
| (b) 盲测(零语法文档) | 只给 MLSP 工具 vs 给全文档背诵(现 `skills/marqdo`) | 语法错误率、契约违规率、token 消耗 | 任务集(QuixBugs 类 + 业务逻辑类) |
|
||||
| (c) 契约消融 | 契约开 / 关 | 契约拦截率、误报率、无契约路径零回归 | 同上 |
|
||||
| (d) 防漂移演练 | 契约故意写错(类型 typo / 参数名与升参不符) | 必报率 = 100% | 契约金样 |
|
||||
| (e) 核心守卫演练 | 故意加新标记不更新清单 | CI 红 = 必须 | — |
|
||||
|
||||
实验基建:复用 MarqdoThesis `formal/exp1–3` 的 runner / oracle / 容错重试;报告即 README 愿景 6 的「可靠性基准」材料。**判据矩阵全绿 + 实验数字可复现之前,三大问题一律视为未解决**——这是「不遮盖」的最终保险。
|
||||
|
||||
---
|
||||
|
||||
## 6. 未决问题(进 M0/M2 的 ADR 逐条锁定)
|
||||
|
||||
1. 契约表消歧的边界案例:紧邻绑定的未绑定表(注释表)紧贴数据表时的归属规则要不要「隔空行才算分离」?(M2-S1 金样定夺)
|
||||
2. 参数表与返回表合并成单表(首列 `参数`/`返回` 混排)是否更可读?(M2-S1 对比样例后定)
|
||||
3. 对象字段契约:与 [objects.md](../design/objects.md) 「不把表当对象定义语言」的非目标如何划界——契约是**可选校验元数据**,不定义对象布局(对象仍是运行时 map);需 ADR 明文,避免与 [tables-maps-footnotes.md](tables-maps-footnotes.md) 非目标条目互相打架。
|
||||
4. 类型词汇是否收 `对象名` 之外的复合形(如 `list<number>`)?——默认**不收**,需要时走 ADR(P2 + 问题一 S4)。
|
||||
5. `marqdo check` 独立子命令 vs `run --check` 并存?(M2-S3 定,倾向独立 + `run` 默认轻查)
|
||||
6. `repair` 的 patch 边界:行级 patch 的具体格式(unified diff vs 语句级替换)?(M3-S4 定,倾向语句级——与 AST span 对齐)
|
||||
+1
-1
@@ -1 +1 @@
|
||||
1.0.4
|
||||
1.1.1
|
||||
+518
-24
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: ext/ai/agent
|
||||
description: Document-driven agent — step / plan; tools via lib/subtask; runtime via ABI v2 agent plugin.
|
||||
description: Document-driven agent v2 — step / plan / run; Skill Compilation via ABI plugin (episodes → learn → llm_free).
|
||||
import llm:ext/ai/llm.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
@@ -49,6 +49,37 @@ The runbook `.mq.md` is ground truth (code as documentation). Default budgets ke
|
||||
|
||||
---
|
||||
|
||||
## model_turn
|
||||
+ `model`
|
||||
+ `prompt`
|
||||
+ `stream`=False
|
||||
+ `echo`=False
|
||||
|
||||
One LLM turn via the semantic surface (`ask` / `stream`+`collect`). Returns `{text,llm_calls,tokens}` for episode metrics.
|
||||
|
||||
1. `stream`
|
||||
**evs = > model.stream prompt=`prompt` echo=`echo`**
|
||||
**text = > llm.collect events=`evs`**
|
||||
**tokens = 0**
|
||||
2. *
|
||||
**r = > model.ask prompt=`prompt`**
|
||||
**text = [text](r)**
|
||||
**tokens = [tokens](r)**
|
||||
1. `tokens`
|
||||
**_ = 1**
|
||||
2. *
|
||||
**tokens = 0**
|
||||
|
||||
`out` =
|
||||
|
||||
| text | llm_calls | tokens |
|
||||
|------|-----------|--------|
|
||||
| `text` | 1 | `tokens` |
|
||||
|
||||
*out*
|
||||
|
||||
---
|
||||
|
||||
## dump_step_context
|
||||
+ `agent`
|
||||
+ `task`=""
|
||||
@@ -424,6 +455,128 @@ Call a named tool from a MCP-shaped fixture (`results` / `calls` map). Optional
|
||||
|
||||
---
|
||||
|
||||
## mcp_connect
|
||||
+ `command`
|
||||
+ `args`=None
|
||||
+ `session`="default"
|
||||
+ `name`="mcp"
|
||||
|
||||
Open a stdio MCP client session (spawn `command` + `args`, JSON-RPC initialize). Evidence only — workbook remains authority.
|
||||
|
||||
**p = > plugin.native_path name="agent"**
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
2. *
|
||||
> print text=ext/ai/agent: native agent plugin not found
|
||||
> sys.exit code=1
|
||||
|
||||
1. `args`
|
||||
*> agent_mcp_client action="connect" command=`command` args=`args` session=`session` name=`name`*
|
||||
2. *
|
||||
*> agent_mcp_client action="connect" command=`command` session=`session` name=`name`*
|
||||
|
||||
## mcp_client_list
|
||||
+ `session`="default"
|
||||
+ `command`=None
|
||||
+ `args`=None
|
||||
|
||||
List tools from an open MCP session, or one-shot when `command=` is set (no session retained).
|
||||
|
||||
1. `command`
|
||||
1. `args`
|
||||
*> agent_mcp_client action="list" command=`command` args=`args`*
|
||||
2. *
|
||||
*> agent_mcp_client action="list" command=`command`*
|
||||
2. *
|
||||
*> agent_mcp_client action="list" session=`session`*
|
||||
|
||||
## mcp_client_call
|
||||
+ `name`=None
|
||||
+ `session`="default"
|
||||
+ `arguments`=None
|
||||
+ `command`=None
|
||||
+ `args`=None
|
||||
+ `tool`=None
|
||||
|
||||
Call a tool on an open MCP session, or one-shot when `command=` is set. Prefer `tool=` for oneshot when `name` would collide with server name.
|
||||
|
||||
1. `command`
|
||||
1. `tool`
|
||||
**tn = tool**
|
||||
2. *
|
||||
**tn = name**
|
||||
1. `args`
|
||||
*> agent_mcp_client action="call" command=`command` args=`args` tool=`tn` arguments=`arguments`*
|
||||
2. *
|
||||
*> agent_mcp_client action="call" command=`command` tool=`tn` arguments=`arguments`*
|
||||
2. *
|
||||
*> agent_mcp_client action="call" session=`session` name=`name` arguments=`arguments`*
|
||||
|
||||
## mcp_disconnect
|
||||
+ `session`="default"
|
||||
|
||||
Close a stdio MCP client session.
|
||||
|
||||
*> agent_mcp_client action="close" session=`session`*
|
||||
|
||||
---
|
||||
|
||||
## resume_save
|
||||
+ `id`
|
||||
+ `goal`=None
|
||||
+ `task`=None
|
||||
+ `status`=running
|
||||
+ `round`=0
|
||||
+ `workbook`=None
|
||||
+ `events`=None
|
||||
+ `route`=None
|
||||
+ `result`=None
|
||||
+ `resume_dir`=.marqdo/agent-resume
|
||||
|
||||
Persist a plan/run checkpoint as `.marqdo/agent-resume/<id>.json`.
|
||||
|
||||
**p = > plugin.native_path name="agent"**
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
2. *
|
||||
> print text=ext/ai/agent: native agent plugin not found
|
||||
> sys.exit code=1
|
||||
|
||||
*> agent_resume_save id=`id` goal=`goal` task=`task` status=`status` round=`round` workbook=`workbook` events=`events` route=`route` result=`result` resume_dir=`resume_dir`*
|
||||
|
||||
## resume_load
|
||||
+ `id`
|
||||
+ `resume_dir`=.marqdo/agent-resume
|
||||
|
||||
Load a resume checkpoint (or `{ok:false,found:false}`).
|
||||
|
||||
*> agent_resume_load id=`id` resume_dir=`resume_dir`*
|
||||
|
||||
## resume_clear
|
||||
+ `id`
|
||||
+ `resume_dir`=.marqdo/agent-resume
|
||||
|
||||
Delete a resume checkpoint.
|
||||
|
||||
*> agent_resume_clear id=`id` resume_dir=`resume_dir`*
|
||||
|
||||
## resume_list
|
||||
+ `resume_dir`=.marqdo/agent-resume
|
||||
|
||||
List checkpoint ids under `resume_dir`.
|
||||
|
||||
*> agent_resume_list resume_dir=`resume_dir`*
|
||||
|
||||
## resume
|
||||
+ `id`
|
||||
+ `resume_dir`=.marqdo/agent-resume
|
||||
|
||||
Author entry: load checkpoint for continuation. Returns the checkpoint map (`found`/`round`/`workbook`/…). Callers continue with `plan`/`run` using saved fields; clear when done.
|
||||
|
||||
*> resume_load id=`id` resume_dir=`resume_dir`*
|
||||
|
||||
---
|
||||
|
||||
## extract_call_args
|
||||
+ `reply`=None
|
||||
|
||||
@@ -1264,7 +1417,7 @@ Parent Plan-and-Move prompt. Short protocol only — no long monologues.
|
||||
+ `events`
|
||||
+ `max_acts`=6
|
||||
|
||||
LLM Plan → CALL/READ/DECISION loop. Returns `{decision,reply,observation,events}`.
|
||||
LLM Plan → CALL/READ/DECISION loop. Returns `{decision,reply,observation,events,llm_calls,tokens}`.
|
||||
|
||||
**model = > json.get value=`agent` key="model"**
|
||||
**last_obs = observation**
|
||||
@@ -1272,6 +1425,8 @@ LLM Plan → CALL/READ/DECISION loop. Returns `{decision,reply,observation,event
|
||||
**decision = None**
|
||||
**last_reply = None**
|
||||
**evs_all = events**
|
||||
**llm_calls = 0**
|
||||
**tokens = 0**
|
||||
|
||||
- `left` > 0
|
||||
1. `decision`
|
||||
@@ -1279,11 +1434,17 @@ LLM Plan → CALL/READ/DECISION loop. Returns `{decision,reply,observation,event
|
||||
2. *
|
||||
**ctx = > build_plan_context agent=`agent` goal=`goal` observation=`last_obs` explore_attempt=`explore_attempt` explore_n=`explore_n` phase=`phase`**
|
||||
1. `stream`
|
||||
**evs = > model.complete prompt=`ctx` stream=True echo=`echo`**
|
||||
**last_reply = > llm.stream_result events=`evs`**
|
||||
**evs = > model.stream prompt=`ctx` echo=`echo`**
|
||||
**last_reply = > llm.collect events=`evs`**
|
||||
**evs_all = > plan_merge_deltas events=`evs_all` from=`evs` stream=`stream`**
|
||||
**llm_calls = llm_calls + 1**
|
||||
2. *
|
||||
**last_reply = > model.complete prompt=`ctx`**
|
||||
**turn = > model_turn model=`model` prompt=`ctx` stream=False echo=`echo`**
|
||||
**last_reply = > json.get value=`turn` key="text"**
|
||||
**c = > json.get value=`turn` key="llm_calls"**
|
||||
**t = > json.get value=`turn` key="tokens"**
|
||||
**llm_calls = llm_calls + c**
|
||||
**tokens = tokens + t**
|
||||
**last_reply = > trim value=`last_reply`**
|
||||
**act = > extract_plan_act reply=`last_reply`**
|
||||
**kind = > json.get value=`act` key="kind"**
|
||||
@@ -1314,6 +1475,8 @@ LLM Plan → CALL/READ/DECISION loop. Returns `{decision,reply,observation,event
|
||||
**out = > json.set map=`out` key="reply" value=`last_reply`**
|
||||
**out = > json.set map=`out` key="observation" value=`last_obs`**
|
||||
**out = > json.set map=`out` key="events" value=`evs_all`**
|
||||
**out = > json.set map=`out` key="llm_calls" value=`llm_calls`**
|
||||
**out = > json.set map=`out` key="tokens" value=`tokens`**
|
||||
*out*
|
||||
|
||||
---
|
||||
@@ -1515,6 +1678,316 @@ Clear the plugin session bag for this agent id.
|
||||
> agent_history_clear id=`id`
|
||||
****
|
||||
|
||||
## resolve
|
||||
+ `task`
|
||||
+ `kb_dir`=.marqdo/agent-kb
|
||||
+ `near_match`=True
|
||||
+ `near_threshold`=0.78
|
||||
|
||||
Skill resolver (v2): exact → alias → canonical → near. Returns `mode` / `resource` / `llm_free` / `confidence`. Plugin ABI — no core host_* calls.
|
||||
|
||||
**tools = > json.get value=`self` key="tools"**
|
||||
*> agent_resolve task=`task` kb_dir=`kb_dir` near_match=`near_match` near_threshold=`near_threshold` tools=`tools`*
|
||||
|
||||
## route
|
||||
+ `task`
|
||||
+ `backend`=auto
|
||||
+ `kb_dir`=.marqdo/agent-kb
|
||||
+ `memory_dir`=.marqdo/agent-memory
|
||||
+ `threshold`=0.78
|
||||
|
||||
Adaptive Routing (Phase 4): abstract router — `marqdo` | `small-llm` | `llm` | `auto`. `jev` is accepted as a **compat alias** of `small-llm` (not a first-class backend). `auto` cascades policy → kb → small-llm request. May return `needs_llm=True` + `prompt` for the caller to complete with `router_model` (any llm handle), then `route_apply`.
|
||||
|
||||
**tools = > json.get value=`self` key="tools"**
|
||||
*> agent_route task=`task` backend=`backend` kb_dir=`kb_dir` memory_dir=`memory_dir` threshold=`threshold` tools=`tools`*
|
||||
|
||||
## route_apply
|
||||
+ `task`
|
||||
+ `reply`
|
||||
+ `kb_dir`=.marqdo/agent-kb
|
||||
+ `backend`=small-llm
|
||||
|
||||
Apply a router model line (`SKILL:<slug>` / `EXPLORE`) to a kb skill hit.
|
||||
|
||||
*> agent_route_apply task=`task` reply=`reply` kb_dir=`kb_dir` backend=`backend`*
|
||||
|
||||
## compile_policy
|
||||
+ `memory_dir`=.marqdo/agent-memory
|
||||
+ `min_evidence`=3
|
||||
+ `force`=False
|
||||
|
||||
Policy Compilation: stable episode skill selections → `policies/router.mq.md` (deterministic Marqdo rules).
|
||||
|
||||
*> agent_compile_policy memory_dir=`memory_dir` min_evidence=`min_evidence` force=`force`*
|
||||
|
||||
## maybe_compile_policy
|
||||
+ `memory_dir`=.marqdo/agent-memory
|
||||
+ `min_evidence`=3
|
||||
|
||||
Learn gate for the Adaptive Router policy layer.
|
||||
|
||||
*> agent_maybe_compile_policy memory_dir=`memory_dir` min_evidence=`min_evidence`*
|
||||
|
||||
## record_episode
|
||||
+ `task`
|
||||
+ `status`=ok
|
||||
+ `result`=None
|
||||
+ `mode`=explore
|
||||
+ `skill`=none
|
||||
+ `observation`=None
|
||||
+ `workbook`=None
|
||||
+ `cache`=None
|
||||
+ `match`=None
|
||||
+ `llm_calls`=0
|
||||
+ `tool_calls`=0
|
||||
+ `error`=None
|
||||
+ `memory_dir`=.marqdo/agent-memory
|
||||
|
||||
Persist one run as `.mq.md` under `memory_dir/episodes/YYYY/MM/` (v2 Phase 1). Memory is executable documentation, not a vector bag.
|
||||
|
||||
*> agent_record_episode task=`task` status=`status` result=`result` mode=`mode` skill=`skill` observation=`observation` workbook=`workbook` cache=`cache` match=`match` llm_calls=`llm_calls` tool_calls=`tool_calls` error=`error` memory_dir=`memory_dir`*
|
||||
|
||||
## maybe_learn
|
||||
+ `task`
|
||||
+ `kb_dir`=.marqdo/agent-kb
|
||||
+ `memory_dir`=.marqdo/agent-memory
|
||||
+ `improve_every`=3
|
||||
+ `promote`=True
|
||||
|
||||
Offline learn gate: when ≥ `improve_every` successful episodes share a task hash, generalize → synthesize → compile → optional OKF promote (`llm_free`). Also attempts Policy Compilation for Adaptive Routing.
|
||||
|
||||
**skill_learn = > agent_maybe_learn task=`task` kb_dir=`kb_dir` memory_dir=`memory_dir` improve_every=`improve_every` promote=`promote`**
|
||||
**policy_learn = > agent_maybe_compile_policy memory_dir=`memory_dir` min_evidence=`improve_every`**
|
||||
**out = > json.parse text={"learned":false}**
|
||||
**sl = > json.get value=`skill_learn` key="learned"**
|
||||
**out = > json.set map=`out` key="learned" value=`sl`**
|
||||
**out = > json.set map=`out` key="skill" value=`skill_learn`**
|
||||
**out = > json.set map=`out` key="policy" value=`policy_learn`**
|
||||
*out*
|
||||
|
||||
## compile
|
||||
+ `task`
|
||||
+ `kb_dir`=.marqdo/agent-kb
|
||||
+ `memory_dir`=.marqdo/agent-memory
|
||||
+ `min_evidence`=3
|
||||
+ `promote`=True
|
||||
+ `force`=False
|
||||
|
||||
Compile a candidate skill into versioned `skills/<slug>/vN.mq.md` and optionally promote into agent-kb.
|
||||
|
||||
*> agent_compile_skill task=`task` kb_dir=`kb_dir` memory_dir=`memory_dir` min_evidence=`min_evidence` promote=`promote` force=`force`*
|
||||
|
||||
## metrics
|
||||
+ `memory_dir`=.marqdo/agent-memory
|
||||
|
||||
Reasoning amortization stats over recorded episodes (`avg_llm_calls`, `compiled_runs`, …).
|
||||
|
||||
*> agent_metrics memory_dir=`memory_dir`*
|
||||
|
||||
## run
|
||||
+ `task`
|
||||
+ `kb_dir`=.marqdo/agent-kb
|
||||
+ `memory_dir`=.marqdo/agent-memory
|
||||
+ `learn`=True
|
||||
+ `verify`=True
|
||||
+ `writeback`=True
|
||||
+ `reuse`=True
|
||||
+ `near_match`=True
|
||||
+ `near_threshold`=0.78
|
||||
+ `promote`=True
|
||||
+ `improve_every`=3
|
||||
+ `explore_n`=3
|
||||
+ `max_rounds`=4
|
||||
+ `stream`=False
|
||||
+ `echo`=False
|
||||
+ `trace`=False
|
||||
+ `force`=False
|
||||
+ `router`=auto
|
||||
+ `router_model`=None
|
||||
+ `resume_id`=None
|
||||
+ `resume_dir`=.marqdo/agent-resume
|
||||
|
||||
Primary author entry (v2 + P4): Adaptive `route` → execute (policy/kb skill) or `plan` → record episode → maybe_learn (skill + policy). `router` = `auto`|`marqdo`|`small-llm`|`llm` (`jev`→`small-llm` alias). Optional `router_model` (llm handle) answers `needs_llm` prompts — small models welcome.
|
||||
|
||||
**routed = > `self`.route task=`task` backend=`router` kb_dir=`kb_dir` memory_dir=`memory_dir` threshold=`near_threshold`**
|
||||
**route_matched = > json.get value=`routed` key="matched"**
|
||||
**needs_llm = > json.get value=`routed` key="needs_llm"**
|
||||
**exec_mode = "explore"**
|
||||
**llm_calls = 0**
|
||||
**tokens = 0**
|
||||
**out = None**
|
||||
**route_backend = > json.get value=`routed` key="backend"**
|
||||
|
||||
1. `route_matched`
|
||||
**path = > json.get value=`routed` key="resource"**
|
||||
1. `path`
|
||||
**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=`task` improve_every=`improve_every`
|
||||
**lf = > json.get value=`routed` key="llm_free"**
|
||||
1. `lf`
|
||||
**exec_mode = "compiled"**
|
||||
2. *
|
||||
**exec_mode = "routed"**
|
||||
**out = > json.parse text={"status":"ok"}**
|
||||
**out = > json.set map=`out` key="status" value="ok"**
|
||||
**out = > json.set map=`out` key="goal" value=`task`**
|
||||
**out = > json.set map=`out` key="result" value=`child_val`**
|
||||
**out = > json.set map=`out` key="workbook" value=`path`**
|
||||
**out = > json.set map=`out` key="cache" value="hit"**
|
||||
**rmode = > json.get value=`routed` key="mode"**
|
||||
**out = > json.set map=`out` key="match" value=`rmode`**
|
||||
**sk = > json.get value=`routed` key="skill"**
|
||||
**out = > json.set map=`out` key="skill" value=`sk`**
|
||||
**out = > json.set map=`out` key="observation" value=`last_obs`**
|
||||
**out = > json.set map=`out` key="summary" value="adaptive route hit"**
|
||||
**out = > json.set map=`out` key="route" value=`routed`**
|
||||
**llm_calls = 0**
|
||||
2. *
|
||||
**route_matched = False**
|
||||
2. *
|
||||
**route_matched = False**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `route_matched`
|
||||
**_ = 1**
|
||||
2. `needs_llm`
|
||||
1. `router_model`
|
||||
**prompt = > json.get value=`routed` key="prompt"**
|
||||
**turn = > model_turn model=`router_model` prompt=`prompt` stream=`stream` echo=`echo`**
|
||||
**reply = > json.get value=`turn` key="text"**
|
||||
**llm_calls = > json.get value=`turn` key="llm_calls"**
|
||||
**tokens = > json.get value=`turn` key="tokens"**
|
||||
**applied = > `self`.route_apply task=`task` reply=`reply` kb_dir=`kb_dir` backend=`router`**
|
||||
**am = > json.get value=`applied` key="matched"**
|
||||
1. `am`
|
||||
**path = > json.get value=`applied` 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
|
||||
**exec_mode = "routed"**
|
||||
**out = > json.parse text={"status":"ok"}**
|
||||
**out = > json.set map=`out` key="status" value="ok"**
|
||||
**out = > json.set map=`out` key="goal" value=`task`**
|
||||
**out = > json.set map=`out` key="result" value=`child_val`**
|
||||
**out = > json.set map=`out` key="workbook" value=`path`**
|
||||
**out = > json.set map=`out` key="cache" value="soft-hit"**
|
||||
**out = > json.set map=`out` key="match" value="routed"**
|
||||
**sk = > json.get value=`applied` key="skill"**
|
||||
**out = > json.set map=`out` key="skill" value=`sk`**
|
||||
**out = > json.set map=`out` key="observation" value=`last_obs`**
|
||||
**out = > json.set map=`out` key="summary" value="router model selected skill"**
|
||||
**out = > json.set map=`out` key="route" value=`applied`**
|
||||
**route_matched = True**
|
||||
2. *
|
||||
**route_matched = False**
|
||||
2. *
|
||||
**route_matched = False**
|
||||
2. *
|
||||
**route_matched = False**
|
||||
3. *
|
||||
**_ = 1**
|
||||
|
||||
1. `route_matched`
|
||||
**_ = 1**
|
||||
2. *
|
||||
**resolved = > `self`.resolve task=`task` kb_dir=`kb_dir` near_match=`near_match` near_threshold=`near_threshold`**
|
||||
**mode = > json.get value=`resolved` key="mode"**
|
||||
**llm_free = > json.get value=`resolved` key="llm_free"**
|
||||
1. `llm_free`
|
||||
**path = > json.get value=`resolved` 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=`task` improve_every=`improve_every`
|
||||
**exec_mode = "compiled"**
|
||||
**out = > json.parse text={"status":"ok"}**
|
||||
**out = > json.set map=`out` key="status" value="ok"**
|
||||
**out = > json.set map=`out` key="goal" value=`task`**
|
||||
**out = > json.set map=`out` key="result" value=`child_val`**
|
||||
**out = > json.set map=`out` key="workbook" value=`path`**
|
||||
**out = > json.set map=`out` key="cache" value="hit"**
|
||||
**out = > json.set map=`out` key="match" value=`mode`**
|
||||
**sk = > json.get value=`resolved` key="skill"**
|
||||
**out = > json.set map=`out` key="skill" value=`sk`**
|
||||
**out = > json.set map=`out` key="observation" value=`last_obs`**
|
||||
**out = > json.set map=`out` key="summary" value="compiled skill (llm_free)"**
|
||||
**llm_calls = 0**
|
||||
2. *
|
||||
**out = > `self`.plan goal=`task` kb_dir=`kb_dir` writeback=`writeback` reuse=`reuse` near_match=`near_match` near_threshold=`near_threshold` promote=`promote` improve_every=`improve_every` explore_n=`explore_n` max_rounds=`max_rounds` stream=`stream` echo=`echo` trace=`trace` force=`force`**
|
||||
**exec_mode = "explore"**
|
||||
**llm_calls = 1**
|
||||
2. *
|
||||
**out = > `self`.plan goal=`task` kb_dir=`kb_dir` writeback=`writeback` reuse=`reuse` near_match=`near_match` near_threshold=`near_threshold` promote=`promote` improve_every=`improve_every` explore_n=`explore_n` max_rounds=`max_rounds` stream=`stream` echo=`echo` trace=`trace` force=`force`**
|
||||
**cache = > json.get value=`out` key="cache"**
|
||||
1. `cache` == hit
|
||||
**exec_mode = "compiled"**
|
||||
**llm_calls = 0**
|
||||
2. `cache` == soft-hit
|
||||
**exec_mode = "near"**
|
||||
**llm_calls = 0**
|
||||
3. *
|
||||
**exec_mode = "explore"**
|
||||
**llm_calls = 1**
|
||||
|
||||
**st = > json.get value=`out` key="status"**
|
||||
**res = > json.get value=`out` key="result"**
|
||||
**wb_path = > json.get value=`out` key="workbook"**
|
||||
**obs = > json.get value=`out` key="observation"**
|
||||
**sk2 = > json.get value=`out` key="skill"**
|
||||
**cache2 = > json.get value=`out` key="cache"**
|
||||
**match2 = > json.get value=`out` key="match"**
|
||||
**err = > json.get value=`out` key="error"**
|
||||
|
||||
1. `verify`
|
||||
1. `st` == "ok"
|
||||
**verification = "status: passed"**
|
||||
2. *
|
||||
**verification = "status: failed"**
|
||||
2. *
|
||||
**verification = "status: skipped"**
|
||||
|
||||
**ep = > `self`.record_episode task=`task` status=`st` result=`res` mode=`exec_mode` skill=`sk2` observation=`obs` workbook=`wb_path` cache=`cache2` match=`match2` llm_calls=`llm_calls` tokens=`tokens` error=`err` memory_dir=`memory_dir`**
|
||||
|
||||
**learning = None**
|
||||
1. `learn`
|
||||
1. `st` == "ok"
|
||||
**learning = > `self`.maybe_learn task=`task` kb_dir=`kb_dir` memory_dir=`memory_dir` improve_every=`improve_every` promote=`promote`**
|
||||
2. *
|
||||
**learning = None**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
**execution = > json.parse text={"mode":"explore","llm_calls":0,"tokens":0}**
|
||||
**execution = > json.set map=`execution` key="mode" value=`exec_mode`**
|
||||
**execution = > json.set map=`execution` key="llm_calls" value=`llm_calls`**
|
||||
**execution = > json.set map=`execution` key="tokens" value=`tokens`**
|
||||
**execution = > json.set map=`execution` key="skill" value=`sk2`**
|
||||
**execution = > json.set map=`execution` key="router" value=`route_backend`**
|
||||
**out = > json.set map=`out` key="execution" value=`execution`**
|
||||
**out = > json.set map=`out` key="episode" value=`ep`**
|
||||
**out = > json.set map=`out` key="route" value=`routed`**
|
||||
1. `learning`
|
||||
**out = > json.set map=`out` key="learning" value=`learning`**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `resume_id`
|
||||
**_ckpt = > resume_save id=`resume_id` goal=`task` task=`task` status=`st` round=1 workbook=`wb_path` result=`res` route=`routed` resume_dir=`resume_dir`**
|
||||
**out = > json.set map=`out` key="resume" value=`_ckpt`**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
*out*
|
||||
|
||||
## step
|
||||
+ `task`
|
||||
+ `writeback`=True
|
||||
@@ -1539,17 +2012,20 @@ With `stream=True`, the model call uses SSE; `echo=True` prints delta text to st
|
||||
**reads_left = max_reads**
|
||||
**reply = None**
|
||||
**decision = None**
|
||||
**llm_calls = 0**
|
||||
**tokens = 0**
|
||||
|
||||
- `reads_left` > 0
|
||||
1. `decision`
|
||||
**reads_left = 0**
|
||||
2. *
|
||||
**ctx = > build_step_context agent=`self` task=`task` source_depth=`source_depth` skill_depth=`skill_depth`**
|
||||
1. `stream`
|
||||
**evs = > model.complete prompt=`ctx` stream=True echo=`echo`**
|
||||
**reply = > llm.stream_result events=`evs`**
|
||||
2. *
|
||||
**reply = > model.complete prompt=`ctx`**
|
||||
**turn = > model_turn model=`model` prompt=`ctx` stream=`stream` echo=`echo`**
|
||||
**reply = > json.get value=`turn` key="text"**
|
||||
**c = > json.get value=`turn` key="llm_calls"**
|
||||
**t = > json.get value=`turn` key="tokens"**
|
||||
**llm_calls = llm_calls + c**
|
||||
**tokens = tokens + t**
|
||||
**reply = > trim value=`reply`**
|
||||
**rk = > extract_plan_read reply=`reply`**
|
||||
1. `rk` == source
|
||||
@@ -1578,11 +2054,12 @@ With `stream=True`, the model call uses SSE; `echo=True` prints delta text to st
|
||||
**task_s = > json.stringify value=`task`**
|
||||
**fp = "Tool CALL was rejected (not on the tools whitelist). Do NOT emit CALL. Answer the user task directly with the final answer only.\n\nUser task:\n"**
|
||||
**fp = fp + task_s**
|
||||
1. `stream`
|
||||
**evs_retry = > model.complete prompt=`fp` stream=True echo=`echo`**
|
||||
**reply = > llm.stream_result events=`evs_retry`**
|
||||
2. *
|
||||
**reply = > model.complete prompt=`fp`**
|
||||
**turn2 = > model_turn model=`model` prompt=`fp` stream=`stream` echo=`echo`**
|
||||
**reply = > json.get value=`turn2` key="text"**
|
||||
**c = > json.get value=`turn2` key="llm_calls"**
|
||||
**t = > json.get value=`turn2` key="tokens"**
|
||||
**llm_calls = llm_calls + c**
|
||||
**tokens = tokens + t**
|
||||
**out = > json.set map=`out` key="status" value="ok"**
|
||||
**out = > json.set map=`out` key="decision" value=`decision`**
|
||||
**out = > json.set map=`out` key="denied_tool" value=`tool_name`**
|
||||
@@ -1596,17 +2073,21 @@ With `stream=True`, the model call uses SSE; `echo=True` prints delta text to st
|
||||
**fp = fp + "ran via subtask and returned:"**
|
||||
**fp = fp + tool_s**
|
||||
**fp = fp + ". Reply to the user briefly."**
|
||||
1. `stream`
|
||||
**evs2 = > model.complete prompt=`fp` stream=True echo=`echo`**
|
||||
**reply = > llm.stream_result events=`evs2`**
|
||||
2. *
|
||||
**reply = > model.complete prompt=`fp`**
|
||||
**turn2 = > model_turn model=`model` prompt=`fp` stream=`stream` echo=`echo`**
|
||||
**reply = > json.get value=`turn2` key="text"**
|
||||
**c = > json.get value=`turn2` key="llm_calls"**
|
||||
**t = > json.get value=`turn2` key="tokens"**
|
||||
**llm_calls = llm_calls + c**
|
||||
**tokens = tokens + t**
|
||||
**out = > json.set map=`out` key="tool" value=`tool_name`**
|
||||
**out = > json.set map=`out` key="tool_result" value=`tool_s`**
|
||||
**out = > json.set map=`out` key="result" value=`reply`**
|
||||
2. *
|
||||
**out = > json.set map=`out` key="result" value=`reply`**
|
||||
|
||||
**out = > json.set map=`out` key="llm_calls" value=`llm_calls`**
|
||||
**out = > json.set map=`out` key="tokens" value=`tokens`**
|
||||
|
||||
**as_turn = > json.parse text={"role":"assistant"}**
|
||||
**as_turn = > json.set map=`as_turn` key="content" value=`reply`**
|
||||
> agent_history_append id=`id` item=`as_turn`
|
||||
@@ -1644,6 +2125,8 @@ With `stream=True`, the model call uses SSE; `echo=True` prints delta text to st
|
||||
+ `stream`=False
|
||||
+ `echo`=False
|
||||
+ `trace`=False
|
||||
+ `resume_id`=None
|
||||
+ `resume_dir`=.marqdo/agent-resume
|
||||
|
||||
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`.
|
||||
|
||||
@@ -1799,11 +2282,12 @@ Reuse lookup: exact → alias → canonicalize → optional local n-gram `near`
|
||||
**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`**
|
||||
**evs = > model.stream prompt=`soft_prompt` echo=`echo`**
|
||||
**soft_reply = > llm.collect events=`evs`**
|
||||
**events = > plan_merge_deltas events=`events` from=`evs` stream=`stream`**
|
||||
2. *
|
||||
**soft_reply = > model.complete prompt=`soft_prompt`**
|
||||
**soft_turn = > model_turn model=`model` prompt=`soft_prompt` stream=False echo=`echo`**
|
||||
**soft_reply = > json.get value=`soft_turn` key="text"**
|
||||
**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`**
|
||||
@@ -1898,18 +2382,28 @@ Reuse lookup: exact → alias → canonicalize → optional local n-gram `near`
|
||||
**b = > json.get value=`parts` key="b"**
|
||||
**c = > json.get value=`parts` key="c"**
|
||||
**path = workbook_dir + a + slug + b + ts + c**
|
||||
> fs.write_text path=`path` text=`skel`
|
||||
2. `explore`
|
||||
**parts = > json.parse text={"a":"/explore/","b":"/","c":".mq.md"}**
|
||||
**a = > json.get value=`parts` key="a"**
|
||||
**b = > json.get value=`parts` key="b"**
|
||||
**c = > json.get value=`parts` key="c"**
|
||||
**path = kb_dir + a + slug + b + explore_attempt + c**
|
||||
**ex = > fs.exists path=`path`**
|
||||
1. `ex`
|
||||
**_ = 1**
|
||||
2. *
|
||||
> fs.write_text path=`path` text=`skel`
|
||||
3. *
|
||||
**parts = > json.parse text={"a":"/resources/","b":".mq.md"}**
|
||||
**a = > json.get value=`parts` key="a"**
|
||||
**b = > json.get value=`parts` key="b"**
|
||||
**path = kb_dir + a + slug + b**
|
||||
> fs.write_text path=`path` text=`skel`
|
||||
**ex = > fs.exists path=`path`**
|
||||
1. `ex`
|
||||
**_ = 1**
|
||||
2. *
|
||||
> fs.write_text path=`path` text=`skel`
|
||||
|
||||
1. `confirm`
|
||||
**out = > json.parse text={"status":"pending"}**
|
||||
|
||||
+295
-68
@@ -1,23 +1,26 @@
|
||||
---
|
||||
title: ext/ai/llm
|
||||
description: OpenAI-compatible LLM object (English). Import ext/ai/llm.mq.md.
|
||||
description: >-
|
||||
Marqdo LLM intelligence primitive — ask / stream / collect / named handles.
|
||||
OpenAI-compatible and Ollama HTTP stay behind the semantic surface.
|
||||
import sys:lib/sys.mq.md
|
||||
import net:lib/net.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import table:lib/table.mq.md
|
||||
import fs:lib/fs.mq.md
|
||||
import text:lib/text.mq.md
|
||||
import openai:ext/ai/llm/openai.mq.md
|
||||
---
|
||||
|
||||
## load_env
|
||||
+ `path`=None
|
||||
|
||||
Load `.env` from cwd (optional named arg `path=`). Does not override existing variables.
|
||||
Load `.env` from cwd (optional `path=`). Does not override existing variables.
|
||||
|
||||
*> sys.load_dotenv path=`path`*
|
||||
|
||||
## stream_result
|
||||
## collect
|
||||
+ `events`
|
||||
|
||||
Reduce a `complete stream=True` event list to the final `done.result` text (or `error.message`). Unknown types (e.g. `reasoning`) are ignored.
|
||||
Collapse a stream event list to final answer text (`done.result` or `error.message`). Prefer this over raw SSE fields. Alias: `stream_result`.
|
||||
|
||||
**answer = ""**
|
||||
|
||||
@@ -28,97 +31,321 @@ Reduce a `complete stream=True` event list to the final `done.result` text (or `
|
||||
2. `t` == "error"
|
||||
**answer = [message](ev)**
|
||||
3. `t` == "delta"
|
||||
4. *
|
||||
4. `t` == "text"
|
||||
**chunk = [text](ev)**
|
||||
1. `chunk`
|
||||
**answer = answer + chunk**
|
||||
2. *
|
||||
**_ = 1**
|
||||
5. *
|
||||
**_ = 1**
|
||||
|
||||
*answer*
|
||||
|
||||
# llm
|
||||
## stream_result
|
||||
+ `events`
|
||||
|
||||
Construct an LLM handle from `OPENAI_*` / `MARQDO_LLM_*`.
|
||||
Compatibility alias for `collect`.
|
||||
|
||||
**api_key = > sys.env_get name="OPENAI_API_KEY"**
|
||||
1. not `api_key`
|
||||
**api_key = > sys.env_get name="MARQDO_LLM_API_KEY"**
|
||||
*> collect events=`events`*
|
||||
|
||||
1. not `api_key`
|
||||
> print text=ext/ai/llm: set OPENAI_API_KEY or MARQDO_LLM_API_KEY
|
||||
## usage_from
|
||||
+ `data`
|
||||
|
||||
Extract `{prompt_tokens,completion_tokens,total_tokens}` from an OpenAI-shaped response map (or empty zeros).
|
||||
|
||||
*> openai.usage_from data=`data`*
|
||||
|
||||
## create
|
||||
+ `model`=None
|
||||
+ `base_url`=None
|
||||
+ `api_key`=None
|
||||
+ `backend`=openai-compatible
|
||||
+ `name`=None
|
||||
|
||||
Factory for an LLM handle (same as constructing `# llm`). Env fallbacks apply inside the constructor. Optional `name=` tags the handle (`fast` / `reasoning`).
|
||||
|
||||
**h = > llm model=`model` base_url=`base_url` api_key=`api_key` backend=`backend` name=`name`**
|
||||
*h*
|
||||
|
||||
## fast
|
||||
+ `model`=None
|
||||
+ `base_url`=None
|
||||
+ `api_key`=None
|
||||
|
||||
Named small/fast handle. Model from arg, else `MARQDO_LLM_FAST_MODEL`, else default create model.
|
||||
|
||||
1. `model`
|
||||
**mdl = model**
|
||||
2. *
|
||||
**mdl = > sys.env_get name="MARQDO_LLM_FAST_MODEL"**
|
||||
|
||||
*> create model=`mdl` base_url=`base_url` api_key=`api_key` name="fast" backend="openai-compatible"*
|
||||
|
||||
## reasoning
|
||||
+ `model`=None
|
||||
+ `base_url`=None
|
||||
+ `api_key`=None
|
||||
|
||||
Named strong/reasoning handle. Model from arg, else `MARQDO_LLM_REASONING_MODEL`, else default create model.
|
||||
|
||||
1. `model`
|
||||
**mdl = model**
|
||||
2. *
|
||||
**mdl = > sys.env_get name="MARQDO_LLM_REASONING_MODEL"**
|
||||
|
||||
*> create model=`mdl` base_url=`base_url` api_key=`api_key` name="reasoning" backend="openai-compatible"*
|
||||
|
||||
## prompt_body
|
||||
+ `src`
|
||||
|
||||
If `src` starts with YAML frontmatter declaring `type: prompt` / `类型: 提示`, return the body after the closing `---`; otherwise return `src` unchanged.
|
||||
|
||||
**sw = > text.starts_with text=`src` prefix="---"**
|
||||
1. not `sw`
|
||||
*src*
|
||||
2. *
|
||||
**parts = > split value=`src` sep="\n---\n"**
|
||||
**n = > len value=`parts`**
|
||||
1. `n` < 2
|
||||
*src*
|
||||
2. *
|
||||
**fm = > at value=`parts` index=0**
|
||||
**is_p = > text.contains text=`fm` sub="type: prompt"**
|
||||
1. not `is_p`
|
||||
**is_p = > text.contains text=`fm` sub="类型: 提示"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. `is_p`
|
||||
**body = > at value=`parts` index=1**
|
||||
*> text.str_trim s=`body`*
|
||||
2. *
|
||||
*src*
|
||||
|
||||
## prompt_load
|
||||
+ `path`
|
||||
|
||||
Load a prompt artifact (`.mq.md` / `.md`) from disk for `ask`.
|
||||
|
||||
**raw = > fs.read_text path=`path`**
|
||||
*> prompt_body src=`raw`*
|
||||
|
||||
## prompt
|
||||
+ `path`=None
|
||||
+ `text`=None
|
||||
|
||||
Prompt artifact helper: `path=` loads a file; `text=` strips `type: prompt` frontmatter from an in-memory string.
|
||||
|
||||
1. `path`
|
||||
*> prompt_load path=`path`*
|
||||
2. `text`
|
||||
*> prompt_body src=`text`*
|
||||
3. *
|
||||
> print text=ext/ai/llm.prompt: pass path= or text=
|
||||
> sys.exit code=1
|
||||
|
||||
**base_url = > sys.env_get name="OPENAI_BASE_URL"**
|
||||
1. not `base_url`
|
||||
**base_url = > sys.env_get name="MARQDO_LLM_BASE_URL"**
|
||||
1. not `base_url`
|
||||
**base_url = "https://api.openai.com/v1"**
|
||||
## ask
|
||||
+ `prompt`=None
|
||||
+ `path`=None
|
||||
+ `model`=None
|
||||
+ `base_url`=None
|
||||
+ `api_key`=None
|
||||
|
||||
**model = > sys.env_get name="OPENAI_MODEL"**
|
||||
1. not `model`
|
||||
**model = > sys.env_get name="MARQDO_LLM_MODEL"**
|
||||
1. not `model`
|
||||
**model = "gpt-4o-mini"**
|
||||
Module convenience: create a default handle and return **answer text**. Pass `path=` to load a prompt document, `prompt=` text, or omit both to use the entry `.mq.md` as a prompt artifact (`type: prompt` body via `sys.module_source`).
|
||||
|
||||
1. `path`
|
||||
**p = > prompt_load path=`path`**
|
||||
2. `prompt`
|
||||
**p = prompt**
|
||||
3. *
|
||||
**raw = > sys.module_source**
|
||||
1. not `raw`
|
||||
> print text=ext/ai/llm.ask: pass prompt= or path= (or run a prompt document as entry)
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
**p = > prompt_body src=`raw`**
|
||||
|
||||
**m = > create model=`model` base_url=`base_url` api_key=`api_key`**
|
||||
**r = > m.ask prompt=`p`**
|
||||
*[text](r)*
|
||||
|
||||
# llm
|
||||
+ `model`=None
|
||||
+ `base_url`=None
|
||||
+ `api_key`=None
|
||||
+ `backend`=openai-compatible
|
||||
+ `name`=None
|
||||
|
||||
LLM handle — intelligence primitive, not an HTTP client.
|
||||
|
||||
Resolution: explicit args → entry-scope / Artifact Metadata (`sys.meta_get`) → `sys.env_get` → defaults.
|
||||
Prefer declaring `model` / `api_key` / `base_url` in frontmatter Binding, then use the lifted names.
|
||||
|
||||
1. `api_key`
|
||||
**key = api_key**
|
||||
2. *
|
||||
**key = > sys.meta_get key="api_key"**
|
||||
1. not `key`
|
||||
**key = > sys.env_get name="OPENAI_API_KEY"**
|
||||
1. not `key`
|
||||
**key = > sys.env_get name="MARQDO_LLM_API_KEY"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `backend` == ollama
|
||||
1. not `key`
|
||||
**key = "ollama"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
2. not `key`
|
||||
> print text=ext/ai/llm: set OPENAI_API_KEY or MARQDO_LLM_API_KEY (or pass api_key= / metadata api_key)
|
||||
> sys.exit code=1
|
||||
3. *
|
||||
**_ = 1**
|
||||
|
||||
1. `base_url`
|
||||
**url = base_url**
|
||||
2. *
|
||||
**url = > sys.meta_get key="base_url"**
|
||||
1. `url`
|
||||
**_ = 1**
|
||||
2. `backend` == ollama
|
||||
**url = > sys.env_get name="OLLAMA_HOST"**
|
||||
1. not `url`
|
||||
**url = "http://127.0.0.1:11434/v1"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
3. *
|
||||
**url = > sys.env_get name="OPENAI_BASE_URL"**
|
||||
1. not `url`
|
||||
**url = > sys.env_get name="MARQDO_LLM_BASE_URL"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. not `url`
|
||||
**url = "https://api.openai.com/v1"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `model`
|
||||
**mdl = model**
|
||||
2. *
|
||||
**mdl = > sys.meta_get key="model"**
|
||||
1. `mdl`
|
||||
**_ = 1**
|
||||
2. *
|
||||
**mdl = > sys.env_get name="OPENAI_MODEL"**
|
||||
1. not `mdl`
|
||||
**mdl = > sys.env_get name="MARQDO_LLM_MODEL"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. not `mdl`
|
||||
1. `backend` == ollama
|
||||
**mdl = "llama3.2"**
|
||||
2. *
|
||||
**mdl = "gpt-4o-mini"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `name`
|
||||
**nm = name**
|
||||
2. *
|
||||
**nm = None**
|
||||
|
||||
`h` =
|
||||
|
||||
| api_key | base_url | model | suffix | bearer |
|
||||
|---------|----------|-------|--------|--------|
|
||||
| `api_key` | `base_url` | `model` | /chat/completions | "Bearer " |
|
||||
| _type | backend | api_key | base_url | model | suffix | bearer | name |
|
||||
|-------|---------|---------|----------|-------|--------|--------|------|
|
||||
| llm | `backend` | `key` | `url` | `mdl` | /chat/completions | "Bearer " | `nm` |
|
||||
|
||||
*h*
|
||||
|
||||
## ask
|
||||
+ `prompt`=None
|
||||
+ `path`=None
|
||||
|
||||
Semantic ask → **LLMResult** (`text`, `model`, `finish`, `backend`, `usage`, `name`, `tool_calls`). Prefer `[text](result)` for the answer string. Omit `prompt=` / `path=` to use the entry document (`sys.module_source` + `prompt_body`).
|
||||
|
||||
1. `path`
|
||||
**p = > prompt_load path=`path`**
|
||||
2. `prompt`
|
||||
**p = prompt**
|
||||
3. *
|
||||
**raw = > sys.module_source**
|
||||
1. not `raw`
|
||||
> print text=ext/ai/llm: ask needs prompt= or path= (or entry prompt document)
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
**p = > prompt_body src=`raw`**
|
||||
|
||||
**pack = > self.complete_result prompt=`p` stream=False echo=False**
|
||||
**text = > json.get value=`pack` key="text"**
|
||||
**usage = > json.get value=`pack` key="usage"**
|
||||
**finish = > json.get value=`pack` key="finish"**
|
||||
**tc = > json.get value=`pack` key="tool_calls"**
|
||||
1. not `tc`
|
||||
**tc = > json.parse text=[]**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
`result` =
|
||||
|
||||
| text | model | finish | backend | usage | name | llm_calls | tokens | tool_calls |
|
||||
|------|-------|--------|---------|-------|------|-----------|--------|------------|
|
||||
| `text` | [model](self) | `finish` | [backend](self) | `usage` | [name](self) | 1 | [total_tokens](usage) | `tc` |
|
||||
|
||||
*result*
|
||||
|
||||
## stream
|
||||
+ `prompt`=None
|
||||
+ `path`=None
|
||||
+ `echo`=False
|
||||
|
||||
Semantic stream: return the event list (use `llm.collect` to reduce to text). Omit `prompt=` / `path=` to use the entry document.
|
||||
|
||||
1. `path`
|
||||
**p = > prompt_load path=`path`**
|
||||
2. `prompt`
|
||||
**p = prompt**
|
||||
3. *
|
||||
**raw = > sys.module_source**
|
||||
1. not `raw`
|
||||
> print text=ext/ai/llm: stream needs prompt= or path= (or entry prompt document)
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
**p = > prompt_body src=`raw`**
|
||||
|
||||
*> self.complete prompt=`p` stream=True echo=`echo`*
|
||||
|
||||
## complete_result
|
||||
+ `prompt`
|
||||
+ `stream`=False
|
||||
+ `echo`=False
|
||||
|
||||
Transport pack via `ext/ai/llm/openai` (Ollama uses the same OpenAI-compatible path). Non-stream → `{text,usage,finish}`; stream → `{events}`.
|
||||
|
||||
*> openai.chat_completions base_url=[base_url](self) api_key=[api_key](self) model=[model](self) prompt=`prompt` stream=`stream` echo=`echo` suffix=[suffix](self) bearer=[bearer](self)*
|
||||
|
||||
## complete
|
||||
+ `prompt`
|
||||
+ `stream`=False
|
||||
+ `echo`=False
|
||||
|
||||
Chat completion using `self` handle fields.
|
||||
|
||||
Wire body is `{model, messages:[{role,content}], stream?}`. Messages are an `@` row table; headers and optional `stream` use `table.put`. `json` is only for stringify/parse.
|
||||
|
||||
**url = [base_url](self) + [suffix](self)**
|
||||
**auth = [bearer](self) + [api_key](self)**
|
||||
**headers = > table.put in=None at="Authorization" value=`auth`**
|
||||
|
||||
`messages` =
|
||||
|
||||
| @ | role | content |
|
||||
|---|------|---------|
|
||||
| 1 | user | `prompt` |
|
||||
|
||||
`req` =
|
||||
|
||||
| model | messages |
|
||||
|-------|----------|
|
||||
| [model](self) | `messages` |
|
||||
Internal / compatibility primitive. Prefer `ask` / `stream` in new code.
|
||||
|
||||
1. `stream`
|
||||
**req = > table.put in=`req` at="stream" value=True**
|
||||
**body = > json.stringify value=`req`**
|
||||
**resp = > net.http_post_sse url=`url` body=`body` headers=`headers` echo=`echo`**
|
||||
1. [status](resp) == 200
|
||||
*[events](resp)*
|
||||
2. *
|
||||
> print text=ext/ai/llm: HTTP error (stream)
|
||||
> print text=[status](resp)
|
||||
> sys.exit code=1
|
||||
**pack = > self.complete_result prompt=`prompt` stream=True echo=`echo`**
|
||||
*[events](pack)*
|
||||
2. *
|
||||
**body = > json.stringify value=`req`**
|
||||
**resp = > net.http_post url=`url` body=`body` headers=`headers`**
|
||||
1. [status](resp) == 200
|
||||
**data = > json.parse text=[body](resp)**
|
||||
*[content]([message]([1]([choices](data))))*
|
||||
2. *
|
||||
> print text=ext/ai/llm: HTTP error
|
||||
> print text=[status](resp)
|
||||
> print text=[body](resp)
|
||||
> sys.exit code=1
|
||||
|
||||
---
|
||||
**pack = > self.complete_result prompt=`prompt` stream=False echo=`echo`**
|
||||
*[text](pack)*
|
||||
|
||||
## chat
|
||||
+ `prompt`
|
||||
+ `stream`=False
|
||||
+ `echo`=False
|
||||
|
||||
Alias for `complete`.
|
||||
Alias for `complete` (compat).
|
||||
|
||||
*> self.complete prompt=`prompt` stream=`stream` echo=`echo`*
|
||||
@@ -0,0 +1,55 @@
|
||||
---
|
||||
title: ext/ai/llm/ollama
|
||||
description: >-
|
||||
Ollama backend defaults. Transport reuses openai-compatible /v1 chat completions.
|
||||
import openai:ext/ai/llm/openai.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
---
|
||||
|
||||
## resolve
|
||||
+ `model`=None
|
||||
+ `base_url`=None
|
||||
+ `api_key`=None
|
||||
|
||||
Resolve Ollama handle fields: default host `http://127.0.0.1:11434/v1`, key `ollama`, model `llama3.2`.
|
||||
|
||||
1. `api_key`
|
||||
**key = api_key**
|
||||
2. *
|
||||
**key = "ollama"**
|
||||
|
||||
1. `base_url`
|
||||
**url = base_url**
|
||||
2. *
|
||||
**url = > sys.env_get name="OLLAMA_HOST"**
|
||||
1. not `url`
|
||||
**url = "http://127.0.0.1:11434/v1"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `model`
|
||||
**mdl = model**
|
||||
2. *
|
||||
**mdl = > sys.env_get name="MARQDO_LLM_MODEL"**
|
||||
1. not `mdl`
|
||||
**mdl = "llama3.2"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
`cfg` =
|
||||
|
||||
| backend | api_key | base_url | model |
|
||||
|---------|---------|----------|-------|
|
||||
| ollama | `key` | `url` | `mdl` |
|
||||
|
||||
*cfg*
|
||||
|
||||
## chat_completions
|
||||
+ `base_url`
|
||||
+ `api_key`
|
||||
+ `model`
|
||||
+ `prompt`
|
||||
+ `stream`=False
|
||||
+ `echo`=False
|
||||
|
||||
*> openai.chat_completions base_url=`base_url` api_key=`api_key` model=`model` prompt=`prompt` stream=`stream` echo=`echo`*
|
||||
@@ -0,0 +1,145 @@
|
||||
---
|
||||
title: ext/ai/llm/openai
|
||||
description: >-
|
||||
OpenAI-compatible chat completions transport (HTTP + SSE).
|
||||
Prefers optional plugins/llm ABI when present; falls back to lib/net.
|
||||
Used by ext/ai/llm; authors should prefer llm.ask / llm.stream.
|
||||
import net:lib/net.mq.md
|
||||
import json:lib/json.mq.md
|
||||
import table:lib/table.mq.md
|
||||
import sys:lib/sys.mq.md
|
||||
import plugin:lib/plugin.mq.md
|
||||
---
|
||||
|
||||
## usage_from
|
||||
+ `data`
|
||||
|
||||
Extract `{prompt_tokens,completion_tokens,total_tokens}` from an OpenAI-shaped response map.
|
||||
|
||||
**u = > json.get value=`data` key="usage"**
|
||||
1. `u`
|
||||
**pt = > json.get value=`u` key="prompt_tokens"**
|
||||
**ct = > json.get value=`u` key="completion_tokens"**
|
||||
**tt = > json.get value=`u` key="total_tokens"**
|
||||
1. not `pt`
|
||||
**pt = 0**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. not `ct`
|
||||
**ct = 0**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. not `tt`
|
||||
**tt = pt + ct**
|
||||
2. *
|
||||
**_ = 1**
|
||||
2. *
|
||||
**pt = 0**
|
||||
**ct = 0**
|
||||
**tt = 0**
|
||||
|
||||
`usage` =
|
||||
|
||||
| prompt_tokens | completion_tokens | total_tokens |
|
||||
|---------------|-------------------|--------------|
|
||||
| `pt` | `ct` | `tt` |
|
||||
|
||||
*usage*
|
||||
|
||||
## tool_calls_from
|
||||
+ `data`
|
||||
|
||||
Extract OpenAI-shaped `choices[0].message.tool_calls` as a list (or empty list).
|
||||
|
||||
**choices = > json.get value=`data` key="choices"**
|
||||
1. not `choices`
|
||||
**empty = > json.parse text=[]**
|
||||
*empty*
|
||||
2. *
|
||||
**c0 = [1](choices)**
|
||||
**msg = > json.get value=`c0` key="message"**
|
||||
1. not `msg`
|
||||
**empty = > json.parse text=[]**
|
||||
*empty*
|
||||
2. *
|
||||
**tc = > json.get value=`msg` key="tool_calls"**
|
||||
1. `tc`
|
||||
*tc*
|
||||
2. *
|
||||
**empty = > json.parse text=[]**
|
||||
*empty*
|
||||
|
||||
## chat_completions
|
||||
+ `base_url`
|
||||
+ `api_key`
|
||||
+ `model`
|
||||
+ `prompt`
|
||||
+ `stream`=False
|
||||
+ `echo`=False
|
||||
+ `suffix`=/chat/completions
|
||||
+ `bearer`="Bearer "
|
||||
|
||||
POST `/chat/completions` (or stream SSE). Prefers `plugins/llm` ABI (`llm_chat_completions`) when the native plugin is resolvable; otherwise `lib/net` HTTP/SSE. Non-stream → `{text,usage,finish,tool_calls}`; stream → `{events,finish}`.
|
||||
|
||||
**p = > plugin.native_path name="llm"**
|
||||
1. `p`
|
||||
> plugin.load path=`p`
|
||||
*> llm_chat_completions base_url=`base_url` api_key=`api_key` model=`model` prompt=`prompt` stream=`stream` echo=`echo` suffix=`suffix` bearer=`bearer`*
|
||||
2. *
|
||||
**url = base_url + suffix**
|
||||
**auth = bearer + api_key**
|
||||
**headers = > table.put in=None at="Authorization" value=`auth`**
|
||||
|
||||
`messages` =
|
||||
|
||||
| @ | role | content |
|
||||
|---|------|---------|
|
||||
| 1 | user | `prompt` |
|
||||
|
||||
`req` =
|
||||
|
||||
| model | messages |
|
||||
|-------|----------|
|
||||
| `model` | `messages` |
|
||||
|
||||
1. `stream`
|
||||
**req = > table.put in=`req` at="stream" value=True**
|
||||
**body = > json.stringify value=`req`**
|
||||
**resp = > net.http_post_sse url=`url` body=`body` headers=`headers` echo=`echo`**
|
||||
1. [status](resp) == 200
|
||||
**pack = > json.parse text={"finish":"stop"}**
|
||||
**pack = > json.set map=`pack` key="events" value=[events](resp)**
|
||||
*pack*
|
||||
2. *
|
||||
> print text=ext/ai/llm/openai: HTTP error (stream)
|
||||
> print text=[status](resp)
|
||||
> sys.exit code=1
|
||||
2. *
|
||||
**body = > json.stringify value=`req`**
|
||||
**resp = > net.http_post url=`url` body=`body` headers=`headers`**
|
||||
1. [status](resp) == 200
|
||||
**data = > json.parse text=[body](resp)**
|
||||
**msg = [message]([1]([choices](data)))**
|
||||
**text = > json.get value=`msg` key="content"**
|
||||
1. not `text`
|
||||
**text = ""**
|
||||
2. *
|
||||
**_ = 1**
|
||||
**usage = > usage_from data=`data`**
|
||||
**fr = > json.get value=[1]([choices](data)) key="finish_reason"**
|
||||
1. not `fr`
|
||||
**fr = "stop"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
**tc = > tool_calls_from data=`data`**
|
||||
**pack = > json.parse text={"finish":"stop"}**
|
||||
**pack = > json.set map=`pack` key="text" value=`text`**
|
||||
**pack = > json.set map=`pack` key="usage" value=`usage`**
|
||||
**pack = > json.set map=`pack` key="finish" value=`fr`**
|
||||
**pack = > json.set map=`pack` key="tool_calls" value=`tc`**
|
||||
*pack*
|
||||
2. *
|
||||
> print text=ext/ai/llm/openai: HTTP error
|
||||
> print text=[status](resp)
|
||||
> print text=[body](resp)
|
||||
> sys.exit code=1
|
||||
+238
-72
@@ -1,24 +1,23 @@
|
||||
---
|
||||
title: ext/ai/大模型
|
||||
description: OpenAI 兼容大模型对象(中文)。导入 ext/ai/大模型.mq.md。
|
||||
description: >-
|
||||
Marqdo 大模型智能原语 — 提问 / 流式 / 收集 / 命名句柄。
|
||||
OpenAI 兼容与 Ollama HTTP 藏在语义面之后。
|
||||
导入 系统:lib/系统.mq.md
|
||||
导入 网络:lib/网络.mq.md
|
||||
import json:lib/json.mq.md
|
||||
导入 表:lib/表.mq.md
|
||||
导入 文件:lib/文件.mq.md
|
||||
导入 文本:lib/文本.mq.md
|
||||
import openai:ext/ai/llm/openai.mq.md
|
||||
---
|
||||
|
||||
## 加载环境
|
||||
+ `路径`=None
|
||||
|
||||
从工作目录加载 `.env`(可选命名实参 `路径=`)。不覆盖已有变量。
|
||||
|
||||
*> 系统.加载环境 路径=`路径`*
|
||||
|
||||
## 流式结果
|
||||
## 收集
|
||||
+ `事件`
|
||||
|
||||
把 `运行 流式=真` 的事件列表收成最终 `done.result`(或 `error.message`)。未知类型(如 `reasoning`)忽略。
|
||||
|
||||
**answer = ""**
|
||||
|
||||
- [ev](事件)
|
||||
@@ -28,97 +27,264 @@ import json:lib/json.mq.md
|
||||
2. `t` == "error"
|
||||
**answer = [message](ev)**
|
||||
3. `t` == "delta"
|
||||
4. *
|
||||
4. `t` == "text"
|
||||
**chunk = [text](ev)**
|
||||
1. `chunk`
|
||||
**answer = answer + chunk**
|
||||
2. *
|
||||
**_ = 1**
|
||||
5. *
|
||||
**_ = 1**
|
||||
|
||||
*answer*
|
||||
|
||||
# 大模型
|
||||
## 流式结果
|
||||
+ `事件`
|
||||
|
||||
从 `OPENAI_*` / `MARQDO_LLM_*` 构造句柄。
|
||||
*> 收集 事件=`事件`*
|
||||
|
||||
**api_key = > 系统.取环境 名="OPENAI_API_KEY"**
|
||||
1. 非 `api_key`
|
||||
**api_key = > 系统.取环境 名="MARQDO_LLM_API_KEY"**
|
||||
## 用量
|
||||
+ `数据`
|
||||
|
||||
1. 非 `api_key`
|
||||
> 打印 内容=ext/ai/大模型: 请设置 OPENAI_API_KEY 或 MARQDO_LLM_API_KEY
|
||||
*> openai.usage_from data=`数据`*
|
||||
|
||||
## 创建
|
||||
+ `模型`=None
|
||||
+ `基址`=None
|
||||
+ `密钥`=None
|
||||
+ `后端`=openai-compatible
|
||||
+ `名`=None
|
||||
|
||||
**h = > 大模型 模型=`模型` 基址=`基址` 密钥=`密钥` 后端=`后端` 名=`名`**
|
||||
*h*
|
||||
|
||||
## 快速
|
||||
+ `模型`=None
|
||||
+ `基址`=None
|
||||
+ `密钥`=None
|
||||
|
||||
1. `模型`
|
||||
**mdl = 模型**
|
||||
2. *
|
||||
**mdl = > 系统.取环境 名="MARQDO_LLM_FAST_MODEL"**
|
||||
|
||||
*> 创建 模型=`mdl` 基址=`基址` 密钥=`密钥` 名="fast" 后端="openai-compatible"*
|
||||
|
||||
## 推理
|
||||
+ `模型`=None
|
||||
+ `基址`=None
|
||||
+ `密钥`=None
|
||||
|
||||
1. `模型`
|
||||
**mdl = 模型**
|
||||
2. *
|
||||
**mdl = > 系统.取环境 名="MARQDO_LLM_REASONING_MODEL"**
|
||||
|
||||
*> 创建 模型=`mdl` 基址=`基址` 密钥=`密钥` 名="reasoning" 后端="openai-compatible"*
|
||||
|
||||
## 提示正文
|
||||
+ `源`
|
||||
|
||||
**sw = > 文本.开头是 内容=`源` 前缀="---"**
|
||||
1. 非 `sw`
|
||||
*源*
|
||||
2. *
|
||||
**parts = > split value=`源` sep="\n---\n"**
|
||||
**n = > len value=`parts`**
|
||||
1. `n` < 2
|
||||
*源*
|
||||
2. *
|
||||
**fm = > at value=`parts` index=0**
|
||||
**is_p = > 文本.包含 内容=`fm` 子串="type: prompt"**
|
||||
1. 非 `is_p`
|
||||
**is_p = > 文本.包含 内容=`fm` 子串="类型: 提示"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. `is_p`
|
||||
**body = > at value=`parts` index=1**
|
||||
*> 文本.去空白 s=`body`*
|
||||
2. *
|
||||
*源*
|
||||
|
||||
## 加载提示
|
||||
+ `路径`
|
||||
|
||||
**raw = > 文件.读文本 路径=`路径`**
|
||||
*> 提示正文 源=`raw`*
|
||||
|
||||
## 提示
|
||||
+ `路径`=None
|
||||
+ `文本`=None
|
||||
|
||||
1. `路径`
|
||||
*> 加载提示 路径=`路径`*
|
||||
2. `文本`
|
||||
*> 提示正文 源=`文本`*
|
||||
3. *
|
||||
> 打印 内容=ext/ai/大模型.提示: 请传 路径= 或 文本=
|
||||
> 系统.退出 码=1
|
||||
|
||||
**base_url = > 系统.取环境 名="OPENAI_BASE_URL"**
|
||||
1. 非 `base_url`
|
||||
**base_url = > 系统.取环境 名="MARQDO_LLM_BASE_URL"**
|
||||
1. 非 `base_url`
|
||||
**base_url = "https://api.openai.com/v1"**
|
||||
## 提问
|
||||
+ `提示`=None
|
||||
+ `路径`=None
|
||||
+ `模型`=None
|
||||
+ `基址`=None
|
||||
+ `密钥`=None
|
||||
|
||||
**model = > 系统.取环境 名="OPENAI_MODEL"**
|
||||
1. 非 `model`
|
||||
**model = > 系统.取环境 名="MARQDO_LLM_MODEL"**
|
||||
1. 非 `model`
|
||||
**model = "gpt-4o-mini"**
|
||||
1. `路径`
|
||||
**p = > 加载提示 路径=`路径`**
|
||||
2. `提示`
|
||||
**p = 提示**
|
||||
3. *
|
||||
> 打印 内容=ext/ai/大模型.提问: 请传 提示= 或 路径=
|
||||
> 系统.退出 码=1
|
||||
|
||||
**m = > 创建 模型=`模型` 基址=`基址` 密钥=`密钥`**
|
||||
**r = > m.提问 提示=`p`**
|
||||
*[text](r)*
|
||||
|
||||
# 大模型
|
||||
+ `模型`=None
|
||||
+ `基址`=None
|
||||
+ `密钥`=None
|
||||
+ `后端`=openai-compatible
|
||||
+ `名`=None
|
||||
|
||||
1. `密钥`
|
||||
**key = 密钥**
|
||||
2. *
|
||||
**key = > 系统.取环境 名="OPENAI_API_KEY"**
|
||||
1. 非 `key`
|
||||
**key = > 系统.取环境 名="MARQDO_LLM_API_KEY"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `后端` == ollama
|
||||
1. 非 `key`
|
||||
**key = "ollama"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
2. 非 `key`
|
||||
> 打印 内容=ext/ai/大模型: 请设置 OPENAI_API_KEY 或 MARQDO_LLM_API_KEY
|
||||
> 系统.退出 码=1
|
||||
3. *
|
||||
**_ = 1**
|
||||
|
||||
1. `基址`
|
||||
**url = 基址**
|
||||
2. `后端` == ollama
|
||||
**url = > 系统.取环境 名="OLLAMA_HOST"**
|
||||
1. 非 `url`
|
||||
**url = "http://127.0.0.1:11434/v1"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
3. *
|
||||
**url = > 系统.取环境 名="OPENAI_BASE_URL"**
|
||||
1. 非 `url`
|
||||
**url = > 系统.取环境 名="MARQDO_LLM_BASE_URL"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. 非 `url`
|
||||
**url = "https://api.openai.com/v1"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `模型`
|
||||
**mdl = 模型**
|
||||
2. *
|
||||
**mdl = > 系统.取环境 名="OPENAI_MODEL"**
|
||||
1. 非 `mdl`
|
||||
**mdl = > 系统.取环境 名="MARQDO_LLM_MODEL"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
1. 非 `mdl`
|
||||
1. `后端` == ollama
|
||||
**mdl = "llama3.2"**
|
||||
2. *
|
||||
**mdl = "gpt-4o-mini"**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
1. `名`
|
||||
**nm = 名**
|
||||
2. *
|
||||
**nm = None**
|
||||
|
||||
`h` =
|
||||
|
||||
| api_key | base_url | model | suffix | bearer |
|
||||
|---------|----------|-------|--------|--------|
|
||||
| `api_key` | `base_url` | `model` | /chat/completions | "Bearer " |
|
||||
| _type | backend | api_key | base_url | model | suffix | bearer | name |
|
||||
|-------|---------|---------|----------|-------|--------|--------|------|
|
||||
| 大模型 | `后端` | `key` | `url` | `mdl` | /chat/completions | "Bearer " | `nm` |
|
||||
|
||||
*h*
|
||||
|
||||
## 提问
|
||||
+ `提示`=None
|
||||
+ `路径`=None
|
||||
|
||||
1. `路径`
|
||||
**p = > 加载提示 路径=`路径`**
|
||||
2. `提示`
|
||||
**p = 提示**
|
||||
3. *
|
||||
> 打印 内容=ext/ai/大模型: 提问需要 提示= 或 路径=
|
||||
> 系统.退出 码=1
|
||||
|
||||
**pack = > 自.完整结果 提示=`p` 流式=False 打印增量=False**
|
||||
**text = > json.get value=`pack` key="text"**
|
||||
**usage = > json.get value=`pack` key="usage"**
|
||||
**finish = > json.get value=`pack` key="finish"**
|
||||
**tc = > json.get value=`pack` key="tool_calls"**
|
||||
1. not `tc`
|
||||
**tc = > json.parse text=[]**
|
||||
2. *
|
||||
**_ = 1**
|
||||
|
||||
`result` =
|
||||
|
||||
| text | model | finish | backend | usage | name | llm_calls | tokens | tool_calls |
|
||||
|------|-------|--------|---------|-------|------|-----------|--------|------------|
|
||||
| `text` | [model](自) | `finish` | [backend](自) | `usage` | [name](自) | 1 | [total_tokens](usage) | `tc` |
|
||||
|
||||
*result*
|
||||
|
||||
## 流式
|
||||
+ `提示`=None
|
||||
+ `路径`=None
|
||||
+ `打印增量`=False
|
||||
|
||||
1. `路径`
|
||||
**p = > 加载提示 路径=`路径`**
|
||||
2. `提示`
|
||||
**p = 提示**
|
||||
3. *
|
||||
> 打印 内容=ext/ai/大模型: 流式需要 提示= 或 路径=
|
||||
> 系统.退出 码=1
|
||||
|
||||
*> 自.运行 提示=`p` 流式=True 打印增量=`打印增量`*
|
||||
|
||||
## 完整结果
|
||||
+ `提示`
|
||||
+ `流式`=False
|
||||
+ `打印增量`=False
|
||||
|
||||
*> openai.chat_completions base_url=[base_url](自) api_key=[api_key](自) model=[model](自) prompt=`提示` stream=`流式` echo=`打印增量` suffix=[suffix](自) bearer=[bearer](自)*
|
||||
|
||||
## 运行
|
||||
+ `提示`
|
||||
+ `流式`=False
|
||||
+ `打印增量`=False
|
||||
|
||||
使用 `自` 句柄字段做聊天补全。
|
||||
|
||||
线上 body 为 `{model, messages:[{role,content}], stream?}`。消息用 `@` 行向表;headers 与可选 `stream` 用 `表.改`。`json` 只做 stringify/parse。
|
||||
|
||||
**url = [base_url](自) + [suffix](自)**
|
||||
**auth = [bearer](自) + [api_key](自)**
|
||||
**headers = > 表.改 入=None 于="Authorization" 值=`auth`**
|
||||
|
||||
`messages` =
|
||||
|
||||
| @ | role | content |
|
||||
|---|------|---------|
|
||||
| 1 | user | `提示` |
|
||||
|
||||
`req` =
|
||||
|
||||
| model | messages |
|
||||
|-------|----------|
|
||||
| [model](自) | `messages` |
|
||||
|
||||
1. `流式`
|
||||
**req = > 表.改 入=`req` 于="stream" 值=True**
|
||||
**body = > json.stringify value=`req`**
|
||||
**resp = > 网络.提交流式 地址=`url` 内容=`body` headers=`headers` 打印增量=`打印增量`**
|
||||
1. [status](resp) == 200
|
||||
*[events](resp)*
|
||||
2. *
|
||||
> 打印 内容=ext/ai/大模型: HTTP 错误(流式)
|
||||
> 打印 内容=[status](resp)
|
||||
> 系统.退出 码=1
|
||||
**pack = > 自.完整结果 提示=`提示` 流式=True 打印增量=`打印增量`**
|
||||
*[events](pack)*
|
||||
2. *
|
||||
**body = > json.stringify value=`req`**
|
||||
**resp = > 网络.提交 地址=`url` 内容=`body` headers=`headers`**
|
||||
1. [status](resp) == 200
|
||||
**data = > json.parse text=[body](resp)**
|
||||
*[content]([message]([1]([choices](data))))*
|
||||
2. *
|
||||
> 打印 内容=ext/ai/大模型: HTTP 错误
|
||||
> 打印 内容=[status](resp)
|
||||
> 打印 内容=[body](resp)
|
||||
> 系统.退出 码=1
|
||||
|
||||
---
|
||||
**pack = > 自.完整结果 提示=`提示` 流式=False 打印增量=`打印增量`**
|
||||
*[text](pack)*
|
||||
|
||||
## 聊天
|
||||
+ `提示`
|
||||
+ `流式`=False
|
||||
+ `打印增量`=False
|
||||
|
||||
运行 的别名。
|
||||
|
||||
*> 自.运行 提示=`提示` 流式=`流式` 打印增量=`打印增量`*
|
||||
+110
-1
@@ -1,6 +1,6 @@
|
||||
---
|
||||
title: ext/ai/智能体
|
||||
description: 文档驱动智能体 — 单步 / 多步;工具经 lib/子任务;运行时经 ABI v2 agent 插件。
|
||||
description: 文档驱动智能体 v2 — 单步 / 多步 / 运行;技能编译经 ABI v2 agent 插件(情景 → 学习 → llm_free)。
|
||||
导入 大模型:ext/ai/大模型.mq.md
|
||||
import agent:ext/ai/agent.mq.md
|
||||
import json:lib/json.mq.md
|
||||
@@ -212,3 +212,112 @@ import json:lib/json.mq.md
|
||||
**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=`强制` soft_match=`软命中` near_match=`近义命中` near_threshold=`近义阈值` promote=`晋升` kb_dir=`知识库目录` improve_every=`改进周期` explore_n=`探索次数` stream=`流式` echo=`打印增量` trace=`轨迹`*
|
||||
|
||||
## 解析
|
||||
+ `任务`
|
||||
+ `知识库目录`=.marqdo/agent-kb
|
||||
+ `近义命中`=True
|
||||
+ `近义阈值`=0.78
|
||||
|
||||
技能解析(v2):exact → alias → canonical → near。
|
||||
|
||||
**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.resolve task=`任务` kb_dir=`知识库目录` near_match=`近义命中` near_threshold=`近义阈值`*
|
||||
|
||||
## 记录情景
|
||||
+ `任务`
|
||||
+ `状态`=ok
|
||||
+ `结果`=None
|
||||
+ `模式`=explore
|
||||
+ `技能`=none
|
||||
+ `观察`=None
|
||||
+ `工作簿`=None
|
||||
+ `缓存`=None
|
||||
+ `匹配`=None
|
||||
+ `大模型次数`=0
|
||||
+ `工具次数`=0
|
||||
+ `错误`=None
|
||||
+ `记忆目录`=.marqdo/agent-memory
|
||||
|
||||
将一次执行沉淀为 `.mq.md` 情景记忆。
|
||||
|
||||
**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.record_episode task=`任务` status=`状态` result=`结果` mode=`模式` skill=`技能` observation=`观察` workbook=`工作簿` cache=`缓存` match=`匹配` llm_calls=`大模型次数` tool_calls=`工具次数` error=`错误` memory_dir=`记忆目录`*
|
||||
|
||||
## 尝试学习
|
||||
+ `任务`
|
||||
+ `知识库目录`=.marqdo/agent-kb
|
||||
+ `记忆目录`=.marqdo/agent-memory
|
||||
+ `改进周期`=3
|
||||
+ `晋升`=True
|
||||
|
||||
情景足够时:归纳 → 合成候选 → 编译 → 可选晋升为 `llm_free` 技能。
|
||||
|
||||
**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.maybe_learn task=`任务` kb_dir=`知识库目录` memory_dir=`记忆目录` improve_every=`改进周期` promote=`晋升`*
|
||||
|
||||
## 运行
|
||||
+ `任务`
|
||||
+ `知识库目录`=.marqdo/agent-kb
|
||||
+ `记忆目录`=.marqdo/agent-memory
|
||||
+ `学习`=True
|
||||
+ `校验`=True
|
||||
+ `写回`=True
|
||||
+ `复用`=True
|
||||
+ `近义命中`=True
|
||||
+ `近义阈值`=0.78
|
||||
+ `晋升`=True
|
||||
+ `改进周期`=3
|
||||
+ `探索次数`=3
|
||||
+ `最大轮次`=4
|
||||
+ `流式`=False
|
||||
+ `打印增量`=False
|
||||
+ `轨迹`=False
|
||||
+ `强制`=False
|
||||
+ `路由`=auto
|
||||
+ `路由模型`=None
|
||||
|
||||
v2+P4 主入口:自适应路由 → 执行 → 记录情景 → 尝试学习(技能 + 策略)。`路由`=`auto`|`marqdo`|`small-llm`|`llm`(`jev` 为 `small-llm` 兼容别名);决策器用 `路由模型`(任意 llm 句柄)。
|
||||
|
||||
**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.run task=`任务` kb_dir=`知识库目录` memory_dir=`记忆目录` learn=`学习` verify=`校验` writeback=`写回` reuse=`复用` near_match=`近义命中` near_threshold=`近义阈值` promote=`晋升` improve_every=`改进周期` explore_n=`探索次数` max_rounds=`最大轮次` stream=`流式` echo=`打印增量` trace=`轨迹` force=`强制` router=`路由` router_model=`路由模型`*
|
||||
|
||||
## 路由
|
||||
+ `任务`
|
||||
+ `后端`=auto
|
||||
+ `知识库目录`=.marqdo/agent-kb
|
||||
+ `记忆目录`=.marqdo/agent-memory
|
||||
+ `阈值`=0.78
|
||||
|
||||
自适应路由(阶段 4):`marqdo` / `small-llm` / `llm` / `auto`(`jev`→别名)。
|
||||
|
||||
**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.route task=`任务` backend=`后端` kb_dir=`知识库目录` memory_dir=`记忆目录` threshold=`阈值`*
|
||||
|
||||
## 编译策略
|
||||
+ `记忆目录`=.marqdo/agent-memory
|
||||
+ `最少证据`=3
|
||||
+ `强制`=False
|
||||
|
||||
策略编译:稳定的 task→skill 映射 → `policies/router.mq.md`。
|
||||
|
||||
**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.compile_policy memory_dir=`记忆目录` min_evidence=`最少证据` force=`强制`*
|
||||
+90
-8
@@ -242,6 +242,17 @@ Assemble main from a bind table (`|属性|值|样式|` or `|front|back|css|`).
|
||||
|
||||
*> web_compose_main page=`self` main=`main`*
|
||||
|
||||
## compose_list
|
||||
+ `main`
|
||||
+ `query`=None
|
||||
+ `order`=""
|
||||
+ `target`=""
|
||||
|
||||
Secondary list bind (e.g. comments on a detail page). Same bind shape as compose_main.
|
||||
Optional query supports route param placeholders; optional target injects into an intro element id.
|
||||
|
||||
*> web_compose_list page=`self` main=`main` query=`query` order=`order` target=`target`*
|
||||
|
||||
## compose_intro
|
||||
+ `intro`
|
||||
|
||||
@@ -345,6 +356,50 @@ Embed a form into the page main slot. Optional `target` / `form_slot` (CSS id, e
|
||||
|
||||
*> web_compose_form page=`self` form=`form` id=`id` target=`target`*
|
||||
|
||||
## compose_form_load
|
||||
+ `table`
|
||||
+ `id_param`="id"
|
||||
|
||||
Prefill the composed form from DB using route/query `id` (desk edit pages).
|
||||
|
||||
*> web_compose_form_load page=`self` table=`table` id_param=`id_param`*
|
||||
|
||||
## compose_auth_form
|
||||
+ `action`
|
||||
+ `submit`=""
|
||||
+ `form_id`=""
|
||||
+ `err_id`=""
|
||||
+ `target`=None
|
||||
+ `next`=""
|
||||
+ `kind`=""
|
||||
|
||||
Progressive-enhancement login/register form: server injects `_csrf` / `next`,
|
||||
re-renders the same branded page on failure. No author JS.
|
||||
|
||||
*> web_compose_auth_form page=`self` action=`action` submit=`submit` form_id=`form_id` err_id=`err_id` target=`target` next=`next` kind=`kind`*
|
||||
|
||||
## compose_nav_brand
|
||||
+ `title`=""
|
||||
+ `href`="/"
|
||||
+ `logo`=""
|
||||
+ `logo_light`=""
|
||||
+ `theme_key`="mq-theme"
|
||||
|
||||
SSR topnav brand (logo + title), theme toggle, and drawer button. Interactive
|
||||
behavior stays in `client.mq.md` via `compose_client` / `client_embed`.
|
||||
|
||||
*> web_compose_nav_brand page=`self` title=`title` href=`href` logo=`logo` logo_light=`logo_light` theme_key=`theme_key`*
|
||||
|
||||
## compose_client
|
||||
+ `source`
|
||||
+ `bridge`="/static/marqdo-bridge.js"
|
||||
+ `wasm`="/static/marqdo_wasm.wasm"
|
||||
|
||||
Stamp `page.client` so render injects official bridge auto-mount (`data-mq-wasm` /
|
||||
`data-mq-source-url`). Build with `marqdo wasm build -o static` first.
|
||||
|
||||
*> web_compose_client page=`self` source=`source` bridge=`bridge` wasm=`wasm`*
|
||||
|
||||
## render
|
||||
+ `db`=None
|
||||
|
||||
@@ -610,6 +665,15 @@ Field table + rules table; submit writes through `# db`.
|
||||
|
||||
*> web_form_rules form=`self` rules=`rules`*
|
||||
|
||||
## labels
|
||||
+ `submit`="Submit"
|
||||
+ `cancel`="cancel"
|
||||
+ `cancel_href`=""
|
||||
|
||||
Localized submit / cancel copy for rendered forms.
|
||||
|
||||
*> web_form_labels form=`self` submit=`submit` cancel=`cancel` cancel_href=`cancel_href`*
|
||||
|
||||
## validate
|
||||
+ `rules`=None
|
||||
+ `data`
|
||||
@@ -643,7 +707,7 @@ Field table + rules table; submit writes through `# db`.
|
||||
+ `layout`=None
|
||||
+ `asset_version`=None
|
||||
|
||||
Construct an app. `admin=True` mounts the built-in CRUD UI under `admin_prefix` (default `/admin`). When `admin=False`, that prefix is not reserved — you may `route` your own pages there. Optional `login_redirect` / `logout_redirect` override post-auth landing. Optional `shell_css` / `layout` / `asset_version` become defaults for pages that do not set their own (see `page.shell_css` / `page.layout` / `page.asset_version`).
|
||||
Construct an app. `admin=True` mounts the built-in **table browser** (not a CMS) under `admin_prefix` (default `/admin`). When `admin=False`, that prefix is not reserved and is not auto-gated — declare your own `route` + `gate` desk. Optional `login_redirect` / `logout_redirect` override post-auth landing. Optional `shell_css` / `layout` / `asset_version` become defaults for pages that do not set their own (see `page.shell_css` / `page.layout` / `page.asset_version`).
|
||||
|
||||
> ensure_plugin
|
||||
*> web_app_new page=`page` db=`db` admin=`admin` host=`host` port=`port` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` shell_css=`shell_css` layout=`layout` asset_version=`asset_version`*
|
||||
@@ -736,10 +800,11 @@ Serve `/`, routed pages, `/_part/{id}` (home) and `{path}/_part/{id}` (routes),
|
||||
+ `room_key`=None
|
||||
+ `on_message`=None
|
||||
+ `presence`=False
|
||||
+ `require_auth`=None
|
||||
|
||||
Register a WebSocket endpoint at `path` (e.g. `/live` or `/chat/{id}`). `mode` is `echo` (default), `broadcast` (fan-out text to all sockets on this path), `room` (named room via `room_key`, supports `{id}` from the path), or `drain`. Legacy `echo=False` maps to `drain`. `on_message` is an optional `lib.member` hook (G-WS2): called before fan-out with `message`/`room`/`user`; return `{skip:true}` to drop or `{message:…}` to rewrite. `presence=True` also joins `room.presence` and emits join/leave JSON. Connect from a client with `web.ws.connect`.
|
||||
Register a WebSocket endpoint at `path` (e.g. `/live` or `/chat/{id}`). `mode` is `echo` (default), `broadcast` (fan-out text to all sockets on this path), `room` (named room via `room_key`, supports `{id}` from the path), or `drain`. Legacy `echo=False` maps to `drain`. `on_message` is an optional `lib.member` hook (G-WS2): called before fan-out with `message`/`room`/`user`; return `{skip:true}` to drop or `{message:…}` to rewrite. `presence=True` also joins `room.presence` and emits join/leave JSON. Stateful `broadcast` and `room` endpoints require a logged-in session by default; set `require_auth=False` only for intentionally public live feeds. Browser handshakes are same-origin. Connect from a client with `web.ws.connect`.
|
||||
|
||||
*> web_app_route_ws app=`self` path=`path` echo=`echo` mode=`mode` room_key=`room_key` on_message=`on_message` presence=`presence`*
|
||||
*> web_app_route_ws app=`self` path=`path` echo=`echo` mode=`mode` room_key=`room_key` on_message=`on_message` presence=`presence` require_auth=`require_auth`*
|
||||
|
||||
## auth
|
||||
+ `users`
|
||||
@@ -751,10 +816,26 @@ Register a WebSocket endpoint at `path` (e.g. `/live` or `/chat/{id}`). `mode` i
|
||||
+ `register`=False
|
||||
+ `register_path`=/register
|
||||
+ `default_role`="member"
|
||||
+ `session_url`=None
|
||||
|
||||
Keep the app's `admin=True`, and gate `{admin_prefix}` (segment-boundary prefix) behind a login page (default `/admin`). Unauthenticated requests redirect to `login_path` (default `{admin_prefix}/login`, auto-excluded). Does not match `/admin-publish`-style siblings. With `register=True` and `enable_rbac`, exposes `register_path` (writes `web_users` + default role).
|
||||
Keep the app's `admin=True`, and gate `{admin_prefix}` (segment-boundary prefix) behind a login page (default `/admin`). Unauthenticated requests redirect to `login_path` (default `{admin_prefix}/login`, auto-excluded). Does not match `/admin-publish`-style siblings. With `register=True` and `enable_rbac`, exposes `register_path` (writes `web_users` + default role). Optional `session_url` (`redis://…` / `rediss://…`) stores sessions in Redis separately from the Postgres/SQLite app DB; omit to keep `_marqdo_sessions` (or in-memory). With OIDC, do not set `logout_redirect` to a login path (it would bounce to the IdP); conflicting values fall back to `/`.
|
||||
|
||||
*> web_app_auth app=`self` users=`users` session_ttl=`session_ttl` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` login_path=`login_path` register=`register` register_path=`register_path` default_role=`default_role`*
|
||||
*> web_app_auth app=`self` users=`users` session_ttl=`session_ttl` admin_prefix=`admin_prefix` login_redirect=`login_redirect` logout_redirect=`logout_redirect` login_path=`login_path` register=`register` register_path=`register_path` default_role=`default_role` session_url=`session_url`*
|
||||
|
||||
## oidc
|
||||
+ `issuer`=None
|
||||
+ `client_id`
|
||||
+ `client_secret`
|
||||
+ `redirect_uri`
|
||||
+ `scopes`="openid profile email"
|
||||
+ `callback_path`=None
|
||||
+ `authorize_url`=None
|
||||
+ `token_url`=None
|
||||
+ `userinfo_url`=None
|
||||
|
||||
Enable OAuth 2.0 authorization-code / OIDC login (e.g. CFLMY IdP). When set, `login_path` / `register_path` / desk login redirect to the IdP; the `redirect_uri` callback exchanges the code and opens a local session. userinfo `is_admin` / `admin_role` map to local role `admin`; otherwise `default_role`. Secrets stay server-side; PKCE S256 is always sent.
|
||||
|
||||
*> web_app_oidc app=`self` issuer=`issuer` client_id=`client_id` client_secret=`client_secret` redirect_uri=`redirect_uri` scopes=`scopes` callback_path=`callback_path` authorize_url=`authorize_url` token_url=`token_url` userinfo_url=`userinfo_url`*
|
||||
|
||||
## gate
|
||||
+ `path`
|
||||
@@ -770,10 +851,11 @@ Require access for `path`. Prefer `permissions` (CSV of `resource:action` codes
|
||||
|
||||
## enable_rbac
|
||||
+ `catalog`=None
|
||||
+ `desk`=False
|
||||
|
||||
Enable database-backed RBAC (role ⊥ permission). On listen, ensures `web_permissions` / `web_roles` / `web_role_permissions` / `web_users` / `web_user_roles` and seeds system roles `superadmin` and `member`. See `doc/design/ext-web-rbac.md`. Management UI: `GET /_rbac/desk` (needs `roles:manage`).
|
||||
Enable database-backed RBAC (role ⊥ permission). On listen, ensures `web_permissions` / `web_roles` / `web_role_permissions` / `web_users` / `web_user_roles` and seeds system roles `superadmin` and `member`. See `doc/design/ext-web-rbac.md`. By default **no** `/_rbac/*` routes; set `desk=True` to mount the API and `GET /_rbac/desk` (needs `roles:manage`). Permission gates work either way.
|
||||
|
||||
*> web_app_rbac app=`self` catalog=`catalog`*
|
||||
*> web_app_rbac app=`self` catalog=`catalog` desk=`desk`*
|
||||
|
||||
## tenant
|
||||
+ `mode`
|
||||
@@ -1079,7 +1161,7 @@ WebSocket client helper.
|
||||
+ `message`=""
|
||||
+ `headers`=None
|
||||
|
||||
Single request–response: connect to `url`, send `message`, collect all server text replies, close. Returns `{ok, messages}`.
|
||||
Single request–response: connect to `url`, send `message`, collect up to 256 server text replies (each up to 1 MiB), close. Transport errors and timeouts return `{ok:false, error}` instead of looking like an empty successful reply.
|
||||
|
||||
**timeout = [timeout_sec](self)**
|
||||
*> web_ws_connect url=`url` message=`message` headers=`headers` timeout_sec=`timeout`*
|
||||
+93
-9
@@ -227,6 +227,18 @@ import table:lib/table.mq.md
|
||||
|
||||
*> web_compose_main page=`self` main=`主体`*
|
||||
|
||||
## 列表装配
|
||||
+ `主体`
|
||||
+ `条件`=None
|
||||
+ `排序`=""
|
||||
+ `插槽`=""
|
||||
|
||||
详情页副绑定(如评论列表)。绑定表形同 `主体装配`;`条件` 支持 `{参数}`;
|
||||
可选 `插槽`(元素 id)把列表注入引言 HTML(同表单插槽)。
|
||||
`插槽=rail` / `轨` / `新闻轨` 时渲染为右侧 `aside.side-rail`(量子新闻轨),并为 body 加上 `has-rail`。
|
||||
|
||||
*> web_compose_list page=`self` main=`主体` query=`条件` order=`排序` target=`插槽`*
|
||||
|
||||
## 引言装配
|
||||
+ `引言`
|
||||
|
||||
@@ -323,6 +335,51 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
|
||||
*> web_compose_form page=`self` form=`表单` id=`id` target=`表单插槽`*
|
||||
|
||||
## 表单载入
|
||||
+ `表`
|
||||
+ `id参数`="id"
|
||||
|
||||
编辑页:按路由/查询参数中的 id 从数据库预填已装配表单,无需作者 JS 拉行。
|
||||
|
||||
*> web_compose_form_load page=`self` table=`表` id_param=`id参数`*
|
||||
|
||||
## 鉴权表单
|
||||
+ `动作`
|
||||
+ `提交`=""
|
||||
+ `表单id`=""
|
||||
+ `错误id`=""
|
||||
+ `表单插槽`=None
|
||||
+ `回跳`=""
|
||||
+ `种类`=""
|
||||
|
||||
装配原生登录/注册表单(隐藏 `_csrf` / `next`,失败时服务端回渲同一页 flash)。
|
||||
`动作` 为 POST 路径(如 `/login`、`/desk/login`、`/register`)。可选 `表单插槽`
|
||||
把表单注入引言 HTML;`回跳` 为默认 `next`(写作台登录用 `/desk`)。
|
||||
无需站点业务 JS。
|
||||
|
||||
*> web_compose_auth_form page=`self` action=`动作` submit=`提交` form_id=`表单id` err_id=`错误id` target=`表单插槽` next=`回跳` kind=`种类`*
|
||||
|
||||
## 导航品牌
|
||||
+ `标题`=""
|
||||
+ `链接`="/"
|
||||
+ `标志`=""
|
||||
+ `浅色标志`=""
|
||||
+ `主题键`="mq-theme"
|
||||
|
||||
SSR 顶栏刊名 Logo、主题切换钮与目录抽屉钮。交互写在 `client.mq.md`,配合 `客户端`。
|
||||
|
||||
*> web_compose_nav_brand page=`self` title=`标题` href=`链接` logo=`标志` logo_light=`浅色标志` theme_key=`主题键`*
|
||||
|
||||
## 客户端
|
||||
+ `源`
|
||||
+ `桥`="/static/marqdo-bridge.js"
|
||||
+ `wasm`="/static/marqdo_wasm.wasm"
|
||||
|
||||
写入 `page.client`,渲染时注入官方 bridge 自启(`data-mq-wasm` / `data-mq-source-url`)。
|
||||
先 `marqdo wasm build -o public`(或站点 static 目录)。
|
||||
|
||||
*> web_compose_client page=`self` source=`源` bridge=`桥` wasm=`wasm`*
|
||||
|
||||
## 渲染
|
||||
+ `数据库`=None
|
||||
|
||||
@@ -566,7 +623,7 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
+ `动作`=插入
|
||||
+ `id`=None
|
||||
|
||||
字段表 + 校验表;提交经 `# 数据库` 写入。
|
||||
字段表 + 校验表;提交经 `# 数据库` 写入。`动作` 为 `插入` / `更新` / `删除`(英:`insert` / `update` / `delete`)。删除按 `id` 删行,字段表可只读展示上下文。站点后台用「列表 + 表单页」声明即可,不必改插件。
|
||||
|
||||
> 确保插件
|
||||
*> web_form_new table=`表` action=`动作` id=`id`*
|
||||
@@ -581,6 +638,15 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
|
||||
*> web_form_rules form=`self` rules=`规则`*
|
||||
|
||||
## 文案
|
||||
+ `提交`="提交"
|
||||
+ `取消`="取消"
|
||||
+ `取消链接`=""
|
||||
|
||||
表单提交钮 / 取消链接文案(默认英文 Submit/cancel 由渲染层回退)。
|
||||
|
||||
*> web_form_labels form=`self` submit=`提交` cancel=`取消` cancel_href=`取消链接`*
|
||||
|
||||
## 校验
|
||||
+ `规则`=None
|
||||
+ `数据`
|
||||
@@ -614,7 +680,7 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
+ `布局`=None
|
||||
+ `资源版本`=None
|
||||
|
||||
构造应用。`后台=真` 时在 `后台前缀`(默认 `/admin`)挂载内置 CRUD。`后台=假` 时该前缀不再保留,可自行 `路由` 到 `/admin…`。可选 `登录回跳` / `登出回跳`。可选 `壳样式` / `布局` / `资源版本` 作为未自设页面的默认(见 `页面.壳样式` / `页面.布局` / `页面.资源版本`)。
|
||||
构造应用。`后台=真` 时在 `后台前缀`(默认 `/admin`)挂载**内置表浏览器**(不是 CMS)。`后台=假` 时该前缀不保留、不自动门禁,可自行 `路由` + `门禁` 做写作台。可选 `登录回跳` / `登出回跳`。可选 `壳样式` / `布局` / `资源版本` 作为未自设页面的默认(见 `页面.壳样式` / `页面.布局` / `页面.资源版本`)。
|
||||
|
||||
> 确保插件
|
||||
*> web_app_new page=`页面` db=`数据库` admin=`后台` host=`主机` port=`端口` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` shell_css=`壳样式` layout=`布局` asset_version=`资源版本`*
|
||||
@@ -702,10 +768,11 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
+ `房间`=None
|
||||
+ `消息钩子`=None
|
||||
+ `在场`=假
|
||||
+ `需要登录`=None
|
||||
|
||||
在 `路径`(如 `/live` 或 `/chat/{id}`)注册 WebSocket。`模式` 为 `echo`(默认)、`broadcast`、`room`(需 `房间` 键,可含 `{id}`)或 `drain`。旧参数 `回显=假` 等价于 `drain`。`消息钩子` 为可选 `库.成员`(G-WS2):广播前调用,入参含 `message`/`room`/`user`;返回 `{skip:真}` 丢弃或 `{message:…}` 改写。`在场=真` 时另进 `房间.presence` 并推送 join/leave。客户端用 `网页.实时` 的 `连接` 接入。
|
||||
在 `路径`(如 `/live` 或 `/chat/{id}`)注册 WebSocket。`模式` 为 `echo`(默认)、`broadcast`、`room`(需 `房间` 键,可含 `{id}`)或 `drain`。旧参数 `回显=假` 等价于 `drain`。`消息钩子` 为可选 `库.成员`(G-WS2):广播前调用,入参含 `message`/`room`/`user`;返回 `{skip:真}` 丢弃或 `{message:…}` 改写。`在场=真` 时另进 `房间.presence` 并推送 join/leave。有状态的 `broadcast` 与 `room` 端点默认需登录会话;仅对有意公开的直播流设 `需要登录=假`。浏览器握手同源。客户端用 `网页.实时` 的 `连接` 接入。
|
||||
|
||||
*> web_app_route_ws app=`self` path=`路径` echo=`回显` mode=`模式` room_key=`房间` on_message=`消息钩子` presence=`在场`*
|
||||
*> web_app_route_ws app=`self` path=`路径` echo=`回显` mode=`模式` room_key=`房间` on_message=`消息钩子` presence=`在场` require_auth=`需要登录`*
|
||||
|
||||
## 鉴权
|
||||
+ `用户表`
|
||||
@@ -717,10 +784,26 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
+ `注册`=假
|
||||
+ `注册路径`=/register
|
||||
+ `默认角色`="member"
|
||||
+ `会话地址`=None
|
||||
|
||||
在保持 `后台=真` 的同时,为 `{后台前缀}` 加段边界前缀门禁(默认 `/admin`)。未登录 重定向 到 `登录路径`(默认 `{前缀}/login`,自动排除)。不会匹配 `/admin-publish` 这类旁系路径。`注册=真` 且已 `启用权限` 时开放 `注册路径`(写入 `web_users` 并赋默认角色)。
|
||||
在保持 `后台=真` 的同时,为 `{后台前缀}` 加段边界前缀门禁(默认 `/admin`)。未登录 重定向 到 `登录路径`(默认 `{前缀}/login`,自动排除)。不会匹配 `/admin-publish` 这类旁系路径。`注册=真` 且已 `启用权限` 时开放 `注册路径`(写入 `web_users` 并赋默认角色)。`会话地址` 可选:`redis://…` / `rediss://…` 将会话存 Redis(与 Postgres/SQLite 业务库分离);缺省仍落在业务库的 `_marqdo_sessions` 或内存。启用统一身份时,`登出回跳` 勿设为登录页(会立刻再跳 IdP);默认/冲突时落首页 `/`。
|
||||
|
||||
*> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` login_path=`登录路径` register=`注册` register_path=`注册路径` default_role=`默认角色`*
|
||||
*> web_app_auth app=`self` users=`用户表` session_ttl=`会话时长` admin_prefix=`后台前缀` login_redirect=`登录回跳` logout_redirect=`登出回跳` login_path=`登录路径` register=`注册` register_path=`注册路径` default_role=`默认角色` session_url=`会话地址`*
|
||||
|
||||
## 统一身份
|
||||
+ `发行方`=None
|
||||
+ `客户端编号`
|
||||
+ `客户端密钥`
|
||||
+ `回调`
|
||||
+ `范围`="openid profile email"
|
||||
+ `回调路径`=None
|
||||
+ `授权地址`=None
|
||||
+ `令牌地址`=None
|
||||
+ `用户信息地址`=None
|
||||
|
||||
接入 OIDC / OAuth 2.0 授权码(如 [CFLMY 统一身份](https://id.cflmy.cn/api-docs))。listen 后:`登录路径` / `注册路径` / 后台登录页 302 到 IdP;回调 `回调`(默认 `/oidc/callback`)换票并建本地会话。userinfo 的 `is_admin` / `admin_role` 映射为本地角色 `admin`(写作台权限),否则为 `默认角色`。`client_secret` 仅服务端;推荐配 PKCE(框架已启用 S256)。
|
||||
|
||||
*> web_app_oidc app=`self` issuer=`发行方` client_id=`客户端编号` client_secret=`客户端密钥` redirect_uri=`回调` scopes=`范围` callback_path=`回调路径` authorize_url=`授权地址` token_url=`令牌地址` userinfo_url=`用户信息地址`*
|
||||
|
||||
## 门禁
|
||||
+ `路径`
|
||||
@@ -736,10 +819,11 @@ SEO / OpenGraph 元数据表(`|键|值|`)。如 `title`、`description`、`c
|
||||
|
||||
## 启用权限
|
||||
+ `目录`=None
|
||||
+ `管理台`=假
|
||||
|
||||
启用库表 RBAC(角色 ⊥ 权限)。listen 时确保五表并种子 `superadmin` / `member`。见 `doc/design/ext-web-rbac.md`。管理台:`GET /_rbac/desk`(需 `roles:manage`)。
|
||||
启用库表 RBAC(角色 ⊥ 权限)。listen 时确保五表并种子 `superadmin` / `member`。见 `doc/design/ext-web-rbac.md`。默认**不**挂 `/_rbac/*`;`管理台=真` 时才开放 API 与 `GET /_rbac/desk`(需 `roles:manage`)。门禁 `权限=` 在两种模式下均可用。
|
||||
|
||||
*> web_app_rbac app=`self` catalog=`目录`*
|
||||
*> web_app_rbac app=`self` catalog=`目录` desk=`管理台`*
|
||||
|
||||
## 租户
|
||||
+ `模式`
|
||||
@@ -1045,7 +1129,7 @@ WebSocket 客户端工具。
|
||||
+ `消息`=""
|
||||
+ `headers`=None
|
||||
|
||||
单次请求-响应:连接 `地址`,发送 `消息`,收集服务器全部文本回复后关闭。返回 `{ok, messages}`。
|
||||
单次请求-响应:连接 `地址`,发送 `消息`,最多收集 256 条服务器文本回复(每条 ≤1 MiB)后关闭。传输错误与超时返回 `{ok:false, error}`,不再像空的成功回复。
|
||||
|
||||
**timeout = [timeout_sec](self)**
|
||||
*> web_ws_connect url=`地址` message=`消息` headers=`headers` timeout_sec=`timeout`*
|
||||
@@ -44,6 +44,42 @@ Build set_class effect map.
|
||||
**`m` = > table.put in=None at=sel value=class**
|
||||
*> table.put in=None at="set_class" value=m*
|
||||
|
||||
## add_class
|
||||
+ `sel`
|
||||
+ `class`
|
||||
|
||||
Build add_class effect map (classList.add; space-separated names OK).
|
||||
|
||||
**`m` = > table.put in=None at=sel value=class**
|
||||
*> table.put in=None at="add_class" value=m*
|
||||
|
||||
## remove_class
|
||||
+ `sel`
|
||||
+ `class`
|
||||
|
||||
Build remove_class effect map.
|
||||
|
||||
**`m` = > table.put in=None at=sel value=class**
|
||||
*> table.put in=None at="remove_class" value=m*
|
||||
|
||||
## toggle_class
|
||||
+ `sel`
|
||||
+ `class`
|
||||
|
||||
Build toggle_class effect map.
|
||||
|
||||
**`m` = > table.put in=None at=sel value=class**
|
||||
*> table.put in=None at="toggle_class" value=m*
|
||||
|
||||
## set_style
|
||||
+ `sel`
|
||||
+ `style`
|
||||
|
||||
Build set_style effect map; style is a map of CSS properties or a cssText string.
|
||||
|
||||
**`m` = > table.put in=None at=sel value=style**
|
||||
*> table.put in=None at="set_style" value=m*
|
||||
|
||||
## set_attr
|
||||
+ `sel`
|
||||
+ `attrs`
|
||||
@@ -158,8 +194,10 @@ focus.
|
||||
+ `key`
|
||||
+ `value`
|
||||
+ `scope`="memory"
|
||||
+ `then`=""
|
||||
|
||||
In-memory (or local/session) key write for SPA client state. Prefer `scope=memory` for tab-local app state.
|
||||
Optional `then` is called after the write with `{ok, op, key, scope}`.
|
||||
|
||||
`spec` =
|
||||
|
||||
@@ -167,6 +205,8 @@ In-memory (or local/session) key write for SPA client state. Prefer `scope=memor
|
||||
|----|-----|-------|-------|
|
||||
| set | `key` | `value` | `scope` |
|
||||
|
||||
1. `then` != ""
|
||||
**`spec` = > table.put in=spec at="then" value=then**
|
||||
*> table.put in=None at="storage" value=spec*
|
||||
|
||||
## store_get
|
||||
|
||||
+2
-2
@@ -9,7 +9,7 @@ English HTTP(S) wrappers. Optional headers / content_type via host aliases.
|
||||
+ `url`
|
||||
+ `headers`=None
|
||||
|
||||
HTTPS supported. Optional named arg `headers=` (map).
|
||||
HTTPS supported. Optional named arg `headers=` (map). Responses are capped at 8 MiB before decoding, so an upstream cannot exhaust the interpreter's memory.
|
||||
|
||||
*> host_http_get url=`url` headers=`headers`*
|
||||
|
||||
@@ -56,7 +56,7 @@ Optional `body=` / `content_type=` / `headers=`.
|
||||
## url_encode
|
||||
+ `text`
|
||||
|
||||
Percent-encode a URL segment.
|
||||
Percent-encode a URL segment. Spaces become `%20` (not form/query `+`), so literal `+` remains unambiguous in paths.
|
||||
|
||||
*> host_url_encode text=`text`*
|
||||
|
||||
|
||||
@@ -39,6 +39,25 @@ Current working directory.
|
||||
|
||||
*> host_cwd*
|
||||
|
||||
## meta
|
||||
|
||||
Bound Artifact Metadata from the entry `.mq.md` (map). See `doc/design/binding.md`.
|
||||
|
||||
*> host_meta*
|
||||
|
||||
## meta_get
|
||||
+ `key`
|
||||
|
||||
Lookup one metadata key; missing → None.
|
||||
|
||||
*> host_meta_get key=`key`*
|
||||
|
||||
## module_source
|
||||
|
||||
Entry `.mq.md` source text (for document-as-prompt). Empty string if unset.
|
||||
|
||||
*> host_module_source*
|
||||
|
||||
## exit
|
||||
+ `code`
|
||||
|
||||
|
||||
@@ -43,6 +43,42 @@ import table:lib/table.mq.md
|
||||
**`m` = > table.put in=None at=选择器 value=类**
|
||||
*> table.put in=None at="set_class" value=m*
|
||||
|
||||
## 加类
|
||||
+ `选择器`
|
||||
+ `类`
|
||||
|
||||
构造 add_class 效应(classList.add;可用空格分隔多个类名)。
|
||||
|
||||
**`m` = > table.put in=None at=选择器 value=类**
|
||||
*> table.put in=None at="add_class" value=m*
|
||||
|
||||
## 去类
|
||||
+ `选择器`
|
||||
+ `类`
|
||||
|
||||
构造 remove_class 效应。
|
||||
|
||||
**`m` = > table.put in=None at=选择器 value=类**
|
||||
*> table.put in=None at="remove_class" value=m*
|
||||
|
||||
## 切类
|
||||
+ `选择器`
|
||||
+ `类`
|
||||
|
||||
构造 toggle_class 效应。
|
||||
|
||||
**`m` = > table.put in=None at=选择器 value=类**
|
||||
*> table.put in=None at="toggle_class" value=m*
|
||||
|
||||
## 设样式
|
||||
+ `选择器`
|
||||
+ `样式`
|
||||
|
||||
构造 set_style 效应;样式为属性表或 cssText 字符串。
|
||||
|
||||
**`m` = > table.put in=None at=选择器 value=样式**
|
||||
*> table.put in=None at="set_style" value=m*
|
||||
|
||||
## 设属性
|
||||
+ `选择器`
|
||||
+ `属性`
|
||||
@@ -157,8 +193,10 @@ import table:lib/table.mq.md
|
||||
+ `键`
|
||||
+ `值`
|
||||
+ `作用域`="memory"
|
||||
+ `然后`=""
|
||||
|
||||
SPA 客户端状态写入。优先 `作用域=memory`(页内内存)。
|
||||
可选 `然后`:写入后回调,参数含 `{ok, op, key, scope}`。
|
||||
|
||||
`spec` =
|
||||
|
||||
@@ -166,6 +204,8 @@ SPA 客户端状态写入。优先 `作用域=memory`(页内内存)。
|
||||
|----|-----|-------|-------|
|
||||
| set | `键` | `值` | `作用域` |
|
||||
|
||||
1. `然后` != ""
|
||||
**`spec` = > table.put in=spec at="then" value=然后**
|
||||
*> table.put in=None at="storage" value=spec*
|
||||
|
||||
## 存取
|
||||
|
||||
@@ -39,6 +39,25 @@ description: 中文系统包装(加载环境 可选 路径=)
|
||||
|
||||
*> host_cwd*
|
||||
|
||||
## 元信息
|
||||
|
||||
入口 `.mq.md` 已绑定的 Artifact Metadata(映射)。见 `doc/design/binding.md`。
|
||||
|
||||
*> host_meta*
|
||||
|
||||
## 取元信息
|
||||
+ `键`
|
||||
|
||||
按键取一条元信息;缺失 → None。
|
||||
|
||||
*> host_meta_get key=`键`*
|
||||
|
||||
## 模块源码
|
||||
|
||||
入口 `.mq.md` 全文(供 document-as-prompt)。未设置时为空文本。
|
||||
|
||||
*> host_module_source*
|
||||
|
||||
## 退出
|
||||
+ `码`
|
||||
|
||||
|
||||
@@ -1269,7 +1269,7 @@ pub fn workbook_solidify(args: &Value) -> Result<Value, String> {
|
||||
};
|
||||
let lit = marqdo_quoted_text(&result);
|
||||
let body = format!(
|
||||
"---\ntitle: agent workbook\n---\n\n# main\n\n*`result` = {lit} *\n**`result`**\n"
|
||||
"---\ntitle: agent workbook\n---\n\n# main\n\n**`result` = {lit}**\n*`result`*\n"
|
||||
);
|
||||
fs::write(&p, body).map_err(|e| format!("solidify write: {e}"))?;
|
||||
Ok(json!({
|
||||
|
||||
+134
-1
@@ -1,8 +1,12 @@
|
||||
//! Agent plugin (C ABI v2): layout helpers + session bag + context via host_query + agent-kb.
|
||||
//! Agent plugin (C ABI v2): layout helpers + session bag + context via host_query + agent-kb + v2 memory + adaptive route.
|
||||
|
||||
mod corpus;
|
||||
mod kb;
|
||||
mod mcp_client;
|
||||
mod mcp_server;
|
||||
mod memory;
|
||||
mod resume;
|
||||
mod route;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::ffi::{CStr, CString};
|
||||
@@ -902,6 +906,30 @@ kb_ffi!(
|
||||
"agent_workbook_solidify"
|
||||
);
|
||||
kb_ffi!(agent_kb_task_files, kb::kb_task_files, "agent_kb_task_files");
|
||||
kb_ffi!(agent_memory_ensure, memory::memory_ensure, "agent_memory_ensure");
|
||||
kb_ffi!(agent_record_episode, memory::record_episode, "agent_record_episode");
|
||||
kb_ffi!(agent_list_episodes, memory::list_episodes, "agent_list_episodes");
|
||||
kb_ffi!(agent_get_episode, memory::get_episode, "agent_get_episode");
|
||||
kb_ffi!(agent_cluster_episodes, memory::cluster_episodes, "agent_cluster_episodes");
|
||||
kb_ffi!(agent_extract_pattern, memory::extract_pattern, "agent_extract_pattern");
|
||||
kb_ffi!(agent_generalize, memory::generalize, "agent_generalize");
|
||||
kb_ffi!(agent_synthesize_skill, memory::synthesize_skill, "agent_synthesize_skill");
|
||||
kb_ffi!(agent_compile_skill, memory::compile_skill, "agent_compile_skill");
|
||||
kb_ffi!(agent_maybe_learn, memory::maybe_learn, "agent_maybe_learn");
|
||||
kb_ffi!(agent_metrics, memory::metrics, "agent_metrics");
|
||||
kb_ffi!(agent_resolve, memory::resolve_task, "agent_resolve");
|
||||
kb_ffi!(agent_route, route::route, "agent_route");
|
||||
kb_ffi!(agent_route_apply, route::route_apply, "agent_route_apply");
|
||||
kb_ffi!(agent_compile_policy, route::compile_policy, "agent_compile_policy");
|
||||
kb_ffi!(
|
||||
agent_maybe_compile_policy,
|
||||
route::maybe_compile_policy,
|
||||
"agent_maybe_compile_policy"
|
||||
);
|
||||
kb_ffi!(agent_resume_save, resume::resume_save, "agent_resume_save");
|
||||
kb_ffi!(agent_resume_load, resume::resume_load, "agent_resume_load");
|
||||
kb_ffi!(agent_resume_clear, resume::resume_clear, "agent_resume_clear");
|
||||
kb_ffi!(agent_resume_list, resume::resume_list, "agent_resume_list");
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
|
||||
@@ -1001,6 +1029,27 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_mcp_client",
|
||||
"action,command",
|
||||
mcp_client::agent_mcp_client,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resume_save", "id,goal,status,round", agent_resume_save) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resume_load", "id,resume_dir", agent_resume_load) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resume_clear", "id,resume_dir", agent_resume_clear) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resume_list", "resume_dir", agent_resume_list) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_kb_add_alias", "kb_dir,slug,alias", agent_kb_add_alias) != 0 {
|
||||
return 1;
|
||||
}
|
||||
@@ -1022,6 +1071,90 @@ pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int
|
||||
if register(host, "agent_kb_task_files", "kb_dir,goal", agent_kb_task_files) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_memory_ensure", "memory_dir", agent_memory_ensure) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_record_episode",
|
||||
"task,status,result,mode",
|
||||
agent_record_episode,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_list_episodes", "memory_dir,task", agent_list_episodes) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_get_episode", "id", agent_get_episode) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_cluster_episodes",
|
||||
"memory_dir,task,threshold",
|
||||
agent_cluster_episodes,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_extract_pattern", "task", agent_extract_pattern) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_generalize", "task", agent_generalize) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_synthesize_skill", "task", agent_synthesize_skill) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_compile_skill",
|
||||
"task,kb_dir,min_evidence",
|
||||
agent_compile_skill,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_maybe_learn",
|
||||
"task,kb_dir,improve_every",
|
||||
agent_maybe_learn,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_metrics", "memory_dir", agent_metrics) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_resolve", "task,kb_dir", agent_resolve) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_route", "task,backend,kb_dir,memory_dir", agent_route) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(host, "agent_route_apply", "task,reply,kb_dir", agent_route_apply) != 0 {
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_compile_policy",
|
||||
"memory_dir,min_evidence",
|
||||
agent_compile_policy,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
if register(
|
||||
host,
|
||||
"agent_maybe_compile_policy",
|
||||
"memory_dir,min_evidence",
|
||||
agent_maybe_compile_policy,
|
||||
) != 0
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
0
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,327 @@
|
||||
//! MCP Client (stdio JSON-RPC) — spawn a server process, initialize, list/call tools.
|
||||
|
||||
use serde_json::{json, Value};
|
||||
use std::ffi::CStr;
|
||||
use std::io::{BufRead, BufReader, Write};
|
||||
use std::os::raw::{c_char, c_int};
|
||||
use std::process::{Child, Command, Stdio};
|
||||
use std::sync::Mutex;
|
||||
use std::time::Duration;
|
||||
|
||||
use crate::{parse_args, set_err, set_out};
|
||||
|
||||
struct Session {
|
||||
child: Child,
|
||||
stdin: std::process::ChildStdin,
|
||||
reader: BufReader<std::process::ChildStdout>,
|
||||
next_id: i64,
|
||||
server_name: String,
|
||||
}
|
||||
|
||||
static SESSIONS: Mutex<Vec<(String, Session)>> = Mutex::new(Vec::new());
|
||||
|
||||
fn opt_text<'a>(args: &'a Value, key: &str) -> Option<&'a str> {
|
||||
args.get(key).and_then(|v| v.as_str()).filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
fn arg_text<'a>(args: &'a Value, key: &str) -> Result<&'a str, String> {
|
||||
opt_text(args, key).ok_or_else(|| format!("missing text `{key}`"))
|
||||
}
|
||||
|
||||
fn write_rpc(stdin: &mut impl Write, msg: &Value) -> Result<(), String> {
|
||||
let s = serde_json::to_string(msg).map_err(|e| e.to_string())?;
|
||||
writeln!(stdin, "{s}").map_err(|e| format!("mcp client write: {e}"))?;
|
||||
stdin.flush().map_err(|e| format!("mcp client flush: {e}"))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn read_rpc(reader: &mut impl BufRead) -> Result<Value, String> {
|
||||
let mut line = String::new();
|
||||
loop {
|
||||
line.clear();
|
||||
let n = reader
|
||||
.read_line(&mut line)
|
||||
.map_err(|e| format!("mcp client read: {e}"))?;
|
||||
if n == 0 {
|
||||
return Err("mcp client: server closed stdout".into());
|
||||
}
|
||||
let t = line.trim();
|
||||
if t.is_empty() {
|
||||
continue;
|
||||
}
|
||||
return serde_json::from_str(t).map_err(|e| format!("mcp client json: {e}"));
|
||||
}
|
||||
}
|
||||
|
||||
fn rpc_call(session: &mut Session, method: &str, params: Value) -> Result<Value, String> {
|
||||
let id = session.next_id;
|
||||
session.next_id += 1;
|
||||
let req = json!({
|
||||
"jsonrpc": "2.0",
|
||||
"id": id,
|
||||
"method": method,
|
||||
"params": params,
|
||||
});
|
||||
write_rpc(&mut session.stdin, &req)?;
|
||||
// Read until matching id (skip notifications).
|
||||
for _ in 0..64 {
|
||||
let resp = read_rpc(&mut session.reader)?;
|
||||
if resp.get("id") == Some(&json!(id)) {
|
||||
if let Some(err) = resp.get("error") {
|
||||
let msg = err
|
||||
.get("message")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("rpc error");
|
||||
return Err(msg.to_string());
|
||||
}
|
||||
return Ok(resp.get("result").cloned().unwrap_or(Value::Null));
|
||||
}
|
||||
}
|
||||
Err("mcp client: no matching response".into())
|
||||
}
|
||||
|
||||
fn spawn_session(command: &str, args: &[String], name: &str) -> Result<Session, String> {
|
||||
let mut cmd = Command::new(command);
|
||||
for a in args {
|
||||
cmd.arg(a);
|
||||
}
|
||||
cmd.stdin(Stdio::piped())
|
||||
.stdout(Stdio::piped())
|
||||
.stderr(Stdio::inherit());
|
||||
let mut child = cmd
|
||||
.spawn()
|
||||
.map_err(|e| format!("mcp client spawn `{command}`: {e}"))?;
|
||||
let stdin = child
|
||||
.stdin
|
||||
.take()
|
||||
.ok_or_else(|| "mcp client: no stdin".to_string())?;
|
||||
let stdout = child
|
||||
.stdout
|
||||
.take()
|
||||
.ok_or_else(|| "mcp client: no stdout".to_string())?;
|
||||
let mut session = Session {
|
||||
child,
|
||||
stdin,
|
||||
reader: BufReader::new(stdout),
|
||||
next_id: 1,
|
||||
server_name: name.to_string(),
|
||||
};
|
||||
// initialize
|
||||
let _ = rpc_call(
|
||||
&mut session,
|
||||
"initialize",
|
||||
json!({
|
||||
"protocolVersion": "2024-11-05",
|
||||
"capabilities": {},
|
||||
"clientInfo": { "name": "marqdo-agent", "version": "0.1.0" }
|
||||
}),
|
||||
)?;
|
||||
// optional initialized notification
|
||||
let _ = write_rpc(
|
||||
&mut session.stdin,
|
||||
&json!({
|
||||
"jsonrpc": "2.0",
|
||||
"method": "notifications/initialized",
|
||||
"params": {}
|
||||
}),
|
||||
);
|
||||
let _ = session.server_name;
|
||||
// Give slow servers a beat (ignore).
|
||||
let _ = Duration::from_millis(1);
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
fn command_args(args: &Value) -> Result<(String, Vec<String>), String> {
|
||||
let command = arg_text(args, "command")?;
|
||||
let mut argv = Vec::new();
|
||||
if let Some(arr) = args.get("args").and_then(|v| v.as_array()) {
|
||||
for a in arr {
|
||||
if let Some(s) = a.as_str() {
|
||||
argv.push(s.to_string());
|
||||
} else {
|
||||
argv.push(a.to_string());
|
||||
}
|
||||
}
|
||||
} else if let Some(s) = opt_text(args, "args") {
|
||||
// space-split simple args
|
||||
for part in s.split_whitespace() {
|
||||
argv.push(part.to_string());
|
||||
}
|
||||
}
|
||||
Ok((command.to_string(), argv))
|
||||
}
|
||||
|
||||
fn session_id_of(args: &Value) -> String {
|
||||
opt_text(args, "session")
|
||||
.or_else(|| opt_text(args, "id"))
|
||||
.unwrap_or("default")
|
||||
.to_string()
|
||||
}
|
||||
|
||||
fn with_session<F>(args: &Value, f: F) -> Result<Value, String>
|
||||
where
|
||||
F: FnOnce(&mut Session) -> Result<Value, String>,
|
||||
{
|
||||
let sid = session_id_of(args);
|
||||
let mut guard = SESSIONS
|
||||
.lock()
|
||||
.map_err(|_| "mcp client: session lock poisoned".to_string())?;
|
||||
let idx = guard
|
||||
.iter()
|
||||
.position(|(id, _)| id == &sid)
|
||||
.ok_or_else(|| format!("mcp client: no session `{sid}` (call connect first)"))?;
|
||||
f(&mut guard[idx].1)
|
||||
}
|
||||
|
||||
fn action_connect(args: &Value) -> Result<Value, String> {
|
||||
let (command, argv) = command_args(args)?;
|
||||
let name = opt_text(args, "name").unwrap_or("mcp");
|
||||
let sid = session_id_of(args);
|
||||
let session = spawn_session(&command, &argv, name)?;
|
||||
let mut guard = SESSIONS
|
||||
.lock()
|
||||
.map_err(|_| "mcp client: session lock poisoned".to_string())?;
|
||||
// Replace existing
|
||||
guard.retain(|(id, _)| id != &sid);
|
||||
guard.push((sid.clone(), session));
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"session": sid,
|
||||
"transport": "stdio",
|
||||
"command": command,
|
||||
}))
|
||||
}
|
||||
|
||||
fn action_list(args: &Value) -> Result<Value, String> {
|
||||
with_session(args, |s| {
|
||||
let result = rpc_call(s, "tools/list", json!({}))?;
|
||||
let mut out = result;
|
||||
if let Some(obj) = out.as_object_mut() {
|
||||
obj.insert("authority".into(), json!("workbook"));
|
||||
obj.insert("session".into(), json!(session_id_of(args)));
|
||||
}
|
||||
Ok(out)
|
||||
})
|
||||
}
|
||||
|
||||
fn action_call(args: &Value) -> Result<Value, String> {
|
||||
let name = arg_text(args, "name")?;
|
||||
let arguments = args.get("arguments").cloned().unwrap_or(json!({}));
|
||||
with_session(args, |s| {
|
||||
let result = rpc_call(
|
||||
s,
|
||||
"tools/call",
|
||||
json!({ "name": name, "arguments": arguments }),
|
||||
)?;
|
||||
let mut out = if result.is_object() {
|
||||
result
|
||||
} else {
|
||||
json!({ "result": result })
|
||||
};
|
||||
if let Some(obj) = out.as_object_mut() {
|
||||
obj.insert("authority".into(), json!("workbook"));
|
||||
obj.insert("session".into(), json!(session_id_of(args)));
|
||||
}
|
||||
Ok(out)
|
||||
})
|
||||
}
|
||||
|
||||
fn action_close(args: &Value) -> Result<Value, String> {
|
||||
let sid = session_id_of(args);
|
||||
let mut guard = SESSIONS
|
||||
.lock()
|
||||
.map_err(|_| "mcp client: session lock poisoned".to_string())?;
|
||||
if let Some(idx) = guard.iter().position(|(id, _)| id == &sid) {
|
||||
let (_, mut session) = guard.remove(idx);
|
||||
let _ = session.child.kill();
|
||||
let _ = session.child.wait();
|
||||
Ok(json!({ "ok": true, "session": sid, "closed": true }))
|
||||
} else {
|
||||
Ok(json!({ "ok": true, "session": sid, "closed": false }))
|
||||
}
|
||||
}
|
||||
|
||||
/// One-shot: connect → list or call → close (no persistent session).
|
||||
fn action_oneshot(args: &Value, kind: &str) -> Result<Value, String> {
|
||||
let (command, argv) = command_args(args)?;
|
||||
let name = opt_text(args, "name").unwrap_or("mcp");
|
||||
let mut session = spawn_session(&command, &argv, name)?;
|
||||
let result = match kind {
|
||||
"list" => {
|
||||
let r = rpc_call(&mut session, "tools/list", json!({}))?;
|
||||
Ok(r)
|
||||
}
|
||||
"call" => {
|
||||
let tname = arg_text(args, "tool").or_else(|_| arg_text(args, "name"))?;
|
||||
// When oneshot call, `name` may be server name — prefer `tool=`.
|
||||
let tname = opt_text(args, "tool").unwrap_or(tname);
|
||||
let arguments = args.get("arguments").cloned().unwrap_or(json!({}));
|
||||
rpc_call(
|
||||
&mut session,
|
||||
"tools/call",
|
||||
json!({ "name": tname, "arguments": arguments }),
|
||||
)
|
||||
}
|
||||
_ => Err(format!("unknown oneshot kind `{kind}`")),
|
||||
};
|
||||
let _ = session.child.kill();
|
||||
let _ = session.child.wait();
|
||||
let mut out = result?;
|
||||
if let Some(obj) = out.as_object_mut() {
|
||||
obj.insert("authority".into(), json!("workbook"));
|
||||
obj.insert("transport".into(), json!("stdio"));
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn mcp_client(args: &Value) -> Result<Value, String> {
|
||||
let action = opt_text(args, "action").unwrap_or("list");
|
||||
match action {
|
||||
"connect" => action_connect(args),
|
||||
"list" => {
|
||||
if opt_text(args, "command").is_some() && opt_text(args, "session").is_none() {
|
||||
action_oneshot(args, "list")
|
||||
} else {
|
||||
action_list(args)
|
||||
}
|
||||
}
|
||||
"call" => {
|
||||
if opt_text(args, "command").is_some() && opt_text(args, "session").is_none() {
|
||||
action_oneshot(args, "call")
|
||||
} else {
|
||||
action_call(args)
|
||||
}
|
||||
}
|
||||
"close" | "disconnect" => action_close(args),
|
||||
other => Err(format!(
|
||||
"mcp client: unknown action `{other}` (connect|list|call|close)"
|
||||
)),
|
||||
}
|
||||
}
|
||||
|
||||
pub unsafe extern "C" fn agent_mcp_client(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int {
|
||||
let args = match parse_args(args_json) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
set_err(err_msg, &e);
|
||||
return 1;
|
||||
}
|
||||
};
|
||||
match mcp_client(&args) {
|
||||
Ok(v) => {
|
||||
set_out(out_json, &v.to_string());
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
set_err(err_msg, &e);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
fn _unused(_: &CStr) {}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,181 @@
|
||||
//! Agent plan/run resume checkpoints under `.marqdo/agent-resume/`.
|
||||
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
const DEFAULT_RESUME_DIR: &str = ".marqdo/agent-resume";
|
||||
|
||||
fn resolve_path(rel: &str) -> PathBuf {
|
||||
let p = PathBuf::from(rel);
|
||||
if p.is_absolute() {
|
||||
return p;
|
||||
}
|
||||
if p.exists() {
|
||||
return p;
|
||||
}
|
||||
let base = (|| {
|
||||
let q = crate::host_query_json("cwd")?;
|
||||
q.as_str()
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| "cwd not text".to_string())
|
||||
})()
|
||||
.or_else(|_| std::env::current_dir().map_err(|e| e.to_string()))
|
||||
.unwrap_or_else(|_| PathBuf::from("."));
|
||||
base.join(p)
|
||||
}
|
||||
|
||||
fn opt_text<'a>(args: &'a Value, key: &str) -> Option<&'a str> {
|
||||
args.get(key).and_then(|v| v.as_str()).filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
fn arg_text<'a>(args: &'a Value, key: &str) -> Result<&'a str, String> {
|
||||
opt_text(args, key).ok_or_else(|| format!("missing text `{key}`"))
|
||||
}
|
||||
|
||||
fn opt_i64(args: &Value, key: &str, default: i64) -> i64 {
|
||||
match args.get(key) {
|
||||
None | Some(Value::Null) => default,
|
||||
Some(Value::Number(n)) => n.as_i64().unwrap_or(default),
|
||||
Some(Value::String(s)) => s.parse().unwrap_or(default),
|
||||
_ => default,
|
||||
}
|
||||
}
|
||||
|
||||
fn resume_root(args: &Value) -> PathBuf {
|
||||
let rel = opt_text(args, "resume_dir").unwrap_or(DEFAULT_RESUME_DIR);
|
||||
resolve_path(rel)
|
||||
}
|
||||
|
||||
fn checkpoint_path(root: &Path, id: &str) -> PathBuf {
|
||||
let safe: String = id
|
||||
.chars()
|
||||
.map(|c| {
|
||||
if c.is_ascii_alphanumeric() || c == '-' || c == '_' {
|
||||
c
|
||||
} else {
|
||||
'_'
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
root.join(format!("{safe}.json"))
|
||||
}
|
||||
|
||||
fn now_rfc3339() -> String {
|
||||
chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
|
||||
}
|
||||
|
||||
fn ensure_dir(root: &Path) -> Result<(), String> {
|
||||
fs::create_dir_all(root).map_err(|e| format!("resume mkdir: {e}"))
|
||||
}
|
||||
|
||||
pub fn resume_save(args: &Value) -> Result<Value, String> {
|
||||
let id = arg_text(args, "id")?;
|
||||
let root = resume_root(args);
|
||||
ensure_dir(&root)?;
|
||||
let path = checkpoint_path(&root, id);
|
||||
let mut doc = json!({
|
||||
"id": id,
|
||||
"updated_at": now_rfc3339(),
|
||||
});
|
||||
if let Some(obj) = doc.as_object_mut() {
|
||||
for key in [
|
||||
"goal", "task", "status", "workbook", "events", "route", "result", "round", "meta",
|
||||
] {
|
||||
if let Some(v) = args.get(key) {
|
||||
if !v.is_null() {
|
||||
obj.insert(key.to_string(), v.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
if !obj.contains_key("round") {
|
||||
obj.insert("round".into(), json!(opt_i64(args, "round", 0)));
|
||||
}
|
||||
if !obj.contains_key("status") {
|
||||
obj.insert("status".into(), json!("running"));
|
||||
}
|
||||
}
|
||||
let text = serde_json::to_string_pretty(&doc).map_err(|e| e.to_string())?;
|
||||
fs::write(&path, text).map_err(|e| format!("resume save: {e}"))?;
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"id": id,
|
||||
"path": path.to_string_lossy(),
|
||||
"updated_at": doc.get("updated_at").cloned().unwrap_or(Value::Null),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn resume_load(args: &Value) -> Result<Value, String> {
|
||||
let id = arg_text(args, "id")?;
|
||||
let root = resume_root(args);
|
||||
let path = checkpoint_path(&root, id);
|
||||
if !path.is_file() {
|
||||
return Ok(json!({
|
||||
"ok": false,
|
||||
"found": false,
|
||||
"id": id,
|
||||
"path": path.to_string_lossy(),
|
||||
}));
|
||||
}
|
||||
let text = fs::read_to_string(&path).map_err(|e| format!("resume load: {e}"))?;
|
||||
let mut doc: Value = serde_json::from_str(&text).map_err(|e| format!("resume json: {e}"))?;
|
||||
if let Some(obj) = doc.as_object_mut() {
|
||||
obj.insert("ok".into(), json!(true));
|
||||
obj.insert("found".into(), json!(true));
|
||||
obj.insert("path".into(), json!(path.to_string_lossy()));
|
||||
}
|
||||
Ok(doc)
|
||||
}
|
||||
|
||||
pub fn resume_clear(args: &Value) -> Result<Value, String> {
|
||||
let id = arg_text(args, "id")?;
|
||||
let root = resume_root(args);
|
||||
let path = checkpoint_path(&root, id);
|
||||
let existed = path.is_file();
|
||||
if existed {
|
||||
fs::remove_file(&path).map_err(|e| format!("resume clear: {e}"))?;
|
||||
}
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"id": id,
|
||||
"cleared": existed,
|
||||
"path": path.to_string_lossy(),
|
||||
}))
|
||||
}
|
||||
|
||||
pub fn resume_list(args: &Value) -> Result<Value, String> {
|
||||
let root = resume_root(args);
|
||||
if !root.is_dir() {
|
||||
return Ok(json!({ "ok": true, "items": [], "resume_dir": root.to_string_lossy() }));
|
||||
}
|
||||
let mut items = Vec::new();
|
||||
let rd = fs::read_dir(&root).map_err(|e| format!("resume list: {e}"))?;
|
||||
for ent in rd.flatten() {
|
||||
let p = ent.path();
|
||||
if p.extension().and_then(|e| e.to_str()) != Some("json") {
|
||||
continue;
|
||||
}
|
||||
let id = p
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let meta = fs::metadata(&p).ok();
|
||||
items.push(json!({
|
||||
"id": id,
|
||||
"path": p.to_string_lossy(),
|
||||
"len": meta.map(|m| m.len()).unwrap_or(0),
|
||||
}));
|
||||
}
|
||||
items.sort_by(|a, b| {
|
||||
let ai = a.get("id").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let bi = b.get("id").and_then(|v| v.as_str()).unwrap_or("");
|
||||
ai.cmp(bi)
|
||||
});
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"items": items,
|
||||
"resume_dir": root.to_string_lossy(),
|
||||
}))
|
||||
}
|
||||
@@ -0,0 +1,719 @@
|
||||
//! Adaptive Routing / Policy Compilation (Phase 4).
|
||||
//! Router is abstract: marqdo | small-llm | llm | auto.
|
||||
//! `jev` is accepted as an alias of `small-llm` (compat); not a first-class backend.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde_json::{json, Value};
|
||||
|
||||
use crate::kb::{goal_sig_hex, goal_slug_str, normalize_goal};
|
||||
use crate::memory::{self};
|
||||
|
||||
const DEFAULT_MEMORY_DIR: &str = ".marqdo/agent-memory";
|
||||
const DEFAULT_KB_DIR: &str = ".marqdo/agent-kb";
|
||||
const POLICY_NAME: &str = "router.mq.md";
|
||||
|
||||
fn arg_text<'a>(args: &'a Value, key: &str) -> Result<&'a str, String> {
|
||||
args.get(key)
|
||||
.and_then(|v| v.as_str())
|
||||
.ok_or_else(|| format!("missing text `{key}`"))
|
||||
}
|
||||
|
||||
fn opt_text<'a>(args: &'a Value, key: &str) -> Option<&'a str> {
|
||||
args.get(key).and_then(|v| v.as_str()).filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
fn opt_i64(args: &Value, key: &str, default: i64) -> i64 {
|
||||
match args.get(key) {
|
||||
None | Some(Value::Null) => default,
|
||||
Some(Value::Number(n)) => n.as_i64().unwrap_or(default),
|
||||
Some(Value::String(s)) => s.parse().unwrap_or(default),
|
||||
_ => default,
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_f64(args: &Value, key: &str, default: f64) -> f64 {
|
||||
match args.get(key) {
|
||||
None | Some(Value::Null) => default,
|
||||
Some(Value::Number(n)) => n.as_f64().unwrap_or(default),
|
||||
Some(Value::String(s)) => s.parse().unwrap_or(default),
|
||||
_ => default,
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_bool(args: &Value, key: &str, default: bool) -> bool {
|
||||
match args.get(key) {
|
||||
None | Some(Value::Null) => default,
|
||||
Some(Value::Bool(b)) => *b,
|
||||
Some(Value::String(s)) => matches!(s.as_str(), "true" | "True" | "1" | "yes"),
|
||||
Some(Value::Number(n)) => n.as_i64().unwrap_or(0) != 0,
|
||||
_ => default,
|
||||
}
|
||||
}
|
||||
|
||||
fn now_rfc3339() -> String {
|
||||
chrono::Utc::now().to_rfc3339_opts(chrono::SecondsFormat::Secs, true)
|
||||
}
|
||||
|
||||
fn yaml_escape(s: &str) -> String {
|
||||
if s.is_empty()
|
||||
|| s.contains(':')
|
||||
|| s.contains('#')
|
||||
|| s.contains('|')
|
||||
|| s.contains('"')
|
||||
|| s.contains('\n')
|
||||
{
|
||||
format!("\"{}\"", s.replace('\\', "\\\\").replace('"', "\\\""))
|
||||
} else {
|
||||
s.to_string()
|
||||
}
|
||||
}
|
||||
|
||||
fn resolve_path(rel: &str) -> PathBuf {
|
||||
let p = PathBuf::from(rel);
|
||||
if p.is_absolute() {
|
||||
return p;
|
||||
}
|
||||
if p.exists() {
|
||||
return p;
|
||||
}
|
||||
let base = (|| {
|
||||
let q = crate::host_query_json("cwd")?;
|
||||
q.as_str()
|
||||
.map(PathBuf::from)
|
||||
.ok_or_else(|| "cwd not text".to_string())
|
||||
})()
|
||||
.or_else(|_| std::env::current_dir().map_err(|e| e.to_string()))
|
||||
.unwrap_or_else(|_| PathBuf::from("."));
|
||||
base.join(p)
|
||||
}
|
||||
|
||||
fn memory_root(args: &Value) -> PathBuf {
|
||||
let rel = opt_text(args, "memory_dir").unwrap_or(DEFAULT_MEMORY_DIR);
|
||||
resolve_path(rel)
|
||||
}
|
||||
|
||||
fn policy_path(root: &Path) -> PathBuf {
|
||||
root.join("policies").join(POLICY_NAME)
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct PolicyRule {
|
||||
task_hash: String,
|
||||
slug: String,
|
||||
skill: String,
|
||||
task: String,
|
||||
count: i64,
|
||||
confidence: f64,
|
||||
}
|
||||
|
||||
fn parse_policy(src: &str) -> Vec<PolicyRule> {
|
||||
let mut rules = Vec::new();
|
||||
let mut in_table = false;
|
||||
let mut header_ok = false;
|
||||
for line in src.lines() {
|
||||
let t = line.trim();
|
||||
if t.starts_with("|") && t.contains("task_hash") {
|
||||
in_table = true;
|
||||
header_ok = true;
|
||||
continue;
|
||||
}
|
||||
if in_table && t.starts_with("|") && t.contains("---") {
|
||||
continue;
|
||||
}
|
||||
if in_table && t.starts_with("|") {
|
||||
let cols: Vec<&str> = t
|
||||
.trim_matches('|')
|
||||
.split('|')
|
||||
.map(|c| c.trim().trim_matches('"'))
|
||||
.collect();
|
||||
if cols.len() >= 5 {
|
||||
let count = cols[4].parse().unwrap_or(1);
|
||||
let confidence = if cols.len() >= 6 {
|
||||
cols[5].parse().unwrap_or(0.9)
|
||||
} else {
|
||||
0.9
|
||||
};
|
||||
let task = if cols.len() >= 7 {
|
||||
cols[6].to_string()
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
rules.push(PolicyRule {
|
||||
task_hash: cols[0].to_string(),
|
||||
slug: cols[1].to_string(),
|
||||
skill: cols[2].to_string(),
|
||||
count,
|
||||
confidence,
|
||||
task,
|
||||
});
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if in_table && header_ok && !t.starts_with('|') && !t.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
rules
|
||||
}
|
||||
|
||||
fn write_policy(root: &Path, rules: &[PolicyRule]) -> Result<PathBuf, String> {
|
||||
fs::create_dir_all(root.join("policies")).map_err(|e| format!("mkdir policies: {e}"))?;
|
||||
let path = policy_path(root);
|
||||
let ts = now_rfc3339();
|
||||
let mut body = format!(
|
||||
"---\ntype: policy\nstatus: compiled\nbackend: marqdo\ngenerated_at: {ts}\nrules: {}\n---\n\n# Adaptive router (compiled Marqdo policy)\n\nDeterministic task → skill routing. No LLM required.\n\n# Rules\n\n| task_hash | slug | skill | resource | count | confidence | task |\n|-----------|------|-------|----------|------:|-----------:|------|\n",
|
||||
rules.len()
|
||||
);
|
||||
for r in rules {
|
||||
body.push_str(&format!(
|
||||
"| {} | {} | {} | {} | {} | {:.2} | {} |\n",
|
||||
r.task_hash,
|
||||
yaml_escape(&r.slug),
|
||||
yaml_escape(&r.skill),
|
||||
yaml_escape(&r.skill), // resource often == skill path for OKF
|
||||
r.count,
|
||||
r.confidence,
|
||||
yaml_escape(&r.task),
|
||||
));
|
||||
}
|
||||
body.push_str("\n# Fallback\n\nllm\n");
|
||||
fs::write(&path, body).map_err(|e| format!("write policy: {e}"))?;
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
fn load_rules(root: &Path) -> Vec<PolicyRule> {
|
||||
let path = policy_path(root);
|
||||
let Ok(src) = fs::read_to_string(&path) else {
|
||||
return Vec::new();
|
||||
};
|
||||
parse_policy(&src)
|
||||
}
|
||||
|
||||
fn match_policy(rules: &[PolicyRule], task: &str, threshold: f64) -> Option<PolicyRule> {
|
||||
let th = goal_sig_hex(&normalize_goal(task));
|
||||
if let Some(r) = rules.iter().find(|r| r.task_hash == th) {
|
||||
return Some(r.clone());
|
||||
}
|
||||
// Near-match on stored task text / slug.
|
||||
let mut best: Option<(f64, PolicyRule)> = None;
|
||||
for r in rules {
|
||||
let score = memory::task_near_score(task, &r.task);
|
||||
if score >= threshold {
|
||||
if best.as_ref().map(|(s, _)| score > *s).unwrap_or(true) {
|
||||
best = Some((score, r.clone()));
|
||||
}
|
||||
}
|
||||
let slug_score = memory::task_near_score(task, &r.slug);
|
||||
if slug_score >= threshold {
|
||||
if best.as_ref().map(|(s, _)| slug_score > *s).unwrap_or(true) {
|
||||
let mut rr = r.clone();
|
||||
rr.confidence = slug_score;
|
||||
best = Some((slug_score, rr));
|
||||
}
|
||||
}
|
||||
}
|
||||
best.map(|(s, mut r)| {
|
||||
r.confidence = s;
|
||||
r
|
||||
})
|
||||
}
|
||||
|
||||
fn build_route_prompt(task: &str, candidates: &[Value]) -> String {
|
||||
let mut s = String::from(
|
||||
"You are a Marqdo Agent router. Pick the best skill slug for the task, or say EXPLORE.\n\nReply with exactly one line:\nSKILL:<slug>\nor\nEXPLORE\n\nTask:\n",
|
||||
);
|
||||
s.push_str(task);
|
||||
s.push_str("\n\nCandidates:\n");
|
||||
for (i, c) in candidates.iter().enumerate() {
|
||||
let slug = c
|
||||
.get("slug")
|
||||
.or_else(|| c.get("title"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("?");
|
||||
let title = c
|
||||
.get("title")
|
||||
.or_else(|| c.get("goal"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("");
|
||||
s.push_str(&format!("{}. {slug} — {title}\n", i + 1));
|
||||
}
|
||||
s
|
||||
}
|
||||
|
||||
fn candidate_list(args: &Value) -> Result<Vec<Value>, String> {
|
||||
if let Some(Value::Array(items)) = args.get("candidates") {
|
||||
return Ok(items.clone());
|
||||
}
|
||||
let kb = opt_text(args, "kb_dir").unwrap_or(DEFAULT_KB_DIR);
|
||||
let listed = crate::kb::kb_list_tasks(&json!({ "kb_dir": kb }))?;
|
||||
let tasks = listed
|
||||
.get("tasks")
|
||||
.and_then(|v| v.as_array())
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
if !tasks.is_empty() {
|
||||
return Ok(tasks);
|
||||
}
|
||||
let goal = arg_text(args, "task").or_else(|_| arg_text(args, "goal"))?;
|
||||
let near = crate::kb::kb_near_match(&json!({
|
||||
"kb_dir": kb,
|
||||
"goal": goal,
|
||||
}))?;
|
||||
Ok(near
|
||||
.get("candidates")
|
||||
.and_then(|v| v.as_array())
|
||||
.cloned()
|
||||
.unwrap_or_default())
|
||||
}
|
||||
|
||||
fn enrich_with_kb(rule: &PolicyRule, kb: &str, task: &str) -> Result<Value, String> {
|
||||
let hit = crate::kb::kb_lookup(&json!({
|
||||
"kb_dir": kb,
|
||||
"goal": task,
|
||||
"slug": rule.slug,
|
||||
"near_match": true,
|
||||
"near_threshold": 0.5,
|
||||
}))
|
||||
.unwrap_or(Value::Null);
|
||||
let resource = hit
|
||||
.get("resource")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| Value::String(rule.skill.clone()));
|
||||
let skill = hit
|
||||
.get("skill")
|
||||
.cloned()
|
||||
.unwrap_or_else(|| Value::String(rule.skill.clone()));
|
||||
let llm_free = hit
|
||||
.get("llm_free")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(true);
|
||||
Ok(json!({
|
||||
"backend": "marqdo",
|
||||
"mode": "policy",
|
||||
"matched": true,
|
||||
"needs_llm": false,
|
||||
"slug": rule.slug,
|
||||
"skill": skill,
|
||||
"resource": resource,
|
||||
"task_hash": rule.task_hash,
|
||||
"confidence": rule.confidence,
|
||||
"llm_free": llm_free,
|
||||
"count": rule.count,
|
||||
}))
|
||||
}
|
||||
|
||||
fn route_marqdo(args: &Value) -> Result<Value, String> {
|
||||
let root = memory_root(args);
|
||||
let _ = memory::memory_ensure(&json!({
|
||||
"memory_dir": opt_text(args, "memory_dir").unwrap_or(DEFAULT_MEMORY_DIR),
|
||||
}));
|
||||
let task = arg_text(args, "task").or_else(|_| arg_text(args, "goal"))?;
|
||||
let threshold = opt_f64(args, "threshold", 0.78);
|
||||
let kb = opt_text(args, "kb_dir").unwrap_or(DEFAULT_KB_DIR);
|
||||
let rules = load_rules(&root);
|
||||
if let Some(rule) = match_policy(&rules, task, threshold) {
|
||||
if rule.confidence >= threshold {
|
||||
return enrich_with_kb(&rule, kb, task);
|
||||
}
|
||||
}
|
||||
Ok(json!({
|
||||
"backend": "marqdo",
|
||||
"mode": "miss",
|
||||
"matched": false,
|
||||
"needs_llm": false,
|
||||
"confidence": 0.0,
|
||||
}))
|
||||
}
|
||||
|
||||
fn route_resolve_fallback(args: &Value) -> Result<Value, String> {
|
||||
let resolved = memory::resolve_task(args)?;
|
||||
let conf = resolved
|
||||
.get("confidence")
|
||||
.and_then(|v| v.as_f64())
|
||||
.unwrap_or(0.0);
|
||||
let llm_free = resolved
|
||||
.get("llm_free")
|
||||
.and_then(|v| v.as_bool())
|
||||
.unwrap_or(false);
|
||||
if conf >= opt_f64(args, "threshold", 0.78) || llm_free {
|
||||
return Ok(json!({
|
||||
"backend": "kb",
|
||||
"mode": resolved.get("mode").cloned().unwrap_or(Value::String("near".into())),
|
||||
"matched": true,
|
||||
"needs_llm": false,
|
||||
"slug": resolved.get("slug").cloned().unwrap_or(Value::Null),
|
||||
"skill": resolved.get("skill").cloned().unwrap_or(Value::Null),
|
||||
"resource": resolved.get("resource").cloned().unwrap_or(Value::Null),
|
||||
"confidence": conf,
|
||||
"llm_free": llm_free,
|
||||
}));
|
||||
}
|
||||
Ok(json!({
|
||||
"backend": "kb",
|
||||
"mode": "miss",
|
||||
"matched": false,
|
||||
"needs_llm": false,
|
||||
"confidence": conf,
|
||||
"resolve": resolved,
|
||||
}))
|
||||
}
|
||||
|
||||
fn route_model_request(args: &Value, level: &str) -> Result<Value, String> {
|
||||
let task = arg_text(args, "task").or_else(|_| arg_text(args, "goal"))?;
|
||||
let cands = candidate_list(args)?;
|
||||
let prompt = build_route_prompt(task, &cands);
|
||||
Ok(json!({
|
||||
"backend": level,
|
||||
"mode": "needs_llm",
|
||||
"matched": false,
|
||||
"needs_llm": true,
|
||||
"level": level,
|
||||
"prompt": prompt,
|
||||
"candidates": cands,
|
||||
"confidence": 0.0,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Cascade Adaptive Router (Phase 4).
|
||||
///
|
||||
/// `backend`: `auto` | `marqdo` | `small-llm` | `llm` (`jev` → alias of `small-llm`).
|
||||
///
|
||||
/// `auto` order: marqdo policy → kb resolve → small-llm request.
|
||||
pub fn route(args: &Value) -> Result<Value, String> {
|
||||
let backend = opt_text(args, "backend").unwrap_or("auto");
|
||||
let threshold = opt_f64(args, "threshold", 0.78);
|
||||
|
||||
match backend {
|
||||
"marqdo" => {
|
||||
let hit = route_marqdo(args)?;
|
||||
if hit.get("matched").and_then(|v| v.as_bool()) == Some(true) {
|
||||
return Ok(hit);
|
||||
}
|
||||
route_resolve_fallback(args)
|
||||
}
|
||||
"small-llm" | "small_llm" | "small" | "jev" => route_model_request(args, "small-llm"),
|
||||
"llm" | "large" | "large-llm" => route_model_request(args, "llm"),
|
||||
"auto" | _ => {
|
||||
let mut trace: Vec<Value> = Vec::new();
|
||||
let m = route_marqdo(args)?;
|
||||
trace.push(json!({"try": "marqdo", "matched": m.get("matched")}));
|
||||
if m.get("matched").and_then(|v| v.as_bool()) == Some(true)
|
||||
&& m.get("confidence").and_then(|v| v.as_f64()).unwrap_or(0.0) >= threshold
|
||||
{
|
||||
let mut out = m;
|
||||
if let Some(obj) = out.as_object_mut() {
|
||||
obj.insert("trace".into(), Value::Array(trace));
|
||||
obj.insert("backend".into(), Value::String("auto/marqdo".into()));
|
||||
}
|
||||
return Ok(out);
|
||||
}
|
||||
let kb = route_resolve_fallback(args)?;
|
||||
trace.push(json!({"try": "kb", "matched": kb.get("matched")}));
|
||||
if kb.get("matched").and_then(|v| v.as_bool()) == Some(true) {
|
||||
let mut out = kb;
|
||||
if let Some(obj) = out.as_object_mut() {
|
||||
obj.insert("trace".into(), Value::Array(trace));
|
||||
obj.insert("backend".into(), Value::String("auto/kb".into()));
|
||||
}
|
||||
return Ok(out);
|
||||
}
|
||||
// Prefer small-llm request in auto when nothing matched (caller may use large).
|
||||
let mut req = route_model_request(args, "small-llm")?;
|
||||
if let Some(obj) = req.as_object_mut() {
|
||||
obj.insert("trace".into(), Value::Array(trace));
|
||||
obj.insert("backend".into(), Value::String("auto/small-llm".into()));
|
||||
}
|
||||
Ok(req)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Apply a router model reply (`SKILL:<slug>` / `EXPLORE`) to a concrete skill hit.
|
||||
pub fn route_apply(args: &Value) -> Result<Value, String> {
|
||||
let reply = arg_text(args, "reply")?;
|
||||
let kb = opt_text(args, "kb_dir").unwrap_or(DEFAULT_KB_DIR);
|
||||
let task = arg_text(args, "task").or_else(|_| arg_text(args, "goal"))?;
|
||||
let line = reply
|
||||
.lines()
|
||||
.map(|l| l.trim())
|
||||
.find(|l| !l.is_empty())
|
||||
.unwrap_or("");
|
||||
let upper = line.to_ascii_uppercase();
|
||||
if upper.starts_with("EXPLORE") || upper == "LLM" || upper == "NEW" {
|
||||
return Ok(json!({
|
||||
"matched": false,
|
||||
"mode": "explore",
|
||||
"decision": "EXPLORE",
|
||||
"needs_llm": false,
|
||||
}));
|
||||
}
|
||||
let slug = if let Some(rest) = line.strip_prefix("SKILL:") {
|
||||
rest.trim()
|
||||
} else if let Some(rest) = line.strip_prefix("skill:") {
|
||||
rest.trim()
|
||||
} else if let Some(rest) = line.strip_prefix("调用:") {
|
||||
rest.trim()
|
||||
} else {
|
||||
line.trim()
|
||||
};
|
||||
if slug.is_empty() {
|
||||
return Ok(json!({
|
||||
"matched": false,
|
||||
"mode": "explore",
|
||||
"decision": "EXPLORE",
|
||||
"reason": "empty_slug",
|
||||
}));
|
||||
}
|
||||
let hit = crate::kb::kb_lookup(&json!({
|
||||
"kb_dir": kb,
|
||||
"goal": task,
|
||||
"slug": slug,
|
||||
"near_match": true,
|
||||
"near_threshold": 0.5,
|
||||
}))?;
|
||||
if hit.is_null() {
|
||||
return Ok(json!({
|
||||
"matched": false,
|
||||
"mode": "miss",
|
||||
"decision": "SKILL",
|
||||
"slug": slug,
|
||||
"reason": "slug_not_in_kb",
|
||||
}));
|
||||
}
|
||||
Ok(json!({
|
||||
"matched": true,
|
||||
"mode": "routed",
|
||||
"decision": "SKILL",
|
||||
"backend": opt_text(args, "backend").unwrap_or("llm"),
|
||||
"slug": slug,
|
||||
"skill": hit.get("skill").cloned().unwrap_or(Value::Null),
|
||||
"resource": hit.get("resource").cloned().unwrap_or(Value::Null),
|
||||
"llm_free": hit.get("llm_free").cloned().unwrap_or(Value::Bool(false)),
|
||||
"confidence": 0.85,
|
||||
"needs_llm": false,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Compile policy from successful episode skill selections (Policy Compilation).
|
||||
pub fn compile_policy(args: &Value) -> Result<Value, String> {
|
||||
let root = memory_root(args);
|
||||
let mem_arg = opt_text(args, "memory_dir").unwrap_or(DEFAULT_MEMORY_DIR);
|
||||
let _ = memory::memory_ensure(&json!({ "memory_dir": mem_arg }));
|
||||
let min_evidence = opt_i64(args, "min_evidence", opt_i64(args, "improve_every", 3));
|
||||
let listed = memory::list_episodes(&json!({
|
||||
"memory_dir": mem_arg,
|
||||
"status": "success",
|
||||
"limit": 500,
|
||||
}))?;
|
||||
let eps = listed
|
||||
.get("episodes")
|
||||
.and_then(|v| v.as_array())
|
||||
.cloned()
|
||||
.unwrap_or_default();
|
||||
|
||||
// task_hash → (slug → count, sample task, skill path)
|
||||
let mut by_hash: HashMap<String, HashMap<String, (i64, String, String)>> = HashMap::new();
|
||||
for e in &eps {
|
||||
let id = e.get("id").and_then(|v| v.as_str()).unwrap_or("");
|
||||
if id.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let full = memory::get_episode(&json!({
|
||||
"memory_dir": mem_arg,
|
||||
"id": id,
|
||||
}))?;
|
||||
if full.is_null() {
|
||||
continue;
|
||||
}
|
||||
let src = full.get("source").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let skill = extract_fm(src, "skill").unwrap_or_default();
|
||||
if skill.is_empty() || skill == "none" || skill == "null" {
|
||||
continue;
|
||||
}
|
||||
let task_hash = extract_fm(src, "task_hash")
|
||||
.or_else(|| e.get("task_hash").and_then(|v| v.as_str()).map(|s| s.to_string()))
|
||||
.unwrap_or_default();
|
||||
if task_hash.is_empty() {
|
||||
continue;
|
||||
}
|
||||
let task = full
|
||||
.get("task")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let slug = extract_fm(src, "slug")
|
||||
.filter(|s| !s.is_empty())
|
||||
.unwrap_or_else(|| goal_slug_str(&task));
|
||||
// Prefer skill path as key when it looks like a path; else slug.
|
||||
let key = if skill.contains('/') || skill.ends_with(".md") {
|
||||
skill.clone()
|
||||
} else {
|
||||
slug.clone()
|
||||
};
|
||||
let entry = by_hash.entry(task_hash).or_default();
|
||||
let cell = entry.entry(key).or_insert((0, task, skill));
|
||||
cell.0 += 1;
|
||||
}
|
||||
|
||||
let mut rules: Vec<PolicyRule> = Vec::new();
|
||||
for (task_hash, skills) in by_hash {
|
||||
let Some((skill_key, (count, task, skill_path))) =
|
||||
skills.into_iter().max_by_key(|(_, (c, _, _))| *c)
|
||||
else {
|
||||
continue;
|
||||
};
|
||||
if count < min_evidence {
|
||||
continue;
|
||||
}
|
||||
let slug = if skill_key.contains('/') {
|
||||
Path::new(&skill_key)
|
||||
.file_stem()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or(&skill_key)
|
||||
.trim_end_matches(".mq")
|
||||
.to_string()
|
||||
} else {
|
||||
skill_key
|
||||
};
|
||||
let confidence = (count as f64 / (count as f64 + 1.0)).min(0.99);
|
||||
rules.push(PolicyRule {
|
||||
task_hash,
|
||||
slug,
|
||||
skill: skill_path,
|
||||
task,
|
||||
count,
|
||||
confidence,
|
||||
});
|
||||
}
|
||||
rules.sort_by(|a, b| b.count.cmp(&a.count));
|
||||
|
||||
if rules.is_empty() && !opt_bool(args, "force", false) {
|
||||
return Ok(json!({
|
||||
"ok": false,
|
||||
"compiled": false,
|
||||
"reason": "insufficient_stable_mappings",
|
||||
"min_evidence": min_evidence,
|
||||
}));
|
||||
}
|
||||
|
||||
let path = write_policy(&root, &rules)?;
|
||||
Ok(json!({
|
||||
"ok": true,
|
||||
"compiled": true,
|
||||
"path": path.to_string_lossy(),
|
||||
"rules": rules.len() as i64,
|
||||
"min_evidence": min_evidence,
|
||||
}))
|
||||
}
|
||||
|
||||
fn extract_fm(source: &str, key: &str) -> Option<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 None;
|
||||
}
|
||||
let prefix = format!("{key}:");
|
||||
for line in lines.iter().skip(1) {
|
||||
let t = line.trim();
|
||||
if t == "---" {
|
||||
break;
|
||||
}
|
||||
if let Some(rest) = t.strip_prefix(&prefix) {
|
||||
return Some(rest.trim().trim_matches('"').to_string());
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Learn gate for policy: compile when enough skill-tagged episodes exist.
|
||||
pub fn maybe_compile_policy(args: &Value) -> Result<Value, String> {
|
||||
let compiled = compile_policy(args)?;
|
||||
Ok(json!({
|
||||
"policy_learned": compiled.get("ok").and_then(|v| v.as_bool()).unwrap_or(false),
|
||||
"compile": compiled,
|
||||
}))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use serde_json::json;
|
||||
|
||||
#[test]
|
||||
fn compile_and_route_marqdo() {
|
||||
let dir = std::env::temp_dir().join(format!("marqdo-route-{}", std::process::id()));
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
fs::create_dir_all(&dir).unwrap();
|
||||
let mem = dir.join("agent-memory");
|
||||
let kb = dir.join("agent-kb");
|
||||
let mem_s = mem.to_string_lossy().into_owned();
|
||||
let kb_s = kb.to_string_lossy().into_owned();
|
||||
let task = "Reply with exactly the word pong and nothing else.";
|
||||
|
||||
// Seed a solidified skill via memory compile path.
|
||||
for _ in 0..3 {
|
||||
memory::record_episode(&json!({
|
||||
"memory_dir": mem_s,
|
||||
"task": task,
|
||||
"status": "ok",
|
||||
"mode": "explore",
|
||||
"result": "pong",
|
||||
"skill": "concepts/skills/reply-with-exactly-the-word-pong-and-nothing-els.md",
|
||||
"llm_calls": 1,
|
||||
}))
|
||||
.unwrap();
|
||||
}
|
||||
let learned = memory::maybe_learn(&json!({
|
||||
"memory_dir": mem_s,
|
||||
"kb_dir": kb_s,
|
||||
"task": task,
|
||||
"improve_every": 3,
|
||||
"promote": true,
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(learned.get("learned").and_then(|v| v.as_bool()), Some(true));
|
||||
|
||||
// Tag episodes with skill path for policy mining (re-record with skill).
|
||||
for _ in 0..3 {
|
||||
memory::record_episode(&json!({
|
||||
"memory_dir": mem_s,
|
||||
"task": task,
|
||||
"status": "ok",
|
||||
"mode": "compiled",
|
||||
"result": "pong",
|
||||
"skill": "concepts/skills/reply-with-exactly-the-word-pong-and-nothing-els.md",
|
||||
"llm_calls": 0,
|
||||
}))
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
let pol = compile_policy(&json!({
|
||||
"memory_dir": mem_s,
|
||||
"min_evidence": 3,
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(pol.get("ok").and_then(|v| v.as_bool()), Some(true), "{pol}");
|
||||
|
||||
let routed = route(&json!({
|
||||
"memory_dir": mem_s,
|
||||
"kb_dir": kb_s,
|
||||
"task": task,
|
||||
"backend": "auto",
|
||||
}))
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
routed.get("matched").and_then(|v| v.as_bool()),
|
||||
Some(true),
|
||||
"{routed}"
|
||||
);
|
||||
assert_eq!(
|
||||
routed.get("needs_llm").and_then(|v| v.as_bool()),
|
||||
Some(false),
|
||||
"{routed}"
|
||||
);
|
||||
let _ = fs::remove_dir_all(&dir);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
[package]
|
||||
name = "marqdo_plugin_llm"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
license = "Apache-2.0"
|
||||
description = "Marqdo LLM transport plugin (ABI v2) — OpenAI-compatible chat completions"
|
||||
publish = false
|
||||
|
||||
[lib]
|
||||
name = "llm"
|
||||
crate-type = ["cdylib"]
|
||||
|
||||
[dependencies]
|
||||
serde_json = "1"
|
||||
# Pin with host (edition2024-safe for rustc 1.81).
|
||||
ureq = { version = "=2.9.7", default-features = false, features = ["tls", "gzip"] }
|
||||
idna_adapter = { version = "=1.1.0" }
|
||||
zeroize = { version = "=1.8.1" }
|
||||
@@ -0,0 +1,369 @@
|
||||
//! Marqdo LLM transport plugin (C ABI v2): OpenAI-compatible chat completions.
|
||||
|
||||
use std::ffi::{CStr, CString};
|
||||
use std::io::{BufRead, BufReader, Read};
|
||||
use std::os::raw::{c_char, c_int, c_void};
|
||||
use std::ptr;
|
||||
use std::time::Duration;
|
||||
|
||||
use serde_json::{json, Map, Value};
|
||||
|
||||
const ABI_VERSION: u32 = 2;
|
||||
const MAX_BODY: usize = 32 * 1024 * 1024;
|
||||
|
||||
type PluginFn = unsafe extern "C" fn(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
type HostQueryFn = unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int;
|
||||
|
||||
#[repr(C)]
|
||||
pub struct MarqdoHostApi {
|
||||
pub userdata: *mut c_void,
|
||||
pub register_fn: Option<
|
||||
unsafe extern "C" fn(
|
||||
userdata: *mut c_void,
|
||||
name: *const c_char,
|
||||
params: *const c_char,
|
||||
fn_ptr: PluginFn,
|
||||
) -> c_int,
|
||||
>,
|
||||
pub alloc: Option<unsafe extern "C" fn(n: usize) -> *mut c_void>,
|
||||
pub free: Option<unsafe extern "C" fn(p: *mut c_void)>,
|
||||
pub host_query: Option<HostQueryFn>,
|
||||
}
|
||||
|
||||
static mut HOST_FREE: Option<unsafe extern "C" fn(*mut c_void)> = None;
|
||||
static mut HOST_ALLOC: Option<unsafe extern "C" fn(usize) -> *mut c_void> = None;
|
||||
|
||||
unsafe fn host_strdup(s: &str) -> *mut c_char {
|
||||
let alloc = HOST_ALLOC.expect("host alloc");
|
||||
let bytes = s.as_bytes();
|
||||
let p = alloc(bytes.len() + 1) as *mut u8;
|
||||
if p.is_null() {
|
||||
return ptr::null_mut();
|
||||
}
|
||||
std::ptr::copy_nonoverlapping(bytes.as_ptr(), p, bytes.len());
|
||||
*p.add(bytes.len()) = 0;
|
||||
p as *mut c_char
|
||||
}
|
||||
|
||||
fn set_out(out: *mut *mut c_char, s: &str) {
|
||||
if out.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*out = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_err(err: *mut *mut c_char, s: &str) {
|
||||
if err.is_null() {
|
||||
return;
|
||||
}
|
||||
unsafe {
|
||||
*err = host_strdup(s);
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_args(args_json: *const c_char) -> Result<Value, String> {
|
||||
if args_json.is_null() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
let s = unsafe { CStr::from_ptr(args_json) }
|
||||
.to_str()
|
||||
.map_err(|_| "args not utf-8".to_string())?;
|
||||
if s.trim().is_empty() {
|
||||
return Ok(json!({}));
|
||||
}
|
||||
serde_json::from_str(s).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn reply(out_json: *mut *mut c_char, err_msg: *mut *mut c_char, r: Result<Value, String>) -> c_int {
|
||||
match r {
|
||||
Ok(v) => {
|
||||
set_out(out_json, &v.to_string());
|
||||
0
|
||||
}
|
||||
Err(e) => {
|
||||
set_err(err_msg, &e);
|
||||
1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn opt_text<'a>(args: &'a Value, key: &str) -> Option<&'a str> {
|
||||
args.get(key).and_then(|v| v.as_str()).filter(|s| !s.is_empty())
|
||||
}
|
||||
|
||||
fn arg_text<'a>(args: &'a Value, key: &str) -> Result<&'a str, String> {
|
||||
opt_text(args, key).ok_or_else(|| format!("missing text `{key}`"))
|
||||
}
|
||||
|
||||
fn opt_bool(args: &Value, key: &str, default: bool) -> bool {
|
||||
match args.get(key) {
|
||||
None | Some(Value::Null) => default,
|
||||
Some(Value::Bool(b)) => *b,
|
||||
Some(Value::String(s)) => matches!(s.as_str(), "true" | "True" | "1" | "yes"),
|
||||
Some(Value::Number(n)) => n.as_i64().unwrap_or(0) != 0,
|
||||
_ => default,
|
||||
}
|
||||
}
|
||||
|
||||
fn read_limited(mut r: impl Read) -> Result<String, String> {
|
||||
let mut buf = Vec::new();
|
||||
let mut chunk = [0u8; 8192];
|
||||
loop {
|
||||
let n = r.read(&mut chunk).map_err(|e| e.to_string())?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
if buf.len() + n > MAX_BODY {
|
||||
return Err("response body too large".into());
|
||||
}
|
||||
buf.extend_from_slice(&chunk[..n]);
|
||||
}
|
||||
String::from_utf8(buf).map_err(|e| e.to_string())
|
||||
}
|
||||
|
||||
fn usage_from(data: &Value) -> Value {
|
||||
let u = data.get("usage").cloned().unwrap_or(Value::Null);
|
||||
let pt = u.get("prompt_tokens").and_then(|v| v.as_i64()).unwrap_or(0);
|
||||
let ct = u
|
||||
.get("completion_tokens")
|
||||
.and_then(|v| v.as_i64())
|
||||
.unwrap_or(0);
|
||||
let tt = u
|
||||
.get("total_tokens")
|
||||
.and_then(|v| v.as_i64())
|
||||
.unwrap_or(pt + ct);
|
||||
json!({
|
||||
"prompt_tokens": pt,
|
||||
"completion_tokens": ct,
|
||||
"total_tokens": tt,
|
||||
})
|
||||
}
|
||||
|
||||
fn tool_calls_from(data: &Value) -> Value {
|
||||
data.get("choices")
|
||||
.and_then(|c| c.as_array())
|
||||
.and_then(|a| a.first())
|
||||
.and_then(|c0| c0.get("message"))
|
||||
.and_then(|m| m.get("tool_calls"))
|
||||
.cloned()
|
||||
.unwrap_or_else(|| json!([]))
|
||||
}
|
||||
|
||||
fn parse_sse_events(reader: impl BufRead, echo: bool) -> Result<Vec<Value>, String> {
|
||||
let mut events = Vec::new();
|
||||
let mut data_lines: Vec<String> = Vec::new();
|
||||
for line in reader.lines() {
|
||||
let line = line.map_err(|e| e.to_string())?;
|
||||
if echo {
|
||||
eprint!("{line}\n");
|
||||
}
|
||||
if line.is_empty() {
|
||||
if !data_lines.is_empty() {
|
||||
let data = data_lines.join("\n");
|
||||
data_lines.clear();
|
||||
if data.trim() == "[DONE]" {
|
||||
events.push(json!({"type": "done", "data": "[DONE]"}));
|
||||
break;
|
||||
}
|
||||
events.push(json!({"type": "data", "data": data}));
|
||||
}
|
||||
continue;
|
||||
}
|
||||
if let Some(rest) = line.strip_prefix("data:") {
|
||||
data_lines.push(rest.trim_start().to_string());
|
||||
}
|
||||
}
|
||||
if !data_lines.is_empty() {
|
||||
let data = data_lines.join("\n");
|
||||
if data.trim() == "[DONE]" {
|
||||
events.push(json!({"type": "done", "data": "[DONE]"}));
|
||||
} else {
|
||||
events.push(json!({"type": "data", "data": data}));
|
||||
}
|
||||
}
|
||||
Ok(events)
|
||||
}
|
||||
|
||||
fn chat_completions(args: &Value) -> Result<Value, String> {
|
||||
let base_url = arg_text(args, "base_url")?;
|
||||
let api_key = arg_text(args, "api_key").unwrap_or("");
|
||||
let model = arg_text(args, "model")?;
|
||||
let prompt = arg_text(args, "prompt")?;
|
||||
let stream = opt_bool(args, "stream", false);
|
||||
let echo = opt_bool(args, "echo", false);
|
||||
let suffix = opt_text(args, "suffix").unwrap_or("/chat/completions");
|
||||
let bearer = opt_text(args, "bearer").unwrap_or("Bearer ");
|
||||
|
||||
let url = format!("{}{}", base_url.trim_end_matches('/'), suffix);
|
||||
if !(url.starts_with("http://") || url.starts_with("https://")) {
|
||||
return Err("url must start with http:// or https://".into());
|
||||
}
|
||||
|
||||
let mut req_body = Map::new();
|
||||
req_body.insert("model".into(), Value::String(model.to_string()));
|
||||
req_body.insert(
|
||||
"messages".into(),
|
||||
json!([{"role": "user", "content": prompt}]),
|
||||
);
|
||||
if stream {
|
||||
req_body.insert("stream".into(), Value::Bool(true));
|
||||
}
|
||||
let body = Value::Object(req_body).to_string();
|
||||
let auth = format!("{bearer}{api_key}");
|
||||
|
||||
let agent = ureq::AgentBuilder::new()
|
||||
.timeout_connect(Duration::from_secs(15))
|
||||
.timeout_read(Duration::from_secs(120))
|
||||
.build();
|
||||
let mut req = agent
|
||||
.post(&url)
|
||||
.set("User-Agent", "marqdo-plugin-llm")
|
||||
.set("Content-Type", "application/json; charset=utf-8");
|
||||
if stream {
|
||||
req = req.set("Accept", "text/event-stream");
|
||||
}
|
||||
if !api_key.is_empty() {
|
||||
req = req.set("Authorization", &auth);
|
||||
}
|
||||
|
||||
if stream {
|
||||
match req.send_string(&body) {
|
||||
Ok(r) => {
|
||||
let status = r.status();
|
||||
if status != 200 {
|
||||
let text = read_limited(r.into_reader())?;
|
||||
return Err(format!("HTTP {status}: {text}"));
|
||||
}
|
||||
let events = parse_sse_events(BufReader::new(r.into_reader()), echo)?;
|
||||
Ok(json!({
|
||||
"events": events,
|
||||
"finish": "stop",
|
||||
"transport": "plugin",
|
||||
}))
|
||||
}
|
||||
Err(ureq::Error::Status(code, r)) => {
|
||||
let text = read_limited(r.into_reader()).unwrap_or_default();
|
||||
Err(format!("HTTP {code}: {text}"))
|
||||
}
|
||||
Err(e) => Err(format!("http POST sse {url}: {e}")),
|
||||
}
|
||||
} else {
|
||||
match req.send_string(&body) {
|
||||
Ok(r) => {
|
||||
let status = r.status();
|
||||
let text = read_limited(r.into_reader())?;
|
||||
if status != 200 {
|
||||
return Err(format!("HTTP {status}: {text}"));
|
||||
}
|
||||
let data: Value = serde_json::from_str(&text).map_err(|e| e.to_string())?;
|
||||
let msg = data
|
||||
.get("choices")
|
||||
.and_then(|c| c.as_array())
|
||||
.and_then(|a| a.first())
|
||||
.and_then(|c0| c0.get("message"))
|
||||
.cloned()
|
||||
.unwrap_or(json!({}));
|
||||
let content = msg
|
||||
.get("content")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let fr = data
|
||||
.get("choices")
|
||||
.and_then(|c| c.as_array())
|
||||
.and_then(|a| a.first())
|
||||
.and_then(|c0| c0.get("finish_reason"))
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("stop")
|
||||
.to_string();
|
||||
Ok(json!({
|
||||
"text": content,
|
||||
"usage": usage_from(&data),
|
||||
"finish": fr,
|
||||
"tool_calls": tool_calls_from(&data),
|
||||
"transport": "plugin",
|
||||
}))
|
||||
}
|
||||
Err(ureq::Error::Status(code, r)) => {
|
||||
let text = read_limited(r.into_reader()).unwrap_or_default();
|
||||
Err(format!("HTTP {code}: {text}"))
|
||||
}
|
||||
Err(e) => Err(format!("http POST {url}: {e}")),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe extern "C" fn llm_chat_completions(
|
||||
args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int {
|
||||
let r = (|| {
|
||||
let args = parse_args(args_json)?;
|
||||
chat_completions(&args)
|
||||
})();
|
||||
reply(out_json, err_msg, r)
|
||||
}
|
||||
|
||||
unsafe extern "C" fn llm_ping(
|
||||
_args_json: *const c_char,
|
||||
out_json: *mut *mut c_char,
|
||||
err_msg: *mut *mut c_char,
|
||||
) -> c_int {
|
||||
reply(out_json, err_msg, Ok(json!({"ok": true, "plugin": "llm"})))
|
||||
}
|
||||
|
||||
fn register(host: &MarqdoHostApi, name: &str, params: &str, fn_ptr: PluginFn) -> c_int {
|
||||
let register = match host.register_fn {
|
||||
Some(f) => f,
|
||||
None => return 1,
|
||||
};
|
||||
let n = CString::new(name).unwrap();
|
||||
let p = CString::new(params).unwrap();
|
||||
unsafe { register(host.userdata, n.as_ptr(), p.as_ptr(), fn_ptr) }
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_abi_version() -> u32 {
|
||||
ABI_VERSION
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_shutdown() {}
|
||||
|
||||
#[no_mangle]
|
||||
pub unsafe extern "C" fn marqdo_plugin_init(host: *const MarqdoHostApi) -> c_int {
|
||||
if host.is_null() {
|
||||
return 1;
|
||||
}
|
||||
let host = &*host;
|
||||
HOST_ALLOC = host.alloc;
|
||||
HOST_FREE = host.free;
|
||||
let regs = [
|
||||
("llm_ping", "", llm_ping as PluginFn),
|
||||
(
|
||||
"llm_chat_completions",
|
||||
"base_url,api_key,model,prompt,stream,echo,suffix,bearer",
|
||||
llm_chat_completions as PluginFn,
|
||||
),
|
||||
];
|
||||
for (name, params, f) in regs {
|
||||
if register(host, name, params, f) != 0 {
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
0
|
||||
}
|
||||
@@ -174,6 +174,27 @@ func web_form_rules(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
return replyJSON(outJSON, errMsg, form.SetRules(formBag, args["rules"]), nil)
|
||||
}
|
||||
|
||||
//export web_form_labels
|
||||
func web_form_labels(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
submit, _ := argStr(args, "submit")
|
||||
if submit == "" {
|
||||
submit, _ = argStr(args, "提交")
|
||||
}
|
||||
cancel, _ := argStr(args, "cancel")
|
||||
if cancel == "" {
|
||||
cancel, _ = argStr(args, "取消")
|
||||
}
|
||||
cancelHref, _ := argStr(args, "cancel_href")
|
||||
if cancelHref == "" {
|
||||
cancelHref, _ = argStr(args, "取消链接")
|
||||
}
|
||||
return replyJSON(outJSON, errMsg, form.SetLabels(asPageMap(args["form"]), submit, cancel, cancelHref), nil)
|
||||
}
|
||||
|
||||
//export web_form_validate
|
||||
func web_form_validate(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
@@ -481,6 +502,16 @@ func web_app_auth(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
|
||||
//export web_app_oidc
|
||||
func web_app_oidc(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
if err != nil {
|
||||
return replyJSON(outJSON, errMsg, nil, err)
|
||||
}
|
||||
out, err := app.OIDC(asPageMap(args["app"]), args)
|
||||
return replyJSON(outJSON, errMsg, out, err)
|
||||
}
|
||||
|
||||
//export web_app_gate
|
||||
func web_app_gate(argsJSON *C.char, outJSON **C.char, errMsg **C.char) C.int {
|
||||
args, err := parseArgs(argsJSON)
|
||||
|
||||
@@ -15,13 +15,19 @@ require (
|
||||
github.com/cespare/xxhash/v2 v2.3.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/pgx/v5 v5.7.4 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
github.com/mattn/go-isatty v0.0.24 // indirect
|
||||
github.com/ncruces/go-strftime v1.0.0 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
github.com/yuin/goldmark v1.7.8 // indirect
|
||||
github.com/yuin/gopher-lua v1.1.1 // indirect
|
||||
go.uber.org/atomic v1.11.0 // indirect
|
||||
golang.org/x/sync v0.22.0 // indirect
|
||||
golang.org/x/sys v0.47.0 // indirect
|
||||
golang.org/x/text v0.28.0 // indirect
|
||||
modernc.org/libc v1.75.6 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.12.1 // indirect
|
||||
|
||||
@@ -8,6 +8,7 @@ github.com/bsm/gomega v1.27.10 h1:yeMWxP2pV2fG3FgAODIY8EiRE3dy0aeFYt4l7wh6yKA=
|
||||
github.com/bsm/gomega v1.27.10/go.mod h1:JyEr/xRbxbtgWNi8tIEVPUYZ5Dzef52k01W3YH0H+O0=
|
||||
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
|
||||
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
@@ -20,6 +21,14 @@ github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aN
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7 h1:a+bsQ5rvGLjzHuww6tVxozPZFVghXaHOwFs4luLUK2k=
|
||||
github.com/hashicorp/golang-lru/v2 v2.0.7/go.mod h1:QeFd9opnmA6QUJc5vARoKUSoFhyfM2/ZepoAG6RGpeM=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.4 h1:9wKznZrhWa2QiHL+NjTSPP6yjl3451BX3imWDnokYlg=
|
||||
github.com/jackc/pgx/v5 v5.7.4/go.mod h1:ncY89UGWxg82EykZUwSpUKEfccBGGYq1xjrOpsbsfGQ=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
|
||||
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI=
|
||||
@@ -32,8 +41,11 @@ github.com/redis/go-redis/v9 v9.22.0 h1:laDvpYXTJtZLloinw1fA5Kqd6HAEH2XKxOkG/PDq
|
||||
github.com/redis/go-redis/v9 v9.22.0/go.mod h1:y2g0Wj8rQvuK0ELM+oxSudcLtC09JScs98I/X9gRWY4=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic=
|
||||
github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
|
||||
github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M=
|
||||
@@ -50,8 +62,12 @@ golang.org/x/sync v0.22.0 h1:SZjpbeLmrCk4xhRSZFNZW5gFUeCeFgjekvI/+gfScek=
|
||||
golang.org/x/sync v0.22.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
|
||||
golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
|
||||
golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
|
||||
golang.org/x/text v0.28.0 h1:rhazDwis8INMIwQ4tpjLDzUhx6RlXqZNPEM0huQojng=
|
||||
golang.org/x/text v0.28.0/go.mod h1:U8nCwOR8jO/marOQ0QbDiOngZVEBB7MAiitBuMjXiNU=
|
||||
golang.org/x/tools v0.48.0 h1:3+hClM1aLL5mjMKm5ovokw9epgRXPuu2tILgismM6RE=
|
||||
golang.org/x/tools v0.48.0/go.mod h1:08xX0orndb/F7jJxGDicx061tyd5pcMto75YMAXr6lk=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
modernc.org/cc/v4 v4.29.2 h1:h6+9ciCnPKutf4I03CvheAvDLX7+IHlqR6Iy6J+cgd8=
|
||||
modernc.org/cc/v4 v4.29.2/go.mod h1:OnovgIhbbMXMu1aISnJ0wvVD1KnW+cAUJkIrAWh+kVI=
|
||||
modernc.org/ccgo/v4 v4.35.0 h1:F+TUsmw09QxLzmi3aeYYGxjAXarmZaKgj3mKQHNaA8w=
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/assets"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/oidc"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/tenant"
|
||||
)
|
||||
@@ -213,9 +214,15 @@ func Auth(appBag map[string]any, users any, opts map[string]any) (map[string]any
|
||||
if v := firstStr(opts, "default_role", "默认角色"); v != "" {
|
||||
authBag["default_role"] = v
|
||||
}
|
||||
if v := firstStr(opts, "session_url", "会话地址", "redis_url", "Redis"); v != "" {
|
||||
out["session_url"] = v
|
||||
authBag["session_url"] = v
|
||||
}
|
||||
out["auth"] = authBag
|
||||
gates := gatesOf(out)
|
||||
if !hasAdminGate(gates, prefix) {
|
||||
// Auto-gate the built-in admin prefix only when admin=true. Custom desks
|
||||
// (admin=false) declare their own gates and must not inherit /admin noise.
|
||||
if boolish(out["admin"]) && !hasAdminGate(gates, prefix) {
|
||||
gates = append(gates, map[string]any{
|
||||
"path": prefix,
|
||||
"roles": []any{"admin"},
|
||||
@@ -223,10 +230,50 @@ func Auth(appBag map[string]any, users any, opts map[string]any) (map[string]any
|
||||
"on_deny": "redirect",
|
||||
"exclude": []any{loginPath},
|
||||
})
|
||||
out["gates"] = gates
|
||||
} else {
|
||||
out["gates"] = gates
|
||||
}
|
||||
out["gates"] = gates
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// OIDC configures OAuth2/OIDC authorization-code login (CFLMY IdP or compatible).
|
||||
// When enabled, GET/POST on login_path and register_path redirect to the IdP;
|
||||
// callback at redirect_uri path establishes the local session. IdP is_admin /
|
||||
// admin_role map to local role "admin".
|
||||
func OIDC(appBag map[string]any, opts map[string]any) (map[string]any, error) {
|
||||
if opts == nil {
|
||||
opts = map[string]any{}
|
||||
}
|
||||
cfg := oidc.ParseConfig(opts)
|
||||
if !cfg.Enabled() {
|
||||
return nil, fmt.Errorf("oidc: need client_id, client_secret, redirect_uri, and issuer (or authorize_url)")
|
||||
}
|
||||
out := clone(appBag)
|
||||
bag := map[string]any{
|
||||
"issuer": cfg.Issuer,
|
||||
"client_id": cfg.ClientID,
|
||||
"client_secret": cfg.ClientSecret,
|
||||
"redirect_uri": cfg.RedirectURI,
|
||||
"scopes": cfg.ScopesCSV(),
|
||||
"callback_path": cfg.Callback(),
|
||||
}
|
||||
if cfg.AuthorizeURL != "" {
|
||||
bag["authorize_url"] = cfg.AuthorizeURL
|
||||
}
|
||||
if cfg.TokenURL != "" {
|
||||
bag["token_url"] = cfg.TokenURL
|
||||
}
|
||||
if cfg.UserInfoURL != "" {
|
||||
bag["userinfo_url"] = cfg.UserInfoURL
|
||||
}
|
||||
out["oidc"] = bag
|
||||
authBag, _ := out["auth"].(map[string]any)
|
||||
if authBag == nil {
|
||||
authBag = map[string]any{}
|
||||
} else {
|
||||
authBag = clone(authBag)
|
||||
}
|
||||
authBag["oidc"] = true
|
||||
out["auth"] = authBag
|
||||
return out, nil
|
||||
}
|
||||
|
||||
@@ -290,7 +337,8 @@ func Gate(appBag map[string]any, path string, opts map[string]any) (map[string]a
|
||||
}
|
||||
|
||||
// Rbac enables DB-backed RBAC on the app (ensure schema at listen).
|
||||
// opts may include catalog table (unused beyond flag for now).
|
||||
// opts: catalog/目录 (optional seed table); desk/管理台 (bool, default false) —
|
||||
// when true, listen mounts /_rbac/* API + desk UI; permission gates work either way.
|
||||
func Rbac(appBag map[string]any, opts map[string]any) (map[string]any, error) {
|
||||
out := clone(appBag)
|
||||
authBag, _ := out["auth"].(map[string]any)
|
||||
@@ -300,10 +348,14 @@ func Rbac(appBag map[string]any, opts map[string]any) (map[string]any, error) {
|
||||
authBag = clone(authBag)
|
||||
}
|
||||
authBag["rbac"] = true
|
||||
authBag["rbac_desk"] = false
|
||||
if opts != nil {
|
||||
if v, ok := first(opts, "catalog", "目录"); ok && v != nil {
|
||||
authBag["permission_catalog"] = v
|
||||
}
|
||||
if v, ok := first(opts, "desk", "管理台"); ok {
|
||||
authBag["rbac_desk"] = boolish(v)
|
||||
}
|
||||
}
|
||||
out["auth"] = authBag
|
||||
return out, nil
|
||||
|
||||
@@ -114,6 +114,50 @@ func TestAuthDefaultGate(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthNoAutoGateWhenAdminOff(t *testing.T) {
|
||||
a := app.New(map[string]any{"admin": false})
|
||||
out, err := app.Auth(a, []any{}, map[string]any{"login_path": "/login"})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if gates, ok := out["gates"].([]any); ok && len(gates) != 0 {
|
||||
t.Fatalf("expected no auto gate when admin=false, got %v", gates)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRbacDeskOptIn(t *testing.T) {
|
||||
a := app.New(map[string]any{"admin": false})
|
||||
out, err := app.Rbac(a, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
authBag := out["auth"].(map[string]any)
|
||||
if !boolishTest(authBag["rbac"]) || boolishTest(authBag["rbac_desk"]) {
|
||||
t.Fatalf("default rbac_desk should be false: %v", authBag)
|
||||
}
|
||||
out2, err := app.Rbac(a, map[string]any{"管理台": true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !boolishTest(out2["auth"].(map[string]any)["rbac_desk"]) {
|
||||
t.Fatal("expected rbac_desk true")
|
||||
}
|
||||
}
|
||||
|
||||
func boolishTest(v any) bool {
|
||||
switch x := v.(type) {
|
||||
case bool:
|
||||
return x
|
||||
case string:
|
||||
s := strings.ToLower(strings.TrimSpace(x))
|
||||
return s == "1" || s == "true" || s == "yes" || s == "真"
|
||||
case float64:
|
||||
return x != 0
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthCustomPrefix(t *testing.T) {
|
||||
a := app.New(map[string]any{"admin": true})
|
||||
out, err := app.Auth(a, []any{}, map[string]any{
|
||||
|
||||
@@ -152,14 +152,10 @@ func ComposeComponents(page any, layout any, callLib CallLib) (any, error) {
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
// ComposeMain merges a main bind table into a page bag.
|
||||
func ComposeMain(page any, mainTable any, callLib CallLib) (any, error) {
|
||||
obj := asObject(page)
|
||||
func resolveBinds(mainTable any, callLib CallLib) (binds []any, css string, err error) {
|
||||
raw := table.AsBind(mainTable)
|
||||
arr, _ := raw.([]any)
|
||||
binds := make([]any, 0, len(arr))
|
||||
css := strField(obj, "styles_css")
|
||||
|
||||
binds = make([]any, 0, len(arr))
|
||||
for _, b := range arr {
|
||||
bm, ok := b.(map[string]any)
|
||||
if !ok {
|
||||
@@ -191,7 +187,7 @@ func ComposeMain(page any, mainTable any, callLib CallLib) (any, error) {
|
||||
case table.SitePathLibMember:
|
||||
st, err := callLib(sp.Lib + "." + sp.Member)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, "", err
|
||||
}
|
||||
css += table.AsCSSNamed(sp.Member, st)
|
||||
cssName = sp.Member
|
||||
@@ -207,6 +203,17 @@ func ComposeMain(page any, mainTable any, callLib CallLib) (any, error) {
|
||||
"css": cssName,
|
||||
})
|
||||
}
|
||||
return binds, css, nil
|
||||
}
|
||||
|
||||
// ComposeMain merges a main bind table into a page bag.
|
||||
func ComposeMain(page any, mainTable any, callLib CallLib) (any, error) {
|
||||
obj := asObject(page)
|
||||
binds, cssExtra, err := resolveBinds(mainTable, callLib)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
css := strField(obj, "styles_css") + cssExtra
|
||||
|
||||
if t, ok := table.BindTableName(binds); ok {
|
||||
obj["table"] = t
|
||||
@@ -234,6 +241,41 @@ func ComposeMain(page any, mainTable any, callLib CallLib) (any, error) {
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
// ComposeList appends a secondary bind (detail + child list), with optional
|
||||
// query/order/target (inject into intro element id, same as form_target).
|
||||
func ComposeList(page any, mainTable any, query any, order, target string, callLib CallLib) (any, error) {
|
||||
obj := asObject(page)
|
||||
binds, cssExtra, err := resolveBinds(mainTable, callLib)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if cssExtra != "" {
|
||||
prev := strField(obj, "styles_css")
|
||||
if prev != "" {
|
||||
obj["styles_css"] = prev + cssExtra
|
||||
} else {
|
||||
obj["styles_css"] = cssExtra
|
||||
}
|
||||
}
|
||||
entry := map[string]any{"main": binds}
|
||||
if query != nil {
|
||||
entry["query"] = query
|
||||
}
|
||||
if strings.TrimSpace(order) != "" {
|
||||
entry["order"] = strings.TrimSpace(order)
|
||||
}
|
||||
if t := strings.TrimSpace(target); t != "" {
|
||||
entry["target"] = t
|
||||
}
|
||||
lists := []any{}
|
||||
if existing, ok := obj["lists"].([]any); ok {
|
||||
lists = append(lists, existing...)
|
||||
}
|
||||
lists = append(lists, entry)
|
||||
obj["lists"] = lists
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
// ComposeForm embeds a form into the page main slot.
|
||||
func ComposeForm(page any, form any, formID string, formTarget *string) (any, error) {
|
||||
id := strings.TrimSpace(formID)
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
package compose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ComposeAuthForm stamps page.auth_form for progressive-enhancement login/register.
|
||||
// action is the form POST path (e.g. /login, /desk/login, /register).
|
||||
func ComposeAuthForm(page any, action, submit, formID, errID, target, nextDefault, kind string) (any, error) {
|
||||
action = strings.TrimSpace(action)
|
||||
if action == "" {
|
||||
return nil, fmt.Errorf("compose_auth_form requires non-empty action")
|
||||
}
|
||||
obj := asObject(page)
|
||||
kind = strings.TrimSpace(strings.ToLower(kind))
|
||||
if kind == "" {
|
||||
if strings.Contains(strings.ToLower(action), "register") {
|
||||
kind = "register"
|
||||
} else {
|
||||
kind = "login"
|
||||
}
|
||||
}
|
||||
spec := map[string]any{
|
||||
"action": action,
|
||||
"kind": kind,
|
||||
}
|
||||
if s := strings.TrimSpace(submit); s != "" {
|
||||
spec["submit"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(formID); s != "" {
|
||||
spec["form_id"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(errID); s != "" {
|
||||
spec["err_id"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(target); s != "" {
|
||||
spec["target"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(nextDefault); s != "" {
|
||||
spec["next"] = s
|
||||
}
|
||||
obj["auth_form"] = spec
|
||||
return obj, nil
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package compose
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ComposeFormLoad stamps page.form_load so render prefills the composed form
|
||||
// from DB by route/query id (desk edit pages without author JS).
|
||||
func ComposeFormLoad(page any, table, idParam string) (any, error) {
|
||||
obj := asObject(page)
|
||||
table = strings.TrimSpace(table)
|
||||
if table == "" {
|
||||
return nil, fmt.Errorf("compose_form_load requires non-empty table")
|
||||
}
|
||||
spec := map[string]any{"table": table}
|
||||
if s := strings.TrimSpace(idParam); s != "" {
|
||||
spec["id_param"] = s
|
||||
} else {
|
||||
spec["id_param"] = "id"
|
||||
}
|
||||
obj["form_load"] = spec
|
||||
return obj, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package compose
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestComposeFormLoad(t *testing.T) {
|
||||
page := map[string]any{"title": "Edit"}
|
||||
out, err := ComposeFormLoad(page, "posts", "")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
obj, ok := out.(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("want map, got %T", out)
|
||||
}
|
||||
load, ok := obj["form_load"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("missing form_load: %#v", obj)
|
||||
}
|
||||
if load["table"] != "posts" {
|
||||
t.Fatalf("table=%v", load["table"])
|
||||
}
|
||||
if load["id_param"] != "id" {
|
||||
t.Fatalf("id_param=%v", load["id_param"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeFormLoadRequiresTable(t *testing.T) {
|
||||
_, err := ComposeFormLoad(map[string]any{}, " ", "id")
|
||||
if err == nil {
|
||||
t.Fatal("expected error for empty table")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package compose
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestComposeListAppends(t *testing.T) {
|
||||
page := map[string]any{"title": "p"}
|
||||
main := []any{
|
||||
map[string]any{"front": "title", "back": "comments.author", "css": ""},
|
||||
map[string]any{"front": "body", "back": "comments.body", "css": ""},
|
||||
}
|
||||
q := []any{map[string]any{"field": "post_slug", "op": "=", "value": "{slug}"}}
|
||||
out, err := ComposeList(page, main, q, "-created_at", "#comment-list", func(string) (any, error) {
|
||||
return nil, nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := out.(map[string]any)
|
||||
lists, ok := m["lists"].([]any)
|
||||
if !ok || len(lists) != 1 {
|
||||
t.Fatalf("lists=%v", m["lists"])
|
||||
}
|
||||
entry := lists[0].(map[string]any)
|
||||
if entry["order"] != "-created_at" || entry["target"] != "#comment-list" {
|
||||
t.Fatalf("entry=%v", entry)
|
||||
}
|
||||
if entry["query"] == nil {
|
||||
t.Fatal("missing query")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package compose
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ComposeNavBrand stamps page.nav_brand for SSR topnav lockup (logo + title),
|
||||
// theme toggle, and drawer button chrome. Client WASM only wires behavior.
|
||||
func ComposeNavBrand(page any, title, href, logo, logoLight, themeKey string) (any, error) {
|
||||
obj := asObject(page)
|
||||
spec := map[string]any{}
|
||||
if s := strings.TrimSpace(title); s != "" {
|
||||
spec["title"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(href); s != "" {
|
||||
spec["href"] = s
|
||||
} else {
|
||||
spec["href"] = "/"
|
||||
}
|
||||
if s := strings.TrimSpace(logo); s != "" {
|
||||
spec["logo"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(logoLight); s != "" {
|
||||
spec["logo_light"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(themeKey); s != "" {
|
||||
spec["theme_key"] = s
|
||||
} else {
|
||||
spec["theme_key"] = "mq-theme"
|
||||
}
|
||||
obj["nav_brand"] = spec
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
// ComposeClient stamps page.client so render injects official bridge auto-mount
|
||||
// (data-mq-wasm / data-mq-source-url). Authors ship client.mq.md, not business JS.
|
||||
func ComposeClient(page any, bridge, wasm, source string) (any, error) {
|
||||
obj := asObject(page)
|
||||
spec := map[string]any{}
|
||||
if s := strings.TrimSpace(bridge); s != "" {
|
||||
spec["bridge"] = s
|
||||
} else {
|
||||
spec["bridge"] = "/static/marqdo-bridge.js"
|
||||
}
|
||||
if s := strings.TrimSpace(wasm); s != "" {
|
||||
spec["wasm"] = s
|
||||
} else {
|
||||
spec["wasm"] = "/static/marqdo_wasm.wasm"
|
||||
}
|
||||
if s := strings.TrimSpace(source); s != "" {
|
||||
spec["source"] = s
|
||||
}
|
||||
obj["client"] = spec
|
||||
return obj, nil
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package compose_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/compose"
|
||||
)
|
||||
|
||||
func TestComposeNavBrandAndClient(t *testing.T) {
|
||||
page := map[string]any{"title": "t"}
|
||||
out, err := compose.ComposeNavBrand(page, "求道量子", "/", "/static/logo.png", "/static/logo-light.png", "mq-theme")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m := out.(map[string]any)
|
||||
b, ok := m["nav_brand"].(map[string]any)
|
||||
if !ok || b["title"] != "求道量子" || b["logo"] != "/static/logo.png" {
|
||||
t.Fatalf("nav_brand: %#v", m["nav_brand"])
|
||||
}
|
||||
out, err = compose.ComposeClient(m, "", "", "/static/client.mq.md")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
m = out.(map[string]any)
|
||||
c, ok := m["client"].(map[string]any)
|
||||
if !ok || c["source"] != "/static/client.mq.md" {
|
||||
t.Fatalf("client: %#v", m["client"])
|
||||
}
|
||||
if c["bridge"] != "/static/marqdo-bridge.js" {
|
||||
t.Fatalf("bridge default: %#v", c)
|
||||
}
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/table"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
_ "modernc.org/sqlite"
|
||||
)
|
||||
|
||||
@@ -54,13 +55,27 @@ func pathOf(url string) string {
|
||||
}
|
||||
|
||||
func open(url string) (*sql.DB, error) {
|
||||
path := pathOf(url)
|
||||
poolMu.Lock()
|
||||
defer poolMu.Unlock()
|
||||
key := ResolveURL(url)
|
||||
if db, ok := pool[key]; ok {
|
||||
return db, nil
|
||||
}
|
||||
if IsPostgres(url) {
|
||||
db, err := sql.Open("pgx", key)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db.SetMaxOpenConns(16)
|
||||
db.SetMaxIdleConns(4)
|
||||
if err := db.Ping(); err != nil {
|
||||
_ = db.Close()
|
||||
return nil, fmt.Errorf("postgres ping: %w", err)
|
||||
}
|
||||
pool[key] = db
|
||||
return db, nil
|
||||
}
|
||||
path := pathOf(url)
|
||||
if parent := filepath.Dir(path); parent != "" && parent != "." {
|
||||
if err := os.MkdirAll(parent, 0o755); err != nil {
|
||||
return nil, err
|
||||
@@ -78,8 +93,8 @@ func open(url string) (*sql.DB, error) {
|
||||
return db, nil
|
||||
}
|
||||
|
||||
// OpenSQL returns a pooled *sql.DB for sqlite: (and existing) URLs.
|
||||
// Used by packages that need raw SQL beyond CRUD helpers (e.g. rbac).
|
||||
// OpenSQL returns a pooled *sql.DB (sqlite or postgres). Prefer OpenSQLDB when
|
||||
// issuing `?` placeholders against Postgres (RBAC).
|
||||
func OpenSQL(url string) (*sql.DB, error) {
|
||||
return open(url)
|
||||
}
|
||||
@@ -123,6 +138,17 @@ func sqlType(t string) string {
|
||||
}
|
||||
}
|
||||
|
||||
func sqlTypeFor(url, t string) string {
|
||||
ty := sqlType(t)
|
||||
if IsPostgres(url) && ty == "REAL" {
|
||||
return "DOUBLE PRECISION"
|
||||
}
|
||||
if IsPostgres(url) && ty == "BLOB" {
|
||||
return "BYTEA"
|
||||
}
|
||||
return ty
|
||||
}
|
||||
|
||||
func cellStr(v any) string {
|
||||
switch t := v.(type) {
|
||||
case nil:
|
||||
@@ -242,11 +268,15 @@ func Init(url, name string, fields any) (map[string]any, error) {
|
||||
nullable := boolField(m, []string{"nullable", "可空"}, true)
|
||||
unique := boolField(m, []string{"unique", "唯一"}, false)
|
||||
index := boolField(m, []string{"index", "索引"}, false)
|
||||
col := fmt.Sprintf(`"%s" %s`, colName, sqlType(ty))
|
||||
col := fmt.Sprintf(`"%s" %s`, colName, sqlTypeFor(url, ty))
|
||||
if colName == "id" && !hasPK {
|
||||
col += " PRIMARY KEY"
|
||||
if sqlType(ty) == "INTEGER" {
|
||||
col += " AUTOINCREMENT"
|
||||
if IsPostgres(url) && sqlType(ty) == "INTEGER" {
|
||||
col = fmt.Sprintf(`"%s" INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY`, colName)
|
||||
} else {
|
||||
col += " PRIMARY KEY"
|
||||
if sqlType(ty) == "INTEGER" {
|
||||
col += " AUTOINCREMENT"
|
||||
}
|
||||
}
|
||||
hasPK = true
|
||||
} else if !nullable {
|
||||
@@ -298,7 +328,26 @@ func Init(url, name string, fields any) (map[string]any, error) {
|
||||
}, nil
|
||||
}
|
||||
|
||||
func columnNames(c dbConn, tableName string) ([]string, error) {
|
||||
func columnNames(c dbConn, tableName string, dbURL string) ([]string, error) {
|
||||
if IsPostgres(dbURL) {
|
||||
rows, err := c.Query(`
|
||||
SELECT column_name FROM information_schema.columns
|
||||
WHERE table_schema = current_schema() AND table_name = ?
|
||||
ORDER BY ordinal_position`, tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []string
|
||||
for rows.Next() {
|
||||
var name string
|
||||
if err := rows.Scan(&name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out = append(out, name)
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
rows, err := c.Query(fmt.Sprintf(`PRAGMA table_info("%s")`, tableName))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -380,7 +429,7 @@ func Insert(url, tableName string, rows any, txnID ...string) (map[string]any, e
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
colsPresent, err := columnNames(c, tn)
|
||||
colsPresent, err := columnNames(c, tn, url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -875,7 +924,7 @@ func Update(url, tableName, id string, row any, txnID ...string) (map[string]any
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
colsPresent, err := columnNames(c, tn)
|
||||
colsPresent, err := columnNames(c, tn, url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -996,17 +1045,52 @@ type ColumnInfo struct {
|
||||
PK bool
|
||||
}
|
||||
|
||||
// TableInfo returns column metadata for a SQLite table.
|
||||
// TableInfo returns column metadata for a table (SQLite or Postgres).
|
||||
func TableInfo(url, tableName string) ([]ColumnInfo, error) {
|
||||
tn, err := ident(tableName)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
db, err := open(url)
|
||||
c, err := connFor(url, "")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
rows, err := db.Query(fmt.Sprintf(`PRAGMA table_info("%s")`, tn))
|
||||
if IsPostgres(url) {
|
||||
rows, err := c.Query(`
|
||||
SELECT c.column_name, c.data_type, c.is_nullable,
|
||||
CASE WHEN tc.constraint_type = 'PRIMARY KEY' THEN 1 ELSE 0 END AS is_pk
|
||||
FROM information_schema.columns c
|
||||
LEFT JOIN information_schema.key_column_usage kcu
|
||||
ON c.table_schema = kcu.table_schema AND c.table_name = kcu.table_name AND c.column_name = kcu.column_name
|
||||
LEFT JOIN information_schema.table_constraints tc
|
||||
ON kcu.constraint_name = tc.constraint_name AND kcu.table_schema = tc.table_schema
|
||||
AND tc.constraint_type = 'PRIMARY KEY'
|
||||
WHERE c.table_schema = current_schema() AND c.table_name = ?
|
||||
ORDER BY c.ordinal_position`, tn)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var out []ColumnInfo
|
||||
for rows.Next() {
|
||||
var name, dtype, nullable string
|
||||
var pk int
|
||||
if err := rows.Scan(&name, &dtype, &nullable, &pk); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if dtype == "" {
|
||||
dtype = "TEXT"
|
||||
}
|
||||
out = append(out, ColumnInfo{
|
||||
Name: name,
|
||||
SQLType: dtype,
|
||||
NotNull: strings.EqualFold(nullable, "NO"),
|
||||
PK: pk != 0,
|
||||
})
|
||||
}
|
||||
return out, rows.Err()
|
||||
}
|
||||
rows, err := c.Query(fmt.Sprintf(`PRAGMA table_info("%s")`, tn))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,144 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// IsPostgres reports whether url selects the Postgres backend.
|
||||
func IsPostgres(url string) bool {
|
||||
lower := strings.ToLower(strings.TrimSpace(url))
|
||||
return strings.HasPrefix(lower, "postgres://") || strings.HasPrefix(lower, "postgresql://")
|
||||
}
|
||||
|
||||
// RewritePlaceholdersPG turns `?` into `$1,$2,…` for Postgres.
|
||||
func RewritePlaceholdersPG(sqlStmt string) string {
|
||||
var b strings.Builder
|
||||
b.Grow(len(sqlStmt) + 8)
|
||||
n := 0
|
||||
for _, c := range sqlStmt {
|
||||
if c == '?' {
|
||||
n++
|
||||
b.WriteByte('$')
|
||||
b.WriteString(itoa(n))
|
||||
} else {
|
||||
b.WriteRune(c)
|
||||
}
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
func itoa(n int) string {
|
||||
// small positive ints only
|
||||
if n < 10 {
|
||||
return string(rune('0' + n))
|
||||
}
|
||||
var buf [12]byte
|
||||
i := len(buf)
|
||||
for n > 0 {
|
||||
i--
|
||||
buf[i] = byte('0' + n%10)
|
||||
n /= 10
|
||||
}
|
||||
return string(buf[i:])
|
||||
}
|
||||
|
||||
// rewritingDB rewrites `?` placeholders when talking to Postgres.
|
||||
type rewritingDB struct {
|
||||
db *sql.DB
|
||||
pg bool
|
||||
}
|
||||
|
||||
func (d *rewritingDB) Exec(query string, args ...any) (sql.Result, error) {
|
||||
if d.pg {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return d.db.Exec(query, args...)
|
||||
}
|
||||
|
||||
func (d *rewritingDB) Query(query string, args ...any) (*sql.Rows, error) {
|
||||
if d.pg {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return d.db.Query(query, args...)
|
||||
}
|
||||
|
||||
func (d *rewritingDB) QueryRow(query string, args ...any) *sql.Row {
|
||||
if d.pg {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return d.db.QueryRow(query, args...)
|
||||
}
|
||||
|
||||
func (d *rewritingDB) Begin() (*sql.Tx, error) {
|
||||
return d.db.Begin()
|
||||
}
|
||||
|
||||
func (d *rewritingDB) Underlying() *sql.DB { return d.db }
|
||||
|
||||
type rewritingTx struct {
|
||||
tx *sql.Tx
|
||||
pg bool
|
||||
}
|
||||
|
||||
func (t *rewritingTx) Exec(query string, args ...any) (sql.Result, error) {
|
||||
if t.pg {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return t.tx.Exec(query, args...)
|
||||
}
|
||||
|
||||
func (t *rewritingTx) Query(query string, args ...any) (*sql.Rows, error) {
|
||||
if t.pg {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return t.tx.Query(query, args...)
|
||||
}
|
||||
|
||||
func (t *rewritingTx) QueryRow(query string, args ...any) *sql.Row {
|
||||
if t.pg {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return t.tx.QueryRow(query, args...)
|
||||
}
|
||||
|
||||
func (t *rewritingTx) Commit() error { return t.tx.Commit() }
|
||||
func (t *rewritingTx) Rollback() error { return t.tx.Rollback() }
|
||||
|
||||
// SQLDB is *sql.DB with optional Postgres `?` rewrite (RBAC / admin).
|
||||
type SQLDB interface {
|
||||
Exec(query string, args ...any) (sql.Result, error)
|
||||
Query(query string, args ...any) (*sql.Rows, error)
|
||||
QueryRow(query string, args ...any) *sql.Row
|
||||
Begin() (SQLTx, error)
|
||||
}
|
||||
|
||||
// SQLTx is a transaction with optional placeholder rewrite.
|
||||
type SQLTx interface {
|
||||
Exec(query string, args ...any) (sql.Result, error)
|
||||
Query(query string, args ...any) (*sql.Rows, error)
|
||||
QueryRow(query string, args ...any) *sql.Row
|
||||
Commit() error
|
||||
Rollback() error
|
||||
}
|
||||
|
||||
type sqlDBHandle struct {
|
||||
*rewritingDB
|
||||
}
|
||||
|
||||
func (h *sqlDBHandle) Begin() (SQLTx, error) {
|
||||
tx, err := h.rewritingDB.Begin()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rewritingTx{tx: tx, pg: h.pg}, nil
|
||||
}
|
||||
|
||||
// OpenSQLDB returns a pooled handle that rewrites `?` on Postgres.
|
||||
func OpenSQLDB(url string) (SQLDB, error) {
|
||||
raw, err := open(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &sqlDBHandle{rewritingDB: &rewritingDB{db: raw, pg: IsPostgres(url)}}, nil
|
||||
}
|
||||
@@ -23,14 +23,23 @@ type txnConn struct {
|
||||
}
|
||||
|
||||
func (t *txnConn) Exec(query string, args ...any) (sql.Result, error) {
|
||||
if IsPostgres(t.url) {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return t.conn.ExecContext(context.Background(), query, args...)
|
||||
}
|
||||
|
||||
func (t *txnConn) Query(query string, args ...any) (*sql.Rows, error) {
|
||||
if IsPostgres(t.url) {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return t.conn.QueryContext(context.Background(), query, args...)
|
||||
}
|
||||
|
||||
func (t *txnConn) QueryRow(query string, args ...any) *sql.Row {
|
||||
if IsPostgres(t.url) {
|
||||
query = RewritePlaceholdersPG(query)
|
||||
}
|
||||
return t.conn.QueryRowContext(context.Background(), query, args...)
|
||||
}
|
||||
|
||||
@@ -74,7 +83,11 @@ func connFor(url, txnID string) (dbConn, error) {
|
||||
}
|
||||
return t, nil
|
||||
}
|
||||
return open(url)
|
||||
raw, err := open(url)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &rewritingDB{db: raw, pg: IsPostgres(url)}, nil
|
||||
}
|
||||
|
||||
func takeTxn(txnID string) (*txnConn, error) {
|
||||
@@ -98,7 +111,7 @@ func resetTxns() {
|
||||
}
|
||||
}
|
||||
|
||||
// Begin starts a SQLite transaction (BEGIN IMMEDIATE).
|
||||
// Begin starts a transaction (BEGIN IMMEDIATE on SQLite, BEGIN on Postgres).
|
||||
// Returns {"_type":"txn","txn":id,"url":url,"事务":id,"地址":url}.
|
||||
func Begin(url string) (map[string]any, error) {
|
||||
db, err := open(url)
|
||||
@@ -109,7 +122,11 @@ func Begin(url string) (map[string]any, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := conn.ExecContext(context.Background(), "BEGIN IMMEDIATE"); err != nil {
|
||||
beginSQL := "BEGIN IMMEDIATE"
|
||||
if IsPostgres(url) {
|
||||
beginSQL = "BEGIN"
|
||||
}
|
||||
if _, err := conn.ExecContext(context.Background(), beginSQL); err != nil {
|
||||
_ = conn.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
package form
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -17,6 +18,7 @@ var (
|
||||
typeKeys = []string{"类型", "type"}
|
||||
reqKeys = []string{"必填", "required"}
|
||||
defKeys = []string{"默认", "default"}
|
||||
sourceKeys = []string{"来源", "source"}
|
||||
ruleFieldKeys = []string{"字段", "field", "name"}
|
||||
ruleKeys = []string{"规则", "rule"}
|
||||
msgKeys = []string{"消息", "message", "msg"}
|
||||
@@ -45,6 +47,8 @@ func normalizeAction(action string) string {
|
||||
switch action {
|
||||
case "update", "更新":
|
||||
return "update"
|
||||
case "delete", "删除":
|
||||
return "delete"
|
||||
case "":
|
||||
return "insert"
|
||||
default:
|
||||
@@ -66,6 +70,24 @@ func SetRules(formBag map[string]any, rules any) map[string]any {
|
||||
return out
|
||||
}
|
||||
|
||||
// SetLabels stamps localized submit / cancel button copy (and optional cancel href).
|
||||
// When cancelHref is set, also stamps redirect so successful POST lands on the list
|
||||
// (or other cancel target) instead of the composing page's _mq_return path.
|
||||
func SetLabels(formBag map[string]any, submit, cancel, cancelHref string) map[string]any {
|
||||
out := cloneMap(formBag)
|
||||
if s := strings.TrimSpace(submit); s != "" {
|
||||
out["submit"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(cancel); s != "" {
|
||||
out["cancel"] = s
|
||||
}
|
||||
if s := strings.TrimSpace(cancelHref); s != "" {
|
||||
out["cancel_href"] = s
|
||||
out["redirect"] = s
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func cloneMap(m map[string]any) map[string]any {
|
||||
out := map[string]any{}
|
||||
for k, v := range m {
|
||||
@@ -177,12 +199,19 @@ func asFormFields(fields any) []any {
|
||||
if v, ok := pick(m, defKeys); ok {
|
||||
def = cellStr(v)
|
||||
}
|
||||
source := "client"
|
||||
if v, ok := pick(m, sourceKeys); ok {
|
||||
if s := strings.TrimSpace(cellStr(v)); s != "" {
|
||||
source = s
|
||||
}
|
||||
}
|
||||
item := map[string]any{
|
||||
"name": name,
|
||||
"label": label,
|
||||
"type": ty,
|
||||
"required": required,
|
||||
"default": def,
|
||||
"source": normalizeSource(source),
|
||||
}
|
||||
if v, ok := m["pk"]; ok {
|
||||
item["pk"] = boolish(v)
|
||||
@@ -211,6 +240,10 @@ func asFormFields(fields any) []any {
|
||||
if v, ok := pick(t, defKeys); ok {
|
||||
defs = asStrList(v)
|
||||
}
|
||||
sources := []string{}
|
||||
if v, ok := pick(t, sourceKeys); ok {
|
||||
sources = asStrList(v)
|
||||
}
|
||||
var out []any
|
||||
for i, nameRaw := range names {
|
||||
name := table.NormalizeRef(nameRaw)
|
||||
@@ -233,12 +266,17 @@ func asFormFields(fields any) []any {
|
||||
if i < len(defs) {
|
||||
def = defs[i]
|
||||
}
|
||||
source := "client"
|
||||
if i < len(sources) && strings.TrimSpace(sources[i]) != "" {
|
||||
source = sources[i]
|
||||
}
|
||||
out = append(out, map[string]any{
|
||||
"name": name,
|
||||
"label": label,
|
||||
"type": ty,
|
||||
"required": required,
|
||||
"default": def,
|
||||
"source": normalizeSource(source),
|
||||
})
|
||||
}
|
||||
return out
|
||||
@@ -614,6 +652,11 @@ func strOf(m map[string]any, key, def string) string {
|
||||
|
||||
// RenderBody emits form markup only (no document shell).
|
||||
func RenderBody(formBag map[string]any, formID string, data, errors any, csrf string) string {
|
||||
return RenderBodyCtx(formBag, formID, data, errors, csrf, nil)
|
||||
}
|
||||
|
||||
// RenderBodyCtx is RenderBody plus request-context hidden fields for POST.
|
||||
func RenderBodyCtx(formBag map[string]any, formID string, data, errors any, csrf string, ctx *RequestContext) string {
|
||||
fields := fieldSlice(formBag)
|
||||
dataM := map[string]any{}
|
||||
if data != nil {
|
||||
@@ -646,6 +689,9 @@ func RenderBody(formBag map[string]any, formID string, data, errors any, csrf st
|
||||
if formBag != nil {
|
||||
rowID = cellStr(formBag["id"])
|
||||
}
|
||||
if rowID == "" {
|
||||
rowID = fieldText(dataM, "id")
|
||||
}
|
||||
showMeta := true
|
||||
if formBag != nil {
|
||||
if v, ok := formBag["show_meta"].(bool); ok {
|
||||
@@ -654,21 +700,28 @@ func RenderBody(formBag map[string]any, formID string, data, errors any, csrf st
|
||||
}
|
||||
|
||||
var body strings.Builder
|
||||
body.WriteString(`<div class="site-form">`)
|
||||
if showMeta {
|
||||
fmt.Fprintf(&body, `<p class="meta">table=<code>%s</code> · action=<code>%s</code> · id=<code>%s</code></p>`,
|
||||
esc(tableName), esc(action), esc(heading))
|
||||
}
|
||||
hasFile := false
|
||||
hasMarkdown := false
|
||||
for _, f := range fields {
|
||||
if fm, ok := f.(map[string]any); ok {
|
||||
ty, _ := fm["type"].(string)
|
||||
if strings.EqualFold(ty, "file") {
|
||||
hasFile = true
|
||||
break
|
||||
}
|
||||
if strings.EqualFold(ty, "markdown") {
|
||||
hasMarkdown = true
|
||||
}
|
||||
}
|
||||
}
|
||||
formClass := "site-form"
|
||||
if hasMarkdown {
|
||||
formClass += " editor-skin"
|
||||
}
|
||||
body.WriteString(`<div class="` + formClass + `">`)
|
||||
if showMeta {
|
||||
fmt.Fprintf(&body, `<p class="meta">table=<code>%s</code> · action=<code>%s</code> · id=<code>%s</code></p>`,
|
||||
esc(tableName), esc(action), esc(heading))
|
||||
}
|
||||
body.WriteString(`<form method="post" action="` + esc(postTo) + `"`)
|
||||
if hasFile {
|
||||
body.WriteString(` enctype="multipart/form-data"`)
|
||||
@@ -677,9 +730,32 @@ func RenderBody(formBag map[string]any, formID string, data, errors any, csrf st
|
||||
if csrf != "" {
|
||||
fmt.Fprintf(&body, `<input type="hidden" name="_csrf" value="%s"/>`, esc(csrf))
|
||||
}
|
||||
if action == "update" && rowID != "" {
|
||||
hasIDField := false
|
||||
for _, f := range fields {
|
||||
if fm, ok := f.(map[string]any); ok {
|
||||
if strOf(fm, "name", "") == "id" && IsClientSource(strOf(fm, "source", "client")) {
|
||||
hasIDField = true
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if (action == "update" || action == "delete") && rowID != "" && !hasIDField {
|
||||
fmt.Fprintf(&body, `<input type="hidden" name="id" value="%s"/>`, esc(rowID))
|
||||
}
|
||||
if ctx != nil {
|
||||
if len(ctx.Params) > 0 {
|
||||
if b, err := json.Marshal(ctx.Params); err == nil {
|
||||
fmt.Fprintf(&body, `<input type="hidden" name="_mq_params" value="%s"/>`, esc(string(b)))
|
||||
}
|
||||
}
|
||||
ret := strings.TrimSpace(ctx.ReturnPath)
|
||||
if ret == "" {
|
||||
ret = strings.TrimSpace(ctx.Path)
|
||||
}
|
||||
if ret != "" && strings.HasPrefix(ret, "/") && !strings.HasPrefix(ret, "//") {
|
||||
fmt.Fprintf(&body, `<input type="hidden" name="_mq_return" value="%s"/>`, esc(ret))
|
||||
}
|
||||
}
|
||||
for _, f := range fields {
|
||||
fm, ok := f.(map[string]any)
|
||||
if !ok {
|
||||
@@ -689,6 +765,9 @@ func RenderBody(formBag map[string]any, formID string, data, errors any, csrf st
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
if !IsClientSource(strOf(fm, "source", "client")) {
|
||||
continue
|
||||
}
|
||||
label := strOf(fm, "label", name)
|
||||
ty := strOf(fm, "type", "text")
|
||||
required, _ := fm["required"].(bool)
|
||||
@@ -712,11 +791,26 @@ func RenderBody(formBag map[string]any, formID string, data, errors any, csrf st
|
||||
if required {
|
||||
reqAttr = " required"
|
||||
}
|
||||
if ty == "hidden" {
|
||||
fmt.Fprintf(&body, `<input type="hidden" name="%s" value="%s"/>`, esc(name), esc(value))
|
||||
continue
|
||||
}
|
||||
body.WriteString("<label>")
|
||||
body.WriteString(esc(label))
|
||||
if ty == "textarea" {
|
||||
fmt.Fprintf(&body, `<textarea name="%s" rows="5"%s>%s</textarea>`,
|
||||
esc(name), reqAttr, esc(value))
|
||||
if ty == "textarea" || ty == "markdown" {
|
||||
rows := "5"
|
||||
cls := ""
|
||||
if ty == "markdown" {
|
||||
rows = "18"
|
||||
cls = ` class="mq-markdown" data-mq-field="markdown"`
|
||||
body.WriteString(`<div class="md-field">`)
|
||||
fmt.Fprintf(&body, `<textarea name="%s" rows="%s"%s%s>%s</textarea>`,
|
||||
esc(name), rows, cls, reqAttr, esc(value))
|
||||
body.WriteString(`<div class="mq-md-preview md-pane-preview" aria-live="polite"></div></div>`)
|
||||
} else {
|
||||
fmt.Fprintf(&body, `<textarea name="%s" rows="%s"%s%s>%s</textarea>`,
|
||||
esc(name), rows, cls, reqAttr, esc(value))
|
||||
}
|
||||
} else {
|
||||
inputType := "text"
|
||||
switch ty {
|
||||
@@ -743,8 +837,28 @@ func RenderBody(formBag map[string]any, formID string, data, errors any, csrf st
|
||||
}
|
||||
body.WriteString("</label>")
|
||||
}
|
||||
fmt.Fprintf(&body, `<div class="actions"><button type="submit">Submit</button><a href="%s">cancel</a></div>`,
|
||||
esc(cancel))
|
||||
submitLabel := strOf(formBag, "submit", "")
|
||||
if submitLabel == "" {
|
||||
submitLabel = strOf(formBag, "提交", "")
|
||||
}
|
||||
if submitLabel == "" {
|
||||
submitLabel = "Submit"
|
||||
}
|
||||
cancelLabel := strOf(formBag, "cancel", "")
|
||||
if cancelLabel == "" {
|
||||
cancelLabel = strOf(formBag, "取消", "")
|
||||
}
|
||||
if cancelLabel == "" {
|
||||
cancelLabel = "cancel"
|
||||
}
|
||||
cancelHref := cancel
|
||||
if c := strOf(formBag, "cancel_href", ""); c != "" {
|
||||
cancelHref = c
|
||||
} else if c := strOf(formBag, "取消链接", ""); c != "" {
|
||||
cancelHref = c
|
||||
}
|
||||
fmt.Fprintf(&body, `<div class="actions"><button type="submit">%s</button><a href="%s">%s</a></div>`,
|
||||
esc(submitLabel), esc(cancelHref), esc(cancelLabel))
|
||||
body.WriteString(`</form></div>`)
|
||||
return body.String()
|
||||
}
|
||||
@@ -784,7 +898,7 @@ body{font-family:"IBM Plex Sans","Noto Sans SC",sans-serif;margin:1.5rem;backgro
|
||||
func Submit(formBag map[string]any, data any, dbURL string) (map[string]any, error) {
|
||||
action := strOf(formBag, "action", "insert")
|
||||
rowMap := dataMap(data)
|
||||
if action == "update" {
|
||||
if action == "update" || action == "delete" {
|
||||
if formBag != nil {
|
||||
if fid := cellStr(formBag["id"]); fid != "" {
|
||||
if _, ok := rowMap["id"]; !ok {
|
||||
@@ -793,14 +907,16 @@ func Submit(formBag map[string]any, data any, dbURL string) (map[string]any, err
|
||||
}
|
||||
}
|
||||
}
|
||||
v := Validate(formBag, nil, rowMap)
|
||||
ok, _ := v["ok"].(bool)
|
||||
if !ok {
|
||||
errs := v["errors"]
|
||||
if errs == nil {
|
||||
errs = []any{}
|
||||
if action != "delete" {
|
||||
v := Validate(formBag, nil, rowMap)
|
||||
ok, _ := v["ok"].(bool)
|
||||
if !ok {
|
||||
errs := v["errors"]
|
||||
if errs == nil {
|
||||
errs = []any{}
|
||||
}
|
||||
return map[string]any{"ok": false, "errors": errs}, nil
|
||||
}
|
||||
return map[string]any{"ok": false, "errors": errs}, nil
|
||||
}
|
||||
tableName := ""
|
||||
if formBag != nil {
|
||||
@@ -811,7 +927,20 @@ func Submit(formBag map[string]any, data any, dbURL string) (map[string]any, err
|
||||
}
|
||||
var result map[string]any
|
||||
var err error
|
||||
if action == "update" {
|
||||
switch action {
|
||||
case "delete":
|
||||
id := ""
|
||||
if formBag != nil {
|
||||
id = cellStr(formBag["id"])
|
||||
}
|
||||
if id == "" {
|
||||
id = cellStr(rowMap["id"])
|
||||
}
|
||||
if id == "" {
|
||||
return nil, fmt.Errorf("delete requires `id`")
|
||||
}
|
||||
result, err = db.Delete(dbURL, tableName, id)
|
||||
case "update":
|
||||
id := ""
|
||||
if formBag != nil {
|
||||
id = cellStr(formBag["id"])
|
||||
@@ -823,7 +952,7 @@ func Submit(formBag map[string]any, data any, dbURL string) (map[string]any, err
|
||||
return nil, fmt.Errorf("update requires `id`")
|
||||
}
|
||||
result, err = db.Update(dbURL, tableName, id, rowMap)
|
||||
} else {
|
||||
default:
|
||||
result, err = db.Insert(dbURL, tableName, []any{rowMap})
|
||||
}
|
||||
if err != nil {
|
||||
|
||||
@@ -68,3 +68,28 @@ func TestRenderContainsInputFields(t *testing.T) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderMarkdownAndLabels(t *testing.T) {
|
||||
f := form.New("posts", "insert", "")
|
||||
f = form.SetFields(f, []any{
|
||||
map[string]any{"字段": "content", "标签": "正文", "类型": "markdown", "必填": true},
|
||||
})
|
||||
f = form.SetLabels(f, "发布文章", "返回列表", "/desk/posts")
|
||||
if f["redirect"] != "/desk/posts" || f["cancel_href"] != "/desk/posts" {
|
||||
t.Fatalf("labels should stamp redirect/cancel_href, got %#v %#v", f["redirect"], f["cancel_href"])
|
||||
}
|
||||
html := form.Render(f, "post", nil, nil, "")
|
||||
for _, want := range []string{
|
||||
`data-mq-field="markdown"`,
|
||||
`class="mq-markdown"`,
|
||||
`class="md-field"`,
|
||||
`mq-md-preview`,
|
||||
">发布文章</button>",
|
||||
`href="/desk/posts"`,
|
||||
">返回列表</a>",
|
||||
} {
|
||||
if !strings.Contains(html, want) {
|
||||
t.Fatalf("missing %q in:\n%s", want, html)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package form
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
func normalizeSource(raw string) string {
|
||||
s := strings.TrimSpace(raw)
|
||||
if s == "" {
|
||||
return "client"
|
||||
}
|
||||
switch strings.ToLower(s) {
|
||||
case "client", "用户", "input", "form":
|
||||
return "client"
|
||||
case "user", "username", "session.user", "session.username", "用户名":
|
||||
return "session.username"
|
||||
case "now", "utc.now", "server.now", "时间", "utc":
|
||||
return "now"
|
||||
case "now_if_empty", "空则现在", "now?":
|
||||
return "now_if_empty"
|
||||
default:
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
// IsClientSource reports whether the field is filled by the browser.
|
||||
func IsClientSource(source string) bool {
|
||||
src := normalizeSource(source)
|
||||
return src == "client" || src == "now_if_empty"
|
||||
}
|
||||
|
||||
// RequestContext carries session user and route params for form source stamping
|
||||
// (design: ext-web-request-context.md). Domain-agnostic — no table-name branches.
|
||||
type RequestContext struct {
|
||||
Username string
|
||||
Params map[string]any
|
||||
ReturnPath string
|
||||
Path string
|
||||
Method string
|
||||
}
|
||||
|
||||
// ApplySources strips client-supplied values for non-client fields, then stamps
|
||||
// from ctx. Always overwrites server-owned keys. `now_if_empty` keeps a non-empty
|
||||
// client value and only stamps UTC now when the field was left blank.
|
||||
func ApplySources(formBag map[string]any, data map[string]any, ctx *RequestContext) {
|
||||
if data == nil {
|
||||
return
|
||||
}
|
||||
if ctx == nil {
|
||||
ctx = &RequestContext{}
|
||||
}
|
||||
for _, f := range fieldSlice(formBag) {
|
||||
fm, ok := f.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
name, _ := fm["name"].(string)
|
||||
if name == "" {
|
||||
continue
|
||||
}
|
||||
src := normalizeSource(strOf(fm, "source", "client"))
|
||||
switch src {
|
||||
case "client":
|
||||
continue
|
||||
case "now_if_empty":
|
||||
if data[name] != nil && strings.TrimSpace(cellStr(data[name])) != "" {
|
||||
continue
|
||||
}
|
||||
if v, ok := resolveSource("now", ctx); ok {
|
||||
data[name] = v
|
||||
}
|
||||
default:
|
||||
delete(data, name)
|
||||
if v, ok := resolveSource(src, ctx); ok {
|
||||
data[name] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func resolveSource(src string, ctx *RequestContext) (any, bool) {
|
||||
src = normalizeSource(src)
|
||||
switch src {
|
||||
case "client":
|
||||
return nil, false
|
||||
case "session.username":
|
||||
if ctx.Username == "" {
|
||||
return nil, false
|
||||
}
|
||||
return ctx.Username, true
|
||||
case "now":
|
||||
return time.Now().UTC().Format("2006-01-02 15:04:05"), true
|
||||
}
|
||||
lower := strings.ToLower(src)
|
||||
for _, prefix := range []string{"route.", "param.", "params."} {
|
||||
if strings.HasPrefix(lower, prefix) {
|
||||
key := src[len(prefix):]
|
||||
if key == "" || ctx.Params == nil {
|
||||
return nil, false
|
||||
}
|
||||
if v, ok := ctx.Params[key]; ok {
|
||||
return v, true
|
||||
}
|
||||
for k, v := range ctx.Params {
|
||||
if strings.EqualFold(k, key) {
|
||||
return v, true
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// ParseMQParams decodes the hidden `_mq_params` JSON posted with forms.
|
||||
func ParseMQParams(raw string) map[string]any {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return nil
|
||||
}
|
||||
var out map[string]any
|
||||
if err := json.Unmarshal([]byte(raw), &out); err != nil {
|
||||
return nil
|
||||
}
|
||||
return out
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package form
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestApplySourcesOverwritesClient(t *testing.T) {
|
||||
frm := SetFields(New("comments", "insert", "c"), []any{
|
||||
map[string]any{"字段": "body", "标签": "body", "类型": "textarea", "必填": true, "来源": "client"},
|
||||
map[string]any{"字段": "author", "标签": "a", "类型": "text", "必填": true, "来源": "session.username"},
|
||||
map[string]any{"字段": "post_slug", "标签": "s", "类型": "text", "必填": true, "来源": "route.slug"},
|
||||
map[string]any{"字段": "created_at", "标签": "t", "类型": "text", "来源": "now"},
|
||||
})
|
||||
data := map[string]any{
|
||||
"body": "hi",
|
||||
"author": "forged",
|
||||
"post_slug": "evil",
|
||||
}
|
||||
ApplySources(frm, data, &RequestContext{
|
||||
Username: "alice",
|
||||
Params: map[string]any{"slug": "hello"},
|
||||
})
|
||||
if data["author"] != "alice" {
|
||||
t.Fatalf("author=%v", data["author"])
|
||||
}
|
||||
if data["post_slug"] != "hello" {
|
||||
t.Fatalf("slug=%v", data["post_slug"])
|
||||
}
|
||||
if data["body"] != "hi" {
|
||||
t.Fatalf("body=%v", data["body"])
|
||||
}
|
||||
if data["created_at"] == nil || data["created_at"] == "" {
|
||||
t.Fatalf("created_at missing")
|
||||
}
|
||||
}
|
||||
|
||||
func TestApplySourcesNowIfEmpty(t *testing.T) {
|
||||
frm := SetFields(New("news", "insert", "n"), []any{
|
||||
map[string]any{"字段": "title", "标签": "t", "类型": "text", "必填": true},
|
||||
map[string]any{"字段": "published_at", "标签": "d", "类型": "text", "来源": "空则现在"},
|
||||
})
|
||||
empty := map[string]any{"title": "x", "published_at": ""}
|
||||
ApplySources(frm, empty, &RequestContext{})
|
||||
if empty["published_at"] == nil || empty["published_at"] == "" {
|
||||
t.Fatalf("expected stamp, got %#v", empty["published_at"])
|
||||
}
|
||||
kept := map[string]any{"title": "x", "published_at": "2020-01-02"}
|
||||
ApplySources(frm, kept, &RequestContext{})
|
||||
if kept["published_at"] != "2020-01-02" {
|
||||
t.Fatalf("should keep client date, got %#v", kept["published_at"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenderSkipsServerSources(t *testing.T) {
|
||||
frm := SetFields(New("comments", "insert", "c"), []any{
|
||||
map[string]any{"字段": "body", "标签": "Body", "类型": "textarea", "必填": true},
|
||||
map[string]any{"字段": "author", "标签": "Author", "类型": "text", "来源": "session.username"},
|
||||
})
|
||||
html := RenderBodyCtx(frm, "c", nil, nil, "", &RequestContext{
|
||||
Username: "alice",
|
||||
Params: map[string]any{"slug": "x"},
|
||||
ReturnPath: "/post/x",
|
||||
})
|
||||
if stringsContains(html, `name="author"`) {
|
||||
t.Fatalf("author field should not render: %s", html)
|
||||
}
|
||||
if !stringsContains(html, `name="body"`) {
|
||||
t.Fatalf("body missing")
|
||||
}
|
||||
if !stringsContains(html, `_mq_params`) || !stringsContains(html, `_mq_return`) {
|
||||
t.Fatalf("missing mq context fields: %s", html)
|
||||
}
|
||||
}
|
||||
|
||||
func stringsContains(s, sub string) bool {
|
||||
return len(s) >= len(sub) && (s == sub || len(sub) == 0 ||
|
||||
(func() bool {
|
||||
for i := 0; i+len(sub) <= len(s); i++ {
|
||||
if s[i:i+len(sub)] == sub {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
})())
|
||||
}
|
||||
@@ -51,6 +51,7 @@ type authConfig struct {
|
||||
logoutRedirect string
|
||||
gates []gate
|
||||
rbac bool
|
||||
rbacDesk bool
|
||||
register bool
|
||||
registerPath string
|
||||
defaultRole string
|
||||
@@ -86,6 +87,7 @@ func authConfigOf(appBag map[string]any) authConfig {
|
||||
cfg.sessionTTL = n
|
||||
}
|
||||
cfg.rbac = boolish(authBag["rbac"])
|
||||
cfg.rbacDesk = boolish(authBag["rbac_desk"])
|
||||
cfg.register = boolish(authBag["register"])
|
||||
if p := strOpt(authBag, "register_path", ""); p != "" {
|
||||
cfg.registerPath = p
|
||||
@@ -245,6 +247,13 @@ func pathExcluded(path string, exclude []string, loginPath string) bool {
|
||||
if path == loginPath || strings.HasPrefix(path, loginPath+"?") {
|
||||
return true
|
||||
}
|
||||
if strings.HasPrefix(path, "/oidc/") {
|
||||
return true
|
||||
}
|
||||
// Local logout must always run; never bounce visitors to login→IdP first.
|
||||
if path == "/logout" || strings.HasSuffix(path, "/logout") {
|
||||
return true
|
||||
}
|
||||
for _, ex := range exclude {
|
||||
ex = strings.TrimRight(strings.TrimSuffix(ex, "*"), "/")
|
||||
if ex == "" {
|
||||
@@ -355,28 +364,46 @@ func withNavAuth(page map[string]any, cookieHeader string) {
|
||||
}
|
||||
}
|
||||
|
||||
func (st *state) hasPageRoute(path string) bool {
|
||||
if path == "" || st.routes == nil {
|
||||
return false
|
||||
}
|
||||
_, ok := st.routes[path]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (st *state) mountAuthRoutes(mux *http.ServeMux) {
|
||||
if st.auth.users == nil && !(st.auth.rbac && st.auth.register) {
|
||||
st.mountOIDCRoutes(mux)
|
||||
if st.auth.users == nil && !(st.auth.rbac && st.auth.register) && !st.oidcEnabled() {
|
||||
return
|
||||
}
|
||||
if st.auth.users != nil || st.auth.rbac {
|
||||
if st.auth.users != nil || st.auth.rbac || st.oidcEnabled() {
|
||||
lp := st.auth.loginPath
|
||||
mux.HandleFunc("GET "+lp, st.handleLoginGet)
|
||||
// Site pages may own GET /login (or /desk/login); always keep POST for auth.
|
||||
if !st.hasPageRoute(lp) {
|
||||
mux.HandleFunc("GET "+lp, st.handleLoginGet)
|
||||
}
|
||||
mux.HandleFunc("POST "+lp, st.handleLoginPost)
|
||||
altLogin := strings.TrimRight(st.auth.adminPrefix, "/") + "/login"
|
||||
if altLogin != lp {
|
||||
mux.HandleFunc("GET "+altLogin, st.handleLoginGet)
|
||||
if !st.hasPageRoute(altLogin) {
|
||||
mux.HandleFunc("GET "+altLogin, st.handleLoginGet)
|
||||
}
|
||||
mux.HandleFunc("POST "+altLogin, st.handleLoginPost)
|
||||
}
|
||||
st.mountExtraLoginPosts(mux, map[string]bool{lp: true, altLogin: true})
|
||||
logoutPath := strings.TrimRight(st.auth.adminPrefix, "/") + "/logout"
|
||||
mux.HandleFunc("GET "+logoutPath, st.handleLogout)
|
||||
}
|
||||
if st.auth.register && st.auth.rbac {
|
||||
if st.auth.register && st.auth.rbac && !st.oidcEnabled() {
|
||||
rp := st.auth.registerPath
|
||||
mux.HandleFunc("GET "+rp, st.handleRegisterGet)
|
||||
if !st.hasPageRoute(rp) {
|
||||
mux.HandleFunc("GET "+rp, st.handleRegisterGet)
|
||||
}
|
||||
mux.HandleFunc("POST "+rp, st.handleRegisterPost)
|
||||
}
|
||||
if st.auth.rbac && st.dbURL != "" {
|
||||
// Opt-in: enable_rbac alone wires gates/schema; /_rbac HTTP surface needs desk=true.
|
||||
if st.auth.rbac && st.auth.rbacDesk && st.dbURL != "" {
|
||||
mux.HandleFunc("GET /_rbac/roles", st.handleRbacListRoles)
|
||||
mux.HandleFunc("GET /_rbac/permissions", st.handleRbacListPermissions)
|
||||
mux.HandleFunc("POST /_rbac/roles", st.handleRbacCreateRole)
|
||||
@@ -394,6 +421,10 @@ func (st *state) mountAuthRoutes(mux *http.ServeMux) {
|
||||
}
|
||||
|
||||
func (st *state) handleLoginGet(w http.ResponseWriter, r *http.Request) {
|
||||
if st.oidcEnabled() {
|
||||
st.handleOIDCStart(w, r)
|
||||
return
|
||||
}
|
||||
if st.auth.users == nil && !st.auth.rbac {
|
||||
http.Redirect(w, r, st.auth.loginRedirect, http.StatusSeeOther)
|
||||
return
|
||||
@@ -411,6 +442,10 @@ func (st *state) handleLoginGet(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
if st.oidcEnabled() {
|
||||
st.handleOIDCStart(w, r)
|
||||
return
|
||||
}
|
||||
if st.auth.users == nil && !st.auth.rbac {
|
||||
http.Redirect(w, r, st.auth.loginRedirect, http.StatusSeeOther)
|
||||
return
|
||||
@@ -420,14 +455,12 @@ func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
cookieHdr := r.Header.Get("Cookie")
|
||||
sid, csrf, setCookie := resolveSession(r)
|
||||
sid, _, _ := resolveSession(r)
|
||||
_, hadCookie := session.IDFromCookie(cookieHdr)
|
||||
csrfToken := r.FormValue("_csrf")
|
||||
if csrfToken != "" || hadCookie {
|
||||
if !session.ValidateCSRF(sid, csrfToken) {
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, "<p class=\"flash err\">Invalid or missing CSRF token. Refresh and try again.</p>")
|
||||
st.writeAuthFailure(w, r, "Invalid or missing CSRF token. Refresh and try again.")
|
||||
return
|
||||
}
|
||||
}
|
||||
@@ -435,30 +468,40 @@ func (st *state) handleLoginPost(w http.ResponseWriter, r *http.Request) {
|
||||
password := r.FormValue("password")
|
||||
ip := clientIP(r)
|
||||
if err := ratelimit.Check(ip, username); err != nil {
|
||||
msg := err.Error()
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, loginPageHTML(st.auth.loginPath, &msg, csrf, r.FormValue("next")))
|
||||
st.writeAuthFailure(w, r, err.Error())
|
||||
return
|
||||
}
|
||||
res := st.tryLogin(username, password)
|
||||
if ok, _ := res["ok"].(bool); !ok {
|
||||
ratelimit.RecordFailure(ip, username)
|
||||
errMsg := "Invalid username or password."
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, loginPageHTML(st.auth.loginPath, &errMsg, csrf, r.FormValue("next")))
|
||||
st.writeAuthFailure(w, r, "Invalid username or password.")
|
||||
return
|
||||
}
|
||||
ratelimit.ClearSuccess(ip, username)
|
||||
sessID, _ := res["session_id"].(string)
|
||||
role, _ := res["role"].(string)
|
||||
st.attachLoginPermissions(sessID, username, role)
|
||||
dest := loginDest(r, st.auth.loginRedirect)
|
||||
fallback := st.auth.loginRedirect
|
||||
if page := st.pageForAuthPath(r.URL.Path); page != nil {
|
||||
if af, ok := page["auth_form"].(map[string]any); ok {
|
||||
if n, ok := af["next"].(string); ok {
|
||||
if n = strings.TrimSpace(n); n != "" {
|
||||
fallback = n
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
dest := loginDest(r, fallback)
|
||||
held := session.PermissionsFromSession(sessID)
|
||||
if (strings.HasPrefix(dest, "/desk") || strings.HasPrefix(dest, "/admin")) &&
|
||||
!session.PermissionAllowed(held, []string{"desk:access"}) {
|
||||
dest = "/"
|
||||
}
|
||||
w.Header().Add("Set-Cookie", session.IssueCookie(sessID))
|
||||
http.Redirect(w, r, dest, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
|
||||
// tryLogin: GFM users first, then web_users when RBAC is on.
|
||||
func (st *state) tryLogin(username, password string) map[string]any {
|
||||
if st.auth.users != nil {
|
||||
@@ -505,7 +548,13 @@ func (st *state) handleLogout(w http.ResponseWriter, r *http.Request) {
|
||||
auth.Logout(sid)
|
||||
}
|
||||
w.Header().Add("Set-Cookie", session.ClearCookie())
|
||||
http.Redirect(w, r, st.auth.logoutRedirect, http.StatusSeeOther)
|
||||
dest := strings.TrimSpace(st.auth.logoutRedirect)
|
||||
// With OIDC, /login (and siblings) immediately bounce to the IdP — that is
|
||||
// re-auth, not logout. Land on a public page instead.
|
||||
if dest == "" || (st.oidcEnabled() && st.isOIDCEntryPath(dest)) {
|
||||
dest = "/"
|
||||
}
|
||||
http.Redirect(w, r, dest, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (st *state) handleAdminHome(w http.ResponseWriter, r *http.Request) {
|
||||
@@ -619,24 +668,19 @@ func (st *state) handleRegisterPost(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
sid, csrf, setCookie := resolveSession(r)
|
||||
sid, _, _ := resolveSession(r)
|
||||
_, hadCookie := session.IDFromCookie(r.Header.Get("Cookie"))
|
||||
csrfToken := r.FormValue("_csrf")
|
||||
if csrfToken != "" {
|
||||
if csrfToken != "" || hadCookie {
|
||||
if !session.ValidateCSRF(sid, csrfToken) {
|
||||
msg := "Invalid CSRF token."
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
st.writeRegisterFailure(w, r, "Invalid CSRF token.")
|
||||
return
|
||||
}
|
||||
}
|
||||
username := strings.TrimSpace(r.FormValue("username"))
|
||||
pass := r.FormValue("password")
|
||||
if len(username) < 2 || len(pass) < 4 {
|
||||
msg := "Username (≥2) and password (≥4) required."
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
st.writeRegisterFailure(w, r, "Username (≥2) and password (≥4) required.")
|
||||
return
|
||||
}
|
||||
hash, err := password.Hash(pass)
|
||||
@@ -645,10 +689,7 @@ func (st *state) handleRegisterPost(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
if err := rbac.RegisterUser(st.dbURL, username, hash, st.auth.defaultRole); err != nil {
|
||||
msg := err.Error()
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &msg, csrf))
|
||||
st.writeRegisterFailure(w, r, err.Error())
|
||||
return
|
||||
}
|
||||
res := st.tryLogin(username, pass)
|
||||
|
||||
@@ -0,0 +1,164 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
)
|
||||
|
||||
func isLoginAuthPage(page any) bool {
|
||||
m, ok := page.(map[string]any)
|
||||
if !ok || m == nil {
|
||||
return false
|
||||
}
|
||||
af, ok := m["auth_form"].(map[string]any)
|
||||
if !ok || af == nil {
|
||||
return false
|
||||
}
|
||||
kind := strings.ToLower(strings.TrimSpace(fmt.Sprint(af["kind"])))
|
||||
action := strings.ToLower(strings.TrimSpace(fmt.Sprint(af["action"])))
|
||||
if kind == "register" || strings.Contains(action, "register") {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (st *state) pageForAuthPath(path string) map[string]any {
|
||||
if path == "" || st.routes == nil {
|
||||
return nil
|
||||
}
|
||||
if p, ok := st.routes[path].(map[string]any); ok && p != nil {
|
||||
return p
|
||||
}
|
||||
alt := strings.TrimRight(st.auth.adminPrefix, "/") + "/login"
|
||||
if path == alt {
|
||||
if p, ok := st.routes["/desk/login"].(map[string]any); ok {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func localizeAuthErr(msg string) string {
|
||||
s := strings.TrimSpace(msg)
|
||||
lower := strings.ToLower(s)
|
||||
switch {
|
||||
case s == "":
|
||||
return "操作失败,请重试。"
|
||||
case strings.Contains(s, "Too many failed"):
|
||||
return "尝试次数过多,请约 15 分钟后再试。"
|
||||
case strings.Contains(s, "Invalid username or password"):
|
||||
return "用户名或密码错误。"
|
||||
case strings.Contains(s, "Invalid or missing CSRF"), strings.Contains(s, "Invalid CSRF"):
|
||||
return "登录令牌失效,请刷新页面后重试。"
|
||||
case strings.Contains(s, "Username") && strings.Contains(lower, "password"):
|
||||
return "请填写有效的用户名(≥2)与密码(≥4)。"
|
||||
case strings.Contains(lower, "already"), strings.Contains(lower, "exists"), strings.Contains(s, "存在"):
|
||||
return "该用户名已被注册,请换一个。"
|
||||
default:
|
||||
return s
|
||||
}
|
||||
}
|
||||
|
||||
func safeAuthNext(raw string) string {
|
||||
next := strings.TrimSpace(raw)
|
||||
if next != "" && strings.HasPrefix(next, "/") && !strings.HasPrefix(next, "//") {
|
||||
return next
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func (st *state) writeAuthFailure(w http.ResponseWriter, r *http.Request, msg string) {
|
||||
path := r.URL.Path
|
||||
page := st.pageForAuthPath(path)
|
||||
if page == nil && path == strings.TrimRight(st.auth.adminPrefix, "/")+"/login" {
|
||||
page = st.pageForAuthPath("/desk/login")
|
||||
}
|
||||
if page != nil {
|
||||
p := cloneMap(page)
|
||||
p["_flash_err"] = localizeAuthErr(msg)
|
||||
if next := safeAuthNext(r.FormValue("next")); next != "" {
|
||||
p["_auth_next"] = next
|
||||
} else if next := safeAuthNext(r.URL.Query().Get("next")); next != "" {
|
||||
p["_auth_next"] = next
|
||||
}
|
||||
st.writePage(w, r, p)
|
||||
return
|
||||
}
|
||||
_, csrf, setCookie := resolveSession(r)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
loc := localizeAuthErr(msg)
|
||||
_, _ = io.WriteString(w, loginPageHTML(st.auth.loginPath, &loc, csrf, r.FormValue("next")))
|
||||
}
|
||||
|
||||
func (st *state) writeRegisterFailure(w http.ResponseWriter, r *http.Request, msg string) {
|
||||
path := r.URL.Path
|
||||
page := st.pageForAuthPath(path)
|
||||
if page == nil {
|
||||
page = st.pageForAuthPath(st.auth.registerPath)
|
||||
}
|
||||
if page != nil {
|
||||
p := cloneMap(page)
|
||||
p["_flash_err"] = localizeAuthErr(msg)
|
||||
st.writePage(w, r, p)
|
||||
return
|
||||
}
|
||||
_, csrf, setCookie := resolveSession(r)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
loc := localizeAuthErr(msg)
|
||||
_, _ = io.WriteString(w, registerPageHTML(st.auth.registerPath, &loc, csrf))
|
||||
}
|
||||
|
||||
func (st *state) mountExtraLoginPosts(mux *http.ServeMux, already map[string]bool) {
|
||||
for path, pageV := range st.routes {
|
||||
if already[path] {
|
||||
continue
|
||||
}
|
||||
if isLoginAuthPage(pageV) {
|
||||
mux.HandleFunc("POST "+path, st.handleLoginPost)
|
||||
already[path] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (st *state) authEntryRedirect(w http.ResponseWriter, r *http.Request, page map[string]any) bool {
|
||||
if page == nil || r == nil {
|
||||
return false
|
||||
}
|
||||
path := r.URL.Path
|
||||
isEntry := false
|
||||
if path == st.auth.loginPath || path == st.auth.registerPath {
|
||||
isEntry = true
|
||||
}
|
||||
alt := strings.TrimRight(st.auth.adminPrefix, "/") + "/login"
|
||||
if path == alt {
|
||||
isEntry = true
|
||||
}
|
||||
if _, ok := page["auth_form"]; ok {
|
||||
isEntry = true
|
||||
}
|
||||
if !isEntry {
|
||||
return false
|
||||
}
|
||||
if session.RoleFromCookie(r.Header.Get("Cookie")) == "visitor" {
|
||||
return false
|
||||
}
|
||||
fallback := st.auth.loginRedirect
|
||||
if af, ok := page["auth_form"].(map[string]any); ok {
|
||||
if n, ok := af["next"].(string); ok {
|
||||
if n = strings.TrimSpace(n); n != "" {
|
||||
fallback = n
|
||||
}
|
||||
}
|
||||
}
|
||||
if path == st.auth.registerPath {
|
||||
fallback = st.auth.loginRedirect
|
||||
}
|
||||
http.Redirect(w, r, loginDest(r, fallback), http.StatusSeeOther)
|
||||
return true
|
||||
}
|
||||
@@ -0,0 +1,245 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/auth"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/oidc"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/rbac"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
)
|
||||
|
||||
const oidcPasswordSentinel = "!oidc!"
|
||||
|
||||
var (
|
||||
oidcHTTPOnce sync.Once
|
||||
oidcHTTP *http.Client
|
||||
)
|
||||
|
||||
func oidcClient() *http.Client {
|
||||
oidcHTTPOnce.Do(func() {
|
||||
oidcHTTP = &http.Client{Timeout: 20 * time.Second}
|
||||
})
|
||||
return oidcHTTP
|
||||
}
|
||||
|
||||
func (st *state) oidcEnabled() bool {
|
||||
return st.oidc.Enabled()
|
||||
}
|
||||
|
||||
func (st *state) ensureOIDCEndpoints() error {
|
||||
if !st.oidc.Enabled() {
|
||||
return fmt.Errorf("oidc not configured")
|
||||
}
|
||||
return st.oidc.ResolveEndpoints(oidcClient())
|
||||
}
|
||||
|
||||
func (st *state) isOIDCEntryPath(path string) bool {
|
||||
if !st.oidcEnabled() || path == "" {
|
||||
return false
|
||||
}
|
||||
if path == st.auth.loginPath || path == st.auth.registerPath {
|
||||
return true
|
||||
}
|
||||
alt := strings.TrimRight(st.auth.adminPrefix, "/") + "/login"
|
||||
if path == alt || path == "/desk/login" || path == "/admin/login" {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// oidcFallbackNext picks where to land after IdP login when ?next= is absent.
|
||||
// Admin entry pages must not fall back to the public homepage.
|
||||
func (st *state) oidcFallbackNext(r *http.Request) string {
|
||||
path := ""
|
||||
if r != nil {
|
||||
path = r.URL.Path
|
||||
}
|
||||
switch path {
|
||||
case "/desk/login", "/admin/login", "/oidc/login":
|
||||
return "/admin"
|
||||
case "/desk", "/admin":
|
||||
return "/admin"
|
||||
}
|
||||
if strings.HasPrefix(path, "/desk/") || strings.HasPrefix(path, "/admin/") {
|
||||
return "/admin"
|
||||
}
|
||||
if st.auth.loginRedirect != "" {
|
||||
return st.auth.loginRedirect
|
||||
}
|
||||
return "/"
|
||||
}
|
||||
|
||||
func (st *state) mountOIDCRoutes(mux *http.ServeMux) {
|
||||
if !st.oidcEnabled() {
|
||||
return
|
||||
}
|
||||
cb := st.oidc.Callback()
|
||||
mux.HandleFunc("GET "+cb, st.handleOIDCCallback)
|
||||
mux.HandleFunc("GET /oidc/login", st.handleOIDCStart)
|
||||
mux.HandleFunc("GET /oidc/register", st.handleOIDCStart)
|
||||
}
|
||||
|
||||
func (st *state) handleOIDCStart(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.oidcEnabled() {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
// Already signed in → skip IdP round-trip when destination is allowed.
|
||||
cookie := r.Header.Get("Cookie")
|
||||
if session.RoleFromCookie(cookie) != "visitor" {
|
||||
next := loginDest(r, st.oidcFallbackNext(r))
|
||||
needsDesk := strings.HasPrefix(next, "/admin") || strings.HasPrefix(next, "/desk")
|
||||
held := session.PermissionsFromCookie(cookie)
|
||||
if needsDesk && !session.PermissionAllowed(held, []string{"desk:access"}) {
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, next, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
if err := st.ensureOIDCEndpoints(); err != nil {
|
||||
http.Error(w, "oidc discovery failed: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
sid, _, setCookie := resolveSession(r)
|
||||
state, err := oidc.RandomURLString(24)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
verifier, err := oidc.RandomURLString(32)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
session.SetValue(sid, "oidc_state", state)
|
||||
session.SetValue(sid, "oidc_verifier", verifier)
|
||||
next := loginDest(r, st.oidcFallbackNext(r))
|
||||
session.SetValue(sid, "oidc_next", next)
|
||||
loc, err := st.oidc.AuthorizeRedirect(state, oidc.PKCEChallengeS256(verifier))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
appendSetCookie(w, setCookie)
|
||||
w.Header().Add("Set-Cookie", session.IssueCookie(sid))
|
||||
http.Redirect(w, r, loc, http.StatusFound)
|
||||
}
|
||||
|
||||
func (st *state) handleOIDCCallback(w http.ResponseWriter, r *http.Request) {
|
||||
if !st.oidcEnabled() {
|
||||
http.NotFound(w, r)
|
||||
return
|
||||
}
|
||||
if err := st.ensureOIDCEndpoints(); err != nil {
|
||||
http.Error(w, "oidc discovery failed: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
q := r.URL.Query()
|
||||
if errCode := q.Get("error"); errCode != "" {
|
||||
msg := errCode
|
||||
if d := q.Get("error_description"); d != "" {
|
||||
msg = errCode + ": " + d
|
||||
}
|
||||
http.Error(w, "oidc: "+msg, http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
code := q.Get("code")
|
||||
state := q.Get("state")
|
||||
if code == "" || state == "" {
|
||||
http.Error(w, "oidc: missing code or state", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
sid, _, setCookie := resolveSession(r)
|
||||
want, _ := session.GetValue(sid, "oidc_state")
|
||||
wantS, _ := want.(string)
|
||||
if wantS == "" || wantS != state {
|
||||
http.Error(w, "oidc: state mismatch", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
verifier, _ := session.GetValue(sid, "oidc_verifier")
|
||||
verifierS, _ := verifier.(string)
|
||||
nextVal, _ := session.GetValue(sid, "oidc_next")
|
||||
next, _ := nextVal.(string)
|
||||
if next == "" {
|
||||
next = st.auth.loginRedirect
|
||||
}
|
||||
session.DelKey(sid, "oidc_state")
|
||||
session.DelKey(sid, "oidc_verifier")
|
||||
session.DelKey(sid, "oidc_next")
|
||||
|
||||
tok, err := st.oidc.ExchangeCode(oidcClient(), code, verifierS)
|
||||
if err != nil {
|
||||
http.Error(w, "oidc token exchange: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
info, err := st.oidc.FetchUserInfo(oidcClient(), tok.AccessToken)
|
||||
if err != nil {
|
||||
http.Error(w, "oidc userinfo: "+err.Error(), http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
username := info.LocalUsername()
|
||||
if username == "" {
|
||||
http.Error(w, "oidc: empty username", http.StatusBadGateway)
|
||||
return
|
||||
}
|
||||
role := info.LocalRole(st.auth.defaultRole)
|
||||
if role == "admin" {
|
||||
// System RBAC seed uses superadmin; keep session role "admin" for ExpandRole.
|
||||
if st.dbURL != "" && st.auth.rbac {
|
||||
_ = rbac.EnsureUserWithRole(st.dbURL, username, oidcPasswordSentinel, "superadmin")
|
||||
}
|
||||
} else if st.dbURL != "" && st.auth.rbac {
|
||||
_ = rbac.EnsureUserWithRole(st.dbURL, username, oidcPasswordSentinel, role)
|
||||
}
|
||||
newID := session.NewID(st.auth.sessionTTL)
|
||||
session.SetValue(newID, "username", username)
|
||||
session.SetValue(newID, "role", role)
|
||||
session.SetValue(newID, "oidc_sub", info.Sub)
|
||||
if info.Email != "" {
|
||||
session.SetValue(newID, "email", info.Email)
|
||||
}
|
||||
if info.AdminRole != "" {
|
||||
session.SetValue(newID, "admin_role", info.AdminRole)
|
||||
}
|
||||
st.attachOIDCPermissions(newID, username, role)
|
||||
|
||||
held := session.PermissionsFromSession(newID)
|
||||
if (strings.HasPrefix(next, "/desk") || strings.HasPrefix(next, "/admin")) &&
|
||||
!session.PermissionAllowed(held, []string{"desk:access"}) {
|
||||
next = "/"
|
||||
}
|
||||
appendSetCookie(w, setCookie)
|
||||
w.Header().Add("Set-Cookie", session.IssueCookie(newID))
|
||||
http.Redirect(w, r, next, http.StatusSeeOther)
|
||||
}
|
||||
|
||||
func (st *state) attachOIDCPermissions(sessID, username, role string) {
|
||||
// Prefer IdP-derived role expansion so platform admins always get desk access
|
||||
// even before local RBAC rows catch up.
|
||||
perms := rbac.ExpandRole(role)
|
||||
if len(perms) == 0 {
|
||||
perms = expandLegacyRole(role)
|
||||
}
|
||||
if st.dbURL != "" && st.auth.rbac {
|
||||
if dbPerms, err := rbac.PermissionsForUser(st.dbURL, username); err == nil && len(dbPerms) > 0 {
|
||||
// Union IdP role perms with any local grants.
|
||||
seen := map[string]bool{}
|
||||
var out []string
|
||||
for _, p := range append(perms, dbPerms...) {
|
||||
if p == "" || seen[p] {
|
||||
continue
|
||||
}
|
||||
seen[p] = true
|
||||
out = append(out, p)
|
||||
}
|
||||
perms = out
|
||||
}
|
||||
}
|
||||
auth.AttachPermissions(sessID, perms)
|
||||
}
|
||||
@@ -70,3 +70,25 @@ func TestWithNavAuth(t *testing.T) {
|
||||
t.Fatalf("nav user=%v", page["_nav_user"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogoutPathExcludedFromGate(t *testing.T) {
|
||||
gates := []gate{{
|
||||
path: "/admin",
|
||||
permissions: []string{"desk:access"},
|
||||
matchMode: gateMatchPrefix,
|
||||
onDeny: onDenyRedirect,
|
||||
exclude: []string{"/admin/login"},
|
||||
}}
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /admin/logout", func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
})
|
||||
handler := withRBAC(mux, gates, "/login")
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/admin/logout", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
handler.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusNoContent {
|
||||
t.Fatalf("visitor /admin/logout should reach handler, got %d loc=%s", rec.Code, rec.Header().Get("Location"))
|
||||
}
|
||||
}
|
||||
@@ -67,15 +67,13 @@ func goMuxPattern(path string) string {
|
||||
func resolveSession(r *http.Request) (sid, csrf string, setCookie *string) {
|
||||
cookieHdr := r.Header.Get("Cookie")
|
||||
id, hadCookie := session.IDFromCookie(cookieHdr)
|
||||
if hadCookie {
|
||||
ok := true
|
||||
if ok && id != "" {
|
||||
sid = id
|
||||
if tok, ok := session.CSRFFor(sid); ok {
|
||||
csrf = tok
|
||||
}
|
||||
return sid, csrf, nil
|
||||
if hadCookie && id != "" {
|
||||
if tok, ok := session.CSRFFor(id); ok && tok != "" {
|
||||
return id, tok, nil
|
||||
}
|
||||
// Stale/expired/missing session: mint a fresh session and replace the cookie.
|
||||
// Previously we returned the dead sid with empty CSRF, so login forms had no
|
||||
// _csrf while POST still saw hadCookie=true → "登录令牌失效".
|
||||
}
|
||||
sid = session.NewID(0)
|
||||
tok, _ := session.CSRFFor(sid)
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
package httpx
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/session"
|
||||
)
|
||||
|
||||
func TestGoMuxPattern(t *testing.T) {
|
||||
if got := goMuxPattern("/_media/{*key}"); got != "/_media/{key...}" {
|
||||
@@ -10,3 +16,44 @@ func TestGoMuxPattern(t *testing.T) {
|
||||
t.Fatalf("got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPathParamNamesIncludesID(t *testing.T) {
|
||||
got := pathParamNames("/desk/posts/{id}")
|
||||
if len(got) != 1 || got[0] != "id" {
|
||||
t.Fatalf("got %#v", got)
|
||||
}
|
||||
got = pathParamNames("/post/{slug}")
|
||||
if len(got) != 1 || got[0] != "slug" {
|
||||
t.Fatalf("got %#v", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveSessionReplacesStaleCookie(t *testing.T) {
|
||||
session.Configure(session.Config{TTLSec: 3600})
|
||||
session.Reset(3600)
|
||||
|
||||
req := httptest.NewRequest("GET", "/login", nil)
|
||||
req.Header.Set("Cookie", "marqdo_sid="+strings.Repeat("ab", 32))
|
||||
sid, csrf, setCookie := resolveSession(req)
|
||||
if sid == "" || csrf == "" {
|
||||
t.Fatalf("expected fresh sid+csrf, got sid=%q csrf=%q", sid, csrf)
|
||||
}
|
||||
if setCookie == nil || !strings.Contains(*setCookie, "marqdo_sid=") {
|
||||
t.Fatalf("expected Set-Cookie for replacement session, got %#v", setCookie)
|
||||
}
|
||||
if !session.ValidateCSRF(sid, csrf) {
|
||||
t.Fatal("csrf should validate for new session")
|
||||
}
|
||||
|
||||
// Live session should keep the same id without re-issuing cookie.
|
||||
req2 := httptest.NewRequest("GET", "/login", nil)
|
||||
req2.Header.Set("Cookie", "marqdo_sid="+sid)
|
||||
sid2, csrf2, setCookie2 := resolveSession(req2)
|
||||
if sid2 != sid || csrf2 != csrf {
|
||||
t.Fatalf("live session should be stable: %q/%q vs %q/%q", sid, csrf, sid2, csrf2)
|
||||
}
|
||||
if setCookie2 != nil {
|
||||
t.Fatalf("live session should not Set-Cookie again")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"io"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"os"
|
||||
"os/signal"
|
||||
"path/filepath"
|
||||
@@ -21,6 +22,7 @@ import (
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/db"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/form"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/middleware"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/oidc"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ratelimit"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/rbac"
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/render"
|
||||
@@ -44,6 +46,7 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
}
|
||||
session.Configure(session.Config{
|
||||
DBURL: dbURL,
|
||||
RedisURL: sessionURLOf(appBag),
|
||||
TTLSec: authCfg.sessionTTL,
|
||||
CookieSecure: authCfg.cookieSecure,
|
||||
})
|
||||
@@ -54,10 +57,24 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
if page == nil {
|
||||
page = map[string]any{}
|
||||
}
|
||||
if _, ok := page["asset_version"]; !ok {
|
||||
if v := strOpt(appBag, "asset_version", ""); v != "" {
|
||||
page["asset_version"] = v
|
||||
}
|
||||
}
|
||||
routes := map[string]any{}
|
||||
if r, ok := appBag["routes"].(map[string]any); ok {
|
||||
routes = r
|
||||
}
|
||||
if av, ok := page["asset_version"]; ok {
|
||||
for _, p := range routes {
|
||||
if pm, ok := p.(map[string]any); ok {
|
||||
if _, has := pm["asset_version"]; !has {
|
||||
pm["asset_version"] = av
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
forms := map[string]any{}
|
||||
if f, ok := appBag["forms"].(map[string]any); ok {
|
||||
forms = cloneMap(f)
|
||||
@@ -106,14 +123,18 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
proxyRoutes: routeMapOf(appBag, "proxy_routes"),
|
||||
invokeRoutes: routeMapOf(appBag, "invoke_routes"),
|
||||
auth: authCfg,
|
||||
oidc: oidc.ParseConfig(appBag),
|
||||
tenant: tenant.FromBag(appBag),
|
||||
}
|
||||
|
||||
mux := http.NewServeMux()
|
||||
mux.HandleFunc("GET /{$}", st.handleHome)
|
||||
mux.HandleFunc("GET /_part/{id}", st.handleHomePart)
|
||||
// Part slot uses {part} (not {id}) so dynamic routes like /desk/posts/{id}
|
||||
// do not collide with Go ServeMux duplicate wildcard names.
|
||||
mux.HandleFunc("GET /_part/{part}", st.handleHomePart)
|
||||
mux.HandleFunc("GET /_form/{id}", st.handleFormGet)
|
||||
mux.HandleFunc("POST /_form/{id}", st.handleFormPost)
|
||||
mux.HandleFunc("POST /_md", st.handleMarkdownPreview)
|
||||
|
||||
paths := sortedKeys(routes)
|
||||
for _, p := range paths {
|
||||
@@ -124,14 +145,14 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
}
|
||||
if strings.Contains(routePath, "{") {
|
||||
mux.HandleFunc("GET "+goMuxPattern(routePath), st.makeDynamicPage(pageV, routePath))
|
||||
mux.HandleFunc("GET "+goMuxPattern(routePath)+"/_part/{id}", st.makeDynamicPart(pageV, routePath))
|
||||
mux.HandleFunc("GET "+goMuxPattern(routePath)+"/_part/{part}", st.makeDynamicPart(pageV, routePath))
|
||||
} else {
|
||||
pv := pageV
|
||||
mux.HandleFunc("GET "+routePath, func(w http.ResponseWriter, r *http.Request) {
|
||||
st.writePage(w, r, pv)
|
||||
})
|
||||
mux.HandleFunc("GET "+routePath+"/_part/{id}", func(w http.ResponseWriter, r *http.Request) {
|
||||
st.writePart(w, r, pv, r.PathValue("id"))
|
||||
mux.HandleFunc("GET "+routePath+"/_part/{part}", func(w http.ResponseWriter, r *http.Request) {
|
||||
st.writePart(w, r, pv, r.PathValue("part"))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -169,20 +190,18 @@ func NewHandler(appBag map[string]any, entryDir string) (http.Handler, error) {
|
||||
fs := http.StripPrefix(staticMount, http.FileServer(http.Dir(staticDir)))
|
||||
// Register without method so GET+HEAD share one pattern (avoids conflicts
|
||||
// with more-specific GET/HEAD icon paths under the same prefix).
|
||||
mux.Handle(staticMount+"/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
serveStatic := func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
// Long browser cache; FileServer still honors Last-Modified / 304.
|
||||
// Prefer ?v= / asset_version on HTML refs when assets change.
|
||||
w.Header().Set("Cache-Control", "public, max-age=604800")
|
||||
fs.ServeHTTP(w, r)
|
||||
}))
|
||||
mux.Handle(staticMount, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodGet && r.Method != http.MethodHead {
|
||||
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
fs.ServeHTTP(w, r)
|
||||
}))
|
||||
}
|
||||
mux.Handle(staticMount+"/", http.HandlerFunc(serveStatic))
|
||||
mux.Handle(staticMount, http.HandlerFunc(serveStatic))
|
||||
}
|
||||
|
||||
for _, from := range sortedKeys(st.redirects) {
|
||||
@@ -289,6 +308,7 @@ type state struct {
|
||||
proxyRoutes map[string]any
|
||||
invokeRoutes map[string]any
|
||||
auth authConfig
|
||||
oidc oidc.Config
|
||||
tenant tenant.Config
|
||||
}
|
||||
|
||||
@@ -297,7 +317,21 @@ func (st *state) handleHome(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
func (st *state) handleHomePart(w http.ResponseWriter, r *http.Request) {
|
||||
st.writePart(w, r, st.page, r.PathValue("id"))
|
||||
st.writePart(w, r, st.page, r.PathValue("part"))
|
||||
}
|
||||
|
||||
func (st *state) handleMarkdownPreview(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(io.LimitReader(r.Body, 1<<20))
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
html := render.MarkdownToHTML(string(body))
|
||||
if html == "" {
|
||||
html = `<p class="lede mq-md-empty">预览</p>`
|
||||
}
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) preparePage(page map[string]any, r *http.Request) map[string]any {
|
||||
@@ -305,19 +339,66 @@ func (st *state) preparePage(page map[string]any, r *http.Request) map[string]an
|
||||
assets.MergeSiteHead(p, st.siteHead)
|
||||
if r != nil {
|
||||
withNavAuth(p, r.Header.Get("Cookie"))
|
||||
params, _ := p["params"].(map[string]any)
|
||||
if params == nil {
|
||||
params = map[string]any{}
|
||||
} else {
|
||||
params = cloneMap(params)
|
||||
}
|
||||
for k, vs := range r.URL.Query() {
|
||||
if len(vs) == 0 {
|
||||
continue
|
||||
}
|
||||
if _, exists := params[k]; !exists {
|
||||
params[k] = vs[0]
|
||||
}
|
||||
}
|
||||
p["params"] = params
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// attachCSRF stamps page._csrf so custom login/register pages expose a token
|
||||
// in meta and (via InjectAuthFields) in POST forms — progressive enhancement, no author JS.
|
||||
func (st *state) attachCSRF(page map[string]any, r *http.Request) *string {
|
||||
if page == nil || r == nil {
|
||||
return nil
|
||||
}
|
||||
if st.auth.users == nil && !st.auth.rbac && !st.oidcEnabled() {
|
||||
return nil
|
||||
}
|
||||
_, csrf, setCookie := resolveSession(r)
|
||||
if csrf != "" {
|
||||
page["_csrf"] = csrf
|
||||
}
|
||||
return setCookie
|
||||
}
|
||||
|
||||
func (st *state) writePage(w http.ResponseWriter, r *http.Request, page map[string]any) {
|
||||
html := render.RenderPage(st.preparePage(page, r), st.dbURL, "")
|
||||
if st.oidcEnabled() && st.isOIDCEntryPath(r.URL.Path) {
|
||||
st.handleOIDCStart(w, r)
|
||||
return
|
||||
}
|
||||
p := st.preparePage(page, r)
|
||||
if st.authEntryRedirect(w, r, p) {
|
||||
return
|
||||
}
|
||||
if next := safeAuthNext(r.URL.Query().Get("next")); next != "" {
|
||||
p["_auth_next"] = next
|
||||
}
|
||||
setCookie := st.attachCSRF(p, r)
|
||||
html := render.RenderPage(p, st.dbURL, "")
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) writePart(w http.ResponseWriter, r *http.Request, page map[string]any, id string) {
|
||||
html := render.RenderPage(st.preparePage(page, r), st.dbURL, id)
|
||||
p := st.preparePage(page, r)
|
||||
setCookie := st.attachCSRF(p, r)
|
||||
html := render.RenderPage(p, st.dbURL, id)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
appendSetCookie(w, setCookie)
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
@@ -402,7 +483,7 @@ func (st *state) makeDynamicPart(page map[string]any, pattern string) http.Handl
|
||||
params[name] = r.PathValue(name)
|
||||
}
|
||||
injectParams(p, params)
|
||||
st.writePart(w, r, p, r.PathValue("id"))
|
||||
st.writePart(w, r, p, r.PathValue("part"))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -447,21 +528,82 @@ func (st *state) handleFormPost(w http.ResponseWriter, r *http.Request) {
|
||||
data = tenant.StampRow(data, st.tenant.Column, tid)
|
||||
}
|
||||
}
|
||||
ctx := st.requestContext(r, data)
|
||||
delete(data, "_mq_params")
|
||||
delete(data, "_mq_return")
|
||||
delete(data, "_csrf")
|
||||
form.ApplySources(frm, data, ctx)
|
||||
res, err := form.Submit(frm, data, st.dbURL)
|
||||
if err != nil {
|
||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if ok, _ := res["ok"].(bool); ok {
|
||||
// Prefer an explicit form redirect (e.g. cancel_href list URL) over the
|
||||
// composing page path stamped as _mq_return.
|
||||
redir := strOpt(res, "redirect", "/")
|
||||
if (redir == "" || redir == "/") && ctx.ReturnPath != "" {
|
||||
redir = ctx.ReturnPath
|
||||
}
|
||||
http.Redirect(w, r, redir, http.StatusSeeOther)
|
||||
return
|
||||
}
|
||||
html := form.Render(frm, id, data, res["errors"], "")
|
||||
html := form.RenderBodyCtx(frm, id, data, res["errors"], "", ctx)
|
||||
w.Header().Set("Content-Type", "text/html; charset=utf-8")
|
||||
_, _ = io.WriteString(w, html)
|
||||
}
|
||||
|
||||
func (st *state) requestContext(r *http.Request, data map[string]any) *form.RequestContext {
|
||||
ctx := &form.RequestContext{
|
||||
Username: session.UsernameFromCookie(r.Header.Get("Cookie")),
|
||||
Path: r.URL.Path,
|
||||
Method: r.Method,
|
||||
}
|
||||
if data != nil {
|
||||
if p := form.ParseMQParams(fmt.Sprint(data["_mq_params"])); len(p) > 0 {
|
||||
ctx.Params = p
|
||||
}
|
||||
if ret, ok := data["_mq_return"].(string); ok {
|
||||
if back := sameHostPath(ret, r.Host); back != "" {
|
||||
ctx.ReturnPath = back
|
||||
} else if strings.HasPrefix(ret, "/") && !strings.HasPrefix(ret, "//") {
|
||||
ctx.ReturnPath = ret
|
||||
}
|
||||
}
|
||||
}
|
||||
if ctx.ReturnPath == "" {
|
||||
if back := sameHostPath(r.Referer(), r.Host); back != "" {
|
||||
ctx.ReturnPath = back
|
||||
}
|
||||
}
|
||||
return ctx
|
||||
}
|
||||
|
||||
func sameHostPath(raw, host string) string {
|
||||
raw = strings.TrimSpace(raw)
|
||||
if raw == "" {
|
||||
return ""
|
||||
}
|
||||
u, err := url.Parse(raw)
|
||||
if err != nil || u == nil {
|
||||
return ""
|
||||
}
|
||||
if u.Host != "" && host != "" && !strings.EqualFold(u.Host, host) {
|
||||
return ""
|
||||
}
|
||||
path := u.Path
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
if u.RawQuery != "" {
|
||||
path += "?" + u.RawQuery
|
||||
}
|
||||
if !strings.HasPrefix(path, "/") || strings.HasPrefix(path, "//") {
|
||||
return ""
|
||||
}
|
||||
return path
|
||||
}
|
||||
|
||||
func (st *state) handleJSON(w http.ResponseWriter, r *http.Request, route middleware.JSONRouteMount) {
|
||||
if st.dbURL == "" {
|
||||
writeJSON(w, http.StatusInternalServerError, map[string]any{"error": "no database"})
|
||||
@@ -703,7 +845,8 @@ func pathParamNames(pattern string) []string {
|
||||
if strings.HasPrefix(seg, "{") && strings.HasSuffix(seg, "}") {
|
||||
name := strings.TrimSuffix(strings.TrimPrefix(seg, "{"), "}")
|
||||
name = strings.TrimPrefix(name, "*")
|
||||
if name != "" && name != "id" {
|
||||
// Include "id" — part slots use {part}, so route {id} is safe for PathValue.
|
||||
if name != "" {
|
||||
names = append(names, name)
|
||||
}
|
||||
}
|
||||
@@ -723,6 +866,18 @@ func dbURLOf(appBag map[string]any) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func sessionURLOf(appBag map[string]any) string {
|
||||
if s, ok := appBag["session_url"].(string); ok && s != "" {
|
||||
return s
|
||||
}
|
||||
if auth, ok := appBag["auth"].(map[string]any); ok {
|
||||
if s, ok := auth["session_url"].(string); ok && s != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func resolvePath(p, entryDir string) string {
|
||||
if filepath.IsAbs(p) {
|
||||
return p
|
||||
|
||||
@@ -58,6 +58,9 @@ func TestNewHandlerHomeRoutesStatic(t *testing.T) {
|
||||
if rec.Code != 200 {
|
||||
t.Fatalf("static code=%d", rec.Code)
|
||||
}
|
||||
if cc := rec.Header().Get("Cache-Control"); !strings.Contains(cc, "max-age=") {
|
||||
t.Fatalf("static cache-control=%q", cc)
|
||||
}
|
||||
b, _ := io.ReadAll(rec.Result().Body)
|
||||
if string(b) != "hello-static" {
|
||||
// httptest body is already in rec.Body
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
@@ -12,8 +13,19 @@ import (
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ws"
|
||||
)
|
||||
|
||||
var wsUpgrader = websocket.Upgrader{
|
||||
CheckOrigin: func(r *http.Request) bool { return true },
|
||||
const maxWSMessageBytes = 1 << 20
|
||||
|
||||
var wsUpgrader = websocket.Upgrader{CheckOrigin: sameOrigin}
|
||||
|
||||
// Browsers send Origin for a cross-site WebSocket handshake. Permit clients
|
||||
// without Origin, but reject browser origins that differ from this application.
|
||||
func sameOrigin(r *http.Request) bool {
|
||||
origin := r.Header.Get("Origin")
|
||||
if origin == "" {
|
||||
return true
|
||||
}
|
||||
u, err := url.Parse(origin)
|
||||
return err == nil && strings.EqualFold(u.Host, r.Host)
|
||||
}
|
||||
|
||||
func (st *state) mountWSRoutes(mux *http.ServeMux) {
|
||||
@@ -57,11 +69,16 @@ func pathValuesOf(r *http.Request, routePath string) map[string]string {
|
||||
}
|
||||
|
||||
func (st *state) handleWS(w http.ResponseWriter, r *http.Request, path string, spec ws.RouteSpec) {
|
||||
if spec.RequireAuth && session.UsernameFromCookie(r.Header.Get("Cookie")) == "" {
|
||||
http.Error(w, "WebSocket authentication required", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
conn, err := wsUpgrader.Upgrade(w, r, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
conn.SetReadLimit(maxWSMessageBytes)
|
||||
|
||||
vals := pathValuesOf(r, path)
|
||||
switch spec.Mode {
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package httpx
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/ws"
|
||||
)
|
||||
|
||||
func TestHandleWSRejectsUnauthenticatedHandshake(t *testing.T) {
|
||||
st := &state{}
|
||||
r := httptest.NewRequest("GET", "http://app.example/live", nil)
|
||||
w := httptest.NewRecorder()
|
||||
st.handleWS(w, r, "/live", ws.RouteSpec{Mode: ws.ModeBroadcast, RequireAuth: true})
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("unauthenticated ws handshake code=%d, want 401", w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHandleWSAllowsPublicRouteThroughAuthGate(t *testing.T) {
|
||||
st := &state{}
|
||||
r := httptest.NewRequest("GET", "http://app.example/live", nil)
|
||||
w := httptest.NewRecorder()
|
||||
// Public route: must not 401. Upgrade then fails on the recorder (no
|
||||
// Hijacker), which is fine — only the auth gate is under test.
|
||||
st.handleWS(w, r, "/live", ws.RouteSpec{Mode: ws.ModeBroadcast, RequireAuth: false})
|
||||
if w.Code == http.StatusUnauthorized {
|
||||
t.Fatalf("public ws route unexpectedly requires authentication")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSameOriginWebSocketHandshake(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
origin string
|
||||
want bool
|
||||
}{
|
||||
{name: "non-browser client", want: true},
|
||||
{name: "same origin", origin: "https://app.example", want: true},
|
||||
{name: "different origin", origin: "https://evil.example", want: false},
|
||||
{name: "malformed origin", origin: "://bad", want: false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
r := httptest.NewRequest("GET", "http://app.example/live", nil)
|
||||
if tc.origin != "" {
|
||||
r.Header.Set("Origin", tc.origin)
|
||||
}
|
||||
if got := sameOrigin(r); got != tc.want {
|
||||
t.Fatalf("sameOrigin(%q)=%v, want %v", tc.origin, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,341 @@
|
||||
// Package oidc implements OAuth 2.0 authorization-code + OIDC userinfo for ext/web.
|
||||
package oidc
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Config is the relying-party settings (CFLMY IdP or any OIDC provider).
|
||||
type Config struct {
|
||||
Issuer string
|
||||
ClientID string
|
||||
ClientSecret string
|
||||
RedirectURI string
|
||||
Scopes string
|
||||
CallbackPath string
|
||||
// AuthorizeURL / TokenURL / UserInfoURL override discovery when set.
|
||||
AuthorizeURL string
|
||||
TokenURL string
|
||||
UserInfoURL string
|
||||
}
|
||||
|
||||
// Enabled reports whether the minimum OIDC fields are present.
|
||||
func (c Config) Enabled() bool {
|
||||
return strings.TrimSpace(c.ClientID) != "" &&
|
||||
strings.TrimSpace(c.ClientSecret) != "" &&
|
||||
strings.TrimSpace(c.RedirectURI) != "" &&
|
||||
(strings.TrimSpace(c.Issuer) != "" || strings.TrimSpace(c.AuthorizeURL) != "")
|
||||
}
|
||||
|
||||
// Callback reports the local path (default /oidc/callback).
|
||||
func (c Config) Callback() string {
|
||||
p := strings.TrimSpace(c.CallbackPath)
|
||||
if p == "" {
|
||||
if u, err := url.Parse(c.RedirectURI); err == nil && u.Path != "" {
|
||||
return u.Path
|
||||
}
|
||||
return "/oidc/callback"
|
||||
}
|
||||
if !strings.HasPrefix(p, "/") {
|
||||
p = "/" + p
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
type discoveryDoc struct {
|
||||
AuthorizationEndpoint string `json:"authorization_endpoint"`
|
||||
TokenEndpoint string `json:"token_endpoint"`
|
||||
UserinfoEndpoint string `json:"userinfo_endpoint"`
|
||||
}
|
||||
|
||||
// ResolveEndpoints fills Authorize/Token/UserInfo from discovery when missing.
|
||||
func (c *Config) ResolveEndpoints(client *http.Client) error {
|
||||
if c.AuthorizeURL != "" && c.TokenURL != "" && c.UserInfoURL != "" {
|
||||
return nil
|
||||
}
|
||||
issuer := strings.TrimRight(strings.TrimSpace(c.Issuer), "/")
|
||||
if issuer == "" {
|
||||
return fmt.Errorf("oidc: issuer or explicit endpoints required")
|
||||
}
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: 15 * time.Second}
|
||||
}
|
||||
discURL := issuer + "/.well-known/openid-configuration"
|
||||
res, err := client.Get(discURL)
|
||||
if err != nil {
|
||||
return fmt.Errorf("oidc discovery: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if res.StatusCode >= 300 {
|
||||
return fmt.Errorf("oidc discovery HTTP %d: %s", res.StatusCode, truncate(string(body), 200))
|
||||
}
|
||||
var doc discoveryDoc
|
||||
if err := json.Unmarshal(body, &doc); err != nil {
|
||||
return fmt.Errorf("oidc discovery json: %w", err)
|
||||
}
|
||||
if c.AuthorizeURL == "" {
|
||||
c.AuthorizeURL = doc.AuthorizationEndpoint
|
||||
}
|
||||
if c.TokenURL == "" {
|
||||
c.TokenURL = doc.TokenEndpoint
|
||||
}
|
||||
if c.UserInfoURL == "" {
|
||||
c.UserInfoURL = doc.UserinfoEndpoint
|
||||
}
|
||||
if c.AuthorizeURL == "" || c.TokenURL == "" || c.UserInfoURL == "" {
|
||||
return fmt.Errorf("oidc: incomplete discovery endpoints")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// ScopesCSV returns space-separated scopes (default openid profile email).
|
||||
func (c Config) ScopesCSV() string {
|
||||
s := strings.TrimSpace(c.Scopes)
|
||||
if s == "" {
|
||||
return "openid profile email"
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// RandomURLString returns a URL-safe random string of n bytes (base64url, no pad).
|
||||
func RandomURLString(n int) (string, error) {
|
||||
b := make([]byte, n)
|
||||
if _, err := rand.Read(b); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return base64.RawURLEncoding.EncodeToString(b), nil
|
||||
}
|
||||
|
||||
// PKCEChallengeS256 returns code_challenge for a verifier.
|
||||
func PKCEChallengeS256(verifier string) string {
|
||||
sum := sha256.Sum256([]byte(verifier))
|
||||
return base64.RawURLEncoding.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
// AuthorizeRedirect builds the browser redirect to the IdP authorize endpoint.
|
||||
func (c Config) AuthorizeRedirect(state, codeChallenge string) (string, error) {
|
||||
if c.AuthorizeURL == "" {
|
||||
return "", fmt.Errorf("oidc: authorize URL not resolved")
|
||||
}
|
||||
q := url.Values{}
|
||||
q.Set("response_type", "code")
|
||||
q.Set("client_id", c.ClientID)
|
||||
q.Set("redirect_uri", c.RedirectURI)
|
||||
q.Set("scope", c.ScopesCSV())
|
||||
q.Set("state", state)
|
||||
if codeChallenge != "" {
|
||||
q.Set("code_challenge", codeChallenge)
|
||||
q.Set("code_challenge_method", "S256")
|
||||
}
|
||||
sep := "?"
|
||||
if strings.Contains(c.AuthorizeURL, "?") {
|
||||
sep = "&"
|
||||
}
|
||||
return c.AuthorizeURL + sep + q.Encode(), nil
|
||||
}
|
||||
|
||||
// TokenResponse is the OAuth token endpoint JSON.
|
||||
type TokenResponse struct {
|
||||
AccessToken string `json:"access_token"`
|
||||
TokenType string `json:"token_type"`
|
||||
ExpiresIn int `json:"expires_in"`
|
||||
RefreshToken string `json:"refresh_token"`
|
||||
IDToken string `json:"id_token"`
|
||||
Scope string `json:"scope"`
|
||||
Error string `json:"error"`
|
||||
ErrorDesc string `json:"error_description"`
|
||||
}
|
||||
|
||||
// ExchangeCode swaps authorization code for tokens (client_secret_post).
|
||||
func (c Config) ExchangeCode(client *http.Client, code, codeVerifier string) (*TokenResponse, error) {
|
||||
if c.TokenURL == "" {
|
||||
return nil, fmt.Errorf("oidc: token URL not resolved")
|
||||
}
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: 15 * time.Second}
|
||||
}
|
||||
form := url.Values{}
|
||||
form.Set("grant_type", "authorization_code")
|
||||
form.Set("code", code)
|
||||
form.Set("redirect_uri", c.RedirectURI)
|
||||
form.Set("client_id", c.ClientID)
|
||||
form.Set("client_secret", c.ClientSecret)
|
||||
if codeVerifier != "" {
|
||||
form.Set("code_verifier", codeVerifier)
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodPost, c.TokenURL, strings.NewReader(form.Encode()))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
||||
req.Header.Set("Accept", "application/json")
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("oidc token: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var tok TokenResponse
|
||||
if err := json.Unmarshal(body, &tok); err != nil {
|
||||
return nil, fmt.Errorf("oidc token json: %w (%s)", err, truncate(string(body), 200))
|
||||
}
|
||||
if tok.Error != "" {
|
||||
return nil, fmt.Errorf("oidc token: %s (%s)", tok.Error, tok.ErrorDesc)
|
||||
}
|
||||
if tok.AccessToken == "" {
|
||||
return nil, fmt.Errorf("oidc token: empty access_token (HTTP %d)", res.StatusCode)
|
||||
}
|
||||
return &tok, nil
|
||||
}
|
||||
|
||||
// UserInfo is the subset we need from /oauth2/userinfo (CFLMY + standard OIDC).
|
||||
type UserInfo struct {
|
||||
Sub string `json:"sub"`
|
||||
Email string `json:"email"`
|
||||
PreferredUsername string `json:"preferred_username"`
|
||||
Name string `json:"name"`
|
||||
Picture string `json:"picture"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
AdminRole string `json:"admin_role"`
|
||||
}
|
||||
|
||||
// FetchUserInfo calls the userinfo endpoint with a bearer access token.
|
||||
func (c Config) FetchUserInfo(client *http.Client, accessToken string) (*UserInfo, error) {
|
||||
if c.UserInfoURL == "" {
|
||||
return nil, fmt.Errorf("oidc: userinfo URL not resolved")
|
||||
}
|
||||
if client == nil {
|
||||
client = &http.Client{Timeout: 15 * time.Second}
|
||||
}
|
||||
req, err := http.NewRequest(http.MethodGet, c.UserInfoURL, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+accessToken)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
res, err := client.Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("oidc userinfo: %w", err)
|
||||
}
|
||||
defer res.Body.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(res.Body, 1<<20))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if res.StatusCode >= 300 {
|
||||
return nil, fmt.Errorf("oidc userinfo HTTP %d: %s", res.StatusCode, truncate(string(body), 200))
|
||||
}
|
||||
var info UserInfo
|
||||
if err := json.Unmarshal(body, &info); err != nil {
|
||||
return nil, fmt.Errorf("oidc userinfo json: %w", err)
|
||||
}
|
||||
if info.Sub == "" {
|
||||
return nil, fmt.Errorf("oidc userinfo: missing sub")
|
||||
}
|
||||
return &info, nil
|
||||
}
|
||||
|
||||
// LocalUsername picks a stable display username for sessions / RBAC.
|
||||
func (u *UserInfo) LocalUsername() string {
|
||||
if u == nil {
|
||||
return ""
|
||||
}
|
||||
if s := strings.TrimSpace(u.PreferredUsername); s != "" {
|
||||
return s
|
||||
}
|
||||
if s := strings.TrimSpace(u.Email); s != "" {
|
||||
if i := strings.IndexByte(s, '@'); i > 0 {
|
||||
return s[:i]
|
||||
}
|
||||
return s
|
||||
}
|
||||
if s := strings.TrimSpace(u.Name); s != "" {
|
||||
return s
|
||||
}
|
||||
sub := strings.TrimSpace(u.Sub)
|
||||
if len(sub) > 32 {
|
||||
return sub[:32]
|
||||
}
|
||||
return sub
|
||||
}
|
||||
|
||||
// IsIdPAdmin reports whether CFLMY IdP marks this user as an administrator.
|
||||
func (u *UserInfo) IsIdPAdmin() bool {
|
||||
if u == nil {
|
||||
return false
|
||||
}
|
||||
if u.IsAdmin {
|
||||
return true
|
||||
}
|
||||
return strings.TrimSpace(u.AdminRole) != ""
|
||||
}
|
||||
|
||||
// LocalRole maps IdP admin → site admin; otherwise defaultRole (member).
|
||||
func (u *UserInfo) LocalRole(defaultRole string) string {
|
||||
if defaultRole == "" {
|
||||
defaultRole = "member"
|
||||
}
|
||||
if u.IsIdPAdmin() {
|
||||
return "admin"
|
||||
}
|
||||
return defaultRole
|
||||
}
|
||||
|
||||
func truncate(s string, n int) string {
|
||||
if len(s) <= n {
|
||||
return s
|
||||
}
|
||||
return s[:n] + "…"
|
||||
}
|
||||
|
||||
// ParseConfig reads oidc bag from app / auth options (EN + ZH keys).
|
||||
func ParseConfig(m map[string]any) Config {
|
||||
if m == nil {
|
||||
return Config{}
|
||||
}
|
||||
if nested, ok := m["oidc"].(map[string]any); ok {
|
||||
m = nested
|
||||
}
|
||||
c := Config{
|
||||
Issuer: firstStr(m, "issuer", "发行方", "issuer_url"),
|
||||
ClientID: firstStr(m, "client_id", "客户端编号", "客户端id", "clientId"),
|
||||
ClientSecret: firstStr(m, "client_secret", "客户端密钥", "clientSecret"),
|
||||
RedirectURI: firstStr(m, "redirect_uri", "回调", "回调地址", "redirect"),
|
||||
Scopes: firstStr(m, "scopes", "范围", "scope"),
|
||||
CallbackPath: firstStr(m, "callback_path", "回调路径"),
|
||||
AuthorizeURL: firstStr(m, "authorize_url", "授权地址"),
|
||||
TokenURL: firstStr(m, "token_url", "令牌地址"),
|
||||
UserInfoURL: firstStr(m, "userinfo_url", "用户信息地址"),
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
func firstStr(m map[string]any, keys ...string) string {
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok {
|
||||
switch t := v.(type) {
|
||||
case string:
|
||||
if s := strings.TrimSpace(t); s != "" && !strings.EqualFold(s, "none") {
|
||||
return s
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package oidc_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/marqdo/marqdo/plugins/web/internal/oidc"
|
||||
)
|
||||
|
||||
func TestUserInfoLocalRole(t *testing.T) {
|
||||
u := &oidc.UserInfo{PreferredUsername: "alice", IsAdmin: false}
|
||||
if u.LocalRole("member") != "member" {
|
||||
t.Fatal("expected member")
|
||||
}
|
||||
u.IsAdmin = true
|
||||
if u.LocalRole("member") != "admin" {
|
||||
t.Fatal("expected admin from is_admin")
|
||||
}
|
||||
u.IsAdmin = false
|
||||
u.AdminRole = "super_admin"
|
||||
if u.LocalRole("member") != "admin" {
|
||||
t.Fatal("expected admin from admin_role")
|
||||
}
|
||||
if u.LocalUsername() != "alice" {
|
||||
t.Fatalf("username=%s", u.LocalUsername())
|
||||
}
|
||||
}
|
||||
|
||||
func TestPKCEAndParse(t *testing.T) {
|
||||
v, err := oidc.RandomURLString(32)
|
||||
if err != nil || v == "" {
|
||||
t.Fatal(err)
|
||||
}
|
||||
ch := oidc.PKCEChallengeS256(v)
|
||||
if len(ch) < 20 {
|
||||
t.Fatalf("challenge=%q", ch)
|
||||
}
|
||||
c := oidc.ParseConfig(map[string]any{
|
||||
"发行方": "https://id.example.com",
|
||||
"客户端编号": "app_x",
|
||||
"客户端密钥": "secret",
|
||||
"回调": "http://localhost:18085/oidc/callback",
|
||||
})
|
||||
if !c.Enabled() {
|
||||
t.Fatal("expected enabled")
|
||||
}
|
||||
if c.Callback() != "/oidc/callback" {
|
||||
t.Fatalf("callback=%s", c.Callback())
|
||||
}
|
||||
}
|
||||
@@ -111,7 +111,7 @@ func MergeUnique(lists ...[]string) []string {
|
||||
return out
|
||||
}
|
||||
|
||||
const schemaSQL = `
|
||||
const schemaSQLSqlite = `
|
||||
CREATE TABLE IF NOT EXISTS web_permissions (
|
||||
code TEXT PRIMARY KEY,
|
||||
resource TEXT NOT NULL,
|
||||
@@ -147,6 +147,49 @@ CREATE TABLE IF NOT EXISTS web_user_roles (
|
||||
);
|
||||
`
|
||||
|
||||
var schemaSQLPostgres = []string{
|
||||
`CREATE TABLE IF NOT EXISTS web_permissions (
|
||||
code TEXT PRIMARY KEY,
|
||||
resource TEXT NOT NULL,
|
||||
action TEXT NOT NULL,
|
||||
description TEXT
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS web_roles (
|
||||
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
is_system INTEGER NOT NULL DEFAULT 0,
|
||||
tenant_id TEXT
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS web_role_permissions (
|
||||
role_id INTEGER NOT NULL,
|
||||
permission_code TEXT NOT NULL,
|
||||
PRIMARY KEY (role_id, permission_code),
|
||||
FOREIGN KEY (role_id) REFERENCES web_roles(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (permission_code) REFERENCES web_permissions(code) ON DELETE CASCADE
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS web_users (
|
||||
id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
|
||||
username TEXT NOT NULL UNIQUE,
|
||||
password_hash TEXT NOT NULL,
|
||||
created_at TEXT
|
||||
)`,
|
||||
`CREATE TABLE IF NOT EXISTS web_user_roles (
|
||||
user_id INTEGER NOT NULL,
|
||||
role_id INTEGER NOT NULL,
|
||||
tenant_id TEXT NOT NULL DEFAULT '',
|
||||
PRIMARY KEY (user_id, role_id, tenant_id),
|
||||
FOREIGN KEY (user_id) REFERENCES web_users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (role_id) REFERENCES web_roles(id) ON DELETE CASCADE
|
||||
)`,
|
||||
}
|
||||
|
||||
func insertIgnoreSQL(dbURL, table, cols, placeholders, conflict string) string {
|
||||
if db.IsPostgres(dbURL) {
|
||||
return fmt.Sprintf(`INSERT INTO %s(%s) VALUES(%s) ON CONFLICT %s DO NOTHING`, table, cols, placeholders, conflict)
|
||||
}
|
||||
return fmt.Sprintf(`INSERT OR IGNORE INTO %s(%s) VALUES(%s)`, table, cols, placeholders)
|
||||
}
|
||||
|
||||
// EnsureSchema creates RBAC tables and seeds default catalog + system roles.
|
||||
func EnsureSchema(dbURL string) error {
|
||||
if strings.TrimSpace(dbURL) == "" {
|
||||
@@ -156,25 +199,29 @@ func EnsureSchema(dbURL string) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := sqlDB.Exec(schemaSQL); err != nil {
|
||||
if db.IsPostgres(dbURL) {
|
||||
for _, stmt := range schemaSQLPostgres {
|
||||
if _, err := sqlDB.Exec(stmt); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if _, err := sqlDB.Exec(schemaSQLSqlite); err != nil {
|
||||
return err
|
||||
}
|
||||
permSQL := insertIgnoreSQL(dbURL, "web_permissions", "code, resource, action, description", "?,?,?,?", "(code)")
|
||||
for _, p := range DefaultCatalog() {
|
||||
_, _ = sqlDB.Exec(
|
||||
`INSERT OR IGNORE INTO web_permissions(code, resource, action, description) VALUES(?,?,?,?)`,
|
||||
p.Code, p.Resource, p.Action, p.Description,
|
||||
)
|
||||
_, _ = sqlDB.Exec(permSQL, p.Code, p.Resource, p.Action, p.Description)
|
||||
}
|
||||
if err := ensureSystemRole(sqlDB, "superadmin", true, AllCodes()); err != nil {
|
||||
if err := ensureSystemRole(sqlDB, dbURL, "superadmin", true, AllCodes()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := ensureSystemRole(sqlDB, "member", true, []string{"comments:create", "comments:delete_own"}); err != nil {
|
||||
if err := ensureSystemRole(sqlDB, dbURL, "member", true, []string{"comments:create", "comments:delete_own"}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func ensureSystemRole(sqlDB *sql.DB, name string, isSystem bool, perms []string) error {
|
||||
func ensureSystemRole(sqlDB db.SQLDB, dbURL, name string, isSystem bool, perms []string) error {
|
||||
var id int64
|
||||
err := sqlDB.QueryRow(`SELECT id FROM web_roles WHERE name = ?`, name).Scan(&id)
|
||||
if err == sql.ErrNoRows {
|
||||
@@ -182,22 +229,27 @@ func ensureSystemRole(sqlDB *sql.DB, name string, isSystem bool, perms []string)
|
||||
if isSystem {
|
||||
sys = 1
|
||||
}
|
||||
res, err := sqlDB.Exec(`INSERT INTO web_roles(name, is_system) VALUES(?,?)`, name, sys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id, err = res.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
if db.IsPostgres(dbURL) {
|
||||
err = sqlDB.QueryRow(`INSERT INTO web_roles(name, is_system) VALUES(?,?) RETURNING id`, name, sys).Scan(&id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
res, err := sqlDB.Exec(`INSERT INTO web_roles(name, is_system) VALUES(?,?)`, name, sys)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
id, err = res.LastInsertId()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
} else if err != nil {
|
||||
return err
|
||||
}
|
||||
rpSQL := insertIgnoreSQL(dbURL, "web_role_permissions", "role_id, permission_code", "?,?", "(role_id, permission_code)")
|
||||
for _, code := range perms {
|
||||
_, _ = sqlDB.Exec(
|
||||
`INSERT OR IGNORE INTO web_role_permissions(role_id, permission_code) VALUES(?,?)`,
|
||||
id, code,
|
||||
)
|
||||
_, _ = sqlDB.Exec(rpSQL, id, code)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -243,7 +295,7 @@ func AssignRole(dbURL, username, roleName string) error {
|
||||
return fmt.Errorf("rbac assign: role %q: %w", roleName, err)
|
||||
}
|
||||
_, err = sqlDB.Exec(
|
||||
`INSERT OR IGNORE INTO web_user_roles(user_id, role_id, tenant_id) VALUES(?,?,?)`,
|
||||
insertIgnoreSQL(dbURL, "web_user_roles", "user_id, role_id, tenant_id", "?,?,?", "(user_id, role_id, tenant_id)"),
|
||||
uid, rid, "",
|
||||
)
|
||||
return err
|
||||
@@ -258,6 +310,11 @@ func CreateRole(dbURL, name string) (int64, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if db.IsPostgres(dbURL) {
|
||||
var id int64
|
||||
err = sqlDB.QueryRow(`INSERT INTO web_roles(name, is_system) VALUES(?,0) RETURNING id`, name).Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
res, err := sqlDB.Exec(`INSERT INTO web_roles(name, is_system) VALUES(?,0)`, name)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
@@ -325,8 +382,17 @@ func CreateUser(dbURL, username, passwordHash string) (int64, error) {
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
now := "datetime('now')"
|
||||
if db.IsPostgres(dbURL) {
|
||||
var id int64
|
||||
err = sqlDB.QueryRow(
|
||||
`INSERT INTO web_users(username, password_hash, created_at) VALUES(?,?,to_char(now() AT TIME ZONE 'UTC','YYYY-MM-DD HH24:MI:SS')) RETURNING id`,
|
||||
username, passwordHash,
|
||||
).Scan(&id)
|
||||
return id, err
|
||||
}
|
||||
res, err := sqlDB.Exec(
|
||||
`INSERT INTO web_users(username, password_hash, created_at) VALUES(?,?,datetime('now'))`,
|
||||
`INSERT INTO web_users(username, password_hash, created_at) VALUES(?,?,`+now+`)`,
|
||||
username, passwordHash,
|
||||
)
|
||||
if err != nil {
|
||||
@@ -504,7 +570,6 @@ func SetRolePermissionsByName(dbURL, roleName string, codes, grantable []string)
|
||||
return SetRolePermissions(dbURL, id, codes, grantable)
|
||||
}
|
||||
|
||||
func openDB(dbURL string) (*sql.DB, error) {
|
||||
// Reuse db package pool via a no-op select path: open through Ensure-compatible helper.
|
||||
return db.OpenSQL(dbURL)
|
||||
func openDB(dbURL string) (db.SQLDB, error) {
|
||||
return db.OpenSQLDB(dbURL)
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package render
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
rePostForm = regexp.MustCompile(`(?is)<form\b([^>]*\bmethod\s*=\s*["']post["'][^>]*)>([\s\S]*?)</form>`)
|
||||
reAction = regexp.MustCompile(`(?i)\baction\s*=\s*["']([^"']*)["']`)
|
||||
reCSRFIn = regexp.MustCompile(`(?i)\bname\s*=\s*["']_csrf["']`)
|
||||
reNextIn = regexp.MustCompile(`(?i)\bname\s*=\s*["']next["']`)
|
||||
)
|
||||
|
||||
// AuthFormSpec describes a progressive-enhancement login/register form.
|
||||
type AuthFormSpec struct {
|
||||
Action string
|
||||
Submit string
|
||||
FormID string
|
||||
ErrID string
|
||||
Target string
|
||||
NextDefault string
|
||||
Kind string // login | register
|
||||
}
|
||||
|
||||
// AuthFormFromPage reads page["auth_form"].
|
||||
func AuthFormFromPage(page map[string]any) *AuthFormSpec {
|
||||
if page == nil {
|
||||
return nil
|
||||
}
|
||||
raw, ok := page["auth_form"].(map[string]any)
|
||||
if !ok || raw == nil {
|
||||
return nil
|
||||
}
|
||||
spec := &AuthFormSpec{
|
||||
Action: strings.TrimSpace(text(raw["action"])),
|
||||
Submit: strings.TrimSpace(text(raw["submit"])),
|
||||
FormID: strings.TrimSpace(text(raw["form_id"])),
|
||||
ErrID: strings.TrimSpace(text(raw["err_id"])),
|
||||
Target: strings.TrimSpace(text(raw["target"])),
|
||||
NextDefault: strings.TrimSpace(text(raw["next"])),
|
||||
Kind: strings.TrimSpace(strings.ToLower(text(raw["kind"]))),
|
||||
}
|
||||
if spec.Action == "" {
|
||||
return nil
|
||||
}
|
||||
if spec.Submit == "" {
|
||||
if spec.Kind == "register" {
|
||||
spec.Submit = "注册"
|
||||
} else {
|
||||
spec.Submit = "登录"
|
||||
}
|
||||
}
|
||||
if spec.FormID == "" {
|
||||
if spec.Kind == "register" {
|
||||
spec.FormID = "site-register-form"
|
||||
} else {
|
||||
spec.FormID = "site-login-form"
|
||||
}
|
||||
}
|
||||
if spec.ErrID == "" {
|
||||
spec.ErrID = "site-auth-err"
|
||||
}
|
||||
if spec.Kind == "" {
|
||||
if strings.Contains(spec.Action, "register") {
|
||||
spec.Kind = "register"
|
||||
} else {
|
||||
spec.Kind = "login"
|
||||
}
|
||||
}
|
||||
return spec
|
||||
}
|
||||
|
||||
// RenderAuthFormHTML builds a complete auth form (CSRF/next filled at inject time too).
|
||||
func RenderAuthFormHTML(spec *AuthFormSpec, csrf, next, flash string) string {
|
||||
if spec == nil {
|
||||
return ""
|
||||
}
|
||||
next = strings.TrimSpace(next)
|
||||
if next == "" {
|
||||
next = strings.TrimSpace(spec.NextDefault)
|
||||
}
|
||||
var b strings.Builder
|
||||
fmt.Fprintf(&b, `<form id="%s" class="auth-form mq-auth-form" method="post" action="%s">`, esc(spec.FormID), esc(spec.Action))
|
||||
if csrf != "" {
|
||||
fmt.Fprintf(&b, `<input type="hidden" name="_csrf" value="%s"/>`, esc(csrf))
|
||||
}
|
||||
if next != "" && spec.Kind != "register" {
|
||||
fmt.Fprintf(&b, `<input type="hidden" name="next" value="%s"/>`, esc(next))
|
||||
}
|
||||
b.WriteString(`<label>用户名<input name="username" autocomplete="username" required autofocus/></label>`)
|
||||
b.WriteString(`<label>密码<input name="password" type="password" autocomplete="`)
|
||||
if spec.Kind == "register" {
|
||||
b.WriteString(`new-password`)
|
||||
} else {
|
||||
b.WriteString(`current-password`)
|
||||
}
|
||||
b.WriteString(`" required`)
|
||||
if spec.Kind == "register" {
|
||||
b.WriteString(` minlength="4"`)
|
||||
}
|
||||
b.WriteString(`/></label>`)
|
||||
fmt.Fprintf(&b, `<button type="submit">%s</button>`, esc(spec.Submit))
|
||||
b.WriteString(`</form>`)
|
||||
flashAttr := ` hidden`
|
||||
flashText := ""
|
||||
if strings.TrimSpace(flash) != "" {
|
||||
flashAttr = ""
|
||||
flashText = strings.TrimSpace(flash)
|
||||
}
|
||||
fmt.Fprintf(&b, `<p id="%s" class="auth-err"%s>%s</p>`, esc(spec.ErrID), flashAttr, esc(flashText))
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// ApplyAuthFormIntoIntro injects generated form into intro HTML target, or appends it.
|
||||
func ApplyAuthFormIntoIntro(intro string, spec *AuthFormSpec, csrf, next, flash string) string {
|
||||
if spec == nil {
|
||||
return intro
|
||||
}
|
||||
formHTML := RenderAuthFormHTML(spec, csrf, next, flash)
|
||||
target := strings.TrimSpace(spec.Target)
|
||||
if target != "" {
|
||||
if injected, ok := injectHTMLIntoID(intro, target, formHTML); ok {
|
||||
return injected
|
||||
}
|
||||
}
|
||||
if intro == "" {
|
||||
return formHTML
|
||||
}
|
||||
return intro + formHTML
|
||||
}
|
||||
|
||||
// IsAuthAction reports whether a form action path is a login/register endpoint.
|
||||
func IsAuthAction(action string) bool {
|
||||
a := strings.TrimSpace(action)
|
||||
if a == "" {
|
||||
return false
|
||||
}
|
||||
// Absolute same-origin paths only.
|
||||
if strings.HasPrefix(a, "http://") || strings.HasPrefix(a, "https://") || strings.HasPrefix(a, "//") {
|
||||
return false
|
||||
}
|
||||
path := a
|
||||
if i := strings.IndexByte(path, '?'); i >= 0 {
|
||||
path = path[:i]
|
||||
}
|
||||
path = strings.TrimRight(path, "/")
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
base := path
|
||||
if i := strings.LastIndexByte(path, '/'); i >= 0 {
|
||||
base = path[i+1:]
|
||||
}
|
||||
return base == "login" || base == "register" ||
|
||||
strings.HasSuffix(path, "/login") || strings.HasSuffix(path, "/register")
|
||||
}
|
||||
|
||||
// InjectAuthFields ensures POST auth forms carry _csrf / next, and surfaces flash errors.
|
||||
func InjectAuthFields(html, csrf, next, flash string) string {
|
||||
csrf = strings.TrimSpace(csrf)
|
||||
next = strings.TrimSpace(next)
|
||||
flash = strings.TrimSpace(flash)
|
||||
if csrf == "" && next == "" && flash == "" {
|
||||
return html
|
||||
}
|
||||
out := rePostForm.ReplaceAllStringFunc(html, func(full string) string {
|
||||
m := rePostForm.FindStringSubmatch(full)
|
||||
if len(m) < 3 {
|
||||
return full
|
||||
}
|
||||
attrs, body := m[1], m[2]
|
||||
am := reAction.FindStringSubmatch(attrs)
|
||||
action := ""
|
||||
if len(am) > 1 {
|
||||
action = am[1]
|
||||
}
|
||||
if !IsAuthAction(action) {
|
||||
return full
|
||||
}
|
||||
var inject strings.Builder
|
||||
if csrf != "" && !reCSRFIn.MatchString(body) {
|
||||
fmt.Fprintf(&inject, `<input type="hidden" name="_csrf" value="%s"/>`, esc(csrf))
|
||||
}
|
||||
if next != "" && !reNextIn.MatchString(body) && !strings.Contains(strings.ToLower(action), "register") {
|
||||
fmt.Fprintf(&inject, `<input type="hidden" name="next" value="%s"/>`, esc(next))
|
||||
}
|
||||
if inject.Len() == 0 {
|
||||
return full
|
||||
}
|
||||
return `<form` + attrs + `>` + inject.String() + body + `</form>`
|
||||
})
|
||||
if flash != "" {
|
||||
out = surfaceAuthFlash(out, flash)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
var reErrSlot = regexp.MustCompile(`(?is)(<(?:p|div)\b[^>]*\bid\s*=\s*["'](?:site-auth-err|desk-login-err|auth-err)["'][^>]*)(>)([^<]*)(</(?:p|div)>)`)
|
||||
|
||||
func surfaceAuthFlash(html, flash string) string {
|
||||
replaced := false
|
||||
out := reErrSlot.ReplaceAllStringFunc(html, func(full string) string {
|
||||
m := reErrSlot.FindStringSubmatch(full)
|
||||
if len(m) < 5 {
|
||||
return full
|
||||
}
|
||||
replaced = true
|
||||
open := m[1]
|
||||
open = regexp.MustCompile(`(?i)\s*\bhidden\b`).ReplaceAllString(open, "")
|
||||
return open + m[2] + esc(flash) + m[4]
|
||||
})
|
||||
if replaced {
|
||||
return out
|
||||
}
|
||||
flashHTML := `<p class="flash err auth-err">` + esc(flash) + `</p>`
|
||||
if i := strings.Index(out, `<main`); i >= 0 {
|
||||
if j := strings.Index(out[i:], `>`); j >= 0 {
|
||||
at := i + j + 1
|
||||
return out[:at] + flashHTML + out[at:]
|
||||
}
|
||||
}
|
||||
return flashHTML + out
|
||||
}
|
||||
Loaded 100 of 191 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user