@code-yeongyu/senpi 2026.9.15-2 → 2026.9.16
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 +21 -0
- package/dist/core/extensions/kernel-tools-context.d.ts +13 -0
- package/dist/core/extensions/kernel-tools-context.d.ts.map +1 -0
- package/dist/core/extensions/kernel-tools-context.js +3 -0
- package/dist/core/extensions/kernel-tools-context.js.map +1 -0
- package/dist/core/extensions/runner.d.ts +19 -0
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +103 -1
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +21 -0
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/settings-manager.d.ts +23 -0
- package/dist/core/settings-manager.d.ts.map +1 -1
- package/dist/core/settings-manager.js +41 -0
- package/dist/core/settings-manager.js.map +1 -1
- package/dist/core/tools/index.d.ts +1 -1
- package/dist/core/tools/index.d.ts.map +1 -1
- package/dist/core/tools/index.js +4 -3
- package/dist/core/tools/index.js.map +1 -1
- package/dist/core/tools/read.d.ts +3 -1
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +16 -2
- package/dist/core/tools/read.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/docs/extensions.md +3 -0
- package/docs/settings.md +12 -0
- package/node_modules/@code-yeongyu/senpi-codemode/CHANGELOG.md +21 -0
- package/node_modules/@code-yeongyu/senpi-codemode/README.md +24 -0
- package/node_modules/@code-yeongyu/senpi-codemode/package.json +4 -4
- package/node_modules/@code-yeongyu/senpi-codemode/src/bridge/http-server.ts +7 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/bridge/kernel-tools-protocol.ts +85 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/bridge/protocol.ts +6 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/bridges/agent-bridge.ts +30 -11
- package/node_modules/@code-yeongyu/senpi-codemode/src/extension/session-manager.ts +11 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/index.ts +10 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/jl/prelude.jl +46 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/jl/runner.jl +1 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/context-manager.ts +29 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-contract.ts +12 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-context.d.ts +13 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-context.js +7 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-define.d.ts +10 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-define.js +12 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-errors.js +22 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-errors.ts +26 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-host.ts +134 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-naming.js +28 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-parse.d.ts +5 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-parse.js +255 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-pump.js +105 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-registry.d.ts +27 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-registry.js +127 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-schema.js +69 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/kernel-tools-types.ts +42 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/prelude.ts +2 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/worker-core.js +26 -3
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/worker-runtime.js +20 -8
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/worker-slot.ts +6 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/worker-startup.ts +9 -7
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/js/workpool.js +37 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/kernel-tools-unavailable.ts +5 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/py/kernel.ts +13 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/py/prelude.py +57 -3
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/rb/prelude.rb +13 -2
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/rb/workpool.rb +38 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/shared/subprocess-kernel.ts +13 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/kernels/shared/subprocess-queue.ts +2 -0
- package/node_modules/@code-yeongyu/senpi-codemode/src/prompt/eval-prompt-template.ts +4 -2
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/cell-handler.ts +8 -1
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/run-eval-cell.ts +49 -30
- package/node_modules/@code-yeongyu/senpi-codemode/src/tool/types.ts +2 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/empty-assistant-recovery.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/empty-assistant-recovery.js +51 -12
- package/node_modules/@earendil-works/pi-agent-core/dist/empty-assistant-recovery.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/read.d.ts +3 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/read.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/read.js +18 -3
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/tools/read.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/brace-scanner.d.ts +4 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/brace-scanner.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/brace-scanner.js +268 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/brace-scanner.js.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/header-protection.d.ts +18 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/header-protection.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/header-protection.js +69 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/header-protection.js.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/index.d.ts +27 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/index.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/index.js +118 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/index.js.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/lexical-context.d.ts +21 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/lexical-context.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/lexical-context.js +24 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/lexical-context.js.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/lexical-spans.d.ts +12 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/lexical-spans.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/lexical-spans.js +92 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/lexical-spans.js.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/types.d.ts +47 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/types.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/types.js +8 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/read-folders/types.js.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/segmented-read-view.d.ts +52 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/segmented-read-view.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/segmented-read-view.js +155 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/harness/utils/segmented-read-view.js.map +1 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/index.d.ts +2 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/dist/index.js +2 -0
- package/node_modules/@earendil-works/pi-agent-core/dist/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-agent-core/package.json +3 -3
- package/node_modules/@earendil-works/pi-ai/dist/index.d.ts +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/index.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/index.js +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/index.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/.manifest.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/alibaba-token-plan.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/amazon-bedrock.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/fireworks.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/openrouter.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/providers/data/qwen-token-plan.json +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/empty-response-errors.d.ts +15 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/empty-response-errors.d.ts.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/empty-response-errors.js +15 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/empty-response-errors.js.map +1 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/retry.d.ts.map +1 -1
- package/node_modules/@earendil-works/pi-ai/dist/utils/retry.js +10 -0
- package/node_modules/@earendil-works/pi-ai/dist/utils/retry.js.map +1 -1
- package/node_modules/@earendil-works/pi-ai/package.json +2 -2
- package/node_modules/@earendil-works/pi-pty/package.json +1 -1
- package/node_modules/@earendil-works/pi-telemetry/package.json +1 -1
- package/node_modules/@earendil-works/pi-tui/package.json +1 -1
- package/package.json +7 -7
|
@@ -57,8 +57,10 @@ tool_schema(name?) → dict
|
|
|
57
57
|
completion(prompt, model?="default", system?=None, schema?=None) → str | dict
|
|
58
58
|
Oneshot, stateless. \`model\`: \`"smol"\` fast | \`"default"\` session | \`"slow"\` most capable. \`schema\` (JSON-Schema) → parsed structured output.
|
|
59
59
|
{{#if spawns}}agent(prompt, agent?="{{spawnDefaultAgent}}", model?=None, label?=None, schema?=None, handle?=False) → str | dict
|
|
60
|
-
Run a subagent → final output. \`agent\` picks a discovered agent. \`schema\` as in completion(). \`handle\` → workflow node { text, output, handle: \`agent://<id>\`, id, agent } (parsed under \`data\` with \`schema\`).
|
|
61
|
-
{{/if}}
|
|
60
|
+
Run a subagent → final output. \`agent\` picks a discovered agent. \`schema\` as in completion(). \`handle\` → workflow node { text, output, handle: \`agent://<id>\`, id, run_epoch, agent } (parsed under \`data\` with \`schema\`).
|
|
61
|
+
{{/if}}workpool(agent, name, mode?) → { pool_id, push(items), close(), inspect(), cancel() }
|
|
62
|
+
Thin host-tool adapter; agent is a plain-data spec. The host owns scheduling and keyed yields; explicitly close for automatic aggregate delivery. Requires the host workpool tool; omitted mode uses its default.
|
|
63
|
+
parallel(thunks) → list
|
|
62
64
|
Thunks through a bounded pool (as wide as a \`task\` batch), input order kept; a throwing thunk propagates.
|
|
63
65
|
pipeline(items, ...stages) → list
|
|
64
66
|
Map items through one-arg stages with a barrier between stages; each stage receives the previous stage's result.
|
|
@@ -90,6 +90,8 @@ export class CellHandler {
|
|
|
90
90
|
case "init-failed":
|
|
91
91
|
case "result":
|
|
92
92
|
case "closed":
|
|
93
|
+
case "kernel-tool-describe-reply":
|
|
94
|
+
case "kernel-tool-invoke-reply":
|
|
93
95
|
return;
|
|
94
96
|
default:
|
|
95
97
|
throw new TypeError(`Unhandled kernel message: ${String(message)}`);
|
|
@@ -223,7 +225,12 @@ export class CellHandler {
|
|
|
223
225
|
type: "tool-reply",
|
|
224
226
|
callId: message.callId,
|
|
225
227
|
ok: false,
|
|
226
|
-
error: {
|
|
228
|
+
error: {
|
|
229
|
+
message: text,
|
|
230
|
+
...(error instanceof Error && "code" in error && typeof error.code === "string"
|
|
231
|
+
? { code: error.code }
|
|
232
|
+
: {}),
|
|
233
|
+
},
|
|
227
234
|
});
|
|
228
235
|
}
|
|
229
236
|
this.#resultBuilder.emitUpdate(false);
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { randomUUID } from "node:crypto";
|
|
2
2
|
import { join } from "node:path";
|
|
3
|
-
import type
|
|
3
|
+
import { type AgentToolResult, type ExtensionContext, kernelToolsStorage } from "@code-yeongyu/senpi";
|
|
4
4
|
import { DEFAULT_FOREGROUND_WINDOW_SECONDS, defaultCodemodeSettings } from "../config/settings.ts";
|
|
5
|
+
import type { KernelToolsCapability } from "../kernels/js/kernel-tools-types.ts";
|
|
5
6
|
import { TIMEOUT_PAUSE_OP, TIMEOUT_RESUME_OP } from "../timeouts/bridge-timeout.ts";
|
|
6
7
|
import { abortError, CellExecution, defaultTimeoutFactory } from "./cell-execution.ts";
|
|
7
8
|
import { CellHandler, type CellState } from "./cell-handler.ts";
|
|
@@ -11,7 +12,7 @@ import { buildEvalExecutionEventPayload, type EvalExecutionSettleOutcome } from
|
|
|
11
12
|
import { evalTimeoutBehavior } from "./eval-request.ts";
|
|
12
13
|
import type { CreateEvalToolOptions, EvalCellInvocation } from "./eval-tool-options.ts";
|
|
13
14
|
import { describeTimeoutState } from "./interrupt-note.ts";
|
|
14
|
-
import type { EvalToolDetails } from "./types.ts";
|
|
15
|
+
import type { EvalKernel, EvalToolDetails } from "./types.ts";
|
|
15
16
|
|
|
16
17
|
export async function runEvalCell(
|
|
17
18
|
options: CreateEvalToolOptions,
|
|
@@ -166,34 +167,39 @@ async function executeCell(
|
|
|
166
167
|
void pending.catch((error: unknown) => execution.cancel(error));
|
|
167
168
|
}),
|
|
168
169
|
);
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
options.kernelManager.setContext
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
170
|
+
const runBound = async (): Promise<AgentToolResult<EvalToolDetails>> => {
|
|
171
|
+
execution.setKernel(kernel);
|
|
172
|
+
const activeHandler = new CellHandler(kernel, state, {
|
|
173
|
+
executeTool: options.executeTool,
|
|
174
|
+
...(options.listTools === undefined ? {} : { listTools: options.listTools }),
|
|
175
|
+
settings: options.settings ?? defaultCodemodeSettings,
|
|
176
|
+
...(options.complete === undefined ? {} : { complete: options.complete }),
|
|
177
|
+
ctx: bridgeContext,
|
|
178
|
+
...(options.artifactsDir === undefined
|
|
179
|
+
? {}
|
|
180
|
+
: { artifactPath: join(options.artifactsDir, `eval-${randomUUID()}.log`) }),
|
|
181
|
+
...(options.imageResizer === undefined ? {} : { imageResizer: options.imageResizer }),
|
|
182
|
+
});
|
|
183
|
+
handler = activeHandler;
|
|
184
|
+
cellManager.markRunning(
|
|
185
|
+
cell,
|
|
186
|
+
kernel,
|
|
187
|
+
() => activeHandler.liveResult(),
|
|
188
|
+
(error) => execution.cancel(error),
|
|
189
|
+
);
|
|
190
|
+
// Includes a steer already queued at execute start or received while acquiring the kernel.
|
|
191
|
+
onReady();
|
|
192
|
+
if ("setContext" in options.kernelManager && typeof options.kernelManager.setContext === "function") {
|
|
193
|
+
options.kernelManager.setContext(bridgeContext);
|
|
194
|
+
}
|
|
195
|
+
if (invocation.input.reset) await execution.wait(kernel.reset());
|
|
196
|
+
const result = await execution.wait(kernel.run({ cellId: invocation.cellId, code: invocation.input.code }));
|
|
197
|
+
if (result.ok && state.pendingBridgeCalls.length > 0)
|
|
198
|
+
await execution.wait(Promise.all(state.pendingBridgeCalls));
|
|
199
|
+
return await handler.finalize(result);
|
|
200
|
+
};
|
|
201
|
+
const kernelTools = jsKernelTools(kernel, invocation.input.language);
|
|
202
|
+
return kernelTools ? await kernelToolsStorage.run(kernelTools, runBound) : await runBound();
|
|
197
203
|
} catch (error) {
|
|
198
204
|
if (handler && error instanceof Error && error.name === "CodemodeSessionDisposedError")
|
|
199
205
|
return await handler.finalizeCancellation(error);
|
|
@@ -206,3 +212,16 @@ async function executeCell(
|
|
|
206
212
|
if (handler) await handler.flushOutput();
|
|
207
213
|
}
|
|
208
214
|
}
|
|
215
|
+
|
|
216
|
+
function jsKernelTools(kernel: EvalKernel, language: string): KernelToolsCapability | undefined {
|
|
217
|
+
if (language !== "js") return undefined;
|
|
218
|
+
if (!("describeKernelTools" in kernel) || typeof kernel.describeKernelTools !== "function") return undefined;
|
|
219
|
+
const js = kernel as EvalKernel & {
|
|
220
|
+
describeKernelTools: KernelToolsCapability["describe"];
|
|
221
|
+
invokeKernelTool: KernelToolsCapability["invoke"];
|
|
222
|
+
};
|
|
223
|
+
return {
|
|
224
|
+
describe: (names) => js.describeKernelTools(names),
|
|
225
|
+
invoke: (request, signal) => js.invokeKernelTool(request, signal),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
@@ -132,6 +132,8 @@ export interface EvalKernel {
|
|
|
132
132
|
deliverToolReply(message: Extract<HostToKernelMessage, { type: "tool-reply" }>): void;
|
|
133
133
|
reset(): Promise<void>;
|
|
134
134
|
close(): Promise<void>;
|
|
135
|
+
/** Names this kernel has registered; JS collides with other languages in the same session. */
|
|
136
|
+
listKernelToolNames?(): readonly string[];
|
|
135
137
|
}
|
|
136
138
|
|
|
137
139
|
export interface EvalKernelManager {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"empty-assistant-recovery.d.ts","sourceRoot":"","sources":["../src/empty-assistant-recovery.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,GAAG,
|
|
1
|
+
{"version":3,"file":"empty-assistant-recovery.d.ts","sourceRoot":"","sources":["../src/empty-assistant-recovery.ts"],"names":[],"mappings":"AAAA,OAAO,EACN,KAAK,GAAG,EAaR,KAAK,KAAK,EAEV,MAAM,uBAAuB,CAAC;AAC/B,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AA4K3C,wBAAgB,0BAA0B,CAAC,IAAI,SAAS,GAAG,EAAE,KAAK,EAAE,KAAK,CAAC,IAAI,CAAC,EAAE,cAAc,EAAE,QAAQ,GAAG,QAAQ,CAOnH"}
|
|
@@ -1,31 +1,60 @@
|
|
|
1
|
-
import { createAssistantMessageEventStream, getToolCallFormat, hasVisibleAssistantContent, hasVisibleText, shouldRecoverTextToolCalls, } from "@earendil-works/pi-ai";
|
|
2
|
-
const EMPTY_RESPONSE_ERROR = "Model returned an empty response twice";
|
|
3
|
-
const EMPTY_TOOL_USE_ERROR = "Model returned tool_use without a tool call twice";
|
|
1
|
+
import { createAssistantMessageEventStream, EMPTY_RESPONSE_ERROR, EMPTY_TOOL_USE_ERROR, FORWARDED_EMPTY_RESPONSE_ERROR, FORWARDED_EMPTY_TOOL_USE_ERROR, getToolCallFormat, hasKimiTextToolCallRecovery, hasVisibleAssistantContent, hasVisibleText, shouldRecoverTextToolCalls, } from "@earendil-works/pi-ai";
|
|
4
2
|
function isEmptyStop(message) {
|
|
5
3
|
return message.stopReason === "stop" && !hasVisibleAssistantContent(message);
|
|
6
4
|
}
|
|
7
5
|
function isEmptyToolUse(message) {
|
|
8
6
|
return message.stopReason === "toolUse" && !message.content.some((block) => block.type === "toolCall");
|
|
9
7
|
}
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
/** Zero-width or whitespace-only deltas never commit, so format-only noise stays buffered. */
|
|
9
|
+
function isMeaningfulContentEvent(event, policy) {
|
|
10
|
+
switch (event.type) {
|
|
11
|
+
case "toolcall_start":
|
|
12
|
+
return true;
|
|
13
|
+
case "text_delta":
|
|
14
|
+
return hasVisibleText(event.delta);
|
|
15
|
+
case "text_end":
|
|
16
|
+
return hasVisibleText(event.content);
|
|
17
|
+
case "thinking_delta":
|
|
18
|
+
return policy.thinkingCommits && hasVisibleText(event.delta);
|
|
19
|
+
case "thinking_end":
|
|
20
|
+
return policy.thinkingCommits && hasVisibleText(event.content);
|
|
21
|
+
default:
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
12
24
|
}
|
|
13
|
-
function appendRetryDiagnostic(message, type = "empty_assistant_response_recovery") {
|
|
25
|
+
function appendRetryDiagnostic(message, type = "empty_assistant_response_recovery", details = { retries: 1 }) {
|
|
14
26
|
return {
|
|
15
27
|
...message,
|
|
16
|
-
diagnostics: [...(message.diagnostics ?? []), { type, timestamp: Date.now(), details
|
|
28
|
+
diagnostics: [...(message.diagnostics ?? []), { type, timestamp: Date.now(), details }],
|
|
17
29
|
};
|
|
18
30
|
}
|
|
31
|
+
function recoveryDiagnosticType(toolUse) {
|
|
32
|
+
return toolUse ? "empty_tool_use_response_recovery" : "empty_assistant_response_recovery";
|
|
33
|
+
}
|
|
19
34
|
function createEmptyResponseFailure(message, toolUse = false) {
|
|
20
35
|
const errorMessage = toolUse ? EMPTY_TOOL_USE_ERROR : EMPTY_RESPONSE_ERROR;
|
|
21
36
|
return {
|
|
22
|
-
...appendRetryDiagnostic(message, toolUse
|
|
37
|
+
...appendRetryDiagnostic(message, recoveryDiagnosticType(toolUse)),
|
|
23
38
|
content: [{ type: "text", text: errorMessage }],
|
|
24
39
|
stopReason: "error",
|
|
25
40
|
errorMessage,
|
|
26
41
|
};
|
|
27
42
|
}
|
|
28
|
-
|
|
43
|
+
/**
|
|
44
|
+
* An attempt whose reasoning already streamed cannot be replayed here: a second `start` would
|
|
45
|
+
* duplicate the partial message, and stitching this attempt's thinking onto a retry's content
|
|
46
|
+
* would break provider replay of signed thinking blocks. The turn ends as a retryable error
|
|
47
|
+
* (see the classifier in pi-ai's retry.ts) and keeps the content the user already saw, so the
|
|
48
|
+
* session's turn retry re-requests it with the failed attempt dropped from the provider context.
|
|
49
|
+
*/
|
|
50
|
+
function createForwardedEmptyFailure(message, toolUse) {
|
|
51
|
+
return {
|
|
52
|
+
...appendRetryDiagnostic(message, recoveryDiagnosticType(toolUse), { retries: 0, forwarded: true }),
|
|
53
|
+
stopReason: "error",
|
|
54
|
+
errorMessage: toolUse ? FORWARDED_EMPTY_TOOL_USE_ERROR : FORWARDED_EMPTY_RESPONSE_ERROR,
|
|
55
|
+
};
|
|
56
|
+
}
|
|
57
|
+
function createRetryingStream(firstStream, createStream, policy) {
|
|
29
58
|
const outerStream = createAssistantMessageEventStream();
|
|
30
59
|
void (async () => {
|
|
31
60
|
try {
|
|
@@ -39,6 +68,15 @@ function createRetryingStream(firstStream, createStream) {
|
|
|
39
68
|
if (event.type === "done") {
|
|
40
69
|
const emptyToolUse = isEmptyToolUse(event.message);
|
|
41
70
|
if (isEmptyStop(event.message) || emptyToolUse) {
|
|
71
|
+
if (forwarding) {
|
|
72
|
+
outerStream.push({
|
|
73
|
+
type: "error",
|
|
74
|
+
reason: "error",
|
|
75
|
+
error: createForwardedEmptyFailure(event.message, emptyToolUse),
|
|
76
|
+
});
|
|
77
|
+
outerStream.end();
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
42
80
|
if (!retrying) {
|
|
43
81
|
retry = true;
|
|
44
82
|
break;
|
|
@@ -51,7 +89,7 @@ function createRetryingStream(firstStream, createStream) {
|
|
|
51
89
|
const terminal = retrying
|
|
52
90
|
? {
|
|
53
91
|
...event,
|
|
54
|
-
message: appendRetryDiagnostic(event.message, isEmptyToolUse(event.message)
|
|
92
|
+
message: appendRetryDiagnostic(event.message, recoveryDiagnosticType(isEmptyToolUse(event.message))),
|
|
55
93
|
}
|
|
56
94
|
: event;
|
|
57
95
|
if (!forwarding) {
|
|
@@ -76,7 +114,7 @@ function createRetryingStream(firstStream, createStream) {
|
|
|
76
114
|
continue;
|
|
77
115
|
}
|
|
78
116
|
buffered.push(event);
|
|
79
|
-
if (
|
|
117
|
+
if (isMeaningfulContentEvent(event, policy)) {
|
|
80
118
|
for (const pending of buffered)
|
|
81
119
|
outerStream.push(pending);
|
|
82
120
|
forwarding = true;
|
|
@@ -103,9 +141,10 @@ function createRetryingStream(firstStream, createStream) {
|
|
|
103
141
|
export function withEmptyAssistantRecovery(model, streamFunction) {
|
|
104
142
|
if (!shouldRecoverTextToolCalls(model) && getToolCallFormat(model) === undefined)
|
|
105
143
|
return streamFunction;
|
|
144
|
+
const policy = { thinkingCommits: !hasKimiTextToolCallRecovery(model) };
|
|
106
145
|
return async (requestedModel, context, options) => {
|
|
107
146
|
const createStream = () => streamFunction(requestedModel, context, options);
|
|
108
|
-
return createRetryingStream(await createStream(), createStream);
|
|
147
|
+
return createRetryingStream(await createStream(), createStream, policy);
|
|
109
148
|
};
|
|
110
149
|
}
|
|
111
150
|
//# sourceMappingURL=empty-assistant-recovery.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"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AQ,CAAC,CAAC,CAAC,qBAAqB,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC;oBACjG,OAAO;gBACR,CAAC;gBACD,QAAQ,GAAG,IAAI,CAAC;gBAChB,MAAM,GAAG,MAAM,YAAY,EAAE,CAAC;YAC/B,CAAC;QACF,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YAChB,WAAW,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;QACzB,CAAC;IACF,CAAC,CAAC,EAAE,CAAC;IAEL,OAAO,WAAW,CAAC;AACpB,CAAC;AAED,yFAAyF;AACzF,4FAA4F;AAC5F,sFAAsF;AACtF,wFAAwF;AACxF,MAAM,UAAU,0BAA0B,CAAmB,KAAkB,EAAE,cAAwB;IACxG,IAAI,CAAC,0BAA0B,CAAC,KAAK,CAAC,IAAI,iBAAiB,CAAC,KAAK,CAAC,KAAK,SAAS;QAAE,OAAO,cAAc,CAAC;IACxG,OAAO,KAAK,EAAE,cAAc,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE;QACjD,MAAM,YAAY,GAAG,GAAyB,EAAE,CAAC,cAAc,CAAC,cAAc,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAClG,OAAO,oBAAoB,CAAC,MAAM,YAAY,EAAE,EAAE,YAAY,CAAC,CAAC;IACjE,CAAC,CAAC;AACH,CAAC","sourcesContent":["import {\n\ttype Api,\n\ttype AssistantMessage,\n\ttype AssistantMessageEvent,\n\ttype AssistantMessageEventStream,\n\tcreateAssistantMessageEventStream,\n\tgetToolCallFormat,\n\thasVisibleAssistantContent,\n\thasVisibleText,\n\ttype Model,\n\tshouldRecoverTextToolCalls,\n} from \"@earendil-works/pi-ai\";\nimport type { StreamFn } from \"./types.ts\";\n\ntype StreamFactory = () => AssistantMessageEventStream | Promise<AssistantMessageEventStream>;\n\nconst EMPTY_RESPONSE_ERROR = \"Model returned an empty response twice\";\nconst EMPTY_TOOL_USE_ERROR = \"Model returned tool_use without a tool call twice\";\n\nfunction isEmptyStop(message: AssistantMessage): boolean {\n\treturn message.stopReason === \"stop\" && !hasVisibleAssistantContent(message);\n}\n\nfunction isEmptyToolUse(message: AssistantMessage): boolean {\n\treturn message.stopReason === \"toolUse\" && !message.content.some((block) => block.type === \"toolCall\");\n}\n\nfunction eventStartsVisibleContent(event: AssistantMessageEvent): boolean {\n\treturn event.type === \"toolcall_start\" || (event.type === \"text_delta\" && hasVisibleText(event.delta));\n}\n\nfunction appendRetryDiagnostic(\n\tmessage: AssistantMessage,\n\ttype = \"empty_assistant_response_recovery\",\n): AssistantMessage {\n\treturn {\n\t\t...message,\n\t\tdiagnostics: [...(message.diagnostics ?? []), { type, timestamp: Date.now(), details: { retries: 1 } }],\n\t};\n}\n\nfunction createEmptyResponseFailure(message: AssistantMessage, toolUse = false): AssistantMessage {\n\tconst errorMessage = toolUse ? EMPTY_TOOL_USE_ERROR : EMPTY_RESPONSE_ERROR;\n\treturn {\n\t\t...appendRetryDiagnostic(message, toolUse ? \"empty_tool_use_response_recovery\" : undefined),\n\t\tcontent: [{ type: \"text\", text: errorMessage }],\n\t\tstopReason: \"error\",\n\t\terrorMessage,\n\t};\n}\n\nfunction createRetryingStream(firstStream: AssistantMessageEventStream, createStream: StreamFactory) {\n\tconst outerStream = createAssistantMessageEventStream();\n\n\tvoid (async (): Promise<void> => {\n\t\ttry {\n\t\t\tlet stream = firstStream;\n\t\t\tlet retrying = false;\n\t\t\tfor (;;) {\n\t\t\t\tconst buffered: AssistantMessageEvent[] = [];\n\t\t\t\tlet forwarding = false;\n\t\t\t\tlet retry = false;\n\t\t\t\tfor await (const event of stream) {\n\t\t\t\t\tif (event.type === \"done\") {\n\t\t\t\t\t\tconst emptyToolUse = isEmptyToolUse(event.message);\n\t\t\t\t\t\tif (isEmptyStop(event.message) || emptyToolUse) {\n\t\t\t\t\t\t\tif (!retrying) {\n\t\t\t\t\t\t\t\tretry = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tconst error = createEmptyResponseFailure(event.message, emptyToolUse);\n\t\t\t\t\t\t\touterStream.push({ type: \"error\", reason: \"error\", error });\n\t\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst terminal = retrying\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t...event,\n\t\t\t\t\t\t\t\t\tmessage: appendRetryDiagnostic(\n\t\t\t\t\t\t\t\t\t\tevent.message,\n\t\t\t\t\t\t\t\t\t\tisEmptyToolUse(event.message) ? \"empty_tool_use_response_recovery\" : undefined,\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: event;\n\t\t\t\t\t\tif (!forwarding) {\n\t\t\t\t\t\t\tfor (const pending of buffered) outerStream.push(pending);\n\t\t\t\t\t\t}\n\t\t\t\t\t\touterStream.push(terminal);\n\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tif (event.type === \"error\") {\n\t\t\t\t\t\tif (!forwarding) {\n\t\t\t\t\t\t\tfor (const pending of buffered) outerStream.push(pending);\n\t\t\t\t\t\t}\n\t\t\t\t\t\touterStream.push(event);\n\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tif (forwarding) {\n\t\t\t\t\t\touterStream.push(event);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tbuffered.push(event);\n\t\t\t\t\tif (eventStartsVisibleContent(event)) {\n\t\t\t\t\t\tfor (const pending of buffered) outerStream.push(pending);\n\t\t\t\t\t\tforwarding = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!retry) {\n\t\t\t\t\touterStream.end(retrying ? appendRetryDiagnostic(await stream.result()) : await stream.result());\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tretrying = true;\n\t\t\t\tstream = await createStream();\n\t\t\t}\n\t\t} catch (error) {\n\t\t\touterStream.fail(error);\n\t\t}\n\t})();\n\n\treturn outerStream;\n}\n\n// Wrapping replaces the provider stream with a buffering proxy, which does not carry the\n// underlying stream's liveness surface (trackLocalWork/hasPendingLocalWork) that the loop's\n// idle watchdog reads. Only wrap models that actually need stream-level recovery; the\n// empty-tool_use contradiction is normalized for every model by the agent loop instead.\nexport function withEmptyAssistantRecovery<TApi extends Api>(model: Model<TApi>, streamFunction: StreamFn): StreamFn {\n\tif (!shouldRecoverTextToolCalls(model) && getToolCallFormat(model) === undefined) return streamFunction;\n\treturn async (requestedModel, context, options) => {\n\t\tconst createStream = (): ReturnType<StreamFn> => streamFunction(requestedModel, context, options);\n\t\treturn createRetryingStream(await createStream(), createStream);\n\t};\n}\n"]}
|
|
1
|
+
{"version":3,"file":"empty-assistant-recovery.js","sourceRoot":"","sources":["../src/empty-assistant-recovery.ts"],"names":[],"mappings":"AAAA,OAAO,EAKN,iCAAiC,EACjC,oBAAoB,EACpB,oBAAoB,EACpB,8BAA8B,EAC9B,8BAA8B,EAC9B,iBAAiB,EACjB,2BAA2B,EAC3B,0BAA0B,EAC1B,cAAc,EAEd,0BAA0B,GAC1B,MAAM,uBAAuB,CAAC;AAK/B,SAAS,WAAW,CAAC,OAAyB;IAC7C,OAAO,OAAO,CAAC,UAAU,KAAK,MAAM,IAAI,CAAC,0BAA0B,CAAC,OAAO,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,cAAc,CAAC,OAAyB;IAChD,OAAO,OAAO,CAAC,UAAU,KAAK,SAAS,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,KAAK,UAAU,CAAC,CAAC;AACxG,CAAC;AAcD,8FAA8F;AAC9F,SAAS,wBAAwB,CAAC,KAA4B,EAAE,MAAo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c,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAClG,OAAO,oBAAoB,CAAC,MAAM,YAAY,EAAE,EAAE,YAAY,EAAE,MAAM,CAAC,CAAC;IACzE,CAAC,CAAC;AACH,CAAC","sourcesContent":["import {\n\ttype Api,\n\ttype AssistantMessage,\n\ttype AssistantMessageEvent,\n\ttype AssistantMessageEventStream,\n\tcreateAssistantMessageEventStream,\n\tEMPTY_RESPONSE_ERROR,\n\tEMPTY_TOOL_USE_ERROR,\n\tFORWARDED_EMPTY_RESPONSE_ERROR,\n\tFORWARDED_EMPTY_TOOL_USE_ERROR,\n\tgetToolCallFormat,\n\thasKimiTextToolCallRecovery,\n\thasVisibleAssistantContent,\n\thasVisibleText,\n\ttype Model,\n\tshouldRecoverTextToolCalls,\n} from \"@earendil-works/pi-ai\";\nimport type { StreamFn } from \"./types.ts\";\n\ntype StreamFactory = () => AssistantMessageEventStream | Promise<AssistantMessageEventStream>;\n\nfunction isEmptyStop(message: AssistantMessage): boolean {\n\treturn message.stopReason === \"stop\" && !hasVisibleAssistantContent(message);\n}\n\nfunction isEmptyToolUse(message: AssistantMessage): boolean {\n\treturn message.stopReason === \"toolUse\" && !message.content.some((block) => block.type === \"toolCall\");\n}\n\ninterface CommitPolicy {\n\t/**\n\t * Whether reasoning commits the attempt. True for models whose thinking channel is native\n\t * (Claude, antml/other text formats): a reasoning model shows its work for seconds before the\n\t * first text or tool call, and holding that back blanks the transcript for the whole phase.\n\t * False for the Kimi XTML lane: its thinking channel is the documented misrouting vector for\n\t * text tool calls and `recoverKimiXtmlThinking` only rewrites the finished message, so a\n\t * leaked protocol fragment forwarded live could never be retracted (#759).\n\t */\n\treadonly thinkingCommits: boolean;\n}\n\n/** Zero-width or whitespace-only deltas never commit, so format-only noise stays buffered. */\nfunction isMeaningfulContentEvent(event: AssistantMessageEvent, policy: CommitPolicy): boolean {\n\tswitch (event.type) {\n\t\tcase \"toolcall_start\":\n\t\t\treturn true;\n\t\tcase \"text_delta\":\n\t\t\treturn hasVisibleText(event.delta);\n\t\tcase \"text_end\":\n\t\t\treturn hasVisibleText(event.content);\n\t\tcase \"thinking_delta\":\n\t\t\treturn policy.thinkingCommits && hasVisibleText(event.delta);\n\t\tcase \"thinking_end\":\n\t\t\treturn policy.thinkingCommits && hasVisibleText(event.content);\n\t\tdefault:\n\t\t\treturn false;\n\t}\n}\n\nfunction appendRetryDiagnostic(\n\tmessage: AssistantMessage,\n\ttype = \"empty_assistant_response_recovery\",\n\tdetails: Record<string, unknown> = { retries: 1 },\n): AssistantMessage {\n\treturn {\n\t\t...message,\n\t\tdiagnostics: [...(message.diagnostics ?? []), { type, timestamp: Date.now(), details }],\n\t};\n}\n\nfunction recoveryDiagnosticType(toolUse: boolean): string {\n\treturn toolUse ? \"empty_tool_use_response_recovery\" : \"empty_assistant_response_recovery\";\n}\n\nfunction createEmptyResponseFailure(message: AssistantMessage, toolUse = false): AssistantMessage {\n\tconst errorMessage = toolUse ? EMPTY_TOOL_USE_ERROR : EMPTY_RESPONSE_ERROR;\n\treturn {\n\t\t...appendRetryDiagnostic(message, recoveryDiagnosticType(toolUse)),\n\t\tcontent: [{ type: \"text\", text: errorMessage }],\n\t\tstopReason: \"error\",\n\t\terrorMessage,\n\t};\n}\n\n/**\n * An attempt whose reasoning already streamed cannot be replayed here: a second `start` would\n * duplicate the partial message, and stitching this attempt's thinking onto a retry's content\n * would break provider replay of signed thinking blocks. The turn ends as a retryable error\n * (see the classifier in pi-ai's retry.ts) and keeps the content the user already saw, so the\n * session's turn retry re-requests it with the failed attempt dropped from the provider context.\n */\nfunction createForwardedEmptyFailure(message: AssistantMessage, toolUse: boolean): AssistantMessage {\n\treturn {\n\t\t...appendRetryDiagnostic(message, recoveryDiagnosticType(toolUse), { retries: 0, forwarded: true }),\n\t\tstopReason: \"error\",\n\t\terrorMessage: toolUse ? FORWARDED_EMPTY_TOOL_USE_ERROR : FORWARDED_EMPTY_RESPONSE_ERROR,\n\t};\n}\n\nfunction createRetryingStream(\n\tfirstStream: AssistantMessageEventStream,\n\tcreateStream: StreamFactory,\n\tpolicy: CommitPolicy,\n) {\n\tconst outerStream = createAssistantMessageEventStream();\n\n\tvoid (async (): Promise<void> => {\n\t\ttry {\n\t\t\tlet stream = firstStream;\n\t\t\tlet retrying = false;\n\t\t\tfor (;;) {\n\t\t\t\tconst buffered: AssistantMessageEvent[] = [];\n\t\t\t\tlet forwarding = false;\n\t\t\t\tlet retry = false;\n\t\t\t\tfor await (const event of stream) {\n\t\t\t\t\tif (event.type === \"done\") {\n\t\t\t\t\t\tconst emptyToolUse = isEmptyToolUse(event.message);\n\t\t\t\t\t\tif (isEmptyStop(event.message) || emptyToolUse) {\n\t\t\t\t\t\t\tif (forwarding) {\n\t\t\t\t\t\t\t\touterStream.push({\n\t\t\t\t\t\t\t\t\ttype: \"error\",\n\t\t\t\t\t\t\t\t\treason: \"error\",\n\t\t\t\t\t\t\t\t\terror: createForwardedEmptyFailure(event.message, emptyToolUse),\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (!retrying) {\n\t\t\t\t\t\t\t\tretry = true;\n\t\t\t\t\t\t\t\tbreak;\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tconst error = createEmptyResponseFailure(event.message, emptyToolUse);\n\t\t\t\t\t\t\touterStream.push({ type: \"error\", reason: \"error\", error });\n\t\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\t\treturn;\n\t\t\t\t\t\t}\n\t\t\t\t\t\tconst terminal = retrying\n\t\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\t\t...event,\n\t\t\t\t\t\t\t\t\tmessage: appendRetryDiagnostic(\n\t\t\t\t\t\t\t\t\t\tevent.message,\n\t\t\t\t\t\t\t\t\t\trecoveryDiagnosticType(isEmptyToolUse(event.message)),\n\t\t\t\t\t\t\t\t\t),\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t: event;\n\t\t\t\t\t\tif (!forwarding) {\n\t\t\t\t\t\t\tfor (const pending of buffered) outerStream.push(pending);\n\t\t\t\t\t\t}\n\t\t\t\t\t\touterStream.push(terminal);\n\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tif (event.type === \"error\") {\n\t\t\t\t\t\tif (!forwarding) {\n\t\t\t\t\t\t\tfor (const pending of buffered) outerStream.push(pending);\n\t\t\t\t\t\t}\n\t\t\t\t\t\touterStream.push(event);\n\t\t\t\t\t\touterStream.end();\n\t\t\t\t\t\treturn;\n\t\t\t\t\t}\n\t\t\t\t\tif (forwarding) {\n\t\t\t\t\t\touterStream.push(event);\n\t\t\t\t\t\tcontinue;\n\t\t\t\t\t}\n\t\t\t\t\tbuffered.push(event);\n\t\t\t\t\tif (isMeaningfulContentEvent(event, policy)) {\n\t\t\t\t\t\tfor (const pending of buffered) outerStream.push(pending);\n\t\t\t\t\t\tforwarding = true;\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\tif (!retry) {\n\t\t\t\t\touterStream.end(retrying ? appendRetryDiagnostic(await stream.result()) : await stream.result());\n\t\t\t\t\treturn;\n\t\t\t\t}\n\t\t\t\tretrying = true;\n\t\t\t\tstream = await createStream();\n\t\t\t}\n\t\t} catch (error) {\n\t\t\touterStream.fail(error);\n\t\t}\n\t})();\n\n\treturn outerStream;\n}\n\n// Wrapping replaces the provider stream with a buffering proxy, which does not carry the\n// underlying stream's liveness surface (trackLocalWork/hasPendingLocalWork) that the loop's\n// idle watchdog reads. Only wrap models that actually need stream-level recovery; the\n// empty-tool_use contradiction is normalized for every model by the agent loop instead.\nexport function withEmptyAssistantRecovery<TApi extends Api>(model: Model<TApi>, streamFunction: StreamFn): StreamFn {\n\tif (!shouldRecoverTextToolCalls(model) && getToolCallFormat(model) === undefined) return streamFunction;\n\tconst policy: CommitPolicy = { thinkingCommits: !hasKimiTextToolCallRecovery(model) };\n\treturn async (requestedModel, context, options) => {\n\t\tconst createStream = (): ReturnType<StreamFn> => streamFunction(requestedModel, context, options);\n\t\treturn createRetryingStream(await createStream(), createStream, policy);\n\t};\n}\n"]}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { type Static, Type } from "typebox";
|
|
2
2
|
import type { Context } from "../context.ts";
|
|
3
3
|
import type { AgentHarnessTool } from "../types.ts";
|
|
4
|
+
import { type ReadFolder } from "../utils/read-folders/index.ts";
|
|
4
5
|
import { type TruncationResult } from "../utils/truncate.ts";
|
|
5
6
|
import type { ExecutionToolContext } from "./tool-context.ts";
|
|
6
7
|
declare const readSchema: Type.TObject<{
|
|
@@ -29,6 +30,8 @@ export interface ReadToolOptions {
|
|
|
29
30
|
autoResizeImages?: boolean;
|
|
30
31
|
/** Optional image conversion/resizing implementation. */
|
|
31
32
|
imageProcessor?: ReadImageProcessor;
|
|
33
|
+
/** Structural folder. Default options select the measured folder; omit here for verbatim reads. */
|
|
34
|
+
folder?: ReadFolder;
|
|
32
35
|
}
|
|
33
36
|
export declare function createReadTool<TContext extends ExecutionToolContext = ExecutionToolContext>(options?: ReadToolOptions): AgentHarnessTool<TContext, typeof readSchema, ReadToolDetails | undefined>;
|
|
34
37
|
export {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read.d.ts","sourceRoot":"","sources":["../../../src/harness/tools/read.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,KAAK,MAAM,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAC5C,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,eAAe,CAAC;AAC7C,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AAEpD,OAAO,EAIN,KAAK,gBAAgB,EAErB,MAAM,sBAAsB,CAAC;AAG9B,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,mBAAmB,CAAC;AAE9D,QAAA,MAAM,UAAU;;;;EAId,CAAC;AAEH,MAAM,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,UAAU,CAAC,CAAC;AAEtD,MAAM,WAAW,eAAe;IAC/B,UAAU,CAAC,EAAE,gBAAgB,CAAC;CAC9B;AAED,MAAM,MAAM,wBAAwB,GACjC;IAAE,EAAE,EAAE,IAAI,CAAC;IAAC,IAAI,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,EAAE,CAAA;CAAE,GAC7D;IAAE,EAAE,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,CAAC;AAElC,MAAM,MAAM,kBAAkB,GAAG,CAChC,KAAK,EAAE,UAAU,EACjB,QAAQ,EAAE,MAAM,EAChB,OAAO,EAAE;IAAE,gBAAgB,EAAE,OAAO,CAAA;CAAE,EACtC,OAAO,EAAE,OAAO,KACZ,OAAO,CAAC,wBAAwB,CAAC,CAAC;AAEvC,MAAM,WAAW,eAAe;IAC/B,+EAA+E;IAC/E,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,yDAAyD;IACzD,cAAc,CAAC,EAAE,kBAAkB,CAAC;IACpC,mGAAmG;IACnG,MAAM,CAAC,EAAE,UAAU,CAAC;CACpB;AAED,wBAAgB,cAAc,CAAC,QAAQ,SAAS,oBAAoB,GAAG,oBAAoB,EAC1F,OAAO,GAAE,eAAgD,GACvD,gBAAgB,CAAC,QAAQ,EAAE,OAAO,UAAU,EAAE,eAAe,GAAG,SAAS,CAAC,CA+G5E"}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { Type } from "typebox";
|
|
2
|
-
import { getOrThrow } from "../types.js";
|
|
2
|
+
import { FileError, getOrThrow } from "../types.js";
|
|
3
|
+
import { selectedReadFolder } from "../utils/read-folders/index.js";
|
|
4
|
+
import { createDefaultReadSummary } from "../utils/segmented-read-view.js";
|
|
3
5
|
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, truncateHead, } from "../utils/truncate.js";
|
|
4
6
|
import { detectSupportedImageMimeType, encodeBase64 } from "./image.js";
|
|
5
7
|
import { resolveReadToolPath } from "./path-utils.js";
|
|
@@ -8,7 +10,7 @@ const readSchema = Type.Object({
|
|
|
8
10
|
offset: Type.Optional(Type.Number({ description: "Line number to start reading from (1-indexed)" })),
|
|
9
11
|
limit: Type.Optional(Type.Number({ description: "Maximum number of lines to read" })),
|
|
10
12
|
});
|
|
11
|
-
export function createReadTool(options) {
|
|
13
|
+
export function createReadTool(options = { folder: selectedReadFolder }) {
|
|
12
14
|
return {
|
|
13
15
|
name: "read",
|
|
14
16
|
label: "read",
|
|
@@ -17,6 +19,8 @@ export function createReadTool(options) {
|
|
|
17
19
|
async execute(_toolCallId, { path, offset, limit }, _onUpdate, { env }, _invocation, context) {
|
|
18
20
|
const absolutePath = await resolveReadToolPath(env, path, context);
|
|
19
21
|
const bytes = getOrThrow(await env.readBinaryFile(absolutePath, context));
|
|
22
|
+
if (context.abortSignal?.aborted)
|
|
23
|
+
throw new FileError("aborted", "Operation aborted", absolutePath);
|
|
20
24
|
const mimeType = detectSupportedImageMimeType(bytes);
|
|
21
25
|
if (mimeType) {
|
|
22
26
|
if (options?.imageProcessor) {
|
|
@@ -74,9 +78,20 @@ export function createReadTool(options) {
|
|
|
74
78
|
selectedContent = allLines.slice(startLine).join("\n");
|
|
75
79
|
}
|
|
76
80
|
const truncation = truncateHead(selectedContent);
|
|
81
|
+
const summary = createDefaultReadSummary({
|
|
82
|
+
path: absolutePath,
|
|
83
|
+
text: textContent,
|
|
84
|
+
offset,
|
|
85
|
+
limit,
|
|
86
|
+
folder: options.folder,
|
|
87
|
+
truncated: truncation.truncated,
|
|
88
|
+
});
|
|
77
89
|
let outputText;
|
|
78
90
|
let details;
|
|
79
|
-
if (
|
|
91
|
+
if (summary) {
|
|
92
|
+
outputText = summary.text;
|
|
93
|
+
}
|
|
94
|
+
else if (truncation.firstLineExceedsLimit) {
|
|
80
95
|
const firstLineSize = formatSize(new TextEncoder().encode(allLines[startLine]).byteLength);
|
|
81
96
|
outputText = `[Line ${startLineDisplay} is ${firstLineSize}, exceeds ${formatSize(DEFAULT_MAX_BYTES)} limit. Use bash: sed -n '${startLineDisplay}p' ${path} | head -c ${DEFAULT_MAX_BYTES}]`;
|
|
82
97
|
details = { truncation };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"read.js","sourceRoot":"","sources":["../../../src/harness/tools/read.ts"],"names":[],"mappings":"AACA,OAAO,EAAe,IAAI,EAAE,MAAM,SAAS,CAAC;AAG5C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EACN,iBAAiB,EACjB,iBAAiB,EACjB,UAAU,EAEV,YAAY,GACZ,MAAM,sBAAsB,CAAC;AAC9B,OAAO,EAAE,4BAA4B,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AACxE,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAC;AAGtD,MAAM,UAAU,GAAG,IAAI,CAAC,MAAM,CAAC;IAC9B,IAAI,EAAE,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,iDAAiD,EAAE,CAAC;IACrF,MAAM,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,+CAA+C,EAAE,CAAC,CAAC;IACpG,KAAK,EAAE,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,WAAW,EAAE,iCAAiC,EAAE,CAAC,CAAC;CACrF,CAAC,CAAC;AA0BH,MAAM,UAAU,cAAc,CAC7B,OAAyB;IAEzB,OAAO;QACN,IAAI,EAAE,MAAM;QACZ,KAAK,EAAE,MAAM;QACb,WAAW,EAAE,kKAAkK,iBAAiB,aAAa,iBAAiB,GAAG,IAAI,kIAAkI;QACvW,UAAU,EAAE,UAAU;QACtB,KAAK,CAAC,OAAO,CAAC,WAAW,EAAE,EAAE,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,EAAE,SAAS,EAAE,EAAE,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AAO,GAAG,EAAE,UAAU,EAAE,CAAC;YAC1B,CAAC;iBAAM,IAAI,gBAAgB,KAAK,SAAS,IAAI,SAAS,GAAG,gBAAgB,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;gBAC7F,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,SAAS,GAAG,gBAAgB,CAAC,CAAC;gBACnE,MAAM,UAAU,GAAG,SAAS,GAAG,gBAAgB,GAAG,CAAC,CAAC;gBACpD,UAAU,GAAG,GAAG,UAAU,CAAC,OAAO,QAAQ,SAAS,mCAAmC,UAAU,gBAAgB,CAAC;YAClH,CAAC;iBAAM,CAAC;gBACP,UAAU,GAAG,UAAU,CAAC,OAAO,CAAC;YACjC,CAAC;YAED,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC;QACnE,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { ImageContent, TextContent } from \"@earendil-works/pi-ai\";\nimport { type Static, Type } from \"typebox\";\nimport type { Context } from \"../context.ts\";\nimport type { AgentHarnessTool } from \"../types.ts\";\nimport { getOrThrow } from \"../types.ts\";\nimport {\n\tDEFAULT_MAX_BYTES,\n\tDEFAULT_MAX_LINES,\n\tformatSize,\n\ttype TruncationResult,\n\ttruncateHead,\n} from \"../utils/truncate.ts\";\nimport { detectSupportedImageMimeType, encodeBase64 } from \"./image.ts\";\nimport { resolveReadToolPath } from \"./path-utils.ts\";\nimport type { ExecutionToolContext } from \"./tool-context.ts\";\n\nconst readSchema = Type.Object({\n\tpath: Type.String({ description: \"Path to the file to read (relative or absolute)\" }),\n\toffset: Type.Optional(Type.Number({ description: \"Line number to start reading from (1-indexed)\" })),\n\tlimit: Type.Optional(Type.Number({ description: \"Maximum number of lines to read\" })),\n});\n\nexport type ReadToolInput = Static<typeof readSchema>;\n\nexport interface ReadToolDetails {\n\ttruncation?: TruncationResult;\n}\n\nexport type ReadImageProcessorResult =\n\t| { ok: true; data: string; mimeType: string; hints: string[] }\n\t| { ok: false; message: string };\n\nexport type ReadImageProcessor = (\n\tbytes: Uint8Array,\n\tmimeType: string,\n\toptions: { autoResizeImages: boolean },\n\tcontext: Context,\n) => Promise<ReadImageProcessorResult>;\n\nexport interface ReadToolOptions {\n\t/** Whether an injected image processor should resize images. Default: true. */\n\tautoResizeImages?: boolean;\n\t/** Optional image conversion/resizing implementation. */\n\timageProcessor?: ReadImageProcessor;\n}\n\nexport function createReadTool<TContext extends ExecutionToolContext = ExecutionToolContext>(\n\toptions?: ReadToolOptions,\n): AgentHarnessTool<TContext, typeof readSchema, ReadToolDetails | undefined> {\n\treturn {\n\t\tname: \"read\",\n\t\tlabel: \"read\",\n\t\tdescription: `Read the contents of a file. Supports text files and images (jpg, png, gif, webp, bmp). Images are sent as attachments. For text files, output is truncated to ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete.`,\n\t\tparameters: readSchema,\n\t\tasync execute(_toolCallId, { path, offset, limit }, _onUpdate, { env }, _invocation, context) {\n\t\t\tconst absolutePath = await resolveReadToolPath(env, path, context);\n\t\t\tconst bytes = getOrThrow(await env.readBinaryFile(absolutePath, context));\n\t\t\tconst mimeType = detectSupportedImageMimeType(bytes);\n\t\t\tif (mimeType) {\n\t\t\t\tif (options?.imageProcessor) {\n\t\t\t\t\tconst processed = await options.imageProcessor(\n\t\t\t\t\t\tbytes,\n\t\t\t\t\t\tmimeType,\n\t\t\t\t\t\t{ autoResizeImages: options.autoResizeImages ?? true },\n\t\t\t\t\t\tcontext,\n\t\t\t\t\t);\n\t\t\t\t\tif (!processed.ok) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: `Read image file [${mimeType}]\\n${processed.message}` }],\n\t\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\tconst hints = processed.hints.length > 0 ? `\\n${processed.hints.join(\"\\n\")}` : \"\";\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{ type: \"text\", text: `Read image file [${processed.mimeType}]${hints}` },\n\t\t\t\t\t\t\t{ type: \"image\", data: processed.data, mimeType: processed.mimeType },\n\t\t\t\t\t\t] satisfies Array<TextContent | ImageContent>,\n\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tif (mimeType === \"image/bmp\") {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: \"Read image file [image/bmp]\\n[Image omitted: configure an imageProcessor to convert BMP images.]\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{ type: \"text\", text: `Read image file [${mimeType}]` },\n\t\t\t\t\t\t{ type: \"image\", data: encodeBase64(bytes), mimeType },\n\t\t\t\t\t] satisfies Array<TextContent | ImageContent>,\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst textContent = new TextDecoder().decode(bytes);\n\t\t\tconst allLines = textContent.split(\"\\n\");\n\t\t\tconst totalFileLines = allLines.length;\n\t\t\tconst startLine = offset ? Math.max(0, offset - 1) : 0;\n\t\t\tconst startLineDisplay = startLine + 1;\n\t\t\tif (startLine >= allLines.length) {\n\t\t\t\tthrow new Error(`Offset ${offset} is beyond end of file (${allLines.length} lines total)`);\n\t\t\t}\n\n\t\t\tlet selectedContent: string;\n\t\t\tlet userLimitedLines: number | undefined;\n\t\t\tif (limit !== undefined) {\n\t\t\t\tconst endLine = Math.min(startLine + limit, allLines.length);\n\t\t\t\tselectedContent = allLines.slice(startLine, endLine).join(\"\\n\");\n\t\t\t\tuserLimitedLines = endLine - startLine;\n\t\t\t} else {\n\t\t\t\tselectedContent = allLines.slice(startLine).join(\"\\n\");\n\t\t\t}\n\n\t\t\tconst truncation = truncateHead(selectedContent);\n\t\t\tlet outputText: string;\n\t\t\tlet details: ReadToolDetails | undefined;\n\t\t\tif (truncation.firstLineExceedsLimit) {\n\t\t\t\tconst firstLineSize = formatSize(new TextEncoder().encode(allLines[startLine]).byteLength);\n\t\t\t\toutputText = `[Line ${startLineDisplay} is ${firstLineSize}, exceeds ${formatSize(DEFAULT_MAX_BYTES)} limit. Use bash: sed -n '${startLineDisplay}p' ${path} | head -c ${DEFAULT_MAX_BYTES}]`;\n\t\t\t\tdetails = { truncation };\n\t\t\t} else if (truncation.truncated) {\n\t\t\t\tconst endLineDisplay = startLineDisplay + truncation.outputLines - 1;\n\t\t\t\tconst nextOffset = endLineDisplay + 1;\n\t\t\t\toutputText = truncation.content;\n\t\t\t\tif (truncation.truncatedBy === \"lines\") {\n\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLineDisplay}-${endLineDisplay} of ${totalFileLines}. Use offset=${nextOffset} to continue.]`;\n\t\t\t\t} else {\n\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLineDisplay}-${endLineDisplay} of ${totalFileLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Use offset=${nextOffset} to continue.]`;\n\t\t\t\t}\n\t\t\t\tdetails = { truncation };\n\t\t\t} else if (userLimitedLines !== undefined && startLine + userLimitedLines < allLines.length) {\n\t\t\t\tconst remaining = allLines.length - (startLine + userLimitedLines);\n\t\t\t\tconst nextOffset = startLine + userLimitedLines + 1;\n\t\t\t\toutputText = `${truncation.content}\\n\\n[${remaining} more lines in file. Use offset=${nextOffset} to continue.]`;\n\t\t\t} else {\n\t\t\t\toutputText = truncation.content;\n\t\t\t}\n\n\t\t\treturn { content: [{ type: \"text\", text: outputText }], details };\n\t\t},\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AAC;gBACrE,MAAM,UAAU,GAAG,cAAc,GAAG,CAAC,CAAC;gBACtC,UAAU,GAAG,UAAU,CAAC,OAAO,CAAC;gBAChC,IAAI,UAAU,CAAC,WAAW,KAAK,OAAO,EAAE,CAAC;oBACxC,UAAU,IAAI,sBAAsB,gBAAgB,IAAI,cAAc,OAAO,cAAc,gBAAgB,UAAU,gBAAgB,CAAC;gBACvI,CAAC;qBAAM,CAAC;oBACP,UAAU,IAAI,sBAAsB,gBAAgB,IAAI,cAAc,OAAO,cAAc,KAAK,UAAU,CAAC,iBAAiB,CAAC,uBAAuB,UAAU,gBAAgB,CAAC;gBAChL,CAAC;gBACD,OAAO,GAAG,EAAE,UAAU,EAAE,CAAC;YAC1B,CAAC;iBAAM,IAAI,gBAAgB,KAAK,SAAS,IAAI,SAAS,GAAG,gBAAgB,GAAG,QAAQ,CAAC,MAAM,EAAE,CAAC;gBAC7F,MAAM,SAAS,GAAG,QAAQ,CAAC,MAAM,GAAG,CAAC,SAAS,GAAG,gBAAgB,CAAC,CAAC;gBACnE,MAAM,UAAU,GAAG,SAAS,GAAG,gBAAgB,GAAG,CAAC,CAAC;gBACpD,UAAU,GAAG,GAAG,UAAU,CAAC,OAAO,QAAQ,SAAS,mCAAmC,UAAU,gBAAgB,CAAC;YAClH,CAAC;iBAAM,CAAC;gBACP,UAAU,GAAG,UAAU,CAAC,OAAO,CAAC;YACjC,CAAC;YAED,OAAO,EAAE,OAAO,EAAE,CAAC,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC;QACnE,CAAC;KACD,CAAC;AACH,CAAC","sourcesContent":["import type { ImageContent, TextContent } from \"@earendil-works/pi-ai\";\nimport { type Static, Type } from \"typebox\";\nimport type { Context } from \"../context.ts\";\nimport type { AgentHarnessTool } from \"../types.ts\";\nimport { FileError, getOrThrow } from \"../types.ts\";\nimport { type ReadFolder, selectedReadFolder } from \"../utils/read-folders/index.ts\";\nimport { createDefaultReadSummary } from \"../utils/segmented-read-view.ts\";\nimport {\n\tDEFAULT_MAX_BYTES,\n\tDEFAULT_MAX_LINES,\n\tformatSize,\n\ttype TruncationResult,\n\ttruncateHead,\n} from \"../utils/truncate.ts\";\nimport { detectSupportedImageMimeType, encodeBase64 } from \"./image.ts\";\nimport { resolveReadToolPath } from \"./path-utils.ts\";\nimport type { ExecutionToolContext } from \"./tool-context.ts\";\n\nconst readSchema = Type.Object({\n\tpath: Type.String({ description: \"Path to the file to read (relative or absolute)\" }),\n\toffset: Type.Optional(Type.Number({ description: \"Line number to start reading from (1-indexed)\" })),\n\tlimit: Type.Optional(Type.Number({ description: \"Maximum number of lines to read\" })),\n});\n\nexport type ReadToolInput = Static<typeof readSchema>;\n\nexport interface ReadToolDetails {\n\ttruncation?: TruncationResult;\n}\n\nexport type ReadImageProcessorResult =\n\t| { ok: true; data: string; mimeType: string; hints: string[] }\n\t| { ok: false; message: string };\n\nexport type ReadImageProcessor = (\n\tbytes: Uint8Array,\n\tmimeType: string,\n\toptions: { autoResizeImages: boolean },\n\tcontext: Context,\n) => Promise<ReadImageProcessorResult>;\n\nexport interface ReadToolOptions {\n\t/** Whether an injected image processor should resize images. Default: true. */\n\tautoResizeImages?: boolean;\n\t/** Optional image conversion/resizing implementation. */\n\timageProcessor?: ReadImageProcessor;\n\t/** Structural folder. Default options select the measured folder; omit here for verbatim reads. */\n\tfolder?: ReadFolder;\n}\n\nexport function createReadTool<TContext extends ExecutionToolContext = ExecutionToolContext>(\n\toptions: ReadToolOptions = { folder: selectedReadFolder },\n): AgentHarnessTool<TContext, typeof readSchema, ReadToolDetails | undefined> {\n\treturn {\n\t\tname: \"read\",\n\t\tlabel: \"read\",\n\t\tdescription: `Read the contents of a file. Supports text files and images (jpg, png, gif, webp, bmp). Images are sent as attachments. For text files, output is truncated to ${DEFAULT_MAX_LINES} lines or ${DEFAULT_MAX_BYTES / 1024}KB (whichever is hit first). Use offset/limit for large files. When you need the full file, continue with offset until complete.`,\n\t\tparameters: readSchema,\n\t\tasync execute(_toolCallId, { path, offset, limit }, _onUpdate, { env }, _invocation, context) {\n\t\t\tconst absolutePath = await resolveReadToolPath(env, path, context);\n\t\t\tconst bytes = getOrThrow(await env.readBinaryFile(absolutePath, context));\n\t\t\tif (context.abortSignal?.aborted) throw new FileError(\"aborted\", \"Operation aborted\", absolutePath);\n\t\t\tconst mimeType = detectSupportedImageMimeType(bytes);\n\t\t\tif (mimeType) {\n\t\t\t\tif (options?.imageProcessor) {\n\t\t\t\t\tconst processed = await options.imageProcessor(\n\t\t\t\t\t\tbytes,\n\t\t\t\t\t\tmimeType,\n\t\t\t\t\t\t{ autoResizeImages: options.autoResizeImages ?? true },\n\t\t\t\t\t\tcontext,\n\t\t\t\t\t);\n\t\t\t\t\tif (!processed.ok) {\n\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\tcontent: [{ type: \"text\", text: `Read image file [${mimeType}]\\n${processed.message}` }],\n\t\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t\t};\n\t\t\t\t\t}\n\t\t\t\t\tconst hints = processed.hints.length > 0 ? `\\n${processed.hints.join(\"\\n\")}` : \"\";\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{ type: \"text\", text: `Read image file [${processed.mimeType}]${hints}` },\n\t\t\t\t\t\t\t{ type: \"image\", data: processed.data, mimeType: processed.mimeType },\n\t\t\t\t\t\t] satisfies Array<TextContent | ImageContent>,\n\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\tif (mimeType === \"image/bmp\") {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tcontent: [\n\t\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\ttype: \"text\",\n\t\t\t\t\t\t\t\ttext: \"Read image file [image/bmp]\\n[Image omitted: configure an imageProcessor to convert BMP images.]\",\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t],\n\t\t\t\t\t\tdetails: undefined,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t\treturn {\n\t\t\t\t\tcontent: [\n\t\t\t\t\t\t{ type: \"text\", text: `Read image file [${mimeType}]` },\n\t\t\t\t\t\t{ type: \"image\", data: encodeBase64(bytes), mimeType },\n\t\t\t\t\t] satisfies Array<TextContent | ImageContent>,\n\t\t\t\t\tdetails: undefined,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst textContent = new TextDecoder().decode(bytes);\n\t\t\tconst allLines = textContent.split(\"\\n\");\n\t\t\tconst totalFileLines = allLines.length;\n\t\t\tconst startLine = offset ? Math.max(0, offset - 1) : 0;\n\t\t\tconst startLineDisplay = startLine + 1;\n\t\t\tif (startLine >= allLines.length) {\n\t\t\t\tthrow new Error(`Offset ${offset} is beyond end of file (${allLines.length} lines total)`);\n\t\t\t}\n\n\t\t\tlet selectedContent: string;\n\t\t\tlet userLimitedLines: number | undefined;\n\t\t\tif (limit !== undefined) {\n\t\t\t\tconst endLine = Math.min(startLine + limit, allLines.length);\n\t\t\t\tselectedContent = allLines.slice(startLine, endLine).join(\"\\n\");\n\t\t\t\tuserLimitedLines = endLine - startLine;\n\t\t\t} else {\n\t\t\t\tselectedContent = allLines.slice(startLine).join(\"\\n\");\n\t\t\t}\n\n\t\t\tconst truncation = truncateHead(selectedContent);\n\t\t\tconst summary = createDefaultReadSummary({\n\t\t\t\tpath: absolutePath,\n\t\t\t\ttext: textContent,\n\t\t\t\toffset,\n\t\t\t\tlimit,\n\t\t\t\tfolder: options.folder,\n\t\t\t\ttruncated: truncation.truncated,\n\t\t\t});\n\t\t\tlet outputText: string;\n\t\t\tlet details: ReadToolDetails | undefined;\n\t\t\tif (summary) {\n\t\t\t\toutputText = summary.text;\n\t\t\t} else if (truncation.firstLineExceedsLimit) {\n\t\t\t\tconst firstLineSize = formatSize(new TextEncoder().encode(allLines[startLine]).byteLength);\n\t\t\t\toutputText = `[Line ${startLineDisplay} is ${firstLineSize}, exceeds ${formatSize(DEFAULT_MAX_BYTES)} limit. Use bash: sed -n '${startLineDisplay}p' ${path} | head -c ${DEFAULT_MAX_BYTES}]`;\n\t\t\t\tdetails = { truncation };\n\t\t\t} else if (truncation.truncated) {\n\t\t\t\tconst endLineDisplay = startLineDisplay + truncation.outputLines - 1;\n\t\t\t\tconst nextOffset = endLineDisplay + 1;\n\t\t\t\toutputText = truncation.content;\n\t\t\t\tif (truncation.truncatedBy === \"lines\") {\n\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLineDisplay}-${endLineDisplay} of ${totalFileLines}. Use offset=${nextOffset} to continue.]`;\n\t\t\t\t} else {\n\t\t\t\t\toutputText += `\\n\\n[Showing lines ${startLineDisplay}-${endLineDisplay} of ${totalFileLines} (${formatSize(DEFAULT_MAX_BYTES)} limit). Use offset=${nextOffset} to continue.]`;\n\t\t\t\t}\n\t\t\t\tdetails = { truncation };\n\t\t\t} else if (userLimitedLines !== undefined && startLine + userLimitedLines < allLines.length) {\n\t\t\t\tconst remaining = allLines.length - (startLine + userLimitedLines);\n\t\t\t\tconst nextOffset = startLine + userLimitedLines + 1;\n\t\t\t\toutputText = `${truncation.content}\\n\\n[${remaining} more lines in file. Use offset=${nextOffset} to continue.]`;\n\t\t\t} else {\n\t\t\t\toutputText = truncation.content;\n\t\t\t}\n\n\t\t\treturn { content: [{ type: \"text\", text: outputText }], details };\n\t\t},\n\t};\n}\n"]}
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { ReadBraceScan, ReadFoldSettings } from "./types.ts";
|
|
2
|
+
/** Measured row-17 brace lexer, restricted to the three selected languages. */
|
|
3
|
+
export declare function scanBraces(source: string, language: "ts" | "js" | "json", settings: ReadFoldSettings): ReadBraceScan;
|
|
4
|
+
//# sourceMappingURL=brace-scanner.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"brace-scanner.d.ts","sourceRoot":"","sources":["../../../../src/harness/utils/read-folders/brace-scanner.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,aAAa,EAAE,gBAAgB,EAAiB,MAAM,YAAY,CAAC;AAEjF,+EAA+E;AAC/E,wBAAgB,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,IAAI,GAAG,IAAI,GAAG,MAAM,EAAE,QAAQ,EAAE,gBAAgB,GAAG,aAAa,CAqPpH"}
|