AgentWorkflow
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:171
AgentWorkflow - An event-driven workflow for executing agents with tools
This class provides a simple interface for creating and running agent workflows based on the LlamaIndexTS workflow system. It supports single agent workflows with multiple tools.
Implements
Section titled “Implements”Workflow
Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new AgentWorkflow(
__namedParameters
):AgentWorkflow
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:182
Parameters
Section titled “Parameters”__namedParameters
Section titled “__namedParameters”Returns
Section titled “Returns”AgentWorkflow
Methods
Section titled “Methods”handle()
Section titled “handle()”handle<
AcceptEvents
,Result
>(accept
,handler
):void
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:240
Registers a handler function for one or more workflow events.
The handler will be invoked whenever any of the accepted events are sent through a workflow context. Handlers can process events and optionally return new events to continue the workflow.
Type Parameters
Section titled “Type Parameters”AcceptEvents
Section titled “AcceptEvents”AcceptEvents
extends WorkflowEvent
<unknown
>[]
Array of event types this handler accepts
Result
Section titled “Result”Result
extends void
| WorkflowEventData
<unknown
, string
>
Return type of the handler (event data or void)
Parameters
Section titled “Parameters”accept
Section titled “accept”AcceptEvents
Array of event types that trigger this handler
handler
Section titled “handler”Handler
<AcceptEvents
, Result
>
Function to execute when matching events are received
Returns
Section titled “Returns”void
Example
Section titled “Example”// Handle multiple event typesworkflow.handle([StartEvent, RestartEvent], async (context, event) => \{ if (StartEvent.include(event)) { return ProcessEvent.with({ action: 'start' \}); } else \{ return ProcessEvent.with({ action: 'restart' \}); }});
Implementation of
Section titled “Implementation of”Workflow.handle
createContext()
Section titled “createContext()”createContext():
WorkflowContext
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:247
Creates a new workflow context for event processing.
The context provides the runtime environment for executing handlers and managing event flow. Each context maintains its own execution state and event queue.
Returns
Section titled “Returns”WorkflowContext
A new workflow context instance
Example
Section titled “Example”const context = workflow.createContext();
// Send events through the contextawait context.send(MyEvent.with(\{ data: 'hello' \}));
// Listen for specific eventsconst result = await context.waitFor(CompletionEvent);
Implementation of
Section titled “Implementation of”Workflow.createContext
addAgent()
Section titled “addAgent()”addAgent(
agent
):this
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:301
Adds a new agent to the workflow
Parameters
Section titled “Parameters”Returns
Section titled “Returns”this
getAgents()
Section titled “getAgents()”getAgents():
BaseWorkflowAgent
[]
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:312
Gets all agents in this workflow
Returns
Section titled “Returns”Array of agents in this workflow
fromTools()
Section titled “fromTools()”
static
fromTools(params
):AgentWorkflow
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:321
Create a simple workflow with a single agent and specified tools
Parameters
Section titled “Parameters”params
Section titled “params”Parameters for the single agent workflow
Returns
Section titled “Returns”AgentWorkflow
A new AgentWorkflow instance
runStream()
Section titled “runStream()”runStream<
Z
>(userInput
,params?
):WorkflowStream
<WorkflowEventData
<AgentResultData
<ZodInfer
<Z
>>>>
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:651
Type Parameters
Section titled “Type Parameters”Z
extends ZodSchema
Parameters
Section titled “Parameters”userInput
Section titled “userInput”MessageContent
params?
Section titled “params?”chatHistory?
Section titled “chatHistory?”ChatMessage
[]
state?
Section titled “state?”responseFormat?
Section titled “responseFormat?”Z
Returns
Section titled “Returns”WorkflowStream
<WorkflowEventData
<AgentResultData
<ZodInfer
<Z
>>>>
run<
Z
>(userInput
,params?
):Promise
<WorkflowEventData
<AgentResultData
<ZodInfer
<Z
>>>>
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:677
Type Parameters
Section titled “Type Parameters”Z
extends ZodSchema
Parameters
Section titled “Parameters”userInput
Section titled “userInput”MessageContent
params?
Section titled “params?”chatHistory?
Section titled “chatHistory?”ChatMessage
[]
state?
Section titled “state?”responseFormat?
Section titled “responseFormat?”Z
Returns
Section titled “Returns”Promise
<WorkflowEventData
<AgentResultData
<ZodInfer
<Z
>>>>
createHandoffTool()
Section titled “createHandoffTool()”createHandoffTool(
agents
):FunctionTool
<{toAgent
:string
;reason
:string
; },JSONValue
|Promise
<JSONValue
>, () =>AgentWorkflowState
>
Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:696
Parameters
Section titled “Parameters”agents
Section titled “agents”Map
<string
, BaseWorkflowAgent
>
Returns
Section titled “Returns”FunctionTool
<{ toAgent
: string
; reason
: string
; }, JSONValue
| Promise
<JSONValue
>, () => AgentWorkflowState
>