Skip to content

AIDK API Reference / aidk-kernel / KernelContext

Interface: KernelContext ​

Defined in: packages/kernel/src/context.ts:170

Execution context that flows through all async operations via AsyncLocalStorage.

The KernelContext contains all state needed during execution:

  • Identity and tracing (requestId, traceId, user)
  • Event buses (events, executionHandle)
  • Cancellation (signal)
  • Communication (channels)
  • Execution tracking (procedureGraph, procedurePid, origin)
  • Extensible storage (metadata, metrics)

Access the current context with Context.get() from anywhere in your code - no need to pass it explicitly through function calls.

Examples ​

typescript
const ctx = Context.create({
  user: { id: 'user-1' },
  metadata: { conversationId: 'conv-123' }
});

await Context.run(ctx, async () => {
  // Context is available here and in all async calls
  const current = Context.get();
  console.log(current.user?.id); // 'user-1'
});
typescript
interface AppContext extends KernelContext {
  customHandle?: MyCustomHandle;
  appMetadata: AppSpecificMetadata;
}

See ​

Properties ​

PropertyTypeDescriptionDefined in
channels?ChannelServiceInterfaceChannel service for bidirectional communication (optional). Injected by Engine when channels are configured. Tools and components can access channels via this service.packages/kernel/src/context.ts:192
eventsEventEmitterGlobal request event bus for subscribing to all eventspackages/kernel/src/context.ts:182
executionHandle?EventEmitter<any>Operation-specific event emitter (from .withHandle())packages/kernel/src/context.ts:186
executionId?stringCurrent execution ID. Set when entering an execution boundary. All procedures within this execution share this ID.packages/kernel/src/context.ts:221
executionType?stringType of execution at this boundary (e.g., 'engine', 'model', 'component_tool', 'fork', 'spawn'). Only meaningful at execution boundaries.packages/kernel/src/context.ts:227
metadataContextMetadataApplication-specific metadatapackages/kernel/src/context.ts:178
metricsContextMetricsAccumulated execution metricspackages/kernel/src/context.ts:180
origin?ProcedureNodeOrigin procedure node - the root procedure that initiated this execution chain. Undefined for the root procedure itself (since it IS the origin). Set automatically by ExecutionTracker when procedures are executed.packages/kernel/src/context.ts:209
parentExecutionId?stringParent execution ID for nested executions (e.g., component_tool called from engine). Enables DevTools to show execution hierarchy.packages/kernel/src/context.ts:233
parentHandle?ExecutionHandleParent execution handle for fork/spawn operations. Set by Engine when creating child executions.packages/core/src/types.ts:91
parentPid?stringParent execution PID for fork/spawn operations. Set by Engine when creating child executions.packages/core/src/types.ts:86
procedureGraph?ProcedureGraphProcedure graph for tracking procedure execution hierarchy. Automatically initialized when first procedure is executed.packages/kernel/src/context.ts:197
procedureNode?ProcedureNodeCurrent procedure node in the graphpackages/kernel/src/context.ts:203
procedurePid?stringCurrent procedure PID (for tracking nested procedures).packages/kernel/src/context.ts:201
requestIdstringUnique identifier for this request/executionpackages/kernel/src/context.ts:172
signal?AbortSignalAbort signal for cooperative cancellationpackages/kernel/src/context.ts:184
tick?numberCurrent tick number (set by engine during tick loop). Enables events to include tick context for correlation.packages/kernel/src/context.ts:239
traceIdstringCorrelation ID for distributed tracingpackages/kernel/src/context.ts:174
user?UserContextUser information (from auth)packages/kernel/src/context.ts:176

Released under the MIT License.