Skip to content

Commit

Permalink
better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
cogentapps committed Mar 20, 2023
1 parent 746981e commit da0d484
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
13 changes: 6 additions & 7 deletions app/src/chat-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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();
Expand Down
10 changes: 8 additions & 2 deletions app/src/openai.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
});

Expand Down Expand Up @@ -164,4 +168,6 @@ async function selectMessagesToSendSafely(messages: OpenAIMessage[], maxTokens:
preserveSystemPrompt: true,
});
return compressor.process();
}
}

setTimeout(() => selectMessagesToSendSafely([], 2048), 2000);

0 comments on commit da0d484

Please sign in to comment.