@ai-zen/agents-sdk 1.0.0 → 1.2.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/CHANGELOG.md +40 -0
  2. package/README.md +5 -17
  3. package/README.zh.md +5 -17
  4. package/dist/agent-plugins/AutoMigratePlugin.js +5 -0
  5. package/dist/agent-plugins/AutoRefreshToolsPlugin.d.ts +28 -1
  6. package/dist/agent-plugins/AutoRefreshToolsPlugin.js +29 -8
  7. package/dist/agent-plugins/SubAgentGuardInstallerPlugin.d.ts +64 -0
  8. package/dist/agent-plugins/SubAgentGuardInstallerPlugin.js +68 -0
  9. package/dist/agent-plugins/UnknownToolHintPlugin.d.ts +0 -1
  10. package/dist/agent-plugins/UnknownToolHintPlugin.js +0 -1
  11. package/dist/config/ConfigManager.d.ts +11 -7
  12. package/dist/config/ConfigManager.js +15 -13
  13. package/dist/config/constants.d.ts +5 -3
  14. package/dist/config/constants.js +10 -4
  15. package/dist/config/migrate.d.ts +15 -6
  16. package/dist/config/migrate.js +16 -12
  17. package/dist/index.d.ts +12 -4
  18. package/dist/index.js +9 -4
  19. package/dist/runtime/HandoffService.d.ts +51 -9
  20. package/dist/runtime/HandoffService.js +158 -97
  21. package/dist/runtime/SdkAgent.d.ts +11 -4
  22. package/dist/runtime/SdkAgent.js +10 -4
  23. package/dist/runtime/completeOnce.d.ts +43 -0
  24. package/dist/runtime/completeOnce.js +38 -0
  25. package/dist/runtime/createAgent.d.ts +27 -1
  26. package/dist/runtime/createAgent.js +37 -7
  27. package/dist/runtime/createModel.d.ts +13 -1
  28. package/dist/runtime/createModel.js +14 -1
  29. package/dist/runtime/resolveModel.d.ts +93 -0
  30. package/dist/runtime/resolveModel.js +78 -0
  31. package/dist/scope/Scope.d.ts +1 -2
  32. package/dist/scope/Scope.js +1 -1
  33. package/dist/scope/ScopePlugin.d.ts +1 -1
  34. package/dist/scope/plugins/allInOne.d.ts +1 -1
  35. package/dist/scope/plugins/allInOne.js +1 -1
  36. package/dist/scope/plugins/builtin/ExecTool.js +4 -0
  37. package/dist/scope/plugins/builtin/SdkCallbackTool.d.ts +1 -2
  38. package/dist/scope/plugins/builtin/SdkCallbackTool.js +0 -1
  39. package/dist/scope/plugins/builtin/SpawnTool.js +2 -2
  40. package/dist/scope/plugins/builtin/ViewImageTool.d.ts +1 -1
  41. package/dist/scope/plugins/builtin/ViewImageTool.js +1 -1
  42. package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +1 -1
  43. package/dist/scope/plugins/mcp/McpScopePlugin.js +1 -1
  44. package/dist/scope/plugins/mcp/mcpTools.d.ts +1 -1
  45. package/dist/scope/plugins/mcp/mcpTools.js +4 -4
  46. package/dist/scope/plugins/skills/SkillsScopePlugin.d.ts +1 -1
  47. package/dist/scope/plugins/skills/SkillsScopePlugin.js +1 -1
  48. package/dist/scope/plugins/skills/discover.js +1 -1
  49. package/dist/scope/plugins/usertools/discover.d.ts +24 -0
  50. package/dist/scope/plugins/usertools/discover.js +17 -5
  51. package/dist/shared/content-blocks.d.ts +6 -9
  52. package/dist/shared/content-blocks.js +38 -58
  53. package/dist/types/index.d.ts +76 -30
  54. package/dist/types/index.js +0 -4
  55. package/package.json +5 -5
