dsh-submodel 0.1.0 → 0.3.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 +304 -0
- package/README.md +205 -28
- package/lib/delegation.js +123 -39
- package/lib/index.js +137 -42
- package/lib/policy.js +407 -36
- package/lib/setup.js +253 -0
- package/lib/team-fit.js +23 -0
- package/package.json +12 -3
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/lib/team-fit.js
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/** 只给启用团队的目标预设主脑看的提示词段。 */
|
|
2
|
+
export const TEAM_FIT_SECTION = 'tool:team-fit';
|
|
3
|
+
|
|
4
|
+
const TEAM_FIT_TEXT = `## 团队队形自检(本会话已开启 Agent Teams)
|
|
5
|
+
|
|
6
|
+
第一次收到实际任务时,由你决定是否并行、开几个队友、每个用什么模型。问候不触发。动手或调工具前,先用下面三个行首向用户报告,每行一个结论,不加标题、编号或空行,然后执行:
|
|
7
|
+
可并行性:能否拆出 2–4 个写域互斥、可各自验证的块?给出依据。
|
|
8
|
+
串行点:指出共用文件、契约、数据库、迁移号、CI、提交权或需用户裁决的环节,没有就写“无”。
|
|
9
|
+
决定:串行时原样写“本次串行,不用任务板”。并行时写“并行 N 个队友”,在同一行逐块写清工作、负责人、写域(只读也注明)、具体模型、验收方法和依赖。
|
|
10
|
+
沿用已给出的决定,只有任务范围或写域改变才重判。普通跟进、进度查询和重试不重复这三行。
|
|
11
|
+
|
|
12
|
+
同一文件或契约上的连续修改、只能做一次的全量验证或真实跑批、写域切不开、须先裁决预算或线上操作、协调成本不低于收益时,选串行。串行由主脑自己做,不建任务、不开子代理。至少两个块可以独立取证或修改不同文件,并能各自验证时才并行。在共享任务板写明每块的验收条件、负责人和 write_scopes,有先后关系填 blocked_by。只启动已就绪的队友,按工具返回的名字认领或分配。
|
|
13
|
+
|
|
14
|
+
模型分配沿用本会话的“模型路由”,主脑保持当前模型。实施、取证、测试、文档等执行块用 delegate_task,不传 provider/model(Flash 继承当前路由,强主脑走配置的执行档)。独立审查使用全新上下文,不用 subagent_fork,只给计划指针、相关差异和验收标准。审查默认同样走执行档。遇到无法自证的复杂判断或执行遇到瓶颈,才按现有阶梯成对指定 provider/model 并说明理由,不因审查或风险标签直接跳到最高档。
|
|
15
|
+
|
|
16
|
+
派工时把边界写进队友任务:只在分配的写域内工作并交回定向测试或原始证据,不得 git commit/push,不改共享契约、迁移号、状态页、索引或 CI。共享文件、集成、提交和最终验证由主脑独占。`;
|
|
17
|
+
|
|
18
|
+
/** 身份、预设或团队状态未确定时,空正文由宿主渲染器丢弃。 */
|
|
19
|
+
export function teamFitTextFor({ role, preset, presetId, teamEnabled } = {}) {
|
|
20
|
+
if (role !== 'root' || teamEnabled !== true) return '';
|
|
21
|
+
if (typeof presetId !== 'string' || presetId.trim() === '' || preset !== presetId) return '';
|
|
22
|
+
return TEAM_FIT_TEXT;
|
|
23
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "dsh-submodel",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "官方 subagent 的默认策略包:装上一次配好「什么时候自己干、什么时候派出去、派给谁」。",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "lib/index.js",
|
|
@@ -9,6 +9,11 @@
|
|
|
9
9
|
"./cordis.patch.yml": "./cordis.patch.yml",
|
|
10
10
|
"./package.json": "./package.json"
|
|
11
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
|
+
},
|
|
12
17
|
"license": "MIT",
|
|
13
18
|
"repository": {
|
|
14
19
|
"type": "git",
|
|
@@ -20,15 +25,19 @@
|
|
|
20
25
|
}
|
|
21
26
|
},
|
|
22
27
|
"engines": {
|
|
23
|
-
"dsh": ">=0.2.
|
|
28
|
+
"dsh": ">=0.2.2-rc.1 <0.3.0"
|
|
24
29
|
},
|
|
25
30
|
"peerDependencies": {
|
|
26
|
-
"@deepseek-ai/schemastery": "
|
|
31
|
+
"@deepseek-ai/schemastery": ">=3.18.4 <4.0.0"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
"@deepseek-ai/schemastery": "^3.19.0"
|
|
27
35
|
},
|
|
28
36
|
"files": [
|
|
29
37
|
"lib",
|
|
30
38
|
"cordis.patch.yml",
|
|
31
39
|
"README.md",
|
|
40
|
+
"README.en.md",
|
|
32
41
|
"LICENSE"
|
|
33
42
|
],
|
|
34
43
|
"keywords": [
|