From 92d88fe76171387c3bf177eedae885991cc3a5a2 Mon Sep 17 00:00:00 2001 From: cflmy Date: Thu, 6 Aug 2026 15:28:39 +0800 Subject: [PATCH] Move LLM and agent into ext/ai with host tool catalog and call_fn. Consolidate official AI extensions under ext/ai/, add agent history/context host APIs, dynamic call_fn, and a first-pass TOOL:name loop so runbooks can list zero-arg tools in a table. Co-authored-by: Cursor --- doc/design/ext-agent.md | 224 +++++++++++--------------- doc/design/ext-cli.md | 47 ++++-- doc/design/ext-llm.md | 16 +- ext/agent.mq.md | 117 -------------- ext/ai/agent.mq.md | 111 +++++++++++++ ext/{ => ai}/llm.mq.md | 8 +- ext/{ => ai}/大模型.mq.md | 8 +- ext/ai/智能体.mq.md | 111 +++++++++++++ ext/智能体.mq.md | 105 ------------- lib/json.mq.md | 13 ++ skills/marqdo/reference.md | 8 +- src/aliases.rs | 2 + src/bytecode/compile.rs | 16 ++ src/bytecode/mod.rs | 6 + src/bytecode/vm.rs | 56 +++++++ src/ext_cli.rs | 14 +- src/host/agent_rt.rs | 277 +++++++++++++++++++++++++++++++++ src/host/dispatch.rs | 79 +++++++++- src/host/mod.rs | 19 +++ src/interp.rs | 35 ++++- src/lib.rs | 8 +- tests/ext/agent-assign.mq.md | 35 ----- tests/ext/agent-run-live.mq.md | 40 +++++ tests/ext/agent-scaffold.mq.md | 30 ---- tests/ext/agent-smoke.mq.md | 60 +++++++ tests/ext/llm-complete.mq.md | 2 +- tests/ext/llm-import.mq.md | 2 +- tests/gold.rs | 127 +++++---------- tests/lib/dotenv-fixture.env | 1 + 29 files changed, 1019 insertions(+), 558 deletions(-) delete mode 100644 ext/agent.mq.md create mode 100644 ext/ai/agent.mq.md rename ext/{ => ai}/llm.mq.md (94%) rename ext/{ => ai}/大模型.mq.md (93%) create mode 100644 ext/ai/智能体.mq.md delete mode 100644 ext/智能体.mq.md create mode 100644 src/host/agent_rt.rs delete mode 100644 tests/ext/agent-assign.mq.md create mode 100644 tests/ext/agent-run-live.mq.md delete mode 100644 tests/ext/agent-scaffold.mq.md create mode 100644 tests/ext/agent-smoke.mq.md create mode 100644 tests/lib/dotenv-fixture.env diff --git a/doc/design/ext-agent.md b/doc/design/ext-agent.md index 9c70f19..e4e85b8 100644 --- a/doc/design/ext-agent.md +++ b/doc/design/ext-agent.md @@ -1,171 +1,137 @@ -# Official agent development framework (`ext/agent`) +# Official agent development framework (`ext/ai/agent`) | | | |---|---| -| Status | Accepted (v0.1 layout) · **roadmap recorded** | +| Status | Accepted · **thin agent loop locked** | | Date | 2026-08-06 | -| Related | [ext-llm.md](ext-llm.md) · [ext-cli.md](ext-cli.md) · [ext-abi.md](ext-abi.md) · [objects.md](objects.md) · [markdown-mapping.md](markdown-mapping.md) | +| Related | [ext-llm.md](ext-llm.md) · [ext-cli.md](ext-cli.md) · [objects.md](objects.md) | -## Positioning +## What we are building -`ext/agent` / `ext/智能体` is an **official agent-development framework**, not a single chatbot wrapper. +An **agent-development framework** — smallest runtime surface for agents on Marqdo. + +**Not** task-dispatch, **not** bundled domain tools, **not** layout/match/ticket helpers in official ext. + +| In scope | Out of scope (your runbook / your code) | +|----------|-------------------------------------------| +| `# 智能体` ctor | `## 技能匹配` · `## 分配任务` · `## 创建工单` … | +| `## 执行` | Anything task-specific in `ext/` | +| `## 清空历史` | | +| Auto context: source, position, history, Marqdo skill | | + +## Repo layout (`ext/`) + +Official extensions for LLM + agent live under **`ext/ai/`** only. No flat `.mq.md` at `ext/` root; future domains get their own subdir. + +```text +ext/ + ai/ + llm.mq.md + 大模型.mq.md + agent.mq.md + 智能体.mq.md +``` + +Import: `> ext/ai/智能体.mq.md` · `> ext/ai/llm.mq.md` + +## Layering | Layer | Role | |-------|------| -| **Marqdo program** | Orchestration: tables, loops, branches, narration — **prompts and control flow live in the same `.mq.md`** | -| **`ext/llm`** | Model I/O (chat / complete); agent framework **imports and uses** it | -| **`ext/agent`** | Workspace conventions + (roadmap) match / assign / report helpers; optional **ABI** for heavy or OS-bound work | -| **`lib/*`** | fs / json / sys / net / table — generic I/O | - -**Why Marqdo:** an agent runbook is readable docs and executable policy at once. Skill matrices, task queues, and escalation rules stay next to the prose that explains them. +| **Your runbook** | Tables, loops, **your** tools & logic | +| **`ext/ai/llm`** | Model I/O | +| **`ext/ai/agent`** | Framework: ctor + `执行` + `清空历史` | +| **`lib/*`** | Generic I/O | ```text -user agent .mq.md - │ > ext/agent.mq.md (+ often > ext/llm.mq.md) +> ext/ai/智能体.mq.md ▼ -framework (# agent / # 智能体 + helpers) - ├── ext/llm → model - ├── lib/fs|json|… → data / side effects - └── plugins/agent → ABI helpers (layout today; more later) +# 智能体 (大模型, 工具, 用户提示信息?) + ## 执行 + ## 清空历史 ``` -Install story for end users (not yet shipped): [`ext-cli.md`](ext-cli.md) — `marqdo ext add agent` / `add llm`. +`工具` = caller-supplied **function catalog** (table of `##` names); framework resolves names via `call_fn` / `调用` and runs the matching zero-arg function in the runbook module. + +Install: [`ext-cli.md`](ext-cli.md) — `marqdo ext add agent` / `add llm`. + +### Tools (v1) + +Tools are **not** framework builtins — you define `## 函数名` in your runbook and list names in a table: + +```markdown +`工具表` = +| 工具 | +|------| +| 获取时间 | +| 分配任务 | +``` + +- Single-column table → list of function **names** (text). +- Multi-column table → each row is a map; name from column `工具` / `tools` / `name`. +- Marqdo has no first-class function values in tables yet; the name must match a `##` in the same module (or imports). `执行` calls it with `call_fn` after the model replies `TOOL:`. +- Do **not** use `parse text=["fn"]` for the tools list — unquoted identifiers inside `parse` are evaluated as calls. --- -## Shipped in v0.1 (layout) +## Public API (v1) -**Layout framework** for agent-oriented projects: +| | Role | +|--|------| +| `# 智能体` / `# agent` | **大模型**, **工具**, optional **用户提示信息** | +| `## 执行` / `## run` | **额外信息**; context + LLM + history append | +| `## 清空历史` / `## clear_history` | Wipe history on this handle | -- Convention dirs under a project root -- Native helpers via **ABI v1** (`plugins/agent`) -- Marqdo object API in `ext/agent.mq.md` / `ext/智能体.mq.md` +Module-level `##` helpers (e.g. context assembly) are internal — not public contract. -### Project layout +### Auto context (each `执行`) -| Path | Role | -|------|------| -| `agents/` | Agent programs (`.mq.md`) | -| `runbooks/` | Executable handbooks / checks | -| `templates/` | Scaffold sources | -| `reports/` | Generated artifacts (convention: gitignore) | +- `.mq.md` source +- Call-site / position +- Conversation history on this handle +- Marqdo skill (`skills/marqdo/`) -Root markers (any): directory `agents` or `runbooks`, or file `marqdo.agent.json`. +### History (v1) -### ABI surface (`plugins/agent`) - -| Name | Params | Result | -|------|--------|--------| -| `agent_find_root` | `start`, `markers` | text path | -| `agent_ensure_layout` | `root` | int (dirs created) | -| `agent_probe` | `root` | map (`has_agents`, …, `root`) | -| `agent_scaffold` | `root`, `name`, `template`, `dest` | path written; body substitutes `{{name}}` | -| `agent_match_skill` | `skill`, `members` | member map or `null` (JSON array of objects) | -| `agent_bump_load` | `member`, `delta` | updated member map | - -Path safety: under `root`; reject `..`. Build: `cargo build -p marqdo_plugin_agent`. - -### Marqdo surface (v0.1) - -| English | Chinese | Role | -|---------|---------|------| -| `## load_native` | `## 加载原生` | Env / `ext add` → load ABI plugin | -| `# agent` | `# 智能体` | Workspace handle; optional `root=` | -| `## probe` / `ensure_layout` / `scaffold` | `## 探测` / `确保布局` / `脚手架` | Layout | -| `## match_skill` | `## 技能匹配` | Match member maps; lowest load | -| `## assign_task` | `## 分配任务` | `{success, task_id, assignee}` | -| `## update_load` | `## 更新负载` | Bump load via ABI | -| `## create_ticket` / `notify` | `## 创建工单` / `通知` | Stubs | -| `## draft_message` | `## 起草消息` | Delegate to `ext/llm` | - -Multi-column GFM tables → **list of maps** (header keys). Single-column stays list of cells. - -Gold: `tests/ext/agent-scaffold.mq.md`, `tests/ext/agent-assign.mq.md`. +- Host-backed id per handle +- Append after each successful `执行` +- `清空历史` ships with `执行` --- -## Target product (record — not all implemented) - -Framework should make it natural to write agents like **task assignment**: read queues and skill tables, match people, assign or escalate — with optional LLM for soft matching / message drafting. - -### Orchestration sketch (illustrative) - -Syntax below is **aspirational**; names need not match final APIs. Point is: **tables + loop + framework methods + narration**. +## Example runbook (application — not framework) ```markdown --- -> ext/agent.mq.md -> ext/llm.mq.md -> lib/table.mq.md +> ext/ai/智能体.mq.md +> ext/ai/大模型.mq.md --- # main -> load_env -> load_native - -*`model` = > llm * -*`bot` = > agent * - -团队成员技能表 - -*`技能表` = -| 成员 | 技能 | 负载 | -|------|------|------| -| 张三 | Python, ML | 2 | -| 李四 | 前端, UX | 1 | -| 王五 | 后端, 运维 | 3 | -| 赵六 | 数据分析 | 0 | - -*`任务队列` = -| 任务ID | 所需技能 | 优先级 | -|--------|----------|--------| -| T001 | Python | 高 | -| T002 | 前端 | 中 | -| T003 | 安全审计 | 高 | - -> 分配策略 任务队列=`任务队列` 技能表=`技能表` model=`model` bot=`bot` - -## 分配策略 - - 任务队列 - - 技能表 - - model - - bot +*`模型` = > 大模型 * +*`工具名` = | 技能匹配 | 分配任务 | … | +*`助手` = > 智能体 大模型=`模型` 工具=`工具名` 用户提示信息=… * - [任务](任务队列) - *`匹配` = > `bot`.技能匹配 技能=`任务`.所需技能 成员表=`技能表` model=`model` * - - + `匹配` - *`结果` = > `bot`.分配任务 任务ID=`任务`.任务ID 分配给=`匹配` * - + … - > `bot`.通知 … # or lib/net / future channel helpers - + * - > `bot`.创建工单 标题=… 详情=… - -> 完成报告 + > `助手`.清空历史 + *`结果` = > `助手`.执行 额外信息=`任务` * ``` -Marqdo strengths here: +Domain `##` functions live in **this file** or your lib — never in `ext/ai/`. -1. **Prompt next to policy** — escalation wording and match rules sit in the same file as the loop. -2. **Tables as data** — skill matrix / queue are first-class collections. -3. **LLM as a tool** — `ext/llm` for fuzzy skill match or message text; deterministic path when tables suffice. -4. **Objects** — `# agent` / `# llm` handles; methods keep the runbook short. +--- -Exact helper names (`技能匹配` / `match_skill`, `分配任务` / `assign`, …) to be fixed when implementing; may be pure Marqdo over `lib/table` + `ext/llm`, with ABI only where native code helps. +## Shipped / roadmap -### Roadmap slices (ordered) +| Item | Status | +|------|--------| +| `ext/ai/` + framework + context injection | in progress | +| Live `执行` test (`tests/ext/agent-run-live.mq.md` + `.env`) | shipped | +| Tool-call loop in `执行` (`TOOL:` → `call_fn`) | shipped | -| Slice | Content | -|-------|---------| -| **A — Install** | [`ext-cli.md`](ext-cli.md): **shipped** `marqdo ext list` / `add` / `remove` | -| **B — Compose LLM** | **shipped**: `ext/agent` imports `ext/llm`; `## draft_message` / `## 起草消息` | -| **C — Match / assign** | **shipped**: multi-column tables → row maps; `match_skill` / `update_load` (ABI); `assign_task`, `create_ticket`, `notify` stubs | -| **D — Channels** | Optional notify hooks (mail/chat) via `lib/net` or thin ABI — still no third-party marketplace | -| **E — Multi-agent** | Later: handoff between agent programs; out of scope until C is solid | +### Non-goals -### Non-goals (still) - -- Third-party plugin registry / arbitrary remote packages -- Embedding native plugins inside the main `marqdo` exe by default -- Replacing `ext/llm` with a second chat stack inside agent +- Domain tools in official ext +- Flat `ext/*.mq.md` at repo root +- Second chat stack beside llm diff --git a/doc/design/ext-cli.md b/doc/design/ext-cli.md index 4f79653..a953eb7 100644 --- a/doc/design/ext-cli.md +++ b/doc/design/ext-cli.md @@ -4,45 +4,58 @@ |---|---| | Status | **Accepted (v0.1)** | | Date | 2026-08-06 | -| Related | [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) · [ext-abi.md](ext-abi.md) | +| Related | [ext-llm.md](ext-llm.md) · [ext-agent.md](ext-agent.md) | ## Commands ```text -marqdo ext list # official catalog + installed? -marqdo ext add llm # install into local ext directory -marqdo ext add agent # .mq.md + platform native plugin +marqdo ext list +marqdo ext add llm +marqdo ext add agent marqdo ext remove llm +marqdo ext remove agent ``` ## Install root 1. `MARQDO_EXT` if set -2. Else `~/.marqdo/ext` (`%USERPROFILE%\.marqdo\ext` on Windows) +2. Else `~/.marqdo/ext` -`ext/…` import resolution also searches this default user dir (see [`load.rs`](../../src/load.rs)). +Imports use paths like `> ext/ai/llm.mq.md` (see [`load.rs`](../../src/load.rs)). ## Source for `add` -1. `MARQDO_EXT_SOURCE` — directory containing `llm.mq.md` / `agent.mq.md` … -2. Else `./ext` near cwd / next to the binary (dev: Marqdo repo `ext/`) +1. `MARQDO_EXT_SOURCE` — directory containing `ai/llm.mq.md` … +2. Else repo `./ext` near cwd / binary -Native plugin for `agent`: `target/debug|release/agent.dll` (or `libagent.so` / `libagent.dylib`), or `MARQDO_EXT_SOURCE/native/`. +## Layout (`ext/`) -`ext add agent` also writes `agent.plugin` (absolute path) and `native/`; `## load_native` uses `host_ext_native_path` when `MARQDO_AGENT_PLUGIN` is unset. Plugin load may open trusted paths under the install root (sandbox allowlist). +Official extensions live under **`ext/ai/`** (LLM + agent framework only). Do not add flat `.mq.md` files at `ext/` root — keeps the tree from accumulating unrelated modules. + +```text +ext/ + ai/ + llm.mq.md + 大模型.mq.md + agent.mq.md + 智能体.mq.md +``` + +Future optional ext domains get their own subdirs (e.g. `ext/foo/`), not mixed into `ai/`. ## Catalog -| Id | Ships | -|----|-------| -| `llm` | `llm.mq.md`, `大模型.mq.md` | -| `agent` | `agent.mq.md`, `智能体.mq.md` + native | +| Id | Installs under `MARQDO_EXT` | +|----|-----------------------------| +| `llm` | `ai/llm.mq.md`, `ai/大模型.mq.md` | +| `agent` | `ai/agent.mq.md`, `ai/智能体.mq.md` | ## Tests -`tests/gold.rs`: `ext_cli_add_list_remove_llm`, `ext_cli_add_agent_with_native`. +`tests/gold.rs`: `ext_cli_add_list_remove_llm`, `ext_cli_add_agent`, `ext_agent_framework_smoke`, `ext_agent_run_live` (needs `tests/ext/.env`). ## Non-goals -- Third-party registry / arbitrary download URLs as default -- Merging into `lib/` +- Third-party registry +- Merging into `lib/` +- Bundling domain/task helpers in official ext diff --git a/doc/design/ext-llm.md b/doc/design/ext-llm.md index d10f3a9..8989e03 100644 --- a/doc/design/ext-llm.md +++ b/doc/design/ext-llm.md @@ -8,19 +8,19 @@ ## Scope -`ext/` holds **official optional extensions** shipped with Marqdo. They are **not** stdlib (`lib/`). Users import only what they need: +`ext/ai/` holds **official LLM + agent** extensions. Import paths: ```markdown --- -> ext/llm.mq.md +> ext/ai/llm.mq.md --- ``` -Resolution: `MARQDO_EXT`, `./ext`, `ext/` next to the binary (same pattern as `MARQDO_LIB`). +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) (**agent development framework** — layout shipped; orchestration + LLM composition on roadmap). Installer CLI (planned): [ext-cli.md](ext-cli.md). Native plugins: [ext-abi.md](ext-abi.md). -**Note:** Prefer `marqdo ext add llm` for end-user install ([ext-cli.md](ext-cli.md)). Resolving `> ext/llm.mq.md` from a repo checkout or local `ext/` still works without the installer. +**Note:** Prefer `marqdo ext add llm`. Import `> ext/ai/llm.mq.md` from repo or install root. ## Platform prerequisites @@ -30,7 +30,7 @@ Other official ext: [ext-agent.md](ext-agent.md) (**agent development framework* | Dotenv | `## load_env` in ext (or `load_dotenv` / `lib/sys`) | | JSON quote | `lib/json` → `quote` | -## `ext/llm` / `ext/大模型` (object handles) +## `ext/ai/llm` / `ext/ai/大模型` (object handles) | English | Chinese | Role | |---------|---------|------| @@ -40,7 +40,7 @@ Other official ext: [ext-agent.md](ext-agent.md) (**agent development framework* ```markdown --- -> ext/llm.mq.md +> ext/ai/llm.mq.md --- # main @@ -65,4 +65,6 @@ Fallbacks: `MARQDO_LLM_API_KEY`, `MARQDO_LLM_BASE_URL`, `MARQDO_LLM_MODEL`. ## Tests - Import smoke: `tests/ext/llm-import.mq.md` -- Live complete (local only): `tests/ext/llm-complete.mq.md` + gitignored `tests/ext/.env` — do not commit API keys +- Live complete (local): `tests/ext/llm-complete.mq.md` + `tests/ext/.env` +- Live agent `执行`: `tests/ext/agent-run-live.mq.md` + same `.env` (gold: `ext_agent_run_live`, skips if missing) +- Offline agent smoke: `tests/ext/agent-smoke.mq.md` (gold: `ext_agent_framework_smoke`) diff --git a/ext/agent.mq.md b/ext/agent.mq.md deleted file mode 100644 index 860fd38..0000000 --- a/ext/agent.mq.md +++ /dev/null @@ -1,117 +0,0 @@ ---- -title: ext/agent -description: Agent development framework (layout ABI + match/assign). Compose with ext/llm. -> lib/plugin.mq.md -> ext/llm.mq.md ---- - -## load_native - -Load `plugins/agent` shared library. Order: env `MARQDO_AGENT_PLUGIN`, then `marqdo ext add agent` (`host_ext_native_path`). - -*`envp` = > env_get name=MARQDO_AGENT_PLUGIN * -+ `envp` - **> load path=`envp`** -+ * - *`np` = > host_ext_native_path name=agent * - + `np` - **> load path=`np`** - + * - > print text=ext/agent: set MARQDO_AGENT_PLUGIN or run marqdo ext add agent - > exit code=1 - -# agent - - root - -Workspace handle. Optional `root=`; else `agent_find_root` from cwd. - -+ `root` - *`r` = `root`* -+ * - *`start` = > cwd * - *`r` = > agent_find_root start=`start` markers=agents,runbooks,marqdo.agent.json * - -*`t` = > parse text={"p":"{\"_type\":\"agent\",\"root\":","s":"}"} * -*`qr` = > quote text=`r` * -*`p` = > get value=`t` key=p * -*`s` = > get value=`t` key=s * -*`raw` = `p` + `qr` + `s` * -**> parse text=`raw`** - -## probe - -*`root` = > get value=`self` key=root * -**> agent_probe root=`root`** - -## ensure_layout - -*`root` = > get value=`self` key=root * -**> agent_ensure_layout root=`root`** - -## scaffold - - name - - template - - dest - -*`root` = > get value=`self` key=root * -**> agent_scaffold root=`root` name=`name` template=`template` dest=`dest`** - -## match_skill - - skill - - members - -Deterministic skill match over a list of member maps (`技能`/`skills`, `负载`/`load`). Picks lowest load among matches. Returns member map or `None`. - -**> agent_match_skill skill=`skill` members=`members`** - -## assign_task - - task_id - - assignee - -Record a successful assignment (map). Caller updates load separately. - -*`t` = > parse text={"p":"{\"success\":true,\"task_id\":","m":",\"assignee\":","s":"}"} * -*`qid` = > quote text=`task_id` * -*`qa` = > stringify value=`assignee` * -*`p` = > get value=`t` key=p * -*`m` = > get value=`t` key=m * -*`s` = > get value=`t` key=s * -*`raw` = `p` + `qid` + `m` + `qa` + `s` * -**> parse text=`raw`** - -## update_load - - member - - delta - -**> agent_bump_load member=`member` delta=`delta`** - -## create_ticket - - title - - detail - -Stub ticket: ensure layout, write `ticket-stub.txt` under cwd (sandbox). Returns the filename. - -*`_n` = > `self`.ensure_layout * -*`body` = `title` * -*`out` = ticket.txt * -> host_write_text path=ticket.txt text=`body` -**`out`** - -## notify - - to - - text - -Stub channel: print a notification line. - -> print text=notify -> print text=`to` -> print text=`text` -**** - -## draft_message - - model - - prompt - -Optional LLM helper: `model` is an `ext/llm` handle. - -**> `model`.complete prompt=`prompt`** diff --git a/ext/ai/agent.mq.md b/ext/ai/agent.mq.md new file mode 100644 index 0000000..365b1ea --- /dev/null +++ b/ext/ai/agent.mq.md @@ -0,0 +1,111 @@ +--- +title: ext/ai/agent +description: Thin agent framework — ctor, run, clear_history only. +> ext/ai/llm.mq.md +--- + +## build_agent_context + - agent + - extra + +Internal: assemble prompt (user prompt, extra, tool list, source, call site, history, Marqdo skill). + +*`user_prompt` = > get value=`agent` key=user_prompt * ++ `user_prompt` + *`up` = `user_prompt`* ++ * + *`up` = None* + +*`tools` = > get value=`agent` key=tools * +*`tools_s` = > host_format_tools_for_llm tools=`tools` * +*`src` = > host_module_source * +*`site` = > host_call_site * +*`site_s` = > stringify value=`site` * +*`id` = > get value=`agent` key=id * +*`hist` = > host_agent_history_get id=`id` * +*`hist_s` = > stringify value=`hist` * +*`skill` = > host_marqdo_skill * +*`extra_s` = > stringify value=`extra` * + +*`sep` = > parse text={"a":"\n\n--- standing ---\n","b":"\n\n--- extra ---\n","c":"\n\n--- tools ---\n","d":"\n\n--- call site ---\n","e":"\n\n--- source ---\n","f":"\n\n--- history ---\n","g":"\n\n--- marqdo skill ---\n"} * +*`a` = > get value=`sep` key=a * +*`b` = > get value=`sep` key=b * +*`c` = > get value=`sep` key=c * +*`d` = > get value=`sep` key=d * +*`e` = > get value=`sep` key=e * +*`f` = > get value=`sep` key=f * +*`g` = > get value=`sep` key=g * + +*`tool_proto` = \n\n--- tool protocol ---\nEach tool is a zero-arg Marqdo function in this module (see tools list). To call one, reply with exactly one line: TOOL:. No other text on that line.\nAfter the tool runs you will get its output and should answer the user.\n * + +*`p` = You are a Marqdo agent. Follow Marqdo skill rules when reading or writing .mq.md. * +*`p` = `p` + `a` + `up` + `b` + `extra_s` + `c` + `tool_proto` + `tools_s` + `d` + `site_s` + `e` + `src` + `f` + `hist_s` + `g` + `skill` * +**`p`** + +# agent + - model + - tools + - user_prompt + +*`id` = > host_agent_alloc * +*`h` = > parse text={"_type":"agent"} * +*`h` = > map_set map=`h` key=id value=`id` * +*`h` = > map_set map=`h` key=model value=`model` * +*`h` = > map_set map=`h` key=tools value=`tools` * + ++ `user_prompt` + *`h` = > map_set map=`h` key=user_prompt value=`user_prompt` * ++ * + *`_` = 1* + +**`h`** + +## clear_history + +*`id` = > get value=`self` key=id * +> host_agent_history_clear id=`id` +**** + +## run + - extra + +*`ctx` = > build_agent_context agent=`self` extra=`extra` * +*`id` = > get value=`self` key=id * + +*`user_turn` = > parse text={"role":"user"} * +*`user_turn` = > map_set map=`user_turn` key=content value=`extra` * +> host_agent_history_append id=`id` item=`user_turn` + +*`model` = > get value=`self` key=model * +*`reply` = > `model`.complete prompt=`ctx` * +*`tools` = > get value=`self` key=tools * +*`reply` = > trim value=`reply` * + +*`parts` = > split value=`reply` sep=: * +*`head` = > at value=`parts` index=0 * +*`tool_name` = > at value=`parts` index=1 * +*`tool_name` = > trim value=`tool_name` * + ++ `head` == TOOL + *`allowed` = > host_tool_allowed tools=`tools` name=`tool_name` * + + `allowed` + *`tool_out` = > call_fn name=`tool_name` * + *`tool_s` = > stringify value=`tool_out` * + *`extra_s` = > stringify value=`extra` * + *`fp` = The user asked: * + *`fp` = `fp` + `extra_s` * + *`fp` = `fp` + . You invoked tool * + *`fp` = `fp` + `tool_name` * + *`fp` = `fp` + which returned: * + *`fp` = `fp` + `tool_s` * + *`fp` = `fp` + . Reply to the user in one brief sentence. * + *`reply` = > `model`.complete prompt=`fp` * + + * + *`reply` = Tool not allowed: `tool_name` * ++ * + *`_` = 1* + +*`as_turn` = > parse text={"role":"assistant"} * +*`as_turn` = > map_set map=`as_turn` key=content value=`reply` * +> host_agent_history_append id=`id` item=`as_turn` +**`reply`** diff --git a/ext/llm.mq.md b/ext/ai/llm.mq.md similarity index 94% rename from ext/llm.mq.md rename to ext/ai/llm.mq.md index 049f671..a360e68 100644 --- a/ext/llm.mq.md +++ b/ext/ai/llm.mq.md @@ -1,6 +1,6 @@ --- -title: ext/llm -description: Official OpenAI-compatible LLM object (English). Not stdlib — import ext/llm.mq.md. +title: ext/ai/llm +description: OpenAI-compatible LLM object (English). Import ext/ai/llm.mq.md. > lib/sys.mq.md > lib/net.mq.md > lib/json.mq.md @@ -26,7 +26,7 @@ Construct an LLM handle from `OPENAI_*` / `MARQDO_LLM_*` (default base OpenAI v1 + `api_key` *`_k` = 1* + * - > print text=ext/llm: set OPENAI_API_KEY or MARQDO_LLM_API_KEY + > print text=ext/ai/llm: set OPENAI_API_KEY or MARQDO_LLM_API_KEY > exit code=1 *`base_url` = > env_get name=OPENAI_BASE_URL * @@ -115,7 +115,7 @@ Chat completion using `self` / `自` handle fields. *`content` = > get value=`message` key=content * **`content`** + * - > print text=ext/llm: HTTP error + > print text=ext/ai/llm: HTTP error > print text=`status` > print text=`raw` > exit code=1 diff --git a/ext/大模型.mq.md b/ext/ai/大模型.mq.md similarity index 93% rename from ext/大模型.mq.md rename to ext/ai/大模型.mq.md index a2b6b46..2482415 100644 --- a/ext/大模型.mq.md +++ b/ext/ai/大模型.mq.md @@ -1,6 +1,6 @@ --- -title: ext/大模型 -description: 官方 OpenAI 兼容大模型对象(中文)。非标准库 — 导入 ext/大模型.mq.md。 +title: ext/ai/大模型 +description: OpenAI 兼容大模型对象(中文)。导入 ext/ai/大模型.mq.md。 > lib/系统.mq.md > lib/网络.mq.md > lib/json.mq.md @@ -26,7 +26,7 @@ description: 官方 OpenAI 兼容大模型对象(中文)。非标准库 — + `api_key` *`_k` = 1* + * - > 打印 内容=ext/大模型: 请设置 OPENAI_API_KEY 或 MARQDO_LLM_API_KEY + > 打印 内容=ext/ai/大模型: 请设置 OPENAI_API_KEY 或 MARQDO_LLM_API_KEY > 退出 码=1 *`base_url` = > 取环境 名=OPENAI_BASE_URL * @@ -115,7 +115,7 @@ description: 官方 OpenAI 兼容大模型对象(中文)。非标准库 — *`content` = > get value=`message` key=content * **`content`** + * - > 打印 内容=ext/大模型: HTTP 错误 + > 打印 内容=ext/ai/大模型: HTTP 错误 > 打印 内容=`status` > 打印 内容=`raw` > 退出 码=1 diff --git a/ext/ai/智能体.mq.md b/ext/ai/智能体.mq.md new file mode 100644 index 0000000..a31a8dc --- /dev/null +++ b/ext/ai/智能体.mq.md @@ -0,0 +1,111 @@ +--- +title: ext/ai/智能体 +description: 薄智能体框架 — 仅构造、执行、清空历史。 +> ext/ai/大模型.mq.md +--- + +## 构建上下文 + - 智能体 + - 额外信息 + +内部:组装提示(用户提示、额外信息、工具列表、源码、位置、历史、Marqdo skill)。 + +*`user_prompt` = > get value=`智能体` key=user_prompt * ++ `user_prompt` + *`up` = `user_prompt`* ++ * + *`up` = 空* + +*`tools` = > get value=`智能体` key=tools * +*`tools_s` = > host_format_tools_for_llm tools=`tools` * +*`src` = > host_module_source * +*`site` = > host_call_site * +*`site_s` = > stringify value=`site` * +*`id` = > get value=`智能体` key=id * +*`hist` = > host_agent_history_get id=`id` * +*`hist_s` = > stringify value=`hist` * +*`skill` = > host_marqdo_skill * +*`extra_s` = > stringify value=`额外信息` * + +*`sep` = > parse text={"a":"\n\n--- standing ---\n","b":"\n\n--- extra ---\n","c":"\n\n--- tools ---\n","d":"\n\n--- call site ---\n","e":"\n\n--- source ---\n","f":"\n\n--- history ---\n","g":"\n\n--- marqdo skill ---\n"} * +*`a` = > get value=`sep` key=a * +*`b` = > get value=`sep` key=b * +*`c` = > get value=`sep` key=c * +*`d` = > get value=`sep` key=d * +*`e` = > get value=`sep` key=e * +*`f` = > get value=`sep` key=f * +*`g` = > get value=`sep` key=g * + +*`tool_proto` = \n\n--- tool protocol ---\nEach tool is a zero-arg Marqdo function in this module (see tools list). To call one, reply with exactly one line: TOOL:. No other text on that line.\nAfter the tool runs you will get its output and should answer the user.\n * + +*`p` = You are a Marqdo agent. Follow Marqdo skill rules when reading or writing .mq.md. * +*`p` = `p` + `a` + `up` + `b` + `extra_s` + `c` + `tool_proto` + `tools_s` + `d` + `site_s` + `e` + `src` + `f` + `hist_s` + `g` + `skill` * +**`p`** + +# 智能体 + - 大模型 + - 工具 + - 用户提示信息 + +*`id` = > host_agent_alloc * +*`h` = > parse text={"_type":"智能体"} * +*`h` = > map_set map=`h` key=id value=`id` * +*`h` = > map_set map=`h` key=model value=`大模型` * +*`h` = > map_set map=`h` key=tools value=`工具` * + ++ `用户提示信息` + *`h` = > map_set map=`h` key=user_prompt value=`用户提示信息` * ++ * + *`_` = 1* + +**`h`** + +## 清空历史 + +*`id` = > get value=`自` key=id * +> host_agent_history_clear id=`id` +**** + +## 执行 + - 额外信息 + +*`ctx` = > 构建上下文 智能体=`自` 额外信息=`额外信息` * +*`id` = > get value=`自` key=id * + +*`user_turn` = > parse text={"role":"user"} * +*`user_turn` = > map_set map=`user_turn` key=content value=`额外信息` * +> host_agent_history_append id=`id` item=`user_turn` + +*`model` = > get value=`自` key=model * +*`reply` = > `model`.运行 提示=`ctx` * +*`tools` = > get value=`自` key=tools * +*`reply` = > trim value=`reply` * + +*`parts` = > split value=`reply` sep=: * +*`head` = > at value=`parts` index=0 * +*`tool_name` = > at value=`parts` index=1 * +*`tool_name` = > trim value=`tool_name` * + ++ `head` == TOOL + *`allowed` = > host_tool_allowed tools=`tools` name=`tool_name` * + + `allowed` + *`tool_out` = > 调用 名=`tool_name` * + *`tool_s` = > stringify value=`tool_out` * + *`extra_s` = > stringify value=`额外信息` * + *`fp` = The user asked: * + *`fp` = `fp` + `extra_s` * + *`fp` = `fp` + . You invoked tool * + *`fp` = `fp` + `tool_name` * + *`fp` = `fp` + which returned: * + *`fp` = `fp` + `tool_s` * + *`fp` = `fp` + . Reply to the user in one brief sentence. * + *`reply` = > `model`.运行 提示=`fp` * + + * + *`reply` = Tool not allowed: `tool_name` * ++ * + *`_` = 1* + +*`as_turn` = > parse text={"role":"assistant"} * +*`as_turn` = > map_set map=`as_turn` key=content value=`reply` * +> host_agent_history_append id=`id` item=`as_turn` +**`reply`** diff --git a/ext/智能体.mq.md b/ext/智能体.mq.md deleted file mode 100644 index 20385bc..0000000 --- a/ext/智能体.mq.md +++ /dev/null @@ -1,105 +0,0 @@ ---- -title: ext/智能体 -description: 智能体开发框架(布局 ABI + 匹配/派发)。可组合 ext/大模型。 -> lib/插件.mq.md -> ext/大模型.mq.md ---- - -## 加载原生 - -加载 `plugins/agent`。顺序:`MARQDO_AGENT_PLUGIN`,否则 `marqdo ext add agent`。 - -*`环境路径` = > 取环境 名=MARQDO_AGENT_PLUGIN * -+ `环境路径` - **> 加载 路径=`环境路径`** -+ * - *`原生` = > host_ext_native_path name=agent * - + `原生` - **> 加载 路径=`原生`** - + * - > 打印 内容=ext/智能体: 请设置 MARQDO_AGENT_PLUGIN 或执行 marqdo ext add agent - > 退出 码=1 - -# 智能体 - - 根 - -+ `根` - *`r` = `根`* -+ * - *`起点` = > 工作目录 * - *`r` = > agent_find_root start=`起点` markers=agents,runbooks,marqdo.agent.json * - -*`t` = > parse text={"p":"{\"_type\":\"智能体\",\"root\":","s":"}"} * -*`qr` = > quote text=`r` * -*`p` = > get value=`t` key=p * -*`s` = > get value=`t` key=s * -*`raw` = `p` + `qr` + `s` * -**> parse text=`raw`** - -## 探测 - -*`root` = > get value=`自` key=root * -**> agent_probe root=`root`** - -## 确保布局 - -*`root` = > get value=`自` key=root * -**> agent_ensure_layout root=`root`** - -## 脚手架 - - 名 - - 模板 - - 目标 - -*`root` = > get value=`自` key=root * -**> agent_scaffold root=`root` name=`名` template=`模板` dest=`目标`** - -## 技能匹配 - - 技能 - - 成员表 - -**> agent_match_skill skill=`技能` members=`成员表`** - -## 分配任务 - - 任务ID - - 分配给 - -*`t` = > parse text={"p":"{\"success\":true,\"task_id\":","m":",\"assignee\":","s":"}"} * -*`qid` = > quote text=`任务ID` * -*`qa` = > stringify value=`分配给` * -*`p` = > get value=`t` key=p * -*`m` = > get value=`t` key=m * -*`s` = > get value=`t` key=s * -*`raw` = `p` + `qid` + `m` + `qa` + `s` * -**> parse text=`raw`** - -## 更新负载 - - 成员 - - 变化 - -**> agent_bump_load member=`成员` delta=`变化`** - -## 创建工单 - - 标题 - - 详情 - -*`_n` = > `自`.确保布局 * -*`body` = `标题` * -*`out` = ticket.txt * -> host_write_text path=ticket.txt text=`body` -**`out`** - -## 通知 - - 收件人 - - 内容 - -> 打印 内容=notify -> 打印 内容=`收件人` -> 打印 内容=`内容` -**** - -## 起草消息 - - 模型 - - 提示 - -**> `模型`.运行 提示=`提示`** diff --git a/lib/json.mq.md b/lib/json.mq.md index 6a5841c..93a9d49 100644 --- a/lib/json.mq.md +++ b/lib/json.mq.md @@ -28,3 +28,16 @@ description: JSON parse/stringify (shared EN/ZH import) - text **> host_json_quote text=`text`** + +## set + - map + - key + - value + +**> host_map_set map=`map` key=`key` value=`value`** + +## append + - list + - item + +**> host_list_append list=`list` item=`item`** diff --git a/skills/marqdo/reference.md b/skills/marqdo/reference.md index ae4e74e..b4d1cea 100644 --- a/skills/marqdo/reference.md +++ b/skills/marqdo/reference.md @@ -20,7 +20,7 @@ Diagnostics look like `path:line:col: message` (1-based line/col). Stdlib search order for `lib/…` imports: `MARQDO_LIB`, `./lib`, directory of the `marqdo` executable (and a few parents). Prefer the **bundle zip** (`marqdo.exe` + `lib/`) over a bare exe. -官方扩展(`ext/…`,非 stdlib):`marqdo ext add` 装到 `MARQDO_EXT` 或 `~/.marqdo/ext`;导入仍写 `> ext/llm.mq.md`。解析还含 cwd `ext/`、二进制旁 `ext/`。对象:`doc/design/objects.md`。Agent:`doc/design/ext-agent.md` · 安装器:`doc/design/ext-cli.md`。 +官方扩展(`ext/ai/…`):`marqdo ext add` 装到 `MARQDO_EXT`;导入写 `> ext/ai/llm.mq.md`。 ## Builtins (no import) @@ -50,8 +50,8 @@ Import one file; use **that** file’s function names. | `lib/json.mq.md` | (see design) | JSON (+ `quote`) | | `lib/math.mq.md` | `lib/数学.mq.md` | num, trig, random, formula, plot | | `lib/foreign.mq.md` | `lib/外联.mq.md` | run foreign fenced blocks | -| `ext/llm.mq.md` | `ext/大模型.mq.md` | `# llm` / `# 大模型` object + chat methods | -| `ext/agent.mq.md` | `ext/智能体.mq.md` | agent **framework** (layout now; LLM orchestration roadmap in `ext-agent.md`) | +| `ext/ai/llm.mq.md` | `ext/ai/大模型.mq.md` | `# llm` / `# 大模型` | +| `ext/ai/agent.mq.md` | `ext/ai/智能体.mq.md` | agent framework: `# 智能体` + `## 执行` + `## 清空历史` | Open the imported `.mq.md` under `lib/` or `ext/` to see exact `##` / `#` names and parameters. Gold tests: `tests/lib/`, `tests/structure/`, `tests/ext/`. @@ -128,6 +128,6 @@ See `doc/design/call-arguments.md`. | `doc/design/stdlib-modules.md` | L1 modules including plugin | | `doc/design/ext-abi.md` | Native C ABI v1 | | `doc/design/ext-llm.md` | `ext/llm` | -| `doc/design/ext-agent.md` | `ext/agent` agent **framework** (layout shipped; LLM orchestration roadmap) | +| `doc/design/ext-agent.md` | `ext/agent` agent **framework** (ctor + run + clear_history; no bundled domain tools) | | `doc/design/ext-cli.md` | `marqdo ext list/add/remove` | | `doc/design/view-debug.md` | view / debug hosts | diff --git a/src/aliases.rs b/src/aliases.rs index 8226f0c..7509eb5 100644 --- a/src/aliases.rs +++ b/src/aliases.rs @@ -36,6 +36,7 @@ pub fn canonical_builtin(name: &str) -> Option<&'static str> { "split" => Some("split"), "join" => Some("join"), "at" => Some("at"), + "call_fn" | "调用" => Some("call_fn"), _ => None, } } @@ -51,6 +52,7 @@ pub fn canonical_param(builtin: &str, param: &str) -> String { ("split" | "join", "sep" | "分隔") => "sep".into(), ("at", "value" | "值") => "value".into(), ("at", "index" | "下标") => "index".into(), + ("call_fn", "name" | "名") => "name".into(), ( "host_plot" | "host_plot_points" | "host_plot_conic", "grid" | "网格", diff --git a/src/bytecode/compile.rs b/src/bytecode/compile.rs index 98317ee..3d5a82c 100644 --- a/src/bytecode/compile.rs +++ b/src/bytecode/compile.rs @@ -65,11 +65,16 @@ pub fn compile_module(path: Option<&Path>, module: &Module) -> Result { functions.push(compile_function(i, &flat, path_buf.as_deref())?); } + let parents: Vec> = flat.iter().map(|f| f.parent).collect(); + let children: Vec> = flat.iter().map(|f| f.children.clone()).collect(); + Ok(Program { functions, entry, objects, methods, + parents, + children, }) } @@ -470,6 +475,17 @@ impl<'a> FnCompiler<'a> { } return Ok(()); } + "call_fn" => { + let name_expr = self + .named_or_first(&call, "name") + .ok_or_else(|| self.err("call_fn requires name"))?; + self.compile_expr(name_expr)?; + self.emit(Op::CallFn); + if as_stmt { + self.emit(Op::Pop); + } + return Ok(()); + } other if crate::host::HostFn::from_name(other).is_some() => { let hf = crate::host::HostFn::from_name(other).unwrap(); let params = hf.all_params(); diff --git a/src/bytecode/mod.rs b/src/bytecode/mod.rs index b1b430e..9c0744d 100644 --- a/src/bytecode/mod.rs +++ b/src/bytecode/mod.rs @@ -56,6 +56,8 @@ pub enum Op { GetIndex, /// Call function by index; argc values are on the stack (param order). Call(u16, u8), + /// Pop function name text → call zero-arg top-level or lexically visible function. + CallFn, /// After a constructor Call: tag top-of-stack map with `_type` = constants[idx]. TagInstance(u16), /// Method call: stack has argc args (param order) then receiver on top. @@ -88,6 +90,10 @@ pub struct Program { pub objects: Vec<(String, usize)>, /// (object_fn_idx, method_name) → method fn idx pub methods: Vec<(usize, String, usize)>, + /// Parallel to `functions`: parent fn index for dynamic `call_fn` lookup. + pub parents: Vec>, + /// Parallel to `functions`: child fn indices. + pub children: Vec>, } impl Program { diff --git a/src/bytecode/vm.rs b/src/bytecode/vm.rs index 3cdf970..ac7b434 100644 --- a/src/bytecode/vm.rs +++ b/src/bytecode/vm.rs @@ -115,6 +115,8 @@ impl Vm { ip: 0, slots: vec![Value::None; program.functions[entry].locals.len().max(1)], }]; + self.host + .push_call_frame(&program.functions[entry].name.clone()); loop { let frame = frames.last_mut().unwrap(); @@ -127,6 +129,7 @@ impl Vm { let op = fun.code[ip]; let span = fun.spans.get(ip).copied().unwrap_or(Span::new(1, 1)); frame.ip += 1; + self.host.current_line = span.line; if self.trace { match op { @@ -347,11 +350,13 @@ impl Vm { for i in (0..argc).rev() { slots[i] = pop(&mut stack).map_err(|m| self.err_at(span, m))?; } + let fname = program.functions[fid].name.clone(); frames.push(Frame { fn_idx: fid, ip: 0, slots, }); + self.host.push_call_frame(&fname); } Op::TagInstance(idx) => { let type_name = match fun.constants.get(idx as usize) { @@ -410,11 +415,13 @@ impl Vm { slots[i] = recv.clone(); } } + let fname = callee.name.clone(); frames.push(Frame { fn_idx: method_fid, ip: 0, slots, }); + self.host.push_call_frame(&fname); } Op::PluginCall(name_idx, argc) => { let argc = argc as usize; @@ -478,9 +485,35 @@ impl Vm { stack.push(result); } } + Op::CallFn => { + let name_v = pop(&mut stack).map_err(|m| self.err_at(span, m))?; + let name = match name_v { + Value::Text(s) => s, + _ => return Err(self.err_at(span, "call_fn requires text name")), + }; + let current = frames.last().unwrap().fn_idx; + let fid = resolve_fn_dynamic(program, current, &name) + .map_err(|m| self.err_at(span, m))?; + let callee = &program.functions[fid]; + if !callee.params.is_empty() { + return Err(self.err_at( + span, + format!("call_fn: `{name}` must have no parameters"), + )); + } + let slots = vec![Value::None; callee.locals.len().max(1)]; + let fname = callee.name.clone(); + frames.push(Frame { + fn_idx: fid, + ip: 0, + slots, + }); + self.host.push_call_frame(&fname); + } Op::Return => { let ret = pop(&mut stack).unwrap_or(Value::None); frames.pop(); + self.host.pop_call_frame(); if frames.is_empty() { return Ok(ret); } @@ -569,3 +602,26 @@ fn tag_instance_value(type_name: &str, value: Value) -> Value { ]), } } + +fn resolve_fn_dynamic(program: &Program, current: usize, name: &str) -> Result { + for &cid in &program.children[current] { + if program.functions[cid].name == name { + return Ok(cid); + } + } + let mut parent = program.parents[current]; + while let Some(pid) = parent { + for &cid in &program.children[pid] { + if program.functions[cid].name == name { + return Ok(cid); + } + } + parent = program.parents[pid]; + } + for (i, f) in program.functions.iter().enumerate() { + if program.parents[i].is_none() && f.name == name { + return Ok(i); + } + } + Err(format!("call_fn: unknown function `{name}`")) +} diff --git a/src/ext_cli.rs b/src/ext_cli.rs index d0d0392..2d89537 100644 --- a/src/ext_cli.rs +++ b/src/ext_cli.rs @@ -20,15 +20,15 @@ pub struct ExtPackage { pub const CATALOG: &[ExtPackage] = &[ ExtPackage { id: "llm", - description: "OpenAI-compatible chat object (ext/llm)", - mq_files: &["llm.mq.md", "大模型.mq.md"], + description: "OpenAI-compatible chat object (ext/ai/llm)", + mq_files: &["ai/llm.mq.md", "ai/大模型.mq.md"], native_crate: None, }, ExtPackage { id: "agent", - description: "Agent development framework (ext/agent + ABI plugin)", - mq_files: &["agent.mq.md", "智能体.mq.md"], - native_crate: Some("marqdo_plugin_agent"), + description: "Agent development framework (ext/ai/agent)", + mq_files: &["ai/agent.mq.md", "ai/智能体.mq.md"], + native_crate: None, }, ]; @@ -198,6 +198,10 @@ pub fn add_ext(id: &str) -> Result<()> { for f in pkg.mq_files { let src = find_source_file(f)?; let dest = root.join(f); + if let Some(parent) = dest.parent() { + fs::create_dir_all(parent) + .with_context(|| format!("create {}", parent.display()))?; + } fs::copy(&src, &dest) .with_context(|| format!("copy {} → {}", src.display(), dest.display()))?; println!("installed {}", dest.display()); diff --git a/src/host/agent_rt.rs b/src/host/agent_rt.rs new file mode 100644 index 0000000..e84d43b --- /dev/null +++ b/src/host/agent_rt.rs @@ -0,0 +1,277 @@ +//! Runtime support for `ext/agent`: module source, call site, skill text, history store. + +use std::path::{Path, PathBuf}; + +use crate::host::HostContext; +use crate::value::Value; + +impl HostContext { + pub fn set_entry_source(&mut self, path: Option<&Path>, source: &str) { + self.entry_source = Some(source.to_string()); + self.entry_path = path.map(|p| p.to_path_buf()); + } + + pub fn push_call_frame(&mut self, name: &str) { + self.call_stack.push(name.to_string()); + } + + pub fn pop_call_frame(&mut self) { + self.call_stack.pop(); + } + + pub fn alloc_agent_id(&mut self) -> String { + self.agent_seq = self.agent_seq.wrapping_add(1); + let id = format!("agent-{}", self.agent_seq); + self.agent_histories.insert(id.clone(), Vec::new()); + id + } +} + +pub fn module_source(ctx: &HostContext) -> Result { + Ok(match &ctx.entry_source { + Some(s) => Value::Text(s.clone()), + None => Value::Text(String::new()), + }) +} + +pub fn call_site(ctx: &HostContext, line: Option) -> Result { + let path = ctx + .entry_path + .as_ref() + .map(|p| p.display().to_string()) + .unwrap_or_default(); + let function = ctx + .call_stack + .last() + .cloned() + .unwrap_or_else(|| "main".into()); + let line_v = match line { + Some(n) => Value::Int(n as i64), + None => Value::None, + }; + Ok(Value::Map(vec![ + ("path".into(), Value::Text(path)), + ("function".into(), Value::Text(function)), + ("line".into(), line_v), + ])) +} + +pub fn marqdo_skill(ctx: &HostContext) -> Result { + let text = load_marqdo_skill(ctx)?; + Ok(Value::Text(text)) +} + +fn load_marqdo_skill(ctx: &HostContext) -> Result { + if let Ok(p) = std::env::var("MARQDO_SKILL") { + let path = PathBuf::from(p); + if path.is_file() { + return std::fs::read_to_string(&path) + .map_err(|e| format!("MARQDO_SKILL read {}: {e}", path.display())); + } + let skill = path.join("SKILL.md"); + if skill.is_file() { + return read_skill_pack(&path); + } + } + + for root in skill_search_roots(ctx) { + let pack = root.join("skills").join("marqdo"); + if pack.join("SKILL.md").is_file() { + return read_skill_pack(&pack); + } + if root.join("SKILL.md").is_file() && root.ends_with("marqdo") { + return read_skill_pack(&root); + } + } + Ok(String::new()) +} + +fn read_skill_pack(dir: &Path) -> Result { + let mut out = String::new(); + for name in ["SKILL.md", "reference.md", "examples.md"] { + let p = dir.join(name); + if p.is_file() { + let body = std::fs::read_to_string(&p) + .map_err(|e| format!("skill read {}: {e}", p.display()))?; + if !out.is_empty() { + out.push_str("\n\n---\n\n"); + } + out.push_str(&format!("# file: {name}\n\n")); + out.push_str(&body); + } + } + Ok(out) +} + +fn skill_search_roots(ctx: &HostContext) -> Vec { + let mut roots = Vec::new(); + if let Ok(exe) = std::env::current_exe() { + if let Some(dir) = exe.parent() { + roots.push(dir.to_path_buf()); + if let Some(parent) = dir.parent() { + roots.push(parent.to_path_buf()); + // target/debug → repo root + if let Some(repo) = parent.parent() { + roots.push(repo.to_path_buf()); + } + } + } + } + roots.push(ctx.cwd.clone()); + if let Some(p) = &ctx.entry_path { + if let Some(parent) = p.parent() { + roots.push(parent.to_path_buf()); + // tests/ext → repo + if let Some(repo) = parent.parent().and_then(|p| p.parent()) { + roots.push(repo.to_path_buf()); + } + } + } + if let Ok(manifest) = std::env::var("CARGO_MANIFEST_DIR") { + roots.push(PathBuf::from(manifest)); + } + roots +} + +pub fn agent_alloc(ctx: &mut HostContext) -> Result { + Ok(Value::Text(ctx.alloc_agent_id())) +} + +pub fn agent_history_get(ctx: &HostContext, id: &Value) -> Result { + let id = text_id(id)?; + Ok(Value::List( + ctx.agent_histories + .get(&id) + .cloned() + .unwrap_or_default(), + )) +} + +pub fn agent_history_clear(ctx: &mut HostContext, id: &Value) -> Result { + let id = text_id(id)?; + ctx.agent_histories.insert(id, Vec::new()); + Ok(Value::None) +} + +pub fn agent_history_append( + ctx: &mut HostContext, + id: &Value, + item: &Value, +) -> Result { + let id = text_id(id)?; + let list = ctx.agent_histories.entry(id).or_default(); + list.push(item.clone()); + Ok(Value::List(list.clone())) +} + +fn text_id(id: &Value) -> Result { + match id { + Value::Text(s) if !s.is_empty() => Ok(s.clone()), + _ => Err("agent history id must be non-empty text".into()), + } +} + +pub fn map_set(map: &Value, key: &Value, value: &Value) -> Result { + let k = match key { + Value::Text(s) => s.clone(), + _ => return Err("map_set key must be text".into()), + }; + match map { + Value::Map(entries) => { + let mut out = entries.clone(); + if let Some((_, slot)) = out.iter_mut().find(|(kk, _)| *kk == k) { + *slot = value.clone(); + } else { + out.push((k, value.clone())); + } + Ok(Value::Map(out)) + } + Value::None => Ok(Value::Map(vec![(k, value.clone())])), + _ => Err("map_set needs map".into()), + } +} + +pub fn list_append(list: &Value, item: &Value) -> Result { + match list { + Value::List(xs) => { + let mut out = xs.clone(); + out.push(item.clone()); + Ok(Value::List(out)) + } + Value::None => Ok(Value::List(vec![item.clone()])), + _ => Err("list_append needs list".into()), + } +} + +fn tool_name_from_row(row: &Value) -> Option { + match row { + Value::Text(s) if !s.is_empty() => Some(s.clone()), + Value::Map(entries) => ["工具", "tools", "name"] + .iter() + .find_map(|key| { + entries.iter().find(|(k, _)| k == *key).and_then(|(_, v)| { + if let Value::Text(s) = v { + (!s.is_empty()).then(|| s.clone()) + } else { + None + } + }) + }), + _ => None, + } +} + +pub fn tool_names(tools: &Value) -> Vec { + match tools { + Value::List(rows) => rows.iter().filter_map(tool_name_from_row).collect(), + _ => Vec::new(), + } +} + +pub fn format_tools_for_llm(tools: &Value) -> Result { + let mut out = String::from("Available tools (invoke via TOOL:):\n"); + for name in tool_names(tools) { + out.push_str("- "); + out.push_str(&name); + out.push('\n'); + } + Ok(Value::Text(out)) +} + +pub fn tool_allowed(tools: &Value, name: &Value) -> Result { + let name = match name { + Value::Text(s) if !s.is_empty() => s.as_str(), + _ => return Ok(Value::Bool(false)), + }; + Ok(Value::Bool( + tool_names(tools).iter().any(|n| n == name), + )) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn tool_catalog_from_table() { + let tools = Value::List(vec![Value::Text("获取时间".into())]); + let text = format_tools_for_llm(&tools).unwrap(); + assert!(matches!(text, Value::Text(s) if s.contains("获取时间"))); + assert!(tool_allowed(&tools, &Value::Text("获取时间".into())).unwrap().truthy()); + assert!(!tool_allowed(&tools, &Value::Text("nope".into())).unwrap().truthy()); + } + + #[test] + fn map_set_and_history() { + let mut ctx = HostContext::default(); + let id = ctx.alloc_agent_id(); + assert!(id.starts_with("agent-")); + agent_history_append(&mut ctx, &Value::Text(id.clone()), &Value::Text("a".into())) + .unwrap(); + let h = agent_history_get(&ctx, &Value::Text(id.clone())).unwrap(); + assert_eq!(h, Value::List(vec![Value::Text("a".into())])); + agent_history_clear(&mut ctx, &Value::Text(id)).unwrap(); + let h2 = agent_history_get(&ctx, &Value::Text("agent-1".into())).unwrap(); + assert_eq!(h2, Value::List(vec![])); + } +} diff --git a/src/host/dispatch.rs b/src/host/dispatch.rs index 83c6bd2..0dbd8e2 100644 --- a/src/host/dispatch.rs +++ b/src/host/dispatch.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; -use crate::host::{foreign, fs, json, math, net, plugin, sys, time, HostContext}; +use crate::host::{agent_rt, foreign, fs, json, math, net, plugin, sys, time, HostContext}; use crate::value::Value; #[derive(Debug, Clone, Copy, PartialEq, Eq)] @@ -81,6 +81,17 @@ pub enum HostFn { PluginUnload = 70, PluginList = 71, ExtNativePath = 72, + ModuleSource = 73, + CallSite = 74, + MarqdoSkill = 75, + MapSet = 76, + ListAppend = 77, + AgentAlloc = 78, + AgentHistoryGet = 79, + AgentHistoryClear = 80, + AgentHistoryAppend = 81, + FormatToolsForLlm = 82, + ToolAllowed = 83, } impl HostFn { @@ -158,6 +169,17 @@ impl HostFn { 70 => Self::PluginUnload, 71 => Self::PluginList, 72 => Self::ExtNativePath, + 73 => Self::ModuleSource, + 74 => Self::CallSite, + 75 => Self::MarqdoSkill, + 76 => Self::MapSet, + 77 => Self::ListAppend, + 78 => Self::AgentAlloc, + 79 => Self::AgentHistoryGet, + 80 => Self::AgentHistoryClear, + 81 => Self::AgentHistoryAppend, + 82 => Self::FormatToolsForLlm, + 83 => Self::ToolAllowed, _ => return None, }) } @@ -239,6 +261,17 @@ impl HostFn { "host_plugin_unload" | "plugin_unload" => Self::PluginUnload, "host_plugin_list" | "plugin_list" => Self::PluginList, "host_ext_native_path" | "ext_native_path" => Self::ExtNativePath, + "host_module_source" | "module_source" => Self::ModuleSource, + "host_call_site" | "call_site" => Self::CallSite, + "host_marqdo_skill" | "marqdo_skill" => Self::MarqdoSkill, + "host_map_set" | "map_set" => Self::MapSet, + "host_list_append" | "list_append" => Self::ListAppend, + "host_agent_alloc" | "agent_alloc" => Self::AgentAlloc, + "host_agent_history_get" | "agent_history_get" => Self::AgentHistoryGet, + "host_agent_history_clear" | "agent_history_clear" => Self::AgentHistoryClear, + "host_agent_history_append" | "agent_history_append" => Self::AgentHistoryAppend, + "host_format_tools_for_llm" | "format_tools_for_llm" => Self::FormatToolsForLlm, + "host_tool_allowed" | "tool_allowed" => Self::ToolAllowed, _ => return None, }) } @@ -321,6 +354,17 @@ impl HostFn { Self::PluginUnload => "host_plugin_unload", Self::PluginList => "host_plugin_list", Self::ExtNativePath => "host_ext_native_path", + Self::ModuleSource => "host_module_source", + Self::CallSite => "host_call_site", + Self::MarqdoSkill => "host_marqdo_skill", + Self::MapSet => "host_map_set", + Self::ListAppend => "host_list_append", + Self::AgentAlloc => "host_agent_alloc", + Self::AgentHistoryGet => "host_agent_history_get", + Self::AgentHistoryClear => "host_agent_history_clear", + Self::AgentHistoryAppend => "host_agent_history_append", + Self::FormatToolsForLlm => "host_format_tools_for_llm", + Self::ToolAllowed => "host_tool_allowed", } } @@ -366,6 +410,13 @@ impl HostFn { Self::PluginLoad => &["path"], Self::PluginUnload | Self::PluginList => &[], Self::ExtNativePath => &["name"], + Self::ModuleSource | Self::CallSite | Self::MarqdoSkill | Self::AgentAlloc => &[], + Self::MapSet => &["map", "key", "value"], + Self::ListAppend => &["list", "item"], + Self::AgentHistoryGet | Self::AgentHistoryClear => &["id"], + Self::AgentHistoryAppend => &["id", "item"], + Self::FormatToolsForLlm => &["tools"], + Self::ToolAllowed => &["tools", "name"], } } @@ -555,6 +606,32 @@ pub fn call_host( None => Value::None, }) } + HostFn::ModuleSource => agent_rt::module_source(ctx), + HostFn::CallSite => { + agent_rt::call_site(ctx, Some(ctx.current_line)) + } + HostFn::MarqdoSkill => agent_rt::marqdo_skill(ctx), + HostFn::MapSet => { + agent_rt::map_set(require(bound, "map")?, require(bound, "key")?, require(bound, "value")?) + } + HostFn::ListAppend => { + agent_rt::list_append(require(bound, "list")?, require(bound, "item")?) + } + HostFn::AgentAlloc => agent_rt::agent_alloc(ctx), + HostFn::AgentHistoryGet => agent_rt::agent_history_get(ctx, require(bound, "id")?), + HostFn::AgentHistoryClear => { + agent_rt::agent_history_clear(ctx, require(bound, "id")?) + } + HostFn::AgentHistoryAppend => { + agent_rt::agent_history_append(ctx, require(bound, "id")?, require(bound, "item")?) + } + HostFn::FormatToolsForLlm => { + agent_rt::format_tools_for_llm(require(bound, "tools")?) + } + HostFn::ToolAllowed => agent_rt::tool_allowed( + require(bound, "tools")?, + require(bound, "name")?, + ), } } diff --git a/src/host/mod.rs b/src/host/mod.rs index 2bb5b40..56ec361 100644 --- a/src/host/mod.rs +++ b/src/host/mod.rs @@ -4,6 +4,7 @@ //! Capabilities default **on** (importing a lib means you intend to use it). //! Soft side-effects (no process::exit; sleep clamp) apply under view / capture only. +pub mod agent_rt; mod dispatch; pub mod foreign; mod fs; @@ -21,6 +22,8 @@ pub use plugin::PluginState; use std::collections::HashMap; use std::path::{Path, PathBuf}; +use crate::value::Value; + /// Host capability flags. Default: all enabled. #[derive(Debug, Clone)] pub struct HostCaps { @@ -65,6 +68,16 @@ pub struct HostContext { pub foreign_cmds: HashMap>, /// Loaded native plugins (ABI v1). pub plugins: PluginState, + /// Entry `.mq.md` source text (for agent context injection). + pub entry_source: Option, + pub entry_path: Option, + /// Active function names (outer → inner) for call-site injection. + pub call_stack: Vec, + /// Per-agent conversation history (id → turns). + pub agent_histories: HashMap>, + pub agent_seq: u64, + /// Last statement line (updated by interpreters for `host_call_site`). + pub current_line: u32, } impl Default for HostContext { @@ -80,6 +93,12 @@ impl Default for HostContext { plots: Vec::new(), foreign_cmds: HashMap::new(), plugins: PluginState::default(), + entry_source: None, + entry_path: None, + call_stack: Vec::new(), + agent_histories: HashMap::new(), + agent_seq: 0, + current_line: 1, } } } diff --git a/src/interp.rs b/src/interp.rs index 76db71b..ab61129 100644 --- a/src/interp.rs +++ b/src/interp.rs @@ -166,13 +166,18 @@ impl Interpreter { } } - let mut ret = Value::None; - for stmt in &fun.body { - if let Some(v) = self.exec_stmt(module, fun, &mut env, stmt)? { - ret = v; - break; + let ret = Value::None; + self.host.push_call_frame(&fun.name); + let body_result = (|| -> Result { + for stmt in &fun.body { + if let Some(v) = self.exec_stmt(module, fun, &mut env, stmt)? { + return Ok(v); + } } - } + Ok(ret) + })(); + self.host.pop_call_frame(); + let ret = body_result?; if self.trace { let display = ret.as_display(); emit_trace( @@ -194,6 +199,7 @@ impl Interpreter { ) -> Result> { let span = stmt_span(stmt); self.current_span = span; + self.host.current_line = span.line; if let Some(dbg) = &self.debug { dbg.on_stmt(span.line, &fun.name, &env.vars, &self.captured_stdout) .map_err(|m| self.err(m))?; @@ -557,6 +563,23 @@ impl Interpreter { .ok_or_else(|| self.err("at requires index"))?; return builtin_at(value, index).map_err(|m| self.err(m)); } + "call_fn" => { + let bound = bind_args(&["name".into()], &ev_args, false) + .map_err(|m| self.err(m))?; + let name = match bound.get("name") { + Some(Value::Text(s)) => s.as_str(), + _ => return Err(self.err("call_fn requires text name")), + }; + let target = lookup_function(module, fun, name).ok_or_else(|| { + self.err(format!("call_fn: unknown function `{name}`")) + })?; + if !target.params.is_empty() { + return Err(self.err(format!( + "call_fn: `{name}` must have no parameters" + ))); + } + return self.run_function(module, target, Env::new(), &[]); + } other if HostFn::from_name(other).is_some() => { let hf = HostFn::from_name(other).unwrap(); let mut params: Vec = diff --git a/src/lib.rs b/src/lib.rs index 94e79c2..f913ef0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -171,7 +171,8 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result { match opts.backend { Backend::Tree => { - let host = HostContext::for_run(Some(path), opts.host_caps(), opts.argv.clone()); + let mut host = HostContext::for_run(Some(path), opts.host_caps(), opts.argv.clone()); + host.set_entry_source(Some(path), &source); let mut interp = Interpreter::new(Some(path), opts.trace_eval) .with_stdin(stdin_lines.clone()) .with_host(host); @@ -190,7 +191,8 @@ pub fn run_file(path: &Path, opts: &RunOptions) -> Result { if opts.dump_bytecode { print!("{}", program.disassemble()); } - let host = HostContext::for_run(Some(path), opts.host_caps(), opts.argv.clone()); + let mut host = HostContext::for_run(Some(path), opts.host_caps(), opts.argv.clone()); + host.set_entry_source(Some(path), &source); let mut vm = Vm::new(Some(path)) .with_stdin(stdin_lines) .with_trace(opts.trace_eval) @@ -225,6 +227,7 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result { match opts.backend { Backend::Tree => { let mut host = HostContext::for_capture(Some(path), opts.host_caps()); + host.set_entry_source(Some(path), &source); host.argv = opts.argv.clone(); if let Some(root) = &opts.fs_root { host.fs_root = Some(root.clone()); @@ -246,6 +249,7 @@ pub fn run_file_capture(path: &Path, opts: &RunOptions) -> Result { Backend::Bytecode => { let program = compile_module(Some(path), &module)?; let mut host = HostContext::for_capture(Some(path), opts.host_caps()); + host.set_entry_source(Some(path), &source); host.argv = opts.argv.clone(); if let Some(root) = &opts.fs_root { host.fs_root = Some(root.clone()); diff --git a/tests/ext/agent-assign.mq.md b/tests/ext/agent-assign.mq.md deleted file mode 100644 index 150fe49..0000000 --- a/tests/ext/agent-assign.mq.md +++ /dev/null @@ -1,35 +0,0 @@ ---- -title: ext/agent match and assign -description: Multi-column tables + skill match (needs MARQDO_AGENT_PLUGIN). -> ext/agent.mq.md ---- - -# main - -> load_native - -*`cwd` = > cwd * -*`bot` = > agent root=`cwd` * - -`技能表` = -| 成员 | 技能 | 负载 | -|------|------|------| -| 张三 | Python, ML | 2 | -| 李四 | 前端, UX | 1 | -| 王五 | 后端, 运维 | 3 | -| 赵六 | 数据分析 | 0 | - -*`m` = > `bot`.match_skill skill=Python members=`技能表` * -*`name` = > get value=`m` key=成员 * -> print text=`name` - -*`m2` = > `bot`.match_skill skill=安全审计 members=`技能表` * -+ `m2` - > print text=unexpected-match -+ * - *`path` = > `bot`.create_ticket title=no_owner detail=security * - > print text=`path` - -*`m3` = > `bot`.update_load member=`m` delta=1 * -*`load` = > get value=`m3` key=负载 * -> print text=`load` diff --git a/tests/ext/agent-run-live.mq.md b/tests/ext/agent-run-live.mq.md new file mode 100644 index 0000000..ef648b6 --- /dev/null +++ b/tests/ext/agent-run-live.mq.md @@ -0,0 +1,40 @@ +--- +title: ext/agent run live (DeepSeek) +description: Real 执行 via tests/ext/.env (gitignored). Skipped in CI when .env missing. +> ext/ai/agent.mq.md +> lib/time.mq.md +--- + +## 获取时间 + +*`u` = > now_unix * +**> format unix=`u` pattern=%Y%m%d ** + +# main + +> load_env + +*`模型` = > llm * + +`工具表` = +| 工具 | +|------| +| 获取时间 | + +*`助手` = > agent model=`模型` tools=`工具表` user_prompt=你是测试助手。需要工具时,必须只回复一行 TOOL:函数名,不要其它文字。 * + +*`id` = > get value=`助手` key=id * + +*`回复` = > `助手`.run extra=请调用获取时间工具,并把当前日期告诉用户。 * + +> print text=`回复` + +*`历史1` = > host_agent_history_get id=`id` * +*`条数1` = > len value=`历史1` * +> print text=`条数1` + +> `助手`.clear_history + +*`历史2` = > host_agent_history_get id=`id` * +*`条数2` = > len value=`历史2` * +> print text=`条数2` diff --git a/tests/ext/agent-scaffold.mq.md b/tests/ext/agent-scaffold.mq.md deleted file mode 100644 index e0925ac..0000000 --- a/tests/ext/agent-scaffold.mq.md +++ /dev/null @@ -1,30 +0,0 @@ ---- -title: ext/agent layout scaffold -description: Requires MARQDO_AGENT_PLUGIN (set by gold harness). -> ext/agent.mq.md -> lib/fs.mq.md ---- - -# main - -> load_native - -*`cwd` = > cwd * - -*`ws` = > agent root=`cwd` * - -*`_n` = > `ws`.ensure_layout * - -*`info` = > `ws`.probe * - -*`ok` = > get value=`info` key=has_agents * - -> print text=`ok` - -> write_text path=skel.mq.md text={{name}} - -*`_p` = > `ws`.scaffold name=demo template=skel.mq.md dest=out-demo.mq.md * - -*`body` = > read_text path=out-demo.mq.md * - -> print text=`body` diff --git a/tests/ext/agent-smoke.mq.md b/tests/ext/agent-smoke.mq.md new file mode 100644 index 0000000..77089a5 --- /dev/null +++ b/tests/ext/agent-smoke.mq.md @@ -0,0 +1,60 @@ +--- +title: ext/agent framework smoke (offline) +description: Ctor, context, clear_history — no network. Gold suite. +> ext/ai/agent.mq.md +--- + +# main + +`工具表` = +| 工具 | +|------| +| 获取时间 | + +*`占位模型` = > parse text={"_type":"llm"} * +*`助手` = > agent model=`占位模型` tools=`工具表` user_prompt=you are a dispatcher * + +*`id` = > get value=`助手` key=id * + +*`ctx` = > build_agent_context agent=`助手` extra=probe * + +*`has_skill` = > split value=`ctx` sep=Marqdo * +*`sk_n` = > len value=`has_skill` * ++ `sk_n` > 1 + > print text=skill-ok ++ * + > print text=skill-missing + +*`has_tools` = > split value=`ctx` sep=获取时间 * +*`tool_n` = > len value=`has_tools` * ++ `tool_n` > 1 + > print text=tools-ok ++ * + > print text=tools-missing + +*`src` = > host_module_source * +*`has_src` = > split value=`src` sep=probe * +*`src_n` = > len value=`has_src` * ++ `src_n` > 1 + > print text=source-ok ++ * + > print text=source-missing + +*`turn` = > parse text={"role":"user","content":"one"} * +> host_agent_history_append id=`id` item=`turn` +*`turn2` = > parse text={"role":"assistant","content":"two"} * +> host_agent_history_append id=`id` item=`turn2` + +*`h1` = > host_agent_history_get id=`id` * +*`n1` = > len value=`h1` * +> print text=`n1` + +> `助手`.clear_history + +*`h2` = > host_agent_history_get id=`id` * +*`n2` = > len value=`h2` * +> print text=`n2` + +*`site` = > host_call_site * +*`fn` = > get value=`site` key=function * +> print text=`fn` diff --git a/tests/ext/llm-complete.mq.md b/tests/ext/llm-complete.mq.md index 83a8a87..c96fb02 100644 --- a/tests/ext/llm-complete.mq.md +++ b/tests/ext/llm-complete.mq.md @@ -1,7 +1,7 @@ --- title: ext llm DeepSeek complete (live) description: Live OpenAI-compatible chat via DeepSeek. Needs tests/ext/.env (gitignored). Not in gold suite. -> ext/llm.mq.md +> ext/ai/llm.mq.md --- # main diff --git a/tests/ext/llm-import.mq.md b/tests/ext/llm-import.mq.md index 5223e24..0a47311 100644 --- a/tests/ext/llm-import.mq.md +++ b/tests/ext/llm-import.mq.md @@ -1,6 +1,6 @@ --- title: ext llm import -> ext/llm.mq.md +> ext/ai/llm.mq.md --- # main diff --git a/tests/gold.rs b/tests/gold.rs index f7ce569..1eba0fc 100644 --- a/tests/gold.rs +++ b/tests/gold.rs @@ -642,7 +642,7 @@ fn lib_plugin_demo() { fn ext_cli_add_list_remove_llm() { let tmp = tempfile_dir("marqdo-ext-cli"); let src = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ext"); - assert!(src.join("llm.mq.md").is_file()); + assert!(src.join("ai").join("llm.mq.md").is_file()); let bin = env!("CARGO_BIN_EXE_marqdo"); let status = Command::new(bin) @@ -654,8 +654,8 @@ fn ext_cli_add_list_remove_llm() { .status() .expect("ext add"); assert!(status.success(), "ext add llm failed"); - assert!(tmp.join("llm.mq.md").is_file()); - assert!(tmp.join("大模型.mq.md").is_file()); + assert!(tmp.join("ai").join("llm.mq.md").is_file()); + assert!(tmp.join("ai").join("大模型.mq.md").is_file()); let out = Command::new(bin) .args(["ext", "list"]) @@ -669,7 +669,7 @@ fn ext_cli_add_list_remove_llm() { let mq = tmp.join("smoke.mq.md"); std::fs::write( &mq, - "---\n> ext/llm.mq.md\n---\n\n# main\n\n> print text=ext-cli-ok\n", + "---\n> ext/ai/llm.mq.md\n---\n\n# main\n\n> print text=ext-cli-ok\n", ) .unwrap(); let run = Command::new(bin) @@ -694,17 +694,11 @@ fn ext_cli_add_list_remove_llm() { .status() .expect("ext remove"); assert!(status.success()); - assert!(!tmp.join("llm.mq.md").is_file()); + assert!(!tmp.join("ai").join("llm.mq.md").is_file()); } #[test] -fn ext_cli_add_agent_with_native() { - let status = Command::new(env!("CARGO")) - .args(["build", "-p", "marqdo_plugin_agent"]) - .status() - .expect("build agent plugin"); - assert!(status.success()); - +fn ext_cli_add_agent() { let tmp = tempfile_dir("marqdo-ext-agent"); let src = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("ext"); let bin = env!("CARGO_BIN_EXE_marqdo"); @@ -716,99 +710,52 @@ fn ext_cli_add_agent_with_native() { .status() .expect("ext add agent"); assert!(status.success(), "ext add agent failed"); - assert!(tmp.join("agent.mq.md").is_file()); - let lib_name = if cfg!(windows) { - "agent.dll" - } else if cfg!(target_os = "macos") { - "libagent.dylib" - } else { - "libagent.so" - }; - assert!(tmp.join("native").join(lib_name).is_file()); - assert!(tmp.join("agent.plugin").is_file()); + assert!(tmp.join("ai").join("agent.mq.md").is_file()); + assert!(tmp.join("ai").join("智能体.mq.md").is_file()); } #[test] -fn ext_agent_assign() { - let status = Command::new(env!("CARGO")) - .args(["build", "-p", "marqdo_plugin_agent"]) - .status() - .expect("cargo build plugin agent"); - assert!(status.success(), "failed to build marqdo_plugin_agent"); - - let lib_name = if cfg!(windows) { - "agent.dll" - } else if cfg!(target_os = "macos") { - "libagent.dylib" - } else { - "libagent.so" - }; - let built = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("target") - .join("debug") - .join(lib_name); - let ext_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("tests") - .join("ext"); - let plugin = ext_dir.join(lib_name); - std::fs::copy(&built, &plugin).expect("copy agent plugin"); - let _ = std::fs::remove_file(ext_dir.join("ticket.txt")); - let _ = std::fs::remove_file(ext_dir.join("ticket-stub.txt")); - +fn ext_agent_framework_smoke() { let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) - .args(["run", "tests/ext/agent-assign.mq.md"]) - .env("MARQDO_AGENT_PLUGIN", lib_name) + .args(["run", "tests/ext/agent-smoke.mq.md"]) .output() - .expect("run agent-assign"); + .expect("run agent-smoke"); let code = output.status.code().unwrap_or(1); let stdout = String::from_utf8_lossy(&output.stdout); let stderr = String::from_utf8_lossy(&output.stderr); - assert_eq!(code, 0, "agent-assign stderr={stderr}"); - assert_eq!(stdout.trim_end(), "张三\nticket.txt\n3", "agent-assign"); -} - -#[test] -fn ext_agent_scaffold() { - let status = Command::new(env!("CARGO")) - .args(["build", "-p", "marqdo_plugin_agent"]) - .status() - .expect("cargo build plugin agent"); - assert!(status.success(), "failed to build marqdo_plugin_agent"); - - let lib_name = if cfg!(windows) { - "agent.dll" - } else if cfg!(target_os = "macos") { - "libagent.dylib" - } else { - "libagent.so" - }; - let built = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("target") - .join("debug") - .join(lib_name); - assert!( - built.is_file(), - "missing plugin artifact {}", - built.display() + assert_eq!(code, 0, "agent-smoke stderr={stderr}"); + assert_eq!( + stdout.trim_end(), + "skill-ok\ntools-ok\nsource-ok\n2\n0\nmain", + "agent-smoke" ); - let ext_dir = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("tests") - .join("ext"); - let plugin = ext_dir.join(lib_name); - std::fs::copy(&built, &plugin).expect("copy agent plugin into tests/ext"); - let _ = std::fs::remove_file(ext_dir.join("skel.mq.md")); - let _ = std::fs::remove_file(ext_dir.join("out-demo.mq.md")); +} + +#[test] +fn ext_agent_run_live() { + let env_file = std::path::PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests/ext/.env"); + if !env_file.is_file() { + eprintln!("skip ext_agent_run_live: missing {}", env_file.display()); + return; + } let output = Command::new(env!("CARGO_BIN_EXE_marqdo")) - .args(["run", "tests/ext/agent-scaffold.mq.md"]) - .env("MARQDO_AGENT_PLUGIN", lib_name) + .args(["run", "tests/ext/agent-run-live.mq.md"]) .output() - .expect("failed to run marqdo"); + .expect("run agent-run-live"); let code = output.status.code().unwrap_or(1); let stdout = String::from_utf8_lossy(&output.stdout); let stderr = String::from_utf8_lossy(&output.stderr); - assert_eq!(code, 0, "agent-scaffold stderr={stderr}"); - assert_eq!(stdout.trim_end(), "True\ndemo", "agent-scaffold"); + assert_eq!(code, 0, "agent-run-live stderr={stderr}"); + let lines: Vec<&str> = stdout.trim_end().lines().collect(); + assert!(lines.len() >= 3, "agent-run-live stdout={stdout}"); + assert!( + lines[0].contains('-') && lines[0].chars().any(|c| c.is_ascii_digit()), + "expected time in reply, got {:?}", + lines[0] + ); + assert_eq!(lines[1], "2", "history after run"); + assert_eq!(lines[2], "0", "history after clear"); } #[test] diff --git a/tests/lib/dotenv-fixture.env b/tests/lib/dotenv-fixture.env new file mode 100644 index 0000000..cb33d85 --- /dev/null +++ b/tests/lib/dotenv-fixture.env @@ -0,0 +1 @@ +MARQDO_DOTENV_FIXTURE=loaded \ No newline at end of file