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,28 @@
|
|
|
1
|
+
export interface SubAgentConfig {
|
|
2
|
+
name: string;
|
|
3
|
+
task: string;
|
|
4
|
+
systemPrompt?: string;
|
|
5
|
+
provider?: string;
|
|
6
|
+
model?: string;
|
|
7
|
+
timeout?: number;
|
|
8
|
+
isolated?: boolean;
|
|
9
|
+
}
|
|
10
|
+
export interface SubAgentResult {
|
|
11
|
+
id: string;
|
|
12
|
+
name: string;
|
|
13
|
+
status: 'completed' | 'failed' | 'timeout';
|
|
14
|
+
result: string;
|
|
15
|
+
duration: number;
|
|
16
|
+
}
|
|
17
|
+
export declare class SubAgentManager {
|
|
18
|
+
private agents;
|
|
19
|
+
spawn(config: SubAgentConfig, parentProvider?: any): Promise<SubAgentResult>;
|
|
20
|
+
spawnParallel(configs: SubAgentConfig[], parentProvider?: any): Promise<SubAgentResult[]>;
|
|
21
|
+
list(): Array<{
|
|
22
|
+
id: string;
|
|
23
|
+
name: string;
|
|
24
|
+
status: string;
|
|
25
|
+
}>;
|
|
26
|
+
kill(id: string): boolean;
|
|
27
|
+
}
|
|
28
|
+
//# sourceMappingURL=subagent.d.ts.map
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.SubAgentManager = void 0;
|
|
4
|
+
const agent_1 = require("./agent");
|
|
5
|
+
const memory_1 = require("../memory");
|
|
6
|
+
class SubAgentManager {
|
|
7
|
+
agents = new Map();
|
|
8
|
+
async spawn(config, parentProvider) {
|
|
9
|
+
const id = `sub_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`;
|
|
10
|
+
const timeout = config.timeout ?? 300000;
|
|
11
|
+
const isolated = config.isolated !== false;
|
|
12
|
+
const agent = new agent_1.BaseAgent({
|
|
13
|
+
name: config.name,
|
|
14
|
+
systemPrompt: config.systemPrompt ?? 'You are a helpful sub-agent.',
|
|
15
|
+
provider: config.provider ?? 'openai',
|
|
16
|
+
model: config.model,
|
|
17
|
+
memory: isolated ? new memory_1.InMemoryStore() : undefined,
|
|
18
|
+
});
|
|
19
|
+
this.agents.set(id, { agent, status: 'running', name: config.name });
|
|
20
|
+
const message = {
|
|
21
|
+
id: `msg_${Date.now()}`,
|
|
22
|
+
role: 'user',
|
|
23
|
+
content: config.task,
|
|
24
|
+
timestamp: Date.now(),
|
|
25
|
+
metadata: { subAgentId: id },
|
|
26
|
+
};
|
|
27
|
+
const start = Date.now();
|
|
28
|
+
try {
|
|
29
|
+
const result = await Promise.race([
|
|
30
|
+
agent.handleMessage(message),
|
|
31
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error('SubAgent timeout')), timeout)),
|
|
32
|
+
]);
|
|
33
|
+
const duration = Date.now() - start;
|
|
34
|
+
this.agents.set(id, { agent, status: 'completed', name: config.name });
|
|
35
|
+
return { id, name: config.name, status: 'completed', result: result.content, duration };
|
|
36
|
+
}
|
|
37
|
+
catch (err) {
|
|
38
|
+
const duration = Date.now() - start;
|
|
39
|
+
const isTimeout = err.message.includes('timeout');
|
|
40
|
+
const status = isTimeout ? 'timeout' : 'failed';
|
|
41
|
+
this.agents.set(id, { agent, status, name: config.name });
|
|
42
|
+
return { id, name: config.name, status, result: err.message, duration };
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async spawnParallel(configs, parentProvider) {
|
|
46
|
+
return Promise.all(configs.map((c) => this.spawn(c, parentProvider)));
|
|
47
|
+
}
|
|
48
|
+
list() {
|
|
49
|
+
return Array.from(this.agents.entries()).map(([id, entry]) => ({
|
|
50
|
+
id,
|
|
51
|
+
name: entry.name,
|
|
52
|
+
status: entry.status,
|
|
53
|
+
}));
|
|
54
|
+
}
|
|
55
|
+
kill(id) {
|
|
56
|
+
const entry = this.agents.get(id);
|
|
57
|
+
if (!entry)
|
|
58
|
+
return false;
|
|
59
|
+
entry.status = 'killed';
|
|
60
|
+
this.agents.set(id, entry);
|
|
61
|
+
return true;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
exports.SubAgentManager = SubAgentManager;
|
|
65
|
+
//# sourceMappingURL=subagent.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/daemon.d.ts
ADDED
package/dist/daemon.js
ADDED
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
15
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
16
|
+
}) : function(o, v) {
|
|
17
|
+
o["default"] = v;
|
|
18
|
+
});
|
|
19
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
20
|
+
var ownKeys = function(o) {
|
|
21
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
22
|
+
var ar = [];
|
|
23
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
24
|
+
return ar;
|
|
25
|
+
};
|
|
26
|
+
return ownKeys(o);
|
|
27
|
+
};
|
|
28
|
+
return function (mod) {
|
|
29
|
+
if (mod && mod.__esModule) return mod;
|
|
30
|
+
var result = {};
|
|
31
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
32
|
+
__setModuleDefault(result, mod);
|
|
33
|
+
return result;
|
|
34
|
+
};
|
|
35
|
+
})();
|
|
36
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
37
|
+
/**
|
|
38
|
+
* Daemon entry point — spawned by `opc start` as a detached background process.
|
|
39
|
+
* Loads agent.yaml, creates runtime, starts all channels, writes heartbeat.
|
|
40
|
+
*/
|
|
41
|
+
const fs = __importStar(require("fs"));
|
|
42
|
+
const path = __importStar(require("path"));
|
|
43
|
+
const runtime_1 = require("./core/runtime");
|
|
44
|
+
const OPC_DIR = path.resolve('.opc');
|
|
45
|
+
const HEARTBEAT_FILE = path.join(OPC_DIR, 'heartbeat');
|
|
46
|
+
const LOG_FILE = path.join(OPC_DIR, 'agent.log');
|
|
47
|
+
const PID_FILE = path.join(OPC_DIR, 'agent.pid');
|
|
48
|
+
const HEARTBEAT_INTERVAL = 30_000;
|
|
49
|
+
function ensureDir(dir) {
|
|
50
|
+
if (!fs.existsSync(dir))
|
|
51
|
+
fs.mkdirSync(dir, { recursive: true });
|
|
52
|
+
}
|
|
53
|
+
function log(msg) {
|
|
54
|
+
const line = `[${new Date().toISOString()}] ${msg}\n`;
|
|
55
|
+
try {
|
|
56
|
+
fs.appendFileSync(LOG_FILE, line);
|
|
57
|
+
}
|
|
58
|
+
catch { /* ignore */ }
|
|
59
|
+
}
|
|
60
|
+
async function main() {
|
|
61
|
+
ensureDir(OPC_DIR);
|
|
62
|
+
// Redirect stdout/stderr to log file
|
|
63
|
+
const logStream = fs.createWriteStream(LOG_FILE, { flags: 'a' });
|
|
64
|
+
process.stdout.write = logStream.write.bind(logStream);
|
|
65
|
+
process.stderr.write = logStream.write.bind(logStream);
|
|
66
|
+
// Write PID
|
|
67
|
+
fs.writeFileSync(PID_FILE, String(process.pid));
|
|
68
|
+
log(`Daemon started, PID=${process.pid}`);
|
|
69
|
+
// Write start time for uptime calculation
|
|
70
|
+
fs.writeFileSync(path.join(OPC_DIR, 'started'), String(Date.now()));
|
|
71
|
+
// Heartbeat
|
|
72
|
+
const heartbeatTimer = setInterval(() => {
|
|
73
|
+
try {
|
|
74
|
+
fs.writeFileSync(HEARTBEAT_FILE, String(Date.now()));
|
|
75
|
+
}
|
|
76
|
+
catch { /* ignore */ }
|
|
77
|
+
}, HEARTBEAT_INTERVAL);
|
|
78
|
+
fs.writeFileSync(HEARTBEAT_FILE, String(Date.now()));
|
|
79
|
+
// Load .env
|
|
80
|
+
const envPath = path.resolve('.env');
|
|
81
|
+
if (fs.existsSync(envPath)) {
|
|
82
|
+
try {
|
|
83
|
+
const content = fs.readFileSync(envPath, 'utf-8');
|
|
84
|
+
for (const line of content.split('\n')) {
|
|
85
|
+
const trimmed = line.trim();
|
|
86
|
+
if (!trimmed || trimmed.startsWith('#'))
|
|
87
|
+
continue;
|
|
88
|
+
const eqIdx = trimmed.indexOf('=');
|
|
89
|
+
if (eqIdx === -1)
|
|
90
|
+
continue;
|
|
91
|
+
const key = trimmed.slice(0, eqIdx).trim();
|
|
92
|
+
const value = trimmed.slice(eqIdx + 1).trim();
|
|
93
|
+
if (!process.env[key])
|
|
94
|
+
process.env[key] = value;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
catch { /* ignore */ }
|
|
98
|
+
}
|
|
99
|
+
// Determine config file
|
|
100
|
+
const configFile = fs.existsSync('agent.yaml') ? 'agent.yaml' : 'oad.yaml';
|
|
101
|
+
const runtime = new runtime_1.AgentRuntime();
|
|
102
|
+
await runtime.loadConfig(configFile);
|
|
103
|
+
await runtime.initialize();
|
|
104
|
+
await runtime.start();
|
|
105
|
+
log(`Agent running (config=${configFile})`);
|
|
106
|
+
// Graceful shutdown
|
|
107
|
+
const shutdown = async (signal) => {
|
|
108
|
+
log(`Received ${signal}, shutting down...`);
|
|
109
|
+
clearInterval(heartbeatTimer);
|
|
110
|
+
await runtime.stop();
|
|
111
|
+
try {
|
|
112
|
+
fs.unlinkSync(PID_FILE);
|
|
113
|
+
}
|
|
114
|
+
catch { /* ignore */ }
|
|
115
|
+
log('Daemon stopped');
|
|
116
|
+
process.exit(0);
|
|
117
|
+
};
|
|
118
|
+
process.on('SIGTERM', () => shutdown('SIGTERM'));
|
|
119
|
+
process.on('SIGINT', () => shutdown('SIGINT'));
|
|
120
|
+
// On Windows, handle the message-based kill
|
|
121
|
+
process.on('message', (msg) => {
|
|
122
|
+
if (msg === 'shutdown')
|
|
123
|
+
shutdown('message:shutdown');
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
main().catch((err) => {
|
|
127
|
+
log(`Fatal error: ${err instanceof Error ? err.message : String(err)}`);
|
|
128
|
+
try {
|
|
129
|
+
fs.unlinkSync(PID_FILE);
|
|
130
|
+
}
|
|
131
|
+
catch { /* ignore */ }
|
|
132
|
+
process.exit(1);
|
|
133
|
+
});
|
|
134
|
+
//# sourceMappingURL=daemon.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
|