@marktoflow/gui 2.0.0-alpha.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.
Files changed (165) hide show
  1. package/.turbo/turbo-build.log +26 -0
  2. package/.turbo/turbo-test.log +22 -0
  3. package/README.md +179 -0
  4. package/dist/client/assets/index-DwTI8opO.js +608 -0
  5. package/dist/client/assets/index-DwTI8opO.js.map +1 -0
  6. package/dist/client/assets/index-RoEdL6gO.css +1 -0
  7. package/dist/client/index.html +20 -0
  8. package/dist/client/vite.svg +9 -0
  9. package/dist/server/index.d.ts +3 -0
  10. package/dist/server/index.d.ts.map +1 -0
  11. package/dist/server/index.js +56 -0
  12. package/dist/server/index.js.map +1 -0
  13. package/dist/server/routes/ai.js +50 -0
  14. package/dist/server/routes/ai.js.map +1 -0
  15. package/dist/server/routes/execute.js +62 -0
  16. package/dist/server/routes/execute.js.map +1 -0
  17. package/dist/server/routes/workflows.js +99 -0
  18. package/dist/server/routes/workflows.js.map +1 -0
  19. package/dist/server/server/index.js +95 -0
  20. package/dist/server/server/index.js.map +1 -0
  21. package/dist/server/server/routes/ai.js +87 -0
  22. package/dist/server/server/routes/ai.js.map +1 -0
  23. package/dist/server/server/routes/execute.js +63 -0
  24. package/dist/server/server/routes/execute.js.map +1 -0
  25. package/dist/server/server/routes/tools.js +518 -0
  26. package/dist/server/server/routes/tools.js.map +1 -0
  27. package/dist/server/server/routes/workflows.js +99 -0
  28. package/dist/server/server/routes/workflows.js.map +1 -0
  29. package/dist/server/server/services/AIService.js +69 -0
  30. package/dist/server/server/services/AIService.js.map +1 -0
  31. package/dist/server/server/services/FileWatcher.js +60 -0
  32. package/dist/server/server/services/FileWatcher.js.map +1 -0
  33. package/dist/server/server/services/WorkflowService.js +363 -0
  34. package/dist/server/server/services/WorkflowService.js.map +1 -0
  35. package/dist/server/server/services/agents/claude-code-provider.js +250 -0
  36. package/dist/server/server/services/agents/claude-code-provider.js.map +1 -0
  37. package/dist/server/server/services/agents/claude-provider.js +204 -0
  38. package/dist/server/server/services/agents/claude-provider.js.map +1 -0
  39. package/dist/server/server/services/agents/copilot-provider.js +227 -0
  40. package/dist/server/server/services/agents/copilot-provider.js.map +1 -0
  41. package/dist/server/server/services/agents/demo-provider.js +167 -0
  42. package/dist/server/server/services/agents/demo-provider.js.map +1 -0
  43. package/dist/server/server/services/agents/index.js +31 -0
  44. package/dist/server/server/services/agents/index.js.map +1 -0
  45. package/dist/server/server/services/agents/ollama-provider.js +220 -0
  46. package/dist/server/server/services/agents/ollama-provider.js.map +1 -0
  47. package/dist/server/server/services/agents/prompts.js +436 -0
  48. package/dist/server/server/services/agents/prompts.js.map +1 -0
  49. package/dist/server/server/services/agents/registry.js +242 -0
  50. package/dist/server/server/services/agents/registry.js.map +1 -0
  51. package/dist/server/server/services/agents/types.js +6 -0
  52. package/dist/server/server/services/agents/types.js.map +1 -0
  53. package/dist/server/server/websocket/index.js +85 -0
  54. package/dist/server/server/websocket/index.js.map +1 -0
  55. package/dist/server/services/AIService.d.ts +30 -0
  56. package/dist/server/services/AIService.d.ts.map +1 -0
  57. package/dist/server/services/AIService.js +216 -0
  58. package/dist/server/services/AIService.js.map +1 -0
  59. package/dist/server/services/FileWatcher.d.ts +10 -0
  60. package/dist/server/services/FileWatcher.d.ts.map +1 -0
  61. package/dist/server/services/FileWatcher.js +62 -0
  62. package/dist/server/services/FileWatcher.js.map +1 -0
  63. package/dist/server/services/WorkflowService.d.ts +54 -0
  64. package/dist/server/services/WorkflowService.d.ts.map +1 -0
  65. package/dist/server/services/WorkflowService.js +323 -0
  66. package/dist/server/services/WorkflowService.js.map +1 -0
  67. package/dist/server/shared/constants.js +175 -0
  68. package/dist/server/shared/constants.js.map +1 -0
  69. package/dist/server/shared/types.js +3 -0
  70. package/dist/server/shared/types.js.map +1 -0
  71. package/dist/server/websocket/index.d.ts +10 -0
  72. package/dist/server/websocket/index.d.ts.map +1 -0
  73. package/dist/server/websocket/index.js +85 -0
  74. package/dist/server/websocket/index.js.map +1 -0
  75. package/index.html +19 -0
  76. package/package.json +96 -0
  77. package/playwright.config.ts +27 -0
  78. package/postcss.config.js +6 -0
  79. package/public/vite.svg +9 -0
  80. package/src/client/App.tsx +520 -0
  81. package/src/client/components/Canvas/Canvas.tsx +405 -0
  82. package/src/client/components/Canvas/ExecutionOverlay.tsx +847 -0
  83. package/src/client/components/Canvas/NodeContextMenu.tsx +188 -0
  84. package/src/client/components/Canvas/OutputNode.tsx +111 -0
  85. package/src/client/components/Canvas/StepNode.tsx +106 -0
  86. package/src/client/components/Canvas/SubWorkflowNode.tsx +141 -0
  87. package/src/client/components/Canvas/Toolbar.tsx +189 -0
  88. package/src/client/components/Canvas/TriggerNode.tsx +128 -0
  89. package/src/client/components/Editor/InputsEditor.tsx +458 -0
  90. package/src/client/components/Editor/NewStepWizard.tsx +344 -0
  91. package/src/client/components/Editor/StepEditor.tsx +532 -0
  92. package/src/client/components/Editor/YamlEditor.tsx +160 -0
  93. package/src/client/components/Panels/PropertiesPanel.tsx +589 -0
  94. package/src/client/components/Prompt/ChangePreview.tsx +281 -0
  95. package/src/client/components/Prompt/PromptHistoryPanel.tsx +209 -0
  96. package/src/client/components/Prompt/PromptInput.tsx +108 -0
  97. package/src/client/components/Sidebar/Sidebar.tsx +343 -0
  98. package/src/client/components/common/Breadcrumb.tsx +40 -0
  99. package/src/client/components/common/Button.tsx +68 -0
  100. package/src/client/components/common/ContextMenu.tsx +202 -0
  101. package/src/client/components/common/KeyboardShortcuts.tsx +143 -0
  102. package/src/client/components/common/Modal.tsx +93 -0
  103. package/src/client/components/common/Tabs.tsx +57 -0
  104. package/src/client/components/common/ThemeToggle.tsx +63 -0
  105. package/src/client/components/index.ts +32 -0
  106. package/src/client/hooks/index.ts +4 -0
  107. package/src/client/hooks/useAIPrompt.ts +108 -0
  108. package/src/client/hooks/useCanvas.ts +247 -0
  109. package/src/client/hooks/useWebSocket.ts +164 -0
  110. package/src/client/hooks/useWorkflow.ts +138 -0
  111. package/src/client/main.tsx +10 -0
  112. package/src/client/stores/canvasStore.ts +348 -0
  113. package/src/client/stores/editorStore.ts +133 -0
  114. package/src/client/stores/executionStore.ts +440 -0
  115. package/src/client/stores/index.ts +4 -0
  116. package/src/client/stores/layoutStore.ts +103 -0
  117. package/src/client/stores/navigationStore.ts +49 -0
  118. package/src/client/stores/promptStore.ts +113 -0
  119. package/src/client/stores/themeStore.ts +75 -0
  120. package/src/client/stores/workflowStore.ts +177 -0
  121. package/src/client/styles/globals.css +346 -0
  122. package/src/client/utils/cn.ts +9 -0
  123. package/src/client/utils/index.ts +4 -0
  124. package/src/client/utils/serviceIcons.tsx +64 -0
  125. package/src/client/utils/stepValidation.ts +155 -0
  126. package/src/client/utils/workflowToGraph.ts +299 -0
  127. package/src/server/index.ts +114 -0
  128. package/src/server/routes/ai.ts +91 -0
  129. package/src/server/routes/execute.ts +71 -0
  130. package/src/server/routes/tools.ts +564 -0
  131. package/src/server/routes/workflows.ts +106 -0
  132. package/src/server/services/AIService.ts +105 -0
  133. package/src/server/services/FileWatcher.ts +69 -0
  134. package/src/server/services/WorkflowService.ts +441 -0
  135. package/src/server/services/agents/claude-code-provider.ts +320 -0
  136. package/src/server/services/agents/claude-provider.ts +248 -0
  137. package/src/server/services/agents/copilot-provider.ts +311 -0
  138. package/src/server/services/agents/demo-provider.ts +184 -0
  139. package/src/server/services/agents/index.ts +31 -0
  140. package/src/server/services/agents/ollama-provider.ts +267 -0
  141. package/src/server/services/agents/prompts.ts +482 -0
  142. package/src/server/services/agents/registry.ts +289 -0
  143. package/src/server/services/agents/types.ts +146 -0
  144. package/src/server/websocket/index.ts +104 -0
  145. package/src/shared/constants.ts +180 -0
  146. package/src/shared/types.ts +179 -0
  147. package/tailwind.config.ts +73 -0
  148. package/tests/e2e/app.spec.ts +90 -0
  149. package/tests/e2e/canvas.spec.ts +128 -0
  150. package/tests/e2e/workflow.spec.ts +185 -0
  151. package/tests/integration/api.test.ts +250 -0
  152. package/tests/integration/testApp.ts +31 -0
  153. package/tests/setup.ts +37 -0
  154. package/tests/unit/canvasStore.test.ts +502 -0
  155. package/tests/unit/components.test.tsx +151 -0
  156. package/tests/unit/executionStore.test.ts +527 -0
  157. package/tests/unit/layoutStore.test.ts +194 -0
  158. package/tests/unit/navigationStore.test.ts +152 -0
  159. package/tests/unit/stepValidation.test.ts +226 -0
  160. package/tests/unit/themeStore.test.ts +141 -0
  161. package/tests/unit/workflowToGraph.test.ts +289 -0
  162. package/tsconfig.json +29 -0
  163. package/tsconfig.server.json +28 -0
  164. package/vite.config.ts +31 -0
  165. package/vitest.config.ts +26 -0
