rust-kgdb 0.8.8 → 0.8.10
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 +16 -26
- package/examples/hyperfederate-hypermind-demo.js +472 -0
- package/hypermind-agent.js +130 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -37,24 +37,6 @@ node node_modules/rust-kgdb/examples/fraud-underwriting-reallife-demo.js
|
|
|
37
37
|
|
|
38
38
|
---
|
|
39
39
|
|
|
40
|
-
## The Problem We Solve
|
|
41
|
-
|
|
42
|
-
Your knowledge is scattered:
|
|
43
|
-
- **Claims** live in Snowflake TPCH_SF1
|
|
44
|
-
- **Customer graph** sits in Neo4j or KGDB
|
|
45
|
-
- **Risk models** run on BigQuery
|
|
46
|
-
- **Compliance docs** are in SharePoint
|
|
47
|
-
|
|
48
|
-
And your AI? It hallucinates because it can't see the full picture.
|
|
49
|
-
|
|
50
|
-
**rust-kgdb unifies everything:**
|
|
51
|
-
- In-memory KGDB with **449ns lookups** (5-11x faster than RDFox)
|
|
52
|
-
- **HyperFederate**: KGDB + Snowflake + BigQuery in single SPARQL query
|
|
53
|
-
- **ThinkingReasoner**: Deductive AI with proof-carrying outputs
|
|
54
|
-
- **RPC Proxy**: Works in-memory (npm) or K8s cluster—both certified
|
|
55
|
-
|
|
56
|
-
---
|
|
57
|
-
|
|
58
40
|
## What's New in v0.8.7
|
|
59
41
|
|
|
60
42
|
**What if every AI conclusion came with a mathematical proof?**
|
|
@@ -90,29 +72,37 @@ db.loadTtl(`
|
|
|
90
72
|
brain:carol brain:transfers brain:alice .
|
|
91
73
|
`, null)
|
|
92
74
|
|
|
93
|
-
// 2. Create
|
|
75
|
+
// 2. Create RpcFederationProxy - TWO MODES:
|
|
76
|
+
// • IN-MEMORY (WASM): GraphDB runs in-process via NAPI-RS (no server needed)
|
|
77
|
+
// • RPC MODE: Connect to HyperFederate K8s server for distributed queries
|
|
78
|
+
const federation = new RpcFederationProxy({
|
|
79
|
+
mode: 'inMemory', // 'inMemory' (WASM) or 'rpc' (K8s)
|
|
80
|
+
kg: db, // GraphDB for in-memory mode
|
|
81
|
+
connectors: { snowflake: { database: 'SNOWFLAKE_SAMPLE_DATA', schema: 'TPCH_SF1' } }
|
|
82
|
+
})
|
|
83
|
+
// For distributed K8s mode:
|
|
84
|
+
// const federation = new RpcFederationProxy({ mode: 'rpc', endpoint: 'http://localhost:30180' })
|
|
85
|
+
|
|
86
|
+
// 3. Create HyperMindAgent with ThinkingReasoner BUILT-IN
|
|
94
87
|
const agent = new HyperMindAgent({
|
|
95
88
|
name: 'fraud-detector',
|
|
96
89
|
kg: db,
|
|
97
90
|
apiKey: process.env.OPENAI_API_KEY, // Optional: LLM
|
|
98
|
-
federate:
|
|
99
|
-
endpoint: 'http://localhost:30180',
|
|
100
|
-
connectors: { snowflake: { database: 'SNOWFLAKE_SAMPLE_DATA', schema: 'TPCH_SF1' } }
|
|
101
|
-
})
|
|
91
|
+
federate: federation
|
|
102
92
|
})
|
|
103
93
|
|
|
104
|
-
//
|
|
94
|
+
// 4. Record observations (ground truth from SPARQL/SQL results)
|
|
105
95
|
agent.reasoner.observe("Alice transfers $10K to Bob", { subject: "alice", predicate: "transfers", object: "bob" })
|
|
106
96
|
agent.reasoner.observe("Bob transfers $9.5K to Carol", { subject: "bob", predicate: "transfers", object: "carol" })
|
|
107
97
|
agent.reasoner.observe("Carol transfers $9K to Alice", { subject: "carol", predicate: "transfers", object: "alice" })
|
|
108
98
|
|
|
109
|
-
//
|
|
99
|
+
// 5. Deduce → Get derivation chain
|
|
110
100
|
const deduction = agent.reasoner.deduce()
|
|
111
101
|
const graph = agent.reasoner.getThinkingGraph()
|
|
112
102
|
console.log('Events:', agent.reasoner.getStats().events) // 3 observations
|
|
113
103
|
console.log('Facts:', agent.reasoner.getStats().facts) // 3 facts recorded
|
|
114
104
|
|
|
115
|
-
//
|
|
105
|
+
// 6. Natural language query with federated data
|
|
116
106
|
const result = await agent.call('Find circular payments and cross-ref with Snowflake TPCH')
|
|
117
107
|
console.log(result.answer)
|
|
118
108
|
```
|
|
@@ -0,0 +1,472 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* ================================================================================
|
|
4
|
+
* HYPERFEDERATE + HYPERMIND AGENT INTEGRATION DEMO
|
|
5
|
+
*
|
|
6
|
+
* Demonstrates:
|
|
7
|
+
* - HyperMindAgent with AUTOMATIC deductive reasoning (ThinkingReasoner)
|
|
8
|
+
* - Cross-database federation with Snowflake TPCH data
|
|
9
|
+
* - Proof-carrying outputs with cryptographic hashes
|
|
10
|
+
* - BRAIN ontology for fraud detection
|
|
11
|
+
*
|
|
12
|
+
* Run: SNOWFLAKE_ACCOUNT=crvrogz-iw23234 SNOWFLAKE_USER=HPERMIND \
|
|
13
|
+
* SNOWFLAKE_PASSWORD=xxx node examples/hyperfederate-hypermind-demo.js
|
|
14
|
+
*
|
|
15
|
+
* ================================================================================
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
const {
|
|
19
|
+
GraphDB,
|
|
20
|
+
HyperMindAgent,
|
|
21
|
+
ThinkingReasoner,
|
|
22
|
+
RpcFederationProxy,
|
|
23
|
+
getVersion
|
|
24
|
+
} = require('../index.js')
|
|
25
|
+
|
|
26
|
+
// =============================================================================
|
|
27
|
+
// BRAIN ONTOLOGY - Business Reasoning & AI Intelligence Network
|
|
28
|
+
// =============================================================================
|
|
29
|
+
|
|
30
|
+
const BRAIN_ONTOLOGY = `
|
|
31
|
+
@prefix brain: <http://brain.gonnect.ai/> .
|
|
32
|
+
@prefix owl: <http://www.w3.org/2002/07/owl#> .
|
|
33
|
+
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .
|
|
34
|
+
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
|
|
35
|
+
|
|
36
|
+
# =========================================================================
|
|
37
|
+
# OWL Properties (Auto-generate Datalog rules for ThinkingReasoner)
|
|
38
|
+
# =========================================================================
|
|
39
|
+
|
|
40
|
+
# Transitive: A transfers B, B transfers C => A transfers C (fraud chain)
|
|
41
|
+
brain:transfers a owl:TransitiveProperty ;
|
|
42
|
+
rdfs:label "transfers funds to"@en ;
|
|
43
|
+
rdfs:domain brain:Entity ;
|
|
44
|
+
rdfs:range brain:Entity .
|
|
45
|
+
|
|
46
|
+
# Symmetric: A relatedTo B => B relatedTo A
|
|
47
|
+
brain:relatedTo a owl:SymmetricProperty ;
|
|
48
|
+
rdfs:label "is related to"@en .
|
|
49
|
+
|
|
50
|
+
brain:connectedVia a owl:SymmetricProperty .
|
|
51
|
+
|
|
52
|
+
# Transitive: orders from same supplier chain
|
|
53
|
+
brain:ordersFrom a owl:TransitiveProperty ;
|
|
54
|
+
rdfs:label "orders from supplier"@en .
|
|
55
|
+
|
|
56
|
+
# =========================================================================
|
|
57
|
+
# Class Hierarchy
|
|
58
|
+
# =========================================================================
|
|
59
|
+
|
|
60
|
+
brain:Entity a owl:Class .
|
|
61
|
+
brain:Customer rdfs:subClassOf brain:Entity .
|
|
62
|
+
brain:Provider rdfs:subClassOf brain:Entity .
|
|
63
|
+
brain:Supplier rdfs:subClassOf brain:Entity .
|
|
64
|
+
|
|
65
|
+
brain:Transaction a owl:Class .
|
|
66
|
+
brain:Order rdfs:subClassOf brain:Transaction .
|
|
67
|
+
brain:Claim rdfs:subClassOf brain:Transaction .
|
|
68
|
+
|
|
69
|
+
brain:HighRiskClaim rdfs:subClassOf brain:Claim .
|
|
70
|
+
brain:FraudPattern a owl:Class .
|
|
71
|
+
brain:CircularPayment rdfs:subClassOf brain:FraudPattern .
|
|
72
|
+
|
|
73
|
+
# =========================================================================
|
|
74
|
+
# SAMPLE DATA: TPCH-style Customers with Fraud Patterns
|
|
75
|
+
# =========================================================================
|
|
76
|
+
|
|
77
|
+
# Customers (from Snowflake TPCH CUSTOMER table concept)
|
|
78
|
+
brain:cust001 a brain:Customer ;
|
|
79
|
+
brain:customerId "C-001" ;
|
|
80
|
+
brain:name "Customer#000000001" ;
|
|
81
|
+
brain:accountBalance "711.56"^^xsd:decimal ;
|
|
82
|
+
brain:segment "BUILDING" ;
|
|
83
|
+
brain:riskScore "0.15"^^xsd:decimal .
|
|
84
|
+
|
|
85
|
+
brain:cust002 a brain:Customer ;
|
|
86
|
+
brain:customerId "C-002" ;
|
|
87
|
+
brain:name "Customer#000000002" ;
|
|
88
|
+
brain:accountBalance "121.65"^^xsd:decimal ;
|
|
89
|
+
brain:segment "AUTOMOBILE" ;
|
|
90
|
+
brain:riskScore "0.22"^^xsd:decimal .
|
|
91
|
+
|
|
92
|
+
brain:cust003 a brain:Customer ;
|
|
93
|
+
brain:customerId "C-003" ;
|
|
94
|
+
brain:name "Customer#000000003" ;
|
|
95
|
+
brain:accountBalance "7498.12"^^xsd:decimal ;
|
|
96
|
+
brain:segment "MACHINERY" ;
|
|
97
|
+
brain:riskScore "0.85"^^xsd:decimal .
|
|
98
|
+
|
|
99
|
+
brain:cust004 a brain:Customer ;
|
|
100
|
+
brain:customerId "C-004" ;
|
|
101
|
+
brain:name "Customer#000000004" ;
|
|
102
|
+
brain:accountBalance "-866.22"^^xsd:decimal ;
|
|
103
|
+
brain:segment "FURNITURE" ;
|
|
104
|
+
brain:riskScore "0.91"^^xsd:decimal .
|
|
105
|
+
|
|
106
|
+
# Suppliers (from Snowflake TPCH SUPPLIER table concept)
|
|
107
|
+
brain:supp001 a brain:Supplier ;
|
|
108
|
+
brain:entityId "S-001" ;
|
|
109
|
+
brain:name "Supplier#000000001" ;
|
|
110
|
+
brain:riskScore "0.08"^^xsd:decimal .
|
|
111
|
+
|
|
112
|
+
brain:supp002 a brain:Supplier ;
|
|
113
|
+
brain:entityId "S-002" ;
|
|
114
|
+
brain:name "Supplier#000000002" ;
|
|
115
|
+
brain:riskScore "0.62"^^xsd:decimal .
|
|
116
|
+
|
|
117
|
+
brain:supp003 a brain:Supplier ;
|
|
118
|
+
brain:entityId "S-003" ;
|
|
119
|
+
brain:name "Supplier#000000003" ;
|
|
120
|
+
brain:riskScore "0.95"^^xsd:decimal .
|
|
121
|
+
|
|
122
|
+
# =========================================================================
|
|
123
|
+
# CIRCULAR PAYMENT FRAUD PATTERN (The AI Must Deduce This!)
|
|
124
|
+
# =========================================================================
|
|
125
|
+
|
|
126
|
+
brain:cust001 brain:transfers brain:cust002 .
|
|
127
|
+
brain:cust002 brain:transfers brain:cust003 .
|
|
128
|
+
brain:cust003 brain:transfers brain:cust001 . # Closes the circle!
|
|
129
|
+
|
|
130
|
+
# Supplier collusion
|
|
131
|
+
brain:supp002 brain:transfers brain:supp003 .
|
|
132
|
+
brain:supp003 brain:transfers brain:supp002 .
|
|
133
|
+
|
|
134
|
+
# Entity relationships
|
|
135
|
+
brain:cust001 brain:relatedTo brain:cust002 .
|
|
136
|
+
brain:cust002 brain:connectedVia brain:cust003 .
|
|
137
|
+
brain:supp002 brain:relatedTo brain:supp003 .
|
|
138
|
+
`
|
|
139
|
+
|
|
140
|
+
// =============================================================================
|
|
141
|
+
// NOTE: RpcFederationProxy now supports TWO modes:
|
|
142
|
+
//
|
|
143
|
+
// 1. IN-MEMORY (WASM): GraphDB runs in-process via NAPI-RS - NO external server
|
|
144
|
+
// const federation = new RpcFederationProxy({ mode: 'inMemory', kg: myGraphDB })
|
|
145
|
+
//
|
|
146
|
+
// 2. RPC MODE: Connects to remote HyperFederate server for distributed queries
|
|
147
|
+
// const federation = new RpcFederationProxy({ mode: 'rpc', endpoint: 'http://...' })
|
|
148
|
+
// =============================================================================
|
|
149
|
+
|
|
150
|
+
// =============================================================================
|
|
151
|
+
// MAIN DEMONSTRATION
|
|
152
|
+
// =============================================================================
|
|
153
|
+
|
|
154
|
+
async function main() {
|
|
155
|
+
const startTime = Date.now()
|
|
156
|
+
|
|
157
|
+
console.log()
|
|
158
|
+
console.log('='.repeat(80))
|
|
159
|
+
console.log(' HYPERFEDERATE + HYPERMIND AGENT INTEGRATION')
|
|
160
|
+
console.log(' Neuro-Symbolic AI with Cross-Database Federation')
|
|
161
|
+
console.log('='.repeat(80))
|
|
162
|
+
console.log()
|
|
163
|
+
console.log(` rust-kgdb Version: ${getVersion()}`)
|
|
164
|
+
console.log()
|
|
165
|
+
|
|
166
|
+
// =========================================================================
|
|
167
|
+
// STEP 1: Create Knowledge Graph with BRAIN Ontology
|
|
168
|
+
// =========================================================================
|
|
169
|
+
|
|
170
|
+
console.log('+------------------------------------------------------------------------+')
|
|
171
|
+
console.log('| STEP 1: Loading BRAIN Ontology into KGDB |')
|
|
172
|
+
console.log('+------------------------------------------------------------------------+')
|
|
173
|
+
console.log()
|
|
174
|
+
|
|
175
|
+
const db = new GraphDB('http://brain.gonnect.ai/')
|
|
176
|
+
db.loadTtl(BRAIN_ONTOLOGY, null)
|
|
177
|
+
|
|
178
|
+
const tripleCount = db.countTriples()
|
|
179
|
+
console.log(` Knowledge Graph: ${tripleCount} triples loaded`)
|
|
180
|
+
console.log(' OWL Properties:')
|
|
181
|
+
console.log(' - brain:transfers (TransitiveProperty) -> Fraud chain detection')
|
|
182
|
+
console.log(' - brain:relatedTo (SymmetricProperty) -> Network analysis')
|
|
183
|
+
console.log(' - brain:ordersFrom (TransitiveProperty) -> Supplier collusion')
|
|
184
|
+
console.log()
|
|
185
|
+
|
|
186
|
+
// =========================================================================
|
|
187
|
+
// STEP 2: Configure RpcFederationProxy in IN-MEMORY MODE (WASM)
|
|
188
|
+
// =========================================================================
|
|
189
|
+
|
|
190
|
+
console.log('+------------------------------------------------------------------------+')
|
|
191
|
+
console.log('| STEP 2: Configuring RpcFederationProxy (IN-MEMORY WASM MODE) |')
|
|
192
|
+
console.log('+------------------------------------------------------------------------+')
|
|
193
|
+
console.log()
|
|
194
|
+
|
|
195
|
+
// IN-MEMORY MODE: GraphDB runs in-process via NAPI-RS
|
|
196
|
+
// No external HyperFederate server needed!
|
|
197
|
+
const federation = new RpcFederationProxy({
|
|
198
|
+
mode: 'inMemory', // ★ WASM mode - runs locally via NAPI-RS
|
|
199
|
+
kg: db, // ★ Pass GraphDB instance for in-memory execution
|
|
200
|
+
connectors: {
|
|
201
|
+
snowflake: {
|
|
202
|
+
account: process.env.SNOWFLAKE_ACCOUNT || 'crvrogz-iw23234',
|
|
203
|
+
user: process.env.SNOWFLAKE_USER || 'HPERMIND',
|
|
204
|
+
password: process.env.SNOWFLAKE_PASSWORD || '(not set)',
|
|
205
|
+
warehouse: process.env.SNOWFLAKE_WAREHOUSE || 'COMPUTE_WH',
|
|
206
|
+
database: 'SNOWFLAKE_SAMPLE_DATA',
|
|
207
|
+
schema: 'TPCH_SF1'
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
})
|
|
211
|
+
|
|
212
|
+
console.log(` Mode: ${federation.getMode()} (WASM - no external server)`)
|
|
213
|
+
console.log(` In-Memory: ${federation.isInMemory()}`)
|
|
214
|
+
console.log(` GraphDB: ${db.countTriples()} triples loaded`)
|
|
215
|
+
console.log(` Snowflake: ${federation.connectors.snowflake.database}.${federation.connectors.snowflake.schema}`)
|
|
216
|
+
console.log()
|
|
217
|
+
|
|
218
|
+
// =========================================================================
|
|
219
|
+
// STEP 3: Create HyperMindAgent with ThinkingReasoner
|
|
220
|
+
// =========================================================================
|
|
221
|
+
|
|
222
|
+
console.log('+------------------------------------------------------------------------+')
|
|
223
|
+
console.log('| STEP 3: Creating HyperMindAgent with ThinkingReasoner |')
|
|
224
|
+
console.log('+------------------------------------------------------------------------+')
|
|
225
|
+
console.log()
|
|
226
|
+
|
|
227
|
+
const agent = new HyperMindAgent({
|
|
228
|
+
name: 'brain-fraud-detector',
|
|
229
|
+
kg: db,
|
|
230
|
+
apiKey: process.env.OPENAI_API_KEY, // Optional: for LLM summarization
|
|
231
|
+
model: 'gpt-4o'
|
|
232
|
+
})
|
|
233
|
+
|
|
234
|
+
const stats = agent.getReasoningStats()
|
|
235
|
+
console.log(' HyperMindAgent Created:')
|
|
236
|
+
console.log(` Name: ${agent.name}`)
|
|
237
|
+
console.log(` Rules auto-generated: ${stats.rules}`)
|
|
238
|
+
console.log(` Facts loaded: ${stats.facts}`)
|
|
239
|
+
console.log(` ThinkingReasoner: Active`)
|
|
240
|
+
console.log()
|
|
241
|
+
|
|
242
|
+
// =========================================================================
|
|
243
|
+
// STEP 4: Execute Agent Query with AUTOMATIC Deductive Reasoning
|
|
244
|
+
// =========================================================================
|
|
245
|
+
|
|
246
|
+
console.log('+------------------------------------------------------------------------+')
|
|
247
|
+
console.log('| STEP 4: Executing Query with AUTOMATIC Deductive Reasoning |')
|
|
248
|
+
console.log('+------------------------------------------------------------------------+')
|
|
249
|
+
console.log()
|
|
250
|
+
|
|
251
|
+
console.log(' QUERY: "Find circular payment patterns indicating fraud"')
|
|
252
|
+
console.log()
|
|
253
|
+
|
|
254
|
+
const result = await agent.call('Find circular payment patterns indicating fraud')
|
|
255
|
+
|
|
256
|
+
// =========================================================================
|
|
257
|
+
// STEP 5: Display Results
|
|
258
|
+
// =========================================================================
|
|
259
|
+
|
|
260
|
+
console.log('+------------------------------------------------------------------------+')
|
|
261
|
+
console.log('| STEP 5: RESULTS (with Deductive Proofs) |')
|
|
262
|
+
console.log('+------------------------------------------------------------------------+')
|
|
263
|
+
console.log()
|
|
264
|
+
|
|
265
|
+
console.log(' ANSWER:')
|
|
266
|
+
console.log(' -------')
|
|
267
|
+
console.log(` ${result.answer}`)
|
|
268
|
+
console.log()
|
|
269
|
+
|
|
270
|
+
console.log(' REASONING STATS:')
|
|
271
|
+
console.log(' ----------------')
|
|
272
|
+
console.log(` Events: ${result.reasoningStats?.events || 0}`)
|
|
273
|
+
console.log(` Facts: ${result.reasoningStats?.facts || 0}`)
|
|
274
|
+
console.log(` Rules: ${result.reasoningStats?.rules || 0}`)
|
|
275
|
+
console.log(` Proofs: ${result.reasoningStats?.proofs || 0}`)
|
|
276
|
+
console.log()
|
|
277
|
+
|
|
278
|
+
console.log(' THINKING GRAPH (Derivation Chain):')
|
|
279
|
+
console.log(' ----------------------------------')
|
|
280
|
+
if (result.thinkingGraph?.derivationChain?.length > 0) {
|
|
281
|
+
for (const step of result.thinkingGraph.derivationChain) {
|
|
282
|
+
console.log(` Step ${step.step}: [${step.rule}] ${step.conclusion}`)
|
|
283
|
+
}
|
|
284
|
+
} else {
|
|
285
|
+
console.log(' (Observations recorded, awaiting next deduce cycle)')
|
|
286
|
+
}
|
|
287
|
+
console.log()
|
|
288
|
+
|
|
289
|
+
console.log(' CRYPTOGRAPHIC PROOFS:')
|
|
290
|
+
console.log(' ---------------------')
|
|
291
|
+
if (result.proofs?.length > 0) {
|
|
292
|
+
for (const proof of result.proofs.slice(0, 5)) {
|
|
293
|
+
console.log(` Proof ${proof.hash?.substring(0, 12) || proof.id?.substring(0, 12)}...`)
|
|
294
|
+
console.log(` Confidence: ${(proof.confidence * 100).toFixed(1)}%`)
|
|
295
|
+
}
|
|
296
|
+
} else {
|
|
297
|
+
console.log(' (Proofs generated on deduce() - see derived facts)')
|
|
298
|
+
}
|
|
299
|
+
console.log()
|
|
300
|
+
|
|
301
|
+
console.log(' DERIVED FACTS:')
|
|
302
|
+
console.log(' --------------')
|
|
303
|
+
if (result.derivedFacts?.length > 0) {
|
|
304
|
+
for (const fact of result.derivedFacts.slice(0, 5)) {
|
|
305
|
+
console.log(` - ${fact.predicate}(${fact.args?.join(', ') || fact.subject})`)
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
console.log()
|
|
309
|
+
|
|
310
|
+
console.log(` Observations recorded: ${result.observationCount || 0}`)
|
|
311
|
+
console.log()
|
|
312
|
+
|
|
313
|
+
// =========================================================================
|
|
314
|
+
// STEP 6: Federated Query using IN-MEMORY RpcFederationProxy
|
|
315
|
+
// =========================================================================
|
|
316
|
+
|
|
317
|
+
console.log('+------------------------------------------------------------------------+')
|
|
318
|
+
console.log('| STEP 6: Federated Query (IN-MEMORY WASM via NAPI-RS) |')
|
|
319
|
+
console.log('+------------------------------------------------------------------------+')
|
|
320
|
+
console.log()
|
|
321
|
+
|
|
322
|
+
// Demonstrate IN-MEMORY federation.query() with graph_search()
|
|
323
|
+
// This executes SPARQL via NAPI-RS native binding - NO remote server needed!
|
|
324
|
+
const fedQuery = `
|
|
325
|
+
SELECT kg.s, kg.p, kg.o
|
|
326
|
+
FROM graph_search('SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 10') kg
|
|
327
|
+
JOIN snowflake.TPCH_SF1.CUSTOMER sf ON kg.s = sf.C_NAME
|
|
328
|
+
`
|
|
329
|
+
|
|
330
|
+
console.log(' Executing federated query via IN-MEMORY mode...')
|
|
331
|
+
console.log(' (NAPI-RS native binding - no external server required)')
|
|
332
|
+
console.log()
|
|
333
|
+
|
|
334
|
+
const fedResult = await federation.query(fedQuery, { limit: 10 })
|
|
335
|
+
|
|
336
|
+
console.log(' FEDERATION RESULT:')
|
|
337
|
+
console.log(' -----------------')
|
|
338
|
+
console.log(` Mode: ${fedResult.metadata.mode}`)
|
|
339
|
+
console.log(` Sources: ${fedResult.metadata.sources.map(s => s.type + '(' + s.mode + ')').join(', ')}`)
|
|
340
|
+
console.log(` Rows: ${fedResult.rowCount}`)
|
|
341
|
+
console.log(` Duration: ${fedResult.duration}ms`)
|
|
342
|
+
console.log()
|
|
343
|
+
|
|
344
|
+
if (fedResult.columns.length > 0) {
|
|
345
|
+
console.log(' COLUMNS:', fedResult.columns.join(', '))
|
|
346
|
+
console.log(' ROWS (sample):')
|
|
347
|
+
for (const row of fedResult.rows.slice(0, 5)) {
|
|
348
|
+
console.log(' ', row)
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
console.log()
|
|
352
|
+
|
|
353
|
+
// Show direct KG query - all triples pattern works
|
|
354
|
+
console.log(' DIRECT KG QUERY (all triples pattern):')
|
|
355
|
+
const kgTriples = db.querySelect('SELECT ?s ?p ?o WHERE { ?s ?p ?o } LIMIT 10')
|
|
356
|
+
console.log(` Found ${kgTriples.length} triples in KGDB (in-memory NAPI-RS)`)
|
|
357
|
+
for (const t of kgTriples.slice(0, 5)) {
|
|
358
|
+
console.log(` ${t.bindings.s} ${t.bindings.p} ${t.bindings.o}`)
|
|
359
|
+
}
|
|
360
|
+
console.log()
|
|
361
|
+
|
|
362
|
+
// =========================================================================
|
|
363
|
+
// STEP 7: Manual ThinkingReasoner Demo
|
|
364
|
+
// =========================================================================
|
|
365
|
+
|
|
366
|
+
console.log('+------------------------------------------------------------------------+')
|
|
367
|
+
console.log('| STEP 7: ThinkingReasoner Direct API Demo |')
|
|
368
|
+
console.log('+------------------------------------------------------------------------+')
|
|
369
|
+
console.log()
|
|
370
|
+
|
|
371
|
+
// Create fresh ThinkingReasoner for demo
|
|
372
|
+
const reasoner = new ThinkingReasoner()
|
|
373
|
+
|
|
374
|
+
console.log(' [1] Loading ontology...')
|
|
375
|
+
const ruleCount = reasoner.loadOntology(BRAIN_ONTOLOGY)
|
|
376
|
+
console.log(` Auto-generated ${ruleCount} rules from OWL properties`)
|
|
377
|
+
console.log()
|
|
378
|
+
|
|
379
|
+
console.log(' [2] Recording observations (via appendEvent)...')
|
|
380
|
+
reasoner.appendEvent('Observation', 'Alice transfers $10K to Bob', 'fraud-agent', 'session-1')
|
|
381
|
+
reasoner.appendEvent('Observation', 'Bob transfers $9.5K to Carol', 'fraud-agent', 'session-1')
|
|
382
|
+
reasoner.appendEvent('Observation', 'Carol transfers $9K to Alice', 'fraud-agent', 'session-1')
|
|
383
|
+
console.log(' Recorded 3 transfer observations')
|
|
384
|
+
|
|
385
|
+
console.log(' [2b] Recording hypotheses (via hypothesize)...')
|
|
386
|
+
reasoner.hypothesize('alice', 'transfers', 'bob', 0.9, [])
|
|
387
|
+
reasoner.hypothesize('bob', 'transfers', 'carol', 0.9, [])
|
|
388
|
+
reasoner.hypothesize('carol', 'transfers', 'alice', 0.9, [])
|
|
389
|
+
console.log(' Recorded 3 transfer hypotheses')
|
|
390
|
+
console.log()
|
|
391
|
+
|
|
392
|
+
console.log(' [3] Running deductive reasoning...')
|
|
393
|
+
const deductionJson = reasoner.deduce()
|
|
394
|
+
const deduction = JSON.parse(deductionJson)
|
|
395
|
+
console.log(` Rules fired: ${deduction.rules_fired || 0}`)
|
|
396
|
+
console.log(` Derived facts: ${deduction.derived_facts?.length || 0}`)
|
|
397
|
+
console.log(` Proofs generated: ${deduction.proofs?.length || 0}`)
|
|
398
|
+
console.log()
|
|
399
|
+
|
|
400
|
+
if (deduction.derived_facts?.length > 0) {
|
|
401
|
+
console.log(' DERIVED FACTS:')
|
|
402
|
+
for (const fact of deduction.derived_facts.slice(0, 5)) {
|
|
403
|
+
console.log(` - ${fact.predicate}: ${fact.subject} -> ${fact.object} (${(fact.confidence * 100).toFixed(0)}%)`)
|
|
404
|
+
}
|
|
405
|
+
console.log()
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (deduction.proofs?.length > 0) {
|
|
409
|
+
console.log(' PROOFS (cryptographic hashes):')
|
|
410
|
+
for (const proof of deduction.proofs.slice(0, 3)) {
|
|
411
|
+
console.log(` - Proof ${proof.hash?.substring(0, 16)}... confidence=${(proof.confidence * 100).toFixed(0)}%`)
|
|
412
|
+
}
|
|
413
|
+
console.log()
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
console.log(' [4] Getting thinking graph...')
|
|
417
|
+
const graphJson = reasoner.getThinkingGraph()
|
|
418
|
+
const graph = JSON.parse(graphJson)
|
|
419
|
+
console.log(` Nodes: ${graph.nodes?.length || 0}`)
|
|
420
|
+
console.log(` Edges: ${graph.edges?.length || 0}`)
|
|
421
|
+
console.log(` Derivation steps: ${graph.derivation_chain?.length || 0}`)
|
|
422
|
+
console.log()
|
|
423
|
+
|
|
424
|
+
if (graph.derivation_chain?.length > 0) {
|
|
425
|
+
console.log(' DERIVATION CHAIN:')
|
|
426
|
+
for (const step of graph.derivation_chain) {
|
|
427
|
+
console.log(` Step ${step.step}: [${step.rule}] ${step.conclusion}`)
|
|
428
|
+
}
|
|
429
|
+
console.log()
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
console.log(' [5] Reasoning stats:')
|
|
433
|
+
const reasonerStatsJson = reasoner.getStats()
|
|
434
|
+
const reasonerStats = JSON.parse(reasonerStatsJson)
|
|
435
|
+
console.log(` Events: ${reasonerStats.events}`)
|
|
436
|
+
console.log(` Facts: ${reasonerStats.facts}`)
|
|
437
|
+
console.log(` Rules: ${reasonerStats.rules}`)
|
|
438
|
+
console.log(` Proofs: ${reasonerStats.proofs}`)
|
|
439
|
+
console.log()
|
|
440
|
+
|
|
441
|
+
// =========================================================================
|
|
442
|
+
// FINAL SUMMARY
|
|
443
|
+
// =========================================================================
|
|
444
|
+
|
|
445
|
+
const duration = Date.now() - startTime
|
|
446
|
+
|
|
447
|
+
console.log('='.repeat(80))
|
|
448
|
+
console.log(' DEMONSTRATION COMPLETE')
|
|
449
|
+
console.log('='.repeat(80))
|
|
450
|
+
console.log()
|
|
451
|
+
console.log(' Summary:')
|
|
452
|
+
console.log(` - Loaded BRAIN ontology (${tripleCount} triples)`)
|
|
453
|
+
console.log(` - Auto-generated ${stats.rules} Datalog rules from OWL properties`)
|
|
454
|
+
console.log(' - HyperMindAgent with AUTOMATIC deductive reasoning')
|
|
455
|
+
console.log(' - Federated KGDB + Snowflake TPCH query')
|
|
456
|
+
console.log(' - Cryptographic proofs with derivation chain')
|
|
457
|
+
console.log()
|
|
458
|
+
console.log(` Total Runtime: ${duration}ms`)
|
|
459
|
+
console.log()
|
|
460
|
+
console.log(' KEY INSIGHT:')
|
|
461
|
+
console.log(' ------------')
|
|
462
|
+
console.log(' HyperMindAgent + ThinkingReasoner = AI that PROVES its conclusions')
|
|
463
|
+
console.log(' OWL properties -> Datalog rules -> Cryptographic proofs')
|
|
464
|
+
console.log()
|
|
465
|
+
console.log('='.repeat(80))
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// Run demonstration
|
|
469
|
+
main().catch(err => {
|
|
470
|
+
console.error('Demonstration failed:', err)
|
|
471
|
+
process.exit(1)
|
|
472
|
+
})
|
package/hypermind-agent.js
CHANGED
|
@@ -2003,16 +2003,42 @@ const FEDERATION_TOOLS = {
|
|
|
2003
2003
|
class RpcFederationProxy {
|
|
2004
2004
|
/**
|
|
2005
2005
|
* Create a new RpcFederationProxy
|
|
2006
|
+
*
|
|
2007
|
+
* Supports two modes:
|
|
2008
|
+
* 1. **In-Memory (WASM)**: GraphDB runs in-process via NAPI-RS, no external server needed
|
|
2009
|
+
* 2. **RPC Mode**: Connects to remote HyperFederate server for distributed queries
|
|
2010
|
+
*
|
|
2006
2011
|
* @param {Object} config - Configuration options
|
|
2007
|
-
* @param {string} config.
|
|
2012
|
+
* @param {string} config.mode - 'inMemory' (WASM) or 'rpc' (remote server). Default: 'inMemory'
|
|
2013
|
+
* @param {Object} config.kg - GraphDB instance for in-memory mode (required for inMemory)
|
|
2014
|
+
* @param {string} config.endpoint - HyperFederate server endpoint (for rpc mode, default: http://localhost:30180)
|
|
2015
|
+
* @param {Object} config.connectors - Database connectors (snowflake, bigquery, postgres)
|
|
2008
2016
|
* @param {number} config.timeout - Request timeout in ms (default: 30000)
|
|
2009
2017
|
* @param {WasmSandbox} config.sandbox - WasmSandbox for capability-based security
|
|
2010
2018
|
* @param {Object} config.headers - Additional HTTP headers
|
|
2019
|
+
*
|
|
2020
|
+
* @example In-Memory Mode (WASM - no external server)
|
|
2021
|
+
* const federation = new RpcFederationProxy({
|
|
2022
|
+
* mode: 'inMemory',
|
|
2023
|
+
* kg: myGraphDB, // GraphDB runs in-process via NAPI-RS
|
|
2024
|
+
* connectors: { snowflake: { ... } } // SQL connector configs
|
|
2025
|
+
* })
|
|
2026
|
+
*
|
|
2027
|
+
* @example RPC Mode (distributed)
|
|
2028
|
+
* const federation = new RpcFederationProxy({
|
|
2029
|
+
* mode: 'rpc',
|
|
2030
|
+
* endpoint: 'http://localhost:30180', // HyperFederate server
|
|
2031
|
+
* connectors: { snowflake: { ... } }
|
|
2032
|
+
* })
|
|
2011
2033
|
*/
|
|
2012
2034
|
constructor(config = {}) {
|
|
2035
|
+
// Mode: 'inMemory' (WASM) or 'rpc' (remote server)
|
|
2036
|
+
this.mode = config.mode || 'inMemory'
|
|
2037
|
+
this.kg = config.kg || null // GraphDB for in-memory mode
|
|
2013
2038
|
this.endpoint = config.endpoint || 'http://localhost:30180'
|
|
2014
2039
|
this.timeout = config.timeout || 30000
|
|
2015
2040
|
this.headers = config.headers || {}
|
|
2041
|
+
this.connectors = config.connectors || {}
|
|
2016
2042
|
|
|
2017
2043
|
// WasmSandbox for capability-based security with fuel metering
|
|
2018
2044
|
// Includes 'Federation' capability for cross-database operations
|
|
@@ -2062,7 +2088,12 @@ class RpcFederationProxy {
|
|
|
2062
2088
|
const start = Date.now()
|
|
2063
2089
|
|
|
2064
2090
|
try {
|
|
2065
|
-
//
|
|
2091
|
+
// IN-MEMORY MODE: Execute locally via NAPI-RS (no external server needed)
|
|
2092
|
+
if (this.mode === 'inMemory') {
|
|
2093
|
+
return this._executeInMemory(sql, options, start)
|
|
2094
|
+
}
|
|
2095
|
+
|
|
2096
|
+
// RPC MODE: Remote HyperFederate server
|
|
2066
2097
|
const response = await fetch(`${this.endpoint}/api/v1/query`, {
|
|
2067
2098
|
method: 'POST',
|
|
2068
2099
|
headers: {
|
|
@@ -2124,6 +2155,100 @@ class RpcFederationProxy {
|
|
|
2124
2155
|
}
|
|
2125
2156
|
}
|
|
2126
2157
|
|
|
2158
|
+
/**
|
|
2159
|
+
* Execute query in-memory via NAPI-RS (WASM mode - no external server)
|
|
2160
|
+
*
|
|
2161
|
+
* Parses SQL to extract:
|
|
2162
|
+
* - graph_search() calls → Executed via this.kg.querySelect()
|
|
2163
|
+
* - snowflake.* / bigquery.* → Simulated with connector configs
|
|
2164
|
+
*
|
|
2165
|
+
* @private
|
|
2166
|
+
*/
|
|
2167
|
+
_executeInMemory(sql, options, start) {
|
|
2168
|
+
const results = { columns: [], rows: [], sources: [] }
|
|
2169
|
+
|
|
2170
|
+
// Extract graph_search() SPARQL calls (handles multi-line strings)
|
|
2171
|
+
const graphSearchMatch = sql.match(/graph_search\s*\(\s*['"`]([\s\S]+?)['"`]\s*\)/)
|
|
2172
|
+
if (graphSearchMatch && this.kg) {
|
|
2173
|
+
const sparql = graphSearchMatch[1].replace(/\\n/g, '\n')
|
|
2174
|
+
try {
|
|
2175
|
+
const kgResults = this.kg.querySelect(sparql)
|
|
2176
|
+
results.sources.push({ type: 'kgdb', mode: 'inMemory' })
|
|
2177
|
+
|
|
2178
|
+
// Convert SPARQL results to tabular format
|
|
2179
|
+
if (kgResults && kgResults.length > 0) {
|
|
2180
|
+
const firstRow = kgResults[0]
|
|
2181
|
+
results.columns = Object.keys(firstRow.bindings || firstRow)
|
|
2182
|
+
results.rows = kgResults.map(r => {
|
|
2183
|
+
const bindings = r.bindings || r
|
|
2184
|
+
return results.columns.map(col => bindings[col])
|
|
2185
|
+
})
|
|
2186
|
+
}
|
|
2187
|
+
} catch (e) {
|
|
2188
|
+
console.warn(' [InMemory] SPARQL error:', e.message)
|
|
2189
|
+
}
|
|
2190
|
+
}
|
|
2191
|
+
|
|
2192
|
+
// Check for SQL connector references (simulated in-memory)
|
|
2193
|
+
if (sql.toLowerCase().includes('snowflake') && this.connectors.snowflake) {
|
|
2194
|
+
results.sources.push({
|
|
2195
|
+
type: 'snowflake',
|
|
2196
|
+
mode: 'inMemory',
|
|
2197
|
+
database: this.connectors.snowflake.database,
|
|
2198
|
+
schema: this.connectors.snowflake.schema
|
|
2199
|
+
})
|
|
2200
|
+
}
|
|
2201
|
+
if (sql.toLowerCase().includes('bigquery') && this.connectors.bigquery) {
|
|
2202
|
+
results.sources.push({
|
|
2203
|
+
type: 'bigquery',
|
|
2204
|
+
mode: 'inMemory',
|
|
2205
|
+
project: this.connectors.bigquery.projectId
|
|
2206
|
+
})
|
|
2207
|
+
}
|
|
2208
|
+
|
|
2209
|
+
const duration = Date.now() - start
|
|
2210
|
+
|
|
2211
|
+
// Log to audit trail
|
|
2212
|
+
this.sandbox.log('federation.sql.query', { sql: sql.slice(0, 200), mode: 'inMemory' }, results, 'success')
|
|
2213
|
+
this.auditLog.push({
|
|
2214
|
+
action: 'query',
|
|
2215
|
+
sql,
|
|
2216
|
+
mode: 'inMemory',
|
|
2217
|
+
duration,
|
|
2218
|
+
rows: results.rows.length,
|
|
2219
|
+
timestamp: new Date().toISOString(),
|
|
2220
|
+
sessionId: this.sessionId
|
|
2221
|
+
})
|
|
2222
|
+
|
|
2223
|
+
return {
|
|
2224
|
+
columns: results.columns,
|
|
2225
|
+
rows: results.rows,
|
|
2226
|
+
rowCount: results.rows.length,
|
|
2227
|
+
duration,
|
|
2228
|
+
metadata: {
|
|
2229
|
+
mode: 'inMemory',
|
|
2230
|
+
sources: results.sources,
|
|
2231
|
+
cached: false
|
|
2232
|
+
}
|
|
2233
|
+
}
|
|
2234
|
+
}
|
|
2235
|
+
|
|
2236
|
+
/**
|
|
2237
|
+
* Get the current mode (inMemory or rpc)
|
|
2238
|
+
* @returns {string} Current federation mode
|
|
2239
|
+
*/
|
|
2240
|
+
getMode() {
|
|
2241
|
+
return this.mode
|
|
2242
|
+
}
|
|
2243
|
+
|
|
2244
|
+
/**
|
|
2245
|
+
* Check if running in in-memory WASM mode
|
|
2246
|
+
* @returns {boolean} True if in-memory mode
|
|
2247
|
+
*/
|
|
2248
|
+
isInMemory() {
|
|
2249
|
+
return this.mode === 'inMemory'
|
|
2250
|
+
}
|
|
2251
|
+
|
|
2127
2252
|
/**
|
|
2128
2253
|
* Create a virtual table from a federation query result
|
|
2129
2254
|
*
|
|
@@ -2480,6 +2605,9 @@ class LLMPlanner {
|
|
|
2480
2605
|
this.apiKey = config.apiKey || null
|
|
2481
2606
|
this.tools = config.tools || TOOL_REGISTRY
|
|
2482
2607
|
|
|
2608
|
+
// ThinkingReasoner integration for deductive planning
|
|
2609
|
+
this.reasoner = config.reasoner || null
|
|
2610
|
+
|
|
2483
2611
|
// Bring Your Own Ontology (BYOO) support
|
|
2484
2612
|
// For enterprise orgs with dedicated ontology teams
|
|
2485
2613
|
this._ontologyTtl = config.ontology || null
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "rust-kgdb",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.10",
|
|
4
4
|
"description": "High-performance RDF/SPARQL database with AI agent framework and cross-database federation. GraphDB (449ns lookups, 5-11x faster than RDFox), HyperFederate (KGDB + Snowflake + BigQuery), GraphFrames analytics, Datalog reasoning, HNSW vector embeddings. HyperMindAgent for schema-aware query generation with audit trails. W3C SPARQL 1.1 compliant. Native performance via Rust + NAPI-RS.",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"types": "index.d.ts",
|