@@ -50,11 +50,15 @@ export namespace SessionProcessor {
5050 while ( true ) {
5151 const parts = new Set < string > ( )
5252 const texts : MessageV2 . TextPart [ ] = [ ]
53- let failed = false
53+ const before = {
54+ finish : input . assistantMessage . finish ,
55+ cost : input . assistantMessage . cost ,
56+ tokens : structuredClone ( input . assistantMessage . tokens ) ,
57+ usageStatus : input . assistantMessage . usageStatus ,
58+ }
5459 try {
5560 let currentText : MessageV2 . TextPart | undefined
5661 let reasoningMap : Record < string , MessageV2 . ReasoningPart > = { }
57- let sawToolCall = false
5862 const stream = await LLM . stream ( streamInput )
5963
6064 for await ( const value of stream . fullStream ) {
@@ -115,7 +119,6 @@ export namespace SessionProcessor {
115119 break
116120
117121 case "tool-input-start" :
118- sawToolCall = true
119122 const part = await Session . updatePart ( {
120123 id : toolcalls [ value . id ] ?. id ?? Identifier . ascending ( "part" ) ,
121124 messageID : input . assistantMessage . id ,
@@ -130,6 +133,7 @@ export namespace SessionProcessor {
130133 } ,
131134 } )
132135 toolcalls [ value . id ] = part as MessageV2 . ToolPart
136+ parts . add ( part . id )
133137 break
134138
135139 case "tool-input-delta" :
@@ -139,7 +143,6 @@ export namespace SessionProcessor {
139143 break
140144
141145 case "tool-call" : {
142- sawToolCall = true
143146 const match = toolcalls [ value . toolCallId ]
144147 if ( match ) {
145148 const part = await Session . updatePart ( {
@@ -155,9 +158,10 @@ export namespace SessionProcessor {
155158 metadata : value . providerMetadata ,
156159 } )
157160 toolcalls [ value . toolCallId ] = part as MessageV2 . ToolPart
161+ parts . add ( part . id )
158162
159- const parts = await MessageV2 . parts ( input . assistantMessage . id )
160- const lastThree = parts . slice ( - DOOM_LOOP_THRESHOLD )
163+ const history = await MessageV2 . parts ( input . assistantMessage . id )
164+ const lastThree = history . slice ( - DOOM_LOOP_THRESHOLD )
161165
162166 if (
163167 lastThree . length === DOOM_LOOP_THRESHOLD &&
@@ -257,7 +261,10 @@ export namespace SessionProcessor {
257261 usage : value . usage ,
258262 metadata : value . providerMetadata ,
259263 } )
260- input . assistantMessage . finish = value . finishReason
264+ input . assistantMessage . finish =
265+ value . finishReason === "unknown" && texts . some ( ( part ) => part . text . trim ( ) )
266+ ? "stop"
267+ : value . finishReason
261268 input . assistantMessage . cost += usage . cost
262269 input . assistantMessage . tokens = usage . tokens
263270 input . assistantMessage . usageStatus = usage . usageStatus
@@ -292,8 +299,12 @@ export namespace SessionProcessor {
292299 cost : usage . cost ,
293300 } )
294301 await Session . updateMessage ( input . assistantMessage )
295- if ( value . finishReason === "unknown" && ! sawToolCall && ! texts . some ( ( part ) => part . text . trim ( ) ) ) {
296- failed = true
302+ if (
303+ value . finishReason === "unknown" &&
304+ ! MessageV2 . hasVisibleOutput (
305+ ( await MessageV2 . parts ( input . assistantMessage . id ) ) . filter ( ( part ) => parts . has ( part . id ) ) ,
306+ )
307+ ) {
297308 throw new MessageV2 . APIError ( {
298309 message : "Provider ended stream without finishReason or a tool call." ,
299310 isRetryable : true ,
@@ -310,8 +321,10 @@ export namespace SessionProcessor {
310321 if ( snapshot ) {
311322 const patch = await Snapshot . patch ( snapshot )
312323 if ( patch . files . length ) {
324+ const id = Identifier . ascending ( "part" )
325+ parts . add ( id )
313326 await Session . updatePart ( {
314- id : Identifier . ascending ( "part" ) ,
327+ id,
315328 messageID : input . assistantMessage . id ,
316329 sessionID : input . sessionID ,
317330 type : "patch" ,
@@ -405,41 +418,69 @@ export namespace SessionProcessor {
405418 const error = MessageV2 . APIError . isInstance ( e )
406419 ? e
407420 : MessageV2 . fromError ( e , { providerID : input . model . providerID } )
421+ const apiError = MessageV2 . APIError . isInstance ( error )
408422 if ( MessageV2 . ContextOverflowError . isInstance ( error ) ) {
409423 // TODO: Handle context overflow error
410424 }
411425 const retry = SessionRetry . retryable ( error )
412426 if ( retry !== undefined ) {
427+ if ( snapshot ) {
428+ const patch = await Snapshot . patch ( snapshot )
429+ if ( patch . files . length ) {
430+ const id = Identifier . ascending ( "part" )
431+ parts . add ( id )
432+ await Session . updatePart ( {
433+ id,
434+ messageID : input . assistantMessage . id ,
435+ sessionID : input . sessionID ,
436+ type : "patch" ,
437+ hash : patch . hash ,
438+ files : patch . files ,
439+ } )
440+ }
441+ snapshot = undefined
442+ }
443+ for ( const partID of parts ) {
444+ await Session . removePart ( {
445+ sessionID : input . sessionID ,
446+ messageID : input . assistantMessage . id ,
447+ partID,
448+ } )
449+ }
450+ for ( const key of Object . keys ( toolcalls ) ) {
451+ if ( parts . has ( toolcalls [ key ] . id ) ) delete toolcalls [ key ]
452+ }
453+ input . assistantMessage . finish = before . finish
454+ input . assistantMessage . cost = before . cost
455+ input . assistantMessage . tokens = before . tokens
456+ input . assistantMessage . usageStatus = before . usageStatus
457+ await Session . updateMessage ( input . assistantMessage )
413458 attempt ++
414459 if ( attempt > SessionRetry . MAX_RETRY_ATTEMPTS ) {
415460 log . error ( "max retry attempts reached" , { attempt, retry } )
416- input . assistantMessage . error = MessageV2 . APIError . isInstance ( error )
461+ input . assistantMessage . error = apiError
417462 ? new MessageV2 . APIError ( {
418- ...error . data ,
419- message : `Max retry attempts (${ SessionRetry . MAX_RETRY_ATTEMPTS } ) reached: ${ retry } ` ,
463+ message : `Max retry attempts (${ SessionRetry . MAX_RETRY_ATTEMPTS } ) reached: ${ SessionRetry . reason ( error ) } ` ,
464+ statusCode : error . data . statusCode ,
465+ isRetryable : error . data . isRetryable ,
466+ metadata : error . data . metadata ?. finishReason
467+ ? { finishReason : error . data . metadata . finishReason }
468+ : undefined ,
420469 } ) . toObject ( )
421470 : new NamedError . Unknown ( {
422- message : `Max retry attempts (${ SessionRetry . MAX_RETRY_ATTEMPTS } ) reached: ${ retry } ` ,
471+ message : `Max retry attempts (${ SessionRetry . MAX_RETRY_ATTEMPTS } ) reached: ${ SessionRetry . reason ( error ) } ` ,
423472 } ) . toObject ( )
424473 await Bus . publish ( Session . Event . Error , {
425474 sessionID : input . assistantMessage . sessionID ,
426475 error : input . assistantMessage . error ,
427476 } )
428477 } else {
429- if ( failed ) {
430- for ( const partID of parts ) {
431- await Session . removePart ( {
432- sessionID : input . sessionID ,
433- messageID : input . assistantMessage . id ,
434- partID,
435- } )
436- }
437- }
438- const delay = SessionRetry . delay ( attempt , error . name === "APIError" ? error : undefined )
478+ const delay = SessionRetry . delay ( attempt , apiError ? error : undefined )
439479 SessionStatus . set ( input . sessionID , {
440480 type : "retry" ,
441481 attempt,
442482 message : retry ,
483+ reason : SessionRetry . reason ( error ) ,
443484 next : Date . now ( ) + delay ,
444485 } )
445486 await SessionRetry . sleep ( delay , input . abort ) . catch ( ( ) => { } )
0 commit comments