forked from EduCraft/curriculum-project-hub
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:
@@ -11,7 +11,7 @@
|
||||
import type { Prisma, PrismaClient } from "@prisma/client";
|
||||
import { z } from "zod";
|
||||
import type { FastifyBaseLogger } from "fastify";
|
||||
import { sendText, sendTextMessage, patchTextMessage, reactToMessage, addReaction, removeReaction, downloadMessageFile, type FeishuRuntime, type MessageReceiveEvent } from "./client.js";
|
||||
import { sendText, sendTextMessage, patchTextMessage, reactToMessage, addReaction, removeReaction, downloadMessageFile, resolveCard, type CardActionEvent, type FeishuRuntime, type MessageReceiveEvent } from "./client.js";
|
||||
import { runAgent as defaultRunAgent, type ProjectContext, type RunRequest, type RunResult } from "../agent/runner.js";
|
||||
import type { RuntimeSettings } from "../settings/runtime.js";
|
||||
import { acquireLock, currentLockRunId, releaseLock } from "../lock.js";
|
||||
@@ -21,6 +21,10 @@ import { PatchableTextStream } from "./textStream.js";
|
||||
import { createFileDeliveryMcpServer } from "./fileDeliveryTool.js";
|
||||
import { readFeishuContext } from "./read.js";
|
||||
import { MessageBatcher, messageBatchKey, type MessageBatcherOptions } from "./messageBatcher.js";
|
||||
import { ApprovalManager } from "./approval.js";
|
||||
|
||||
export { ApprovalManager } from "./approval.js";
|
||||
export type { ApprovalResult, PendingApproval } from "./approval.js";
|
||||
|
||||
interface TriggerDeps {
|
||||
readonly prisma: PrismaClient;
|
||||
@@ -45,14 +49,21 @@ interface TriggerRunContext {
|
||||
readonly actor: TriggerActor;
|
||||
}
|
||||
|
||||
export interface TriggerHandler {
|
||||
(event: MessageReceiveEvent, rt: FeishuRuntime): Promise<void>;
|
||||
readonly onCardAction: (event: CardActionEvent, rt: FeishuRuntime) => Promise<void>;
|
||||
readonly approvalManager: ApprovalManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the message handler. Returns a function suitable for the lark
|
||||
* `im.message.receive_v1` event. The handler is async and long-running (the
|
||||
* agent run is the long leg); the lock guarantees one run per project at a time.
|
||||
*/
|
||||
export function makeTriggerHandler(deps: TriggerDeps) {
|
||||
export function makeTriggerHandler(deps: TriggerDeps): TriggerHandler {
|
||||
const authorizer = deps.authorizer ?? createPermissionAuthorizer(deps.prisma);
|
||||
const runAgent = deps.runAgent ?? defaultRunAgent;
|
||||
const approvalManager = new ApprovalManager();
|
||||
const batchContexts = new Map<string, TriggerRunContext>();
|
||||
const messageBatcher = new MessageBatcher(async (mergedText, key) => {
|
||||
if (key === undefined) {
|
||||
@@ -227,6 +238,7 @@ export function makeTriggerHandler(deps: TriggerDeps) {
|
||||
projectId,
|
||||
runId: run.id,
|
||||
workspaceDir: project.workspaceDir,
|
||||
approvalManager,
|
||||
onDelivered: (path) => {
|
||||
deliveredFiles.push(path);
|
||||
},
|
||||
@@ -309,7 +321,19 @@ export function makeTriggerHandler(deps: TriggerDeps) {
|
||||
});
|
||||
}
|
||||
|
||||
return async (event: MessageReceiveEvent, rt: FeishuRuntime): Promise<void> => {
|
||||
const onCardAction = async (event: CardActionEvent, rt: FeishuRuntime): Promise<void> => {
|
||||
const action = approvalActionFromValue(event.action.value);
|
||||
if (action === null) return;
|
||||
const messageId = nonEmpty(event.context?.open_message_id);
|
||||
if (messageId === undefined || !approvalManager.hasPending(messageId)) return;
|
||||
|
||||
const resolvedBy = nonEmpty(event.operator.open_id) ?? "unknown";
|
||||
const resolution = approvalResolutionFor(action);
|
||||
approvalManager.resolve(messageId, action, resolvedBy);
|
||||
await resolveCard(rt, messageId, resolution.icon, resolution.label, resolution.template, resolvedBy);
|
||||
};
|
||||
|
||||
const onMessage = async (event: MessageReceiveEvent, rt: FeishuRuntime): Promise<void> => {
|
||||
const msg = event.message;
|
||||
const sender = event.sender;
|
||||
void sender; // sender→user mapping is OPEN (principal sub-typology)
|
||||
@@ -468,6 +492,8 @@ export function makeTriggerHandler(deps: TriggerDeps) {
|
||||
|
||||
await startAgentRun(runContext, cleanPrompt);
|
||||
};
|
||||
|
||||
return Object.assign(onMessage, { onCardAction, approvalManager });
|
||||
}
|
||||
|
||||
interface SessionMetadata {
|
||||
@@ -517,6 +543,51 @@ function nonEmpty(value: string | undefined): string | undefined {
|
||||
return value === undefined || value === "" ? undefined : value;
|
||||
}
|
||||
|
||||
function approvalActionFromValue(value: unknown): string | null {
|
||||
if (typeof value === "string") {
|
||||
try {
|
||||
return approvalActionFromValue(JSON.parse(value));
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
|
||||
const action = (value as Record<string, unknown>)["approval_action"];
|
||||
return typeof action === "string" && action !== "" ? action : null;
|
||||
}
|
||||
|
||||
function approvalResolutionFor(action: string): { readonly icon: string; readonly label: string; readonly template: "green" | "red" } {
|
||||
const normalized = action.trim().toLowerCase();
|
||||
switch (normalized) {
|
||||
case "approve":
|
||||
case "approved":
|
||||
return { icon: "✅", label: "Approved", template: "green" };
|
||||
case "accept":
|
||||
case "accepted":
|
||||
return { icon: "✅", label: "Accepted", template: "green" };
|
||||
case "confirm":
|
||||
case "confirmed":
|
||||
case "yes":
|
||||
case "ok":
|
||||
return { icon: "✅", label: "Confirmed", template: "green" };
|
||||
case "reject":
|
||||
case "rejected":
|
||||
return { icon: "❌", label: "Rejected", template: "red" };
|
||||
case "deny":
|
||||
case "denied":
|
||||
case "decline":
|
||||
case "declined":
|
||||
return { icon: "❌", label: "Declined", template: "red" };
|
||||
case "cancel":
|
||||
case "canceled":
|
||||
case "cancelled":
|
||||
case "no":
|
||||
return { icon: "❌", label: "Cancelled", template: "red" };
|
||||
default:
|
||||
return { icon: "✅", label: action, template: "green" };
|
||||
}
|
||||
}
|
||||
|
||||
interface BuildFeishuTriggerContextArgs {
|
||||
readonly msg: MessageReceiveEvent["message"];
|
||||
readonly chatId: string;
|
||||
|
||||
Reference in New Issue
Block a user