Skip to content

Commit fce32b6

Browse files
PR Batch Testerclaude
andcommitted
fix(server): image syntax in code or escaped text still gets a preview
Only a real image embed counts as already shown. Image syntax inside code spans or fences, or escaped with a backslash, renders as text, so the generated image is still appended. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 926d7fa commit fce32b6

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,17 @@ describe("appendGeneratedImageEmbeds", () => {
116116
);
117117
});
118118

119+
it.each([
120+
"```md\n![](/tmp/a.png)\n```",
121+
"Use `![](/tmp/a.png)` to embed it.",
122+
"Escaped \\![](/tmp/a.png)",
123+
])("still previews an image that only appears as text: %s", (text) => {
124+
assert.strictEqual(
125+
CodexAdapterV2.appendGeneratedImageEmbeds(text, ["/tmp/a.png"]),
126+
`${text}\n\n![](</tmp/a.png>)`,
127+
);
128+
});
129+
119130
it("appends each image once", () => {
120131
assert.strictEqual(
121132
CodexAdapterV2.appendGeneratedImageEmbeds("![](/tmp/a.png)", ["/tmp/a.png", "/tmp/b.png"]),

‎apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts‎

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,18 +1121,25 @@ export function appendGeneratedImageEmbeds(text: string, savedPaths: Iterable<st
11211121
: isUncPath(savedPath)
11221122
? `file:${savedPath.replaceAll("\\", "/")}`
11231123
: savedPath;
1124-
const imageEmbed = (path: string) =>
1125-
new RegExp(
1126-
`(!\\[[^\\]]*\\]\\()<?${path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}>?(?=[\\s)])`,
1124+
const destination = `<${embedPath.replace(/[<>]/g, "\\$&")}>`;
1125+
let embedded = false;
1126+
for (const path of new Set([savedPath, embedPath])) {
1127+
// Code spans, fences, and escaped `\![` are skipped: they show text, not an image.
1128+
const pattern = new RegExp(
1129+
"(```[\\s\\S]*?(?:```|$)|~~~[\\s\\S]*?(?:~~~|$)|`[^`\\n]*`)|(?<!\\\\)(!\\[[^\\]]*\\]\\()<?" +
1130+
path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") +
1131+
">?(?=[\\s)])",
11271132
"g",
11281133
);
1129-
const destination = `<${embedPath.replace(/[<>]/g, "\\$&")}>`;
1130-
// The reply may embed the raw Windows path, which some clients misread; point it at embedPath.
1131-
if (embedPath !== savedPath) {
1132-
result = result.replace(imageEmbed(savedPath), (_, prefix: string) => prefix + destination);
1134+
result = result.replace(pattern, (match, code?: string, prefix?: string) => {
1135+
if (code !== undefined || prefix === undefined) return match;
1136+
embedded = true;
1137+
// A raw Windows path would load the wrong file on some clients; point it at embedPath.
1138+
return path === embedPath ? match : prefix + destination;
1139+
});
11331140
}
11341141
// Only an existing image embed counts; a plain path mention or link still needs a preview.
1135-
if (!imageEmbed(embedPath).test(result)) {
1142+
if (!embedded) {
11361143
result += `${result.length > 0 ? "\n\n" : ""}![](${destination})`;
11371144
}
11381145
}

0 commit comments

Comments
 (0)