|
| 1 | +// @license MIT |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | +import { z } from "zod"; |
| 4 | + |
| 5 | +const BaseSchema = z.object({ |
| 6 | + eventId: z.string().min(1), |
| 7 | + loopId: z.string().min(1), |
| 8 | + aggregateId: z.string().min(1), |
| 9 | + orgId: z.string().min(1), |
| 10 | + occurredAt: z.string().min(1), |
| 11 | + correlationId: z.string().min(1), |
| 12 | + causationId: z.string().optional() |
| 13 | +}); |
| 14 | + |
| 15 | +const ActorSchema = z.object({ |
| 16 | + type: z.enum(["human", "automation", "ai-agent", "webhook", "system"]), |
| 17 | + id: z.string().min(1) |
| 18 | +}); |
| 19 | + |
| 20 | +const EvidenceSchema = z.record(z.unknown()); |
| 21 | + |
| 22 | +export const LoopStartedEventSchema = BaseSchema.extend({ |
| 23 | + type: z.literal("loop.started"), |
| 24 | + initialState: z.string().min(1), |
| 25 | + actor: ActorSchema |
| 26 | +}); |
| 27 | + |
| 28 | +export const TransitionRequestedEventSchema = BaseSchema.extend({ |
| 29 | + type: z.literal("loop.transition.requested"), |
| 30 | + transitionId: z.string().min(1), |
| 31 | + actor: ActorSchema, |
| 32 | + evidence: EvidenceSchema, |
| 33 | + requestedAt: z.string().min(1) |
| 34 | +}); |
| 35 | + |
| 36 | +export const TransitionExecutedEventSchema = BaseSchema.extend({ |
| 37 | + type: z.literal("loop.transition.executed"), |
| 38 | + fromState: z.string().min(1), |
| 39 | + toState: z.string().min(1), |
| 40 | + transitionId: z.string().min(1), |
| 41 | + actor: ActorSchema, |
| 42 | + evidence: EvidenceSchema, |
| 43 | + durationMs: z.number().optional() |
| 44 | +}); |
| 45 | + |
| 46 | +export const TransitionBlockedEventSchema = BaseSchema.extend({ |
| 47 | + type: z.literal("loop.transition.blocked"), |
| 48 | + transitionId: z.string().min(1), |
| 49 | + reason: z.enum(["guard_failed", "unauthorized_actor", "invalid_transition", "loop_closed"]), |
| 50 | + actor: ActorSchema, |
| 51 | + guardFailures: z.array(z.object({ guardId: z.string(), message: z.string() })).optional() |
| 52 | +}); |
| 53 | + |
| 54 | +export const GuardFailedEventSchema = BaseSchema.extend({ |
| 55 | + type: z.literal("loop.guard.failed"), |
| 56 | + fromState: z.string().min(1), |
| 57 | + attemptedTransitionId: z.string().min(1), |
| 58 | + guardId: z.string().min(1), |
| 59 | + guardFailureMessage: z.string().min(1), |
| 60 | + severity: z.enum(["hard", "soft"]).optional(), |
| 61 | + actor: ActorSchema |
| 62 | +}); |
| 63 | + |
| 64 | +export const LoopCompletedEventSchema = BaseSchema.extend({ |
| 65 | + type: z.literal("loop.completed"), |
| 66 | + terminalState: z.string().min(1), |
| 67 | + actor: ActorSchema, |
| 68 | + durationMs: z.number(), |
| 69 | + transitionCount: z.number().int().nonnegative(), |
| 70 | + outcomeId: z.string().min(1), |
| 71 | + valueUnit: z.string().min(1) |
| 72 | +}); |
| 73 | + |
| 74 | +export const LoopErrorEventSchema = BaseSchema.extend({ |
| 75 | + type: z.literal("loop.error"), |
| 76 | + errorState: z.string().min(1), |
| 77 | + errorCode: z.string().min(1), |
| 78 | + errorMessage: z.string().min(1), |
| 79 | + actor: ActorSchema |
| 80 | +}); |
| 81 | + |
| 82 | +export const LoopSpawnedEventSchema = BaseSchema.extend({ |
| 83 | + type: z.literal("loop.spawned"), |
| 84 | + parentAggregateId: z.string().min(1), |
| 85 | + childLoopId: z.string().min(1), |
| 86 | + childAggregateId: z.string().min(1) |
| 87 | +}); |
| 88 | + |
| 89 | +export const SignalReceivedEventSchema = BaseSchema.extend({ |
| 90 | + type: z.literal("loop.signal.received"), |
| 91 | + signalType: z.string().min(1), |
| 92 | + confidence: z.number(), |
| 93 | + triggeredLoopId: z.string().optional() |
| 94 | +}); |
| 95 | + |
| 96 | +export const OutcomeRecordedEventSchema = BaseSchema.extend({ |
| 97 | + type: z.literal("loop.outcome.recorded"), |
| 98 | + outcomeId: z.string().min(1), |
| 99 | + valueUnit: z.string().min(1), |
| 100 | + businessMetrics: z.record(z.unknown()).optional(), |
| 101 | + durationMs: z.number() |
| 102 | +}); |
| 103 | + |
| 104 | +export const LoopEventSchema = z.discriminatedUnion("type", [ |
| 105 | + LoopStartedEventSchema, |
| 106 | + TransitionRequestedEventSchema, |
| 107 | + TransitionExecutedEventSchema, |
| 108 | + TransitionBlockedEventSchema, |
| 109 | + GuardFailedEventSchema, |
| 110 | + LoopCompletedEventSchema, |
| 111 | + LoopErrorEventSchema, |
| 112 | + LoopSpawnedEventSchema, |
| 113 | + SignalReceivedEventSchema, |
| 114 | + OutcomeRecordedEventSchema |
| 115 | +]); |
0 commit comments