@@ -68,6 +68,7 @@ export interface CodyClient {
68
68
setEditorScope : ( editor : Editor ) => void
69
69
toggleIncludeInferredRepository : ( ) => void
70
70
toggleIncludeInferredFile : ( ) => void
71
+ abortMessageInProgress : ( ) => void
71
72
}
72
73
73
74
interface CodyClientProps {
@@ -91,6 +92,7 @@ export const useClient = ({
91
92
const [ transcript , setTranscriptState ] = useState < Transcript | null > ( initialTranscript )
92
93
const [ chatMessages , setChatMessagesState ] = useState < ChatMessage [ ] > ( [ ] )
93
94
const [ isMessageInProgress , setIsMessageInProgressState ] = useState < boolean > ( false )
95
+ const [ abortMessageInProgressInternal , setAbortMessageInProgress ] = useState < ( ) => void > ( ( ) => ( ) => undefined )
94
96
95
97
const messageInProgress : ChatMessage | null = useMemo ( ( ) => {
96
98
if ( isMessageInProgress ) {
@@ -104,6 +106,18 @@ export const useClient = ({
104
106
return null
105
107
} , [ chatMessages , isMessageInProgress ] )
106
108
109
+ const abortMessageInProgress = useCallback ( ( ) => {
110
+ abortMessageInProgressInternal ( )
111
+
112
+ transcript
113
+ ?. toChatPromise ( )
114
+ . then ( messages => {
115
+ setChatMessagesState ( messages )
116
+ setIsMessageInProgressState ( false )
117
+ } )
118
+ . catch ( error => console . error ( `aborting in progress message failed: ${ error } ` ) )
119
+ } , [ abortMessageInProgressInternal , transcript , setChatMessagesState , setIsMessageInProgressState ] )
120
+
107
121
const setTranscript = useCallback ( async ( transcript : Transcript ) : Promise < void > => {
108
122
const messages = await transcript . toChatPromise ( )
109
123
@@ -256,8 +270,9 @@ export const useClient = ({
256
270
257
271
const responsePrefix = interaction . getAssistantMessage ( ) . prefix ?? ''
258
272
let rawText = ''
259
- return new Promise ( resolve => {
260
- chatClient . chat ( prompt , {
273
+
274
+ const updatedTranscript = await new Promise < Transcript | null > ( resolve => {
275
+ const abort = chatClient . chat ( prompt , {
261
276
onChange ( _rawText ) {
262
277
rawText = _rawText
263
278
@@ -297,7 +312,16 @@ export const useClient = ({
297
312
resolve ( transcript )
298
313
} ,
299
314
} )
315
+
316
+ setAbortMessageInProgress ( ( ) => ( ) => {
317
+ abort ( )
318
+ resolve ( transcript )
319
+ } )
300
320
} )
321
+
322
+ setAbortMessageInProgress ( ( ) => ( ) => undefined )
323
+
324
+ return updatedTranscript
301
325
} ,
302
326
[
303
327
config ,
@@ -310,6 +334,7 @@ export const useClient = ({
310
334
chatClient ,
311
335
isMessageInProgress ,
312
336
onEvent ,
337
+ setAbortMessageInProgress ,
313
338
]
314
339
)
315
340
@@ -363,6 +388,7 @@ export const useClient = ({
363
388
editMessage,
364
389
toggleIncludeInferredRepository,
365
390
toggleIncludeInferredFile,
391
+ abortMessageInProgress,
366
392
} ) ,
367
393
[
368
394
transcript ,
@@ -381,6 +407,7 @@ export const useClient = ({
381
407
editMessage ,
382
408
toggleIncludeInferredRepository ,
383
409
toggleIncludeInferredFile ,
410
+ abortMessageInProgress ,
384
411
]
385
412
)
386
413
}
0 commit comments