@@ -797,147 +797,6 @@ describe("Task - Streaming Tool Call Handling", () => {
797797 } )
798798 } )
799799
800- describe ( "Task.ts compound key dedup and streaming paths" , ( ) => {
801- // Test-local structural stand-in for the Task.assistantMessageContent entries
802- // this block exercises: partial tool_use objects and parser results.
803- type AssistantContentEntry = {
804- type ?: string
805- id ?: string
806- name ?: string
807- params ?: Partial < Record < ToolParamName , string > >
808- partial ?: boolean
809- }
810-
811- it ( "should build and use compound streaming keys via makeStreamingKey" , ( ) => {
812- const key = NativeToolCallParser . makeStreamingKey ( "toolu_compound123" , "read_file" )
813- expect ( key ) . toBe ( "toolu_compound123::read_file" )
814-
815- NativeToolCallParser . startStreamingToolCall ( "toolu_compound123" , "read_file" , scope )
816- expect ( NativeToolCallParser . getStreamingToolName ( key , scope ) ) . toBe ( "read_file" )
817- } )
818-
819- it ( "should ignore duplicate tool_call_start for same compound key" , ( ) => {
820- const streamingToolCallIndices = new Map < string , number > ( )
821- const warnSpy = vi . spyOn ( console , "warn" ) . mockImplementation ( ( ) => { } )
822- const events = [
823- { type : "tool_call_start" , id : "toolu_dup_compound" , name : "read_file" as const } ,
824- { type : "tool_call_start" , id : "toolu_dup_compound" , name : "read_file" as const } ,
825- ]
826- const assistantMessageContent : AssistantContentEntry [ ] = [ ]
827-
828- for ( const event of events ) {
829- const dedupKey = `${ event . id } ::${ event . name } `
830- if ( streamingToolCallIndices . has ( dedupKey ) ) {
831- console . warn (
832- `[Task#test] Ignoring duplicate tool_call_start for ID: ${ event . id } (tool: ${ event . name } )` ,
833- )
834- continue
835- }
836- streamingToolCallIndices . set ( dedupKey , assistantMessageContent . length )
837- assistantMessageContent . push ( { type : "tool_use" , id : event . id , name : event . name , partial : true } )
838- }
839-
840- expect ( assistantMessageContent ) . toHaveLength ( 1 )
841- expect ( streamingToolCallIndices . has ( "toolu_dup_compound::read_file" ) ) . toBe ( true )
842- expect ( warnSpy ) . toHaveBeenCalledWith ( expect . stringContaining ( "[Task#test]" ) )
843- warnSpy . mockRestore ( )
844- } )
845-
846- it ( "should use compound key for delta events via getStreamingToolCallById" , ( ) => {
847- const streamingToolCallIndices = new Map < string , number > ( )
848- const assistantMessageContent : AssistantContentEntry [ ] = [ ]
849-
850- NativeToolCallParser . startStreamingToolCall ( "toolu_delta_compound" , "read_file" , scope )
851- const dedupKey = NativeToolCallParser . makeStreamingKey ( "toolu_delta_compound" , "read_file" )
852- streamingToolCallIndices . set ( dedupKey , 0 )
853- assistantMessageContent . push ( {
854- type : "tool_use" ,
855- id : "toolu_delta_compound" ,
856- name : "read_file" ,
857- params : { } ,
858- partial : true ,
859- } )
860-
861- const partialToolUse = NativeToolCallParser . processStreamingChunk ( dedupKey , '{"path":"' , scope )
862- expect ( partialToolUse ) . not . toBeNull ( )
863-
864- const existingEntry = NativeToolCallParser . getStreamingToolCallById ( "toolu_delta_compound" , scope )
865- expect ( existingEntry ) . not . toBeNull ( )
866- const resolvedKey = existingEntry
867- ? NativeToolCallParser . makeStreamingKey ( existingEntry . id , existingEntry . name )
868- : undefined
869- const updatedToolUse = NativeToolCallParser . processStreamingChunk ( resolvedKey ! , '"test.ts"}' , scope )
870- expect ( updatedToolUse ) . not . toBeNull ( )
871- const name = NativeToolCallParser . getStreamingToolName ( resolvedKey ! , scope )
872- const toolUseIndex = streamingToolCallIndices . get ( `${ existingEntry ! . id } ::${ name } ` )
873- expect ( toolUseIndex ) . toBe ( 0 )
874- if ( ! updatedToolUse ) {
875- throw new Error ( "Expected processStreamingChunk to return a tool use" )
876- }
877- assistantMessageContent [ toolUseIndex ! ] = updatedToolUse
878-
879- expect ( assistantMessageContent ) . toHaveLength ( 1 )
880- expect ( assistantMessageContent [ 0 ] ?. type ) . toBe ( "tool_use" )
881- expect ( assistantMessageContent [ 0 ] ?. name ) . toBe ( "read_file" )
882- } )
883-
884- it ( "should finalize and clean up tracking using compound key on tool_call_end" , ( ) => {
885- const streamingToolCallIndices = new Map < string , number > ( )
886- const assistantMessageContent : AssistantContentEntry [ ] = [ ]
887- const dedupKey = NativeToolCallParser . makeStreamingKey ( "toolu_cleanup123" , "read_file" )
888-
889- NativeToolCallParser . startStreamingToolCall ( "toolu_cleanup123" , "read_file" , scope )
890- streamingToolCallIndices . set ( dedupKey , 0 )
891- assistantMessageContent . push ( { type : "tool_use" , id : "toolu_cleanup123" , name : "read_file" , partial : true } )
892- NativeToolCallParser . processStreamingChunk ( dedupKey , '{"path":"test.ts"}' , scope )
893-
894- const finalToolUse = NativeToolCallParser . finalizeStreamingToolCall ( dedupKey , scope )
895- expect ( finalToolUse ) . not . toBeNull ( )
896- if ( ! finalToolUse ) {
897- throw new Error ( "Expected finalizeStreamingToolCall to return a tool use" )
898- }
899- assistantMessageContent [ 0 ] = finalToolUse
900- streamingToolCallIndices . delete ( dedupKey )
901-
902- expect ( assistantMessageContent ) . toHaveLength ( 1 )
903- expect ( assistantMessageContent [ 0 ] ?. type ) . toBe ( "tool_use" )
904- expect ( assistantMessageContent [ 0 ] ?. name ) . toBe ( "read_file" )
905- expect ( assistantMessageContent [ 0 ] ?. partial ) . toBe ( false )
906- expect (
907- streamingToolCallIndices . has ( NativeToolCallParser . makeStreamingKey ( "toolu_cleanup123" , "read_file" ) ) ,
908- ) . toBe ( false )
909- } )
910-
911- it ( "should handle malformed JSON finalization with compound key cleanup" , ( ) => {
912- const streamingToolCallIndices = new Map < string , number > ( )
913- const assistantMessageContent : AssistantContentEntry [ ] = [ ]
914- const dedupKey = NativeToolCallParser . makeStreamingKey ( "toolu_malformed123" , "read_file" )
915-
916- NativeToolCallParser . startStreamingToolCall ( "toolu_malformed123" , "read_file" , scope )
917- streamingToolCallIndices . set ( dedupKey , 0 )
918- assistantMessageContent . push ( {
919- type : "tool_use" ,
920- id : "toolu_malformed123" ,
921- name : "read_file" ,
922- partial : true ,
923- } )
924- NativeToolCallParser . processStreamingChunk ( dedupKey , "{invalid json" , scope )
925-
926- const finalToolUse = NativeToolCallParser . finalizeStreamingToolCall ( dedupKey , scope )
927- expect ( finalToolUse ) . toBeNull ( )
928- assistantMessageContent [ 0 ] . partial = false
929- streamingToolCallIndices . delete ( dedupKey )
930-
931- expect ( assistantMessageContent ) . toHaveLength ( 1 )
932- expect ( assistantMessageContent [ 0 ] ?. type ) . toBe ( "tool_use" )
933- expect ( assistantMessageContent [ 0 ] ?. name ) . toBe ( "read_file" )
934- expect ( assistantMessageContent [ 0 ] ?. partial ) . toBe ( false )
935- expect (
936- streamingToolCallIndices . has ( NativeToolCallParser . makeStreamingKey ( "toolu_malformed123" , "read_file" ) ) ,
937- ) . toBe ( false )
938- } )
939- } )
940-
941800 describe ( "real Task streaming integration (attemptApiRequest)" , ( ) => {
942801 // Structural access to the private Task state these tests assert against.
943802 type TaskStreamingTestAccess = {
0 commit comments