@marktoflow/gui 2.0.0-alpha.1 → 2.0.0-alpha.12
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/.marktoflow/state/workflow-state.db +0 -0
- package/.marktoflow/state/workflow-state.db-shm +0 -0
- package/.marktoflow/state/workflow-state.db-wal +0 -0
- package/.turbo/turbo-build.log +24 -8
- package/.turbo/turbo-test.log +29 -13
- package/README.md +49 -3
- package/dist/client/assets/index-CM44OayM.js +704 -0
- package/dist/client/assets/index-CM44OayM.js.map +1 -0
- package/dist/client/assets/index-Dru63gi6.css +1 -0
- package/dist/client/index.html +2 -2
- package/dist/server/index.js +93 -33
- package/dist/server/index.js.map +1 -1
- package/dist/server/routes/ai.js +38 -1
- package/dist/server/routes/ai.js.map +1 -1
- package/dist/server/routes/execute.js +23 -22
- package/dist/server/routes/execute.js.map +1 -1
- package/dist/server/routes/executions.js +125 -0
- package/dist/server/routes/executions.js.map +1 -0
- package/dist/server/{server/routes → routes}/tools.js +406 -0
- package/dist/server/{server/routes → routes}/tools.js.map +1 -1
- package/dist/server/routes/workflows.js +41 -5
- package/dist/server/routes/workflows.js.map +1 -1
- package/dist/server/services/AIService.js +55 -202
- package/dist/server/services/AIService.js.map +1 -1
- package/dist/server/services/FileWatcher.js +0 -2
- package/dist/server/services/FileWatcher.js.map +1 -1
- package/dist/server/services/WorkflowService.js +199 -16
- package/dist/server/services/WorkflowService.js.map +1 -1
- package/dist/server/services/agents/codex-provider.js +270 -0
- package/dist/server/services/agents/codex-provider.js.map +1 -0
- package/dist/server/{server/services → services}/agents/prompts.js +27 -0
- package/dist/server/services/agents/prompts.js.map +1 -0
- package/dist/server/{server/services → services}/agents/registry.js +20 -0
- package/dist/server/services/agents/registry.js.map +1 -0
- package/dist/server/websocket/index.js +12 -0
- package/dist/server/websocket/index.js.map +1 -1
- package/marktoflow-gui-2.0.0-alpha.12.tgz +0 -0
- package/package.json +19 -5
- package/scripts/flatten-dist.js +69 -0
- package/src/client/components/Canvas/Canvas.tsx +27 -7
- package/src/client/components/Canvas/ExecutionOverlay.tsx +120 -32
- package/src/client/components/Canvas/ForEachNode.tsx +152 -0
- package/src/client/components/Canvas/IfElseNode.tsx +141 -0
- package/src/client/components/Canvas/NodeContextMenu.tsx +8 -4
- package/src/client/components/Canvas/ParallelNode.tsx +157 -0
- package/src/client/components/Canvas/SwitchNode.tsx +185 -0
- package/src/client/components/Canvas/Toolbar.tsx +59 -21
- package/src/client/components/Canvas/TransformNode.tsx +194 -0
- package/src/client/components/Canvas/TryCatchNode.tsx +164 -0
- package/src/client/components/Canvas/WhileNode.tsx +161 -0
- package/src/client/components/Canvas/index.ts +24 -0
- package/src/client/components/Debug/VariableInspector.tsx +148 -0
- package/src/client/components/Prompt/PromptInput.tsx +3 -1
- package/src/client/components/Settings/ProviderSwitcher.tsx +228 -0
- package/src/client/components/Sidebar/ImportDialog.tsx +257 -0
- package/src/client/components/Sidebar/Sidebar.tsx +21 -2
- package/src/client/components/common/KeyboardShortcuts.tsx +8 -2
- package/src/client/stores/agentStore.ts +109 -0
- package/src/client/stores/executionStore.ts +64 -2
- package/src/client/stores/workflowStore.ts +10 -2
- package/src/client/styles/globals.css +106 -0
- package/src/client/utils/platform.ts +46 -0
- package/src/client/utils/serviceIcons.tsx +33 -0
- package/src/client/utils/workflowToGraph.ts +245 -21
- package/src/server/index.ts +25 -2
- package/src/server/routes/executions.ts +136 -0
- package/src/server/routes/tools.ts +406 -0
- package/src/server/routes/workflows.ts +42 -1
- package/src/server/services/WorkflowService.ts +176 -16
- package/src/server/services/agents/codex-provider.ts +398 -0
- package/src/server/services/agents/prompts.ts +27 -0
- package/src/server/services/agents/registry.ts +21 -0
- package/src/server/websocket/index.ts +13 -0
- package/tailwind.config.ts +1 -1
- package/tests/integration/api.test.ts +203 -1
- package/tests/integration/testApp.ts +1 -1
- package/tests/setup.ts +35 -0
- package/tests/unit/ForEachNode.test.tsx +308 -0
- package/tests/unit/IfElseNode.test.tsx +235 -0
- package/tests/unit/ParallelNode.test.tsx +344 -0
- package/tests/unit/SwitchNode.test.tsx +327 -0
- package/tests/unit/TransformNode.test.tsx +386 -0
- package/tests/unit/TryCatchNode.test.tsx +243 -0
- package/tests/unit/WhileNode.test.tsx +230 -0
- package/tests/unit/agentStore.test.ts +218 -0
- package/tests/unit/codexProvider.test.ts +399 -0
- package/tests/unit/executionStore.test.ts +40 -0
- package/tests/unit/platform.test.ts +118 -0
- package/tests/unit/serviceIcons.test.ts +197 -0
- package/tests/unit/workflowToGraph.test.ts +22 -0
- package/dist/client/assets/index-DwTI8opO.js +0 -608
- package/dist/client/assets/index-DwTI8opO.js.map +0 -1
- package/dist/client/assets/index-RoEdL6gO.css +0 -1
- package/dist/server/index.d.ts +0 -3
- package/dist/server/index.d.ts.map +0 -1
- package/dist/server/server/index.js +0 -95
- package/dist/server/server/index.js.map +0 -1
- package/dist/server/server/routes/ai.js +0 -87
- package/dist/server/server/routes/ai.js.map +0 -1
- package/dist/server/server/routes/execute.js +0 -63
- package/dist/server/server/routes/execute.js.map +0 -1
- package/dist/server/server/routes/workflows.js +0 -99
- package/dist/server/server/routes/workflows.js.map +0 -1
- package/dist/server/server/services/AIService.js +0 -69
- package/dist/server/server/services/AIService.js.map +0 -1
- package/dist/server/server/services/FileWatcher.js +0 -60
- package/dist/server/server/services/FileWatcher.js.map +0 -1
- package/dist/server/server/services/WorkflowService.js +0 -363
- package/dist/server/server/services/WorkflowService.js.map +0 -1
- package/dist/server/server/services/agents/prompts.js.map +0 -1
- package/dist/server/server/services/agents/registry.js.map +0 -1
- package/dist/server/server/websocket/index.js +0 -85
- package/dist/server/server/websocket/index.js.map +0 -1
- package/dist/server/services/AIService.d.ts +0 -30
- package/dist/server/services/AIService.d.ts.map +0 -1
- package/dist/server/services/FileWatcher.d.ts +0 -10
- package/dist/server/services/FileWatcher.d.ts.map +0 -1
- package/dist/server/services/WorkflowService.d.ts +0 -54
- package/dist/server/services/WorkflowService.d.ts.map +0 -1
- package/dist/server/websocket/index.d.ts +0 -10
- package/dist/server/websocket/index.d.ts.map +0 -1
- /package/dist/server/{server/services → services}/agents/claude-code-provider.js +0 -0
- /package/dist/server/{server/services → services}/agents/claude-code-provider.js.map +0 -0
- /package/dist/server/{server/services → services}/agents/claude-provider.js +0 -0
- /package/dist/server/{server/services → services}/agents/claude-provider.js.map +0 -0
- /package/dist/server/{server/services → services}/agents/copilot-provider.js +0 -0
- /package/dist/server/{server/services → services}/agents/copilot-provider.js.map +0 -0
- /package/dist/server/{server/services → services}/agents/demo-provider.js +0 -0
- /package/dist/server/{server/services → services}/agents/demo-provider.js.map +0 -0
- /package/dist/server/{server/services → services}/agents/index.js +0 -0
- /package/dist/server/{server/services → services}/agents/index.js.map +0 -0
- /package/dist/server/{server/services → services}/agents/ollama-provider.js +0 -0
- /package/dist/server/{server/services → services}/agents/ollama-provider.js.map +0 -0
- /package/dist/server/{server/services → services}/agents/types.js +0 -0
- /package/dist/server/{server/services → services}/agents/types.js.map +0 -0
- /package/dist/{server/shared → shared}/constants.js +0 -0
- /package/dist/{server/shared → shared}/constants.js.map +0 -0
- /package/dist/{server/shared → shared}/types.js +0 -0
- /package/dist/{server/shared → shared}/types.js.map +0 -0
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { readdir, readFile, writeFile, unlink, mkdir } from 'fs/promises';
|
|
2
|
-
import { join, relative, dirname } from 'path';
|
|
2
|
+
import { join, relative, dirname, basename, extname } from 'path';
|
|
3
3
|
import { existsSync } from 'fs';
|
|
4
|
-
import { stringify as yamlStringify } from 'yaml';
|
|
4
|
+
import { stringify as yamlStringify, parse as yamlParse } from 'yaml';
|
|
5
|
+
import AdmZip from 'adm-zip';
|
|
5
6
|
// Import from @marktoflow/core for proper parsing
|
|
6
|
-
import { parseFile as coreParseFile } from '@marktoflow/core';
|
|
7
|
+
import { parseFile as coreParseFile, type ExecutionRecord } from '@marktoflow/core';
|
|
8
|
+
import { getStateStore } from '../index.js';
|
|
7
9
|
|
|
8
10
|
interface WorkflowListItem {
|
|
9
11
|
path: string;
|
|
@@ -44,6 +46,7 @@ interface Workflow {
|
|
|
44
46
|
tools?: Record<string, unknown>;
|
|
45
47
|
inputs?: Record<string, unknown>;
|
|
46
48
|
triggers?: unknown[];
|
|
49
|
+
markdown?: string;
|
|
47
50
|
}
|
|
48
51
|
|
|
49
52
|
export class WorkflowService {
|
|
@@ -75,17 +78,41 @@ export class WorkflowService {
|
|
|
75
78
|
entry.isFile() &&
|
|
76
79
|
(entry.name.endsWith('.md') || entry.name.endsWith('.yaml') || entry.name.endsWith('.yml'))
|
|
77
80
|
) {
|
|
78
|
-
//
|
|
81
|
+
// Skip README files (they're documentation, not workflows)
|
|
82
|
+
if (entry.name.toLowerCase() === 'readme.md') {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Check if it's a workflow file by verifying YAML frontmatter
|
|
79
87
|
try {
|
|
80
88
|
const content = await readFile(fullPath, 'utf-8');
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
89
|
+
|
|
90
|
+
// For markdown files, verify it has YAML frontmatter
|
|
91
|
+
if (entry.name.endsWith('.md')) {
|
|
92
|
+
if (!content.trimStart().startsWith('---\n')) {
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
const frontmatterMatch = content.match(/^---\n[\s\S]*?\n---/);
|
|
96
|
+
if (!frontmatterMatch) {
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const frontmatter = frontmatterMatch[0];
|
|
100
|
+
if (!frontmatter.includes('workflow:') && !frontmatter.includes('steps:')) {
|
|
101
|
+
continue;
|
|
102
|
+
}
|
|
103
|
+
} else {
|
|
104
|
+
// For YAML files, check if they contain workflow structure
|
|
105
|
+
if (!content.includes('workflow:') && !content.includes('steps:')) {
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
88
108
|
}
|
|
109
|
+
|
|
110
|
+
const relativePath = relative(baseDir, fullPath);
|
|
111
|
+
const workflowInfo = extractWorkflowInfo(content, entry.name);
|
|
112
|
+
workflows.push({
|
|
113
|
+
path: relativePath,
|
|
114
|
+
...workflowInfo,
|
|
115
|
+
});
|
|
89
116
|
} catch {
|
|
90
117
|
// Skip files that can't be read
|
|
91
118
|
}
|
|
@@ -108,6 +135,9 @@ export class WorkflowService {
|
|
|
108
135
|
}
|
|
109
136
|
|
|
110
137
|
try {
|
|
138
|
+
// Read the raw markdown content for GUI parser
|
|
139
|
+
const rawContent = await readFile(fullPath, 'utf-8');
|
|
140
|
+
|
|
111
141
|
// Use core parser for proper workflow parsing
|
|
112
142
|
const result = await coreParseFile(fullPath);
|
|
113
143
|
if (result.warnings && result.warnings.length > 0) {
|
|
@@ -115,7 +145,13 @@ export class WorkflowService {
|
|
|
115
145
|
}
|
|
116
146
|
|
|
117
147
|
// Convert core Workflow type to GUI Workflow type
|
|
118
|
-
|
|
148
|
+
const workflow = this.convertCoreWorkflow(result.workflow, workflowPath);
|
|
149
|
+
|
|
150
|
+
// Add raw markdown for GUI control flow parsing
|
|
151
|
+
return {
|
|
152
|
+
...workflow,
|
|
153
|
+
markdown: rawContent,
|
|
154
|
+
};
|
|
119
155
|
} catch (error) {
|
|
120
156
|
console.error(`Error parsing workflow ${workflowPath}:`, error);
|
|
121
157
|
// Fallback to local parsing if core parser fails
|
|
@@ -197,10 +233,134 @@ export class WorkflowService {
|
|
|
197
233
|
}
|
|
198
234
|
}
|
|
199
235
|
|
|
200
|
-
async getExecutionHistory(
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
236
|
+
async getExecutionHistory(workflowPath: string): Promise<ExecutionRecord[]> {
|
|
237
|
+
try {
|
|
238
|
+
const stateStore = getStateStore();
|
|
239
|
+
// Use the workflow path as the workflow ID for querying
|
|
240
|
+
const executions = stateStore.listExecutions({
|
|
241
|
+
workflowId: workflowPath,
|
|
242
|
+
limit: 50,
|
|
243
|
+
});
|
|
244
|
+
return executions;
|
|
245
|
+
} catch (error) {
|
|
246
|
+
console.error('Error getting execution history:', error);
|
|
247
|
+
return [];
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
async importWorkflow(
|
|
252
|
+
fileBuffer: Buffer,
|
|
253
|
+
originalFilename: string
|
|
254
|
+
): Promise<{ success: boolean; results?: Array<{ success: boolean; filename: string; message?: string }>; filename?: string; message?: string }> {
|
|
255
|
+
const ext = extname(originalFilename).toLowerCase();
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
// Handle ZIP files - extract and import all workflow files
|
|
259
|
+
if (ext === '.zip') {
|
|
260
|
+
return await this.importZipBundle(fileBuffer);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
// Handle single workflow files (.md, .yaml, .yml)
|
|
264
|
+
if (['.md', '.yaml', '.yml'].includes(ext)) {
|
|
265
|
+
const content = fileBuffer.toString('utf-8');
|
|
266
|
+
await this.importSingleWorkflow(content, originalFilename);
|
|
267
|
+
|
|
268
|
+
return {
|
|
269
|
+
success: true,
|
|
270
|
+
filename: originalFilename,
|
|
271
|
+
message: 'Workflow imported successfully',
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
throw new Error('Unsupported file type');
|
|
276
|
+
} catch (error) {
|
|
277
|
+
console.error('Import error:', error);
|
|
278
|
+
throw error;
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
private async importSingleWorkflow(content: string, filename: string): Promise<void> {
|
|
283
|
+
// Validate workflow content
|
|
284
|
+
try {
|
|
285
|
+
// Basic validation - try to parse YAML frontmatter or direct YAML
|
|
286
|
+
if (filename.endsWith('.md')) {
|
|
287
|
+
const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---/);
|
|
288
|
+
if (frontmatterMatch) {
|
|
289
|
+
yamlParse(frontmatterMatch[1]);
|
|
290
|
+
} else {
|
|
291
|
+
throw new Error('Invalid markdown workflow: missing YAML frontmatter');
|
|
292
|
+
}
|
|
293
|
+
} else {
|
|
294
|
+
yamlParse(content);
|
|
295
|
+
}
|
|
296
|
+
} catch (error) {
|
|
297
|
+
throw new Error(`Invalid workflow format: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Check for filename conflicts
|
|
301
|
+
let targetFilename = filename;
|
|
302
|
+
let fullPath = this.resolvePath(targetFilename);
|
|
303
|
+
let counter = 1;
|
|
304
|
+
|
|
305
|
+
while (existsSync(fullPath)) {
|
|
306
|
+
const nameWithoutExt = basename(filename, extname(filename));
|
|
307
|
+
targetFilename = `${nameWithoutExt}-${counter}${extname(filename)}`;
|
|
308
|
+
fullPath = this.resolvePath(targetFilename);
|
|
309
|
+
counter++;
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// Write file
|
|
313
|
+
await writeFile(fullPath, content, 'utf-8');
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
private async importZipBundle(zipBuffer: Buffer): Promise<{ success: boolean; results: Array<{ success: boolean; filename: string; message?: string }> }> {
|
|
317
|
+
const results: Array<{ success: boolean; filename: string; message?: string }> = [];
|
|
318
|
+
|
|
319
|
+
try {
|
|
320
|
+
const zip = new AdmZip(zipBuffer);
|
|
321
|
+
const zipEntries = zip.getEntries();
|
|
322
|
+
|
|
323
|
+
for (const entry of zipEntries) {
|
|
324
|
+
// Skip directories and non-workflow files
|
|
325
|
+
if (entry.isDirectory) continue;
|
|
326
|
+
|
|
327
|
+
const filename = basename(entry.entryName);
|
|
328
|
+
const ext = extname(filename).toLowerCase();
|
|
329
|
+
|
|
330
|
+
if (!['.md', '.yaml', '.yml'].includes(ext)) {
|
|
331
|
+
results.push({
|
|
332
|
+
success: false,
|
|
333
|
+
filename,
|
|
334
|
+
message: 'Skipped: not a workflow file',
|
|
335
|
+
});
|
|
336
|
+
continue;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
try {
|
|
340
|
+
const content = entry.getData().toString('utf-8');
|
|
341
|
+
await this.importSingleWorkflow(content, filename);
|
|
342
|
+
|
|
343
|
+
results.push({
|
|
344
|
+
success: true,
|
|
345
|
+
filename,
|
|
346
|
+
message: 'Imported successfully',
|
|
347
|
+
});
|
|
348
|
+
} catch (error) {
|
|
349
|
+
results.push({
|
|
350
|
+
success: false,
|
|
351
|
+
filename,
|
|
352
|
+
message: error instanceof Error ? error.message : 'Unknown error',
|
|
353
|
+
});
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
return {
|
|
358
|
+
success: results.some(r => r.success),
|
|
359
|
+
results,
|
|
360
|
+
};
|
|
361
|
+
} catch (error) {
|
|
362
|
+
throw new Error(`Failed to extract ZIP file: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
|
363
|
+
}
|
|
204
364
|
}
|
|
205
365
|
|
|
206
366
|
private resolvePath(workflowPath: string): string {
|
|
@@ -0,0 +1,398 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OpenAI Codex AI agent provider
|
|
3
|
+
* Uses the @openai/codex-sdk for AI capabilities
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { parse as yamlParse } from 'yaml';
|
|
7
|
+
import type {
|
|
8
|
+
AgentProvider,
|
|
9
|
+
AgentCapabilities,
|
|
10
|
+
AgentConfig,
|
|
11
|
+
PromptResult,
|
|
12
|
+
Workflow,
|
|
13
|
+
} from './types.js';
|
|
14
|
+
import { buildPrompt, generateSuggestions } from './prompts.js';
|
|
15
|
+
|
|
16
|
+
// Dynamic import types for Codex SDK
|
|
17
|
+
interface CodexSDK {
|
|
18
|
+
Codex: new (options?: CodexOptions) => CodexInstance;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface CodexOptions {
|
|
22
|
+
codexPathOverride?: string;
|
|
23
|
+
baseUrl?: string;
|
|
24
|
+
apiKey?: string;
|
|
25
|
+
env?: Record<string, string>;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
interface ThreadOptions {
|
|
29
|
+
model?: string;
|
|
30
|
+
sandboxMode?: 'read-only' | 'workspace-write' | 'danger-full-access';
|
|
31
|
+
workingDirectory?: string;
|
|
32
|
+
skipGitRepoCheck?: boolean;
|
|
33
|
+
modelReasoningEffort?: 'minimal' | 'low' | 'medium' | 'high' | 'xhigh';
|
|
34
|
+
networkAccessEnabled?: boolean;
|
|
35
|
+
webSearchMode?: 'disabled' | 'cached' | 'live';
|
|
36
|
+
approvalPolicy?: 'never' | 'on-request' | 'on-failure' | 'untrusted';
|
|
37
|
+
additionalDirectories?: string[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface ThreadItem {
|
|
41
|
+
id: string;
|
|
42
|
+
type: string;
|
|
43
|
+
text?: string;
|
|
44
|
+
command?: string;
|
|
45
|
+
aggregated_output?: string;
|
|
46
|
+
status?: string;
|
|
47
|
+
changes?: unknown[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface Usage {
|
|
51
|
+
input_tokens: number;
|
|
52
|
+
cached_input_tokens: number;
|
|
53
|
+
output_tokens: number;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
interface TurnResult {
|
|
57
|
+
items: ThreadItem[];
|
|
58
|
+
finalResponse: string;
|
|
59
|
+
usage: Usage | null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
interface ThreadEvent {
|
|
63
|
+
type: string;
|
|
64
|
+
thread_id?: string;
|
|
65
|
+
item?: ThreadItem;
|
|
66
|
+
usage?: Usage;
|
|
67
|
+
message?: string;
|
|
68
|
+
error?: { message: string };
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
interface StreamedTurnResult {
|
|
72
|
+
events: AsyncGenerator<ThreadEvent>;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
interface Thread {
|
|
76
|
+
id: string | null;
|
|
77
|
+
run(input: string, options?: unknown): Promise<TurnResult>;
|
|
78
|
+
runStreamed(input: string, options?: unknown): Promise<StreamedTurnResult>;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
interface CodexInstance {
|
|
82
|
+
startThread(options?: ThreadOptions): Thread;
|
|
83
|
+
resumeThread(id: string, options?: ThreadOptions): Thread;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export class CodexProvider implements AgentProvider {
|
|
87
|
+
readonly id = 'codex';
|
|
88
|
+
readonly name = 'OpenAI Codex';
|
|
89
|
+
readonly capabilities: AgentCapabilities = {
|
|
90
|
+
streaming: true,
|
|
91
|
+
toolUse: true,
|
|
92
|
+
codeExecution: true,
|
|
93
|
+
systemPrompts: true,
|
|
94
|
+
models: [
|
|
95
|
+
'codex-1',
|
|
96
|
+
'o3',
|
|
97
|
+
'o3-mini',
|
|
98
|
+
'o4-mini',
|
|
99
|
+
'gpt-4.1',
|
|
100
|
+
],
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
private codex: CodexInstance | null = null;
|
|
104
|
+
private model: string = 'codex-1';
|
|
105
|
+
private ready: boolean = false;
|
|
106
|
+
private error: string | undefined;
|
|
107
|
+
private workingDirectory: string = process.cwd();
|
|
108
|
+
private lastThreadId: string | null = null;
|
|
109
|
+
|
|
110
|
+
async initialize(config: AgentConfig): Promise<void> {
|
|
111
|
+
try {
|
|
112
|
+
// Try to import the Codex SDK (dynamic import with webpackIgnore to avoid bundling issues)
|
|
113
|
+
const sdkModule = (await import(
|
|
114
|
+
/* webpackIgnore: true */ '@openai/codex-sdk'
|
|
115
|
+
).catch(() => null)) as CodexSDK | null;
|
|
116
|
+
|
|
117
|
+
if (!sdkModule || !sdkModule.Codex) {
|
|
118
|
+
this.ready = false;
|
|
119
|
+
this.error = 'OpenAI Codex SDK not installed. Run: npm install @openai/codex-sdk';
|
|
120
|
+
return;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const { Codex } = sdkModule;
|
|
124
|
+
|
|
125
|
+
const codexOptions: CodexOptions = {};
|
|
126
|
+
|
|
127
|
+
// API key from config or environment
|
|
128
|
+
if (config.apiKey) {
|
|
129
|
+
codexOptions.apiKey = config.apiKey;
|
|
130
|
+
} else if (process.env.OPENAI_API_KEY) {
|
|
131
|
+
codexOptions.apiKey = process.env.OPENAI_API_KEY;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Base URL
|
|
135
|
+
if (config.baseUrl) {
|
|
136
|
+
codexOptions.baseUrl = config.baseUrl;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// Codex path override
|
|
140
|
+
if (config.options?.codexPath) {
|
|
141
|
+
codexOptions.codexPathOverride = config.options.codexPath as string;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Environment variables
|
|
145
|
+
if (config.options?.env) {
|
|
146
|
+
codexOptions.env = config.options.env as Record<string, string>;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
this.codex = new Codex(codexOptions);
|
|
150
|
+
|
|
151
|
+
if (config.model) {
|
|
152
|
+
this.model = config.model;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// Working directory
|
|
156
|
+
if (config.options?.workingDirectory) {
|
|
157
|
+
this.workingDirectory = config.options.workingDirectory as string;
|
|
158
|
+
} else if (config.options?.cwd) {
|
|
159
|
+
this.workingDirectory = config.options.cwd as string;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Test connectivity by starting a simple thread
|
|
163
|
+
try {
|
|
164
|
+
const thread = this.codex.startThread({
|
|
165
|
+
skipGitRepoCheck: true,
|
|
166
|
+
sandboxMode: 'read-only',
|
|
167
|
+
workingDirectory: this.workingDirectory,
|
|
168
|
+
});
|
|
169
|
+
// Just verify it can be created
|
|
170
|
+
if (thread) {
|
|
171
|
+
this.ready = true;
|
|
172
|
+
this.error = undefined;
|
|
173
|
+
}
|
|
174
|
+
} catch (testError) {
|
|
175
|
+
this.ready = false;
|
|
176
|
+
this.error = `Cannot initialize Codex: ${testError instanceof Error ? testError.message : 'Unknown error'}`;
|
|
177
|
+
}
|
|
178
|
+
} catch (err) {
|
|
179
|
+
this.ready = false;
|
|
180
|
+
this.error = err instanceof Error ? err.message : 'Unknown error initializing Codex';
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
isReady(): boolean {
|
|
185
|
+
return this.ready;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
getStatus(): { ready: boolean; model?: string; error?: string } {
|
|
189
|
+
return {
|
|
190
|
+
ready: this.ready,
|
|
191
|
+
model: this.model,
|
|
192
|
+
error: this.error,
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async processPrompt(
|
|
197
|
+
prompt: string,
|
|
198
|
+
workflow: Workflow,
|
|
199
|
+
context?: { selectedStepId?: string; recentHistory?: string[] }
|
|
200
|
+
): Promise<PromptResult> {
|
|
201
|
+
if (!this.codex || !this.ready) {
|
|
202
|
+
return {
|
|
203
|
+
explanation: 'OpenAI Codex provider not available.',
|
|
204
|
+
error: this.error || 'Provider not initialized',
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
try {
|
|
209
|
+
// Build context-aware prompts
|
|
210
|
+
const { systemPrompt, userPrompt } = buildPrompt(prompt, workflow, context);
|
|
211
|
+
|
|
212
|
+
// Combine system and user prompts for Codex
|
|
213
|
+
const fullPrompt = `${systemPrompt}\n\n---\n\n${userPrompt}`;
|
|
214
|
+
|
|
215
|
+
const thread = this.codex.startThread({
|
|
216
|
+
model: this.model,
|
|
217
|
+
skipGitRepoCheck: true,
|
|
218
|
+
sandboxMode: 'read-only', // Safe for workflow modifications
|
|
219
|
+
workingDirectory: this.workingDirectory,
|
|
220
|
+
modelReasoningEffort: 'medium',
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
const result = await thread.run(fullPrompt);
|
|
224
|
+
this.lastThreadId = thread.id;
|
|
225
|
+
|
|
226
|
+
// Extract the response text
|
|
227
|
+
const responseText = result.finalResponse ||
|
|
228
|
+
result.items.find((item) => item.type === 'agent_message')?.text || '';
|
|
229
|
+
|
|
230
|
+
return this.parseAIResponse(responseText, workflow);
|
|
231
|
+
} catch (err) {
|
|
232
|
+
return {
|
|
233
|
+
explanation: '',
|
|
234
|
+
error: err instanceof Error ? err.message : 'Unknown error',
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
async getSuggestions(workflow: Workflow, selectedStepId?: string): Promise<string[]> {
|
|
240
|
+
return generateSuggestions(workflow, selectedStepId);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
async streamPrompt(
|
|
244
|
+
prompt: string,
|
|
245
|
+
workflow: Workflow,
|
|
246
|
+
onChunk: (chunk: string) => void,
|
|
247
|
+
context?: { selectedStepId?: string; recentHistory?: string[] }
|
|
248
|
+
): Promise<PromptResult> {
|
|
249
|
+
if (!this.codex || !this.ready) {
|
|
250
|
+
return this.processPrompt(prompt, workflow, context);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
const { systemPrompt, userPrompt } = buildPrompt(prompt, workflow, context);
|
|
254
|
+
const fullPrompt = `${systemPrompt}\n\n---\n\n${userPrompt}`;
|
|
255
|
+
let fullResponse = '';
|
|
256
|
+
|
|
257
|
+
try {
|
|
258
|
+
const thread = this.codex.startThread({
|
|
259
|
+
model: this.model,
|
|
260
|
+
skipGitRepoCheck: true,
|
|
261
|
+
sandboxMode: 'read-only',
|
|
262
|
+
workingDirectory: this.workingDirectory,
|
|
263
|
+
modelReasoningEffort: 'medium',
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
const streamResult = await thread.runStreamed(fullPrompt);
|
|
267
|
+
|
|
268
|
+
for await (const event of streamResult.events) {
|
|
269
|
+
if (event.type === 'thread.started') {
|
|
270
|
+
this.lastThreadId = event.thread_id || null;
|
|
271
|
+
} else if (event.type === 'item.completed' && event.item?.type === 'agent_message') {
|
|
272
|
+
const text = event.item.text || '';
|
|
273
|
+
fullResponse = text;
|
|
274
|
+
onChunk(text);
|
|
275
|
+
} else if (event.type === 'error') {
|
|
276
|
+
throw new Error(event.message || 'Stream error');
|
|
277
|
+
} else if (event.type === 'turn.failed') {
|
|
278
|
+
throw new Error(event.error?.message || 'Turn failed');
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
return this.parseAIResponse(fullResponse, workflow);
|
|
283
|
+
} catch (err) {
|
|
284
|
+
return {
|
|
285
|
+
explanation: '',
|
|
286
|
+
error: err instanceof Error ? err.message : 'Unknown error',
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
async cancel(): Promise<void> {
|
|
292
|
+
// Codex SDK doesn't have explicit cancellation, but we can reset state
|
|
293
|
+
this.lastThreadId = null;
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/**
|
|
297
|
+
* Get the last thread ID for resumption
|
|
298
|
+
*/
|
|
299
|
+
getLastThreadId(): string | null {
|
|
300
|
+
return this.lastThreadId;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Resume a previous thread
|
|
305
|
+
*/
|
|
306
|
+
async resumePrompt(
|
|
307
|
+
threadId: string,
|
|
308
|
+
prompt: string,
|
|
309
|
+
workflow: Workflow,
|
|
310
|
+
context?: { selectedStepId?: string; recentHistory?: string[] }
|
|
311
|
+
): Promise<PromptResult> {
|
|
312
|
+
if (!this.codex || !this.ready) {
|
|
313
|
+
return {
|
|
314
|
+
explanation: 'OpenAI Codex provider not available.',
|
|
315
|
+
error: this.error || 'Provider not initialized',
|
|
316
|
+
};
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
try {
|
|
320
|
+
const { systemPrompt, userPrompt } = buildPrompt(prompt, workflow, context);
|
|
321
|
+
const fullPrompt = `${systemPrompt}\n\n---\n\n${userPrompt}`;
|
|
322
|
+
|
|
323
|
+
const thread = this.codex.resumeThread(threadId, {
|
|
324
|
+
model: this.model,
|
|
325
|
+
skipGitRepoCheck: true,
|
|
326
|
+
sandboxMode: 'read-only',
|
|
327
|
+
workingDirectory: this.workingDirectory,
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const result = await thread.run(fullPrompt);
|
|
331
|
+
this.lastThreadId = thread.id;
|
|
332
|
+
|
|
333
|
+
const responseText = result.finalResponse ||
|
|
334
|
+
result.items.find((item) => item.type === 'agent_message')?.text || '';
|
|
335
|
+
|
|
336
|
+
return this.parseAIResponse(responseText, workflow);
|
|
337
|
+
} catch (err) {
|
|
338
|
+
return {
|
|
339
|
+
explanation: '',
|
|
340
|
+
error: err instanceof Error ? err.message : 'Unknown error',
|
|
341
|
+
};
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
private parseAIResponse(responseText: string, originalWorkflow: Workflow): PromptResult {
|
|
346
|
+
const yamlMatch = responseText.match(/```yaml\n([\s\S]*?)\n```/);
|
|
347
|
+
let modifiedWorkflow: Workflow | undefined;
|
|
348
|
+
let explanation = responseText;
|
|
349
|
+
|
|
350
|
+
if (yamlMatch) {
|
|
351
|
+
try {
|
|
352
|
+
const parsedYaml = yamlParse(yamlMatch[1]);
|
|
353
|
+
if (parsedYaml && (parsedYaml.steps || parsedYaml.metadata)) {
|
|
354
|
+
modifiedWorkflow = parsedYaml as Workflow;
|
|
355
|
+
const explanationMatch = responseText.match(/^([\s\S]*?)```yaml/);
|
|
356
|
+
if (explanationMatch) {
|
|
357
|
+
explanation = explanationMatch[1].trim();
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
} catch {
|
|
361
|
+
// Failed to parse YAML
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
let diff: string | undefined;
|
|
366
|
+
if (modifiedWorkflow) {
|
|
367
|
+
diff = this.generateDiff(originalWorkflow, modifiedWorkflow);
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
return { explanation, workflow: modifiedWorkflow, diff };
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
private generateDiff(original: Workflow, modified: Workflow): string {
|
|
374
|
+
const originalStepIds = new Set(original.steps?.map((s) => s.id) || []);
|
|
375
|
+
const modifiedStepIds = new Set(modified.steps?.map((s) => s.id) || []);
|
|
376
|
+
|
|
377
|
+
const added = modified.steps?.filter((s) => !originalStepIds.has(s.id)) || [];
|
|
378
|
+
const removed = original.steps?.filter((s) => !modifiedStepIds.has(s.id)) || [];
|
|
379
|
+
|
|
380
|
+
let diff = '';
|
|
381
|
+
if (added.length > 0) {
|
|
382
|
+
diff += `+ Added ${added.length} step(s): ${added.map((s) => s.name || s.id).join(', ')}\n`;
|
|
383
|
+
}
|
|
384
|
+
if (removed.length > 0) {
|
|
385
|
+
diff += `- Removed ${removed.length} step(s): ${removed.map((s) => s.name || s.id).join(', ')}\n`;
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
return diff || 'No structural changes detected';
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
export function createCodexProvider(config?: AgentConfig): CodexProvider {
|
|
393
|
+
const provider = new CodexProvider();
|
|
394
|
+
if (config) {
|
|
395
|
+
provider.initialize(config);
|
|
396
|
+
}
|
|
397
|
+
return provider;
|
|
398
|
+
}
|
|
@@ -142,6 +142,33 @@ export const AVAILABLE_SERVICES = {
|
|
|
142
142
|
description: 'Local Ollama LLM for AI tasks',
|
|
143
143
|
commonActions: ['generate', 'chat', 'embeddings'],
|
|
144
144
|
},
|
|
145
|
+
codex: {
|
|
146
|
+
sdk: '@openai/codex-sdk',
|
|
147
|
+
description: 'OpenAI Codex for AI-powered coding workflows',
|
|
148
|
+
commonActions: [
|
|
149
|
+
'chat',
|
|
150
|
+
'codeModify',
|
|
151
|
+
'codeAnalyze',
|
|
152
|
+
'codeReview',
|
|
153
|
+
'webSearch',
|
|
154
|
+
'execute',
|
|
155
|
+
'structured',
|
|
156
|
+
'resume',
|
|
157
|
+
'withImages',
|
|
158
|
+
],
|
|
159
|
+
},
|
|
160
|
+
copilot: {
|
|
161
|
+
sdk: '@github/copilot-sdk',
|
|
162
|
+
description: 'GitHub Copilot for AI code assistance',
|
|
163
|
+
commonActions: [
|
|
164
|
+
'chat',
|
|
165
|
+
'codeReview',
|
|
166
|
+
'codeModify',
|
|
167
|
+
'withTools',
|
|
168
|
+
'withAgents',
|
|
169
|
+
'withMcp',
|
|
170
|
+
],
|
|
171
|
+
},
|
|
145
172
|
} as const;
|
|
146
173
|
|
|
147
174
|
// Base system prompt with comprehensive context
|
|
@@ -14,6 +14,7 @@ import type {
|
|
|
14
14
|
import { createClaudeProvider } from './claude-provider.js';
|
|
15
15
|
import { createClaudeCodeProvider } from './claude-code-provider.js';
|
|
16
16
|
import { createCopilotProvider } from './copilot-provider.js';
|
|
17
|
+
import { createCodexProvider } from './codex-provider.js';
|
|
17
18
|
import { createDemoProvider } from './demo-provider.js';
|
|
18
19
|
import { createOllamaProvider } from './ollama-provider.js';
|
|
19
20
|
|
|
@@ -38,6 +39,11 @@ export class AgentRegistry {
|
|
|
38
39
|
name: 'GitHub Copilot (SDK)',
|
|
39
40
|
factory: createCopilotProvider,
|
|
40
41
|
});
|
|
42
|
+
this.registerProvider({
|
|
43
|
+
id: 'codex',
|
|
44
|
+
name: 'OpenAI Codex (SDK)',
|
|
45
|
+
factory: createCodexProvider,
|
|
46
|
+
});
|
|
41
47
|
// API key-based providers
|
|
42
48
|
this.registerProvider({
|
|
43
49
|
id: 'claude',
|
|
@@ -153,6 +159,21 @@ export class AgentRegistry {
|
|
|
153
159
|
}
|
|
154
160
|
}
|
|
155
161
|
|
|
162
|
+
// Try OpenAI Codex SDK (uses OPENAI_API_KEY)
|
|
163
|
+
const codexProvider = this.providers.get('codex');
|
|
164
|
+
if (codexProvider) {
|
|
165
|
+
await codexProvider.initialize({
|
|
166
|
+
apiKey: process.env.OPENAI_API_KEY,
|
|
167
|
+
options: {
|
|
168
|
+
cwd: process.cwd(),
|
|
169
|
+
},
|
|
170
|
+
});
|
|
171
|
+
if (codexProvider.isReady()) {
|
|
172
|
+
this.activeProviderId = 'codex';
|
|
173
|
+
return 'codex';
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
|
|
156
177
|
// Try Claude with API key (requires ANTHROPIC_API_KEY)
|
|
157
178
|
const claudeProvider = this.providers.get('claude');
|
|
158
179
|
if (claudeProvider) {
|