@swarmify/agents-mcp 0.2.0

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,704 @@
1
+ import { extractFileOpsFromBash } from './file_ops.js';
2
+ const claudeToolUseMap = new Map();
3
+ export function normalizeEvents(agentType, raw) {
4
+ if (agentType === 'codex') {
5
+ return normalizeCodex(raw);
6
+ }
7
+ else if (agentType === 'cursor') {
8
+ return normalizeCursor(raw);
9
+ }
10
+ else if (agentType === 'gemini') {
11
+ return normalizeGemini(raw);
12
+ }
13
+ else if (agentType === 'claude') {
14
+ return normalizeClaude(raw);
15
+ }
16
+ const timestamp = new Date().toISOString();
17
+ return [{
18
+ type: raw.type || 'unknown',
19
+ agent: agentType,
20
+ raw: raw,
21
+ timestamp: timestamp,
22
+ }];
23
+ }
24
+ export function normalizeEvent(agentType, raw) {
25
+ const events = normalizeEvents(agentType, raw);
26
+ if (events.length > 0) {
27
+ return events[0];
28
+ }
29
+ return {
30
+ type: raw.type || 'unknown',
31
+ agent: agentType,
32
+ raw: raw,
33
+ timestamp: new Date().toISOString(),
34
+ };
35
+ }
36
+ function normalizeCodex(raw) {
37
+ if (!raw || typeof raw !== 'object') {
38
+ return [{
39
+ type: 'unknown',
40
+ agent: 'codex',
41
+ raw: raw,
42
+ timestamp: new Date().toISOString(),
43
+ }];
44
+ }
45
+ const eventType = raw.type || 'unknown';
46
+ const timestamp = new Date().toISOString();
47
+ if (eventType === 'thread.started') {
48
+ return [{
49
+ type: 'init',
50
+ agent: 'codex',
51
+ session_id: raw.thread_id || null,
52
+ timestamp: timestamp,
53
+ }];
54
+ }
55
+ else if (eventType === 'turn.started') {
56
+ return [{
57
+ type: 'turn_start',
58
+ agent: 'codex',
59
+ timestamp: timestamp,
60
+ }];
61
+ }
62
+ else if (eventType === 'item.completed') {
63
+ const item = raw.item || {};
64
+ const itemType = item?.type;
65
+ if (itemType === 'agent_message') {
66
+ return [{
67
+ type: 'message',
68
+ agent: 'codex',
69
+ content: item?.text || '',
70
+ complete: true,
71
+ timestamp: timestamp,
72
+ }];
73
+ }
74
+ else if (itemType === 'command_execution') {
75
+ const command = item?.command || '';
76
+ if (!command.trim()) {
77
+ return [];
78
+ }
79
+ const events = [{
80
+ type: 'bash',
81
+ agent: 'codex',
82
+ tool: 'command_execution',
83
+ command: command,
84
+ timestamp: timestamp,
85
+ }];
86
+ const [filesRead, filesWritten, filesDeleted] = extractFileOpsFromBash(command);
87
+ for (const path of filesRead) {
88
+ events.push({
89
+ type: 'file_read',
90
+ agent: 'codex',
91
+ tool: 'bash',
92
+ path,
93
+ command,
94
+ timestamp,
95
+ });
96
+ }
97
+ for (const path of filesWritten) {
98
+ events.push({
99
+ type: 'file_write',
100
+ agent: 'codex',
101
+ tool: 'bash',
102
+ path,
103
+ command,
104
+ timestamp,
105
+ });
106
+ }
107
+ for (const path of filesDeleted) {
108
+ events.push({
109
+ type: 'file_delete',
110
+ agent: 'codex',
111
+ tool: 'bash',
112
+ path,
113
+ command,
114
+ timestamp,
115
+ });
116
+ }
117
+ return events;
118
+ }
119
+ else if (itemType === 'file_change') {
120
+ const changes = Array.isArray(item?.changes) ? item.changes : [];
121
+ const changeEvents = [];
122
+ for (const change of changes) {
123
+ const path = change?.path || change?.file_path || '';
124
+ if (!path) {
125
+ continue;
126
+ }
127
+ const kind = String(change?.kind || change?.status || '').toLowerCase();
128
+ const baseEvent = {
129
+ agent: 'codex',
130
+ tool: 'file_change',
131
+ path,
132
+ timestamp,
133
+ };
134
+ if (['add', 'create', 'new'].includes(kind)) {
135
+ changeEvents.push({ ...baseEvent, type: 'file_create' });
136
+ }
137
+ else if (['delete', 'remove'].includes(kind)) {
138
+ changeEvents.push({ ...baseEvent, type: 'file_delete' });
139
+ }
140
+ else {
141
+ changeEvents.push({ ...baseEvent, type: 'file_write' });
142
+ }
143
+ }
144
+ if (changeEvents.length > 0) {
145
+ return changeEvents;
146
+ }
147
+ }
148
+ else if (itemType === 'tool_call') {
149
+ const toolName = item?.name || 'unknown';
150
+ const toolArgs = item?.arguments || {};
151
+ if (toolName === 'create_file') {
152
+ const path = toolArgs?.path || toolArgs?.file_path || '';
153
+ if (!path) {
154
+ return [];
155
+ }
156
+ return [{
157
+ type: 'file_create',
158
+ agent: 'codex',
159
+ tool: toolName,
160
+ path: path,
161
+ timestamp: timestamp,
162
+ }];
163
+ }
164
+ else if (toolName === 'write_file' || toolName === 'edit_file') {
165
+ const path = toolArgs?.path || toolArgs?.file_path || '';
166
+ if (!path) {
167
+ return [];
168
+ }
169
+ return [{
170
+ type: 'file_write',
171
+ agent: 'codex',
172
+ tool: toolName,
173
+ path: path,
174
+ timestamp: timestamp,
175
+ }];
176
+ }
177
+ else if (toolName === 'read_file') {
178
+ const path = toolArgs?.path || toolArgs?.file_path || '';
179
+ if (!path) {
180
+ return [];
181
+ }
182
+ return [{
183
+ type: 'file_read',
184
+ agent: 'codex',
185
+ tool: toolName,
186
+ path: path,
187
+ timestamp: timestamp,
188
+ }];
189
+ }
190
+ else if (toolName === 'delete_file' || toolName === 'remove_file') {
191
+ const path = toolArgs?.path || toolArgs?.file_path || '';
192
+ if (!path) {
193
+ return [];
194
+ }
195
+ return [{
196
+ type: 'file_delete',
197
+ agent: 'codex',
198
+ tool: toolName,
199
+ path: path,
200
+ timestamp: timestamp,
201
+ }];
202
+ }
203
+ else if (toolName === 'shell' || toolName === 'bash' || toolName === 'execute') {
204
+ const command = toolArgs?.command || '';
205
+ if (!command.trim()) {
206
+ return [];
207
+ }
208
+ return [{
209
+ type: 'bash',
210
+ agent: 'codex',
211
+ tool: toolName,
212
+ command: command,
213
+ timestamp: timestamp,
214
+ }];
215
+ }
216
+ else {
217
+ return [{
218
+ type: 'tool_use',
219
+ agent: 'codex',
220
+ tool: toolName,
221
+ args: toolArgs,
222
+ timestamp: timestamp,
223
+ }];
224
+ }
225
+ }
226
+ }
227
+ else if (eventType === 'turn.completed') {
228
+ const usage = raw.usage || {};
229
+ return [{
230
+ type: 'result',
231
+ agent: 'codex',
232
+ status: 'success',
233
+ usage: {
234
+ input_tokens: usage?.input_tokens || 0,
235
+ output_tokens: usage?.output_tokens || 0,
236
+ },
237
+ timestamp: timestamp,
238
+ }];
239
+ }
240
+ return [{
241
+ type: eventType,
242
+ agent: 'codex',
243
+ raw: raw,
244
+ timestamp: timestamp,
245
+ }];
246
+ }
247
+ function normalizeCursor(raw) {
248
+ const eventType = raw.type || 'unknown';
249
+ const subtype = raw.subtype;
250
+ const timestamp = new Date().toISOString();
251
+ if (eventType === 'system' && subtype === 'init') {
252
+ return [{
253
+ type: 'init',
254
+ agent: 'cursor',
255
+ model: raw.model,
256
+ session_id: raw.session_id,
257
+ timestamp: timestamp,
258
+ }];
259
+ }
260
+ else if (eventType === 'thinking') {
261
+ if (subtype === 'delta') {
262
+ const text = raw.text || '';
263
+ if (!text.trim()) {
264
+ return [];
265
+ }
266
+ }
267
+ return [{
268
+ type: 'thinking',
269
+ agent: 'cursor',
270
+ content: raw.text || '',
271
+ complete: subtype === 'completed',
272
+ timestamp: timestamp,
273
+ }];
274
+ }
275
+ else if (eventType === 'assistant') {
276
+ const message = raw.message || {};
277
+ const contentBlocks = message.content || [];
278
+ const events = [];
279
+ let textContent = '';
280
+ for (const block of contentBlocks) {
281
+ if (block.type === 'text') {
282
+ textContent += block.text || '';
283
+ }
284
+ else if (block.type === 'tool_use') {
285
+ events.push({
286
+ type: 'tool_use',
287
+ agent: 'cursor',
288
+ tool: block.name || 'unknown',
289
+ args: block.input || {},
290
+ timestamp: timestamp,
291
+ });
292
+ }
293
+ }
294
+ if (textContent) {
295
+ events.push({
296
+ type: 'message',
297
+ agent: 'cursor',
298
+ content: textContent,
299
+ complete: true,
300
+ timestamp: timestamp,
301
+ });
302
+ }
303
+ if (events.length === 0) {
304
+ events.push({
305
+ type: 'message',
306
+ agent: 'cursor',
307
+ content: '',
308
+ complete: true,
309
+ timestamp: timestamp,
310
+ });
311
+ }
312
+ return events;
313
+ }
314
+ else if (eventType === 'result') {
315
+ return [{
316
+ type: 'result',
317
+ agent: 'cursor',
318
+ status: subtype || 'success',
319
+ duration_ms: raw.duration_ms,
320
+ timestamp: timestamp,
321
+ }];
322
+ }
323
+ else if (eventType === 'tool_result') {
324
+ return [{
325
+ type: 'tool_result',
326
+ agent: 'cursor',
327
+ tool: raw.tool_name || 'unknown',
328
+ success: raw.success !== false,
329
+ timestamp: timestamp,
330
+ }];
331
+ }
332
+ else if (eventType === 'tool_call' && subtype === 'completed') {
333
+ const toolCall = raw.tool_call;
334
+ if (toolCall?.shellToolCall) {
335
+ const command = toolCall.shellToolCall.args?.command || '';
336
+ return [{
337
+ type: 'bash',
338
+ agent: 'cursor',
339
+ tool: 'shell',
340
+ command: command,
341
+ timestamp: timestamp,
342
+ }];
343
+ }
344
+ else if (toolCall?.editToolCall) {
345
+ const filePath = toolCall.editToolCall.args?.path || '';
346
+ return [{
347
+ type: 'file_write',
348
+ agent: 'cursor',
349
+ tool: 'edit',
350
+ path: filePath,
351
+ timestamp: timestamp,
352
+ }];
353
+ }
354
+ else if (toolCall?.readToolCall) {
355
+ const filePath = toolCall.readToolCall.args?.path || '';
356
+ return [{
357
+ type: 'file_read',
358
+ agent: 'cursor',
359
+ tool: 'read',
360
+ path: filePath,
361
+ timestamp: timestamp,
362
+ }];
363
+ }
364
+ else if (toolCall?.deleteToolCall) {
365
+ const filePath = toolCall.deleteToolCall.args?.path || '';
366
+ return [{
367
+ type: 'file_delete',
368
+ agent: 'cursor',
369
+ tool: 'delete',
370
+ path: filePath,
371
+ timestamp: timestamp,
372
+ }];
373
+ }
374
+ else if (toolCall?.listToolCall) {
375
+ const dirPath = toolCall.listToolCall.args?.path || '';
376
+ return [{
377
+ type: 'directory_list',
378
+ agent: 'cursor',
379
+ tool: 'list',
380
+ path: dirPath,
381
+ timestamp: timestamp,
382
+ }];
383
+ }
384
+ return [{
385
+ type: 'tool_use',
386
+ agent: 'cursor',
387
+ tool: Object.keys(toolCall || {})[0] || 'unknown',
388
+ timestamp: timestamp,
389
+ }];
390
+ }
391
+ return [{
392
+ type: eventType,
393
+ agent: 'cursor',
394
+ raw: raw,
395
+ timestamp: timestamp,
396
+ }];
397
+ }
398
+ function normalizeGemini(raw) {
399
+ if (!raw || typeof raw !== 'object') {
400
+ return [{
401
+ type: 'unknown',
402
+ agent: 'gemini',
403
+ raw: raw,
404
+ timestamp: new Date().toISOString(),
405
+ }];
406
+ }
407
+ const eventType = raw?.type || 'unknown';
408
+ const timestamp = raw?.timestamp || new Date().toISOString();
409
+ if (eventType === 'init') {
410
+ return [{
411
+ type: 'init',
412
+ agent: 'gemini',
413
+ model: raw?.model,
414
+ session_id: raw?.session_id,
415
+ timestamp: timestamp,
416
+ }];
417
+ }
418
+ else if (eventType === 'message') {
419
+ const role = raw?.role || 'assistant';
420
+ if (role === 'assistant') {
421
+ return [{
422
+ type: 'message',
423
+ agent: 'gemini',
424
+ content: raw?.content || '',
425
+ complete: !raw?.delta,
426
+ timestamp: timestamp,
427
+ }];
428
+ }
429
+ else {
430
+ return [{
431
+ type: 'user_message',
432
+ agent: 'gemini',
433
+ content: raw?.content || '',
434
+ timestamp: timestamp,
435
+ }];
436
+ }
437
+ }
438
+ else if (eventType === 'tool_call' || eventType === 'tool_use') {
439
+ const toolNameRaw = raw?.tool_name || raw?.name || 'unknown';
440
+ const toolName = String(toolNameRaw);
441
+ let toolArgsRaw = raw?.parameters;
442
+ if (toolArgsRaw === null || toolArgsRaw === undefined) {
443
+ toolArgsRaw = raw?.args;
444
+ }
445
+ const toolArgs = (typeof toolArgsRaw === 'object' && toolArgsRaw !== null) ? toolArgsRaw : {};
446
+ const toolNameLower = toolName.toLowerCase();
447
+ const filePath = toolArgs?.file_path || toolArgs?.path || '';
448
+ const command = toolArgs?.command || '';
449
+ // File write/edit tools - Gemini uses 'replace', 'edit', 'patch', 'write_file', etc.
450
+ const writeTools = ['replace', 'edit', 'patch', 'write_file', 'edit_file', 'update_file', 'modify_file'];
451
+ if (writeTools.includes(toolNameLower) || (toolNameLower.includes('write') && toolNameLower.includes('file'))) {
452
+ if (!filePath.trim()) {
453
+ return [];
454
+ }
455
+ return [{
456
+ type: 'file_write',
457
+ agent: 'gemini',
458
+ tool: toolName,
459
+ path: filePath,
460
+ timestamp: timestamp,
461
+ }];
462
+ }
463
+ // File read tools
464
+ const readTools = ['read_file', 'view_file', 'cat_file', 'get_file'];
465
+ if (readTools.includes(toolNameLower) || (toolNameLower.includes('read') && toolNameLower.includes('file'))) {
466
+ if (!filePath.trim()) {
467
+ return [];
468
+ }
469
+ return [{
470
+ type: 'file_read',
471
+ agent: 'gemini',
472
+ tool: toolName,
473
+ path: filePath,
474
+ timestamp: timestamp,
475
+ }];
476
+ }
477
+ // File delete tools
478
+ const deleteTools = ['delete_file', 'remove_file', 'rm_file'];
479
+ if (deleteTools.includes(toolNameLower) || (toolNameLower.includes('delete') && toolNameLower.includes('file'))) {
480
+ if (!filePath.trim()) {
481
+ return [];
482
+ }
483
+ return [{
484
+ type: 'file_delete',
485
+ agent: 'gemini',
486
+ tool: toolName,
487
+ path: filePath,
488
+ timestamp: timestamp,
489
+ }];
490
+ }
491
+ // Shell/bash tools
492
+ if (['shell', 'bash', 'execute', 'run_command', 'run_shell_command'].includes(toolNameLower)) {
493
+ if (!command.trim()) {
494
+ return [];
495
+ }
496
+ return [{
497
+ type: 'bash',
498
+ agent: 'gemini',
499
+ tool: toolName,
500
+ command: command,
501
+ timestamp: timestamp,
502
+ }];
503
+ }
504
+ return [{
505
+ type: 'tool_use',
506
+ agent: 'gemini',
507
+ tool: toolName,
508
+ args: toolArgs,
509
+ timestamp: timestamp,
510
+ }];
511
+ }
512
+ else if (eventType === 'result') {
513
+ const stats = raw?.stats || {};
514
+ return [{
515
+ type: 'result',
516
+ agent: 'gemini',
517
+ status: raw?.status || 'success',
518
+ duration_ms: stats?.duration_ms,
519
+ usage: {
520
+ total_tokens: stats?.total_tokens || 0,
521
+ },
522
+ timestamp: timestamp,
523
+ }];
524
+ }
525
+ return [{
526
+ type: eventType,
527
+ agent: 'gemini',
528
+ raw: raw,
529
+ timestamp: timestamp,
530
+ }];
531
+ }
532
+ function normalizeClaude(raw) {
533
+ const eventType = raw.type || 'unknown';
534
+ const subtype = raw.subtype;
535
+ const timestamp = new Date().toISOString();
536
+ if (eventType === 'system' && subtype === 'init') {
537
+ return [{
538
+ type: 'init',
539
+ agent: 'claude',
540
+ model: raw.model,
541
+ session_id: raw.session_id,
542
+ timestamp: timestamp,
543
+ }];
544
+ }
545
+ else if (eventType === 'assistant') {
546
+ const message = raw.message || {};
547
+ const contentBlocks = message.content || [];
548
+ const events = [];
549
+ let textContent = '';
550
+ for (const block of contentBlocks) {
551
+ if (block.type === 'text') {
552
+ textContent += block.text || '';
553
+ }
554
+ else if (block.type === 'tool_use') {
555
+ const toolName = block.name || 'unknown';
556
+ const toolId = block.id;
557
+ const toolInput = block.input || {};
558
+ if (toolId) {
559
+ if (toolName === 'Bash' && toolInput.command) {
560
+ claudeToolUseMap.set(toolId, { tool: toolName, command: toolInput.command });
561
+ }
562
+ else if ((toolName === 'Edit' || toolName === 'Write') && toolInput.file_path) {
563
+ claudeToolUseMap.set(toolId, { tool: toolName, path: toolInput.file_path });
564
+ }
565
+ else if (toolName === 'Read' && toolInput.file_path) {
566
+ claudeToolUseMap.set(toolId, { tool: toolName, path: toolInput.file_path });
567
+ }
568
+ }
569
+ events.push({
570
+ type: 'tool_use',
571
+ agent: 'claude',
572
+ tool: toolName,
573
+ args: toolInput,
574
+ timestamp: timestamp,
575
+ });
576
+ }
577
+ }
578
+ if (textContent) {
579
+ events.push({
580
+ type: 'message',
581
+ agent: 'claude',
582
+ content: textContent,
583
+ complete: true,
584
+ timestamp: timestamp,
585
+ });
586
+ }
587
+ if (events.length === 0) {
588
+ events.push({
589
+ type: 'message',
590
+ agent: 'claude',
591
+ content: '',
592
+ complete: true,
593
+ timestamp: timestamp,
594
+ });
595
+ }
596
+ return events;
597
+ }
598
+ else if (eventType === 'user') {
599
+ const message = raw.message || {};
600
+ const contentBlocks = message.content || [];
601
+ const toolUseResult = raw.tool_use_result;
602
+ const events = [];
603
+ for (const block of contentBlocks) {
604
+ if (block.type === 'tool_result') {
605
+ const toolUseId = block.tool_use_id;
606
+ if (toolUseResult?.file) {
607
+ events.push({
608
+ type: 'file_read',
609
+ agent: 'claude',
610
+ path: toolUseResult.file.filePath,
611
+ timestamp: timestamp,
612
+ });
613
+ }
614
+ else if (toolUseResult?.stdout !== undefined) {
615
+ const toolUseInfo = claudeToolUseMap.get(toolUseId);
616
+ const command = toolUseInfo?.command || '';
617
+ events.push({
618
+ type: 'bash',
619
+ agent: 'claude',
620
+ command: command,
621
+ timestamp: timestamp,
622
+ });
623
+ claudeToolUseMap.delete(toolUseId);
624
+ }
625
+ else if (!block.is_error && typeof toolUseResult !== 'string') {
626
+ const toolUseInfo = claudeToolUseMap.get(toolUseId);
627
+ if (toolUseInfo && (toolUseInfo.tool === 'Edit' || toolUseInfo.tool === 'Write') && toolUseInfo.path) {
628
+ events.push({
629
+ type: 'file_write',
630
+ agent: 'claude',
631
+ path: toolUseInfo.path,
632
+ timestamp: timestamp,
633
+ });
634
+ claudeToolUseMap.delete(toolUseId);
635
+ }
636
+ else {
637
+ events.push({
638
+ type: 'tool_result',
639
+ agent: 'claude',
640
+ tool_use_id: toolUseId,
641
+ success: true,
642
+ timestamp: timestamp,
643
+ });
644
+ if (toolUseInfo) {
645
+ claudeToolUseMap.delete(toolUseId);
646
+ }
647
+ }
648
+ }
649
+ else if (block.is_error || (typeof toolUseResult === 'string' && toolUseResult.startsWith('Error:'))) {
650
+ events.push({
651
+ type: 'error',
652
+ agent: 'claude',
653
+ message: block.content || (typeof toolUseResult === 'string' ? toolUseResult : ''),
654
+ timestamp: timestamp,
655
+ });
656
+ }
657
+ else {
658
+ const toolUseInfo = claudeToolUseMap.get(toolUseId);
659
+ events.push({
660
+ type: 'tool_result',
661
+ agent: 'claude',
662
+ tool_use_id: toolUseId,
663
+ success: !block.is_error,
664
+ timestamp: timestamp,
665
+ });
666
+ if (toolUseInfo) {
667
+ claudeToolUseMap.delete(toolUseId);
668
+ }
669
+ }
670
+ }
671
+ }
672
+ return events.length > 0 ? events : [{
673
+ type: eventType,
674
+ agent: 'claude',
675
+ raw: raw,
676
+ timestamp: timestamp,
677
+ }];
678
+ }
679
+ else if (eventType === 'result') {
680
+ return [{
681
+ type: 'result',
682
+ agent: 'claude',
683
+ status: subtype || 'success',
684
+ duration_ms: raw.duration_ms,
685
+ timestamp: timestamp,
686
+ }];
687
+ }
688
+ return [{
689
+ type: eventType,
690
+ agent: 'claude',
691
+ raw: raw,
692
+ timestamp: timestamp,
693
+ }];
694
+ }
695
+ export function parseEvent(agentType, line) {
696
+ try {
697
+ const raw = JSON.parse(line);
698
+ return normalizeEvents(agentType, raw);
699
+ }
700
+ catch {
701
+ return null;
702
+ }
703
+ }
704
+ //# sourceMappingURL=parsers.js.map