opc-agent 0.3.0 → 0.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (53) hide show
  1. package/README.md +20 -0
  2. package/dist/channels/voice.d.ts +43 -0
  3. package/dist/channels/voice.js +67 -0
  4. package/dist/channels/webhook.d.ts +40 -0
  5. package/dist/channels/webhook.js +193 -0
  6. package/dist/cli.js +143 -13
  7. package/dist/core/a2a.d.ts +46 -0
  8. package/dist/core/a2a.js +99 -0
  9. package/dist/core/hitl.d.ts +41 -0
  10. package/dist/core/hitl.js +100 -0
  11. package/dist/core/performance.d.ts +50 -0
  12. package/dist/core/performance.js +148 -0
  13. package/dist/core/versioning.d.ts +29 -0
  14. package/dist/core/versioning.js +114 -0
  15. package/dist/core/workflow.d.ts +59 -0
  16. package/dist/core/workflow.js +174 -0
  17. package/dist/deploy/openclaw.d.ts +14 -0
  18. package/dist/deploy/openclaw.js +208 -0
  19. package/dist/index.d.ts +13 -0
  20. package/dist/index.js +18 -1
  21. package/dist/schema/oad.d.ts +352 -15
  22. package/dist/schema/oad.js +41 -2
  23. package/dist/templates/executive-assistant.d.ts +20 -0
  24. package/dist/templates/executive-assistant.js +70 -0
  25. package/dist/templates/financial-advisor.d.ts +15 -0
  26. package/dist/templates/financial-advisor.js +60 -0
  27. package/dist/templates/legal-assistant.d.ts +15 -0
  28. package/dist/templates/legal-assistant.js +70 -0
  29. package/examples/customer-service-demo/README.md +90 -0
  30. package/examples/customer-service-demo/oad.yaml +107 -0
  31. package/package.json +46 -46
  32. package/src/channels/voice.ts +106 -0
  33. package/src/channels/webhook.ts +199 -0
  34. package/src/cli.ts +524 -384
  35. package/src/core/a2a.ts +143 -0
  36. package/src/core/hitl.ts +138 -0
  37. package/src/core/performance.ts +187 -0
  38. package/src/core/versioning.ts +106 -0
  39. package/src/core/workflow.ts +235 -0
  40. package/src/deploy/openclaw.ts +200 -0
  41. package/src/index.ts +15 -0
  42. package/src/schema/oad.ts +45 -1
  43. package/src/templates/executive-assistant.ts +71 -0
  44. package/src/templates/financial-advisor.ts +60 -0
  45. package/src/templates/legal-assistant.ts +71 -0
  46. package/tests/a2a.test.ts +66 -0
  47. package/tests/hitl.test.ts +71 -0
  48. package/tests/performance.test.ts +115 -0
  49. package/tests/templates.test.ts +77 -0
  50. package/tests/versioning.test.ts +75 -0
  51. package/tests/voice.test.ts +61 -0
  52. package/tests/webhook.test.ts +29 -0
  53. package/tests/workflow.test.ts +143 -0
