fix(coding-agent): detect image MIME via file-type (#205)

Co-authored-by: Mario Zechner <badlogicgames@gmail.com>
This commit is contained in:
Peter Steinberger 2025-12-17 17:11:56 +01:00 committed by GitHub
parent 46ba48a35d
commit d70edf571e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 191 additions and 50 deletions

View file

@ -2,29 +2,11 @@ import type { AgentTool, ImageContent, TextContent } from "@mariozechner/pi-ai";
import { Type } from "@sinclair/typebox";
import { constants } from "fs";
import { access, readFile } from "fs/promises";
import { extname, resolve as resolvePath } from "path";
import { resolve as resolvePath } from "path";
import { detectSupportedImageMimeTypeFromFile } from "../../utils/mime.js";
import { resolveReadPath } from "./path-utils.js";
import { DEFAULT_MAX_BYTES, DEFAULT_MAX_LINES, formatSize, type TruncationResult, truncateHead } from "./truncate.js";
/**
* Map of file extensions to MIME types for common image formats
*/
const IMAGE_MIME_TYPES: Record<string, string> = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".webp": "image/webp",
};
/**
* Check if a file is an image based on its extension
*/
function isImageFile(filePath: string): string | null {
const ext = extname(filePath).toLowerCase();
return IMAGE_MIME_TYPES[ext] || null;
}
const readSchema = Type.Object({
path: Type.String({ description: "Path to the file to read (relative or absolute)" }),
offset: Type.Optional(Type.Number({ description: "Line number to start reading from (1-indexed)" })),
@ -46,7 +28,6 @@ export const readTool: AgentTool<typeof readSchema> = {
signal?: AbortSignal,
) => {
const absolutePath = resolvePath(resolveReadPath(path));
const mimeType = isImageFile(absolutePath);
return new Promise<{ content: (TextContent | ImageContent)[]; details: ReadToolDetails | undefined }>(
(resolve, reject) => {
@ -79,6 +60,8 @@ export const readTool: AgentTool<typeof readSchema> = {
return;
}
const mimeType = await detectSupportedImageMimeTypeFromFile(absolutePath);
// Read the file based on type
let content: (TextContent | ImageContent)[];
let details: ReadToolDetails | undefined;