You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

75 lines
2.2 KiB

import { defineWrappedResponseHandler } from "#server/utils/handler";
import { R } from "#server/utils/response";
import { getCurrentUser } from "#server/utils/context";
import { getSessionByIdAndUser, getMessagesBySession, updateMessageParts } from "#server/service/agent/session";
import { type StoredPart } from "./types";
import log4js from "logger";
const logger = log4js.getLogger("APP");
export default defineWrappedResponseHandler(async (event) => {
const user = await getCurrentUser(event);
if (!user) {
return R.error("仅登录用户可审批工具", null);
}
const body = await readBody(event);
const { sessionId, toolCallId, approved, reason } = body as {
sessionId: string;
toolCallId: string;
approved: boolean;
reason?: string;
};
if (!sessionId || !toolCallId) {
return R.error("参数无效", null);
}
const session = await getSessionByIdAndUser(sessionId, user.id, null);
if (!session) {
return R.error("会话不存在", null);
}
logger.info(
"[%s] [AGENT-TOOL-APPROVE] userId=%d sessionId=%s toolCallId=%s approved=%s",
event.context.requestId ?? "-",
user.id,
sessionId,
toolCallId,
approved ? "yes" : "no",
);
const messages = await getMessagesBySession(sessionId, { limit: 100, latest: true });
let updated = false;
for (const msg of messages) {
if (!msg.parts) continue;
let parts: StoredPart[];
try {
parts = JSON.parse(msg.parts);
} catch {
continue;
}
const targetPart = parts.find(
(p) => p.type === "tool-call" && p.toolCallId === toolCallId && p.state === "approval-requested",
);
if (!targetPart) continue;
targetPart.state = "approval-responded";
targetPart.approved = approved;
targetPart.approvalReason = reason;
await updateMessageParts(msg.id, JSON.stringify(parts));
updated = true;
break;
}
if (!updated) {
return R.error("未找到待审批的工具调用", null);
}
if (!approved) {
return R.success({ toolCallId, result: "rejected", message: "用户拒绝了工具调用" });
}
return R.success({ toolCallId, result: "approved", message: "工具调用已批准" });
});