opc-agent 1.1.3 → 1.2.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 +6 -0
- package/CONTRIBUTING.md +75 -75
- package/README.md +429 -429
- package/README.zh-CN.md +415 -415
- package/dist/channels/web.js +256 -256
- package/dist/core/streaming.d.ts +56 -0
- package/dist/core/streaming.js +160 -0
- package/dist/deploy/hermes.js +22 -22
- package/dist/deploy/openclaw.js +31 -31
- package/dist/index.d.ts +4 -0
- package/dist/index.js +7 -1
- package/dist/providers/index.d.ts +1 -1
- package/dist/providers/index.js +13 -148
- package/dist/schema/oad.d.ts +3 -3
- package/dist/templates/code-reviewer.js +5 -5
- package/dist/templates/customer-service.js +2 -2
- package/dist/templates/data-analyst.js +5 -5
- package/dist/templates/knowledge-base.js +2 -2
- package/dist/templates/sales-assistant.js +4 -4
- package/dist/templates/teacher.js +6 -6
- package/dist/tools/gateway.d.ts +28 -0
- package/dist/tools/gateway.js +177 -0
- package/docs/.vitepress/config.ts +103 -103
- package/docs/api/cli.md +48 -48
- package/docs/api/oad-schema.md +64 -64
- package/docs/api/sdk.md +80 -80
- package/docs/guide/concepts.md +51 -51
- package/docs/guide/configuration.md +79 -79
- package/docs/guide/deployment.md +42 -42
- package/docs/guide/getting-started.md +44 -44
- package/docs/guide/templates.md +28 -28
- package/docs/guide/testing.md +84 -84
- package/docs/index.md +27 -27
- package/docs/zh/api/cli.md +54 -54
- package/docs/zh/api/oad-schema.md +87 -87
- package/docs/zh/api/sdk.md +102 -102
- package/docs/zh/guide/concepts.md +104 -104
- package/docs/zh/guide/configuration.md +135 -135
- package/docs/zh/guide/deployment.md +81 -81
- package/docs/zh/guide/getting-started.md +82 -82
- package/docs/zh/guide/templates.md +84 -84
- package/docs/zh/guide/testing.md +88 -88
- package/docs/zh/index.md +27 -27
- package/examples/customer-service-demo/README.md +90 -90
- package/examples/customer-service-demo/oad.yaml +107 -107
- package/package.json +1 -1
- package/src/analytics/index.ts +66 -66
- package/src/channels/discord.ts +192 -192
- package/src/channels/email.ts +177 -177
- package/src/channels/feishu.ts +236 -236
- package/src/channels/index.ts +15 -15
- package/src/channels/slack.ts +160 -160
- package/src/channels/telegram.ts +90 -90
- package/src/channels/voice.ts +106 -106
- package/src/channels/web.ts +17 -17
- package/src/channels/webhook.ts +199 -199
- package/src/channels/websocket.ts +87 -87
- package/src/channels/wechat.ts +149 -149
- package/src/core/a2a.ts +143 -143
- package/src/core/agent.ts +152 -152
- package/src/core/analytics-engine.ts +186 -186
- package/src/core/auth.ts +57 -57
- package/src/core/cache.ts +141 -141
- package/src/core/compose.ts +77 -77
- package/src/core/config.ts +14 -14
- package/src/core/errors.ts +148 -148
- package/src/core/hitl.ts +138 -138
- package/src/core/knowledge.ts +49 -4
- package/src/core/logger.ts +57 -57
- package/src/core/orchestrator.ts +215 -215
- package/src/core/performance.ts +187 -187
- package/src/core/rate-limiter.ts +128 -128
- package/src/core/room.ts +109 -109
- package/src/core/runtime.ts +152 -152
- package/src/core/sandbox.ts +101 -101
- package/src/core/security.ts +171 -171
- package/src/core/streaming.ts +195 -0
- package/src/core/types.ts +68 -68
- package/src/core/versioning.ts +106 -106
- package/src/core/watch.ts +178 -178
- package/src/core/workflow.ts +235 -235
- package/src/deploy/hermes.ts +156 -156
- package/src/deploy/openclaw.ts +200 -200
- package/src/dtv/data.ts +29 -29
- package/src/dtv/trust.ts +43 -43
- package/src/dtv/value.ts +47 -47
- package/src/i18n/index.ts +216 -216
- package/src/index.ts +6 -0
- package/src/marketplace/index.ts +223 -223
- package/src/memory/deepbrain.ts +108 -108
- package/src/memory/index.ts +34 -34
- package/src/plugins/index.ts +208 -208
- package/src/providers/index.ts +12 -3
- package/src/schema/oad.ts +155 -155
- package/src/skills/base.ts +16 -16
- package/src/skills/document.ts +100 -100
- package/src/skills/http.ts +35 -35
- package/src/skills/index.ts +27 -27
- package/src/skills/scheduler.ts +80 -80
- package/src/skills/webhook-trigger.ts +59 -59
- package/src/templates/code-reviewer.ts +34 -34
- package/src/templates/customer-service.ts +80 -80
- package/src/templates/data-analyst.ts +70 -70
- package/src/templates/executive-assistant.ts +71 -71
- package/src/templates/financial-advisor.ts +60 -60
- package/src/templates/knowledge-base.ts +31 -31
- package/src/templates/legal-assistant.ts +71 -71
- package/src/templates/sales-assistant.ts +79 -79
- package/src/templates/teacher.ts +79 -79
- package/src/testing/index.ts +181 -181
- package/src/tools/calculator.ts +73 -73
- package/src/tools/datetime.ts +149 -149
- package/src/tools/gateway.ts +220 -0
- package/src/tools/json-transform.ts +187 -187
- package/src/tools/mcp.ts +76 -76
- package/src/tools/text-analysis.ts +116 -116
- package/templates/Dockerfile +15 -15
- package/templates/code-reviewer/README.md +27 -27
- package/templates/code-reviewer/oad.yaml +41 -41
- package/templates/customer-service/README.md +22 -22
- package/templates/customer-service/oad.yaml +36 -36
- package/templates/docker-compose.yml +21 -21
- package/templates/ecommerce-assistant/README.md +45 -0
- package/templates/ecommerce-assistant/oad.yaml +47 -0
- package/templates/knowledge-base/README.md +28 -28
- package/templates/knowledge-base/oad.yaml +38 -38
- package/templates/sales-assistant/README.md +26 -26
- package/templates/sales-assistant/oad.yaml +43 -43
- package/templates/tech-support/README.md +43 -0
- package/templates/tech-support/oad.yaml +45 -0
- package/tests/a2a.test.ts +66 -66
- package/tests/agent.test.ts +72 -72
- package/tests/analytics.test.ts +50 -50
- package/tests/channel.test.ts +39 -39
- package/tests/e2e.test.ts +134 -134
- package/tests/errors.test.ts +83 -83
- package/tests/gateway.test.ts +71 -0
- package/tests/hitl.test.ts +71 -71
- package/tests/i18n.test.ts +41 -41
- package/tests/mcp.test.ts +54 -54
- package/tests/oad.test.ts +68 -68
- package/tests/performance.test.ts +115 -115
- package/tests/plugin.test.ts +74 -74
- package/tests/room.test.ts +106 -106
- package/tests/runtime.test.ts +42 -42
- package/tests/sandbox.test.ts +46 -46
- package/tests/security.test.ts +60 -60
- package/tests/streaming.test.ts +109 -0
- package/tests/templates.test.ts +77 -77
- package/tests/v070.test.ts +76 -76
- package/tests/versioning.test.ts +75 -75
- package/tests/voice.test.ts +61 -61
- package/tests/webhook.test.ts +29 -29
- package/tests/workflow.test.ts +143 -143
- package/tsconfig.json +19 -19
- package/vitest.config.ts +9 -9
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
// ─── Types ───────────────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
export interface StreamChunk {
|
|
6
|
+
id: string;
|
|
7
|
+
type: 'text' | 'tool_call' | 'error' | 'done';
|
|
8
|
+
data: string;
|
|
9
|
+
timestamp: number;
|
|
10
|
+
metadata?: Record<string, unknown>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface StreamOptions {
|
|
14
|
+
/** High-water mark for backpressure (default 64 chunks). */
|
|
15
|
+
highWaterMark?: number;
|
|
16
|
+
/** Heartbeat interval in ms to keep connection alive (default 15000). */
|
|
17
|
+
heartbeatInterval?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
// ─── StreamableResponse ──────────────────────────────────────
|
|
21
|
+
|
|
22
|
+
export class StreamableResponse extends EventEmitter {
|
|
23
|
+
readonly id: string;
|
|
24
|
+
private chunks: StreamChunk[] = [];
|
|
25
|
+
private ended = false;
|
|
26
|
+
private paused = false;
|
|
27
|
+
private buffer: StreamChunk[] = [];
|
|
28
|
+
private highWaterMark: number;
|
|
29
|
+
|
|
30
|
+
constructor(id: string, options?: StreamOptions) {
|
|
31
|
+
super();
|
|
32
|
+
this.id = id;
|
|
33
|
+
this.highWaterMark = options?.highWaterMark ?? 64;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Push a chunk. Returns false if backpressure threshold reached. */
|
|
37
|
+
push(chunk: StreamChunk): boolean {
|
|
38
|
+
if (this.ended) return false;
|
|
39
|
+
|
|
40
|
+
this.chunks.push(chunk);
|
|
41
|
+
|
|
42
|
+
if (this.paused) {
|
|
43
|
+
this.buffer.push(chunk);
|
|
44
|
+
return this.buffer.length < this.highWaterMark;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
this.emit('chunk', chunk);
|
|
48
|
+
|
|
49
|
+
if (this.chunks.length >= this.highWaterMark) {
|
|
50
|
+
this.paused = true;
|
|
51
|
+
this.emit('backpressure');
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
return true;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Resume after backpressure — flush buffered chunks. */
|
|
58
|
+
resume(): void {
|
|
59
|
+
if (!this.paused) return;
|
|
60
|
+
this.paused = false;
|
|
61
|
+
const buffered = this.buffer.splice(0);
|
|
62
|
+
for (const chunk of buffered) {
|
|
63
|
+
this.emit('chunk', chunk);
|
|
64
|
+
}
|
|
65
|
+
this.emit('drain');
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
end(): void {
|
|
69
|
+
if (this.ended) return;
|
|
70
|
+
this.ended = true;
|
|
71
|
+
if (this.paused) this.resume();
|
|
72
|
+
this.emit('end');
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
getChunks(): StreamChunk[] {
|
|
76
|
+
return [...this.chunks];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/** Collect all text chunks into a single string. */
|
|
80
|
+
getText(): string {
|
|
81
|
+
return this.chunks
|
|
82
|
+
.filter((c) => c.type === 'text')
|
|
83
|
+
.map((c) => c.data)
|
|
84
|
+
.join('');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
get isEnded(): boolean {
|
|
88
|
+
return this.ended;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
get isPaused(): boolean {
|
|
92
|
+
return this.paused;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
get length(): number {
|
|
96
|
+
return this.chunks.length;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// ─── StreamingManager ────────────────────────────────────────
|
|
101
|
+
|
|
102
|
+
export class StreamingManager {
|
|
103
|
+
private streams: Map<string, StreamableResponse> = new Map();
|
|
104
|
+
private counter = 0;
|
|
105
|
+
|
|
106
|
+
/** Create a new stream. */
|
|
107
|
+
createStream(options?: StreamOptions): StreamableResponse {
|
|
108
|
+
const id = `stream_${++this.counter}_${Date.now()}`;
|
|
109
|
+
const stream = new StreamableResponse(id, options);
|
|
110
|
+
this.streams.set(id, stream);
|
|
111
|
+
stream.on('end', () => {
|
|
112
|
+
// Keep ended streams for a bit for late consumers, then clean up
|
|
113
|
+
setTimeout(() => this.streams.delete(id), 30_000);
|
|
114
|
+
});
|
|
115
|
+
return stream;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/** Write a text chunk to a stream. */
|
|
119
|
+
writeChunk(streamId: string, data: string, metadata?: Record<string, unknown>): boolean {
|
|
120
|
+
const stream = this.streams.get(streamId);
|
|
121
|
+
if (!stream) return false;
|
|
122
|
+
return stream.push({
|
|
123
|
+
id: `chunk_${stream.length}`,
|
|
124
|
+
type: 'text',
|
|
125
|
+
data,
|
|
126
|
+
timestamp: Date.now(),
|
|
127
|
+
metadata,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** End a stream. */
|
|
132
|
+
endStream(streamId: string): void {
|
|
133
|
+
const stream = this.streams.get(streamId);
|
|
134
|
+
if (!stream) return;
|
|
135
|
+
stream.push({
|
|
136
|
+
id: `chunk_${stream.length}`,
|
|
137
|
+
type: 'done',
|
|
138
|
+
data: '',
|
|
139
|
+
timestamp: Date.now(),
|
|
140
|
+
});
|
|
141
|
+
stream.end();
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/** Get an existing stream. */
|
|
145
|
+
getStream(streamId: string): StreamableResponse | undefined {
|
|
146
|
+
return this.streams.get(streamId);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** Format a chunk as an SSE event string. */
|
|
150
|
+
static formatSSE(chunk: StreamChunk): string {
|
|
151
|
+
const lines: string[] = [];
|
|
152
|
+
lines.push(`event: ${chunk.type}`);
|
|
153
|
+
lines.push(`id: ${chunk.id}`);
|
|
154
|
+
const payload = JSON.stringify({ data: chunk.data, metadata: chunk.metadata });
|
|
155
|
+
lines.push(`data: ${payload}`);
|
|
156
|
+
lines.push('');
|
|
157
|
+
return lines.join('\n') + '\n';
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Pipe a stream to an SSE-compatible HTTP response (Express-style). */
|
|
161
|
+
static pipeSSE(
|
|
162
|
+
stream: StreamableResponse,
|
|
163
|
+
res: { write(data: string): boolean; end(): void; setHeader?(name: string, value: string): void },
|
|
164
|
+
options?: StreamOptions,
|
|
165
|
+
): void {
|
|
166
|
+
res.setHeader?.('Content-Type', 'text/event-stream');
|
|
167
|
+
res.setHeader?.('Cache-Control', 'no-cache');
|
|
168
|
+
res.setHeader?.('Connection', 'keep-alive');
|
|
169
|
+
|
|
170
|
+
const heartbeatMs = options?.heartbeatInterval ?? 15_000;
|
|
171
|
+
const heartbeat = setInterval(() => {
|
|
172
|
+
res.write(': heartbeat\n\n');
|
|
173
|
+
}, heartbeatMs);
|
|
174
|
+
|
|
175
|
+
stream.on('chunk', (chunk: StreamChunk) => {
|
|
176
|
+
const ok = res.write(StreamingManager.formatSSE(chunk));
|
|
177
|
+
if (!ok && stream.isPaused === false) {
|
|
178
|
+
// Downstream can't keep up — will resume on drain from stream
|
|
179
|
+
}
|
|
180
|
+
});
|
|
181
|
+
|
|
182
|
+
stream.on('end', () => {
|
|
183
|
+
clearInterval(heartbeat);
|
|
184
|
+
res.end();
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
get activeCount(): number {
|
|
189
|
+
let count = 0;
|
|
190
|
+
for (const s of this.streams.values()) {
|
|
191
|
+
if (!s.isEnded) count++;
|
|
192
|
+
}
|
|
193
|
+
return count;
|
|
194
|
+
}
|
|
195
|
+
}
|
package/src/core/types.ts
CHANGED
|
@@ -1,68 +1,68 @@
|
|
|
1
|
-
import { EventEmitter } from 'events';
|
|
2
|
-
|
|
3
|
-
// ─── Core Types ──────────────────────────────────────────────
|
|
4
|
-
|
|
5
|
-
export type AgentState = 'init' | 'ready' | 'running' | 'stopped' | 'error';
|
|
6
|
-
|
|
7
|
-
export interface Message {
|
|
8
|
-
id: string;
|
|
9
|
-
role: 'user' | 'assistant' | 'system';
|
|
10
|
-
content: string;
|
|
11
|
-
timestamp: number;
|
|
12
|
-
metadata?: Record<string, unknown>;
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
export interface AgentContext {
|
|
16
|
-
agentName: string;
|
|
17
|
-
sessionId: string;
|
|
18
|
-
messages: Message[];
|
|
19
|
-
memory: MemoryStore;
|
|
20
|
-
metadata: Record<string, unknown>;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export interface SkillResult {
|
|
24
|
-
handled: boolean;
|
|
25
|
-
response?: string;
|
|
26
|
-
confidence: number;
|
|
27
|
-
metadata?: Record<string, unknown>;
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
export interface ISkill {
|
|
31
|
-
name: string;
|
|
32
|
-
description: string;
|
|
33
|
-
execute(context: AgentContext, message: Message): Promise<SkillResult>;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
export interface IChannel {
|
|
37
|
-
type: string;
|
|
38
|
-
start(): Promise<void>;
|
|
39
|
-
stop(): Promise<void>;
|
|
40
|
-
onMessage(handler: (message: Message) => Promise<Message>): void;
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
export interface MemoryStore {
|
|
44
|
-
get(key: string): Promise<unknown>;
|
|
45
|
-
set(key: string, value: unknown): Promise<void>;
|
|
46
|
-
getConversation(sessionId: string): Promise<Message[]>;
|
|
47
|
-
addMessage(sessionId: string, message: Message): Promise<void>;
|
|
48
|
-
clear(sessionId?: string): Promise<void>;
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
export interface AgentEvents {
|
|
52
|
-
'state:change': (from: AgentState, to: AgentState) => void;
|
|
53
|
-
'message:in': (message: Message) => void;
|
|
54
|
-
'message:out': (message: Message) => void;
|
|
55
|
-
'skill:execute': (skill: string, result: SkillResult) => void;
|
|
56
|
-
'error': (error: Error) => void;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface IAgent extends EventEmitter {
|
|
60
|
-
readonly name: string;
|
|
61
|
-
readonly state: AgentState;
|
|
62
|
-
init(): Promise<void>;
|
|
63
|
-
start(): Promise<void>;
|
|
64
|
-
stop(): Promise<void>;
|
|
65
|
-
handleMessage(message: Message): Promise<Message>;
|
|
66
|
-
registerSkill(skill: ISkill): void;
|
|
67
|
-
bindChannel(channel: IChannel): void;
|
|
68
|
-
}
|
|
1
|
+
import { EventEmitter } from 'events';
|
|
2
|
+
|
|
3
|
+
// ─── Core Types ──────────────────────────────────────────────
|
|
4
|
+
|
|
5
|
+
export type AgentState = 'init' | 'ready' | 'running' | 'stopped' | 'error';
|
|
6
|
+
|
|
7
|
+
export interface Message {
|
|
8
|
+
id: string;
|
|
9
|
+
role: 'user' | 'assistant' | 'system';
|
|
10
|
+
content: string;
|
|
11
|
+
timestamp: number;
|
|
12
|
+
metadata?: Record<string, unknown>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface AgentContext {
|
|
16
|
+
agentName: string;
|
|
17
|
+
sessionId: string;
|
|
18
|
+
messages: Message[];
|
|
19
|
+
memory: MemoryStore;
|
|
20
|
+
metadata: Record<string, unknown>;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface SkillResult {
|
|
24
|
+
handled: boolean;
|
|
25
|
+
response?: string;
|
|
26
|
+
confidence: number;
|
|
27
|
+
metadata?: Record<string, unknown>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface ISkill {
|
|
31
|
+
name: string;
|
|
32
|
+
description: string;
|
|
33
|
+
execute(context: AgentContext, message: Message): Promise<SkillResult>;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export interface IChannel {
|
|
37
|
+
type: string;
|
|
38
|
+
start(): Promise<void>;
|
|
39
|
+
stop(): Promise<void>;
|
|
40
|
+
onMessage(handler: (message: Message) => Promise<Message>): void;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export interface MemoryStore {
|
|
44
|
+
get(key: string): Promise<unknown>;
|
|
45
|
+
set(key: string, value: unknown): Promise<void>;
|
|
46
|
+
getConversation(sessionId: string): Promise<Message[]>;
|
|
47
|
+
addMessage(sessionId: string, message: Message): Promise<void>;
|
|
48
|
+
clear(sessionId?: string): Promise<void>;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export interface AgentEvents {
|
|
52
|
+
'state:change': (from: AgentState, to: AgentState) => void;
|
|
53
|
+
'message:in': (message: Message) => void;
|
|
54
|
+
'message:out': (message: Message) => void;
|
|
55
|
+
'skill:execute': (skill: string, result: SkillResult) => void;
|
|
56
|
+
'error': (error: Error) => void;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export interface IAgent extends EventEmitter {
|
|
60
|
+
readonly name: string;
|
|
61
|
+
readonly state: AgentState;
|
|
62
|
+
init(): Promise<void>;
|
|
63
|
+
start(): Promise<void>;
|
|
64
|
+
stop(): Promise<void>;
|
|
65
|
+
handleMessage(message: Message): Promise<Message>;
|
|
66
|
+
registerSkill(skill: ISkill): void;
|
|
67
|
+
bindChannel(channel: IChannel): void;
|
|
68
|
+
}
|
package/src/core/versioning.ts
CHANGED
|
@@ -1,106 +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
|
-
}
|
|
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
|
+
}
|