From 907a399ca86f3c686e32656809e9db62e4a25834 Mon Sep 17 00:00:00 2001 From: Nate Date: Sun, 31 Aug 2025 17:59:12 -0700 Subject: [PATCH] fix image bugs --- .../MemoizedMessage.formatDisplay.test.tsx | 139 ++++++++++++++++++ .../cli/src/ui/components/MemoizedMessage.tsx | 50 +++++-- .../cli/src/ui/hooks/useChat.helpers.ts | 2 +- package.json | 8 + 4 files changed, 185 insertions(+), 14 deletions(-) create mode 100644 extensions/cli/src/ui/components/MemoizedMessage.formatDisplay.test.tsx diff --git a/extensions/cli/src/ui/components/MemoizedMessage.formatDisplay.test.tsx b/extensions/cli/src/ui/components/MemoizedMessage.formatDisplay.test.tsx new file mode 100644 index 000000000..12aeabb7f --- /dev/null +++ b/extensions/cli/src/ui/components/MemoizedMessage.formatDisplay.test.tsx @@ -0,0 +1,139 @@ +import { render } from "ink-testing-library"; +import React from "react"; +import { describe, it, expect } from "vitest"; + +import type { ChatHistoryItem, MessageContent, MessagePart } from "../../../../../core/index.js"; +import { MemoizedMessage } from "./MemoizedMessage.js"; + +describe("MemoizedMessage formatMessageContentForDisplay", () => { + const createTestHistoryItem = (content: MessageContent): ChatHistoryItem => ({ + message: { + role: "user", + content, + }, + contextItems: [], + }); + + it("should display string content as-is", () => { + const content = "Just a simple text message"; + const historyItem = createTestHistoryItem(content); + + const { lastFrame } = render( + + ); + + expect(lastFrame()).toContain("Just a simple text message"); + }); + + it("should display message with images using placeholders", () => { + const messageParts: MessagePart[] = [ + { type: "text", text: "Here is an image: " }, + { type: "imageUrl", imageUrl: { url: "data:image/png;base64,abc123" } }, + { type: "text", text: " and some more text" }, + ]; + const historyItem = createTestHistoryItem(messageParts); + + const { lastFrame } = render( + + ); + + const output = lastFrame(); + expect(output).toContain("Here is an image: [Image #1] and some more text"); + expect(output).not.toContain("data:image/png;base64,abc123"); + expect(output).not.toContain("imageUrl"); + }); + + it("should display message with multiple images using incrementing placeholders", () => { + const messageParts: MessagePart[] = [ + { type: "text", text: "First " }, + { type: "imageUrl", imageUrl: { url: "data:image/png;base64,abc123" } }, + { type: "text", text: " then text " }, + { type: "imageUrl", imageUrl: { url: "data:image/jpeg;base64,def456" } }, + { type: "text", text: " end" }, + ]; + const historyItem = createTestHistoryItem(messageParts); + + const { lastFrame } = render( + + ); + + const output = lastFrame(); + expect(output).toContain("First [Image #1] then text [Image #2] end"); + expect(output).not.toContain("data:image/png;base64"); + expect(output).not.toContain("data:image/jpeg;base64"); + }); + + it("should display message with only images", () => { + const messageParts: MessagePart[] = [ + { type: "imageUrl", imageUrl: { url: "data:image/png;base64,abc123" } }, + { type: "imageUrl", imageUrl: { url: "data:image/png;base64,def456" } }, + ]; + const historyItem = createTestHistoryItem(messageParts); + + const { lastFrame } = render( + + ); + + const output = lastFrame(); + expect(output).toContain("[Image #1][Image #2]"); + }); + + it("should handle empty text parts gracefully", () => { + const messageParts: MessagePart[] = [ + { type: "text", text: "" }, + { type: "imageUrl", imageUrl: { url: "data:image/png;base64,abc123" } }, + { type: "text", text: "" }, + ]; + const historyItem = createTestHistoryItem(messageParts); + + const { lastFrame } = render( + + ); + + const output = lastFrame(); + expect(output).toContain("[Image #1]"); + expect(output).not.toContain("data:image/png;base64"); + }); + + it("should display text-only message parts as regular string", () => { + const messageParts: MessagePart[] = [ + { type: "text", text: "Just text, no images here" }, + ]; + const historyItem = createTestHistoryItem(messageParts); + + const { lastFrame } = render( + + ); + + expect(lastFrame()).toContain("Just text, no images here"); + }); + + it("should handle unknown part types by converting to JSON", () => { + const messageParts = [ + { type: "text", text: "Start " }, + { type: "unknown", data: { some: "data" } }, + { type: "text", text: " end" }, + ] as MessagePart[]; + const historyItem = createTestHistoryItem(messageParts); + + const { lastFrame } = render( + + ); + + const output = lastFrame(); + expect(output).toContain("Start"); + expect(output).toContain("end"); + expect(output).toContain('{"type":"unknown","data":{"some":"data"}}'); + }); + + it("should handle non-array non-string content by converting to JSON", () => { + const content = { someObject: "data" } as any; + const historyItem = createTestHistoryItem(content); + + const { lastFrame } = render( + + ); + + expect(lastFrame()).toContain('{"someObject":"data"}'); + }); +}); \ No newline at end of file diff --git a/extensions/cli/src/ui/components/MemoizedMessage.tsx b/extensions/cli/src/ui/components/MemoizedMessage.tsx index 58a95616d..4c9a1b953 100644 --- a/extensions/cli/src/ui/components/MemoizedMessage.tsx +++ b/extensions/cli/src/ui/components/MemoizedMessage.tsx @@ -7,6 +7,40 @@ import type { ChatHistoryItem } from "../../../../../core/index.js"; import { MarkdownRenderer } from "../MarkdownRenderer.js"; import { ToolResultSummary } from "../ToolResultSummary.js"; +/** + * Formats message content for display, converting message parts array back to + * user-friendly format with placeholders like [Image #1], [Pasted Text #1], etc. + */ +function formatMessageContentForDisplay( + content: import("../../../../../core/index.js").MessageContent +): string { + if (typeof content === "string") { + return content; + } + + if (!Array.isArray(content)) { + return JSON.stringify(content); + } + + // Convert message parts array back to display format with placeholders + let displayText = ""; + let imageCounter = 0; + + for (const part of content) { + if (part.type === "text") { + displayText += part.text; + } else if (part.type === "imageUrl") { + imageCounter++; + displayText += `[Image #${imageCounter}]`; + } else { + // Handle any other part types by converting to JSON + displayText += JSON.stringify(part); + } + } + + return displayText; +} + interface MemoizedMessageProps { item: ChatHistoryItem; index: number; @@ -62,11 +96,7 @@ export const MemoizedMessage = memo( ● )} @@ -132,17 +162,11 @@ export const MemoizedMessage = memo( {isUser ? ( - {typeof message.content === "string" - ? message.content - : JSON.stringify(message.content)} + {formatMessageContentForDisplay(message.content)} ) : ( )} {isStreaming && ▋} diff --git a/extensions/cli/src/ui/hooks/useChat.helpers.ts b/extensions/cli/src/ui/hooks/useChat.helpers.ts index 21a4f7b6c..c356e6b39 100644 --- a/extensions/cli/src/ui/hooks/useChat.helpers.ts +++ b/extensions/cli/src/ui/hooks/useChat.helpers.ts @@ -36,7 +36,7 @@ async function loadSharp(): Promise { /** * Process image buffer to JPEG with resizing using Sharp (if available) */ -async function processImageWithSharp(imageBuffer: Buffer, maxWidth = 512, maxHeight = 512): Promise<{ buffer: Buffer; isJpeg: boolean }> { +async function processImageWithSharp(imageBuffer: Buffer, maxWidth = 1024, maxHeight = 1024): Promise<{ buffer: Buffer; isJpeg: boolean }> { try { // Try to load Sharp - it's an optional dependency const sharp = await loadSharp(); diff --git a/package.json b/package.json index 28cdb97d8..d5d14d6fc 100644 --- a/package.json +++ b/package.json @@ -16,5 +16,13 @@ "prettier": "^3.3.3", "prettier-plugin-tailwindcss": "^0.6.8", "typescript": "^5.6.3" + }, + "optionalDependencies": { + "@img/sharp-darwin-arm64": "^0.33.5", + "@img/sharp-darwin-x64": "^0.33.5", + "@img/sharp-linux-arm": "^0.33.5", + "@img/sharp-linux-arm64": "^0.33.5", + "@img/sharp-linux-x64": "^0.33.5", + "@img/sharp-win32-x64": "^0.33.5" } }