fix(rectification): restore message actions and retry receipts
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import { NextResponse } from "next/server";
|
||||
import { z } from "zod";
|
||||
import { regenerateV9AssistantTurn } from "@/lib/rectification-agentic/v9/regenerate-turn";
|
||||
import { RectificationToolServiceError } from "@/lib/rectification-agentic/v9/tool-service";
|
||||
import { resolveSessionLanguageModel } from "@/lib/model-catalog";
|
||||
import { createAdminSupabaseClient } from "@/lib/supabase/admin";
|
||||
import { createServerSupabaseClient } from "@/lib/supabase/server";
|
||||
import { getRectificationV9RegenerationAgent } from "@/mastra/agentic-rectification";
|
||||
|
||||
export const runtime = "nodejs";
|
||||
export const maxDuration = 120;
|
||||
|
||||
type RouteContext = { params: Promise<{ caseId: string; turnId: string }> };
|
||||
|
||||
const regenerateSchema = z.object({
|
||||
sessionId: z.string().uuid(),
|
||||
requestId: z.string().uuid(),
|
||||
}).strict();
|
||||
|
||||
function errorResponse(error: unknown) {
|
||||
const code = error instanceof RectificationToolServiceError
|
||||
? error.code
|
||||
: error instanceof Error
|
||||
? error.message
|
||||
: String(error);
|
||||
const known = [
|
||||
["agentic_rectification_case_not_found", 404, "校正记录不存在或无权访问", "case_not_found"],
|
||||
["agentic_rectification_turn_not_found", 404, "这条回答不存在或无法重新生成", "turn_not_found"],
|
||||
["agentic_rectification_case_session_mismatch", 409, "校正记录与会话绑定不一致", "case_session_mismatch"],
|
||||
["agentic_rectification_case_terminal", 409, "该校正已结束,只能查看历史", "case_terminal"],
|
||||
["agentic_rectification_turn_not_latest", 409, "只能重新生成最近一条回答", "turn_not_latest"],
|
||||
["agentic_rectification_turn_not_completed", 409, "这条回答尚未完成", "turn_not_completed"],
|
||||
["agentic_rectification_regeneration_request_conflict", 409, "重新生成请求已用于其他回答", "request_conflict"],
|
||||
] as const;
|
||||
for (const [needle, status, message, publicCode] of known) {
|
||||
if (code.includes(needle)) {
|
||||
return NextResponse.json({ error: message, code: publicCode }, { status });
|
||||
}
|
||||
}
|
||||
return NextResponse.json(
|
||||
{ error: "暂时无法重新生成回答", code: "regeneration_failed" },
|
||||
{ status: 503 },
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Free, read-only reply regeneration. The model receives only the bound Skill
|
||||
* and rectification-read-case tool. The database then replaces the latest
|
||||
* completed Assistant text in place; no turn is appended and billing is never
|
||||
* invoked by this route.
|
||||
*/
|
||||
export async function POST(request: Request, context: RouteContext) {
|
||||
let supabase;
|
||||
let accounting;
|
||||
try {
|
||||
supabase = await createServerSupabaseClient();
|
||||
accounting = createAdminSupabaseClient();
|
||||
} catch {
|
||||
return NextResponse.json({ error: "服务尚未配置" }, { status: 503 });
|
||||
}
|
||||
|
||||
const {
|
||||
data: { user },
|
||||
error: authError,
|
||||
} = await supabase.auth.getUser();
|
||||
if (authError || !user) {
|
||||
return NextResponse.json({ error: "请先登录" }, { status: 401 });
|
||||
}
|
||||
|
||||
const { caseId, turnId } = await context.params;
|
||||
if (!z.string().uuid().safeParse(caseId).success || !z.string().uuid().safeParse(turnId).success) {
|
||||
return NextResponse.json({ error: "请求内容不正确", code: "invalid_reference" }, { status: 400 });
|
||||
}
|
||||
const parsed = regenerateSchema.safeParse(await request.json().catch(() => null));
|
||||
if (!parsed.success) {
|
||||
return NextResponse.json({ error: "请求内容不正确", code: "invalid_regeneration_request" }, { status: 400 });
|
||||
}
|
||||
|
||||
const { data: chatSession, error: chatSessionError } = await supabase
|
||||
.from("chat_sessions")
|
||||
.select("id,session_type,model_id,model_config_version,agentic_rectification_case_id")
|
||||
.eq("id", parsed.data.sessionId)
|
||||
.eq("user_id", user.id)
|
||||
.maybeSingle();
|
||||
if (chatSessionError) {
|
||||
return NextResponse.json({ error: "暂时无法读取会话" }, { status: 503 });
|
||||
}
|
||||
if (!chatSession || chatSession.session_type !== "birth_time_rectification") {
|
||||
return NextResponse.json({ error: "生时校正会话不存在", code: "session_not_found" }, { status: 404 });
|
||||
}
|
||||
if (chatSession.agentic_rectification_case_id !== caseId) {
|
||||
return NextResponse.json({ error: "校正记录与会话绑定不一致", code: "case_session_mismatch" }, { status: 409 });
|
||||
}
|
||||
|
||||
const selectedModel = await resolveSessionLanguageModel(
|
||||
chatSession.model_id,
|
||||
chatSession.model_config_version,
|
||||
);
|
||||
if (!selectedModel) {
|
||||
return NextResponse.json(
|
||||
{ error: "模型暂不可用", message: "请选择其他模型后再试,本次不会扣除点数。" },
|
||||
{ status: 409 },
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const agent = getRectificationV9RegenerationAgent(selectedModel, {
|
||||
userId: user.id,
|
||||
caseId,
|
||||
turnId,
|
||||
accounting,
|
||||
});
|
||||
const result = await regenerateV9AssistantTurn({
|
||||
userId: user.id,
|
||||
caseId,
|
||||
sessionId: parsed.data.sessionId,
|
||||
turnId,
|
||||
requestId: parsed.data.requestId,
|
||||
accounting,
|
||||
agent,
|
||||
signal: request.signal,
|
||||
});
|
||||
return NextResponse.json({
|
||||
ok: true,
|
||||
turnId: result.turnId,
|
||||
assistantMessage: result.assistantMessage,
|
||||
idempotent: result.idempotent,
|
||||
});
|
||||
} catch (error) {
|
||||
return errorResponse(error);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user