@lov3kaizen/agentsea-crews 0.5.1

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.
@@ -0,0 +1,587 @@
1
+ // src/monitoring/CrewDashboard.ts
2
+ import { EventEmitter } from "eventemitter3";
3
+ var CrewDashboard = class extends EventEmitter {
4
+ crew;
5
+ config;
6
+ events = [];
7
+ updateTimer;
8
+ lastStatus;
9
+ subscribed = false;
10
+ constructor(crew, config = {}) {
11
+ super();
12
+ this.crew = crew;
13
+ this.config = {
14
+ updateInterval: config.updateInterval ?? 1e3,
15
+ trackEvents: config.trackEvents ?? true,
16
+ maxEvents: config.maxEvents ?? 1e3
17
+ };
18
+ }
19
+ /**
20
+ * Start monitoring
21
+ */
22
+ start() {
23
+ if (this.subscribed) return;
24
+ this.subscribed = true;
25
+ this.updateTimer = setInterval(() => {
26
+ this.emitUpdate();
27
+ }, this.config.updateInterval);
28
+ this.emitUpdate();
29
+ }
30
+ /**
31
+ * Stop monitoring
32
+ */
33
+ stop() {
34
+ if (this.updateTimer) {
35
+ clearInterval(this.updateTimer);
36
+ this.updateTimer = void 0;
37
+ }
38
+ this.subscribed = false;
39
+ }
40
+ /**
41
+ * Subscribe to updates
42
+ */
43
+ subscribe(callback) {
44
+ this.on("update", callback);
45
+ if (!this.subscribed) {
46
+ this.start();
47
+ }
48
+ return () => {
49
+ this.off("update", callback);
50
+ };
51
+ }
52
+ /**
53
+ * Record an event
54
+ */
55
+ recordEvent(event) {
56
+ if (!this.config.trackEvents) return;
57
+ this.events.push(event);
58
+ if (this.events.length > this.config.maxEvents) {
59
+ this.events.shift();
60
+ }
61
+ this.emit("update", {
62
+ type: "event",
63
+ timestamp: /* @__PURE__ */ new Date(),
64
+ data: event
65
+ });
66
+ }
67
+ // ============ Status Getters ============
68
+ /**
69
+ * Get current crew status
70
+ */
71
+ getCrewStatus() {
72
+ return this.crew.getStatus();
73
+ }
74
+ /**
75
+ * Get agent statuses
76
+ */
77
+ getAgentStatuses() {
78
+ const statuses = /* @__PURE__ */ new Map();
79
+ for (const agent of this.crew.getAgents()) {
80
+ const stats = agent.getStats();
81
+ statuses.set(agent.name, {
82
+ name: agent.name,
83
+ role: agent.role.name,
84
+ status: stats.isBusy ? "busy" : "idle",
85
+ currentTask: stats.currentTask,
86
+ tasksCompleted: stats.tasksCompleted,
87
+ tasksFailed: stats.tasksFailed,
88
+ tokensUsed: stats.totalTokensUsed
89
+ });
90
+ }
91
+ return statuses;
92
+ }
93
+ /**
94
+ * Get crew metrics
95
+ */
96
+ getMetrics() {
97
+ return this.crew.getMetrics();
98
+ }
99
+ /**
100
+ * Get timeline
101
+ */
102
+ getTimeline() {
103
+ return this.crew.getTimeline();
104
+ }
105
+ /**
106
+ * Get events
107
+ */
108
+ getEvents(filter) {
109
+ let filtered = [...this.events];
110
+ if (filter?.type) {
111
+ filtered = filtered.filter((e) => e.type === filter.type);
112
+ }
113
+ if (filter?.agent) {
114
+ filtered = filtered.filter((e) => {
115
+ const anyEvent = e;
116
+ return anyEvent.agentName === filter.agent;
117
+ });
118
+ }
119
+ if (filter?.since) {
120
+ filtered = filtered.filter((e) => {
121
+ return e.timestamp >= filter.since;
122
+ });
123
+ }
124
+ if (filter?.limit) {
125
+ filtered = filtered.slice(-filter.limit);
126
+ }
127
+ return filtered;
128
+ }
129
+ // ============ Progress Tracking ============
130
+ /**
131
+ * Get progress summary
132
+ */
133
+ getProgress() {
134
+ const status = this.getCrewStatus();
135
+ const metrics = this.getMetrics();
136
+ const total = status.tasksPending + status.tasksInProgress + status.tasksCompleted + status.tasksFailed;
137
+ const completed = status.tasksCompleted;
138
+ return {
139
+ percentage: total > 0 ? completed / total * 100 : 0,
140
+ completedTasks: completed,
141
+ totalTasks: total,
142
+ elapsedTime: metrics.totalExecutionTimeMs
143
+ };
144
+ }
145
+ /**
146
+ * Get task breakdown
147
+ */
148
+ getTaskBreakdown() {
149
+ const status = this.getCrewStatus();
150
+ return {
151
+ pending: status.tasksPending,
152
+ inProgress: status.tasksInProgress,
153
+ completed: status.tasksCompleted,
154
+ failed: status.tasksFailed,
155
+ total: status.tasksPending + status.tasksInProgress + status.tasksCompleted + status.tasksFailed
156
+ };
157
+ }
158
+ /**
159
+ * Get agent breakdown
160
+ */
161
+ getAgentBreakdown() {
162
+ const agents = this.crew.getAgents();
163
+ const performance = agents.map((agent) => {
164
+ const stats = agent.getStats();
165
+ return {
166
+ name: agent.name,
167
+ completed: stats.tasksCompleted,
168
+ failed: stats.tasksFailed,
169
+ successRate: stats.successRate
170
+ };
171
+ });
172
+ const status = this.getCrewStatus();
173
+ return {
174
+ total: agents.length,
175
+ busy: status.agentsBusy,
176
+ idle: status.agentsAvailable,
177
+ performance
178
+ };
179
+ }
180
+ // ============ Internal ============
181
+ /**
182
+ * Emit an update
183
+ */
184
+ emitUpdate() {
185
+ const currentStatus = this.getCrewStatus();
186
+ if (this.lastStatus && this.lastStatus.state !== currentStatus.state) {
187
+ this.emit("statusChange", currentStatus);
188
+ }
189
+ this.lastStatus = currentStatus;
190
+ this.emit("update", {
191
+ type: "metrics_update",
192
+ timestamp: /* @__PURE__ */ new Date(),
193
+ data: {
194
+ status: currentStatus,
195
+ metrics: this.getMetrics(),
196
+ agents: Object.fromEntries(this.getAgentStatuses())
197
+ }
198
+ });
199
+ }
200
+ /**
201
+ * Get dashboard snapshot
202
+ */
203
+ getSnapshot() {
204
+ return {
205
+ timestamp: /* @__PURE__ */ new Date(),
206
+ status: this.getCrewStatus(),
207
+ metrics: this.getMetrics(),
208
+ agents: Object.fromEntries(this.getAgentStatuses()),
209
+ progress: this.getProgress(),
210
+ taskBreakdown: this.getTaskBreakdown(),
211
+ agentBreakdown: this.getAgentBreakdown(),
212
+ recentEvents: this.events.slice(-10)
213
+ };
214
+ }
215
+ };
216
+ function createDashboard(crew, config) {
217
+ return new CrewDashboard(crew, config);
218
+ }
219
+
220
+ // src/monitoring/DebugMode.ts
221
+ import { EventEmitter as EventEmitter2 } from "eventemitter3";
222
+ var DebugMode = class extends EventEmitter2 {
223
+ crew;
224
+ config;
225
+ breakpoints = /* @__PURE__ */ new Map();
226
+ eventQueue = [];
227
+ callStack = [];
228
+ context;
229
+ enabled = false;
230
+ paused = false;
231
+ stepMode = false;
232
+ breakpointCounter = 0;
233
+ resolveStep;
234
+ constructor(crew, config = {}) {
235
+ super();
236
+ this.crew = crew;
237
+ this.config = {
238
+ pauseOnError: config.pauseOnError ?? true,
239
+ verbose: config.verbose ?? false,
240
+ maxCallStackDepth: config.maxCallStackDepth ?? 100
241
+ };
242
+ this.context = this.createContext();
243
+ }
244
+ // ============ Enable/Disable ============
245
+ /**
246
+ * Enable debug mode
247
+ */
248
+ enable() {
249
+ if (this.enabled) return;
250
+ this.enabled = true;
251
+ this.paused = false;
252
+ if (this.config.verbose) {
253
+ console.log("[Debug] Debug mode enabled");
254
+ }
255
+ }
256
+ /**
257
+ * Disable debug mode
258
+ */
259
+ disable() {
260
+ this.enabled = false;
261
+ this.paused = false;
262
+ this.stepMode = false;
263
+ this.breakpoints.clear();
264
+ this.eventQueue.length = 0;
265
+ this.callStack.length = 0;
266
+ if (this.resolveStep) {
267
+ this.resolveStep();
268
+ this.resolveStep = void 0;
269
+ }
270
+ if (this.config.verbose) {
271
+ console.log("[Debug] Debug mode disabled");
272
+ }
273
+ }
274
+ /**
275
+ * Check if debug mode is enabled
276
+ */
277
+ isEnabled() {
278
+ return this.enabled;
279
+ }
280
+ // ============ Breakpoints ============
281
+ /**
282
+ * Set a breakpoint
283
+ */
284
+ setBreakpoint(type, condition) {
285
+ const id = `bp-${++this.breakpointCounter}`;
286
+ const breakpoint = {
287
+ id,
288
+ type,
289
+ condition,
290
+ enabled: true,
291
+ hitCount: 0
292
+ };
293
+ this.breakpoints.set(id, breakpoint);
294
+ if (this.config.verbose) {
295
+ console.log(`[Debug] Breakpoint set: ${id} (${type})`);
296
+ }
297
+ return id;
298
+ }
299
+ /**
300
+ * Remove a breakpoint
301
+ */
302
+ removeBreakpoint(id) {
303
+ const removed = this.breakpoints.delete(id);
304
+ if (removed && this.config.verbose) {
305
+ console.log(`[Debug] Breakpoint removed: ${id}`);
306
+ }
307
+ return removed;
308
+ }
309
+ /**
310
+ * Enable a breakpoint
311
+ */
312
+ enableBreakpoint(id) {
313
+ const bp = this.breakpoints.get(id);
314
+ if (bp) {
315
+ bp.enabled = true;
316
+ return true;
317
+ }
318
+ return false;
319
+ }
320
+ /**
321
+ * Disable a breakpoint
322
+ */
323
+ disableBreakpoint(id) {
324
+ const bp = this.breakpoints.get(id);
325
+ if (bp) {
326
+ bp.enabled = false;
327
+ return true;
328
+ }
329
+ return false;
330
+ }
331
+ /**
332
+ * Get all breakpoints
333
+ */
334
+ getBreakpoints() {
335
+ return Array.from(this.breakpoints.values());
336
+ }
337
+ /**
338
+ * Clear all breakpoints
339
+ */
340
+ clearBreakpoints() {
341
+ this.breakpoints.clear();
342
+ }
343
+ // ============ Execution Control ============
344
+ /**
345
+ * Step to next event
346
+ */
347
+ async step() {
348
+ if (!this.enabled) {
349
+ throw new Error("Debug mode not enabled");
350
+ }
351
+ this.stepMode = true;
352
+ this.paused = false;
353
+ return new Promise((resolve) => {
354
+ this.resolveStep = () => {
355
+ const event = this.eventQueue.shift();
356
+ if (!event) {
357
+ resolve({
358
+ event: {
359
+ type: "crew:completed",
360
+ crewName: this.crew.name,
361
+ metrics: this.crew.getMetrics(),
362
+ success: true,
363
+ results: [],
364
+ timestamp: /* @__PURE__ */ new Date()
365
+ },
366
+ agentStates: this.getAgentStates(),
367
+ continueExecution: false
368
+ });
369
+ return;
370
+ }
371
+ this.updateContext(event);
372
+ const hitBreakpoint = this.checkBreakpoints(event);
373
+ resolve({
374
+ event,
375
+ breakpointHit: hitBreakpoint,
376
+ agentStates: this.getAgentStates(),
377
+ continueExecution: true
378
+ });
379
+ };
380
+ });
381
+ }
382
+ /**
383
+ * Continue execution
384
+ */
385
+ continue() {
386
+ if (!this.enabled) {
387
+ throw new Error("Debug mode not enabled");
388
+ }
389
+ this.stepMode = false;
390
+ this.paused = false;
391
+ this.emit("resumed");
392
+ if (this.config.verbose) {
393
+ console.log("[Debug] Execution resumed");
394
+ }
395
+ return Promise.resolve();
396
+ }
397
+ /**
398
+ * Pause execution
399
+ */
400
+ pause() {
401
+ this.paused = true;
402
+ this.emit("paused");
403
+ if (this.config.verbose) {
404
+ console.log("[Debug] Execution paused");
405
+ }
406
+ }
407
+ /**
408
+ * Resume from pause
409
+ */
410
+ resume() {
411
+ this.paused = false;
412
+ this.emit("resumed");
413
+ if (this.config.verbose) {
414
+ console.log("[Debug] Execution resumed");
415
+ }
416
+ }
417
+ /**
418
+ * Check if paused
419
+ */
420
+ isPaused() {
421
+ return this.paused;
422
+ }
423
+ // ============ Inspection ============
424
+ /**
425
+ * Inspect an agent
426
+ */
427
+ inspect(agentName) {
428
+ const agent = this.crew.getAgent(agentName);
429
+ if (!agent) return void 0;
430
+ return this.createAgentInspection(agent);
431
+ }
432
+ /**
433
+ * Inspect all agents
434
+ */
435
+ inspectAll() {
436
+ return this.getAgentStates();
437
+ }
438
+ /**
439
+ * Get debug context
440
+ */
441
+ getContext() {
442
+ return { ...this.context };
443
+ }
444
+ /**
445
+ * Get call stack
446
+ */
447
+ getCallStack() {
448
+ return [...this.callStack];
449
+ }
450
+ /**
451
+ * Evaluate expression in context
452
+ */
453
+ evaluate(expression) {
454
+ if (this.context.variables.has(expression)) {
455
+ return this.context.variables.get(expression);
456
+ }
457
+ const agentMatch = expression.match(/^agent\.(\w+)\.(\w+)$/);
458
+ if (agentMatch) {
459
+ const [, agentName, property] = agentMatch;
460
+ const agent = this.context.agents.get(agentName);
461
+ if (agent) {
462
+ return agent[property];
463
+ }
464
+ }
465
+ return void 0;
466
+ }
467
+ /**
468
+ * Set a watch expression
469
+ */
470
+ watch(expression) {
471
+ return {
472
+ expression,
473
+ value: this.evaluate(expression)
474
+ };
475
+ }
476
+ // ============ Event Handling ============
477
+ /**
478
+ * Handle a crew event (called by crew during execution)
479
+ */
480
+ handleEvent(event) {
481
+ if (!this.enabled) return;
482
+ this.eventQueue.push(event);
483
+ this.updateContext(event);
484
+ if (this.config.verbose) {
485
+ console.log(`[Debug] Event: ${event.type}`);
486
+ }
487
+ const hitBreakpoint = this.checkBreakpoints(event);
488
+ if (hitBreakpoint) {
489
+ this.paused = true;
490
+ this.emit("breakpointHit", hitBreakpoint, event);
491
+ if (this.config.verbose) {
492
+ console.log(`[Debug] Breakpoint hit: ${hitBreakpoint.id}`);
493
+ }
494
+ }
495
+ if (this.config.pauseOnError && event.type === "crew:error") {
496
+ this.pause();
497
+ }
498
+ if (this.stepMode && this.resolveStep) {
499
+ this.resolveStep();
500
+ this.resolveStep = void 0;
501
+ }
502
+ }
503
+ // ============ Internal ============
504
+ /**
505
+ * Create debug context
506
+ */
507
+ createContext() {
508
+ return {
509
+ agents: /* @__PURE__ */ new Map(),
510
+ tasks: [],
511
+ variables: /* @__PURE__ */ new Map(),
512
+ callStack: []
513
+ };
514
+ }
515
+ /**
516
+ * Update context from event
517
+ */
518
+ updateContext(event) {
519
+ this.context.currentEvent = event;
520
+ this.callStack.push(event.type);
521
+ if (this.callStack.length > this.config.maxCallStackDepth) {
522
+ this.callStack.shift();
523
+ }
524
+ this.context.agents = this.getAgentStates();
525
+ this.context.tasks = this.crew.getTasks().map((t) => t.toConfig());
526
+ }
527
+ /**
528
+ * Check breakpoints against event
529
+ */
530
+ checkBreakpoints(event) {
531
+ for (const bp of this.breakpoints.values()) {
532
+ if (!bp.enabled) continue;
533
+ if (bp.type !== "custom" && bp.type !== event.type) continue;
534
+ if (bp.condition && !bp.condition(event, this.context)) continue;
535
+ bp.hitCount++;
536
+ return bp;
537
+ }
538
+ return void 0;
539
+ }
540
+ /**
541
+ * Get agent states
542
+ */
543
+ getAgentStates() {
544
+ const states = /* @__PURE__ */ new Map();
545
+ for (const agent of this.crew.getAgents()) {
546
+ states.set(agent.name, this.createAgentInspection(agent));
547
+ }
548
+ return states;
549
+ }
550
+ /**
551
+ * Create agent inspection
552
+ */
553
+ createAgentInspection(agent) {
554
+ const stats = agent.getStats();
555
+ return {
556
+ name: agent.name,
557
+ role: agent.role.name,
558
+ status: stats.isBusy ? "busy" : this.paused ? "paused" : "idle",
559
+ currentTask: stats.currentTask,
560
+ memory: /* @__PURE__ */ new Map()
561
+ // Would need access to agent's internal state
562
+ };
563
+ }
564
+ /**
565
+ * Get debug summary
566
+ */
567
+ getSummary() {
568
+ return {
569
+ enabled: this.enabled,
570
+ paused: this.paused,
571
+ stepMode: this.stepMode,
572
+ breakpointCount: this.breakpoints.size,
573
+ queuedEvents: this.eventQueue.length,
574
+ callStackDepth: this.callStack.length
575
+ };
576
+ }
577
+ };
578
+ function createDebugMode(crew, config) {
579
+ return new DebugMode(crew, config);
580
+ }
581
+
582
+ export {
583
+ CrewDashboard,
584
+ createDashboard,
585
+ DebugMode,
586
+ createDebugMode
587
+ };