better error messages
This commit is contained in:
		@@ -150,13 +150,13 @@ export class ChatManager extends EventEmitter {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        let lastChunkReceivedAt = Date.now();
 | 
					        let lastChunkReceivedAt = Date.now();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const onError = () => {
 | 
					        const onError = (error?: string) => {
 | 
				
			||||||
            if (reply.done) {
 | 
					            if (reply.done) {
 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            clearInterval(timer);
 | 
					            clearInterval(timer);
 | 
				
			||||||
            cancel();
 | 
					            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.content = reply.content.trim();
 | 
				
			||||||
            reply.done = true;
 | 
					            reply.done = true;
 | 
				
			||||||
            this.activeReplies.delete(reply.id);
 | 
					            this.activeReplies.delete(reply.id);
 | 
				
			||||||
@@ -169,21 +169,20 @@ export class ChatManager extends EventEmitter {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        let timer = setInterval(() => {
 | 
					        let timer = setInterval(() => {
 | 
				
			||||||
            const sinceLastChunk = Date.now() - lastChunkReceivedAt;
 | 
					            const sinceLastChunk = Date.now() - lastChunkReceivedAt;
 | 
				
			||||||
            if (sinceLastChunk > 10000 && !reply.done) {
 | 
					            if (sinceLastChunk > 30000 && !reply.done) {
 | 
				
			||||||
                onError();
 | 
					                onError('no response from OpenAI in the last 30 seconds');
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }, 2000);
 | 
					        }, 2000);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        emitter.on('error', () => {
 | 
					        emitter.on('error', (e: any) => {
 | 
				
			||||||
            if (!reply.content && !reply.done) {
 | 
					            if (!reply.content && !reply.done) {
 | 
				
			||||||
                lastChunkReceivedAt = Date.now();
 | 
					                lastChunkReceivedAt = Date.now();
 | 
				
			||||||
                onError();
 | 
					                onError(e);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        });
 | 
					        });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        emitter.on('data', (data: string) => {
 | 
					        emitter.on('data', (data: string) => {
 | 
				
			||||||
            if (reply.done) {
 | 
					            if (reply.done) {
 | 
				
			||||||
                cancel();
 | 
					 | 
				
			||||||
                return;
 | 
					                return;
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
            lastChunkReceivedAt = Date.now();
 | 
					            lastChunkReceivedAt = Date.now();
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -126,7 +126,11 @@ export async function createStreamingChatCompletion(messages: OpenAIMessage[], p
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    eventSource.addEventListener('error', (event: any) => {
 | 
					    eventSource.addEventListener('error', (event: any) => {
 | 
				
			||||||
        if (!contents) {
 | 
					        if (!contents) {
 | 
				
			||||||
            emitter.emit('error');
 | 
					            let error = event.data;
 | 
				
			||||||
 | 
					            try {
 | 
				
			||||||
 | 
					                error = JSON.parse(error).error.message;
 | 
				
			||||||
 | 
					            } catch (e) {}
 | 
				
			||||||
 | 
					            emitter.emit('error', error);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    });
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -165,3 +169,5 @@ async function selectMessagesToSendSafely(messages: OpenAIMessage[], maxTokens:
 | 
				
			|||||||
    });
 | 
					    });
 | 
				
			||||||
    return compressor.process();
 | 
					    return compressor.process();
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					setTimeout(() => selectMessagesToSendSafely([], 2048), 2000);
 | 
				
			||||||
		Reference in New Issue
	
	Block a user