2025-04-16 12:56:08 -04:00
|
|
|
import type { ResponseInputItem } from "openai/resources/responses/responses";
|
|
|
|
|
|
|
|
|
|
import { fileTypeFromBuffer } from "file-type";
|
|
|
|
|
import fs from "fs/promises";
|
2025-04-18 22:55:24 -07:00
|
|
|
import path from "path";
|
2025-04-16 12:56:08 -04:00
|
|
|
|
|
|
|
|
export async function createInputItem(
|
|
|
|
|
text: string,
|
|
|
|
|
images: Array<string>,
|
|
|
|
|
): Promise<ResponseInputItem.Message> {
|
|
|
|
|
const inputItem: ResponseInputItem.Message = {
|
|
|
|
|
role: "user",
|
|
|
|
|
content: [{ type: "input_text", text }],
|
|
|
|
|
type: "message",
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
for (const filePath of images) {
|
2025-04-18 22:55:24 -07:00
|
|
|
try {
|
|
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
|
const binary = await fs.readFile(filePath);
|
|
|
|
|
const kind = await fileTypeFromBuffer(binary);
|
|
|
|
|
/* eslint-enable no-await-in-loop */
|
|
|
|
|
const encoded = binary.toString("base64");
|
|
|
|
|
const mime = kind?.mime ?? "application/octet-stream";
|
|
|
|
|
inputItem.content.push({
|
|
|
|
|
type: "input_image",
|
|
|
|
|
detail: "auto",
|
|
|
|
|
image_url: `data:${mime};base64,${encoded}`,
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
inputItem.content.push({
|
|
|
|
|
type: "input_text",
|
|
|
|
|
text: `[missing image: ${path.basename(filePath)}]`,
|
|
|
|
|
});
|
|
|
|
|
}
|
2025-04-16 12:56:08 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return inputItem;
|
|
|
|
|
}
|