fix: decouple AI completion from history save failures and improve history API errors

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-06-16 21:31:20 +08:00
parent 123a5cce6d
commit 187b08c3e1
8 changed files with 145 additions and 53 deletions
+22 -11
View File
@@ -1,21 +1,32 @@
import { NextResponse } from "next/server";
import { getHistoryUsername } from "@/lib/history/request-user";
import { deleteHistoryEntry } from "@/lib/history/server-store";
import {
deleteHistoryEntry,
historyStoreErrorMessage,
} from "@/lib/history/server-store";
export async function DELETE(
_req: Request,
{ params }: { params: Promise<{ id: string }> },
) {
const username = await getHistoryUsername();
if (!username) {
return NextResponse.json({ error: "请先登录" }, { status: 401 });
}
try {
const username = await getHistoryUsername();
if (!username) {
return NextResponse.json({ error: "请先登录" }, { status: 401 });
}
const { id } = await params;
const removed = await deleteHistoryEntry(username, id);
if (!removed) {
return NextResponse.json({ error: "记录不存在" }, { status: 404 });
}
const { id } = await params;
const removed = await deleteHistoryEntry(username, id);
if (!removed) {
return NextResponse.json({ error: "记录不存在" }, { status: 404 });
}
return NextResponse.json({ ok: true });
return NextResponse.json({ ok: true });
} catch (err) {
console.error("[history DELETE]", err);
return NextResponse.json(
{ error: historyStoreErrorMessage(err) },
{ status: 500 },
);
}
}
+50 -33
View File
@@ -2,48 +2,65 @@ import { NextResponse } from "next/server";
import { getHistoryUsername } from "@/lib/history/request-user";
import {
addHistoryEntry,
historyStoreErrorMessage,
listHistoryEntries,
} from "@/lib/history/server-store";
import type { CalcHistoryCreate } from "@/lib/history/types";
export async function GET() {
const username = await getHistoryUsername();
if (!username) {
return NextResponse.json({ error: "请先登录" }, { status: 401 });
}
try {
const username = await getHistoryUsername();
if (!username) {
return NextResponse.json({ error: "请先登录" }, { status: 401 });
}
const items = await listHistoryEntries(username);
return NextResponse.json({ items });
const items = await listHistoryEntries(username);
return NextResponse.json({ items });
} catch (err) {
console.error("[history GET]", err);
return NextResponse.json(
{ error: historyStoreErrorMessage(err) },
{ status: 500 },
);
}
}
export async function POST(req: Request) {
const username = await getHistoryUsername();
if (!username) {
return NextResponse.json({ error: "请先登录" }, { status: 401 });
}
let body: CalcHistoryCreate;
try {
body = (await req.json()) as CalcHistoryCreate;
} catch {
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
const username = await getHistoryUsername();
if (!username) {
return NextResponse.json({ error: "请先登录" }, { status: 401 });
}
let body: CalcHistoryCreate;
try {
body = (await req.json()) as CalcHistoryCreate;
} catch {
return NextResponse.json({ error: "请求格式错误" }, { status: 400 });
}
if (!body.mode || !body.title || !body.question || !body.completion) {
return NextResponse.json({ error: "缺少必填字段" }, { status: 400 });
}
const entry = await addHistoryEntry(username, {
mode: body.mode,
title: body.title,
question: body.question,
summary: body.summary ?? "",
completion: body.completion,
meta: body.meta ?? {},
baziInput: body.baziInput,
baziChart: body.baziChart,
hexagram: body.hexagram,
});
return NextResponse.json({ entry });
} catch (err) {
console.error("[history POST]", err);
return NextResponse.json(
{ error: historyStoreErrorMessage(err) },
{ status: 500 },
);
}
if (!body.mode || !body.title || !body.question || !body.completion) {
return NextResponse.json({ error: "缺少必填字段" }, { status: 400 });
}
const entry = await addHistoryEntry(username, {
mode: body.mode,
title: body.title,
question: body.question,
summary: body.summary ?? "",
completion: body.completion,
meta: body.meta ?? {},
baziInput: body.baziInput,
baziChart: body.baziChart,
hexagram: body.hexagram,
});
return NextResponse.json({ entry });
}