-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathplugin-lifecycle-events.zod.ts
More file actions
343 lines (298 loc) · 8.65 KB
/
plugin-lifecycle-events.zod.ts
File metadata and controls
343 lines (298 loc) · 8.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { z } from 'zod';
/**
* Plugin Lifecycle Events Protocol
*
* Zod schemas for plugin lifecycle event data structures.
* These schemas align with the IPluginLifecycleEvents contract interface.
*
* Following ObjectStack "Zod First" principle - all data structures
* must have Zod schemas for runtime validation and JSON Schema generation.
*/
// ============================================================================
// Event Payload Schemas
// ============================================================================
/**
* Event Phase Enum
* Lifecycle phase where an error occurred
*/
export const EventPhaseSchema = z.enum(['init', 'start', 'destroy'])
.describe('Plugin lifecycle phase');
export type EventPhase = z.infer<typeof EventPhaseSchema>;
/**
* Plugin Event Base Schema
* Common fields for all plugin events
*/
export const PluginEventBaseSchema = z.object({
/**
* Plugin name
*/
pluginName: z.string().describe('Name of the plugin'),
/**
* Event timestamp (Unix milliseconds)
*/
timestamp: z.number().int().describe('Unix timestamp in milliseconds when event occurred'),
});
/**
* Plugin Registered Event Schema
*
* @example
* {
* "pluginName": "crm-plugin",
* "timestamp": 1706659200000,
* "version": "1.0.0"
* }
*/
export const PluginRegisteredEventSchema = PluginEventBaseSchema.extend({
/**
* Plugin version (optional)
*/
version: z.string().optional().describe('Plugin version'),
});
export type PluginRegisteredEvent = z.infer<typeof PluginRegisteredEventSchema>;
/**
* Plugin Lifecycle Phase Event Schema
* For init, start, destroy phases
*
* @example
* {
* "pluginName": "crm-plugin",
* "timestamp": 1706659200000,
* "duration": 1250,
* "phase": "init"
* }
*/
export const PluginLifecyclePhaseEventSchema = PluginEventBaseSchema.extend({
/**
* Duration of the phase (milliseconds)
*/
duration: z.number().min(0).optional().describe('Duration of the lifecycle phase in milliseconds'),
/**
* Lifecycle phase
*/
phase: EventPhaseSchema.optional().describe('Lifecycle phase'),
});
export type PluginLifecyclePhaseEvent = z.infer<typeof PluginLifecyclePhaseEventSchema>;
/**
* Plugin Error Event Schema
* When a plugin encounters an error
*
* @example
* {
* "pluginName": "crm-plugin",
* "timestamp": 1706659200000,
* "error": Error("Connection failed"),
* "phase": "start",
* "errorMessage": "Connection failed",
* "errorStack": "Error: Connection failed\n at ..."
* }
*/
export const PluginErrorEventSchema = PluginEventBaseSchema.extend({
/**
* Error object
*/
error: z.object({
name: z.string().describe('Error class name'),
message: z.string().describe('Error message'),
stack: z.string().optional().describe('Stack trace'),
code: z.string().optional().describe('Error code'),
}).describe('Serializable error representation'),
/**
* Lifecycle phase where error occurred
*/
phase: EventPhaseSchema.describe('Lifecycle phase where error occurred'),
/**
* Error message (for serialization)
*/
errorMessage: z.string().optional().describe('Error message'),
/**
* Error stack trace (for debugging)
*/
errorStack: z.string().optional().describe('Error stack trace'),
});
export type PluginErrorEvent = z.infer<typeof PluginErrorEventSchema>;
// ============================================================================
// Service Event Schemas
// ============================================================================
/**
* Service Registered Event Schema
*
* @example
* {
* "serviceName": "database",
* "timestamp": 1706659200000,
* "serviceType": "IDataEngine"
* }
*/
export const ServiceRegisteredEventSchema = z.object({
/**
* Service name
*/
serviceName: z.string().describe('Name of the registered service'),
/**
* Event timestamp (Unix milliseconds)
*/
timestamp: z.number().int().describe('Unix timestamp in milliseconds'),
/**
* Service type (optional)
*/
serviceType: z.string().optional().describe('Type or interface name of the service'),
});
export type ServiceRegisteredEvent = z.infer<typeof ServiceRegisteredEventSchema>;
/**
* Service Unregistered Event Schema
*
* @example
* {
* "serviceName": "database",
* "timestamp": 1706659200000
* }
*/
export const ServiceUnregisteredEventSchema = z.object({
/**
* Service name
*/
serviceName: z.string().describe('Name of the unregistered service'),
/**
* Event timestamp (Unix milliseconds)
*/
timestamp: z.number().int().describe('Unix timestamp in milliseconds'),
});
export type ServiceUnregisteredEvent = z.infer<typeof ServiceUnregisteredEventSchema>;
// ============================================================================
// Hook Event Schemas
// ============================================================================
/**
* Hook Registered Event Schema
*
* @example
* {
* "hookName": "data.beforeInsert",
* "timestamp": 1706659200000,
* "handlerCount": 3
* }
*/
export const HookRegisteredEventSchema = z.object({
/**
* Hook name
*/
hookName: z.string().describe('Name of the hook'),
/**
* Event timestamp (Unix milliseconds)
*/
timestamp: z.number().int().describe('Unix timestamp in milliseconds'),
/**
* Number of handlers registered for this hook
*/
handlerCount: z.number().int().min(0).describe('Number of handlers registered for this hook'),
});
export type HookRegisteredEvent = z.infer<typeof HookRegisteredEventSchema>;
/**
* Hook Triggered Event Schema
*
* @example
* {
* "hookName": "data.beforeInsert",
* "timestamp": 1706659200000,
* "args": [{ "object": "customer", "data": {...} }],
* "handlerCount": 3
* }
*/
export const HookTriggeredEventSchema = z.object({
/**
* Hook name
*/
hookName: z.string().describe('Name of the hook'),
/**
* Event timestamp (Unix milliseconds)
*/
timestamp: z.number().int().describe('Unix timestamp in milliseconds'),
/**
* Arguments passed to the hook
*/
args: z.array(z.unknown()).describe('Arguments passed to the hook handlers'),
/**
* Number of handlers that will handle this event
*/
handlerCount: z.number().int().min(0).optional().describe('Number of handlers that will handle this event'),
});
export type HookTriggeredEvent = z.infer<typeof HookTriggeredEventSchema>;
// ============================================================================
// Kernel Event Schemas
// ============================================================================
/**
* Kernel Event Base Schema
* Common fields for kernel events
*/
export const KernelEventBaseSchema = z.object({
/**
* Event timestamp (Unix milliseconds)
*/
timestamp: z.number().int().describe('Unix timestamp in milliseconds'),
});
/**
* Kernel Ready Event Schema
*
* @example
* {
* "timestamp": 1706659200000,
* "duration": 5400,
* "pluginCount": 12
* }
*/
export const KernelReadyEventSchema = KernelEventBaseSchema.extend({
/**
* Total initialization duration (milliseconds)
*/
duration: z.number().min(0).optional().describe('Total initialization duration in milliseconds'),
/**
* Number of plugins initialized
*/
pluginCount: z.number().int().min(0).optional().describe('Number of plugins initialized'),
});
export type KernelReadyEvent = z.infer<typeof KernelReadyEventSchema>;
/**
* Kernel Shutdown Event Schema
*
* @example
* {
* "timestamp": 1706659200000,
* "reason": "SIGTERM received"
* }
*/
export const KernelShutdownEventSchema = KernelEventBaseSchema.extend({
/**
* Shutdown reason (optional)
*/
reason: z.string().optional().describe('Reason for kernel shutdown'),
});
export type KernelShutdownEvent = z.infer<typeof KernelShutdownEventSchema>;
// ============================================================================
// Event Type Registry
// ============================================================================
/**
* Plugin Lifecycle Event Type Enum
* All possible plugin lifecycle event types
*/
export const PluginLifecycleEventType = z.enum([
'kernel:ready',
'kernel:shutdown',
'kernel:before-init',
'kernel:after-init',
'plugin:registered',
'plugin:before-init',
'plugin:init',
'plugin:after-init',
'plugin:before-start',
'plugin:started',
'plugin:after-start',
'plugin:before-destroy',
'plugin:destroyed',
'plugin:after-destroy',
'plugin:error',
'service:registered',
'service:unregistered',
'hook:registered',
'hook:triggered',
]).describe('Plugin lifecycle event type');
export type PluginLifecycleEventType = z.infer<typeof PluginLifecycleEventType>;