support different image filetypes

This commit is contained in:
starmorph
2025-05-19 12:48:30 -07:00
parent 5e7a8ee03e
commit 217fd43eac

View File

@@ -5,11 +5,22 @@ import { convertToOpenAIImageBlock } from "@langchain/core/messages";
export async function fileToImageBlock( export async function fileToImageBlock(
file: File, file: File,
): Promise<Base64ContentBlock> { ): Promise<Base64ContentBlock> {
const supportedTypes = [
"image/jpeg",
"image/png",
"image/gif",
"image/webp",
];
if (!supportedTypes.includes(file.type)) {
throw new Error(
`Unsupported image type: ${file.type}. Supported types are: ${supportedTypes.join(", ")}`,
);
}
const data = await fileToBase64(file); const data = await fileToBase64(file);
return { return {
type: "image", type: "image",
source_type: "base64", source_type: "base64",
mime_type: "image/jpeg", mime_type: file.type,
data, data,
metadata: { name: file.name }, metadata: { name: file.name },
}; };