84 lines
4.2 KiB
Diff
84 lines
4.2 KiB
Diff
diff --git a/dist/extensions/codemode/execute.js b/dist/extensions/codemode/execute.js
|
|
index f8df8bd49d5017240fffdfcce17ebb3064db9d6c..7d74d844fe6dc86bb71a88589824defcf612a15b 100644
|
|
--- a/dist/extensions/codemode/execute.js
|
|
+++ b/dist/extensions/codemode/execute.js
|
|
@@ -345,7 +345,7 @@ function joinAdjacentText(items) {
|
|
function formatCallSummary(calls) {
|
|
if (calls.length === 0)
|
|
return "No tool calls were made.";
|
|
- return `Tool calls made before the failure (they are not undone): ${calls.map((call) => `${call.name} (${call.status})`).join(", ")}`;
|
|
+ return `Tool calls made before the failure (they are not undone): ${calls.map((call) => `${call.name} (${call.status === "running" ? "outcome unknown" : call.status})`).join(", ")}`;
|
|
}
|
|
function formatError(result, calls) {
|
|
const { error } = result;
|
|
@@ -459,6 +459,14 @@ export async function executeCodemode(toolCallId, input, signal, onUpdate, ctx,
|
|
const startedAt = performance.now();
|
|
const { code, options: sourceOptions } = parseCodemodeSource(input.code);
|
|
const calls = [];
|
|
+ const pendingCalls = new Set();
|
|
+ const track = (run) => {
|
|
+ const pending = run();
|
|
+ pendingCalls.add(pending);
|
|
+ const finish = () => pendingCalls.delete(pending);
|
|
+ void pending.then(finish, finish);
|
|
+ return pending;
|
|
+ };
|
|
// Usage of the script's `models.*` calls. Nested tool calls report theirs through the session.
|
|
let modelUsage;
|
|
// Images returned by `models.generateImages()`, to notice a script that never shows them.
|
|
@@ -480,7 +488,7 @@ export async function executeCodemode(toolCallId, input, signal, onUpdate, ctx,
|
|
const sandboxTools = callable.map((tool) => ({
|
|
name: tool.name,
|
|
description: samples.get(tool.name),
|
|
- execute: async (args, { signal: callSignal }) => {
|
|
+ execute: (args, { signal: callSignal }) => track(async () => {
|
|
const record = {
|
|
id: `${toolCallId}/?`,
|
|
name: tool.name,
|
|
@@ -497,7 +505,7 @@ export async function executeCodemode(toolCallId, input, signal, onUpdate, ctx,
|
|
record.id = outcome.toolCall.id;
|
|
record.durationMs = performance.now() - callStartedAt;
|
|
if (outcome.isError) {
|
|
- record.status = callSignal.aborted ? "cancelled" : "error";
|
|
+ record.status = signal?.aborted ? "cancelled" : "error";
|
|
record.error = truncateText(textOf(outcome.result) || `Tool "${tool.name}" failed`, ERROR_PREVIEW_CHARS);
|
|
}
|
|
else {
|
|
@@ -505,7 +513,7 @@ export async function executeCodemode(toolCallId, input, signal, onUpdate, ctx,
|
|
}
|
|
publish();
|
|
return toScriptValue(tool, outcome);
|
|
- },
|
|
+ }),
|
|
}));
|
|
// The wait for a worker counts toward the script's deadline.
|
|
const deadline = sourceOptions.timeoutMs ?? Number.POSITIVE_INFINITY;
|
|
@@ -565,12 +573,23 @@ export async function executeCodemode(toolCallId, input, signal, onUpdate, ctx,
|
|
releaseCodemodeWorker();
|
|
}
|
|
}
|
|
- // Calls still marked running were cut off by the script ending, a timeout, or an abort.
|
|
- for (const call of calls) {
|
|
- if (call.status === "running")
|
|
- call.status = "cancelled";
|
|
+ const remainingMs = deadline - (performance.now() - startedAt);
|
|
+ if (pendingCalls.size && !signal?.aborted && remainingMs > 0) {
|
|
+ await new Promise((resolve) => {
|
|
+ let timer;
|
|
+ const finish = () => {
|
|
+ clearTimeout(timer);
|
|
+ signal?.removeEventListener("abort", finish);
|
|
+ resolve();
|
|
+ };
|
|
+ if (Number.isFinite(remainingMs)) timer = setTimeout(finish, remainingMs);
|
|
+ signal?.addEventListener("abort", finish, { once: true });
|
|
+ void Promise.allSettled([...pendingCalls]).then(finish);
|
|
+ });
|
|
}
|
|
const scriptOutput = [...result.output];
|
|
+ if (calls.some((call) => call.status === "running"))
|
|
+ scriptOutput.push({ type: "text", text: "Some tool results are still unknown. Check the outside state before repeating an action." });
|
|
if (result.ok) {
|
|
const { set, delete: deleted } = result.storeWrites;
|
|
if (Object.keys(set).length > 0 || deleted.length > 0) {
|