dsh-submodel 0.1.0 → 0.2.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/README.en.md +249 -0
- package/README.md +160 -28
- package/lib/delegation.js +123 -39
- package/lib/index.js +93 -41
- package/lib/policy.js +407 -36
- package/lib/setup.js +253 -0
- package/package.json +12 -3
package/lib/delegation.js
CHANGED
|
@@ -5,27 +5,34 @@
|
|
|
5
5
|
*
|
|
6
6
|
* 原生的创建校验(allowlist / LLM preflight / permission / depth / persona / toolFilter)
|
|
7
7
|
* 全部发生在 `@deepseek-ai/dsh-tool-subagent` 的 `execute` 内部,**在创建孩子之前**
|
|
8
|
-
*
|
|
9
|
-
*
|
|
8
|
+
* (0.2.2:`dsh-tool-subagent/lib/index.js` 的 `subagent` 工具定义里,`execute(args, exec)`
|
|
9
|
+
* 先读 policy、再 assert 允许路由、再 preflight、最后才 `ctx.subagents.startActivation(...)`)。
|
|
10
|
+
* 绕过它去 `subagents.startActivation(...)` 就等于同时绕过授权与预检——那正是本模块**不能**做的事。
|
|
10
11
|
*
|
|
11
|
-
* 所以本工具只做两件事:**改参数** + **经公开 `ctx.tools.execute`
|
|
12
|
-
* (`dsh-tools/lib/index.js:3116` 是公开入口)。这样原生的一切校验照旧生效。
|
|
12
|
+
* 所以本工具只做两件事:**改参数** + **经公开 `ctx.tools.execute` 发起一次真正的原生调用**。
|
|
13
13
|
*
|
|
14
|
-
* ##
|
|
14
|
+
* ## 0.2.2 的四个契约(都已在本机安装版源码确认,别按 0.2.1 的记忆改)
|
|
15
15
|
*
|
|
16
|
-
* 1.
|
|
17
|
-
*
|
|
18
|
-
*
|
|
19
|
-
* `
|
|
20
|
-
*
|
|
21
|
-
*
|
|
22
|
-
*
|
|
16
|
+
* 1. **工具执行 API 是批量签名**(0.2.2 起的硬变更):`ctx.tools.execute(input)`,
|
|
17
|
+
* `input = {calls:[{id,name,arguments}], signal, agent?, toolSet?, log?, maxParallelToolCalls?}`
|
|
18
|
+
* (`dsh-tools/lib/pipeline.js` 的 `executeToolBatch` 解构即证)。返回
|
|
19
|
+
* `{results:[{callId,result}], additionalContexts, concluded}`;
|
|
20
|
+
* `arguments` 必须是 **JSON 文本**(pipeline 里 `parseArguments` 会 `JSON.parse`),
|
|
21
|
+
* 单次调用就是"只含一个 call 的批量"。
|
|
22
|
+
* 2. **没有 `parent` / `rootCallId` 通道**:批量 API 不暴露它们,`exec` 里也没有 turn/step。
|
|
23
|
+
* 因此嵌套调用在事件归属上与顶层调用无法区分(`parent` 现在只有 PTC 内部用)。
|
|
24
|
+
* 本模块**不传 `log`**:0.2.1 的 `execute` 同样不写 `tool/call`/`tool/result`,不传才与旧行为一致。
|
|
25
|
+
* 3. **body 的返回值一律被当成"成功 value"**(`dsh-tools/lib/pipeline.js` 的
|
|
26
|
+
* `createSuccessResult` / `normalizeDispatchResult`):直接 return 一个 `isError:true` 的结果
|
|
27
|
+
* **不会**变成错误——外层仍是成功。所以取到的 result `isError` 时必须**抛 Error**,
|
|
28
|
+
* 由外层统一 materialize 成失败结果。
|
|
29
|
+
* 4. **同层同名注册会冲突**:所以本工具叫 `delegate_task`,不叫 `subagent`,也不能去 shadow 它。
|
|
23
30
|
*
|
|
24
31
|
* @module dsh-submodel/delegation
|
|
25
32
|
*/
|
|
26
33
|
import {
|
|
27
34
|
DELEGATE_TOOL_NAME,
|
|
28
|
-
|
|
35
|
+
modeForRoute,
|
|
29
36
|
resolveCallerRole,
|
|
30
37
|
resolveDelegationArgs,
|
|
31
38
|
} from './policy.js';
|
|
@@ -33,12 +40,23 @@ import {
|
|
|
33
40
|
/** 子调用 callId 的后缀:既能被日志一眼认出,又不会和原生 callId 撞(原生用随机 id)。 */
|
|
34
41
|
const NESTED_CALL_SUFFIX = ':delegate';
|
|
35
42
|
|
|
43
|
+
/** 子调用固定走原生 `subagent` 工具名。 */
|
|
44
|
+
const NESTED_TOOL_NAME = 'subagent';
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* 宿主拒绝显式路由时的追加提示:这是"宿主授权表没配置"的典型症状,不给出路模型只会重试。
|
|
48
|
+
* 与原生错误同语言(英文),并点名两个修复入口。
|
|
49
|
+
*/
|
|
50
|
+
const SESSION_ROUTE_HINT =
|
|
51
|
+
'hint: enable the host subagent model selection setting and add this route to its `allowedModels`, ' +
|
|
52
|
+
'or run `configure_submodel` (or follow the README two-step configuration) to write both the plugin config and the host authorization.';
|
|
53
|
+
|
|
36
54
|
/**
|
|
37
55
|
* 从原生 `subagent` 的工具定义深拷贝参数 schema。
|
|
38
56
|
*
|
|
39
57
|
* **必须是"每次安装时从本轮 native 定义取"**,不能把 schema 写死在源码里:native 定义
|
|
40
|
-
* 会随 DSH 升级增删参数,写死就等于把新参数悄悄丢掉。`parameters`
|
|
41
|
-
*
|
|
58
|
+
* 会随 DSH 升级增删参数,写死就等于把新参数悄悄丢掉。`parameters` 是结构化 JSON Schema
|
|
59
|
+
* (普通对象),可安全深拷贝。
|
|
42
60
|
*
|
|
43
61
|
* @param nativeDefinition - 本会话可见的原生 subagent 工具定义。
|
|
44
62
|
* @returns 参数 schema 深拷贝。
|
|
@@ -60,14 +78,18 @@ export function cloneNativeParameters(nativeDefinition) {
|
|
|
60
78
|
* `hasTopLevelProvider=false` / `hasPropertiesProvider=true`;读顶层会恒为 false,于是本函数
|
|
61
79
|
* 永远返回 undefined、整条安装路径静默跳过(四个工具一个都不出现)。
|
|
62
80
|
*
|
|
81
|
+
* **0.2.2 复验(2026-11-10)**:`provider`/`model` 参数只在 `dsh-tool-subagent` 的
|
|
82
|
+
* `modelSelection:true` 时才加入 schema(Config 默认 false)。Desktop/Web preset 为 spawn
|
|
83
|
+
* 启用该开关,所以本机判据仍然成立;但它是**弱判据**——换了 `toolName` 或关掉
|
|
84
|
+
* `modelSelection` 的部署里会判不出,那时宁可不装。
|
|
85
|
+
*
|
|
63
86
|
* @param tools - 工具注册表(ctx.tools)。
|
|
64
87
|
* @param scope - 观看作用域:**传 agent 对象本身**。宿主自己的调用范式就是这样
|
|
65
|
-
* (`dsh-
|
|
66
|
-
* 的 `this.get(name, agent)`);传 `agent.ctx` 会退化成 global 视图,永远查不到 scoped 工具。
|
|
88
|
+
* (`dsh-tool-subagent/lib/index.js` 的系统提示段也写 `ctx.tools.get(tool.name, context.scope)`)。
|
|
67
89
|
* @returns 原生定义或 undefined。
|
|
68
90
|
*/
|
|
69
91
|
export function findNativeSubagent(tools, scope) {
|
|
70
|
-
const definition = tools.get?.(
|
|
92
|
+
const definition = tools.get?.(NESTED_TOOL_NAME, scope);
|
|
71
93
|
if (definition === undefined) return undefined;
|
|
72
94
|
const properties = definition.parameters?.properties;
|
|
73
95
|
const hasRouteParams = properties?.provider !== undefined && properties?.model !== undefined;
|
|
@@ -75,23 +97,43 @@ export function findNativeSubagent(tools, scope) {
|
|
|
75
97
|
}
|
|
76
98
|
|
|
77
99
|
/**
|
|
78
|
-
*
|
|
100
|
+
* 组装嵌套调用要用的批量输入。**纯计算**,便于单测。
|
|
101
|
+
*
|
|
102
|
+
* 0.2.2 只认批量形状:`calls[].arguments` 是 JSON 文本,返回结果按 `callId` 对回。
|
|
103
|
+
* `toolSet` 只在存在时透传(与当前步的准入集一致;`toolSet.view(scope)` 在 scope 不匹配时
|
|
104
|
+
* 返回空 Map,所以 `agent` 必须是本步 resolve 时的同一个 agent 对象)。
|
|
105
|
+
*
|
|
79
106
|
* @param outerExec - 外层(delegate_task 自己的)exec。
|
|
80
107
|
* @param newArgs - 已解析好的子代参数。
|
|
81
|
-
* @returns `
|
|
108
|
+
* @returns `ToolBatchInput` 片段(可直接交给 `ctx.tools.execute`)。
|
|
82
109
|
*/
|
|
83
110
|
export function buildNestedCall(outerExec, newArgs) {
|
|
84
111
|
return {
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
112
|
+
calls: [
|
|
113
|
+
{
|
|
114
|
+
id: `${outerExec.callId}${NESTED_CALL_SUFFIX}`,
|
|
115
|
+
name: NESTED_TOOL_NAME,
|
|
116
|
+
arguments: JSON.stringify(newArgs),
|
|
117
|
+
},
|
|
118
|
+
],
|
|
89
119
|
agent: outerExec.agent,
|
|
90
|
-
parent: outerExec.token,
|
|
91
120
|
signal: outerExec.signal,
|
|
121
|
+
...(outerExec.toolSet === undefined ? {} : { toolSet: outerExec.toolSet }),
|
|
92
122
|
};
|
|
93
123
|
}
|
|
94
124
|
|
|
125
|
+
/**
|
|
126
|
+
* 从批量结果里取本次子调用的 result。找不到返回 undefined(调用方必须当失败处理)。
|
|
127
|
+
* @param batch - `ctx.tools.execute` 的返回值。
|
|
128
|
+
* @param callId - 本模块派生的子调用 id。
|
|
129
|
+
* @returns 子调用的 result 快照或 undefined。
|
|
130
|
+
*/
|
|
131
|
+
export function resultOfNestedCall(batch, callId) {
|
|
132
|
+
const results = batch?.results;
|
|
133
|
+
if (!Array.isArray(results)) return undefined;
|
|
134
|
+
return results.find((entry) => entry?.callId === callId)?.result;
|
|
135
|
+
}
|
|
136
|
+
|
|
95
137
|
/**
|
|
96
138
|
* 把子的失败结果转成一段**可读诊断**(保留原始信息,不吞)。
|
|
97
139
|
* @param inner - 原生调用返回的结果快照。
|
|
@@ -114,6 +156,29 @@ export function describeInnerFailure(inner) {
|
|
|
114
156
|
if (block?.type === 'text' && typeof block.text === 'string' && block.text !== '') parts.push(block.text);
|
|
115
157
|
}
|
|
116
158
|
if (parts.length === 0) parts.push('the nested subagent call failed without a diagnostic');
|
|
159
|
+
const text = parts.join('\n');
|
|
160
|
+
// 宿主对"未授权路由"的拒绝是 0.2.x 最常见的失败;原样返回会让模型只是重试,补一句修复入口。
|
|
161
|
+
if (text.includes('is not allowed for this Session')) return `${text}\n${SESSION_ROUTE_HINT}`;
|
|
162
|
+
return text;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* 把批量执行器抛出的内部错误转成可读诊断。
|
|
167
|
+
*
|
|
168
|
+
* 0.2.2 的 `executeToolBatch` 只把**调度器内部失败**包成 `ToolBatchExecutionError`,
|
|
169
|
+
* message 固定是 "Tool batch execution failed",根因在 `cause` 里——不把 cause 带出来,
|
|
170
|
+
* 模型就只会看到那句通用文案。
|
|
171
|
+
* @param error - 抛出的错误。
|
|
172
|
+
* @returns 诊断文本。
|
|
173
|
+
*/
|
|
174
|
+
export function describeBatchFailure(error) {
|
|
175
|
+
const parts = [String(error?.message ?? error)];
|
|
176
|
+
let cause = error?.cause;
|
|
177
|
+
for (let depth = 0; cause !== undefined && cause !== null && depth < 3; depth += 1) {
|
|
178
|
+
const message = typeof cause?.message === 'string' ? cause.message : String(cause);
|
|
179
|
+
if (message !== '') parts.push(`caused by: ${message}`);
|
|
180
|
+
cause = cause?.cause;
|
|
181
|
+
}
|
|
117
182
|
return parts.join('\n');
|
|
118
183
|
}
|
|
119
184
|
|
|
@@ -121,13 +186,16 @@ export function describeInnerFailure(inner) {
|
|
|
121
186
|
* 创建 `delegate_task` 的工具定义。
|
|
122
187
|
*
|
|
123
188
|
* @param options - 依赖注入(全部来自 live agent 的 scope):
|
|
124
|
-
* - `ctx`:该 agent 的 runtime context(提供 tools
|
|
189
|
+
* - `ctx`:该 agent 的 runtime context(提供 tools)
|
|
125
190
|
* - `nativeDefinition`:本轮原生 subagent 定义(参数与 output 的来源)
|
|
126
191
|
* - `executor`:配置的执行路由 `{provider, model, reasoningEffort}`
|
|
192
|
+
* - `economyRoutes`:配置的经济路由列表(执行期模式判定用)
|
|
127
193
|
* - `logger`:日志器
|
|
128
194
|
* @returns 可直接 `ctx.tools.register(...)` 的工具定义。
|
|
129
195
|
*/
|
|
130
|
-
export function createDelegateTool({ ctx, nativeDefinition, executor, logger }) {
|
|
196
|
+
export function createDelegateTool({ ctx, nativeDefinition, executor, economyRoutes = [], logger }) {
|
|
197
|
+
/** 执行期模式判定用的运行期策略(与 policy.js 纯函数同口径)。 */
|
|
198
|
+
const routingPolicy = { executor, economyRoutes: Array.isArray(economyRoutes) ? economyRoutes : [] };
|
|
131
199
|
const parameters = cloneNativeParameters(nativeDefinition);
|
|
132
200
|
// output 同样复用原生 schema/render,保持 runId/agentId 与 foreground/continuable 格式一致。
|
|
133
201
|
const output = nativeDefinition.output;
|
|
@@ -138,9 +206,10 @@ export function createDelegateTool({ ctx, nativeDefinition, executor, logger })
|
|
|
138
206
|
return {
|
|
139
207
|
name: DELEGATE_TOOL_NAME,
|
|
140
208
|
description:
|
|
141
|
-
'Delegate one independent task to a subagent
|
|
209
|
+
'Delegate one independent task to a subagent. It starts an independently managed subagent and ' +
|
|
210
|
+
'immediately returns its id; the runtime notifies you when it finishes. ' +
|
|
142
211
|
'Parameters are identical to the native `subagent` tool. Omit provider/model to inherit the ' +
|
|
143
|
-
'current route
|
|
212
|
+
'current route or use the configured execution route. ' +
|
|
144
213
|
'An explicit route must name provider AND model together. ' +
|
|
145
214
|
'Prefer this over the raw `subagent` tool for ordinary delegation.',
|
|
146
215
|
parameters,
|
|
@@ -154,10 +223,11 @@ export function createDelegateTool({ ctx, nativeDefinition, executor, logger })
|
|
|
154
223
|
// ② 同步快照执行期模式,再 await:描述"当前发起这笔调用的主脑",
|
|
155
224
|
// 不读 GUI 里可能中途变化的 pending selection。
|
|
156
225
|
const accepted = caller.header?.config ?? caller.agent.options ?? {};
|
|
157
|
-
const
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
226
|
+
const route = {
|
|
227
|
+
provider: typeof accepted?.provider === 'string' ? accepted.provider : '',
|
|
228
|
+
model: typeof accepted?.model === 'string' ? accepted.model : '',
|
|
229
|
+
};
|
|
230
|
+
const mode = modeForRoute(route, routingPolicy);
|
|
161
231
|
|
|
162
232
|
// ③ 解析最终子代参数(原 exec/arguments 不可变,派生新对象)。
|
|
163
233
|
const resolved = resolveDelegationArgs(args, mode, executor);
|
|
@@ -168,18 +238,32 @@ export function createDelegateTool({ ctx, nativeDefinition, executor, logger })
|
|
|
168
238
|
`route=${resolved.applied} -> ${resolved.args.provider ?? '(inherit)'}/${resolved.args.model ?? '(inherit)'}`,
|
|
169
239
|
);
|
|
170
240
|
|
|
171
|
-
// ④
|
|
241
|
+
// ④ 发起一次真正的原生调用:保留它的全部校验与事件链(0.2.2 批量签名)。
|
|
172
242
|
const nested = buildNestedCall(exec, resolved.args);
|
|
173
|
-
const
|
|
243
|
+
const callId = nested.calls[0].id;
|
|
244
|
+
let batch;
|
|
245
|
+
try {
|
|
246
|
+
batch = await ctx.tools.execute(nested);
|
|
247
|
+
} catch (error) {
|
|
248
|
+
throw new Error(`delegate_task: nested tool batch failed: ${describeBatchFailure(error)}`);
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
const inner = resultOfNestedCall(batch, callId);
|
|
252
|
+
if (inner === undefined) {
|
|
253
|
+
throw new Error(
|
|
254
|
+
'delegate_task: the nested `subagent` call produced no result (the batch was aborted, ' +
|
|
255
|
+
'or the tool was not admitted for this step)',
|
|
256
|
+
);
|
|
257
|
+
}
|
|
174
258
|
|
|
175
|
-
// ⑤ inner.isError 必须**抛出**:body 返回 error 对象会被当成功 value
|
|
176
|
-
if (inner
|
|
259
|
+
// ⑤ inner.isError 必须**抛出**:body 返回 error 对象会被当成功 value(见文件头契约 3)。
|
|
260
|
+
if (inner.isError === true) {
|
|
177
261
|
throw new Error(describeInnerFailure(inner));
|
|
178
262
|
}
|
|
179
263
|
|
|
180
264
|
// ⑥ 转移延迟上下文 / 结论标记,再只返回 inner.value。
|
|
181
|
-
for (const context of inner
|
|
182
|
-
if (inner
|
|
265
|
+
for (const context of inner.additionalContexts ?? []) exec.deferContext(context);
|
|
266
|
+
if (inner.concludesTurn === true) exec.concludeTurn();
|
|
183
267
|
|
|
184
268
|
return inner.value;
|
|
185
269
|
},
|
package/lib/index.js
CHANGED
|
@@ -44,11 +44,13 @@ import {
|
|
|
44
44
|
ROUTING_ORDER,
|
|
45
45
|
ROUTING_SECTION,
|
|
46
46
|
childDelegationRefusal,
|
|
47
|
-
|
|
47
|
+
isEconomyRoute,
|
|
48
48
|
roleOf,
|
|
49
|
+
routeFromAssembly,
|
|
49
50
|
routingTextFor,
|
|
50
51
|
} from './policy.js';
|
|
51
52
|
import { createDelegateTool, findNativeSubagent } from './delegation.js';
|
|
53
|
+
import { createSetupTool } from './setup.js';
|
|
52
54
|
|
|
53
55
|
export const name = 'dsh-submodel';
|
|
54
56
|
|
|
@@ -92,10 +94,23 @@ export const Config = z.object({
|
|
|
92
94
|
child: z.string().default(''),
|
|
93
95
|
}).default({ economize: '', boss: '', child: '' }),
|
|
94
96
|
/**
|
|
95
|
-
* 是否安装"
|
|
97
|
+
* 是否安装"非经济路由顶层用原生 subagent 时必须给完整显式 pair"的 guard。
|
|
96
98
|
* 默认开启;关掉后原生 subagent 的行为完全交给宿主。
|
|
97
99
|
*/
|
|
98
100
|
guardNativeSubagent: z.boolean().default(true),
|
|
101
|
+
/**
|
|
102
|
+
* 自动配置开关:root 且 `executor` 未配置时注册 `configure_submodel` 向导
|
|
103
|
+
* (探测宿主渠道/模型 → 一张卡确认 → 写插件配置 + 宿主授权表)。默认开启。
|
|
104
|
+
*/
|
|
105
|
+
autoConfigure: z.boolean().default(true),
|
|
106
|
+
/**
|
|
107
|
+
* 经济路由列表:主模型命中其中一项即走 economize 模式(不再要求模型名以 flash 结尾)。
|
|
108
|
+
* 每项 `{provider?, model}`;`provider` 为空时只比 model。为空数组时保留 flash 后缀兼容。
|
|
109
|
+
*/
|
|
110
|
+
economyRoutes: z.array(z.object({
|
|
111
|
+
provider: z.string().default(''),
|
|
112
|
+
model: z.string().min(1),
|
|
113
|
+
})).default([]),
|
|
99
114
|
});
|
|
100
115
|
|
|
101
116
|
/** 判断一个 agent 是否属于本插件负责的 composition。 */
|
|
@@ -126,9 +141,25 @@ export function apply(ctx, config) {
|
|
|
126
141
|
boss: config.texts?.boss ?? '',
|
|
127
142
|
child: config.texts?.child ?? '',
|
|
128
143
|
};
|
|
144
|
+
const economyRoutes = Array.isArray(config.economyRoutes)
|
|
145
|
+
? config.economyRoutes
|
|
146
|
+
.filter(
|
|
147
|
+
(row) =>
|
|
148
|
+
row !== null &&
|
|
149
|
+
typeof row === 'object' &&
|
|
150
|
+
typeof row.model === 'string' &&
|
|
151
|
+
row.model !== '',
|
|
152
|
+
)
|
|
153
|
+
.map((row) => ({ provider: typeof row.provider === 'string' ? row.provider : '', model: row.model }))
|
|
154
|
+
: [];
|
|
129
155
|
const guardNativeSubagent = config.guardNativeSubagent !== false;
|
|
156
|
+
const autoConfigure = config.autoConfigure !== false;
|
|
157
|
+
/** 模式判定用的运行期策略(与 policy.js 纯函数同口径)。 */
|
|
158
|
+
const routingPolicy = { executor, economyRoutes };
|
|
130
159
|
/** 每次 assembly 现渲染正文用的运行期上下文(纯函数层只认它)。 */
|
|
131
|
-
const renderContext = { executor, experts, texts };
|
|
160
|
+
const renderContext = { executor, experts, texts, economyRoutes };
|
|
161
|
+
/** 执行路由是否未配置(未配置时才注册自动配置向导)。 */
|
|
162
|
+
const executorIncomplete = executor.provider === '' || executor.model === '';
|
|
132
163
|
|
|
133
164
|
/** 每个 agent 的 assembly listener 装的文本解析器:`(resolved) => string|null`。 */
|
|
134
165
|
const textResolvers = new WeakMap();
|
|
@@ -148,8 +179,10 @@ export function apply(ctx, config) {
|
|
|
148
179
|
}
|
|
149
180
|
|
|
150
181
|
// ── fail-fast(只记日志、绝不抛) ──────────────────────────────────────────
|
|
151
|
-
if (
|
|
152
|
-
ctx.logger.error(
|
|
182
|
+
if (executorIncomplete) {
|
|
183
|
+
ctx.logger.error(
|
|
184
|
+
'dsh-submodel: config.executor.provider/model 未配置,自动委派不可用;可调用 `configure_submodel` 自动配置或按 README 两步配置',
|
|
185
|
+
);
|
|
153
186
|
}
|
|
154
187
|
ctx.logger.info(
|
|
155
188
|
`dsh-submodel: 自动策略已启用(preset=${presetId}, executor=${executor.provider || '(未配置)'}/${executor.model || '(未配置)'}, experts=${experts.length})`,
|
|
@@ -235,12 +268,15 @@ export function apply(ctx, config) {
|
|
|
235
268
|
// 既对该 agent 可见,也随 agent 一起释放。
|
|
236
269
|
const tools = agent.ctx.tools;
|
|
237
270
|
|
|
271
|
+
// 角色只在安装时判定一次:它来自 durable session header,agent 生命周期内不变。
|
|
272
|
+
// 改读 agent(而不是已作废的 EpochHeader)是为了修复"冷恢复子代永远判成 root"的缺陷。
|
|
273
|
+
const callerRole = roleOf(agent);
|
|
274
|
+
|
|
238
275
|
// ① 角色 guard:子代硬边界。**返回字符串即拒绝**(不是抛异常)。
|
|
239
276
|
const guard = (exec) => {
|
|
240
277
|
if (exec?.agent !== agent) return undefined;
|
|
241
278
|
const toolCallName = exec?.name;
|
|
242
279
|
if (typeof toolCallName !== 'string') return undefined;
|
|
243
|
-
const callerRole = roleOf(agent.session?.requestHeader?.(), agent.options);
|
|
244
280
|
if (callerRole !== 'child') return undefined;
|
|
245
281
|
const forbidden = CHILD_FORBIDDEN_TOOLS.includes(toolCallName) || toolCallName.startsWith('subagent');
|
|
246
282
|
if (!forbidden) return undefined;
|
|
@@ -248,15 +284,14 @@ export function apply(ctx, config) {
|
|
|
248
284
|
};
|
|
249
285
|
disposers.push(tools.guard(guard));
|
|
250
286
|
|
|
251
|
-
// ②
|
|
287
|
+
// ② 执行期硬边界的补充:非经济路由顶层用原生 subagent 但没给完整显式 pair 时,
|
|
252
288
|
// 拒绝并指向 delegate_task,防止模型绕开默认执行路由。
|
|
253
289
|
const nativeGuard = (exec) => {
|
|
254
290
|
if (exec?.agent !== agent) return undefined;
|
|
255
291
|
if (exec?.name !== 'subagent') return undefined;
|
|
256
|
-
const callerRole = roleOf(agent.session?.requestHeader?.(), agent.options);
|
|
257
292
|
if (callerRole !== 'root') return undefined;
|
|
258
|
-
|
|
259
|
-
if (
|
|
293
|
+
// 执行期模式判定:取最新已接受 header 的 config,回退 agent.options。
|
|
294
|
+
if (isEconomyRoute(routeFromAgent(agent), routingPolicy)) return undefined; // 经济路由顶层原生调用不改变行为
|
|
260
295
|
const args = exec.arguments ?? {};
|
|
261
296
|
const hasProvider = args.provider !== undefined && args.provider !== null && args.provider !== '';
|
|
262
297
|
const hasModel = args.model !== undefined && args.model !== null && args.model !== '';
|
|
@@ -288,19 +323,30 @@ export function apply(ctx, config) {
|
|
|
288
323
|
disposers.push(scopedCtx.on('system-prompt/assemble', listener));
|
|
289
324
|
|
|
290
325
|
// ④ 只有顶层注册统一委派入口(子代不再有委派能力)。
|
|
291
|
-
const callerRole = roleOf(agent.session?.requestHeader?.(), agent.options);
|
|
292
326
|
if (callerRole === 'root') {
|
|
293
327
|
disposers.push(
|
|
294
|
-
tools.register(
|
|
328
|
+
tools.register(
|
|
329
|
+
createDelegateTool({
|
|
330
|
+
ctx: agent.ctx,
|
|
331
|
+
nativeDefinition,
|
|
332
|
+
executor,
|
|
333
|
+
economyRoutes,
|
|
334
|
+
logger: ctx.logger,
|
|
335
|
+
}),
|
|
336
|
+
),
|
|
295
337
|
);
|
|
338
|
+
// ⑤ executor 未配置时注册自动配置向导(已配置/子代/autoConfigure=false 都不注册)。
|
|
339
|
+
// 向导走 agent.ctx.tools,随 agent 释放;它内部用插件顶层 ctx 取服务。
|
|
340
|
+
if (autoConfigure && executorIncomplete) {
|
|
341
|
+
disposers.push(tools.register(createSetupTool({ ctx, config, logger: ctx.logger })));
|
|
342
|
+
}
|
|
296
343
|
}
|
|
297
344
|
|
|
298
|
-
//
|
|
345
|
+
// 正文解析器:角色安装时固定,模式每次 assembly 现判。
|
|
299
346
|
// `list_subagent_models` 不可见时**只门控正文段**(info 一次/agent),
|
|
300
347
|
// 工具仍按上面的注册门控安装。
|
|
301
348
|
textResolvers.set(agent, (resolved) => {
|
|
302
|
-
|
|
303
|
-
if (role === 'child') return routingTextFor('child', '', renderContext);
|
|
349
|
+
if (callerRole === 'child') return routingTextFor('child', undefined, renderContext);
|
|
304
350
|
if (ctx.tools.get(toolName, agent) === undefined) {
|
|
305
351
|
if (!informedNoModelSelection.has(agent)) {
|
|
306
352
|
informedNoModelSelection.add(agent);
|
|
@@ -310,7 +356,7 @@ export function apply(ctx, config) {
|
|
|
310
356
|
}
|
|
311
357
|
return null;
|
|
312
358
|
}
|
|
313
|
-
return routingTextFor('root',
|
|
359
|
+
return routingTextFor('root', resolveAssemblyRoute(ctx, agent, resolved), renderContext);
|
|
314
360
|
});
|
|
315
361
|
|
|
316
362
|
installed.set(agent, { disposers });
|
|
@@ -412,39 +458,45 @@ export function apply(ctx, config) {
|
|
|
412
458
|
}
|
|
413
459
|
|
|
414
460
|
/**
|
|
415
|
-
* 解析"本次 assembly
|
|
461
|
+
* 解析"本次 assembly 用的路由"。
|
|
416
462
|
*
|
|
417
463
|
* 顺序(必须对齐 GUI 即将发起的那笔请求,不能用过期的创建期值):
|
|
418
|
-
* 1.
|
|
419
|
-
*
|
|
420
|
-
* 2. `
|
|
421
|
-
* 即创建期值,仅作回退);
|
|
422
|
-
* 3. `agent.options.model`——最后的兜底。
|
|
464
|
+
* 1. `assembly.variables.provider` / `assembly.variables.model`——宿主 agent-loop 注册的
|
|
465
|
+
* 提示词变量,反映本次请求的路由;
|
|
466
|
+
* 2. `agent.options.provider` / `agent.options.model`——创建期值,仅作回退(例如 bundle 未注册变量)。
|
|
423
467
|
*
|
|
424
|
-
*
|
|
468
|
+
* 两处都拿不到 model 时**明确报错**,不猜模式(猜错会把整段策略换成另一套且不报错)。
|
|
469
|
+
* 注意这里**不读** `sessionController.agents.selectionFor`:DSH 0.2.2 没有该 service(死通道已删)。
|
|
425
470
|
*
|
|
426
|
-
* @param ctx - 插件顶层 ctx
|
|
471
|
+
* @param ctx - 插件顶层 ctx(保留参数位,当前实现不需要)。
|
|
427
472
|
* @param agent - 目标 agent。
|
|
428
473
|
* @param assembly - 已完成 next() 的 assembly。
|
|
429
|
-
* @returns
|
|
474
|
+
* @returns `{provider, model}`。
|
|
430
475
|
*/
|
|
431
|
-
function
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
476
|
+
function resolveAssemblyRoute(ctx, agent, assembly) {
|
|
477
|
+
void ctx;
|
|
478
|
+
const variables = assembly?.variables;
|
|
479
|
+
const options = agent?.options ?? {};
|
|
480
|
+
const provider = typeof variables?.provider === 'string' ? variables.provider : options.provider;
|
|
481
|
+
const fromAssembly = typeof variables?.model === 'string' && variables.model !== '' ? variables.model : undefined;
|
|
482
|
+
const model = fromAssembly ?? options.model;
|
|
483
|
+
if (typeof model !== 'string' || model === '') {
|
|
484
|
+
// 两处都没有 model:走严格版本拿统一报错文案(不猜模式)。
|
|
485
|
+
return routeFromAssembly(assembly);
|
|
438
486
|
}
|
|
439
|
-
|
|
440
|
-
if (fromAssembly !== undefined) return fromAssembly;
|
|
441
|
-
const fromOptions = agent?.options?.model;
|
|
442
|
-
if (typeof fromOptions === 'string' && fromOptions !== '') return fromOptions;
|
|
443
|
-
return modelFromAssembly(assembly); // 抛出明确配置错误
|
|
487
|
+
return { provider: typeof provider === 'string' ? provider : '', model };
|
|
444
488
|
}
|
|
445
489
|
|
|
446
|
-
/**
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
490
|
+
/**
|
|
491
|
+
* 执行期取"正在发起这笔工具调用的主脑路由":`session.requestHeader().config` 优先,
|
|
492
|
+
* `agent.options` 兜底。取不到 model 时返回空路由(守卫按 boss 处理,不抛)。
|
|
493
|
+
* @param agent - live agent。
|
|
494
|
+
* @returns `{provider, model}`(字段缺失时为空串/undefined)。
|
|
495
|
+
*/
|
|
496
|
+
function routeFromAgent(agent) {
|
|
497
|
+
const accepted = agent?.session?.requestHeader?.()?.config ?? agent?.options ?? {};
|
|
498
|
+
return {
|
|
499
|
+
provider: typeof accepted?.provider === 'string' ? accepted.provider : '',
|
|
500
|
+
model: typeof accepted?.model === 'string' ? accepted.model : '',
|
|
501
|
+
};
|
|
450
502
|
}
|