Types
import type { MiddlewareHandler, Middleware, AgentMiddlewareAgent, AgentRunRequest, BeforeAgentUpdate } from "@arcgis/ai-components/agent-utils/middlewares/types.js";

Type definitions

MiddlewareHandler

Type definition
Type parameters
<TRequest, TResult>

Handles a middleware request and returns the resulting value.

Parameters

ParameterTypeDescriptionRequired
request
TRequest

The request payload for the current middleware stage.

Returns
Promise | TResult

Middleware

Type definition
Type parameters
<TRequest, TResult>

Wraps a middleware request and delegates to the next handler.

Parameters

ParameterTypeDescriptionRequired
request
TRequest

The request payload for the current middleware stage.

handler
MiddlewareHandler<TRequest, TResult>

The next handler in the middleware chain.

Returns
Promise | TResult

AgentMiddlewareAgent

Type definition

Agent metadata provided to middleware hooks.

description

Property
Type
string | undefined
Since
ArcGIS Maps SDK for JavaScript 5.1

Description of the agent, which can be used to provide context in middleware hooks and interrupts.

id

Property
Type
string
Since
ArcGIS Maps SDK for JavaScript 5.1

Unique identifier for the agent, used for registration and invocation purposes.

name

Property
Type
string
Since
ArcGIS Maps SDK for JavaScript 5.1

Name of the agent, used for display and identification purposes.

AgentRunRequest

Type definition
Type parameters
<TCustomState extends AgentData = EmptyAgentData>

Execution metadata and the read-only current state provided to agent middleware hooks.

agent

Property
Type
AgentMiddlewareAgent
Since
ArcGIS Maps SDK for JavaScript 5.1

The agent that is being executed. This can be used in middleware hooks to access agent metadata and provide context for interrupts or other middleware behavior.

config

Property
Type
AgentExecutionConfig | undefined
Since
ArcGIS Maps SDK for JavaScript 5.1

The current execution configuration for the agent run, which may include configurable parameters, metadata, and other contextual information. This can be used in middleware hooks to make decisions based on the execution context or to store and retrieve information related to the agent run.

nodeName

Property
Type
string
Since
ArcGIS Maps SDK for JavaScript 5.1

The name of the current node being executed in the agent's workflow. This can be used in middleware hooks to provide context or make decisions based on the specific node.

state

Property
Type
Readonly<AgentGraphState>
Since
ArcGIS Maps SDK for JavaScript 5.1

The current reduced state. Read it without mutation; return a partial update from the hook to change state.

BeforeAgentUpdate

Type definition
Type parameters
<TCustomState extends AgentData = EmptyAgentData>

Partial graph-state update returned by a beforeAgent hook.

Supertypes
Partial<AgentGraphState>

jumpTo

Property
Type
"end" | undefined

Applies the update, skips remaining beforeAgent hooks and graph execution, then runs afterAgent hooks.

AgentMiddleware

Type definition
Type parameters
<TCustomState extends AgentData = EmptyAgentData>

Named lifecycle, model-call, and tool-call hooks used by an LLMAgent.

name

Property
Type
string

Name assigned to this middleware.

hooks

Property
Type
AgentMiddlewareHooks

Hooks to run during the agent lifecycle, model calls, and tool calls.

AgentLifecycleMiddleware

Type definition
Type parameters
<TCustomState extends AgentData = EmptyAgentData>

Named lifecycle hooks for FunctionAgent, WorkflowAgent, or LLMAgent, without model-call or tool-call hooks.

name

Property
Type
string

Name assigned to this middleware.

hooks

Property
Type
AgentLifecycleHooks & { beforeModel?: never; afterModel?: never; wrapModelCall?: never; wrapToolCall?: never; }

Lifecycle hooks to run before or after agent execution.

AgentLifecycleHooks

Type definition
Type parameters
<TCustomState extends AgentData = EmptyAgentData>

Hooks that run before agent execution and after it completes.

afterAgent

readonly Property
Type
MiddlewareHandler<AgentRunRequest, Partial<AgentGraphState> | void> | undefined

Runs in reverse registration order after completion, including returned failures and early exits. Does not run as a finally handler for thrown errors, cancellation, or interruption.

beforeAgent

readonly Property
Type
MiddlewareHandler<AgentRunRequest, BeforeAgentUpdate | void> | undefined

Runs in registration order before execution. Return jumpTo: "end" to apply the update, skip remaining before-hooks and execution, and run the after-hooks.

AgentMiddlewareHooks

Type definition
Type parameters
<TCustomState extends AgentData = EmptyAgentData>

Lifecycle and LangChain hooks for model requests made by an LLMAgent.

Supertypes
AgentLifecycleHooks<TCustomState>

afterModel

readonly Property
Type
LangChainAgentMiddleware["afterModel"] | undefined

Runs after a model call so the response or resulting state can be inspected or updated.

beforeModel

readonly Property
Type
LangChainAgentMiddleware["beforeModel"] | undefined

Runs before a model call so its request or state can be inspected or updated.

wrapModelCall

Method
Signature
wrapModelCall (request: Parameters<ModelCallHook>[0], handler: Parameters<ModelCallHook>[1], config?: AgentExecutionConfig): ReturnType<ModelCallHook>

Wraps a model call; inspect or modify its request, invoke the handler, or provide a response.

Parameters

ParameterTypeDescriptionRequired
request
handler
config
Returns
ReturnType<ModelCallHook>

wrapToolCall

Method
Signature
wrapToolCall (request: Parameters<ToolCallHook>[0], handler: Parameters<ToolCallHook>[1], config?: AgentExecutionConfig): ReturnType<ToolCallHook>

Wraps a tool call made during model execution; inspect or modify it, invoke the handler, or provide a result.

Parameters

ParameterTypeDescriptionRequired
request
handler
config
Returns
ReturnType<ToolCallHook>

ToolMiddlewareHooks

Type definition
Type parameters
<TInput extends AgentData = AgentData, TOutput = unknown>
Since
ArcGIS Maps SDK for JavaScript 5.1

Hooks for intercepting and controlling a FunctionTool call.

wrapToolCall

readonly Property
Type
FunctionToolMiddlewareHandler<TInput, TOutput> | undefined
Since
ArcGIS Maps SDK for JavaScript 5.1

Hook that wraps a tool call, allowing you to intercept and modify the request and response of a tool execution. This can be used for logging, modifying inputs/outputs, or implementing custom behavior around tool calls.

ModelCallHook

Type definition
Supertypes
NonNullable<LangChainAgentMiddleware["wrapModelCall"]>

ToolCallHook

Type definition
Supertypes
NonNullable<LangChainAgentMiddleware["wrapToolCall"]>