powr-sdk-api 4.3.7 → 4.3.8
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/dist/managers/tasks.js +195 -11
- package/package.json +1 -1
package/dist/managers/tasks.js
CHANGED
|
@@ -14,9 +14,8 @@ class TasksManager {
|
|
|
14
14
|
const task = {
|
|
15
15
|
name: taskData.name,
|
|
16
16
|
description: taskData.description,
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
params: taskData.params,
|
|
17
|
+
type: taskData.type || 'task',
|
|
18
|
+
// 'task' or 'workflow'
|
|
20
19
|
userId: taskData.userId,
|
|
21
20
|
projectId: taskData.projectId,
|
|
22
21
|
isActive: taskData.isActive !== false,
|
|
@@ -29,6 +28,19 @@ class TasksManager {
|
|
|
29
28
|
updatedAt: new Date()
|
|
30
29
|
};
|
|
31
30
|
|
|
31
|
+
// Handle single task vs workflow
|
|
32
|
+
if (task.type === 'task') {
|
|
33
|
+
// Single task - backward compatible
|
|
34
|
+
task.toolId = taskData.toolId;
|
|
35
|
+
task.actionId = taskData.actionId;
|
|
36
|
+
task.params = taskData.params;
|
|
37
|
+
} else if (task.type === 'workflow') {
|
|
38
|
+
// Workflow - new structure
|
|
39
|
+
task.steps = taskData.steps || [];
|
|
40
|
+
task.trigger = taskData.trigger || 'manual';
|
|
41
|
+
task.schedule = taskData.schedule || null;
|
|
42
|
+
}
|
|
43
|
+
|
|
32
44
|
// Calculate nextRun based on scheduling type
|
|
33
45
|
if (!task.scheduledFor) {
|
|
34
46
|
// Immediate execution - run on next Atlas trigger
|
|
@@ -368,17 +380,153 @@ class TasksManager {
|
|
|
368
380
|
// Execute the actual task action
|
|
369
381
|
async executeTaskAction(task) {
|
|
370
382
|
try {
|
|
371
|
-
|
|
372
|
-
|
|
383
|
+
if (task.type === 'workflow') {
|
|
384
|
+
// Execute workflow
|
|
385
|
+
return await this.executeWorkflow(task);
|
|
386
|
+
} else {
|
|
387
|
+
// Execute single task (backward compatible)
|
|
388
|
+
const toolsManager = require('./tools');
|
|
389
|
+
const result = await toolsManager.executeToolAction(task);
|
|
390
|
+
await this.logTaskExecution(task, result);
|
|
391
|
+
return result;
|
|
392
|
+
}
|
|
393
|
+
} catch (error) {
|
|
394
|
+
console.error(`❌ Task execution failed for ${task._id}:`, error);
|
|
395
|
+
return {
|
|
396
|
+
success: false,
|
|
397
|
+
message: error.message
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// Execute workflow with multiple steps and conditions
|
|
403
|
+
async executeWorkflow(workflow) {
|
|
404
|
+
try {
|
|
405
|
+
console.log(`🔄 Executing workflow: ${workflow.name}`);
|
|
406
|
+
const results = [];
|
|
407
|
+
let workflowSuccess = true;
|
|
408
|
+
for (const step of workflow.steps) {
|
|
409
|
+
try {
|
|
410
|
+
console.log(`📋 Executing step: ${step.type} - ${step.toolId}/${step.actionId}`);
|
|
411
|
+
if (step.type === 'condition') {
|
|
412
|
+
// Execute condition step
|
|
413
|
+
const conditionResult = await this.executeConditionStep(step, workflow.userId);
|
|
414
|
+
results.push({
|
|
415
|
+
stepId: step.id,
|
|
416
|
+
type: 'condition',
|
|
417
|
+
success: conditionResult.success,
|
|
418
|
+
result: conditionResult,
|
|
419
|
+
conditionMet: conditionResult.conditionMet
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
// Store condition result for subsequent steps
|
|
423
|
+
step.result = conditionResult;
|
|
424
|
+
} else if (step.type === 'action') {
|
|
425
|
+
// Check if action should execute based on conditions
|
|
426
|
+
if (this.shouldExecuteAction(step, results)) {
|
|
427
|
+
const actionResult = await this.executeActionStep(step, workflow.userId);
|
|
428
|
+
results.push({
|
|
429
|
+
stepId: step.id,
|
|
430
|
+
type: 'action',
|
|
431
|
+
success: actionResult.success,
|
|
432
|
+
result: actionResult
|
|
433
|
+
});
|
|
434
|
+
if (!actionResult.success) {
|
|
435
|
+
workflowSuccess = false;
|
|
436
|
+
}
|
|
437
|
+
} else {
|
|
438
|
+
console.log(`⏭️ Skipping action step ${step.id} - condition not met`);
|
|
439
|
+
results.push({
|
|
440
|
+
stepId: step.id,
|
|
441
|
+
type: 'action',
|
|
442
|
+
success: true,
|
|
443
|
+
result: {
|
|
444
|
+
message: 'Step skipped - condition not met'
|
|
445
|
+
},
|
|
446
|
+
skipped: true
|
|
447
|
+
});
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
} catch (error) {
|
|
451
|
+
console.error(`❌ Step execution failed: ${step.id}`, error);
|
|
452
|
+
results.push({
|
|
453
|
+
stepId: step.id,
|
|
454
|
+
type: step.type,
|
|
455
|
+
success: false,
|
|
456
|
+
error: error.message
|
|
457
|
+
});
|
|
458
|
+
workflowSuccess = false;
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
// Log workflow execution
|
|
463
|
+
await this.logWorkflowExecution(workflow, results);
|
|
464
|
+
return {
|
|
465
|
+
success: workflowSuccess,
|
|
466
|
+
workflowName: workflow.name,
|
|
467
|
+
stepsExecuted: results.length,
|
|
468
|
+
results: results
|
|
469
|
+
};
|
|
470
|
+
} catch (error) {
|
|
471
|
+
console.error(`❌ Workflow execution failed: ${workflow.name}`, error);
|
|
472
|
+
return {
|
|
473
|
+
success: false,
|
|
474
|
+
message: error.message
|
|
475
|
+
};
|
|
476
|
+
}
|
|
477
|
+
}
|
|
373
478
|
|
|
374
|
-
|
|
375
|
-
|
|
479
|
+
// Execute a condition step (e.g., weather check)
|
|
480
|
+
async executeConditionStep(step, userId) {
|
|
481
|
+
try {
|
|
482
|
+
const toolsManager = require('./tools');
|
|
483
|
+
const result = await toolsManager.executeToolAction({
|
|
484
|
+
userId: userId,
|
|
485
|
+
toolId: step.toolId,
|
|
486
|
+
actionId: step.actionId,
|
|
487
|
+
params: step.params
|
|
488
|
+
});
|
|
489
|
+
if (result.success) {
|
|
490
|
+
var _result$data, _result$data2, _step$params;
|
|
491
|
+
// Extract condition result (e.g., weather condition)
|
|
492
|
+
const conditionValue = ((_result$data = result.data) === null || _result$data === void 0 ? void 0 : _result$data.currentWeather) || ((_result$data2 = result.data) === null || _result$data2 === void 0 ? void 0 : _result$data2.condition) || 'unknown';
|
|
493
|
+
const expectedValue = step.expectedResult || ((_step$params = step.params) === null || _step$params === void 0 ? void 0 : _step$params.condition) || 'rain';
|
|
494
|
+
const conditionMet = conditionValue.toLowerCase() === expectedValue.toLowerCase();
|
|
495
|
+
return {
|
|
496
|
+
success: true,
|
|
497
|
+
conditionMet: conditionMet,
|
|
498
|
+
actualValue: conditionValue,
|
|
499
|
+
expectedValue: expectedValue,
|
|
500
|
+
data: result.data
|
|
501
|
+
};
|
|
502
|
+
} else {
|
|
503
|
+
return {
|
|
504
|
+
success: false,
|
|
505
|
+
conditionMet: false,
|
|
506
|
+
error: result.message
|
|
507
|
+
};
|
|
508
|
+
}
|
|
509
|
+
} catch (error) {
|
|
510
|
+
return {
|
|
511
|
+
success: false,
|
|
512
|
+
conditionMet: false,
|
|
513
|
+
error: error.message
|
|
514
|
+
};
|
|
515
|
+
}
|
|
516
|
+
}
|
|
376
517
|
|
|
377
|
-
|
|
378
|
-
|
|
518
|
+
// Execute an action step (e.g., send email)
|
|
519
|
+
async executeActionStep(step, userId) {
|
|
520
|
+
try {
|
|
521
|
+
const toolsManager = require('./tools');
|
|
522
|
+
const result = await toolsManager.executeToolAction({
|
|
523
|
+
userId: userId,
|
|
524
|
+
toolId: step.toolId,
|
|
525
|
+
actionId: step.actionId,
|
|
526
|
+
params: step.params
|
|
527
|
+
});
|
|
379
528
|
return result;
|
|
380
529
|
} catch (error) {
|
|
381
|
-
console.error(`❌ Task execution failed for ${task.id}:`, error);
|
|
382
530
|
return {
|
|
383
531
|
success: false,
|
|
384
532
|
message: error.message
|
|
@@ -386,13 +534,32 @@ class TasksManager {
|
|
|
386
534
|
}
|
|
387
535
|
}
|
|
388
536
|
|
|
537
|
+
// Check if action should execute based on conditions
|
|
538
|
+
shouldExecuteAction(step, previousResults) {
|
|
539
|
+
if (!step.condition) {
|
|
540
|
+
return true; // No condition, always execute
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// Simple condition evaluation
|
|
544
|
+
// For now, support basic conditions like "step1 === true"
|
|
545
|
+
const condition = step.condition;
|
|
546
|
+
if (condition.includes('step1') && condition.includes('=== true')) {
|
|
547
|
+
const step1Result = previousResults.find(r => r.stepId === 'step1');
|
|
548
|
+
return step1Result && step1Result.conditionMet === true;
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
// Add more condition parsing as needed
|
|
552
|
+
return true; // Default to execute if condition parsing fails
|
|
553
|
+
}
|
|
554
|
+
|
|
389
555
|
// Log task execution
|
|
390
556
|
async logTaskExecution(task, result) {
|
|
391
557
|
try {
|
|
392
558
|
const db = await getDb();
|
|
393
559
|
await db.collection("task_executions").insertOne({
|
|
394
|
-
taskId: task.id,
|
|
560
|
+
taskId: task._id || task.id,
|
|
395
561
|
userId: task.userId,
|
|
562
|
+
type: task.type || 'task',
|
|
396
563
|
toolId: task.toolId,
|
|
397
564
|
actionId: task.actionId,
|
|
398
565
|
params: task.params,
|
|
@@ -404,6 +571,23 @@ class TasksManager {
|
|
|
404
571
|
}
|
|
405
572
|
}
|
|
406
573
|
|
|
574
|
+
// Log workflow execution
|
|
575
|
+
async logWorkflowExecution(workflow, results) {
|
|
576
|
+
try {
|
|
577
|
+
const db = await getDb();
|
|
578
|
+
await db.collection("workflow_executions").insertOne({
|
|
579
|
+
workflowId: workflow._id,
|
|
580
|
+
workflowName: workflow.name,
|
|
581
|
+
userId: workflow.userId,
|
|
582
|
+
projectId: workflow.projectId,
|
|
583
|
+
results: results,
|
|
584
|
+
executedAt: new Date()
|
|
585
|
+
});
|
|
586
|
+
} catch (error) {
|
|
587
|
+
console.error("❌ Failed to log workflow execution:", error);
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
|
|
407
591
|
// Check if a string is a valid timestamp
|
|
408
592
|
isTimestamp(value) {
|
|
409
593
|
try {
|