Skip to content

Commit f4ac0ae

Browse files
Extend reasoning model support and fix error handling in API responses
- Add support for o3, o3-mini, o4-mini, gpt-5, gpt-5-mini, gpt-5-nano models - Fix error checking in openai-api.mjs response handling to prevent runtime errors - Refactor duplicated data.choices[0] access pattern with proper null safety - Add isUsingReasoningModel function while maintaining backward compatibility Co-authored-by: PeterDaveHello <[email protected]>
1 parent 2be919e commit f4ac0ae

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

src/config/index.mjs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,12 @@ export const chatgptApiModelKeys = [
6767
'chatgptApi4_1_nano',
6868
'chatgptApiO1Preview',
6969
'chatgptApiO1Mini',
70+
'chatgptApiO3Preview',
71+
'chatgptApiO3Mini',
72+
'chatgptApiO4Mini',
73+
'chatgptApiGpt5',
74+
'chatgptApiGpt5Mini',
75+
'chatgptApiGpt5Nano',
7076
]
7177
export const customApiModelKeys = ['customModel']
7278
export const ollamaApiModelKeys = ['ollamaModel']
@@ -260,6 +266,12 @@ export const Models = {
260266

261267
chatgptApiO1Preview: { value: 'o1-preview', desc: 'ChatGPT (o1-preview)' },
262268
chatgptApiO1Mini: { value: 'o1-mini', desc: 'ChatGPT (o1-mini)' },
269+
chatgptApiO3Preview: { value: 'o3-preview', desc: 'ChatGPT (o3-preview)' },
270+
chatgptApiO3Mini: { value: 'o3-mini', desc: 'ChatGPT (o3-mini)' },
271+
chatgptApiO4Mini: { value: 'o4-mini', desc: 'ChatGPT (o4-mini)' },
272+
chatgptApiGpt5: { value: 'gpt-5', desc: 'ChatGPT (gpt-5)' },
273+
chatgptApiGpt5Mini: { value: 'gpt-5-mini', desc: 'ChatGPT (gpt-5-mini)' },
274+
chatgptApiGpt5Nano: { value: 'gpt-5-nano', desc: 'ChatGPT (gpt-5-nano)' },
263275

264276
claude2WebFree: { value: '', desc: 'Claude.ai (Web)' },
265277
claude12Api: { value: 'claude-instant-1.2', desc: 'Claude.ai (API, Claude Instant 1.2)' },
@@ -548,6 +560,12 @@ export const defaultConfig = {
548560
'openRouter_openai_o3',
549561
'chatgptApiO1Preview',
550562
'chatgptApiO1Mini',
563+
'chatgptApiO3Preview',
564+
'chatgptApiO3Mini',
565+
'chatgptApiO4Mini',
566+
'chatgptApiGpt5',
567+
'chatgptApiGpt5Mini',
568+
'chatgptApiGpt5Nano',
551569
],
552570
customApiModes: [
553571
{

src/services/apis/openai-api.mjs

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ export async function generateAnswersWithGptCompletionApi(port, question, sessio
6565
return
6666
}
6767

68-
answer += data.choices[0].text
68+
const choice = data.choices?.[0]
69+
if (!choice) {
70+
console.debug('No choice in response data')
71+
return
72+
}
73+
74+
answer += choice.text
6975
port.postMessage({ answer: answer, done: false, session: null })
7076

71-
if (data.choices[0]?.finish_reason) {
77+
if (choice.finish_reason) {
7278
finish()
7379
return
7480
}
@@ -190,17 +196,27 @@ export async function generateAnswersWithChatgptApiCompat(
190196

191197
if (isO1Model) {
192198
// For o1 models (non-streaming), get the complete response
193-
const content = data.choices[0]?.message?.content
199+
const choice = data.choices?.[0]
200+
if (!choice) {
201+
console.debug('No choice in response data for o1 model')
202+
return
203+
}
204+
const content = choice.message?.content
194205
if (content) {
195206
answer = content
196207
port.postMessage({ answer: answer, done: false, session: null })
197208
finish()
198209
}
199210
} else {
200211
// For non-o1 models (streaming), handle delta content
201-
const delta = data.choices[0]?.delta?.content
202-
const content = data.choices[0]?.message?.content
203-
const text = data.choices[0]?.text
212+
const choice = data.choices?.[0]
213+
if (!choice) {
214+
console.debug('No choice in response data')
215+
return
216+
}
217+
const delta = choice.delta?.content
218+
const content = choice.message?.content
219+
const text = choice.text
204220
if (delta !== undefined) {
205221
answer += delta
206222
} else if (content) {
@@ -210,7 +226,7 @@ export async function generateAnswersWithChatgptApiCompat(
210226
}
211227
port.postMessage({ answer: answer, done: false, session: null })
212228

213-
if (data.choices[0]?.finish_reason) {
229+
if (choice.finish_reason) {
214230
finish()
215231
return
216232
}

src/utils/model-name-convert.mjs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,22 @@ export function isInApiModeGroup(apiModeGroup, configOrSession) {
165165
return groupValue === apiModeGroup
166166
}
167167

168-
export function isUsingO1Model(configOrSession) {
168+
export function isUsingReasoningModel(configOrSession) {
169169
const modelValue = getModelValue(configOrSession)
170-
return modelValue && (modelValue === 'o1-preview' || modelValue === 'o1-mini')
170+
return (
171+
modelValue &&
172+
(modelValue === 'o1-preview' ||
173+
modelValue === 'o1-mini' ||
174+
modelValue === 'o3-preview' ||
175+
modelValue === 'o3-mini' ||
176+
modelValue === 'o4-mini' ||
177+
modelValue === 'gpt-5' ||
178+
modelValue === 'gpt-5-mini' ||
179+
modelValue === 'gpt-5-nano')
180+
)
181+
}
182+
183+
// Keep backward compatibility
184+
export function isUsingO1Model(configOrSession) {
185+
return isUsingReasoningModel(configOrSession)
171186
}

0 commit comments

Comments
 (0)