Skip to content

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.

  • Workflow

new AgentWorkflow(__namedParameters): AgentWorkflow

Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:182

AgentWorkflowParams

AgentWorkflow

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.

AcceptEvents extends WorkflowEvent<unknown>[]

Array of event types this handler accepts

Result extends void | WorkflowEventData<unknown, string>

Return type of the handler (event data or void)

AcceptEvents

Array of event types that trigger this handler

Handler<AcceptEvents, Result>

Function to execute when matching events are received

void

// Handle multiple event types
workflow.handle([StartEvent, RestartEvent], async (context, event) => \{
if (StartEvent.include(event)) {
return ProcessEvent.with({ action: 'start' \});
} else \{
return ProcessEvent.with({ action: 'restart' \});
}
});

Workflow.handle


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.

WorkflowContext

A new workflow context instance

const context = workflow.createContext();
// Send events through the context
await context.send(MyEvent.with(\{ data: 'hello' \}));
// Listen for specific events
const result = await context.waitFor(CompletionEvent);

Workflow.createContext


addAgent(agent): this

Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:301

Adds a new agent to the workflow

BaseWorkflowAgent

this


getAgents(): BaseWorkflowAgent[]

Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:312

Gets all agents in this workflow

BaseWorkflowAgent[]

Array of agents in this workflow


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

SingleAgentParams

Parameters for the single agent workflow

AgentWorkflow

A new AgentWorkflow instance


runStream<Z>(userInput, params?): WorkflowStream<WorkflowEventData<AgentResultData<ZodInfer<Z>>>>

Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:651

Z extends ZodSchema

MessageContent

ChatMessage[]

AgentWorkflowState

Z

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

Z extends ZodSchema

MessageContent

ChatMessage[]

AgentWorkflowState

Z

Promise<WorkflowEventData<AgentResultData<ZodInfer<Z>>>>


createHandoffTool(agents): FunctionTool<{ toAgent: string; reason: string; }, JSONValue | Promise<JSONValue>, () => AgentWorkflowState>

Defined in: .build/typescript/packages/workflow/src/agent/agent-workflow.ts:696

Map<string, BaseWorkflowAgent>

FunctionTool<{ toAgent: string; reason: string; }, JSONValue | Promise<JSONValue>, () => AgentWorkflowState>