@@ -13,7 +13,7 @@ import { Settings } from "@llamaindex/core/global";
13
13
import type { ChatMessage , MessageContent } from "@llamaindex/core/llms" ;
14
14
import { ChatMemoryBuffer } from "@llamaindex/core/memory" ;
15
15
import { PromptTemplate } from "@llamaindex/core/prompts" ;
16
- import { FunctionTool } from "@llamaindex/core/tools" ;
16
+ import { tool } from "@llamaindex/core/tools" ;
17
17
import { stringifyJSONToMessageContent } from "@llamaindex/core/utils" ;
18
18
import { z } from "zod" ;
19
19
import type { AgentWorkflowState , BaseWorkflowAgent } from "./base" ;
@@ -244,7 +244,7 @@ export class AgentWorkflow implements Workflow {
244
244
agent . canHandoffTo . forEach ( ( name ) => {
245
245
toHandoffAgents . set ( name , this . agents . get ( name ) ! ) ;
246
246
} ) ;
247
- const handoffTool = createHandoffTool ( toHandoffAgents ) ;
247
+ const handoffTool = this . createHandoffTool ( toHandoffAgents ) ;
248
248
if (
249
249
agent . canHandoffTo . length > 0 &&
250
250
! agent . tools . some ( ( t ) => t . metadata . name === handoffTool . metadata . name )
@@ -558,15 +558,7 @@ export class AgentWorkflow implements Workflow {
558
558
if ( ! tool ) {
559
559
throw new Error ( `Tool ${ toolCall . toolName } not found` ) ;
560
560
}
561
- if ( tool . metadata . requireContext ) {
562
- const input = {
563
- context : this . stateful . getContext ( ) . state ,
564
- ...toolCall . toolKwargs ,
565
- } ;
566
- return tool . call ( input ) ;
567
- } else {
568
- return tool . call ( toolCall . toolKwargs ) ;
569
- }
561
+ return tool . call ( toolCall . toolKwargs ) ;
570
562
}
571
563
572
564
private createInitialState ( ) : AgentWorkflowState {
@@ -620,42 +612,16 @@ export class AgentWorkflow implements Workflow {
620
612
}
621
613
return finalEvent ;
622
614
}
623
- }
624
615
625
- const createHandoffTool = ( agents : Map < string , BaseWorkflowAgent > ) => {
626
- const agentInfo = Array . from ( agents . values ( ) ) . reduce (
627
- ( acc , a ) => {
628
- acc [ a . name ] = a . description ;
629
- return acc ;
630
- } ,
631
- { } as Record < string , string > ,
632
- ) ;
633
- return FunctionTool . from (
634
- ( {
635
- context,
636
- toAgent,
637
- reason,
638
- } : {
639
- context ?: AgentWorkflowState ;
640
- toAgent : string ;
641
- reason : string ;
642
- } ) => {
643
- if ( ! context ) {
644
- throw new Error ( "Context is required for handoff" ) ;
645
- }
646
- const agents = context . agents ;
647
- if ( ! agents . includes ( toAgent ) ) {
648
- return `Agent ${ toAgent } not found. Select a valid agent to hand off to. Valid agents: ${ agents . join (
649
- ", " ,
650
- ) } `;
651
- }
652
- context . nextAgentName = toAgent ;
653
- return DEFAULT_HANDOFF_OUTPUT_PROMPT . format ( {
654
- to_agent : toAgent ,
655
- reason : reason ,
656
- } ) ;
657
- } ,
658
- {
616
+ createHandoffTool ( agents : Map < string , BaseWorkflowAgent > ) {
617
+ const agentInfo = Array . from ( agents . values ( ) ) . reduce (
618
+ ( acc , a ) => {
619
+ acc [ a . name ] = a . description ;
620
+ return acc ;
621
+ } ,
622
+ { } as Record < string , string > ,
623
+ ) ;
624
+ return tool ( {
659
625
name : "handOff" ,
660
626
description : DEFAULT_HANDOFF_PROMPT . format ( {
661
627
agent_info : JSON . stringify ( agentInfo ) ,
@@ -668,7 +634,34 @@ const createHandoffTool = (agents: Map<string, BaseWorkflowAgent>) => {
668
634
description : "The reason for handing off to the agent" ,
669
635
} ) ,
670
636
} ) ,
671
- requireContext : true ,
672
- } ,
673
- ) ;
674
- } ;
637
+ execute : (
638
+ {
639
+ toAgent,
640
+ reason,
641
+ } : {
642
+ toAgent : string ;
643
+ reason : string ;
644
+ } ,
645
+ contextProvider ?: ( ) => AgentWorkflowState ,
646
+ ) => {
647
+ if ( ! contextProvider ) {
648
+ throw new Error (
649
+ "Handoff tool internal error: Context was not provided." ,
650
+ ) ;
651
+ }
652
+ const context = contextProvider ( ) ;
653
+ const agents = context . agents ;
654
+ if ( ! agents . includes ( toAgent ) ) {
655
+ return `Agent ${ toAgent } not found. Select a valid agent to hand off to. Valid agents: ${ agents . join (
656
+ ", " ,
657
+ ) } `;
658
+ }
659
+ context . nextAgentName = toAgent ;
660
+ return DEFAULT_HANDOFF_OUTPUT_PROMPT . format ( {
661
+ to_agent : toAgent ,
662
+ reason : reason ,
663
+ } ) ;
664
+ } ,
665
+ } ) . bind ( ( ) => this . stateful . getContext ( ) . state ) ;
666
+ }
667
+ }
0 commit comments