UNPKG

@ai2070/l0

Version:

L0: The Missing Reliability Substrate for AI

127 lines 4.23 kB
import { EventType } from "../types/observability"; export function registerCallbackWrappers(dispatcher, options) { if (options.onStart) { const callback = options.onStart; dispatcher.onEvent((event) => { if (event.type === EventType.SESSION_START) { const e = event; callback(e.attempt, e.isRetry, e.isFallback); } else if (event.type === EventType.ATTEMPT_START) { const e = event; callback(e.attempt, e.isRetry, e.isFallback); } else if (event.type === EventType.FALLBACK_START) { callback(1, false, true); } }); } if (options.onComplete) { const callback = options.onComplete; dispatcher.onEvent((event) => { if (event.type === EventType.COMPLETE) { const e = event; if (e.state) { callback(e.state); } } }); } if (options.onError) { const callback = options.onError; dispatcher.onEvent((event) => { if (event.type === EventType.ERROR) { const e = event; const willRetry = e.recoveryStrategy === "retry"; const willFallback = e.recoveryStrategy === "fallback"; callback(new Error(e.error), willRetry, willFallback); } }); } if (options.onViolation) { const callback = options.onViolation; dispatcher.onEvent((event) => { if (event.type === EventType.GUARDRAIL_RULE_RESULT) { const e = event; if (e.violation) { callback(e.violation); } } }); } if (options.onRetry) { const callback = options.onRetry; dispatcher.onEvent((event) => { if (event.type === EventType.RETRY_ATTEMPT) { const e = event; callback(e.attempt, e.reason); } }); } if (options.onFallback) { const callback = options.onFallback; dispatcher.onEvent((event) => { if (event.type === EventType.FALLBACK_START) { const e = event; callback(e.toIndex - 1, e.reason); } }); } if (options.onResume) { const callback = options.onResume; dispatcher.onEvent((event) => { if (event.type === EventType.RESUME_START) { const e = event; callback(e.checkpoint, e.tokenCount); } }); } if (options.onCheckpoint) { const callback = options.onCheckpoint; dispatcher.onEvent((event) => { if (event.type === EventType.CHECKPOINT_SAVED) { const e = event; callback(e.checkpoint, e.tokenCount); } }); } if (options.onTimeout) { const callback = options.onTimeout; dispatcher.onEvent((event) => { if (event.type === EventType.TIMEOUT_TRIGGERED) { const e = event; callback(e.timeoutType, e.elapsedMs); } }); } if (options.onAbort) { const callback = options.onAbort; dispatcher.onEvent((event) => { if (event.type === EventType.ABORT_COMPLETED) { const e = event; callback(e.tokenCount, e.contentLength); } }); } if (options.onDrift) { const callback = options.onDrift; dispatcher.onEvent((event) => { if (event.type === EventType.DRIFT_CHECK_RESULT) { const e = event; if (e.detected) { callback(e.types, e.confidence); } } }); } if (options.onToolCall) { const callback = options.onToolCall; dispatcher.onEvent((event) => { if (event.type === EventType.TOOL_REQUESTED) { const e = event; callback(e.toolName, e.toolCallId, e.arguments); } }); } } //# sourceMappingURL=callback-wrappers.js.map