-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexecutor.ts
More file actions
703 lines (591 loc) · 21.4 KB
/
executor.ts
File metadata and controls
703 lines (591 loc) · 21.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import { EventEmitter } from 'events';
import { createSandbox } from './sandbox/index.js';
import { createMCPManager } from './mcp/index.js';
import { createToolsCoordinator } from './tools/index.js';
import { createSecurityManager } from './security/index.js';
import { createAuthenticationManager } from './auth/index.js';
import { createSchemaManager } from './schema/index.js';
import type {
Config,
ExecutionOptions,
ExecutionResult,
ExecutionRequest,
AuthContext
} from './types/core.js';
import { ErrorType } from './types/core.js';
import type { SecurityConfig } from './config/schema.js';
export interface ExecutorConfig {
config: Config;
mcpConfigPath?: string;
enableMCP?: boolean;
enableAuth?: boolean;
enableSchemaGeneration?: boolean;
}
export class CodeModeExecutor extends EventEmitter {
private config: Config;
private mcpConfigPath?: string;
private sandbox: any;
private mcpManager: any;
private toolsCoordinator: any;
private securityManager: any;
private authManager: any;
private schemaManager: any;
private initialized = false;
constructor(executorConfig: ExecutorConfig) {
super();
this.config = executorConfig.config;
this.mcpConfigPath = executorConfig.mcpConfigPath;
}
async initialize(): Promise<void> {
if (this.initialized) return;
console.log('🚀 Initializing Code Mode Executor...');
try {
// Initialize security manager first
this.securityManager = this.createSecurityManager(this.config.security);
// Initialize authentication
this.authManager = this.createAuthenticationManager(this.config.security);
// Initialize sandbox
this.sandbox = createSandbox(this.config.sandbox);
await this.sandbox.initialize();
// Initialize tools coordinator
this.toolsCoordinator = this.createToolsCoordinator();
// Load external MCP configuration if provided
let mcpConfig = null;
if (this.mcpConfigPath) {
try {
const mcpConfigData = await import('fs/promises').then(fs => fs.readFile(this.mcpConfigPath!, 'utf-8'));
const loadedConfig = JSON.parse(mcpConfigData);
// Handle both {mcpServers: ...} and {servers: ...} formats
const servers = loadedConfig.mcpServers || loadedConfig.servers || {};
// Normalize 'type' field to 'transport' for each server config
const normalizedServers: Record<string, any> = {};
for (const [name, config] of Object.entries(servers)) {
const cfg = config as any;
normalizedServers[name] = {
...cfg,
transport: cfg.type || cfg.transport || 'stdio'
};
// Remove the 'type' field alias
delete normalizedServers[name].type;
}
mcpConfig = { servers: normalizedServers };
console.log(`📄 Loaded MCP configuration from ${this.mcpConfigPath}`);
} catch (error) {
console.warn(`⚠️ Failed to load MCP config from ${this.mcpConfigPath}:`, error);
}
}
await this.toolsCoordinator.initialize(mcpConfig || this.config.mcp);
// Initialize MCP manager if enabled
if (mcpConfig || (this.config.mcp && Object.keys(this.config.mcp.servers || {}).length > 0)) {
this.mcpManager = createMCPManager(mcpConfig || this.config.mcp);
await this.mcpManager.initialize();
}
// Initialize schema manager if enabled
if (this.config.schema.enableAutoGeneration) {
this.schemaManager = createSchemaManager(this.config.schema);
await this.schemaManager.initialize();
// Connect schema manager to MCP changes
if (this.mcpManager) {
// MCP Manager doesn't emit events directly, the aggregator does
// We'll need to access the aggregator for events if needed in the future
}
}
this.setupEventHandlers();
this.initialized = true;
console.log('✅ Code Mode Executor initialized successfully');
} catch (error: unknown) {
console.error('❌ Failed to initialize Code Mode Executor:', error);
throw error;
}
}
async execute(request: ExecutionRequest): Promise<ExecutionResult> {
if (!this.initialized) {
throw new Error('Executor not initialized');
}
const requestId = request.requestId || this.generateRequestId();
const startTime = Date.now();
try {
// Security validation
const securityResult = this.securityManager.validateExecution(
request.code,
request.authContext,
request.options?.capabilities,
requestId
);
if (!securityResult.allowed) {
return {
success: false,
error: {
type: ErrorType.SECURITY,
code: 'SECURITY_VIOLATION',
message: 'Code execution denied by security policy',
details: { violations: securityResult.violations },
timestamp: new Date()
},
metrics: {
executionTime: Date.now() - startTime,
memoryUsed: 0,
cpuTime: 0,
apiCalls: 0,
startTime,
endTime: Date.now()
},
logs: [],
requestId
};
}
// Start security monitoring
this.securityManager.startExecution(requestId, {
...request.options,
capabilities: securityResult.effectiveCapabilities
});
// Prepare sandbox injection
const sandboxInjection = this.createSandboxInjection();
// Prepare execution code
const fullCode = this.prepareExecutionCode(request.code, sandboxInjection);
// Execute in sandbox (first pass to capture MCP calls)
const result = await this.sandbox.execute(fullCode, {
...request.options,
capabilities: securityResult.effectiveCapabilities
});
// Process any MCP calls that were made during execution
const processedResult = await this.processMCPCalls(result, request.code, sandboxInjection, {
...request.options,
capabilities: securityResult.effectiveCapabilities
});
// End security monitoring
const securityMetrics = this.securityManager.endExecution(requestId);
// Merge metrics
const finalResult: ExecutionResult = {
...processedResult,
metrics: {
...result.metrics,
...securityMetrics
},
requestId
};
this.emit('executionComplete', finalResult);
return finalResult;
} catch (error: unknown) {
// Handle execution errors
this.securityManager.endExecution(requestId);
const executionError: ExecutionResult = {
success: false,
error: {
type: ErrorType.RUNTIME,
code: 'EXECUTION_ERROR',
message: (error instanceof Error ? error.message : String(error)),
stack: error instanceof Error ? error.stack : undefined,
timestamp: new Date()
},
metrics: {
executionTime: Date.now() - startTime,
memoryUsed: 0,
cpuTime: 0,
apiCalls: 0,
startTime,
endTime: Date.now()
},
logs: [],
requestId
};
this.emit('executionError', executionError);
return executionError;
}
}
private createSandboxInjection(): string {
// Get MCP tools if available
const mcpTools = this.mcpManager ? this.mcpManager.getAvailableTools() : [];
const mcpServers = this.mcpManager ? this.mcpManager.getServerStatus() : [];
// Create MCP tool functions
let mcpInjection = '';
if (mcpTools.length > 0) {
mcpInjection = this.generateMCPInjection(mcpTools);
}
return `
// Code Mode Unified - Sandbox Runtime with MCP Integration
globalThis._logs = [];
globalThis._errors = [];
globalThis._debug = [];
globalThis.console = {
log: function(...args) {
const message = args.map(arg => {
if (typeof arg === 'object' && arg !== null) {
try { return JSON.stringify(arg); }
catch(e) { return String(arg); }
}
return String(arg);
}).join(' ');
globalThis._logs.push(message);
},
error: function(...args) {
const message = args.map(arg => String(arg)).join(' ');
globalThis._errors.push(message);
globalThis._logs.push('ERROR: ' + message);
},
warn: function(...args) {
const message = args.map(arg => String(arg)).join(' ');
globalThis._logs.push('WARN: ' + message);
},
debug: function(...args) {
const message = args.map(arg => String(arg)).join(' ');
globalThis._debug.push(message);
globalThis._logs.push('DEBUG: ' + message);
}
};
// Enhanced error handling
globalThis._handleError = function(error, context) {
const errorInfo = {
message: error.message || String(error),
stack: error.stack,
context: context,
timestamp: new Date().toISOString()
};
globalThis._errors.push(errorInfo);
globalThis._logs.push('EXECUTION ERROR in ' + context + ': ' + errorInfo.message);
return errorInfo;
};
// Execution tracking
globalThis._executionState = {
phase: 'initialization',
completedStatements: 0,
errors: 0,
mcpCalls: 0
};
${mcpInjection}
`;
}
private generateMCPInjection(mcpTools: any[]): string {
// Group tools by namespace
const toolsByNamespace = new Map<string, any[]>();
for (const tool of mcpTools) {
const [namespace] = tool.namespace.split('.', 1);
if (!toolsByNamespace.has(namespace)) {
toolsByNamespace.set(namespace, []);
}
toolsByNamespace.get(namespace)!.push(tool);
}
// Generate namespace objects with tool functions
const namespaceObjects = Array.from(toolsByNamespace.entries()).map(([namespace, tools]) => {
const toolFunctions = tools.map(tool => {
const toolName = tool.name;
return ` ${toolName}: function(args = {}) {
try {
globalThis._executionState.mcpCalls++;
console.debug('Calling MCP tool ${tool.namespace} with args:', JSON.stringify(args));
const result = globalThis.__mcpCallTool('${tool.namespace}', args);
console.debug('MCP tool ${tool.namespace} returned:', JSON.stringify(result));
return result;
} catch (error) {
const errorInfo = globalThis._handleError(error, 'MCP tool ${tool.namespace}');
console.error('MCP tool call failed for ${tool.namespace}:', errorInfo.message);
throw error;
}
}`;
}).join(',\n');
return ` ${namespace}: {
${toolFunctions}
}`;
}).join(',\n');
return `
// MCP Tools Integration
// MCP Tool Call Queue (for processing outside sandbox)
globalThis.__mcpCalls = [];
globalThis.__mcpCallId = 0;
globalThis.__mcpCallTool = function(namespace, args) {
const callId = ++globalThis.__mcpCallId;
const call = {
id: callId,
namespace: namespace,
args: args,
timestamp: new Date().toISOString()
};
globalThis.__mcpCalls.push(call);
// Log the call for processing
console.log('MCP_CALL_TRACKING: ' + JSON.stringify(call));
// Return a simple placeholder that can be assigned to variables
// The actual result will be substituted post-execution
return '__MCP_RESULT_' + callId + '__';
};
// MCP Global Object
globalThis.mcp = {
${namespaceObjects}
};
// Helper to list available tools
globalThis.mcp.listTools = function() {
const tools = [];
${Array.from(toolsByNamespace.entries()).map(([namespace, tools]) =>
tools.map(tool => ` tools.push({namespace: '${namespace}', name: '${tool.name}', description: '${tool.description}', fullName: '${tool.namespace}'});`).join('\n')
).join('\n')}
return tools;
};
`;
}
private prepareExecutionCode(userCode: string, injection: string): string {
// Check if it's a simple expression
const isExpression = !userCode.trim().includes(';') &&
!userCode.trim().startsWith('const ') &&
!userCode.trim().startsWith('let ') &&
!userCode.trim().startsWith('var ') &&
!userCode.trim().startsWith('function ') &&
!userCode.trim().startsWith('if ') &&
!userCode.trim().startsWith('for ') &&
!userCode.trim().startsWith('while ') &&
!userCode.trim().startsWith('{');
if (isExpression) {
// For simple expressions, add enhanced execution tracking
return `
// Code Mode Unified - Sandbox Runtime
${injection}
// User Code Execution (Expression)
globalThis._executionState.phase = 'expression-execution';
try {
console.debug('Executing expression:', ${JSON.stringify(userCode)});
const _result = ${userCode};
globalThis._executionState.phase = 'expression-complete';
console.debug('Expression result type:', typeof _result);
_result;
} catch (error) {
globalThis._handleError(error, 'user-expression');
throw error;
}
`;
} else {
// For complex code, enhanced statement execution with proper error handling
return `
// Code Mode Unified - Sandbox Runtime
${injection}
// User Code Execution (Statements)
globalThis._executionState.phase = 'statement-execution';
try {
console.debug('Executing statements:', ${JSON.stringify(userCode)});
// Execute user code with enhanced tracking
${userCode.split(';').map((stmt, i) =>
stmt.trim() ? `
try {
globalThis._executionState.completedStatements = ${i + 1};
console.debug('Executing statement ${i + 1}:', ${JSON.stringify(stmt.trim())});
${stmt.trim()};
} catch (stmtError) {
globalThis._handleError(stmtError, 'statement-${i + 1}');
throw stmtError;
}` : ''
).filter(Boolean).join('\n')}
globalThis._executionState.phase = 'statements-complete';
console.log('All statements completed successfully');
} catch (error) {
globalThis._executionState.phase = 'statements-failed';
globalThis._executionState.errors++;
globalThis._handleError(error, 'user-statements');
throw error;
}
`;
}
}
private setupEventHandlers(): void {
// Security event handlers
this.securityManager.on('resourceViolation', (violation: any) => {
this.emit('securityAlert', { type: 'resource', violation });
});
this.securityManager.on('securityViolation', (violation: any) => {
this.emit('securityAlert', { type: 'security', violation });
});
// Authentication event handlers
this.authManager.on('userAuthenticated', (authContext: AuthContext) => {
this.emit('userAuthenticated', authContext);
});
this.authManager.on('userLoggedOut', (sessionId: string) => {
this.emit('userLoggedOut', sessionId);
});
// MCP event handlers - Skip for now as MCPManager doesn't extend EventEmitter
// Events are logged by the MCPManager internally
}
// Get execution capabilities available to user
getCapabilities(authContext?: AuthContext): any {
const tools = this.toolsCoordinator ? this.toolsCoordinator.getAllTools() : { native: [], mcp: [] };
const mcpServers = this.mcpManager ? this.mcpManager.getServerStatus() : [];
const securityPolicy = this.securityManager.policyEngine?.getEffectivePolicy?.(authContext) || null;
return {
tools,
mcpServers,
securityPolicy: securityPolicy ? {
id: securityPolicy.id,
name: securityPolicy.name,
capabilities: securityPolicy.capabilities
} : null,
sandbox: {
runtime: this.config.sandbox.runtime,
limits: this.config.sandbox.limits
}
};
}
// Get system health
getHealth(): any {
const health: any = {
status: this.initialized ? 'healthy' : 'initializing',
components: {}
};
if (this.sandbox) {
health.components.sandbox = { status: 'healthy' };
}
if (this.securityManager) {
health.components.security = this.securityManager.getSecurityHealth();
}
if (this.authManager) {
health.components.auth = this.authManager.getAuthStats();
}
if (this.mcpManager) {
health.components.mcp = {
servers: this.mcpManager.getServerStatus(),
tools: this.mcpManager.getAvailableTools().length
};
}
return health;
}
// Authentication methods
async authenticate(token: string): Promise<{ success: boolean; authContext?: AuthContext; error?: string }> {
if (!this.authManager) {
return { success: false, error: 'Authentication not configured' };
}
return this.authManager.authenticateWithJWT(token);
}
async createUserSession(userId: string, scopes: string[]): Promise<{ success: boolean; token?: string; error?: string }> {
if (!this.authManager) {
return { success: false, error: 'Authentication not configured' };
}
const result = await this.authManager.createUserSession(userId, scopes);
return {
success: result.success,
token: result.authContext?.metadata?.accessToken as string,
error: result.error
};
}
private createSecurityManager(config: SecurityConfig): any {
return createSecurityManager(config);
}
private createAuthenticationManager(config: SecurityConfig): any {
return createAuthenticationManager({
provider: 'jwt' as const,
jwt: config.auth || {} as any
});
}
private createToolsCoordinator(): any {
return createToolsCoordinator();
}
private generateRequestId(): string {
return `req_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
}
async shutdown(): Promise<void> {
console.log('🔄 Shutting down Code Mode Executor...');
const shutdownPromises = [];
if (this.sandbox) {
shutdownPromises.push(this.sandbox.shutdown());
}
if (this.mcpManager) {
shutdownPromises.push(this.mcpManager.shutdown());
}
if (this.toolsCoordinator) {
shutdownPromises.push(this.toolsCoordinator.shutdown());
}
if (this.securityManager) {
shutdownPromises.push(this.securityManager.shutdown());
}
if (this.authManager) {
shutdownPromises.push(this.authManager.shutdown());
}
if (this.schemaManager) {
shutdownPromises.push(this.schemaManager.shutdown());
}
await Promise.allSettled(shutdownPromises);
this.removeAllListeners();
this.initialized = false;
console.log('✅ Code Mode Executor shutdown complete');
}
private async processMCPCalls(
result: ExecutionResult,
originalCode: string,
sandboxInjection: string,
executionOptions: any
): Promise<ExecutionResult> {
if (!this.mcpManager || !result.logs) {
return result;
}
// Extract MCP calls from tracking logs
const trackingLogs = result.logs.filter(log => log.startsWith('MCP_CALL_TRACKING: '));
if (trackingLogs.length === 0) {
return result;
}
try {
// Parse all MCP calls from tracking logs
const mcpCalls = trackingLogs.map(log => {
const callData = log.replace('MCP_CALL_TRACKING: ', '');
return JSON.parse(callData);
});
console.log(`Processing ${mcpCalls.length} MCP calls...`);
const mcpResults = [];
const callMap = new Map<number, any>();
// Execute all MCP calls and collect results
for (const call of mcpCalls) {
try {
console.log(`Calling MCP tool: ${call.namespace} with args:`, call.args);
const mcpResult = await this.mcpManager.callTool(call.namespace, call.args);
mcpResults.push({
callId: call.id,
namespace: call.namespace,
success: true,
result: mcpResult
});
callMap.set(call.id, mcpResult);
console.log(`MCP tool ${call.namespace} returned:`, mcpResult);
} catch (error) {
console.error(`MCP tool call failed for ${call.namespace}:`, error);
const errorResult = {
callId: call.id,
namespace: call.namespace,
success: false,
error: error instanceof Error ? error.message : String(error)
};
mcpResults.push(errorResult);
callMap.set(call.id, { error: errorResult.error });
}
}
// Inject MCP results into globalThis for second pass execution
console.log(`Injecting ${callMap.size} MCP results into globalThis...`);
// Create modified sandbox injection that includes MCP results
const mcpResultsInjection = `
globalThis.__mcpResults = ${JSON.stringify(Object.fromEntries(callMap))};
`;
// Prepend MCP results to the sandbox injection
const modifiedSandboxInjection = mcpResultsInjection + sandboxInjection.replace(
'return \'__MCP_RESULT_\' + callId + \'__\';',
`if (globalThis.__mcpResults && globalThis.__mcpResults[callId]) {
return globalThis.__mcpResults[callId];
}
return '__MCP_RESULT_' + callId + '__';`
);
// Re-execute with the actual MCP results available
if (mcpResults.length > 0) {
console.log('Re-executing code with MCP results injected into globalThis...');
const fullModifiedCode = this.prepareExecutionCode(originalCode, modifiedSandboxInjection);
const finalResult = await this.sandbox.execute(fullModifiedCode, executionOptions);
return {
...finalResult,
mcpCalls: mcpResults,
logs: [...(result.logs || []), ...(finalResult.logs || [])]
};
}
// No MCP results to process
return {
...result,
mcpCalls: mcpResults
};
} catch (error) {
console.error('Error processing MCP calls:', error);
return result;
}
}
}
// Factory function
export function createExecutor(config: ExecutorConfig): CodeModeExecutor {
return new CodeModeExecutor(config);
}