-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathevents.ts
More file actions
354 lines (308 loc) · 8.52 KB
/
events.ts
File metadata and controls
354 lines (308 loc) · 8.52 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
344
345
346
347
348
349
350
351
352
353
354
/**
* Moltslack Relay Event Schemas
*
* Event types for real-time communication and system notifications.
* All events are immutable and cryptographically signed.
*/
import type {
UUID,
Timestamp,
Signature,
Hash,
Agent,
AgentStatus,
Channel,
Message,
Presence,
PresenceStatus
} from './models.js';
// ============================================================================
// EVENT ENVELOPE
// ============================================================================
export enum EventCategory {
AGENT = 'agent',
CHANNEL = 'channel',
MESSAGE = 'message',
PRESENCE = 'presence',
SYSTEM = 'system',
SECURITY = 'security'
}
export interface EventMetadata {
/** Unique event identifier */
id: UUID;
/** Event category */
category: EventCategory;
/** Specific event type */
type: string;
/** Project scope */
projectId: UUID;
/** Agent that triggered the event (null for system events) */
actorId?: UUID;
/** Timestamp when event occurred */
timestamp: Timestamp;
/** Correlation ID for related events */
correlationId?: UUID;
/** Causation ID (event that caused this event) */
causationId?: UUID;
/** Event version for schema evolution */
version: number;
/** Cryptographic signature */
signature: Signature;
}
export interface RelayEvent<T = unknown> {
/** Event metadata envelope */
metadata: EventMetadata;
/** Event-specific payload */
payload: T;
}
// ============================================================================
// AGENT EVENTS
// ============================================================================
export enum AgentEventType {
SPAWNED = 'agent.spawned',
RELEASED = 'agent.released',
STATUS_CHANGED = 'agent.status_changed',
CAPABILITY_CHANGED = 'agent.capability_changed',
CREDENTIALS_ROTATED = 'agent.credentials_rotated',
METADATA_UPDATED = 'agent.metadata_updated'
}
export interface AgentSpawnedPayload {
agent: Agent;
spawnedBy: UUID;
spawnReason?: string;
}
export interface AgentReleasedPayload {
agentId: UUID;
agentName: string;
releasedBy: UUID;
releaseReason?: string;
}
export interface AgentStatusChangedPayload {
agentId: UUID;
previousStatus: AgentStatus;
newStatus: AgentStatus;
reason?: string;
}
export interface AgentCapabilityChangedPayload {
agentId: UUID;
capabilityId: string;
action: 'enabled' | 'disabled' | 'added' | 'removed';
changedBy: UUID;
}
export interface AgentCredentialsRotatedPayload {
agentId: UUID;
previousTokenHash: Hash;
newTokenHash: Hash;
rotatedBy: UUID;
}
// ============================================================================
// CHANNEL EVENTS
// ============================================================================
export enum ChannelEventType {
CREATED = 'channel.created',
DELETED = 'channel.deleted',
UPDATED = 'channel.updated',
MEMBER_JOINED = 'channel.member_joined',
MEMBER_LEFT = 'channel.member_left',
ACCESS_CHANGED = 'channel.access_changed'
}
export interface ChannelCreatedPayload {
channel: Channel;
}
export interface ChannelDeletedPayload {
channelId: UUID;
channelName: string;
deletedBy: UUID;
reason?: string;
}
export interface ChannelUpdatedPayload {
channelId: UUID;
changes: Partial<Channel>;
updatedBy: UUID;
}
export interface ChannelMemberJoinedPayload {
channelId: UUID;
agentId: UUID;
joinedAt: Timestamp;
}
export interface ChannelMemberLeftPayload {
channelId: UUID;
agentId: UUID;
leftAt: Timestamp;
reason?: 'voluntary' | 'kicked' | 'banned' | 'released';
}
export interface ChannelAccessChangedPayload {
channelId: UUID;
agentId: UUID;
previousAccess: string | null;
newAccess: string | null;
changedBy: UUID;
}
// ============================================================================
// MESSAGE EVENTS
// ============================================================================
export enum MessageEventType {
SENT = 'message.sent',
DELIVERED = 'message.delivered',
READ = 'message.read',
EDITED = 'message.edited',
DELETED = 'message.deleted',
REACTION_ADDED = 'message.reaction_added',
REACTION_REMOVED = 'message.reaction_removed'
}
export interface MessageSentPayload {
message: Message;
}
export interface MessageDeliveredPayload {
messageId: UUID;
recipientId: UUID;
deliveredAt: Timestamp;
}
export interface MessageReadPayload {
messageId: UUID;
readerId: UUID;
readAt: Timestamp;
}
export interface MessageEditedPayload {
messageId: UUID;
previousContent: string;
newContent: string;
editedBy: UUID;
editedAt: Timestamp;
}
export interface MessageDeletedPayload {
messageId: UUID;
deletedBy: UUID;
deletedAt: Timestamp;
reason?: string;
}
export interface ReactionPayload {
messageId: UUID;
agentId: UUID;
reaction: string;
timestamp: Timestamp;
}
// ============================================================================
// PRESENCE EVENTS
// ============================================================================
export enum PresenceEventType {
ONLINE = 'presence.online',
OFFLINE = 'presence.offline',
STATUS_CHANGED = 'presence.status_changed',
ACTIVITY_STARTED = 'presence.activity_started',
ACTIVITY_ENDED = 'presence.activity_ended',
TYPING_STARTED = 'presence.typing_started',
TYPING_STOPPED = 'presence.typing_stopped',
HEARTBEAT = 'presence.heartbeat'
}
export interface PresenceOnlinePayload {
presence: Presence;
}
export interface PresenceOfflinePayload {
agentId: UUID;
lastSeen: Timestamp;
reason: 'graceful' | 'timeout' | 'error' | 'kicked';
}
export interface PresenceStatusChangedPayload {
agentId: UUID;
previousStatus: PresenceStatus;
newStatus: PresenceStatus;
statusMessage?: string;
}
export interface PresenceActivityPayload {
agentId: UUID;
activityType: string;
description?: string;
contextId?: UUID;
}
export interface PresenceTypingPayload {
agentId: UUID;
channelId: UUID;
}
export interface PresenceHeartbeatPayload {
agentId: UUID;
timestamp: Timestamp;
activeChannels: UUID[];
}
// ============================================================================
// SYSTEM EVENTS
// ============================================================================
export enum SystemEventType {
PROJECT_CREATED = 'system.project_created',
PROJECT_UPDATED = 'system.project_updated',
MAINTENANCE_SCHEDULED = 'system.maintenance_scheduled',
RATE_LIMIT_EXCEEDED = 'system.rate_limit_exceeded',
ERROR = 'system.error'
}
export interface SystemErrorPayload {
code: string;
message: string;
details?: Record<string, unknown>;
affectedAgents?: UUID[];
}
// ============================================================================
// SECURITY EVENTS
// ============================================================================
export enum SecurityEventType {
AUTH_SUCCESS = 'security.auth_success',
AUTH_FAILURE = 'security.auth_failure',
TOKEN_CREATED = 'security.token_created',
TOKEN_REVOKED = 'security.token_revoked',
PERMISSION_DENIED = 'security.permission_denied',
SUSPICIOUS_ACTIVITY = 'security.suspicious_activity'
}
export interface AuthAttemptPayload {
agentId?: UUID;
tokenHash?: Hash;
ipAddress?: string;
userAgent?: string;
success: boolean;
failureReason?: string;
}
export interface PermissionDeniedPayload {
agentId: UUID;
action: string;
resourceType: string;
resourceId: UUID;
requiredPermissions: string[];
}
export interface SuspiciousActivityPayload {
agentId?: UUID;
activityType: string;
description: string;
severity: 'low' | 'medium' | 'high' | 'critical';
evidence: Record<string, unknown>;
}
// ============================================================================
// EVENT SUBSCRIPTION
// ============================================================================
export interface EventSubscription {
/** Subscription identifier */
id: UUID;
/** Subscribing agent */
agentId: UUID;
/** Event categories to subscribe to */
categories: EventCategory[];
/** Specific event types (if empty, all types in categories) */
eventTypes?: string[];
/** Filter by specific resources */
resourceFilters?: {
channelIds?: UUID[];
agentIds?: UUID[];
};
/** Delivery configuration */
delivery: {
/** Delivery method */
method: 'push' | 'poll';
/** Webhook URL for push delivery */
webhookUrl?: string;
/** Maximum batch size */
batchSize: number;
/** Maximum delivery delay in ms */
maxDelayMs: number;
};
/** Whether subscription is active */
isActive: boolean;
/** Timestamp when subscription was created */
createdAt: Timestamp;
}