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?", 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; 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 = export type AgentMessagePart =
| { readonly type: "text"; readonly text: string } | { readonly type: "text"; readonly text: string }
| { readonly type: "tool-call"; readonly call: AgentToolCall } | { readonly type: "tool-call"; readonly call: AgentToolCall }
| { readonly type: "tool-result"; readonly result: AgentToolResult } | { readonly type: "tool-result"; readonly result: AgentToolResult }
| { readonly type: "presentation-action"; readonly action: PresentationToolAction } | { 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 }; | { readonly type: "error"; readonly message: string };
export type AgentMessage = { export type AgentMessage = {
@@ -68,11 +82,13 @@ export const approvalRequestPart = (
callId: string, callId: string,
name: AgentToolName, name: AgentToolName,
prompt: string, prompt: string,
contract?: AgentApprovalContract,
): AgentMessagePart => ({ ): AgentMessagePart => ({
type: "approval-request", type: "approval-request",
callId, callId,
name, name,
prompt, prompt,
contract,
}); });
export type AgentApproval = { export type AgentApproval = {
@@ -66,6 +66,17 @@ describe("prepared recipe driver", () => {
expect(messages.some((m) => expect(messages.some((m) =>
m.parts.some((p) => p.type === "tool-call" && p.call.name === "readRunTrace"), m.parts.some((p) => p.type === "tool-call" && p.call.name === "readRunTrace"),
)).toBe(true); )).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 () => { 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 type { DemoRecording } from "../timeline/models.js";
import { import {
approvalRequestPart,
agentTextMessage, agentTextMessage,
agentToolCallPart, agentToolCallPart,
agentToolResultPart, agentToolResultPart,
approvalRequestPart,
presentationActionPart, presentationActionPart,
type AgentApproval, type AgentApproval,
type AgentApprovalContract,
type AgentDriver, type AgentDriver,
type AgentMessage, type AgentMessage,
type AgentMessagePart, type AgentMessagePart,
} from "./events.js"; } from "./events.js";
import { PREPARE_THESIS_REPORT_RECIPE, type RecipeTool } from "./recipes.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 => { export const assertNever = (value: never): never => {
throw new Error(`Unhandled case: ${String(value)}`); throw new Error(`Unhandled case: ${String(value)}`);
}; };
@@ -73,9 +77,25 @@ export async function* runPreparedRecipeReplay(
break; break;
case "resumeIssueReview": { case "resumeIssueReview": {
const callId = `${step.id}-call`; 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[] = [ const approvalParts: AgentMessagePart[] = [
agentToolCallPart(callId, step.toolName, { runId }), 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 }; yield { id: step.id, role: "assistant", parts: approvalParts };
const decision = await requestApproval(signal); const decision = await requestApproval(signal);
@@ -74,7 +74,43 @@ describe("OperatorChat", () => {
expect(screen.getByText(/Found prepared workflow recipe/)).toBeInTheDocument(); expect(screen.getByText(/Found prepared workflow recipe/)).toBeInTheDocument();
}); });
it("renders approval controls and wires decisions", async () => { it("renders schema approval surface inside chat approval request", async () => {
const user = userEvent.setup();
const onApprove = vi.fn();
const onDeny = vi.fn();
const messages: ReadonlyArray<AgentMessage> = [
{
id: "approval",
role: "assistant",
parts: [
{
type: "approval-request",
callId: "call-1",
name: "resumeIssueReview",
prompt: "Approve resuming?",
contract: {
kind: "issue_review",
outcomes: ["submitted", "cancelled"],
resumeSchema: { type: "object", properties: { comment: { type: "string" } } },
resumePayloadPreview: { comment: "Looks good." },
runId: "run_recorded_lda_report",
},
},
],
},
];
render(<OperatorChat state={initialPresentationState} messages={messages} onApprove={onApprove} onDeny={onDeny} />);
expect(screen.getByRole("group", { name: /issue_review resume/i })).toBeInTheDocument();
await user.click(screen.getByRole("button", { name: /submit/i }));
await user.click(screen.getByRole("button", { name: /cancel/i }));
expect(onApprove).toHaveBeenCalledTimes(1);
expect(onDeny).toHaveBeenCalledTimes(1);
});
it("falls back to plain approve/deny buttons when approval request has no contract", async () => {
const user = userEvent.setup(); const user = userEvent.setup();
const onApprove = vi.fn(); const onApprove = vi.fn();
const onDeny = vi.fn(); const onDeny = vi.fn();
@@ -95,6 +131,7 @@ describe("OperatorChat", () => {
render(<OperatorChat state={initialPresentationState} messages={messages} onApprove={onApprove} onDeny={onDeny} />); render(<OperatorChat state={initialPresentationState} messages={messages} onApprove={onApprove} onDeny={onDeny} />);
expect(screen.queryByRole("group", { name: /issue_review resume/i })).not.toBeInTheDocument();
await user.click(screen.getByRole("button", { name: "Approve" })); await user.click(screen.getByRole("button", { name: "Approve" }));
await user.click(screen.getByRole("button", { name: "Deny" })); await user.click(screen.getByRole("button", { name: "Deny" }));
expect(onApprove).toHaveBeenCalledTimes(1); expect(onApprove).toHaveBeenCalledTimes(1);
@@ -3,6 +3,7 @@ import { PREPARE_THESIS_REPORT_RECIPE } from "../demo/agent/recipes.js";
import type { AgentMessage, AgentMessagePart } from "../demo/agent/events.js"; import type { AgentMessage, AgentMessagePart } from "../demo/agent/events.js";
import type { PresentationState } from "./presentation-state.js"; import type { PresentationState } from "./presentation-state.js";
import { compositionForState } from "./presentation-state.js"; import { compositionForState } from "./presentation-state.js";
import { SchemaApprovalSurface } from "./approval/SchemaApprovalSurface.js";
type OperatorChatProps = { type OperatorChatProps = {
readonly state: PresentationState; readonly state: PresentationState;
@@ -78,10 +79,22 @@ const renderPart = (
<span>Approval required</span> <span>Approval required</span>
<code>{part.name}</code> <code>{part.name}</code>
<p>{part.prompt}</p> <p>{part.prompt}</p>
<div className="chat-approval-actions"> {part.contract ? (
<button type="button" onClick={onApprove} disabled={!onApprove}>Approve</button> <SchemaApprovalSurface
<button type="button" onClick={onDeny} disabled={!onDeny}>Deny</button> title={`${part.contract.kind} resume`}
</div> schema={part.contract.resumeSchema}
payload={part.contract.resumePayloadPreview}
outcomes={part.contract.outcomes}
runId={part.contract.runId}
onSubmit={onApprove}
onCancel={onDeny}
/>
) : (
<div className="chat-approval-actions">
<button type="button" onClick={onApprove} disabled={!onApprove}>Approve</button>
<button type="button" onClick={onDeny} disabled={!onDeny}>Deny</button>
</div>
)}
</div> </div>
); );
case "error": case "error":