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.
 
 
 
 

66 lines
1.9 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 } = body as { sessionId: string };
if (!sessionId) {
return R.error("参数无效", null);
}
const session = await getSessionByIdAndUser(sessionId, user.id, null);
if (!session) {
return R.error("会话不存在", null);
}
const messages = await getMessagesBySession(sessionId, { limit: 100, latest: true });
const updatedMessageIds: string[] = [];
for (const msg of messages) {
if (msg.role !== "assistant" || !msg.parts) continue;
let parts: StoredPart[];
try {
parts = JSON.parse(msg.parts);
} catch {
continue;
}
const hasText = parts.some((p) => p.type === "text" && p.text);
if (!hasText) continue;
let modified = false;
for (const p of parts) {
if (p.state === "approval-requested" && !p.isAutomaticApproval) {
p.state = "result";
p.result = "(此工具调用已被跳过——AI 已给出最终回复)";
modified = true;
}
}
if (modified) {
await updateMessageParts(msg.id, JSON.stringify(parts));
updatedMessageIds.push(msg.id);
}
}
logger.info(
"[%s] [AGENT-CLEANUP-APPROVALS] userId=%d sessionId=%s updated=%d",
event.context.requestId ?? "-",
user.id,
sessionId,
updatedMessageIds.length,
);
return R.success({ updatedCount: updatedMessageIds.length });
});