@@ -21,8 +21,8 @@ export interface PrepareResult {
2121 */
2222export interface Service {
2323 clearUnsafe ( ) : void
24- readonly markParts : ( parts : ReadonlyArray < object > , responseId : string ) => void
25- readonly prepareUnsafe : ( prompt : Prompt . Prompt ) => Option . Option < PrepareResult >
24+ markParts ( parts : ReadonlyArray < object > , responseId : string ) : void
25+ prepareUnsafe ( prompt : Prompt . Prompt ) : Option . Option < PrepareResult >
2626}
2727
2828/**
@@ -40,16 +40,21 @@ export class ResponseIdTracker
4040export const make : Effect . Effect < Service > = Effect . sync ( ( ) => {
4141 let sentParts = new WeakMap < object , string > ( )
4242
43+ const none = ( ) => {
44+ sentParts = new WeakMap < object , string > ( )
45+ return Option . none < PrepareResult > ( )
46+ }
47+
4348 return {
4449 clearUnsafe ( ) {
4550 sentParts = new WeakMap < object , string > ( )
4651 } ,
47- markParts : ( parts , responseId ) => {
48- for ( const part of parts ) {
49- sentParts . set ( part , responseId )
52+ markParts ( parts , responseId ) {
53+ for ( let i = 0 ; i < parts . length ; i ++ ) {
54+ sentParts . set ( parts [ i ] , responseId )
5055 }
5156 } ,
52- prepareUnsafe : ( prompt ) => {
57+ prepareUnsafe ( prompt ) {
5358 const messages = prompt . content
5459
5560 let anyTracked = false
@@ -59,7 +64,7 @@ export const make: Effect.Effect<Service> = Effect.sync(() => {
5964 break
6065 }
6166 }
62- if ( ! anyTracked ) return Option . none ( )
67+ if ( ! anyTracked ) return none ( )
6368
6469 let lastAssistantIndex = - 1
6570 for ( let i = messages . length - 1 ; i >= 0 ; i -- ) {
@@ -68,19 +73,19 @@ export const make: Effect.Effect<Service> = Effect.sync(() => {
6873 break
6974 }
7075 }
71- if ( lastAssistantIndex === - 1 ) return Option . none ( )
76+ if ( lastAssistantIndex === - 1 ) return none ( )
7277
7378 let responseId : string | undefined
7479 for ( let i = 0 ; i < lastAssistantIndex ; i ++ ) {
7580 const id = sentParts . get ( messages [ i ] )
76- if ( id === undefined ) return Option . none ( )
81+ if ( id === undefined ) return none ( )
7782 responseId = id
7883 }
79- if ( responseId === undefined ) return Option . none ( )
84+ if ( responseId === undefined ) return none ( )
8085
8186 const partsAfterLastAssistant = messages . slice ( lastAssistantIndex + 1 )
8287 if ( partsAfterLastAssistant . length === 0 ) {
83- return Option . none ( )
88+ return none ( )
8489 }
8590
8691 return Option . some ( {
0 commit comments