feat: render schema approval in chat
This commit is contained in:
@@ -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",
|
||||
},
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -74,7 +74,43 @@ describe("OperatorChat", () => {
|
||||
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 onApprove = vi.fn();
|
||||
const onDeny = vi.fn();
|
||||
@@ -95,6 +131,7 @@ describe("OperatorChat", () => {
|
||||
|
||||
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: "Deny" }));
|
||||
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 { PresentationState } from "./presentation-state.js";
|
||||
import { compositionForState } from "./presentation-state.js";
|
||||
import { SchemaApprovalSurface } from "./approval/SchemaApprovalSurface.js";
|
||||
|
||||
type OperatorChatProps = {
|
||||
readonly state: PresentationState;
|
||||
@@ -78,10 +79,22 @@ const renderPart = (
|
||||
<span>Approval required</span>
|
||||
<code>{part.name}</code>
|
||||
<p>{part.prompt}</p>
|
||||
<div className="chat-approval-actions">
|
||||
<button type="button" onClick={onApprove} disabled={!onApprove}>Approve</button>
|
||||
<button type="button" onClick={onDeny} disabled={!onDeny}>Deny</button>
|
||||
</div>
|
||||
{part.contract ? (
|
||||
<SchemaApprovalSurface
|
||||
title={`${part.contract.kind} resume`}
|
||||
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>
|
||||
);
|
||||
case "error":
|
||||
|
||||
Reference in New Issue
Block a user