AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.
fix(frontend/copilot): settle a held call's inline step once its approval runs (#14973)
Reinier van der Leer0bf46bc84b3e
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/heldOutcomes.test.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/heldOutcomes.test.tsindex 9de65d5fd5..0aead2085e 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/heldOutcomes.test.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/heldOutcomes.test.ts@@ -68,3 +68,56 @@ test("an output that contains the closing tag is kept whole", () => {]);expect(outcomes.get("call-7")?.output).toBe(body);});++function heldCall(+ toolCallId: string,+ reviewId: string,+): UIMessage<unknown, UIDataTypes, UITools> {+ return {+ id: `a-${toolCallId}`,+ role: "assistant",+ parts: [+ {+ type: "tool-bash_exec",+ toolCallId,+ state: "output-available",+ input: {},+ output: JSON.stringify({+ type: "approval_required",+ review_id: reviewId,+ }),+ },+ ],+ };+}++const standIn = (reviewId: string, outcome: string) =>+ lateResult(+ {+ held_call: {+ tool_call_id: "sdk-365200f8ce9e",+ review_id: reviewId,+ outcome,+ },+ },+ "done",+ );++test("a result under the SDK engine's stand-in id settles the model's held call by review id", () => {+ const outcomes = getHeldOutcomes([+ heldCall("toolu_01", "gate:abc"),+ standIn("gate:abc", "approved"),+ ]);+ expect(outcomes.get("toolu_01")?.outcome).toBe("approved");+ expect(outcomes.get("sdk-365200f8ce9e")?.outcome).toBe("approved");+});++test("a repeat of the same call waits for its own result, not the first one's", () => {+ const outcomes = getHeldOutcomes([+ heldCall("toolu_01", "gate:abc"),+ standIn("gate:abc", "rejected"),+ heldCall("toolu_02", "gate:abc"),+ ]);+ expect(outcomes.get("toolu_01")?.outcome).toBe("rejected");+ expect(outcomes.has("toolu_02")).toBe(false);+});diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.tsindex 0ab76a597f..64e5880875 100644--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.ts+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/heldCallRows.ts@@ -24,27 +24,29 @@ export function countHeldCalls((count, message) =>count +message.parts.filter(- (part) => "output" in part && isHeldOutput(part.output),+ (part) => "output" in part && heldReviewId(part.output) !== null,).length,0,);}-function isHeldOutput(output: unknown): boolean {+function heldReviewId(output: unknown): string | null {let value = output;if (typeof value === "string") {try {value = JSON.parse(value);} catch {- return false;+ return null;}}- return (- !!value &&- typeof value === "object" &&- (value as { type?: unknown }).type === "approval_required" &&- typeof (value as { review_id?: unknown }).review_id === "string"- );+ if (+ !value ||+ typeof value !== "object" ||+ (value as { type?: unknown }).type !== "approval_required"+ )+ return null;+ const reviewId = (value as { review_id?: unknown }).review_id;+ return typeof reviewId === "string" ? reviewId : null;}export type HeldOutcomeKind =@@ -75,20 +77,43 @@ export function getHeldOutcomes(messages: UIMessage<unknown, UIDataTypes, UITools>[],): Map<string, HeldOutcome> {const outcomes = new Map<string, HeldOutcome>();+ // Held calls not yet paired with a result, oldest first per review id.+ const unanswered = new Map<string, string[]>();for (const message of messages) {const held = heldCallMetadata(message.metadata);- if (!held) continue;+ if (!held) {+ for (const part of message.parts) {+ if (!("output" in part) || !("toolCallId" in part)) continue;+ const reviewId = heldReviewId(part.output);+ if (reviewId === null) continue;+ unanswered.set(reviewId, [+ ...(unanswered.get(reviewId) ?? []),+ part.toolCallId,+ ]);+ }+ continue;+ }const text = message.parts.map((part) => (part.type === "text" ? part.text : "")).join("");const body = RESULT_RE.exec(text)?.[1] ?? "";- outcomes.set(held.toolCallId, {+ const outcome: HeldOutcome = {// Rows persisted before the outcome was recorded say "Nothing ran" when refused.outcome:held.outcome ??(body.startsWith("Nothing ran") ? "closed" : "approved"),output: parseOutput(body),- });+ };+ outcomes.set(held.toolCallId, outcome);+ // The SDK engine records a stand-in id, not the model's; the review id+ // pairs the result with the earliest held call still waiting on it.+ const waiting = held.reviewId ? unanswered.get(held.reviewId) : undefined;+ if (!waiting?.length) continue;+ const index = waiting.includes(held.toolCallId)+ ? waiting.indexOf(held.toolCallId)+ : 0;+ outcomes.set(waiting[index], outcome);+ waiting.splice(index, 1);}return outcomes;}@@ -97,10 +122,11 @@ function heldCallMetadata(metadata: unknown) {if (!metadata || typeof metadata !== "object") return null;const held = (metadata as { held_call?: unknown }).held_call;if (!held || typeof held !== "object") return null;- const { tool_call_id, outcome } = held as Record<string, unknown>;+ const { tool_call_id, review_id, outcome } = held as Record<string, unknown>;if (typeof tool_call_id !== "string" || !tool_call_id) return null;return {toolCallId: tool_call_id,+ reviewId: typeof review_id === "string" ? review_id : null,outcome:typeof outcome === "string" && OUTCOMES.has(outcome)? (outcome as HeldOutcomeKind)