2026-05-06 21:44:20 +08:00
|
|
|
import { errorResponses, safeGetJsonBody, validateRequestBody } from "@/lib/api-utils";
|
2026-04-16 15:24:37 +08:00
|
|
|
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";
|
|
|
|
|
|
2026-04-16 15:24:37 +08:00
|
|
|
export async function POST(request: Request) {
|
2026-05-06 21:44:20 +08:00
|
|
|
const payload = await safeGetJsonBody<MnoteCliAgentRunPayload>(request);
|
2026-04-16 15:24:37 +08:00
|
|
|
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;
|
2026-04-16 15:24:37 +08:00
|
|
|
if (isConvexEnabled()) {
|
|
|
|
|
const { auth } = await getAuthedConvexClient();
|
|
|
|
|
userId = auth.userId ?? "";
|
2026-05-06 21:44:20 +08:00
|
|
|
userEmail = auth.email;
|
|
|
|
|
userName = auth.name;
|
2026-04-16 15:24:37 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
}
|