fix(chat): render assistant-generated images inline #6558
ClosedaGamingGod1234(GitHub profile, opens in a new tab)wants to merge 3 commits into
main fromrefs/pull/6558/headopened View on GitHubgithub-actions[bot](GitHub profile, opens in a new tab) added labelsvouch:unvouched
github-actions[bot](GitHub profile, opens in a new tab) added labelssize:XL
- apps/server/src/orchestration/AssistantImageAttachments.ts lines 162 to 168OutdatedResolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +148,21 @@148+ if (savedPath) {149+ const name = basename(savedPath).slice(0, 255);150+ if (name.length > 0) {151+ add({ _tag: "local-file", path: savedPath, name });152+ }153+ return;154+ }155+ const result = stringProperty(value, "result");156+ if (result) {157+ add(dataUrlInput(result, suggestedName));158+ }159+ return;160+ }161+162+ const imageUrl = stringProperty(value, "image_url") ?? stringProperty(value, "imageUrl");163+ if (imageUrl) {164+ add(dataUrlInput(imageUrl, suggestedName));165+ if (type === "generated_image" || type === "generatedImage") {166+ return;167+ }168+ }apps/server/src/orchestration/AssistantImageAttachments.ts line 159Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +126,21 @@126+ const visit = (value: unknown, depth: number): void => {127+ if (depth > MAX_TRAVERSAL_DEPTH || inputs.length >= PROVIDER_SEND_TURN_MAX_ATTACHMENTS) {128+ return;129+ }130+ if (Array.isArray(value)) {131+ for (const entry of value) visit(entry, depth + 1);132+ return;133+ }134+ if (!Predicate.isObject(value) || seenObjects.has(value)) {135+ return;136+ }137+ seenObjects.add(value);138+139+ const type = stringProperty(value, "type");140+ const suggestedName =141+ stringProperty(value, "output_hint") ??142+ stringProperty(value, "outputHint") ??143+ stringProperty(value, "fileName") ??144+ stringProperty(value, "name");145+146+ if (type === "imageGeneration") {apps/server/src/orchestration/AssistantImageAttachments.ts lines 293 to 297Resolvedby macroscopeapp[bot](GitHub profile, opens in a new tab)@@ -0,0 +245,21 @@245+ const extension = /\.[a-z0-9]{1,8}$/i.exec(imageInput.name)?.[0]?.toLowerCase();246+ if (247+ !mimeType?.startsWith("image/") ||248+ !extension ||249+ !SAFE_IMAGE_FILE_EXTENSIONS.has(extension)250+ ) {251+ return null;252+ }253+ const stat = yield* fs.stat(imageInput.path);254+ if (255+ stat.type !== "File" ||256+ stat.size <= 0n ||257+ stat.size > BigInt(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES)258+ ) {259+ return null;260+ }261+ image = {262+ bytes: yield* fs.readFile(imageInput.path),263+ mimeType,264+ name: safeImageName(imageInput.name, mimeType),265+ };apps/web/src/components/chat/AssistantMessageImages.tsx line 36Resolvedby aGamingGod1234(GitHub profile, opens in a new tab)@@ -0,0 +11,21 @@11+12+interface AssistantMessageImagesProps {13+ readonly images: ReadonlyArray<AssistantMessageImage>;14+ readonly onExpand: (imageId: string) => void;15+}16+17+export function isSafeAssistantImagePreviewUrl(value: string): boolean {18+ const trimmed = value.trim();19+ if (/^data:image\/[a-z0-9.+-]+(?:;[a-z0-9.+-]+=[^;,]+)*;base64,/i.test(trimmed)) {20+ return true;21+ }22+ if (trimmed.startsWith("/api/assets/")) {23+ return true;24+ }25+ try {26+ const url = new URL(trimmed);27+ if (url.protocol === "blob:") {28+ return true;29+ }30+ return (31+ (url.protocol === "https:" || url.protocol === "http:") &&
- apps/server/src/orchestration/AssistantImageAttachments.ts line 178Resolvedby cursor[bot](GitHub profile, opens in a new tab)@@ -0,0 +138,21 @@138+139+ const type = stringProperty(value, "type");140+ const suggestedName =141+ stringProperty(value, "output_hint") ??142+ stringProperty(value, "outputHint") ??143+ stringProperty(value, "fileName") ??144+ stringProperty(value, "name");145+146+ if (type === "imageGeneration") {147+ const savedPath = stringProperty(value, "savedPath");148+ if (savedPath) {149+ const name = basename(savedPath).slice(0, 255);150+ if (name.length > 0) {151+ add({ _tag: "local-file", path: savedPath, name });152+ }153+ return;154+ }155+ const result = stringProperty(value, "result");156+ if (result) {157+ add(dataUrlInput(result, suggestedName));158+ }
- apps/web/src/components/chat/AssistantMessageImages.tsx lines 138 to 141Resolvedby aGamingGod1234(GitHub profile, opens in a new tab)@@ -0,0 +120,21 @@120+ Image failed to load121+ </div>122+ ) : null}123+ <Button124+ type="button"125+ size="icon-sm"126+ variant="secondary"127+ className="absolute bottom-2 right-2 z-10 opacity-0 shadow-sm transition-opacity focus-visible:opacity-100 group-focus-within/image:opacity-100 group-hover/image:opacity-100"128+ aria-label={`Download ${image.name}`}129+ onClick={() => void downloadImage(safePreviewUrl, image.name)}130+ >131+ <DownloadIcon aria-hidden="true" />132+ </Button>133+ </figure>134+ );135+});136+137+export const AssistantMessageImages = memo(function AssistantMessageImages({138+ images,139+ onExpand,140+}: AssistantMessageImagesProps) {apps/server/src/orchestration/AssistantImageAttachments.ts lines 189 to 190OutdatedResolvedby aGamingGod1234(GitHub profile, opens in a new tab)@@ -0,0 +170,21 @@170+ if (type === "image") {171+ const data = stringProperty(value, "data");172+ const mimeType = stringProperty(value, "mimeType") ?? stringProperty(value, "mime_type");173+ if (data) {174+ add(dataUrlInput(data, suggestedName) ?? rawBase64Input(data, mimeType, suggestedName));175+ return;176+ }177+178+ const source = value["source"];179+ if (Predicate.isObject(source) && stringProperty(source, "type") === "base64") {180+ const sourceData = stringProperty(source, "data");181+ const sourceMimeType = stringProperty(source, "media_type");182+ if (sourceData) {183+ add(rawBase64Input(sourceData, sourceMimeType, suggestedName));184+ return;185+ }186+ }187+ }188+189+ for (const nested of Object.values(value)) {190+ visit(nested, depth + 1);apps/web/src/components/chat/AssistantMessageImages.tsx lines 72 to 79Resolvedby aGamingGod1234(GitHub profile, opens in a new tab)@@ -0,0 +65,21 @@65+66+ if (previewUrl && !safePreviewUrl) {67+ return (68+ <div69+ className="flex min-h-28 w-full max-w-xl items-center justify-center gap-2 rounded-xl border border-border/70 bg-muted/30 px-4 py-8 text-sm text-muted-foreground"70+ role="alert"71+ >72+ <ImageOffIcon className="size-4" aria-hidden="true" />73+ Image unavailable74+ </div>75+ );76+ }77+78+ if (!safePreviewUrl) {79+ return (80+ <div81+ className="flex min-h-28 w-full max-w-xl items-center justify-center rounded-xl border border-border/70 bg-muted/30 px-4 py-8 text-sm text-muted-foreground"82+ role="status"83+ >84+ Loading image…85+ </div>apps/web/src/components/chat/AssistantMessageImages.tsx lines 125 to 129OutdatedResolvedby aGamingGod1234(GitHub profile, opens in a new tab)@@ -0,0 +109,21 @@109+ role="status"110+ >111+ Loading image…112+ </div>113+ ) : null}114+ {loadState === "error" ? (115+ <div116+ className="flex min-h-28 items-center justify-center gap-2 px-4 py-8 text-sm text-muted-foreground"117+ role="alert"118+ >119+ <ImageOffIcon className="size-4" aria-hidden="true" />120+ Image failed to load121+ </div>122+ ) : null}123+ <Button124+ type="button"125+ size="icon-sm"126+ variant="secondary"127+ className="absolute bottom-2 right-2 z-10 opacity-0 shadow-sm transition-opacity focus-visible:opacity-100 group-focus-within/image:opacity-100 group-hover/image:opacity-100"128+ aria-label={`Download ${image.name}`}129+ onClick={() => void downloadImage(safePreviewUrl, image.name)}apps/server/src/orchestration/AssistantImageAttachments.ts lines 53 to 58OutdatedResolvedby aGamingGod1234(GitHub profile, opens in a new tab)@@ -0,0 +38,21 @@38+const MAX_TRAVERSAL_DEPTH = 8;39+const MAX_BASE64_IMAGE_CHARS = Math.ceil(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / 3) * 4;40+const MAX_IMAGE_DATA_URL_CHARS = MAX_BASE64_IMAGE_CHARS + 1_024;41+42+function stringProperty(record: { [x: PropertyKey]: unknown }, key: string): string | undefined {43+ const value = record[key];44+ return Predicate.isString(value) && value.trim().length > 0 ? value.trim() : undefined;45+}46+47+function basename(value: string): string {48+ return value.replaceAll("\\", "/").split("/").at(-1)?.trim() ?? "";49+}50+51+function safeImageName(value: string | undefined, mimeType: string): string {52+ const candidate = basename(value ?? "").slice(0, 255);53+ const inferredExtension = inferImageExtension({ mimeType, fileName: candidate });54+ const extension = /\.[a-z0-9]{1,8}$/i.exec(candidate)?.[0]?.toLowerCase();55+ if (candidate.length > 0 && extension && SAFE_IMAGE_FILE_EXTENSIONS.has(extension)) {56+ return candidate;57+ }58+ return `generated-image${inferredExtension === ".bin" ? ".png" : inferredExtension}`;apps/server/src/orchestration/AssistantImageAttachments.ts lines 159 to 161Resolvedby aGamingGod1234(GitHub profile, opens in a new tab)@@ -0,0 +133,21 @@133+ }134+ if (!Predicate.isObject(value) || seenObjects.has(value)) {135+ return;136+ }137+ seenObjects.add(value);138+139+ const type = stringProperty(value, "type");140+ const suggestedName =141+ stringProperty(value, "output_hint") ??142+ stringProperty(value, "outputHint") ??143+ stringProperty(value, "fileName") ??144+ stringProperty(value, "name");145+146+ if (type === "imageGeneration") {147+ const savedPath = stringProperty(value, "savedPath");148+ if (savedPath) {149+ const name = basename(savedPath).slice(0, 255);150+ if (name.length > 0) {151+ add({ _tag: "local-file", path: savedPath, name });152+ }153+ return;
- apps/web/src/components/chat/AssistantMessageImages.tsx line 56OutdatedResolvedby aGamingGod1234(GitHub profile, opens in a new tab)@@ -0,0 +36,21 @@36+ (url.protocol === "https:" || url.protocol === "http:") &&37+ url.pathname.startsWith("/api/assets/") &&38+ options?.trustedAsset === true,39+ );40+ } catch {41+ return false;42+ }43+}44+45+async function downloadImage(url: string, name: string): Promise<void> {46+ try {47+ const response = await fetch(url);48+ if (!response.ok) throw new Error(`Image download failed with ${response.status}`);49+ const objectUrl = URL.createObjectURL(await response.blob());50+ const anchor = document.createElement("a");51+ anchor.href = objectUrl;52+ anchor.download = name;53+ anchor.click();54+ URL.revokeObjectURL(objectUrl);55+ } catch {56+ window.open(url, "_blank", "noopener,noreferrer");
t3dotgg(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.


copilot-pull-request-reviewer[bot](GitHub profile, opens in a new tab) reviewed
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.