Skip to content

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): the approval label says what each answered card sent to Otto (#15493)

Reinier van der Leer1ce08ff42c55
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
index b77acc5de6..cf67a0003a 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/__tests__/ChatMessagesContainer.test.tsx
@@ -1528,6 +1528,168 @@ describe("ChatMessagesContainer — held call rows", () => {
expect(screen.queryByText(/folder_created/)).toBeNull();
expect(screen.getByText("Approval answered")).toBeDefined();
});
+
+ it.each([
+ ["approved", "Result sent to Otto."],
+ ["rejected", "Declined; nothing was sent to Otto."],
+ ["expired", "Expired; nothing was sent to Otto."],
+ ["unknown", "Unclear; check before relying on it."],
+ ])(
+ "labels the wake by the card's recorded outcome: %s",
+ (outcome, clause) => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ messages={answeredCards([{ id: "a", outcome }])}
+ />,
+ );
+
+ expect(
+ screen.getByText(`Approval answered: Read "a.example.com". ${clause}`),
+ ).toBeDefined();
+ },
+ );
+
+ // Each cause the backend closes a call for, with the body it writes; only
+ // a read can close after an identical re-read already delivered it.
+ it.each([
+ [
+ "an action whose card is no longer open",
+ false,
+ "Nothing ran: this card is no longer open.",
+ 'Post to "a.example.com". Closed; nothing ran.',
+ ],
+ [
+ "an action whose details were lost",
+ false,
+ "Nothing ran: the approved action's details were lost before it could run.",
+ 'Post to "a.example.com". Closed; nothing ran.',
+ ],
+ [
+ "an action whose tool no longer exists",
+ false,
+ "Nothing ran: this tool no longer exists.",
+ 'Post to "a.example.com". Closed; nothing ran.',
+ ],
+ [
+ "a read whose card is no longer open",
+ true,
+ "Nothing ran: this card is no longer open.",
+ 'Read "a.example.com". Closed; nothing was released, or Otto already had it.',
+ ],
+ [
+ "a read an identical re-read already received",
+ true,
+ "The user released this content and it was already delivered to you once; nothing more arrives for this call.",
+ 'Read "a.example.com". Closed; nothing was released, or Otto already had it.',
+ ],
+ ])("labels a closed card truly: %s", (_cause, read, body, label) => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ messages={answeredCards([{ id: "a", outcome: "closed", read, body }])}
+ />,
+ );
+
+ expect(screen.getByText(`Approval answered: ${label}`)).toBeDefined();
+ });
+
+ it("claims nothing for a result row with no recorded outcome", () => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ messages={answeredCards([{ id: "a" }])}
+ />,
+ );
+
+ expect(
+ screen.getByText('Approval answered: Read "a.example.com".'),
+ ).toBeDefined();
+ });
+
+ it("gives each answer in one wake its own clause", () => {
+ render(
+ <ChatMessagesContainer
+ {...baseProps}
+ messages={answeredCards([
+ { id: "a", outcome: "approved" },
+ { id: "b", outcome: "rejected" },
+ { id: "c", outcome: "approved" },
+ ])}
+ />,
+ );
+
+ expect(
+ screen.getByText(
+ 'Approval answered: Read "a.example.com", Read "c.example.com". ' +
+ 'Results sent to Otto. Read "b.example.com". ' +
+ "Declined; nothing was sent to Otto.",
+ ),
+ ).toBeDefined();
+ });
+
+ interface AnsweredCard {
+ id: string;
+ outcome?: string;
+ read?: boolean;
+ body?: string;
+ }
+
+ // A turn's held cards, its wake row, and a result row per card, as the
+ // server persists them.
+ function answeredCards(cards: AnsweredCard[]): Message[] {
+ return [
+ {
+ id: "assistant-cards",
+ role: "assistant",
+ parts: cards.map(({ id, read = true }) => ({
+ type: "tool-run_capability",
+ toolCallId: `call-${id}`,
+ state: "output-available",
+ input: {},
+ output: JSON.stringify({
+ type: "approval_required",
+ review_id: reviewId(id, read),
+ ask: read ? "Read" : "Post to",
+ object: `${id}.example.com`,
+ }),
+ })),
+ } as Message,
+ {
+ id: "wake",
+ role: "user",
+ parts: [
+ {
+ type: "text",
+ text: "I answered an action that was waiting for my approval.",
+ },
+ ],
+ metadata: { held_calls_answered: true },
+ },
+ ...cards.map(({ id, outcome, read = true, body }) => ({
+ id: `result-${id}`,
+ role: "user" as const,
+ parts: [
+ {
+ type: "text" as const,
+ text: `<held_call_result tool="run_capability">\n${body ?? (outcome === "approved" ? "page text" : "Nothing ran.")}\n</held_call_result>`,
+ },
+ ],
+ metadata: {
+ held_call: {
+ review_id: reviewId(id, read),
+ tool_name: "run_capability",
+ tool_call_id: `call-${id}`,
+ outcome,
+ },
+ },
+ })),
+ ];
+ }
+
+ function reviewId(id: string, read: boolean) {
+ return `copilot-node-gate-${read ? "read-" : ""}run_capability:${id}`;
+ }
});
describe("ChatMessagesContainer — pending reviews", () => {
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.ts
index f58d3390b0..ee5c4e5074 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
@@ -1,7 +1,7 @@
import { AUTOPILOT_NAME } from "@/components/molecules/AutopilotAvatar/helpers";
import type { UIDataTypes, UIMessage, UITools } from "ai";
import { cardArguments, fallbackAsk } from "../ApprovalQueue/helpers";
-import { heldAskText, heldToolName } from "../ToolChain/heldRow";
+import { heldAskText, heldToolName, isHeldReadId } from "../ToolChain/heldRow";
// Rows the server writes for a held call: the one that starts the turn when a
// card is answered, and the call's late result, which the reply narrates.
@@ -33,7 +33,8 @@ export function countHeldCalls(
);
}
-// What a wake row says: the cards its turn delivered, by their own headlines.
+// What a wake row says: the cards its turn delivered, by their own headlines,
+// and what each answer sent on, from the result row's recorded outcome.
export function answeredLabel(
messages: UIMessage<unknown, UIDataTypes, UITools>[],
wakeIndex: number,
@@ -48,7 +49,7 @@ export function answeredLabel(
asks.set(reviewId, heldAskText(card, heldToolName(card, partTool(part))));
}
}
- const names: string[] = [];
+ const byOutcome = new Map<AnswerKind | null, string[]>();
for (const message of messages.slice(wakeIndex + 1)) {
if (getHeldCallRowKind(message.metadata) !== "result") break;
const held = (message.metadata as { held_call: Record<string, unknown> })
@@ -56,11 +57,42 @@ export function answeredLabel(
const tool = str(held?.tool_name);
const name =
asks.get(str(held?.review_id) ?? "") ?? (tool && fallbackAsk(tool));
- if (name) names.push(name);
+ if (!name) continue;
+ const outcome = str(held?.outcome);
+ const recorded =
+ outcome && OUTCOMES.has(outcome) ? (outcome as HeldOutcomeKind) : null;
+ // Only a read closes after an identical re-read already delivered it.
+ const kind =
+ recorded === "closed" && isHeldReadId(str(held?.review_id))
+ ? "closed-read"
+ : recorded;
+ byOutcome.set(kind, [...(byOutcome.get(kind) ?? []), name]);
+ }
+ if (!byOutcome.size) return "Approval answered";
+ const clauses = [...byOutcome].map(([outcome, names]) =>
+ [`${names.join(", ")}.`, outcome && sentOn(outcome, names.length > 1)]
+ .filter(Boolean)
+ .join(" "),
+ );
+ return `Approval answered: ${clauses.join(" ")}`;
+}
+
+// Every answer writes a result row, but only an approval's carries a result.
+function sentOn(outcome: AnswerKind, several: boolean): string {
+ switch (outcome) {
+ case "approved":
+ return `${several ? "Results" : "Result"} sent to ${AUTOPILOT_NAME}.`;
+ case "rejected":
+ return `Declined; nothing was sent to ${AUTOPILOT_NAME}.`;
+ case "expired":
+ return `Expired; nothing was sent to ${AUTOPILOT_NAME}.`;
+ case "closed":
+ return "Closed; nothing ran.";
+ case "closed-read":
+ return `Closed; nothing was released, or ${AUTOPILOT_NAME} already had it.`;
+ case "unknown":
+ return "Unclear; check before relying on it.";
}
- return names.length
- ? `Approval answered: ${names.join(", ")}. Result sent to ${AUTOPILOT_NAME}.`
- : "Approval answered";
}
function heldReviewId(output: unknown): string | null {
@@ -102,6 +134,8 @@ export type HeldOutcomeKind =
| "closed"
| "unknown";
+type AnswerKind = HeldOutcomeKind | "closed-read";
+
export interface HeldOutcome {
outcome: HeldOutcomeKind;
// The run's own output, as the tool would have returned it directly.
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/heldRow.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/heldRow.ts
index 237400aa08..a5b1beaafa 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/heldRow.ts
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ToolChain/heldRow.ts
@@ -128,7 +128,7 @@ function settle(
};
}
-function isHeldReadId(reviewId: string | null) {
+export function isHeldReadId(reviewId: string | null) {
return !!reviewId?.startsWith(`${COPILOT_GATE_NODE_PREFIX}read-`);
}