@unrdf/knowledge-engine 5.0.1 → 26.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/package.json +23 -17
  2. package/src/ai-enhanced-search.mjs +371 -0
  3. package/src/anomaly-detector.mjs +226 -0
  4. package/src/artifact-generator.mjs +252 -0
  5. package/src/browser.mjs +1 -1
  6. package/src/chatman/disruption-arithmetic.mjs +140 -0
  7. package/src/chatman/market-dynamics.mjs +140 -0
  8. package/src/chatman/organizational-dynamics.mjs +140 -0
  9. package/src/chatman/strategic-dynamics.mjs +140 -0
  10. package/src/chatman-config-loader.mjs +282 -0
  11. package/src/chatman-engine.mjs +435 -0
  12. package/src/chatman-operator.mjs +343 -0
  13. package/src/dark-field-detector.mjs +332 -0
  14. package/src/formation-theorems.mjs +345 -0
  15. package/src/index.mjs +20 -2
  16. package/src/knowledge-hook-manager.mjs +1 -1
  17. package/src/lockchain-writer-browser.mjs +2 -2
  18. package/src/observability.mjs +40 -4
  19. package/src/query-optimizer.mjs +1 -1
  20. package/src/resolution-layer.mjs +1 -1
  21. package/src/transaction.mjs +11 -9
  22. package/README.md +0 -84
  23. package/src/browser-shims.mjs +0 -343
  24. package/src/canonicalize.mjs +0 -414
  25. package/src/condition-cache.mjs +0 -109
  26. package/src/condition-evaluator.mjs +0 -722
  27. package/src/dark-matter-core.mjs +0 -742
  28. package/src/define-hook.mjs +0 -213
  29. package/src/effect-sandbox-browser.mjs +0 -283
  30. package/src/effect-sandbox-worker.mjs +0 -170
  31. package/src/effect-sandbox.mjs +0 -517
  32. package/src/engines/index.mjs +0 -11
  33. package/src/engines/rdf-engine.mjs +0 -299
  34. package/src/file-resolver.mjs +0 -387
  35. package/src/hook-executor-batching.mjs +0 -277
  36. package/src/hook-executor.mjs +0 -870
  37. package/src/hook-management.mjs +0 -150
  38. package/src/ken-parliment.mjs +0 -119
  39. package/src/ken.mjs +0 -149
  40. package/src/knowledge-engine/builtin-rules.mjs +0 -190
  41. package/src/knowledge-engine/inference-engine.mjs +0 -418
  42. package/src/knowledge-engine/knowledge-engine.mjs +0 -317
  43. package/src/knowledge-engine/pattern-dsl.mjs +0 -142
  44. package/src/knowledge-engine/pattern-matcher.mjs +0 -215
  45. package/src/knowledge-engine/rules.mjs +0 -184
  46. package/src/knowledge-engine.mjs +0 -319
  47. package/src/knowledge-hook-engine.mjs +0 -360
  48. package/src/knowledge-substrate-core.mjs +0 -927
  49. package/src/lite.mjs +0 -222
  50. package/src/lockchain-writer.mjs +0 -602
  51. package/src/monitoring/andon-signals.mjs +0 -775
  52. package/src/parse.mjs +0 -290
  53. package/src/performance-optimizer.mjs +0 -678
  54. package/src/policy-pack.mjs +0 -572
  55. package/src/query-cache.mjs +0 -116
  56. package/src/query.mjs +0 -306
  57. package/src/reason.mjs +0 -350
  58. package/src/schemas.mjs +0 -1063
  59. package/src/security/error-sanitizer.mjs +0 -257
  60. package/src/security/path-validator.mjs +0 -194
  61. package/src/security/sandbox-restrictions.mjs +0 -331
  62. package/src/security-validator.mjs +0 -389
  63. package/src/store-cache.mjs +0 -137
  64. package/src/telemetry.mjs +0 -167
  65. package/src/utils/adaptive-monitor.mjs +0 -746
  66. package/src/utils/circuit-breaker.mjs +0 -513
  67. package/src/utils/edge-case-handler.mjs +0 -503
  68. package/src/utils/memory-manager.mjs +0 -498
  69. package/src/utils/ring-buffer.mjs +0 -282
  70. package/src/validate.mjs +0 -319
  71. package/src/validators/index.mjs +0 -338
