Merge origin/main into codex/modeltester-attachment-review-fixes

This commit is contained in:
cita
2026-03-20 23:09:27 +08:00
46 changed files with 1633 additions and 139 deletions
@@ -12,6 +12,10 @@ function asTrimmedString(value: unknown): string {
return typeof value === 'string' ? value.trim() : '';
}
function isImageMimeType(mimeType: string): boolean {
return mimeType.toLowerCase().startsWith('image/');
}
function parseJsonIfPossible(value: string): unknown {
const trimmed = value.trim();
if (!trimmed) return {};
@@ -50,6 +50,41 @@ describe('geminiGenerateContentTransformer.inbound', () => {
});
});
it('parses non-image inlineData parts into canonical file parts', () => {
const result = geminiGenerateContentTransformer.parseRequest({
model: 'gemini-2.5-pro',
contents: [
{
role: 'user',
parts: [
{ text: 'summarize this pdf' },
{
inlineData: {
mimeType: 'application/pdf',
data: 'JVBERi0xLjQK',
},
},
],
},
],
});
expect(result.error).toBeUndefined();
expect(result.value?.messages).toEqual([
{
role: 'user',
parts: [
{ type: 'text', text: 'summarize this pdf' },
{
type: 'file',
fileData: 'JVBERi0xLjQK',
mimeType: 'application/pdf',
},
],
},
]);
});
it('builds native Gemini requests from canonical envelopes', () => {
const body = geminiGenerateContentTransformer.buildProtocolRequest({
operation: 'generate',
@@ -338,6 +338,90 @@ function parseClaudeMessageContent(content: unknown): string {
return extractTextAndReasoning(content).content;
}
function buildOpenAiImageUrlBlock(url: string): Record<string, unknown> {
return {
type: 'image_url',
image_url: { url },
};
}
function buildOpenAiFileBlock(input: {
fileData?: string;
fileUrl?: string;
filename?: string;
mimeType?: string;
}): Record<string, unknown> | null {
const fileData = typeof input.fileData === 'string' ? input.fileData.trim() : '';
const fileUrl = typeof input.fileUrl === 'string' ? input.fileUrl.trim() : '';
const filename = typeof input.filename === 'string' ? input.filename.trim() : '';
const mimeType = typeof input.mimeType === 'string' ? input.mimeType.trim() : '';
if (!fileData && !fileUrl) return null;
const file: Record<string, unknown> = {};
if (fileData) file.file_data = fileData;
if (fileUrl && !fileData) file.file_url = fileUrl;
if (filename) file.filename = filename;
if (mimeType) file.mime_type = mimeType;
return {
type: 'file',
file,
};
}
function convertClaudeContentBlockToOpenAi(block: Record<string, unknown>): Record<string, unknown> | null {
const blockType = typeof block.type === 'string' ? block.type : '';
if (blockType === 'text') {
const text = parseClaudeMessageContent(block);
return text ? { type: 'text', text } : null;
}
if (blockType === 'image') {
const source = isRecord(block.source) ? block.source : null;
const sourceType = typeof source?.type === 'string' ? source.type : '';
if (sourceType === 'url' && typeof source?.url === 'string' && source.url.trim()) {
return buildOpenAiImageUrlBlock(source.url.trim());
}
if (
sourceType === 'base64'
&& typeof source?.media_type === 'string'
&& source.media_type.trim()
&& typeof source?.data === 'string'
&& source.data.trim()
) {
return buildOpenAiImageUrlBlock(`data:${source.media_type.trim()};base64,${source.data.trim()}`);
}
return null;
}
if (blockType === 'document') {
const source = isRecord(block.source) ? block.source : null;
const sourceType = typeof source?.type === 'string' ? source.type : '';
return buildOpenAiFileBlock({
fileData: sourceType === 'base64' && typeof source?.data === 'string' ? source.data : undefined,
fileUrl: sourceType === 'url' && typeof source?.url === 'string' ? source.url : undefined,
filename: typeof block.title === 'string' ? block.title : undefined,
mimeType: typeof source?.media_type === 'string' ? source.media_type : undefined,
});
}
const text = parseClaudeMessageContent(block);
return text ? { type: 'text', text } : null;
}
function buildOpenAiMessageContent(
contentBlocks: Array<Record<string, unknown>>,
): string | Array<Record<string, unknown>> | undefined {
if (contentBlocks.length <= 0) return undefined;
if (contentBlocks.every((block) => block.type === 'text' && typeof block.text === 'string')) {
return contentBlocks
.map((block) => String(block.text).trim())
.filter(Boolean)
.join('\n\n');
}
return contentBlocks;
}
function parseResponsesOutputText(payload: Record<string, unknown>): string {
const direct = typeof payload.output_text === 'string' ? payload.output_text : '';
if (direct) return direct;
@@ -695,7 +779,6 @@ function convertClaudeRequestToOpenAiBody(body: Record<string, unknown>): {
role: 'assistant',
tool_calls: toolCalls,
};
// Keep textual assistant preface when present.
assistantMessage.content = merged || '';
messages.push(assistantMessage);
} else if (merged !== null) {
@@ -76,6 +76,21 @@ describe('shared input file helpers', () => {
});
});
it('creates anthropic document blocks from remote file urls', () => {
expect(toAnthropicDocumentBlock({
fileUrl: 'https://example.com/remote.pdf',
filename: 'remote.pdf',
mimeType: 'application/pdf',
})).toEqual({
type: 'document',
source: {
type: 'url',
url: 'https://example.com/remote.pdf',
},
title: 'remote.pdf',
});
});
it('infers common mime types from filenames', () => {
expect(inferInputFileMimeType({ filename: 'notes.md', mimeType: null })).toBe('text/markdown');
expect(inferInputFileMimeType({ filename: 'photo.jpeg', mimeType: null })).toBe('image/jpeg');
@@ -159,6 +159,16 @@ export function toOpenAiChatFileBlock(file: NormalizedInputFile): Record<string,
}
export function toAnthropicDocumentBlock(file: NormalizedInputFile): Record<string, unknown> | null {
if (file.fileUrl) {
return {
type: 'document',
source: {
type: 'url',
url: file.fileUrl,
},
...(file.filename ? { title: file.filename } : {}),
};
}
if (!file.fileData) return null;
const parsedDataUrl = splitBase64DataUrl(file.fileData);
const mimeType = parsedDataUrl?.mimeType || inferInputFileMimeType(file);