feat: update input_file data format to include data URL scheme
This commit is contained in:
@@ -49,7 +49,7 @@ describe('inlineLocalInputFileReferences', () => {
|
||||
{
|
||||
type: 'input_file',
|
||||
filename: 'report.pdf',
|
||||
file_data: Buffer.from('%PDF-demo').toString('base64'),
|
||||
file_data: `data:application/pdf;base64,${Buffer.from('%PDF-demo').toString('base64')}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -455,7 +455,7 @@ describe('buildUpstreamEndpointRequest', () => {
|
||||
role: 'user',
|
||||
content: [
|
||||
{ type: 'input_text', text: 'summarize this file' },
|
||||
{ type: 'input_file', filename: 'brief.pdf', file_data: 'JVBERi0xLjc=' },
|
||||
{ type: 'input_file', filename: 'brief.pdf', file_data: 'data:application/pdf;base64,JVBERi0xLjc=' },
|
||||
],
|
||||
},
|
||||
]);
|
||||
@@ -501,7 +501,7 @@ describe('buildUpstreamEndpointRequest', () => {
|
||||
type: 'input_file',
|
||||
file_id: 'file_local_123',
|
||||
filename: 'paper.pdf',
|
||||
file_data: 'JVBERi0xLjQK',
|
||||
file_data: 'data:application/pdf;base64,JVBERi0xLjQK',
|
||||
},
|
||||
],
|
||||
},
|
||||
@@ -705,7 +705,7 @@ describe('buildUpstreamEndpointRequest', () => {
|
||||
{
|
||||
type: 'input_file',
|
||||
filename: 'notes.txt',
|
||||
file_data: 'aGVsbG8=',
|
||||
file_data: 'data:text/plain;base64,aGVsbG8=',
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -71,7 +71,7 @@ describe('proxyInputFileResolver', () => {
|
||||
type: 'input_file',
|
||||
file_id: 'file-metapi-123',
|
||||
filename: 'brief.pdf',
|
||||
file_data: Buffer.from('%PDF-local').toString('base64'),
|
||||
file_data: `data:application/pdf;base64,${Buffer.from('%PDF-local').toString('base64')}`,
|
||||
},
|
||||
],
|
||||
},
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import type { ProxyResourceOwner } from '../middleware/auth.js';
|
||||
import { getProxyFileByPublicIdForOwner, LOCAL_PROXY_FILE_ID_PREFIX } from './proxyFileStore.js';
|
||||
import { ensureBase64DataUrl } from '../transformers/shared/inputFile.js';
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === 'object' && !Array.isArray(value);
|
||||
@@ -213,7 +214,7 @@ function toResponsesResolvedBlock(file: { fileId?: string; filename: string; fil
|
||||
type: 'input_file',
|
||||
...(file.fileId ? { file_id: file.fileId } : {}),
|
||||
filename: file.filename,
|
||||
file_data: file.fileData,
|
||||
file_data: ensureBase64DataUrl(file.fileData, file.mimeType),
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -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