fix: 超时时,如果已经有部分回复则返回这部分回复

This commit is contained in:
ikechan8370 2023-03-07 12:14:40 +08:00
parent b332170a1d
commit de5707de2b

View file

@ -181,7 +181,8 @@ export default class SydneyAIClient {
invocationId = 0, invocationId = 0,
parentMessageId = invocationId || crypto.randomUUID(), parentMessageId = invocationId || crypto.randomUUID(),
onProgress, onProgress,
abortController = new AbortController() abortController = new AbortController(),
timeout = Config.defaultTimeoutMs
} = opts } = opts
if (typeof onProgress !== 'function') { if (typeof onProgress !== 'function') {
onProgress = () => {} onProgress = () => {}
@ -314,14 +315,26 @@ export default class SydneyAIClient {
const messageTimeout = setTimeout(() => { const messageTimeout = setTimeout(() => {
this.cleanupWebSocketConnection(ws) this.cleanupWebSocketConnection(ws)
reject(new Error('Timed out waiting for response. Try enabling debug mode to see more information.')) if (replySoFar) {
}, 120 * 1000) resolve({
message
})
} else {
reject(new Error('Timed out waiting for response. Try enabling debug mode to see more information.'))
}
}, timeout)
// abort the request if the abort controller is aborted // abort the request if the abort controller is aborted
abortController.signal.addEventListener('abort', () => { abortController.signal.addEventListener('abort', () => {
clearTimeout(messageTimeout) clearTimeout(messageTimeout)
this.cleanupWebSocketConnection(ws) this.cleanupWebSocketConnection(ws)
reject('Request aborted') if (replySoFar) {
resolve({
message
})
} else {
reject('Request aborted')
}
}) })
ws.on('message', (data) => { ws.on('message', (data) => {