31
codex-cli/src/utils/input-utils.ts
Normal file
31
codex-cli/src/utils/input-utils.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import type { ResponseInputItem } from "openai/resources/responses/responses";
|
||||
|
||||
import { fileTypeFromBuffer } from "file-type";
|
||||
import fs from "fs/promises";
|
||||
|
||||
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) {
|
||||
/* 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}`,
|
||||
});
|
||||
}
|
||||
|
||||
return inputItem;
|
||||
}
|
||||
Reference in New Issue
Block a user