@@ -2,7 +2,6 @@ import { Cause, Effect, Exit, Layer, ServiceMap } from "effect"
22import * as Stream from "effect/Stream"
33import { Agent } from "@/agent/agent"
44import { Bus } from "@/bus"
5- import { makeRuntime } from "@/effect/run-service"
65import { Config } from "@/config/config"
76import { Permission } from "@/permission"
87import { Plugin } from "@/plugin"
@@ -35,17 +34,10 @@ export namespace SessionProcessor {
3534 readonly process : ( streamInput : LLM . StreamInput ) => Effect . Effect < Result >
3635 }
3736
38- export interface Info {
39- readonly message : MessageV2 . Assistant
40- readonly partFromToolCall : ( toolCallID : string ) => MessageV2 . ToolPart | undefined
41- readonly process : ( streamInput : LLM . StreamInput ) => Promise < Result >
42- }
43-
4437 type Input = {
4538 assistantMessage : MessageV2 . Assistant
4639 sessionID : SessionID
4740 model : Provider . Model
48- abort : AbortSignal
4941 }
5042
5143 export interface Interface {
@@ -96,7 +88,6 @@ export namespace SessionProcessor {
9688 assistantMessage : input . assistantMessage ,
9789 sessionID : input . sessionID ,
9890 model : input . model ,
99- abort : input . abort ,
10091 toolcalls : { } ,
10192 shouldBreak : false ,
10293 snapshot : undefined ,
@@ -105,11 +96,12 @@ export namespace SessionProcessor {
10596 currentText : undefined ,
10697 reasoningMap : { } ,
10798 }
99+ let aborted = false
108100
109101 const parse = ( e : unknown ) =>
110102 MessageV2 . fromError ( e , {
111103 providerID : input . model . providerID ,
112- aborted : input . abort . aborted ,
104+ aborted,
113105 } )
114106
115107 const handleEvent = Effect . fn ( "SessionProcessor.handleEvent" ) ( function * ( value : StreamEvent ) {
@@ -440,16 +432,12 @@ export namespace SessionProcessor {
440432 const stream = llm . stream ( streamInput )
441433
442434 yield * stream . pipe (
443- Stream . tap ( ( event ) =>
444- Effect . gen ( function * ( ) {
445- input . abort . throwIfAborted ( )
446- yield * handleEvent ( event )
447- } ) ,
448- ) ,
435+ Stream . tap ( ( event ) => handleEvent ( event ) ) ,
449436 Stream . takeUntil ( ( ) => ctx . needsCompaction ) ,
450437 Stream . runDrain ,
451438 )
452439 } ) . pipe (
440+ Effect . onInterrupt ( ( ) => Effect . sync ( ( ) => void ( aborted = true ) ) ) ,
453441 Effect . catchCauseIf (
454442 ( cause ) => ! Cause . hasInterruptsOnly ( cause ) ,
455443 ( cause ) => Effect . fail ( Cause . squash ( cause ) ) ,
@@ -468,17 +456,20 @@ export namespace SessionProcessor {
468456 ) ,
469457 Effect . catchCause ( ( cause ) =>
470458 Cause . hasInterruptsOnly ( cause )
471- ? halt ( new DOMException ( "Aborted" , "AbortError" ) )
459+ ? Effect . gen ( function * ( ) {
460+ aborted = true
461+ yield * halt ( new DOMException ( "Aborted" , "AbortError" ) )
462+ } )
472463 : halt ( Cause . squash ( cause ) ) ,
473464 ) ,
474465 Effect . ensuring ( cleanup ( ) ) ,
475466 )
476467
477- if ( input . abort . aborted && ! ctx . assistantMessage . error ) {
468+ if ( aborted && ! ctx . assistantMessage . error ) {
478469 yield * abort ( )
479470 }
480471 if ( ctx . needsCompaction ) return "compact"
481- if ( ctx . blocked || ctx . assistantMessage . error || input . abort . aborted ) return "stop"
472+ if ( ctx . blocked || ctx . assistantMessage . error || aborted ) return "stop"
482473 return "continue"
483474 } )
484475
@@ -526,29 +517,4 @@ export namespace SessionProcessor {
526517 ) ,
527518 ) ,
528519 )
529-
530- const { runPromise } = makeRuntime ( Service , defaultLayer )
531-
532- export async function create ( input : Input ) : Promise < Info > {
533- const hit = await runPromise ( ( svc ) => svc . create ( input ) )
534- return {
535- get message ( ) {
536- return hit . message
537- } ,
538- partFromToolCall ( toolCallID : string ) {
539- return hit . partFromToolCall ( toolCallID )
540- } ,
541- async process ( streamInput : LLM . StreamInput ) {
542- const exit = await Effect . runPromiseExit ( hit . process ( streamInput ) , { signal : input . abort } )
543- if ( Exit . isFailure ( exit ) ) {
544- if ( Cause . hasInterrupts ( exit . cause ) && input . abort . aborted ) {
545- await Effect . runPromise ( hit . abort ( ) )
546- return "stop"
547- }
548- throw Cause . squash ( exit . cause )
549- }
550- return exit . value
551- } ,
552- }
553- }
554520}
0 commit comments