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 ​
- Context - Static methods to create/access context
- UserContext - User information structure
Properties ​
| Property | Type | Description | Defined in |
|---|---|---|---|
channels? | ChannelServiceInterface | Channel 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 |
events | EventEmitter | Global request event bus for subscribing to all events | packages/kernel/src/context.ts:182 |
executionHandle? | EventEmitter<any> | Operation-specific event emitter (from .withHandle()) | packages/kernel/src/context.ts:186 |
executionId? | string | Current execution ID. Set when entering an execution boundary. All procedures within this execution share this ID. | packages/kernel/src/context.ts:221 |
executionType? | string | Type of execution at this boundary (e.g., 'engine', 'model', 'component_tool', 'fork', 'spawn'). Only meaningful at execution boundaries. | packages/kernel/src/context.ts:227 |
metadata | ContextMetadata | Application-specific metadata | packages/kernel/src/context.ts:178 |
metrics | ContextMetrics | Accumulated execution metrics | packages/kernel/src/context.ts:180 |
origin? | ProcedureNode | Origin 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? | string | Parent 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? | ExecutionHandle | Parent execution handle for fork/spawn operations. Set by Engine when creating child executions. | packages/core/src/types.ts:91 |
parentPid? | string | Parent execution PID for fork/spawn operations. Set by Engine when creating child executions. | packages/core/src/types.ts:86 |
procedureGraph? | ProcedureGraph | Procedure graph for tracking procedure execution hierarchy. Automatically initialized when first procedure is executed. | packages/kernel/src/context.ts:197 |
procedureNode? | ProcedureNode | Current procedure node in the graph | packages/kernel/src/context.ts:203 |
procedurePid? | string | Current procedure PID (for tracking nested procedures). | packages/kernel/src/context.ts:201 |
requestId | string | Unique identifier for this request/execution | packages/kernel/src/context.ts:172 |
signal? | AbortSignal | Abort signal for cooperative cancellation | packages/kernel/src/context.ts:184 |
tick? | number | Current tick number (set by engine during tick loop). Enables events to include tick context for correlation. | packages/kernel/src/context.ts:239 |
traceId | string | Correlation ID for distributed tracing | packages/kernel/src/context.ts:174 |
user? | UserContext | User information (from auth) | packages/kernel/src/context.ts:176 |