fix error handling

This commit is contained in:
Andrew Pareles 2024-11-26 01:55:42 -08:00
parent c7e9175fd7
commit f6f6259f23
7 changed files with 28 additions and 18 deletions

View file

@ -19,7 +19,8 @@ export const ISendLLMMessageService = createDecorator<ISendLLMMessageService>('s
// defines an interface that node/ creates and browser/ uses
export interface ISendLLMMessageService {
readonly _serviceBrand: undefined;
sendLLMMessage: (params: LLMMessageServiceParams) => void;
sendLLMMessage: (params: LLMMessageServiceParams) => string;
abort: (requestId: string) => void;
}
@ -74,6 +75,7 @@ export class SendLLMMessageService implements ISendLLMMessageService {
this._addDisposable(requestId_,
onErrorEvent(e => {
if (requestId_ !== e.requestId) return;
console.log('event onError', JSON.stringify(e))
onError(e)
this._dispose(requestId_)
})

View file

@ -62,10 +62,11 @@ export class LLMMessageChannel implements IServerChannel {
}
}
catch (e) {
console.log('sendLLM channel: call error', e)
console.log('llmMessageChannel: Call Error:', e)
}
}
// the only place sendLLMMessage is actually called
private _callSendLLMMessage(params: ProxyLLMMessageParams) {
const { requestId } = params;
@ -82,7 +83,6 @@ export class LLMMessageChannel implements IServerChannel {
sendLLMMessage(mainThreadParams);
}
private _callAbort(params: ProxyLLMMessageAbortParams) {
const { requestId } = params;
if (!(requestId in this._abortRefOfRequestId)) return

View file

@ -175,7 +175,7 @@ export const SidebarChat = () => {
// state of chat
const [messageStream, setMessageStream] = useState('')
const [isLoading, setIsLoading] = useState(false)
const abortFnRef = useRef<(() => void) | null>(null)
const latestRequestIdRef = useRef<string | null>(null)
const [latestError, setLatestError] = useState<Error | string | null>(null)
@ -225,7 +225,7 @@ export const SidebarChat = () => {
setIsLoading(false)
},
onError: ({ error }) => {
console.log('chat: running error')
console.log('chat: running error', error)
// add assistant's message to chat history, and clear selection
let content = messageStream; // just use the current content
@ -238,10 +238,10 @@ export const SidebarChat = () => {
setLatestError(error)
},
voidConfig,
abortRef: abortFnRef,
}
sendLLMMessageService.sendLLMMessage(object)
const latestRequestId = sendLLMMessageService.sendLLMMessage(object)
latestRequestIdRef.current = latestRequestId
setIsLoading(true)
@ -253,8 +253,9 @@ export const SidebarChat = () => {
}
const onAbort = () => {
// abort claude
abortFnRef.current?.()
// abort the LLM
if (latestRequestIdRef.current)
sendLLMMessageService.abort(latestRequestIdRef.current)
// if messageStream was not empty, add it to the history
const llmContent = messageStream || '(null)'
@ -342,7 +343,7 @@ export const SidebarChat = () => {
</div>
{/* error message */}
{!latestError ? null :
{latestError === null ? null :
<ErrorDisplay
error={latestError}
onDismiss={() => { setLatestError(null) }}

View file

@ -28,9 +28,13 @@ const getErrorDetails = (error: unknown) => {
e = new Error(String(error))
}
const message = e.message && e.error ?
(e.message + ':\n' + e.error)
: e.message || e.error || JSON.stringify(error)
details = {
name: e.name || 'Error',
message: e.message || String(e),
message: message,
stack: e.stack || null,
cause: e.cause ? String(e.cause) : null,
code: e.code || null,

View file

@ -174,7 +174,7 @@ const sendOpenAIMsg: SendLLMMessageFnTypeInternal = ({ messages, onText, onFinal
onError({ error: 'Invalid API key.' });
}
else {
onError(error);
onError({ error });
}
})
@ -206,7 +206,7 @@ export const sendOllamaMsg: SendLLMMessageFnTypeInternal = ({ messages, onText,
})
// when error/fail
.catch(error => {
onError(error)
onError({ error })
})
};
@ -266,7 +266,7 @@ const sendGreptileMsg: SendLLMMessageFnTypeInternal = ({ messages, onText, onFin
})
.catch(error => {
onError(error)
onError({ error })
});
}

View file

@ -15,7 +15,7 @@ export default defineConfig({
// sourcemap: true,
clean: true,
platform: 'browser',
platform: 'browser', // 'node'
target: 'esnext',
injectStyle: true, // bundle css into the output file
outExtension: () => ({ js: '.js' }),

View file

@ -731,9 +731,10 @@ Please finish writing the new file by applying the diff to the original file. Re
// <SUF>${suffix}</SUF>
// <MID>`;
const abortRef = { current: null } as { current: null | (() => void) }
await new Promise<void>((resolve, reject) => {
let streamRequestId: string | null = null
const object: LLMMessageServiceParams = {
logging: { loggingName: 'streamChunk' },
messages: [
@ -756,14 +757,16 @@ Please finish writing the new file by applying the diff to the original file. Re
onError: (e: any) => {
console.error('Error rewriting file with diff', e);
// TODO indicate there was an error
abortRef.current?.()
if (streamRequestId)
this._sendLLMMessageService.abort(streamRequestId)
diffArea._sweepState = { isStreaming: false, line: null }
resolve();
},
voidConfig,
}
this._sendLLMMessageService.sendLLMMessage(object)
streamRequestId = this._sendLLMMessageService.sendLLMMessage(object)
})
onFinishEdit()