@sparkleideas/shared 3.0.0-alpha.7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/README.md +323 -0
  2. package/__tests__/hooks/bash-safety.test.ts +289 -0
  3. package/__tests__/hooks/file-organization.test.ts +335 -0
  4. package/__tests__/hooks/git-commit.test.ts +336 -0
  5. package/__tests__/hooks/index.ts +23 -0
  6. package/__tests__/hooks/session-hooks.test.ts +357 -0
  7. package/__tests__/hooks/task-hooks.test.ts +193 -0
  8. package/docs/EVENTS_IMPLEMENTATION_SUMMARY.md +388 -0
  9. package/docs/EVENTS_QUICK_REFERENCE.md +470 -0
  10. package/docs/EVENTS_README.md +352 -0
  11. package/package.json +39 -0
  12. package/src/core/config/defaults.ts +207 -0
  13. package/src/core/config/index.ts +15 -0
  14. package/src/core/config/loader.ts +271 -0
  15. package/src/core/config/schema.ts +188 -0
  16. package/src/core/config/validator.ts +209 -0
  17. package/src/core/event-bus.ts +236 -0
  18. package/src/core/index.ts +22 -0
  19. package/src/core/interfaces/agent.interface.ts +251 -0
  20. package/src/core/interfaces/coordinator.interface.ts +363 -0
  21. package/src/core/interfaces/event.interface.ts +267 -0
  22. package/src/core/interfaces/index.ts +19 -0
  23. package/src/core/interfaces/memory.interface.ts +332 -0
  24. package/src/core/interfaces/task.interface.ts +223 -0
  25. package/src/core/orchestrator/event-coordinator.ts +122 -0
  26. package/src/core/orchestrator/health-monitor.ts +214 -0
  27. package/src/core/orchestrator/index.ts +89 -0
  28. package/src/core/orchestrator/lifecycle-manager.ts +263 -0
  29. package/src/core/orchestrator/session-manager.ts +279 -0
  30. package/src/core/orchestrator/task-manager.ts +317 -0
  31. package/src/events/domain-events.ts +584 -0
  32. package/src/events/event-store.test.ts +387 -0
  33. package/src/events/event-store.ts +588 -0
  34. package/src/events/example-usage.ts +293 -0
  35. package/src/events/index.ts +90 -0
  36. package/src/events/projections.ts +561 -0
  37. package/src/events/state-reconstructor.ts +349 -0
  38. package/src/events.ts +367 -0
  39. package/src/hooks/INTEGRATION.md +658 -0
  40. package/src/hooks/README.md +532 -0
  41. package/src/hooks/example-usage.ts +499 -0
  42. package/src/hooks/executor.ts +379 -0
  43. package/src/hooks/hooks.test.ts +421 -0
  44. package/src/hooks/index.ts +131 -0
  45. package/src/hooks/registry.ts +333 -0
  46. package/src/hooks/safety/bash-safety.ts +604 -0
  47. package/src/hooks/safety/file-organization.ts +473 -0
  48. package/src/hooks/safety/git-commit.ts +623 -0
  49. package/src/hooks/safety/index.ts +46 -0
  50. package/src/hooks/session-hooks.ts +559 -0
  51. package/src/hooks/task-hooks.ts +513 -0
  52. package/src/hooks/types.ts +357 -0
  53. package/src/hooks/verify-exports.test.ts +125 -0
  54. package/src/index.ts +195 -0
  55. package/src/mcp/connection-pool.ts +438 -0
  56. package/src/mcp/index.ts +183 -0
  57. package/src/mcp/server.ts +774 -0
  58. package/src/mcp/session-manager.ts +428 -0
  59. package/src/mcp/tool-registry.ts +566 -0
  60. package/src/mcp/transport/http.ts +557 -0
  61. package/src/mcp/transport/index.ts +294 -0
  62. package/src/mcp/transport/stdio.ts +324 -0
  63. package/src/mcp/transport/websocket.ts +484 -0
  64. package/src/mcp/types.ts +565 -0
  65. package/src/plugin-interface.ts +663 -0
  66. package/src/plugin-loader.ts +638 -0
  67. package/src/plugin-registry.ts +604 -0
  68. package/src/plugins/index.ts +34 -0
  69. package/src/plugins/official/hive-mind-plugin.ts +330 -0
  70. package/src/plugins/official/index.ts +24 -0
  71. package/src/plugins/official/maestro-plugin.ts +508 -0
  72. package/src/plugins/types.ts +108 -0
  73. package/src/resilience/bulkhead.ts +277 -0
  74. package/src/resilience/circuit-breaker.ts +326 -0
  75. package/src/resilience/index.ts +26 -0
  76. package/src/resilience/rate-limiter.ts +420 -0
  77. package/src/resilience/retry.ts +224 -0
  78. package/src/security/index.ts +39 -0
  79. package/src/security/input-validation.ts +265 -0
  80. package/src/security/secure-random.ts +159 -0
  81. package/src/services/index.ts +16 -0
  82. package/src/services/v3-progress.service.ts +505 -0
  83. package/src/types/agent.types.ts +144 -0
  84. package/src/types/index.ts +22 -0
  85. package/src/types/mcp.types.ts +300 -0
  86. package/src/types/memory.types.ts +263 -0
  87. package/src/types/swarm.types.ts +255 -0
  88. package/src/types/task.types.ts +205 -0
  89. package/src/types.ts +367 -0
  90. package/src/utils/secure-logger.d.ts +69 -0
  91. package/src/utils/secure-logger.d.ts.map +1 -0
  92. package/src/utils/secure-logger.js +208 -0
  93. package/src/utils/secure-logger.js.map +1 -0
  94. package/src/utils/secure-logger.ts +257 -0
  95. package/tmp.json +0 -0
  96. package/tsconfig.json +9 -0
