fix(server): Codex generated images show in chat, including on Android with a Windows host #19066
Mergedshivamhwp(GitHub profile, opens in a new tab)merged 6 commits into
main fromrefs/pull/19066/headmerged by shivamhwp(GitHub profile, opens in a new tab) View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:trusted
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:M
- apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts line 6053OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -5996,3 +6050,4 @@59966050 (yield* Ref.get(interruptingNativeTurns)).has(payload.turn.id)59976051 ? "interrupted"59986052 : nativeStatus;6053+ const images = (yield* Ref.get(generatedImagesByTurn)).get(payload.turn.id);apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts lines 6061 to 6063OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -5996,3 +6050,14 @@59966050 (yield* Ref.get(interruptingNativeTurns)).has(payload.turn.id)59976051 ? "interrupted"59986052 : nativeStatus;6053+ const images = (yield* Ref.get(generatedImagesByTurn)).get(payload.turn.id);6054+ const firstImageItemId = images?.keys().next().value;6055+ if (images !== undefined && firstImageItemId !== undefined) {6056+ const finalAnswer = (yield* Ref.get(lastFinalAnswerByTurn)).get(payload.turn.id);6057+ let text = finalAnswer?.text ?? "";6058+ for (const savedPath of images.values()) {6059+ // Markdown reads the `\.` in `C:\Users\me\.codex` as an escape. Clients without6060+ // Windows path handling, such as released mobile apps, would load the wrong file.6061+ const embedPath = isWindowsAbsolutePath(savedPath)6062+ ? savedPath.replaceAll("\\", "/")6063+ : savedPath;
github-actions[bot](GitHub profile, opens in a new tab) removed labelssize:M
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:L
- apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts lines 1125 to 1127OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -1106,3 +1107,21 @@11061107 return { attempt, maxAttempts };11071108 }110811091110+/**1111+ * Appends a Markdown image for each saved path the text does not already embed.1112+ * Markdown reads the `\.` in `C:\Users\me\.codex` as an escape, and clients without1113+ * Windows path handling, such as released mobile apps, would load the wrong file. Drive1114+ * paths use forward slashes; UNC paths use `file:` URLs, since `//host` reads as a web URL.1115+ */1116+export function appendGeneratedImageEmbeds(text: string, savedPaths: Iterable<string>): string {1117+ let result = text;1118+ for (const savedPath of savedPaths) {1119+ const embedPath = isWindowsDrivePath(savedPath)1120+ ? savedPath.replaceAll("\\", "/")1121+ : isUncPath(savedPath)1122+ ? `file:${savedPath.replaceAll("\\", "/")}`1123+ : savedPath;1124+ // Only an existing image embed counts; a plain path mention or link still needs a preview.1125+ const embedded = [savedPath, embedPath].some((candidate) => {1126+ const escapedPath = candidate.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");1127+ return new RegExp(`!\\[[^\\]]*\\]\\(<?${escapedPath}>?[\\s)]`).test(result);apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts lines 5032 to 5033OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -4968,3 +5018,16 @@49685018 return;49695019 }497050205021+ if (payload.item.type === "imageGeneration") {5022+ const { id, savedPath, status, failure } = payload.item;5023+ if (savedPath?.trim() && status !== "failed" && failure == null) {5024+ yield* Ref.update(generatedImagesByTurn, (current) => {5025+ const updated = new Map(current);5026+ const images = new Map(updated.get(payload.turnId));5027+ images.set(id, savedPath);5028+ updated.set(payload.turnId, images);5029+ return updated;5030+ });5031+ // Work that outlives turn/completed, such as a backgrounded exec, can finish later.5032+ if (settled) {5033+ yield* projectGeneratedImages(context, payload.turnId);
- apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts line 1135OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -1108,0 +1115,21 @@1115+ */1116+export function appendGeneratedImageEmbeds(text: string, savedPaths: Iterable<string>): string {1117+ let result = text;1118+ for (const savedPath of savedPaths) {1119+ const embedPath = isWindowsDrivePath(savedPath)1120+ ? savedPath.replaceAll("\\", "/")1121+ : isUncPath(savedPath)1122+ ? `file:${savedPath.replaceAll("\\", "/")}`1123+ : savedPath;1124+ const imageEmbed = (path: string) =>1125+ new RegExp(1126+ `(!\\[[^\\]]*\\]\\()<?${path.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")}>?(?=[\\s)])`,1127+ "g",1128+ );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);1133+ }1134+ // Only an existing image embed counts; a plain path mention or link still needs a preview.1135+ if (!imageEmbed(embedPath).test(result)) {
- apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts lines 2223 to 2225OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2174,3 +2220,6 @@21742220 if ((yield* Ref.get(pendingMcpAppCaptures)).has(nativeTurnId)) {21752221 return true;21762222 }2223+ if ((yield* Ref.get(runningImagesByTurn)).get(nativeTurnId)?.size) {2224+ return true;2225+ }
- apps/server/src/orchestration-v2/Adapters/CodexAdapterV2.ts lines 2208 to 2210OutdatedResolvedby coderabbitai[bot](GitHub profile, opens in a new tab)@@ -2159,3 +2205,6 @@21592205 if ((yield* Ref.get(pendingMcpAppCaptures)).has(nativeTurnId)) {21602206 return true;21612207 }2208+ if ((yield* Ref.get(runningImagesByTurn)).get(nativeTurnId)?.size) {2209+ return true;2210+ }
juliusmarminge assignedjuliusmarminge
- packages/shared/src/generatedImageMarkdown.ts lines 42 to 42OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +22,21 @@22+ readonly position?:23+ | {24+ readonly start: { readonly offset?: number | undefined };25+ readonly end: { readonly offset?: number | undefined };26+ }27+ | undefined;28+ readonly children?: ReadonlyArray<MarkdownNode> | undefined;29+}30+31+// Mobile Markdown parsers consume Windows backslashes; UNC paths also need file: to32+// distinguish them from protocol-relative web URLs.33+function imageDestination(path: string): string {34+ return isWindowsDrivePath(path)35+ ? path.replaceAll("\\", "/")36+ : isUncPath(path)37+ ? `file:${path.replaceAll("\\", "/")}`38+ : path;39+}40+41+function angleDestination(path: string): string {42+ return `<${path.replace(/[%?#]/g, encodeURIComponent).replace(/[<>]/g, "\\$&")}>`;
- packages/shared/src/generatedImageMarkdown.ts line 110OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +90,21 @@90+ end !== undefined91+ ) {92+ const label =93+ target.type === "image" ? (target.alt ?? "") : (target.label ?? target.identifier ?? "");94+ const escapedLabel = label.replace(/[\\[\]]/g, "\\$&");95+ const title = target.title == null ? "" : ` "${target.title.replace(/["\\]/g, "\\$&")}"`;96+ edits.set(start, {97+ end,98+ text:99+ target.type === "image"100+ ? ``101+ : `[${escapedLabel}]: ${destination}${title}`,102+ });103+ }104+ }105+ let result = text;106+ for (const [start, edit] of Array.from(edits).sort(([left], [right]) => right - left)) {107+ result = result.slice(0, start) + edit.text + result.slice(edit.end);108+ }109+ for (const path of missing.values()) {110+ result += `${result.length > 0 ? "\n\n" : ""})})`;
- packages/shared/src/generatedImageMarkdown.ts line 95OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +75,21 @@75+ ? parseFileUrlHref(target.url.replace(/[%?#]/g, encodeURIComponent))76+ : null;77+ const rawPath = stripSlashPrefixedWindowsDrive(78+ file === null ? target.url : safeDecodeURIComponent(file.path),79+ );80+ const path = [rawPath, safeDecodeURIComponent(rawPath)].find((candidate) =>81+ destinations.has(normalizeProjectPathForComparison(candidate)),82+ );83+ if (path === undefined) continue;84+ missing.delete(normalizeProjectPathForComparison(path));85+ const destination = angleDestination(imageDestination(path));86+ const start = target.position?.start.offset;87+ const end = target.position?.end.offset;88+ if (89+ (imageDestination(target.url) !== target.url ||90+ /[?#]/.test(target.url) ||91+ (path === rawPath && path.includes("%"))) &&92+ start !== undefined &&93+ end !== undefined94+ ) {95+ const label =
macroscopeapp[bot](GitHub profile, opens in a new tab) approved these changes
shivamhwp(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
macroscopeapp[bot](GitHub profile, opens in a new tab) reviewed