feat: update input_file data format to include data URL scheme
This commit is contained in:
@@ -231,7 +231,7 @@ describe('convertOpenAiBodyToResponsesBody', () => {
|
||||
{
|
||||
type: 'input_file',
|
||||
filename: 'notes.md',
|
||||
file_data: Buffer.from('# hello').toString('base64'),
|
||||
file_data: `data:text/markdown;base64,${Buffer.from('# hello').toString('base64')}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -427,7 +427,7 @@ describe('convertOpenAiBodyToResponsesBody', () => {
|
||||
type: 'input_file',
|
||||
file_id: 'file_local_123',
|
||||
filename: 'report.pdf',
|
||||
file_data: 'JVBERi0xLjQK',
|
||||
file_data: 'data:application/pdf;base64,JVBERi0xLjQK',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -15,6 +15,16 @@ function splitBase64DataUrl(value: string): { mimeType: string; data: string } |
|
||||
};
|
||||
}
|
||||
|
||||
export function ensureBase64DataUrl(fileData: string, mimeType?: string | null): string {
|
||||
const trimmedData = asTrimmedString(fileData);
|
||||
if (!trimmedData) return trimmedData;
|
||||
if (splitBase64DataUrl(trimmedData)) return trimmedData;
|
||||
|
||||
const normalizedMimeType = asTrimmedString(mimeType).toLowerCase();
|
||||
if (!normalizedMimeType) return trimmedData;
|
||||
return `data:${normalizedMimeType};base64,${trimmedData}`;
|
||||
}
|
||||
|
||||
export type NormalizedInputFile = {
|
||||
sourceType?: 'file' | 'input_file';
|
||||
fileId?: string;
|
||||
@@ -94,7 +104,12 @@ export function toResponsesInputFileBlock(file: NormalizedInputFile): Record<str
|
||||
const parsedDataUrl = file.fileData ? splitBase64DataUrl(file.fileData) : null;
|
||||
const block: Record<string, unknown> = { type: 'input_file' };
|
||||
if (file.fileId) block.file_id = file.fileId;
|
||||
if (file.fileData) block.file_data = parsedDataUrl?.data || file.fileData;
|
||||
if (file.fileData) {
|
||||
block.file_data = ensureBase64DataUrl(
|
||||
file.fileData,
|
||||
parsedDataUrl?.mimeType || inferInputFileMimeType(file),
|
||||
);
|
||||
}
|
||||
if (file.filename) block.filename = file.filename;
|
||||
return block;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user