fix image bugs
This commit is contained in:
parent
d999b447b3
commit
907a399ca8
4 changed files with 185 additions and 14 deletions
|
|
@ -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(
|
||||
<MemoizedMessage item={historyItem} index={1} />
|
||||
);
|
||||
|
||||
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(
|
||||
<MemoizedMessage item={historyItem} index={1} />
|
||||
);
|
||||
|
||||
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(
|
||||
<MemoizedMessage item={historyItem} index={1} />
|
||||
);
|
||||
|
||||
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(
|
||||
<MemoizedMessage item={historyItem} index={1} />
|
||||
);
|
||||
|
||||
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(
|
||||
<MemoizedMessage item={historyItem} index={1} />
|
||||
);
|
||||
|
||||
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(
|
||||
<MemoizedMessage item={historyItem} index={1} />
|
||||
);
|
||||
|
||||
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(
|
||||
<MemoizedMessage item={historyItem} index={1} />
|
||||
);
|
||||
|
||||
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(
|
||||
<MemoizedMessage item={historyItem} index={1} />
|
||||
);
|
||||
|
||||
expect(lastFrame()).toContain('{"someObject":"data"}');
|
||||
});
|
||||
});
|
||||
|
|
@ -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<MemoizedMessageProps>(
|
|||
<Text color="white">●</Text>
|
||||
<Text> </Text>
|
||||
<MarkdownRenderer
|
||||
content={
|
||||
typeof message.content === "string"
|
||||
? message.content
|
||||
: JSON.stringify(message.content)
|
||||
}
|
||||
content={formatMessageContentForDisplay(message.content)}
|
||||
/>
|
||||
</Box>
|
||||
)}
|
||||
|
|
@ -132,17 +162,11 @@ export const MemoizedMessage = memo<MemoizedMessageProps>(
|
|||
<Text> </Text>
|
||||
{isUser ? (
|
||||
<Text color="gray">
|
||||
{typeof message.content === "string"
|
||||
? message.content
|
||||
: JSON.stringify(message.content)}
|
||||
{formatMessageContentForDisplay(message.content)}
|
||||
</Text>
|
||||
) : (
|
||||
<MarkdownRenderer
|
||||
content={
|
||||
typeof message.content === "string"
|
||||
? message.content
|
||||
: JSON.stringify(message.content)
|
||||
}
|
||||
content={formatMessageContentForDisplay(message.content)}
|
||||
/>
|
||||
)}
|
||||
{isStreaming && <Text color="gray">▋</Text>}
|
||||
|
|
|
|||
|
|
@ -36,7 +36,7 @@ async function loadSharp(): Promise<any> {
|
|||
/**
|
||||
* 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();
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue