opc-agent 1.1.1 → 1.1.3

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 (142) hide show
  1. package/CHANGELOG.md +51 -51
  2. package/CONTRIBUTING.md +75 -75
  3. package/README.md +222 -126
  4. package/README.zh-CN.md +129 -80
  5. package/dist/channels/web.js +256 -256
  6. package/dist/deploy/hermes.js +22 -22
  7. package/dist/deploy/openclaw.js +31 -31
  8. package/dist/providers/index.d.ts +1 -1
  9. package/dist/providers/index.js +148 -13
  10. package/dist/schema/oad.d.ts +3 -3
  11. package/dist/templates/code-reviewer.js +5 -5
  12. package/dist/templates/customer-service.js +2 -2
  13. package/dist/templates/data-analyst.js +5 -5
  14. package/dist/templates/knowledge-base.js +2 -2
  15. package/dist/templates/sales-assistant.js +4 -4
  16. package/dist/templates/teacher.js +6 -6
  17. package/docs/.vitepress/config.ts +103 -103
  18. package/docs/api/cli.md +48 -48
  19. package/docs/api/oad-schema.md +64 -64
  20. package/docs/api/sdk.md +80 -80
  21. package/docs/guide/concepts.md +51 -51
  22. package/docs/guide/configuration.md +79 -79
  23. package/docs/guide/deployment.md +42 -42
  24. package/docs/guide/getting-started.md +44 -44
  25. package/docs/guide/templates.md +28 -28
  26. package/docs/guide/testing.md +84 -84
  27. package/docs/index.md +27 -27
  28. package/docs/zh/api/cli.md +54 -54
  29. package/docs/zh/api/oad-schema.md +87 -87
  30. package/docs/zh/api/sdk.md +102 -102
  31. package/docs/zh/guide/concepts.md +104 -104
  32. package/docs/zh/guide/configuration.md +135 -135
  33. package/docs/zh/guide/deployment.md +81 -81
  34. package/docs/zh/guide/getting-started.md +82 -82
  35. package/docs/zh/guide/templates.md +84 -84
  36. package/docs/zh/guide/testing.md +88 -88
  37. package/docs/zh/index.md +27 -27
  38. package/examples/customer-service-demo/README.md +90 -90
  39. package/examples/customer-service-demo/oad.yaml +107 -107
  40. package/package.json +1 -1
  41. package/src/analytics/index.ts +66 -66
  42. package/src/channels/discord.ts +192 -192
  43. package/src/channels/email.ts +177 -177
  44. package/src/channels/feishu.ts +236 -236
  45. package/src/channels/index.ts +15 -15
  46. package/src/channels/slack.ts +160 -160
  47. package/src/channels/telegram.ts +90 -90
  48. package/src/channels/voice.ts +106 -106
  49. package/src/channels/web.ts +596 -596
  50. package/src/channels/webhook.ts +199 -199
  51. package/src/channels/websocket.ts +87 -87
  52. package/src/channels/wechat.ts +149 -149
  53. package/src/core/a2a.ts +143 -143
  54. package/src/core/agent.ts +152 -152
  55. package/src/core/analytics-engine.ts +186 -186
  56. package/src/core/auth.ts +57 -57
  57. package/src/core/cache.ts +141 -141
  58. package/src/core/compose.ts +77 -77
  59. package/src/core/config.ts +14 -14
  60. package/src/core/errors.ts +148 -148
  61. package/src/core/hitl.ts +138 -138
  62. package/src/core/knowledge.ts +210 -210
  63. package/src/core/logger.ts +57 -57
  64. package/src/core/orchestrator.ts +215 -215
  65. package/src/core/performance.ts +187 -187
  66. package/src/core/rate-limiter.ts +128 -128
  67. package/src/core/room.ts +109 -109
  68. package/src/core/runtime.ts +152 -152
  69. package/src/core/sandbox.ts +101 -101
  70. package/src/core/security.ts +171 -171
  71. package/src/core/types.ts +68 -68
  72. package/src/core/versioning.ts +106 -106
  73. package/src/core/watch.ts +178 -178
  74. package/src/core/workflow.ts +235 -235
  75. package/src/deploy/hermes.ts +156 -156
  76. package/src/deploy/openclaw.ts +200 -200
  77. package/src/dtv/data.ts +29 -29
  78. package/src/dtv/trust.ts +43 -43
  79. package/src/dtv/value.ts +47 -47
  80. package/src/i18n/index.ts +216 -216
  81. package/src/index.ts +110 -110
  82. package/src/marketplace/index.ts +223 -223
  83. package/src/memory/deepbrain.ts +108 -108
  84. package/src/memory/index.ts +34 -34
  85. package/src/plugins/index.ts +208 -208
  86. package/src/providers/index.ts +322 -183
  87. package/src/schema/oad.ts +155 -155
  88. package/src/skills/base.ts +16 -16
  89. package/src/skills/document.ts +100 -100
  90. package/src/skills/http.ts +35 -35
  91. package/src/skills/index.ts +27 -27
  92. package/src/skills/scheduler.ts +80 -80
  93. package/src/skills/webhook-trigger.ts +59 -59
  94. package/src/templates/code-reviewer.ts +34 -34
  95. package/src/templates/customer-service.ts +80 -80
  96. package/src/templates/data-analyst.ts +70 -70
  97. package/src/templates/executive-assistant.ts +71 -71
  98. package/src/templates/financial-advisor.ts +60 -60
  99. package/src/templates/knowledge-base.ts +31 -31
  100. package/src/templates/legal-assistant.ts +71 -71
  101. package/src/templates/sales-assistant.ts +79 -79
  102. package/src/templates/teacher.ts +79 -79
  103. package/src/testing/index.ts +181 -181
  104. package/src/tools/calculator.ts +73 -73
  105. package/src/tools/datetime.ts +149 -149
  106. package/src/tools/json-transform.ts +187 -187
  107. package/src/tools/mcp.ts +76 -76
  108. package/src/tools/text-analysis.ts +116 -116
  109. package/templates/Dockerfile +15 -15
  110. package/templates/code-reviewer/README.md +27 -27
  111. package/templates/code-reviewer/oad.yaml +41 -41
  112. package/templates/customer-service/README.md +22 -22
  113. package/templates/customer-service/oad.yaml +36 -36
  114. package/templates/docker-compose.yml +21 -21
  115. package/templates/knowledge-base/README.md +28 -28
  116. package/templates/knowledge-base/oad.yaml +38 -38
  117. package/templates/sales-assistant/README.md +26 -26
  118. package/templates/sales-assistant/oad.yaml +43 -43
  119. package/tests/a2a.test.ts +66 -66
  120. package/tests/agent.test.ts +72 -72
  121. package/tests/analytics.test.ts +50 -50
  122. package/tests/channel.test.ts +39 -39
  123. package/tests/e2e.test.ts +134 -134
  124. package/tests/errors.test.ts +83 -83
  125. package/tests/hitl.test.ts +71 -71
  126. package/tests/i18n.test.ts +41 -41
  127. package/tests/mcp.test.ts +54 -54
  128. package/tests/oad.test.ts +68 -68
  129. package/tests/performance.test.ts +115 -115
  130. package/tests/plugin.test.ts +74 -74
  131. package/tests/room.test.ts +106 -106
  132. package/tests/runtime.test.ts +42 -42
  133. package/tests/sandbox.test.ts +46 -46
  134. package/tests/security.test.ts +60 -60
  135. package/tests/templates.test.ts +77 -77
  136. package/tests/v070.test.ts +76 -76
  137. package/tests/versioning.test.ts +75 -75
  138. package/tests/voice.test.ts +61 -61
  139. package/tests/webhook.test.ts +29 -29
  140. package/tests/workflow.test.ts +143 -143
  141. package/tsconfig.json +19 -19
  142. package/vitest.config.ts +9 -9
