@baselineos/protocol-core 1.0.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.
- package/.turbo/turbo-build.log +14 -0
- package/.turbo/turbo-test.log +15 -0
- package/CHANGELOG.md +49 -0
- package/LICENSE +17 -0
- package/README.md +18 -0
- package/dist/index.d.ts +1322 -0
- package/dist/index.js +2653 -0
- package/package.json +31 -0
- package/src/__tests__/functional.test.ts +269 -0
- package/src/__tests__/smoke.test.ts +23 -0
- package/src/chromadb.d.ts +9 -0
- package/src/index.ts +117 -0
- package/src/knowledge/knowledge-graph.ts +1441 -0
- package/src/knowledge/vector-store.ts +722 -0
- package/src/lang/lang.ts +278 -0
- package/src/lexicon/lexicon.ts +414 -0
- package/src/parser/grammar.ts +240 -0
- package/src/parser/parser.ts +420 -0
- package/src/types/index.ts +799 -0
- package/tsconfig.json +9 -0
package/dist/index.js
ADDED
|
@@ -0,0 +1,2653 @@
|
|
|
1
|
+
// src/lang/lang.ts
|
|
2
|
+
var BaselineLang = class {
|
|
3
|
+
parser;
|
|
4
|
+
lexicon;
|
|
5
|
+
constructor(parser, lexicon) {
|
|
6
|
+
this.parser = parser;
|
|
7
|
+
this.lexicon = lexicon;
|
|
8
|
+
}
|
|
9
|
+
async initialize() {
|
|
10
|
+
await this.initializeLanguageComponents();
|
|
11
|
+
await this.initializeDefaultLexicons();
|
|
12
|
+
await this.initializeLanguagePatterns();
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Get the lexicon instance
|
|
16
|
+
*/
|
|
17
|
+
getLexicon() {
|
|
18
|
+
return this.lexicon;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Parse Baseline Language source code
|
|
22
|
+
*/
|
|
23
|
+
async parseSource(source) {
|
|
24
|
+
try {
|
|
25
|
+
const program = await this.parser.parse(source);
|
|
26
|
+
return program;
|
|
27
|
+
} catch (error) {
|
|
28
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
29
|
+
throw new Error(`Parsing failed: ${errorMessage}`);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Compile Baseline Language to Intermediate Representation
|
|
34
|
+
*/
|
|
35
|
+
async compileToIR(program) {
|
|
36
|
+
try {
|
|
37
|
+
const ir = await this.compileProgram(program);
|
|
38
|
+
return ir;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
41
|
+
throw new Error(`Compilation failed: ${errorMessage}`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Execute Baseline Language program
|
|
46
|
+
*/
|
|
47
|
+
async execute(program) {
|
|
48
|
+
try {
|
|
49
|
+
const ir = await this.compileToIR(program);
|
|
50
|
+
const result = await this.executeIR(ir);
|
|
51
|
+
return result;
|
|
52
|
+
} catch (error) {
|
|
53
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
54
|
+
throw new Error(`Execution failed: ${errorMessage}`);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Validate Baseline Language program
|
|
59
|
+
*/
|
|
60
|
+
async validate(program) {
|
|
61
|
+
try {
|
|
62
|
+
const validation = await this.validateProgram(program);
|
|
63
|
+
return validation;
|
|
64
|
+
} catch (error) {
|
|
65
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
66
|
+
throw new Error(`Validation failed: ${errorMessage}`);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Generate Baseline Language from template
|
|
71
|
+
*/
|
|
72
|
+
async generateFromTemplate(template, variables) {
|
|
73
|
+
try {
|
|
74
|
+
const generated = await this.generateLanguage(template, variables);
|
|
75
|
+
return generated;
|
|
76
|
+
} catch (error) {
|
|
77
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
78
|
+
throw new Error(`Generation failed: ${errorMessage}`);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Get language statistics and metrics
|
|
83
|
+
*/
|
|
84
|
+
async getLanguageStats() {
|
|
85
|
+
try {
|
|
86
|
+
const stats = await this.calculateLanguageStats();
|
|
87
|
+
return stats;
|
|
88
|
+
} catch (error) {
|
|
89
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
90
|
+
throw new Error(`Stats calculation failed: ${errorMessage}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
// ─── Private Implementation ─────────────────────────────────────────
|
|
94
|
+
async initializeLanguageComponents() {
|
|
95
|
+
await this.initializeLexiconSystem();
|
|
96
|
+
await this.initializeSyntaxSystem();
|
|
97
|
+
await this.initializeCommandSystem();
|
|
98
|
+
}
|
|
99
|
+
async initializeDefaultLexicons() {
|
|
100
|
+
await this.lexicon.loadDefaultPacks();
|
|
101
|
+
}
|
|
102
|
+
async initializeLanguagePatterns() {
|
|
103
|
+
await this.initializePatterns();
|
|
104
|
+
}
|
|
105
|
+
async initializeLexiconSystem() {
|
|
106
|
+
}
|
|
107
|
+
async initializeSyntaxSystem() {
|
|
108
|
+
}
|
|
109
|
+
async initializeCommandSystem() {
|
|
110
|
+
}
|
|
111
|
+
async initializePatterns() {
|
|
112
|
+
}
|
|
113
|
+
async compileProgram(program) {
|
|
114
|
+
return {
|
|
115
|
+
version: "1.0.0",
|
|
116
|
+
blocks: program.blocks,
|
|
117
|
+
metadata: program.metadata,
|
|
118
|
+
optimizations: []
|
|
119
|
+
};
|
|
120
|
+
}
|
|
121
|
+
async executeIR(_ir) {
|
|
122
|
+
return {
|
|
123
|
+
success: true,
|
|
124
|
+
outputs: [],
|
|
125
|
+
metrics: {},
|
|
126
|
+
timestamp: /* @__PURE__ */ new Date()
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
async validateProgram(program) {
|
|
130
|
+
const validation = {
|
|
131
|
+
isValid: true,
|
|
132
|
+
errors: [],
|
|
133
|
+
warnings: [],
|
|
134
|
+
suggestions: []
|
|
135
|
+
};
|
|
136
|
+
if (!program.blocks || program.blocks.length === 0) {
|
|
137
|
+
validation.isValid = false;
|
|
138
|
+
validation.errors.push("Program must contain at least one block");
|
|
139
|
+
}
|
|
140
|
+
if (!program.metadata) {
|
|
141
|
+
validation.isValid = false;
|
|
142
|
+
validation.errors.push("Program must have metadata");
|
|
143
|
+
}
|
|
144
|
+
for (const block of program.blocks) {
|
|
145
|
+
const blockValidation = await this.validateBlock(block);
|
|
146
|
+
if (!blockValidation.isValid) {
|
|
147
|
+
validation.isValid = false;
|
|
148
|
+
validation.errors.push(...blockValidation.errors);
|
|
149
|
+
}
|
|
150
|
+
validation.warnings.push(...blockValidation.warnings);
|
|
151
|
+
validation.suggestions.push(...blockValidation.suggestions);
|
|
152
|
+
}
|
|
153
|
+
return validation;
|
|
154
|
+
}
|
|
155
|
+
async validateBlock(block) {
|
|
156
|
+
const validation = {
|
|
157
|
+
isValid: true,
|
|
158
|
+
errors: [],
|
|
159
|
+
warnings: [],
|
|
160
|
+
suggestions: []
|
|
161
|
+
};
|
|
162
|
+
if (!block.type) {
|
|
163
|
+
validation.isValid = false;
|
|
164
|
+
validation.errors.push("Block must have a type");
|
|
165
|
+
}
|
|
166
|
+
if (!block.metadata) {
|
|
167
|
+
validation.isValid = false;
|
|
168
|
+
validation.errors.push("Block must have metadata");
|
|
169
|
+
}
|
|
170
|
+
return validation;
|
|
171
|
+
}
|
|
172
|
+
async generateLanguage(template, variables) {
|
|
173
|
+
let generated = template;
|
|
174
|
+
for (const [key, value] of Object.entries(variables)) {
|
|
175
|
+
const placeholder = `{{${key}}}`;
|
|
176
|
+
generated = generated.replace(new RegExp(placeholder, "g"), String(value));
|
|
177
|
+
}
|
|
178
|
+
return generated;
|
|
179
|
+
}
|
|
180
|
+
async calculateLanguageStats() {
|
|
181
|
+
return {
|
|
182
|
+
totalBlocks: 0,
|
|
183
|
+
blockTypes: {},
|
|
184
|
+
lexiconSize: 0,
|
|
185
|
+
patternCount: 0,
|
|
186
|
+
templateCount: 0,
|
|
187
|
+
complexity: "low"
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
};
|
|
191
|
+
|
|
192
|
+
// src/parser/parser.ts
|
|
193
|
+
var BaselineParser = class {
|
|
194
|
+
tokens = [];
|
|
195
|
+
currentPosition = 0;
|
|
196
|
+
grammar = null;
|
|
197
|
+
async initialize() {
|
|
198
|
+
await this.initializeGrammar();
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Parse Baseline Language source code
|
|
202
|
+
*/
|
|
203
|
+
async parse(source) {
|
|
204
|
+
try {
|
|
205
|
+
this.tokens = this.tokenize(source);
|
|
206
|
+
this.currentPosition = 0;
|
|
207
|
+
const program = await this.parseProgram();
|
|
208
|
+
return program;
|
|
209
|
+
} catch (error) {
|
|
210
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
211
|
+
throw new Error(`Parsing failed: ${errorMessage}`);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* Tokenize source code into tokens
|
|
216
|
+
*/
|
|
217
|
+
tokenize(source) {
|
|
218
|
+
const tokens = [];
|
|
219
|
+
let position = 0;
|
|
220
|
+
while (position < source.length) {
|
|
221
|
+
const char = source[position];
|
|
222
|
+
if (/\s/.test(char)) {
|
|
223
|
+
position++;
|
|
224
|
+
continue;
|
|
225
|
+
}
|
|
226
|
+
if (char === "/" && source[position + 1] === "/") {
|
|
227
|
+
const commentEnd = source.indexOf("\n", position);
|
|
228
|
+
if (commentEnd === -1) {
|
|
229
|
+
position = source.length;
|
|
230
|
+
} else {
|
|
231
|
+
position = commentEnd + 1;
|
|
232
|
+
}
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (char === "/" && source[position + 1] === "*") {
|
|
236
|
+
const commentEnd = source.indexOf("*/", position);
|
|
237
|
+
if (commentEnd === -1) {
|
|
238
|
+
throw new Error("Unterminated multi-line comment");
|
|
239
|
+
}
|
|
240
|
+
position = commentEnd + 2;
|
|
241
|
+
continue;
|
|
242
|
+
}
|
|
243
|
+
if (/[a-zA-Z_]/.test(char)) {
|
|
244
|
+
const identifier = this.extractIdentifier(source, position);
|
|
245
|
+
tokens.push({
|
|
246
|
+
type: "IDENTIFIER",
|
|
247
|
+
value: identifier,
|
|
248
|
+
position
|
|
249
|
+
});
|
|
250
|
+
position += identifier.length;
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (char === '"') {
|
|
254
|
+
const str = this.extractString(source, position);
|
|
255
|
+
tokens.push({
|
|
256
|
+
type: "STRING",
|
|
257
|
+
value: str,
|
|
258
|
+
position
|
|
259
|
+
});
|
|
260
|
+
position += str.length + 2;
|
|
261
|
+
continue;
|
|
262
|
+
}
|
|
263
|
+
if (/[0-9]/.test(char)) {
|
|
264
|
+
const num = this.extractNumber(source, position);
|
|
265
|
+
tokens.push({
|
|
266
|
+
type: "NUMBER",
|
|
267
|
+
value: num,
|
|
268
|
+
position
|
|
269
|
+
});
|
|
270
|
+
position += num.length;
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
if (this.isOperator(char) || this.isPunctuation(char)) {
|
|
274
|
+
tokens.push({
|
|
275
|
+
type: "OPERATOR",
|
|
276
|
+
value: char,
|
|
277
|
+
position
|
|
278
|
+
});
|
|
279
|
+
position++;
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
throw new Error(`Unknown character at position ${position}: ${char}`);
|
|
283
|
+
}
|
|
284
|
+
tokens.push({
|
|
285
|
+
type: "EOF",
|
|
286
|
+
value: "",
|
|
287
|
+
position
|
|
288
|
+
});
|
|
289
|
+
return tokens;
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Parse program structure
|
|
293
|
+
*/
|
|
294
|
+
async parseProgram() {
|
|
295
|
+
const blocks = [];
|
|
296
|
+
while (this.currentToken().type !== "EOF") {
|
|
297
|
+
const block = await this.parseBlock();
|
|
298
|
+
if (block) {
|
|
299
|
+
blocks.push(block);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
const metadata = {
|
|
303
|
+
version: "1.0.0",
|
|
304
|
+
created: /* @__PURE__ */ new Date(),
|
|
305
|
+
author: "Baseline Lang",
|
|
306
|
+
description: "Generated Baseline Language program",
|
|
307
|
+
tags: ["baseline", "lang", "generated"]
|
|
308
|
+
};
|
|
309
|
+
return {
|
|
310
|
+
blocks,
|
|
311
|
+
metadata
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Parse individual block
|
|
316
|
+
*/
|
|
317
|
+
async parseBlock() {
|
|
318
|
+
const token = this.currentToken();
|
|
319
|
+
if (token.type === "IDENTIFIER") {
|
|
320
|
+
switch (token.value) {
|
|
321
|
+
case "lang":
|
|
322
|
+
return await this.parseLangBlock();
|
|
323
|
+
case "frame":
|
|
324
|
+
return await this.parseFrameBlock();
|
|
325
|
+
case "core":
|
|
326
|
+
return await this.parseCoreBlock();
|
|
327
|
+
default:
|
|
328
|
+
throw new Error(`Unknown block type: ${token.value}`);
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return null;
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Parse language block
|
|
335
|
+
*/
|
|
336
|
+
async parseLangBlock() {
|
|
337
|
+
this.expect("IDENTIFIER", "lang");
|
|
338
|
+
this.expect("OPERATOR", "{");
|
|
339
|
+
const content = await this.parseLangBlockContent();
|
|
340
|
+
this.expect("OPERATOR", "}");
|
|
341
|
+
return {
|
|
342
|
+
type: "lang",
|
|
343
|
+
content,
|
|
344
|
+
metadata: this.createBlockMetadata("lang")
|
|
345
|
+
};
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Parse frame block
|
|
349
|
+
*/
|
|
350
|
+
async parseFrameBlock() {
|
|
351
|
+
this.expect("IDENTIFIER", "frame");
|
|
352
|
+
this.expect("OPERATOR", "{");
|
|
353
|
+
const content = await this.parseFrameBlockContent();
|
|
354
|
+
this.expect("OPERATOR", "}");
|
|
355
|
+
return {
|
|
356
|
+
type: "frame",
|
|
357
|
+
content,
|
|
358
|
+
metadata: this.createBlockMetadata("frame")
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
/**
|
|
362
|
+
* Parse core block
|
|
363
|
+
*/
|
|
364
|
+
async parseCoreBlock() {
|
|
365
|
+
this.expect("IDENTIFIER", "core");
|
|
366
|
+
this.expect("OPERATOR", "{");
|
|
367
|
+
const content = await this.parseCoreBlockContent();
|
|
368
|
+
this.expect("OPERATOR", "}");
|
|
369
|
+
return {
|
|
370
|
+
type: "core",
|
|
371
|
+
content,
|
|
372
|
+
metadata: this.createBlockMetadata("core")
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
/**
|
|
376
|
+
* Parse language block content
|
|
377
|
+
* TODO: Full implementation should parse lexicon, syntax, and commands
|
|
378
|
+
*/
|
|
379
|
+
async parseLangBlockContent() {
|
|
380
|
+
while (this.currentPosition < this.tokens.length && this.currentToken().type !== "EOF") {
|
|
381
|
+
const token = this.currentToken();
|
|
382
|
+
if (token.type === "OPERATOR" && token.value === "}") {
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
this.currentPosition++;
|
|
386
|
+
}
|
|
387
|
+
return {
|
|
388
|
+
type: "lexicon",
|
|
389
|
+
domain: "default",
|
|
390
|
+
vocabulary: [],
|
|
391
|
+
templates: [],
|
|
392
|
+
patterns: []
|
|
393
|
+
};
|
|
394
|
+
}
|
|
395
|
+
/**
|
|
396
|
+
* Parse frame block content
|
|
397
|
+
* TODO: Full implementation should parse intent, mode, environment, etc.
|
|
398
|
+
*/
|
|
399
|
+
async parseFrameBlockContent() {
|
|
400
|
+
while (this.currentPosition < this.tokens.length && this.currentToken().type !== "EOF") {
|
|
401
|
+
const token = this.currentToken();
|
|
402
|
+
if (token.type === "OPERATOR" && token.value === "}") {
|
|
403
|
+
break;
|
|
404
|
+
}
|
|
405
|
+
this.currentPosition++;
|
|
406
|
+
}
|
|
407
|
+
return {
|
|
408
|
+
type: "intent",
|
|
409
|
+
goals: [],
|
|
410
|
+
deliverables: [],
|
|
411
|
+
successCriteria: [],
|
|
412
|
+
constraints: []
|
|
413
|
+
};
|
|
414
|
+
}
|
|
415
|
+
/**
|
|
416
|
+
* Parse core block content
|
|
417
|
+
* TODO: Full implementation should parse fidelity, spec, artifact, etc.
|
|
418
|
+
*/
|
|
419
|
+
async parseCoreBlockContent() {
|
|
420
|
+
while (this.currentPosition < this.tokens.length && this.currentToken().type !== "EOF") {
|
|
421
|
+
const token = this.currentToken();
|
|
422
|
+
if (token.type === "OPERATOR" && token.value === "}") {
|
|
423
|
+
break;
|
|
424
|
+
}
|
|
425
|
+
this.currentPosition++;
|
|
426
|
+
}
|
|
427
|
+
return {
|
|
428
|
+
type: "fidelity",
|
|
429
|
+
precision: "medium",
|
|
430
|
+
verification: "standard",
|
|
431
|
+
quality: [],
|
|
432
|
+
constraints: []
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
// ─── Helper Methods ─────────────────────────────────────────────────
|
|
436
|
+
extractIdentifier(source, position) {
|
|
437
|
+
let end = position;
|
|
438
|
+
while (end < source.length && /[a-zA-Z0-9_]/.test(source[end])) {
|
|
439
|
+
end++;
|
|
440
|
+
}
|
|
441
|
+
return source.substring(position, end);
|
|
442
|
+
}
|
|
443
|
+
extractString(source, position) {
|
|
444
|
+
let end = position + 1;
|
|
445
|
+
while (end < source.length && source[end] !== '"') {
|
|
446
|
+
if (source[end] === "\\") {
|
|
447
|
+
end += 2;
|
|
448
|
+
} else {
|
|
449
|
+
end++;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
if (end >= source.length) {
|
|
453
|
+
throw new Error("Unterminated string");
|
|
454
|
+
}
|
|
455
|
+
return source.substring(position + 1, end);
|
|
456
|
+
}
|
|
457
|
+
extractNumber(source, position) {
|
|
458
|
+
let end = position;
|
|
459
|
+
while (end < source.length && /[0-9]/.test(source[end])) {
|
|
460
|
+
end++;
|
|
461
|
+
}
|
|
462
|
+
if (end < source.length && source[end] === ".") {
|
|
463
|
+
end++;
|
|
464
|
+
while (end < source.length && /[0-9]/.test(source[end])) {
|
|
465
|
+
end++;
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
return source.substring(position, end);
|
|
469
|
+
}
|
|
470
|
+
isOperator(char) {
|
|
471
|
+
return /[+\-*/=<>!&|]/.test(char);
|
|
472
|
+
}
|
|
473
|
+
isPunctuation(char) {
|
|
474
|
+
return /[(){}[\],;:.]/.test(char);
|
|
475
|
+
}
|
|
476
|
+
currentToken() {
|
|
477
|
+
return this.tokens[this.currentPosition];
|
|
478
|
+
}
|
|
479
|
+
expect(type, value) {
|
|
480
|
+
const token = this.currentToken();
|
|
481
|
+
if (token.type !== type) {
|
|
482
|
+
throw new Error(`Expected ${type}, got ${token.type}`);
|
|
483
|
+
}
|
|
484
|
+
if (value && token.value !== value) {
|
|
485
|
+
throw new Error(`Expected ${value}, got ${token.value}`);
|
|
486
|
+
}
|
|
487
|
+
this.currentPosition++;
|
|
488
|
+
}
|
|
489
|
+
createBlockMetadata(type) {
|
|
490
|
+
return {
|
|
491
|
+
id: `${type}_${Date.now()}`,
|
|
492
|
+
name: `${type} block`,
|
|
493
|
+
description: `Generated ${type} block`,
|
|
494
|
+
version: "1.0.0",
|
|
495
|
+
created: /* @__PURE__ */ new Date(),
|
|
496
|
+
modified: /* @__PURE__ */ new Date(),
|
|
497
|
+
tags: [type, "generated"],
|
|
498
|
+
priority: "medium"
|
|
499
|
+
};
|
|
500
|
+
}
|
|
501
|
+
async initializeGrammar() {
|
|
502
|
+
}
|
|
503
|
+
};
|
|
504
|
+
|
|
505
|
+
// src/lexicon/lexicon.ts
|
|
506
|
+
var BaselineLexicon = class {
|
|
507
|
+
domains = /* @__PURE__ */ new Map();
|
|
508
|
+
vocabulary = /* @__PURE__ */ new Map();
|
|
509
|
+
templates = /* @__PURE__ */ new Map();
|
|
510
|
+
patterns = /* @__PURE__ */ new Map();
|
|
511
|
+
async initialize() {
|
|
512
|
+
await this.initializeDefaultDomains();
|
|
513
|
+
await this.initializeDefaultVocabulary();
|
|
514
|
+
await this.initializeDefaultTemplates();
|
|
515
|
+
await this.initializeDefaultPatterns();
|
|
516
|
+
}
|
|
517
|
+
/**
|
|
518
|
+
* Load default lexicon packs
|
|
519
|
+
*/
|
|
520
|
+
async loadDefaultPacks() {
|
|
521
|
+
await this.loadCoreDomainPack();
|
|
522
|
+
await this.loadAIMLDomainPack();
|
|
523
|
+
await this.loadBusinessDomainPack();
|
|
524
|
+
}
|
|
525
|
+
/**
|
|
526
|
+
* Add a new domain
|
|
527
|
+
*/
|
|
528
|
+
addDomain(domain) {
|
|
529
|
+
this.domains.set(domain.name, domain);
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Get domain by name
|
|
533
|
+
*/
|
|
534
|
+
getDomain(name) {
|
|
535
|
+
return this.domains.get(name);
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* List all domains
|
|
539
|
+
*/
|
|
540
|
+
listDomains() {
|
|
541
|
+
return Array.from(this.domains.values());
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Add vocabulary set
|
|
545
|
+
*/
|
|
546
|
+
addVocabulary(domain, vocabulary) {
|
|
547
|
+
this.vocabulary.set(domain, vocabulary);
|
|
548
|
+
}
|
|
549
|
+
/**
|
|
550
|
+
* Get vocabulary for domain
|
|
551
|
+
*/
|
|
552
|
+
getVocabulary(domain) {
|
|
553
|
+
return this.vocabulary.get(domain);
|
|
554
|
+
}
|
|
555
|
+
/**
|
|
556
|
+
* Add template
|
|
557
|
+
*/
|
|
558
|
+
addTemplate(template) {
|
|
559
|
+
this.templates.set(template.name, template);
|
|
560
|
+
}
|
|
561
|
+
/**
|
|
562
|
+
* Get template by name
|
|
563
|
+
*/
|
|
564
|
+
getTemplate(name) {
|
|
565
|
+
return this.templates.get(name);
|
|
566
|
+
}
|
|
567
|
+
/**
|
|
568
|
+
* List all templates
|
|
569
|
+
*/
|
|
570
|
+
listTemplates() {
|
|
571
|
+
return Array.from(this.templates.values());
|
|
572
|
+
}
|
|
573
|
+
/**
|
|
574
|
+
* Add pattern
|
|
575
|
+
*/
|
|
576
|
+
addPattern(pattern) {
|
|
577
|
+
this.patterns.set(pattern.name, pattern);
|
|
578
|
+
}
|
|
579
|
+
/**
|
|
580
|
+
* Get pattern by name
|
|
581
|
+
*/
|
|
582
|
+
getPattern(name) {
|
|
583
|
+
return this.patterns.get(name);
|
|
584
|
+
}
|
|
585
|
+
/**
|
|
586
|
+
* List all patterns
|
|
587
|
+
*/
|
|
588
|
+
listPatterns() {
|
|
589
|
+
return Array.from(this.patterns.values());
|
|
590
|
+
}
|
|
591
|
+
/**
|
|
592
|
+
* Search vocabulary across domains
|
|
593
|
+
*/
|
|
594
|
+
searchVocabulary(query, domain) {
|
|
595
|
+
const results = [];
|
|
596
|
+
const searchDomains = domain ? [domain] : Array.from(this.vocabulary.keys());
|
|
597
|
+
for (const searchDomain of searchDomains) {
|
|
598
|
+
const vocab = this.vocabulary.get(searchDomain);
|
|
599
|
+
if (vocab) {
|
|
600
|
+
for (const term of vocab.terms) {
|
|
601
|
+
if (this.matchesSearch(term.name, query) || this.matchesSearch(term.definition, query)) {
|
|
602
|
+
results.push({
|
|
603
|
+
type: "term",
|
|
604
|
+
domain: searchDomain,
|
|
605
|
+
item: term,
|
|
606
|
+
relevance: this.calculateRelevance(term.name, query)
|
|
607
|
+
});
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
for (const definition of vocab.definitions) {
|
|
611
|
+
if (this.matchesSearch(definition.meaning, query)) {
|
|
612
|
+
results.push({
|
|
613
|
+
type: "definition",
|
|
614
|
+
domain: searchDomain,
|
|
615
|
+
item: definition,
|
|
616
|
+
relevance: this.calculateRelevance(definition.meaning, query)
|
|
617
|
+
});
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
results.sort((a, b) => b.relevance - a.relevance);
|
|
623
|
+
return results;
|
|
624
|
+
}
|
|
625
|
+
/**
|
|
626
|
+
* Get lexicon statistics
|
|
627
|
+
*/
|
|
628
|
+
getLexiconStats() {
|
|
629
|
+
return {
|
|
630
|
+
totalDomains: this.domains.size,
|
|
631
|
+
totalTerms: Array.from(this.vocabulary.values()).reduce(
|
|
632
|
+
(sum, vocab) => sum + vocab.terms.length,
|
|
633
|
+
0
|
|
634
|
+
),
|
|
635
|
+
totalTemplates: this.templates.size,
|
|
636
|
+
totalPatterns: this.patterns.size,
|
|
637
|
+
domains: Array.from(this.domains.keys()),
|
|
638
|
+
vocabularySizes: Object.fromEntries(
|
|
639
|
+
Array.from(this.vocabulary.entries()).map(([d, vocab]) => [d, vocab.terms.length])
|
|
640
|
+
)
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
// ─── Default Initialization ─────────────────────────────────────────
|
|
644
|
+
async initializeDefaultDomains() {
|
|
645
|
+
this.addDomain({
|
|
646
|
+
name: "core",
|
|
647
|
+
description: "Core Baseline Language concepts and vocabulary",
|
|
648
|
+
vocabulary: ["program", "block", "metadata", "type", "content"],
|
|
649
|
+
patterns: ["structure", "validation", "execution"],
|
|
650
|
+
examples: ["lang { ... }", "frame { ... }", "core { ... }"]
|
|
651
|
+
});
|
|
652
|
+
this.addDomain({
|
|
653
|
+
name: "ai-ml",
|
|
654
|
+
description: "Artificial Intelligence and Machine Learning concepts",
|
|
655
|
+
vocabulary: ["model", "training", "inference", "accuracy", "precision"],
|
|
656
|
+
patterns: ["supervised", "unsupervised", "reinforcement"],
|
|
657
|
+
examples: ['model { type: "llm", version: "1.0" }']
|
|
658
|
+
});
|
|
659
|
+
this.addDomain({
|
|
660
|
+
name: "business",
|
|
661
|
+
description: "Business and enterprise concepts",
|
|
662
|
+
vocabulary: ["kpi", "metric", "goal", "deliverable", "success"],
|
|
663
|
+
patterns: ["measurement", "tracking", "reporting"],
|
|
664
|
+
examples: ['goal { name: "revenue", target: 1000000 }']
|
|
665
|
+
});
|
|
666
|
+
}
|
|
667
|
+
async initializeDefaultVocabulary() {
|
|
668
|
+
this.addVocabulary("core", {
|
|
669
|
+
terms: [
|
|
670
|
+
{
|
|
671
|
+
name: "program",
|
|
672
|
+
definition: "A complete Baseline Language program",
|
|
673
|
+
synonyms: ["script", "code", "application"],
|
|
674
|
+
antonyms: [],
|
|
675
|
+
examples: ["Complete program structure"]
|
|
676
|
+
},
|
|
677
|
+
{
|
|
678
|
+
name: "block",
|
|
679
|
+
definition: "A logical section of a Baseline Language program",
|
|
680
|
+
synonyms: ["section", "module", "component"],
|
|
681
|
+
antonyms: [],
|
|
682
|
+
examples: ["lang block", "frame block", "core block"]
|
|
683
|
+
}
|
|
684
|
+
],
|
|
685
|
+
synonyms: [],
|
|
686
|
+
antonyms: [],
|
|
687
|
+
definitions: []
|
|
688
|
+
});
|
|
689
|
+
this.addVocabulary("ai-ml", {
|
|
690
|
+
terms: [
|
|
691
|
+
{
|
|
692
|
+
name: "model",
|
|
693
|
+
definition: "An AI or ML model for processing",
|
|
694
|
+
synonyms: ["algorithm", "system", "engine"],
|
|
695
|
+
antonyms: [],
|
|
696
|
+
examples: ["LLM model", "NLP model", "Vision model"]
|
|
697
|
+
}
|
|
698
|
+
],
|
|
699
|
+
synonyms: [],
|
|
700
|
+
antonyms: [],
|
|
701
|
+
definitions: []
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
async initializeDefaultTemplates() {
|
|
705
|
+
this.addTemplate({
|
|
706
|
+
name: "basic-program",
|
|
707
|
+
description: "Basic Baseline Language program structure",
|
|
708
|
+
pattern: "lang { lexicon { ... } }\nframe { intent { ... } }\ncore { fidelity { ... } }",
|
|
709
|
+
variables: [
|
|
710
|
+
{
|
|
711
|
+
name: "programName",
|
|
712
|
+
type: "string",
|
|
713
|
+
description: "Name of the program",
|
|
714
|
+
required: true
|
|
715
|
+
},
|
|
716
|
+
{
|
|
717
|
+
name: "description",
|
|
718
|
+
type: "string",
|
|
719
|
+
description: "Program description",
|
|
720
|
+
required: false
|
|
721
|
+
}
|
|
722
|
+
],
|
|
723
|
+
examples: ["Basic program with lexicon, intent, and fidelity"]
|
|
724
|
+
});
|
|
725
|
+
this.addTemplate({
|
|
726
|
+
name: "intent-block",
|
|
727
|
+
description: "Intent block with goals and deliverables",
|
|
728
|
+
pattern: 'intent {\n goal {{goalName}} {\n description: "{{goalDescription}}"\n priority: "{{priority}}"\n }\n}',
|
|
729
|
+
variables: [
|
|
730
|
+
{
|
|
731
|
+
name: "goalName",
|
|
732
|
+
type: "string",
|
|
733
|
+
description: "Name of the goal",
|
|
734
|
+
required: true
|
|
735
|
+
},
|
|
736
|
+
{
|
|
737
|
+
name: "goalDescription",
|
|
738
|
+
type: "string",
|
|
739
|
+
description: "Description of the goal",
|
|
740
|
+
required: true
|
|
741
|
+
},
|
|
742
|
+
{
|
|
743
|
+
name: "priority",
|
|
744
|
+
type: "string",
|
|
745
|
+
description: "Priority level",
|
|
746
|
+
required: false,
|
|
747
|
+
default: "medium"
|
|
748
|
+
}
|
|
749
|
+
],
|
|
750
|
+
examples: ["Intent block with defined goal and priority"]
|
|
751
|
+
});
|
|
752
|
+
}
|
|
753
|
+
async initializeDefaultPatterns() {
|
|
754
|
+
this.addPattern({
|
|
755
|
+
name: "block-structure",
|
|
756
|
+
description: "Standard block structure pattern",
|
|
757
|
+
regex: "^[a-zA-Z_][a-zA-Z0-9_]*\\s*\\{[^}]*\\}$",
|
|
758
|
+
examples: ["lang { ... }", "frame { ... }", "core { ... }"],
|
|
759
|
+
usage: ["Program blocks", "Content blocks", "Configuration blocks"]
|
|
760
|
+
});
|
|
761
|
+
this.addPattern({
|
|
762
|
+
name: "validation-rule",
|
|
763
|
+
description: "Validation rule pattern",
|
|
764
|
+
regex: "^validation\\s+[a-zA-Z_][a-zA-Z0-9_]*\\s*\\{[^}]*\\}$",
|
|
765
|
+
examples: ["validation required { ... }", "validation format { ... }"],
|
|
766
|
+
usage: ["Input validation", "Data validation", "Format validation"]
|
|
767
|
+
});
|
|
768
|
+
}
|
|
769
|
+
async loadCoreDomainPack() {
|
|
770
|
+
}
|
|
771
|
+
async loadAIMLDomainPack() {
|
|
772
|
+
}
|
|
773
|
+
async loadBusinessDomainPack() {
|
|
774
|
+
}
|
|
775
|
+
// ─── Search Utilities ─────────────────────────────────────────────
|
|
776
|
+
matchesSearch(text, query) {
|
|
777
|
+
return text.toLowerCase().includes(query.toLowerCase());
|
|
778
|
+
}
|
|
779
|
+
calculateRelevance(text, query) {
|
|
780
|
+
const textLower = text.toLowerCase();
|
|
781
|
+
const queryLower = query.toLowerCase();
|
|
782
|
+
if (textLower === queryLower) return 1;
|
|
783
|
+
if (textLower.startsWith(queryLower)) return 0.9;
|
|
784
|
+
if (textLower.includes(queryLower)) return 0.7;
|
|
785
|
+
const textWords = textLower.split(/\s+/);
|
|
786
|
+
const queryWords = queryLower.split(/\s+/);
|
|
787
|
+
let matches = 0;
|
|
788
|
+
for (const queryWord of queryWords) {
|
|
789
|
+
if (textWords.some((textWord) => textWord.includes(queryWord))) {
|
|
790
|
+
matches++;
|
|
791
|
+
}
|
|
792
|
+
}
|
|
793
|
+
return matches / queryWords.length * 0.5;
|
|
794
|
+
}
|
|
795
|
+
};
|
|
796
|
+
|
|
797
|
+
// src/parser/grammar.ts
|
|
798
|
+
var baselineGrammarRules = {
|
|
799
|
+
// Program structure
|
|
800
|
+
program: `program = langBlock* frameBlock* coreBlock*`,
|
|
801
|
+
// Top-level blocks
|
|
802
|
+
langBlock: `langBlock = "lang" "{" (lexiconBlock / syntaxBlock / commandsBlock) "}"`,
|
|
803
|
+
frameBlock: `frameBlock = "frame" "{" (intentBlock / modeBlock / environmentBlock / scopeBlock / authorityBlock / basisBlock) "}"`,
|
|
804
|
+
coreBlock: `coreBlock = "core" "{" (fidelityBlock / specBlock / artifactBlock / productBlock / planBlock / promptBlock) "}"`,
|
|
805
|
+
// Lang: Lexicon
|
|
806
|
+
lexiconBlock: `lexiconBlock = "lexicon" "{" domain vocabulary template* pattern* "}"`,
|
|
807
|
+
domain: `domain = "domain" ":" identifier "{" description vocabulary patterns examples "}"`,
|
|
808
|
+
vocabulary: `vocabulary = "vocabulary" "{" term* synonym* antonym* definition* "}"`,
|
|
809
|
+
template: `template = "template" identifier "{" description pattern variables examples "}"`,
|
|
810
|
+
pattern: `pattern = "pattern" identifier "{" description regex examples usage "}"`,
|
|
811
|
+
// Lang: Syntax
|
|
812
|
+
syntaxBlock: `syntaxBlock = "syntax" "{" grammarRule* operator* precedenceRule* "}"`,
|
|
813
|
+
grammarRule: `grammarRule = "rule" identifier "{" pattern precedence associativity "}"`,
|
|
814
|
+
operator: `operator = "operator" symbol "{" precedence associativity description "}"`,
|
|
815
|
+
precedenceRule: `precedenceRule = "precedence" level "{" operators description "}"`,
|
|
816
|
+
// Lang: Commands
|
|
817
|
+
commandsBlock: `commandsBlock = "commands" "{" action* directive* operation* "}"`,
|
|
818
|
+
action: `action = "action" identifier "{" description parameters examples "}"`,
|
|
819
|
+
directive: `directive = "directive" identifier "{" description parameters examples "}"`,
|
|
820
|
+
operation: `operation = "operation" identifier "{" description parameters examples "}"`,
|
|
821
|
+
// Frame: Intent
|
|
822
|
+
intentBlock: `intentBlock = "intent" "{" goal* deliverable* successCriterion* constraint* "}"`,
|
|
823
|
+
goal: `goal = "goal" identifier "{" description priority deadline successCriteria "}"`,
|
|
824
|
+
deliverable: `deliverable = "deliverable" identifier "{" description type format quality "}"`,
|
|
825
|
+
successCriterion: `successCriterion = "criterion" identifier "{" description metric target unit "}"`,
|
|
826
|
+
constraint: `constraint = "constraint" identifier "{" description type value enforcement "}"`,
|
|
827
|
+
// Frame: Mode
|
|
828
|
+
modeBlock: `modeBlock = "mode" "{" executionPattern reasoning checkpoint* iterationRule* "}"`,
|
|
829
|
+
executionPattern: `executionPattern = "pattern" identifier "{" description steps checkpoints iterations "}"`,
|
|
830
|
+
reasoningMode: `reasoningMode = "reasoning" type "{" description parameters "}"`,
|
|
831
|
+
checkpoint: `checkpoint = "checkpoint" identifier "{" description validation actions "}"`,
|
|
832
|
+
iterationRule: `iterationRule = "iteration" identifier "{" condition maxIterations convergence "}"`,
|
|
833
|
+
// Frame: Environment
|
|
834
|
+
environmentBlock: `environmentBlock = "environment" "{" model* tool* runtime policy* "}"`,
|
|
835
|
+
model: `model = "model" identifier "{" type version capabilities constraints "}"`,
|
|
836
|
+
tool: `tool = "tool" identifier "{" description type parameters examples "}"`,
|
|
837
|
+
runtime: `runtime = "runtime" "{" environment version resources policies "}"`,
|
|
838
|
+
policy: `policy = "policy" identifier "{" description type rules enforcement "}"`,
|
|
839
|
+
// Frame: Scope
|
|
840
|
+
scopeBlock: `scopeBlock = "scope" "{" boundary* limit* exclusion* inclusion* "}"`,
|
|
841
|
+
boundary: `boundary = "boundary" identifier "{" description type value enforcement "}"`,
|
|
842
|
+
limit: `limit = "limit" identifier "{" description type value unit "}"`,
|
|
843
|
+
exclusion: `exclusion = "exclusion" identifier "{" description reason alternatives "}"`,
|
|
844
|
+
inclusion: `inclusion = "inclusion" identifier "{" description reason requirements "}"`,
|
|
845
|
+
// Frame: Authority
|
|
846
|
+
authorityBlock: `authorityBlock = "authority" "{" role* permission* accessControl* delegationRule* "}"`,
|
|
847
|
+
role: `role = "role" identifier "{" description permissions responsibilities "}"`,
|
|
848
|
+
permission: `permission = "permission" identifier "{" description scope level "}"`,
|
|
849
|
+
accessControl: `accessControl = "access" resource "{" permissions roles conditions "}"`,
|
|
850
|
+
delegationRule: `delegationRule = "delegation" identifier "{" description fromRole toRole conditions expiration "}"`,
|
|
851
|
+
// Frame: Basis
|
|
852
|
+
basisBlock: `basisBlock = "basis" "{" organization kpi* metric* context* "}"`,
|
|
853
|
+
organization: `organization = "organization" "{" name description structure culture values "}"`,
|
|
854
|
+
kpi: `kpi = "kpi" identifier "{" description metric target unit frequency "}"`,
|
|
855
|
+
metric: `metric = "metric" identifier "{" description type calculation unit "}"`,
|
|
856
|
+
context: `context = "context" identifier "{" description type value source "}"`,
|
|
857
|
+
// Core: Fidelity
|
|
858
|
+
fidelityBlock: `fidelityBlock = "fidelity" "{" precision verification quality constraints "}"`,
|
|
859
|
+
precisionLevel: `precisionLevel = "precision" level "{" description requirements "}"`,
|
|
860
|
+
verificationLevel: `verificationLevel = "verification" level "{" description methods "}"`,
|
|
861
|
+
qualityStandard: `qualityStandard = "quality" identifier "{" description criteria measurement "}"`,
|
|
862
|
+
// Core: Spec
|
|
863
|
+
specBlock: `specBlock = "spec" "{" requirement* contract* interface* validation* "}"`,
|
|
864
|
+
requirement: `requirement = "requirement" identifier "{" description type priority acceptance "}"`,
|
|
865
|
+
contract: `contract = "contract" identifier "{" description parties terms enforcement "}"`,
|
|
866
|
+
interface: `interface = "interface" identifier "{" description type methods properties "}"`,
|
|
867
|
+
validation: `validation = "validation" identifier "{" description type rules errorHandling "}"`,
|
|
868
|
+
// Core: Artifact
|
|
869
|
+
artifactBlock: `artifactBlock = "artifact" "{" output* receipt* signature* retentionPolicy* "}"`,
|
|
870
|
+
output: `output = "output" identifier "{" description type format quality "}"`,
|
|
871
|
+
receipt: `receipt = "receipt" identifier "{" description timestamp hash signature "}"`,
|
|
872
|
+
signature: `signature = "signature" "{" algorithm value timestamp key "}"`,
|
|
873
|
+
retentionPolicy: `retentionPolicy = "retention" identifier "{" description duration conditions disposal "}"`,
|
|
874
|
+
// Core: Product
|
|
875
|
+
productBlock: `productBlock = "product" "{" deliverable* document* result* outcome* "}"`,
|
|
876
|
+
document: `document = "document" identifier "{" description type format content "}"`,
|
|
877
|
+
result: `result = "result" identifier "{" description type value quality "}"`,
|
|
878
|
+
outcome: `outcome = "outcome" identifier "{" description type success metrics "}"`,
|
|
879
|
+
// Core: Plan
|
|
880
|
+
planBlock: `planBlock = "plan" "{" roadmap timeline milestone* dependency* "}"`,
|
|
881
|
+
roadmap: `roadmap = "roadmap" "{" name description phases timeline milestones "}"`,
|
|
882
|
+
timeline: `timeline = "timeline" "{" start end phases dependencies "}"`,
|
|
883
|
+
milestone: `milestone = "milestone" identifier "{" description date deliverables successCriteria "}"`,
|
|
884
|
+
dependency: `dependency = "dependency" identifier "{" description from to type critical "}"`,
|
|
885
|
+
// Core: Prompt (IR)
|
|
886
|
+
promptBlock: `promptBlock = "prompt" "{" ir instruction* context* constraint* "}"`,
|
|
887
|
+
intermediateRepresentation: `intermediateRepresentation = "ir" "{" version blocks metadata optimizations "}"`,
|
|
888
|
+
instruction: `instruction = "instruction" identifier "{" description type parameters examples "}"`,
|
|
889
|
+
// Utility elements
|
|
890
|
+
parameter: `parameter = "parameter" identifier "{" type description required default validation "}"`,
|
|
891
|
+
term: `term = "term" identifier "{" definition synonyms antonyms examples "}"`,
|
|
892
|
+
synonym: `synonym = "synonym" term "{" alternatives context "}"`,
|
|
893
|
+
antonym: `antonym = "antonym" term "{" opposites context "}"`,
|
|
894
|
+
definition: `definition = "definition" term "{" meaning context examples "}"`,
|
|
895
|
+
variable: `variable = "variable" identifier "{" type description required default "}"`,
|
|
896
|
+
step: `step = "step" identifier "{" description action validation next "}"`,
|
|
897
|
+
convergenceCriteria: `convergenceCriteria = "convergence" "{" metric threshold tolerance maxIterations "}"`,
|
|
898
|
+
capability: `capability = "capability" identifier "{" description parameters examples "}"`,
|
|
899
|
+
resource: `resource = "resource" "{" type name capacity unit available "}"`,
|
|
900
|
+
rule: `rule = "rule" identifier "{" description condition action priority "}"`,
|
|
901
|
+
condition: `condition = "condition" identifier "{" description expression parameters "}"`,
|
|
902
|
+
structure: `structure = "structure" "{" type levels departments reporting "}"`,
|
|
903
|
+
culture: `culture = "culture" "{" values principles practices norms "}"`,
|
|
904
|
+
value: `value = "value" identifier "{" description importance examples "}"`,
|
|
905
|
+
phase: `phase = "phase" identifier "{" description start end deliverables milestones "}"`,
|
|
906
|
+
optimization: `optimization = "optimization" identifier "{" description type impact implementation "}"`,
|
|
907
|
+
errorHandling: `errorHandling = "errorHandling" "{" strategy fallback logging userNotification "}"`,
|
|
908
|
+
method: `method = "method" identifier "{" description parameters returnType examples "}"`,
|
|
909
|
+
property: `property = "property" identifier "{" description type required default "}"`,
|
|
910
|
+
acceptanceCriteria: `acceptanceCriteria = "criteria" identifier "{" description test passCondition priority "}"`,
|
|
911
|
+
// Primitives
|
|
912
|
+
identifier: `identifier = [a-zA-Z_][a-zA-Z0-9_]*`,
|
|
913
|
+
description: `description = "description" ":" string`,
|
|
914
|
+
string: `string = '"' [^"]* '"'`,
|
|
915
|
+
number: `number = [0-9]+ ("." [0-9]+)?`,
|
|
916
|
+
boolean: `boolean = "true" / "false"`,
|
|
917
|
+
date: `date = [0-9]{4}-[0-9]{2}-[0-9]{2}`,
|
|
918
|
+
time: `time = [0-9]{2}:[0-9]{2}:[0-9]{2}`,
|
|
919
|
+
datetime: `datetime = date "T" time`,
|
|
920
|
+
array: `array = "[" (valueElement ("," valueElement)*)? "]"`,
|
|
921
|
+
object: `object = "{" (pair ("," pair)*)? "}"`,
|
|
922
|
+
pair: `pair = key ":" valueElement`,
|
|
923
|
+
key: `key = identifier / string`,
|
|
924
|
+
valueElement: `valueElement = string / number / boolean / date / time / datetime / array / object / identifier`
|
|
925
|
+
};
|
|
926
|
+
var GrammarValidator = class {
|
|
927
|
+
static validateRule(rule) {
|
|
928
|
+
const stack = [];
|
|
929
|
+
const pairs = { "(": ")", "[": "]", "{": "}" };
|
|
930
|
+
for (const char of rule) {
|
|
931
|
+
if (pairs[char]) {
|
|
932
|
+
stack.push(char);
|
|
933
|
+
} else if (Object.values(pairs).includes(char)) {
|
|
934
|
+
const open = stack.pop();
|
|
935
|
+
if (!open || pairs[open] !== char) {
|
|
936
|
+
return false;
|
|
937
|
+
}
|
|
938
|
+
}
|
|
939
|
+
}
|
|
940
|
+
return stack.length === 0;
|
|
941
|
+
}
|
|
942
|
+
static extractReferencedRules(rule) {
|
|
943
|
+
const referencedRules = [];
|
|
944
|
+
const rulePattern = /[a-zA-Z_][a-zA-Z0-9_]*/g;
|
|
945
|
+
let match;
|
|
946
|
+
while ((match = rulePattern.exec(rule)) !== null) {
|
|
947
|
+
const ruleName = match[0];
|
|
948
|
+
if (!["true", "false", "null"].includes(ruleName)) {
|
|
949
|
+
referencedRules.push(ruleName);
|
|
950
|
+
}
|
|
951
|
+
}
|
|
952
|
+
return referencedRules;
|
|
953
|
+
}
|
|
954
|
+
static hasRecursion(ruleName, grammar) {
|
|
955
|
+
const visited = /* @__PURE__ */ new Set();
|
|
956
|
+
const recursionStack = /* @__PURE__ */ new Set();
|
|
957
|
+
const check = (name) => {
|
|
958
|
+
if (recursionStack.has(name)) return true;
|
|
959
|
+
if (visited.has(name)) return false;
|
|
960
|
+
visited.add(name);
|
|
961
|
+
recursionStack.add(name);
|
|
962
|
+
const ruleContent = grammar[name];
|
|
963
|
+
if (ruleContent) {
|
|
964
|
+
if (ruleContent.includes(name)) return true;
|
|
965
|
+
for (const ref of this.extractReferencedRules(ruleContent)) {
|
|
966
|
+
if (check(ref)) return true;
|
|
967
|
+
}
|
|
968
|
+
}
|
|
969
|
+
recursionStack.delete(name);
|
|
970
|
+
return false;
|
|
971
|
+
};
|
|
972
|
+
return check(ruleName);
|
|
973
|
+
}
|
|
974
|
+
static generateSummary(grammar) {
|
|
975
|
+
const rules = Object.keys(grammar);
|
|
976
|
+
return {
|
|
977
|
+
totalRules: rules.length,
|
|
978
|
+
ruleTypes: {
|
|
979
|
+
structural: rules.filter((r) => r.includes("Block") || r.includes("Program")),
|
|
980
|
+
lexical: rules.filter((r) => r.includes("Lexicon") || r.includes("Vocabulary")),
|
|
981
|
+
syntactic: rules.filter((r) => r.includes("Syntax") || r.includes("Grammar")),
|
|
982
|
+
semantic: rules.filter((r) => r.includes("Intent") || r.includes("Mode")),
|
|
983
|
+
execution: rules.filter((r) => r.includes("Core") || r.includes("Prompt"))
|
|
984
|
+
},
|
|
985
|
+
hasRecursion: rules.some((r) => this.hasRecursion(r, grammar)),
|
|
986
|
+
validationStatus: rules.every((r) => this.validateRule(grammar[r]))
|
|
987
|
+
};
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
var baselineGrammar = {
|
|
991
|
+
rules: baselineGrammarRules,
|
|
992
|
+
validator: GrammarValidator,
|
|
993
|
+
summary: () => GrammarValidator.generateSummary(baselineGrammarRules)
|
|
994
|
+
};
|
|
995
|
+
|
|
996
|
+
// src/knowledge/knowledge-graph.ts
|
|
997
|
+
import { EventEmitter } from "events";
|
|
998
|
+
var KnowledgeGraphEngine = class extends EventEmitter {
|
|
999
|
+
config;
|
|
1000
|
+
nodes;
|
|
1001
|
+
relationships;
|
|
1002
|
+
personaKnowledge;
|
|
1003
|
+
knowledgePatterns;
|
|
1004
|
+
agents;
|
|
1005
|
+
chains;
|
|
1006
|
+
tools;
|
|
1007
|
+
metrics;
|
|
1008
|
+
constructor(config = {}) {
|
|
1009
|
+
super();
|
|
1010
|
+
this.config = {
|
|
1011
|
+
maxNodes: config.maxNodes ?? 1e4,
|
|
1012
|
+
maxRelationships: config.maxRelationships ?? 5e4,
|
|
1013
|
+
knowledgeRetention: config.knowledgeRetention ?? 0.95,
|
|
1014
|
+
reasoningDepth: config.reasoningDepth ?? 5,
|
|
1015
|
+
vectorStore: config.vectorStore ?? {}
|
|
1016
|
+
};
|
|
1017
|
+
this.nodes = /* @__PURE__ */ new Map();
|
|
1018
|
+
this.relationships = /* @__PURE__ */ new Map();
|
|
1019
|
+
this.personaKnowledge = /* @__PURE__ */ new Map();
|
|
1020
|
+
this.knowledgePatterns = /* @__PURE__ */ new Map();
|
|
1021
|
+
this.agents = /* @__PURE__ */ new Map();
|
|
1022
|
+
this.chains = /* @__PURE__ */ new Map();
|
|
1023
|
+
this.tools = /* @__PURE__ */ new Map();
|
|
1024
|
+
this.metrics = {
|
|
1025
|
+
totalNodes: 0,
|
|
1026
|
+
totalRelationships: 0,
|
|
1027
|
+
knowledgeQueries: 0,
|
|
1028
|
+
reasoningOperations: 0,
|
|
1029
|
+
lastOptimization: Date.now()
|
|
1030
|
+
};
|
|
1031
|
+
}
|
|
1032
|
+
// ── Initialization ──────────────────────────────────────────────────────────
|
|
1033
|
+
async initializeEngine() {
|
|
1034
|
+
console.log("[KnowledgeGraph] Initializing knowledge graph engine...");
|
|
1035
|
+
this.initializeKnowledgeStructures();
|
|
1036
|
+
this.initializeAgents();
|
|
1037
|
+
this.initializeChains();
|
|
1038
|
+
this.initializeTools();
|
|
1039
|
+
console.log("[KnowledgeGraph] Knowledge graph engine initialized.");
|
|
1040
|
+
this.emit("initialized", { metrics: this.metrics });
|
|
1041
|
+
}
|
|
1042
|
+
initializeKnowledgeStructures() {
|
|
1043
|
+
const rootNode = this.createNode({
|
|
1044
|
+
id: "root",
|
|
1045
|
+
type: "root",
|
|
1046
|
+
name: "Knowledge Root",
|
|
1047
|
+
level: 0,
|
|
1048
|
+
properties: { description: "Root node of the knowledge graph" },
|
|
1049
|
+
confidence: 1,
|
|
1050
|
+
tags: ["root", "system"]
|
|
1051
|
+
});
|
|
1052
|
+
const conceptNode = this.createNode({
|
|
1053
|
+
id: "concepts",
|
|
1054
|
+
type: "category",
|
|
1055
|
+
name: "Concepts",
|
|
1056
|
+
level: 1,
|
|
1057
|
+
properties: { description: "Abstract concepts and ideas" },
|
|
1058
|
+
confidence: 1,
|
|
1059
|
+
tags: ["category", "concepts"]
|
|
1060
|
+
});
|
|
1061
|
+
const entityNode = this.createNode({
|
|
1062
|
+
id: "entities",
|
|
1063
|
+
type: "category",
|
|
1064
|
+
name: "Entities",
|
|
1065
|
+
level: 1,
|
|
1066
|
+
properties: { description: "Concrete entities and objects" },
|
|
1067
|
+
confidence: 1,
|
|
1068
|
+
tags: ["category", "entities"]
|
|
1069
|
+
});
|
|
1070
|
+
const processNode = this.createNode({
|
|
1071
|
+
id: "processes",
|
|
1072
|
+
type: "category",
|
|
1073
|
+
name: "Processes",
|
|
1074
|
+
level: 1,
|
|
1075
|
+
properties: { description: "Actions, workflows, and procedures" },
|
|
1076
|
+
confidence: 1,
|
|
1077
|
+
tags: ["category", "processes"]
|
|
1078
|
+
});
|
|
1079
|
+
const relationNode = this.createNode({
|
|
1080
|
+
id: "relations",
|
|
1081
|
+
type: "category",
|
|
1082
|
+
name: "Relations",
|
|
1083
|
+
level: 1,
|
|
1084
|
+
properties: { description: "Relationships and connections" },
|
|
1085
|
+
confidence: 1,
|
|
1086
|
+
tags: ["category", "relations"]
|
|
1087
|
+
});
|
|
1088
|
+
this.createRelationship({ from: rootNode.id, to: conceptNode.id, type: "contains", weight: 1 });
|
|
1089
|
+
this.createRelationship({ from: rootNode.id, to: entityNode.id, type: "contains", weight: 1 });
|
|
1090
|
+
this.createRelationship({ from: rootNode.id, to: processNode.id, type: "contains", weight: 1 });
|
|
1091
|
+
this.createRelationship({ from: rootNode.id, to: relationNode.id, type: "contains", weight: 1 });
|
|
1092
|
+
console.log("[KnowledgeGraph] Knowledge structures initialized with root nodes.");
|
|
1093
|
+
}
|
|
1094
|
+
initializeAgents() {
|
|
1095
|
+
const agentTypes = [
|
|
1096
|
+
"knowledge-curator",
|
|
1097
|
+
"pattern-recognizer",
|
|
1098
|
+
"relationship-builder",
|
|
1099
|
+
"query-optimizer",
|
|
1100
|
+
"learning-agent"
|
|
1101
|
+
];
|
|
1102
|
+
for (const agentType of agentTypes) {
|
|
1103
|
+
this.createAgent(agentType);
|
|
1104
|
+
}
|
|
1105
|
+
console.log(`[KnowledgeGraph] Initialized ${agentTypes.length} knowledge agents.`);
|
|
1106
|
+
}
|
|
1107
|
+
initializeChains() {
|
|
1108
|
+
const chainTypes = [
|
|
1109
|
+
"knowledge-acquisition",
|
|
1110
|
+
"pattern-detection",
|
|
1111
|
+
"relationship-inference",
|
|
1112
|
+
"knowledge-optimization",
|
|
1113
|
+
"learning-pipeline"
|
|
1114
|
+
];
|
|
1115
|
+
for (const chainType of chainTypes) {
|
|
1116
|
+
this.createChain(chainType);
|
|
1117
|
+
}
|
|
1118
|
+
console.log(`[KnowledgeGraph] Initialized ${chainTypes.length} knowledge chains.`);
|
|
1119
|
+
}
|
|
1120
|
+
initializeTools() {
|
|
1121
|
+
const toolTypes = [
|
|
1122
|
+
"knowledge-query",
|
|
1123
|
+
"pattern-matcher",
|
|
1124
|
+
"relationship-analyzer",
|
|
1125
|
+
"graph-optimizer",
|
|
1126
|
+
"learning-analyzer"
|
|
1127
|
+
];
|
|
1128
|
+
for (const toolType of toolTypes) {
|
|
1129
|
+
this.createTool(toolType);
|
|
1130
|
+
}
|
|
1131
|
+
console.log(`[KnowledgeGraph] Initialized ${toolTypes.length} knowledge tools.`);
|
|
1132
|
+
}
|
|
1133
|
+
// ── Node / Relationship / Agent / Chain / Tool Creation ─────────────────────
|
|
1134
|
+
createNode(nodeData) {
|
|
1135
|
+
const id = nodeData.id ?? `node-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
1136
|
+
const node = {
|
|
1137
|
+
id,
|
|
1138
|
+
type: nodeData.type ?? "generic",
|
|
1139
|
+
name: nodeData.name ?? id,
|
|
1140
|
+
level: nodeData.level ?? 0,
|
|
1141
|
+
properties: nodeData.properties ?? {},
|
|
1142
|
+
metadata: {
|
|
1143
|
+
created: Date.now(),
|
|
1144
|
+
lastModified: Date.now(),
|
|
1145
|
+
version: 1,
|
|
1146
|
+
confidence: nodeData.confidence ?? 0.5
|
|
1147
|
+
},
|
|
1148
|
+
embeddings: nodeData.embeddings ?? [],
|
|
1149
|
+
tags: nodeData.tags ?? []
|
|
1150
|
+
};
|
|
1151
|
+
if (this.nodes.size >= this.config.maxNodes) {
|
|
1152
|
+
this.pruneNodes();
|
|
1153
|
+
}
|
|
1154
|
+
this.nodes.set(id, node);
|
|
1155
|
+
this.metrics.totalNodes = this.nodes.size;
|
|
1156
|
+
this.emit("nodeCreated", { node });
|
|
1157
|
+
return node;
|
|
1158
|
+
}
|
|
1159
|
+
createRelationship(relData) {
|
|
1160
|
+
const id = relData.id ?? `rel-${Date.now()}-${Math.random().toString(36).slice(2, 9)}`;
|
|
1161
|
+
const relationship = {
|
|
1162
|
+
id,
|
|
1163
|
+
from: relData.from,
|
|
1164
|
+
to: relData.to,
|
|
1165
|
+
type: relData.type ?? "related",
|
|
1166
|
+
weight: relData.weight ?? 0.5,
|
|
1167
|
+
properties: relData.properties ?? {},
|
|
1168
|
+
metadata: {
|
|
1169
|
+
created: Date.now(),
|
|
1170
|
+
lastModified: Date.now(),
|
|
1171
|
+
confidence: relData.confidence ?? 0.5
|
|
1172
|
+
}
|
|
1173
|
+
};
|
|
1174
|
+
if (this.relationships.size >= this.config.maxRelationships) {
|
|
1175
|
+
this.pruneRelationships();
|
|
1176
|
+
}
|
|
1177
|
+
this.relationships.set(id, relationship);
|
|
1178
|
+
this.metrics.totalRelationships = this.relationships.size;
|
|
1179
|
+
this.emit("relationshipCreated", { relationship });
|
|
1180
|
+
return relationship;
|
|
1181
|
+
}
|
|
1182
|
+
createAgent(agentType) {
|
|
1183
|
+
const id = `agent-${agentType}-${Date.now()}`;
|
|
1184
|
+
const agent = {
|
|
1185
|
+
id,
|
|
1186
|
+
type: agentType,
|
|
1187
|
+
capabilities: this.getAgentCapabilities(agentType),
|
|
1188
|
+
state: "idle",
|
|
1189
|
+
knowledge: /* @__PURE__ */ new Map(),
|
|
1190
|
+
reasoning: this.createReasoningEngine(agentType),
|
|
1191
|
+
tools: this.getAgentTools(agentType)
|
|
1192
|
+
};
|
|
1193
|
+
this.agents.set(id, agent);
|
|
1194
|
+
this.emit("agentCreated", { agent });
|
|
1195
|
+
return agent;
|
|
1196
|
+
}
|
|
1197
|
+
createChain(chainType) {
|
|
1198
|
+
const id = `chain-${chainType}-${Date.now()}`;
|
|
1199
|
+
const chain = {
|
|
1200
|
+
id,
|
|
1201
|
+
type: chainType,
|
|
1202
|
+
steps: this.getChainSteps(chainType),
|
|
1203
|
+
state: "idle",
|
|
1204
|
+
executionHistory: [],
|
|
1205
|
+
performance: {
|
|
1206
|
+
totalExecutions: 0,
|
|
1207
|
+
averageExecutionTime: 0,
|
|
1208
|
+
successRate: 1
|
|
1209
|
+
}
|
|
1210
|
+
};
|
|
1211
|
+
this.chains.set(id, chain);
|
|
1212
|
+
this.emit("chainCreated", { chain });
|
|
1213
|
+
return chain;
|
|
1214
|
+
}
|
|
1215
|
+
createTool(toolType) {
|
|
1216
|
+
const id = `tool-${toolType}-${Date.now()}`;
|
|
1217
|
+
const tool = {
|
|
1218
|
+
id,
|
|
1219
|
+
type: toolType,
|
|
1220
|
+
capabilities: this.getToolCapabilities(toolType),
|
|
1221
|
+
state: "ready",
|
|
1222
|
+
usage: {
|
|
1223
|
+
totalUses: 0,
|
|
1224
|
+
lastUsed: null,
|
|
1225
|
+
successRate: 1
|
|
1226
|
+
}
|
|
1227
|
+
};
|
|
1228
|
+
this.tools.set(id, tool);
|
|
1229
|
+
this.emit("toolCreated", { tool });
|
|
1230
|
+
return tool;
|
|
1231
|
+
}
|
|
1232
|
+
// ── Querying ────────────────────────────────────────────────────────────────
|
|
1233
|
+
async queryKnowledge(query, options = {}) {
|
|
1234
|
+
this.metrics.knowledgeQueries++;
|
|
1235
|
+
const queryType = options.type ?? "semantic";
|
|
1236
|
+
const depth = options.depth ?? this.config.reasoningDepth;
|
|
1237
|
+
try {
|
|
1238
|
+
let results;
|
|
1239
|
+
switch (queryType) {
|
|
1240
|
+
case "semantic":
|
|
1241
|
+
results = this.semanticQuery(query, depth);
|
|
1242
|
+
break;
|
|
1243
|
+
case "pattern":
|
|
1244
|
+
results = this.patternQuery(query, depth);
|
|
1245
|
+
break;
|
|
1246
|
+
case "relationship":
|
|
1247
|
+
results = this.relationshipQuery(query, depth);
|
|
1248
|
+
break;
|
|
1249
|
+
case "persona":
|
|
1250
|
+
results = this.personaQuery(query, depth, options.persona);
|
|
1251
|
+
break;
|
|
1252
|
+
default:
|
|
1253
|
+
results = this.semanticQuery(query, depth);
|
|
1254
|
+
}
|
|
1255
|
+
const reasonedResults = this.applyReasoning(results, query, depth);
|
|
1256
|
+
this.emit("queryCompleted", { query, results: reasonedResults });
|
|
1257
|
+
return {
|
|
1258
|
+
success: true,
|
|
1259
|
+
query,
|
|
1260
|
+
results: reasonedResults,
|
|
1261
|
+
metadata: {
|
|
1262
|
+
type: queryType,
|
|
1263
|
+
depth,
|
|
1264
|
+
totalResults: reasonedResults.length,
|
|
1265
|
+
queryTime: Date.now()
|
|
1266
|
+
}
|
|
1267
|
+
};
|
|
1268
|
+
} catch (error) {
|
|
1269
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1270
|
+
console.log(`[KnowledgeGraph] Query failed: ${message}`);
|
|
1271
|
+
return {
|
|
1272
|
+
success: false,
|
|
1273
|
+
query,
|
|
1274
|
+
error: message
|
|
1275
|
+
};
|
|
1276
|
+
}
|
|
1277
|
+
}
|
|
1278
|
+
semanticQuery(query, depth) {
|
|
1279
|
+
const results = [];
|
|
1280
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1281
|
+
const queue = [];
|
|
1282
|
+
for (const [nodeId, node] of this.nodes) {
|
|
1283
|
+
if (node.level === 0 || node.level === 1) {
|
|
1284
|
+
queue.push({ nodeId, currentDepth: 0 });
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
while (queue.length > 0) {
|
|
1288
|
+
const item = queue.shift();
|
|
1289
|
+
if (!item) break;
|
|
1290
|
+
const { nodeId, currentDepth } = item;
|
|
1291
|
+
if (visited.has(nodeId) || currentDepth > depth) continue;
|
|
1292
|
+
visited.add(nodeId);
|
|
1293
|
+
const node = this.nodes.get(nodeId);
|
|
1294
|
+
if (!node) continue;
|
|
1295
|
+
if (this.matchesQuery(node, query)) {
|
|
1296
|
+
const relevance = this.calculateRelevance(node, query);
|
|
1297
|
+
results.push({
|
|
1298
|
+
node,
|
|
1299
|
+
relevance,
|
|
1300
|
+
depth: currentDepth,
|
|
1301
|
+
path: this.findPathToNode(nodeId)
|
|
1302
|
+
});
|
|
1303
|
+
}
|
|
1304
|
+
const connected = this.getConnectedNodes(nodeId);
|
|
1305
|
+
for (const connectedId of connected) {
|
|
1306
|
+
if (!visited.has(connectedId)) {
|
|
1307
|
+
queue.push({ nodeId: connectedId, currentDepth: currentDepth + 1 });
|
|
1308
|
+
}
|
|
1309
|
+
}
|
|
1310
|
+
}
|
|
1311
|
+
results.sort((a, b) => {
|
|
1312
|
+
const aRel = a.relevance;
|
|
1313
|
+
const bRel = b.relevance;
|
|
1314
|
+
return bRel - aRel;
|
|
1315
|
+
});
|
|
1316
|
+
return results;
|
|
1317
|
+
}
|
|
1318
|
+
patternQuery(query, depth) {
|
|
1319
|
+
const results = [];
|
|
1320
|
+
for (const [patternKey, patterns] of this.knowledgePatterns) {
|
|
1321
|
+
const lowerKey = patternKey.toLowerCase();
|
|
1322
|
+
const lowerQuery = query.toLowerCase();
|
|
1323
|
+
if (lowerKey.includes(lowerQuery) || lowerQuery.includes(lowerKey)) {
|
|
1324
|
+
for (const pattern of patterns) {
|
|
1325
|
+
results.push({
|
|
1326
|
+
pattern,
|
|
1327
|
+
patternKey,
|
|
1328
|
+
relevance: this.calculateStringRelevance(patternKey, query),
|
|
1329
|
+
depth: 0
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
}
|
|
1333
|
+
}
|
|
1334
|
+
for (const [, node] of this.nodes) {
|
|
1335
|
+
if (node.tags.some((tag) => query.toLowerCase().includes(tag.toLowerCase()))) {
|
|
1336
|
+
results.push({
|
|
1337
|
+
node,
|
|
1338
|
+
relevance: this.calculateRelevance(node, query),
|
|
1339
|
+
depth: 0,
|
|
1340
|
+
matchType: "tag-pattern"
|
|
1341
|
+
});
|
|
1342
|
+
}
|
|
1343
|
+
}
|
|
1344
|
+
results.sort((a, b) => {
|
|
1345
|
+
const aRel = a.relevance;
|
|
1346
|
+
const bRel = b.relevance;
|
|
1347
|
+
return bRel - aRel;
|
|
1348
|
+
});
|
|
1349
|
+
return results.slice(0, depth * 10);
|
|
1350
|
+
}
|
|
1351
|
+
relationshipQuery(query, depth) {
|
|
1352
|
+
const results = [];
|
|
1353
|
+
for (const [, relationship] of this.relationships) {
|
|
1354
|
+
const fromNode = this.nodes.get(relationship.from);
|
|
1355
|
+
const toNode = this.nodes.get(relationship.to);
|
|
1356
|
+
if (!fromNode || !toNode) continue;
|
|
1357
|
+
const fromMatch = this.matchesQuery(fromNode, query);
|
|
1358
|
+
const toMatch = this.matchesQuery(toNode, query);
|
|
1359
|
+
if (fromMatch || toMatch) {
|
|
1360
|
+
results.push({
|
|
1361
|
+
relationship,
|
|
1362
|
+
fromNode,
|
|
1363
|
+
toNode,
|
|
1364
|
+
relevance: (this.calculateRelevance(fromNode, query) + this.calculateRelevance(toNode, query)) / 2,
|
|
1365
|
+
depth: 0
|
|
1366
|
+
});
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
results.sort((a, b) => {
|
|
1370
|
+
const aRel = a.relevance;
|
|
1371
|
+
const bRel = b.relevance;
|
|
1372
|
+
return bRel - aRel;
|
|
1373
|
+
});
|
|
1374
|
+
return results.slice(0, depth * 10);
|
|
1375
|
+
}
|
|
1376
|
+
personaQuery(query, depth, persona) {
|
|
1377
|
+
const results = [];
|
|
1378
|
+
if (persona && this.personaKnowledge.has(persona)) {
|
|
1379
|
+
const pKnowledge = this.personaKnowledge.get(persona);
|
|
1380
|
+
for (const [key, value] of pKnowledge) {
|
|
1381
|
+
if (key.toLowerCase().includes(query.toLowerCase()) || query.toLowerCase().includes(key.toLowerCase())) {
|
|
1382
|
+
results.push({
|
|
1383
|
+
key,
|
|
1384
|
+
value,
|
|
1385
|
+
persona,
|
|
1386
|
+
relevance: this.calculateStringRelevance(key, query),
|
|
1387
|
+
depth: 0
|
|
1388
|
+
});
|
|
1389
|
+
}
|
|
1390
|
+
}
|
|
1391
|
+
} else {
|
|
1392
|
+
for (const [personaId, pKnowledge] of this.personaKnowledge) {
|
|
1393
|
+
for (const [key, value] of pKnowledge) {
|
|
1394
|
+
if (key.toLowerCase().includes(query.toLowerCase()) || query.toLowerCase().includes(key.toLowerCase())) {
|
|
1395
|
+
results.push({
|
|
1396
|
+
key,
|
|
1397
|
+
value,
|
|
1398
|
+
persona: personaId,
|
|
1399
|
+
relevance: this.calculateStringRelevance(key, query),
|
|
1400
|
+
depth: 0
|
|
1401
|
+
});
|
|
1402
|
+
}
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
}
|
|
1406
|
+
results.sort((a, b) => {
|
|
1407
|
+
const aRel = a.relevance;
|
|
1408
|
+
const bRel = b.relevance;
|
|
1409
|
+
return bRel - aRel;
|
|
1410
|
+
});
|
|
1411
|
+
return results.slice(0, depth * 10);
|
|
1412
|
+
}
|
|
1413
|
+
// ── Reasoning ───────────────────────────────────────────────────────────────
|
|
1414
|
+
applyReasoning(results, query, depth) {
|
|
1415
|
+
this.metrics.reasoningOperations++;
|
|
1416
|
+
if (results.length === 0) return results;
|
|
1417
|
+
const filtered = results.filter((result) => {
|
|
1418
|
+
const r = result;
|
|
1419
|
+
const relevance = r.relevance ?? 0;
|
|
1420
|
+
return relevance >= 1 - this.config.knowledgeRetention;
|
|
1421
|
+
});
|
|
1422
|
+
const weighted = filtered.map((result) => {
|
|
1423
|
+
const r = result;
|
|
1424
|
+
const d = r.depth ?? 0;
|
|
1425
|
+
const relevance = r.relevance ?? 0;
|
|
1426
|
+
const depthWeight = 1 / (1 + d * 0.1);
|
|
1427
|
+
return {
|
|
1428
|
+
...r,
|
|
1429
|
+
adjustedRelevance: relevance * depthWeight
|
|
1430
|
+
};
|
|
1431
|
+
});
|
|
1432
|
+
weighted.sort((a, b) => {
|
|
1433
|
+
return b.adjustedRelevance - a.adjustedRelevance;
|
|
1434
|
+
});
|
|
1435
|
+
const limited = weighted.slice(0, depth * 20);
|
|
1436
|
+
this.emit("reasoningApplied", { query, inputCount: results.length, outputCount: limited.length });
|
|
1437
|
+
return limited;
|
|
1438
|
+
}
|
|
1439
|
+
// ── Chain Execution ─────────────────────────────────────────────────────────
|
|
1440
|
+
async executeChain(chain, input) {
|
|
1441
|
+
const startTime = Date.now();
|
|
1442
|
+
chain.state = "executing";
|
|
1443
|
+
let currentInput = input;
|
|
1444
|
+
try {
|
|
1445
|
+
for (const step of chain.steps) {
|
|
1446
|
+
currentInput = await this.executeStep(step, currentInput);
|
|
1447
|
+
}
|
|
1448
|
+
const executionTime = Date.now() - startTime;
|
|
1449
|
+
chain.executionHistory.push({
|
|
1450
|
+
input,
|
|
1451
|
+
output: currentInput,
|
|
1452
|
+
executionTime,
|
|
1453
|
+
timestamp: Date.now()
|
|
1454
|
+
});
|
|
1455
|
+
chain.performance.totalExecutions++;
|
|
1456
|
+
chain.performance.averageExecutionTime = (chain.performance.averageExecutionTime * (chain.performance.totalExecutions - 1) + executionTime) / chain.performance.totalExecutions;
|
|
1457
|
+
chain.performance.successRate = (chain.performance.successRate * (chain.performance.totalExecutions - 1) + 1) / chain.performance.totalExecutions;
|
|
1458
|
+
chain.state = "idle";
|
|
1459
|
+
this.emit("chainExecuted", { chainId: chain.id, executionTime });
|
|
1460
|
+
return currentInput;
|
|
1461
|
+
} catch (error) {
|
|
1462
|
+
chain.state = "error";
|
|
1463
|
+
chain.performance.totalExecutions++;
|
|
1464
|
+
chain.performance.successRate = chain.performance.successRate * (chain.performance.totalExecutions - 1) / chain.performance.totalExecutions;
|
|
1465
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1466
|
+
this.emit("chainError", { chainId: chain.id, error: message });
|
|
1467
|
+
throw error;
|
|
1468
|
+
}
|
|
1469
|
+
}
|
|
1470
|
+
async executeStep(step, input) {
|
|
1471
|
+
const toolId = step.tool;
|
|
1472
|
+
let tool;
|
|
1473
|
+
for (const [, t] of this.tools) {
|
|
1474
|
+
if (t.type === toolId || t.id === toolId) {
|
|
1475
|
+
tool = t;
|
|
1476
|
+
break;
|
|
1477
|
+
}
|
|
1478
|
+
}
|
|
1479
|
+
if (!tool) {
|
|
1480
|
+
console.log(`[KnowledgeGraph] Tool not found for step: ${toolId}, passing input through.`);
|
|
1481
|
+
return input;
|
|
1482
|
+
}
|
|
1483
|
+
return this.executeTool(tool, step, input);
|
|
1484
|
+
}
|
|
1485
|
+
async executeTool(tool, step, input) {
|
|
1486
|
+
tool.state = "executing";
|
|
1487
|
+
tool.usage.totalUses++;
|
|
1488
|
+
tool.usage.lastUsed = Date.now();
|
|
1489
|
+
let result;
|
|
1490
|
+
try {
|
|
1491
|
+
switch (tool.type) {
|
|
1492
|
+
case "knowledge-query":
|
|
1493
|
+
result = this.executeKnowledgeQuery(input, step.parameters);
|
|
1494
|
+
break;
|
|
1495
|
+
case "pattern-matcher":
|
|
1496
|
+
result = this.executePatternMatching(input, step.parameters);
|
|
1497
|
+
break;
|
|
1498
|
+
case "relationship-analyzer":
|
|
1499
|
+
result = this.executeRelationshipBuilding(input, step.parameters);
|
|
1500
|
+
break;
|
|
1501
|
+
case "graph-optimizer":
|
|
1502
|
+
result = this.executeOptimization(input, step.parameters);
|
|
1503
|
+
break;
|
|
1504
|
+
case "learning-analyzer":
|
|
1505
|
+
result = this.executeLearningAnalysis(input, step.parameters);
|
|
1506
|
+
break;
|
|
1507
|
+
default:
|
|
1508
|
+
result = input;
|
|
1509
|
+
}
|
|
1510
|
+
tool.state = "ready";
|
|
1511
|
+
tool.usage.successRate = (tool.usage.successRate * (tool.usage.totalUses - 1) + 1) / tool.usage.totalUses;
|
|
1512
|
+
this.emit("toolExecuted", { toolId: tool.id, toolType: tool.type });
|
|
1513
|
+
return result;
|
|
1514
|
+
} catch (error) {
|
|
1515
|
+
tool.state = "error";
|
|
1516
|
+
tool.usage.successRate = tool.usage.successRate * (tool.usage.totalUses - 1) / tool.usage.totalUses;
|
|
1517
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1518
|
+
this.emit("toolError", { toolId: tool.id, error: message });
|
|
1519
|
+
throw error;
|
|
1520
|
+
}
|
|
1521
|
+
}
|
|
1522
|
+
// ── Query Matching & Relevance ──────────────────────────────────────────────
|
|
1523
|
+
matchesQuery(node, query) {
|
|
1524
|
+
const lowerQuery = query.toLowerCase();
|
|
1525
|
+
const lowerName = node.name.toLowerCase();
|
|
1526
|
+
if (lowerName.includes(lowerQuery) || lowerQuery.includes(lowerName)) {
|
|
1527
|
+
return true;
|
|
1528
|
+
}
|
|
1529
|
+
const description = node.properties.description ?? "";
|
|
1530
|
+
if (description.toLowerCase().includes(lowerQuery)) {
|
|
1531
|
+
return true;
|
|
1532
|
+
}
|
|
1533
|
+
for (const tag of node.tags) {
|
|
1534
|
+
if (tag.toLowerCase().includes(lowerQuery) || lowerQuery.includes(tag.toLowerCase())) {
|
|
1535
|
+
return true;
|
|
1536
|
+
}
|
|
1537
|
+
}
|
|
1538
|
+
return false;
|
|
1539
|
+
}
|
|
1540
|
+
calculateRelevance(node, query) {
|
|
1541
|
+
let relevance = 0;
|
|
1542
|
+
const lowerQuery = query.toLowerCase();
|
|
1543
|
+
const lowerName = node.name.toLowerCase();
|
|
1544
|
+
if (lowerName === lowerQuery) {
|
|
1545
|
+
relevance += 1;
|
|
1546
|
+
} else if (lowerName.includes(lowerQuery)) {
|
|
1547
|
+
relevance += 0.7;
|
|
1548
|
+
} else if (lowerQuery.includes(lowerName)) {
|
|
1549
|
+
relevance += 0.5;
|
|
1550
|
+
}
|
|
1551
|
+
const description = node.properties.description ?? "";
|
|
1552
|
+
if (description.toLowerCase().includes(lowerQuery)) {
|
|
1553
|
+
relevance += 0.3;
|
|
1554
|
+
}
|
|
1555
|
+
for (const tag of node.tags) {
|
|
1556
|
+
if (tag.toLowerCase() === lowerQuery) {
|
|
1557
|
+
relevance += 0.4;
|
|
1558
|
+
} else if (tag.toLowerCase().includes(lowerQuery) || lowerQuery.includes(tag.toLowerCase())) {
|
|
1559
|
+
relevance += 0.2;
|
|
1560
|
+
}
|
|
1561
|
+
}
|
|
1562
|
+
relevance *= node.metadata.confidence;
|
|
1563
|
+
return Math.min(1, Math.max(0, relevance));
|
|
1564
|
+
}
|
|
1565
|
+
calculateStringRelevance(str, query) {
|
|
1566
|
+
const lowerStr = str.toLowerCase();
|
|
1567
|
+
const lowerQuery = query.toLowerCase();
|
|
1568
|
+
if (lowerStr === lowerQuery) return 1;
|
|
1569
|
+
if (lowerStr.includes(lowerQuery)) return 0.7;
|
|
1570
|
+
if (lowerQuery.includes(lowerStr)) return 0.5;
|
|
1571
|
+
const strTokens = lowerStr.split(/\s+/);
|
|
1572
|
+
const queryTokens = lowerQuery.split(/\s+/);
|
|
1573
|
+
let overlapCount = 0;
|
|
1574
|
+
for (const qt of queryTokens) {
|
|
1575
|
+
if (strTokens.some((st) => st.includes(qt) || qt.includes(st))) {
|
|
1576
|
+
overlapCount++;
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
return queryTokens.length > 0 ? overlapCount / queryTokens.length * 0.5 : 0;
|
|
1580
|
+
}
|
|
1581
|
+
// ── Graph Traversal ─────────────────────────────────────────────────────────
|
|
1582
|
+
findPathToNode(nodeId) {
|
|
1583
|
+
const path = [];
|
|
1584
|
+
const visited = /* @__PURE__ */ new Set();
|
|
1585
|
+
const queue = [{ id: "root", trail: ["root"] }];
|
|
1586
|
+
if (nodeId === "root") return ["root"];
|
|
1587
|
+
while (queue.length > 0) {
|
|
1588
|
+
const item = queue.shift();
|
|
1589
|
+
if (!item) break;
|
|
1590
|
+
const { id, trail } = item;
|
|
1591
|
+
if (visited.has(id)) continue;
|
|
1592
|
+
visited.add(id);
|
|
1593
|
+
if (id === nodeId) {
|
|
1594
|
+
return trail;
|
|
1595
|
+
}
|
|
1596
|
+
const connected = this.getConnectedNodes(id);
|
|
1597
|
+
for (const connectedId of connected) {
|
|
1598
|
+
if (!visited.has(connectedId)) {
|
|
1599
|
+
queue.push({ id: connectedId, trail: [...trail, connectedId] });
|
|
1600
|
+
}
|
|
1601
|
+
}
|
|
1602
|
+
}
|
|
1603
|
+
return path.length > 0 ? path : [nodeId];
|
|
1604
|
+
}
|
|
1605
|
+
getConnectedNodes(nodeId) {
|
|
1606
|
+
const connected = [];
|
|
1607
|
+
for (const [, relationship] of this.relationships) {
|
|
1608
|
+
if (relationship.from === nodeId) {
|
|
1609
|
+
connected.push(relationship.to);
|
|
1610
|
+
} else if (relationship.to === nodeId) {
|
|
1611
|
+
connected.push(relationship.from);
|
|
1612
|
+
}
|
|
1613
|
+
}
|
|
1614
|
+
return connected;
|
|
1615
|
+
}
|
|
1616
|
+
// ── Lookup Tables ───────────────────────────────────────────────────────────
|
|
1617
|
+
getAgentCapabilities(agentType) {
|
|
1618
|
+
const capabilities = {
|
|
1619
|
+
"knowledge-curator": [
|
|
1620
|
+
"knowledge-acquisition",
|
|
1621
|
+
"knowledge-validation",
|
|
1622
|
+
"knowledge-organization",
|
|
1623
|
+
"knowledge-deduplication"
|
|
1624
|
+
],
|
|
1625
|
+
"pattern-recognizer": [
|
|
1626
|
+
"pattern-detection",
|
|
1627
|
+
"pattern-classification",
|
|
1628
|
+
"anomaly-detection",
|
|
1629
|
+
"trend-analysis"
|
|
1630
|
+
],
|
|
1631
|
+
"relationship-builder": [
|
|
1632
|
+
"relationship-inference",
|
|
1633
|
+
"relationship-validation",
|
|
1634
|
+
"relationship-strengthening",
|
|
1635
|
+
"relationship-pruning"
|
|
1636
|
+
],
|
|
1637
|
+
"query-optimizer": [
|
|
1638
|
+
"query-planning",
|
|
1639
|
+
"query-caching",
|
|
1640
|
+
"query-optimization",
|
|
1641
|
+
"result-ranking"
|
|
1642
|
+
],
|
|
1643
|
+
"learning-agent": [
|
|
1644
|
+
"supervised-learning",
|
|
1645
|
+
"unsupervised-learning",
|
|
1646
|
+
"reinforcement-learning",
|
|
1647
|
+
"transfer-learning"
|
|
1648
|
+
]
|
|
1649
|
+
};
|
|
1650
|
+
return capabilities[agentType] ?? ["generic-capability"];
|
|
1651
|
+
}
|
|
1652
|
+
createReasoningEngine(agentType) {
|
|
1653
|
+
return {
|
|
1654
|
+
type: agentType,
|
|
1655
|
+
rules: this.getReasoningRules(agentType),
|
|
1656
|
+
context: /* @__PURE__ */ new Map(),
|
|
1657
|
+
history: []
|
|
1658
|
+
};
|
|
1659
|
+
}
|
|
1660
|
+
getReasoningRules(agentType) {
|
|
1661
|
+
const rules = {
|
|
1662
|
+
"knowledge-curator": [
|
|
1663
|
+
"validate-source-credibility",
|
|
1664
|
+
"check-knowledge-consistency",
|
|
1665
|
+
"merge-duplicate-knowledge",
|
|
1666
|
+
"maintain-knowledge-hierarchy"
|
|
1667
|
+
],
|
|
1668
|
+
"pattern-recognizer": [
|
|
1669
|
+
"identify-recurring-patterns",
|
|
1670
|
+
"classify-pattern-types",
|
|
1671
|
+
"detect-anomalies",
|
|
1672
|
+
"track-pattern-evolution"
|
|
1673
|
+
],
|
|
1674
|
+
"relationship-builder": [
|
|
1675
|
+
"infer-implicit-relationships",
|
|
1676
|
+
"validate-relationship-consistency",
|
|
1677
|
+
"strengthen-confirmed-relationships",
|
|
1678
|
+
"prune-weak-relationships"
|
|
1679
|
+
],
|
|
1680
|
+
"query-optimizer": [
|
|
1681
|
+
"analyze-query-complexity",
|
|
1682
|
+
"select-optimal-traversal",
|
|
1683
|
+
"cache-frequent-queries",
|
|
1684
|
+
"rank-results-by-relevance"
|
|
1685
|
+
],
|
|
1686
|
+
"learning-agent": [
|
|
1687
|
+
"extract-features",
|
|
1688
|
+
"update-model-weights",
|
|
1689
|
+
"evaluate-performance",
|
|
1690
|
+
"adapt-learning-rate"
|
|
1691
|
+
]
|
|
1692
|
+
};
|
|
1693
|
+
return rules[agentType] ?? ["generic-rule"];
|
|
1694
|
+
}
|
|
1695
|
+
getAgentTools(agentType) {
|
|
1696
|
+
const tools = {
|
|
1697
|
+
"knowledge-curator": ["knowledge-query", "pattern-matcher"],
|
|
1698
|
+
"pattern-recognizer": ["pattern-matcher", "learning-analyzer"],
|
|
1699
|
+
"relationship-builder": ["relationship-analyzer", "knowledge-query"],
|
|
1700
|
+
"query-optimizer": ["knowledge-query", "graph-optimizer"],
|
|
1701
|
+
"learning-agent": ["learning-analyzer", "pattern-matcher"]
|
|
1702
|
+
};
|
|
1703
|
+
return tools[agentType] ?? ["knowledge-query"];
|
|
1704
|
+
}
|
|
1705
|
+
getChainSteps(chainType) {
|
|
1706
|
+
const steps = {
|
|
1707
|
+
"knowledge-acquisition": [
|
|
1708
|
+
{ tool: "knowledge-query", parameters: { action: "extract" } },
|
|
1709
|
+
{ tool: "pattern-matcher", parameters: { action: "validate" } },
|
|
1710
|
+
{ tool: "knowledge-query", parameters: { action: "store" } }
|
|
1711
|
+
],
|
|
1712
|
+
"pattern-detection": [
|
|
1713
|
+
{ tool: "knowledge-query", parameters: { action: "gather" } },
|
|
1714
|
+
{ tool: "pattern-matcher", parameters: { action: "detect" } },
|
|
1715
|
+
{ tool: "learning-analyzer", parameters: { action: "classify" } }
|
|
1716
|
+
],
|
|
1717
|
+
"relationship-inference": [
|
|
1718
|
+
{ tool: "knowledge-query", parameters: { action: "gather" } },
|
|
1719
|
+
{ tool: "relationship-analyzer", parameters: { action: "infer" } },
|
|
1720
|
+
{ tool: "relationship-analyzer", parameters: { action: "validate" } }
|
|
1721
|
+
],
|
|
1722
|
+
"knowledge-optimization": [
|
|
1723
|
+
{ tool: "knowledge-query", parameters: { action: "analyze" } },
|
|
1724
|
+
{ tool: "graph-optimizer", parameters: { action: "optimize" } },
|
|
1725
|
+
{ tool: "graph-optimizer", parameters: { action: "verify" } }
|
|
1726
|
+
],
|
|
1727
|
+
"learning-pipeline": [
|
|
1728
|
+
{ tool: "knowledge-query", parameters: { action: "extract" } },
|
|
1729
|
+
{ tool: "learning-analyzer", parameters: { action: "learn" } },
|
|
1730
|
+
{ tool: "learning-analyzer", parameters: { action: "evaluate" } }
|
|
1731
|
+
]
|
|
1732
|
+
};
|
|
1733
|
+
return steps[chainType] ?? [{ tool: "knowledge-query", parameters: { action: "default" } }];
|
|
1734
|
+
}
|
|
1735
|
+
getToolCapabilities(toolType) {
|
|
1736
|
+
const capabilities = {
|
|
1737
|
+
"knowledge-query": [
|
|
1738
|
+
"semantic-search",
|
|
1739
|
+
"pattern-search",
|
|
1740
|
+
"relationship-search",
|
|
1741
|
+
"full-text-search"
|
|
1742
|
+
],
|
|
1743
|
+
"pattern-matcher": [
|
|
1744
|
+
"exact-match",
|
|
1745
|
+
"fuzzy-match",
|
|
1746
|
+
"regex-match",
|
|
1747
|
+
"semantic-match"
|
|
1748
|
+
],
|
|
1749
|
+
"relationship-analyzer": [
|
|
1750
|
+
"path-finding",
|
|
1751
|
+
"cluster-detection",
|
|
1752
|
+
"centrality-analysis",
|
|
1753
|
+
"community-detection"
|
|
1754
|
+
],
|
|
1755
|
+
"graph-optimizer": [
|
|
1756
|
+
"node-pruning",
|
|
1757
|
+
"relationship-pruning",
|
|
1758
|
+
"index-optimization",
|
|
1759
|
+
"cache-optimization"
|
|
1760
|
+
],
|
|
1761
|
+
"learning-analyzer": [
|
|
1762
|
+
"feature-extraction",
|
|
1763
|
+
"model-training",
|
|
1764
|
+
"performance-evaluation",
|
|
1765
|
+
"prediction"
|
|
1766
|
+
]
|
|
1767
|
+
};
|
|
1768
|
+
return capabilities[toolType] ?? ["generic-capability"];
|
|
1769
|
+
}
|
|
1770
|
+
// ── Tool Execution Implementations ──────────────────────────────────────────
|
|
1771
|
+
executeKnowledgeQuery(input, parameters) {
|
|
1772
|
+
console.log(`[KnowledgeGraph] Executing knowledge query with action: ${parameters.action}`);
|
|
1773
|
+
return input;
|
|
1774
|
+
}
|
|
1775
|
+
executePatternMatching(input, parameters) {
|
|
1776
|
+
console.log(`[KnowledgeGraph] Executing pattern matching with action: ${parameters.action}`);
|
|
1777
|
+
return input;
|
|
1778
|
+
}
|
|
1779
|
+
executeRelationshipBuilding(input, parameters) {
|
|
1780
|
+
console.log(`[KnowledgeGraph] Executing relationship building with action: ${parameters.action}`);
|
|
1781
|
+
return input;
|
|
1782
|
+
}
|
|
1783
|
+
executeOptimization(input, parameters) {
|
|
1784
|
+
console.log(`[KnowledgeGraph] Executing optimization with action: ${parameters.action}`);
|
|
1785
|
+
return input;
|
|
1786
|
+
}
|
|
1787
|
+
executeLearningAnalysis(input, parameters) {
|
|
1788
|
+
console.log(`[KnowledgeGraph] Executing learning analysis with action: ${parameters.action}`);
|
|
1789
|
+
return input;
|
|
1790
|
+
}
|
|
1791
|
+
// ── Pruning ─────────────────────────────────────────────────────────────────
|
|
1792
|
+
pruneNodes() {
|
|
1793
|
+
const threshold = 1 - this.config.knowledgeRetention;
|
|
1794
|
+
const toRemove = [];
|
|
1795
|
+
for (const [nodeId, node] of this.nodes) {
|
|
1796
|
+
if (node.metadata.confidence < threshold && node.type !== "root" && node.type !== "category") {
|
|
1797
|
+
toRemove.push(nodeId);
|
|
1798
|
+
}
|
|
1799
|
+
}
|
|
1800
|
+
toRemove.sort((a, b) => {
|
|
1801
|
+
const aConf = this.nodes.get(a)?.metadata.confidence ?? 0;
|
|
1802
|
+
const bConf = this.nodes.get(b)?.metadata.confidence ?? 0;
|
|
1803
|
+
return aConf - bConf;
|
|
1804
|
+
});
|
|
1805
|
+
const removeCount = Math.min(toRemove.length, Math.floor(this.config.maxNodes * 0.1));
|
|
1806
|
+
for (let i = 0; i < removeCount; i++) {
|
|
1807
|
+
this.nodes.delete(toRemove[i]);
|
|
1808
|
+
for (const [relId, rel] of this.relationships) {
|
|
1809
|
+
if (rel.from === toRemove[i] || rel.to === toRemove[i]) {
|
|
1810
|
+
this.relationships.delete(relId);
|
|
1811
|
+
}
|
|
1812
|
+
}
|
|
1813
|
+
}
|
|
1814
|
+
this.metrics.totalNodes = this.nodes.size;
|
|
1815
|
+
this.metrics.totalRelationships = this.relationships.size;
|
|
1816
|
+
console.log(`[KnowledgeGraph] Pruned ${removeCount} low-confidence nodes.`);
|
|
1817
|
+
}
|
|
1818
|
+
pruneRelationships() {
|
|
1819
|
+
const toRemove = [];
|
|
1820
|
+
for (const [relId, rel] of this.relationships) {
|
|
1821
|
+
if (rel.metadata.confidence < 1 - this.config.knowledgeRetention) {
|
|
1822
|
+
toRemove.push(relId);
|
|
1823
|
+
}
|
|
1824
|
+
}
|
|
1825
|
+
toRemove.sort((a, b) => {
|
|
1826
|
+
const aConf = this.relationships.get(a)?.metadata.confidence ?? 0;
|
|
1827
|
+
const bConf = this.relationships.get(b)?.metadata.confidence ?? 0;
|
|
1828
|
+
return aConf - bConf;
|
|
1829
|
+
});
|
|
1830
|
+
const removeCount = Math.min(toRemove.length, Math.floor(this.config.maxRelationships * 0.1));
|
|
1831
|
+
for (let i = 0; i < removeCount; i++) {
|
|
1832
|
+
this.relationships.delete(toRemove[i]);
|
|
1833
|
+
}
|
|
1834
|
+
this.metrics.totalRelationships = this.relationships.size;
|
|
1835
|
+
console.log(`[KnowledgeGraph] Pruned ${removeCount} weak relationships.`);
|
|
1836
|
+
}
|
|
1837
|
+
// ── Status & Optimization ──────────────────────────────────────────────────
|
|
1838
|
+
getStatus() {
|
|
1839
|
+
return {
|
|
1840
|
+
config: this.config,
|
|
1841
|
+
metrics: { ...this.metrics },
|
|
1842
|
+
nodes: {
|
|
1843
|
+
total: this.nodes.size,
|
|
1844
|
+
byType: this.getNodeCountsByType()
|
|
1845
|
+
},
|
|
1846
|
+
relationships: {
|
|
1847
|
+
total: this.relationships.size,
|
|
1848
|
+
byType: this.getRelationshipCountsByType()
|
|
1849
|
+
},
|
|
1850
|
+
agents: {
|
|
1851
|
+
total: this.agents.size,
|
|
1852
|
+
byState: this.getAgentCountsByState()
|
|
1853
|
+
},
|
|
1854
|
+
chains: {
|
|
1855
|
+
total: this.chains.size,
|
|
1856
|
+
byState: this.getChainCountsByState()
|
|
1857
|
+
},
|
|
1858
|
+
tools: {
|
|
1859
|
+
total: this.tools.size,
|
|
1860
|
+
byState: this.getToolCountsByState()
|
|
1861
|
+
},
|
|
1862
|
+
personaKnowledge: {
|
|
1863
|
+
totalPersonas: this.personaKnowledge.size
|
|
1864
|
+
},
|
|
1865
|
+
knowledgePatterns: {
|
|
1866
|
+
totalPatterns: this.knowledgePatterns.size
|
|
1867
|
+
}
|
|
1868
|
+
};
|
|
1869
|
+
}
|
|
1870
|
+
getNodeCountsByType() {
|
|
1871
|
+
const counts = {};
|
|
1872
|
+
for (const [, node] of this.nodes) {
|
|
1873
|
+
counts[node.type] = (counts[node.type] ?? 0) + 1;
|
|
1874
|
+
}
|
|
1875
|
+
return counts;
|
|
1876
|
+
}
|
|
1877
|
+
getRelationshipCountsByType() {
|
|
1878
|
+
const counts = {};
|
|
1879
|
+
for (const [, rel] of this.relationships) {
|
|
1880
|
+
counts[rel.type] = (counts[rel.type] ?? 0) + 1;
|
|
1881
|
+
}
|
|
1882
|
+
return counts;
|
|
1883
|
+
}
|
|
1884
|
+
getAgentCountsByState() {
|
|
1885
|
+
const counts = {};
|
|
1886
|
+
for (const [, agent] of this.agents) {
|
|
1887
|
+
counts[agent.state] = (counts[agent.state] ?? 0) + 1;
|
|
1888
|
+
}
|
|
1889
|
+
return counts;
|
|
1890
|
+
}
|
|
1891
|
+
getChainCountsByState() {
|
|
1892
|
+
const counts = {};
|
|
1893
|
+
for (const [, chain] of this.chains) {
|
|
1894
|
+
counts[chain.state] = (counts[chain.state] ?? 0) + 1;
|
|
1895
|
+
}
|
|
1896
|
+
return counts;
|
|
1897
|
+
}
|
|
1898
|
+
getToolCountsByState() {
|
|
1899
|
+
const counts = {};
|
|
1900
|
+
for (const [, tool] of this.tools) {
|
|
1901
|
+
counts[tool.state] = (counts[tool.state] ?? 0) + 1;
|
|
1902
|
+
}
|
|
1903
|
+
return counts;
|
|
1904
|
+
}
|
|
1905
|
+
async optimize() {
|
|
1906
|
+
console.log("[KnowledgeGraph] Starting knowledge graph optimization...");
|
|
1907
|
+
const nodeThreshold = 1 - this.config.knowledgeRetention;
|
|
1908
|
+
const nodesToRemove = [];
|
|
1909
|
+
for (const [nodeId, node] of this.nodes) {
|
|
1910
|
+
if (node.metadata.confidence < nodeThreshold && node.type !== "root" && node.type !== "category") {
|
|
1911
|
+
nodesToRemove.push(nodeId);
|
|
1912
|
+
}
|
|
1913
|
+
}
|
|
1914
|
+
for (const nodeId of nodesToRemove) {
|
|
1915
|
+
this.nodes.delete(nodeId);
|
|
1916
|
+
}
|
|
1917
|
+
const relsToRemove = [];
|
|
1918
|
+
for (const [relId, rel] of this.relationships) {
|
|
1919
|
+
if (rel.metadata.confidence < nodeThreshold) {
|
|
1920
|
+
relsToRemove.push(relId);
|
|
1921
|
+
}
|
|
1922
|
+
if (!this.nodes.has(rel.from) || !this.nodes.has(rel.to)) {
|
|
1923
|
+
relsToRemove.push(relId);
|
|
1924
|
+
}
|
|
1925
|
+
}
|
|
1926
|
+
for (const relId of relsToRemove) {
|
|
1927
|
+
this.relationships.delete(relId);
|
|
1928
|
+
}
|
|
1929
|
+
this.metrics.totalNodes = this.nodes.size;
|
|
1930
|
+
this.metrics.totalRelationships = this.relationships.size;
|
|
1931
|
+
this.metrics.lastOptimization = Date.now();
|
|
1932
|
+
console.log(
|
|
1933
|
+
`[KnowledgeGraph] Optimization complete. Removed ${nodesToRemove.length} nodes and ${relsToRemove.length} relationships.`
|
|
1934
|
+
);
|
|
1935
|
+
this.emit("optimized", {
|
|
1936
|
+
removedNodes: nodesToRemove.length,
|
|
1937
|
+
removedRelationships: relsToRemove.length,
|
|
1938
|
+
metrics: this.metrics
|
|
1939
|
+
});
|
|
1940
|
+
}
|
|
1941
|
+
// ── Persona Knowledge ──────────────────────────────────────────────────────
|
|
1942
|
+
addPersonaKnowledge(personaId, key, value) {
|
|
1943
|
+
if (!this.personaKnowledge.has(personaId)) {
|
|
1944
|
+
this.personaKnowledge.set(personaId, /* @__PURE__ */ new Map());
|
|
1945
|
+
}
|
|
1946
|
+
this.personaKnowledge.get(personaId).set(key, value);
|
|
1947
|
+
this.emit("personaKnowledgeAdded", { personaId, key });
|
|
1948
|
+
}
|
|
1949
|
+
getPersonaKnowledge(personaId) {
|
|
1950
|
+
return this.personaKnowledge.get(personaId);
|
|
1951
|
+
}
|
|
1952
|
+
// ── Knowledge Pattern Management ───────────────────────────────────────────
|
|
1953
|
+
addKnowledgePattern(patternKey, pattern) {
|
|
1954
|
+
if (!this.knowledgePatterns.has(patternKey)) {
|
|
1955
|
+
this.knowledgePatterns.set(patternKey, []);
|
|
1956
|
+
}
|
|
1957
|
+
this.knowledgePatterns.get(patternKey).push(pattern);
|
|
1958
|
+
this.emit("patternAdded", { patternKey });
|
|
1959
|
+
}
|
|
1960
|
+
getKnowledgePatterns(patternKey) {
|
|
1961
|
+
return this.knowledgePatterns.get(patternKey);
|
|
1962
|
+
}
|
|
1963
|
+
// ── Node & Relationship Accessors ──────────────────────────────────────────
|
|
1964
|
+
getNode(nodeId) {
|
|
1965
|
+
return this.nodes.get(nodeId);
|
|
1966
|
+
}
|
|
1967
|
+
getRelationship(relId) {
|
|
1968
|
+
return this.relationships.get(relId);
|
|
1969
|
+
}
|
|
1970
|
+
getAllNodes() {
|
|
1971
|
+
return Array.from(this.nodes.values());
|
|
1972
|
+
}
|
|
1973
|
+
getAllRelationships() {
|
|
1974
|
+
return Array.from(this.relationships.values());
|
|
1975
|
+
}
|
|
1976
|
+
getNodesByType(type) {
|
|
1977
|
+
const result = [];
|
|
1978
|
+
for (const [, node] of this.nodes) {
|
|
1979
|
+
if (node.type === type) {
|
|
1980
|
+
result.push(node);
|
|
1981
|
+
}
|
|
1982
|
+
}
|
|
1983
|
+
return result;
|
|
1984
|
+
}
|
|
1985
|
+
getRelationshipsByType(type) {
|
|
1986
|
+
const result = [];
|
|
1987
|
+
for (const [, rel] of this.relationships) {
|
|
1988
|
+
if (rel.type === type) {
|
|
1989
|
+
result.push(rel);
|
|
1990
|
+
}
|
|
1991
|
+
}
|
|
1992
|
+
return result;
|
|
1993
|
+
}
|
|
1994
|
+
updateNode(nodeId, updates) {
|
|
1995
|
+
const node = this.nodes.get(nodeId);
|
|
1996
|
+
if (!node) return void 0;
|
|
1997
|
+
if (updates.name !== void 0) node.name = updates.name;
|
|
1998
|
+
if (updates.type !== void 0) node.type = updates.type;
|
|
1999
|
+
if (updates.level !== void 0) node.level = updates.level;
|
|
2000
|
+
if (updates.properties !== void 0) node.properties = { ...node.properties, ...updates.properties };
|
|
2001
|
+
if (updates.tags !== void 0) node.tags = updates.tags;
|
|
2002
|
+
if (updates.embeddings !== void 0) node.embeddings = updates.embeddings;
|
|
2003
|
+
node.metadata.lastModified = Date.now();
|
|
2004
|
+
node.metadata.version++;
|
|
2005
|
+
this.emit("nodeUpdated", { node });
|
|
2006
|
+
return node;
|
|
2007
|
+
}
|
|
2008
|
+
updateRelationship(relId, updates) {
|
|
2009
|
+
const rel = this.relationships.get(relId);
|
|
2010
|
+
if (!rel) return void 0;
|
|
2011
|
+
if (updates.type !== void 0) rel.type = updates.type;
|
|
2012
|
+
if (updates.weight !== void 0) rel.weight = updates.weight;
|
|
2013
|
+
if (updates.properties !== void 0) rel.properties = { ...rel.properties, ...updates.properties };
|
|
2014
|
+
rel.metadata.lastModified = Date.now();
|
|
2015
|
+
this.emit("relationshipUpdated", { relationship: rel });
|
|
2016
|
+
return rel;
|
|
2017
|
+
}
|
|
2018
|
+
deleteNode(nodeId) {
|
|
2019
|
+
if (!this.nodes.has(nodeId)) return false;
|
|
2020
|
+
this.nodes.delete(nodeId);
|
|
2021
|
+
const relsToRemove = [];
|
|
2022
|
+
for (const [relId, rel] of this.relationships) {
|
|
2023
|
+
if (rel.from === nodeId || rel.to === nodeId) {
|
|
2024
|
+
relsToRemove.push(relId);
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
for (const relId of relsToRemove) {
|
|
2028
|
+
this.relationships.delete(relId);
|
|
2029
|
+
}
|
|
2030
|
+
this.metrics.totalNodes = this.nodes.size;
|
|
2031
|
+
this.metrics.totalRelationships = this.relationships.size;
|
|
2032
|
+
this.emit("nodeDeleted", { nodeId, removedRelationships: relsToRemove.length });
|
|
2033
|
+
return true;
|
|
2034
|
+
}
|
|
2035
|
+
deleteRelationship(relId) {
|
|
2036
|
+
if (!this.relationships.has(relId)) return false;
|
|
2037
|
+
this.relationships.delete(relId);
|
|
2038
|
+
this.metrics.totalRelationships = this.relationships.size;
|
|
2039
|
+
this.emit("relationshipDeleted", { relId });
|
|
2040
|
+
return true;
|
|
2041
|
+
}
|
|
2042
|
+
getMetrics() {
|
|
2043
|
+
return { ...this.metrics };
|
|
2044
|
+
}
|
|
2045
|
+
};
|
|
2046
|
+
|
|
2047
|
+
// src/knowledge/vector-store.ts
|
|
2048
|
+
var ChromaVectorStore = class {
|
|
2049
|
+
config;
|
|
2050
|
+
client;
|
|
2051
|
+
collection;
|
|
2052
|
+
isInitialized;
|
|
2053
|
+
metrics;
|
|
2054
|
+
queryCache;
|
|
2055
|
+
cacheMaxAge;
|
|
2056
|
+
constructor(config = {}) {
|
|
2057
|
+
this.config = {
|
|
2058
|
+
host: config.host ?? "localhost",
|
|
2059
|
+
port: config.port ?? 8e3,
|
|
2060
|
+
collectionName: config.collectionName ?? "baseline-vectors",
|
|
2061
|
+
embeddingFunction: config.embeddingFunction ?? "default",
|
|
2062
|
+
metadata: config.metadata ?? {}
|
|
2063
|
+
};
|
|
2064
|
+
this.client = null;
|
|
2065
|
+
this.collection = null;
|
|
2066
|
+
this.isInitialized = false;
|
|
2067
|
+
this.metrics = {
|
|
2068
|
+
totalDocuments: 0,
|
|
2069
|
+
totalQueries: 0,
|
|
2070
|
+
averageQueryTime: 0,
|
|
2071
|
+
lastOptimization: Date.now()
|
|
2072
|
+
};
|
|
2073
|
+
this.queryCache = /* @__PURE__ */ new Map();
|
|
2074
|
+
this.cacheMaxAge = 5 * 60 * 1e3;
|
|
2075
|
+
}
|
|
2076
|
+
// ── Initialization ──────────────────────────────────────────────────────────
|
|
2077
|
+
async initializeClient() {
|
|
2078
|
+
if (this.isInitialized) {
|
|
2079
|
+
console.log("[VectorStore] Client already initialized.");
|
|
2080
|
+
return;
|
|
2081
|
+
}
|
|
2082
|
+
try {
|
|
2083
|
+
let chromadb;
|
|
2084
|
+
try {
|
|
2085
|
+
chromadb = await import("chromadb");
|
|
2086
|
+
} catch {
|
|
2087
|
+
console.log("[VectorStore] chromadb package not found. Install it with: npm install chromadb");
|
|
2088
|
+
console.log("[VectorStore] Running in fallback mode without vector storage.");
|
|
2089
|
+
this.isInitialized = false;
|
|
2090
|
+
return;
|
|
2091
|
+
}
|
|
2092
|
+
const ChromaClient = chromadb.ChromaClient;
|
|
2093
|
+
this.client = new ChromaClient({
|
|
2094
|
+
path: `http://${this.config.host}:${this.config.port}`
|
|
2095
|
+
});
|
|
2096
|
+
await this.initializeCollection();
|
|
2097
|
+
this.isInitialized = true;
|
|
2098
|
+
console.log(`[VectorStore] Client initialized. Connected to ${this.config.host}:${this.config.port}.`);
|
|
2099
|
+
} catch (error) {
|
|
2100
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2101
|
+
console.log(`[VectorStore] Failed to initialize client: ${message}`);
|
|
2102
|
+
console.log("[VectorStore] Running in fallback mode without vector storage.");
|
|
2103
|
+
this.isInitialized = false;
|
|
2104
|
+
}
|
|
2105
|
+
}
|
|
2106
|
+
async initializeCollection() {
|
|
2107
|
+
if (!this.client) {
|
|
2108
|
+
throw new Error("Client not initialized");
|
|
2109
|
+
}
|
|
2110
|
+
try {
|
|
2111
|
+
const client = this.client;
|
|
2112
|
+
const embeddingFunction = this.createEmbeddingFunction();
|
|
2113
|
+
this.collection = await client.getOrCreateCollection({
|
|
2114
|
+
name: this.config.collectionName,
|
|
2115
|
+
metadata: this.config.metadata,
|
|
2116
|
+
embeddingFunction
|
|
2117
|
+
});
|
|
2118
|
+
console.log(`[VectorStore] Collection "${this.config.collectionName}" ready.`);
|
|
2119
|
+
} catch (error) {
|
|
2120
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2121
|
+
console.log(`[VectorStore] Failed to initialize collection: ${message}`);
|
|
2122
|
+
throw error;
|
|
2123
|
+
}
|
|
2124
|
+
}
|
|
2125
|
+
// ── Document Operations ─────────────────────────────────────────────────────
|
|
2126
|
+
async addDocuments(input) {
|
|
2127
|
+
if (!this.isInitialized || !this.collection) {
|
|
2128
|
+
return {
|
|
2129
|
+
success: false,
|
|
2130
|
+
error: "Vector store not initialized. Call initializeClient() first."
|
|
2131
|
+
};
|
|
2132
|
+
}
|
|
2133
|
+
try {
|
|
2134
|
+
const collection = this.collection;
|
|
2135
|
+
const addPayload = {
|
|
2136
|
+
ids: input.ids,
|
|
2137
|
+
documents: input.documents
|
|
2138
|
+
};
|
|
2139
|
+
if (input.embeddings) {
|
|
2140
|
+
addPayload.embeddings = input.embeddings;
|
|
2141
|
+
}
|
|
2142
|
+
if (input.metadatas) {
|
|
2143
|
+
addPayload.metadatas = input.metadatas;
|
|
2144
|
+
}
|
|
2145
|
+
await collection.add(addPayload);
|
|
2146
|
+
this.metrics.totalDocuments += input.ids.length;
|
|
2147
|
+
console.log(`[VectorStore] Added ${input.ids.length} documents.`);
|
|
2148
|
+
return {
|
|
2149
|
+
success: true,
|
|
2150
|
+
metadata: {
|
|
2151
|
+
addedCount: input.ids.length,
|
|
2152
|
+
totalDocuments: this.metrics.totalDocuments
|
|
2153
|
+
}
|
|
2154
|
+
};
|
|
2155
|
+
} catch (error) {
|
|
2156
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2157
|
+
console.log(`[VectorStore] Failed to add documents: ${message}`);
|
|
2158
|
+
return {
|
|
2159
|
+
success: false,
|
|
2160
|
+
error: message
|
|
2161
|
+
};
|
|
2162
|
+
}
|
|
2163
|
+
}
|
|
2164
|
+
async query(queryText, options = {}) {
|
|
2165
|
+
if (!this.isInitialized || !this.collection) {
|
|
2166
|
+
return {
|
|
2167
|
+
success: false,
|
|
2168
|
+
query: queryText,
|
|
2169
|
+
error: "Vector store not initialized. Call initializeClient() first."
|
|
2170
|
+
};
|
|
2171
|
+
}
|
|
2172
|
+
const startTime = Date.now();
|
|
2173
|
+
const cacheKey = `${queryText}:${JSON.stringify(options)}`;
|
|
2174
|
+
const cached = this.queryCache.get(cacheKey);
|
|
2175
|
+
if (cached && Date.now() - cached.timestamp < this.cacheMaxAge) {
|
|
2176
|
+
this.metrics.totalQueries++;
|
|
2177
|
+
return {
|
|
2178
|
+
success: true,
|
|
2179
|
+
query: queryText,
|
|
2180
|
+
results: cached.result,
|
|
2181
|
+
metadata: { fromCache: true, queryTime: 0 }
|
|
2182
|
+
};
|
|
2183
|
+
}
|
|
2184
|
+
try {
|
|
2185
|
+
const collection = this.collection;
|
|
2186
|
+
const queryPayload = {
|
|
2187
|
+
queryTexts: [queryText],
|
|
2188
|
+
nResults: options.nResults ?? 10
|
|
2189
|
+
};
|
|
2190
|
+
if (options.where) {
|
|
2191
|
+
queryPayload.where = options.where;
|
|
2192
|
+
}
|
|
2193
|
+
if (options.whereDocument) {
|
|
2194
|
+
queryPayload.whereDocument = options.whereDocument;
|
|
2195
|
+
}
|
|
2196
|
+
if (options.include) {
|
|
2197
|
+
queryPayload.include = options.include;
|
|
2198
|
+
}
|
|
2199
|
+
const results = await collection.query(queryPayload);
|
|
2200
|
+
const queryTime = Date.now() - startTime;
|
|
2201
|
+
this.metrics.totalQueries++;
|
|
2202
|
+
this.metrics.averageQueryTime = (this.metrics.averageQueryTime * (this.metrics.totalQueries - 1) + queryTime) / this.metrics.totalQueries;
|
|
2203
|
+
this.queryCache.set(cacheKey, { result: results, timestamp: Date.now() });
|
|
2204
|
+
return {
|
|
2205
|
+
success: true,
|
|
2206
|
+
query: queryText,
|
|
2207
|
+
results,
|
|
2208
|
+
metadata: {
|
|
2209
|
+
queryTime,
|
|
2210
|
+
nResults: options.nResults ?? 10,
|
|
2211
|
+
fromCache: false
|
|
2212
|
+
}
|
|
2213
|
+
};
|
|
2214
|
+
} catch (error) {
|
|
2215
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2216
|
+
console.log(`[VectorStore] Query failed: ${message}`);
|
|
2217
|
+
return {
|
|
2218
|
+
success: false,
|
|
2219
|
+
query: queryText,
|
|
2220
|
+
error: message
|
|
2221
|
+
};
|
|
2222
|
+
}
|
|
2223
|
+
}
|
|
2224
|
+
async semanticSearch(queryText, nResults = 10) {
|
|
2225
|
+
return this.query(queryText, {
|
|
2226
|
+
nResults,
|
|
2227
|
+
include: ["documents", "metadatas", "distances"]
|
|
2228
|
+
});
|
|
2229
|
+
}
|
|
2230
|
+
async filterSearch(queryText, options = {}) {
|
|
2231
|
+
return this.query(queryText, {
|
|
2232
|
+
nResults: options.nResults ?? 10,
|
|
2233
|
+
where: options.where,
|
|
2234
|
+
whereDocument: options.whereDocument,
|
|
2235
|
+
include: options.include ?? ["documents", "metadatas", "distances"]
|
|
2236
|
+
});
|
|
2237
|
+
}
|
|
2238
|
+
async fullTextSearch(searchText, nResults = 10) {
|
|
2239
|
+
return this.query(searchText, {
|
|
2240
|
+
nResults,
|
|
2241
|
+
whereDocument: { $contains: searchText },
|
|
2242
|
+
include: ["documents", "metadatas", "distances"]
|
|
2243
|
+
});
|
|
2244
|
+
}
|
|
2245
|
+
async regexSearch(pattern, nResults = 10) {
|
|
2246
|
+
if (!this.isInitialized || !this.collection) {
|
|
2247
|
+
return {
|
|
2248
|
+
success: false,
|
|
2249
|
+
query: pattern,
|
|
2250
|
+
error: "Vector store not initialized. Call initializeClient() first."
|
|
2251
|
+
};
|
|
2252
|
+
}
|
|
2253
|
+
try {
|
|
2254
|
+
const collection = this.collection;
|
|
2255
|
+
const allResults = await collection.get({
|
|
2256
|
+
include: ["documents", "metadatas"]
|
|
2257
|
+
});
|
|
2258
|
+
const resultsObj = allResults;
|
|
2259
|
+
const ids = resultsObj.ids ?? [];
|
|
2260
|
+
const documents = resultsObj.documents ?? [];
|
|
2261
|
+
const metadatas = resultsObj.metadatas ?? [];
|
|
2262
|
+
const regex = new RegExp(pattern, "i");
|
|
2263
|
+
const matchedIds = [];
|
|
2264
|
+
const matchedDocuments = [];
|
|
2265
|
+
const matchedMetadatas = [];
|
|
2266
|
+
for (let i = 0; i < ids.length; i++) {
|
|
2267
|
+
const doc = documents[i];
|
|
2268
|
+
if (doc && regex.test(doc)) {
|
|
2269
|
+
matchedIds.push(ids[i]);
|
|
2270
|
+
matchedDocuments.push(doc);
|
|
2271
|
+
matchedMetadatas.push(metadatas[i] ?? {});
|
|
2272
|
+
if (matchedIds.length >= nResults) break;
|
|
2273
|
+
}
|
|
2274
|
+
}
|
|
2275
|
+
return {
|
|
2276
|
+
success: true,
|
|
2277
|
+
query: pattern,
|
|
2278
|
+
results: {
|
|
2279
|
+
ids: [matchedIds],
|
|
2280
|
+
documents: [matchedDocuments],
|
|
2281
|
+
metadatas: [matchedMetadatas]
|
|
2282
|
+
},
|
|
2283
|
+
metadata: {
|
|
2284
|
+
matchCount: matchedIds.length,
|
|
2285
|
+
searchType: "regex"
|
|
2286
|
+
}
|
|
2287
|
+
};
|
|
2288
|
+
} catch (error) {
|
|
2289
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2290
|
+
console.log(`[VectorStore] Regex search failed: ${message}`);
|
|
2291
|
+
return {
|
|
2292
|
+
success: false,
|
|
2293
|
+
query: pattern,
|
|
2294
|
+
error: message
|
|
2295
|
+
};
|
|
2296
|
+
}
|
|
2297
|
+
}
|
|
2298
|
+
async updateDocuments(input) {
|
|
2299
|
+
if (!this.isInitialized || !this.collection) {
|
|
2300
|
+
return {
|
|
2301
|
+
success: false,
|
|
2302
|
+
error: "Vector store not initialized. Call initializeClient() first."
|
|
2303
|
+
};
|
|
2304
|
+
}
|
|
2305
|
+
try {
|
|
2306
|
+
const collection = this.collection;
|
|
2307
|
+
const updatePayload = {
|
|
2308
|
+
ids: input.ids,
|
|
2309
|
+
documents: input.documents
|
|
2310
|
+
};
|
|
2311
|
+
if (input.embeddings) {
|
|
2312
|
+
updatePayload.embeddings = input.embeddings;
|
|
2313
|
+
}
|
|
2314
|
+
if (input.metadatas) {
|
|
2315
|
+
updatePayload.metadatas = input.metadatas;
|
|
2316
|
+
}
|
|
2317
|
+
await collection.update(updatePayload);
|
|
2318
|
+
this.queryCache.clear();
|
|
2319
|
+
console.log(`[VectorStore] Updated ${input.ids.length} documents.`);
|
|
2320
|
+
return {
|
|
2321
|
+
success: true,
|
|
2322
|
+
metadata: {
|
|
2323
|
+
updatedCount: input.ids.length
|
|
2324
|
+
}
|
|
2325
|
+
};
|
|
2326
|
+
} catch (error) {
|
|
2327
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2328
|
+
console.log(`[VectorStore] Failed to update documents: ${message}`);
|
|
2329
|
+
return {
|
|
2330
|
+
success: false,
|
|
2331
|
+
error: message
|
|
2332
|
+
};
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
async deleteDocuments(ids) {
|
|
2336
|
+
if (!this.isInitialized || !this.collection) {
|
|
2337
|
+
return {
|
|
2338
|
+
success: false,
|
|
2339
|
+
error: "Vector store not initialized. Call initializeClient() first."
|
|
2340
|
+
};
|
|
2341
|
+
}
|
|
2342
|
+
try {
|
|
2343
|
+
const collection = this.collection;
|
|
2344
|
+
await collection.delete({ ids });
|
|
2345
|
+
this.metrics.totalDocuments = Math.max(0, this.metrics.totalDocuments - ids.length);
|
|
2346
|
+
this.queryCache.clear();
|
|
2347
|
+
console.log(`[VectorStore] Deleted ${ids.length} documents.`);
|
|
2348
|
+
return {
|
|
2349
|
+
success: true,
|
|
2350
|
+
metadata: {
|
|
2351
|
+
deletedCount: ids.length,
|
|
2352
|
+
totalDocuments: this.metrics.totalDocuments
|
|
2353
|
+
}
|
|
2354
|
+
};
|
|
2355
|
+
} catch (error) {
|
|
2356
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2357
|
+
console.log(`[VectorStore] Failed to delete documents: ${message}`);
|
|
2358
|
+
return {
|
|
2359
|
+
success: false,
|
|
2360
|
+
error: message
|
|
2361
|
+
};
|
|
2362
|
+
}
|
|
2363
|
+
}
|
|
2364
|
+
// ── Collection Management ───────────────────────────────────────────────────
|
|
2365
|
+
async getCollectionStats() {
|
|
2366
|
+
if (!this.isInitialized || !this.collection) {
|
|
2367
|
+
console.log("[VectorStore] Not initialized, cannot get collection stats.");
|
|
2368
|
+
return null;
|
|
2369
|
+
}
|
|
2370
|
+
try {
|
|
2371
|
+
const collection = this.collection;
|
|
2372
|
+
const count = await collection.count();
|
|
2373
|
+
const peek = await collection.peek({ limit: 1 });
|
|
2374
|
+
this.metrics.totalDocuments = count;
|
|
2375
|
+
return {
|
|
2376
|
+
name: this.config.collectionName,
|
|
2377
|
+
count,
|
|
2378
|
+
metadata: {
|
|
2379
|
+
...this.config.metadata,
|
|
2380
|
+
peekSample: peek
|
|
2381
|
+
}
|
|
2382
|
+
};
|
|
2383
|
+
} catch (error) {
|
|
2384
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2385
|
+
console.log(`[VectorStore] Failed to get collection stats: ${message}`);
|
|
2386
|
+
return null;
|
|
2387
|
+
}
|
|
2388
|
+
}
|
|
2389
|
+
async optimizeCollection() {
|
|
2390
|
+
if (!this.isInitialized || !this.collection) {
|
|
2391
|
+
return {
|
|
2392
|
+
success: false,
|
|
2393
|
+
error: "Vector store not initialized. Call initializeClient() first."
|
|
2394
|
+
};
|
|
2395
|
+
}
|
|
2396
|
+
console.log("[VectorStore] Starting collection optimization...");
|
|
2397
|
+
try {
|
|
2398
|
+
const cacheSize = this.queryCache.size;
|
|
2399
|
+
this.queryCache.clear();
|
|
2400
|
+
this.metrics.lastOptimization = Date.now();
|
|
2401
|
+
const stats = await this.getCollectionStats();
|
|
2402
|
+
console.log(`[VectorStore] Optimization complete. Cache cleared (${cacheSize} entries).`);
|
|
2403
|
+
return {
|
|
2404
|
+
success: true,
|
|
2405
|
+
metadata: {
|
|
2406
|
+
cacheEntriesCleared: cacheSize,
|
|
2407
|
+
collectionStats: stats,
|
|
2408
|
+
optimizationTime: Date.now()
|
|
2409
|
+
}
|
|
2410
|
+
};
|
|
2411
|
+
} catch (error) {
|
|
2412
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2413
|
+
console.log(`[VectorStore] Optimization failed: ${message}`);
|
|
2414
|
+
return {
|
|
2415
|
+
success: false,
|
|
2416
|
+
error: message
|
|
2417
|
+
};
|
|
2418
|
+
}
|
|
2419
|
+
}
|
|
2420
|
+
// ── Embedding Function ──────────────────────────────────────────────────────
|
|
2421
|
+
createEmbeddingFunction() {
|
|
2422
|
+
const funcType = this.config.embeddingFunction;
|
|
2423
|
+
switch (funcType) {
|
|
2424
|
+
case "openai": {
|
|
2425
|
+
console.log("[VectorStore] Using OpenAI embedding function.");
|
|
2426
|
+
return {
|
|
2427
|
+
generate: async (texts) => {
|
|
2428
|
+
return texts.map((_text) => {
|
|
2429
|
+
const embedding = [];
|
|
2430
|
+
for (let i = 0; i < 1536; i++) {
|
|
2431
|
+
embedding.push(Math.random() * 2 - 1);
|
|
2432
|
+
}
|
|
2433
|
+
return embedding;
|
|
2434
|
+
});
|
|
2435
|
+
}
|
|
2436
|
+
};
|
|
2437
|
+
}
|
|
2438
|
+
case "cohere": {
|
|
2439
|
+
console.log("[VectorStore] Using Cohere embedding function.");
|
|
2440
|
+
return {
|
|
2441
|
+
generate: async (texts) => {
|
|
2442
|
+
return texts.map((_text) => {
|
|
2443
|
+
const embedding = [];
|
|
2444
|
+
for (let i = 0; i < 768; i++) {
|
|
2445
|
+
embedding.push(Math.random() * 2 - 1);
|
|
2446
|
+
}
|
|
2447
|
+
return embedding;
|
|
2448
|
+
});
|
|
2449
|
+
}
|
|
2450
|
+
};
|
|
2451
|
+
}
|
|
2452
|
+
case "huggingface": {
|
|
2453
|
+
console.log("[VectorStore] Using HuggingFace embedding function.");
|
|
2454
|
+
return {
|
|
2455
|
+
generate: async (texts) => {
|
|
2456
|
+
return texts.map((_text) => {
|
|
2457
|
+
const embedding = [];
|
|
2458
|
+
for (let i = 0; i < 384; i++) {
|
|
2459
|
+
embedding.push(Math.random() * 2 - 1);
|
|
2460
|
+
}
|
|
2461
|
+
return embedding;
|
|
2462
|
+
});
|
|
2463
|
+
}
|
|
2464
|
+
};
|
|
2465
|
+
}
|
|
2466
|
+
case "default":
|
|
2467
|
+
default: {
|
|
2468
|
+
console.log("[VectorStore] Using default embedding function.");
|
|
2469
|
+
return {
|
|
2470
|
+
generate: async (texts) => {
|
|
2471
|
+
return texts.map((text) => {
|
|
2472
|
+
const embedding = [];
|
|
2473
|
+
for (let i = 0; i < 384; i++) {
|
|
2474
|
+
let hash = 0;
|
|
2475
|
+
for (let j = 0; j < text.length; j++) {
|
|
2476
|
+
hash = (hash << 5) - hash + text.charCodeAt(j) + i | 0;
|
|
2477
|
+
}
|
|
2478
|
+
embedding.push(hash % 1e3 / 1e3);
|
|
2479
|
+
}
|
|
2480
|
+
return embedding;
|
|
2481
|
+
});
|
|
2482
|
+
}
|
|
2483
|
+
};
|
|
2484
|
+
}
|
|
2485
|
+
}
|
|
2486
|
+
}
|
|
2487
|
+
// ── Batch Operations ────────────────────────────────────────────────────────
|
|
2488
|
+
async batchOperation(operations) {
|
|
2489
|
+
if (!this.isInitialized || !this.collection) {
|
|
2490
|
+
return {
|
|
2491
|
+
success: false,
|
|
2492
|
+
error: "Vector store not initialized. Call initializeClient() first."
|
|
2493
|
+
};
|
|
2494
|
+
}
|
|
2495
|
+
const results = [];
|
|
2496
|
+
let successCount = 0;
|
|
2497
|
+
let failureCount = 0;
|
|
2498
|
+
for (const operation of operations) {
|
|
2499
|
+
try {
|
|
2500
|
+
switch (operation.type) {
|
|
2501
|
+
case "add": {
|
|
2502
|
+
const addResult = await this.addDocuments(operation.data);
|
|
2503
|
+
results.push({ type: "add", success: addResult.success, error: addResult.error });
|
|
2504
|
+
if (addResult.success) successCount++;
|
|
2505
|
+
else failureCount++;
|
|
2506
|
+
break;
|
|
2507
|
+
}
|
|
2508
|
+
case "update": {
|
|
2509
|
+
const updateResult = await this.updateDocuments(operation.data);
|
|
2510
|
+
results.push({ type: "update", success: updateResult.success, error: updateResult.error });
|
|
2511
|
+
if (updateResult.success) successCount++;
|
|
2512
|
+
else failureCount++;
|
|
2513
|
+
break;
|
|
2514
|
+
}
|
|
2515
|
+
case "delete": {
|
|
2516
|
+
const ids = operation.data.ids;
|
|
2517
|
+
const deleteResult = await this.deleteDocuments(ids);
|
|
2518
|
+
results.push({ type: "delete", success: deleteResult.success, error: deleteResult.error });
|
|
2519
|
+
if (deleteResult.success) successCount++;
|
|
2520
|
+
else failureCount++;
|
|
2521
|
+
break;
|
|
2522
|
+
}
|
|
2523
|
+
default:
|
|
2524
|
+
results.push({ type: operation.type, success: false, error: `Unknown operation type: ${operation.type}` });
|
|
2525
|
+
failureCount++;
|
|
2526
|
+
}
|
|
2527
|
+
} catch (error) {
|
|
2528
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
2529
|
+
results.push({ type: operation.type, success: false, error: message });
|
|
2530
|
+
failureCount++;
|
|
2531
|
+
}
|
|
2532
|
+
}
|
|
2533
|
+
console.log(`[VectorStore] Batch operation complete: ${successCount} succeeded, ${failureCount} failed.`);
|
|
2534
|
+
return {
|
|
2535
|
+
success: failureCount === 0,
|
|
2536
|
+
metadata: {
|
|
2537
|
+
totalOperations: operations.length,
|
|
2538
|
+
successCount,
|
|
2539
|
+
failureCount,
|
|
2540
|
+
results
|
|
2541
|
+
}
|
|
2542
|
+
};
|
|
2543
|
+
}
|
|
2544
|
+
// ── Client Status ───────────────────────────────────────────────────────────
|
|
2545
|
+
getClientStatus() {
|
|
2546
|
+
return {
|
|
2547
|
+
isInitialized: this.isInitialized,
|
|
2548
|
+
config: {
|
|
2549
|
+
host: this.config.host,
|
|
2550
|
+
port: this.config.port,
|
|
2551
|
+
collectionName: this.config.collectionName,
|
|
2552
|
+
embeddingFunction: this.config.embeddingFunction
|
|
2553
|
+
},
|
|
2554
|
+
metrics: { ...this.metrics },
|
|
2555
|
+
cache: {
|
|
2556
|
+
entries: this.queryCache.size,
|
|
2557
|
+
maxAge: this.cacheMaxAge
|
|
2558
|
+
},
|
|
2559
|
+
hasClient: this.client !== null,
|
|
2560
|
+
hasCollection: this.collection !== null
|
|
2561
|
+
};
|
|
2562
|
+
}
|
|
2563
|
+
// ── Cleanup ─────────────────────────────────────────────────────────────────
|
|
2564
|
+
async close() {
|
|
2565
|
+
console.log("[VectorStore] Closing vector store connection...");
|
|
2566
|
+
this.queryCache.clear();
|
|
2567
|
+
this.client = null;
|
|
2568
|
+
this.collection = null;
|
|
2569
|
+
this.isInitialized = false;
|
|
2570
|
+
console.log("[VectorStore] Vector store connection closed.");
|
|
2571
|
+
}
|
|
2572
|
+
// ── Utility Accessors ──────────────────────────────────────────────────────
|
|
2573
|
+
getMetrics() {
|
|
2574
|
+
return { ...this.metrics };
|
|
2575
|
+
}
|
|
2576
|
+
isReady() {
|
|
2577
|
+
return this.isInitialized;
|
|
2578
|
+
}
|
|
2579
|
+
getCollectionName() {
|
|
2580
|
+
return this.config.collectionName;
|
|
2581
|
+
}
|
|
2582
|
+
clearCache() {
|
|
2583
|
+
this.queryCache.clear();
|
|
2584
|
+
console.log("[VectorStore] Query cache cleared.");
|
|
2585
|
+
}
|
|
2586
|
+
};
|
|
2587
|
+
|
|
2588
|
+
// src/index.ts
|
|
2589
|
+
var BaselineLangCore = class {
|
|
2590
|
+
lexicon;
|
|
2591
|
+
parser;
|
|
2592
|
+
lang;
|
|
2593
|
+
constructor() {
|
|
2594
|
+
this.lexicon = new BaselineLexicon();
|
|
2595
|
+
this.parser = new BaselineParser();
|
|
2596
|
+
this.lang = new BaselineLang(this.parser, this.lexicon);
|
|
2597
|
+
}
|
|
2598
|
+
async initialize() {
|
|
2599
|
+
await this.lexicon.initialize();
|
|
2600
|
+
await this.parser.initialize();
|
|
2601
|
+
await this.lang.initialize();
|
|
2602
|
+
}
|
|
2603
|
+
getLang() {
|
|
2604
|
+
return this.lang;
|
|
2605
|
+
}
|
|
2606
|
+
getParser() {
|
|
2607
|
+
return this.parser;
|
|
2608
|
+
}
|
|
2609
|
+
getLexicon() {
|
|
2610
|
+
return this.lexicon;
|
|
2611
|
+
}
|
|
2612
|
+
};
|
|
2613
|
+
export {
|
|
2614
|
+
BaselineLang,
|
|
2615
|
+
BaselineLangCore,
|
|
2616
|
+
BaselineLexicon,
|
|
2617
|
+
BaselineParser,
|
|
2618
|
+
ChromaVectorStore,
|
|
2619
|
+
GrammarValidator,
|
|
2620
|
+
KnowledgeGraphEngine,
|
|
2621
|
+
baselineGrammar,
|
|
2622
|
+
baselineGrammarRules
|
|
2623
|
+
};
|
|
2624
|
+
/**
|
|
2625
|
+
* Baseline Lang -- Core Language Implementation
|
|
2626
|
+
* The Language & Expression Layer of Baseline Stack
|
|
2627
|
+
*
|
|
2628
|
+
* @license Apache-2.0
|
|
2629
|
+
*/
|
|
2630
|
+
/**
|
|
2631
|
+
* Baseline Language Parser
|
|
2632
|
+
* PEG-based parser for Baseline Language syntax
|
|
2633
|
+
*
|
|
2634
|
+
* @license Apache-2.0
|
|
2635
|
+
*/
|
|
2636
|
+
/**
|
|
2637
|
+
* Baseline Language Lexicon Management
|
|
2638
|
+
* Domain-specific vocabulary and template system
|
|
2639
|
+
*
|
|
2640
|
+
* @license Apache-2.0
|
|
2641
|
+
*/
|
|
2642
|
+
/**
|
|
2643
|
+
* Baseline Language PEG Grammar
|
|
2644
|
+
* Parsing Expression Grammar for the Baseline prompt engineering language
|
|
2645
|
+
*
|
|
2646
|
+
* @license Apache-2.0
|
|
2647
|
+
*/
|
|
2648
|
+
/**
|
|
2649
|
+
* Baseline Lang -- Main Entry Point
|
|
2650
|
+
* The Language & Expression Layer of Baseline Stack
|
|
2651
|
+
*
|
|
2652
|
+
* @license Apache-2.0
|
|
2653
|
+
*/
|