|
| 1 | +import { pushRecord, setAbortController } from './shared.mjs' |
| 2 | +import { getUserConfig } from '../../config/index.mjs' |
| 3 | +import { fetchSSE } from '../../utils/fetch-sse' |
| 4 | +import { isEmpty } from 'lodash-es' |
| 5 | + |
| 6 | +/** |
| 7 | + * @param {Runtime.Port} port |
| 8 | + * @param {string} question |
| 9 | + * @param {Session} session |
| 10 | + */ |
| 11 | +export async function generateAnswersWithWaylaidwandererApi(port, question, session) { |
| 12 | + const { controller, messageListener } = setAbortController(port) |
| 13 | + |
| 14 | + const config = await getUserConfig() |
| 15 | + |
| 16 | + let answer = '' |
| 17 | + await fetchSSE(config.githubThirdPartyUrl, { |
| 18 | + method: 'POST', |
| 19 | + signal: controller.signal, |
| 20 | + headers: { |
| 21 | + 'Content-Type': 'application/json', |
| 22 | + }, |
| 23 | + body: JSON.stringify({ |
| 24 | + message: question, |
| 25 | + stream: true, |
| 26 | + ...(session.bingWeb_conversationId && { |
| 27 | + conversationId: session.bingWeb_conversationId, |
| 28 | + conversationSignature: session.bingWeb_conversationSignature, |
| 29 | + clientId: session.bingWeb_clientId, |
| 30 | + invocationId: session.bingWeb_invocationId, |
| 31 | + }), |
| 32 | + ...(session.conversationId && { |
| 33 | + conversationId: session.conversationId, |
| 34 | + parentMessageId: session.parentMessageId, |
| 35 | + }), |
| 36 | + }), |
| 37 | + onMessage(message) { |
| 38 | + console.debug('sse message', message) |
| 39 | + if (message === '[DONE]') { |
| 40 | + pushRecord(session, question, answer) |
| 41 | + console.debug('conversation history', { content: session.conversationRecords }) |
| 42 | + port.postMessage({ answer: null, done: true, session: session }) |
| 43 | + return |
| 44 | + } |
| 45 | + let data |
| 46 | + try { |
| 47 | + data = JSON.parse(message) |
| 48 | + } catch (error) { |
| 49 | + console.debug('json error', error) |
| 50 | + return |
| 51 | + } |
| 52 | + if (data.conversationId) session.conversationId = data.conversationId |
| 53 | + if (data.parentMessageId) session.parentMessageId = data.parentMessageId |
| 54 | + if (data.conversationSignature) |
| 55 | + session.bingWeb_conversationSignature = data.conversationSignature |
| 56 | + if (data.conversationId) session.bingWeb_conversationId = data.conversationId |
| 57 | + if (data.clientId) session.bingWeb_clientId = data.clientId |
| 58 | + if (data.invocationId) session.bingWeb_invocationId = data.invocationId |
| 59 | + |
| 60 | + if (typeof data === 'string') { |
| 61 | + answer += data |
| 62 | + port.postMessage({ answer: answer, done: false, session: null }) |
| 63 | + } |
| 64 | + }, |
| 65 | + async onStart() {}, |
| 66 | + async onEnd() { |
| 67 | + port.onMessage.removeListener(messageListener) |
| 68 | + }, |
| 69 | + async onError(resp) { |
| 70 | + port.onMessage.removeListener(messageListener) |
| 71 | + if (resp instanceof Error) throw resp |
| 72 | + const error = await resp.json().catch(() => ({})) |
| 73 | + throw new Error(!isEmpty(error) ? JSON.stringify(error) : `${resp.status} ${resp.statusText}`) |
| 74 | + }, |
| 75 | + }) |
| 76 | +} |
0 commit comments