[codex] strip codex responses max_output_tokens (#399)

* strip codex responses max_output_tokens

* address codex responses review comments
This commit is contained in:
Cita
2026-04-02 17:28:38 +08:00
committed by GitHub
parent 19bfed2e33
commit 6ad9ec6911
6 changed files with 165 additions and 68 deletions
@@ -0,0 +1,45 @@
import { describe, expect, it } from 'vitest';
import { normalizeCodexResponsesBodyForProxy } from './codexCompatibility.js';
describe('normalizeCodexResponsesBodyForProxy', () => {
it('normalizes codex responses bodies before proxying upstream', () => {
const body = normalizeCodexResponsesBodyForProxy({
input: [
{
type: 'message',
role: 'system',
content: [{ type: 'input_text', text: 'be precise' }],
},
],
max_output_tokens: 512,
max_completion_tokens: 256,
max_tokens: 128,
temperature: 0.3,
store: true,
}, 'codex');
expect(body).toEqual({
input: [
{
type: 'message',
role: 'developer',
content: [{ type: 'input_text', text: 'be precise' }],
},
],
instructions: '',
store: false,
temperature: 0.3,
});
});
it('leaves non-codex bodies untouched', () => {
const source = {
input: 'hello',
max_output_tokens: 512,
};
const body = normalizeCodexResponsesBodyForProxy(source, 'openai');
expect(body).toBe(source);
});
});
@@ -0,0 +1,90 @@
function isRecord(value: unknown): value is Record<string, unknown> {
return !!value && typeof value === 'object';
}
function asTrimmedString(value: unknown): string {
return typeof value === 'string' ? value.trim() : '';
}
function ensureCodexResponsesInstructions(
body: Record<string, unknown>,
sitePlatform: string,
): Record<string, unknown> {
if (sitePlatform !== 'codex') return body;
if (typeof body.instructions === 'string') return body;
return {
...body,
instructions: '',
};
}
function ensureCodexResponsesStoreFalse(
body: Record<string, unknown>,
sitePlatform: string,
): Record<string, unknown> {
if (sitePlatform !== 'codex') return body;
return {
...body,
store: false,
};
}
function stripCodexUnsupportedResponsesFields(
body: Record<string, unknown>,
sitePlatform: string,
): Record<string, unknown> {
if (sitePlatform !== 'codex') return body;
const next = { ...body };
delete next.max_output_tokens;
delete next.max_completion_tokens;
delete next.max_tokens;
return next;
}
function convertCodexSystemRoleToDeveloper(input: unknown): unknown {
if (!Array.isArray(input)) return input;
return input.map((item) => {
if (!isRecord(item)) return item;
if (asTrimmedString(item.type).toLowerCase() !== 'message') return item;
if (asTrimmedString(item.role).toLowerCase() !== 'system') return item;
return {
...item,
role: 'developer',
};
});
}
function applyCodexResponsesCompatibility(
body: Record<string, unknown>,
sitePlatform: string,
): Record<string, unknown> {
if (sitePlatform !== 'codex') return body;
const next: Record<string, unknown> = {
...body,
input: convertCodexSystemRoleToDeveloper(body.input),
};
if (typeof next.instructions !== 'string') {
next.instructions = '';
}
return next;
}
export function normalizeCodexResponsesBodyForProxy(
body: Record<string, unknown>,
sitePlatform: string,
): Record<string, unknown> {
if (sitePlatform !== 'codex') return body;
return ensureCodexResponsesStoreFalse(
stripCodexUnsupportedResponsesFields(
ensureCodexResponsesInstructions(
applyCodexResponsesCompatibility(body, sitePlatform),
sitePlatform,
),
sitePlatform,
),
sitePlatform,
);
}