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
@@ -16,7 +16,7 @@ import RegionSelect, {
} from "@/components/shared/region-select";
import { calculateBazi, type BaziChart } from "@/lib/calc/bazi";
import { streamAiCompletion } from "@/lib/ai/client-stream";
import { saveHistoryEntry } from "@/lib/history/storage";
import { saveHistoryEntrySafe } from "@/lib/history/storage";
export default function BaziForm() {
const [date, setDate] = useState(todaySolarYmd());
@@ -29,6 +29,7 @@ export default function BaziForm() {
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 location = useRegionLocation(provinceCode, cityCode);
@@ -77,6 +78,7 @@ export default function BaziForm() {
}
setError("");
setWarning("");
setCompletion("");
setIsLoading(true);
@@ -92,7 +94,7 @@ export default function BaziForm() {
},
setCompletion,
);
await saveHistoryEntry({
const saveResult = await saveHistoryEntrySafe({
mode: "bazi",
title: "生辰八字解读",
question,
@@ -115,6 +117,9 @@ export default function BaziForm() {
: `${activeChart.pillars.year.ganZhi} ${activeChart.pillars.month.ganZhi} ${activeChart.pillars.day.ganZhi} ${activeChart.pillars.time.ganZhi}`,
},
});
if (!saveResult.ok) {
setWarning(saveResult.error);
}
} catch (err) {
setError(err instanceof Error ? err.message : String(err));
} finally {
@@ -137,6 +142,7 @@ export default function BaziForm() {
isLoading={isLoading}
onCompletion={handleAnalyze}
error={error}
warning={warning}
emptyHint="排盘后点击「AI 测算」获取解读"
downloadFilename={completion ? "生辰八字解读.md" : undefined}
downloadPreamble={downloadPreamble}