@@ -0,0 +1,143 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Room } from './room';
3
+ import type { Message, IAgent } from './types';
4
+ import { Logger } from './logger';
5
+
6
+ // ── A2A Types ───────────────────────────────────────────────
7
+
8
+ export interface AgentCapability {
9
+ name: string;
10
+ description: string;
11
+ inputSchema?: Record<string, unknown>;
12
+ outputSchema?: Record<string, unknown>;
13
+ }
14
+
15
+ export interface AgentRegistration {
16
+ agentName: string;
17
+ capabilities: AgentCapability[];
18
+ endpoint?: string;
19
+ metadata?: Record<string, unknown>;
20
+ }
21
+
22
+ export interface A2ARequest {
23
+ id: string;
24
+ from: string;
25
+ to: string;
26
+ capability: string;
27
+ payload: string;
28
+ timestamp: number;
29
+ timeout?: number;
30
+ }
31
+
32
+ export interface A2AResponse {
33
+ requestId: string;
34
+ from: string;
35
+ status: 'success' | 'error' | 'timeout';
36
+ payload?: string;
37
+ error?: string;
38
+ timestamp: number;
39
+ }
40
+
41
+ // ── Agent Registry ──────────────────────────────────────────
42
+
43
+ export class AgentRegistry extends EventEmitter {
44
+ private registrations: Map<string, AgentRegistration> = new Map();
45
+ private agents: Map<string, IAgent> = new Map();
46
+ private room: Room;
47
+ private logger = new Logger('a2a');
48
+
49
+ constructor(room?: Room) {
50
+ super();
51
+ this.room = room ?? new Room('a2a-default');
52
+ }
53
+
54
+ register(agent: IAgent, capabilities: AgentCapability[]): void {
55
+ const reg: AgentRegistration = { agentName: agent.name, capabilities };
56
+ this.registrations.set(agent.name, reg);
57
+ this.agents.set(agent.name, agent);
58
+ this.room.addAgent(agent);
59
+ this.logger.info('Agent registered', { name: agent.name, capabilities: capabilities.map(c => c.name) });
60
+ this.emit('agent:registered', reg);
61
+ }
62
+
63
+ unregister(name: string): void {
64
+ this.registrations.delete(name);
65
+ this.agents.delete(name);
66
+ this.room.removeAgent(name);
67
+ this.emit('agent:unregistered', name);
68
+ }
69
+
70
+ discover(capability?: string): AgentRegistration[] {
71
+ const all = Array.from(this.registrations.values());
72
+ if (!capability) return all;
73
+ return all.filter(r => r.capabilities.some(c => c.name === capability));
74
+ }
75
+
76
+ getAgent(name: string): IAgent | undefined {
77
+ return this.agents.get(name);
78
+ }
79
+
80
+ async request(req: A2ARequest): Promise<A2AResponse> {
81
+ const agent = this.agents.get(req.to);
82
+ if (!agent) {
83
+ return {
84
+ requestId: req.id,
85
+ from: req.to,
86
+ status: 'error',
87
+ error: `Agent "${req.to}" not found`,
88
+ timestamp: Date.now(),
89
+ };
90
+ }
91
+
92
+ const message: Message = {
93
+ id: req.id,
94
+ role: 'user',
95
+ content: req.payload,
96
+ timestamp: req.timestamp,
97
+ metadata: { a2a: true, from: req.from, capability: req.capability },
98
+ };
99
+
100
+ this.emit('request', req);
101
+
102
+ try {
103
+ const timeoutMs = req.timeout ?? 30000;
104
+ const response = await Promise.race([
105
+ agent.handleMessage(message),
106
+ new Promise<never>((_, reject) =>
107
+ setTimeout(() => reject(new Error('A2A request timeout')), timeoutMs),
108
+ ),
109
+ ]);
110
+
111
+ const res: A2AResponse = {
112
+ requestId: req.id,
113
+ from: req.to,
114
+ status: 'success',
115
+ payload: response.content,
116
+ timestamp: Date.now(),
117
+ };
118
+ this.emit('response', res);
119
+ return res;
120
+ } catch (err) {
121
+ const res: A2AResponse = {
122
+ requestId: req.id,
123
+ from: req.to,
124
+ status: (err as Error).message.includes('timeout') ? 'timeout' : 'error',
125
+ error: (err as Error).message,
126
+ timestamp: Date.now(),
127
+ };
128
+ this.emit('response', res);
129
+ return res;
130
+ }
131
+ }
132
+
133
+ async call(from: string, to: string, capability: string, payload: string): Promise<A2AResponse> {
134
+ return this.request({
135
+ id: `a2a_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
136
+ from,
137
+ to,
138
+ capability,
139
+ payload,
140
+ timestamp: Date.now(),
141
+ });
142
+ }
143
+ }
@@ -0,0 +1,138 @@
1
+ import { EventEmitter } from 'events';
2
+ import { Logger } from './logger';
3
+
4
+ // ── HITL Types ──────────────────────────────────────────────
5
+
6
+ export interface ApprovalRequest {
7
+ id: string;
8
+ action: string;
9
+ description: string;
10
+ context?: Record<string, unknown>;
11
+ timeoutMs: number;
12
+ defaultAction: 'approve' | 'deny';
13
+ createdAt: number;
14
+ }
15
+
16
+ export interface ApprovalResponse {
17
+ requestId: string;
18
+ decision: 'approve' | 'deny';
19
+ respondedBy?: string;
20
+ respondedAt: number;
21
+ timedOut: boolean;
22
+ }
23
+
24
+ export type ApprovalHandler = (request: ApprovalRequest) => Promise<ApprovalResponse>;
25
+
26
+ export interface HITLConfig {
27
+ /** Actions that always require approval */
28
+ requireApproval: string[];
29
+ /** Default timeout in ms */
30
+ defaultTimeoutMs: number;
31
+ /** Default action on timeout */
32
+ defaultAction: 'approve' | 'deny';
33
+ }
34
+
35
+ // ── HITL Manager ────────────────────────────────────────────
36
+
37
+ export class HITLManager extends EventEmitter {
38
+ private config: HITLConfig;
39
+ private handler: ApprovalHandler | null = null;
40
+ private pending: Map<string, { request: ApprovalRequest; resolve: (r: ApprovalResponse) => void }> = new Map();
41
+ private logger = new Logger('hitl');
42
+
43
+ constructor(config?: Partial<HITLConfig>) {
44
+ super();
45
+ this.config = {
46
+ requireApproval: config?.requireApproval ?? [],
47
+ defaultTimeoutMs: config?.defaultTimeoutMs ?? 60000,
48
+ defaultAction: config?.defaultAction ?? 'deny',
49
+ };
50
+ }
51
+
52
+ setHandler(handler: ApprovalHandler): void {
53
+ this.handler = handler;
54
+ }
55
+
56
+ needsApproval(action: string): boolean {
57
+ if (this.config.requireApproval.includes('*')) return true;
58
+ return this.config.requireApproval.includes(action);
59
+ }
60
+
61
+ async requestApproval(action: string, description: string, context?: Record<string, unknown>): Promise<ApprovalResponse> {
62
+ const request: ApprovalRequest = {
63
+ id: `hitl_${Date.now()}_${Math.random().toString(36).slice(2, 8)}`,
64
+ action,
65
+ description,
66
+ context,
67
+ timeoutMs: this.config.defaultTimeoutMs,
68
+ defaultAction: this.config.defaultAction,
69
+ createdAt: Date.now(),
70
+ };
71
+
72
+ this.emit('approval:requested', request);
73
+ this.logger.info('Approval requested', { id: request.id, action });
74
+
75
+ if (this.handler) {
76
+ try {
77
+ const response = await Promise.race([
78
+ this.handler(request),
79
+ this.createTimeout(request),
80
+ ]);
81
+ this.emit('approval:responded', response);
82
+ return response;
83
+ } catch {
84
+ return this.timeoutResponse(request);
85
+ }
86
+ }
87
+
88
+ // No handler: wait for manual response via respond()
89
+ return new Promise<ApprovalResponse>((resolve) => {
90
+ this.pending.set(request.id, { request, resolve });
91
+
92
+ setTimeout(() => {
93
+ if (this.pending.has(request.id)) {
94
+ this.pending.delete(request.id);
95
+ const response = this.timeoutResponse(request);
96
+ this.emit('approval:timeout', response);
97
+ resolve(response);
98
+ }
99
+ }, request.timeoutMs);
100
+ });
101
+ }
102
+
103
+ respond(requestId: string, decision: 'approve' | 'deny', respondedBy?: string): boolean {
104
+ const entry = this.pending.get(requestId);
105
+ if (!entry) return false;
106
+
107
+ this.pending.delete(requestId);
108
+ const response: ApprovalResponse = {
109
+ requestId,
110
+ decision,
111
+ respondedBy,
112
+ respondedAt: Date.now(),
113
+ timedOut: false,
114
+ };
115
+ entry.resolve(response);
116
+ this.emit('approval:responded', response);
117
+ return true;
118
+ }
119
+
120
+ getPending(): ApprovalRequest[] {
121
+ return Array.from(this.pending.values()).map(e => e.request);
122
+ }
123
+
124
+ private createTimeout(request: ApprovalRequest): Promise<ApprovalResponse> {
125
+ return new Promise((resolve) => {
126
+ setTimeout(() => resolve(this.timeoutResponse(request)), request.timeoutMs);
127
+ });
128
+ }
129
+
130
+ private timeoutResponse(request: ApprovalRequest): ApprovalResponse {
131
+ return {
132
+ requestId: request.id,
133
+ decision: request.defaultAction,
134
+ respondedAt: Date.now(),
135
+ timedOut: true,
136
+ };
137
+ }
138
+ }
@@ -0,0 +1,187 @@
1
+ import { Logger } from '../core/logger';
2
+
3
+ // ── Connection Pool ─────────────────────────────────────────
4
+
5
+ export interface PooledConnection {
6
+ id: string;
7
+ provider: string;
8
+ createdAt: number;
9
+ lastUsedAt: number;
10
+ inUse: boolean;
11
+ }
12
+
13
+ export class ConnectionPool {
14
+ private pool: Map<string, PooledConnection[]> = new Map();
15
+ private maxPerProvider: number;
16
+ private ttlMs: number;
17
+ private logger = new Logger('connection-pool');
18
+
19
+ constructor(maxPerProvider = 5, ttlMs = 300000) {
20
+ this.maxPerProvider = maxPerProvider;
21
+ this.ttlMs = ttlMs;
22
+ }
23
+
24
+ acquire(provider: string): PooledConnection {
25
+ const connections = this.pool.get(provider) ?? [];
26
+
27
+ // Cleanup expired
28
+ const now = Date.now();
29
+ const active = connections.filter(c => now - c.createdAt < this.ttlMs);
30
+
31
+ // Find available
32
+ const available = active.find(c => !c.inUse);
33
+ if (available) {
34
+ available.inUse = true;
35
+ available.lastUsedAt = now;
36
+ return available;
37
+ }
38
+
39
+ // Create new if under limit
40
+ if (active.length < this.maxPerProvider) {
41
+ const conn: PooledConnection = {
42
+ id: `conn_${now}_${Math.random().toString(36).slice(2, 8)}`,
43
+ provider,
44
+ createdAt: now,
45
+ lastUsedAt: now,
46
+ inUse: true,
47
+ };
48
+ active.push(conn);
49
+ this.pool.set(provider, active);
50
+ return conn;
51
+ }
52
+
53
+ // Wait for one (return oldest used for now)
54
+ const oldest = active.sort((a, b) => a.lastUsedAt - b.lastUsedAt)[0];
55
+ oldest.inUse = true;
56
+ oldest.lastUsedAt = now;
57
+ return oldest;
58
+ }
59
+
60
+ release(id: string): void {
61
+ for (const connections of this.pool.values()) {
62
+ const conn = connections.find(c => c.id === id);
63
+ if (conn) {
64
+ conn.inUse = false;
65
+ return;
66
+ }
67
+ }
68
+ }
69
+
70
+ getStats(): Record<string, { total: number; inUse: number }> {
71
+ const stats: Record<string, { total: number; inUse: number }> = {};
72
+ for (const [provider, connections] of this.pool) {
73
+ stats[provider] = {
74
+ total: connections.length,
75
+ inUse: connections.filter(c => c.inUse).length,
76
+ };
77
+ }
78
+ return stats;
79
+ }
80
+
81
+ drain(): void {
82
+ this.pool.clear();
83
+ }
84
+ }
85
+
86
+ // ── Request Batcher ─────────────────────────────────────────
87
+
88
+ export interface BatchRequest<T> {
89
+ payload: T;
90
+ resolve: (result: unknown) => void;
91
+ reject: (error: Error) => void;
92
+ }
93
+
94
+ export class RequestBatcher<T> {
95
+ private queue: BatchRequest<T>[] = [];
96
+ private timer: ReturnType<typeof setTimeout> | null = null;
97
+ private maxBatchSize: number;
98
+ private delayMs: number;
99
+ private processor: (batch: T[]) => Promise<unknown[]>;
100
+ private logger = new Logger('batcher');
101
+
102
+ constructor(
103
+ processor: (batch: T[]) => Promise<unknown[]>,
104
+ maxBatchSize = 10,
105
+ delayMs = 50,
106
+ ) {
107
+ this.processor = processor;
108
+ this.maxBatchSize = maxBatchSize;
109
+ this.delayMs = delayMs;
110
+ }
111
+
112
+ add(payload: T): Promise<unknown> {
113
+ return new Promise((resolve, reject) => {
114
+ this.queue.push({ payload, resolve, reject });
115
+
116
+ if (this.queue.length >= this.maxBatchSize) {
117
+ this.flush();
118
+ } else if (!this.timer) {
119
+ this.timer = setTimeout(() => this.flush(), this.delayMs);
120
+ }
121
+ });
122
+ }
123
+
124
+ async flush(): Promise<void> {
125
+ if (this.timer) {
126
+ clearTimeout(this.timer);
127
+ this.timer = null;
128
+ }
129
+
130
+ if (this.queue.length === 0) return;
131
+
132
+ const batch = this.queue.splice(0, this.maxBatchSize);
133
+ try {
134
+ const results = await this.processor(batch.map(b => b.payload));
135
+ batch.forEach((req, i) => req.resolve(results[i]));
136
+ } catch (err) {
137
+ batch.forEach(req => req.reject(err as Error));
138
+ }
139
+ }
140
+
141
+ get pending(): number {
142
+ return this.queue.length;
143
+ }
144
+ }
145
+
146
+ // ── Lazy Loader ─────────────────────────────────────────────
147
+
148
+ export class LazyLoader<T> {
149
+ private cache: Map<string, T> = new Map();
150
+ private loaders: Map<string, () => Promise<T>> = new Map();
151
+
152
+ register(name: string, loader: () => Promise<T>): void {
153
+ this.loaders.set(name, loader);
154
+ }
155
+
156
+ async get(name: string): Promise<T> {
157
+ const cached = this.cache.get(name);
158
+ if (cached) return cached;
159
+
160
+ const loader = this.loaders.get(name);
161
+ if (!loader) throw new Error(`No loader registered for "${name}"`);
162
+
163
+ const instance = await loader();
164
+ this.cache.set(name, instance);
165
+ return instance;
166
+ }
167
+
168
+ isLoaded(name: string): boolean {
169
+ return this.cache.has(name);
170
+ }
171
+
172
+ evict(name: string): void {
173
+ this.cache.delete(name);
174
+ }
175
+
176
+ clear(): void {
177
+ this.cache.clear();
178
+ }
179
+
180
+ get loadedCount(): number {
181
+ return this.cache.size;
182
+ }
183
+
184
+ get registeredCount(): number {
185
+ return this.loaders.size;
186
+ }
187
+ }
@@ -0,0 +1,106 @@
1
+ import * as fs from 'fs';
2
+ import * as path from 'path';
3
+ import { Logger } from './logger';
4
+
5
+ // ── Versioning Types ────────────────────────────────────────
6
+
7
+ export interface VersionEntry {
8
+ version: string;
9
+ timestamp: number;
10
+ description?: string;
11
+ oadSnapshot: string; // serialized OAD YAML
12
+ }
13
+
14
+ export interface Migration {
15
+ fromVersion: string;
16
+ toVersion: string;
17
+ migrate: (oad: Record<string, unknown>) => Record<string, unknown>;
18
+ }
19
+
20
+ // ── Version Manager ─────────────────────────────────────────
21
+
22
+ export class VersionManager {
23
+ private versions: VersionEntry[] = [];
24
+ private migrations: Migration[] = [];
25
+ private storePath: string;
26
+ private logger = new Logger('versioning');
27
+
28
+ constructor(storePath?: string) {
29
+ this.storePath = storePath ?? '.opc-versions.json';
30
+ this.load();
31
+ }
32
+
33
+ private load(): void {
34
+ try {
35
+ if (fs.existsSync(this.storePath)) {
36
+ const data = JSON.parse(fs.readFileSync(this.storePath, 'utf-8'));
37
+ this.versions = data.versions ?? [];
38
+ }
39
+ } catch {
40
+ this.versions = [];
41
+ }
42
+ }
43
+
44
+ private save(): void {
45
+ fs.writeFileSync(this.storePath, JSON.stringify({ versions: this.versions }, null, 2));
46
+ }
47
+
48
+ snapshot(version: string, oadYaml: string, description?: string): void {
49
+ this.versions.push({
50
+ version,
51
+ timestamp: Date.now(),
52
+ description,
53
+ oadSnapshot: oadYaml,
54
+ });
55
+ this.save();
56
+ this.logger.info('Version snapshot saved', { version });
57
+ }
58
+
59
+ list(): VersionEntry[] {
60
+ return [...this.versions];
61
+ }
62
+
63
+ get(version: string): VersionEntry | undefined {
64
+ return this.versions.find(v => v.version === version);
65
+ }
66
+
67
+ getCurrent(): VersionEntry | undefined {
68
+ return this.versions[this.versions.length - 1];
69
+ }
70
+
71
+ rollback(version: string): string | null {
72
+ const entry = this.get(version);
73
+ if (!entry) {
74
+ this.logger.warn('Version not found', { version });
75
+ return null;
76
+ }
77
+ this.logger.info('Rolling back to version', { version });
78
+ return entry.oadSnapshot;
79
+ }
80
+
81
+ registerMigration(migration: Migration): void {
82
+ this.migrations.push(migration);
83
+ }
84
+
85
+ migrate(oad: Record<string, unknown>, fromVersion: string, toVersion: string): Record<string, unknown> {
86
+ let current = fromVersion;
87
+ let result = { ...oad };
88
+
89
+ while (current !== toVersion) {
90
+ const migration = this.migrations.find(m => m.fromVersion === current);
91
+ if (!migration) {
92
+ throw new Error(`No migration path from ${current} to ${toVersion}`);
93
+ }
94
+ result = migration.migrate(result);
95
+ current = migration.toVersion;
96
+ this.logger.info('Migration applied', { from: migration.fromVersion, to: migration.toVersion });
97
+ }
98
+
99
+ return result;
100
+ }
101
+
102
+ clear(): void {
103
+ this.versions = [];
104
+ this.save();
105
+ }
106
+ }