feat: render schema approval in chat

This commit is contained in:
lda
2026-07-09 05:45:41 +07:00 Verified
parent 22386f7e2d
commit 7594ba4b0d
6 changed files with 128 additions and 8 deletions
@@ -49,4 +49,27 @@ describe("agent events", () => {
prompt: "Approve the typed issue review?",
});
});
it("creates approval request parts with optional contract data", () => {
const part = approvalRequestPart(
"call-1",
"resumeIssueReview",
"Approve?",
{
kind: "issue_review",
outcomes: ["submitted", "cancelled"],
resumeSchema: { type: "object" },
resumePayloadPreview: { selected_issue_ids: ["risk-1"] },
runId: "run_recorded_lda_report",
},
);
expect(part).toMatchObject({
type: "approval-request",
contract: {
kind: "issue_review",
runId: "run_recorded_lda_report",
},
});
});
});
+17 -1
View File
@@ -20,12 +20,26 @@ export type AgentToolResult = {
readonly output: unknown;
};
export type AgentApprovalContract = {
readonly kind: string;
readonly outcomes: ReadonlyArray<string>;
readonly resumeSchema: unknown;
readonly resumePayloadPreview: unknown;
readonly runId: string | null;
};
export type AgentMessagePart =
| { readonly type: "text"; readonly text: string }
| { readonly type: "tool-call"; readonly call: AgentToolCall }
| { readonly type: "tool-result"; readonly result: AgentToolResult }
| { readonly type: "presentation-action"; readonly action: PresentationToolAction }
| { readonly type: "approval-request"; readonly callId: string; readonly name: AgentToolName; readonly prompt: string }
| {
readonly type: "approval-request";
readonly callId: string;
readonly name: AgentToolName;
readonly prompt: string;
readonly contract?: AgentApprovalContract | undefined;
}
| { readonly type: "error"; readonly message: string };
export type AgentMessage = {
@@ -68,11 +82,13 @@ export const approvalRequestPart = (
callId: string,
name: AgentToolName,
prompt: string,
contract?: AgentApprovalContract,
): AgentMessagePart => ({
type: "approval-request",
callId,
name,
prompt,
contract,
});
export type AgentApproval = {
@@ -66,6 +66,17 @@ describe("prepared recipe driver", () => {
expect(messages.some((m) =>
m.parts.some((p) => p.type === "tool-call" && p.call.name === "readRunTrace"),
)).toBe(true);
const approvalPart = messages
.flatMap((message) => message.parts)
.find((part) => part.type === "approval-request");
expect(approvalPart).toMatchObject({
type: "approval-request",
contract: {
kind: "issue_review",
runId: "run_recorded_lda_report",
},
});
});
it("stops after denial and does not emit resume result, trace, or evidence", async () => {
@@ -1,17 +1,21 @@
import type { DemoRecording } from "../timeline/models.js";
import {
approvalRequestPart,
agentTextMessage,
agentToolCallPart,
agentToolResultPart,
approvalRequestPart,
presentationActionPart,
type AgentApproval,
type AgentApprovalContract,
type AgentDriver,
type AgentMessage,
type AgentMessagePart,
} from "./events.js";
import { PREPARE_THESIS_REPORT_RECIPE, type RecipeTool } from "./recipes.js";
const isObject = (value: unknown): value is Record<string, unknown> =>
typeof value === "object" && value !== null && !Array.isArray(value);
export const assertNever = (value: never): never => {
throw new Error(`Unhandled case: ${String(value)}`);
};
@@ -73,9 +77,25 @@ export async function* runPreparedRecipeReplay(
break;
case "resumeIssueReview": {
const callId = `${step.id}-call`;
const interrupt = runStart?.interpreted && typeof runStart.interpreted === "object"
&& "interrupt" in runStart.interpreted
? runStart.interpreted.interrupt
: null;
const resumePayload = resume?.params && typeof resume.params === "object" && "resume_payload" in resume.params
? resume.params.resume_payload
: null;
const approvalContract: AgentApprovalContract | undefined = isObject(interrupt) && Array.isArray(interrupt.outcomes)
? {
kind: typeof interrupt.kind === "string" ? interrupt.kind : "issue_review",
outcomes: interrupt.outcomes.filter((entry): entry is string => typeof entry === "string"),
resumeSchema: "resume_schema" in interrupt ? interrupt.resume_schema : { type: "object" },
resumePayloadPreview: resumePayload,
runId,
}
: undefined;
const approvalParts: AgentMessagePart[] = [
agentToolCallPart(callId, step.toolName, { runId }),
approvalRequestPart(callId, step.toolName, "Approve resuming the workflow run with the selected issues?"),
approvalRequestPart(callId, step.toolName, "Approve resuming the workflow run with the selected issues?", approvalContract),
];
yield { id: step.id, role: "assistant", parts: approvalParts };
const decision = await requestApproval(signal);