@@ -0,0 +1,155 @@
1
+ import type { WorkflowStep } from '@shared/types';
2
+
3
+ export interface ValidationError {
4
+ field: string;
5
+ message: string;
6
+ }
7
+
8
+ export interface ValidationResult {
9
+ valid: boolean;
10
+ errors: ValidationError[];
11
+ }
12
+
13
+ /**
14
+ * Validates a workflow step
15
+ */
16
+ export function validateStep(step: WorkflowStep): ValidationResult {
17
+ const errors: ValidationError[] = [];
18
+
19
+ // ID is required
20
+ if (!step.id || step.id.trim() === '') {
21
+ errors.push({
22
+ field: 'id',
23
+ message: 'Step ID is required',
24
+ });
25
+ } else {
26
+ // ID must be a valid identifier (letters, numbers, underscores, hyphens)
27
+ if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(step.id)) {
28
+ errors.push({
29
+ field: 'id',
30
+ message: 'Step ID must start with a letter and contain only letters, numbers, underscores, and hyphens',
31
+ });
32
+ }
33
+ }
34
+
35
+ // Must have either an action or a workflow reference
36
+ if (!step.action && !step.workflow) {
37
+ errors.push({
38
+ field: 'action',
39
+ message: 'Step must have either an action or a workflow reference',
40
+ });
41
+ }
42
+
43
+ // Action format validation (service.method or service.namespace.method)
44
+ if (step.action) {
45
+ const actionParts = step.action.split('.');
46
+ if (actionParts.length < 2) {
47
+ errors.push({
48
+ field: 'action',
49
+ message: 'Action must be in format: service.method (e.g., slack.chat.postMessage)',
50
+ });
51
+ }
52
+ }
53
+
54
+ // Workflow reference validation
55
+ if (step.workflow) {
56
+ if (!step.workflow.endsWith('.md') && !step.workflow.endsWith('.yaml') && !step.workflow.endsWith('.yml')) {
57
+ errors.push({
58
+ field: 'workflow',
59
+ message: 'Workflow reference should end with .md, .yaml, or .yml',
60
+ });
61
+ }
62
+ }
63
+
64
+ // Output variable name validation
65
+ if (step.outputVariable) {
66
+ if (!/^[a-zA-Z][a-zA-Z0-9_]*$/.test(step.outputVariable)) {
67
+ errors.push({
68
+ field: 'outputVariable',
69
+ message: 'Output variable must start with a letter and contain only letters, numbers, and underscores',
70
+ });
71
+ }
72
+ }
73
+
74
+ // Timeout validation
75
+ if (step.timeout !== undefined) {
76
+ if (typeof step.timeout !== 'number' || step.timeout <= 0) {
77
+ errors.push({
78
+ field: 'timeout',
79
+ message: 'Timeout must be a positive number',
80
+ });
81
+ }
82
+ }
83
+
84
+ // Error handling validation
85
+ if (step.errorHandling) {
86
+ const validActions = ['stop', 'continue', 'retry'];
87
+ if (!validActions.includes(step.errorHandling.action)) {
88
+ errors.push({
89
+ field: 'errorHandling.action',
90
+ message: 'Error action must be one of: stop, continue, retry',
91
+ });
92
+ }
93
+
94
+ if (step.errorHandling.action === 'retry') {
95
+ if (step.errorHandling.maxRetries !== undefined) {
96
+ if (typeof step.errorHandling.maxRetries !== 'number' || step.errorHandling.maxRetries < 1) {
97
+ errors.push({
98
+ field: 'errorHandling.maxRetries',
99
+ message: 'Max retries must be at least 1',
100
+ });
101
+ }
102
+ }
103
+
104
+ if (step.errorHandling.retryDelay !== undefined) {
105
+ if (typeof step.errorHandling.retryDelay !== 'number' || step.errorHandling.retryDelay < 0) {
106
+ errors.push({
107
+ field: 'errorHandling.retryDelay',
108
+ message: 'Retry delay must be a non-negative number',
109
+ });
110
+ }
111
+ }
112
+ }
113
+
114
+ if (step.errorHandling.fallbackStep) {
115
+ if (!/^[a-zA-Z][a-zA-Z0-9_-]*$/.test(step.errorHandling.fallbackStep)) {
116
+ errors.push({
117
+ field: 'errorHandling.fallbackStep',
118
+ message: 'Fallback step ID must be a valid identifier',
119
+ });
120
+ }
121
+ }
122
+ }
123
+
124
+ // Conditions validation
125
+ if (step.conditions && step.conditions.length > 0) {
126
+ step.conditions.forEach((condition, index) => {
127
+ if (!condition || condition.trim() === '') {
128
+ errors.push({
129
+ field: `conditions[${index}]`,
130
+ message: `Condition ${index + 1} cannot be empty`,
131
+ });
132
+ }
133
+ });
134
+ }
135
+
136
+ return {
137
+ valid: errors.length === 0,
138
+ errors,
139
+ };
140
+ }
141
+
142
+ /**
143
+ * Get validation errors for a specific field
144
+ */
145
+ export function getFieldError(errors: ValidationError[], field: string): string | undefined {
146
+ const error = errors.find((e) => e.field === field);
147
+ return error?.message;
148
+ }
149
+
150
+ /**
151
+ * Check if a field has validation errors
152
+ */
153
+ export function hasFieldError(errors: ValidationError[], field: string): boolean {
154
+ return errors.some((e) => e.field === field);
155
+ }
@@ -0,0 +1,299 @@
1
+ import type { Node, Edge } from '@xyflow/react';
2
+
3
+ interface WorkflowStep {
4
+ id: string;
5
+ name?: string;
6
+ action?: string;
7
+ workflow?: string;
8
+ inputs: Record<string, unknown>;
9
+ outputVariable?: string;
10
+ conditions?: string[];
11
+ }
12
+
13
+ interface WorkflowTrigger {
14
+ type: 'manual' | 'schedule' | 'webhook' | 'event';
15
+ cron?: string;
16
+ path?: string;
17
+ events?: string[];
18
+ }
19
+
20
+ interface Workflow {
21
+ metadata: {
22
+ id: string;
23
+ name: string;
24
+ };
25
+ steps: WorkflowStep[];
26
+ triggers?: WorkflowTrigger[];
27
+ }
28
+
29
+ interface GraphResult {
30
+ nodes: Node[];
31
+ edges: Edge[];
32
+ }
33
+
34
+ /**
35
+ * Converts a marktoflow Workflow to React Flow nodes and edges
36
+ */
37
+ export function workflowToGraph(workflow: Workflow): GraphResult {
38
+ const nodes: Node[] = [];
39
+ const edges: Edge[] = [];
40
+
41
+ const VERTICAL_SPACING = 120;
42
+ const HORIZONTAL_OFFSET = 250;
43
+ let currentY = 0;
44
+
45
+ // Add trigger node if triggers are defined
46
+ if (workflow.triggers && workflow.triggers.length > 0) {
47
+ const trigger = workflow.triggers[0]; // Primary trigger
48
+ const triggerId = `trigger-${workflow.metadata.id}`;
49
+
50
+ nodes.push({
51
+ id: triggerId,
52
+ type: 'trigger',
53
+ position: { x: HORIZONTAL_OFFSET, y: currentY },
54
+ data: {
55
+ id: triggerId,
56
+ name: workflow.metadata.name,
57
+ type: trigger.type || 'manual',
58
+ cron: trigger.cron,
59
+ path: trigger.path,
60
+ events: trigger.events,
61
+ active: true,
62
+ },
63
+ });
64
+
65
+ currentY += VERTICAL_SPACING;
66
+
67
+ // Edge from trigger to first step
68
+ if (workflow.steps.length > 0) {
69
+ edges.push({
70
+ id: `e-${triggerId}-${workflow.steps[0].id}`,
71
+ source: triggerId,
72
+ target: workflow.steps[0].id,
73
+ type: 'smoothstep',
74
+ animated: false,
75
+ style: { stroke: '#ff6d5a', strokeWidth: 2 },
76
+ });
77
+ }
78
+ }
79
+
80
+ // Create nodes for each step
81
+ workflow.steps.forEach((step, index) => {
82
+ const isSubWorkflow = !!step.workflow;
83
+
84
+ const node: Node = {
85
+ id: step.id,
86
+ type: isSubWorkflow ? 'subworkflow' : 'step',
87
+ position: {
88
+ x: HORIZONTAL_OFFSET,
89
+ y: currentY + index * VERTICAL_SPACING,
90
+ },
91
+ data: {
92
+ id: step.id,
93
+ name: step.name,
94
+ action: step.action,
95
+ workflowPath: step.workflow,
96
+ status: 'pending',
97
+ },
98
+ };
99
+
100
+ nodes.push(node);
101
+
102
+ // Create edge to next step
103
+ if (index < workflow.steps.length - 1) {
104
+ const nextStep = workflow.steps[index + 1];
105
+ const edge: Edge = {
106
+ id: `e-${step.id}-${nextStep.id}`,
107
+ source: step.id,
108
+ target: nextStep.id,
109
+ type: 'smoothstep',
110
+ animated: false,
111
+ style: { stroke: '#ff6d5a', strokeWidth: 2 },
112
+ };
113
+
114
+ // Add condition label if present
115
+ if (nextStep.conditions && nextStep.conditions.length > 0) {
116
+ edge.label = 'conditional';
117
+ edge.labelStyle = { fill: '#a0a0c0', fontSize: 10 };
118
+ edge.labelBgStyle = { fill: '#232340' };
119
+ }
120
+
121
+ edges.push(edge);
122
+ }
123
+ });
124
+
125
+ // Add output node at the end
126
+ if (workflow.steps.length > 0) {
127
+ const outputId = `output-${workflow.metadata.id}`;
128
+ const lastStep = workflow.steps[workflow.steps.length - 1];
129
+ const outputY = currentY + workflow.steps.length * VERTICAL_SPACING;
130
+
131
+ // Collect all output variables
132
+ const outputVariables = workflow.steps
133
+ .filter((s) => s.outputVariable)
134
+ .map((s) => s.outputVariable as string);
135
+
136
+ nodes.push({
137
+ id: outputId,
138
+ type: 'output',
139
+ position: { x: HORIZONTAL_OFFSET, y: outputY },
140
+ data: {
141
+ id: outputId,
142
+ name: 'Workflow Output',
143
+ variables: outputVariables,
144
+ status: 'pending',
145
+ },
146
+ });
147
+
148
+ // Edge from last step to output
149
+ edges.push({
150
+ id: `e-${lastStep.id}-${outputId}`,
151
+ source: lastStep.id,
152
+ target: outputId,
153
+ type: 'smoothstep',
154
+ animated: false,
155
+ style: { stroke: '#ff6d5a', strokeWidth: 2 },
156
+ });
157
+ }
158
+
159
+ // Add data flow edges based on variable references
160
+ const variableEdges = findVariableDependencies(workflow.steps);
161
+ edges.push(...variableEdges);
162
+
163
+ return { nodes, edges };
164
+ }
165
+
166
+ /**
167
+ * Finds variable dependencies between steps
168
+ * Creates additional edges showing data flow
169
+ */
170
+ function findVariableDependencies(steps: WorkflowStep[]): Edge[] {
171
+ const edges: Edge[] = [];
172
+ const outputVariables = new Map<string, string>(); // variable name -> step id
173
+
174
+ // First pass: collect all output variables
175
+ steps.forEach((step) => {
176
+ if (step.outputVariable) {
177
+ outputVariables.set(step.outputVariable, step.id);
178
+ }
179
+ });
180
+
181
+ // Second pass: find references in inputs
182
+ steps.forEach((step) => {
183
+ const references = findTemplateVariables(step.inputs);
184
+
185
+ references.forEach((ref) => {
186
+ // Extract the root variable name (e.g., "pr_details" from "pr_details.title")
187
+ const rootVar = ref.split('.')[0];
188
+
189
+ // Check if this references an output variable
190
+ const sourceStepId = outputVariables.get(rootVar);
191
+ if (sourceStepId && sourceStepId !== step.id) {
192
+ // Create data flow edge
193
+ const edgeId = `data-${sourceStepId}-${step.id}-${rootVar}`;
194
+
195
+ // Check if edge already exists
196
+ if (!edges.find((e) => e.id === edgeId)) {
197
+ edges.push({
198
+ id: edgeId,
199
+ source: sourceStepId,
200
+ target: step.id,
201
+ type: 'smoothstep',
202
+ animated: true,
203
+ style: {
204
+ stroke: '#5bc0de',
205
+ strokeWidth: 1,
206
+ strokeDasharray: '5,5',
207
+ },
208
+ label: rootVar,
209
+ labelStyle: { fill: '#5bc0de', fontSize: 9 },
210
+ labelBgStyle: { fill: '#1a1a2e', fillOpacity: 0.8 },
211
+ });
212
+ }
213
+ }
214
+ });
215
+ });
216
+
217
+ return edges;
218
+ }
219
+
220
+ /**
221
+ * Extracts template variable references from inputs
222
+ */
223
+ function findTemplateVariables(inputs: Record<string, unknown>): string[] {
224
+ const variables: string[] = [];
225
+ const templateRegex = /\{\{\s*([^}]+)\s*\}\}/g;
226
+
227
+ function extractFromValue(value: unknown): void {
228
+ if (typeof value === 'string') {
229
+ let match;
230
+ while ((match = templateRegex.exec(value)) !== null) {
231
+ // Extract variable name, removing any method calls
232
+ const varExpr = match[1].trim();
233
+ const varName = varExpr.split('.')[0].replace(/\[.*\]/, '');
234
+
235
+ // Filter out 'inputs' as those are workflow inputs, not step outputs
236
+ if (varName !== 'inputs' && !variables.includes(varName)) {
237
+ variables.push(varName);
238
+ }
239
+ }
240
+ } else if (Array.isArray(value)) {
241
+ value.forEach(extractFromValue);
242
+ } else if (value && typeof value === 'object') {
243
+ Object.values(value).forEach(extractFromValue);
244
+ }
245
+ }
246
+
247
+ Object.values(inputs).forEach(extractFromValue);
248
+ return variables;
249
+ }
250
+
251
+ /**
252
+ * Converts React Flow nodes and edges back to a Workflow
253
+ */
254
+ export function graphToWorkflow(
255
+ nodes: Node[],
256
+ _edges: Edge[],
257
+ metadata: Workflow['metadata']
258
+ ): Workflow {
259
+ // Filter out trigger and output nodes, sort by vertical position
260
+ const stepNodes = nodes
261
+ .filter((node) => node.type === 'step' || node.type === 'subworkflow')
262
+ .sort((a, b) => a.position.y - b.position.y);
263
+
264
+ // Extract trigger info if present
265
+ const triggerNode = nodes.find((node) => node.type === 'trigger');
266
+ const triggers: WorkflowTrigger[] = [];
267
+
268
+ if (triggerNode) {
269
+ const data = triggerNode.data as Record<string, unknown>;
270
+ triggers.push({
271
+ type: (data.type as WorkflowTrigger['type']) || 'manual',
272
+ cron: data.cron as string | undefined,
273
+ path: data.path as string | undefined,
274
+ events: data.events as string[] | undefined,
275
+ });
276
+ }
277
+
278
+ const steps: WorkflowStep[] = stepNodes.map((node) => {
279
+ const data = node.data as Record<string, unknown>;
280
+ const step: WorkflowStep = {
281
+ id: (data.id as string) || node.id,
282
+ inputs: (data.inputs as Record<string, unknown>) || {},
283
+ };
284
+
285
+ if (data.name) step.name = data.name as string;
286
+ if (data.action) step.action = data.action as string;
287
+ if (data.workflowPath) step.workflow = data.workflowPath as string;
288
+ if (data.outputVariable) step.outputVariable = data.outputVariable as string;
289
+ if (data.conditions) step.conditions = data.conditions as string[];
290
+
291
+ return step;
292
+ });
293
+
294
+ return {
295
+ metadata,
296
+ steps,
297
+ triggers: triggers.length > 0 ? triggers : undefined,
298
+ };
299
+ }
@@ -0,0 +1,114 @@
1
+ #!/usr/bin/env node
2
+
3
+ import express from 'express';
4
+ import cors from 'cors';
5
+ import { createServer, type Server } from 'http';
6
+ import { Server as SocketIOServer } from 'socket.io';
7
+ import { join } from 'path';
8
+ import { existsSync } from 'fs';
9
+ import { workflowRoutes } from './routes/workflows.js';
10
+ import { aiRoutes } from './routes/ai.js';
11
+ import { executeRoutes } from './routes/execute.js';
12
+ import { toolsRoutes } from './routes/tools.js';
13
+ import { setupWebSocket } from './websocket/index.js';
14
+ import { FileWatcher } from './services/FileWatcher.js';
15
+
16
+ export interface ServerOptions {
17
+ port?: number;
18
+ workflowDir?: string;
19
+ staticDir?: string;
20
+ }
21
+
22
+ let httpServer: Server | null = null;
23
+ let fileWatcher: FileWatcher | null = null;
24
+
25
+ /**
26
+ * Start the GUI server programmatically
27
+ */
28
+ export async function startServer(options: ServerOptions = {}): Promise<Server> {
29
+ const PORT = options.port || parseInt(process.env.PORT || '3001', 10);
30
+ const WORKFLOW_DIR = options.workflowDir || process.env.WORKFLOW_DIR || process.cwd();
31
+ const STATIC_DIR = options.staticDir || process.env.STATIC_DIR;
32
+
33
+ const app = express();
34
+ httpServer = createServer(app);
35
+ const io = new SocketIOServer(httpServer, {
36
+ cors: {
37
+ origin: ['http://localhost:5173', 'http://localhost:3000', `http://localhost:${PORT}`],
38
+ methods: ['GET', 'POST'],
39
+ },
40
+ });
41
+
42
+ // Middleware
43
+ app.use(cors());
44
+ app.use(express.json());
45
+
46
+ // Routes
47
+ app.use('/api/workflows', workflowRoutes);
48
+ app.use('/api/ai', aiRoutes);
49
+ app.use('/api/execute', executeRoutes);
50
+ app.use('/api/tools', toolsRoutes);
51
+
52
+ // Health check
53
+ app.get('/api/health', (_req, res) => {
54
+ res.json({ status: 'ok', version: '2.0.0-alpha.1' });
55
+ });
56
+
57
+ // Serve static files if static dir is provided
58
+ if (STATIC_DIR && existsSync(STATIC_DIR)) {
59
+ app.use(express.static(STATIC_DIR));
60
+ // SPA fallback
61
+ app.get('*', (_req, res) => {
62
+ res.sendFile(join(STATIC_DIR, 'index.html'));
63
+ });
64
+ }
65
+
66
+ // WebSocket
67
+ setupWebSocket(io);
68
+
69
+ // File watcher for live updates
70
+ fileWatcher = new FileWatcher(WORKFLOW_DIR, io);
71
+
72
+ return new Promise((resolve) => {
73
+ httpServer!.listen(PORT, () => {
74
+ console.log(`
75
+ ╔══════════════════════════════════════════════════════════╗
76
+ ║ ║
77
+ ║ Marktoflow GUI Server ║
78
+ ║ ║
79
+ ║ Server: http://localhost:${String(PORT).padEnd(25)}║
80
+ ║ Workflows: ${WORKFLOW_DIR.slice(0, 40).padEnd(40)}║
81
+ ║ ║
82
+ ╚══════════════════════════════════════════════════════════╝
83
+ `);
84
+ resolve(httpServer!);
85
+ });
86
+ });
87
+ }
88
+
89
+ /**
90
+ * Stop the GUI server
91
+ */
92
+ export function stopServer(): void {
93
+ if (fileWatcher) {
94
+ fileWatcher.stop();
95
+ fileWatcher = null;
96
+ }
97
+ if (httpServer) {
98
+ httpServer.close();
99
+ httpServer = null;
100
+ }
101
+ }
102
+
103
+ // Graceful shutdown
104
+ process.on('SIGINT', () => {
105
+ console.log('\nShutting down...');
106
+ stopServer();
107
+ process.exit(0);
108
+ });
109
+
110
+ // Auto-start if run directly
111
+ const isDirectRun = process.argv[1]?.endsWith('index.js') || process.argv[1]?.endsWith('index.ts');
112
+ if (isDirectRun) {
113
+ startServer();
114
+ }
@@ -0,0 +1,91 @@
1
+ import { Router, type Router as RouterType } from 'express';
2
+ import { AIService } from '../services/AIService.js';
3
+
4
+ const router: RouterType = Router();
5
+ const aiService = new AIService();
6
+
7
+ // Process AI prompt
8
+ router.post('/prompt', async (req, res) => {
9
+ try {
10
+ const { prompt, workflow } = req.body;
11
+
12
+ if (!prompt) {
13
+ return res.status(400).json({ error: 'Prompt is required' });
14
+ }
15
+
16
+ const result = await aiService.processPrompt(prompt, workflow);
17
+ res.json(result);
18
+ } catch (error) {
19
+ res.status(500).json({
20
+ error: 'Failed to process prompt',
21
+ message: error instanceof Error ? error.message : 'Unknown error',
22
+ });
23
+ }
24
+ });
25
+
26
+ // Get prompt history
27
+ router.get('/history', async (_req, res) => {
28
+ try {
29
+ const history = await aiService.getHistory();
30
+ res.json({ history });
31
+ } catch (error) {
32
+ res.status(500).json({
33
+ error: 'Failed to get history',
34
+ message: error instanceof Error ? error.message : 'Unknown error',
35
+ });
36
+ }
37
+ });
38
+
39
+ // Get AI suggestions for current context
40
+ router.post('/suggestions', async (req, res) => {
41
+ try {
42
+ const { workflow, selectedStepId } = req.body;
43
+ const suggestions = await aiService.getSuggestions(workflow, selectedStepId);
44
+ res.json({ suggestions });
45
+ } catch (error) {
46
+ res.status(500).json({
47
+ error: 'Failed to get suggestions',
48
+ message: error instanceof Error ? error.message : 'Unknown error',
49
+ });
50
+ }
51
+ });
52
+
53
+ // Get available AI providers and status
54
+ router.get('/providers', async (_req, res) => {
55
+ try {
56
+ const status = aiService.getStatus();
57
+ res.json(status);
58
+ } catch (error) {
59
+ res.status(500).json({
60
+ error: 'Failed to get providers',
61
+ message: error instanceof Error ? error.message : 'Unknown error',
62
+ });
63
+ }
64
+ });
65
+
66
+ // Set active AI provider
67
+ router.post('/providers/:providerId', async (req, res) => {
68
+ try {
69
+ const { providerId } = req.params;
70
+ const { apiKey, baseUrl, model } = req.body;
71
+
72
+ const success = await aiService.setProvider(providerId, { apiKey, baseUrl, model });
73
+
74
+ if (success) {
75
+ const status = aiService.getStatus();
76
+ res.json({ success: true, status });
77
+ } else {
78
+ res.status(400).json({
79
+ error: 'Failed to set provider',
80
+ message: `Provider "${providerId}" is not available or failed to initialize`,
81
+ });
82
+ }
83
+ } catch (error) {
84
+ res.status(500).json({
85
+ error: 'Failed to set provider',
86
+ message: error instanceof Error ? error.message : 'Unknown error',
87
+ });
88
+ }
89
+ });
90
+
91
+ export { router as aiRoutes };