@@ -173,10 +173,24 @@ export function registerMessageListeners(
173173 break
174174 }
175175 case chatRequestType . method : {
176- const chatParams = { ...message . params } as ChatParams
176+ const chatParams : ChatParams = { ...message . params }
177177 const partialResultToken = uuidv4 ( )
178- const chatDisposable = languageClient . onProgress ( chatRequestType , partialResultToken , ( partialResult ) =>
179- handlePartialResult < ChatResult > ( partialResult , encryptionKey , provider , chatParams . tabId )
178+ let lastPartialResult : ChatResult | undefined
179+ const chatDisposable = languageClient . onProgress (
180+ chatRequestType ,
181+ partialResultToken ,
182+ ( partialResult ) => {
183+ // Store the latest partial result
184+ if ( typeof partialResult === 'string' && encryptionKey ) {
185+ void decodeRequest < ChatResult > ( partialResult , encryptionKey ) . then (
186+ ( decoded ) => ( lastPartialResult = decoded )
187+ )
188+ } else {
189+ lastPartialResult = partialResult as ChatResult
190+ }
191+
192+ void handlePartialResult < ChatResult > ( partialResult , encryptionKey , provider , chatParams . tabId )
193+ }
180194 )
181195
182196 const editor =
@@ -188,17 +202,36 @@ export function registerMessageListeners(
188202 }
189203
190204 const chatRequest = await encryptRequest < ChatParams > ( chatParams , encryptionKey )
191- const chatResult = ( await languageClient . sendRequest ( chatRequestType . method , {
192- ...chatRequest ,
193- partialResultToken,
194- } ) ) as string | ChatResult
195- void handleCompleteResult < ChatResult > (
196- chatResult ,
197- encryptionKey ,
198- provider ,
199- chatParams . tabId ,
200- chatDisposable
201- )
205+ try {
206+ const chatResult = await languageClient . sendRequest < string | ChatResult > ( chatRequestType . method , {
207+ ...chatRequest ,
208+ partialResultToken,
209+ } )
210+ await handleCompleteResult < ChatResult > (
211+ chatResult ,
212+ encryptionKey ,
213+ provider ,
214+ chatParams . tabId ,
215+ chatDisposable
216+ )
217+ } catch ( e ) {
218+ languageClient . info ( `Error occurred during chat request: ${ e } ` )
219+ // Use the last partial result if available, append error message
220+ const errorResult : ChatResult = {
221+ ...lastPartialResult ,
222+ body : lastPartialResult ?. body
223+ ? `${ lastPartialResult . body } \n\n ❌ Error: Request failed to complete`
224+ : '❌ An error occurred while processing your request' ,
225+ }
226+
227+ await handleCompleteResult < ChatResult > (
228+ errorResult ,
229+ encryptionKey ,
230+ provider ,
231+ chatParams . tabId ,
232+ chatDisposable
233+ )
234+ }
202235 break
203236 }
204237 case quickActionRequestType . method : {
0 commit comments