@@ -365,6 +365,38 @@ export const layer: Layer.Layer<
365365 }
366366 } )
367367
368+ const finishText = Effect . fn ( "SessionProcessor.finishText" ) ( function * (
369+ providerMetadata ?: Extract < StreamEvent , { type : "text-end" } > [ "providerMetadata" ] ,
370+ ) {
371+ if ( ! ctx . currentText ) return
372+ // oxlint-disable-next-line no-self-assign -- reactivity trigger
373+ ctx . currentText . text = ctx . currentText . text
374+ ctx . currentText . text = ( yield * plugin . trigger (
375+ "experimental.text.complete" ,
376+ {
377+ sessionID : ctx . sessionID ,
378+ messageID : ctx . assistantMessage . id ,
379+ partID : ctx . currentText . id ,
380+ } ,
381+ { text : ctx . currentText . text } ,
382+ ) ) . text
383+ if ( ! ctx . assistantMessage . summary ) {
384+ // TODO(v2): Temporary dual-write while migrating session messages to v2 events.
385+ if ( Flag . OPENCODE_EXPERIMENTAL_EVENT_SYSTEM ) {
386+ yield * sync . run ( SessionEvent . Text . Ended . Sync , {
387+ sessionID : ctx . sessionID ,
388+ text : ctx . currentText . text ,
389+ timestamp : DateTime . makeUnsafe ( Date . now ( ) ) ,
390+ } )
391+ }
392+ }
393+ const end = Date . now ( )
394+ ctx . currentText . time = { start : ctx . currentText . time ?. start ?? end , end }
395+ if ( providerMetadata ) ctx . currentText . metadata = providerMetadata
396+ yield * session . updatePart ( ctx . currentText )
397+ ctx . currentText = undefined
398+ } )
399+
368400 const handleEvent = Effect . fnUntraced ( function * ( value : StreamEvent ) {
369401 switch ( value . type ) {
370402 case "request-start" :
@@ -696,38 +728,11 @@ export const layer: Layer.Layer<
696728 return
697729
698730 case "text-end" :
699- if ( ! ctx . currentText ) return
700- // oxlint-disable-next-line no-self-assign -- reactivity trigger
701- ctx . currentText . text = ctx . currentText . text
702- ctx . currentText . text = ( yield * plugin . trigger (
703- "experimental.text.complete" ,
704- {
705- sessionID : ctx . sessionID ,
706- messageID : ctx . assistantMessage . id ,
707- partID : ctx . currentText . id ,
708- } ,
709- { text : ctx . currentText . text } ,
710- ) ) . text
711- if ( ! ctx . assistantMessage . summary ) {
712- // TODO(v2): Temporary dual-write while migrating session messages to v2 events.
713- if ( Flag . OPENCODE_EXPERIMENTAL_EVENT_SYSTEM ) {
714- yield * sync . run ( SessionEvent . Text . Ended . Sync , {
715- sessionID : ctx . sessionID ,
716- text : ctx . currentText . text ,
717- timestamp : DateTime . makeUnsafe ( Date . now ( ) ) ,
718- } )
719- }
720- }
721- {
722- const end = Date . now ( )
723- ctx . currentText . time = { start : ctx . currentText . time ?. start ?? end , end }
724- }
725- if ( value . providerMetadata ) ctx . currentText . metadata = value . providerMetadata
726- yield * session . updatePart ( ctx . currentText )
727- ctx . currentText = undefined
731+ yield * finishText ( value . providerMetadata )
728732 return
729733
730734 case "request-finish" :
735+ yield * finishText ( )
731736 if ( ! ctx . assistantMessage . finish ) yield * finishStep ( value )
732737 return
733738 }
0 commit comments