rust-kgdb 0.6.3 → 0.6.5
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/README.md +38 -23
- package/examples/core-concepts-demo.ts +50 -35
- package/examples/datalog-example.ts +246 -339
- package/examples/fraud-detection-agent.js +23 -36
- package/examples/hypermind-fraud-underwriter.ts +54 -51
- package/examples/underwriting-agent.js +16 -20
- package/hypermind-agent.js +1003 -2347
- package/index.d.ts +79 -0
- package/package.json +1 -1
- package/rust-kgdb-napi.darwin-x64.node +0 -0
package/README.md
CHANGED
|
@@ -205,33 +205,48 @@ agent.chat("What fraud patterns did we find with Provider P001?")
|
|
|
205
205
|
// Cost: Re-run entire fraud detection pipeline ($5 in API calls, 30 seconds)
|
|
206
206
|
```
|
|
207
207
|
|
|
208
|
-
**With Memory Hypergraph** (rust-kgdb):
|
|
208
|
+
**With Memory Hypergraph** (rust-kgdb HyperMind Framework):
|
|
209
209
|
```javascript
|
|
210
|
-
//
|
|
211
|
-
const
|
|
212
|
-
|
|
210
|
+
// HyperMind API: Recall memories with KG context (typed, not raw SPARQL)
|
|
211
|
+
const enrichedMemories = await agent.recallWithKG({
|
|
212
|
+
query: "Provider P001 fraud",
|
|
213
|
+
kgFilter: { predicate: ":amount", operator: ">", value: 25000 },
|
|
214
|
+
limit: 10
|
|
215
|
+
})
|
|
213
216
|
|
|
214
|
-
//
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
217
|
+
// Returns typed results:
|
|
218
|
+
// {
|
|
219
|
+
// episode: "Episode:001",
|
|
220
|
+
// finding: "Fraud ring detected in Provider P001",
|
|
221
|
+
// kgContext: {
|
|
222
|
+
// provider: "Provider:P001",
|
|
223
|
+
// claims: [{ id: "Claim:C123", amount: 50000 }],
|
|
224
|
+
// riskScore: 0.87
|
|
225
|
+
// },
|
|
226
|
+
// semanticHash: "semhash:fraud-provider-p001-ring-detection"
|
|
227
|
+
// }
|
|
228
|
+
|
|
229
|
+
// Framework generates optimized SPARQL internally:
|
|
230
|
+
// - Joins memory graph with KG automatically
|
|
231
|
+
// - Applies semantic hashing for deduplication
|
|
232
|
+
// - Returns typed objects, not raw bindings
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
**Under the hood**, HyperMind generates the SPARQL:
|
|
236
|
+
```sparql
|
|
237
|
+
PREFIX am: <https://gonnect.ai/ontology/agent-memory#>
|
|
238
|
+
PREFIX : <http://insurance.org/>
|
|
239
|
+
|
|
240
|
+
SELECT ?episode ?finding ?claimAmount WHERE {
|
|
241
|
+
GRAPH <https://gonnect.ai/memory/> {
|
|
242
|
+
?episode a am:Episode ; am:prompt ?finding .
|
|
243
|
+
?edge am:source ?episode ; am:target ?provider .
|
|
231
244
|
}
|
|
232
|
-
|
|
233
|
-
|
|
245
|
+
?claim :provider ?provider ; :amount ?claimAmount .
|
|
246
|
+
FILTER(?claimAmount > 25000)
|
|
247
|
+
}
|
|
234
248
|
```
|
|
249
|
+
*You never write this - the typed API builds it for you.*
|
|
235
250
|
|
|
236
251
|
### Rolling Context Window
|
|
237
252
|
|
|
@@ -18,9 +18,12 @@
|
|
|
18
18
|
|
|
19
19
|
import {
|
|
20
20
|
GraphDB,
|
|
21
|
-
|
|
21
|
+
DatalogProgram,
|
|
22
|
+
evaluateDatalog,
|
|
23
|
+
queryDatalog,
|
|
22
24
|
EmbeddingService,
|
|
23
25
|
GraphFrame,
|
|
26
|
+
HyperMindAgent,
|
|
24
27
|
} from 'rust-kgdb';
|
|
25
28
|
|
|
26
29
|
// =============================================================================
|
|
@@ -242,46 +245,53 @@ function demoDatalogReasoning(): void {
|
|
|
242
245
|
console.log('='.repeat(70));
|
|
243
246
|
console.log();
|
|
244
247
|
|
|
245
|
-
const
|
|
248
|
+
const program = new DatalogProgram();
|
|
246
249
|
|
|
247
|
-
// Add facts
|
|
250
|
+
// Add facts using JSON format
|
|
248
251
|
console.log(' Facts (parent relationship):');
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
+
program.addFact(JSON.stringify({ predicate: 'parent', terms: ['alice', 'bob'] }));
|
|
253
|
+
program.addFact(JSON.stringify({ predicate: 'parent', terms: ['bob', 'charlie'] }));
|
|
254
|
+
program.addFact(JSON.stringify({ predicate: 'parent', terms: ['charlie', 'david'] }));
|
|
252
255
|
console.log(' parent(alice, bob)');
|
|
253
256
|
console.log(' parent(bob, charlie)');
|
|
254
257
|
console.log(' parent(charlie, david)');
|
|
258
|
+
console.log(` Total facts: ${program.factCount()}`);
|
|
255
259
|
console.log();
|
|
256
260
|
|
|
257
|
-
// Add recursive rules
|
|
261
|
+
// Add recursive rules using JSON format
|
|
258
262
|
console.log(' Rules (transitive closure):');
|
|
259
263
|
console.log(' ancestor(X, Y) :- parent(X, Y) % base case');
|
|
260
264
|
console.log(' ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z) % recursive');
|
|
261
265
|
console.log();
|
|
262
266
|
|
|
263
|
-
// Base case
|
|
264
|
-
|
|
265
|
-
{ predicate: '
|
|
266
|
-
|
|
267
|
+
// Base case: ancestor(X, Y) :- parent(X, Y)
|
|
268
|
+
program.addRule(JSON.stringify({
|
|
269
|
+
head: { predicate: 'ancestor', terms: ['X', 'Y'] },
|
|
270
|
+
body: [{ predicate: 'parent', terms: ['X', 'Y'] }]
|
|
271
|
+
}));
|
|
267
272
|
|
|
268
|
-
// Recursive case
|
|
269
|
-
|
|
270
|
-
{ predicate: '
|
|
271
|
-
|
|
272
|
-
|
|
273
|
+
// Recursive case: ancestor(X, Z) :- parent(X, Y), ancestor(Y, Z)
|
|
274
|
+
program.addRule(JSON.stringify({
|
|
275
|
+
head: { predicate: 'ancestor', terms: ['X', 'Z'] },
|
|
276
|
+
body: [
|
|
277
|
+
{ predicate: 'parent', terms: ['X', 'Y'] },
|
|
278
|
+
{ predicate: 'ancestor', terms: ['Y', 'Z'] }
|
|
279
|
+
]
|
|
280
|
+
}));
|
|
273
281
|
|
|
274
|
-
// Evaluate
|
|
275
|
-
const
|
|
276
|
-
console.log(` Semi-naive evaluation completed
|
|
282
|
+
// Evaluate using semi-naive algorithm
|
|
283
|
+
const evalResult = evaluateDatalog(program);
|
|
284
|
+
console.log(` Semi-naive evaluation completed`);
|
|
285
|
+
console.log(` Result: ${evalResult}`);
|
|
277
286
|
console.log();
|
|
278
287
|
|
|
279
|
-
// Query
|
|
280
|
-
const results =
|
|
281
|
-
|
|
288
|
+
// Query ancestors
|
|
289
|
+
const results = queryDatalog(program, 'ancestor');
|
|
290
|
+
const parsed = JSON.parse(results);
|
|
291
|
+
console.log(' Query: ancestor(?who, ?descendant)');
|
|
282
292
|
console.log(' Results:');
|
|
283
|
-
for (const r of
|
|
284
|
-
console.log(` -
|
|
293
|
+
for (const r of parsed) {
|
|
294
|
+
console.log(` - ${r.terms[0]} is ancestor of ${r.terms[1]}`);
|
|
285
295
|
}
|
|
286
296
|
console.log();
|
|
287
297
|
}
|
|
@@ -418,23 +428,28 @@ function demoHyperMindAgents(): void {
|
|
|
418
428
|
console.log(' Agent Creation (TypeScript):');
|
|
419
429
|
console.log(' ------------------------------');
|
|
420
430
|
console.log(`
|
|
421
|
-
import {
|
|
431
|
+
import { HyperMindAgent } from 'rust-kgdb';
|
|
422
432
|
|
|
423
|
-
|
|
433
|
+
// Spawn a HyperMind agent with typed tools
|
|
434
|
+
const agent = await HyperMindAgent.spawn({
|
|
424
435
|
name: 'fraud-detector',
|
|
425
|
-
model:
|
|
436
|
+
model: 'claude-sonnet-4', // or 'gpt-4o', 'claude-opus-4', 'mock'
|
|
426
437
|
tools: [
|
|
427
|
-
'kg.sparql.query',
|
|
428
|
-
'kg.motif.find',
|
|
429
|
-
'kg.datalog.apply',
|
|
438
|
+
'kg.sparql.query', // Query -> BindingSet
|
|
439
|
+
'kg.motif.find', // Graph -> PatternSet
|
|
440
|
+
'kg.datalog.apply', // Rules -> InferredFacts
|
|
430
441
|
],
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
wasmSandbox: true,
|
|
442
|
+
endpoint: 'http://localhost:30080', // K8s cluster endpoint
|
|
443
|
+
tracing: true,
|
|
434
444
|
});
|
|
435
445
|
|
|
436
|
-
|
|
437
|
-
await agent.
|
|
446
|
+
// Execute with natural language
|
|
447
|
+
const result = await agent.call('Find circular payment patterns');
|
|
448
|
+
console.log(result.sparql); // Generated SPARQL
|
|
449
|
+
console.log(result.results); // Query results
|
|
450
|
+
|
|
451
|
+
// Get execution trace for audit
|
|
452
|
+
const trace = agent.getTrace();
|
|
438
453
|
`);
|
|
439
454
|
console.log();
|
|
440
455
|
}
|