hebbrix 2.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/README.md +748 -0
- package/dist/index.d.mts +430 -0
- package/dist/index.d.ts +430 -0
- package/dist/index.js +594 -0
- package/dist/index.mjs +550 -0
- package/package.json +69 -0
package/README.md
ADDED
|
@@ -0,0 +1,748 @@
|
|
|
1
|
+
# Hebbrix TypeScript SDK
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/hebbrix)
|
|
4
|
+
[](https://www.typescriptlang.org/)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Official TypeScript/JavaScript SDK for the Hebbrix api - **the only memory API with Reinforcement Learning**.
|
|
8
|
+
|
|
9
|
+
## 🚀 Features
|
|
10
|
+
|
|
11
|
+
- ✅ **Complete API Coverage** - All 50+ endpoints supported
|
|
12
|
+
- ✅ **Reinforcement Learning** - Train AI agents to optimize memory operations
|
|
13
|
+
- ✅ **Temporal Knowledge Graphs** - Track facts over time with bi-temporal model
|
|
14
|
+
- ✅ **Procedural Memory** - Store and execute learned skills
|
|
15
|
+
- ✅ **Working Memory** - Short-term context buffer for conversations
|
|
16
|
+
- ✅ **Memory Consolidation** - Automatic compression of episodic memories
|
|
17
|
+
- ✅ **Promise-based** - Native async/await support
|
|
18
|
+
- ✅ **Type-safe** - Complete TypeScript type definitions
|
|
19
|
+
- ✅ **Universal** - Works in Node.js and browsers
|
|
20
|
+
- ✅ **Clean API** - Intuitive, developer-friendly interface
|
|
21
|
+
|
|
22
|
+
## 📦 Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install hebbrix
|
|
26
|
+
# or
|
|
27
|
+
yarn add hebbrix
|
|
28
|
+
# or
|
|
29
|
+
pnpm add hebbrix
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## 🔥 Quick Start
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { MemoryClient } from 'hebbrix';
|
|
36
|
+
|
|
37
|
+
const main = async () => {
|
|
38
|
+
// Initialize client
|
|
39
|
+
const client = new MemoryClient({ apiKey: 'hbx_your_api_key' });
|
|
40
|
+
|
|
41
|
+
// Create a collection
|
|
42
|
+
const collection = await client.collections.create({
|
|
43
|
+
name: 'My AI Agent',
|
|
44
|
+
description: 'Personal memory for my chatbot',
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
// Store a memory
|
|
48
|
+
const memory = await client.memories.create({
|
|
49
|
+
collection_id: collection.id,
|
|
50
|
+
content: 'User prefers dark mode and loves TypeScript',
|
|
51
|
+
importance: 0.9,
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
// Search memories
|
|
55
|
+
const results = await client.search({
|
|
56
|
+
query: 'What programming language does user like?',
|
|
57
|
+
collection_id: collection.id,
|
|
58
|
+
limit: 5,
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
console.log(results);
|
|
62
|
+
|
|
63
|
+
// Reason over memories
|
|
64
|
+
const answer = await client.reason({
|
|
65
|
+
query: 'What are user preferences?',
|
|
66
|
+
provider: 'gemini',
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
console.log(answer.answer);
|
|
70
|
+
console.log(answer.sources);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
main();
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
## 📚 Complete API Guide
|
|
77
|
+
|
|
78
|
+
### 1. Authentication
|
|
79
|
+
|
|
80
|
+
```typescript
|
|
81
|
+
import { MemoryClient } from 'hebbrix';
|
|
82
|
+
|
|
83
|
+
// Using API key
|
|
84
|
+
const client = new MemoryClient({ apiKey: 'hbx_...' });
|
|
85
|
+
|
|
86
|
+
// Or register a new user
|
|
87
|
+
const authResponse = await client.auth.register(
|
|
88
|
+
'user@example.com',
|
|
89
|
+
'secure_password',
|
|
90
|
+
'John Doe'
|
|
91
|
+
);
|
|
92
|
+
|
|
93
|
+
// Login
|
|
94
|
+
const loginResponse = await client.auth.login(
|
|
95
|
+
'user@example.com',
|
|
96
|
+
'secure_password'
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
// Create API key
|
|
100
|
+
const apiKey = await client.auth.createApiKey('My App Key');
|
|
101
|
+
console.log(apiKey.api_key); // hbx_...
|
|
102
|
+
|
|
103
|
+
// Get current user
|
|
104
|
+
const user = await client.auth.getMe();
|
|
105
|
+
console.log(user.email);
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### 2. Collections
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
// Create collection
|
|
112
|
+
const collection = await client.collections.create({
|
|
113
|
+
name: 'Research Notes',
|
|
114
|
+
description: 'AI research papers and notes',
|
|
115
|
+
metadata: { category: 'research' },
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// List collections
|
|
119
|
+
const collections = await client.collections.list({ limit: 10, skip: 0 });
|
|
120
|
+
|
|
121
|
+
// Get collection
|
|
122
|
+
const retrieved = await client.collections.get(collection.id);
|
|
123
|
+
|
|
124
|
+
// Update collection
|
|
125
|
+
const updated = await client.collections.update(collection.id, {
|
|
126
|
+
name: 'Updated Name',
|
|
127
|
+
description: 'New description',
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Delete collection
|
|
131
|
+
await client.collections.delete(collection.id);
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
### 3. Memories (Episodic)
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
// Create memory
|
|
138
|
+
const memory = await client.memories.create({
|
|
139
|
+
collection_id: collection.id,
|
|
140
|
+
content: 'User completed onboarding tutorial',
|
|
141
|
+
importance: 0.7,
|
|
142
|
+
metadata: { event: 'onboarding', timestamp: new Date().toISOString() },
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
// List memories
|
|
146
|
+
const memories = await client.memories.list({
|
|
147
|
+
collection_id: collection.id,
|
|
148
|
+
limit: 50,
|
|
149
|
+
skip: 0,
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
// Get memory with metadata
|
|
153
|
+
const retrieved = await client.memories.get(memory.id);
|
|
154
|
+
console.log(retrieved.content);
|
|
155
|
+
console.log(retrieved.importance);
|
|
156
|
+
console.log(retrieved.metadata);
|
|
157
|
+
|
|
158
|
+
// Update memory
|
|
159
|
+
const updated = await client.memories.update(memory.id, {
|
|
160
|
+
content: 'Updated content',
|
|
161
|
+
importance: 0.9,
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
// Delete memory
|
|
165
|
+
await client.memories.delete(memory.id);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
### 4. Search & Retrieval
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
// Hybrid search (Vector + BM25)
|
|
172
|
+
const results = await client.search({
|
|
173
|
+
query: 'machine learning concepts',
|
|
174
|
+
collection_id: collection.id,
|
|
175
|
+
limit: 10,
|
|
176
|
+
search_type: 'hybrid', // 'vector' | 'bm25' | 'hybrid' | 'graph'
|
|
177
|
+
filters: { category: 'research' },
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
// Vector-only search
|
|
181
|
+
const vectorResults = await client.search({
|
|
182
|
+
query: 'neural networks',
|
|
183
|
+
search_type: 'vector',
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
// BM25 keyword search
|
|
187
|
+
const keywordResults = await client.search({
|
|
188
|
+
query: 'specific technical terms',
|
|
189
|
+
search_type: 'bm25',
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
// Knowledge graph search
|
|
193
|
+
const graphResults = await client.search({
|
|
194
|
+
query: 'related concepts',
|
|
195
|
+
search_type: 'graph',
|
|
196
|
+
});
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
### 5. Reasoning (RAG)
|
|
200
|
+
|
|
201
|
+
```typescript
|
|
202
|
+
// Reason over memories with Gemini
|
|
203
|
+
const answer = await client.reason({
|
|
204
|
+
query: 'Summarize what I learned about reinforcement learning',
|
|
205
|
+
collection_id: collection.id,
|
|
206
|
+
provider: 'gemini',
|
|
207
|
+
include_steps: true,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
console.log(answer.answer);
|
|
211
|
+
console.log(answer.sources); // Source memories used
|
|
212
|
+
console.log(answer.reasoning_context); // Reasoning steps (if include_steps=true)
|
|
213
|
+
|
|
214
|
+
// Use different LLM providers
|
|
215
|
+
const openaiAnswer = await client.reason({
|
|
216
|
+
query: 'Explain the key concepts',
|
|
217
|
+
provider: 'openai', // 'gemini' | 'openai' | 'anthropic'
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
const anthropicAnswer = await client.reason({
|
|
221
|
+
query: 'What are the main takeaways?',
|
|
222
|
+
provider: 'anthropic',
|
|
223
|
+
});
|
|
224
|
+
```
|
|
225
|
+
|
|
226
|
+
### 6. Reinforcement Learning
|
|
227
|
+
|
|
228
|
+
**The only memory API with RL training!**
|
|
229
|
+
|
|
230
|
+
```typescript
|
|
231
|
+
// Train Memory Manager agent (decides what to remember/forget)
|
|
232
|
+
const training = await client.rl.trainMemoryManager({
|
|
233
|
+
collection_id: collection.id,
|
|
234
|
+
num_episodes: 100,
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
console.log(training.metrics); // Training metrics
|
|
238
|
+
|
|
239
|
+
// Train Answer Agent (optimizes retrieval strategy)
|
|
240
|
+
const answerTraining = await client.rl.trainAnswerAgent({
|
|
241
|
+
collection_id: collection.id,
|
|
242
|
+
num_episodes: 50,
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
// Get RL training metrics
|
|
246
|
+
const metrics = await client.rl.getMetrics();
|
|
247
|
+
console.log(metrics.memory_manager_performance);
|
|
248
|
+
console.log(metrics.answer_agent_performance);
|
|
249
|
+
|
|
250
|
+
// Evaluate trained agent
|
|
251
|
+
const evaluation = await client.rl.evaluate('memory-manager', collection.id);
|
|
252
|
+
console.log(evaluation.accuracy);
|
|
253
|
+
console.log(evaluation.recall);
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
### 7. Procedural Memory
|
|
257
|
+
|
|
258
|
+
Store and execute learned procedures.
|
|
259
|
+
|
|
260
|
+
```typescript
|
|
261
|
+
// Create a procedure
|
|
262
|
+
const procedure = await client.procedural.create({
|
|
263
|
+
name: 'Daily Summary',
|
|
264
|
+
description: 'Generate daily summary of important events',
|
|
265
|
+
trigger_condition: 'time.hour == 18', // 6 PM daily
|
|
266
|
+
action_sequence: [
|
|
267
|
+
'search(query="today", limit=20)',
|
|
268
|
+
'consolidate(threshold=0.7)',
|
|
269
|
+
'generate_summary()',
|
|
270
|
+
],
|
|
271
|
+
collection_id: collection.id,
|
|
272
|
+
category: 'automation',
|
|
273
|
+
metadata: { frequency: 'daily' },
|
|
274
|
+
});
|
|
275
|
+
|
|
276
|
+
// List procedures
|
|
277
|
+
const procedures = await client.procedural.list({
|
|
278
|
+
collection_id: collection.id,
|
|
279
|
+
category: 'automation',
|
|
280
|
+
limit: 50,
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
// Get procedure
|
|
284
|
+
const retrieved = await client.procedural.get(procedure.id);
|
|
285
|
+
|
|
286
|
+
// Execute procedure
|
|
287
|
+
const result = await client.procedural.execute(procedure.id, {
|
|
288
|
+
context: { user_id: 'user_123' },
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
console.log(result.output);
|
|
292
|
+
console.log(result.execution_time);
|
|
293
|
+
|
|
294
|
+
// Update procedure
|
|
295
|
+
const updated = await client.procedural.update(procedure.id, {
|
|
296
|
+
name: 'Updated Daily Summary',
|
|
297
|
+
action_sequence: ['search(query="important", limit=10)', 'summarize()'],
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
// Delete procedure
|
|
301
|
+
await client.procedural.delete(procedure.id);
|
|
302
|
+
```
|
|
303
|
+
|
|
304
|
+
### 8. Temporal Knowledge Graphs
|
|
305
|
+
|
|
306
|
+
Track facts over time with bi-temporal modeling.
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
// Add temporal fact
|
|
310
|
+
const fact = await client.temporal.addFact({
|
|
311
|
+
subject: 'User_123',
|
|
312
|
+
predicate: 'works_at',
|
|
313
|
+
object: 'TechCorp',
|
|
314
|
+
valid_from: '2024-01-01',
|
|
315
|
+
valid_until: '2024-12-31',
|
|
316
|
+
confidence: 0.95,
|
|
317
|
+
source_memory_id: memory.id,
|
|
318
|
+
metadata: { position: 'Engineer' },
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
// Query facts (current state)
|
|
322
|
+
const facts = await client.temporal.queryFacts({
|
|
323
|
+
subject: 'User_123',
|
|
324
|
+
predicate: 'works_at',
|
|
325
|
+
});
|
|
326
|
+
|
|
327
|
+
// Query facts at specific time
|
|
328
|
+
const historicalFacts = await client.temporal.queryFacts({
|
|
329
|
+
subject: 'User_123',
|
|
330
|
+
at_time: '2024-06-15',
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
// Point-in-time query (knowledge state at timestamp)
|
|
334
|
+
const snapshot = await client.temporal.pointInTime(
|
|
335
|
+
'2024-06-15T10:00:00Z',
|
|
336
|
+
'User_123'
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
console.log(snapshot.facts); // All facts valid at that time
|
|
340
|
+
console.log(snapshot.relationships); // Graph relationships
|
|
341
|
+
```
|
|
342
|
+
|
|
343
|
+
### 9. Working Memory
|
|
344
|
+
|
|
345
|
+
Short-term context buffer for conversations.
|
|
346
|
+
|
|
347
|
+
```typescript
|
|
348
|
+
// Add to working memory
|
|
349
|
+
await client.workingMemory.add({
|
|
350
|
+
role: 'user',
|
|
351
|
+
content: 'I want to learn about machine learning',
|
|
352
|
+
metadata: { timestamp: new Date().toISOString() },
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
await client.workingMemory.add({
|
|
356
|
+
role: 'assistant',
|
|
357
|
+
content: 'I can help you with that! What aspect interests you?',
|
|
358
|
+
metadata: { timestamp: new Date().toISOString() },
|
|
359
|
+
});
|
|
360
|
+
|
|
361
|
+
// Get current context (last N items)
|
|
362
|
+
const context = await client.workingMemory.getContext();
|
|
363
|
+
console.log(context.items); // Recent conversation items
|
|
364
|
+
console.log(context.buffer_size); // Current buffer size
|
|
365
|
+
|
|
366
|
+
// Compress working memory (convert to episodic)
|
|
367
|
+
const compressed = await client.workingMemory.compress();
|
|
368
|
+
console.log(compressed.compressed_memories); // New episodic memories created
|
|
369
|
+
console.log(compressed.compression_ratio);
|
|
370
|
+
|
|
371
|
+
// Clear working memory
|
|
372
|
+
await client.workingMemory.clear();
|
|
373
|
+
```
|
|
374
|
+
|
|
375
|
+
### 10. Memory Consolidation
|
|
376
|
+
|
|
377
|
+
Automatic compression of episodic memories.
|
|
378
|
+
|
|
379
|
+
```typescript
|
|
380
|
+
// Trigger consolidation
|
|
381
|
+
const consolidation = await client.consolidation.consolidate(collection.id, 100);
|
|
382
|
+
|
|
383
|
+
console.log(consolidation.original_count); // Before consolidation
|
|
384
|
+
console.log(consolidation.consolidated_count); // After consolidation
|
|
385
|
+
console.log(consolidation.compression_ratio);
|
|
386
|
+
|
|
387
|
+
// Get consolidation statistics
|
|
388
|
+
const stats = await client.consolidation.getStats(collection.id);
|
|
389
|
+
console.log(stats.total_memories);
|
|
390
|
+
console.log(stats.consolidation_eligible);
|
|
391
|
+
console.log(stats.last_consolidation);
|
|
392
|
+
|
|
393
|
+
// Archive old memories
|
|
394
|
+
const archived = await client.consolidation.archive(
|
|
395
|
+
collection.id,
|
|
396
|
+
'2024-01-01' // Archive memories before this date
|
|
397
|
+
);
|
|
398
|
+
|
|
399
|
+
console.log(archived.archived_count);
|
|
400
|
+
console.log(archived.archived_ids);
|
|
401
|
+
```
|
|
402
|
+
|
|
403
|
+
### 11. Memory Tools (Self-Editing)
|
|
404
|
+
|
|
405
|
+
Advanced memory manipulation tools.
|
|
406
|
+
|
|
407
|
+
```typescript
|
|
408
|
+
// Replace memory content
|
|
409
|
+
const replaced = await client.memoryTools.replace({
|
|
410
|
+
memory_id: memory.id,
|
|
411
|
+
new_content: 'Updated information with corrections',
|
|
412
|
+
reason: 'Fixed inaccurate information',
|
|
413
|
+
});
|
|
414
|
+
|
|
415
|
+
// Insert memory at specific position
|
|
416
|
+
const inserted = await client.memoryTools.insert({
|
|
417
|
+
collection_id: collection.id,
|
|
418
|
+
content: 'Important context that was missing',
|
|
419
|
+
position: 5,
|
|
420
|
+
reason: 'Adding missing context',
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
// Rethink memory in light of new information
|
|
424
|
+
const rethought = await client.memoryTools.rethink(
|
|
425
|
+
memory.id,
|
|
426
|
+
'New evidence suggests different interpretation'
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
console.log(rethought.original_content);
|
|
430
|
+
console.log(rethought.updated_content);
|
|
431
|
+
console.log(rethought.changes);
|
|
432
|
+
```
|
|
433
|
+
|
|
434
|
+
### 12. World Model & Planning
|
|
435
|
+
|
|
436
|
+
Simulate retrieval and plan memory operations.
|
|
437
|
+
|
|
438
|
+
```typescript
|
|
439
|
+
// Imagine retrieval without actually retrieving
|
|
440
|
+
const imagination = await client.worldModel.imagineRetrieval(
|
|
441
|
+
'machine learning papers',
|
|
442
|
+
collection.id
|
|
443
|
+
);
|
|
444
|
+
|
|
445
|
+
console.log(imagination.expected_results); // Predicted results
|
|
446
|
+
console.log(imagination.confidence); // Prediction confidence
|
|
447
|
+
console.log(imagination.should_retrieve); // Recommendation
|
|
448
|
+
|
|
449
|
+
// Plan memory operations to achieve goal
|
|
450
|
+
const plan = await client.worldModel.plan(
|
|
451
|
+
'Prepare comprehensive summary of Q1 research',
|
|
452
|
+
collection.id
|
|
453
|
+
);
|
|
454
|
+
|
|
455
|
+
console.log(plan.steps); // Planned operations
|
|
456
|
+
console.log(plan.estimated_time); // Time estimate
|
|
457
|
+
console.log(plan.required_resources); // Resources needed
|
|
458
|
+
|
|
459
|
+
// Example plan output:
|
|
460
|
+
// {
|
|
461
|
+
// steps: [
|
|
462
|
+
// { action: 'search', params: { query: 'Q1 research', limit: 50 } },
|
|
463
|
+
// { action: 'consolidate', params: { threshold: 0.8 } },
|
|
464
|
+
// { action: 'reason', params: { query: 'summarize findings' } }
|
|
465
|
+
// ],
|
|
466
|
+
// estimated_time: 2.5,
|
|
467
|
+
// required_resources: { tokens: 5000, api_calls: 3 }
|
|
468
|
+
// }
|
|
469
|
+
```
|
|
470
|
+
|
|
471
|
+
## 🎯 Advanced Examples
|
|
472
|
+
|
|
473
|
+
### Building a Smart Chatbot
|
|
474
|
+
|
|
475
|
+
```typescript
|
|
476
|
+
import { MemoryClient } from 'hebbrix';
|
|
477
|
+
|
|
478
|
+
class SmartChatbot {
|
|
479
|
+
private client: MemoryClient;
|
|
480
|
+
private collectionId: string;
|
|
481
|
+
|
|
482
|
+
constructor(apiKey: string) {
|
|
483
|
+
this.client = new MemoryClient({ apiKey });
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
async initialize() {
|
|
487
|
+
const collection = await this.client.collections.create({
|
|
488
|
+
name: 'Chatbot Memory',
|
|
489
|
+
description: 'User interactions and preferences',
|
|
490
|
+
});
|
|
491
|
+
this.collectionId = collection.id;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
async chat(userMessage: string): Promise<string> {
|
|
495
|
+
// Add to working memory
|
|
496
|
+
await this.client.workingMemory.add({
|
|
497
|
+
role: 'user',
|
|
498
|
+
content: userMessage,
|
|
499
|
+
});
|
|
500
|
+
|
|
501
|
+
// Search relevant memories
|
|
502
|
+
const relevantMemories = await this.client.search({
|
|
503
|
+
query: userMessage,
|
|
504
|
+
collection_id: this.collectionId,
|
|
505
|
+
limit: 5,
|
|
506
|
+
});
|
|
507
|
+
|
|
508
|
+
// Generate response using reasoning
|
|
509
|
+
const response = await this.client.reason({
|
|
510
|
+
query: userMessage,
|
|
511
|
+
collection_id: this.collectionId,
|
|
512
|
+
provider: 'gemini',
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
// Store interaction
|
|
516
|
+
await this.client.memories.create({
|
|
517
|
+
collection_id: this.collectionId,
|
|
518
|
+
content: `User asked: "${userMessage}". Bot responded: "${response.answer}"`,
|
|
519
|
+
importance: 0.6,
|
|
520
|
+
});
|
|
521
|
+
|
|
522
|
+
// Add response to working memory
|
|
523
|
+
await this.client.workingMemory.add({
|
|
524
|
+
role: 'assistant',
|
|
525
|
+
content: response.answer,
|
|
526
|
+
});
|
|
527
|
+
|
|
528
|
+
return response.answer;
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
async trainOnConversations() {
|
|
532
|
+
// Train RL agent to optimize memory management
|
|
533
|
+
await this.client.rl.trainMemoryManager({
|
|
534
|
+
collection_id: this.collectionId,
|
|
535
|
+
num_episodes: 100,
|
|
536
|
+
});
|
|
537
|
+
}
|
|
538
|
+
}
|
|
539
|
+
|
|
540
|
+
// Usage
|
|
541
|
+
const bot = new SmartChatbot('hbx_...');
|
|
542
|
+
await bot.initialize();
|
|
543
|
+
|
|
544
|
+
const answer = await bot.chat('What did we discuss yesterday?');
|
|
545
|
+
console.log(answer);
|
|
546
|
+
|
|
547
|
+
await bot.trainOnConversations();
|
|
548
|
+
```
|
|
549
|
+
|
|
550
|
+
### Knowledge Graph Construction
|
|
551
|
+
|
|
552
|
+
```typescript
|
|
553
|
+
// Build temporal knowledge graph from documents
|
|
554
|
+
async function buildKnowledgeGraph(client: MemoryClient, collectionId: string) {
|
|
555
|
+
// Add entities and relationships
|
|
556
|
+
await client.temporal.addFact({
|
|
557
|
+
subject: 'GPT-4',
|
|
558
|
+
predicate: 'developed_by',
|
|
559
|
+
object: 'OpenAI',
|
|
560
|
+
valid_from: '2023-03-14',
|
|
561
|
+
confidence: 1.0,
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
await client.temporal.addFact({
|
|
565
|
+
subject: 'GPT-4',
|
|
566
|
+
predicate: 'is_a',
|
|
567
|
+
object: 'Large Language Model',
|
|
568
|
+
valid_from: '2023-03-14',
|
|
569
|
+
confidence: 1.0,
|
|
570
|
+
});
|
|
571
|
+
|
|
572
|
+
await client.temporal.addFact({
|
|
573
|
+
subject: 'OpenAI',
|
|
574
|
+
predicate: 'founded_in',
|
|
575
|
+
object: '2015',
|
|
576
|
+
valid_from: '2015-12-11',
|
|
577
|
+
confidence: 1.0,
|
|
578
|
+
});
|
|
579
|
+
|
|
580
|
+
// Query relationships
|
|
581
|
+
const facts = await client.temporal.queryFacts({
|
|
582
|
+
subject: 'GPT-4',
|
|
583
|
+
});
|
|
584
|
+
|
|
585
|
+
console.log(facts); // All facts about GPT-4
|
|
586
|
+
|
|
587
|
+
// Historical query
|
|
588
|
+
const snapshot = await client.temporal.pointInTime(
|
|
589
|
+
'2023-01-01T00:00:00Z',
|
|
590
|
+
'OpenAI'
|
|
591
|
+
);
|
|
592
|
+
|
|
593
|
+
console.log(snapshot); // OpenAI's state before GPT-4 release
|
|
594
|
+
}
|
|
595
|
+
```
|
|
596
|
+
|
|
597
|
+
## 🛡️ Error Handling
|
|
598
|
+
|
|
599
|
+
```typescript
|
|
600
|
+
import {
|
|
601
|
+
HebbrixError,
|
|
602
|
+
AuthenticationError,
|
|
603
|
+
ValidationError,
|
|
604
|
+
NotFoundError,
|
|
605
|
+
RateLimitError,
|
|
606
|
+
ServerError,
|
|
607
|
+
} from '@hebbrix/sdk';
|
|
608
|
+
|
|
609
|
+
try {
|
|
610
|
+
const memory = await client.memories.get('invalid_id');
|
|
611
|
+
} catch (error) {
|
|
612
|
+
if (error instanceof NotFoundError) {
|
|
613
|
+
console.log('Memory not found');
|
|
614
|
+
} else if (error instanceof AuthenticationError) {
|
|
615
|
+
console.log('Invalid API key');
|
|
616
|
+
} else if (error instanceof RateLimitError) {
|
|
617
|
+
console.log('Rate limit exceeded, retry after:', error.retryAfter);
|
|
618
|
+
} else if (error instanceof ValidationError) {
|
|
619
|
+
console.log('Validation errors:', error.errors);
|
|
620
|
+
} else if (error instanceof ServerError) {
|
|
621
|
+
console.log('Server error:', error.message);
|
|
622
|
+
} else if (error instanceof HebbrixError) {
|
|
623
|
+
console.log('API error:', error.statusCode, error.message);
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
```
|
|
627
|
+
|
|
628
|
+
## ⚙️ Configuration
|
|
629
|
+
|
|
630
|
+
```typescript
|
|
631
|
+
const client = new MemoryClient({
|
|
632
|
+
// Required: API key
|
|
633
|
+
apiKey: 'hbx_...',
|
|
634
|
+
|
|
635
|
+
// Optional: Custom base URL (default: http://localhost:8000)
|
|
636
|
+
baseUrl: 'https://api.yourdomain.com',
|
|
637
|
+
|
|
638
|
+
// Optional: Request timeout in milliseconds (default: 30000)
|
|
639
|
+
timeout: 60000,
|
|
640
|
+
});
|
|
641
|
+
```
|
|
642
|
+
|
|
643
|
+
## 🌐 Browser Usage
|
|
644
|
+
|
|
645
|
+
The SDK works in browsers with native Fetch API support:
|
|
646
|
+
|
|
647
|
+
```html
|
|
648
|
+
<script type="module">
|
|
649
|
+
import { MemoryClient } from 'https://cdn.jsdelivr.net/npm/hebbrix/dist/index.mjs';
|
|
650
|
+
|
|
651
|
+
const client = new MemoryClient({ apiKey: 'hbx_...' });
|
|
652
|
+
|
|
653
|
+
const results = await client.search({ query: 'test' });
|
|
654
|
+
console.log(results);
|
|
655
|
+
</script>
|
|
656
|
+
```
|
|
657
|
+
|
|
658
|
+
## 📊 Comparison with Competitors
|
|
659
|
+
|
|
660
|
+
| Feature | Hebbrix | Mem0 | Zep | Letta | Supermemory |
|
|
661
|
+
|---------|-----------|------|-----|-------|-------------|
|
|
662
|
+
| Reinforcement Learning | ✅ | ❌ | ❌ | ❌ | ❌ |
|
|
663
|
+
| Temporal Knowledge Graphs | ✅ | ❌ | ✅ | ❌ | ❌ |
|
|
664
|
+
| Procedural Memory | ✅ | ❌ | ❌ | ✅ | ❌ |
|
|
665
|
+
| Working Memory Buffer | ✅ | ❌ | ✅ | ✅ | ❌ |
|
|
666
|
+
| Memory Consolidation | ✅ | ❌ | ❌ | ❌ | ❌ |
|
|
667
|
+
| Hybrid Search | ✅ | ✅ | ✅ | ❌ | ✅ |
|
|
668
|
+
| Graph Search | ✅ | ❌ | ❌ | ❌ | ❌ |
|
|
669
|
+
| Self-Editing Tools | ✅ | ❌ | ❌ | ❌ | ❌ |
|
|
670
|
+
| World Model Planning | ✅ | ❌ | ❌ | ❌ | ❌ |
|
|
671
|
+
| Multi-LLM Support | ✅ | ✅ | ✅ | ✅ | ✅ |
|
|
672
|
+
|
|
673
|
+
**Hebbrix is the only memory API with:**
|
|
674
|
+
- RL-based memory optimization (Memory-R1 framework)
|
|
675
|
+
- Bi-temporal knowledge graphs
|
|
676
|
+
- Full memory consolidation pipeline
|
|
677
|
+
- Self-editing capabilities
|
|
678
|
+
- World model-based planning
|
|
679
|
+
|
|
680
|
+
## 🏗️ Development
|
|
681
|
+
|
|
682
|
+
```bash
|
|
683
|
+
# Install dependencies
|
|
684
|
+
npm install
|
|
685
|
+
|
|
686
|
+
# Build
|
|
687
|
+
npm run build
|
|
688
|
+
|
|
689
|
+
# Development mode with watch
|
|
690
|
+
npm run dev
|
|
691
|
+
|
|
692
|
+
# Run tests
|
|
693
|
+
npm test
|
|
694
|
+
|
|
695
|
+
# Lint code
|
|
696
|
+
npm run lint
|
|
697
|
+
|
|
698
|
+
# Format code
|
|
699
|
+
npm run format
|
|
700
|
+
```
|
|
701
|
+
|
|
702
|
+
## 📘 TypeScript Support
|
|
703
|
+
|
|
704
|
+
The SDK is written in TypeScript and includes complete type definitions:
|
|
705
|
+
|
|
706
|
+
```typescript
|
|
707
|
+
import type {
|
|
708
|
+
MemoryClient,
|
|
709
|
+
Memory,
|
|
710
|
+
Collection,
|
|
711
|
+
SearchResult,
|
|
712
|
+
ReasoningResponse,
|
|
713
|
+
} from '@hebbrix/sdk';
|
|
714
|
+
|
|
715
|
+
const client = new MemoryClient({ apiKey: 'hbx_...' });
|
|
716
|
+
|
|
717
|
+
// Full type inference
|
|
718
|
+
const memory: Memory = await client.memories.create({
|
|
719
|
+
collection_id: 'col_123',
|
|
720
|
+
content: 'Typed content',
|
|
721
|
+
});
|
|
722
|
+
|
|
723
|
+
// Type-safe search results
|
|
724
|
+
const results: SearchResult[] = await client.search({
|
|
725
|
+
query: 'test',
|
|
726
|
+
});
|
|
727
|
+
|
|
728
|
+
// Type-safe reasoning response
|
|
729
|
+
const answer: ReasoningResponse = await client.reason({
|
|
730
|
+
query: 'question',
|
|
731
|
+
});
|
|
732
|
+
```
|
|
733
|
+
|
|
734
|
+
## 🔗 Links
|
|
735
|
+
|
|
736
|
+
- **Documentation**: https://docs.hebbrix.com
|
|
737
|
+
- **API Reference**: https://api.hebbrix.com/docs
|
|
738
|
+
- **GitHub**: https://github.com/hebbrix/hebbrix
|
|
739
|
+
- **Examples**: https://github.com/hebbrix/examples
|
|
740
|
+
- **npm Package**: https://www.npmjs.com/package/hebbrix
|
|
741
|
+
|
|
742
|
+
## 📄 License
|
|
743
|
+
|
|
744
|
+
MIT License - see [LICENSE](LICENSE) for details
|
|
745
|
+
|
|
746
|
+
---
|
|
747
|
+
|
|
748
|
+
**Built with ❤️ by the Hebbrix team**
|