Skip to content
10 min read · 2,023 words

Interface: DispatchContext

Defined in: lib/contracts/dispatch_context.ts:326

Context object for a single LLM execution call.

Remarks

Mirrors the surface of @nhtio/adk!TurnContext but is path-agnostic — it knows nothing about a parent context. Mutations apply to local Sets immediately, call persistence callbacks immediately, and fire the corresponding mutation hook (storedMemory, mutatedMemory, deletedMemory, etc.) in both standalone and derived dispatches.

The @nhtio/adk!DispatchRunner is the only thing that creates a context with a parent relationship: when dispatched with a source: TurnContext, the runner subscribes to the mutation hooks, queues deltas internally, and flushes them to the parent's Sets at the end of each iteration. The context itself remains unaware of the parent.

Middleware/executor signals termination via DispatchContext.ack (clean completion) or DispatchContext.nack (failure). Both set an internal flag the runner reads at end-of-iteration to decide whether to loop or exit. DispatchContext.isSignalled, DispatchContext.isAcked, and DispatchContext.nackError are publicly readable getters so middleware can inspect signal state and bail early.

Properties

PropertyModifierTypeDescriptionDefined in
abortreadonly(reason?: unknown) => voidAborts the dispatch's AbortController with the supplied reason. Middleware should call this when refusing to proceed — the runner short-circuits cleanly, dispatchEnd.status resolves to 'aborted', and no error event is emitted.lib/contracts/dispatch_context.ts:1067
abortedreadonlybooleantrue when the abort controller signal has fired.lib/contracts/dispatch_context.ts:1059
abortSignalreadonlyAbortSignalThe AbortSignal from the execution's AbortController.lib/contracts/dispatch_context.ts:1061
deleteMemoryreadonly(id: string) => Promise<void>Removes a memory from the local Set and persistence layer by ID.lib/contracts/dispatch_context.ts:1119
deleteMessagereadonly(id: string) => Promise<void>Removes a message from the local Set and persistence layer by ID.lib/contracts/dispatch_context.ts:1131
deleteRetrievablereadonly(id: string) => Promise<void>Removes a retrievable record from the local Set and persistence layer by ID.lib/contracts/dispatch_context.ts:1125
deleteStandingInstructionreadonly(v: string | Tokenizable) => Promise<void>Removes a standing instruction from the local Set and persistence layer.lib/contracts/dispatch_context.ts:1113
deleteThoughtreadonly(id: string) => Promise<void>Removes a thought from the local Set and persistence layer by ID.lib/contracts/dispatch_context.ts:1137
deleteToolCallreadonly(id: string) => Promise<void>Removes a tool call from the local Set and persistence layer by ID.lib/contracts/dispatch_context.ts:1143
dispatchIdreadonlystringStable identifier for the dispatch this context belongs to; set by DispatchRunner.lib/contracts/dispatch_context.ts:1055
emitMessagereadonlyEmitMessageFnEmits a message hook; fires registered handlers synchronously.lib/contracts/dispatch_context.ts:1173
emitThoughtreadonlyEmitThoughtFnEmits a thought hook; fires registered handlers synchronously.lib/contracts/dispatch_context.ts:1175
emitToolCallreadonlyEmitToolCallFnEmits a toolCall hook; fires registered handlers synchronously.lib/contracts/dispatch_context.ts:1177
emitToolExecutionEndreadonlyEmitToolExecutionEndFnEmits a toolExecutionEnd hook; fires registered handlers synchronously.lib/contracts/dispatch_context.ts:1181
emitToolExecutionStartreadonlyEmitToolExecutionStartFnEmits a toolExecutionStart hook; fires registered handlers synchronously.lib/contracts/dispatch_context.ts:1179
fetchMemoriesreadonly() => | Memory[] | Promise<Memory[]>Fetches memories; delegates to the callback supplied at construction.lib/contracts/dispatch_context.ts:1093
fetchMessagesreadonly() => | Message[] | Promise<Message[]>Fetches messages; delegates to the callback supplied at construction.lib/contracts/dispatch_context.ts:1097
fetchRetrievablesreadonly() => | Retrievable[] | Promise<Retrievable[]>Fetches retrievable records; delegates to the callback supplied at construction.lib/contracts/dispatch_context.ts:1095
fetchThoughtsreadonly() => | Thought[] | Promise<Thought[]>Fetches thoughts; delegates to the callback supplied at construction.lib/contracts/dispatch_context.ts:1099
fetchToolCallsreadonly() => | ToolCall[] | Promise<ToolCall[]>Fetches tool calls; delegates to the callback supplied at construction.lib/contracts/dispatch_context.ts:1101
fetchToolsreadonly() => | Tool<SpooledArtifact>[] | Promise<Tool<SpooledArtifact>[]>Fetches tools; delegates to the callback supplied at construction.lib/contracts/dispatch_context.ts:1103
idreadonlystringUnique identifier for this execution context, generated as UUIDv6 at construction time.lib/contracts/dispatch_context.ts:1053
isAckedreadonlybooleantrue when the context was signalled via DispatchContext.ack.lib/contracts/dispatch_context.ts:1071
isSignalledreadonlybooleantrue once DispatchContext.ack or DispatchContext.nack has been called.lib/contracts/dispatch_context.ts:1069
iterationreadonlynumber0-based iteration count within the current dispatch; updated by DispatchRunner.lib/contracts/dispatch_context.ts:1057
mutateMemoryreadonly(v: Memory) => Promise<void>Updates an existing memory in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1117
mutateMessagereadonly(v: Message) => Promise<void>Updates an existing message in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1129
mutateRetrievablereadonly(v: Retrievable) => Promise<void>Updates an existing retrievable record in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1123
mutateStandingInstructionreadonly(v: string | Tokenizable) => Promise<void>Updates an existing standing instruction in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1111
mutateThoughtreadonly(v: Thought) => Promise<void>Updates an existing thought in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1135
mutateToolCallreadonly(v: ToolCall) => Promise<void>Updates an existing tool call in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1141
nackErrorreadonlyError | undefinedThe error stored by DispatchContext.nack, or undefined if not nacked.lib/contracts/dispatch_context.ts:1073
refreshStandingInstructionsreadonly() => | (string | Tokenizable)[] | Promise<(string | Tokenizable)[]>Refreshes and returns standing instructions.lib/contracts/dispatch_context.ts:1105
standingInstructionsreadonlySet<Tokenizable>Standing instructions for this execution, in insertion order.lib/contracts/dispatch_context.ts:1079
stashreadonlyRegistryArbitrary key-value store for cross-step state.lib/contracts/dispatch_context.ts:1075
storeMediaBytesreadonly(id: string, bytes: ConduitBytes) => | MediaReader | Promise<MediaReader>Persists tool-generated media bytes into consumer storage and returns a @nhtio/adk!MediaReader. Remarks This is a low-level persistence conduit, NOT a mutation: it does not add to turnMessages/ turnToolCalls or fire a stored* hook. The handler builds a @nhtio/adk!Media from the returned reader (Media.toolGenerated({ reader })) and stores the owning primitive — a @nhtio/adk!Message attachment or @nhtio/adk!ToolCall result — via the relevant store* method separately. Persisting bytes without storing the primitive means the framework never sees the media.lib/contracts/dispatch_context.ts:1155
storeMemoryreadonly(v: Memory) => Promise<void>Stores a new memory in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1115
storeMessagereadonly(v: Message) => Promise<void>Stores a new message in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1127
storeRetrievablereadonly(v: Retrievable) => Promise<void>Stores a new retrievable record in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1121
storeRetrievableBytesreadonly(id: string, bytes: ConduitBytes) => | SpoolReader | Promise<SpoolReader>Persists extracted retrievable text bytes into consumer storage and returns a @nhtio/adk!SpoolReader. Remarks Low-level persistence conduit, same posture as DispatchContext.storeMediaBytes: returns a value, touches no Sets, fires no hook. Wrap the reader in a @nhtio/adk!SpooledArtifact and pass it as Retrievable.content, then persist the record via DispatchContext.storeRetrievable.lib/contracts/dispatch_context.ts:1168
storeStandingInstructionreadonly(v: string | Tokenizable) => Promise<void>Stores a new standing instruction in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1109
storeThoughtreadonly(v: Thought) => Promise<void>Stores a new thought in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1133
storeToolCallreadonly(v: ToolCall) => Promise<void>Stores a new tool call in the local Set and persistence layer.lib/contracts/dispatch_context.ts:1139
systemPromptreadonlyTokenizableThe system prompt for this execution.lib/contracts/dispatch_context.ts:1077
toolsreadonlyToolRegistryTool registry for this execution.lib/contracts/dispatch_context.ts:1091
turnMemoriesreadonlySet<Memory>Memories loaded for this execution.lib/contracts/dispatch_context.ts:1081
turnMessagesreadonlySet<Message>Messages loaded for this execution.lib/contracts/dispatch_context.ts:1085
turnRetrievablesreadonlySet<Retrievable>Retrievable records loaded for this execution.lib/contracts/dispatch_context.ts:1083
turnThoughtsreadonlySet<Thought>Thoughts loaded for this execution.lib/contracts/dispatch_context.ts:1087
turnToolCallsreadonlySet<ToolCall>Tool calls loaded for this execution.lib/contracts/dispatch_context.ts:1089
waitForreadonlyOpenGateFnOpens a gate and suspends until it resolves, rejects, times out, or is aborted.lib/contracts/dispatch_context.ts:1183

