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
+8 -2
View File
@@ -23,7 +23,7 @@ import RegionSelect, {
import { calculateBazi, type BaziChart } from "@/lib/calc/bazi";
import type { GuaResult } from "@/lib/calc/hexagram";
import { streamAiCompletion } from "@/lib/ai/client-stream";
import { saveHistoryEntry } from "@/lib/history/storage";
import { saveHistoryEntrySafe } from "@/lib/history/storage";
export default function CombinedForm() {
const [birthDate, setBirthDate] = useState(todaySolarYmd());
@@ -45,6 +45,7 @@ export default function CombinedForm() {
const [chart, setChart] = useState<BaziChart | null>(null);
const [completion, setCompletion] = useState("");
const [error, setError] = useState("");
const [warning, setWarning] = useState("");
const [isLoading, setIsLoading] = useState(false);
const birthLocation = useRegionLocation(birthProvince, birthCity);
@@ -112,6 +113,7 @@ export default function CombinedForm() {
}
setError("");
setWarning("");
setCompletion("");
setIsLoading(true);
@@ -155,7 +157,7 @@ export default function CombinedForm() {
},
setCompletion,
);
await saveHistoryEntry({
const saveResult = await saveHistoryEntrySafe({
mode: "combined",
title: "综合测算解读",
question,
@@ -189,6 +191,9 @@ export default function CombinedForm() {
六爻: withHexagram && guaData ? guaData.result.guaTitle : "无",
},
});
if (!saveResult.ok) {
setWarning(saveResult.error);
}
} catch (e) {
setError(e instanceof Error ? e.message : String(e));
} finally {
@@ -211,6 +216,7 @@ export default function CombinedForm() {
isLoading={isLoading}
onCompletion={handleAnalyze}
error={error}
warning={warning}
emptyHint="填写完整信息后,点击「综合测算」"
downloadFilename={completion ? "综合测算解读.md" : undefined}
downloadPreamble={downloadPreamble}