package/CHANGELOG.md CHANGED
@@ -1,3 +1,43 @@
1
+ ## [1.1.0] - 2026-11-09
2
+
3
+ > Cancelling a handoff now means "go back to how it was". Before this release `Ctrl+C` only
4
+ > stopped the work: the handoff turn — a multi-KB skill instruction, its tool results and the
5
+ > acknowledgement — stayed in `agent.messages`, so the CLI showed it in the transcript, `↑`
6
+ > history offered it back as a draft, and the next request resent it as conversation history.
7
+ >
8
+ > Worse, on the auto-migration path the interrupt was not honoured at all: `AutoMigratePlugin`
9
+ > drives `migrate()` directly, so a caller-supplied `signal` never reached the service. The
10
+ > aborted `self` round was then read as "the model never wrote the document", burning one retry
11
+ > and one `summary` round — and then migrating anyway.
12
+ >
13
+ > Requires `@ai-zen/agents-core@^4.5.4`. Not a breaking change: the defaults of `handoff()` /
14
+ > `migrate()` are untouched and an external `signal` keeps working.
15
+
16
+ ### ✨ Cancellation belongs to the service
17
+
18
+ - **`HandoffService.abort()` (new public method)** — the service owns the cancel handle of the
19
+ run it is executing and merges it with any caller-supplied `signal` (`AbortSignal.any`), so
20
+ `/handoff`, `/migrate` **and auto-migration** share one switch. On an idle service it is a
21
+ no-op (`null` handle), which is what keeps a stale `abort()` from "succeeding" silently later.
22
+ - **Cancel means roll back** — `agent.messages` is truncated back to the length it had before
23
+ the handoff turn, a document created by this run is deleted, and `HandoffAbortedError` is
24
+ thrown. No retry, no degradation, no migration. The cleanup lives in one place so it also
25
+ covers the state that no per-stage `catch` used to see: document written, not yet validated.
26
+ - **Commit point** — once the document passes validation (`isHandoffFileReady`) the run is
27
+ committed: saving the old conversation, pruning history and persisting the new session no
28
+ longer react to cancellation. Stopping there would leave a third state ("user cancelled, old
29
+ conversation archived, history already pruned"), which is worse than finishing.
30
+ - **A cancelled auto-migration is no longer logged as a failure** — `AutoMigratePlugin`
31
+ swallows `HandoffAbortedError` instead of writing "migration failed" through the logger
32
+ (default `console.error`, i.e. a torn frame in a TUI).
33
+
34
+ ### 🧪 Tests
35
+
36
+ - 635 unit cases in 53 files — 7 new: service-side `abort()`, message rollback, document removal
37
+ after a late cancel, commit-point semantics, idle `abort()` as a no-op, a `summary` request
38
+ interrupted through the service handle, and the silent cancel in the plugin. `tsc --noEmit`
39
+ is clean for both the production and the test config.
40
+
1
41
  ## [1.0.0] - 2026-11-03
2
42
 
3
43
  > First stable release. The whole `1.0.0-alpha.x` line — up to and including `alpha.26` — is
package/README.md CHANGED
@@ -4,7 +4,7 @@ AI-Zen SDK — shared business logic layer providing a unified Agent runtime for
4
4
 
5
5
  ## Source of Truth
6
6
 
7
- **[`docs/sdk-design-v1.md`](./docs/sdk-design-v1.md)** is the single source of truth for this package's design. All implementations must stay consistent with the document. (Pre-refactor design is kept in [`docs/sdk-design-v0.md`](./docs/sdk-design-v0.md).)
7
+ **[`docs/sdk-design-v1.md`](./docs/sdk-design-v1.md)** is the single source of truth for this package's design. All implementations must stay consistent with the document.
8
8
 
9
9
  ## Architecture
10
10
 
@@ -23,7 +23,7 @@ config ← ConfigManager + constants: config.json read/write + directory
23
23
  crud ← capability-entity CRUD (Agent definitions; conversations/drafts are persisted by each consumer)
24
24
  runtime ← model factory + Agent assembly + task handoff/migration (cancellable via `AbortSignal`; createModel / createAgent / SdkAgent / HandoffService / HandoffGuardPlugin)
25
25
  scope ← Scope orchestration container + ScopePlugin contract + PermissionEvaluator / disclosure + plugins/ (5 self-contained capability sources: discovery + tools + plugin)
26
- agent-plugins ← Agent plugins (autoMigrate, autoRefreshTools, contextGuard, unknownToolHint)
26
+ agent-plugins ← Agent plugins (autoMigrate, autoRefreshTools, contextGuard, unknownToolHint, subAgentGuardInstaller)
27
27
  shared ← content blocks (file → content block transport), entity repository, logging, errors
28
28
  ```
29
29
 
@@ -34,7 +34,7 @@ Dependency direction: `agent-plugins → runtime → scope`; `runtime → scope
34
34
  | Entity | Description |
35
35
  |--------|-------------|
36
36
  | **Scope** | Orchestration container: global context (`config` / `cwd` / `agentsDir`) + the three-phase capability pipeline (discover → filter → instantiate) + plugin registry. Explicitly assembled via `scope.use(...)` |
37
- | **ScopePlugin** | Capability plugin (the v1 extension point): self-owns one capability source's discovery state, candidate names, and instantiation (`discover` / `candidates` / `instantiate` / `dispose`). 5 standard plugins ship out of the box, assembled via `allInOne()` |
37
+ | **ScopePlugin** | Capability plugin (the extension point): self-owns one capability source's discovery state, candidate names, and instantiation (`discover` / `candidates` / `instantiate` / `dispose`). 5 standard plugins ship out of the box, assembled via `allInOne()` |
38
38
  | **ToolEnv** | Tool environment `{ cwd, config }`; injected when the built-in-tools plugin (`BuiltinToolsScopePlugin`) instantiates tools, serving as the base for relative path resolution and config reads |
39
39
  | **SdkCallbackTool** | Abstract base for built-in tools: `env` constructor injection + subclass `call()` + `resolve()` relative path resolution |
40
40
  | **SdkAgent** | Extends the Core Agent, carries SDK metadata, supports `use()` plugin registration |
@@ -79,18 +79,6 @@ await agent.send("Hello");
79
79
  await scope.dispose(); // disconnect MCP, release plugin resources
80
80
  ```
81
81
 
82
- ## Development Status
83
-
84
- | Module | Status |
85
- |--------|--------|
86
- | `types` | ✅ Implemented — core entities, permission model, MCP types complete |
87
- | `config` | ✅ Implemented — ConfigManager + factory defaults + one-shot bootstrap |
88
- | `crud` | ✅ Implemented — capability-entity CRUD for Agents, etc. (conversations/drafts persisted by each consumer) |
89
- | `scope` | ✅ Implemented — Scope + ScopePlugin + 5 standard plugins (builtin / user-tools / skills / mcp / subagents) + allInOne |
90
- | `runtime` | ✅ Implemented — createAgent, MCP connection management, task migration |
91
- | `agent-plugins` | ✅ Implemented — AutoMigratePlugin / AutoRefreshToolsPlugin / ContextGuardPlugin / UnknownToolHintPlugin |
92
- | `shared` | ✅ Implemented — SdkError + injectable Logger |
93
-
94
82
  ## Built-in Tools
95
83
 
96
84
  All built-in tools are classes (extending `SdkCallbackTool`), instantiated by the built-in-tools plugin (`BuiltinToolsScopePlugin`) with a `ToolEnv` — one set of instances per Scope, with `cwd` injected and relative paths resolved against `Scope.cwd`, never depending on the global `process.cwd()`.
@@ -123,7 +111,7 @@ Conditionally injected based on the active model / config:
123
111
  | `generate_image` | Only when `defaultImageModel` is configured | Generate an image from a text description |
124
112
  | `view_image` | Only for vision models (the agent's `modelId` resolves to a model with `vision: true`) | View / analyze an image: local images are auto-uploaded via the Files API, network URLs are referenced directly |
125
113
 
126
- Tool output protection: `AppConfig.maxToolOutput` (in characters, default 32768) is the unified ceiling for tool output. Each tool handles overflow itself — `exec` dumps `stdout.log` / `stderr.log` separately (returning head/tail previews per stream), `find_text` / `glob` / `ls` dump `result.json` (returning a head preview), and `read` only warns and suggests batched reads via `range`. Dumps go to `<tmpdir>/ai-zen/tool-output/<tool>-<timestamp>-<random>/`, one directory per call, with no self-cleanup. See [`docs/sdk-design-v1.md` §12](docs/sdk-design-v1.md).
114
+ Tool output protection: `AppConfig.maxToolOutput` (in characters, default 32768) is the unified ceiling for tool output. Each tool handles overflow itself — `exec` dumps `stdout.log` / `stderr.log` separately (returning head/tail previews per stream), `find_text` / `glob` / `ls` dump `result.json` (returning a head preview), and `read` only warns and suggests batched reads via `range`. Dumps go to `<tmpdir>/ai-zen/tool-output/<tool>-<timestamp>-<random>/`, one directory per call, with no self-cleanup. See [`docs/sdk-design-v1.md`](docs/sdk-design-v1.md).
127
115
 
128
116
  Interrupt strategies live in the config: `AppConfig.abortedMessageStrategy` (`keep` / `drop`, factory `keep` — the model can continue the half-sentence after an interrupt) and `AppConfig.abortedToolRoundStrategy` (`fill` / `drop`, factory `fill` — whole rounds go in or out, because `tool_calls` must pair with results). `createAgent()` reads them from `scope.config` when no explicit option is passed, keeping a single source of truth.
129
117
 
@@ -147,7 +135,7 @@ This is a performance boundary, not a convenience choice — **SDK behaviour and
147
135
  - **Cache prefixes stop being comparable.** Prompt caching matches on the prefix; letting language into the SDK multiplies the prefix shapes of one code path and removes the single variable that benchmarks rely on.
148
136
  - **Diagnosis needs one shape.** Performance data, bug reproductions and tool-call success rates have to be comparable across users; once behaviour drifts with locale, every conclusion needs a "what language are you on?" qualifier first.
149
137
 
150
- Following the user's language is handled by one language-neutral instruction in the factory system prompt — `Always reply in the same language the user writes in.` — which adapts per **message** rather than per installation, and needs no locale plumbing in the SDK. Full rationale: [`docs/sdk-design-v1.md` §1](docs/sdk-design-v1.md).
138
+ Following the user's language is handled by one language-neutral instruction in the factory system prompt — `Always reply in the same language the user writes in.` — which adapts per **message** rather than per installation, and needs no locale plumbing in the SDK. Full rationale: [`docs/sdk-design-v1.md`](docs/sdk-design-v1.md).
151
139
 
152
140
  ## Design Principles
153
141
 
package/README.zh.md CHANGED
@@ -4,7 +4,7 @@ AI-Zen SDK — 共享业务逻辑层,为 CLI 和 Desktop 提供统一的 Agent
4
4
 
5
5
  ## 真相源
6
6
 
7
- **[`docs/sdk-design-v1.md`](./docs/sdk-design-v1.md)** 是本包的唯一设计真相源。所有实现必须与文档一致。(重构前的设计保留在 [`docs/sdk-design-v0.md`](./docs/sdk-design-v0.md)。)
7
+ **[`docs/sdk-design-v1.md`](./docs/sdk-design-v1.md)** 是本包的唯一设计真相源。所有实现必须与文档一致。
8
8
  ## 架构
9
9
 
10
10
  ```
@@ -22,7 +22,7 @@ config ← ConfigManager + constants:读写 config.json + 目录初始
22
22
  crud ← 能力实体 CRUD(Agent 定义等;会话/草稿已下放给各端自行持久化)
23
23
  runtime ← 模型工厂 + Agent 组装 + 任务交接/迁移(可 `AbortSignal` 取消;createModel / createAgent / SdkAgent / HandoffService / HandoffGuardPlugin)
24
24
  scope ← Scope 编排容器 + ScopePlugin 契约 + PermissionEvaluator / disclosure + plugins/(5 个自包含能力来源:发现 + 工具 + 插件)
25
- agent-plugins ← Agent 插件(autoMigrate、autoRefreshTools、contextGuard、unknownToolHint)
25
+ agent-plugins ← Agent 插件(autoMigrate、autoRefreshTools、contextGuard、unknownToolHint、subAgentGuardInstaller)
26
26
  shared ← 内容块(文件 → 内容块的传输决策)、实体仓储、日志、错误
27
27
  ```
28
28
 
@@ -33,7 +33,7 @@ shared ← 内容块(文件 → 内容块的传输决策)、实体仓
33
33
  | 实体 | 说明 |
34
34
  |------|------|
35
35
  | **Scope** | 编排容器:全局上下文(`config` / `cwd` / `agentsDir`)+ 三阶段能力管线(发现 → 过滤 → 实例化)+ 插件注册表。经 `scope.use(...)` 显式装配 |
36
- | **ScopePlugin** | 能力插件(v1 扩展点):自持一类能力的发现状态、候选名与实例化(`discover` / `candidates` / `instantiate` / `dispose`)。内置 5 个标准插件,经 `allInOne()` 一键装配 |
36
+ | **ScopePlugin** | 能力插件(扩展点):自持一类能力的发现状态、候选名与实例化(`discover` / `candidates` / `instantiate` / `dispose`)。内置 5 个标准插件,经 `allInOne()` 一键装配 |
37
37
  | **ToolEnv** | 工具环境 `{ cwd, config }`,内置工具插件(`BuiltinToolsScopePlugin`)实例化工具时注入,作为相对路径解析与配置读取的基准 |
38
38
  | **SdkCallbackTool** | 内置工具抽象基类:`env` 构造注入 + 子类实现 `call()` + `resolve()` 相对路径解析 |
39
39
  | **SdkAgent** | 继承 Core Agent,携带 SDK 元数据,支持 `use()` 插件注册 |
@@ -78,18 +78,6 @@ await agent.send("你好");
78
78
  await scope.dispose(); // 断开 MCP、释放插件资源
79
79
  ```
80
80
 
81
- ## 开发状态
82
-
83
- | 模块 | 状态 |
84
- |------|------|
85
- | `types` | ✅ 已实现 — 核心实体、权限模型、MCP 类型完整 |
86
- | `config` | ✅ 已实现 — ConfigManager + 出厂默认配置 + 一键 bootstrap |
87
- | `crud` | ✅ 已实现 — Agent 等能力实体 CRUD(会话/草稿由各端自行持久化) |
88
- | `scope` | ✅ 已实现 — Scope + ScopePlugin + 5 个标准插件(builtin / user-tools / skills / mcp / subagents)+ allInOne |
89
- | `runtime` | ✅ 已实现 — createAgent、MCP 连接管理、任务迁移 |
90
- | `agent-plugins` | ✅ 已实现 — AutoMigratePlugin / AutoRefreshToolsPlugin / ContextGuardPlugin / UnknownToolHintPlugin |
91
- | `shared` | ✅ 已实现 — 内容块传输决策(`content-blocks`)+ 实体仓储 + SdkError + 可注入 Logger |
92
-
93
81
  ## 内置工具
94
82
 
95
83
  内置工具全部类化(继承 `SdkCallbackTool`),由内置工具插件(`BuiltinToolsScopePlugin`)用 `ToolEnv` 实例化——每个 Scope 一套实例,`cwd` 注入,相对路径以 `Scope.cwd` 为基准,不依赖全局 `process.cwd()`。
@@ -122,7 +110,7 @@ await scope.dispose(); // 断开 MCP、释放插件资源
122
110
  | `generate_image` | 配置了 `defaultImageModel` 才注册 | 根据文字描述生成图片 |
123
111
  | `view_image` | 仅视觉模型可用(Agent 的 `modelId` 解析为 `vision: true` 的模型) | 查看/分析图片:本地图片自动经 Files API 上传,网络 URL 直接引用 |
124
112
 
125
- 工具输出保护:`AppConfig.maxToolOutput`(字符数,缺省 32768)是工具输出的统一上限,超限时由各工具自行处置——`exec` 分文件落盘 `stdout.log` / `stderr.log`(返回各流头尾预览),`find_text` / `glob` / `ls` 落盘 `result.json`(返回头部预览),`read` 仅警告并提示用 `range` 分批读取。落盘目录为 `<tmpdir>/ai-zen/tool-output/<工具名>-<时间戳>-<随机串>/`,每次调用独立、不自清理。详见 [`docs/sdk-design-v1.md` §12](docs/sdk-design-v1.md)。
113
+ 工具输出保护:`AppConfig.maxToolOutput`(字符数,缺省 32768)是工具输出的统一上限,超限时由各工具自行处置——`exec` 分文件落盘 `stdout.log` / `stderr.log`(返回各流头尾预览),`find_text` / `glob` / `ls` 落盘 `result.json`(返回头部预览),`read` 仅警告并提示用 `range` 分批读取。落盘目录为 `<tmpdir>/ai-zen/tool-output/<工具名>-<时间戳>-<随机串>/`,每次调用独立、不自清理。详见 [`docs/sdk-design-v1.md`](docs/sdk-design-v1.md)。
126
114
 
127
115
  中断策略进配置:`AppConfig.abortedMessageStrategy`(`keep` / `drop`,出厂 `keep`——中断后模型能接着半截往下说)与 `AppConfig.abortedToolRoundStrategy`(`fill` / `drop`,出厂 `fill`——整轮进出,`tool_calls` 必须与结果配对)。`createAgent()` 不传参数时直接取 `scope.config`,于是策略只有一个真相源。
128
116
 
@@ -146,7 +134,7 @@ SDK 是 **English-only**:所有面向模型与面向用户的文案(工具 s
146
134
  - **缓存前缀不再可比。** prompt 缓存按前缀匹配;语言一旦进入 SDK,同一条代码路径就有了多个前缀形态,基准测试失去单一变量。
147
135
  - **基准与排查需要单一形态。** 性能数据、bug 复现、工具调用成功率都要能横向比较;行为若随 locale 漂移,任何结论都得先问「你是什么语言」。
148
136
 
149
- 「模型跟随用户语言」由一条语言中立的 system prompt 指令承担 —— `Always reply in the same language the user writes in.` —— 按**每条消息**自适应,比按安装实例绑定 locale 更准确,也不需要 SDK 参与。完整论述见 [`docs/sdk-design-v1.md` §1](docs/sdk-design-v1.md)。
137
+ 「模型跟随用户语言」由一条语言中立的 system prompt 指令承担 —— `Always reply in the same language the user writes in.` —— 按**每条消息**自适应,比按安装实例绑定 locale 更准确,也不需要 SDK 参与。完整论述见 [`docs/sdk-design-v1.md`](docs/sdk-design-v1.md)。
150
138
 
151
139
  ## 设计原则
152
140
 
@@ -1,3 +1,4 @@
1
+ import { HandoffAbortedError } from "../runtime/HandoffService.js";
1
2
  import { getLogger } from "../shared/logger.js";
2
3
  /**
3
4
  * 自动迁移插件 —— 只负责「何时触发」,实际迁移统一委托给持有的 `HandoffService`。
@@ -41,6 +42,10 @@ export class AutoMigratePlugin {
41
42
  await service.migrate({ agent, promptTokens, handoffTokens, mode: mode ?? undefined });
42
43
  }
43
44
  catch (err) {
45
+ // 用户主动取消不是失败:服务已把会话退回交接前,这里再报错只会在界面上
46
+ // 凭空多一条「迁移失败」(默认 logger 是 console.error,TUI 下直接撕画面)
47
+ if (err instanceof HandoffAbortedError)
48
+ return;
44
49
  getLogger().error(`[autoMigrate] migration failed: ${err?.message ?? err}`);
45
50
  }
46
51
  }
@@ -1,12 +1,39 @@
1
1
  import type { AgentPlugin, SendContext } from "@ai-zen/agents-core";
2
+ /**
3
+ * 自动刷新能力的时机。
4
+ *
5
+ * 三个档位是**开销与时效**的取舍,没有对错:
6
+ *
7
+ * | 档位 | 钩子 | 会话中新装的 MCP / skill / 工具什么时候能被模型看见 |
8
+ * |------|------|---------------------------------------------|
9
+ * | `inner-loop-start` | `onInnerLoopStart`(每次请求发出前) | 同一轮里下一次请求就看见(含工具回合的空档) |
10
+ * | `outer-loop-start` | `onBeforeSend`(整个 `send()` 开头) | 下一轮对话才看见;一轮只刷一次,开销最小 |
11
+ * | `off` | 不装插件 | 只靠 `/reload` |
12
+ *
13
+ * 为什么不是「每轮固定刷一次」:一次 `refresh()` 会重扫 MCP 配置、按需建连接,
14
+ * 长工具链的一轮里可能要跑好几次。默认 `inner-loop-start` 是时效优先的取舍,
15
+ * 嫌开销大就换成 `outer-loop-start`。
16
+ */
17
+ export type AutoRefreshWhen = "inner-loop-start" | "outer-loop-start";
2
18
  /**
3
19
  * 自动刷新能力插件。
4
20
  *
5
21
  * ```ts
6
- * agent.use(new AutoRefreshToolsPlugin());
22
+ * agent.use(new AutoRefreshToolsPlugin()); // 默认按内循环刷
23
+ * agent.use(new AutoRefreshToolsPlugin({ when: "outer-loop-start" }));
7
24
  * await agent.init();
8
25
  * ```
26
+ *
27
+ * 两件事必须成对做:先 `scope.refresh()` 重新发现,再按**新的**发现结果重建
28
+ * `agent.tools` —— 只刷前者,模型看到的还是旧工具集。
9
29
  */
10
30
  export declare class AutoRefreshToolsPlugin implements AgentPlugin {
31
+ private readonly when;
32
+ constructor(options?: {
33
+ when?: AutoRefreshWhen;
34
+ });
35
+ /** 内循环开始(每次请求前):能力变化在同一轮里就能生效 */
36
+ onInnerLoopStart(ctx: SendContext): Promise<void>;
37
+ /** 外循环开始(整个 `send()` 一次):最省,代价是要等到下一轮 */
11
38
  onBeforeSend(ctx: SendContext): Promise<void>;
12
39
  }
@@ -2,18 +2,39 @@
2
2
  * 自动刷新能力插件。
3
3
  *
4
4
  * ```ts
5
- * agent.use(new AutoRefreshToolsPlugin());
5
+ * agent.use(new AutoRefreshToolsPlugin()); // 默认按内循环刷
6
+ * agent.use(new AutoRefreshToolsPlugin({ when: "outer-loop-start" }));
6
7
  * await agent.init();
7
8
  * ```
9
+ *
10
+ * 两件事必须成对做:先 `scope.refresh()` 重新发现,再按**新的**发现结果重建
11
+ * `agent.tools` —— 只刷前者,模型看到的还是旧工具集。
8
12
  */
9
13
  export class AutoRefreshToolsPlugin {
14
+ when;
15
+ constructor(options = {}) {
16
+ this.when = options.when ?? "inner-loop-start";
17
+ }
18
+ /** 内循环开始(每次请求前):能力变化在同一轮里就能生效 */
19
+ async onInnerLoopStart(ctx) {
20
+ if (this.when !== "inner-loop-start")
21
+ return;
22
+ await refreshTools(ctx);
23
+ }
24
+ /** 外循环开始(整个 `send()` 一次):最省,代价是要等到下一轮 */
10
25
  async onBeforeSend(ctx) {
11
- const agent = ctx.agent;
12
- await agent.scope.refresh({ silent: true });
13
- agent.tools = agent.scope.buildTools(agent.definition, {
14
- exclude: {
15
- subagents: agent.definition.function?.name ? [agent.definition.function.name] : undefined,
16
- },
17
- });
26
+ if (this.when !== "outer-loop-start")
27
+ return;
28
+ await refreshTools(ctx);
18
29
  }
19
30
  }
31
+ /** 重新发现 + 按新结果重建工具集(两个档位共用同一段,免得两处漂) */
32
+ async function refreshTools(ctx) {
33
+ const agent = ctx.agent;
34
+ await agent.scope.refresh({ silent: true });
35
+ agent.tools = agent.scope.buildTools(agent.definition, {
36
+ exclude: {
37
+ subagents: agent.definition.function?.name ? [agent.definition.function.name] : undefined,
38
+ },
39
+ });
40
+ }
@@ -0,0 +1,64 @@
1
+ /**
2
+ * 子 Agent 护栏安装器 — SubAgentGuardInstallerPlugin
3
+ *
4
+ * 为什么在 SDK 而不在 CLI:子 Agent 内循环缺护栏是**所有宿主**共有的问题,不是 CLI 特性。
5
+ * 会话执行搬到服务端之后,服务端装配插件的那一处(`installPlugins`)装不到 CLI 仓里的实现,
6
+ * 远程会话的子 Agent 就会裸奔;把它放进 SDK,CLI 与服务端才能装同一份护栏、
7
+ * 共享同一套阈值语义。
8
+ *
9
+ * 背景:SDK 的子 Agent 由 `AgentTool` / `AgentToolLazy` 在委派时 `new Agent({...})` 构建,
10
+ * 新建实例不携带任何插件(`Agent._plugins` 每实例私有,不向子 Agent 传播),且子 Agent 走
11
+ * `run()` 而非 `send()`(`onBeforeSend` / `onAfterSend` 不会触发)。因此主 Agent 上的
12
+ * `ContextGuardPlugin` 对子 Agent 完全无效:子 Agent 的内循环没有上下文 token 护栏。
13
+ *
14
+ * 介入点:core 的 `onSubAgentStart` 钩子在「子 Agent 已构建、尚未 run()」时由宿主 Agent
15
+ * 分发,载荷携带子 Agent 实例。`use()` 只是把插件推入 `_plugins`,而钩子分发每次都实时遍历
16
+ * 该列表,故在钩子内安插对**本次 run 立即生效**(无需 init;AgentTool 也不会调用子 Agent 的
17
+ * init)。
18
+ *
19
+ * 约束口径(与主 Agent 同款,一项不落):把主 Agent 同款的 `ContextGuardPlugin` 装到子 Agent 上
20
+ * —— 同一个 `guardTokens`(当前模型的 `tokenThresholds.guard`),使「请求前用量超过硬线即中断」
21
+ * 对子 Agent 同样成立;同一个 `isSuspended`(会话层的交接/迁移让行判断),使「交接回合让行」
22
+ * 对子 Agent 同样成立。
23
+ *
24
+ * 为什么让行判断也必须透传:交接回合本身就是上下文最满的时刻 —— 主 Agent 那颗护栏靠
25
+ * `isSuspended` 让行才不至于「交接触发拦截 → 拦截又触发交接」地打转。子 Agent 若只带阈值、
26
+ * 不带让行,就会在交接中派出去的子任务上照拦,护栏比主 Agent 还严一档;递归安插时更是层层
27
+ * 都要带上,否则孙级又会把让行丢掉(同一个缺口往下挪一层而已)。
28
+ * **不设**轮次上限,**不设**工具调用次数上限。
29
+ *
30
+ * 中断语义:抛错即结束子 Agent 的 run;异常经 `AgentTool` 的 finally(分发 onSubAgentEnd)
31
+ * 上抛至父 Agent,因父 Agent 默认 `allowJsonParseError = true` 而降级为一条工具结果文本
32
+ * (`执行工具 X 时出错: …`),父 Agent 继续下一轮——即「软中断」:停掉越界的子 Agent,但不
33
+ * 终止用户对话(如需硬停,父 Agent 须设 allowJsonParseError=false)。
34
+ *
35
+ * 覆盖范围:core 只有两条委派路径(`AgentTool` / `AgentToolLazy`),均分发 onSubAgentStart,
36
+ * 故本插件对**全部**子 Agent 生效。SDK 的 `call_skill_sub_agent`(技能子 Agent)同样由
37
+ * `AgentToolLazy` 承载、走同一委派边界,因此也在覆盖范围内。
38
+ */
39
+ import type { AgentPlugin, SubAgentContext } from "@ai-zen/agents-core";
40
+ export interface SubAgentGuardOptions {
41
+ /** 强行阻断阈值;与主 Agent 同源(当前模型的 `tokenThresholds.guard`) */
42
+ guardTokens: number;
43
+ /**
44
+ * 让行判断(与主 Agent 的 `ContextGuardPlugin` 同一个来源)。
45
+ * 交接回合本身就是上下文最满的时刻,硬线必须让行 —— 否则「交接触发拦截 → 拦截
46
+ * 又触发交接」形成死循环。子 Agent 也必须遵守同一判断,否则交接中派出去的子任务
47
+ * 会被自己的硬线打断。
48
+ */
49
+ isSuspended?: () => boolean;
50
+ }
51
+ /**
52
+ * 子 Agent 护栏安装器(装在**宿主 Agent** 上)。
53
+ *
54
+ * 每次委派边界把 `ContextGuardPlugin` 装到子 Agent 身上(阈值与让行判断都与主 Agent 相同),
55
+ * 并递归安插自身,使子 Agent 再委派(孙级)时同样受约束。始终返回 undefined,永不拒绝委派
56
+ * ——是否中断由 `ContextGuardPlugin` 按阈值决定。
57
+ */
58
+ export declare class SubAgentGuardInstallerPlugin implements AgentPlugin {
59
+ private readonly guardTokens;
60
+ /** 让行判断按**引用**存下:它读的是「此刻是否在交接」,不是装配那一刻的快照 */
61
+ private readonly isSuspended?;
62
+ constructor(options: SubAgentGuardOptions);
63
+ onSubAgentStart({ subAgent }: SubAgentContext): void;
64
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * 子 Agent 护栏安装器 — SubAgentGuardInstallerPlugin
3
+ *
4
+ * 为什么在 SDK 而不在 CLI:子 Agent 内循环缺护栏是**所有宿主**共有的问题,不是 CLI 特性。
5
+ * 会话执行搬到服务端之后,服务端装配插件的那一处(`installPlugins`)装不到 CLI 仓里的实现,
6
+ * 远程会话的子 Agent 就会裸奔;把它放进 SDK,CLI 与服务端才能装同一份护栏、
7
+ * 共享同一套阈值语义。
8
+ *
9
+ * 背景:SDK 的子 Agent 由 `AgentTool` / `AgentToolLazy` 在委派时 `new Agent({...})` 构建,
10
+ * 新建实例不携带任何插件(`Agent._plugins` 每实例私有,不向子 Agent 传播),且子 Agent 走
11
+ * `run()` 而非 `send()`(`onBeforeSend` / `onAfterSend` 不会触发)。因此主 Agent 上的
12
+ * `ContextGuardPlugin` 对子 Agent 完全无效:子 Agent 的内循环没有上下文 token 护栏。
13
+ *
14
+ * 介入点:core 的 `onSubAgentStart` 钩子在「子 Agent 已构建、尚未 run()」时由宿主 Agent
15
+ * 分发,载荷携带子 Agent 实例。`use()` 只是把插件推入 `_plugins`,而钩子分发每次都实时遍历
16
+ * 该列表,故在钩子内安插对**本次 run 立即生效**(无需 init;AgentTool 也不会调用子 Agent 的
17
+ * init)。
18
+ *
19
+ * 约束口径(与主 Agent 同款,一项不落):把主 Agent 同款的 `ContextGuardPlugin` 装到子 Agent 上
20
+ * —— 同一个 `guardTokens`(当前模型的 `tokenThresholds.guard`),使「请求前用量超过硬线即中断」
21
+ * 对子 Agent 同样成立;同一个 `isSuspended`(会话层的交接/迁移让行判断),使「交接回合让行」
22
+ * 对子 Agent 同样成立。
23
+ *
24
+ * 为什么让行判断也必须透传:交接回合本身就是上下文最满的时刻 —— 主 Agent 那颗护栏靠
25
+ * `isSuspended` 让行才不至于「交接触发拦截 → 拦截又触发交接」地打转。子 Agent 若只带阈值、
26
+ * 不带让行,就会在交接中派出去的子任务上照拦,护栏比主 Agent 还严一档;递归安插时更是层层
27
+ * 都要带上,否则孙级又会把让行丢掉(同一个缺口往下挪一层而已)。
28
+ * **不设**轮次上限,**不设**工具调用次数上限。
29
+ *
30
+ * 中断语义:抛错即结束子 Agent 的 run;异常经 `AgentTool` 的 finally(分发 onSubAgentEnd)
31
+ * 上抛至父 Agent,因父 Agent 默认 `allowJsonParseError = true` 而降级为一条工具结果文本
32
+ * (`执行工具 X 时出错: …`),父 Agent 继续下一轮——即「软中断」:停掉越界的子 Agent,但不
33
+ * 终止用户对话(如需硬停,父 Agent 须设 allowJsonParseError=false)。
34
+ *
35
+ * 覆盖范围:core 只有两条委派路径(`AgentTool` / `AgentToolLazy`),均分发 onSubAgentStart,
36
+ * 故本插件对**全部**子 Agent 生效。SDK 的 `call_skill_sub_agent`(技能子 Agent)同样由
37
+ * `AgentToolLazy` 承载、走同一委派边界,因此也在覆盖范围内。
38
+ */
39
+ import { ContextGuardPlugin } from "./ContextGuardPlugin.js";
40
+ /**
41
+ * 子 Agent 护栏安装器(装在**宿主 Agent** 上)。
42
+ *
43
+ * 每次委派边界把 `ContextGuardPlugin` 装到子 Agent 身上(阈值与让行判断都与主 Agent 相同),
44
+ * 并递归安插自身,使子 Agent 再委派(孙级)时同样受约束。始终返回 undefined,永不拒绝委派
45
+ * ——是否中断由 `ContextGuardPlugin` 按阈值决定。
46
+ */
47
+ export class SubAgentGuardInstallerPlugin {
48
+ guardTokens;
49
+ /** 让行判断按**引用**存下:它读的是「此刻是否在交接」,不是装配那一刻的快照 */
50
+ isSuspended;
51
+ constructor(options) {
52
+ this.guardTokens = options.guardTokens;
53
+ this.isSuspended = options.isSuspended;
54
+ }
55
+ onSubAgentStart({ subAgent }) {
56
+ // 与主 Agent 完全同款:同一个 guardTokens 绝对阈值 + 同一个让行判断
57
+ subAgent.use(new ContextGuardPlugin({
58
+ guardTokens: this.guardTokens,
59
+ isSuspended: this.isSuspended,
60
+ }));
61
+ // 传染到孙级:子 Agent 若再委派,其子 Agent 同样被安插。
62
+ // 让行判断也必须跟着递归走,否则只差一层就把「交接中让行」丢了。
63
+ subAgent.use(new SubAgentGuardInstallerPlugin({
64
+ guardTokens: this.guardTokens,
65
+ isSuspended: this.isSuspended,
66
+ }));
67
+ }
68
+ }
@@ -14,7 +14,6 @@ import type { Scope } from "../scope/Scope.js";
14
14
  * 是否"有 MCP 配置"经 `scope.getPluginById(McpScopePlugin.ID)` 查询 MCP 插件获得。
15
15
  */
16
16
  export declare class UnknownToolHintPlugin implements AgentPlugin {
17
- /** 全局 Scope 实例 */
18
17
  private readonly scope;
19
18
  constructor(options: {
20
19
  scope: Scope;
@@ -13,7 +13,6 @@ import { McpScopePlugin } from "../scope/plugins/mcp/McpScopePlugin.js";
13
13
  * 是否"有 MCP 配置"经 `scope.getPluginById(McpScopePlugin.ID)` 查询 MCP 插件获得。
14
14
  */
15
15
  export class UnknownToolHintPlugin {
16
- /** 全局 Scope 实例 */
17
16
  scope;
18
17
  constructor(options) {
19
18
  this.scope = options.scope;
@@ -42,14 +42,16 @@ export declare class ConfigManager {
42
42
  * - agents/ 为空 → 写入默认 Agent(含 modelId)
43
43
  * - 已有其他 Agent → 返回 null
44
44
  *
45
- * 模型真相源只有 `AgentDefinition.modelId`:这里保证默认 Agent **一定有** modelId,
46
- * 运行期不再有任何 `config.defaultModel` 兜底。
45
+ * 模型按三级解析(`会话级覆盖 > AgentDefinition.modelId > AppConfig.defaultModel`)。
46
+ * 这里只负责把默认 Agent 的 `modelId` 补齐 —— 老配置里的 Agent 定义可能根本没写
47
+ * `modelId`(用户的兜底是 `config.defaultModel`),补齐时优先用它,升级才不会改变用户
48
+ * 实际在用的模型。
47
49
  *
48
- * @param legacyDefaultModel 老配置里的 `config.defaultModel`。仅在 Agent **既无 modelId**
49
- * 且该模型仍在模型清单里时用它补齐,让升级不改变用户实际在用的模型;之后
50
- * 这个字段就彻底不存在了。
50
+ * @param defaultModel 配置里的 `defaultModel`(三级解析的兜底那一级)。仅在默认 Agent
51
+ * **既无 modelId** 且该模型仍在模型清单里时用它补齐,让升级不改变用户实际在用的模型。
52
+ * 注意它不是「永远生效的兜底」:Agent 自己绑了模型就轮不到它(见 `resolveModel`)。
51
53
  */
52
- ensureDefaultAgent(legacyDefaultModel?: string): Promise<AgentDefinition | null>;
54
+ ensureDefaultAgent(defaultModel?: string): Promise<AgentDefinition | null>;
53
55
  /**
54
56
  * 确保 basePath/sub-agents/{DEFAULT_SUBAGENT_ID}.json 存在。
55
57
  *
@@ -131,7 +133,9 @@ export declare class ConfigManager {
131
133
  * - 默认模型 ID(defaultImageModel / defaultMigrationModel)
132
134
  * 在合并后若已不存在 → 回退到出厂默认,避免悬空引用。
133
135
  *
134
- * 注意 `defaultModel` 已不再存在:模型真相源是 `AgentDefinition.modelId`。
136
+ * `defaultModel`(三级解析的全局兜底)**不在这里校验、也不在这里改写**:它指向一个已被删掉
137
+ * 的模型时,`createAgent` 会在真要跑的时候报 `Model "x" not found` —— 那时用户看得见自己
138
+ * 刚删了什么;而在这里默默换成别的模型,只会让「我明明没改它」变成一宗悬案。
135
139
  *
136
140
  * 端点(endpoints)不参与托管,始终保持用户配置。
137
141
  */
@@ -98,18 +98,20 @@ export class ConfigManager {
98
98
  * - agents/ 为空 → 写入默认 Agent(含 modelId)
99
99
  * - 已有其他 Agent → 返回 null
100
100
  *
101
- * 模型真相源只有 `AgentDefinition.modelId`:这里保证默认 Agent **一定有** modelId,
102
- * 运行期不再有任何 `config.defaultModel` 兜底。
101
+ * 模型按三级解析(`会话级覆盖 > AgentDefinition.modelId > AppConfig.defaultModel`)。
102
+ * 这里只负责把默认 Agent 的 `modelId` 补齐 —— 老配置里的 Agent 定义可能根本没写
103
+ * `modelId`(用户的兜底是 `config.defaultModel`),补齐时优先用它,升级才不会改变用户
104
+ * 实际在用的模型。
103
105
  *
104
- * @param legacyDefaultModel 老配置里的 `config.defaultModel`。仅在 Agent **既无 modelId**
105
- * 且该模型仍在模型清单里时用它补齐,让升级不改变用户实际在用的模型;之后
106
- * 这个字段就彻底不存在了。
106
+ * @param defaultModel 配置里的 `defaultModel`(三级解析的兜底那一级)。仅在默认 Agent
107
+ * **既无 modelId** 且该模型仍在模型清单里时用它补齐,让升级不改变用户实际在用的模型。
108
+ * 注意它不是「永远生效的兜底」:Agent 自己绑了模型就轮不到它(见 `resolveModel`)。
107
109
  */
108
- async ensureDefaultAgent(legacyDefaultModel) {
110
+ async ensureDefaultAgent(defaultModel) {
109
111
  const agentsDir = join(this.basePath, "agents");
110
112
  const defaultPath = join(agentsDir, `${DEFAULT_AGENT_ID}.json`);
111
- const fallbackModelId = legacyDefaultModel && this.knownModelIds.has(legacyDefaultModel)
112
- ? legacyDefaultModel
113
+ const fallbackModelId = defaultModel && this.knownModelIds.has(defaultModel)
114
+ ? defaultModel
113
115
  : DEFAULT_AGENT_DEFINITION.modelId;
114
116
  try {
115
117
  const existing = JSON.parse(await fs.readFile(defaultPath, "utf-8"));
@@ -206,12 +208,10 @@ export class ConfigManager {
206
208
  */
207
209
  async bootstrap() {
208
210
  await this.ensureDirs();
209
- // 迁移前读一次磁盘原文:只为了拿旧的 `defaultModel`(字段已从类型里移除),
210
- // 让升级不改变用户原本在用的模型
211
- const raw = (await this.readRaw());
211
+ // 先把配置读到当前形状(内含迁移),再从里面取 `defaultModel` 去补默认 Agent 的模型
212
212
  const config = await this.ensureDefaultConfig();
213
213
  this.knownModelIds = new Set((config.models ?? []).map((m) => m.id));
214
- const agent = await this.ensureDefaultAgent(raw?.defaultModel);
214
+ const agent = await this.ensureDefaultAgent(config.defaultModel);
215
215
  const subAgent = await this.ensureDefaultSubAgent();
216
216
  await this.ensureDefaultMcpConfig();
217
217
  await this.ensureFactorySkills();
@@ -394,7 +394,9 @@ export class ConfigManager {
394
394
  * - 默认模型 ID(defaultImageModel / defaultMigrationModel)
395
395
  * 在合并后若已不存在 → 回退到出厂默认,避免悬空引用。
396
396
  *
397
- * 注意 `defaultModel` 已不再存在:模型真相源是 `AgentDefinition.modelId`。
397
+ * `defaultModel`(三级解析的全局兜底)**不在这里校验、也不在这里改写**:它指向一个已被删掉
398
+ * 的模型时,`createAgent` 会在真要跑的时候报 `Model "x" not found` —— 那时用户看得见自己
399
+ * 刚删了什么;而在这里默默换成别的模型,只会让「我明明没改它」变成一宗悬案。
398
400
  *
399
401
  * 端点(endpoints)不参与托管,始终保持用户配置。
400
402
  */
@@ -4,8 +4,10 @@ export declare const DEFAULT_AGENT_ID = "default";
4
4
  /**
5
5
  * 出厂默认模型 id。
6
6
  *
7
- * 模型的**唯一真相源是 `AgentDefinition.modelId`**:配置里不再有 `defaultModel`,
8
- * 会话存档里也不记模型。这个常量只用于给出厂默认 Agent 填一个初始值。
7
+ * 模型按三级解析(`会话级覆盖 > AgentDefinition.modelId > AppConfig.defaultModel`,
8
+ * 唯一实现在 `runtime/resolveModel.ts`)。出厂配置里**只有**默认 Agent 定义绑了模型,
9
+ * `defaultModel` 刻意留空:兜底层是给用户声明「我清空 Agent 的 modelId 之后用哪个」的,
10
+ * 出厂就填上等于替用户做了这个决定。这个常量只用于给出厂默认 Agent 填初始值。
9
11
  */
10
12
  export declare const DEFAULT_MODEL_ID = "deepseek-flash";
11
13
  /** 默认 Agent 定义(不含时间戳) */
@@ -33,6 +35,6 @@ export declare const DEFAULT_MCP_CONFIG: {
33
35
  * 各客户端(CLI/Desktop)的运行时数据(config.json、conversations/、drafts/)
34
36
  * 由客户端自行在各自目录下管理:
35
37
  * ~/.ai-zen/cli/ ← CLI 运行时
36
- * ~/.ai-zen/desktop/ ← Desktop 运行时(未来)
38
+ * ~/.ai-zen/desktop/ ← Desktop 运行时
37
39
  */
38
40
  export declare const CONFIG_SUB_DIRS: readonly ["agents", "sub-agents", "skills", "tools", "mcp-oauth"];
@@ -7,8 +7,10 @@ export const DEFAULT_AGENT_ID = "default";
7
7
  /**
8
8
  * 出厂默认模型 id。
9
9
  *
10
- * 模型的**唯一真相源是 `AgentDefinition.modelId`**:配置里不再有 `defaultModel`,
11
- * 会话存档里也不记模型。这个常量只用于给出厂默认 Agent 填一个初始值。
10
+ * 模型按三级解析(`会话级覆盖 > AgentDefinition.modelId > AppConfig.defaultModel`,
11
+ * 唯一实现在 `runtime/resolveModel.ts`)。出厂配置里**只有**默认 Agent 定义绑了模型,
12
+ * `defaultModel` 刻意留空:兜底层是给用户声明「我清空 Agent 的 modelId 之后用哪个」的,
13
+ * 出厂就填上等于替用户做了这个决定。这个常量只用于给出厂默认 Agent 填初始值。
12
14
  */
13
15
  export const DEFAULT_MODEL_ID = "deepseek-flash";
14
16
  /** 默认 Agent 定义(不含时间戳) */
@@ -16,7 +18,8 @@ export const DEFAULT_AGENT_DEFINITION = {
16
18
  id: DEFAULT_AGENT_ID,
17
19
  name: "Default Assistant",
18
20
  description: "The default AI assistant, suited for everyday Q&A and task execution.",
19
- // 模型写在 Agent 定义上,是运行期唯一真相源(无 config.defaultModel 兜底)
21
+ // 模型写在 Agent 定义上(三级解析的第二级,也是最常生效的那一级);
22
+ // 配置层的 `defaultModel` 只是 Agent 没绑模型时的兜底,出厂不预置
20
23
  modelId: DEFAULT_MODEL_ID,
21
24
  messages: [
22
25
  Message.System(`You are a witty, rigorous and reliable assistant. Always reply in the same language the user writes in. Current operating system: ${process.platform}.
@@ -238,6 +241,9 @@ export const DEFAULT_APP_CONFIG = {
238
241
  defaultImageModel: "cogview-4",
239
242
  defaultAgent: "default",
240
243
  defaultMigrationModel: "deepseek-flash",
244
+ // 自动标题:新会话第一轮结束后异步让模型起个名字(缺省开启,纯体验功能,
245
+ // 关掉只省一次几十 token 的请求;缺字段的老配置也按开启算,判定见 `AppConfig.autoTitle`)
246
+ autoTitle: true,
241
247
  // 工具输出上限(字符数),与 DEFAULT_MAX_TOOL_OUTPUT 保持一致
242
248
  maxToolOutput: 32_768,
243
249
  // 中断策略:出厂取「保留已输出的内容」—— 中断后模型能接着半截往下说,
@@ -282,6 +288,6 @@ export const DEFAULT_MCP_CONFIG = {
282
288
  * 各客户端(CLI/Desktop)的运行时数据(config.json、conversations/、drafts/)
283
289
  * 由客户端自行在各自目录下管理:
284
290
  * ~/.ai-zen/cli/ ← CLI 运行时
285
- * ~/.ai-zen/desktop/ ← Desktop 运行时(未来)
291
+ * ~/.ai-zen/desktop/ ← Desktop 运行时
286
292
  */
287
293
  export const CONFIG_SUB_DIRS = ["agents", "sub-agents", "skills", "tools", "mcp-oauth"];