Skip to content

Commit 97fc651

Browse files
committed
improve fetch onmessage
1 parent e29f8a1 commit 97fc651

File tree

2 files changed

+42
-36
lines changed

2 files changed

+42
-36
lines changed

src/services/apis/custom-api.mjs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
3232

3333
let answer = ''
3434
let finished = false
35+
const finish = () => {
36+
finished = true
37+
pushRecord(session, question, answer)
38+
console.debug('conversation history', { content: session.conversationRecords })
39+
port.postMessage({ answer: null, done: true, session: session })
40+
}
3541
await fetchSSE(apiUrl, {
3642
method: 'POST',
3743
signal: controller.signal,
@@ -48,11 +54,9 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
4854
}),
4955
onMessage(message) {
5056
console.debug('sse message', message)
51-
if (!finished && message.trim() === '[DONE]') {
52-
finished = true
53-
pushRecord(session, question, answer)
54-
console.debug('conversation history', { content: session.conversationRecords })
55-
port.postMessage({ answer: null, done: true, session: session })
57+
if (finished) return
58+
if (message.trim() === '[DONE]') {
59+
finish()
5660
return
5761
}
5862
let data
@@ -62,13 +66,6 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
6266
console.debug('json error', error)
6367
return
6468
}
65-
if (!finished && data.choices[0]?.finish_reason) {
66-
finished = true
67-
pushRecord(session, question, answer)
68-
console.debug('conversation history', { content: session.conversationRecords })
69-
port.postMessage({ answer: null, done: true, session: session })
70-
return
71-
}
7269

7370
if (data.response) answer = data.response
7471
else {
@@ -84,6 +81,11 @@ export async function generateAnswersWithCustomApi(port, question, session, apiK
8481
}
8582
}
8683
port.postMessage({ answer: answer, done: false, session: null })
84+
85+
if (data.choices[0]?.finish_reason) {
86+
finish()
87+
return
88+
}
8789
},
8890
async onStart() {},
8991
async onEnd() {

src/services/apis/openai-api.mjs

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,12 @@ export async function generateAnswersWithGptCompletionApi(
3939

4040
let answer = ''
4141
let finished = false
42+
const finish = () => {
43+
finished = true
44+
pushRecord(session, question, answer)
45+
console.debug('conversation history', { content: session.conversationRecords })
46+
port.postMessage({ answer: null, done: true, session: session })
47+
}
4248
await fetchSSE(`${apiUrl}/v1/completions`, {
4349
method: 'POST',
4450
signal: controller.signal,
@@ -56,11 +62,9 @@ export async function generateAnswersWithGptCompletionApi(
5662
}),
5763
onMessage(message) {
5864
console.debug('sse message', message)
59-
if (!finished && message.trim() === '[DONE]') {
60-
finished = true
61-
pushRecord(session, question, answer)
62-
console.debug('conversation history', { content: session.conversationRecords })
63-
port.postMessage({ answer: null, done: true, session: session })
65+
if (finished) return
66+
if (message.trim() === '[DONE]') {
67+
finish()
6468
return
6569
}
6670
let data
@@ -70,16 +74,14 @@ export async function generateAnswersWithGptCompletionApi(
7074
console.debug('json error', error)
7175
return
7276
}
73-
if (!finished && data.choices[0]?.finish_reason) {
74-
finished = true
75-
pushRecord(session, question, answer)
76-
console.debug('conversation history', { content: session.conversationRecords })
77-
port.postMessage({ answer: null, done: true, session: session })
78-
return
79-
}
8077

8178
answer += data.choices[0].text
8279
port.postMessage({ answer: answer, done: false, session: null })
80+
81+
if (data.choices[0]?.finish_reason) {
82+
finish()
83+
return
84+
}
8385
},
8486
async onStart() {},
8587
async onEnd() {
@@ -136,6 +138,12 @@ export async function generateAnswersWithChatgptApiCompat(
136138

137139
let answer = ''
138140
let finished = false
141+
const finish = () => {
142+
finished = true
143+
pushRecord(session, question, answer)
144+
console.debug('conversation history', { content: session.conversationRecords })
145+
port.postMessage({ answer: null, done: true, session: session })
146+
}
139147
await fetchSSE(`${baseUrl}/v1/chat/completions`, {
140148
method: 'POST',
141149
signal: controller.signal,
@@ -152,11 +160,9 @@ export async function generateAnswersWithChatgptApiCompat(
152160
}),
153161
onMessage(message) {
154162
console.debug('sse message', message)
155-
if (!finished && message.trim() === '[DONE]') {
156-
finished = true
157-
pushRecord(session, question, answer)
158-
console.debug('conversation history', { content: session.conversationRecords })
159-
port.postMessage({ answer: null, done: true, session: session })
163+
if (finished) return
164+
if (message.trim() === '[DONE]') {
165+
finish()
160166
return
161167
}
162168
let data
@@ -166,13 +172,6 @@ export async function generateAnswersWithChatgptApiCompat(
166172
console.debug('json error', error)
167173
return
168174
}
169-
if (!finished && data.choices[0]?.finish_reason) {
170-
finished = true
171-
pushRecord(session, question, answer)
172-
console.debug('conversation history', { content: session.conversationRecords })
173-
port.postMessage({ answer: null, done: true, session: session })
174-
return
175-
}
176175

177176
const delta = data.choices[0]?.delta?.content
178177
const content = data.choices[0]?.message?.content
@@ -185,6 +184,11 @@ export async function generateAnswersWithChatgptApiCompat(
185184
answer += text
186185
}
187186
port.postMessage({ answer: answer, done: false, session: null })
187+
188+
if (data.choices[0]?.finish_reason) {
189+
finish()
190+
return
191+
}
188192
},
189193
async onStart() {},
190194
async onEnd() {

0 commit comments

Comments
 (0)