返回 oh-my-ppt
openai-model-options.test.ts
根目录 / tests / unit / openai-model-options.test.ts
1 import { describe, expect, it } from 'vitest'
2 import {
3 buildOpenAIModelOptions,
4 normalizeOpenAIBaseUrl,
5 resolveOpenAIThinkingModelKwargs
6 } from '../../src/main/agent-runtime/model'
7
8 describe('buildOpenAIModelOptions', () => {
9 it.each(['', 'https://api.openai.com', 'https://api.openai.com/v1', 'https://API.OPENAI.COM/v1/'])(
10 'does not inject compatibility thinking parameters for official OpenAI: %s',
11 (baseUrl) => {
12 const options = buildOpenAIModelOptions({
13 model: 'test-model',
14 apiKey: 'secret',
15 baseUrl,
16 temperatureOptions: { temperature: 0.7 },
17 maxTokens: 4096
18 })
19
20 expect(options).toEqual({
21 model: 'test-model',
22 apiKey: 'secret',
23 temperature: 0.7,
24 maxTokens: 4096,
25 configuration: baseUrl ? { baseURL: baseUrl.replace(/\/+$/, '') } : undefined,
26 modelKwargs: {}
27 })
28 }
29 )
30
31 it('keeps thinking disabled for custom OpenAI-compatible endpoints', () => {
32 expect(
33 buildOpenAIModelOptions({
34 model: 'test-model',
35 apiKey: 'secret',
36 baseUrl: 'https://api.example-compatible.com/v1',
37 temperatureOptions: {},
38 maxTokens: 2048
39 })
40 ).toEqual({
41 model: 'test-model',
42 apiKey: 'secret',
43 maxTokens: 2048,
44 configuration: { baseURL: 'https://api.example-compatible.com/v1' },
45 modelKwargs: { thinking: { type: 'disabled' } }
46 })
47 })
48
49 it('omits thinking parameters for custom endpoints when configured', () => {
50 expect(
51 buildOpenAIModelOptions({
52 model: 'test-model',
53 apiKey: 'secret',
54 baseUrl: 'https://api.example-compatible.com/v1',
55 temperatureOptions: {},
56 maxTokens: 2048,
57 thinkingParameterMode: 'omit'
58 })
59 ).toEqual({
60 model: 'test-model',
61 apiKey: 'secret',
62 maxTokens: 2048,
63 configuration: { baseURL: 'https://api.example-compatible.com/v1' },
64 modelKwargs: {}
65 })
66 })
67
68 it('does not inject Chat Completions compatibility kwargs for Responses API models', () => {
69 expect(
70 buildOpenAIModelOptions({
71 model: 'gpt-5.1',
72 apiKey: 'secret',
73 baseUrl: 'https://api.example-compatible.com/v1',
74 temperatureOptions: {},
75 maxTokens: 2048,
76 useResponsesApi: true
77 })
78 ).toEqual({
79 model: 'gpt-5.1',
80 apiKey: 'secret',
81 maxTokens: 2048,
82 configuration: { baseURL: 'https://api.example-compatible.com/v1' },
83 modelKwargs: {}
84 })
85 })
86
87 it('normalizes accidental /responses suffix only for Responses API models', () => {
88 expect(normalizeOpenAIBaseUrl('https://api.example.com/v1/responses', true)).toBe(
89 'https://api.example.com/v1'
90 )
91 expect(normalizeOpenAIBaseUrl('https://api.example.com/v1/responses/', true)).toBe(
92 'https://api.example.com/v1'
93 )
94 expect(normalizeOpenAIBaseUrl('https://api.example.com/v1/responses', false)).toBe(
95 'https://api.example.com/v1/responses'
96 )
97 })
98
99 it('centralizes thinking model kwargs resolution', () => {
100 expect(
101 resolveOpenAIThinkingModelKwargs({
102 baseUrl: 'https://api.example-compatible.com/v1',
103 thinkingParameterMode: 'auto'
104 })
105 ).toEqual({ thinking: { type: 'disabled' } })
106 expect(
107 resolveOpenAIThinkingModelKwargs({
108 baseUrl: 'https://api.example-compatible.com/v1',
109 thinkingParameterMode: 'omit'
110 })
111 ).toEqual({})
112 })
113 })
114
114 lines TYPESCRIPT