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
+18
View File
@@ -15,6 +15,24 @@ function getDataDir(): string {
return path.join(process.cwd(), "data", "history");
}
export function historyStoreErrorMessage(err: unknown): string {
if (!(err instanceof Error)) {
return "保存测算历史失败";
}
const code = (err as NodeJS.ErrnoException).code;
const dir = getDataDir();
if (code === "EACCES" || code === "EPERM") {
return `测算历史目录无写入权限(${dir}),请检查 HISTORY_DATA_DIR 或目录权限`;
}
if (code === "ENOENT") {
return `测算历史目录不存在(${dir}),请创建目录或配置 HISTORY_DATA_DIR`;
}
if (code === "ENOSPC") {
return "磁盘空间不足,无法保存测算历史";
}
return `保存测算历史失败:${err.message}`;
}
function sanitizeUsername(username: string): string {
const safe = username.replace(/[^a-zA-Z0-9_-]/g, "_");
return safe || "guest";
+16
View File
@@ -34,6 +34,22 @@ export async function saveHistoryEntry(
return data.entry;
}
export async function saveHistoryEntrySafe(
entry: CalcHistoryCreate,
): Promise<
{ ok: true; entry: CalcHistoryEntry } | { ok: false; error: string }
> {
try {
const saved = await saveHistoryEntry(entry);
return { ok: true, entry: saved };
} catch (err) {
return {
ok: false,
error: err instanceof Error ? err.message : String(err),
};
}
}
export async function deleteHistoryEntry(id: string): Promise<void> {
const res = await fetch(`/api/history/${encodeURIComponent(id)}`, {
method: "DELETE",