dsh-submodel 0.0.0-stage → 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/LICENSE +21 -0
- package/README.en.md +249 -0
- package/README.md +228 -2
- package/cordis.patch.yml +3 -0
- package/lib/delegation.js +271 -0
- package/lib/index.js +502 -0
- package/lib/policy.js +683 -0
- package/lib/setup.js +253 -0
- package/package.json +50 -4
package/lib/setup.js
ADDED
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* dsh-submodel / setup.js —— 自动配置向导 `configure_submodel`。
|
|
3
|
+
*
|
|
4
|
+
* ## 为什么是「探测 + 一张确认卡」而不是纯自动
|
|
5
|
+
*
|
|
6
|
+
* 宿主 `llm` 服务的模型元数据只有 `id/name/description/context`,**没有价格字段**
|
|
7
|
+
* (`resolveModelInfo` 的返回里也没有)。纯自动只能靠模型名猜贵贱,开源后一定会把活
|
|
8
|
+
* 派错模型,而且不报错。所以这里把「探测」与「决定」拆开:本模块探测候选并预填顺序,
|
|
9
|
+
* 用户点一次卡完成选择,同时把花钱风险交回用户。
|
|
10
|
+
*
|
|
11
|
+
* ## 为什么必须连宿主授权表一起写
|
|
12
|
+
*
|
|
13
|
+
* `dsh-tool-subagent` 在会话没有记录 model-selection policy 时只允许父路由;不写
|
|
14
|
+
* `allowedModels`,执行路由与专家路由第一次调用就报
|
|
15
|
+
* `child LLM route "…" is not allowed for this Session`——这是 README「5 行配置」不完整的根因。
|
|
16
|
+
* 写入走官方 `configEditor`(带锁、原子写、失败回滚),不自改 YAML、不建私有状态文件。
|
|
17
|
+
*
|
|
18
|
+
* ## 写入顺序:先宿主、后插件
|
|
19
|
+
*
|
|
20
|
+
* 宿主字段是 `.volatile()`,写入即时生效且不重载插件;插件配置写入会重载本插件并销毁
|
|
21
|
+
* 正在执行的本工具注册。因此宿主授权先写、插件配置最后写,返回文案在写之前拼好。
|
|
22
|
+
* 写成功后本工具实例会被卸载——属预期,不是失败。
|
|
23
|
+
*
|
|
24
|
+
* ## 失败降级
|
|
25
|
+
*
|
|
26
|
+
* 任何宿主服务缺失/异常(llm、userQuestions、configEditor、宿主授权行)都**不抛**,
|
|
27
|
+
* 降级为「未写入原因 + 手动两步 YAML」返回给模型;只有非 root 调用才抛错。
|
|
28
|
+
*
|
|
29
|
+
* @module dsh-submodel/setup
|
|
30
|
+
*/
|
|
31
|
+
import {
|
|
32
|
+
HOST_SELECTION_ENTRY_ID,
|
|
33
|
+
SETUP_TOOL_NAME,
|
|
34
|
+
buildSetupQuestions,
|
|
35
|
+
composeNextHostConfig,
|
|
36
|
+
composeNextPluginConfig,
|
|
37
|
+
deriveSetupCandidates,
|
|
38
|
+
parseSetupAnswers,
|
|
39
|
+
roleOf,
|
|
40
|
+
} from './policy.js';
|
|
41
|
+
|
|
42
|
+
/** 本插件在 profile 补丁里的配置行 id(cordis.patch.yml 的 insert 行)。 */
|
|
43
|
+
const PLUGIN_ENTRY_ID = 'submodel';
|
|
44
|
+
|
|
45
|
+
/** 路由的展示文本。 */
|
|
46
|
+
function routeText(route) {
|
|
47
|
+
return `${route.provider}/${route.model}`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** 自动配置不可用时给用户的手动两步配置(示例路由零本机信息)。 */
|
|
51
|
+
function manualConfigText() {
|
|
52
|
+
return [
|
|
53
|
+
'请在 profile 补丁层(cordis.patch.yml)手动完成两步配置,然后新开会话:',
|
|
54
|
+
'',
|
|
55
|
+
'① 宿主授权(加到已有行或新增该行):',
|
|
56
|
+
'- id: subagent-model-selection-settings',
|
|
57
|
+
' config:',
|
|
58
|
+
' enabled: true',
|
|
59
|
+
' allowedModels:',
|
|
60
|
+
' - { provider: your-provider, model: your-model }',
|
|
61
|
+
'',
|
|
62
|
+
'② 本插件配置:',
|
|
63
|
+
'- id: submodel',
|
|
64
|
+
' config:',
|
|
65
|
+
' executor:',
|
|
66
|
+
' provider: your-provider',
|
|
67
|
+
' model: your-model',
|
|
68
|
+
' reasoningEffort: max',
|
|
69
|
+
' experts: []',
|
|
70
|
+
' economyRoutes:',
|
|
71
|
+
' - { provider: your-provider, model: your-model }',
|
|
72
|
+
].join('\n');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* 收集宿主当前可用的 provider/model。
|
|
77
|
+
* 单个 provider 的 `listModels` 失败只跳过该 provider,不影响整体(best-effort)。
|
|
78
|
+
* @param llm - 宿主 llm 服务。
|
|
79
|
+
* @returns `[{provider, model, name, description}]`。
|
|
80
|
+
*/
|
|
81
|
+
async function collectRoutes(llm) {
|
|
82
|
+
const pack = [];
|
|
83
|
+
let providers;
|
|
84
|
+
try {
|
|
85
|
+
providers = llm.listProviders();
|
|
86
|
+
} catch {
|
|
87
|
+
return pack;
|
|
88
|
+
}
|
|
89
|
+
for (const provider of Array.isArray(providers) ? providers : []) {
|
|
90
|
+
const providerId = provider?.id;
|
|
91
|
+
if (typeof providerId !== 'string' || providerId === '') continue;
|
|
92
|
+
let models;
|
|
93
|
+
try {
|
|
94
|
+
models = await llm.listModels(providerId);
|
|
95
|
+
} catch {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
for (const model of Array.isArray(models) ? models : []) {
|
|
99
|
+
if (typeof model?.id !== 'string' || model.id === '') continue;
|
|
100
|
+
pack.push({
|
|
101
|
+
provider: providerId,
|
|
102
|
+
model: model.id,
|
|
103
|
+
name: typeof model.name === 'string' ? model.name : '',
|
|
104
|
+
description: typeof model.description === 'string' ? model.description : '',
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return pack;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* 创建 `configure_submodel` 工具定义。
|
|
113
|
+
*
|
|
114
|
+
* @param options - 依赖注入:
|
|
115
|
+
* - `ctx`:**插件顶层 ctx**(用 `ctx.get(...)` 取服务、`ctx.fiber.entry` 定位本插件配置行);
|
|
116
|
+
* - `config`:插件原始配置(保留字段时用,允许为空);
|
|
117
|
+
* - `logger`:日志器。
|
|
118
|
+
* @returns 可直接 `agent.ctx.tools.register(...)` 的工具定义。
|
|
119
|
+
*/
|
|
120
|
+
export function createSetupTool({ ctx, config, logger }) {
|
|
121
|
+
void config; // 写回值时以 configEditor 的当前原始配置为基准,不重新拼整个 config。
|
|
122
|
+
/** 同一 agent 的并发保护:正在提问时第二次调用直接返回,不重复弹卡。 */
|
|
123
|
+
let asking = false;
|
|
124
|
+
|
|
125
|
+
/** 真正执行:所有失败降级为文本,不抛(非 root 调用由外层抛)。 */
|
|
126
|
+
async function run(exec) {
|
|
127
|
+
const signal = exec?.signal;
|
|
128
|
+
const llm = ctx.get('llm');
|
|
129
|
+
if (llm === undefined || llm === null) {
|
|
130
|
+
return `configure_submodel: 当前宿主没有可用的 llm 服务,无法自动探测路由,未写入任何配置。\n\n${manualConfigText()}`;
|
|
131
|
+
}
|
|
132
|
+
const userQuestions = ctx.get('userQuestions');
|
|
133
|
+
if (userQuestions === undefined || userQuestions === null) {
|
|
134
|
+
return `configure_submodel: 当前宿主没有提问服务(纯 CLI 会话常见),无法弹卡,未写入任何配置。\n\n${manualConfigText()}`;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
// ① 探测候选(best-effort;空则引导用户先配置渠道)。
|
|
138
|
+
const pack = await collectRoutes(llm);
|
|
139
|
+
if (pack.length === 0) {
|
|
140
|
+
return `configure_submodel: 还没有可用的渠道/模型(或它们全部无法列出)。请先在设置里配置渠道与密钥后重试。\n\n${manualConfigText()}`;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// ② 宿主当前授权状态(best-effort,仅用于候选排序与预填)。
|
|
144
|
+
let host;
|
|
145
|
+
try {
|
|
146
|
+
host = ctx.get('subagentModelSelection')?.current?.();
|
|
147
|
+
} catch {
|
|
148
|
+
host = undefined;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// ③ 一张卡问三题:执行路由 / 专家路由 / 是否写宿主授权。
|
|
152
|
+
const candidates = deriveSetupCandidates(pack, host);
|
|
153
|
+
const questions = buildSetupQuestions(candidates);
|
|
154
|
+
let answer;
|
|
155
|
+
try {
|
|
156
|
+
answer = await userQuestions.ask({ questions, agent: exec?.agent, signal });
|
|
157
|
+
} catch (error) {
|
|
158
|
+
const detail = error?.code ?? error?.message ?? String(error);
|
|
159
|
+
return `configure_submodel: 未能完成提问(${String(detail)}),未写入任何配置。\n\n${manualConfigText()}`;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// ④ 解析回答:selected 必须来自候选;custom 只接受 provider/model,且要过 llm 存在性校验。
|
|
163
|
+
const parsed = parseSetupAnswers(answer?.answers, candidates);
|
|
164
|
+
if (!parsed.ok) {
|
|
165
|
+
return `configure_submodel: 回答无效(${parsed.reason}),未写入任何配置。\n\n${manualConfigText()}`;
|
|
166
|
+
}
|
|
167
|
+
for (const route of parsed.customRoutes) {
|
|
168
|
+
try {
|
|
169
|
+
await llm.resolveModelInfo(route.provider, route.model, signal);
|
|
170
|
+
} catch {
|
|
171
|
+
return `configure_submodel: 自定义路由 ${routeText(route)} 在宿主里不存在,未写入任何配置。\n\n${manualConfigText()}`;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// ⑤ 定位本插件配置行(写插件配置会在成功后卸载本工具实例,属预期)。
|
|
176
|
+
const editor = ctx.get('configEditor');
|
|
177
|
+
if (editor === undefined || editor === null) {
|
|
178
|
+
return `configure_submodel: 当前宿主没有配置编辑器(非 GUI / 只读 profile),无法自动写配置。\n\n${manualConfigText()}`;
|
|
179
|
+
}
|
|
180
|
+
let entries;
|
|
181
|
+
try {
|
|
182
|
+
entries = editor.entries();
|
|
183
|
+
} catch {
|
|
184
|
+
entries = undefined;
|
|
185
|
+
}
|
|
186
|
+
if (!Array.isArray(entries)) {
|
|
187
|
+
return `configure_submodel: 读取 profile 配置失败,未写入任何配置。\n\n${manualConfigText()}`;
|
|
188
|
+
}
|
|
189
|
+
const entry = ctx.fiber?.entry ?? entries.find((row) => row?.options?.id === PLUGIN_ENTRY_ID);
|
|
190
|
+
if (entry === undefined) {
|
|
191
|
+
return `configure_submodel: 找不到本插件(id=${PLUGIN_ENTRY_ID})的配置行,未写入任何配置。\n\n${manualConfigText()}`;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// ⑥ 先写宿主授权(volatile、即时生效、不重载);失败只记录并继续写插件配置。
|
|
195
|
+
const hostRoutes = [parsed.executor, ...parsed.experts];
|
|
196
|
+
let hostNote;
|
|
197
|
+
if (!parsed.authorize) {
|
|
198
|
+
hostNote = '宿主授权未写入(按你的选择)';
|
|
199
|
+
} else {
|
|
200
|
+
const hostEntry = entries.find((row) => row?.options?.id === HOST_SELECTION_ENTRY_ID);
|
|
201
|
+
if (hostEntry === undefined) {
|
|
202
|
+
hostNote = '宿主授权行不存在,请按 README 手动添加该行';
|
|
203
|
+
} else {
|
|
204
|
+
try {
|
|
205
|
+
await editor.edit(hostEntry, (current) => composeNextHostConfig(current, hostRoutes));
|
|
206
|
+
hostNote = '宿主授权已写入(只对新会话生效)';
|
|
207
|
+
} catch (error) {
|
|
208
|
+
logger?.warn?.(`dsh-submodel: configure_submodel 写宿主授权失败: ${String(error)}`);
|
|
209
|
+
hostNote = `宿主授权写入失败(${String(error?.message ?? error)}),请按 README 手动添加该行`;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// ⑦ 返回文案在写插件配置**之前**拼好:写入会卸载本实例,写后可能拿不到执行权。
|
|
215
|
+
const summary =
|
|
216
|
+
`已写入执行路由 ${routeText(parsed.executor)}、${parsed.experts.length} 条专家路由、` +
|
|
217
|
+
`economyRoutes=[${routeText(parsed.executor)}];${hostNote}。新会话生效(宿主授权只对新会话生效)。`;
|
|
218
|
+
|
|
219
|
+
// ⑧ 再写插件配置(触发本插件重载,放最后)。
|
|
220
|
+
try {
|
|
221
|
+
await editor.edit(entry, (current) => composeNextPluginConfig(current, parsed));
|
|
222
|
+
} catch (error) {
|
|
223
|
+
logger?.warn?.(`dsh-submodel: configure_submodel 写插件配置失败: ${String(error)}`);
|
|
224
|
+
return `configure_submodel: 插件配置写入失败(${String(error?.message ?? error)});${hostNote}。\n\n${manualConfigText()}`;
|
|
225
|
+
}
|
|
226
|
+
return summary;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
return {
|
|
230
|
+
name: SETUP_TOOL_NAME,
|
|
231
|
+
description:
|
|
232
|
+
'Configure subagent routing by asking the user once: pick the executor route, pick expert routes, ' +
|
|
233
|
+
'and optionally authorize those routes in the host subagent model-selection table. ' +
|
|
234
|
+
'Only available while no executor route is configured.',
|
|
235
|
+
parameters: {},
|
|
236
|
+
output: {
|
|
237
|
+
schema: { type: 'string' },
|
|
238
|
+
render: (_args, value) => [{ type: 'text', text: String(value) }],
|
|
239
|
+
},
|
|
240
|
+
async execute(_args, exec) {
|
|
241
|
+
if (roleOf(exec?.agent) !== 'root') {
|
|
242
|
+
throw new Error('configure_submodel is only available to a top-level agent (delegated subagents cannot configure routing)');
|
|
243
|
+
}
|
|
244
|
+
if (asking) return 'configure_submodel: 配置进行中,请先回答当前弹卡;不要重复调用。';
|
|
245
|
+
asking = true;
|
|
246
|
+
try {
|
|
247
|
+
return await run(exec);
|
|
248
|
+
} finally {
|
|
249
|
+
asking = false;
|
|
250
|
+
}
|
|
251
|
+
},
|
|
252
|
+
};
|
|
253
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,52 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-submodel",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "官方 subagent 的默认策略包:装上一次配好「什么时候自己干、什么时候派出去、派给谁」。",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "lib/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": "./lib/index.js",
|
|
9
|
+
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
10
|
+
"./package.json": "./package.json"
|
|
11
|
+
},
|
|
12
|
+
"scripts": {
|
|
13
|
+
"test": "npm run test:node && npm run test:python",
|
|
14
|
+
"test:node": "node tests/unit/flash-routing-fixture.mjs ./lib && node tests/unit/public-profile-smoke.mjs",
|
|
15
|
+
"test:python": "python -m unittest discover -s tests/unit -v"
|
|
16
|
+
},
|
|
17
|
+
"license": "MIT",
|
|
18
|
+
"repository": {
|
|
19
|
+
"type": "git",
|
|
20
|
+
"url": "git+https://github.com/angel291592/dsh-submodel.git"
|
|
21
|
+
},
|
|
22
|
+
"dsh": {
|
|
23
|
+
"bundle": {
|
|
24
|
+
"patch": "./cordis.patch.yml"
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"engines": {
|
|
28
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0"
|
|
29
|
+
},
|
|
30
|
+
"peerDependencies": {
|
|
31
|
+
"@deepseek-ai/schemastery": ">=3.18.4 <4.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@deepseek-ai/schemastery": "^3.19.0"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"lib",
|
|
38
|
+
"cordis.patch.yml",
|
|
39
|
+
"README.md",
|
|
40
|
+
"README.en.md",
|
|
41
|
+
"LICENSE"
|
|
42
|
+
],
|
|
43
|
+
"keywords": [
|
|
44
|
+
"dsh",
|
|
45
|
+
"dsh-plugin",
|
|
46
|
+
"deepseek-harness",
|
|
47
|
+
"subagent",
|
|
48
|
+
"delegation",
|
|
49
|
+
"policy",
|
|
50
|
+
"model-routing"
|
|
51
|
+
]
|
|
52
|
+
}
|