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.
52 lines
1.6 KiB
52 lines
1.6 KiB
import { defineWrappedResponseHandler } from "#server/utils/handler";
|
|
import { R } from "#server/utils/response";
|
|
import { getCurrentUser } from "#server/utils/context";
|
|
import { getSessionByIdAndUser, getMessageById } from "#server/service/agent/session";
|
|
import { executeAgentTool } from "#server/service/agent-tool";
|
|
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, messageId, toolCallId, approved } = body as {
|
|
sessionId: string;
|
|
messageId: string;
|
|
toolCallId: string;
|
|
approved: boolean;
|
|
};
|
|
|
|
if (!sessionId || !messageId || !toolCallId) {
|
|
return R.error("参数无效", null);
|
|
}
|
|
|
|
const session = await getSessionByIdAndUser(sessionId, user.id, null);
|
|
if (!session) {
|
|
return R.error("会话不存在", null);
|
|
}
|
|
|
|
const message = await getMessageById(messageId);
|
|
if (!message || message.sessionId !== sessionId) {
|
|
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",
|
|
);
|
|
|
|
if (!approved) {
|
|
return R.success({ toolCallId, result: "rejected", message: "用户拒绝了工具调用" });
|
|
}
|
|
|
|
return R.success({ toolCallId, result: "approved", message: "工具调用已批准,请继续对话" });
|
|
});
|
|
|