fix(chat): 解决token传输、本地文件存储顺序、读取的问题

This commit is contained in:
2026-06-04 18:19:29 +08:00
parent 10c11a5254
commit fc0e76439d
3 changed files with 76 additions and 16 deletions
+9 -4
View File
@@ -1,4 +1,4 @@
import { createHash } from "node:crypto";
import { createHash, randomUUID } from "node:crypto";
import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
import { basename, dirname, join, relative } from "node:path";
@@ -13,9 +13,14 @@ export const ensureDirectory = async (path: string) => {
export const atomicWriteFile = async (path: string, content: string) => {
await ensureDirectory(dirname(path));
const tempPath = `${path}.${process.pid}.${Date.now().toString(36)}.tmp`;
await writeFile(tempPath, content, "utf8");
await rename(tempPath, path);
const tempPath = `${path}.${process.pid}.${Date.now().toString(36)}.${randomUUID()}.tmp`;
try {
await writeFile(tempPath, content, "utf8");
await rename(tempPath, path);
} catch (error) {
await removeFileIfExists(tempPath);
throw error;
}
};
type HistoricalWriteOptions = {