From da0d4844f183fce65974a611e28526459676e2be Mon Sep 17 00:00:00 2001 From: Cogent Apps Date: Mon, 20 Mar 2023 14:14:03 +0000 Subject: [PATCH] better error messages --- app/src/chat-manager.ts | 13 ++++++------- app/src/openai.ts | 10 ++++++++-- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/app/src/chat-manager.ts b/app/src/chat-manager.ts index 9ab024d..6a8eaa5 100644 --- a/app/src/chat-manager.ts +++ b/app/src/chat-manager.ts @@ -150,13 +150,13 @@ export class ChatManager extends EventEmitter { let lastChunkReceivedAt = Date.now(); - const onError = () => { + const onError = (error?: string) => { if (reply.done) { return; } clearInterval(timer); cancel(); - reply.content += "\n\nI'm sorry, I'm having trouble connecting to OpenAI. Please make sure you've entered your OpenAI API key correctly and try again."; + reply.content += `\n\nI'm sorry, I'm having trouble connecting to OpenAI (${error || 'no response from the API'}). Please make sure you've entered your OpenAI API key correctly and try again.`; reply.content = reply.content.trim(); reply.done = true; this.activeReplies.delete(reply.id); @@ -169,21 +169,20 @@ export class ChatManager extends EventEmitter { let timer = setInterval(() => { const sinceLastChunk = Date.now() - lastChunkReceivedAt; - if (sinceLastChunk > 10000 && !reply.done) { - onError(); + if (sinceLastChunk > 30000 && !reply.done) { + onError('no response from OpenAI in the last 30 seconds'); } }, 2000); - emitter.on('error', () => { + emitter.on('error', (e: any) => { if (!reply.content && !reply.done) { lastChunkReceivedAt = Date.now(); - onError(); + onError(e); } }); emitter.on('data', (data: string) => { if (reply.done) { - cancel(); return; } lastChunkReceivedAt = Date.now(); diff --git a/app/src/openai.ts b/app/src/openai.ts index 0e9eb9c..d3f56ff 100644 --- a/app/src/openai.ts +++ b/app/src/openai.ts @@ -126,7 +126,11 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p eventSource.addEventListener('error', (event: any) => { if (!contents) { - emitter.emit('error'); + let error = event.data; + try { + error = JSON.parse(error).error.message; + } catch (e) {} + emitter.emit('error', error); } }); @@ -164,4 +168,6 @@ async function selectMessagesToSendSafely(messages: OpenAIMessage[], maxTokens: preserveSystemPrompt: true, }); return compressor.process(); -} \ No newline at end of file +} + +setTimeout(() => selectMessagesToSendSafely([], 2048), 2000); \ No newline at end of file