rax-flow-core 0.1.0
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/cache/semantic-cache.d.ts +10 -0
- package/dist/cache/semantic-cache.d.ts.map +1 -0
- package/dist/cache/semantic-cache.js +50 -0
- package/dist/cache/semantic-cache.js.map +1 -0
- package/dist/governance/policies/pii-policy.d.ts +37 -0
- package/dist/governance/policies/pii-policy.d.ts.map +1 -0
- package/dist/governance/policies/pii-policy.js +177 -0
- package/dist/governance/policies/pii-policy.js.map +1 -0
- package/dist/graph/workflow-graph.d.ts +5 -0
- package/dist/graph/workflow-graph.d.ts.map +1 -0
- package/dist/graph/workflow-graph.js +38 -0
- package/dist/graph/workflow-graph.js.map +1 -0
- package/dist/index.d.ts +25 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +25 -0
- package/dist/index.js.map +1 -0
- package/dist/memory/graph-memory.d.ts +46 -0
- package/dist/memory/graph-memory.d.ts.map +1 -0
- package/dist/memory/graph-memory.js +122 -0
- package/dist/memory/graph-memory.js.map +1 -0
- package/dist/memory/local-vector-store.d.ts +36 -0
- package/dist/memory/local-vector-store.d.ts.map +1 -0
- package/dist/memory/local-vector-store.js +125 -0
- package/dist/memory/local-vector-store.js.map +1 -0
- package/dist/memory/memory-manager.d.ts +42 -0
- package/dist/memory/memory-manager.d.ts.map +1 -0
- package/dist/memory/memory-manager.js +98 -0
- package/dist/memory/memory-manager.js.map +1 -0
- package/dist/metrics/metrics-engine.d.ts +13 -0
- package/dist/metrics/metrics-engine.d.ts.map +1 -0
- package/dist/metrics/metrics-engine.js +27 -0
- package/dist/metrics/metrics-engine.js.map +1 -0
- package/dist/orchestrator/blueprint-committer.d.ts +69 -0
- package/dist/orchestrator/blueprint-committer.d.ts.map +1 -0
- package/dist/orchestrator/blueprint-committer.js +305 -0
- package/dist/orchestrator/blueprint-committer.js.map +1 -0
- package/dist/orchestrator/core-orchestrator.d.ts +168 -0
- package/dist/orchestrator/core-orchestrator.d.ts.map +1 -0
- package/dist/orchestrator/core-orchestrator.js +451 -0
- package/dist/orchestrator/core-orchestrator.js.map +1 -0
- package/dist/orchestrator/decomposition-engine.d.ts +41 -0
- package/dist/orchestrator/decomposition-engine.d.ts.map +1 -0
- package/dist/orchestrator/decomposition-engine.js +133 -0
- package/dist/orchestrator/decomposition-engine.js.map +1 -0
- package/dist/orchestrator/decomposition.d.ts +10 -0
- package/dist/orchestrator/decomposition.d.ts.map +1 -0
- package/dist/orchestrator/decomposition.js +15 -0
- package/dist/orchestrator/decomposition.js.map +1 -0
- package/dist/orchestrator/default-workflow.d.ts +59 -0
- package/dist/orchestrator/default-workflow.d.ts.map +1 -0
- package/dist/orchestrator/default-workflow.js +221 -0
- package/dist/orchestrator/default-workflow.js.map +1 -0
- package/dist/orchestrator/dynamic-planner.d.ts +46 -0
- package/dist/orchestrator/dynamic-planner.d.ts.map +1 -0
- package/dist/orchestrator/dynamic-planner.js +232 -0
- package/dist/orchestrator/dynamic-planner.js.map +1 -0
- package/dist/orchestrator/kernel-bridge.d.ts +57 -0
- package/dist/orchestrator/kernel-bridge.d.ts.map +1 -0
- package/dist/orchestrator/kernel-bridge.js +211 -0
- package/dist/orchestrator/kernel-bridge.js.map +1 -0
- package/dist/orchestrator/mutation-applier.d.ts +47 -0
- package/dist/orchestrator/mutation-applier.d.ts.map +1 -0
- package/dist/orchestrator/mutation-applier.js +253 -0
- package/dist/orchestrator/mutation-applier.js.map +1 -0
- package/dist/orchestrator/routing.d.ts +4 -0
- package/dist/orchestrator/routing.d.ts.map +1 -0
- package/dist/orchestrator/routing.js +41 -0
- package/dist/orchestrator/routing.js.map +1 -0
- package/dist/orchestrator/verify-fix.d.ts +79 -0
- package/dist/orchestrator/verify-fix.d.ts.map +1 -0
- package/dist/orchestrator/verify-fix.js +153 -0
- package/dist/orchestrator/verify-fix.js.map +1 -0
- package/dist/plugins/governance-plugin.d.ts +43 -0
- package/dist/plugins/governance-plugin.d.ts.map +1 -0
- package/dist/plugins/governance-plugin.js +88 -0
- package/dist/plugins/governance-plugin.js.map +1 -0
- package/dist/plugins/long-term-memory-plugin.d.ts +31 -0
- package/dist/plugins/long-term-memory-plugin.d.ts.map +1 -0
- package/dist/plugins/long-term-memory-plugin.js +67 -0
- package/dist/plugins/long-term-memory-plugin.js.map +1 -0
- package/dist/plugins/plugin-system.d.ts +29 -0
- package/dist/plugins/plugin-system.d.ts.map +1 -0
- package/dist/plugins/plugin-system.js +22 -0
- package/dist/plugins/plugin-system.js.map +1 -0
- package/dist/recovery/error-recovery.d.ts +15 -0
- package/dist/recovery/error-recovery.d.ts.map +1 -0
- package/dist/recovery/error-recovery.js +12 -0
- package/dist/recovery/error-recovery.js.map +1 -0
- package/dist/runtime/concurrency-scheduler.d.ts +2 -0
- package/dist/runtime/concurrency-scheduler.d.ts.map +1 -0
- package/dist/runtime/concurrency-scheduler.js +18 -0
- package/dist/runtime/concurrency-scheduler.js.map +1 -0
- package/dist/runtime/runtime-events.d.ts +59 -0
- package/dist/runtime/runtime-events.d.ts.map +1 -0
- package/dist/runtime/runtime-events.js +12 -0
- package/dist/runtime/runtime-events.js.map +1 -0
- package/dist/types/contracts.d.ts +127 -0
- package/dist/types/contracts.d.ts.map +1 -0
- package/dist/types/contracts.js +2 -0
- package/dist/types/contracts.js.map +1 -0
- package/dist/validator/structured-output-validator.d.ts +10 -0
- package/dist/validator/structured-output-validator.d.ts.map +1 -0
- package/dist/validator/structured-output-validator.js +19 -0
- package/dist/validator/structured-output-validator.js.map +1 -0
- package/package.json +18 -0
- package/src/cache/semantic-cache.ts +64 -0
- package/src/governance/policies/pii-policy.ts +213 -0
- package/src/graph/workflow-graph.ts +41 -0
- package/src/index.ts +25 -0
- package/src/memory/graph-memory.ts +150 -0
- package/src/memory/local-vector-store.ts +129 -0
- package/src/memory/memory-manager.ts +126 -0
- package/src/metrics/metrics-engine.ts +39 -0
- package/src/orchestrator/blueprint-committer.ts +351 -0
- package/src/orchestrator/core-orchestrator.ts +582 -0
- package/src/orchestrator/decomposition-engine.ts +165 -0
- package/src/orchestrator/decomposition.ts +25 -0
- package/src/orchestrator/default-workflow.ts +269 -0
- package/src/orchestrator/dynamic-planner.ts +277 -0
- package/src/orchestrator/kernel-bridge.ts +251 -0
- package/src/orchestrator/mutation-applier.ts +279 -0
- package/src/orchestrator/routing.ts +44 -0
- package/src/orchestrator/verify-fix.ts +218 -0
- package/src/plugins/governance-plugin.ts +106 -0
- package/src/plugins/long-term-memory-plugin.ts +72 -0
- package/src/plugins/plugin-system.ts +34 -0
- package/src/recovery/error-recovery.ts +23 -0
- package/src/runtime/concurrency-scheduler.ts +18 -0
- package/src/runtime/runtime-events.ts +27 -0
- package/src/types/contracts.ts +159 -0
- package/src/validator/structured-output-validator.ts +20 -0
- package/tsconfig.json +17 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { Intent } from "../types/contracts.js";
|
|
2
|
+
|
|
3
|
+
export interface TaskChunk {
|
|
4
|
+
id: string;
|
|
5
|
+
description: string;
|
|
6
|
+
preferredAgent: string;
|
|
7
|
+
canRunInParallel: boolean;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface Subtask {
|
|
11
|
+
id: string;
|
|
12
|
+
title: string;
|
|
13
|
+
description: string;
|
|
14
|
+
estimatedDuration: number;
|
|
15
|
+
priority: number;
|
|
16
|
+
dependencies?: string[];
|
|
17
|
+
parallelizable: boolean;
|
|
18
|
+
complexity: "low" | "medium" | "high";
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class Decomposition {
|
|
22
|
+
async decompose(task: string, options?: { constraints?: { maxSubtasks?: number; maxDuration?: number } }): Promise<Subtask[]> {
|
|
23
|
+
const complexity = this.getComplexity(task);
|
|
24
|
+
const wordCount = task.split(" ").length;
|
|
25
|
+
const baseDuration = wordCount * 5;
|
|
26
|
+
|
|
27
|
+
const subtasks: Subtask[] = [
|
|
28
|
+
{
|
|
29
|
+
id: "analyze",
|
|
30
|
+
title: "Analyze Requirements",
|
|
31
|
+
description: `Analyze: ${task}`,
|
|
32
|
+
estimatedDuration: baseDuration * 0.2,
|
|
33
|
+
priority: 1,
|
|
34
|
+
parallelizable: false,
|
|
35
|
+
complexity
|
|
36
|
+
},
|
|
37
|
+
{
|
|
38
|
+
id: "design",
|
|
39
|
+
title: "Design Solution",
|
|
40
|
+
description: "Design technical solution",
|
|
41
|
+
estimatedDuration: baseDuration * 0.3,
|
|
42
|
+
priority: 2,
|
|
43
|
+
dependencies: ["analyze"],
|
|
44
|
+
parallelizable: false,
|
|
45
|
+
complexity
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
id: "implement",
|
|
49
|
+
title: "Implement",
|
|
50
|
+
description: "Implement the solution",
|
|
51
|
+
estimatedDuration: baseDuration * 0.3,
|
|
52
|
+
priority: 3,
|
|
53
|
+
dependencies: ["design"],
|
|
54
|
+
parallelizable: complexity === "low",
|
|
55
|
+
complexity
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
id: "test",
|
|
59
|
+
title: "Test & Validate",
|
|
60
|
+
description: "Test and validate",
|
|
61
|
+
estimatedDuration: baseDuration * 0.2,
|
|
62
|
+
priority: 4,
|
|
63
|
+
dependencies: ["implement"],
|
|
64
|
+
parallelizable: true,
|
|
65
|
+
complexity
|
|
66
|
+
}
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
// Apply constraints
|
|
70
|
+
if (options?.constraints?.maxSubtasks) {
|
|
71
|
+
return subtasks.slice(0, Math.min(options.constraints.maxSubtasks, subtasks.length));
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
return subtasks;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
private getComplexity(task: string): "low" | "medium" | "high" {
|
|
78
|
+
const complexKeywords = ["distributed", "replication", "concurrent", "async", "architecture"];
|
|
79
|
+
const hasComplex = complexKeywords.some(kw => task.toLowerCase().includes(kw));
|
|
80
|
+
const wordCount = task.split(" ").length;
|
|
81
|
+
|
|
82
|
+
if (hasComplex || wordCount > 15) return "high";
|
|
83
|
+
if (wordCount > 8) return "medium";
|
|
84
|
+
return "low";
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async analyzeComplexity(task: string): Promise<{ complexity: "low" | "medium" | "high"; score: number }> {
|
|
88
|
+
const complexity = this.getComplexity(task);
|
|
89
|
+
const scoreMap = { low: 1, medium: 5, high: 9 };
|
|
90
|
+
return {
|
|
91
|
+
complexity,
|
|
92
|
+
score: scoreMap[complexity]
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
analyzeDependencies(subtasks: Subtask[]): Map<string, string[]> {
|
|
97
|
+
const deps = new Map<string, string[]>();
|
|
98
|
+
for (const task of subtasks) {
|
|
99
|
+
deps.set(task.id, task.dependencies || []);
|
|
100
|
+
}
|
|
101
|
+
return deps;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
buildHierarchy(subtasks: Subtask[]) {
|
|
105
|
+
return {
|
|
106
|
+
id: "root",
|
|
107
|
+
title: "Main Task",
|
|
108
|
+
children: subtasks,
|
|
109
|
+
totalDuration: subtasks.reduce((sum, t) => sum + t.estimatedDuration, 0)
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
optimizeOrder(subtasks: Subtask[]): Subtask[] {
|
|
114
|
+
const sorted = [...subtasks].sort((a, b) => {
|
|
115
|
+
if (a.dependencies?.includes(b.id)) return 1;
|
|
116
|
+
if (b.dependencies?.includes(a.id)) return -1;
|
|
117
|
+
return a.priority - b.priority;
|
|
118
|
+
});
|
|
119
|
+
return sorted;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
detectParallel(subtasks: Subtask[]): Subtask[][] {
|
|
123
|
+
const batches: Subtask[][] = [];
|
|
124
|
+
const processed = new Set<string>();
|
|
125
|
+
|
|
126
|
+
for (const task of subtasks) {
|
|
127
|
+
if (processed.has(task.id)) continue;
|
|
128
|
+
|
|
129
|
+
const batch: Subtask[] = [task];
|
|
130
|
+
processed.add(task.id);
|
|
131
|
+
|
|
132
|
+
if (task.parallelizable) {
|
|
133
|
+
for (const other of subtasks) {
|
|
134
|
+
if (!processed.has(other.id) && other.parallelizable) {
|
|
135
|
+
const canParallelize = !task.dependencies?.includes(other.id) && !other.dependencies?.includes(task.id);
|
|
136
|
+
if (canParallelize) {
|
|
137
|
+
batch.push(other);
|
|
138
|
+
processed.add(other.id);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
batches.push(batch);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
return batches;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
export function decomposeTask(intent: Intent, prompt: string): TaskChunk[] {
|
|
152
|
+
const base = [
|
|
153
|
+
{ id: "analyze", description: `Analyze: ${prompt}`, preferredAgent: "IntentClassifierAgent", canRunInParallel: false },
|
|
154
|
+
{ id: "spec", description: "Draft technical specification", preferredAgent: "SpecAgent", canRunInParallel: false },
|
|
155
|
+
{ id: "implement", description: "Produce implementation artifacts", preferredAgent: "CodeGeneratorAgent", canRunInParallel: intent !== "architecture" },
|
|
156
|
+
{ id: "verify", description: "Run tests and validate schema", preferredAgent: "ValidatorAgent", canRunInParallel: true },
|
|
157
|
+
{ id: "fix", description: "Fix detected issues", preferredAgent: "FixAgent", canRunInParallel: false }
|
|
158
|
+
];
|
|
159
|
+
|
|
160
|
+
if (intent === "benchmark") {
|
|
161
|
+
base.push({ id: "benchmark", description: "Generate benchmark report", preferredAgent: "BenchmarkAgent", canRunInParallel: true });
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
return base;
|
|
165
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { Intent } from "../types/contracts.js";
|
|
2
|
+
export { Decomposition, Subtask } from "./decomposition-engine.js";
|
|
3
|
+
|
|
4
|
+
export interface TaskChunk {
|
|
5
|
+
id: string;
|
|
6
|
+
description: string;
|
|
7
|
+
preferredAgent: string;
|
|
8
|
+
canRunInParallel: boolean;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function decomposeTask(intent: Intent, prompt: string): TaskChunk[] {
|
|
12
|
+
const base = [
|
|
13
|
+
{ id: "analyze", description: `Analyze: ${prompt}`, preferredAgent: "IntentClassifierAgent", canRunInParallel: false },
|
|
14
|
+
{ id: "spec", description: "Draft technical specification", preferredAgent: "SpecAgent", canRunInParallel: false },
|
|
15
|
+
{ id: "implement", description: "Produce implementation artifacts", preferredAgent: "CodeGeneratorAgent", canRunInParallel: intent !== "architecture" },
|
|
16
|
+
{ id: "verify", description: "Run tests and validate schema", preferredAgent: "ValidatorAgent", canRunInParallel: true },
|
|
17
|
+
{ id: "fix", description: "Fix detected issues", preferredAgent: "FixAgent", canRunInParallel: false }
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
if (intent === "benchmark") {
|
|
21
|
+
base.push({ id: "benchmark", description: "Generate benchmark report", preferredAgent: "BenchmarkAgent", canRunInParallel: true });
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
return base;
|
|
25
|
+
}
|
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
import { WorkflowGraph } from "../types/contracts.js";
|
|
2
|
+
import { TaskChunk } from "./decomposition.js";
|
|
3
|
+
|
|
4
|
+
export interface WorkflowStep {
|
|
5
|
+
id: string;
|
|
6
|
+
action?: Function | string;
|
|
7
|
+
condition?: () => boolean;
|
|
8
|
+
retries?: number;
|
|
9
|
+
timeout?: number;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export interface WorkflowDefinition {
|
|
13
|
+
id?: string;
|
|
14
|
+
steps: WorkflowStep[];
|
|
15
|
+
metadata?: Record<string, any>;
|
|
16
|
+
status?: string;
|
|
17
|
+
state?: Record<string, any>;
|
|
18
|
+
context?: Record<string, any>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export class DefaultWorkflow {
|
|
22
|
+
private defaultSteps: WorkflowStep[] = [
|
|
23
|
+
{ id: "init", action: () => {} },
|
|
24
|
+
{ id: "process", action: () => {} },
|
|
25
|
+
{ id: "finalize", action: () => {} }
|
|
26
|
+
];
|
|
27
|
+
private workflowState: Record<string, any> = {};
|
|
28
|
+
private workflowStatus: string = "created";
|
|
29
|
+
private workflowContext: Record<string, any> = {};
|
|
30
|
+
private eventLog: string[] = [];
|
|
31
|
+
private savedState: Record<string, any> = {};
|
|
32
|
+
|
|
33
|
+
create(): WorkflowDefinition {
|
|
34
|
+
return {
|
|
35
|
+
id: `workflow_${Date.now()}`,
|
|
36
|
+
steps: JSON.parse(JSON.stringify(this.defaultSteps)),
|
|
37
|
+
status: "created",
|
|
38
|
+
state: {},
|
|
39
|
+
context: {}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
async init(workflow?: WorkflowDefinition): Promise<WorkflowDefinition> {
|
|
44
|
+
const flow = workflow || this.create();
|
|
45
|
+
this.workflowStatus = "initialized";
|
|
46
|
+
this.eventLog.push("init");
|
|
47
|
+
return { ...flow, status: "initialized" };
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async execute(flow: WorkflowDefinition, options?: { continueOnError?: boolean }): Promise<any> {
|
|
51
|
+
const results: any[] = [];
|
|
52
|
+
let completedSteps = 0;
|
|
53
|
+
this.workflowStatus = "running";
|
|
54
|
+
this.eventLog.push("start");
|
|
55
|
+
|
|
56
|
+
for (const step of flow.steps) {
|
|
57
|
+
try {
|
|
58
|
+
if (step.condition && !step.condition()) {
|
|
59
|
+
results.push({ stepId: step.id, status: "skipped" });
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (step.action && typeof step.action === 'function') {
|
|
63
|
+
await step.action();
|
|
64
|
+
}
|
|
65
|
+
results.push({ stepId: step.id, status: "completed" });
|
|
66
|
+
completedSteps++;
|
|
67
|
+
this.eventLog.push(step.id);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const errorMsg = error instanceof Error ? error.message : String(error);
|
|
70
|
+
this.eventLog.push(errorMsg);
|
|
71
|
+
if (!options?.continueOnError) {
|
|
72
|
+
results.push({ stepId: step.id, status: "failed", error });
|
|
73
|
+
throw error;
|
|
74
|
+
}
|
|
75
|
+
results.push({ stepId: step.id, status: "failed", error });
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
this.workflowStatus = "completed";
|
|
80
|
+
this.eventLog.push("end");
|
|
81
|
+
return { results, completedSteps, status: "completed" };
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async executeSteps(steps: WorkflowStep[], context?: Record<string, any>): Promise<any[]> {
|
|
85
|
+
const results: any[] = [];
|
|
86
|
+
for (const step of steps) {
|
|
87
|
+
try {
|
|
88
|
+
if (step.action && typeof step.action === 'function') {
|
|
89
|
+
const result = await step.action();
|
|
90
|
+
results.push(result);
|
|
91
|
+
} else {
|
|
92
|
+
results.push(undefined);
|
|
93
|
+
}
|
|
94
|
+
} catch (error) {
|
|
95
|
+
results.push({ error });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
return results;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
validate(flow?: WorkflowDefinition): { isValid: boolean; errors: string[] } {
|
|
102
|
+
const errors: string[] = [];
|
|
103
|
+
|
|
104
|
+
if (!flow) {
|
|
105
|
+
return { isValid: true, errors };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (!flow.steps || !Array.isArray(flow.steps)) {
|
|
109
|
+
errors.push("Workflow must have steps array");
|
|
110
|
+
} else if (flow.steps.length === 0) {
|
|
111
|
+
errors.push("Workflow must have at least one step");
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return { isValid: errors.length === 0, errors };
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
clone(flow: WorkflowDefinition): WorkflowDefinition {
|
|
118
|
+
return JSON.parse(JSON.stringify(flow));
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
merge(...workflows: WorkflowDefinition[]): WorkflowDefinition {
|
|
122
|
+
const merged: WorkflowStep[] = [];
|
|
123
|
+
for (const workflow of workflows) {
|
|
124
|
+
merged.push(...(workflow.steps || []));
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
id: `merged_${Date.now()}`,
|
|
128
|
+
steps: merged
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
listSteps(): string[] {
|
|
133
|
+
return this.defaultSteps.map(s => s.id);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
getStep(stepId: string): WorkflowStep | undefined {
|
|
137
|
+
return this.defaultSteps.find(s => s.id === stepId);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
addStep(step: WorkflowStep): void {
|
|
141
|
+
this.defaultSteps.push(step);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
removeStep(stepId: string): boolean {
|
|
145
|
+
const idx = this.defaultSteps.findIndex(s => s.id === stepId);
|
|
146
|
+
if (idx >= 0) {
|
|
147
|
+
this.defaultSteps.splice(idx, 1);
|
|
148
|
+
return true;
|
|
149
|
+
}
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
toGraph(id: string): WorkflowGraph {
|
|
154
|
+
return {
|
|
155
|
+
id,
|
|
156
|
+
nodes: this.defaultSteps.map((step, index) => ({
|
|
157
|
+
id: step.id,
|
|
158
|
+
agent: "default",
|
|
159
|
+
dependsOn: index === 0 ? [] : [this.defaultSteps[index - 1].id]
|
|
160
|
+
}))
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
getState(): Record<string, any> {
|
|
165
|
+
return { ...this.workflowState };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
setState(state: Record<string, any>): void {
|
|
169
|
+
this.workflowState = { ...this.workflowState, ...state };
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
getStatus(): string {
|
|
173
|
+
return this.workflowStatus;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
pause(): void {
|
|
177
|
+
this.workflowStatus = "paused";
|
|
178
|
+
this.eventLog.push("pause");
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
resume(): void {
|
|
182
|
+
if (this.workflowStatus === "paused") {
|
|
183
|
+
this.workflowStatus = "running";
|
|
184
|
+
this.eventLog.push("resume");
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
cancel(): void {
|
|
189
|
+
this.workflowStatus = "cancelled";
|
|
190
|
+
this.eventLog.push("cancel");
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
async saveState(id?: string): Promise<void> {
|
|
194
|
+
const workflowId = id || `workflow_${Date.now()}`;
|
|
195
|
+
this.savedState[workflowId] = {
|
|
196
|
+
state: { ...this.workflowState },
|
|
197
|
+
status: this.workflowStatus,
|
|
198
|
+
context: { ...this.workflowContext }
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async loadState(id: string): Promise<void> {
|
|
203
|
+
const saved = this.savedState[id];
|
|
204
|
+
if (saved) {
|
|
205
|
+
this.workflowState = saved.state;
|
|
206
|
+
this.workflowStatus = saved.status;
|
|
207
|
+
this.workflowContext = saved.context;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
exportDefinition(): string {
|
|
212
|
+
return JSON.stringify({
|
|
213
|
+
steps: this.defaultSteps,
|
|
214
|
+
state: this.workflowState,
|
|
215
|
+
context: this.workflowContext
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
importDefinition(data: string): WorkflowDefinition {
|
|
220
|
+
const parsed = JSON.parse(data);
|
|
221
|
+
this.defaultSteps = parsed.steps || [];
|
|
222
|
+
this.workflowState = parsed.state || {};
|
|
223
|
+
this.workflowContext = parsed.context || {};
|
|
224
|
+
return {
|
|
225
|
+
id: `imported_${Date.now()}`,
|
|
226
|
+
steps: this.defaultSteps,
|
|
227
|
+
state: this.workflowState,
|
|
228
|
+
context: this.workflowContext
|
|
229
|
+
};
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
getTemplates(): Record<string, WorkflowDefinition> {
|
|
233
|
+
return {
|
|
234
|
+
basic: { id: "basic", steps: [{ id: "step1" }] },
|
|
235
|
+
sequential: { id: "sequential", steps: [{ id: "step1" }, { id: "step2" }] }
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
fromTemplate(templateName: string): WorkflowDefinition | undefined {
|
|
240
|
+
const templates = this.getTemplates();
|
|
241
|
+
return templates[templateName] ? JSON.parse(JSON.stringify(templates[templateName])) : undefined;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
getEventLog(): string[] {
|
|
245
|
+
return [...this.eventLog];
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
setContextValue(key: string, value: any): void {
|
|
249
|
+
if (!this.workflowContext) {
|
|
250
|
+
this.workflowContext = {};
|
|
251
|
+
}
|
|
252
|
+
this.workflowContext[key] = value;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
getContextValue(key: string): any {
|
|
256
|
+
return this.workflowContext?.[key];
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export function chunksToWorkflow(id: string, chunks: TaskChunk[]): WorkflowGraph {
|
|
261
|
+
return {
|
|
262
|
+
id,
|
|
263
|
+
nodes: chunks.map((chunk, index) => ({
|
|
264
|
+
id: chunk.id,
|
|
265
|
+
agent: chunk.preferredAgent,
|
|
266
|
+
dependsOn: index === 0 ? [] : [chunks[index - 1].id]
|
|
267
|
+
}))
|
|
268
|
+
};
|
|
269
|
+
}
|