




















@@ -131,6 +131,28 @@ async function fileExists(pathName: string): Promise<boolean> {
131131}
132132}
133133134+function addCollisionSuffix(filePath: string, suffix: number): string {
135+const ext = path.extname(filePath);
136+const baseName = path.basename(filePath, ext);
137+return path.join(path.dirname(filePath), `${baseName}-${suffix}${ext}`);
138+}
139+140+async function writeNewDefaultExportFile(filePath: string, html: string): Promise<string> {
141+for (let suffix = 1; suffix <= 100; suffix++) {
142+const candidate = suffix === 1 ? filePath : addCollisionSuffix(filePath, suffix);
143+try {
144+await fsp.writeFile(candidate, html, { encoding: "utf-8", flag: "wx" });
145+return candidate;
146+} catch (error) {
147+if (typeof error === "object" && error && "code" in error && error.code === "EEXIST") {
148+continue;
149+}
150+throw error;
151+}
152+}
153+throw new Error(`Could not find an unused export filename near ${filePath}`);
154+}
155+134156async function readSessionDataFromTranscript(sessionFile: string): Promise<{
135157header: SessionHeader | null;
136158entries: PiSessionEntry[];
@@ -193,7 +215,7 @@ export async function buildExportSessionReply(params: HandleCommandsParams): Pro
193215// 6. Determine output path
194216const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
195217const defaultFileName = `openclaw-session-${entry.sessionId.slice(0, 8)}-${timestamp}.html`;
196-const outputPath = args.outputPath
218+let outputPath = args.outputPath
197219 ? path.resolve(
198220args.outputPath.startsWith("~")
199221 ? args.outputPath.replace("~", process.env.HOME ?? "")
@@ -206,7 +228,11 @@ export async function buildExportSessionReply(params: HandleCommandsParams): Pro
206228await fsp.mkdir(outputDir, { recursive: true });
207229208230// 7. Write file
209-await fsp.writeFile(outputPath, html, "utf-8");
231+if (args.outputPath) {
232+await fsp.writeFile(outputPath, html, "utf-8");
233+} else {
234+outputPath = await writeNewDefaultExportFile(outputPath, html);
235+}
210236211237const relativePath = path.relative(params.workspaceDir, outputPath);
212238const displayPath = relativePath.startsWith("..") ? outputPath : relativePath;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。