dsh-any-agent 0.5.2 → 0.6.1
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 +143 -1
- package/USAGE-NOTES.en.md +3 -1
- package/USAGE-NOTES.md +9 -2
- package/bin/dsh-any-agent-workspaces.mjs +12 -5
- package/bin/dsh-any-agent.mjs +27 -2
- package/bin/restore.mjs +1 -1
- package/docs/en/compatibility.md +27 -6
- package/docs/en/reference.md +4 -1
- package/docs/en/release.md +4 -0
- package/docs/en/security.md +1 -0
- package/docs/en/tests.md +59 -15
- package/docs/en/usage.md +22 -0
- package/docs/zh/compatibility.md +25 -6
- package/docs/zh/live-view-design.md +192 -0
- package/docs/zh/reference.md +4 -1
- package/docs/zh/release.md +3 -0
- package/docs/zh/security.md +1 -0
- package/docs/zh/tests.md +59 -15
- package/docs/zh/usage.md +22 -0
- package/package.json +4 -1
- package/scripts/detect-dsh.mjs +55 -0
- package/scripts/lint.mjs +595 -0
- package/scripts/sync-test-counts.mjs +699 -0
- package/src/acp-session.mjs +63 -0
- package/src/acp.mjs +14 -5
- package/src/config.mjs +39 -111
- package/src/conversation.mjs +1714 -0
- package/src/doctor.mjs +203 -0
- package/src/dsh-locate.mjs +560 -0
- package/src/git-snapshot.mjs +196 -0
- package/src/hub-store.mjs +108 -0
- package/src/hub.mjs +33 -1972
- package/src/live-cli.mjs +115 -0
- package/src/live-dashboard.html +535 -0
- package/src/live-dashboard.mjs +176 -0
- package/src/live-view.mjs +592 -0
- package/src/live.mjs +186 -0
- package/src/mcp.mjs +24 -75
- package/src/residue.mjs +282 -0
- package/src/sentinel.mjs +97 -0
- package/src/tool-defs.mjs +399 -0
- package/src/tool-render.mjs +262 -0
- package/src/tools.mjs +18 -767
- package/src/workspace-guard.mjs +89 -0
- package/src/workspace.mjs +295 -59
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 工具结果的文本渲染:审核单、回合结果、后台收据、幂等重放、入队回执,以及 PowerShell
|
|
3
|
+
* 版的哨兵等待 / 读取命令。
|
|
4
|
+
*
|
|
5
|
+
* 从 tools.mjs 拆出来:这些都是"结构化结果 -> 给模型看的文本"的纯函数(只读入参,
|
|
6
|
+
* 不碰 hub 的运行期状态),所以能脱离处理器表单独测试。tools.mjs 仍然重新导出
|
|
7
|
+
* `compactResultForBudget` / `psWaitCommand` / `psReadCommand`,外部的导入路径不变。
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { toPosixPath } from './config.mjs';
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* 把审核单渲染成调用方一眼能看到的文本块。
|
|
14
|
+
*
|
|
15
|
+
* 为什么放在正文后面而不是单独一次工具调用:MCP 调用方拿到结果后往往直接转述,
|
|
16
|
+
* 审核要求必须和结果出现在同一条消息里,才有机会改变它的下一步动作。
|
|
17
|
+
*/
|
|
18
|
+
export function renderReviewSection(review) {
|
|
19
|
+
if (!review || typeof review !== 'object') return '';
|
|
20
|
+
const lines = [];
|
|
21
|
+
const pending = review.status === 'pending';
|
|
22
|
+
lines.push(`【调用方审核${pending ? ' · 待提交' : ` · ${review.status}`}】`);
|
|
23
|
+
if (review.prompt_preview) lines.push(`原任务: ${review.prompt_preview}`);
|
|
24
|
+
const ev = review.evidence ?? {};
|
|
25
|
+
const bits = [];
|
|
26
|
+
if (ev.stop_reason) bits.push(`stop=${ev.stop_reason}`);
|
|
27
|
+
if (ev.workspace_changed != null) bits.push(`工作区变化=${ev.workspace_changed ? '是' : '否'}`);
|
|
28
|
+
if (ev.changed_files != null) bits.push(`改动文件=${ev.changed_files}`);
|
|
29
|
+
if (Array.isArray(ev.tools) && ev.tools.length) bits.push(`工具=[${ev.tools.join(', ')}]`);
|
|
30
|
+
if (ev.auto_approvals) {
|
|
31
|
+
bits.push(`⚠ 自动放行审批=${ev.auto_approvals}${ev.auto_escalations ? `(含提权 ${ev.auto_escalations})` : ''}`);
|
|
32
|
+
}
|
|
33
|
+
if (bits.length) lines.push(`证据: ${bits.join(' · ')}`);
|
|
34
|
+
if (pending) {
|
|
35
|
+
lines.push('在把这次结果当作完成之前,请独立核验:');
|
|
36
|
+
(review.checklist ?? []).forEach((item, i) => lines.push(`${i + 1}. ${item}`));
|
|
37
|
+
if (review.instructions) lines.push(review.instructions);
|
|
38
|
+
lines.push(
|
|
39
|
+
`提交裁决: dsh_review(conversation_id="${review.conversation_id}", run_id="${review.run_id}", ` +
|
|
40
|
+
'verdict="pass|partial|fail", notes="审核理由")',
|
|
41
|
+
);
|
|
42
|
+
} else {
|
|
43
|
+
if (review.notes) lines.push(`审核理由: ${review.notes}`);
|
|
44
|
+
if (Array.isArray(review.findings) && review.findings.length) {
|
|
45
|
+
lines.push(`审核问题: ${review.findings.join(';')}`);
|
|
46
|
+
}
|
|
47
|
+
if (review.review_file) lines.push(`审核记录: ${review.review_file}`);
|
|
48
|
+
}
|
|
49
|
+
return lines.join('\n');
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* 按 `max_chars` 预算压缩一份回合结果(只用于返回给调用方,不改原对象)。
|
|
54
|
+
*
|
|
55
|
+
* 体积大头是 tools_used[].input:edit/write 会带完整的新旧文本,几十次工具调用
|
|
56
|
+
* 轻松几万字。压缩后每个工具只留 id/title/name/status/kind 与 input_chars;
|
|
57
|
+
* 正文与思考截到预算长度。完整版本仍在哨兵文件里。
|
|
58
|
+
*/
|
|
59
|
+
export function compactResultForBudget(result, cap) {
|
|
60
|
+
if (!result || typeof result !== 'object' || !(cap > 0)) return result ?? null;
|
|
61
|
+
const out = { ...result };
|
|
62
|
+
if (typeof out.answer === 'string' && out.answer.length > cap) out.answer = out.answer.slice(0, cap);
|
|
63
|
+
if (typeof out.thinking === 'string' && out.thinking.length > cap) out.thinking = out.thinking.slice(0, cap);
|
|
64
|
+
if (Array.isArray(out.tools_used)) {
|
|
65
|
+
out.tools_used = out.tools_used.map((t) => {
|
|
66
|
+
if (!t || typeof t !== 'object') return t;
|
|
67
|
+
const { input, ...meta } = t;
|
|
68
|
+
if (input === undefined) return meta;
|
|
69
|
+
let chars = 0;
|
|
70
|
+
try {
|
|
71
|
+
chars = typeof input === 'string' ? input.length : JSON.stringify(input)?.length ?? 0;
|
|
72
|
+
} catch {
|
|
73
|
+
chars = 0;
|
|
74
|
+
}
|
|
75
|
+
return { ...meta, input_chars: chars };
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
return out;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* PowerShell 版的哨兵等待命令(Windows 宿主上 Git Bash 可能不可用或挂住)。
|
|
83
|
+
* 路径放进单引号字面量:PowerShell 单引号里只有 `'` 需要转义(写成 `''`),
|
|
84
|
+
* `$`、反引号都不展开,所以不会被路径内容注入。
|
|
85
|
+
*/
|
|
86
|
+
export function psWaitCommand(sentinelPath, timeoutSec = 2100) {
|
|
87
|
+
const lit = `'${String(sentinelPath).replace(/'/g, "''")}'`;
|
|
88
|
+
return (
|
|
89
|
+
`$f=${lit}; $dl=(Get-Date).AddSeconds(${Math.max(1, Math.floor(timeoutSec))}); ` +
|
|
90
|
+
`while(-not (Test-Path -LiteralPath $f)){ if((Get-Date) -gt $dl){ 'TIMEOUT'; exit 1 }; Start-Sleep -Seconds 2 }; 'DONE'`
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* PowerShell 版的哨兵**读取**命令。哨兵是无 BOM 的 UTF-8:Windows PowerShell 5.1 的
|
|
96
|
+
* `Get-Content` 默认按 ANSI 代码页读,中文正文会乱码、`ConvertFrom-Json` 直接解析失败,
|
|
97
|
+
* 所以必须显式 `-Encoding UTF8`(PowerShell 7 默认就是 UTF-8,加上也无害)。
|
|
98
|
+
*/
|
|
99
|
+
export function psReadCommand(sentinelPath) {
|
|
100
|
+
const lit = `'${String(sentinelPath).replace(/'/g, "''")}'`;
|
|
101
|
+
return `Get-Content -LiteralPath ${lit} -Raw -Encoding UTF8 | ConvertFrom-Json`;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** 把结构化结果渲染成给模型看的文本。 */
|
|
105
|
+
export function renderSendResult(r) {
|
|
106
|
+
const lines = [];
|
|
107
|
+
if (r.answer?.trim()) lines.push(r.answer.trim());
|
|
108
|
+
else lines.push('(DSH 本次没有产生正文输出)');
|
|
109
|
+
|
|
110
|
+
if (r.thinking && r.thinking.trim()) {
|
|
111
|
+
lines.push('');
|
|
112
|
+
lines.push('--- 思考内容 ---');
|
|
113
|
+
lines.push(r.thinking.trim());
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const meta = [];
|
|
117
|
+
meta.push(`stop=${r.stop_reason}`);
|
|
118
|
+
if (typeof r.elapsed_ms === 'number') meta.push(`耗时=${(r.elapsed_ms / 1000).toFixed(1)}s`);
|
|
119
|
+
if (r.thinking_stats?.chars) {
|
|
120
|
+
meta.push(
|
|
121
|
+
r.thinking_stats.hidden
|
|
122
|
+
? `思考已隐藏(${r.thinking_stats.chars}字/${(r.thinking_stats.ms / 1000).toFixed(1)}s)`
|
|
123
|
+
: `思考=${r.thinking_stats.chars}字`,
|
|
124
|
+
);
|
|
125
|
+
}
|
|
126
|
+
const tools = (r.tools_used ?? []).map((t) => t.name ?? t.title).filter(Boolean);
|
|
127
|
+
if (tools.length) meta.push(`工具=[${[...new Set(tools)].join(', ')}]`);
|
|
128
|
+
if (r.workspace_changed) meta.push(`本轮工作区状态发生变化(当前共 ${r.diff_stat?.changed_files ?? '?'} 个文件有改动)`);
|
|
129
|
+
if (r.auto_approvals_total) {
|
|
130
|
+
meta.push(
|
|
131
|
+
`自动放行审批 ${r.auto_approvals_total} 次` +
|
|
132
|
+
(r.auto_escalations_total ? `(含沙箱提权 ${r.auto_escalations_total} 次)` : ''),
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
lines.push('');
|
|
136
|
+
lines.push(`[${meta.join(' · ')}]`);
|
|
137
|
+
|
|
138
|
+
if (r.review) {
|
|
139
|
+
lines.push('');
|
|
140
|
+
lines.push(renderReviewSection(r.review));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
if (r.error) {
|
|
144
|
+
lines.push('');
|
|
145
|
+
lines.push(`⚠️ 回合失败: ${r.error}`);
|
|
146
|
+
}
|
|
147
|
+
return lines.join('\n');
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* 后台收据的渲染:wait=false 直接走这里;wait=true 等超时会自动降级到这里。
|
|
152
|
+
* 关键语义:**回合没有被取消**,只是我们不再原地等 —— 结果稍后由哨兵文件通知。
|
|
153
|
+
*/
|
|
154
|
+
export function backgroundReceipt(hub, conv, res) {
|
|
155
|
+
const sentinel = res.sentinel_file ?? hub.runSentinelPath(conv.id, res.run_id);
|
|
156
|
+
const sentinelPosix = toPosixPath(sentinel);
|
|
157
|
+
const review = conv.pendingReviewTicket?.(res.run_id) ?? null;
|
|
158
|
+
const waited = res.waited_ms
|
|
159
|
+
? `已等待 ${(res.waited_ms / 1000).toFixed(0)}s 仍未结束,改为后台继续(**回合没有被取消**)。`
|
|
160
|
+
: '已在后台执行(**回合没有被取消**)。';
|
|
161
|
+
return {
|
|
162
|
+
structured: {
|
|
163
|
+
conversation_id: conv.id,
|
|
164
|
+
run_id: res.run_id,
|
|
165
|
+
accepted: true,
|
|
166
|
+
background: true,
|
|
167
|
+
still_running: true,
|
|
168
|
+
sentinel_file: sentinel,
|
|
169
|
+
/** Bash/MSYS 形式(D:\a\b → /d/a/b);非 Windows 等于 sentinel_file。 */
|
|
170
|
+
sentinel_file_posix: sentinelPosix,
|
|
171
|
+
/** Windows 宿主:现成的 PowerShell 等待命令(路径已按单引号字面量转义)。 */
|
|
172
|
+
...(process.platform === 'win32'
|
|
173
|
+
? { sentinel_wait_powershell: psWaitCommand(sentinel), sentinel_read_powershell: psReadCommand(sentinel) }
|
|
174
|
+
: {}),
|
|
175
|
+
...(review ? { review } : {}),
|
|
176
|
+
},
|
|
177
|
+
text:
|
|
178
|
+
`${waited}(run_id=${res.run_id})\n\n` +
|
|
179
|
+
`【跑完自动通知,无需轮询】完成时(成功/失败/被取消)会原子写出这个文件(含最终结果,但**不含思考内容**):\n` +
|
|
180
|
+
` ${sentinel}\n` +
|
|
181
|
+
`直接用下面这个 Bash 形式路径,**不要手工转换**(写错会一直等到超时):\n` +
|
|
182
|
+
` ${sentinelPosix}\n` +
|
|
183
|
+
`推荐用后台任务等它出现,文件一到就会唤醒你:\n` +
|
|
184
|
+
` Bash(run_in_background): sent="${sentinelPosix}"; \\\n` +
|
|
185
|
+
` dl=$(( $(date +%s)+2100 )); until [ -f "$sent" ]; do [ $(date +%s) -ge $dl ] && { echo TIMEOUT; exit 1; }; sleep 2; done; echo DONE\n` +
|
|
186
|
+
(process.platform === 'win32'
|
|
187
|
+
? `Windows 上 Bash 不可用(或会挂住)时,用 PowerShell 版本(原生路径,已转义好):\n` +
|
|
188
|
+
` PowerShell(run_in_background): ${psWaitCommand(sentinel)}\n` +
|
|
189
|
+
`读取时必须带 -Encoding UTF8(Windows PowerShell 5.1 默认按 ANSI 读,会乱码/解析失败):\n` +
|
|
190
|
+
` ${psReadCommand(sentinel)}\n`
|
|
191
|
+
: '') +
|
|
192
|
+
`文件出现后:直接读它拿结果(推荐——不受内存窗口限制、还扛服务重启),或 dsh_get(conversation_id, run_id) 验收。\n` +
|
|
193
|
+
`消费完可以 rm 掉它。其它随时可用:dsh_status(是否在跑)、dsh_read(增量看输出)。` +
|
|
194
|
+
(review ? `\n\n${renderReviewSection(review)}` : '') +
|
|
195
|
+
(res.waited_ms
|
|
196
|
+
? `\n\n下次想立刻返回就传 wait=false;想等更久就调大 timeout_ms(**0 = 一直等**)。`
|
|
197
|
+
: ''),
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 幂等命中:返回同一个 client_run_id 首次对应 run 的结果或收据。
|
|
203
|
+
* 绝不创建第二个 run —— 这正是"响应在传输层丢了、想安全重发"需要的语义。
|
|
204
|
+
*/
|
|
205
|
+
export function replayRun(hub, conv, run, wantResult) {
|
|
206
|
+
const sentinel = hub.runSentinelPath(conv.id, run.run_id);
|
|
207
|
+
const sentinelPosix = toPosixPath(sentinel);
|
|
208
|
+
const terminal = run.status === 'done' || run.status === 'error' || run.status === 'cancelled';
|
|
209
|
+
if (terminal && run.result) {
|
|
210
|
+
return {
|
|
211
|
+
structured: { ...run.result, replayed: true, ...(wantResult ? {} : { background: true }) },
|
|
212
|
+
text:
|
|
213
|
+
`(幂等命中:client_run_id 已对应一个已结束的回合,未重复派活)\n` +
|
|
214
|
+
renderSendResult(run.result) +
|
|
215
|
+
`\n哨兵文件: ${sentinel}`,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const review = conv.pendingReviewTicket?.(run.run_id) ?? null;
|
|
219
|
+
return {
|
|
220
|
+
structured: {
|
|
221
|
+
conversation_id: conv.id,
|
|
222
|
+
run_id: run.run_id,
|
|
223
|
+
status: run.status,
|
|
224
|
+
accepted: true,
|
|
225
|
+
background: true,
|
|
226
|
+
still_running: true,
|
|
227
|
+
replayed: true,
|
|
228
|
+
sentinel_file: sentinel,
|
|
229
|
+
sentinel_file_posix: sentinelPosix,
|
|
230
|
+
...(review ? { review } : {}),
|
|
231
|
+
},
|
|
232
|
+
text:
|
|
233
|
+
`(幂等命中:同一个 client_run_id 已在跑或排队,未重复派活)run_id=${run.run_id} status=${run.status}\n` +
|
|
234
|
+
`哨兵文件: ${sentinel}` +
|
|
235
|
+
(review ? `\n\n${renderReviewSection(review)}` : ''),
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/** 入队回执:入队时不写哨兵,真正结束才写。 */
|
|
240
|
+
export function queuedReceipt(hub, conv, run, position) {
|
|
241
|
+
const sentinel = hub.runSentinelPath(conv.id, run.run_id);
|
|
242
|
+
const sentinelPosix = toPosixPath(sentinel);
|
|
243
|
+
const review = conv.pendingReviewTicket?.(run.run_id) ?? null;
|
|
244
|
+
return {
|
|
245
|
+
structured: {
|
|
246
|
+
conversation_id: conv.id,
|
|
247
|
+
run_id: run.run_id,
|
|
248
|
+
accepted: true,
|
|
249
|
+
queued: true,
|
|
250
|
+
queue_position: position,
|
|
251
|
+
status: 'queued',
|
|
252
|
+
sentinel_file: sentinel,
|
|
253
|
+
sentinel_file_posix: sentinelPosix,
|
|
254
|
+
...(review ? { review } : {}),
|
|
255
|
+
},
|
|
256
|
+
text:
|
|
257
|
+
`已入队(第 ${position} 位)。当前回合结束后自动执行;**入队时不会写哨兵**,真正结束时才写:\n` +
|
|
258
|
+
` ${sentinel}\n` +
|
|
259
|
+
`用 dsh_queue list 查看/修改/删除;取消:dsh_queue(conversation_id, action="remove", run_id="${run.run_id}")。` +
|
|
260
|
+
(review ? `\n\n${renderReviewSection(review)}` : ''),
|
|
261
|
+
};
|
|
262
|
+
}
|