- BudgetTracker tracks LLM token cost separately from tool cost - LlmClient auto-records cost into request-scoped budget - Router selects tier from classify confidence, intent risk, escalation count - Supervisor short-circuits via regex fast-path when rule confidence is high - Onboarding skill loads user escalation history to inform routing
55 lines
1.2 KiB
TypeScript
55 lines
1.2 KiB
TypeScript
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
|
|
import type { Logger } from 'pino';
|
|
|
|
import { getLogger } from './logger.js';
|
|
import type { BudgetTracker } from '@agent/budget';
|
|
|
|
export interface RequestContext {
|
|
chatroomId: string;
|
|
deadlineAt?: string;
|
|
budget?: BudgetTracker | undefined;
|
|
handoffChain?: string[] | undefined;
|
|
handoffDepth?: number | undefined;
|
|
logger: Logger;
|
|
requestId: string;
|
|
skill: string;
|
|
startedAt: string;
|
|
trigger: string;
|
|
userId: string;
|
|
}
|
|
|
|
const requestContextStorage = new AsyncLocalStorage<RequestContext>();
|
|
|
|
export function runWithContext<T>(
|
|
context: Omit<RequestContext, 'logger'> & { logger?: Logger },
|
|
fn: () => T,
|
|
): T {
|
|
const logger =
|
|
context.logger ??
|
|
getLogger({
|
|
chatroomId: context.chatroomId,
|
|
requestId: context.requestId,
|
|
skill: context.skill,
|
|
handoffDepth: context.handoffDepth,
|
|
trigger: context.trigger,
|
|
userId: context.userId,
|
|
});
|
|
|
|
return requestContextStorage.run(
|
|
{
|
|
...context,
|
|
logger,
|
|
},
|
|
fn,
|
|
);
|
|
}
|
|
|
|
export function getContext(): RequestContext | undefined {
|
|
return requestContextStorage.getStore();
|
|
}
|
|
|
|
export function getRequestLogger(): Logger {
|
|
return getContext()?.logger ?? getLogger();
|
|
}
|