Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/__tests__/unit/core/openai-codec.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { describe, expect, it } from 'vitest';
import { OpenAiCodec } from '@/core/llm/adapters/openai/openai-codec.js';
import type { ChatMessage } from '@/core/llm/types.js';

const messages: ChatMessage[] = [
{ role: 'system', content: 'You are helpful.' },
{ role: 'user', content: 'Hello' },
];

describe('OpenAiCodec.buildResponsesRequest reasoning parameter', () => {
it('omits reasoning for non-reasoning API-key models like gpt-4.1', () => {
const request = OpenAiCodec.buildResponsesRequest(messages, {
model: 'gpt-4.1',
tools: [],
oauthMode: false,
});

// gpt-4.1 rejects `reasoning.summary` with a 400; it must not be sent.
expect(request.reasoning).toBeUndefined();
expect(request.model).toBe('gpt-4.1');
});

it('sends reasoning with the resolved default effort for reasoning API-key models', () => {
const request = OpenAiCodec.buildResponsesRequest(messages, {
model: 'gpt-5.4',
tools: [],
oauthMode: false,
});

expect(request.reasoning).toEqual({ summary: 'detailed', effort: 'medium' });
});

it('keeps summary auto in OAuth mode', () => {
const request = OpenAiCodec.buildResponsesRequest(messages, {
model: 'gpt-5.4',
tools: [],
oauthMode: true,
});

expect(request.reasoning).toEqual(expect.objectContaining({ summary: 'auto' }));
});

it('omits reasoning for API-key models without a reasoning policy entry', () => {
// Codex-family models are OAuth-first and have no API-key reasoning policy;
// sending nothing lets the server apply its own default.
const request = OpenAiCodec.buildResponsesRequest(messages, {
model: 'gpt-5.2-codex',
tools: [],
oauthMode: false,
});

expect(request.reasoning).toBeUndefined();
});

it('still sends reasoning in OAuth mode for codex models', () => {
const request = OpenAiCodec.buildResponsesRequest(messages, {
model: 'gpt-5.2-codex',
tools: [],
oauthMode: true,
});

expect(request.reasoning).toEqual({ summary: 'auto' });
});
});
20 changes: 15 additions & 5 deletions src/core/llm/adapters/openai/openai-codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ export class OpenAiCodec {
input: ResponseInputItem[];
tools?: FunctionTool[];
store: boolean;
reasoning: { summary: 'auto' | 'detailed'; effort?: OpenAiReasoningEffort };
reasoning?: { summary: 'auto' | 'detailed'; effort?: OpenAiReasoningEffort };
instructions?: string;
} {
const systemMessages = options.oauthMode ? messages.filter((message): message is Extract<ChatMessage, { role: 'system' }> => message.role === 'system') : [];
Expand All @@ -141,16 +141,26 @@ export class OpenAiCodec {
model: options.model,
explicitEffort: options.reasoningEffort,
});
// Non-reasoning API-key models (e.g. gpt-4.1) reject `reasoning.summary`
// with a 400, so only send the reasoning block when the model supports it.
// OAuth mode keeps its current behavior: the account sign-in allowlist is
// reasoning models only, and that path expects `summary: 'auto'`.
const includeReasoning =
options.oauthMode ||
Boolean(reasoningEffort) ||
ModelPolicyService.supportsReasoningEffort(options.model);
Comment thread
roackb2 marked this conversation as resolved.
Outdated

return {
model: options.model,
input: OpenAiCodec.toResponseInput(inputMessages),
tools: options.tools.length > 0 ? options.tools.map((tool) => OpenAiCodec.toResponseTool(tool)) : undefined,
store: false,
reasoning: {
summary: options.oauthMode ? 'auto' : 'detailed',
...(reasoningEffort ? { effort: reasoningEffort } : {}),
},
...(includeReasoning ? {
reasoning: {
summary: options.oauthMode ? 'auto' as const : 'detailed' as const,
...(reasoningEffort ? { effort: reasoningEffort } : {}),
},
} : {}),
...(instructions ? { instructions } : {}),
};
}
Expand Down
Loading