opc-agent 2.0.0 ā 2.0.2
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/README.md +545 -365
- 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/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 +712 -11
- package/dist/core/a2a.d.ts +17 -0
- package/dist/core/a2a.js +43 -1
- package/dist/core/agent.d.ts +16 -0
- package/dist/core/agent.js +108 -0
- package/dist/core/runtime.d.ts +6 -0
- package/dist/core/runtime.js +161 -2
- package/dist/core/sandbox.d.ts +26 -0
- package/dist/core/sandbox.js +117 -0
- package/dist/core/workflow-graph.d.ts +93 -0
- package/dist/core/workflow-graph.js +247 -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 +32 -6
- package/dist/index.js +63 -4
- 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/publish/index.d.ts +45 -0
- package/dist/publish/index.js +350 -0
- package/dist/schema/oad.d.ts +682 -65
- package/dist/schema/oad.js +36 -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/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/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/websocket.ts +399 -87
- package/src/channels/wechat.ts +329 -149
- package/src/cli.ts +783 -12
- package/src/core/a2a.ts +60 -0
- package/src/core/agent.ts +125 -0
- package/src/core/runtime.ts +127 -0
- package/src/core/sandbox.ts +143 -0
- package/src/core/workflow-graph.ts +365 -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 +58 -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/publish/index.ts +376 -0
- package/src/schema/oad.ts +39 -2
- package/src/security/approval.ts +131 -0
- package/src/security/index.ts +3 -0
- package/src/security/keys.ts +87 -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/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/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/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 +63 -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
package/dist/core/sandbox.js
CHANGED
|
@@ -35,6 +35,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
36
|
exports.Sandbox = void 0;
|
|
37
37
|
const path = __importStar(require("path"));
|
|
38
|
+
const fs = __importStar(require("fs"));
|
|
38
39
|
const TRUST_RESTRICTIONS = {
|
|
39
40
|
sandbox: {
|
|
40
41
|
fileSystem: { read: ['.'], write: ['.'] },
|
|
@@ -60,6 +61,9 @@ const TRUST_RESTRICTIONS = {
|
|
|
60
61
|
class Sandbox {
|
|
61
62
|
config;
|
|
62
63
|
restrictions;
|
|
64
|
+
violations = 0;
|
|
65
|
+
maxFileSize;
|
|
66
|
+
maxFiles;
|
|
63
67
|
constructor(config) {
|
|
64
68
|
this.config = config;
|
|
65
69
|
this.restrictions = {
|
|
@@ -71,6 +75,11 @@ class Sandbox {
|
|
|
71
75
|
if (config.shellAllowed !== undefined) {
|
|
72
76
|
this.restrictions.shell = config.shellAllowed;
|
|
73
77
|
}
|
|
78
|
+
if (config.networkAccess === false) {
|
|
79
|
+
this.restrictions.network.allowed = [];
|
|
80
|
+
}
|
|
81
|
+
this.maxFileSize = config.maxFileSize ?? 10 * 1024 * 1024; // 10MB
|
|
82
|
+
this.maxFiles = config.maxFiles ?? 1000;
|
|
74
83
|
}
|
|
75
84
|
get trustLevel() {
|
|
76
85
|
return this.config.trustLevel;
|
|
@@ -113,6 +122,114 @@ class Sandbox {
|
|
|
113
122
|
checkShellAccess() {
|
|
114
123
|
return this.restrictions.shell;
|
|
115
124
|
}
|
|
125
|
+
validateFileOp(action, filePath) {
|
|
126
|
+
const resolved = path.resolve(filePath);
|
|
127
|
+
if (action === 'write' || action === 'delete') {
|
|
128
|
+
// Check read-only paths
|
|
129
|
+
if (this.config.readOnlyPaths) {
|
|
130
|
+
for (const ro of this.config.readOnlyPaths) {
|
|
131
|
+
const roResolved = path.resolve(ro);
|
|
132
|
+
if (resolved.startsWith(roResolved) || resolved === roResolved) {
|
|
133
|
+
this.violations++;
|
|
134
|
+
return { allowed: false, reason: `Path is read-only: ${ro}` };
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
// Check file size for writes
|
|
139
|
+
if (action === 'write') {
|
|
140
|
+
try {
|
|
141
|
+
if (fs.existsSync(resolved)) {
|
|
142
|
+
const stat = fs.statSync(resolved);
|
|
143
|
+
if (stat.size > this.maxFileSize) {
|
|
144
|
+
this.violations++;
|
|
145
|
+
return { allowed: false, reason: `File exceeds max size: ${this.maxFileSize} bytes` };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
// File doesn't exist yet ā that's fine
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
const mode = action === 'read' ? 'read' : 'write';
|
|
155
|
+
if (!this.checkFileAccess(filePath, mode)) {
|
|
156
|
+
this.violations++;
|
|
157
|
+
return { allowed: false, reason: `File access denied for ${action}: ${filePath}` };
|
|
158
|
+
}
|
|
159
|
+
return { allowed: true };
|
|
160
|
+
}
|
|
161
|
+
validateCommand(command) {
|
|
162
|
+
if (!this.restrictions.shell) {
|
|
163
|
+
this.violations++;
|
|
164
|
+
return { allowed: false, reason: 'Shell access is disabled' };
|
|
165
|
+
}
|
|
166
|
+
// Check blocklist
|
|
167
|
+
if (this.config.blockedCommands) {
|
|
168
|
+
for (const blocked of this.config.blockedCommands) {
|
|
169
|
+
if (command.includes(blocked)) {
|
|
170
|
+
this.violations++;
|
|
171
|
+
return { allowed: false, reason: `Command is blocked: ${blocked}` };
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
// Check allowlist (if set, only allowed commands pass)
|
|
176
|
+
if (this.config.allowedCommands && this.config.allowedCommands.length > 0) {
|
|
177
|
+
const allowed = this.config.allowedCommands.some(a => command.startsWith(a) || command.includes(a));
|
|
178
|
+
if (!allowed) {
|
|
179
|
+
this.violations++;
|
|
180
|
+
return { allowed: false, reason: 'Command not in allowlist' };
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
return { allowed: true };
|
|
184
|
+
}
|
|
185
|
+
validateNetwork(url) {
|
|
186
|
+
if (this.config.networkAccess === false) {
|
|
187
|
+
this.violations++;
|
|
188
|
+
return { allowed: false, reason: 'Network access is disabled' };
|
|
189
|
+
}
|
|
190
|
+
if (!this.checkNetworkAccess(url)) {
|
|
191
|
+
this.violations++;
|
|
192
|
+
return { allowed: false, reason: `Network access denied for: ${url}` };
|
|
193
|
+
}
|
|
194
|
+
return { allowed: true };
|
|
195
|
+
}
|
|
196
|
+
getStatus() {
|
|
197
|
+
let files = 0;
|
|
198
|
+
let totalSize = 0;
|
|
199
|
+
try {
|
|
200
|
+
const agentDir = path.resolve(this.config.agentDir);
|
|
201
|
+
if (fs.existsSync(agentDir)) {
|
|
202
|
+
const countFiles = (dir) => {
|
|
203
|
+
const entries = fs.readdirSync(dir, { withFileTypes: true });
|
|
204
|
+
for (const entry of entries) {
|
|
205
|
+
const full = path.join(dir, entry.name);
|
|
206
|
+
if (entry.isDirectory() && entry.name !== 'node_modules') {
|
|
207
|
+
countFiles(full);
|
|
208
|
+
}
|
|
209
|
+
else if (entry.isFile()) {
|
|
210
|
+
files++;
|
|
211
|
+
try {
|
|
212
|
+
totalSize += fs.statSync(full).size;
|
|
213
|
+
}
|
|
214
|
+
catch { }
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
};
|
|
218
|
+
countFiles(agentDir);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch { }
|
|
222
|
+
return { files, totalSize, violations: this.violations };
|
|
223
|
+
}
|
|
224
|
+
getViolations() {
|
|
225
|
+
return this.violations;
|
|
226
|
+
}
|
|
227
|
+
getMaxFileSize() {
|
|
228
|
+
return this.maxFileSize;
|
|
229
|
+
}
|
|
230
|
+
getMaxFiles() {
|
|
231
|
+
return this.maxFiles;
|
|
232
|
+
}
|
|
116
233
|
}
|
|
117
234
|
exports.Sandbox = Sandbox;
|
|
118
235
|
//# sourceMappingURL=sandbox.js.map
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Graph-based Workflow Engine with conditional branching, parallel execution,
|
|
3
|
+
* loops, retry, timeout, and error handling.
|
|
4
|
+
*
|
|
5
|
+
* This is a standalone engine that works with function-based steps (no skill/agent coupling).
|
|
6
|
+
* The original WorkflowEngine in workflow.ts is preserved for backward compatibility.
|
|
7
|
+
*/
|
|
8
|
+
export interface WorkflowContext {
|
|
9
|
+
variables: Map<string, any>;
|
|
10
|
+
results: Map<string, any>;
|
|
11
|
+
currentStep: string;
|
|
12
|
+
startTime: Date;
|
|
13
|
+
errors: Array<{
|
|
14
|
+
step: string;
|
|
15
|
+
error: Error;
|
|
16
|
+
}>;
|
|
17
|
+
}
|
|
18
|
+
export interface GraphWorkflowStep {
|
|
19
|
+
id: string;
|
|
20
|
+
name: string;
|
|
21
|
+
type: 'action' | 'condition' | 'parallel' | 'loop';
|
|
22
|
+
action?: (context: WorkflowContext) => Promise<any>;
|
|
23
|
+
condition?: (context: WorkflowContext) => boolean;
|
|
24
|
+
onTrue?: string;
|
|
25
|
+
onFalse?: string;
|
|
26
|
+
parallel?: string[];
|
|
27
|
+
loopCondition?: (context: WorkflowContext) => boolean;
|
|
28
|
+
loopBody?: string;
|
|
29
|
+
maxIterations?: number;
|
|
30
|
+
next?: string;
|
|
31
|
+
retryCount?: number;
|
|
32
|
+
retryDelay?: number;
|
|
33
|
+
timeout?: number;
|
|
34
|
+
onError?: 'stop' | 'skip' | 'retry';
|
|
35
|
+
}
|
|
36
|
+
export interface GraphWorkflow {
|
|
37
|
+
name: string;
|
|
38
|
+
entryPoint: string;
|
|
39
|
+
steps: Map<string, GraphWorkflowStep>;
|
|
40
|
+
}
|
|
41
|
+
export interface GraphWorkflowResult {
|
|
42
|
+
workflow: string;
|
|
43
|
+
status: 'completed' | 'failed';
|
|
44
|
+
context: WorkflowContext;
|
|
45
|
+
totalDurationMs: number;
|
|
46
|
+
}
|
|
47
|
+
export declare class GraphWorkflowEngine {
|
|
48
|
+
execute(workflow: GraphWorkflow): Promise<GraphWorkflowResult>;
|
|
49
|
+
private executeAction;
|
|
50
|
+
private executeParallel;
|
|
51
|
+
private executeLoop;
|
|
52
|
+
}
|
|
53
|
+
export declare class WorkflowBuilder {
|
|
54
|
+
private steps;
|
|
55
|
+
private entry;
|
|
56
|
+
private workflowName;
|
|
57
|
+
name(n: string): this;
|
|
58
|
+
start(id: string): this;
|
|
59
|
+
addAction(id: string, action: (context: WorkflowContext) => Promise<any>, options?: Partial<GraphWorkflowStep>): this;
|
|
60
|
+
addCondition(id: string, condition: (context: WorkflowContext) => boolean, onTrue: string, onFalse: string, options?: Partial<GraphWorkflowStep>): this;
|
|
61
|
+
addParallel(id: string, stepIds: string[], next?: string, options?: Partial<GraphWorkflowStep>): this;
|
|
62
|
+
addLoop(id: string, condition: (context: WorkflowContext) => boolean, body: string, options?: {
|
|
63
|
+
maxIterations?: number;
|
|
64
|
+
next?: string;
|
|
65
|
+
name?: string;
|
|
66
|
+
}): this;
|
|
67
|
+
build(): GraphWorkflow;
|
|
68
|
+
}
|
|
69
|
+
export interface OADWorkflowStepDef {
|
|
70
|
+
id: string;
|
|
71
|
+
type: 'action' | 'condition' | 'parallel' | 'loop';
|
|
72
|
+
name?: string;
|
|
73
|
+
next?: string;
|
|
74
|
+
onTrue?: string;
|
|
75
|
+
onFalse?: string;
|
|
76
|
+
parallel?: string[];
|
|
77
|
+
loopBody?: string;
|
|
78
|
+
maxIterations?: number;
|
|
79
|
+
retryCount?: number;
|
|
80
|
+
retryDelay?: number;
|
|
81
|
+
timeout?: number;
|
|
82
|
+
onError?: 'stop' | 'skip' | 'retry';
|
|
83
|
+
}
|
|
84
|
+
export interface OADWorkflowDef {
|
|
85
|
+
name: string;
|
|
86
|
+
steps: OADWorkflowStepDef[];
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Parse an OAD workflow definition into a GraphWorkflow.
|
|
90
|
+
* Action handlers must be supplied via the actionMap.
|
|
91
|
+
*/
|
|
92
|
+
export declare function parseOADWorkflow(def: OADWorkflowDef, actionMap?: Map<string, (context: WorkflowContext) => Promise<any>>, conditionMap?: Map<string, (context: WorkflowContext) => boolean>): GraphWorkflow;
|
|
93
|
+
//# sourceMappingURL=workflow-graph.d.ts.map
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Graph-based Workflow Engine with conditional branching, parallel execution,
|
|
4
|
+
* loops, retry, timeout, and error handling.
|
|
5
|
+
*
|
|
6
|
+
* This is a standalone engine that works with function-based steps (no skill/agent coupling).
|
|
7
|
+
* The original WorkflowEngine in workflow.ts is preserved for backward compatibility.
|
|
8
|
+
*/
|
|
9
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
10
|
+
exports.WorkflowBuilder = exports.GraphWorkflowEngine = void 0;
|
|
11
|
+
exports.parseOADWorkflow = parseOADWorkflow;
|
|
12
|
+
// āā Helpers āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
13
|
+
function sleep(ms) {
|
|
14
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
15
|
+
}
|
|
16
|
+
function rejectAfter(ms) {
|
|
17
|
+
return new Promise((_, reject) => setTimeout(() => reject(new Error(`Timeout after ${ms}ms`)), ms));
|
|
18
|
+
}
|
|
19
|
+
function neverResolve() {
|
|
20
|
+
return new Promise(() => { });
|
|
21
|
+
}
|
|
22
|
+
function createContext() {
|
|
23
|
+
return {
|
|
24
|
+
variables: new Map(),
|
|
25
|
+
results: new Map(),
|
|
26
|
+
currentStep: '',
|
|
27
|
+
startTime: new Date(),
|
|
28
|
+
errors: [],
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
// āā Graph Workflow Engine āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
32
|
+
class GraphWorkflowEngine {
|
|
33
|
+
async execute(workflow) {
|
|
34
|
+
const startTime = Date.now();
|
|
35
|
+
const context = createContext();
|
|
36
|
+
let currentStep = workflow.entryPoint;
|
|
37
|
+
try {
|
|
38
|
+
while (currentStep) {
|
|
39
|
+
const step = workflow.steps.get(currentStep);
|
|
40
|
+
if (!step)
|
|
41
|
+
break;
|
|
42
|
+
context.currentStep = step.id;
|
|
43
|
+
switch (step.type) {
|
|
44
|
+
case 'action':
|
|
45
|
+
await this.executeAction(step, context, workflow);
|
|
46
|
+
currentStep = step.next;
|
|
47
|
+
break;
|
|
48
|
+
case 'condition':
|
|
49
|
+
if (!step.condition)
|
|
50
|
+
throw new Error(`Step "${step.id}" missing condition function`);
|
|
51
|
+
const result = step.condition(context);
|
|
52
|
+
currentStep = result ? step.onTrue : step.onFalse;
|
|
53
|
+
break;
|
|
54
|
+
case 'parallel':
|
|
55
|
+
await this.executeParallel(step, context, workflow);
|
|
56
|
+
currentStep = step.next;
|
|
57
|
+
break;
|
|
58
|
+
case 'loop':
|
|
59
|
+
await this.executeLoop(step, context, workflow);
|
|
60
|
+
currentStep = step.next;
|
|
61
|
+
break;
|
|
62
|
+
default:
|
|
63
|
+
currentStep = step.next;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
catch (err) {
|
|
68
|
+
// Error already recorded in context.errors by executeAction
|
|
69
|
+
}
|
|
70
|
+
return {
|
|
71
|
+
workflow: workflow.name,
|
|
72
|
+
status: context.errors.length > 0 ? 'failed' : 'completed',
|
|
73
|
+
context,
|
|
74
|
+
totalDurationMs: Date.now() - startTime,
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
async executeAction(step, context, _workflow) {
|
|
78
|
+
if (!step.action) {
|
|
79
|
+
context.results.set(step.id, undefined);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
let attempts = 0;
|
|
83
|
+
const maxAttempts = (step.retryCount ?? 0) + 1;
|
|
84
|
+
const errorPolicy = step.onError ?? 'stop';
|
|
85
|
+
while (attempts < maxAttempts) {
|
|
86
|
+
try {
|
|
87
|
+
const promises = [step.action(context)];
|
|
88
|
+
if (step.timeout)
|
|
89
|
+
promises.push(rejectAfter(step.timeout));
|
|
90
|
+
else
|
|
91
|
+
promises.push(neverResolve());
|
|
92
|
+
const result = await Promise.race(promises);
|
|
93
|
+
context.results.set(step.id, result);
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
catch (e) {
|
|
97
|
+
attempts++;
|
|
98
|
+
if (attempts >= maxAttempts) {
|
|
99
|
+
if (errorPolicy === 'skip') {
|
|
100
|
+
context.results.set(step.id, undefined);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const error = e instanceof Error ? e : new Error(String(e));
|
|
104
|
+
context.errors.push({ step: step.id, error });
|
|
105
|
+
if (errorPolicy === 'stop')
|
|
106
|
+
throw error;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
if (step.retryDelay)
|
|
110
|
+
await sleep(step.retryDelay);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
async executeParallel(step, context, workflow) {
|
|
115
|
+
if (!step.parallel || step.parallel.length === 0)
|
|
116
|
+
return;
|
|
117
|
+
const tasks = step.parallel.map(async (stepId) => {
|
|
118
|
+
const subStep = workflow.steps.get(stepId);
|
|
119
|
+
if (!subStep)
|
|
120
|
+
throw new Error(`Parallel step "${stepId}" not found`);
|
|
121
|
+
if (subStep.type === 'action') {
|
|
122
|
+
await this.executeAction(subStep, context, workflow);
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
await Promise.all(tasks);
|
|
126
|
+
}
|
|
127
|
+
async executeLoop(step, context, workflow) {
|
|
128
|
+
if (!step.loopCondition || !step.loopBody)
|
|
129
|
+
return;
|
|
130
|
+
const max = step.maxIterations ?? 100;
|
|
131
|
+
let iterations = 0;
|
|
132
|
+
while (step.loopCondition(context) && iterations < max) {
|
|
133
|
+
const bodyStep = workflow.steps.get(step.loopBody);
|
|
134
|
+
if (!bodyStep)
|
|
135
|
+
break;
|
|
136
|
+
if (bodyStep.type === 'action') {
|
|
137
|
+
await this.executeAction(bodyStep, context, workflow);
|
|
138
|
+
}
|
|
139
|
+
iterations++;
|
|
140
|
+
}
|
|
141
|
+
context.results.set(step.id, { iterations });
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
exports.GraphWorkflowEngine = GraphWorkflowEngine;
|
|
145
|
+
// āā Workflow Builder āāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāāā
|
|
146
|
+
class WorkflowBuilder {
|
|
147
|
+
steps = new Map();
|
|
148
|
+
entry = '';
|
|
149
|
+
workflowName = 'unnamed';
|
|
150
|
+
name(n) {
|
|
151
|
+
this.workflowName = n;
|
|
152
|
+
return this;
|
|
153
|
+
}
|
|
154
|
+
start(id) {
|
|
155
|
+
this.entry = id;
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
addAction(id, action, options) {
|
|
159
|
+
this.steps.set(id, {
|
|
160
|
+
id,
|
|
161
|
+
name: options?.name ?? id,
|
|
162
|
+
type: 'action',
|
|
163
|
+
action,
|
|
164
|
+
...options,
|
|
165
|
+
});
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
addCondition(id, condition, onTrue, onFalse, options) {
|
|
169
|
+
this.steps.set(id, {
|
|
170
|
+
id,
|
|
171
|
+
name: options?.name ?? id,
|
|
172
|
+
type: 'condition',
|
|
173
|
+
condition,
|
|
174
|
+
onTrue,
|
|
175
|
+
onFalse,
|
|
176
|
+
...options,
|
|
177
|
+
});
|
|
178
|
+
return this;
|
|
179
|
+
}
|
|
180
|
+
addParallel(id, stepIds, next, options) {
|
|
181
|
+
this.steps.set(id, {
|
|
182
|
+
id,
|
|
183
|
+
name: options?.name ?? id,
|
|
184
|
+
type: 'parallel',
|
|
185
|
+
parallel: stepIds,
|
|
186
|
+
next,
|
|
187
|
+
...options,
|
|
188
|
+
});
|
|
189
|
+
return this;
|
|
190
|
+
}
|
|
191
|
+
addLoop(id, condition, body, options) {
|
|
192
|
+
this.steps.set(id, {
|
|
193
|
+
id,
|
|
194
|
+
name: options?.name ?? id,
|
|
195
|
+
type: 'loop',
|
|
196
|
+
loopCondition: condition,
|
|
197
|
+
loopBody: body,
|
|
198
|
+
maxIterations: options?.maxIterations,
|
|
199
|
+
next: options?.next,
|
|
200
|
+
});
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
build() {
|
|
204
|
+
if (!this.entry)
|
|
205
|
+
throw new Error('Workflow must have an entry point. Call start() first.');
|
|
206
|
+
return {
|
|
207
|
+
name: this.workflowName,
|
|
208
|
+
entryPoint: this.entry,
|
|
209
|
+
steps: new Map(this.steps),
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
exports.WorkflowBuilder = WorkflowBuilder;
|
|
214
|
+
/**
|
|
215
|
+
* Parse an OAD workflow definition into a GraphWorkflow.
|
|
216
|
+
* Action handlers must be supplied via the actionMap.
|
|
217
|
+
*/
|
|
218
|
+
function parseOADWorkflow(def, actionMap = new Map(), conditionMap = new Map()) {
|
|
219
|
+
const steps = new Map();
|
|
220
|
+
for (const s of def.steps) {
|
|
221
|
+
const step = {
|
|
222
|
+
id: s.id,
|
|
223
|
+
name: s.name ?? s.id,
|
|
224
|
+
type: s.type,
|
|
225
|
+
next: s.next,
|
|
226
|
+
onTrue: s.onTrue,
|
|
227
|
+
onFalse: s.onFalse,
|
|
228
|
+
parallel: s.parallel,
|
|
229
|
+
loopBody: s.loopBody,
|
|
230
|
+
maxIterations: s.maxIterations,
|
|
231
|
+
retryCount: s.retryCount,
|
|
232
|
+
retryDelay: s.retryDelay,
|
|
233
|
+
timeout: s.timeout,
|
|
234
|
+
onError: s.onError,
|
|
235
|
+
};
|
|
236
|
+
if (s.type === 'action') {
|
|
237
|
+
step.action = actionMap.get(s.id);
|
|
238
|
+
}
|
|
239
|
+
if (s.type === 'condition') {
|
|
240
|
+
step.condition = conditionMap.get(s.id);
|
|
241
|
+
}
|
|
242
|
+
steps.set(s.id, step);
|
|
243
|
+
}
|
|
244
|
+
const entryPoint = def.steps[0]?.id ?? '';
|
|
245
|
+
return { name: def.name, entryPoint, steps };
|
|
246
|
+
}
|
|
247
|
+
//# sourceMappingURL=workflow-graph.js.map
|
package/dist/doctor.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface CheckResult {
|
|
2
|
+
ok: boolean;
|
|
3
|
+
detail: string;
|
|
4
|
+
fix?: string;
|
|
5
|
+
}
|
|
6
|
+
export interface DoctorCheck {
|
|
7
|
+
name: string;
|
|
8
|
+
check: () => CheckResult | Promise<CheckResult>;
|
|
9
|
+
}
|
|
10
|
+
export declare function getDoctorChecks(): DoctorCheck[];
|
|
11
|
+
export declare function runDoctor(): Promise<{
|
|
12
|
+
passed: number;
|
|
13
|
+
total: number;
|
|
14
|
+
}>;
|
|
15
|
+
//# sourceMappingURL=doctor.d.ts.map
|
package/dist/doctor.js
ADDED
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.getDoctorChecks = getDoctorChecks;
|
|
37
|
+
exports.runDoctor = runDoctor;
|
|
38
|
+
const child_process_1 = require("child_process");
|
|
39
|
+
const fs_1 = require("fs");
|
|
40
|
+
const net = __importStar(require("net"));
|
|
41
|
+
function getDoctorChecks() {
|
|
42
|
+
return [
|
|
43
|
+
{
|
|
44
|
+
name: 'Node.js version',
|
|
45
|
+
check: () => {
|
|
46
|
+
const v = process.versions.node.split('.').map(Number);
|
|
47
|
+
return {
|
|
48
|
+
ok: v[0] >= 18,
|
|
49
|
+
detail: `v${process.versions.node}`,
|
|
50
|
+
fix: v[0] < 18 ? 'Upgrade to Node 18+: https://nodejs.org' : undefined,
|
|
51
|
+
};
|
|
52
|
+
},
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
name: 'npm version',
|
|
56
|
+
check: () => {
|
|
57
|
+
try {
|
|
58
|
+
const v = (0, child_process_1.execSync)('npm --version', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] }).trim();
|
|
59
|
+
return { ok: true, detail: `v${v}` };
|
|
60
|
+
}
|
|
61
|
+
catch {
|
|
62
|
+
return { ok: false, detail: 'Not found', fix: 'Install npm: https://nodejs.org' };
|
|
63
|
+
}
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
name: 'Ollama running',
|
|
68
|
+
check: async () => {
|
|
69
|
+
try {
|
|
70
|
+
const controller = new AbortController();
|
|
71
|
+
const timeout = setTimeout(() => controller.abort(), 3000);
|
|
72
|
+
const r = await fetch('http://localhost:11434/api/tags', { signal: controller.signal });
|
|
73
|
+
clearTimeout(timeout);
|
|
74
|
+
const data = await r.json();
|
|
75
|
+
return { ok: true, detail: `${data.models?.length || 0} models available` };
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
return { ok: false, detail: 'Not running', fix: 'Install Ollama: https://ollama.ai' };
|
|
79
|
+
}
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
{
|
|
83
|
+
name: 'agent.yaml exists',
|
|
84
|
+
check: () => {
|
|
85
|
+
const found = (0, fs_1.existsSync)('./agent.yaml');
|
|
86
|
+
return { ok: found, detail: found ? 'Found' : 'Not found', fix: found ? undefined : 'Run `opc init` to create a project' };
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: 'SOUL.md exists',
|
|
91
|
+
check: () => {
|
|
92
|
+
const found = (0, fs_1.existsSync)('./SOUL.md');
|
|
93
|
+
return { ok: found, detail: found ? 'Found' : 'Not found', fix: found ? undefined : 'Run `opc init` to generate one' };
|
|
94
|
+
},
|
|
95
|
+
},
|
|
96
|
+
{
|
|
97
|
+
name: 'TypeScript installed',
|
|
98
|
+
check: () => {
|
|
99
|
+
try {
|
|
100
|
+
(0, child_process_1.execSync)('npx tsc --version', { encoding: 'utf-8', stdio: ['pipe', 'pipe', 'pipe'] });
|
|
101
|
+
return { ok: true, detail: 'Available' };
|
|
102
|
+
}
|
|
103
|
+
catch {
|
|
104
|
+
return { ok: false, detail: 'Not found', fix: 'npm install -D typescript' };
|
|
105
|
+
}
|
|
106
|
+
},
|
|
107
|
+
},
|
|
108
|
+
{
|
|
109
|
+
name: 'Disk space',
|
|
110
|
+
check: () => {
|
|
111
|
+
return { ok: true, detail: 'Check passed' };
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
name: 'DeepBrain package',
|
|
116
|
+
check: () => {
|
|
117
|
+
try {
|
|
118
|
+
require.resolve('deepbrain');
|
|
119
|
+
return { ok: true, detail: 'Installed' };
|
|
120
|
+
}
|
|
121
|
+
catch {
|
|
122
|
+
return { ok: false, detail: 'Not installed', fix: 'npm install deepbrain' };
|
|
123
|
+
}
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
name: 'Port 3000 available',
|
|
128
|
+
check: () => {
|
|
129
|
+
return new Promise((resolve) => {
|
|
130
|
+
const server = net.createServer();
|
|
131
|
+
server.once('error', () => {
|
|
132
|
+
resolve({ ok: false, detail: 'In use', fix: 'Free port 3000 or configure a different port' });
|
|
133
|
+
});
|
|
134
|
+
server.once('listening', () => {
|
|
135
|
+
server.close(() => {
|
|
136
|
+
resolve({ ok: true, detail: 'Available' });
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
server.listen(3000);
|
|
140
|
+
});
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
];
|
|
144
|
+
}
|
|
145
|
+
async function runDoctor() {
|
|
146
|
+
const checks = getDoctorChecks();
|
|
147
|
+
const color = {
|
|
148
|
+
green: (s) => `\x1b[32m${s}\x1b[0m`,
|
|
149
|
+
red: (s) => `\x1b[31m${s}\x1b[0m`,
|
|
150
|
+
dim: (s) => `\x1b[2m${s}\x1b[0m`,
|
|
151
|
+
bold: (s) => `\x1b[1m${s}\x1b[0m`,
|
|
152
|
+
};
|
|
153
|
+
console.log(`\nš ${color.bold('OPC Agent Doctor')}\n`);
|
|
154
|
+
let passed = 0;
|
|
155
|
+
const total = checks.length;
|
|
156
|
+
for (const check of checks) {
|
|
157
|
+
try {
|
|
158
|
+
const result = await check.check();
|
|
159
|
+
const icon = result.ok ? color.green('ā
') : color.red('ā');
|
|
160
|
+
const name = check.name.padEnd(22);
|
|
161
|
+
console.log(` ${icon} ${name} ${result.detail}`);
|
|
162
|
+
if (!result.ok && result.fix) {
|
|
163
|
+
console.log(` ā ${result.fix}`);
|
|
164
|
+
}
|
|
165
|
+
if (result.ok)
|
|
166
|
+
passed++;
|
|
167
|
+
}
|
|
168
|
+
catch (err) {
|
|
169
|
+
const name = check.name.padEnd(22);
|
|
170
|
+
console.log(` ${color.red('ā')} ${name} Error: ${err instanceof Error ? err.message : String(err)}`);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
console.log(`\n Result: ${passed}/${total} checks passed`);
|
|
174
|
+
if (passed < total) {
|
|
175
|
+
console.log(`\n Fix the issues above to get the best experience.`);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
console.log(`\n ${color.green('All checks passed!')} You're good to go.`);
|
|
179
|
+
}
|
|
180
|
+
console.log();
|
|
181
|
+
return { passed, total };
|
|
182
|
+
}
|
|
183
|
+
//# sourceMappingURL=doctor.js.map
|