opc-agent 1.4.0 → 2.0.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.
- package/CHANGELOG.md +25 -0
- package/README.md +91 -32
- package/dist/channels/email.d.ts +32 -26
- package/dist/channels/email.js +239 -62
- package/dist/channels/feishu.d.ts +21 -6
- package/dist/channels/feishu.js +225 -126
- package/dist/channels/telegram.d.ts +30 -9
- package/dist/channels/telegram.js +125 -33
- package/dist/channels/websocket.d.ts +46 -3
- package/dist/channels/websocket.js +306 -37
- package/dist/channels/wechat.d.ts +33 -13
- package/dist/channels/wechat.js +229 -42
- package/dist/cli.js +1127 -19
- package/dist/core/a2a.d.ts +17 -0
- package/dist/core/a2a.js +43 -1
- package/dist/core/agent.d.ts +39 -0
- package/dist/core/agent.js +228 -3
- package/dist/core/runtime.d.ts +7 -0
- package/dist/core/runtime.js +205 -2
- package/dist/core/sandbox.d.ts +26 -0
- package/dist/core/sandbox.js +117 -0
- package/dist/core/scheduler.d.ts +52 -0
- package/dist/core/scheduler.js +168 -0
- package/dist/core/subagent.d.ts +28 -0
- package/dist/core/subagent.js +65 -0
- package/dist/core/workflow-graph.d.ts +93 -0
- package/dist/core/workflow-graph.js +247 -0
- package/dist/daemon.d.ts +3 -0
- package/dist/daemon.js +134 -0
- package/dist/doctor.d.ts +15 -0
- package/dist/doctor.js +183 -0
- package/dist/eval/index.d.ts +65 -0
- package/dist/eval/index.js +191 -0
- package/dist/index.d.ts +37 -6
- package/dist/index.js +75 -3
- package/dist/plugins/content-filter.d.ts +7 -0
- package/dist/plugins/content-filter.js +25 -0
- package/dist/plugins/index.d.ts +42 -0
- package/dist/plugins/index.js +108 -2
- package/dist/plugins/logger.d.ts +6 -0
- package/dist/plugins/logger.js +20 -0
- package/dist/plugins/rate-limiter.d.ts +7 -0
- package/dist/plugins/rate-limiter.js +35 -0
- package/dist/protocols/a2a/client.d.ts +25 -0
- package/dist/protocols/a2a/client.js +115 -0
- package/dist/protocols/a2a/index.d.ts +6 -0
- package/dist/protocols/a2a/index.js +12 -0
- package/dist/protocols/a2a/server.d.ts +41 -0
- package/dist/protocols/a2a/server.js +295 -0
- package/dist/protocols/a2a/types.d.ts +91 -0
- package/dist/protocols/a2a/types.js +15 -0
- package/dist/protocols/a2a/utils.d.ts +6 -0
- package/dist/protocols/a2a/utils.js +47 -0
- package/dist/protocols/agui/client.d.ts +10 -0
- package/dist/protocols/agui/client.js +75 -0
- package/dist/protocols/agui/index.d.ts +4 -0
- package/dist/protocols/agui/index.js +25 -0
- package/dist/protocols/agui/server.d.ts +37 -0
- package/dist/protocols/agui/server.js +191 -0
- package/dist/protocols/agui/types.d.ts +107 -0
- package/dist/protocols/agui/types.js +17 -0
- package/dist/protocols/index.d.ts +2 -0
- package/dist/protocols/index.js +19 -0
- package/dist/protocols/mcp/agent-tools.d.ts +11 -0
- package/dist/protocols/mcp/agent-tools.js +129 -0
- package/dist/protocols/mcp/index.d.ts +5 -0
- package/dist/protocols/mcp/index.js +11 -0
- package/dist/protocols/mcp/server.d.ts +31 -0
- package/dist/protocols/mcp/server.js +248 -0
- package/dist/protocols/mcp/types.d.ts +92 -0
- package/dist/protocols/mcp/types.js +17 -0
- package/dist/providers/index.d.ts +5 -1
- package/dist/providers/index.js +16 -9
- package/dist/publish/index.d.ts +45 -0
- package/dist/publish/index.js +350 -0
- package/dist/schema/oad.d.ts +859 -67
- package/dist/schema/oad.js +47 -3
- package/dist/security/approval.d.ts +36 -0
- package/dist/security/approval.js +113 -0
- package/dist/security/index.d.ts +4 -0
- package/dist/security/index.js +8 -0
- package/dist/security/keys.d.ts +16 -0
- package/dist/security/keys.js +117 -0
- package/dist/skills/auto-learn.d.ts +28 -0
- package/dist/skills/auto-learn.js +257 -0
- package/dist/studio/server.d.ts +63 -0
- package/dist/studio/server.js +625 -0
- package/dist/studio-ui/index.html +662 -0
- package/dist/telemetry/index.d.ts +93 -0
- package/dist/telemetry/index.js +285 -0
- package/dist/tools/builtin/datetime.d.ts +3 -0
- package/dist/tools/builtin/datetime.js +44 -0
- package/dist/tools/builtin/file.d.ts +3 -0
- package/dist/tools/builtin/file.js +151 -0
- package/dist/tools/builtin/index.d.ts +15 -0
- package/dist/tools/builtin/index.js +30 -0
- package/dist/tools/builtin/shell.d.ts +3 -0
- package/dist/tools/builtin/shell.js +43 -0
- package/dist/tools/builtin/web.d.ts +3 -0
- package/dist/tools/builtin/web.js +37 -0
- package/dist/tools/mcp-client.d.ts +24 -0
- package/dist/tools/mcp-client.js +119 -0
- package/package.json +5 -3
- package/scripts/install.ps1 +31 -0
- package/scripts/install.sh +40 -0
- package/src/channels/email.ts +351 -177
- package/src/channels/feishu.ts +349 -236
- package/src/channels/telegram.ts +212 -90
- package/src/channels/websocket.ts +399 -87
- package/src/channels/wechat.ts +329 -149
- package/src/cli.ts +1201 -20
- package/src/core/a2a.ts +60 -0
- package/src/core/agent.ts +420 -152
- package/src/core/runtime.ts +174 -0
- package/src/core/sandbox.ts +143 -0
- package/src/core/scheduler.ts +187 -0
- package/src/core/subagent.ts +98 -0
- package/src/core/workflow-graph.ts +365 -0
- package/src/daemon.ts +96 -0
- package/src/doctor.ts +156 -0
- package/src/eval/index.ts +211 -0
- package/src/eval/suites/basic.json +16 -0
- package/src/eval/suites/memory.json +12 -0
- package/src/eval/suites/safety.json +14 -0
- package/src/index.ts +65 -6
- package/src/plugins/content-filter.ts +23 -0
- package/src/plugins/index.ts +133 -2
- package/src/plugins/logger.ts +18 -0
- package/src/plugins/rate-limiter.ts +38 -0
- package/src/protocols/a2a/client.ts +132 -0
- package/src/protocols/a2a/index.ts +8 -0
- package/src/protocols/a2a/server.ts +333 -0
- package/src/protocols/a2a/types.ts +88 -0
- package/src/protocols/a2a/utils.ts +50 -0
- package/src/protocols/agui/client.ts +83 -0
- package/src/protocols/agui/index.ts +4 -0
- package/src/protocols/agui/server.ts +218 -0
- package/src/protocols/agui/types.ts +153 -0
- package/src/protocols/index.ts +2 -0
- package/src/protocols/mcp/agent-tools.ts +134 -0
- package/src/protocols/mcp/index.ts +8 -0
- package/src/protocols/mcp/server.ts +262 -0
- package/src/protocols/mcp/types.ts +69 -0
- package/src/providers/index.ts +354 -339
- package/src/publish/index.ts +376 -0
- package/src/schema/oad.ts +204 -154
- package/src/security/approval.ts +131 -0
- package/src/security/index.ts +3 -0
- package/src/security/keys.ts +87 -0
- package/src/skills/auto-learn.ts +262 -0
- package/src/studio/server.ts +629 -0
- package/src/studio-ui/index.html +662 -0
- package/src/telemetry/index.ts +324 -0
- package/src/tools/builtin/datetime.ts +41 -0
- package/src/tools/builtin/file.ts +107 -0
- package/src/tools/builtin/index.ts +28 -0
- package/src/tools/builtin/shell.ts +43 -0
- package/src/tools/builtin/web.ts +35 -0
- package/src/tools/mcp-client.ts +131 -0
- package/src/types/agent-workstation.d.ts +2 -0
- package/tests/a2a-protocol.test.ts +285 -0
- package/tests/agui-protocol.test.ts +246 -0
- package/tests/auto-learn.test.ts +105 -0
- package/tests/builtin-tools.test.ts +83 -0
- package/tests/channels/discord.test.ts +79 -0
- package/tests/channels/email.test.ts +148 -0
- package/tests/channels/feishu.test.ts +123 -0
- package/tests/channels/telegram.test.ts +129 -0
- package/tests/channels/websocket.test.ts +53 -0
- package/tests/channels/wechat.test.ts +170 -0
- package/tests/chat-cli.test.ts +160 -0
- package/tests/cli.test.ts +46 -0
- package/tests/daemon.test.ts +135 -0
- package/tests/deepbrain-wire.test.ts +234 -0
- package/tests/doctor.test.ts +38 -0
- package/tests/eval.test.ts +173 -0
- package/tests/init-role.test.ts +124 -0
- package/tests/mcp-client.test.ts +92 -0
- package/tests/mcp-server.test.ts +178 -0
- package/tests/plugin-a2a-enhanced.test.ts +230 -0
- package/tests/publish.test.ts +231 -0
- package/tests/scheduler.test.ts +200 -0
- package/tests/security-enhanced.test.ts +233 -0
- package/tests/skill-learner.test.ts +161 -0
- package/tests/studio.test.ts +229 -0
- package/tests/subagent.test.ts +193 -0
- package/tests/telegram-discord.test.ts +60 -0
- package/tests/telemetry.test.ts +186 -0
- package/tests/tools/builtin-extended.test.ts +138 -0
- package/tests/workflow-graph.test.ts +279 -0
- package/tutorial/customer-service-agent/README.md +612 -0
- package/tutorial/customer-service-agent/SOUL.md +26 -0
- package/tutorial/customer-service-agent/agent.yaml +63 -0
- package/tutorial/customer-service-agent/package.json +19 -0
- package/tutorial/customer-service-agent/src/index.ts +69 -0
- package/tutorial/customer-service-agent/src/skills/faq.ts +27 -0
- package/tutorial/customer-service-agent/src/skills/ticket.ts +22 -0
- package/tutorial/customer-service-agent/tsconfig.json +14 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import * as os from 'os';
|
|
5
|
+
import { getBuiltinTools, getBuiltinToolsByName, fileTool, datetimeTool, shellTool, webTool } from '../../src/tools/builtin';
|
|
6
|
+
|
|
7
|
+
describe('getBuiltinTools', () => {
|
|
8
|
+
it('returns 4 tools', () => {
|
|
9
|
+
const tools = getBuiltinTools();
|
|
10
|
+
expect(tools).toHaveLength(4);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('returns copies (not same array)', () => {
|
|
14
|
+
const a = getBuiltinTools();
|
|
15
|
+
const b = getBuiltinTools();
|
|
16
|
+
expect(a).not.toBe(b);
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('contains file_operations tool', () => {
|
|
20
|
+
const tools = getBuiltinTools();
|
|
21
|
+
expect(tools.some(t => t.name === 'file_operations')).toBe(true);
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
it('contains datetime tool', () => {
|
|
25
|
+
const tools = getBuiltinTools();
|
|
26
|
+
expect(tools.some(t => t.name === 'datetime')).toBe(true);
|
|
27
|
+
});
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe('getBuiltinToolsByName', () => {
|
|
31
|
+
it('returns all when no names given', () => {
|
|
32
|
+
expect(getBuiltinToolsByName()).toHaveLength(4);
|
|
33
|
+
expect(getBuiltinToolsByName([])).toHaveLength(4);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('filters by name correctly', () => {
|
|
37
|
+
const tools = getBuiltinToolsByName(['datetime']);
|
|
38
|
+
expect(tools).toHaveLength(1);
|
|
39
|
+
expect(tools[0].name).toBe('datetime');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('returns empty for unknown names', () => {
|
|
43
|
+
const tools = getBuiltinToolsByName(['nonexistent']);
|
|
44
|
+
expect(tools).toHaveLength(0);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it('returns multiple matching tools', () => {
|
|
48
|
+
const tools = getBuiltinToolsByName(['datetime', 'file_operations']);
|
|
49
|
+
expect(tools).toHaveLength(2);
|
|
50
|
+
});
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
describe('fileTool', () => {
|
|
54
|
+
let tmpDir: string;
|
|
55
|
+
let origCwd: string;
|
|
56
|
+
|
|
57
|
+
beforeEach(() => {
|
|
58
|
+
tmpDir = fs.mkdtempSync(path.join(os.tmpdir(), 'builtin-test-'));
|
|
59
|
+
origCwd = process.cwd();
|
|
60
|
+
process.chdir(tmpDir);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
afterEach(() => {
|
|
64
|
+
process.chdir(origCwd);
|
|
65
|
+
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it('has name file_operations', () => {
|
|
69
|
+
expect(fileTool.name).toBe('file_operations');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('read existing file', async () => {
|
|
73
|
+
fs.writeFileSync(path.join(tmpDir, 'test.txt'), 'hello world');
|
|
74
|
+
const result = await fileTool.execute({ action: 'read', path: 'test.txt' });
|
|
75
|
+
expect(result.content).toContain('hello world');
|
|
76
|
+
expect(result.isError).toBeFalsy();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('read non-existent returns error', async () => {
|
|
80
|
+
const result = await fileTool.execute({ action: 'read', path: 'nonexistent.txt' });
|
|
81
|
+
expect(result.isError).toBe(true);
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('write creates file', async () => {
|
|
85
|
+
const result = await fileTool.execute({ action: 'write', path: 'out.txt', content: 'data' });
|
|
86
|
+
expect(result.isError).toBeFalsy();
|
|
87
|
+
expect(fs.readFileSync(path.join(tmpDir, 'out.txt'), 'utf-8')).toBe('data');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('list directory', async () => {
|
|
91
|
+
fs.writeFileSync(path.join(tmpDir, 'a.txt'), 'a');
|
|
92
|
+
fs.writeFileSync(path.join(tmpDir, 'b.txt'), 'b');
|
|
93
|
+
const result = await fileTool.execute({ action: 'list', path: '.' });
|
|
94
|
+
expect(result.content).toContain('a.txt');
|
|
95
|
+
expect(result.content).toContain('b.txt');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('exists returns true for existing file', async () => {
|
|
99
|
+
fs.writeFileSync(path.join(tmpDir, 'exists.txt'), 'yes');
|
|
100
|
+
const result = await fileTool.execute({ action: 'exists', path: 'exists.txt' });
|
|
101
|
+
expect(result.content).toContain('true');
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it('exists returns false for missing file', async () => {
|
|
105
|
+
const result = await fileTool.execute({ action: 'exists', path: 'nope.txt' });
|
|
106
|
+
expect(result.content).toContain('false');
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it('path escape blocked (../)', async () => {
|
|
110
|
+
const result = await fileTool.execute({ action: 'read', path: '../../../etc/passwd' });
|
|
111
|
+
expect(result.isError).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
describe('datetimeTool', () => {
|
|
116
|
+
it('has name datetime', () => {
|
|
117
|
+
expect(datetimeTool.name).toBe('datetime');
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('returns valid ISO string', async () => {
|
|
121
|
+
const result = await datetimeTool.execute({});
|
|
122
|
+
expect(result.isError).toBeFalsy();
|
|
123
|
+
// Should contain a date-like string
|
|
124
|
+
expect(result.content).toMatch(/\d{4}/);
|
|
125
|
+
});
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
describe('shellTool', () => {
|
|
129
|
+
it('has name shell_exec', () => {
|
|
130
|
+
expect(shellTool.name).toBe('shell_exec');
|
|
131
|
+
});
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
describe('webTool', () => {
|
|
135
|
+
it('has name web_fetch', () => {
|
|
136
|
+
expect(webTool.name).toBe('web_fetch');
|
|
137
|
+
});
|
|
138
|
+
});
|
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
GraphWorkflowEngine,
|
|
4
|
+
WorkflowBuilder,
|
|
5
|
+
parseOADWorkflow,
|
|
6
|
+
type WorkflowContext,
|
|
7
|
+
type GraphWorkflow,
|
|
8
|
+
type GraphWorkflowStep,
|
|
9
|
+
} from '../src/core/workflow-graph';
|
|
10
|
+
|
|
11
|
+
function buildSimpleWorkflow(steps: Map<string, GraphWorkflowStep>, entry: string): GraphWorkflow {
|
|
12
|
+
return { name: 'test', entryPoint: entry, steps };
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
describe('GraphWorkflowEngine', () => {
|
|
16
|
+
const engine = new GraphWorkflowEngine();
|
|
17
|
+
|
|
18
|
+
it('executes a simple linear workflow', async () => {
|
|
19
|
+
const log: string[] = [];
|
|
20
|
+
const wf = new WorkflowBuilder()
|
|
21
|
+
.name('linear')
|
|
22
|
+
.start('a')
|
|
23
|
+
.addAction('a', async (ctx) => { log.push('a'); ctx.variables.set('x', 1); return 'done-a'; }, { next: 'b' })
|
|
24
|
+
.addAction('b', async (ctx) => { log.push('b'); return 'done-b'; }, { next: 'c' })
|
|
25
|
+
.addAction('c', async () => { log.push('c'); })
|
|
26
|
+
.build();
|
|
27
|
+
|
|
28
|
+
const result = await engine.execute(wf);
|
|
29
|
+
expect(result.status).toBe('completed');
|
|
30
|
+
expect(log).toEqual(['a', 'b', 'c']);
|
|
31
|
+
expect(result.context.results.get('a')).toBe('done-a');
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it('condition branches to onTrue', async () => {
|
|
35
|
+
const log: string[] = [];
|
|
36
|
+
const wf = new WorkflowBuilder()
|
|
37
|
+
.name('cond-true')
|
|
38
|
+
.start('check')
|
|
39
|
+
.addCondition('check', () => true, 'yes', 'no')
|
|
40
|
+
.addAction('yes', async () => { log.push('yes'); })
|
|
41
|
+
.addAction('no', async () => { log.push('no'); })
|
|
42
|
+
.build();
|
|
43
|
+
|
|
44
|
+
await engine.execute(wf);
|
|
45
|
+
expect(log).toEqual(['yes']);
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('condition branches to onFalse', async () => {
|
|
49
|
+
const log: string[] = [];
|
|
50
|
+
const wf = new WorkflowBuilder()
|
|
51
|
+
.name('cond-false')
|
|
52
|
+
.start('check')
|
|
53
|
+
.addCondition('check', () => false, 'yes', 'no')
|
|
54
|
+
.addAction('yes', async () => { log.push('yes'); })
|
|
55
|
+
.addAction('no', async () => { log.push('no'); })
|
|
56
|
+
.build();
|
|
57
|
+
|
|
58
|
+
await engine.execute(wf);
|
|
59
|
+
expect(log).toEqual(['no']);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it('condition uses context variables', async () => {
|
|
63
|
+
const wf = new WorkflowBuilder()
|
|
64
|
+
.name('cond-ctx')
|
|
65
|
+
.start('init')
|
|
66
|
+
.addAction('init', async (ctx) => { ctx.variables.set('flag', true); }, { next: 'check' })
|
|
67
|
+
.addCondition('check', (ctx) => ctx.variables.get('flag') === true, 'pass', 'fail')
|
|
68
|
+
.addAction('pass', async () => 'passed')
|
|
69
|
+
.addAction('fail', async () => 'failed')
|
|
70
|
+
.build();
|
|
71
|
+
|
|
72
|
+
const result = await engine.execute(wf);
|
|
73
|
+
expect(result.context.results.get('pass')).toBe('passed');
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it('parallel executes all steps', async () => {
|
|
77
|
+
const log: string[] = [];
|
|
78
|
+
const wf = new WorkflowBuilder()
|
|
79
|
+
.name('par')
|
|
80
|
+
.start('p')
|
|
81
|
+
.addAction('a', async () => { log.push('a'); })
|
|
82
|
+
.addAction('b', async () => { log.push('b'); })
|
|
83
|
+
.addParallel('p', ['a', 'b'], 'done')
|
|
84
|
+
.addAction('done', async () => { log.push('done'); })
|
|
85
|
+
.build();
|
|
86
|
+
|
|
87
|
+
const result = await engine.execute(wf);
|
|
88
|
+
expect(result.status).toBe('completed');
|
|
89
|
+
expect(log).toContain('a');
|
|
90
|
+
expect(log).toContain('b');
|
|
91
|
+
expect(log).toContain('done');
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it('loop iterates correct number of times', async () => {
|
|
95
|
+
let count = 0;
|
|
96
|
+
const wf = new WorkflowBuilder()
|
|
97
|
+
.name('loop')
|
|
98
|
+
.start('init')
|
|
99
|
+
.addAction('init', async (ctx) => { ctx.variables.set('i', 0); }, { next: 'loop' })
|
|
100
|
+
.addAction('body', async (ctx) => {
|
|
101
|
+
const i = ctx.variables.get('i');
|
|
102
|
+
ctx.variables.set('i', i + 1);
|
|
103
|
+
count++;
|
|
104
|
+
})
|
|
105
|
+
.addLoop('loop', (ctx) => (ctx.variables.get('i') ?? 0) < 5, 'body')
|
|
106
|
+
.build();
|
|
107
|
+
|
|
108
|
+
await engine.execute(wf);
|
|
109
|
+
expect(count).toBe(5);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('loop respects maxIterations', async () => {
|
|
113
|
+
let count = 0;
|
|
114
|
+
const wf = new WorkflowBuilder()
|
|
115
|
+
.name('loop-max')
|
|
116
|
+
.start('loop')
|
|
117
|
+
.addAction('body', async () => { count++; })
|
|
118
|
+
.addLoop('loop', () => true, 'body', { maxIterations: 3 })
|
|
119
|
+
.build();
|
|
120
|
+
|
|
121
|
+
await engine.execute(wf);
|
|
122
|
+
expect(count).toBe(3);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it('retries on failure then succeeds', async () => {
|
|
126
|
+
let attempts = 0;
|
|
127
|
+
const wf = new WorkflowBuilder()
|
|
128
|
+
.name('retry-ok')
|
|
129
|
+
.start('flaky')
|
|
130
|
+
.addAction('flaky', async () => {
|
|
131
|
+
attempts++;
|
|
132
|
+
if (attempts < 3) throw new Error('fail');
|
|
133
|
+
return 'ok';
|
|
134
|
+
}, { retryCount: 3, retryDelay: 1 })
|
|
135
|
+
.build();
|
|
136
|
+
|
|
137
|
+
const result = await engine.execute(wf);
|
|
138
|
+
expect(result.status).toBe('completed');
|
|
139
|
+
expect(attempts).toBe(3);
|
|
140
|
+
expect(result.context.results.get('flaky')).toBe('ok');
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
it('retry exhaustion records error', async () => {
|
|
144
|
+
const wf = new WorkflowBuilder()
|
|
145
|
+
.name('retry-fail')
|
|
146
|
+
.start('bad')
|
|
147
|
+
.addAction('bad', async () => { throw new Error('always-fail'); }, { retryCount: 2, retryDelay: 1, onError: 'stop' })
|
|
148
|
+
.build();
|
|
149
|
+
|
|
150
|
+
const result = await engine.execute(wf);
|
|
151
|
+
expect(result.status).toBe('failed');
|
|
152
|
+
expect(result.context.errors).toHaveLength(1);
|
|
153
|
+
expect(result.context.errors[0].step).toBe('bad');
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('timeout kills slow step', async () => {
|
|
157
|
+
const wf = new WorkflowBuilder()
|
|
158
|
+
.name('timeout')
|
|
159
|
+
.start('slow')
|
|
160
|
+
.addAction('slow', async () => {
|
|
161
|
+
await new Promise(r => setTimeout(r, 5000));
|
|
162
|
+
}, { timeout: 50, onError: 'stop' })
|
|
163
|
+
.build();
|
|
164
|
+
|
|
165
|
+
const result = await engine.execute(wf);
|
|
166
|
+
expect(result.status).toBe('failed');
|
|
167
|
+
expect(result.context.errors[0].error.message).toContain('Timeout');
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('onError skip continues to next step', async () => {
|
|
171
|
+
const log: string[] = [];
|
|
172
|
+
const wf = new WorkflowBuilder()
|
|
173
|
+
.name('skip')
|
|
174
|
+
.start('bad')
|
|
175
|
+
.addAction('bad', async () => { throw new Error('oops'); }, { onError: 'skip', next: 'good' })
|
|
176
|
+
.addAction('good', async () => { log.push('good'); })
|
|
177
|
+
.build();
|
|
178
|
+
|
|
179
|
+
const result = await engine.execute(wf);
|
|
180
|
+
// skip doesn't add to errors, doesn't throw
|
|
181
|
+
expect(log).toEqual(['good']);
|
|
182
|
+
expect(result.status).toBe('completed');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('onError stop halts execution', async () => {
|
|
186
|
+
const log: string[] = [];
|
|
187
|
+
const wf = new WorkflowBuilder()
|
|
188
|
+
.name('stop')
|
|
189
|
+
.start('bad')
|
|
190
|
+
.addAction('bad', async () => { throw new Error('halt'); }, { onError: 'stop', next: 'after' })
|
|
191
|
+
.addAction('after', async () => { log.push('after'); })
|
|
192
|
+
.build();
|
|
193
|
+
|
|
194
|
+
const result = await engine.execute(wf);
|
|
195
|
+
expect(result.status).toBe('failed');
|
|
196
|
+
expect(log).toEqual([]);
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it('WorkflowBuilder creates valid workflow', () => {
|
|
200
|
+
const wf = new WorkflowBuilder()
|
|
201
|
+
.name('builder-test')
|
|
202
|
+
.start('s1')
|
|
203
|
+
.addAction('s1', async () => 'ok', { next: 's2' })
|
|
204
|
+
.addAction('s2', async () => 'done')
|
|
205
|
+
.build();
|
|
206
|
+
|
|
207
|
+
expect(wf.name).toBe('builder-test');
|
|
208
|
+
expect(wf.entryPoint).toBe('s1');
|
|
209
|
+
expect(wf.steps.size).toBe(2);
|
|
210
|
+
expect(wf.steps.get('s1')?.next).toBe('s2');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('WorkflowBuilder throws without start', () => {
|
|
214
|
+
expect(() => new WorkflowBuilder().addAction('a', async () => {}).build())
|
|
215
|
+
.toThrow('entry point');
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
it('context variables persist across steps', async () => {
|
|
219
|
+
const wf = new WorkflowBuilder()
|
|
220
|
+
.name('persist')
|
|
221
|
+
.start('a')
|
|
222
|
+
.addAction('a', async (ctx) => { ctx.variables.set('msg', 'hello'); }, { next: 'b' })
|
|
223
|
+
.addAction('b', async (ctx) => {
|
|
224
|
+
return ctx.variables.get('msg') + ' world';
|
|
225
|
+
})
|
|
226
|
+
.build();
|
|
227
|
+
|
|
228
|
+
const result = await engine.execute(wf);
|
|
229
|
+
expect(result.context.results.get('b')).toBe('hello world');
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
it('parseOADWorkflow creates valid graph workflow', () => {
|
|
233
|
+
const def = {
|
|
234
|
+
name: 'onboarding',
|
|
235
|
+
steps: [
|
|
236
|
+
{ id: 'greet', type: 'action' as const, next: 'check' },
|
|
237
|
+
{ id: 'check', type: 'condition' as const, onTrue: 'existing', onFalse: 'new' },
|
|
238
|
+
{ id: 'existing', type: 'action' as const, next: 'done' },
|
|
239
|
+
{ id: 'new', type: 'action' as const, next: 'done' },
|
|
240
|
+
{ id: 'done', type: 'action' as const },
|
|
241
|
+
],
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
const actionMap = new Map<string, (ctx: WorkflowContext) => Promise<any>>();
|
|
245
|
+
actionMap.set('greet', async () => 'hi');
|
|
246
|
+
actionMap.set('existing', async () => 'welcome back');
|
|
247
|
+
actionMap.set('new', async () => 'welcome');
|
|
248
|
+
actionMap.set('done', async () => 'bye');
|
|
249
|
+
|
|
250
|
+
const condMap = new Map<string, (ctx: WorkflowContext) => boolean>();
|
|
251
|
+
condMap.set('check', () => true);
|
|
252
|
+
|
|
253
|
+
const wf = parseOADWorkflow(def, actionMap, condMap);
|
|
254
|
+
expect(wf.name).toBe('onboarding');
|
|
255
|
+
expect(wf.entryPoint).toBe('greet');
|
|
256
|
+
expect(wf.steps.size).toBe(5);
|
|
257
|
+
expect(wf.steps.get('check')?.type).toBe('condition');
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
it('parseOADWorkflow executes correctly', async () => {
|
|
261
|
+
const def = {
|
|
262
|
+
name: 'flow',
|
|
263
|
+
steps: [
|
|
264
|
+
{ id: 's1', type: 'action' as const, next: 's2' },
|
|
265
|
+
{ id: 's2', type: 'action' as const },
|
|
266
|
+
],
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const log: string[] = [];
|
|
270
|
+
const actionMap = new Map<string, (ctx: WorkflowContext) => Promise<any>>();
|
|
271
|
+
actionMap.set('s1', async () => { log.push('s1'); });
|
|
272
|
+
actionMap.set('s2', async () => { log.push('s2'); });
|
|
273
|
+
|
|
274
|
+
const wf = parseOADWorkflow(def, actionMap);
|
|
275
|
+
const result = await engine.execute(wf);
|
|
276
|
+
expect(result.status).toBe('completed');
|
|
277
|
+
expect(log).toEqual(['s1', 's2']);
|
|
278
|
+
});
|
|
279
|
+
});
|