@ai-zen/agents-sdk 1.0.0-alpha.9 → 1.1.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.
- package/CHANGELOG.md +691 -2
- package/README.md +22 -6
- package/README.zh.md +23 -7
- package/dist/agent-plugins/AutoMigratePlugin.d.ts +35 -9
- package/dist/agent-plugins/AutoMigratePlugin.js +26 -10
- package/dist/agent-plugins/AutoRefreshToolsPlugin.js +1 -3
- package/dist/agent-plugins/ContextGuardPlugin.d.ts +28 -16
- package/dist/agent-plugins/ContextGuardPlugin.js +23 -17
- package/dist/agent-plugins/UnknownToolHintPlugin.js +4 -4
- package/dist/config/ConfigManager.d.ts +57 -8
- package/dist/config/ConfigManager.js +167 -26
- package/dist/config/constants.d.ts +7 -0
- package/dist/config/constants.js +70 -43
- package/dist/config/factory-skills.d.ts +33 -0
- package/dist/config/factory-skills.js +126 -0
- package/dist/config/migrate.d.ts +106 -0
- package/dist/config/migrate.js +196 -0
- package/dist/config/reasoning-presets.d.ts +37 -0
- package/dist/config/reasoning-presets.js +64 -0
- package/dist/index.d.ts +13 -7
- package/dist/index.js +11 -3
- package/dist/runtime/HandoffGuardPlugin.d.ts +35 -0
- package/dist/runtime/HandoffGuardPlugin.js +65 -0
- package/dist/runtime/HandoffService.d.ts +288 -0
- package/dist/runtime/HandoffService.js +538 -0
- package/dist/runtime/SdkAgent.d.ts +7 -1
- package/dist/runtime/SdkAgent.js +3 -0
- package/dist/runtime/createAgent.d.ts +20 -1
- package/dist/runtime/createAgent.js +14 -9
- package/dist/runtime/createModel.d.ts +8 -4
- package/dist/runtime/createModel.js +22 -5
- package/dist/scope/PermissionEvaluator.js +1 -1
- package/dist/scope/Scope.d.ts +3 -1
- package/dist/scope/Scope.js +4 -2
- package/dist/scope/plugins/builtin/BatchEditTool.js +24 -14
- package/dist/scope/plugins/builtin/CopyTool.js +4 -4
- package/dist/scope/plugins/builtin/CwdTool.js +1 -1
- package/dist/scope/plugins/builtin/DownloadFileTool.d.ts +1 -0
- package/dist/scope/plugins/builtin/DownloadFileTool.js +58 -34
- package/dist/scope/plugins/builtin/EditTool.js +25 -25
- package/dist/scope/plugins/builtin/ExecTool.js +134 -25
- package/dist/scope/plugins/builtin/ExistTool.js +2 -2
- package/dist/scope/plugins/builtin/FindTextTool.js +15 -10
- package/dist/scope/plugins/builtin/GenerateImageTool.js +91 -25
- package/dist/scope/plugins/builtin/GlobTool.js +10 -5
- package/dist/scope/plugins/builtin/InspectFileTool.js +7 -7
- package/dist/scope/plugins/builtin/LsTool.js +5 -5
- package/dist/scope/plugins/builtin/MkdirTool.js +3 -3
- package/dist/scope/plugins/builtin/ReadTool.js +8 -8
- package/dist/scope/plugins/builtin/RenameTool.js +3 -3
- package/dist/scope/plugins/builtin/RmTool.js +3 -3
- package/dist/scope/plugins/builtin/SleepTool.js +6 -6
- package/dist/scope/plugins/builtin/SpawnTool.js +32 -49
- package/dist/scope/plugins/builtin/ViewImageTool.d.ts +6 -4
- package/dist/scope/plugins/builtin/ViewImageTool.js +50 -62
- package/dist/scope/plugins/builtin/WriteTool.js +3 -3
- package/dist/scope/plugins/builtin/searchBase.d.ts +9 -0
- package/dist/scope/plugins/builtin/searchBase.js +17 -0
- package/dist/scope/plugins/builtin/test-helpers.d.ts +3 -3
- package/dist/scope/plugins/builtin/test-helpers.js +3 -3
- package/dist/scope/plugins/builtin/textEdit.d.ts +17 -0
- package/dist/scope/plugins/builtin/textEdit.js +37 -0
- package/dist/scope/plugins/mcp/McpConnectionManager.d.ts +9 -1
- package/dist/scope/plugins/mcp/McpConnectionManager.js +95 -16
- package/dist/scope/plugins/mcp/McpScopePlugin.d.ts +1 -1
- package/dist/scope/plugins/mcp/McpScopePlugin.js +8 -6
- package/dist/scope/plugins/mcp/discover.js +1 -1
- package/dist/scope/plugins/mcp/mcpTools.d.ts +9 -3
- package/dist/scope/plugins/mcp/mcpTools.js +247 -37
- package/dist/scope/plugins/skills/SkillsScopePlugin.js +2 -3
- package/dist/scope/plugins/skills/discover.js +28 -9
- package/dist/scope/plugins/skills/skillTools.js +30 -20
- package/dist/scope/plugins/subagents/SubAgentsScopePlugin.js +1 -3
- package/dist/scope/plugins/subagents/subAgentTools.js +4 -3
- package/dist/scope/plugins/usertools/discover.d.ts +8 -3
- package/dist/scope/plugins/usertools/discover.js +139 -12
- package/dist/shared/EntityRepository.d.ts +10 -1
- package/dist/shared/EntityRepository.js +18 -6
- package/dist/shared/content-blocks.d.ts +101 -0
- package/dist/shared/content-blocks.js +253 -0
- package/dist/shared/errors.d.ts +4 -8
- package/dist/shared/errors.js +8 -14
- package/dist/types/index.d.ts +170 -8
- package/package.json +16 -7
- package/dist/runtime/TaskMigrationService.d.ts +0 -128
- package/dist/runtime/TaskMigrationService.js +0 -211
|
@@ -0,0 +1,538 @@
|
|
|
1
|
+
import { promises as fs } from "node:fs";
|
|
2
|
+
import { dirname, join, resolve as resolvePath } from "node:path";
|
|
3
|
+
import { Message } from "@ai-zen/agents-core";
|
|
4
|
+
import { getLogger } from "../shared/logger.js";
|
|
5
|
+
import { readSkill } from "../scope/plugins/skills/discover.js";
|
|
6
|
+
import { FALLBACK_HANDOFF_TEMPLATE, HANDOFF_SKILL_ID } from "../config/factory-skills.js";
|
|
7
|
+
import { HandoffGuardPlugin } from "./HandoffGuardPlugin.js";
|
|
8
|
+
/**
|
|
9
|
+
* 交接被调用方取消(`AbortSignal` 触发)。
|
|
10
|
+
*
|
|
11
|
+
* 单独成类型而不是复用 `Error`,原因有两个:
|
|
12
|
+
* - 调用方(如 TUI)要把「用户按了 Ctrl+C」与「交接真的失败了」区分开 ——
|
|
13
|
+
* 前者应静默收尾并提示「已取消」,后者才该报错;
|
|
14
|
+
* - 它**不可降级**:`self` 被取消后绝不能退到 `summary` 再跑一遍。
|
|
15
|
+
* 把「用户中止」当成「模型没写出文档」,会白白多烧一轮全量输入。
|
|
16
|
+
*/
|
|
17
|
+
export class HandoffAbortedError extends Error {
|
|
18
|
+
constructor() {
|
|
19
|
+
super("Handoff cancelled");
|
|
20
|
+
this.name = "HandoffAbortedError";
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* 错误是否来自取消。
|
|
25
|
+
*
|
|
26
|
+
* 两条路径都要认:
|
|
27
|
+
* - 调用方传了 `signal` 且已 `aborted`(`agent.abort()` 就是这种情况 ——
|
|
28
|
+
* 它**不抛错**,core 层把消息标记成 Aborted 后正常返回);
|
|
29
|
+
* - OpenAI SDK 在请求被 abort 时抛的 `APIUserAbortError`(`name` 为 `AbortError`)。
|
|
30
|
+
*/
|
|
31
|
+
function isAbortError(err, signal) {
|
|
32
|
+
if (signal?.aborted)
|
|
33
|
+
return true;
|
|
34
|
+
const name = err?.name;
|
|
35
|
+
return name === "AbortError" || name === "APIUserAbortError";
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* 合并两条取消信号:任一触发即视为取消。
|
|
39
|
+
*
|
|
40
|
+
* 服务自身的 `abort()`(见 {@link HandoffService.abort})与调用方传入的 `signal`
|
|
41
|
+
* 都要认:前者是自动迁移唯一的取消入口(那条链路调用方递不进 signal),后者留给
|
|
42
|
+
* 「外层还有自己的超时 / 取消」的调用方。
|
|
43
|
+
*/
|
|
44
|
+
function mergeSignals(own, external) {
|
|
45
|
+
return external ? AbortSignal.any([own, external]) : own;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* 任务交接服务。
|
|
49
|
+
*
|
|
50
|
+
* 职责分层(这是本模块唯一的设计主线):
|
|
51
|
+
* - `handoff()`:**只产出交接文档**(停工 → 写文档 → 校验 → 失败降级),不动会话;
|
|
52
|
+
* - `migrate()`:`handoff()` 之后清历史并开新会话(原「任务迁移」语义)。
|
|
53
|
+
*
|
|
54
|
+
* 模板的唯一真相源是 `handoff` Skill(`SKILL.md`);代码只负责
|
|
55
|
+
* 「读正文 → 注入 → 校验文件 → 读不到时用兜底模板」,不持有第二份章节清单。
|
|
56
|
+
*
|
|
57
|
+
* 重入:`handoff()` 与 `migrate()` 共用 `busy` 标志。交接回合本身也是一次 `send()`,
|
|
58
|
+
* 若不在 busy 期间让 `AutoMigratePlugin` / `ContextGuardPlugin` 让行,就会「交接触发迁移、
|
|
59
|
+
* 迁移再触发交接」无限递归,或在最满的时刻被护栏直接击杀。
|
|
60
|
+
*
|
|
61
|
+
* 取消:取消句柄由服务自己持有({@link HandoffService.abort}),而不是只认调用方传进来的
|
|
62
|
+
* `signal` —— 自动迁移由 `AutoMigratePlugin` 直接驱动,调用方根本没有机会把 signal 递进来,
|
|
63
|
+
* 只认外部 signal 的后果是「自动迁移期间按 Ctrl+C 会被当成『文档没写出来』,于是重试一轮、
|
|
64
|
+
* 再降级 summary 一轮,最后照样迁移」。取消后一律回到交接前的状态(见 {@link HandoffService.restoreBeforeHandoff})。
|
|
65
|
+
*/
|
|
66
|
+
export class HandoffService {
|
|
67
|
+
/** 交接文档兜底模板(仅在读不到 Skill 时使用) */
|
|
68
|
+
static FALLBACK_TEMPLATE = FALLBACK_HANDOFF_TEMPLATE;
|
|
69
|
+
handoffDirPath;
|
|
70
|
+
defaultMode;
|
|
71
|
+
defaultStrategy;
|
|
72
|
+
skillDirs;
|
|
73
|
+
client;
|
|
74
|
+
model;
|
|
75
|
+
modelConfig;
|
|
76
|
+
minHandoffBytes;
|
|
77
|
+
onBeforeHandoff;
|
|
78
|
+
onHandoff;
|
|
79
|
+
onMigrated;
|
|
80
|
+
logger;
|
|
81
|
+
/** 交接/迁移进行中标志(插件据此让行) */
|
|
82
|
+
_busy = false;
|
|
83
|
+
/**
|
|
84
|
+
* 当前这次交接/迁移的取消句柄(`null` = 没有进行中的交接)。
|
|
85
|
+
*
|
|
86
|
+
* 三条链路(`/handoff`、`/migrate`、自动迁移)共用同一个开关:前两条还有调用方能
|
|
87
|
+
* 传 `signal`,自动迁移没有 —— 句柄放在服务里,三者的取消语义才不会各不相同。
|
|
88
|
+
*/
|
|
89
|
+
abortController = null;
|
|
90
|
+
/** 交接回合的工具守卫(同一实例复用 + arm/disarm,避免插件数组无限增长) */
|
|
91
|
+
guard;
|
|
92
|
+
/** 已注册过守卫的 agent(每个 agent 只 use 一次) */
|
|
93
|
+
guardedAgents = new WeakSet();
|
|
94
|
+
constructor(options = {}) {
|
|
95
|
+
const { handoffDir, mode, strategy, skillDirs, client, model, modelConfig, minHandoffBytes, onBeforeHandoff, onHandoff, onMigrated, logger, } = options;
|
|
96
|
+
this.handoffDirPath = resolvePath(handoffDir ?? join(process.cwd(), ".ai-zen", "handoffs"));
|
|
97
|
+
this.defaultMode = mode ?? "self";
|
|
98
|
+
this.defaultStrategy = strategy ?? "omit";
|
|
99
|
+
this.skillDirs = skillDirs ?? [];
|
|
100
|
+
this.client = client;
|
|
101
|
+
this.model = model;
|
|
102
|
+
this.modelConfig = modelConfig;
|
|
103
|
+
this.minHandoffBytes = minHandoffBytes ?? 400;
|
|
104
|
+
this.onBeforeHandoff = onBeforeHandoff;
|
|
105
|
+
this.onHandoff = onHandoff;
|
|
106
|
+
this.onMigrated = onMigrated;
|
|
107
|
+
this.logger = logger ?? getLogger();
|
|
108
|
+
}
|
|
109
|
+
/** 是否正在交接/迁移(`AutoMigratePlugin` / `ContextGuardPlugin` 据此让行) */
|
|
110
|
+
get busy() {
|
|
111
|
+
return this._busy;
|
|
112
|
+
}
|
|
113
|
+
/** 交接文档目录 */
|
|
114
|
+
get handoffDir() {
|
|
115
|
+
return this.handoffDirPath;
|
|
116
|
+
}
|
|
117
|
+
// ==================================================================
|
|
118
|
+
// 静态纯工具(供上层复用 / 单测)
|
|
119
|
+
// ==================================================================
|
|
120
|
+
/**
|
|
121
|
+
* 交接文件名:`<YYYYMMDD-HHmmss>.md`(本地时间,便于人眼归档)。
|
|
122
|
+
*
|
|
123
|
+
* 只有到秒的时间戳。同一秒内两次交接会撞名 —— 不做去重兜底:同一时刻并发交接
|
|
124
|
+
* 本来就是调用方该避免的事,加随机后缀只会让文件名更难认。
|
|
125
|
+
*/
|
|
126
|
+
static createHandoffFileName(date = new Date()) {
|
|
127
|
+
const p = (n) => String(n).padStart(2, "0");
|
|
128
|
+
const stamp = `${date.getFullYear()}${p(date.getMonth() + 1)}${p(date.getDate())}` +
|
|
129
|
+
`-${p(date.getHours())}${p(date.getMinutes())}${p(date.getSeconds())}`;
|
|
130
|
+
return `${stamp}.md`;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* 拼装「本轮交接」指令:`[Skill 正文] + [本次交接参数]`。
|
|
134
|
+
*
|
|
135
|
+
* 不在 Skill 正文里发明占位符(本体系没有模板变量引擎),参数一律由代码拼在后面,
|
|
136
|
+
* 这样 Skill 只讲「怎么写」、代码只讲「写到哪、有什么能力」。
|
|
137
|
+
*/
|
|
138
|
+
static createHandoffInstruction(params) {
|
|
139
|
+
const { skillContent, handoffPath, toolsAvailable } = params;
|
|
140
|
+
const tail = toolsAvailable
|
|
141
|
+
? [
|
|
142
|
+
"Start writing now: use the write tool to put the complete document at the path given above (use the path exactly as given, do not rename it and do not move it to another directory).",
|
|
143
|
+
"After writing, read it back once with the read tool to confirm it is complete, then reply with a single confirmation of no more than 40 words, **do not restate the document content**.",
|
|
144
|
+
]
|
|
145
|
+
: [
|
|
146
|
+
"Output the complete document body directly: no explanations, do not wrap it in a code block, the caller will persist it to disk itself.",
|
|
147
|
+
];
|
|
148
|
+
return [
|
|
149
|
+
"You are now to write a task handoff document for this conversation. The following handoff rules are mandatory.",
|
|
150
|
+
"",
|
|
151
|
+
"---",
|
|
152
|
+
"",
|
|
153
|
+
skillContent.trim(),
|
|
154
|
+
"",
|
|
155
|
+
"---",
|
|
156
|
+
"",
|
|
157
|
+
"## Handoff parameters",
|
|
158
|
+
`- Handoff document absolute path: ${handoffPath}`,
|
|
159
|
+
`- Tools available: ${toolsAvailable
|
|
160
|
+
? "yes (read / exec, etc., use them to verify the actual state)"
|
|
161
|
+
: "no (you cannot inspect the disk, so for any state information always write '⚠️ Unverified', never speculate)"}`,
|
|
162
|
+
"",
|
|
163
|
+
...tail,
|
|
164
|
+
].join("\n");
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* 生成迁移后追加的「交接断点」消息(role=user):只给路径,让接手方自己 read。
|
|
168
|
+
*
|
|
169
|
+
* 为什么不内联全文:接手方第一次真正需要的是「去哪里取交接」,读一次文件的输入成本
|
|
170
|
+
* 与内联全文相当,但内联会让每条请求都携带全文;路径方案把成本推迟到真正需要时。
|
|
171
|
+
*/
|
|
172
|
+
static createPostMessages(handoffPath) {
|
|
173
|
+
const content = [
|
|
174
|
+
"The previous conversation has been handed off, the handoff document is saved at:",
|
|
175
|
+
handoffPath,
|
|
176
|
+
"",
|
|
177
|
+
"Read that file first with the read tool, and once you understand the context and the unfinished work, continue helping the user with the task.",
|
|
178
|
+
].join("\n");
|
|
179
|
+
return [Message.User(content)];
|
|
180
|
+
}
|
|
181
|
+
/** 序列化消息为 `[role]: content` 文本(仅 `summary` 模式使用) */
|
|
182
|
+
static serializeMessages(messages) {
|
|
183
|
+
return messages
|
|
184
|
+
.map((m) => {
|
|
185
|
+
const role = m.role ?? "unknown";
|
|
186
|
+
const content = typeof m.content === "string" ? m.content : JSON.stringify(m.content);
|
|
187
|
+
return `[${role}]: ${content}`;
|
|
188
|
+
})
|
|
189
|
+
.join("\n\n");
|
|
190
|
+
}
|
|
191
|
+
// ==================================================================
|
|
192
|
+
// 公开流程
|
|
193
|
+
// ==================================================================
|
|
194
|
+
/**
|
|
195
|
+
* 取消进行中的交接/迁移(用户按 Ctrl+C)。没有进行中的交接时是 no-op。
|
|
196
|
+
*
|
|
197
|
+
* 边界:本方法只保证「停下 + 回退」——不再重试、不降级 summary、不迁移,并把会话
|
|
198
|
+
* 退回交接前。**它不负责立刻掐断正在跑的模型请求**:`self` 模式的回合是
|
|
199
|
+
* `agent.send()`,调用方通常要同时 `agent.abort()`(TUI 就是这么做的);`summary`
|
|
200
|
+
* 模式的那次请求拿的是本服务合并出的 signal,会被真正中断。
|
|
201
|
+
*/
|
|
202
|
+
abort() {
|
|
203
|
+
this.abortController?.abort();
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* 只产出交接文档,**不改动会话**(`/handoff`)。
|
|
207
|
+
*
|
|
208
|
+
* 不清理历史:交接回合(指令 + 工具调用 + 回执)会留在 `agent.messages` 里,
|
|
209
|
+
* 用户可继续让 agent 打磨这份文档(重写同一路径即可)。被取消时相反:整个交接
|
|
210
|
+
* 回合从 `agent.messages` 里截掉,本次新建的文档也不留。
|
|
211
|
+
*/
|
|
212
|
+
async handoff(params) {
|
|
213
|
+
return this.withBusy((own) => this.runHandoff({ ...params, signal: mergeSignals(own, params.signal) }, false));
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* 交接 + 开新会话(`/migrate`、自动迁移)。
|
|
217
|
+
*
|
|
218
|
+
* 策略(`strategy`):
|
|
219
|
+
* - `omit`(默认):历史标记 `omit: true`(保留可审计),末尾追加交接断点;
|
|
220
|
+
* - `prune`:物理剔除历史,只留 `definition.messages` + 交接断点。
|
|
221
|
+
*/
|
|
222
|
+
async migrate(params) {
|
|
223
|
+
return this.withBusy(async (own) => {
|
|
224
|
+
// 拿到 ctx 即「已提交」:文档已通过校验,取消不再生效(见 runHandoff 末尾的取消点)。
|
|
225
|
+
// 在 prune 之前再拦一次取消会留下第三种状态 ——「用户点了取消,旧会话却已存档、
|
|
226
|
+
// 历史却已剔除」,比让迁移做完更糟
|
|
227
|
+
const ctx = await this.runHandoff({ ...params, signal: mergeSignals(own, params.signal) }, true);
|
|
228
|
+
const { agent } = ctx;
|
|
229
|
+
const strategy = params.strategy ?? this.defaultStrategy;
|
|
230
|
+
const breakpointMessages = HandoffService.createPostMessages(ctx.handoffPath);
|
|
231
|
+
// 交接回合可能已经把 messages 换成新会话,definition 也可能是手写的(没有 messages):
|
|
232
|
+
// 这里按空模板处理,好让报错落在"模型该补的配置"上,而不是原生 TypeError
|
|
233
|
+
const templateMessages = agent.definition.messages ?? [];
|
|
234
|
+
if (strategy === "prune") {
|
|
235
|
+
// 浅拷贝模板消息,避免污染 definition.messages 引用的对象
|
|
236
|
+
agent.messages = [...templateMessages, ...breakpointMessages];
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
const preserveCount = Math.min(templateMessages.length, agent.messages.length);
|
|
240
|
+
agent.messages = [
|
|
241
|
+
...agent.messages.map((m, i) => ({
|
|
242
|
+
...m,
|
|
243
|
+
omit: i >= preserveCount ? true : m.omit,
|
|
244
|
+
})),
|
|
245
|
+
...breakpointMessages,
|
|
246
|
+
];
|
|
247
|
+
}
|
|
248
|
+
await this.safeHook(this.onMigrated, ctx);
|
|
249
|
+
return ctx;
|
|
250
|
+
});
|
|
251
|
+
}
|
|
252
|
+
// ==================================================================
|
|
253
|
+
// 内部
|
|
254
|
+
// ==================================================================
|
|
255
|
+
/**
|
|
256
|
+
* 串行化 + 防重入:交接与迁移共用同一把锁,也共用同一个取消句柄。
|
|
257
|
+
*
|
|
258
|
+
* 句柄与 `busy` 同寿命:`finally` 里清掉,否则下一次 `abort()` 会往一个已结束的
|
|
259
|
+
* controller 上发出无意义的 abort(看起来「取消成功」,实际什么都没发生)。
|
|
260
|
+
*/
|
|
261
|
+
async withBusy(fn) {
|
|
262
|
+
if (this._busy)
|
|
263
|
+
throw new Error("A handoff is already in progress, wait for it to finish");
|
|
264
|
+
this._busy = true;
|
|
265
|
+
const controller = new AbortController();
|
|
266
|
+
this.abortController = controller;
|
|
267
|
+
try {
|
|
268
|
+
return await fn(controller.signal);
|
|
269
|
+
}
|
|
270
|
+
finally {
|
|
271
|
+
this._busy = false;
|
|
272
|
+
if (this.abortController === controller)
|
|
273
|
+
this.abortController = null;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
/** 取消点检查:每跨过一个阶段就确认一次,避免取消后继续往下一步冲 */
|
|
277
|
+
throwIfAborted(signal) {
|
|
278
|
+
if (signal?.aborted)
|
|
279
|
+
throw new HandoffAbortedError();
|
|
280
|
+
}
|
|
281
|
+
async fileExists(path) {
|
|
282
|
+
try {
|
|
283
|
+
await fs.access(path);
|
|
284
|
+
return true;
|
|
285
|
+
}
|
|
286
|
+
catch {
|
|
287
|
+
return false;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* 取消后清掉半成品文档。
|
|
292
|
+
*
|
|
293
|
+
* 只删「本次新建」的文件:交接文件名含时间戳,正常不会撞已有文件,但调用方也
|
|
294
|
+
* 可以把 `handoffDir` 指向人工目录;不管怎样都不该删掉别人的文件。
|
|
295
|
+
* 留着半成品更糟 —— 下一轮接手方会把不完整的内容当成真实交接读进去。
|
|
296
|
+
*/
|
|
297
|
+
async discardPartialFile(path, existedBefore) {
|
|
298
|
+
if (existedBefore)
|
|
299
|
+
return;
|
|
300
|
+
try {
|
|
301
|
+
await fs.unlink(path);
|
|
302
|
+
}
|
|
303
|
+
catch {
|
|
304
|
+
/* 文件本就没写出来:忽略 */
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
/**
|
|
308
|
+
* 跑完一次交接("/handoff" 与 "/migrate" 共用),取消时**由本方法统一回退**。
|
|
309
|
+
*
|
|
310
|
+
* 为什么回退只写在这一处:取消可能从任何一个阶段冒出来(`self` 回合后的主动检查、
|
|
311
|
+
* `summary` 请求被 abort、前置的 `readSkillContent`),分散到各自的 catch 里就会
|
|
312
|
+
* 漏掉「文档已落盘但还没校验通过」这种半成品状态。这里一次拿全三件事:
|
|
313
|
+
* 交接前的消息条数、文档路径、以及「文档本就不存在」这个事实。
|
|
314
|
+
*
|
|
315
|
+
* 取消形态有两类,都要认:{@link HandoffAbortedError}(主动检查)与底层 abort 错误
|
|
316
|
+
* (OpenAI 的 `APIUserAbortError`)。调用方只需要认前者。
|
|
317
|
+
*/
|
|
318
|
+
async runHandoff(params, migrating) {
|
|
319
|
+
const { agent, promptTokens, handoffTokens, signal } = params;
|
|
320
|
+
const requestedMode = params.mode ?? this.defaultMode;
|
|
321
|
+
const handoffPath = join(this.handoffDirPath, HandoffService.createHandoffFileName(new Date()));
|
|
322
|
+
const model = this.model ?? agent.model;
|
|
323
|
+
const messageCountBefore = agent.messages.length;
|
|
324
|
+
// 只删「本次新建」的文档:交接文件名含时间戳,正常不会撞已有文件,但调用方也可以
|
|
325
|
+
// 把 `handoffDir` 指向人工目录,不管怎样都不该删掉别人的文件
|
|
326
|
+
const existedBefore = await this.fileExists(handoffPath);
|
|
327
|
+
try {
|
|
328
|
+
// 迁移前上下文(不含文档,语义固定)
|
|
329
|
+
const beforeCtx = {
|
|
330
|
+
agent,
|
|
331
|
+
mode: requestedMode,
|
|
332
|
+
requestedMode,
|
|
333
|
+
handoffPath,
|
|
334
|
+
model,
|
|
335
|
+
promptTokens,
|
|
336
|
+
handoffTokens,
|
|
337
|
+
migrating,
|
|
338
|
+
messageCountBefore,
|
|
339
|
+
historyText: "",
|
|
340
|
+
};
|
|
341
|
+
await this.safeHook(this.onBeforeHandoff, beforeCtx);
|
|
342
|
+
this.throwIfAborted(signal);
|
|
343
|
+
// ---- 执行者:self 优先,失败/不可用则单向降级 summary ----
|
|
344
|
+
let mode = requestedMode;
|
|
345
|
+
let handoffDoc;
|
|
346
|
+
let historyText = "";
|
|
347
|
+
if (mode === "self" && !this.supportsSelfHandoff(agent)) {
|
|
348
|
+
this.logger.warn("[handoff] agent has no write tool or send capability, falling back to standalone summary mode");
|
|
349
|
+
mode = "summary";
|
|
350
|
+
}
|
|
351
|
+
if (mode === "self") {
|
|
352
|
+
const ok = await this.runSelfHandoff(agent, handoffPath, signal);
|
|
353
|
+
if (!ok) {
|
|
354
|
+
this.logger.warn("[handoff] self handoff failed, falling back to standalone summary mode");
|
|
355
|
+
mode = "summary";
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
if (mode === "summary") {
|
|
359
|
+
historyText = HandoffService.serializeMessages(agent.messages.filter((m) => !m.omit));
|
|
360
|
+
handoffDoc = await this.generateSummary(agent, historyText, handoffPath, signal);
|
|
361
|
+
await this.writeHandoffFile(handoffPath, handoffDoc);
|
|
362
|
+
}
|
|
363
|
+
// 最后一道取消点:即使两步都跑完,只要期间被取消就不能算成功。跨过这一步就是
|
|
364
|
+
// 「已提交」——之后的 `onHandoff` 钩子(存旧会话等)与迁移收尾不再响应取消
|
|
365
|
+
this.throwIfAborted(signal);
|
|
366
|
+
const ctx = {
|
|
367
|
+
agent,
|
|
368
|
+
mode,
|
|
369
|
+
requestedMode,
|
|
370
|
+
handoffPath,
|
|
371
|
+
handoffDoc,
|
|
372
|
+
model,
|
|
373
|
+
promptTokens,
|
|
374
|
+
handoffTokens,
|
|
375
|
+
migrating,
|
|
376
|
+
messageCountBefore,
|
|
377
|
+
historyText,
|
|
378
|
+
};
|
|
379
|
+
await this.safeHook(this.onHandoff, ctx);
|
|
380
|
+
return ctx;
|
|
381
|
+
}
|
|
382
|
+
catch (err) {
|
|
383
|
+
if (!(err instanceof HandoffAbortedError) && !isAbortError(err, signal))
|
|
384
|
+
throw err;
|
|
385
|
+
await this.restoreBeforeHandoff({ agent, messageCountBefore, handoffPath, existedBefore });
|
|
386
|
+
throw new HandoffAbortedError();
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
/**
|
|
390
|
+
* 取消后回到交接前的状态:把交接回合追加的消息全部截掉,并删掉本次新建的文档。
|
|
391
|
+
*
|
|
392
|
+
* 为什么必须截消息:`self` 模式的交接回合是「一条几 KB 的 Skill 指令 user 消息 +
|
|
393
|
+
* 工具结果 + 回执」,留在 `agent.messages` 里既让用户看到半段无关对话,也让下一次
|
|
394
|
+
* 请求把它当历史再发一遍(`↑` 翻历史还会把它翻出来)。`agent.messages` 是唯一真相源,
|
|
395
|
+
* 截回原长度之后,转录、历史、用量全都自动复位,不需要第二处补偿。
|
|
396
|
+
*/
|
|
397
|
+
async restoreBeforeHandoff(params) {
|
|
398
|
+
const { agent, messageCountBefore, handoffPath, existedBefore } = params;
|
|
399
|
+
if (agent.messages.length > messageCountBefore)
|
|
400
|
+
agent.messages.splice(messageCountBefore);
|
|
401
|
+
await this.discardPartialFile(handoffPath, existedBefore);
|
|
402
|
+
}
|
|
403
|
+
/** 自交接是否可用:需要 `write` 工具 + `send` 能力 */
|
|
404
|
+
supportsSelfHandoff(agent) {
|
|
405
|
+
if (typeof agent.send !== "function")
|
|
406
|
+
return false;
|
|
407
|
+
return Boolean(agent.tools?.some((tool) => tool.function?.name === "write"));
|
|
408
|
+
}
|
|
409
|
+
/**
|
|
410
|
+
* 自交接回合:让原 agent 停工、核实、写文档,并校验文件确实落盘。
|
|
411
|
+
*
|
|
412
|
+
* 失败判定(任一成立即视为失败,交由上层单向降级 summary):
|
|
413
|
+
* - 文件不存在 / 小于阈值;
|
|
414
|
+
* - 模型/工具链抛错(例如没有权限、被护栏中断)。
|
|
415
|
+
*
|
|
416
|
+
* **取消不算失败**:`signal` 已触发时抛 {@link HandoffAbortedError},既不走
|
|
417
|
+
* 「文件没写好→重试一次」也不降级 summary。 这条分支必须显式写:
|
|
418
|
+
* `agent.abort()` 不抛错(core 把消息标记为 Aborted 后正常返回),否则「用户
|
|
419
|
+
* 按了 Ctrl+C」会被下面的文件校验误判成「模型没写出文档」,于是重试一轮、再降级
|
|
420
|
+
* 一轮 —— 用户以为取消了,实际多烧了两轮全量输入。
|
|
421
|
+
*
|
|
422
|
+
* 取消的善后(删文档、截消息)不在这里做:交给 {@link runHandoff} 的 catch 一处处理,
|
|
423
|
+
* 因为「文档已写出但还没校验」的取消根本进不到本方法的 catch。
|
|
424
|
+
*/
|
|
425
|
+
async runSelfHandoff(agent, handoffPath, signal) {
|
|
426
|
+
const guard = this.ensureGuard(agent);
|
|
427
|
+
const skillContent = await this.readSkillContent();
|
|
428
|
+
const cwd = agent.scope?.cwd ?? process.cwd();
|
|
429
|
+
/*
|
|
430
|
+
* 交接回合的护栏让行由 `ContextGuardPlugin.isSuspended` 负责(接线为
|
|
431
|
+
* `() => handoffService.busy`),这里不再去改写用量数据:
|
|
432
|
+
* - `lastUsage` 是从 `messages` 派生的只读值,本就不可写;
|
|
433
|
+
* - 「谁做限制谁放宽」—— 限制在护栏自己身上,放宽也必须写在护栏的选项里。
|
|
434
|
+
*/
|
|
435
|
+
guard.arm(handoffPath, cwd);
|
|
436
|
+
try {
|
|
437
|
+
await agent.send(HandoffService.createHandoffInstruction({
|
|
438
|
+
skillContent,
|
|
439
|
+
handoffPath,
|
|
440
|
+
toolsAvailable: true,
|
|
441
|
+
}));
|
|
442
|
+
// `agent.abort()` 不抛错,必须主动检查(见方法注释)
|
|
443
|
+
this.throwIfAborted(signal);
|
|
444
|
+
if (await this.isHandoffFileReady(handoffPath))
|
|
445
|
+
return true;
|
|
446
|
+
this.logger.warn(`[handoff] handoff document missing or too small, retrying once: ${handoffPath}`);
|
|
447
|
+
await agent.send([
|
|
448
|
+
`The handoff document does not exist or is too small to deliver: ${handoffPath}`,
|
|
449
|
+
"Use the write tool right now to write the **complete** handoff document to that path (use the path exactly as given) and do nothing else.",
|
|
450
|
+
].join("\n"));
|
|
451
|
+
this.throwIfAborted(signal);
|
|
452
|
+
return await this.isHandoffFileReady(handoffPath);
|
|
453
|
+
}
|
|
454
|
+
catch (err) {
|
|
455
|
+
if (isAbortError(err, signal))
|
|
456
|
+
throw new HandoffAbortedError();
|
|
457
|
+
this.logger.error(`[handoff] self handoff round failed: ${err?.message ?? err}`);
|
|
458
|
+
return false;
|
|
459
|
+
}
|
|
460
|
+
finally {
|
|
461
|
+
guard.disarm();
|
|
462
|
+
}
|
|
463
|
+
}
|
|
464
|
+
/** 独立摘要:一次模型调用,用同一份 Skill 正文作为规范 */
|
|
465
|
+
async generateSummary(agent, historyText, handoffPath, signal) {
|
|
466
|
+
const client = this.client ?? agent.client;
|
|
467
|
+
const model = this.model ?? agent.model;
|
|
468
|
+
const modelConfig = this.modelConfig ?? agent.modelConfig;
|
|
469
|
+
const skillContent = await this.readSkillContent();
|
|
470
|
+
this.throwIfAborted(signal);
|
|
471
|
+
// `signal` 走 RequestOptions(第二参数),与 `core/Agent.ts` 调用模型时一致:
|
|
472
|
+
// 放在 body 里不会被 OpenAI SDK 当成取消信号。
|
|
473
|
+
const response = await client.chat.completions.create({
|
|
474
|
+
model,
|
|
475
|
+
...(modelConfig ?? {}),
|
|
476
|
+
messages: [
|
|
477
|
+
{
|
|
478
|
+
role: "system",
|
|
479
|
+
content: HandoffService.createHandoffInstruction({
|
|
480
|
+
skillContent,
|
|
481
|
+
handoffPath,
|
|
482
|
+
toolsAvailable: false,
|
|
483
|
+
}),
|
|
484
|
+
},
|
|
485
|
+
{ role: "user", content: historyText },
|
|
486
|
+
],
|
|
487
|
+
}, signal ? { signal } : undefined);
|
|
488
|
+
const handoffDoc = response.choices[0]?.message?.content;
|
|
489
|
+
if (!handoffDoc || !handoffDoc.trim()) {
|
|
490
|
+
throw new Error("Failed to generate the handoff document");
|
|
491
|
+
}
|
|
492
|
+
return handoffDoc;
|
|
493
|
+
}
|
|
494
|
+
/** 落盘(仅 `summary` 模式需要;`self` 模式由 agent 的 write 工具完成) */
|
|
495
|
+
async writeHandoffFile(handoffPath, content) {
|
|
496
|
+
await fs.mkdir(dirname(handoffPath), { recursive: true });
|
|
497
|
+
await fs.writeFile(handoffPath, content, "utf-8");
|
|
498
|
+
}
|
|
499
|
+
/** 交接文档是否可用:文件存在且不小于阈值(不读内容,避免二次读取) */ async isHandoffFileReady(handoffPath) {
|
|
500
|
+
try {
|
|
501
|
+
const stat = await fs.stat(handoffPath);
|
|
502
|
+
return stat.isFile() && stat.size >= this.minHandoffBytes;
|
|
503
|
+
}
|
|
504
|
+
catch {
|
|
505
|
+
return false;
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
/** 读取 `handoff` Skill 正文;读不到时回落到兜底模板 */
|
|
509
|
+
async readSkillContent() {
|
|
510
|
+
if (this.skillDirs.length > 0) {
|
|
511
|
+
const skill = await readSkill(this.skillDirs, HANDOFF_SKILL_ID, { silent: true });
|
|
512
|
+
if (skill?.content?.trim())
|
|
513
|
+
return skill.content;
|
|
514
|
+
}
|
|
515
|
+
this.logger.warn(`[handoff] ${HANDOFF_SKILL_ID} skill not found, using the built-in fallback template (paths: ${this.skillDirs.join(", ") || "none configured"})`);
|
|
516
|
+
return FALLBACK_HANDOFF_TEMPLATE;
|
|
517
|
+
}
|
|
518
|
+
/** 守卫按 agent 注册一次(`Agent.use()` 无移除 API,故复用实例 + 开关) */
|
|
519
|
+
ensureGuard(agent) {
|
|
520
|
+
const guard = (this.guard ??= new HandoffGuardPlugin());
|
|
521
|
+
if (!this.guardedAgents.has(agent)) {
|
|
522
|
+
agent.use(guard);
|
|
523
|
+
this.guardedAgents.add(agent);
|
|
524
|
+
}
|
|
525
|
+
return guard;
|
|
526
|
+
}
|
|
527
|
+
/** 钩子容错:抛错只记录,不中断交接(与既有迁移钩子语义一致) */
|
|
528
|
+
async safeHook(hook, ctx) {
|
|
529
|
+
if (!hook)
|
|
530
|
+
return;
|
|
531
|
+
try {
|
|
532
|
+
await hook(ctx);
|
|
533
|
+
}
|
|
534
|
+
catch (err) {
|
|
535
|
+
this.logger.error(`[handoff] hook execution failed: ${err?.message ?? err}`);
|
|
536
|
+
}
|
|
537
|
+
}
|
|
538
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type OpenAI from "openai";
|
|
2
2
|
import { Agent } from "@ai-zen/agents-core";
|
|
3
|
-
import type { AgentNS, Tool } from "@ai-zen/agents-core";
|
|
3
|
+
import type { AbortedMessageStrategy, AbortedToolRoundStrategy, AgentNS, Tool } from "@ai-zen/agents-core";
|
|
4
4
|
import type { AgentDefinition } from "../types/index.js";
|
|
5
5
|
import type { Scope } from "../scope/Scope.js";
|
|
6
6
|
export type { AgentPlugin, SendContext } from "@ai-zen/agents-core";
|
|
@@ -30,5 +30,11 @@ export declare class SdkAgent extends Agent {
|
|
|
30
30
|
messages?: AgentNS.Message[];
|
|
31
31
|
tools?: Tool[];
|
|
32
32
|
allowJsonParseError?: boolean;
|
|
33
|
+
/** 工具轮次被中断时的归一策略(core 默认 `fill`) */
|
|
34
|
+
abortedToolRoundStrategy?: AbortedToolRoundStrategy;
|
|
35
|
+
/** `fill` 策略下未完成工具结果的填充原因 */
|
|
36
|
+
abortedToolRoundReason?: string;
|
|
37
|
+
/** 被中断的 assistant(内容/思考)是否保留(core 默认 `drop`) */
|
|
38
|
+
abortedMessageStrategy?: AbortedMessageStrategy;
|
|
33
39
|
});
|
|
34
40
|
}
|
package/dist/runtime/SdkAgent.js
CHANGED
|
@@ -24,6 +24,9 @@ export class SdkAgent extends Agent {
|
|
|
24
24
|
messages: params.messages,
|
|
25
25
|
tools: params.tools,
|
|
26
26
|
allowJsonParseError: params.allowJsonParseError,
|
|
27
|
+
abortedToolRoundStrategy: params.abortedToolRoundStrategy,
|
|
28
|
+
abortedToolRoundReason: params.abortedToolRoundReason,
|
|
29
|
+
abortedMessageStrategy: params.abortedMessageStrategy,
|
|
27
30
|
});
|
|
28
31
|
this.scope = params.scope;
|
|
29
32
|
this.definition = params.definition;
|
|
@@ -1,5 +1,24 @@
|
|
|
1
1
|
import { SdkAgent } from "./SdkAgent.js";
|
|
2
2
|
import type { Scope } from "../scope/Scope.js";
|
|
3
|
+
import type { AbortedMessageStrategy, AbortedToolRoundStrategy } from "@ai-zen/agents-core";
|
|
4
|
+
/**
|
|
5
|
+
* `createAgent` 的可选行为开关。
|
|
6
|
+
*
|
|
7
|
+
* 不传时先取 `scope.config`(`config.json`)里的同名配置,再往下才是 core 默认值:
|
|
8
|
+
* 中断策略属于**产品配置**,调用方不该自己读配置再转一道参数。
|
|
9
|
+
*/
|
|
10
|
+
export interface CreateAgentOptions {
|
|
11
|
+
/** 工具轮次被中断(或结果不完整)时的归一策略(core 默认 `fill`) */
|
|
12
|
+
abortedToolRoundStrategy?: AbortedToolRoundStrategy;
|
|
13
|
+
/** `fill` 策略下,未完成工具结果的填充原因 */
|
|
14
|
+
abortedToolRoundReason?: string;
|
|
15
|
+
/**
|
|
16
|
+
* 被中断的 assistant 消息(已输出的内容 / 思考)如何处置(未传时取 `scope.config`,
|
|
17
|
+
* 再缺则用 core 默认 `drop`)。
|
|
18
|
+
* `keep` 可让模型在下一轮接着半截往下说,而不是忘掉自己说过什么。
|
|
19
|
+
*/
|
|
20
|
+
abortedMessageStrategy?: AbortedMessageStrategy;
|
|
21
|
+
}
|
|
3
22
|
/**
|
|
4
23
|
* 从磁盘创建 Agent。
|
|
5
24
|
*
|
|
@@ -10,4 +29,4 @@ import type { Scope } from "../scope/Scope.js";
|
|
|
10
29
|
* // agent 是 SdkAgent,可直接注册插件、发送消息
|
|
11
30
|
* ```
|
|
12
31
|
*/
|
|
13
|
-
export declare function createAgent(scope: Scope, agentId: string): Promise<SdkAgent>;
|
|
32
|
+
export declare function createAgent(scope: Scope, agentId: string, options?: CreateAgentOptions): Promise<SdkAgent>;
|
|
@@ -11,19 +11,18 @@ import { createModel } from "./createModel.js";
|
|
|
11
11
|
* // agent 是 SdkAgent,可直接注册插件、发送消息
|
|
12
12
|
* ```
|
|
13
13
|
*/
|
|
14
|
-
export async function createAgent(scope, agentId) {
|
|
14
|
+
export async function createAgent(scope, agentId, options = {}) {
|
|
15
15
|
const definition = await new AgentRepository(scope.agentsDir).read(agentId);
|
|
16
16
|
if (!definition)
|
|
17
|
-
throw new Error(`Agent "${agentId}"
|
|
18
|
-
const modelId = definition.modelId
|
|
19
|
-
if (!modelId)
|
|
20
|
-
throw new Error("
|
|
21
|
-
|
|
17
|
+
throw new Error(`Agent "${agentId}" not found`);
|
|
18
|
+
const modelId = definition.modelId;
|
|
19
|
+
if (!modelId) {
|
|
20
|
+
throw new Error(`Agent "${agentId}" has no model bound (modelId is missing). Assign a model to it in the config`);
|
|
21
|
+
}
|
|
22
|
+
const { client, model, modelConfig } = createModel(scope, modelId, definition.modelParams);
|
|
22
23
|
const tools = scope.buildTools(definition, {
|
|
23
24
|
exclude: {
|
|
24
|
-
subagents: definition.function?.name
|
|
25
|
-
? [definition.function.name]
|
|
26
|
-
: undefined,
|
|
25
|
+
subagents: definition.function?.name ? [definition.function.name] : undefined,
|
|
27
26
|
},
|
|
28
27
|
});
|
|
29
28
|
const agent = new SdkAgent({
|
|
@@ -39,6 +38,12 @@ export async function createAgent(scope, agentId) {
|
|
|
39
38
|
// 快照后,运行时改动只作用于会话副本,模板保持纯净。
|
|
40
39
|
messages: [...(definition.messages ?? [])],
|
|
41
40
|
tools,
|
|
41
|
+
// 中断相关策略:**显式参数 > `scope.config` > core 默认值**。
|
|
42
|
+
// 中间那一层是「配置即事实」:策略写在 `config.json` 里,各端(CLI / Desktop)
|
|
43
|
+
// 不必自己读配置再转成参数,子 Agent 创建时也能自然继承同一份策略。
|
|
44
|
+
abortedToolRoundStrategy: options.abortedToolRoundStrategy ?? scope.config?.abortedToolRoundStrategy,
|
|
45
|
+
abortedToolRoundReason: options.abortedToolRoundReason,
|
|
46
|
+
abortedMessageStrategy: options.abortedMessageStrategy ?? scope.config?.abortedMessageStrategy,
|
|
42
47
|
});
|
|
43
48
|
return agent;
|
|
44
49
|
}
|
|
@@ -1,21 +1,25 @@
|
|
|
1
1
|
import OpenAI from "openai";
|
|
2
2
|
import type { Scope } from "../scope/Scope.js";
|
|
3
|
+
import type { ModelParams } from "../types/index.js";
|
|
3
4
|
/**
|
|
4
5
|
* Agent 模型装配结果:openai SDK client + 模型名 + 模型参数。
|
|
5
6
|
* Agent 构造时直接消费此结构。
|
|
6
7
|
*/
|
|
7
8
|
export interface AgentModel {
|
|
8
|
-
/** openai
|
|
9
|
+
/** openai SDK 客户端 */
|
|
9
10
|
client: OpenAI;
|
|
10
11
|
/** 发送给 API 的模型名 */
|
|
11
12
|
model: string;
|
|
12
|
-
/**
|
|
13
|
-
modelConfig?:
|
|
13
|
+
/** 模型参数,透传给 API(可含厂商特有字段,如 DeepSeek thinking) */
|
|
14
|
+
modelConfig?: ModelParams;
|
|
14
15
|
}
|
|
15
16
|
/**
|
|
16
17
|
* 模型工厂 — 根据 modelId 和 Scope 装配 openai SDK client。
|
|
17
18
|
*
|
|
18
19
|
* 端点(baseUrl / apiKey)来自 config.endpoints,发送给 API 的模型名 = modelName || id。
|
|
19
20
|
* 任何 OpenAI 兼容端点(OpenAI / 智谱 BigModel / DeepSeek 等)均可通过 baseURL 接入。
|
|
21
|
+
*
|
|
22
|
+
* `overrides` 是 **Agent 级参数覆盖**(`AgentDefinition.modelParams`):
|
|
23
|
+
* 同一个模型可以给不同 Agent 配不同参数,而模型自身的 `defaultParams` 保持不变。
|
|
20
24
|
*/
|
|
21
|
-
export declare function createModel(scope: Scope, modelId: string): AgentModel;
|
|
25
|
+
export declare function createModel(scope: Scope, modelId: string, overrides?: ModelParams): AgentModel;
|