Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

fix(sessions): include final reply image labels in Markdown exports (#10203)

Jiarui Fan4fb27701f1eb
diff --git a/src/components/sessions/reader/exportMarkdown.ts b/src/components/sessions/reader/exportMarkdown.ts
index 78ee160ab..5469d45bf 100644
--- a/src/components/sessions/reader/exportMarkdown.ts
+++ b/src/components/sessions/reader/exportMarkdown.ts
@@ -16,6 +16,8 @@ import type {
TimelineStep,
TurnChapter,
TurnEvent,
+ TurnFinal,
+ TurnQuestion,
} from "./turns";
/**
@@ -113,6 +115,19 @@ const chapterSection = (chapter: TurnChapter) =>
const heading = (label: string, ts?: number) =>
`**${label}**${ts ? ` ${formatMessageTime(ts)}` : ""}`;
+const messageSection = (
+ label: string,
+ message: TurnQuestion | TurnFinal,
+ t: Translate,
+) => {
+ const images = message.images.map(
+ (image, index) => `[${image.alt || t("image.alt", { index: index + 1 })}]`,
+ );
+ return [heading(label, message.ts), message.text, images.join(" ")]
+ .filter(Boolean)
+ .join("\n\n");
+};
+
export const turnToMarkdown = (
turn: SessionTurn,
options: MarkdownExportOptions,
@@ -121,19 +136,7 @@ export const turnToMarkdown = (
const sections: string[] = [];
turn.leadingEvents.forEach((event) => sections.push(eventLine(event, t)));
if (turn.question) {
- const images = turn.question.images.map(
- (image, index) =>
- `[${image.alt || t("image.alt", { index: index + 1 })}]`,
- );
- sections.push(
- [
- heading(options.youLabel, turn.question.ts),
- turn.question.text,
- images.join(" "),
- ]
- .filter(Boolean)
- .join("\n\n"),
- );
+ sections.push(messageSection(options.youLabel, turn.question, t));
}
// 步骤列表在章节处断开:章节小标题插在它后面第一个步骤之前
let lines: string[] = [];
@@ -155,9 +158,7 @@ export const turnToMarkdown = (
pushChapters();
flush();
if (turn.final) {
- sections.push(
- `${heading(options.appName, turn.final.ts)}\n\n${turn.final.text}`,
- );
+ sections.push(messageSection(options.appName, turn.final, t));
}
turn.trailingEvents.forEach((event) => sections.push(eventLine(event, t)));
return sections.join("\n\n");
diff --git a/tests/components/SessionReader.test.tsx b/tests/components/SessionReader.test.tsx
index afc268d11..d85477271 100644
--- a/tests/components/SessionReader.test.tsx
+++ b/tests/components/SessionReader.test.tsx
@@ -18,7 +18,7 @@ import {
clearSessionImageCache,
type SessionTranscriptResult,
} from "@/lib/query/sessions";
-import type { SessionMessage, SessionMeta } from "@/types";
+import type { SessionBlock, SessionMessage, SessionMeta } from "@/types";
import { call, fixtures, msg, result, text } from "./sessions/reader/helpers";
import { createTestQueryClient } from "../utils/testQueryClient";
@@ -610,6 +610,69 @@ describe("SessionReader", () => {
expect(markdown).toContain("### 梳理现状\n\n> 先读代码");
});
+ it("keeps final reply image labels in all Markdown outputs", async () => {
+ const image = (file: string, alt?: string): SessionBlock => ({
+ type: "image",
+ image: {
+ alt,
+ mediaType: "image/png",
+ size: 8,
+ source: { kind: "local_file", path: `/fixture/images/${file}` },
+ },
+ });
+ const messages = [
+ msg(
+ "user",
+ [text("Review the output."), image("reference.png", "reference.png")],
+ { turnId: "t1" },
+ ),
+ msg(
+ "assistant",
+ [
+ call("read-source", "read"),
+ result("read-source"),
+ call("check"),
+ result("check"),
+ text("Here are the two results."),
+ image("diagram.png", "diagram.png"),
+ image("unnamed.png"),
+ ],
+ { turnId: "t1" },
+ ),
+ ];
+ const writeMarkdown = vi
+ .spyOn(sessionsApi, "exportMarkdown")
+ .mockResolvedValue("/tmp/result.md");
+ renderReader("opencode", messages);
+ await screen.findByRole("button", { name: "放大查看:diagram.png" });
+ await screen.findByRole("button", { name: "放大查看:图 2" });
+ fireEvent.click(screen.getByRole("button", { name: "复制这一轮" }));
+ await userEvent.click(
+ screen.getByRole("button", { name: "opencode 会话 的更多操作" }),
+ );
+ await userEvent.click(
+ await screen.findByRole("menuitem", { name: "复制整段为 Markdown" }),
+ );
+ fireEvent.click(
+ screen.getByRole("button", { name: "导出为 Markdown 文件" }),
+ );
+ await waitFor(() => expect(writeMarkdown).toHaveBeenCalledTimes(1));
+ const [turn, transcript] = onCopy.mock.calls.map(
+ ([markdown]) => markdown as string,
+ );
+ const exported = writeMarkdown.mock.calls[0][1];
+ expect(exported).toBe(transcript);
+ const outputs = [turn, transcript, exported];
+ for (const markdown of outputs) {
+ expect(markdown).toContain("[reference.png]");
+ expect(markdown).toContain(
+ "Here are the two results.\n\n[diagram.png] [图 2]",
+ );
+ expect(markdown).not.toContain("/fixture/images/");
+ expect(markdown).not.toContain("data:image/");
+ }
+ });
+
it("导出为 Markdown 文件:用会话标题做文件名,内容与复制为 Markdown 一致", async () => {
const exportMarkdown = vi
.spyOn(sessionsApi, "exportMarkdown")