Files
mnote/wolai-frontend/src/app/api/ai-agent/run/route.ts
T

43 lines
1.2 KiB
TypeScript
Raw Normal View History

2026-05-06 21:44:20 +08:00
import { errorResponses, safeGetJsonBody, validateRequestBody } from "@/lib/api-utils";
import { isConvexEnabled } from "@/lib/convex/enabled";
import { getAuthedConvexClient } from "@/lib/convex/route";
import {
2026-05-06 21:44:20 +08:00
startMnoteCliAgentHostRun,
type MnoteCliAgentRunPayload,
} from "@/lib/server/mnote-cli-agent-host";
2026-01-10 23:08:56 +08:00
export const dynamic = "force-dynamic";
export async function POST(request: Request) {
2026-05-06 21:44:20 +08:00
const payload = await safeGetJsonBody<MnoteCliAgentRunPayload>(request);
if (!payload) {
return errorResponses.badRequest("请求体不能为空");
}
const validationError = validateRequestBody(payload, ["messages"] as const);
if (validationError) return validationError;
if (!Array.isArray(payload.messages) || payload.messages.length === 0) {
return errorResponses.badRequest("缺少 messages");
}
let userId = "";
2026-05-06 21:44:20 +08:00
let userEmail: string | undefined;
let userName: string | undefined;
if (isConvexEnabled()) {
const { auth } = await getAuthedConvexClient();
userId = auth.userId ?? "";
2026-05-06 21:44:20 +08:00
userEmail = auth.email;
userName = auth.name;
}
if (!userId) {
return errorResponses.unauthorized();
}
2026-05-06 21:44:20 +08:00
return startMnoteCliAgentHostRun({
request,
userId,
userEmail,
userName,
payload,
2026-01-10 23:08:56 +08:00
});
}