Skip to content

Commit

Permalink
Merge pull request #372 from fjc0k/main
Browse files Browse the repository at this point in the history
  • Loading branch information
transitive-bullshit authored Feb 21, 2023
2 parents 96c2afe + f0ce925 commit 8943cd3
Showing 1 changed file with 35 additions and 31 deletions.
66 changes: 35 additions & 31 deletions src/chatgpt-unofficial-proxy-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,47 +155,51 @@ export class ChatGPTUnofficialProxyAPI {
console.log('POST', url, { body, headers })
}

fetchSSE(url, {
method: 'POST',
headers,
body: JSON.stringify(body),
signal: abortSignal,
onMessage: (data: string) => {
if (data === '[DONE]') {
return resolve(result)
}

try {
const convoResponseEvent: types.ConversationResponseEvent =
JSON.parse(data)
if (convoResponseEvent.conversation_id) {
result.conversationId = convoResponseEvent.conversation_id
fetchSSE(
url,
{
method: 'POST',
headers,
body: JSON.stringify(body),
signal: abortSignal,
onMessage: (data: string) => {
if (data === '[DONE]') {
return resolve(result)
}

if (convoResponseEvent.message?.id) {
result.id = convoResponseEvent.message.id
}
try {
const convoResponseEvent: types.ConversationResponseEvent =
JSON.parse(data)
if (convoResponseEvent.conversation_id) {
result.conversationId = convoResponseEvent.conversation_id
}

if (convoResponseEvent.message?.id) {
result.id = convoResponseEvent.message.id
}

const message = convoResponseEvent.message
// console.log('event', JSON.stringify(convoResponseEvent, null, 2))
const message = convoResponseEvent.message
// console.log('event', JSON.stringify(convoResponseEvent, null, 2))

if (message) {
let text = message?.content?.parts?.[0]
if (message) {
let text = message?.content?.parts?.[0]

if (text) {
result.text = text
if (text) {
result.text = text

if (onProgress) {
onProgress(result)
if (onProgress) {
onProgress(result)
}
}
}
} catch (err) {
// ignore for now; there seem to be some non-json messages
// console.warn('fetchSSE onMessage unexpected error', err)
}
} catch (err) {
// ignore for now; there seem to be some non-json messages
// console.warn('fetchSSE onMessage unexpected error', err)
}
}
}).catch((err) => {
},
this._fetch
).catch((err) => {
const errMessageL = err.toString().toLowerCase()

if (
Expand Down

0 comments on commit 8943cd3

Please sign in to comment.