feat: add interactive approval/confirmation cards

- sendApprovalCard: interactive card with configurable buttons
- resolveCard: patch card to show resolution state (green/red)
- ApprovalManager: register/resolve lifecycle with 5min timeout
- request_approval MCP tool: agent can ask user for confirmation
- Wire onCardAction in trigger handler and server startup
- Add unit tests (5) for card JSON, manager, and routing
This commit is contained in:
2026-07-08 16:47:31 +08:00
parent ad65d93b2e
commit 4736610cf3
6 changed files with 435 additions and 6 deletions
+51 -2
View File
@@ -1,8 +1,9 @@
import { createSdkMcpServer, tool, type McpSdkServerConfigWithInstance } from "@anthropic-ai/claude-agent-sdk";
import { z } from "zod";
import { sendFile, type FeishuRuntime } from "./client.js";
import { sendApprovalCard, sendFile, type FeishuRuntime } from "./client.js";
import { resolveDeliverableFile } from "./fileDelivery.js";
import { readFeishuContext } from "./read.js";
import type { ApprovalManager } from "./approval.js";
export interface FileDeliveryToolOptions {
readonly rt: FeishuRuntime;
@@ -10,6 +11,7 @@ export interface FileDeliveryToolOptions {
readonly projectId: string;
readonly runId: string;
readonly workspaceDir: string;
readonly approvalManager: ApprovalManager;
readonly onDelivered?: (path: string) => void;
}
@@ -22,7 +24,8 @@ export function createFileDeliveryMcpServer(options: FileDeliveryToolOptions): M
"Use send_file when the user asks to receive, resend, download, or attach a file. " +
"Do not claim a file was sent unless send_file returns success. " +
"If a generated file path is uncertain, list or inspect the workspace first, then call send_file with the actual path. " +
"Use feishu_read_context when the trigger context contains a reply, thread, or message anchor and more Feishu context is needed.",
"Use feishu_read_context when the trigger context contains a reply, thread, or message anchor and more Feishu context is needed. " +
"Use request_approval when explicit human approval or confirmation is required before continuing.",
tools: [
tool(
"send_file",
@@ -77,6 +80,52 @@ export function createFileDeliveryMcpServer(options: FileDeliveryToolOptions): M
},
{ alwaysLoad: true },
),
tool(
"request_approval",
"Send an interactive Feishu approval/confirmation card to the current chat and wait for a button click.",
{
title: z.string().describe("Card title."),
body: z.string().describe("Markdown card body explaining what needs approval or confirmation."),
options: z
.array(z.object({
label: z.string().describe("Button label shown to the user."),
value: z.string().describe("Action value returned to the agent when this button is clicked."),
style: z.enum(["primary", "default", "danger"]).optional().describe("Optional Feishu button style."),
}))
.min(1)
.describe("Button choices for the approval card."),
},
async (args) => {
const messageId = await sendApprovalCard(
options.rt,
options.chatId,
args.title,
args.body,
args.options.map((option) => ({
label: option.label,
value: option.value,
...(option.style === undefined ? {} : { style: option.style }),
})),
);
if (messageId === null) {
return {
isError: true,
content: [{ type: "text", text: "Failed to send approval card." }],
};
}
try {
const result = await options.approvalManager.register(messageId, options.chatId);
return { content: [{ type: "text", text: JSON.stringify(result) }] };
} catch (e) {
return {
isError: true,
content: [{ type: "text", text: e instanceof Error ? e.message : String(e) }],
};
}
},
{ alwaysLoad: true },
),
],
});
}