@@ -0,0 +1,565 @@
1
+ /**
2
+ * V3 MCP Types and Interfaces
3
+ *
4
+ * Optimized type definitions for the V3 MCP server with:
5
+ * - Strict typing for performance
6
+ * - Connection pooling types
7
+ * - Transport layer abstractions
8
+ * - Tool registry interfaces
9
+ *
10
+ * Performance Targets:
11
+ * - Server startup: <400ms
12
+ * - Tool registration: <10ms
13
+ * - Tool execution: <50ms overhead
14
+ */
15
+
16
+ // ============================================================================
17
+ // Core Protocol Types
18
+ // ============================================================================
19
+
20
+ /**
21
+ * JSON-RPC 2.0 Protocol Version
22
+ */
23
+ export type JsonRpcVersion = '2.0';
24
+
25
+ /**
26
+ * MCP Protocol Version
27
+ */
28
+ export interface MCPProtocolVersion {
29
+ major: number;
30
+ minor: number;
31
+ patch: number;
32
+ }
33
+
34
+ /**
35
+ * MCP Request ID (can be string, number, or null)
36
+ */
37
+ export type RequestId = string | number | null;
38
+
39
+ /**
40
+ * Base MCP Message
41
+ */
42
+ export interface MCPMessage {
43
+ jsonrpc: JsonRpcVersion;
44
+ }
45
+
46
+ /**
47
+ * MCP Request
48
+ */
49
+ export interface MCPRequest extends MCPMessage {
50
+ id: RequestId;
51
+ method: string;
52
+ params?: Record<string, unknown>;
53
+ }
54
+
55
+ /**
56
+ * MCP Notification (request without id)
57
+ */
58
+ export interface MCPNotification extends MCPMessage {
59
+ method: string;
60
+ params?: Record<string, unknown>;
61
+ }
62
+
63
+ /**
64
+ * MCP Error
65
+ */
66
+ export interface MCPError {
67
+ code: number;
68
+ message: string;
69
+ data?: unknown;
70
+ }
71
+
72
+ /**
73
+ * MCP Response
74
+ */
75
+ export interface MCPResponse extends MCPMessage {
76
+ id: RequestId;
77
+ result?: unknown;
78
+ error?: MCPError;
79
+ }
80
+
81
+ // ============================================================================
82
+ // Server Configuration
83
+ // ============================================================================
84
+
85
+ /**
86
+ * Transport type options
87
+ */
88
+ export type TransportType = 'stdio' | 'http' | 'websocket' | 'in-process';
89
+
90
+ /**
91
+ * Authentication method
92
+ */
93
+ export type AuthMethod = 'token' | 'oauth' | 'api-key' | 'none';
94
+
95
+ /**
96
+ * Authentication configuration
97
+ */
98
+ export interface AuthConfig {
99
+ enabled: boolean;
100
+ method: AuthMethod;
101
+ tokens?: string[];
102
+ apiKeys?: string[];
103
+ jwtSecret?: string;
104
+ oauth?: {
105
+ clientId: string;
106
+ clientSecret: string;
107
+ authorizationUrl: string;
108
+ tokenUrl: string;
109
+ };
110
+ }
111
+
112
+ /**
113
+ * Load balancer configuration
114
+ */
115
+ export interface LoadBalancerConfig {
116
+ enabled: boolean;
117
+ maxConcurrentRequests: number;
118
+ rateLimit?: {
119
+ requestsPerSecond: number;
120
+ burstSize: number;
121
+ };
122
+ circuitBreaker?: {
123
+ failureThreshold: number;
124
+ resetTimeout: number;
125
+ };
126
+ }
127
+
128
+ /**
129
+ * Connection pool configuration
130
+ */
131
+ export interface ConnectionPoolConfig {
132
+ maxConnections: number;
133
+ minConnections: number;
134
+ idleTimeout: number;
135
+ acquireTimeout: number;
136
+ maxWaitingClients: number;
137
+ evictionRunInterval: number;
138
+ }
139
+
140
+ /**
141
+ * MCP Server configuration
142
+ */
143
+ export interface MCPServerConfig {
144
+ name: string;
145
+ version: string;
146
+ transport: TransportType;
147
+ host?: string;
148
+ port?: number;
149
+ tlsEnabled?: boolean;
150
+ tlsCert?: string;
151
+ tlsKey?: string;
152
+ auth?: AuthConfig;
153
+ loadBalancer?: LoadBalancerConfig;
154
+ connectionPool?: ConnectionPoolConfig;
155
+ corsEnabled?: boolean;
156
+ corsOrigins?: string[];
157
+ maxRequestSize?: number;
158
+ requestTimeout?: number;
159
+ enableMetrics?: boolean;
160
+ enableCaching?: boolean;
161
+ cacheTTL?: number;
162
+ logLevel?: 'debug' | 'info' | 'warn' | 'error';
163
+ }
164
+
165
+ // ============================================================================
166
+ // Session Types
167
+ // ============================================================================
168
+
169
+ /**
170
+ * MCP Session state
171
+ */
172
+ export type SessionState = 'created' | 'initializing' | 'ready' | 'closing' | 'closed' | 'error';
173
+
174
+ /**
175
+ * MCP Session
176
+ */
177
+ export interface MCPSession {
178
+ id: string;
179
+ state: SessionState;
180
+ transport: TransportType;
181
+ createdAt: Date;
182
+ lastActivityAt: Date;
183
+ isInitialized: boolean;
184
+ isAuthenticated: boolean;
185
+ clientInfo?: MCPClientInfo;
186
+ protocolVersion?: MCPProtocolVersion;
187
+ capabilities?: MCPCapabilities;
188
+ metadata?: Record<string, unknown>;
189
+ }
190
+
191
+ /**
192
+ * Client information from initialization
193
+ */
194
+ export interface MCPClientInfo {
195
+ name: string;
196
+ version: string;
197
+ }
198
+
199
+ // ============================================================================
200
+ // Capability Types
201
+ // ============================================================================
202
+
203
+ /**
204
+ * MCP Capabilities
205
+ */
206
+ export interface MCPCapabilities {
207
+ logging?: {
208
+ level: 'debug' | 'info' | 'warn' | 'error';
209
+ };
210
+ tools?: {
211
+ listChanged: boolean;
212
+ };
213
+ resources?: {
214
+ listChanged: boolean;
215
+ subscribe: boolean;
216
+ };
217
+ prompts?: {
218
+ listChanged: boolean;
219
+ };
220
+ experimental?: Record<string, unknown>;
221
+ }
222
+
223
+ /**
224
+ * Initialize request parameters
225
+ */
226
+ export interface MCPInitializeParams {
227
+ protocolVersion: MCPProtocolVersion;
228
+ capabilities: MCPCapabilities;
229
+ clientInfo: MCPClientInfo;
230
+ }
231
+
232
+ /**
233
+ * Initialize response result
234
+ */
235
+ export interface MCPInitializeResult {
236
+ protocolVersion: MCPProtocolVersion;
237
+ capabilities: MCPCapabilities;
238
+ serverInfo: {
239
+ name: string;
240
+ version: string;
241
+ };
242
+ instructions?: string;
243
+ }
244
+
245
+ // ============================================================================
246
+ // Tool Types
247
+ // ============================================================================
248
+
249
+ /**
250
+ * JSON Schema type for tool input
251
+ */
252
+ export interface JSONSchema {
253
+ type: string;
254
+ properties?: Record<string, JSONSchema>;
255
+ required?: string[];
256
+ items?: JSONSchema;
257
+ enum?: string[];
258
+ description?: string;
259
+ default?: unknown;
260
+ minimum?: number;
261
+ maximum?: number;
262
+ minLength?: number;
263
+ maxLength?: number;
264
+ pattern?: string;
265
+ additionalProperties?: boolean | JSONSchema;
266
+ }
267
+
268
+ /**
269
+ * Tool execution context
270
+ */
271
+ export interface ToolContext {
272
+ sessionId: string;
273
+ requestId?: RequestId;
274
+ orchestrator?: unknown;
275
+ swarmCoordinator?: unknown;
276
+ agentManager?: unknown;
277
+ resourceManager?: unknown;
278
+ messageBus?: unknown;
279
+ monitor?: unknown;
280
+ metadata?: Record<string, unknown>;
281
+ }
282
+
283
+ /**
284
+ * Tool handler function type
285
+ */
286
+ export type ToolHandler<TInput = unknown, TOutput = unknown> = (
287
+ input: TInput,
288
+ context?: ToolContext
289
+ ) => Promise<TOutput>;
290
+
291
+ /**
292
+ * MCP Tool definition
293
+ */
294
+ export interface MCPTool<TInput = unknown, TOutput = unknown> {
295
+ name: string;
296
+ description: string;
297
+ inputSchema: JSONSchema;
298
+ handler: ToolHandler<TInput, TOutput>;
299
+ category?: string;
300
+ tags?: string[];
301
+ version?: string;
302
+ deprecated?: boolean;
303
+ cacheable?: boolean;
304
+ cacheTTL?: number;
305
+ timeout?: number;
306
+ }
307
+
308
+ /**
309
+ * Tool call result
310
+ */
311
+ export interface ToolCallResult {
312
+ content: Array<{
313
+ type: 'text' | 'image' | 'resource';
314
+ text?: string;
315
+ data?: string;
316
+ mimeType?: string;
317
+ }>;
318
+ isError: boolean;
319
+ }
320
+
321
+ /**
322
+ * Tool registration options
323
+ */
324
+ export interface ToolRegistrationOptions {
325
+ override?: boolean;
326
+ validate?: boolean;
327
+ preload?: boolean;
328
+ }
329
+
330
+ // ============================================================================
331
+ // Transport Types
332
+ // ============================================================================
333
+
334
+ /**
335
+ * Request handler function type
336
+ */
337
+ export type RequestHandler = (request: MCPRequest) => Promise<MCPResponse>;
338
+
339
+ /**
340
+ * Notification handler function type
341
+ */
342
+ export type NotificationHandler = (notification: MCPNotification) => Promise<void>;
343
+
344
+ /**
345
+ * Transport health status
346
+ */
347
+ export interface TransportHealthStatus {
348
+ healthy: boolean;
349
+ error?: string;
350
+ metrics?: Record<string, number>;
351
+ }
352
+
353
+ /**
354
+ * Transport interface
355
+ */
356
+ export interface ITransport {
357
+ readonly type: TransportType;
358
+
359
+ start(): Promise<void>;
360
+ stop(): Promise<void>;
361
+
362
+ onRequest(handler: RequestHandler): void;
363
+ onNotification(handler: NotificationHandler): void;
364
+
365
+ sendNotification?(notification: MCPNotification): Promise<void>;
366
+
367
+ getHealthStatus(): Promise<TransportHealthStatus>;
368
+ }
369
+
370
+ // ============================================================================
371
+ // Connection Pool Types
372
+ // ============================================================================
373
+
374
+ /**
375
+ * Connection state
376
+ */
377
+ export type ConnectionState = 'idle' | 'busy' | 'closing' | 'closed' | 'error';
378
+
379
+ /**
380
+ * Pooled connection
381
+ */
382
+ export interface PooledConnection {
383
+ id: string;
384
+ state: ConnectionState;
385
+ createdAt: Date;
386
+ lastUsedAt: Date;
387
+ useCount: number;
388
+ transport: TransportType;
389
+ metadata?: Record<string, unknown>;
390
+ }
391
+
392
+ /**
393
+ * Connection pool statistics
394
+ */
395
+ export interface ConnectionPoolStats {
396
+ totalConnections: number;
397
+ idleConnections: number;
398
+ busyConnections: number;
399
+ pendingRequests: number;
400
+ totalAcquired: number;
401
+ totalReleased: number;
402
+ totalCreated: number;
403
+ totalDestroyed: number;
404
+ avgAcquireTime: number;
405
+ }
406
+
407
+ /**
408
+ * Connection pool interface
409
+ */
410
+ export interface IConnectionPool {
411
+ acquire(): Promise<PooledConnection>;
412
+ release(connection: PooledConnection): void;
413
+ destroy(connection: PooledConnection): void;
414
+
415
+ getStats(): ConnectionPoolStats;
416
+
417
+ drain(): Promise<void>;
418
+ clear(): Promise<void>;
419
+ }
420
+
421
+ // ============================================================================
422
+ // Metrics Types
423
+ // ============================================================================
424
+
425
+ /**
426
+ * Tool call metrics
427
+ */
428
+ export interface ToolCallMetrics {
429
+ toolName: string;
430
+ duration: number;
431
+ success: boolean;
432
+ timestamp: number;
433
+ transport: TransportType;
434
+ cached?: boolean;
435
+ }
436
+
437
+ /**
438
+ * MCP Server metrics
439
+ */
440
+ export interface MCPServerMetrics {
441
+ totalRequests: number;
442
+ successfulRequests: number;
443
+ failedRequests: number;
444
+ averageResponseTime: number;
445
+ activeSessions: number;
446
+ toolInvocations: Record<string, number>;
447
+ errors: Record<string, number>;
448
+ lastReset: Date;
449
+ startupTime?: number;
450
+ uptime?: number;
451
+ }
452
+
453
+ /**
454
+ * Session metrics
455
+ */
456
+ export interface SessionMetrics {
457
+ total: number;
458
+ active: number;
459
+ authenticated: number;
460
+ expired: number;
461
+ }
462
+
463
+ // ============================================================================
464
+ // Event Types
465
+ // ============================================================================
466
+
467
+ /**
468
+ * MCP Event types
469
+ */
470
+ export type MCPEventType =
471
+ | 'server:started'
472
+ | 'server:stopped'
473
+ | 'server:error'
474
+ | 'session:created'
475
+ | 'session:initialized'
476
+ | 'session:closed'
477
+ | 'session:error'
478
+ | 'tool:registered'
479
+ | 'tool:unregistered'
480
+ | 'tool:called'
481
+ | 'tool:completed'
482
+ | 'tool:error'
483
+ | 'transport:connected'
484
+ | 'transport:disconnected'
485
+ | 'transport:error'
486
+ | 'pool:connection:acquired'
487
+ | 'pool:connection:released'
488
+ | 'pool:connection:created'
489
+ | 'pool:connection:destroyed';
490
+
491
+ /**
492
+ * MCP Event
493
+ */
494
+ export interface MCPEvent {
495
+ type: MCPEventType;
496
+ timestamp: Date;
497
+ data?: unknown;
498
+ }
499
+
500
+ /**
501
+ * Event handler function type
502
+ */
503
+ export type EventHandler = (event: MCPEvent) => void;
504
+
505
+ // ============================================================================
506
+ // Logger Interface
507
+ // ============================================================================
508
+
509
+ /**
510
+ * Log level
511
+ */
512
+ export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
513
+
514
+ /**
515
+ * Logger interface
516
+ */
517
+ export interface ILogger {
518
+ debug(message: string, data?: unknown): void;
519
+ info(message: string, data?: unknown): void;
520
+ warn(message: string, data?: unknown): void;
521
+ error(message: string, data?: unknown): void;
522
+ }
523
+
524
+ // ============================================================================
525
+ // Error Codes
526
+ // ============================================================================
527
+
528
+ /**
529
+ * Standard JSON-RPC error codes
530
+ */
531
+ export const ErrorCodes = {
532
+ PARSE_ERROR: -32700,
533
+ INVALID_REQUEST: -32600,
534
+ METHOD_NOT_FOUND: -32601,
535
+ INVALID_PARAMS: -32602,
536
+ INTERNAL_ERROR: -32603,
537
+ SERVER_NOT_INITIALIZED: -32002,
538
+ UNKNOWN_ERROR: -32001,
539
+ REQUEST_CANCELLED: -32800,
540
+ RATE_LIMITED: -32000,
541
+ AUTHENTICATION_REQUIRED: -32001,
542
+ AUTHORIZATION_FAILED: -32002,
543
+ } as const;
544
+
545
+ /**
546
+ * MCP Error class
547
+ */
548
+ export class MCPServerError extends Error {
549
+ constructor(
550
+ message: string,
551
+ public code: number = ErrorCodes.INTERNAL_ERROR,
552
+ public data?: unknown
553
+ ) {
554
+ super(message);
555
+ this.name = 'MCPServerError';
556
+ }
557
+
558
+ toMCPError(): MCPError {
559
+ return {
560
+ code: this.code,
561
+ message: this.message,
562
+ data: this.data,
563
+ };
564
+ }
565
+ }