@@ -1,343 +0,0 @@
1
- /**
2
- * @file Browser compatibility shims for Node.js APIs
3
- * @module browser-shims
4
- *
5
- * @description
6
- * Provides browser-compatible polyfills for Node.js APIs used in the knowledge engine.
7
- * This allows the same codebase to work in both Node.js and browser environments.
8
- */
9
-
10
- /**
11
- * Check if we're running in a browser environment
12
- */
13
- export const isBrowser =
14
- typeof globalThis?.window !== 'undefined' && typeof globalThis?.window?.document !== 'undefined';
15
-
16
- /**
17
- * Check if we're running in Node.js environment
18
- */
19
- export const isNode = (() => {
20
- try {
21
- return typeof process !== 'undefined' && !!process?.versions?.node;
22
- } catch {
23
- return false;
24
- }
25
- })();
26
-
27
- // Random UUID generation - use crypto.randomUUID if available, otherwise fallback
28
- /**
29
- *
30
- */
31
- export function randomUUID() {
32
- if (typeof crypto !== 'undefined' && crypto.randomUUID) {
33
- return crypto.randomUUID();
34
- }
35
-
36
- // Fallback UUID generation
37
- return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
38
- const r = (Math.random() * 16) | 0;
39
- const v = c === 'x' ? r : (r & 0x3) | 0x8;
40
- return v.toString(16);
41
- });
42
- }
43
-
44
- // Path utilities - browser-compatible version
45
- export const path = {
46
- join: (...args) => args.filter(Boolean).join('/').replace(/\/+/g, '/'),
47
- resolve: (...args) => args.filter(Boolean).join('/').replace(/\/+/g, '/'),
48
- dirname: path => path.replace(/\/$/, '').split('/').slice(0, -1).join('/') || '.',
49
- basename: path => path.split('/').pop() || '',
50
- extname: path => {
51
- const ext = path.split('.').pop();
52
- return ext && ext !== path ? '.' + ext : '';
53
- },
54
- };
55
-
56
- // Process utilities
57
- export const process = {
58
- cwd: () => '/',
59
- env: isBrowser
60
- ? {}
61
- : (() => {
62
- try {
63
- return process.env;
64
- } catch {
65
- return {};
66
- }
67
- })(),
68
- versions: isBrowser
69
- ? {}
70
- : (() => {
71
- try {
72
- return process.versions;
73
- } catch {
74
- return {};
75
- }
76
- })(),
77
- };
78
-
79
- // File system operations - browser-compatible (no-ops or memory-based)
80
- class BrowserFileSystem {
81
- #files = new Map();
82
- #directories = new Set(['/']);
83
-
84
- constructor() {
85
- // Create some default directories
86
- this.#directories.add('/src');
87
- this.#directories.add('/dist');
88
- this.#directories.add('/examples');
89
- }
90
-
91
- clear() {
92
- this.#files.clear();
93
- this.#directories.clear();
94
- this.#directories.add('/');
95
- this.#directories.add('/src');
96
- this.#directories.add('/dist');
97
- this.#directories.add('/examples');
98
- }
99
-
100
- existsSync(path) {
101
- return this.#files.has(path) || this.#directories.has(path);
102
- }
103
-
104
- readFileSync(path, encoding = 'utf8') {
105
- const content = this.#files.get(path);
106
- if (!content) {
107
- throw new Error(`ENOENT: no such file or directory, open '${path}'`);
108
- }
109
- return encoding === 'utf8' ? content : Buffer.from(content, 'utf8');
110
- }
111
-
112
- writeFileSync(path, data, encoding = 'utf8') {
113
- const content = encoding === 'utf8' ? data : data.toString();
114
- this.#files.set(path, content);
115
-
116
- // Ensure parent directories exist
117
- const parent = path.dirname(path);
118
- if (!this.#directories.has(parent) && parent !== path) {
119
- this.#directories.add(parent);
120
- }
121
- }
122
-
123
- mkdirSync(path, _options = {}) {
124
- if (this.#directories.has(path)) {
125
- throw new Error(`EEXIST: file already exists, mkdir '${path}'`);
126
- }
127
- this.#directories.add(path);
128
- }
129
-
130
- readdirSync(path) {
131
- return Array.from(this.#files.keys()).filter(file => file.startsWith(path + '/'));
132
- }
133
-
134
- // Async versions
135
- async readFile(path, encoding = 'utf8') {
136
- return Promise.resolve(this.readFileSync(path, encoding));
137
- }
138
-
139
- async writeFile(path, data, encoding = 'utf8') {
140
- this.writeFileSync(path, data, encoding);
141
- return Promise.resolve();
142
- }
143
-
144
- async mkdir(path, options = {}) {
145
- this.mkdirSync(path, options);
146
- return Promise.resolve();
147
- }
148
- }
149
-
150
- export const fs = isBrowser
151
- ? new BrowserFileSystem()
152
- : await import('node:fs').then(m => m.default);
153
- export const fsPromises = isBrowser
154
- ? new BrowserFileSystem()
155
- : await import('node:fs/promises').then(m => m.default);
156
-
157
- // Worker thread polyfill for browser
158
- /**
159
- *
160
- */
161
- export class BrowserWorker {
162
- /**
163
- *
164
- */
165
- constructor(source, options = {}) {
166
- if (typeof source === 'string' && isBrowser) {
167
- // Convert string to blob URL (browser only)
168
- const blob = new Blob([source], { type: 'application/javascript' });
169
- this.worker = new Worker(URL.createObjectURL(blob), options);
170
- } else if (typeof source === 'string' && isNode) {
171
- // In Node.js, can't create Worker from inline code - need a file
172
- // Create a mock worker for testing purposes
173
- this.worker = null;
174
- this.mockMode = true;
175
- } else {
176
- // Assume it's a file path
177
- this.worker = new Worker(source, options);
178
- }
179
-
180
- this.messageHandlers = [];
181
- this.errorHandlers = [];
182
- this.exitHandlers = [];
183
- this.terminated = false;
184
-
185
- if (this.worker) {
186
- this.worker.onmessage = event => {
187
- this.messageHandlers.forEach(handler => handler(event.data));
188
- };
189
-
190
- this.worker.onerror = error => {
191
- this.errorHandlers.forEach(handler => handler(error));
192
- };
193
- }
194
- }
195
-
196
- /**
197
- *
198
- */
199
- postMessage(data) {
200
- if (this.terminated) return;
201
- if (this.worker) {
202
- this.worker.postMessage(data);
203
- }
204
- }
205
-
206
- /**
207
- *
208
- */
209
- terminate() {
210
- this.terminated = true;
211
- if (this.worker) {
212
- this.worker.terminate();
213
- }
214
- }
215
-
216
- /**
217
- *
218
- */
219
- on(event, handler) {
220
- switch (event) {
221
- case 'message':
222
- this.messageHandlers.push(handler);
223
- break;
224
- case 'error':
225
- this.errorHandlers.push(handler);
226
- break;
227
- case 'exit':
228
- this.exitHandlers.push(handler);
229
- break;
230
- }
231
- }
232
-
233
- /**
234
- *
235
- */
236
- once(event, handler) {
237
- const wrappedHandler = data => {
238
- handler(data);
239
- this.removeListener(event, wrappedHandler);
240
- };
241
- this.on(event, wrappedHandler);
242
- }
243
-
244
- /**
245
- *
246
- */
247
- removeListener(event, handler) {
248
- switch (event) {
249
- case 'message':
250
- this.messageHandlers = this.messageHandlers.filter(h => h !== handler);
251
- break;
252
- case 'error':
253
- this.errorHandlers = this.errorHandlers.filter(h => h !== handler);
254
- break;
255
- case 'exit':
256
- this.exitHandlers = this.exitHandlers.filter(h => h !== handler);
257
- break;
258
- }
259
- }
260
- }
261
-
262
- export const Worker = isBrowser
263
- ? BrowserWorker
264
- : await import('node:worker_threads').then(m => m.Worker);
265
-
266
- // Mock child_process.execSync - browser-compatible
267
- /**
268
- *
269
- */
270
- export async function execSync(command, options = {}) {
271
- if (isBrowser) {
272
- console.warn(`[Browser] execSync not available in browser: ${command}`);
273
- return ''; // Return empty string as mock
274
- }
275
-
276
- const { execSync: nodeExecSync } = await import('child_process');
277
- const result = nodeExecSync(command, { encoding: 'utf8', ...options });
278
- // Ensure we return a string, not a Buffer
279
- return typeof result === 'string' ? result : result.toString('utf8');
280
- }
281
-
282
- // Hash utilities - use Web Crypto API in browser
283
- /**
284
- *
285
- */
286
- export class BrowserHash {
287
- /**
288
- *
289
- */
290
- constructor(algorithm = 'SHA-256') {
291
- this.algorithm = algorithm.toLowerCase().replace(/[^a-z0-9]/g, '');
292
- }
293
-
294
- /**
295
- *
296
- */
297
- update(data) {
298
- this.data = typeof data === 'string' ? new TextEncoder().encode(data) : new Uint8Array(data);
299
- return this;
300
- }
301
-
302
- /**
303
- *
304
- */
305
- async digest(encoding = 'hex') {
306
- const hashBuffer = await crypto.subtle.digest(this.algorithm, this.data);
307
- const hashArray = new Uint8Array(hashBuffer);
308
-
309
- if (encoding === 'hex') {
310
- return Array.from(hashArray)
311
- .map(b => b.toString(16).padStart(2, '0'))
312
- .join('');
313
- }
314
-
315
- return hashArray;
316
- }
317
- }
318
-
319
- /**
320
- *
321
- */
322
- export async function createHash(algorithm) {
323
- if (isBrowser) {
324
- return new BrowserHash(algorithm);
325
- }
326
-
327
- const { createHash: nodeCreateHash } = await import('node:crypto');
328
- return nodeCreateHash(algorithm);
329
- }
330
-
331
- // Export all shims as a unified object
332
- export default {
333
- isBrowser,
334
- isNode,
335
- randomUUID,
336
- path,
337
- process,
338
- fs,
339
- fsPromises,
340
- Worker: BrowserWorker,
341
- execSync,
342
- createHash,
343
- };