-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.js
More file actions
1093 lines (932 loc) · 36.8 KB
/
index.js
File metadata and controls
1093 lines (932 loc) · 36.8 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
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* @fileoverview Main orchestration module for the AI agent system.
* Manages tool loading, execution flow, and user interactions.
*/
const fs = require('fs');
const path = require('path');
const ascii = require('./utils/ascii');
const contextManager = require('./utils/context');
const logger = require('./utils/logger');
const config = require('./utils/config');
const io = require('./socket');
const sanitize = require('sanitize-filename');
const prompts = require('./tools/AI/prompts');
const ai = require('./tools/AI/ai');
const { taskStepFunctions } = require('./database');
require('dotenv').config();
const SCREENSHOT_INTERVAL = config.tasks.screenshotInterval;
/**
* Sanitizes a path by ensuring it contains only safe characters.
* @param {string} unsafePath - The potentially unsafe path to sanitize.
* @returns {string} A sanitized version of the path.
*/
function sanitizePath(unsafePath) {
if (!unsafePath) return '';
const isAbsolute = path.isAbsolute(unsafePath);
const pathSegments = unsafePath.split(/[\/\\]/g).map(segment => sanitize(segment));
let safePath = pathSegments.join(path.sep);
if (isAbsolute && !path.isAbsolute(safePath)) {
safePath = path.resolve('/', safePath);
}
return safePath;
}
/**
* Sanitizes a file path and ensures it's within the user's output directory.
* @param {string} unsafePath - The potentially unsafe path to sanitize.
* @param {string} userId - The user ID to determine the output directory.
* @returns {string} A sanitized file path within the user's directory.
*/
function sanitizeFilePath(unsafePath, userId) {
let safePath = sanitizePath(unsafePath);
if (userId && userId !== 'default') {
const userDir = path.join('output', userId);
if (!fs.existsSync(userDir)) {
fs.mkdirSync(userDir, { recursive: true });
}
if (!safePath.startsWith(userDir)) {
const filename = path.basename(safePath);
safePath = path.join(userDir, filename);
}
}
return safePath;
}
// Storage for loaded tools and their descriptions
const toolsDirectory = path.join(__dirname, 'tools');
const tools = {};
const toolDescriptions = [];
const toolConfigs = {};
/**
* Loads all enabled tools from the tools directory.
* Each tool must have a valid tool.json configuration file.
*/
function loadTools() {
const toolFolders = fs.readdirSync(toolsDirectory).filter(folder => {
const stat = fs.statSync(path.join(toolsDirectory, folder));
return stat.isDirectory();
});
toolFolders.forEach(folder => {
const toolJsonPath = path.join(toolsDirectory, folder, 'tool.json');
try {
if (fs.existsSync(toolJsonPath)) {
const toolConfig = JSON.parse(fs.readFileSync(toolJsonPath, 'utf8'));
if (toolConfig.enabled) {
let mainFile = toolConfig.main || 'main.js';
if (mainFile === 'main.js' && !fs.existsSync(path.join(toolsDirectory, folder, mainFile))) {
if (fs.existsSync(path.join(toolsDirectory, folder, 'index.js'))) {
mainFile = 'index.js';
} else {
logger.warn('Main file not found for tool, skipping', { mainFile, folder });
return;
}
}
const toolModule = require(path.join(toolsDirectory, folder, mainFile));
tools[toolConfig.title] = toolModule;
toolConfigs[toolConfig.title] = toolConfig;
toolDescriptions.push({
title: toolConfig.title,
description: toolConfig.description,
example: toolConfig.example
});
logger.tool(toolConfig.title, 'Tool loaded successfully');
// Initialize browser tool if it's the one we just loaded
if (toolConfig.title === 'webBrowser' && folder === 'browser') {
// Look for the initialize module
const initializePath = path.join(toolsDirectory, folder, 'initialize.js');
if (fs.existsSync(initializePath)) {
const initModule = require(initializePath);
if (typeof initModule.initialize === 'function') {
// Initialize async but don't block startup
initModule.initialize().catch(err => {
logger.error('Failed to initialize browser tool', { error: err.message });
});
}
}
}
}
} else {
logger.warn('No tool.json found, skipping tool folder', { folder });
}
} catch (error) {
logger.error('Error loading tool', { folder, error: error.message });
}
});
// Set tool descriptions in the prompts module
prompts.setToolDescriptions(toolDescriptions);
}
// Initialize the application
ascii.printWelcome();
loadTools();
/**
* Main orchestrator for processing user requests.
* @param {string} question - The user's question or request.
* @param {string} userId - The user ID (default: 'default').
* @param {number} chatId - The chat ID (default: 1).
* @param {boolean} isFollowUp - Whether this is a follow-up request (default: false).
* @returns {string} The final response to the user.
*/
async function centralOrchestrator(question, userId, chatId = 1, isFollowUp = false) {
try {
// Validate inputs
if (!question || typeof question !== 'string') {
throw new Error('Invalid question format');
}
if (!userId) {
throw new Error('Authentication required: valid user ID is mandatory for production use');
}
chatId = parseInt(chatId, 10);
if (isNaN(chatId) || chatId < 1) {
chatId = 1;
}
// Check if a task is already running for this user
if (contextManager.isTaskRunning(userId, chatId)) {
logger.warn('Task already running', { userId, chatId });
io.to(`user:${userId}`).emit('task_error', {
userId,
chatId,
error: 'A task is already running. Please wait for it to complete before submitting a new task.'
});
return 'Task already running. Please wait for completion.';
}
// Initialize or continue context first
if (!isFollowUp) {
contextManager.resetContext(userId, chatId);
}
// Clear any previous cancellation history when starting a new task
contextManager.clearCancellationHistory(userId, chatId);
// Create AbortController for cancellation support
const abortController = new AbortController();
contextManager.setCancellationToken(abortController, userId, chatId);
// Set task as running only once after context is initialized/reset
contextManager.setTaskRunning(true, userId, chatId);
// Notify user that task is received
io.to(`user:${userId}`).emit('task_received', { userId, chatId, task: question, isFollowUp });
io.to(`user:${userId}`).emit('status_update', { userId, chatId, status: 'Improving prompt' });
// Set the question in context
contextManager.setQuestion(question, userId, chatId);
// Check for cancellation
if (contextManager.isTaskCancelled(userId, chatId)) {
throw new Error('Task cancelled by user');
}
// Generate planning prompt
const history = contextManager.getHistoryWithChatId(userId, chatId);
const prompt = await prompts.generatePlanningPrompt(question, history, userId);
io.to(`user:${userId}`).emit('status_update', { userId, chatId, status: 'Planning task execution' });
// Check for cancellation
if (contextManager.isTaskCancelled(userId, chatId)) {
throw new Error('Task cancelled by user');
}
// Get plan from AI using planning model
let planObject = await ai.callAI(prompt, question, history, undefined, true, "planning", userId, chatId);
// Handle direct answers without complex planning
if (planObject.directAnswer === true && planObject.answer) {
return await handleDirectAnswer(planObject, question, userId, chatId);
}
// Process complex tasks with multi-step plan
return await executeTaskPlan(planObject, question, userId, chatId, isFollowUp);
} catch (error) {
console.error("Critical error in orchestration:", error.message);
// Check if this was a cancellation
const isCancelled = error.message === 'Task cancelled by user';
// Clean up task state
contextManager.setTaskRunning(false, userId, chatId);
contextManager.clearCancellationToken(userId, chatId);
// Send appropriate message to client
if (isCancelled) {
io.to(`user:${userId}`).emit('task_cancelled', { userId, chatId, message: 'Task cancelled successfully' });
} else {
io.to(`user:${userId}`).emit('task_error', { userId, chatId, error: error.message });
}
try {
await cleanupUserResources(userId);
} catch (cleanupError) {
console.error("Error during cleanup:", cleanupError.message);
}
return isCancelled ? 'Task cancelled by user' : `Critical error occurred during execution: ${error.message}`;
}
}
/**
* Handles direct answers without complex planning.
* @param {Object} planObject - The plan object with directAnswer property.
* @param {string} question - The user's question.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
* @returns {string} The direct answer.
*/
async function handleDirectAnswer(planObject, question, userId, chatId) {
io.to(`user:${userId}`).emit('status_update', { userId, chatId, status: 'Direct answer provided' });
// Add to conversation history
contextManager.addToHistory({
role: "user",
content: [
{type: "text", text: question}
]
}, userId, chatId);
contextManager.addToHistory({
role: "assistant",
content: [
{type: "text", text: planObject.answer}
]
}, userId, chatId);
// Emit task completion for direct answers
io.to(`user:${userId}`).emit('task_completed', {
userId,
chatId,
result: cleanJsonResponses(planObject.answer),
duration: 0,
completedAt: new Date().toISOString(),
outputFiles: { host: [], container: [] },
metrics: {
stepCount: 0,
successCount: 0,
totalSteps: 0,
durationSeconds: 0,
averageStepTime: 0
}
});
// Mark task as no longer running
contextManager.setTaskRunning(false, userId, chatId);
contextManager.clearCancellationToken(userId, chatId);
await cleanupUserResources(userId);
return planObject.answer;
}
/**
* Executes a multi-step task plan.
* @param {Object} planObject - The plan object from the AI.
* @param {string} question - The user's question.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
* @param {boolean} isFollowUp - Whether this is a follow-up request.
* @returns {string} The final response after task execution.
*/
async function executeTaskPlan(planObject, question, userId, chatId, isFollowUp) {
// Extract plan steps
const plan = Object.values(planObject).filter(item => item && typeof item === 'object');
// Store plan in context
contextManager.setPlan(plan, userId, chatId);
// Add to conversation history
contextManager.addToHistory({
role: "user",
content: [
{type: "text", text: question}
]
}, userId, chatId);
contextManager.addToHistory({
role: "assistant",
content: [
{type: "text", text: isFollowUp ?
JSON.stringify(planObject) :
(planObject.directAnswer === true ? planObject.answer : "Processing your task...")}
]
}, userId, chatId);
// Check if email service is available
const emailService = require('./utils/emailService');
const emailServiceAvailable = emailService.isEmailServiceAvailable();
// Emit steps to client
io.to(`user:${userId}`).emit('steps', { userId, chatId, plan, emailServiceAvailable });
// Store steps in database for history reconstruction
try {
await taskStepFunctions.storeTaskSteps(userId, chatId, plan);
} catch (error) {
console.error('Error storing task steps:', error.message);
}
// Setup screenshot interval if using browser
let screenshotInterval = setupScreenshotInterval(plan, userId, chatId);
// Execute each step in the plan
await executeSteps(plan, question, userId, chatId);
// Clear screenshot interval if set
if (screenshotInterval) {
clearInterval(screenshotInterval);
}
// Finalize and return results
return await finalizeAndReturn(question, plan, userId, chatId);
}
/**
* Sets up screenshot interval for browser-based tools.
* @param {Array} plan - The execution plan.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
* @returns {number|null} The interval ID or null if not using browser.
*/
function setupScreenshotInterval(plan, userId, chatId) {
if (plan.some(step => step.action === "webBrowser") && tools.webBrowser) {
return setInterval(async () => {
try {
const screenshot = await tools.webBrowser.takeScreenshot(userId);
if (screenshot) {
io.to(`user:${userId}`).emit('browser_screenshot', { userId, chatId, screenshot });
}
} catch (error) {
console.error("Error taking screenshot:", error.message);
}
}, SCREENSHOT_INTERVAL);
}
return null;
}
/**
* Executes all steps in the plan sequentially.
* @param {Array} plan - The execution plan.
* @param {string} question - The user's question.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
*/
async function executeSteps(plan, question, userId, chatId) {
while (contextManager.getCurrentStepIndex(userId, chatId) < plan.length) {
// Check for cancellation before each step
if (contextManager.isTaskCancelled(userId, chatId)) {
throw new Error('Task cancelled by user');
}
const currentStepIndex = contextManager.getCurrentStepIndex(userId, chatId);
const step = plan[currentStepIndex];
// Check if reasoning should be skipped for this tool
const toolConfig = toolConfigs[step.action];
const shouldSkipReasoning = toolConfig?.skipReasoning === true;
let enhancedStep;
if (shouldSkipReasoning) {
// Skip reasoning, use step as-is
io.to(`user:${userId}`).emit('status_update', { userId, chatId, status: `Executing: ${step.step} using ${step.action}` });
enhancedStep = step;
} else {
// Reason about the step
io.to(`user:${userId}`).emit('status_update', { userId, chatId, status: `Reasoning about: ${step.step}` });
enhancedStep = await tools.react.processStep(step, userId, chatId);
// Execute the step
io.to(`user:${userId}`).emit('status_update', { userId, chatId, status: `Executing: ${enhancedStep.step} using ${enhancedStep.action}` });
}
// Get filtered step outputs based on data dependencies
const filteredStepsOutput = contextManager.getFilteredStepsOutput(enhancedStep.usingData, userId, chatId);
const inputData = enhancedStep.usingData === "none" ? "" : filteredStepsOutput.map(item => `${item.action}: ${item.output}`).join("; ");
// Execute the appropriate tool
const summary = await executeToolAction(enhancedStep, inputData, currentStepIndex, plan, userId, chatId);
// Emit step completion
emitStepCompletion(enhancedStep, currentStepIndex, plan, userId, chatId);
// Update step status in database
try {
await taskStepFunctions.updateStepStatus(userId, chatId, currentStepIndex, 'completed');
} catch (error) {
console.error('Error updating step status:', error.message);
}
// Store step output
const stepOutput = {
step: enhancedStep.step,
action: enhancedStep.action,
output: summary
};
contextManager.addStepOutput(stepOutput, userId, chatId);
// Reflect on result (only if reasoning wasn't skipped)
if (!shouldSkipReasoning) {
io.to(`user:${userId}`).emit('status_update', { userId, chatId, status: `Reflecting on: ${enhancedStep.step}` });
const reflection = await tools.react.reflectOnResult(enhancedStep, summary, userId, chatId);
// Check if plan needs to be updated
if (reflection && reflection.changePlan === true) {
await updatePlanIfNeeded(question, userId, chatId);
}
}
// Move to next step
contextManager.incrementStepIndex(userId, chatId);
// Periodically save thought chain (only if reasoning was used)
if (!shouldSkipReasoning && (currentStepIndex % 3 === 0 || currentStepIndex === plan.length - 1)) {
await tools.react.saveThoughtChain(tools.fileSystem, userId, chatId);
}
}
}
/**
* Executes a specific tool action for a step.
* @param {Object} enhancedStep - The enhanced step details.
* @param {string} inputData - Input data for the tool.
* @param {number} currentStepIndex - Current step index.
* @param {Array} plan - The execution plan.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
* @returns {Object} The result of the tool execution.
*/
async function executeToolAction(enhancedStep, inputData, currentStepIndex, plan, userId, chatId) {
const tool = tools[enhancedStep.action];
if (!tool) {
console.error(`Tool '${enhancedStep.action}' not found or not enabled`);
return {
error: `Tool '${enhancedStep.action}' not found or not enabled`,
success: false
};
}
// Handle chat completion tool
if (enhancedStep.action === "chatCompletion") {
const updatedHistory = contextManager.getHistoryWithChatId(userId, chatId);
const modelToUse = enhancedStep.model || "auto";
const summary = await tool.callAI(enhancedStep.step, inputData, updatedHistory, undefined, true, modelToUse, userId, chatId);
contextManager.addToHistory({
role: "assistant",
content: [
{type: "text", text: JSON.stringify(summary)}
]
}, userId, chatId);
return summary;
}
// Handle research tools
if (["deepResearch", "webSearch"].includes(enhancedStep.action)) {
const intensity = enhancedStep.intensity || undefined;
return await tool.runTask(enhancedStep.step, inputData, (summary) => {
emitStepCompletion(enhancedStep, currentStepIndex, plan, userId, chatId);
}, userId, chatId, intensity);
}
// Handle writer tool
if (enhancedStep.action === "writer") {
const summary = await tool.write(
`${enhancedStep.step} Expected output: ${enhancedStep.expectedOutput}`,
inputData,
userId,
chatId
);
if (summary && summary.error) {
console.error(`Writer error: ${summary.error}`, summary.details || '');
return {
error: summary.error,
success: false,
partial: "Writer tool failed to generate content. See logs for details."
};
}
return summary;
}
// Handle other tools
const summary = await tool.runTask(
`${enhancedStep.step} Expected output: ${enhancedStep.expectedOutput}`,
inputData,
(summary) => {
emitStepCompletion(enhancedStep, currentStepIndex, plan, userId, chatId);
// Handle file system updates
if (enhancedStep.action === "fileSystem" && summary && summary.filePath) {
io.to(`user:${userId}`).emit('file_updated', {
userId,
chatId,
filePath: summary.filePath,
content: summary.content || 'File created/updated'
});
}
},
userId,
chatId
);
// Track container files if needed
if (["execute", "bash"].includes(enhancedStep.action) &&
summary && Array.isArray(summary.createdContainerFiles) &&
tools.fileSystem) {
for (const containerPath of summary.createdContainerFiles) {
try {
await tools.fileSystem.trackContainerFile(userId, containerPath, chatId);
} catch (trackingError) {
console.warn(`Failed to track container file ${containerPath}: ${trackingError.message}`);
}
}
}
return summary;
}
/**
* Emits step completion status to the client.
* @param {Object} step - The step that completed.
* @param {number} currentStepIndex - Current step index.
* @param {Array} plan - The execution plan.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
*/
function emitStepCompletion(step, currentStepIndex, plan, userId, chatId) {
io.to(`user:${userId}`).emit('step_completed', {
userId,
chatId,
step: step.step,
action: step.action,
metrics: {
stepIndex: currentStepIndex,
stepCount: currentStepIndex + 1,
totalSteps: plan.length,
successCount: contextManager.getStepsOutput(userId, chatId).filter(step =>
step && step.output && !step.output.error && step.output.success !== false
).length
}
});
}
/**
* Updates the plan if needed based on reflection.
* @param {string} question - The user's question.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
*/
async function updatePlanIfNeeded(question, userId, chatId) {
try {
const currentPlan = contextManager.getPlan(userId, chatId);
const currentStepIndex = contextManager.getCurrentStepIndex(userId, chatId);
const stepsOutput = contextManager.getStepsOutput(userId, chatId);
const updatedPlan = await checkProgress(question, currentPlan, stepsOutput, currentStepIndex, userId, chatId);
if (updatedPlan !== currentPlan) {
contextManager.updatePlan(updatedPlan, userId, chatId);
io.to(`user:${userId}`).emit('steps', { userId, chatId, plan: updatedPlan });
}
} catch (error) {
console.error("Error updating plan based on reflection:", error.message);
}
}
/**
* Finalizes task execution and returns results to the user.
* @param {string} question - The user's question.
* @param {Array} plan - The execution plan.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
* @returns {string} The final response.
*/
async function finalizeAndReturn(question, plan, userId, chatId) {
let finalOutput;
try {
const stepsOutput = contextManager.getStepsOutput(userId, chatId);
finalOutput = await finalizeTask(question, stepsOutput, userId, chatId);
// Clean up conversation history
const context = contextManager.getContext(userId, chatId);
const processedHistory = context.history.filter(msg =>
!(msg.role === "assistant" && msg.content &&
msg.content.length > 0 &&
msg.content[0].text === "Processing your task...")
);
context.history = processedHistory;
// Add final response to history
contextManager.addToHistory({
role: "assistant",
content: [
{type: "text", text: finalOutput}
]
}, userId, chatId);
// Get task metrics
const duration = contextManager.getTaskDuration(userId, chatId);
const fileData = await tools.fileSystem.getWrittenFiles(userId, chatId);
// Process host files
const hostFiles = fileData.hostFiles.map(file => ({
id: file.id,
fileName: file.fileName,
path: file.path,
content: file.content
})) || [];
// Process container files
const containerFiles = fileData.containerFiles.map(file => ({
id: file.id,
fileName: file.originalName || (file.containerPath ? file.containerPath.split('/').pop() : `file_${file.id}`),
path: file.containerPath,
content: file.fileContent
})) || [];
// Check for email notification preference and send email if task has >2 steps
if (plan.length > 2) {
try {
const { settingsFunctions, userFunctions } = require('./database');
const emailNotifications = await settingsFunctions.getSetting(userId, `email_notifications_${chatId}`);
if (emailNotifications === 'true') {
const user = await userFunctions.getUserById(userId);
if (user && user.email) {
const emailService = require('./utils/emailService');
await emailService.sendTaskCompletionEmail(
user.email,
question,
finalOutput,
plan.length,
duration
);
}
}
} catch (emailError) {
console.error('Error sending task completion email:', emailError.message);
}
}
// Emit task completion
io.to(`user:${userId}`).emit('task_completed', {
userId,
chatId,
result: cleanJsonResponses(finalOutput),
duration: duration,
completedAt: new Date().toISOString(),
outputFiles: {
host: hostFiles,
container: containerFiles
},
metrics: {
stepCount: contextManager.getStepsOutput(userId, chatId).length,
successCount: contextManager.getStepsOutput(userId, chatId).filter(step =>
step && step.output && !step.output.error && step.output.success !== false
).length,
totalSteps: plan.length,
durationSeconds: Math.round(duration / 1000),
averageStepTime: Math.round(duration / contextManager.getStepsOutput(userId, chatId).length / 1000)
}
});
} catch (error) {
console.error("Error finalizing task:", error.message);
finalOutput = "Task completed but could not be finalized: " + error.message;
io.to(`user:${userId}`).emit('task_error', { userId, chatId, error: error.message });
}
// Mark task as no longer running
contextManager.setTaskRunning(false, userId, chatId);
contextManager.clearCancellationToken(userId, chatId);
await cleanupUserResources(userId);
return finalOutput;
}
/**
* Executes a promise with a timeout.
* @param {Promise} promise - The promise to execute.
* @param {number} timeoutMs - Timeout in milliseconds (default: 60000).
* @returns {Promise} The result of the promise or a timeout error.
*/
async function withTimeout(promise, timeoutMs = 60000) {
let timeoutId;
const timeoutPromise = new Promise((_, reject) => {
timeoutId = setTimeout(() => {
reject(new Error(`Operation timed out after ${timeoutMs}ms`));
}, timeoutMs);
});
try {
return await Promise.race([promise, timeoutPromise]);
} finally {
clearTimeout(timeoutId);
}
}
/**
* Analyzes task progress and potentially updates the execution plan.
* @param {string} question - The original user question.
* @param {Array} plan - The current execution plan.
* @param {Array} stepsOutput - The output from steps executed so far.
* @param {number} currentStepIndex - The current step index.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
* @returns {Array} The original or updated plan.
*/
async function checkProgress(question, plan, stepsOutput, currentStepIndex, userId = 'default', chatId = 1) {
try {
// Skip if too early in execution or plan is too short
if (currentStepIndex < 2 || plan.length <= 2) {
return plan;
}
// Only check every 3 steps to avoid excessive replanning
if (currentStepIndex % 3 !== 0) {
return plan;
}
const prompt = prompts.generateProgressAnalysisPrompt(question, plan, stepsOutput, currentStepIndex);
const history = contextManager.getHistoryWithChatId(userId, chatId);
const response = await withTimeout(
ai.callAI(prompt, "Analyze task progress and suggest plan changes", history, undefined, true, "reflection", userId, chatId),
30000
);
if (!response || response.error || response === "NO_CHANGES_NEEDED") {
return plan;
}
const updatedPlan = Array.isArray(response) ? response : Object.values(response);
if (!Array.isArray(updatedPlan) || updatedPlan.length === 0) {
console.error("Invalid updated plan format");
return plan;
}
return updatedPlan;
} catch (error) {
console.error("Error checking progress:", error.message);
return plan;
}
}
/**
* Generates a final response after task completion.
* @param {string} question - The original user question.
* @param {Array} stepsOutput - The output from all executed steps.
* @param {string} userId - The user ID.
* @param {number} chatId - The chat ID.
* @returns {string} The final response to the user.
*/
async function finalizeTask(question, stepsOutput, userId = 'default', chatId = 1) {
try {
const prompt = prompts.generateFinalizationPrompt(question, stepsOutput);
const history = contextManager.getHistoryWithChatId(userId, chatId);
const response = await withTimeout(
ai.callAI(prompt, "Generate final response", history, undefined, false, "auto", userId, chatId),
60000
);
if (!response) {
console.error("Failed to generate final response");
return "Task completed but final summary could not be generated.";
}
let finalResponse = response;
try {
const parsedResponse = JSON.parse(response);
if (parsedResponse.answer) {
finalResponse = parsedResponse.answer;
} else if (parsedResponse.explanation) {
finalResponse = parsedResponse.explanation;
} else if (parsedResponse.result) {
finalResponse = parsedResponse.result;
} else if (parsedResponse.response) {
finalResponse = parsedResponse.response;
} else if (parsedResponse.text) {
finalResponse = parsedResponse.text;
} else if (parsedResponse.output) {
finalResponse = parsedResponse.output;
}
} catch (e) {
finalResponse = response;
}
return finalResponse;
} catch (error) {
console.error("Error finalizing task:", error.message);
return "Task completed but encountered an error during finalization: " + error.message;
}
}
/**
* Clean up resources for a specific user after task completion.
* @param {string} userId - User identifier.
*/
async function cleanupUserResources(userId) {
try {
logger.debug('Starting cleanup for user', { userId });
// Clean up browser resources if used
if (tools.webBrowser) {
try {
await tools.webBrowser.cleanupResources(userId);
logger.debug('Browser cleanup completed', { userId });
} catch (browserError) {
logger.error('Error closing browser instance', { error: browserError.message, userId });
}
}
// Clean up MCP resources if used
if (tools.mcpClient && typeof tools.mcpClient.cleanupMcpManager === 'function') {
try {
await tools.mcpClient.cleanupMcpManager(userId);
logger.debug('MCP cleanup completed', { userId });
} catch (mcpError) {
logger.error('Error during MCP cleanup', { error: mcpError.message, userId });
}
}
logger.info('Cleanup completed for user', { userId });
} catch (error) {
logger.error('Error during cleanup', { error: error.message, userId });
}
}
/**
* Cleans JSON responses by extracting useful information.
* @param {string} text - The response text potentially containing JSON.
* @returns {string} Cleaned response text.
*/
function cleanJsonResponses(text) {
if (!text || typeof text !== 'string') return text;
if (text.trim().startsWith('{') || text.trim().startsWith('[')) {
try {
const parsed = JSON.parse(text);
if (parsed.directAnswer === true && parsed.answer) {
return parsed.answer;
} else if (parsed.explanation) {
return parsed.explanation;
} else if (parsed.answer) {
return parsed.answer;
} else if (parsed.result) {
return parsed.result;
} else if (parsed.response) {
return parsed.response;
} else if (parsed.output) {
return parsed.output;
} else if (parsed.text) {
return parsed.text;
}
return text;
} catch (e) {
return text;
}
}
return text;
}
/**
* Module exports for external use.
*/
module.exports = {
centralOrchestrator,
cleanupUserResources,
sanitizeFilePath,
contextManager
};
/**
* Main entry point when run directly.
* Sets up socket event handlers for real-time communication.
*/
// Global cleanup function for process termination
async function globalCleanup() {
logger.info('Received termination signal, cleaning up');
try {
// Clean up resources for all known users
const userIds = new Set();
for (const key of contextManager.contexts.keys()) {
userIds.add(key.split('_')[0]);
}
if (userIds.size === 0) {
userIds.add('default');
}
for (const id of userIds) {
await cleanupUserResources(id);
}
logger.info('Global cleanup completed');
} catch (error) {
logger.error('Error during global cleanup', { error: error.message });
}
process.exit(0);
}
// Handle process termination signals
process.on('SIGINT', globalCleanup);
process.on('SIGTERM', globalCleanup);
process.on('beforeExit', globalCleanup);
if (require.main === module) {
io.on('connection', (socket) => {
/**
* Handle submission of a new task.
*/
socket.on('submit_task', async (data) => {
const { task, userId = `socket_${Date.now()}`, chatId = 1, isFollowUp = false } = data;
centralOrchestrator(task, userId, chatId, isFollowUp);
});
/**
* Handle request to load conversation history.
*/
socket.on('load_history', async (data) => {
const { userId = `socket_${Date.now()}`, chatId = 1 } = data;
try {
const history = contextManager.getHistoryWithChatId(userId, chatId);
const context = contextManager.getContext(userId, chatId);
if (history && history.length) {
let finalAnswer = "";
let foundFinalAnswer = false;