@@ -1,210 +1,210 @@
1
- /**
2
- * Knowledge Base / RAG - Local vector storage with semantic search
3
- */
4
- import * as fs from 'fs';
5
- import * as path from 'path';
6
- import * as crypto from 'crypto';
7
-
8
- // Simple in-memory vector store (PGlite-compatible interface for future migration)
9
- interface VectorEntry {
10
- id: string;
11
- content: string;
12
- embedding: number[];
13
- metadata: Record<string, unknown>;
14
- }
15
-
16
- interface KnowledgeStore {
17
- entries: VectorEntry[];
18
- version: number;
19
- updatedAt: string;
20
- }
21
-
22
- const CHUNK_SIZE = 500; // chars per chunk
23
- const CHUNK_OVERLAP = 50;
24
- const STORE_FILE = '.opc-knowledge.json';
25
-
26
- function splitText(text: string, chunkSize = CHUNK_SIZE, overlap = CHUNK_OVERLAP): string[] {
27
- const chunks: string[] = [];
28
- // Split by paragraphs first, then by size
29
- const paragraphs = text.split(/\n\s*\n/).filter(p => p.trim());
30
- let current = '';
31
-
32
- for (const para of paragraphs) {
33
- if (current.length + para.length > chunkSize && current.length > 0) {
34
- chunks.push(current.trim());
35
- // Keep overlap from end of current
36
- current = current.slice(-overlap) + '\n\n' + para;
37
- } else {
38
- current += (current ? '\n\n' : '') + para;
39
- }
40
- }
41
- if (current.trim()) chunks.push(current.trim());
42
-
43
- // If any chunk is still too large, split by sentences
44
- const result: string[] = [];
45
- for (const chunk of chunks) {
46
- if (chunk.length <= chunkSize * 1.5) {
47
- result.push(chunk);
48
- } else {
49
- const sentences = chunk.split(/(?<=[.!?])\s+/);
50
- let buf = '';
51
- for (const s of sentences) {
52
- if (buf.length + s.length > chunkSize && buf) {
53
- result.push(buf.trim());
54
- buf = buf.slice(-overlap) + ' ' + s;
55
- } else {
56
- buf += (buf ? ' ' : '') + s;
57
- }
58
- }
59
- if (buf.trim()) result.push(buf.trim());
60
- }
61
- }
62
- return result;
63
- }
64
-
65
- // Simple TF-IDF-like embedding (no external dependencies)
66
- // For production, replace with real embedding API
67
- function simpleEmbed(text: string): number[] {
68
- const words = text.toLowerCase().replace(/[^a-z0-9\s]/g, '').split(/\s+/).filter(Boolean);
69
- const dim = 128;
70
- const vec = new Array(dim).fill(0);
71
-
72
- for (const word of words) {
73
- const hash = crypto.createHash('md5').update(word).digest();
74
- for (let i = 0; i < dim; i++) {
75
- vec[i] += (hash[i % hash.length] - 128) / 128;
76
- }
77
- }
78
-
79
- // Normalize
80
- const mag = Math.sqrt(vec.reduce((s, v) => s + v * v, 0)) || 1;
81
- return vec.map(v => v / mag);
82
- }
83
-
84
- function cosineSimilarity(a: number[], b: number[]): number {
85
- let dot = 0, magA = 0, magB = 0;
86
- for (let i = 0; i < a.length; i++) {
87
- dot += a[i] * b[i];
88
- magA += a[i] * a[i];
89
- magB += b[i] * b[i];
90
- }
91
- return dot / (Math.sqrt(magA) * Math.sqrt(magB) || 1);
92
- }
93
-
94
- export class KnowledgeBase {
95
- private store: KnowledgeStore;
96
- private storePath: string;
97
-
98
- constructor(baseDir: string = '.') {
99
- this.storePath = path.join(baseDir, STORE_FILE);
100
- this.store = this.load();
101
- }
102
-
103
- private load(): KnowledgeStore {
104
- try {
105
- if (fs.existsSync(this.storePath)) {
106
- return JSON.parse(fs.readFileSync(this.storePath, 'utf-8'));
107
- }
108
- } catch { /* ignore */ }
109
- return { entries: [], version: 1, updatedAt: new Date().toISOString() };
110
- }
111
-
112
- private save(): void {
113
- this.store.updatedAt = new Date().toISOString();
114
- fs.writeFileSync(this.storePath, JSON.stringify(this.store), 'utf-8');
115
- }
116
-
117
- async addFile(filePath: string): Promise<{ chunks: number }> {
118
- const absPath = path.resolve(filePath);
119
- if (!fs.existsSync(absPath)) {
120
- throw new Error(`File not found: ${absPath}`);
121
- }
122
-
123
- const content = fs.readFileSync(absPath, 'utf-8');
124
- const filename = path.basename(absPath);
125
-
126
- // Remove existing entries for this file
127
- this.store.entries = this.store.entries.filter(
128
- e => e.metadata.source !== filename
129
- );
130
-
131
- const chunks = splitText(content);
132
- for (let i = 0; i < chunks.length; i++) {
133
- const chunk = chunks[i];
134
- this.store.entries.push({
135
- id: `${filename}_${i}_${Date.now()}`,
136
- content: chunk,
137
- embedding: simpleEmbed(chunk),
138
- metadata: {
139
- source: filename,
140
- chunkIndex: i,
141
- totalChunks: chunks.length,
142
- addedAt: new Date().toISOString(),
143
- },
144
- });
145
- }
146
-
147
- this.save();
148
- return { chunks: chunks.length };
149
- }
150
-
151
- async addText(text: string, source: string = 'manual'): Promise<{ chunks: number }> {
152
- const chunks = splitText(text);
153
- for (let i = 0; i < chunks.length; i++) {
154
- this.store.entries.push({
155
- id: `${source}_${i}_${Date.now()}`,
156
- content: chunks[i],
157
- embedding: simpleEmbed(chunks[i]),
158
- metadata: { source, chunkIndex: i, totalChunks: chunks.length, addedAt: new Date().toISOString() },
159
- });
160
- }
161
- this.save();
162
- return { chunks: chunks.length };
163
- }
164
-
165
- async search(query: string, topK: number = 5): Promise<Array<{ content: string; score: number; source: string }>> {
166
- if (this.store.entries.length === 0) return [];
167
-
168
- const queryEmb = simpleEmbed(query);
169
- const scored = this.store.entries.map(entry => ({
170
- content: entry.content,
171
- score: cosineSimilarity(queryEmb, entry.embedding),
172
- source: String(entry.metadata.source ?? 'unknown'),
173
- }));
174
-
175
- scored.sort((a, b) => b.score - a.score);
176
- return scored.slice(0, topK);
177
- }
178
-
179
- /** Build context string for injection into LLM calls */
180
- async getContext(query: string, topK: number = 3, minScore: number = 0.1): Promise<string> {
181
- const results = await this.search(query, topK);
182
- const relevant = results.filter(r => r.score >= minScore);
183
- if (relevant.length === 0) return '';
184
-
185
- return `\n\n--- Relevant Knowledge ---\n${relevant.map((r, i) =>
186
- `[${i + 1}] (source: ${r.source}, relevance: ${(r.score * 100).toFixed(0)}%)\n${r.content}`
187
- ).join('\n\n')}\n--- End Knowledge ---\n`;
188
- }
189
-
190
- getStats(): { totalEntries: number; sources: string[]; updatedAt: string } {
191
- const sources = [...new Set(this.store.entries.map(e => String(e.metadata.source)))];
192
- return {
193
- totalEntries: this.store.entries.length,
194
- sources,
195
- updatedAt: this.store.updatedAt,
196
- };
197
- }
198
-
199
- clear(): void {
200
- this.store.entries = [];
201
- this.save();
202
- }
203
-
204
- removeSource(source: string): number {
205
- const before = this.store.entries.length;
206
- this.store.entries = this.store.entries.filter(e => e.metadata.source !== source);
207
- this.save();
208
- return before - this.store.entries.length;
209
- }
210
- }
1
+ /**
2
+ * Knowledge Base / RAG - Local vector storage with semantic search
3
+ */
4
+ import * as fs from 'fs';
5
+ import * as path from 'path';
6
+ import * as crypto from 'crypto';
7
+
8
+ // Simple in-memory vector store (PGlite-compatible interface for future migration)
9
+ interface VectorEntry {
10
+ id: string;
11
+ content: string;
12
+ embedding: number[];
13
+ metadata: Record<string, unknown>;
14
+ }
15
+
16
+ interface KnowledgeStore {
17
+ entries: VectorEntry[];
18
+ version: number;
19
+ updatedAt: string;
20
+ }
21
+
22
+ const CHUNK_SIZE = 500; // chars per chunk
23
+ const CHUNK_OVERLAP = 50;
24
+ const STORE_FILE = '.opc-knowledge.json';
25
+
26
+ function splitText(text: string, chunkSize = CHUNK_SIZE, overlap = CHUNK_OVERLAP): string[] {
27
+ const chunks: string[] = [];
28
+ // Split by paragraphs first, then by size
29
+ const paragraphs = text.split(/\n\s*\n/).filter(p => p.trim());
30
+ let current = '';
31
+
32
+ for (const para of paragraphs) {
33
+ if (current.length + para.length > chunkSize && current.length > 0) {
34
+ chunks.push(current.trim());
35
+ // Keep overlap from end of current
36
+ current = current.slice(-overlap) + '\n\n' + para;
37
+ } else {
38
+ current += (current ? '\n\n' : '') + para;
39
+ }
40
+ }
41
+ if (current.trim()) chunks.push(current.trim());
42
+
43
+ // If any chunk is still too large, split by sentences
44
+ const result: string[] = [];
45
+ for (const chunk of chunks) {
46
+ if (chunk.length <= chunkSize * 1.5) {
47
+ result.push(chunk);
48
+ } else {
49
+ const sentences = chunk.split(/(?<=[.!?])\s+/);
50
+ let buf = '';
51
+ for (const s of sentences) {
52
+ if (buf.length + s.length > chunkSize && buf) {
53
+ result.push(buf.trim());
54
+ buf = buf.slice(-overlap) + ' ' + s;
55
+ } else {
56
+ buf += (buf ? ' ' : '') + s;
57
+ }
58
+ }
59
+ if (buf.trim()) result.push(buf.trim());
60
+ }
61
+ }
62
+ return result;
63
+ }
64
+
65
+ // Simple TF-IDF-like embedding (no external dependencies)
66
+ // For production, replace with real embedding API
67
+ function simpleEmbed(text: string): number[] {
68
+ const words = text.toLowerCase().replace(/[^a-z0-9\s]/g, '').split(/\s+/).filter(Boolean);
69
+ const dim = 128;
70
+ const vec = new Array(dim).fill(0);
71
+
72
+ for (const word of words) {
73
+ const hash = crypto.createHash('md5').update(word).digest();
74
+ for (let i = 0; i < dim; i++) {
75
+ vec[i] += (hash[i % hash.length] - 128) / 128;
76
+ }
77
+ }
78
+
79
+ // Normalize
80
+ const mag = Math.sqrt(vec.reduce((s, v) => s + v * v, 0)) || 1;
81
+ return vec.map(v => v / mag);
82
+ }
83
+
84
+ function cosineSimilarity(a: number[], b: number[]): number {
85
+ let dot = 0, magA = 0, magB = 0;
86
+ for (let i = 0; i < a.length; i++) {
87
+ dot += a[i] * b[i];
88
+ magA += a[i] * a[i];
89
+ magB += b[i] * b[i];
90
+ }
91
+ return dot / (Math.sqrt(magA) * Math.sqrt(magB) || 1);
92
+ }
93
+
94
+ export class KnowledgeBase {
95
+ private store: KnowledgeStore;
96
+ private storePath: string;
97
+
98
+ constructor(baseDir: string = '.') {
99
+ this.storePath = path.join(baseDir, STORE_FILE);
100
+ this.store = this.load();
101
+ }
102
+
103
+ private load(): KnowledgeStore {
104
+ try {
105
+ if (fs.existsSync(this.storePath)) {
106
+ return JSON.parse(fs.readFileSync(this.storePath, 'utf-8'));
107
+ }
108
+ } catch { /* ignore */ }
109
+ return { entries: [], version: 1, updatedAt: new Date().toISOString() };
110
+ }
111
+
112
+ private save(): void {
113
+ this.store.updatedAt = new Date().toISOString();
114
+ fs.writeFileSync(this.storePath, JSON.stringify(this.store), 'utf-8');
115
+ }
116
+
117
+ async addFile(filePath: string): Promise<{ chunks: number }> {
118
+ const absPath = path.resolve(filePath);
119
+ if (!fs.existsSync(absPath)) {
120
+ throw new Error(`File not found: ${absPath}`);
121
+ }
122
+
123
+ const content = fs.readFileSync(absPath, 'utf-8');
124
+ const filename = path.basename(absPath);
125
+
126
+ // Remove existing entries for this file
127
+ this.store.entries = this.store.entries.filter(
128
+ e => e.metadata.source !== filename
129
+ );
130
+
131
+ const chunks = splitText(content);
132
+ for (let i = 0; i < chunks.length; i++) {
133
+ const chunk = chunks[i];
134
+ this.store.entries.push({
135
+ id: `${filename}_${i}_${Date.now()}`,
136
+ content: chunk,
137
+ embedding: simpleEmbed(chunk),
138
+ metadata: {
139
+ source: filename,
140
+ chunkIndex: i,
141
+ totalChunks: chunks.length,
142
+ addedAt: new Date().toISOString(),
143
+ },
144
+ });
145
+ }
146
+
147
+ this.save();
148
+ return { chunks: chunks.length };
149
+ }
150
+
151
+ async addText(text: string, source: string = 'manual'): Promise<{ chunks: number }> {
152
+ const chunks = splitText(text);
153
+ for (let i = 0; i < chunks.length; i++) {
154
+ this.store.entries.push({
155
+ id: `${source}_${i}_${Date.now()}`,
156
+ content: chunks[i],
157
+ embedding: simpleEmbed(chunks[i]),
158
+ metadata: { source, chunkIndex: i, totalChunks: chunks.length, addedAt: new Date().toISOString() },
159
+ });
160
+ }
161
+ this.save();
162
+ return { chunks: chunks.length };
163
+ }
164
+
165
+ async search(query: string, topK: number = 5): Promise<Array<{ content: string; score: number; source: string }>> {
166
+ if (this.store.entries.length === 0) return [];
167
+
168
+ const queryEmb = simpleEmbed(query);
169
+ const scored = this.store.entries.map(entry => ({
170
+ content: entry.content,
171
+ score: cosineSimilarity(queryEmb, entry.embedding),
172
+ source: String(entry.metadata.source ?? 'unknown'),
173
+ }));
174
+
175
+ scored.sort((a, b) => b.score - a.score);
176
+ return scored.slice(0, topK);
177
+ }
178
+
179
+ /** Build context string for injection into LLM calls */
180
+ async getContext(query: string, topK: number = 3, minScore: number = 0.1): Promise<string> {
181
+ const results = await this.search(query, topK);
182
+ const relevant = results.filter(r => r.score >= minScore);
183
+ if (relevant.length === 0) return '';
184
+
185
+ return `\n\n--- Relevant Knowledge ---\n${relevant.map((r, i) =>
186
+ `[${i + 1}] (source: ${r.source}, relevance: ${(r.score * 100).toFixed(0)}%)\n${r.content}`
187
+ ).join('\n\n')}\n--- End Knowledge ---\n`;
188
+ }
189
+
190
+ getStats(): { totalEntries: number; sources: string[]; updatedAt: string } {
191
+ const sources = [...new Set(this.store.entries.map(e => String(e.metadata.source)))];
192
+ return {
193
+ totalEntries: this.store.entries.length,
194
+ sources,
195
+ updatedAt: this.store.updatedAt,
196
+ };
197
+ }
198
+
199
+ clear(): void {
200
+ this.store.entries = [];
201
+ this.save();
202
+ }
203
+
204
+ removeSource(source: string): number {
205
+ const before = this.store.entries.length;
206
+ this.store.entries = this.store.entries.filter(e => e.metadata.source !== source);
207
+ this.save();
208
+ return before - this.store.entries.length;
209
+ }
210
+ }
@@ -1,57 +1,57 @@
1
- /**
2
- * Structured logger with log levels.
3
- */
4
- export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
5
-
6
- const LOG_LEVEL_ORDER: Record<LogLevel, number> = {
7
- debug: 0,
8
- info: 1,
9
- warn: 2,
10
- error: 3,
11
- };
12
-
13
- export class Logger {
14
- private context: string;
15
- private level: LogLevel;
16
-
17
- constructor(context: string, level: LogLevel = 'info') {
18
- this.context = context;
19
- this.level = level;
20
- }
21
-
22
- private shouldLog(level: LogLevel): boolean {
23
- return LOG_LEVEL_ORDER[level] >= LOG_LEVEL_ORDER[this.level];
24
- }
25
-
26
- private format(level: LogLevel, message: string, data?: Record<string, unknown>): string {
27
- const ts = new Date().toISOString();
28
- const prefix = `[${ts}] [${level.toUpperCase()}] [${this.context}]`;
29
- return data ? `${prefix} ${message} ${JSON.stringify(data)}` : `${prefix} ${message}`;
30
- }
31
-
32
- debug(message: string, data?: Record<string, unknown>): void {
33
- if (this.shouldLog('debug')) console.debug(this.format('debug', message, data));
34
- }
35
-
36
- info(message: string, data?: Record<string, unknown>): void {
37
- if (this.shouldLog('info')) console.info(this.format('info', message, data));
38
- }
39
-
40
- warn(message: string, data?: Record<string, unknown>): void {
41
- if (this.shouldLog('warn')) console.warn(this.format('warn', message, data));
42
- }
43
-
44
- error(message: string, data?: Record<string, unknown>): void {
45
- if (this.shouldLog('error')) console.error(this.format('error', message, data));
46
- }
47
-
48
- setLevel(level: LogLevel): void {
49
- this.level = level;
50
- }
51
-
52
- child(context: string): Logger {
53
- return new Logger(`${this.context}:${context}`, this.level);
54
- }
55
- }
56
-
57
- export const defaultLogger = new Logger('opc');
1
+ /**
2
+ * Structured logger with log levels.
3
+ */
4
+ export type LogLevel = 'debug' | 'info' | 'warn' | 'error';
5
+
6
+ const LOG_LEVEL_ORDER: Record<LogLevel, number> = {
7
+ debug: 0,
8
+ info: 1,
9
+ warn: 2,
10
+ error: 3,
11
+ };
12
+
13
+ export class Logger {
14
+ private context: string;
15
+ private level: LogLevel;
16
+
17
+ constructor(context: string, level: LogLevel = 'info') {
18
+ this.context = context;
19
+ this.level = level;
20
+ }
21
+
22
+ private shouldLog(level: LogLevel): boolean {
23
+ return LOG_LEVEL_ORDER[level] >= LOG_LEVEL_ORDER[this.level];
24
+ }
25
+
26
+ private format(level: LogLevel, message: string, data?: Record<string, unknown>): string {
27
+ const ts = new Date().toISOString();
28
+ const prefix = `[${ts}] [${level.toUpperCase()}] [${this.context}]`;
29
+ return data ? `${prefix} ${message} ${JSON.stringify(data)}` : `${prefix} ${message}`;
30
+ }
31
+
32
+ debug(message: string, data?: Record<string, unknown>): void {
33
+ if (this.shouldLog('debug')) console.debug(this.format('debug', message, data));
34
+ }
35
+
36
+ info(message: string, data?: Record<string, unknown>): void {
37
+ if (this.shouldLog('info')) console.info(this.format('info', message, data));
38
+ }
39
+
40
+ warn(message: string, data?: Record<string, unknown>): void {
41
+ if (this.shouldLog('warn')) console.warn(this.format('warn', message, data));
42
+ }
43
+
44
+ error(message: string, data?: Record<string, unknown>): void {
45
+ if (this.shouldLog('error')) console.error(this.format('error', message, data));
46
+ }
47
+
48
+ setLevel(level: LogLevel): void {
49
+ this.level = level;
50
+ }
51
+
52
+ child(context: string): Logger {
53
+ return new Logger(`${this.context}:${context}`, this.level);
54
+ }
55
+ }
56
+
57
+ export const defaultLogger = new Logger('opc');