claude-mpm 3.4.10__py3-none-any.whl → 3.4.14__py3-none-any.whl
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.
- claude_mpm/cli/commands/run.py +10 -10
- claude_mpm/dashboard/index.html +13 -0
- claude_mpm/dashboard/static/css/dashboard.css +2722 -0
- claude_mpm/dashboard/static/js/components/agent-inference.js +619 -0
- claude_mpm/dashboard/static/js/components/event-processor.js +641 -0
- claude_mpm/dashboard/static/js/components/event-viewer.js +914 -0
- claude_mpm/dashboard/static/js/components/export-manager.js +362 -0
- claude_mpm/dashboard/static/js/components/file-tool-tracker.js +611 -0
- claude_mpm/dashboard/static/js/components/hud-library-loader.js +211 -0
- claude_mpm/dashboard/static/js/components/hud-manager.js +671 -0
- claude_mpm/dashboard/static/js/components/hud-visualizer.js +1718 -0
- claude_mpm/dashboard/static/js/components/module-viewer.js +2701 -0
- claude_mpm/dashboard/static/js/components/session-manager.js +520 -0
- claude_mpm/dashboard/static/js/components/socket-manager.js +343 -0
- claude_mpm/dashboard/static/js/components/ui-state-manager.js +427 -0
- claude_mpm/dashboard/static/js/components/working-directory.js +866 -0
- claude_mpm/dashboard/static/js/dashboard-original.js +4134 -0
- claude_mpm/dashboard/static/js/dashboard.js +1978 -0
- claude_mpm/dashboard/static/js/socket-client.js +537 -0
- claude_mpm/dashboard/templates/index.html +346 -0
- claude_mpm/dashboard/test_dashboard.html +372 -0
- claude_mpm/scripts/socketio_daemon.py +51 -6
- claude_mpm/services/socketio_server.py +41 -5
- {claude_mpm-3.4.10.dist-info → claude_mpm-3.4.14.dist-info}/METADATA +2 -1
- {claude_mpm-3.4.10.dist-info → claude_mpm-3.4.14.dist-info}/RECORD +29 -9
- {claude_mpm-3.4.10.dist-info → claude_mpm-3.4.14.dist-info}/WHEEL +0 -0
- {claude_mpm-3.4.10.dist-info → claude_mpm-3.4.14.dist-info}/entry_points.txt +0 -0
- {claude_mpm-3.4.10.dist-info → claude_mpm-3.4.14.dist-info}/licenses/LICENSE +0 -0
- {claude_mpm-3.4.10.dist-info → claude_mpm-3.4.14.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,619 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Agent Inference Module
|
|
3
|
+
*
|
|
4
|
+
* Handles agent inference and processing logic for determining whether events
|
|
5
|
+
* originate from the main agent or subagents based on event patterns and context.
|
|
6
|
+
*
|
|
7
|
+
* WHY: Separated from main dashboard to isolate complex agent inference logic
|
|
8
|
+
* that analyzes event patterns to determine agent context. This provides better
|
|
9
|
+
* maintainability and testability for a critical feature.
|
|
10
|
+
*
|
|
11
|
+
* DESIGN DECISION: This module maintains its own state for inference tracking
|
|
12
|
+
* but relies on the event viewer for source data, keeping clear separation of
|
|
13
|
+
* concerns while enabling delegation context tracking across events.
|
|
14
|
+
*/
|
|
15
|
+
class AgentInference {
|
|
16
|
+
constructor(eventViewer) {
|
|
17
|
+
this.eventViewer = eventViewer;
|
|
18
|
+
|
|
19
|
+
// Agent inference state tracking
|
|
20
|
+
this.state = {
|
|
21
|
+
// Track current subagent delegation context
|
|
22
|
+
currentDelegation: null,
|
|
23
|
+
// Map of session_id -> agent context
|
|
24
|
+
sessionAgents: new Map(),
|
|
25
|
+
// Map of event indices -> inferred agent
|
|
26
|
+
eventAgentMap: new Map(),
|
|
27
|
+
// PM delegation tracking for unique instance views
|
|
28
|
+
pmDelegations: new Map(), // delegation_id -> delegation context
|
|
29
|
+
// Map of agent events to their PM delegation
|
|
30
|
+
agentToDelegation: new Map() // agent_name -> delegation_id
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
console.log('Agent inference system initialized');
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Initialize the agent inference system
|
|
38
|
+
* Called when the dashboard initializes
|
|
39
|
+
*/
|
|
40
|
+
initialize() {
|
|
41
|
+
this.state = {
|
|
42
|
+
currentDelegation: null,
|
|
43
|
+
sessionAgents: new Map(),
|
|
44
|
+
eventAgentMap: new Map(),
|
|
45
|
+
pmDelegations: new Map(),
|
|
46
|
+
agentToDelegation: new Map()
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Infer agent context from event payload
|
|
52
|
+
* Based on production-ready detection from design document
|
|
53
|
+
* @param {Object} event - Event payload
|
|
54
|
+
* @returns {Object} - {type: 'main_agent'|'subagent', confidence: 'definitive'|'high'|'medium'|'default', agentName: string}
|
|
55
|
+
*/
|
|
56
|
+
inferAgentFromEvent(event) {
|
|
57
|
+
// Handle both direct properties and nested data properties
|
|
58
|
+
const data = event.data || {};
|
|
59
|
+
const sessionId = event.session_id || data.session_id || 'unknown';
|
|
60
|
+
const eventType = event.hook_event_name || data.hook_event_name || event.type || '';
|
|
61
|
+
const subtype = event.subtype || data.subtype || '';
|
|
62
|
+
const toolName = event.tool_name || data.tool_name || '';
|
|
63
|
+
|
|
64
|
+
// Debug logging for first few events to understand structure
|
|
65
|
+
if (Math.random() < 0.1) {
|
|
66
|
+
console.log('Agent inference debug:', {
|
|
67
|
+
eventType,
|
|
68
|
+
toolName,
|
|
69
|
+
hasData: !!event.data,
|
|
70
|
+
dataKeys: Object.keys(data),
|
|
71
|
+
eventKeys: Object.keys(event),
|
|
72
|
+
agentType: event.agent_type || data.agent_type,
|
|
73
|
+
subagentType: event.subagent_type || data.subagent_type
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
// Direct event detection (highest confidence) - from design doc
|
|
78
|
+
if (eventType === 'SubagentStop' || subtype === 'subagent_stop') {
|
|
79
|
+
const agentName = this.extractAgentNameFromEvent(event);
|
|
80
|
+
return {
|
|
81
|
+
type: 'subagent',
|
|
82
|
+
confidence: 'definitive',
|
|
83
|
+
agentName: agentName,
|
|
84
|
+
reason: 'SubagentStop event'
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (eventType === 'Stop' || subtype === 'stop') {
|
|
89
|
+
return {
|
|
90
|
+
type: 'main_agent',
|
|
91
|
+
confidence: 'definitive',
|
|
92
|
+
agentName: 'PM',
|
|
93
|
+
reason: 'Stop event'
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Tool-based detection (high confidence) - from design doc
|
|
98
|
+
if (toolName === 'Task') {
|
|
99
|
+
const agentName = this.extractSubagentTypeFromTask(event);
|
|
100
|
+
if (agentName) {
|
|
101
|
+
return {
|
|
102
|
+
type: 'subagent',
|
|
103
|
+
confidence: 'high',
|
|
104
|
+
agentName: agentName,
|
|
105
|
+
reason: 'Task tool with subagent_type'
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Hook event pattern analysis (high confidence)
|
|
111
|
+
if (eventType === 'PreToolUse' && toolName === 'Task') {
|
|
112
|
+
const agentName = this.extractSubagentTypeFromTask(event);
|
|
113
|
+
if (agentName) {
|
|
114
|
+
return {
|
|
115
|
+
type: 'subagent',
|
|
116
|
+
confidence: 'high',
|
|
117
|
+
agentName: agentName,
|
|
118
|
+
reason: 'PreToolUse Task delegation'
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Session pattern analysis (medium confidence) - from design doc
|
|
124
|
+
if (sessionId) {
|
|
125
|
+
const sessionLower = sessionId.toLowerCase();
|
|
126
|
+
if (['subagent', 'task', 'agent-'].some(pattern => sessionLower.includes(pattern))) {
|
|
127
|
+
return {
|
|
128
|
+
type: 'subagent',
|
|
129
|
+
confidence: 'medium',
|
|
130
|
+
agentName: 'Subagent',
|
|
131
|
+
reason: 'Session ID pattern'
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
// Agent type field analysis - check multiple possible locations
|
|
137
|
+
const agentType = event.agent_type || data.agent_type || event.agent_id || data.agent_id;
|
|
138
|
+
const subagentType = event.subagent_type || data.subagent_type;
|
|
139
|
+
|
|
140
|
+
if (subagentType && subagentType !== 'unknown') {
|
|
141
|
+
return {
|
|
142
|
+
type: 'subagent',
|
|
143
|
+
confidence: 'high',
|
|
144
|
+
agentName: subagentType,
|
|
145
|
+
reason: 'subagent_type field'
|
|
146
|
+
};
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
if (agentType && agentType !== 'unknown' && agentType !== 'main') {
|
|
150
|
+
return {
|
|
151
|
+
type: 'subagent',
|
|
152
|
+
confidence: 'medium',
|
|
153
|
+
agentName: agentType,
|
|
154
|
+
reason: 'agent_type field'
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
// Check for delegation_details from hook handler
|
|
159
|
+
if (data.delegation_details?.agent_type) {
|
|
160
|
+
return {
|
|
161
|
+
type: 'subagent',
|
|
162
|
+
confidence: 'high',
|
|
163
|
+
agentName: data.delegation_details.agent_type,
|
|
164
|
+
reason: 'delegation_details'
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// Check if this looks like a Hook event from Socket.IO
|
|
169
|
+
if (event.type && event.type.startsWith('hook.')) {
|
|
170
|
+
// Extract the hook type
|
|
171
|
+
const hookType = event.type.replace('hook.', '');
|
|
172
|
+
if (hookType === 'subagent_stop' || (data.hook_event_name === 'SubagentStop')) {
|
|
173
|
+
const agentName = data.agent_type || data.agent_id || 'Subagent';
|
|
174
|
+
return {
|
|
175
|
+
type: 'subagent',
|
|
176
|
+
confidence: 'high',
|
|
177
|
+
agentName: agentName,
|
|
178
|
+
reason: 'Socket.IO hook SubagentStop'
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
// Default to main agent (from design doc)
|
|
184
|
+
return {
|
|
185
|
+
type: 'main_agent',
|
|
186
|
+
confidence: 'default',
|
|
187
|
+
agentName: 'PM',
|
|
188
|
+
reason: 'default classification'
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Extract subagent type from Task tool parameters
|
|
194
|
+
* @param {Object} event - Event with Task tool
|
|
195
|
+
* @returns {string|null} - Subagent type or null
|
|
196
|
+
*/
|
|
197
|
+
extractSubagentTypeFromTask(event) {
|
|
198
|
+
// Check tool_parameters directly
|
|
199
|
+
if (event.tool_parameters?.subagent_type) {
|
|
200
|
+
return event.tool_parameters.subagent_type;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// Check nested in data.tool_parameters (hook events)
|
|
204
|
+
if (event.data?.tool_parameters?.subagent_type) {
|
|
205
|
+
return event.data.tool_parameters.subagent_type;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
// Check delegation_details (new structure)
|
|
209
|
+
if (event.data?.delegation_details?.agent_type) {
|
|
210
|
+
return event.data.delegation_details.agent_type;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// Check tool_input fallback
|
|
214
|
+
if (event.tool_input?.subagent_type) {
|
|
215
|
+
return event.tool_input.subagent_type;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Extract agent name from any event
|
|
223
|
+
* @param {Object} event - Event payload
|
|
224
|
+
* @returns {string} - Agent name
|
|
225
|
+
*/
|
|
226
|
+
extractAgentNameFromEvent(event) {
|
|
227
|
+
// Priority order based on reliability from design doc
|
|
228
|
+
const data = event.data || {};
|
|
229
|
+
|
|
230
|
+
// 1. Task tool subagent_type (highest priority)
|
|
231
|
+
if (event.tool_name === 'Task' || data.tool_name === 'Task') {
|
|
232
|
+
const taskAgent = this.extractSubagentTypeFromTask(event);
|
|
233
|
+
if (taskAgent) return taskAgent;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// 2. Direct subagent_type field
|
|
237
|
+
if (event.subagent_type && event.subagent_type !== 'unknown') {
|
|
238
|
+
return event.subagent_type;
|
|
239
|
+
}
|
|
240
|
+
if (data.subagent_type && data.subagent_type !== 'unknown') {
|
|
241
|
+
return data.subagent_type;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
// 2.5. Check delegation_details
|
|
245
|
+
if (data.delegation_details?.agent_type && data.delegation_details.agent_type !== 'unknown') {
|
|
246
|
+
return data.delegation_details.agent_type;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
// 3. Agent type fields (but not 'main' or 'unknown')
|
|
250
|
+
if (event.agent_type && !['main', 'unknown'].includes(event.agent_type)) {
|
|
251
|
+
return event.agent_type;
|
|
252
|
+
}
|
|
253
|
+
if (data.agent_type && !['main', 'unknown'].includes(data.agent_type)) {
|
|
254
|
+
return data.agent_type;
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
// 4. Agent ID field as fallback
|
|
258
|
+
if (event.agent_id && !['main', 'unknown'].includes(event.agent_id)) {
|
|
259
|
+
return event.agent_id;
|
|
260
|
+
}
|
|
261
|
+
if (data.agent_id && !['main', 'unknown'].includes(data.agent_id)) {
|
|
262
|
+
return data.agent_id;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
// 5. Other fallbacks
|
|
266
|
+
if (event.agent && event.agent !== 'unknown') {
|
|
267
|
+
return event.agent;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
if (event.name && event.name !== 'unknown') {
|
|
271
|
+
return event.name;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
// Default fallback
|
|
275
|
+
return 'Unknown';
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Process all events and build agent inference context
|
|
280
|
+
* This tracks delegation boundaries and agent context throughout the session
|
|
281
|
+
*/
|
|
282
|
+
processAgentInference() {
|
|
283
|
+
const events = this.eventViewer.events;
|
|
284
|
+
|
|
285
|
+
// Reset inference state
|
|
286
|
+
this.state.currentDelegation = null;
|
|
287
|
+
this.state.sessionAgents.clear();
|
|
288
|
+
this.state.eventAgentMap.clear();
|
|
289
|
+
this.state.pmDelegations.clear();
|
|
290
|
+
this.state.agentToDelegation.clear();
|
|
291
|
+
|
|
292
|
+
console.log('Processing agent inference for', events.length, 'events');
|
|
293
|
+
|
|
294
|
+
// Early return if no events
|
|
295
|
+
if (!events || events.length === 0) {
|
|
296
|
+
console.log('No events to process for agent inference');
|
|
297
|
+
return;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Process events chronologically to track delegation context
|
|
301
|
+
events.forEach((event, index) => {
|
|
302
|
+
let finalAgent; // Declare outside try-catch to ensure scope availability
|
|
303
|
+
|
|
304
|
+
try {
|
|
305
|
+
const inference = this.inferAgentFromEvent(event);
|
|
306
|
+
const sessionId = event.session_id || event.data?.session_id || 'default';
|
|
307
|
+
|
|
308
|
+
// Determine agent for this event based on context
|
|
309
|
+
finalAgent = inference;
|
|
310
|
+
|
|
311
|
+
// If we're in a delegation context and this event doesn't have high confidence agent info,
|
|
312
|
+
// inherit from delegation context
|
|
313
|
+
if (this.state.currentDelegation &&
|
|
314
|
+
inference.confidence === 'default' &&
|
|
315
|
+
sessionId === this.state.currentDelegation.sessionId) {
|
|
316
|
+
finalAgent = {
|
|
317
|
+
type: 'subagent',
|
|
318
|
+
confidence: 'inherited',
|
|
319
|
+
agentName: this.state.currentDelegation.agentName,
|
|
320
|
+
reason: 'inherited from delegation context'
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
// Track delegation boundaries and PM delegations
|
|
325
|
+
if (event.tool_name === 'Task' && inference.type === 'subagent') {
|
|
326
|
+
// Start of subagent delegation - create PM delegation entry
|
|
327
|
+
const delegationId = `pm_${sessionId}_${index}_${inference.agentName}`;
|
|
328
|
+
const pmDelegation = {
|
|
329
|
+
id: delegationId,
|
|
330
|
+
agentName: inference.agentName,
|
|
331
|
+
sessionId: sessionId,
|
|
332
|
+
startIndex: index,
|
|
333
|
+
endIndex: null,
|
|
334
|
+
pmCall: event, // Store the PM call event
|
|
335
|
+
timestamp: event.timestamp,
|
|
336
|
+
agentEvents: [] // Collect all events from this agent
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
this.state.pmDelegations.set(delegationId, pmDelegation);
|
|
340
|
+
this.state.agentToDelegation.set(inference.agentName, delegationId);
|
|
341
|
+
|
|
342
|
+
this.state.currentDelegation = {
|
|
343
|
+
agentName: inference.agentName,
|
|
344
|
+
sessionId: sessionId,
|
|
345
|
+
startIndex: index,
|
|
346
|
+
endIndex: null,
|
|
347
|
+
delegationId: delegationId
|
|
348
|
+
};
|
|
349
|
+
console.log('Delegation started:', this.state.currentDelegation);
|
|
350
|
+
} else if (inference.confidence === 'definitive' && inference.reason === 'SubagentStop event') {
|
|
351
|
+
// End of subagent delegation
|
|
352
|
+
if (this.state.currentDelegation) {
|
|
353
|
+
this.state.currentDelegation.endIndex = index;
|
|
354
|
+
|
|
355
|
+
// Update PM delegation end point
|
|
356
|
+
const pmDelegation = this.state.pmDelegations.get(this.state.currentDelegation.delegationId);
|
|
357
|
+
if (pmDelegation) {
|
|
358
|
+
pmDelegation.endIndex = index;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
console.log('Delegation ended:', this.state.currentDelegation);
|
|
362
|
+
this.state.currentDelegation = null;
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
// Track events within PM delegation context
|
|
367
|
+
if (this.state.currentDelegation && finalAgent.type === 'subagent') {
|
|
368
|
+
const pmDelegation = this.state.pmDelegations.get(this.state.currentDelegation.delegationId);
|
|
369
|
+
if (pmDelegation) {
|
|
370
|
+
pmDelegation.agentEvents.push({
|
|
371
|
+
eventIndex: index,
|
|
372
|
+
event: event,
|
|
373
|
+
inference: finalAgent
|
|
374
|
+
});
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Store the inference result
|
|
379
|
+
this.state.eventAgentMap.set(index, finalAgent);
|
|
380
|
+
|
|
381
|
+
// Update session agent tracking
|
|
382
|
+
this.state.sessionAgents.set(sessionId, finalAgent);
|
|
383
|
+
|
|
384
|
+
// Debug first few inferences
|
|
385
|
+
if (index < 5) {
|
|
386
|
+
console.log(`Event ${index} agent inference:`, {
|
|
387
|
+
event_type: event.type || event.hook_event_name,
|
|
388
|
+
subtype: event.subtype,
|
|
389
|
+
tool_name: event.tool_name,
|
|
390
|
+
inference: finalAgent,
|
|
391
|
+
hasData: !!event.data,
|
|
392
|
+
agentType: event.agent_type || event.data?.agent_type
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
} catch (error) {
|
|
396
|
+
console.error(`Error processing event ${index} for agent inference:`, error);
|
|
397
|
+
|
|
398
|
+
// Set a default finalAgent if not already set due to error
|
|
399
|
+
if (!finalAgent) {
|
|
400
|
+
finalAgent = {
|
|
401
|
+
type: 'main_agent',
|
|
402
|
+
confidence: 'error',
|
|
403
|
+
agentName: 'PM',
|
|
404
|
+
reason: 'error during processing'
|
|
405
|
+
};
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
// Store the default inference for this event
|
|
409
|
+
this.state.eventAgentMap.set(index, finalAgent);
|
|
410
|
+
}
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
console.log('Agent inference processing complete. Results:', {
|
|
414
|
+
total_events: events.length,
|
|
415
|
+
inferred_agents: this.state.eventAgentMap.size,
|
|
416
|
+
unique_sessions: this.state.sessionAgents.size,
|
|
417
|
+
pm_delegations: this.state.pmDelegations.size,
|
|
418
|
+
agent_to_delegation_mappings: this.state.agentToDelegation.size
|
|
419
|
+
});
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Get inferred agent for a specific event
|
|
424
|
+
* @param {number} eventIndex - Index of event in events array
|
|
425
|
+
* @returns {Object|null} - Agent inference result or null
|
|
426
|
+
*/
|
|
427
|
+
getInferredAgent(eventIndex) {
|
|
428
|
+
return this.state.eventAgentMap.get(eventIndex) || null;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Get inferred agent for an event object
|
|
433
|
+
* @param {Object} event - Event object
|
|
434
|
+
* @returns {Object|null} - Agent inference result or null
|
|
435
|
+
*/
|
|
436
|
+
getInferredAgentForEvent(event) {
|
|
437
|
+
const events = this.eventViewer.events;
|
|
438
|
+
|
|
439
|
+
// Try to find by exact reference first
|
|
440
|
+
let eventIndex = events.indexOf(event);
|
|
441
|
+
|
|
442
|
+
// If exact match fails, try to find by timestamp or session_id + timestamp
|
|
443
|
+
if (eventIndex === -1 && event.timestamp) {
|
|
444
|
+
eventIndex = events.findIndex(e =>
|
|
445
|
+
e.timestamp === event.timestamp &&
|
|
446
|
+
e.session_id === event.session_id
|
|
447
|
+
);
|
|
448
|
+
}
|
|
449
|
+
|
|
450
|
+
// If we still can't find it, perform inline inference
|
|
451
|
+
if (eventIndex === -1) {
|
|
452
|
+
console.log('Agent inference: Could not find event in events array, performing inline inference');
|
|
453
|
+
return this.inferAgentFromEvent(event);
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
// Get cached inference or perform new inference
|
|
457
|
+
let inference = this.getInferredAgent(eventIndex);
|
|
458
|
+
if (!inference) {
|
|
459
|
+
inference = this.inferAgentFromEvent(event);
|
|
460
|
+
// Cache the result
|
|
461
|
+
this.state.eventAgentMap.set(eventIndex, inference);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
return inference;
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Get current delegation context
|
|
469
|
+
* @returns {Object|null} - Current delegation or null
|
|
470
|
+
*/
|
|
471
|
+
getCurrentDelegation() {
|
|
472
|
+
return this.state.currentDelegation;
|
|
473
|
+
}
|
|
474
|
+
|
|
475
|
+
/**
|
|
476
|
+
* Get session agents map
|
|
477
|
+
* @returns {Map} - Map of session IDs to agent contexts
|
|
478
|
+
*/
|
|
479
|
+
getSessionAgents() {
|
|
480
|
+
return this.state.sessionAgents;
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Get event agent map
|
|
485
|
+
* @returns {Map} - Map of event indices to agent contexts
|
|
486
|
+
*/
|
|
487
|
+
getEventAgentMap() {
|
|
488
|
+
return this.state.eventAgentMap;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Get PM delegations for unique instance views
|
|
493
|
+
* @returns {Map} - Map of delegation IDs to PM delegation contexts
|
|
494
|
+
*/
|
|
495
|
+
getPMDelegations() {
|
|
496
|
+
return this.state.pmDelegations;
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
/**
|
|
500
|
+
* Get agent to delegation mapping
|
|
501
|
+
* @returns {Map} - Map of agent names to delegation IDs
|
|
502
|
+
*/
|
|
503
|
+
getAgentToDelegationMap() {
|
|
504
|
+
return this.state.agentToDelegation;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
/**
|
|
508
|
+
* Get unique agent instances (one per agent type, consolidating multiple delegations)
|
|
509
|
+
* This is used for the unique instance view in the agents tab
|
|
510
|
+
* @returns {Array} - Array of unique agent instances
|
|
511
|
+
*/
|
|
512
|
+
getUniqueAgentInstances() {
|
|
513
|
+
const agentMap = new Map(); // agentName -> consolidated data
|
|
514
|
+
|
|
515
|
+
// Consolidate all PM delegations by agent name
|
|
516
|
+
for (const [delegationId, delegation] of this.state.pmDelegations) {
|
|
517
|
+
const agentName = delegation.agentName;
|
|
518
|
+
|
|
519
|
+
if (!agentMap.has(agentName)) {
|
|
520
|
+
// First delegation for this agent type
|
|
521
|
+
agentMap.set(agentName, {
|
|
522
|
+
id: `consolidated_${agentName}`,
|
|
523
|
+
type: 'consolidated_agent',
|
|
524
|
+
agentName: agentName,
|
|
525
|
+
delegations: [], // Array of all delegations
|
|
526
|
+
pmCalls: [], // Array of all PM calls
|
|
527
|
+
allEvents: [], // Combined events from all delegations
|
|
528
|
+
firstTimestamp: delegation.timestamp,
|
|
529
|
+
lastTimestamp: delegation.timestamp,
|
|
530
|
+
totalEventCount: delegation.agentEvents.length,
|
|
531
|
+
delegationCount: 1
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
// Add this delegation to the consolidated agent
|
|
536
|
+
const agent = agentMap.get(agentName);
|
|
537
|
+
agent.delegations.push({
|
|
538
|
+
id: delegationId,
|
|
539
|
+
pmCall: delegation.pmCall,
|
|
540
|
+
timestamp: delegation.timestamp,
|
|
541
|
+
eventCount: delegation.agentEvents.length,
|
|
542
|
+
startIndex: delegation.startIndex,
|
|
543
|
+
endIndex: delegation.endIndex,
|
|
544
|
+
events: delegation.agentEvents
|
|
545
|
+
});
|
|
546
|
+
|
|
547
|
+
if (delegation.pmCall) {
|
|
548
|
+
agent.pmCalls.push(delegation.pmCall);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// Merge events from all delegations
|
|
552
|
+
agent.allEvents = agent.allEvents.concat(delegation.agentEvents);
|
|
553
|
+
|
|
554
|
+
// Update consolidated metadata
|
|
555
|
+
if (new Date(delegation.timestamp) < new Date(agent.firstTimestamp)) {
|
|
556
|
+
agent.firstTimestamp = delegation.timestamp;
|
|
557
|
+
}
|
|
558
|
+
if (new Date(delegation.timestamp) > new Date(agent.lastTimestamp)) {
|
|
559
|
+
agent.lastTimestamp = delegation.timestamp;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
agent.totalEventCount += delegation.agentEvents.length;
|
|
563
|
+
agent.delegationCount++;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// Handle agents that appear without explicit PM delegation (implied PM)
|
|
567
|
+
const events = this.eventViewer.events;
|
|
568
|
+
for (let index = 0; index < events.length; index++) {
|
|
569
|
+
const inference = this.getInferredAgent(index);
|
|
570
|
+
if (inference && inference.type === 'subagent' && !agentMap.has(inference.agentName)) {
|
|
571
|
+
// Create consolidated agent for implied delegation
|
|
572
|
+
agentMap.set(inference.agentName, {
|
|
573
|
+
id: `consolidated_${inference.agentName}`,
|
|
574
|
+
type: 'consolidated_agent',
|
|
575
|
+
agentName: inference.agentName,
|
|
576
|
+
delegations: [{
|
|
577
|
+
id: `implied_pm_${inference.agentName}_${index}`,
|
|
578
|
+
pmCall: null,
|
|
579
|
+
timestamp: events[index].timestamp,
|
|
580
|
+
eventCount: 1,
|
|
581
|
+
startIndex: index,
|
|
582
|
+
endIndex: null,
|
|
583
|
+
events: [{
|
|
584
|
+
eventIndex: index,
|
|
585
|
+
event: events[index],
|
|
586
|
+
inference: inference
|
|
587
|
+
}]
|
|
588
|
+
}],
|
|
589
|
+
pmCalls: [],
|
|
590
|
+
allEvents: [{
|
|
591
|
+
eventIndex: index,
|
|
592
|
+
event: events[index],
|
|
593
|
+
inference: inference
|
|
594
|
+
}],
|
|
595
|
+
firstTimestamp: events[index].timestamp,
|
|
596
|
+
lastTimestamp: events[index].timestamp,
|
|
597
|
+
totalEventCount: 1,
|
|
598
|
+
delegationCount: 1,
|
|
599
|
+
isImplied: true
|
|
600
|
+
});
|
|
601
|
+
}
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
// Convert map to array and sort by first appearance (timestamp)
|
|
605
|
+
const uniqueInstances = Array.from(agentMap.values())
|
|
606
|
+
.sort((a, b) => new Date(a.firstTimestamp) - new Date(b.firstTimestamp));
|
|
607
|
+
|
|
608
|
+
console.log('Consolidated unique agents:', {
|
|
609
|
+
total_unique_agents: uniqueInstances.length,
|
|
610
|
+
agents: uniqueInstances.map(agent => ({
|
|
611
|
+
name: agent.agentName,
|
|
612
|
+
delegations: agent.delegationCount,
|
|
613
|
+
totalEvents: agent.totalEventCount
|
|
614
|
+
}))
|
|
615
|
+
});
|
|
616
|
+
|
|
617
|
+
return uniqueInstances;
|
|
618
|
+
}
|
|
619
|
+
}
|