Skip to content

Commit 712eb8e

Browse files
Address review comments: fix system message filtering, improve error handling, and enhance model detection
Co-authored-by: PeterDaveHello <[email protected]>
1 parent 65ec6b2 commit 712eb8e

File tree

2 files changed

+13
-11
lines changed

2 files changed

+13
-11
lines changed

src/services/apis/openai-api.mjs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,21 +130,21 @@ export async function generateAnswersWithChatgptApiCompat(
130130
false,
131131
)
132132

133-
// Always filter out system messages; for reasoning models, only allow user and assistant
134-
const promptWithoutSystem = prompt.filter((msg) => msg.role !== 'system')
133+
// Filter messages based on model type
135134
const filteredPrompt = isReasoningModel
136-
? promptWithoutSystem.filter((msg) => msg.role === 'user' || msg.role === 'assistant')
137-
: promptWithoutSystem
135+
? prompt.filter((msg) => msg.role === 'user' || msg.role === 'assistant')
136+
: prompt
138137

139138
filteredPrompt.push({ role: 'user', content: question })
140139

141140
let answer = ''
142141
let finished = false
143142
const finish = () => {
143+
if (finished) return
144144
finished = true
145145
pushRecord(session, question, answer)
146146
console.debug('conversation history', { content: session.conversationRecords })
147-
port.postMessage({ answer: null, done: true, session: session })
147+
port.postMessage({ answer: null, done: true, session })
148148
}
149149

150150
// Build request body with reasoning model-specific parameters
@@ -202,10 +202,12 @@ export async function generateAnswersWithChatgptApiCompat(
202202
console.debug('No choice in response data for reasoning model')
203203
return
204204
}
205-
const content = choice.message?.content
206-
if (content) {
205+
const content = choice.message?.content ?? choice.text
206+
if (content !== undefined && content !== null) {
207207
answer = content
208-
port.postMessage({ answer: answer, done: false, session: null })
208+
port.postMessage({ answer, done: false, session: null })
209+
}
210+
if (choice.finish_reason || content !== undefined) {
209211
finish()
210212
}
211213
} else {
@@ -225,7 +227,7 @@ export async function generateAnswersWithChatgptApiCompat(
225227
} else if (text) {
226228
answer += text
227229
}
228-
port.postMessage({ answer: answer, done: false, session: null })
230+
port.postMessage({ answer, done: false, session: null })
229231

230232
if (choice.finish_reason) {
231233
finish()

src/utils/model-name-convert.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,8 @@ export function isUsingReasoningModel(configOrSession) {
169169
const modelValue = getModelValue(configOrSession)
170170
if (!modelValue) return false
171171

172-
// Match o[134] pattern with optional dash and suffix (e.g., o1, o1-preview, o3-mini, o4-mini)
173-
if (/^o[134](-|$)/.test(modelValue)) {
172+
// Explicitly match o1, o3, or o4 with optional dash and suffix (e.g., o1, o1-preview, o3-mini, o4-mini)
173+
if (/^(o1|o3|o4)(?:-|$)/.test(modelValue)) {
174174
return true
175175
}
176176

0 commit comments

Comments
 (0)