Methods

_getHooks()

ts
_getHooks(): Hooks<DispatchContextHooks>;

Defined in: lib/contracts/dispatch_context.ts:938

Internal

Accessor used by @nhtio/adk!DispatchRunner to register forwarding handlers.

Returns

Hooks<DispatchContextHooks>


_setDispatchId()

ts
_setDispatchId(id: string): void;

Defined in: lib/contracts/dispatch_context.ts:928

Internal

Set by @nhtio/adk!DispatchRunner after construction.

Parameters

ParameterType
idstring

Returns

void


_setIteration()

ts
_setIteration(n: number): void;

Defined in: lib/contracts/dispatch_context.ts:933

Internal

Updated by @nhtio/adk!DispatchRunner on each iteration.

Parameters

ParameterType
nnumber

Returns

void


ack()

ts
ack(): void;

Defined in: lib/contracts/dispatch_context.ts:969

Signals successful completion of this execution.

Returns

void

Remarks

Sets the context's internal signal flag. The @nhtio/adk!DispatchRunner reads the flag at the end of each iteration to decide whether to loop or exit. Calling ack() does NOT abort the current iteration — the current pipeline and flush complete first.

Throws

@nhtio/adk!E_LLM_EXECUTION_ALREADY_SIGNALLED when the context has already been signalled (whether via ack() or nack()).


