@loxia-labs/loxia-autopilot-one 1.0.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.
- package/LICENSE +267 -0
- package/README.md +509 -0
- package/bin/cli.js +117 -0
- package/package.json +94 -0
- package/scripts/install-scanners.js +236 -0
- package/src/analyzers/CSSAnalyzer.js +297 -0
- package/src/analyzers/ConfigValidator.js +690 -0
- package/src/analyzers/ESLintAnalyzer.js +320 -0
- package/src/analyzers/JavaScriptAnalyzer.js +261 -0
- package/src/analyzers/PrettierFormatter.js +247 -0
- package/src/analyzers/PythonAnalyzer.js +266 -0
- package/src/analyzers/SecurityAnalyzer.js +729 -0
- package/src/analyzers/TypeScriptAnalyzer.js +247 -0
- package/src/analyzers/codeCloneDetector/analyzer.js +344 -0
- package/src/analyzers/codeCloneDetector/detector.js +203 -0
- package/src/analyzers/codeCloneDetector/index.js +160 -0
- package/src/analyzers/codeCloneDetector/parser.js +199 -0
- package/src/analyzers/codeCloneDetector/reporter.js +148 -0
- package/src/analyzers/codeCloneDetector/scanner.js +59 -0
- package/src/core/agentPool.js +1474 -0
- package/src/core/agentScheduler.js +2147 -0
- package/src/core/contextManager.js +709 -0
- package/src/core/messageProcessor.js +732 -0
- package/src/core/orchestrator.js +548 -0
- package/src/core/stateManager.js +877 -0
- package/src/index.js +631 -0
- package/src/interfaces/cli.js +549 -0
- package/src/interfaces/webServer.js +2162 -0
- package/src/modules/fileExplorer/controller.js +280 -0
- package/src/modules/fileExplorer/index.js +37 -0
- package/src/modules/fileExplorer/middleware.js +92 -0
- package/src/modules/fileExplorer/routes.js +125 -0
- package/src/modules/fileExplorer/types.js +44 -0
- package/src/services/aiService.js +1232 -0
- package/src/services/apiKeyManager.js +164 -0
- package/src/services/benchmarkService.js +366 -0
- package/src/services/budgetService.js +539 -0
- package/src/services/contextInjectionService.js +247 -0
- package/src/services/conversationCompactionService.js +637 -0
- package/src/services/errorHandler.js +810 -0
- package/src/services/fileAttachmentService.js +544 -0
- package/src/services/modelRouterService.js +366 -0
- package/src/services/modelsService.js +322 -0
- package/src/services/qualityInspector.js +796 -0
- package/src/services/tokenCountingService.js +536 -0
- package/src/tools/agentCommunicationTool.js +1344 -0
- package/src/tools/agentDelayTool.js +485 -0
- package/src/tools/asyncToolManager.js +604 -0
- package/src/tools/baseTool.js +800 -0
- package/src/tools/browserTool.js +920 -0
- package/src/tools/cloneDetectionTool.js +621 -0
- package/src/tools/dependencyResolverTool.js +1215 -0
- package/src/tools/fileContentReplaceTool.js +875 -0
- package/src/tools/fileSystemTool.js +1107 -0
- package/src/tools/fileTreeTool.js +853 -0
- package/src/tools/imageTool.js +901 -0
- package/src/tools/importAnalyzerTool.js +1060 -0
- package/src/tools/jobDoneTool.js +248 -0
- package/src/tools/seekTool.js +956 -0
- package/src/tools/staticAnalysisTool.js +1778 -0
- package/src/tools/taskManagerTool.js +2873 -0
- package/src/tools/terminalTool.js +2304 -0
- package/src/tools/webTool.js +1430 -0
- package/src/types/agent.js +519 -0
- package/src/types/contextReference.js +972 -0
- package/src/types/conversation.js +730 -0
- package/src/types/toolCommand.js +747 -0
- package/src/utilities/attachmentValidator.js +292 -0
- package/src/utilities/configManager.js +582 -0
- package/src/utilities/constants.js +722 -0
- package/src/utilities/directoryAccessManager.js +535 -0
- package/src/utilities/fileProcessor.js +307 -0
- package/src/utilities/logger.js +436 -0
- package/src/utilities/tagParser.js +1246 -0
- package/src/utilities/toolConstants.js +317 -0
- package/web-ui/build/index.html +15 -0
- package/web-ui/build/logo.png +0 -0
- package/web-ui/build/logo2.png +0 -0
- package/web-ui/build/static/index-CjkkcnFA.js +344 -0
- package/web-ui/build/static/index-Dy2bYbOa.css +1 -0
|
@@ -0,0 +1,548 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Orchestrator - Central coordination hub for Loxia AI Agents System
|
|
3
|
+
*
|
|
4
|
+
* Purpose:
|
|
5
|
+
* - Unified request/response handling for all interfaces (CLI, Web, VSCode)
|
|
6
|
+
* - Agent lifecycle management
|
|
7
|
+
* - Session management
|
|
8
|
+
* - Error handling and response formatting
|
|
9
|
+
* - Interface-agnostic communication protocol
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import {
|
|
13
|
+
INTERFACE_TYPES,
|
|
14
|
+
AGENT_TYPES,
|
|
15
|
+
AGENT_STATUS,
|
|
16
|
+
MESSAGE_MODES,
|
|
17
|
+
ORCHESTRATOR_ACTIONS,
|
|
18
|
+
SYSTEM_DEFAULTS
|
|
19
|
+
} from '../utilities/constants.js';
|
|
20
|
+
|
|
21
|
+
class Orchestrator {
|
|
22
|
+
constructor(config, logger, agentPool, messageProcessor, aiService, stateManager) {
|
|
23
|
+
this.config = config;
|
|
24
|
+
this.logger = logger;
|
|
25
|
+
this.agentPool = agentPool;
|
|
26
|
+
this.messageProcessor = messageProcessor;
|
|
27
|
+
this.aiService = aiService;
|
|
28
|
+
this.stateManager = stateManager;
|
|
29
|
+
|
|
30
|
+
this.activeSessions = new Map();
|
|
31
|
+
this.requestHandlers = new Map();
|
|
32
|
+
|
|
33
|
+
this._initializeRequestHandlers();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Main entry point for all requests from client interfaces
|
|
38
|
+
* @param {Object} request - Request object with interface, sessionId, action, payload, projectDir, user
|
|
39
|
+
* @returns {Promise<Object>} Response object with success, data, error, metadata
|
|
40
|
+
*/
|
|
41
|
+
async processRequest(request) {
|
|
42
|
+
const startTime = Date.now();
|
|
43
|
+
|
|
44
|
+
try {
|
|
45
|
+
this._validateRequest(request);
|
|
46
|
+
|
|
47
|
+
const handler = this.requestHandlers.get(request.action);
|
|
48
|
+
if (!handler) {
|
|
49
|
+
this.logger.error(`Unknown action received: ${request.action}`, {
|
|
50
|
+
availableActions: Array.from(this.requestHandlers.keys()),
|
|
51
|
+
requestAction: request.action,
|
|
52
|
+
actionType: typeof request.action
|
|
53
|
+
});
|
|
54
|
+
throw new Error(`Unknown action: ${request.action}`);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Ensure session exists
|
|
58
|
+
await this._ensureSession(request.sessionId, request.projectDir);
|
|
59
|
+
|
|
60
|
+
// Execute request handler
|
|
61
|
+
const result = await handler.call(this, request);
|
|
62
|
+
|
|
63
|
+
// Generate response metadata
|
|
64
|
+
const metadata = {
|
|
65
|
+
timestamp: new Date().toISOString(),
|
|
66
|
+
executionTime: Date.now() - startTime,
|
|
67
|
+
sessionId: request.sessionId,
|
|
68
|
+
interface: request.interface
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return {
|
|
72
|
+
success: true,
|
|
73
|
+
data: result,
|
|
74
|
+
error: null,
|
|
75
|
+
metadata
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
} catch (error) {
|
|
79
|
+
this.logger.error(`Request processing failed: ${error.message}`, {
|
|
80
|
+
request: this._sanitizeRequestForLogging(request),
|
|
81
|
+
error: error.stack
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
return {
|
|
85
|
+
success: false,
|
|
86
|
+
data: null,
|
|
87
|
+
error: error.message,
|
|
88
|
+
metadata: {
|
|
89
|
+
timestamp: new Date().toISOString(),
|
|
90
|
+
executionTime: Date.now() - startTime,
|
|
91
|
+
sessionId: request.sessionId,
|
|
92
|
+
interface: request.interface
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Create a new agent with specified configuration
|
|
100
|
+
* @param {string} systemPrompt - Agent's system prompt
|
|
101
|
+
* @param {string} model - Preferred LLM model
|
|
102
|
+
* @param {Object} options - Additional agent configuration
|
|
103
|
+
* @returns {Promise<Object>} Created agent object
|
|
104
|
+
*/
|
|
105
|
+
async createAgent(systemPrompt, model, options = {}) {
|
|
106
|
+
const agentConfig = {
|
|
107
|
+
name: options.name || `Agent-${Date.now()}`,
|
|
108
|
+
type: options.type || AGENT_TYPES.USER_CREATED,
|
|
109
|
+
systemPrompt,
|
|
110
|
+
preferredModel: model,
|
|
111
|
+
capabilities: options.capabilities || [],
|
|
112
|
+
...options
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
const agent = await this.agentPool.createAgent(agentConfig);
|
|
116
|
+
|
|
117
|
+
this.logger.info(`Agent created: ${agent.id}`, {
|
|
118
|
+
agentId: agent.id,
|
|
119
|
+
name: agent.name,
|
|
120
|
+
model: agent.preferredModel
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
return agent;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Route message to specified agent
|
|
128
|
+
* @param {string} agentId - Target agent ID
|
|
129
|
+
* @param {string} message - Message content
|
|
130
|
+
* @param {Object} context - Message context (projectDir, contextReferences, etc.)
|
|
131
|
+
* @returns {Promise<Object>} Agent response
|
|
132
|
+
*/
|
|
133
|
+
async routeToAgent(agentId, message, context = {}) {
|
|
134
|
+
const agent = await this.agentPool.getAgent(agentId);
|
|
135
|
+
if (!agent) {
|
|
136
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Check if agent is paused
|
|
140
|
+
if (agent.status === AGENT_STATUS.PAUSED && agent.pausedUntil && new Date() < new Date(agent.pausedUntil)) {
|
|
141
|
+
throw new Error(`Agent ${agentId} is paused until ${agent.pausedUntil}`);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Process message through message processor (NEW ARCHITECTURE: just queues message)
|
|
145
|
+
const result = await this.messageProcessor.processMessage(agentId, message, context);
|
|
146
|
+
|
|
147
|
+
// NEW ARCHITECTURE: MessageProcessor just queues messages, actual processing happens in AgentScheduler
|
|
148
|
+
// Return immediate queuing confirmation to UI
|
|
149
|
+
if (result.success) {
|
|
150
|
+
const response = {
|
|
151
|
+
success: true,
|
|
152
|
+
data: {
|
|
153
|
+
message: `Message queued for agent processing`,
|
|
154
|
+
agentId: result.agentId,
|
|
155
|
+
queuedAt: result.queuedAt,
|
|
156
|
+
status: 'queued',
|
|
157
|
+
// Legacy fields for backward compatibility
|
|
158
|
+
toolResults: [],
|
|
159
|
+
agentRedirects: [],
|
|
160
|
+
currentModel: agent.currentModel
|
|
161
|
+
},
|
|
162
|
+
processingId: `queued-${Date.now()}`
|
|
163
|
+
};
|
|
164
|
+
|
|
165
|
+
this.logger.info(`Message queued for agent: ${agentId}`, {
|
|
166
|
+
agentName: agent.name,
|
|
167
|
+
messageLength: message.length,
|
|
168
|
+
sessionId: context.sessionId
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
return response;
|
|
172
|
+
} else {
|
|
173
|
+
return result; // Return error response as-is
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Get current session state
|
|
179
|
+
* @param {string} sessionId - Session identifier
|
|
180
|
+
* @returns {Promise<Object>} Session state object
|
|
181
|
+
*/
|
|
182
|
+
async getSessionState(sessionId) {
|
|
183
|
+
const session = this.activeSessions.get(sessionId);
|
|
184
|
+
if (!session) {
|
|
185
|
+
throw new Error(`Session not found: ${sessionId}`);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const agents = await this.agentPool.listActiveAgents();
|
|
189
|
+
const projectState = await this.stateManager.getProjectState(session.projectDir);
|
|
190
|
+
|
|
191
|
+
return {
|
|
192
|
+
sessionId,
|
|
193
|
+
projectDir: session.projectDir,
|
|
194
|
+
createdAt: session.createdAt,
|
|
195
|
+
lastActivity: session.lastActivity,
|
|
196
|
+
agents: agents, // Return full agent objects since listActiveAgents already filters appropriately
|
|
197
|
+
projectState
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Shutdown orchestrator and cleanup resources
|
|
203
|
+
* @returns {Promise<void>}
|
|
204
|
+
*/
|
|
205
|
+
async shutdown() {
|
|
206
|
+
this.logger.info('Shutting down orchestrator...');
|
|
207
|
+
|
|
208
|
+
// Save all agent states
|
|
209
|
+
const agents = await this.agentPool.listActiveAgents();
|
|
210
|
+
for (const agent of agents) {
|
|
211
|
+
await this.stateManager.persistAgentState(agent.id);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
// Clear active sessions
|
|
215
|
+
this.activeSessions.clear();
|
|
216
|
+
|
|
217
|
+
this.logger.info('Orchestrator shutdown complete');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Initialize request handlers for different actions
|
|
222
|
+
* @private
|
|
223
|
+
*/
|
|
224
|
+
_initializeRequestHandlers() {
|
|
225
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.CREATE_AGENT, this._handleCreateAgent.bind(this));
|
|
226
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.UPDATE_AGENT, this._handleUpdateAgent.bind(this));
|
|
227
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.DELETE_AGENT, this._handleDeleteAgent.bind(this));
|
|
228
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.SEND_MESSAGE, this._handleSendMessage.bind(this));
|
|
229
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.LIST_AGENTS, this._handleListAgents.bind(this));
|
|
230
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.RESUME_SESSION, this._handleResumeSession.bind(this));
|
|
231
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.GET_SESSION_STATE, this._handleGetSessionState.bind(this));
|
|
232
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.PAUSE_AGENT, this._handlePauseAgent.bind(this));
|
|
233
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.RESUME_AGENT, this._handleResumeAgent.bind(this));
|
|
234
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.SWITCH_MODEL, this._handleSwitchModel.bind(this));
|
|
235
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.GET_AGENT_STATUS, this._handleGetAgentStatus.bind(this));
|
|
236
|
+
this.requestHandlers.set(ORCHESTRATOR_ACTIONS.GET_AGENT_CONVERSATIONS, this._handleGetAgentConversations.bind(this));
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* Handle create agent requests
|
|
241
|
+
* @private
|
|
242
|
+
*/
|
|
243
|
+
async _handleCreateAgent(request) {
|
|
244
|
+
const { name, systemPrompt, model, capabilities, dynamicModelRouting, platformProvided, directoryAccess } = request.payload;
|
|
245
|
+
|
|
246
|
+
this.logger.info('Creating agent with payload', {
|
|
247
|
+
name,
|
|
248
|
+
model,
|
|
249
|
+
dynamicModelRouting,
|
|
250
|
+
platformProvided,
|
|
251
|
+
capabilities,
|
|
252
|
+
directoryAccess: directoryAccess ? {
|
|
253
|
+
workingDirectory: directoryAccess.workingDirectory,
|
|
254
|
+
readOnlyDirectories: directoryAccess.readOnlyDirectories?.length || 0,
|
|
255
|
+
writeEnabledDirectories: directoryAccess.writeEnabledDirectories?.length || 0,
|
|
256
|
+
restrictToProject: directoryAccess.restrictToProject
|
|
257
|
+
} : null
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
return await this.createAgent(systemPrompt, model, {
|
|
261
|
+
name,
|
|
262
|
+
capabilities,
|
|
263
|
+
dynamicModelRouting,
|
|
264
|
+
platformProvided,
|
|
265
|
+
directoryAccess,
|
|
266
|
+
sessionId: request.sessionId,
|
|
267
|
+
projectDir: request.projectDir
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Handle update agent requests
|
|
273
|
+
* @private
|
|
274
|
+
*/
|
|
275
|
+
async _handleUpdateAgent(request) {
|
|
276
|
+
const { agentId, updates } = request.payload;
|
|
277
|
+
|
|
278
|
+
if (!agentId) {
|
|
279
|
+
throw new Error('Agent ID is required for update');
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
if (!updates || typeof updates !== 'object') {
|
|
283
|
+
throw new Error('Updates object is required');
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Get agent before update to ensure it exists
|
|
287
|
+
const agent = await this.agentPool.getAgent(agentId);
|
|
288
|
+
if (!agent) {
|
|
289
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
this.logger.info('Updating agent with payload', {
|
|
293
|
+
agentId,
|
|
294
|
+
updates,
|
|
295
|
+
agentName: agent.name
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
// Update the agent through the agent pool
|
|
299
|
+
// Include sessionId in updates to maintain API key context
|
|
300
|
+
const updatesWithSession = {
|
|
301
|
+
...updates,
|
|
302
|
+
sessionId: request.sessionId // Ensure current session context is preserved
|
|
303
|
+
};
|
|
304
|
+
const updatedAgent = await this.agentPool.updateAgent(agentId, updatesWithSession);
|
|
305
|
+
|
|
306
|
+
this.logger.info(`Agent updated: ${agentId}`, {
|
|
307
|
+
agentName: updatedAgent.name,
|
|
308
|
+
newMode: updatedAgent.mode,
|
|
309
|
+
sessionId: request.sessionId
|
|
310
|
+
});
|
|
311
|
+
|
|
312
|
+
return updatedAgent;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/**
|
|
316
|
+
* Handle delete agent requests
|
|
317
|
+
* @private
|
|
318
|
+
*/
|
|
319
|
+
async _handleDeleteAgent(request) {
|
|
320
|
+
const { agentId } = request.payload;
|
|
321
|
+
|
|
322
|
+
if (!agentId) {
|
|
323
|
+
throw new Error('Agent ID is required for deletion');
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
// Get agent before deletion to return info
|
|
327
|
+
const agent = await this.agentPool.getAgent(agentId);
|
|
328
|
+
if (!agent) {
|
|
329
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
// Delete the agent
|
|
333
|
+
const result = await this.agentPool.deleteAgent(agentId);
|
|
334
|
+
|
|
335
|
+
this.logger.info(`Agent deleted: ${agentId}`, {
|
|
336
|
+
agentName: agent.name,
|
|
337
|
+
sessionId: request.sessionId
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
return {
|
|
341
|
+
success: true,
|
|
342
|
+
deletedAgent: {
|
|
343
|
+
id: agent.id,
|
|
344
|
+
name: agent.name
|
|
345
|
+
},
|
|
346
|
+
...result
|
|
347
|
+
};
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
/**
|
|
351
|
+
* Handle send message requests
|
|
352
|
+
* @private
|
|
353
|
+
*/
|
|
354
|
+
async _handleSendMessage(request) {
|
|
355
|
+
const { agentId, message, mode, contextReferences, apiKey, customApiKeys } = request.payload;
|
|
356
|
+
|
|
357
|
+
const context = {
|
|
358
|
+
projectDir: request.projectDir,
|
|
359
|
+
sessionId: request.sessionId,
|
|
360
|
+
interface: request.interface,
|
|
361
|
+
mode: mode || MESSAGE_MODES.CHAT,
|
|
362
|
+
contextReferences: contextReferences || [],
|
|
363
|
+
apiKey: apiKey, // Pass Loxia API key through context
|
|
364
|
+
customApiKeys: customApiKeys || {} // Pass custom API keys through context
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
return await this.routeToAgent(agentId, message, context);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
/**
|
|
371
|
+
* Handle list agents requests
|
|
372
|
+
* @private
|
|
373
|
+
*/
|
|
374
|
+
async _handleListAgents(request) {
|
|
375
|
+
return await this.agentPool.listActiveAgents();
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
/**
|
|
379
|
+
* Handle resume session requests
|
|
380
|
+
* @private
|
|
381
|
+
*/
|
|
382
|
+
async _handleResumeSession(request) {
|
|
383
|
+
const { projectDir } = request.payload;
|
|
384
|
+
|
|
385
|
+
const resumedState = await this.stateManager.resumeProject(projectDir);
|
|
386
|
+
|
|
387
|
+
// Restore agents to agent pool
|
|
388
|
+
for (const agent of resumedState.agents) {
|
|
389
|
+
await this.agentPool.restoreAgent(agent);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
return resumedState;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Handle get session state requests
|
|
397
|
+
* @private
|
|
398
|
+
*/
|
|
399
|
+
async _handleGetSessionState(request) {
|
|
400
|
+
return await this.getSessionState(request.sessionId);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* Handle pause agent requests
|
|
405
|
+
* @private
|
|
406
|
+
*/
|
|
407
|
+
async _handlePauseAgent(request) {
|
|
408
|
+
const { agentId, duration, reason } = request.payload;
|
|
409
|
+
|
|
410
|
+
return await this.agentPool.pauseAgent(agentId, duration, reason);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Handle resume agent requests
|
|
415
|
+
* @private
|
|
416
|
+
*/
|
|
417
|
+
async _handleResumeAgent(request) {
|
|
418
|
+
const { agentId } = request.payload;
|
|
419
|
+
|
|
420
|
+
return await this.agentPool.resumeAgent(agentId);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Handle switch model requests
|
|
425
|
+
* @private
|
|
426
|
+
*/
|
|
427
|
+
async _handleSwitchModel(request) {
|
|
428
|
+
const { agentId, newModel } = request.payload;
|
|
429
|
+
|
|
430
|
+
const agent = await this.agentPool.getAgent(agentId);
|
|
431
|
+
if (!agent) {
|
|
432
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
// Switch model via AI service conversation manager
|
|
436
|
+
return await this.aiService.switchAgentModel(agentId, newModel);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Handle get agent status requests
|
|
441
|
+
* @private
|
|
442
|
+
*/
|
|
443
|
+
async _handleGetAgentStatus(request) {
|
|
444
|
+
const { agentId } = request.payload;
|
|
445
|
+
|
|
446
|
+
const agent = await this.agentPool.getAgent(agentId);
|
|
447
|
+
if (!agent) {
|
|
448
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
return {
|
|
452
|
+
id: agent.id,
|
|
453
|
+
name: agent.name,
|
|
454
|
+
status: agent.status,
|
|
455
|
+
mode: agent.mode,
|
|
456
|
+
modeState: agent.modeState,
|
|
457
|
+
currentModel: agent.currentModel,
|
|
458
|
+
lastActivity: agent.lastActivity,
|
|
459
|
+
isPaused: agent.status === 'paused',
|
|
460
|
+
pausedUntil: agent.pausedUntil
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Handle get agent conversations requests
|
|
466
|
+
* @private
|
|
467
|
+
*/
|
|
468
|
+
async _handleGetAgentConversations(request) {
|
|
469
|
+
const { agentId } = request.payload;
|
|
470
|
+
|
|
471
|
+
const agent = await this.agentPool.getAgent(agentId);
|
|
472
|
+
if (!agent) {
|
|
473
|
+
throw new Error(`Agent not found: ${agentId}`);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
return {
|
|
477
|
+
agentId: agent.id,
|
|
478
|
+
conversations: agent.conversations,
|
|
479
|
+
messageCount: agent.conversations?.full?.messages?.length || 0
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
/**
|
|
484
|
+
* Validate request format
|
|
485
|
+
* @private
|
|
486
|
+
*/
|
|
487
|
+
_validateRequest(request) {
|
|
488
|
+
if (!request || typeof request !== 'object') {
|
|
489
|
+
throw new Error('Invalid request format');
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const requiredFields = ['interface', 'sessionId', 'action', 'payload'];
|
|
493
|
+
for (const field of requiredFields) {
|
|
494
|
+
if (!(field in request)) {
|
|
495
|
+
throw new Error(`Missing required field: ${field}`);
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const validInterfaces = Object.values(INTERFACE_TYPES);
|
|
500
|
+
if (!validInterfaces.includes(request.interface)) {
|
|
501
|
+
throw new Error(`Invalid interface: ${request.interface}`);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/**
|
|
506
|
+
* Ensure session exists, create if needed
|
|
507
|
+
* @private
|
|
508
|
+
*/
|
|
509
|
+
async _ensureSession(sessionId, projectDir) {
|
|
510
|
+
if (!this.activeSessions.has(sessionId)) {
|
|
511
|
+
const session = {
|
|
512
|
+
id: sessionId,
|
|
513
|
+
projectDir: projectDir || process.cwd(),
|
|
514
|
+
createdAt: new Date().toISOString(),
|
|
515
|
+
lastActivity: new Date().toISOString()
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
this.activeSessions.set(sessionId, session);
|
|
519
|
+
this.logger.info(`Session created: ${sessionId}`, { projectDir: session.projectDir });
|
|
520
|
+
} else {
|
|
521
|
+
// Update last activity
|
|
522
|
+
const session = this.activeSessions.get(sessionId);
|
|
523
|
+
session.lastActivity = new Date().toISOString();
|
|
524
|
+
}
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
/**
|
|
528
|
+
* Sanitize request for logging (remove sensitive data)
|
|
529
|
+
* @private
|
|
530
|
+
*/
|
|
531
|
+
_sanitizeRequestForLogging(request) {
|
|
532
|
+
const sanitized = { ...request };
|
|
533
|
+
|
|
534
|
+
// Remove potentially sensitive user data
|
|
535
|
+
if (sanitized.user) {
|
|
536
|
+
sanitized.user = { id: sanitized.user.id };
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
// Truncate large message content
|
|
540
|
+
if (sanitized.payload && sanitized.payload.message && sanitized.payload.message.length > 500) {
|
|
541
|
+
sanitized.payload.message = sanitized.payload.message.substring(0, 500) + '... [truncated]';
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
return sanitized;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
export default Orchestrator;
|