fix: finish review-driven single-source cleanup

This commit is contained in:
Cita
2026-03-23 11:26:01 +08:00
parent 29c9fcbd29
commit 328b492ab3
21 changed files with 926 additions and 320 deletions
@@ -34,6 +34,47 @@ describe('responses conversion single source of truth', () => {
expect(convertOpenAiBodyToResponsesBodyViaCompatibility).toBe(convertOpenAiBodyToResponsesBody);
expect(sanitizeResponsesBodyForProxyViaCompatibility).toBe(sanitizeResponsesBodyForProxy);
});
it('preserves extra properties when normalizing role-only message items', () => {
expect(normalizeResponsesInputForCompatibility([
{
role: 'assistant',
content: 'done',
id: 'msg_1',
status: 'completed',
},
])).toEqual([
{
type: 'message',
role: 'assistant',
content: [{ type: 'output_text', text: 'done' }],
id: 'msg_1',
status: 'completed',
},
]);
});
it('filters whitespace-only string entries from normalized responses input arrays', () => {
expect(normalizeResponsesInputForCompatibility([
'hello',
' ',
{
role: 'user',
content: 'world',
},
])).toEqual([
{
type: 'message',
role: 'user',
content: [{ type: 'input_text', text: 'hello' }],
},
{
type: 'message',
role: 'user',
content: [{ type: 'input_text', text: 'world' }],
},
]);
});
});
describe('sanitizeResponsesBodyForProxy', () => {
@@ -136,6 +136,7 @@ export function normalizeResponsesMessageItem(item: Record<string, unknown>): Re
if (asTrimmedString(item.role)) {
return {
...item,
type: 'message',
role,
content: normalizedContent,
@@ -158,13 +159,13 @@ export function normalizeResponsesInputForCompatibility(input: unknown): unknown
}
if (Array.isArray(input)) {
return input.map((item) => {
return input.flatMap((item) => {
if (typeof item === 'string') {
const normalized = item.trim();
return normalized ? toResponsesInputMessageFromText(normalized) : item;
return normalized ? [toResponsesInputMessageFromText(normalized)] : [];
}
if (!isRecord(item)) return item;
return normalizeResponsesMessageItem(item);
if (!isRecord(item)) return [item];
return [normalizeResponsesMessageItem(item)];
});
}