UNPKG

@ai2070/l0

Version:

L0: The Missing Reliability Substrate for AI

202 lines (201 loc) 5.17 kB
import { z } from "zod4"; import { GuardrailViolationSchema } from "./guardrails"; import { BackoffStrategySchema } from "./retry"; import { FailureTypeSchema, RecoveryStrategySchema, RecoveryPolicySchema } from "./observability"; const L0RecordedEventTypeSchema = z.enum( [ "START", "TOKEN", "CHECKPOINT", "GUARDRAIL", "DRIFT", "RETRY", "FALLBACK", "CONTINUATION", "COMPLETE", "ERROR" ] ); const SerializedOptionsSchema = z.object({ prompt: z.string().optional(), model: z.string().optional(), retry: z.object({ attempts: z.number().optional(), maxRetries: z.number().optional(), baseDelay: z.number().optional(), maxDelay: z.number().optional(), backoff: BackoffStrategySchema.optional() }).optional(), timeout: z.object({ initialToken: z.number().optional(), interToken: z.number().optional() }).optional(), checkIntervals: z.object({ guardrails: z.number().optional(), drift: z.number().optional(), checkpoint: z.number().optional() }).optional(), continueFromLastKnownGoodToken: z.boolean().optional(), detectDrift: z.boolean().optional(), detectZeroTokens: z.boolean().optional(), fallbackCount: z.number().optional(), guardrailCount: z.number().optional(), metadata: z.record(z.string(), z.unknown()).optional() }); const SerializedErrorSchema = z.object({ name: z.string(), message: z.string(), code: z.string().optional(), stack: z.string().optional(), metadata: z.record(z.string(), z.unknown()).optional() }); const GuardrailEventResultSchema = z.object({ violations: z.array(GuardrailViolationSchema), shouldRetry: z.boolean(), shouldHalt: z.boolean() }); const DriftEventResultSchema = z.object({ detected: z.boolean(), types: z.array(z.string()), confidence: z.number() }); const L0StartEventSchema = z.object({ type: z.literal("START"), ts: z.number(), options: SerializedOptionsSchema }); const L0TokenEventSchema = z.object({ type: z.literal("TOKEN"), ts: z.number(), value: z.string(), index: z.number() }); const L0CheckpointEventSchema = z.object({ type: z.literal("CHECKPOINT"), ts: z.number(), at: z.number(), content: z.string() }); const L0GuardrailEventSchema = z.object({ type: z.literal("GUARDRAIL"), ts: z.number(), at: z.number(), result: GuardrailEventResultSchema }); const L0DriftEventSchema = z.object({ type: z.literal("DRIFT"), ts: z.number(), at: z.number(), result: DriftEventResultSchema }); const L0RetryEventSchema = z.object({ type: z.literal("RETRY"), ts: z.number(), reason: z.string(), attempt: z.number(), countsTowardLimit: z.boolean() }); const L0FallbackEventSchema = z.object({ type: z.literal("FALLBACK"), ts: z.number(), to: z.number() }); const L0ContinuationEventSchema = z.object({ type: z.literal("CONTINUATION"), ts: z.number(), checkpoint: z.string(), at: z.number() }); const L0CompleteEventSchema = z.object({ type: z.literal("COMPLETE"), ts: z.number(), content: z.string(), tokenCount: z.number() }); const L0ErrorEventSchema = z.object({ type: z.literal("ERROR"), ts: z.number(), error: SerializedErrorSchema, failureType: FailureTypeSchema, recoveryStrategy: RecoveryStrategySchema, policy: RecoveryPolicySchema }); const L0RecordedEventSchema = z.union([ L0StartEventSchema, L0TokenEventSchema, L0CheckpointEventSchema, L0GuardrailEventSchema, L0DriftEventSchema, L0RetryEventSchema, L0FallbackEventSchema, L0ContinuationEventSchema, L0CompleteEventSchema, L0ErrorEventSchema ]); const L0EventEnvelopeSchema = z.object({ streamId: z.string(), seq: z.number(), event: L0RecordedEventSchema }); const L0SnapshotSchema = z.object({ streamId: z.string(), seq: z.number(), ts: z.number(), content: z.string(), tokenCount: z.number(), checkpoint: z.string(), violations: z.array(GuardrailViolationSchema), driftDetected: z.boolean(), retryAttempts: z.number(), networkRetryCount: z.number(), fallbackIndex: z.number() }); const L0ExecutionModeSchema = z.enum([ "live", "record", "replay" ]); const L0ReplayOptionsSchema = z.object({ streamId: z.string(), eventStore: z.any(), // L0EventStore interface speed: z.number().optional(), fireCallbacks: z.boolean().optional(), fromSeq: z.number().optional(), toSeq: z.number().optional() }); const L0RecordOptionsSchema = z.object({ eventStore: z.any(), // L0EventStore interface streamId: z.string().optional(), saveSnapshots: z.boolean().optional(), snapshotInterval: z.number().optional() }); export { DriftEventResultSchema, GuardrailEventResultSchema, L0CheckpointEventSchema, L0CompleteEventSchema, L0ContinuationEventSchema, L0DriftEventSchema, L0ErrorEventSchema, L0EventEnvelopeSchema, L0ExecutionModeSchema, L0FallbackEventSchema, L0GuardrailEventSchema, L0RecordOptionsSchema, L0RecordedEventSchema, L0RecordedEventTypeSchema, L0ReplayOptionsSchema, L0RetryEventSchema, L0SnapshotSchema, L0StartEventSchema, L0TokenEventSchema, SerializedErrorSchema, SerializedOptionsSchema }; //# sourceMappingURL=events.js.map