nack()

ts
nack(error?: Error): void;

Defined in: lib/contracts/dispatch_context.ts:1027

Signals failed completion of this execution, optionally with an error.

Parameters

ParameterTypeDescription
error?ErrorOptional error describing the failure. If omitted, a generic Error is used.

Returns

void

Remarks

Sets the context's internal signal flag and stores the error. The @nhtio/adk!DispatchRunner reads the flag at the end of each iteration and surfaces the error via the dispatchEnd observability payload and as the rejection reason of dispatch(). Calling nack() does NOT abort the current iteration — the current pipeline and flush complete first.

Throws

@nhtio/adk!E_LLM_EXECUTION_ALREADY_SIGNALLED when the context has already been signalled.


onAck()

ts
onAck(handler: () => void): () => void;

Defined in: lib/contracts/dispatch_context.ts:1008

Registers a handler to run when this context completes successfully via ack.

Parameters

ParameterTypeDescription
handler() => voidCallback invoked when ack() is called.

Returns

An unsubscribe function that removes the handler.

() => void

Remarks

The handler does NOT fire on nack — failed executor runs should leave any ack-tied subscriptions alone so the consumer can inspect what was registered when debugging the failure. Returns an unsubscribe function; subscriptions are short-lived and die with the context regardless.

The canonical consumer is ToolRegistry.bindContext(ctx), which uses this hook to drop ephemeral tools (notably forged artifact-query tools from SpooledArtifact.forgeTools(ctx)) at ctx-completion. Consumers may also register custom handlers here for any per-executor cleanup.

See


toolCallCount()

ts
toolCallCount(checksum: string): number;

Defined in: lib/contracts/dispatch_context.ts:954

Returns how many times a tool call with the given checksum has been stored in this execution.

Parameters

ParameterTypeDescription
checksumstringThe ToolCall.checksum value to look up.

Returns

number

Remarks

Checksums are computed over tool + args (see @nhtio/adk!ToolCall.checksum). This count lets the executor detect repeat invocations of the same call without scanning the full Set. Returns 0 when the checksum has not been seen.