Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(claude-code): 按图片内容识别媒体类型

DavidShen0c78cddb8a30
diff --git a/apps/desktop/src/main/__tests__/claudeInput.test.ts b/apps/desktop/src/main/__tests__/claudeInput.test.ts
index 786881079c..f169d27a32 100644
--- a/apps/desktop/src/main/__tests__/claudeInput.test.ts
+++ b/apps/desktop/src/main/__tests__/claudeInput.test.ts
@@ -23,7 +23,7 @@ describe('Claude Code SDK input', () => {
it('inlines an original image while keeping file attachments as path refs', async () => {
const tempDir = await createTempDir();
const imagePath = path.join(tempDir, 'small.png');
- const imageBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47]);
+ const imageBytes = Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]);
await fs.writeFile(imagePath, imageBytes);
const imageResizer = { process: vi.fn(async (inputPath: string) => inputPath) };
const content: UserMessage['content'] = [
@@ -50,7 +50,7 @@ describe('Claude Code SDK input', () => {
const tempDir = await createTempDir();
const sourcePath = path.join(tempDir, 'large.png');
const resizedPath = path.join(tempDir, 'large.webp');
- const resizedBytes = Buffer.from([0x52, 0x49, 0x46, 0x46]);
+ const resizedBytes = Buffer.from('RIFF0000WEBP', 'ascii');
await fs.writeFile(resizedPath, resizedBytes);
const imageResizer = { process: vi.fn(async () => resizedPath) };
const content: UserMessage['content'] = [
@@ -72,6 +72,30 @@ describe('Claude Code SDK input', () => {
expect(imageResizer.process).toHaveBeenCalledWith(sourcePath);
});
+ it('uses image bytes instead of a misleading extension or declared MIME type', async () => {
+ const tempDir = await createTempDir();
+ const imagePath = path.join(tempDir, 'misleading.png');
+ const imageBytes = Buffer.from([0xff, 0xd8, 0xff, 0xe0]);
+ await fs.writeFile(imagePath, imageBytes);
+ const imageResizer = { process: vi.fn(async () => imagePath) };
+ const content: UserMessage['content'] = [
+ { type: 'image', path: imagePath, mimeType: 'image/webp' },
+ { type: 'text', text: 'Inspect this' },
+ ];
+
+ expect(await toClaudeSdkContent(content, imageResizer)).toEqual([
+ {
+ type: 'image',
+ source: {
+ type: 'base64',
+ media_type: 'image/jpeg',
+ data: imageBytes.toString('base64'),
+ },
+ },
+ { type: 'text', text: 'Inspect this' },
+ ]);
+ });
+
it('falls back to a quoted path when the final image cannot be read', async () => {
const tempDir = await createTempDir();
const missingPath = path.join(tempDir, 'missing.png');
diff --git a/packages/maker-core/src/agents/claude-code/index.ts b/packages/maker-core/src/agents/claude-code/index.ts
index eda4cec5a7..d03893e01a 100644
--- a/packages/maker-core/src/agents/claude-code/index.ts
+++ b/packages/maker-core/src/agents/claude-code/index.ts
@@ -422,41 +422,31 @@ interface ClaudeInputImageResizer {
const CLAUDE_INLINE_IMAGE_MAX_BYTES = 5 * 1024 * 1024;
-function resolveClaudeImageMediaType(
- imagePath: string,
- mimeType?: string,
-): ClaudeImageMediaType | null {
- const extensionMediaType: Record<string, ClaudeImageMediaType> = {
- '.gif': 'image/gif',
- '.jfif': 'image/jpeg',
- '.jpeg': 'image/jpeg',
- '.jpg': 'image/jpeg',
- '.png': 'image/png',
- '.webp': 'image/webp',
- };
- const fromExtension = extensionMediaType[path.extname(imagePath).toLowerCase()];
- if (fromExtension) return fromExtension;
-
- switch (mimeType?.toLowerCase()) {
- case 'image/gif':
- case 'image/jpeg':
- case 'image/png':
- case 'image/webp':
- return mimeType.toLowerCase() as ClaudeImageMediaType;
- case 'image/jpg':
- return 'image/jpeg';
- default:
- return null;
+function detectClaudeImageMediaType(data: Buffer): ClaudeImageMediaType | null {
+ if (
+ data.length >= 8
+ && data.subarray(0, 8).equals(Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]))
+ ) {
+ return 'image/png';
+ }
+ if (data.length >= 3 && data[0] === 0xff && data[1] === 0xd8 && data[2] === 0xff) {
+ return 'image/jpeg';
}
+ if (data.length >= 6) {
+ const signature = data.toString('ascii', 0, 6);
+ if (signature === 'GIF87a' || signature === 'GIF89a') return 'image/gif';
+ }
+ if (
+ data.length >= 12
+ && data.toString('ascii', 0, 4) === 'RIFF'
+ && data.toString('ascii', 8, 12) === 'WEBP'
+ ) {
+ return 'image/webp';
+ }
+ return null;
}
-async function toClaudeImageBlock(
- imagePath: string,
- mimeType?: string,
-): Promise<ClaudeSdkContentBlock | null> {
- const mediaType = resolveClaudeImageMediaType(imagePath, mimeType);
- if (!mediaType) return null;
-
+async function toClaudeImageBlock(imagePath: string): Promise<ClaudeSdkContentBlock | null> {
let handle: Awaited<ReturnType<typeof fs.open>> | undefined;
try {
handle = await fs.open(imagePath, 'r');
@@ -466,6 +456,8 @@ async function toClaudeImageBlock(
}
const data = await handle.readFile();
if (data.length === 0 || data.length > CLAUDE_INLINE_IMAGE_MAX_BYTES) return null;
+ const mediaType = detectClaudeImageMediaType(data);
+ if (!mediaType) return null;
return {
type: 'image',
source: {
@@ -506,7 +498,7 @@ export async function toClaudeSdkContent(
imageBlockPromises.set(
idx,
imageResizer.process(block.path).then(async (finalPath) => ({
- block: await toClaudeImageBlock(finalPath, block.mimeType),
+ block: await toClaudeImageBlock(finalPath),
finalPath,
})),
);