@armadra/agent 0.15.0 → 0.16.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 +36 -0
- package/CHANGELOG.zh-CN.md +36 -0
- package/dist/agent/recovery-reminder.d.ts +23 -0
- package/dist/agent/recovery-reminder.js +42 -0
- package/dist/agent/reminders.d.ts +10 -4
- package/dist/agent/reminders.js +59 -19
- package/dist/agent/session-plan.js +3 -4
- package/dist/agent/session-settings.d.ts +5 -0
- package/dist/agent/session-settings.js +15 -2
- package/dist/agent/session-subagent.d.ts +5 -0
- package/dist/agent/session-subagent.js +65 -12
- package/dist/agent/session-tools.d.ts +3 -1
- package/dist/agent/session-tools.js +47 -3
- package/dist/agent/subagent-background.js +1 -0
- package/dist/agent/subagent-fork.js +3 -1
- package/dist/agent/subagent-handle.d.ts +22 -0
- package/dist/agent/subagent-handle.js +55 -0
- package/dist/agent/subagent-lifetime.d.ts +35 -0
- package/dist/agent/subagent-lifetime.js +112 -0
- package/dist/agent/subagent-registry.d.ts +23 -15
- package/dist/agent/subagent-registry.js +68 -81
- package/dist/agent/system-prompt.d.ts +8 -0
- package/dist/agent/system-prompt.js +4 -0
- package/dist/agent/types-w5.d.ts +8 -3
- package/dist/agent/types.d.ts +7 -0
- package/dist/agents/builtin.d.ts +1 -1
- package/dist/agents/builtin.js +3 -2
- package/dist/agents/report.d.ts +25 -0
- package/dist/agents/report.js +51 -0
- package/dist/agents/result.d.ts +9 -1
- package/dist/agents/result.js +16 -0
- package/dist/agents/task-control.d.ts +12 -0
- package/dist/agents/task-record.d.ts +24 -1
- package/dist/agents/task-record.js +24 -0
- package/dist/bundle/ama.cjs +3144 -1771
- package/dist/cli/bootstrap.js +14 -7
- package/dist/cli/compose-agents.d.ts +2 -1
- package/dist/cli/compose-agents.js +4 -1
- package/dist/cli/compose-events.d.ts +18 -3
- package/dist/cli/compose-events.js +17 -2
- package/dist/cli/compose-session.d.ts +1 -1
- package/dist/cli/compose-session.js +8 -16
- package/dist/cli/compose.d.ts +8 -0
- package/dist/cli/compose.js +25 -1
- package/dist/cli/deps.d.ts +12 -0
- package/dist/config/json-schema.js +4 -0
- package/dist/config/key-docs.js +3 -1
- package/dist/config/schema-w5.js +3 -0
- package/dist/config/schema.js +12 -1
- package/dist/config/settings-registry.js +4 -0
- package/dist/config/types-w5.d.ts +2 -0
- package/dist/config/types.d.ts +7 -0
- package/dist/drivers/acp/types.d.ts +4 -0
- package/dist/drivers/runner.js +5 -0
- package/dist/drivers/turn.d.ts +7 -0
- package/dist/drivers/turn.js +21 -0
- package/dist/drivers/types.d.ts +6 -0
- package/dist/hooks/dispatcher.d.ts +7 -1
- package/dist/hooks/dispatcher.js +28 -1
- package/dist/hooks/matcher.d.ts +4 -0
- package/dist/hooks/matcher.js +19 -2
- package/dist/host/api-impl.d.ts +8 -0
- package/dist/host/api-impl.js +10 -0
- package/dist/host/resources.d.ts +25 -0
- package/dist/host/resources.js +63 -0
- package/dist/host/types.d.ts +24 -2
- package/dist/host/types.js +9 -2
- package/dist/host.d.ts +1 -0
- package/dist/host.js +2 -0
- package/dist/i18n/catalog.d.ts +26 -0
- package/dist/i18n/messages/approval.d.ts +9 -0
- package/dist/i18n/messages/approval.js +9 -0
- package/dist/i18n/messages/config-keys.d.ts +8 -0
- package/dist/i18n/messages/config-keys.js +8 -0
- package/dist/i18n/messages/config.d.ts +8 -0
- package/dist/i18n/messages/drivers.d.ts +16 -0
- package/dist/i18n/messages/drivers.js +16 -0
- package/dist/i18n/messages/panels.d.ts +15 -0
- package/dist/i18n/messages/panels.js +5 -0
- package/dist/i18n/messages/settings.d.ts +6 -0
- package/dist/i18n/messages/settings.js +6 -0
- package/dist/modes/acp/acp-events.d.ts +3 -0
- package/dist/modes/acp/acp-events.js +21 -0
- package/dist/modes/interactive/agent-bar.d.ts +7 -2
- package/dist/modes/interactive/agent-bar.js +16 -6
- package/dist/modes/interactive/approval-ui.d.ts +2 -0
- package/dist/modes/interactive/approval-ui.js +4 -1
- package/dist/modes/interactive/interactive-mode.js +6 -1
- package/dist/modes/interactive/key-dispatch.d.ts +5 -0
- package/dist/modes/interactive/key-dispatch.js +28 -4
- package/dist/modes/interactive/program-status-ui.d.ts +1 -0
- package/dist/modes/interactive/program-status-ui.js +38 -5
- package/dist/modes/interactive/session-events.js +13 -1
- package/dist/modes/interactive/subagent-view.d.ts +4 -1
- package/dist/modes/interactive/subagent-view.js +26 -6
- package/dist/modes/interactive/tasks-report.d.ts +4 -1
- package/dist/modes/interactive/tasks-report.js +12 -0
- package/dist/modes/rpc/commands.d.ts +10 -1
- package/dist/modes/rpc/commands.js +19 -1
- package/dist/permissions/broker.d.ts +38 -3
- package/dist/permissions/broker.js +118 -7
- package/dist/permissions/pipeline.d.ts +11 -10
- package/dist/permissions/pipeline.js +18 -46
- package/dist/permissions/rules.d.ts +21 -4
- package/dist/permissions/rules.js +43 -9
- package/dist/permissions/session-grants.d.ts +14 -0
- package/dist/permissions/session-grants.js +47 -0
- package/dist/permissions/types.d.ts +18 -0
- package/dist/session/unclean-end.d.ts +47 -0
- package/dist/session/unclean-end.js +153 -0
- package/dist/tools/background-jobs.d.ts +50 -3
- package/dist/tools/background-jobs.js +70 -6
- package/dist/tools/bash-jobs.d.ts +26 -0
- package/dist/tools/bash-jobs.js +165 -0
- package/dist/tools/bash.d.ts +22 -5
- package/dist/tools/bash.js +76 -103
- package/dist/tools/output-accumulator.d.ts +5 -0
- package/dist/tools/output-accumulator.js +9 -0
- package/dist/tools/read-lines.d.ts +2 -0
- package/dist/tools/read-lines.js +4 -0
- package/dist/tools/read-resource.d.ts +24 -0
- package/dist/tools/read-resource.js +129 -0
- package/dist/tools/read.d.ts +7 -2
- package/dist/tools/read.js +16 -5
- package/dist/tools/registry.d.ts +5 -2
- package/dist/tools/registry.js +8 -3
- package/dist/tools/report.d.ts +24 -0
- package/dist/tools/report.js +81 -0
- package/dist/tools/resource-ref.d.ts +90 -0
- package/dist/tools/resource-ref.js +90 -0
- package/dist/tools/task-ctl.d.ts +8 -1
- package/dist/tools/task-ctl.js +31 -5
- package/dist/tools/task.d.ts +2 -0
- package/dist/tools/task.js +3 -0
- package/dist/tools/timers.d.ts +18 -0
- package/dist/tools/timers.js +26 -0
- package/dist/tools/types.d.ts +30 -0
- package/dist/tools/types.js +2 -0
- package/docs/en/guides/permissions.md +14 -0
- package/docs/en/guides/tui.md +21 -21
- package/docs/en/reference/acp.md +15 -14
- package/docs/en/reference/host-api.md +48 -20
- package/docs/en/reference/rpc.md +12 -9
- package/docs/guides/agents.md +83 -22
- package/docs/guides/hooks.md +12 -9
- package/docs/guides/permissions.md +14 -0
- package/docs/guides/tui.md +21 -21
- package/docs/reference/acp.md +15 -14
- package/docs/reference/host-api.md +69 -41
- package/docs/reference/rpc.md +15 -9
- package/docs/reference/session-format.md +29 -11
- package/package.json +2 -2
package/dist/tools/bash.js
CHANGED
|
@@ -8,9 +8,12 @@
|
|
|
8
8
|
* 信号退出 `128 + signo`;非零退出码 → isError;
|
|
9
9
|
* - 注入 `AMA_*` 环境变量(shell.ts)。shell 退出后不等仍占着管道的后台进程。
|
|
10
10
|
* - [W5-H2] 后台命令(§8.3 H6,不加新工具):`{ command, background: true }` 立即返回
|
|
11
|
-
* `{ jobId, outputPath, pid }`;`{ job, action: "wait" | "output" | "stop" }` 查询 / 结束
|
|
12
|
-
* (background-jobs.ts,按会话登记,会话 dispose
|
|
13
|
-
*
|
|
11
|
+
* `{ jobId, outputPath, pid }`;`{ job, action: "wait" | "output" | "stop" | "extend" }` 查询 / 结束 / 续命
|
|
12
|
+
* (bash-jobs.ts + background-jobs.ts,按会话登记,会话 dispose 时回收进程树;退出经提醒通道通知)。
|
|
13
|
+
* - [#266] 前台命令运行超过 `autoBackgroundAfterMs`(0 关)仍未结束时**原地转后台**:不重启进程,清掉超时计时器、
|
|
14
|
+
* 解绑 abort、停止流式更新,把已累积输出落盘为任务输出文件,交给后台任务表登记(`adopt`),工具立即返回
|
|
15
|
+
* `{ jobId, outputPath, movedToBackground: true, output }`;之后受 [#267] 寿命上限约束(从转换时刻起算),
|
|
16
|
+
* 原来的 120 / 600 s 超时不再生效。阈值 ≥ 超时时超时先到,照旧杀掉、不转。
|
|
14
17
|
* - **进程只在 `spawnShell` 里创建**(前台与后台共用)。[S2] `sandbox.bash: auto` 且有能限制写入的
|
|
15
18
|
* OS 沙箱时在这一处包一层(src/sandbox/bash.ts);`sandbox: false` 不包装(权限管线照常审批)。
|
|
16
19
|
* 失败输出像是被沙箱拒绝时末尾追加一行说明。
|
|
@@ -22,14 +25,16 @@ import { resolvePath } from "./paths.js";
|
|
|
22
25
|
import { buildShellArgs, buildToolEnv, resolveShell } from "./shell.js";
|
|
23
26
|
import { exitCodeOf, isGroupAlive, killProcessTree, trackProcessGroup, untrackProcessGroup, } from "./process-tree.js";
|
|
24
27
|
import { OutputAccumulator } from "./output-accumulator.js";
|
|
25
|
-
import { formatSize, resolveOutputDir, safeFileName, toolOutputBytes } from "./truncate.js";
|
|
26
|
-
import {
|
|
28
|
+
import { DEFAULT_MAX_LINES, formatSize, resolveOutputDir, safeFileName, toolOutputBytes, } from "./truncate.js";
|
|
29
|
+
import { backgroundMaxMs, fail, jobAction, jobsOf, movedResult, startBackground, } from "./bash-jobs.js";
|
|
30
|
+
import { REAL_TIMERS } from "./timers.js";
|
|
27
31
|
import { looksLikeSandboxDenial, runsSandboxed, sandboxDenialHint, wrapBashCommand, } from "../sandbox/bash.js";
|
|
28
32
|
export const DEFAULT_BASH_TIMEOUT_MS = 120_000;
|
|
29
33
|
export const MAX_BASH_TIMEOUT_MS = 600_000;
|
|
30
34
|
const UPDATE_THROTTLE_MS = 100;
|
|
31
35
|
/** shell 退出后等管道关闭的上限(后台子进程可能一直占着)。 */
|
|
32
36
|
const STDIO_DRAIN_MS = 200;
|
|
37
|
+
export { DEFAULT_BASH_AUTO_BACKGROUND_MS, DEFAULT_BASH_BACKGROUND_MAX_MS } from "./bash-jobs.js";
|
|
33
38
|
/** [W5-H2] `wait` 的缺省等待(毫秒)。 */
|
|
34
39
|
export const DEFAULT_JOB_WAIT_MS = 30_000;
|
|
35
40
|
/**
|
|
@@ -49,9 +54,6 @@ export function spawnShell(shell, command, options) {
|
|
|
49
54
|
windowsHide: true,
|
|
50
55
|
});
|
|
51
56
|
}
|
|
52
|
-
function fail(message) {
|
|
53
|
-
return { content: message, isError: true };
|
|
54
|
-
}
|
|
55
57
|
/** 等 shell 退出;之后最多再等 STDIO_DRAIN_MS 让管道读完,再强制关闭。 */
|
|
56
58
|
function waitForExit(child) {
|
|
57
59
|
return new Promise((resolve, reject) => {
|
|
@@ -83,9 +85,12 @@ function waitForExit(child) {
|
|
|
83
85
|
});
|
|
84
86
|
});
|
|
85
87
|
}
|
|
88
|
+
function hasExited(child) {
|
|
89
|
+
return child.exitCode !== null || child.signalCode !== null;
|
|
90
|
+
}
|
|
86
91
|
export async function executeBash(input, ctx, options = {}) {
|
|
87
92
|
if (input.job !== undefined)
|
|
88
|
-
return jobAction(input, ctx, options);
|
|
93
|
+
return jobAction(input, ctx, options, MAX_BASH_TIMEOUT_MS, DEFAULT_JOB_WAIT_MS);
|
|
89
94
|
if (input.action !== undefined)
|
|
90
95
|
return fail("action needs job (a background job id)");
|
|
91
96
|
if (typeof input.command !== "string" || input.command.trim() === "") {
|
|
@@ -126,7 +131,13 @@ export async function executeBash(input, ctx, options = {}) {
|
|
|
126
131
|
}
|
|
127
132
|
: undefined;
|
|
128
133
|
if (input.background === true)
|
|
129
|
-
return startBackground(command,
|
|
134
|
+
return startBackground(command, (fd) => spawnShell(shell, command, {
|
|
135
|
+
cwd,
|
|
136
|
+
env,
|
|
137
|
+
detached: !isWindows,
|
|
138
|
+
stdio: ["ignore", fd, fd],
|
|
139
|
+
...(sandbox === undefined ? {} : { sandbox }),
|
|
140
|
+
}), shell.shell, cwd, sandbox !== undefined, ctx, options);
|
|
130
141
|
const started = Date.now();
|
|
131
142
|
const acc = new OutputAccumulator({
|
|
132
143
|
maxBytes: toolOutputBytes(ctx.maxResultChars),
|
|
@@ -148,6 +159,7 @@ export async function executeBash(input, ctx, options = {}) {
|
|
|
148
159
|
const pid = child.pid;
|
|
149
160
|
if (pid !== undefined && !isWindows)
|
|
150
161
|
trackProcessGroup(pid);
|
|
162
|
+
let moved = false;
|
|
151
163
|
let lastUpdate = 0;
|
|
152
164
|
let pendingUpdate;
|
|
153
165
|
const emitUpdate = () => {
|
|
@@ -157,7 +169,7 @@ export async function executeBash(input, ctx, options = {}) {
|
|
|
157
169
|
};
|
|
158
170
|
const onData = (chunk) => {
|
|
159
171
|
acc.append(chunk);
|
|
160
|
-
if (pendingUpdate)
|
|
172
|
+
if (moved || pendingUpdate)
|
|
161
173
|
return;
|
|
162
174
|
const wait = Math.max(0, lastUpdate + UPDATE_THROTTLE_MS - Date.now());
|
|
163
175
|
pendingUpdate = setTimeout(emitUpdate, wait);
|
|
@@ -181,15 +193,66 @@ export async function executeBash(input, ctx, options = {}) {
|
|
|
181
193
|
kill();
|
|
182
194
|
};
|
|
183
195
|
ctx.signal.addEventListener("abort", onAbort, { once: true });
|
|
196
|
+
// [#266] 阈值到了仍在跑 → 原地转后台(已退出的不转:让正常路径收尾)
|
|
197
|
+
const timers = options.timers ?? REAL_TIMERS;
|
|
198
|
+
const afterMs = options.autoBackgroundAfterMs ?? 0;
|
|
199
|
+
let resolveMoved;
|
|
200
|
+
const movedPromise = new Promise((resolve) => {
|
|
201
|
+
resolveMoved = resolve;
|
|
202
|
+
});
|
|
203
|
+
const moveTimer = afterMs > 0
|
|
204
|
+
? timers.set(() => {
|
|
205
|
+
if (moved || hasExited(child) || timedOut || aborted)
|
|
206
|
+
return;
|
|
207
|
+
// 先落盘、登记,成功后才解绑前台的超时与 abort:persist / adopt 抛错(磁盘满、目录不可写)时命令
|
|
208
|
+
// 留在前台照常受超时约束,不能让异常逃出计时器回调(会成为 uncaughtException)
|
|
209
|
+
let job;
|
|
210
|
+
try {
|
|
211
|
+
const outputPath = acc.persist();
|
|
212
|
+
job = jobsOf(ctx, options).adopt(command, {
|
|
213
|
+
cwd,
|
|
214
|
+
outputPath,
|
|
215
|
+
child,
|
|
216
|
+
startedAt: started,
|
|
217
|
+
maxMs: backgroundMaxMs(options),
|
|
218
|
+
...(options.processDeps === undefined ? {} : { processDeps: options.processDeps }),
|
|
219
|
+
...(options.timers === undefined ? {} : { timers: options.timers }),
|
|
220
|
+
});
|
|
221
|
+
}
|
|
222
|
+
catch (err) {
|
|
223
|
+
ctx.log("warn", `bash: could not move the command to the background, keeping it in the foreground: ${err.message}`);
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
moved = true;
|
|
227
|
+
clearTimeout(timer);
|
|
228
|
+
ctx.signal.removeEventListener("abort", onAbort);
|
|
229
|
+
if (pendingUpdate) {
|
|
230
|
+
clearTimeout(pendingUpdate);
|
|
231
|
+
pendingUpdate = undefined;
|
|
232
|
+
}
|
|
233
|
+
resolveMoved?.(movedResult(job, acc.tail(DEFAULT_MAX_LINES), afterMs, sandbox !== undefined, command));
|
|
234
|
+
}, afterMs)
|
|
235
|
+
: undefined;
|
|
236
|
+
const exited = waitForExit(child);
|
|
237
|
+
// 转后台后这个 promise 没人等了:出错不能变成 unhandled rejection
|
|
238
|
+
exited.catch(() => undefined);
|
|
184
239
|
let exit;
|
|
185
240
|
try {
|
|
186
|
-
|
|
241
|
+
const outcome = await Promise.race([
|
|
242
|
+
exited.then((info) => ({ kind: "exit", info })),
|
|
243
|
+
movedPromise.then((result) => ({ kind: "moved", result })),
|
|
244
|
+
]);
|
|
245
|
+
if (outcome.kind === "moved")
|
|
246
|
+
return outcome.result;
|
|
247
|
+
exit = outcome.info;
|
|
187
248
|
}
|
|
188
249
|
catch (err) {
|
|
189
250
|
return fail(`Failed to run command with ${shell.shell}: ${err.message}`);
|
|
190
251
|
}
|
|
191
252
|
finally {
|
|
192
253
|
clearTimeout(timer);
|
|
254
|
+
if (moveTimer !== undefined)
|
|
255
|
+
timers.clear(moveTimer);
|
|
193
256
|
ctx.signal.removeEventListener("abort", onAbort);
|
|
194
257
|
if (pendingUpdate) {
|
|
195
258
|
// 冲掉节流中的最后一次更新,保证 UI 看到的尾部是完整的。
|
|
@@ -245,96 +308,6 @@ export async function executeBash(input, ctx, options = {}) {
|
|
|
245
308
|
details,
|
|
246
309
|
};
|
|
247
310
|
}
|
|
248
|
-
// ---------------------------------------------------------------------------
|
|
249
|
-
// [W5-H2] 后台命令
|
|
250
|
-
// ---------------------------------------------------------------------------
|
|
251
|
-
function jobsOf(ctx, options) {
|
|
252
|
-
return options.jobs ? options.jobs(ctx.sessionId) : jobsForSession(ctx.sessionId);
|
|
253
|
-
}
|
|
254
|
-
function startBackground(command, spawnInfo, ctx, options) {
|
|
255
|
-
const { cwd, env, shell, isWindows, sandbox } = spawnInfo;
|
|
256
|
-
const outputPath = join(resolveOutputDir(ctx.outputDir), safeFileName(`bash-bg-${ctx.toolCallId}.log`));
|
|
257
|
-
let job;
|
|
258
|
-
try {
|
|
259
|
-
job = jobsOf(ctx, options).start(command, {
|
|
260
|
-
cwd,
|
|
261
|
-
outputPath,
|
|
262
|
-
...(options.processDeps === undefined ? {} : { processDeps: options.processDeps }),
|
|
263
|
-
spawn: (fd) => spawnShell(shell, command, {
|
|
264
|
-
cwd,
|
|
265
|
-
env,
|
|
266
|
-
detached: !isWindows,
|
|
267
|
-
stdio: ["ignore", fd, fd],
|
|
268
|
-
...(sandbox === undefined ? {} : { sandbox }),
|
|
269
|
-
}),
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
catch (err) {
|
|
273
|
-
return fail(`Failed to start shell ${shell.shell}: ${err.message}`);
|
|
274
|
-
}
|
|
275
|
-
const structured = { jobId: job.id, outputPath: job.outputPath };
|
|
276
|
-
if (job.pid !== undefined)
|
|
277
|
-
structured.pid = job.pid;
|
|
278
|
-
if (sandbox !== undefined)
|
|
279
|
-
structured.sandboxed = true;
|
|
280
|
-
return {
|
|
281
|
-
content: `Started background job ${job.id}${job.pid !== undefined ? ` (pid ${job.pid})` : ""}; ` +
|
|
282
|
-
`output: ${job.outputPath}. Check it with bash {"job":"${job.id}","action":"wait"|"output"|"stop"}; ` +
|
|
283
|
-
"you will be told when it exits.",
|
|
284
|
-
structured,
|
|
285
|
-
details: { ...structured, background: true, command, cwd },
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
function jobStatusLine(job) {
|
|
289
|
-
const state = job.status === "running"
|
|
290
|
-
? "still running"
|
|
291
|
-
: job.status === "stopped"
|
|
292
|
-
? `stopped (exit code ${job.exitCode ?? "?"})`
|
|
293
|
-
: `exited with code ${job.exitCode ?? "?"}`;
|
|
294
|
-
return `[job ${job.id} ${state}; output: ${job.outputPath}]`;
|
|
295
|
-
}
|
|
296
|
-
async function jobAction(input, ctx, options) {
|
|
297
|
-
const id = input.job;
|
|
298
|
-
if (input.command !== undefined || input.background === true || input.sandbox !== undefined)
|
|
299
|
-
return fail("job queries take only job, action and timeoutMs");
|
|
300
|
-
const action = input.action ?? "output";
|
|
301
|
-
const jobs = jobsOf(ctx, options);
|
|
302
|
-
if (jobs.get(id) === undefined) {
|
|
303
|
-
const known = jobs.list().map((job) => job.id);
|
|
304
|
-
return fail(`Unknown background job ${id}${known.length > 0 ? ` (jobs: ${known.join(", ")})` : ""}`);
|
|
305
|
-
}
|
|
306
|
-
if (action === "stop") {
|
|
307
|
-
const job = (await jobs.stop(id));
|
|
308
|
-
const out = jobs.output(id, { maxBytes: toolOutputBytes(ctx.maxResultChars) });
|
|
309
|
-
const text = out?.content ?? "";
|
|
310
|
-
return {
|
|
311
|
-
content: `${text === "" ? "(no output)" : text}\n\n${jobStatusLine(job)}`,
|
|
312
|
-
structured: { jobId: id, status: job.status, exit_code: job.exitCode ?? null },
|
|
313
|
-
};
|
|
314
|
-
}
|
|
315
|
-
if (action === "wait") {
|
|
316
|
-
const waitMs = input.timeoutMs ?? DEFAULT_JOB_WAIT_MS;
|
|
317
|
-
if (!Number.isFinite(waitMs) || waitMs <= 0 || waitMs > MAX_BASH_TIMEOUT_MS)
|
|
318
|
-
return fail(`timeoutMs must be between 1 and ${MAX_BASH_TIMEOUT_MS}`);
|
|
319
|
-
await jobs.wait(id, waitMs, ctx.signal);
|
|
320
|
-
}
|
|
321
|
-
const out = jobs.output(id, { maxBytes: toolOutputBytes(ctx.maxResultChars) });
|
|
322
|
-
if (out === undefined)
|
|
323
|
-
return fail(`Unknown background job ${id}`);
|
|
324
|
-
const job = out.job;
|
|
325
|
-
if (job.status !== "running")
|
|
326
|
-
jobs.markSeen(id);
|
|
327
|
-
const parts = [out.content === "" ? "(no output)" : out.content];
|
|
328
|
-
if (out.truncated)
|
|
329
|
-
parts.push(`[Output truncated: showing the last ${out.outputLines} of ${out.totalLines} lines ` +
|
|
330
|
-
`(${formatSize(out.totalBytes)} total). Full output: ${job.outputPath}]`);
|
|
331
|
-
parts.push(jobStatusLine(job));
|
|
332
|
-
return {
|
|
333
|
-
content: parts.join("\n\n"),
|
|
334
|
-
isError: job.status === "exited" && job.exitCode !== 0,
|
|
335
|
-
structured: { jobId: id, status: job.status, exit_code: job.exitCode ?? null },
|
|
336
|
-
};
|
|
337
|
-
}
|
|
338
311
|
/** [S2] 沙箱生效时追加到描述末尾的一句(会话内不变)。 */
|
|
339
312
|
export function sandboxDescription(sandbox) {
|
|
340
313
|
if (sandbox?.active !== true)
|
|
@@ -360,7 +333,7 @@ export function createBashTool(options = {}) {
|
|
|
360
333
|
description: { type: "string" },
|
|
361
334
|
background: { type: "boolean" },
|
|
362
335
|
job: { type: "string" },
|
|
363
|
-
action: { type: "string", enum: ["wait", "output", "stop"] },
|
|
336
|
+
action: { type: "string", enum: ["wait", "output", "stop", "extend"] },
|
|
364
337
|
// [S2] 只在沙箱生效时出现:沙箱关闭的会话工具 schema 与以前逐字节相同
|
|
365
338
|
...(sandboxed ? { sandbox: { type: "boolean" } } : {}),
|
|
366
339
|
},
|
|
@@ -42,6 +42,11 @@ export declare class OutputAccumulator {
|
|
|
42
42
|
append(chunk: Buffer | string): void;
|
|
43
43
|
private pushText;
|
|
44
44
|
private spill;
|
|
45
|
+
/**
|
|
46
|
+
* [#266] 把到目前为止的全部输出落盘并返回路径(已落盘的直接返回);之后的 `append` 继续追加到同一文件。
|
|
47
|
+
* 前台命令转后台时用:后台任务表从这个文件读输出。
|
|
48
|
+
*/
|
|
49
|
+
persist(): string;
|
|
45
50
|
/** 当前滚动尾部(已清洗、尾截断),给流式 onUpdate。 */
|
|
46
51
|
tail(maxLines?: number): string;
|
|
47
52
|
finish(): AccumulatedOutput;
|
|
@@ -70,6 +70,15 @@ export class OutputAccumulator {
|
|
|
70
70
|
this.chunks = [];
|
|
71
71
|
this.memoryBytes = 0;
|
|
72
72
|
}
|
|
73
|
+
/**
|
|
74
|
+
* [#266] 把到目前为止的全部输出落盘并返回路径(已落盘的直接返回);之后的 `append` 继续追加到同一文件。
|
|
75
|
+
* 前台命令转后台时用:后台任务表从这个文件读输出。
|
|
76
|
+
*/
|
|
77
|
+
persist() {
|
|
78
|
+
if (this.spilledTo === undefined)
|
|
79
|
+
this.spill();
|
|
80
|
+
return this.spilledTo;
|
|
81
|
+
}
|
|
73
82
|
/** 当前滚动尾部(已清洗、尾截断),给流式 onUpdate。 */
|
|
74
83
|
tail(maxLines = 50) {
|
|
75
84
|
return truncateTail(sanitizeOutput(this.tailText), { maxLines, maxBytes: this.maxBytes })
|
|
@@ -16,6 +16,8 @@
|
|
|
16
16
|
*/
|
|
17
17
|
/** 超过这个大小的文本文件走字节窗口;不超过的保留整读路径。 */
|
|
18
18
|
export declare const STREAM_READ_THRESHOLD: number;
|
|
19
|
+
/** `cat -n` 形状(行号右对齐 6 位 + Tab);文件分支与宿主资源分支共用。 */
|
|
20
|
+
export declare function numberLines(lines: readonly string[], firstLine: number): string;
|
|
19
21
|
export interface LineWindow {
|
|
20
22
|
/** 窗口内的行:首行已去 BOM、已按 LF 切分、已去行尾 `\r`。 */
|
|
21
23
|
lines: string[];
|
package/dist/tools/read-lines.js
CHANGED
|
@@ -23,6 +23,10 @@ const CHUNK_BYTES = 64 * 1024;
|
|
|
23
23
|
const LINE_SLACK = 64;
|
|
24
24
|
const LF = 0x0a;
|
|
25
25
|
const CR = 0x0d;
|
|
26
|
+
/** `cat -n` 形状(行号右对齐 6 位 + Tab);文件分支与宿主资源分支共用。 */
|
|
27
|
+
export function numberLines(lines, firstLine) {
|
|
28
|
+
return lines.map((line, i) => `${String(firstLine + i).padStart(6)}\t${line}`).join("\n");
|
|
29
|
+
}
|
|
26
30
|
/** 文件开头至多 `bytes` 字节(一次 `readSync`,用于二进制嗅探)。 */
|
|
27
31
|
export function readHead(abs, bytes) {
|
|
28
32
|
const fd = openSync(abs, "r");
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `read` 工具的宿主资源分支(#270):路径是已注册的 `scheme://path` 时交给宿主读,照 `read` 的口径
|
|
3
|
+
* 编号、分页与截断,首行标注不可信(与 `web_fetch` 同一套)。
|
|
4
|
+
*
|
|
5
|
+
* - 先校验 `offset` / `limit`(与文件分支相同),再调宿主 `read(path, { offset, limit, signal })`;
|
|
6
|
+
* `signal` 合并会话中断与 `HOST_RESOURCE_TIMEOUT_MS` 超时。
|
|
7
|
+
* - 宿主抛错 / 超时 / 返回形状不对 / 返回 undefined 都是 `isError` 的工具结果,不抛到会话。
|
|
8
|
+
* - `text` 只收文本(含 NUL 按二进制拒绝),按宿主的 `limit` 再切一刀(宿主多给的行丢弃),`cat -n`
|
|
9
|
+
* 编号后 `truncateHead`(2000 行 / 会话字节上限),续读提示 `Use offset=N`。
|
|
10
|
+
* - 未注册的 scheme 回错误并列出已注册的;`http(s)://` 另提示用 `web_fetch`。
|
|
11
|
+
*/
|
|
12
|
+
import type { ToolContext, ToolResult } from "./types.js";
|
|
13
|
+
import { type HostResourceLookup, type ResourceRef } from "./resource-ref.js";
|
|
14
|
+
/** 未注册 scheme 的错误:列出已注册的;URL 指向 web_fetch。 */
|
|
15
|
+
export declare function unknownSchemeMessage(ref: ResourceRef, schemes: readonly string[]): string;
|
|
16
|
+
export interface ResourceReadOptions {
|
|
17
|
+
timeoutMs?: number;
|
|
18
|
+
}
|
|
19
|
+
/** `read` 输入里与资源有关的部分(path 已解析成 ref)。 */
|
|
20
|
+
export interface ResourceReadInput {
|
|
21
|
+
offset?: number;
|
|
22
|
+
limit?: number;
|
|
23
|
+
}
|
|
24
|
+
export declare function executeResourceRead(ref: ResourceRef, input: ResourceReadInput, ctx: ToolContext, lookup: HostResourceLookup, options?: ResourceReadOptions): Promise<ToolResult>;
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `read` 工具的宿主资源分支(#270):路径是已注册的 `scheme://path` 时交给宿主读,照 `read` 的口径
|
|
3
|
+
* 编号、分页与截断,首行标注不可信(与 `web_fetch` 同一套)。
|
|
4
|
+
*
|
|
5
|
+
* - 先校验 `offset` / `limit`(与文件分支相同),再调宿主 `read(path, { offset, limit, signal })`;
|
|
6
|
+
* `signal` 合并会话中断与 `HOST_RESOURCE_TIMEOUT_MS` 超时。
|
|
7
|
+
* - 宿主抛错 / 超时 / 返回形状不对 / 返回 undefined 都是 `isError` 的工具结果,不抛到会话。
|
|
8
|
+
* - `text` 只收文本(含 NUL 按二进制拒绝),按宿主的 `limit` 再切一刀(宿主多给的行丢弃),`cat -n`
|
|
9
|
+
* 编号后 `truncateHead`(2000 行 / 会话字节上限),续读提示 `Use offset=N`。
|
|
10
|
+
* - 未注册的 scheme 回错误并列出已注册的;`http(s)://` 另提示用 `web_fetch`。
|
|
11
|
+
*/
|
|
12
|
+
import { numberLines } from "./read-lines.js";
|
|
13
|
+
import { DEFAULT_MAX_LINES, formatSize, toolOutputBytes, truncateHead } from "./truncate.js";
|
|
14
|
+
import { HOST_RESOURCE_TIMEOUT_MS, } from "./resource-ref.js";
|
|
15
|
+
const ERROR_MESSAGE_CHARS = 200;
|
|
16
|
+
function error(message) {
|
|
17
|
+
return { content: message, isError: true };
|
|
18
|
+
}
|
|
19
|
+
function errorText(err) {
|
|
20
|
+
const text = (err instanceof Error ? err.message : String(err)).replace(/\s+/g, " ").trim();
|
|
21
|
+
return text.length > ERROR_MESSAGE_CHARS ? `${text.slice(0, ERROR_MESSAGE_CHARS - 1)}…` : text;
|
|
22
|
+
}
|
|
23
|
+
/** 未注册 scheme 的错误:列出已注册的;URL 指向 web_fetch。 */
|
|
24
|
+
export function unknownSchemeMessage(ref, schemes) {
|
|
25
|
+
const listed = schemes.map((s) => `${s}://`).join(", ");
|
|
26
|
+
const hint = ref.scheme === "http" || ref.scheme === "https" ? "; use web_fetch for http(s) URLs" : "";
|
|
27
|
+
return `Unknown resource scheme "${ref.scheme}://"${hint}; registered: ${listed}`;
|
|
28
|
+
}
|
|
29
|
+
function isContent(value) {
|
|
30
|
+
if (typeof value !== "object" || value === null)
|
|
31
|
+
return false;
|
|
32
|
+
const record = value;
|
|
33
|
+
if (typeof record["text"] !== "string")
|
|
34
|
+
return false;
|
|
35
|
+
for (const key of ["next", "total"]) {
|
|
36
|
+
const n = record[key];
|
|
37
|
+
if (n !== undefined && (!Number.isInteger(n) || n < 0))
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
return true;
|
|
41
|
+
}
|
|
42
|
+
export async function executeResourceRead(ref, input, ctx, lookup, options = {}) {
|
|
43
|
+
const resource = lookup.get(ref.scheme);
|
|
44
|
+
if (resource === undefined)
|
|
45
|
+
return error(unknownSchemeMessage(ref, lookup.schemes()));
|
|
46
|
+
const offset = input.offset ?? 1;
|
|
47
|
+
if (!Number.isInteger(offset) || offset < 1)
|
|
48
|
+
return error("offset must be an integer ≥ 1");
|
|
49
|
+
if (input.limit !== undefined && (!Number.isInteger(input.limit) || input.limit < 1)) {
|
|
50
|
+
return error("limit must be an integer ≥ 1");
|
|
51
|
+
}
|
|
52
|
+
const timeoutMs = options.timeoutMs ?? HOST_RESOURCE_TIMEOUT_MS;
|
|
53
|
+
const controller = new AbortController();
|
|
54
|
+
const timer = setTimeout(() => controller.abort(new Error("timeout")), timeoutMs);
|
|
55
|
+
const signal = AbortSignal.any([ctx.signal, controller.signal]);
|
|
56
|
+
let content;
|
|
57
|
+
try {
|
|
58
|
+
content = await new Promise((resolve, reject) => {
|
|
59
|
+
if (signal.aborted) {
|
|
60
|
+
reject(signal.reason);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
signal.addEventListener("abort", () => reject(signal.reason), { once: true });
|
|
64
|
+
Promise.resolve()
|
|
65
|
+
.then(() => resource.read(ref.path, {
|
|
66
|
+
offset,
|
|
67
|
+
...(input.limit !== undefined ? { limit: input.limit } : {}),
|
|
68
|
+
signal,
|
|
69
|
+
}))
|
|
70
|
+
.then(resolve, reject);
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
catch (err) {
|
|
74
|
+
const reason = controller.signal.aborted
|
|
75
|
+
? `the host did not answer within ${timeoutMs / 1000}s`
|
|
76
|
+
: ctx.signal.aborted
|
|
77
|
+
? "aborted"
|
|
78
|
+
: errorText(err);
|
|
79
|
+
return error(`Cannot read ${ref.raw}: ${reason}`);
|
|
80
|
+
}
|
|
81
|
+
finally {
|
|
82
|
+
clearTimeout(timer);
|
|
83
|
+
}
|
|
84
|
+
if (content === undefined || content === null)
|
|
85
|
+
return error(`Resource not found: ${ref.raw}`);
|
|
86
|
+
if (!isContent(content))
|
|
87
|
+
return error(`Cannot read ${ref.raw}: the host returned an invalid result`);
|
|
88
|
+
if (content.text.includes("\0")) {
|
|
89
|
+
return error(`${ref.raw} appears to be binary content; refusing to read it`);
|
|
90
|
+
}
|
|
91
|
+
const normalized = content.text.replace(/\r\n?/g, "\n");
|
|
92
|
+
let lines = normalized === "" ? [] : normalized.replace(/\n$/, "").split("\n");
|
|
93
|
+
let next = content.next;
|
|
94
|
+
if (input.limit !== undefined && lines.length > input.limit) {
|
|
95
|
+
lines = lines.slice(0, input.limit);
|
|
96
|
+
next = offset + input.limit;
|
|
97
|
+
}
|
|
98
|
+
const details = {
|
|
99
|
+
resource: ref.raw,
|
|
100
|
+
scheme: ref.scheme,
|
|
101
|
+
firstLine: offset,
|
|
102
|
+
lastLine: offset - 1 + lines.length,
|
|
103
|
+
...(content.total !== undefined ? { total: content.total } : {}),
|
|
104
|
+
truncated: false,
|
|
105
|
+
};
|
|
106
|
+
if (lines.length === 0) {
|
|
107
|
+
if (offset > 1) {
|
|
108
|
+
const total = content.total !== undefined ? ` (${content.total} lines)` : "";
|
|
109
|
+
return error(`offset ${offset} is beyond the end of ${ref.raw}${total}`);
|
|
110
|
+
}
|
|
111
|
+
return { content: `(${ref.raw} is empty)`, details };
|
|
112
|
+
}
|
|
113
|
+
const maxBytes = toolOutputBytes(ctx.maxResultChars);
|
|
114
|
+
const cut = truncateHead(numberLines(lines, offset), { maxLines: DEFAULT_MAX_LINES, maxBytes });
|
|
115
|
+
const lastShown = offset - 1 + cut.outputLines;
|
|
116
|
+
let body = cut.content;
|
|
117
|
+
const more = cut.truncated || (next !== undefined && next > lastShown);
|
|
118
|
+
if (more) {
|
|
119
|
+
const of = content.total !== undefined ? ` of ${content.total}` : "";
|
|
120
|
+
const reason = cut.truncated
|
|
121
|
+
? ` (output limit ${cut.truncatedBy === "bytes" ? formatSize(maxBytes) : `${DEFAULT_MAX_LINES} lines`} reached)`
|
|
122
|
+
: "";
|
|
123
|
+
body += `\n\n[Showing lines ${offset}-${lastShown}${of}${reason}. Use offset=${lastShown + 1} to continue.]`;
|
|
124
|
+
}
|
|
125
|
+
return {
|
|
126
|
+
content: `Content read from ${ref.raw} (untrusted):\n\n${body}`,
|
|
127
|
+
details: { ...details, lastLine: lastShown, truncated: cut.truncated },
|
|
128
|
+
};
|
|
129
|
+
}
|
package/dist/tools/read.d.ts
CHANGED
|
@@ -7,10 +7,13 @@
|
|
|
7
7
|
* 与 `--image` / `@图片` 共用 image-file.ts;模型不支持图片、超过当前端点的单图上限(base64 后,
|
|
8
8
|
* [W5-I] 按端点分档)或任一边 > 8000 px 时只给路径与尺寸);成功后 `ctx.markRead(abs)`。
|
|
9
9
|
* [M-E] 超过 1 MiB 的文本按字节窗口读(read-lines.ts),输出与整读逐字节相同。
|
|
10
|
+
* [#270] 宿主注册过资源时,`scheme://path` 交给宿主读(read-resource.ts),同一套分页与截断。
|
|
10
11
|
*/
|
|
11
12
|
import type { ToolContext, ToolDefinition, ToolResult } from "./types.js";
|
|
13
|
+
import { type HostResourceLookup } from "./resource-ref.js";
|
|
12
14
|
import { type ImageFitOptions } from "./image-file.js";
|
|
13
15
|
export { imageSize, type ImageSize } from "./image-file.js";
|
|
16
|
+
export { numberLines } from "./read-lines.js";
|
|
14
17
|
export interface ReadInput {
|
|
15
18
|
path: string;
|
|
16
19
|
offset?: number;
|
|
@@ -23,6 +26,10 @@ export interface ReadToolOptions {
|
|
|
23
26
|
imageOptions?(ctx: ToolContext): ImageFitOptions;
|
|
24
27
|
/** [M-E] 超过这个字节数的文本文件走字节窗口;缺省 `STREAM_READ_THRESHOLD`,测试注入 0 / Infinity。 */
|
|
25
28
|
streamThreshold?: number;
|
|
29
|
+
/** [#270] 宿主资源注册表:非空时 `scheme://path` 交给宿主读(read-resource.ts);缺省 / 为空时行为不变。 */
|
|
30
|
+
resources?: HostResourceLookup;
|
|
31
|
+
/** [#270] 宿主 `read()` 的时限;缺省 `HOST_RESOURCE_TIMEOUT_MS`,测试注入。 */
|
|
32
|
+
resourceTimeoutMs?: number;
|
|
26
33
|
}
|
|
27
34
|
export declare function isBinary(buf: Buffer): boolean;
|
|
28
35
|
/**
|
|
@@ -30,7 +37,5 @@ export declare function isBinary(buf: Buffer): boolean;
|
|
|
30
37
|
* 活动集未知(宿主自建上下文)时沿用旧文案。
|
|
31
38
|
*/
|
|
32
39
|
export declare function directoryHint(shown: string, ctx: Pick<ToolContext, "activeTools">): string;
|
|
33
|
-
/** `cat -n` 形状。 */
|
|
34
|
-
export declare function numberLines(lines: readonly string[], firstLine: number): string;
|
|
35
40
|
export declare function executeRead(input: ReadInput, ctx: ToolContext, options?: ReadToolOptions): Promise<ToolResult>;
|
|
36
41
|
export declare function createReadTool(options?: ReadToolOptions): ToolDefinition<ReadInput>;
|
package/dist/tools/read.js
CHANGED
|
@@ -7,15 +7,19 @@
|
|
|
7
7
|
* 与 `--image` / `@图片` 共用 image-file.ts;模型不支持图片、超过当前端点的单图上限(base64 后,
|
|
8
8
|
* [W5-I] 按端点分档)或任一边 > 8000 px 时只给路径与尺寸);成功后 `ctx.markRead(abs)`。
|
|
9
9
|
* [M-E] 超过 1 MiB 的文本按字节窗口读(read-lines.ts),输出与整读逐字节相同。
|
|
10
|
+
* [#270] 宿主注册过资源时,`scheme://path` 交给宿主读(read-resource.ts),同一套分页与截断。
|
|
10
11
|
*/
|
|
11
12
|
import { readFile, stat } from "node:fs/promises";
|
|
12
13
|
import { internImage } from "../ai/image-intern.js";
|
|
13
14
|
import { displayPath, resolvePath } from "./paths.js";
|
|
14
15
|
import { DEFAULT_MAX_LINES, formatSize, toolOutputBytes, truncateHead } from "./truncate.js";
|
|
15
16
|
import { normalizeToLF, splitBom } from "./edit-fuzzy.js";
|
|
16
|
-
import { STREAM_READ_THRESHOLD, readHeadAsync, readLineWindowAsync } from "./read-lines.js";
|
|
17
|
+
import { STREAM_READ_THRESHOLD, numberLines, readHeadAsync, readLineWindowAsync, } from "./read-lines.js";
|
|
18
|
+
import { executeResourceRead } from "./read-resource.js";
|
|
19
|
+
import { parseResourceRef } from "./resource-ref.js";
|
|
17
20
|
import { MAX_IMAGE_FILE_BYTES, fitImage, imageMimeFromPath, imageSize, sniffImageMime, } from "./image-file.js";
|
|
18
21
|
export { imageSize } from "./image-file.js";
|
|
22
|
+
export { numberLines } from "./read-lines.js";
|
|
19
23
|
const SNIFF_BYTES = 8000;
|
|
20
24
|
export function isBinary(buf) {
|
|
21
25
|
const end = Math.min(buf.length, SNIFF_BYTES);
|
|
@@ -47,10 +51,6 @@ export function directoryHint(shown, ctx) {
|
|
|
47
51
|
}
|
|
48
52
|
return "read a file inside it";
|
|
49
53
|
}
|
|
50
|
-
/** `cat -n` 形状。 */
|
|
51
|
-
export function numberLines(lines, firstLine) {
|
|
52
|
-
return lines.map((line, i) => `${String(firstLine + i).padStart(6)}\t${line}`).join("\n");
|
|
53
|
-
}
|
|
54
54
|
async function readImage(abs, shown, ctx, options) {
|
|
55
55
|
const buf = await readFile(abs);
|
|
56
56
|
const mimeType = sniffImageMime(buf) ?? imageMimeFromPath(abs) ?? "application/octet-stream";
|
|
@@ -78,6 +78,17 @@ async function readImage(abs, shown, ctx, options) {
|
|
|
78
78
|
return { content: blocks, details };
|
|
79
79
|
}
|
|
80
80
|
export async function executeRead(input, ctx, options = {}) {
|
|
81
|
+
// [#270] 有宿主资源时先认 `scheme://path`;没有注册任何资源时不解析,输出逐字节不变
|
|
82
|
+
if (options.resources !== undefined && options.resources.schemes().length > 0) {
|
|
83
|
+
const ref = typeof input.path === "string" ? parseResourceRef(input.path) : undefined;
|
|
84
|
+
if (ref !== undefined) {
|
|
85
|
+
return executeResourceRead(ref, input, ctx, options.resources, {
|
|
86
|
+
...(options.resourceTimeoutMs !== undefined
|
|
87
|
+
? { timeoutMs: options.resourceTimeoutMs }
|
|
88
|
+
: {}),
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
81
92
|
const abs = resolvePath(input.path, ctx.cwd);
|
|
82
93
|
const shown = displayPath(abs, ctx.cwd);
|
|
83
94
|
let info;
|
package/dist/tools/registry.d.ts
CHANGED
|
@@ -16,7 +16,10 @@ import { type ReadToolOptions } from "./read.js";
|
|
|
16
16
|
import { type BashToolOptions } from "./bash.js";
|
|
17
17
|
import { type TaskToolOptions } from "./task.js";
|
|
18
18
|
import { type WebFetchOptions } from "./web/fetch.js";
|
|
19
|
-
/**
|
|
19
|
+
/**
|
|
20
|
+
* [W5-C0] 主工具 → 随它出现 / 消失的伴随工具。[#269] `report` 也跟着 `task`:子会话用它报告状态,父会话
|
|
21
|
+
* 的表里也要有它(运行时拒绝),子会话的工具表才与父逐字节一致。
|
|
22
|
+
*/
|
|
20
23
|
export declare const TOOL_COMPANIONS: Readonly<Record<string, readonly string[]>>;
|
|
21
24
|
/** 按 `TOOL_COMPANIONS` 配对:有主工具就带上(已注册的)伴随工具,没有就去掉。 */
|
|
22
25
|
export declare function pairCompanions(names: Iterable<string>, available: (name: string) => boolean): string[];
|
|
@@ -43,7 +46,7 @@ export interface BuiltinToolOptions {
|
|
|
43
46
|
task?: TaskToolOptions;
|
|
44
47
|
webFetch?: WebFetchOptions;
|
|
45
48
|
}
|
|
46
|
-
/** 全部内置工具(read / write / edit / bash / grep / glob / ls / todo / task / task_ctl / web_fetch)。 */
|
|
49
|
+
/** 全部内置工具(read / write / edit / bash / grep / glob / ls / todo / task / task_ctl / report / web_fetch)。 */
|
|
47
50
|
export declare function builtinTools(options?: BuiltinToolOptions): ToolDefinition[];
|
|
48
51
|
/** 新建注册表并登记全部内置工具;`disabled` 来自 config.tools.disabled。 */
|
|
49
52
|
export declare function createToolRegistry(options?: BuiltinToolOptions & {
|
package/dist/tools/registry.js
CHANGED
|
@@ -23,10 +23,14 @@ import { createLsTool } from "./ls.js";
|
|
|
23
23
|
import { createTodoTool } from "./todo.js";
|
|
24
24
|
import { createTaskTool } from "./task.js";
|
|
25
25
|
import { TASK_CTL_TOOL, createTaskCtlTool } from "./task-ctl.js";
|
|
26
|
+
import { REPORT_TOOL, createReportTool } from "./report.js";
|
|
26
27
|
import { createWebFetchTool } from "./web/fetch.js";
|
|
27
|
-
/**
|
|
28
|
+
/**
|
|
29
|
+
* [W5-C0] 主工具 → 随它出现 / 消失的伴随工具。[#269] `report` 也跟着 `task`:子会话用它报告状态,父会话
|
|
30
|
+
* 的表里也要有它(运行时拒绝),子会话的工具表才与父逐字节一致。
|
|
31
|
+
*/
|
|
28
32
|
export const TOOL_COMPANIONS = Object.freeze({
|
|
29
|
-
task: [TASK_CTL_TOOL],
|
|
33
|
+
task: [TASK_CTL_TOOL, REPORT_TOOL],
|
|
30
34
|
});
|
|
31
35
|
/** 按 `TOOL_COMPANIONS` 配对:有主工具就带上(已注册的)伴随工具,没有就去掉。 */
|
|
32
36
|
export function pairCompanions(names, available) {
|
|
@@ -103,7 +107,7 @@ export class ToolRegistry {
|
|
|
103
107
|
this.activeNames = new Set(pairCompanions(names, (n) => this.entries.has(n) && !this.disabled.has(n)));
|
|
104
108
|
}
|
|
105
109
|
}
|
|
106
|
-
/** 全部内置工具(read / write / edit / bash / grep / glob / ls / todo / task / task_ctl / web_fetch)。 */
|
|
110
|
+
/** 全部内置工具(read / write / edit / bash / grep / glob / ls / todo / task / task_ctl / report / web_fetch)。 */
|
|
107
111
|
export function builtinTools(options = {}) {
|
|
108
112
|
const tools = [
|
|
109
113
|
createReadTool(options.read),
|
|
@@ -116,6 +120,7 @@ export function builtinTools(options = {}) {
|
|
|
116
120
|
createTodoTool(),
|
|
117
121
|
createTaskTool(options.task),
|
|
118
122
|
createTaskCtlTool(),
|
|
123
|
+
createReportTool(),
|
|
119
124
|
createWebFetchTool(options.webFetch),
|
|
120
125
|
];
|
|
121
126
|
return tools;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* `report` 工具:子 Agent 向父会话报告结构化状态(#269,语义见 agents/report.ts)。
|
|
3
|
+
*
|
|
4
|
+
* - `task` 的伴随工具(registry.ts 的 `TOOL_COMPANIONS`):父子会话的工具表里都有它,子会话的工具表才能与父
|
|
5
|
+
* 逐字节一致(缓存前缀,D23);父会话(depth 0)调用被拒并提示直接回答用户,子会话(depth ≥ 1)才记录。
|
|
6
|
+
* - 工具本身不折叠:校验后把这一次调用放进 `details.report`,由 ama runner(session-subagent.ts)按运行折叠
|
|
7
|
+
* 成 `SubagentResult.report`;工具结果文案固定英文。
|
|
8
|
+
* - `humanAction` 的 schema 不写 `type`(子集不支持可空类型):运行时接受字符串或 `null`。
|
|
9
|
+
* - `sequential`:同一轮里多个 `report` 按调用顺序执行,折叠时的「最后一次」是确定的。
|
|
10
|
+
*/
|
|
11
|
+
import type { SubagentReportUpdate, ToolDefinition } from "./types.js";
|
|
12
|
+
export declare const REPORT_TOOL = "report";
|
|
13
|
+
/** 父会话里的拒绝文案(固定英文)。 */
|
|
14
|
+
export declare const REPORT_PARENT_REJECTION: string;
|
|
15
|
+
export interface ReportInput {
|
|
16
|
+
state: string;
|
|
17
|
+
summary: string;
|
|
18
|
+
humanAction?: string | null;
|
|
19
|
+
}
|
|
20
|
+
/** 校验并归一化一次调用;出错返回文案。 */
|
|
21
|
+
export declare function parseReportInput(input: ReportInput): SubagentReportUpdate | string;
|
|
22
|
+
/** 工具结果(固定英文):说明记下了什么。 */
|
|
23
|
+
export declare function reportRecordedText(update: SubagentReportUpdate): string;
|
|
24
|
+
export declare function createReportTool(): ToolDefinition<ReportInput>;
|