@usetransactional/memory 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/README.md +300 -0
- package/dist/index.cjs +607 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +914 -0
- package/dist/index.d.ts +914 -0
- package/dist/index.js +592 -0
- package/dist/index.js.map +1 -0
- package/package.json +57 -0
package/README.md
ADDED
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
# @transactional/memory-sdk
|
|
2
|
+
|
|
3
|
+
Official TypeScript/JavaScript SDK for Transactional Memory - a powerful memory system for AI applications with semantic search, knowledge graphs, and context management.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @transactional/memory-sdk
|
|
9
|
+
# or
|
|
10
|
+
yarn add @transactional/memory-sdk
|
|
11
|
+
# or
|
|
12
|
+
pnpm add @transactional/memory-sdk
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
## Quick Start
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { MemoryClient } from '@transactional/memory-sdk';
|
|
19
|
+
|
|
20
|
+
const client = new MemoryClient({
|
|
21
|
+
apiKey: process.env.TRANSACTIONAL_API_KEY!,
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Add a memory
|
|
25
|
+
await client.add({
|
|
26
|
+
content: 'User prefers TypeScript over JavaScript',
|
|
27
|
+
userId: 'user_123',
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
// Search memories
|
|
31
|
+
const results = await client.search({
|
|
32
|
+
query: 'programming language preferences',
|
|
33
|
+
userId: 'user_123',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Get context for LLM prompts
|
|
37
|
+
const context = await client.getContext({
|
|
38
|
+
userId: 'user_123',
|
|
39
|
+
query: 'What programming language does the user prefer?',
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
console.log(context.context);
|
|
43
|
+
// Output: "User Preferences:\n- Prefers TypeScript over JavaScript"
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## Features
|
|
47
|
+
|
|
48
|
+
- **Memory Management**: Add, update, delete, and search memories
|
|
49
|
+
- **Semantic Search**: Find relevant memories using hybrid search (keyword + embedding)
|
|
50
|
+
- **Session Management**: Organize memories into sessions
|
|
51
|
+
- **User Profiles**: Maintain persistent user profiles with static and dynamic facts
|
|
52
|
+
- **Knowledge Graph**: Explore relationships between entities
|
|
53
|
+
- **URL Ingestion**: Extract and store content from web pages
|
|
54
|
+
- **PDF Ingestion**: Parse and store content from PDF documents
|
|
55
|
+
- **Context Generation**: Build context for LLM prompts
|
|
56
|
+
|
|
57
|
+
## Configuration
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
const client = new MemoryClient({
|
|
61
|
+
// Required
|
|
62
|
+
apiKey: 'your-api-key',
|
|
63
|
+
|
|
64
|
+
// Optional
|
|
65
|
+
baseUrl: 'https://api.usetransactional.com', // Default
|
|
66
|
+
timeout: 30000, // Request timeout in ms (default: 30000)
|
|
67
|
+
maxRetries: 3, // Max retry attempts (default: 3)
|
|
68
|
+
headers: {}, // Custom headers
|
|
69
|
+
});
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## API Reference
|
|
73
|
+
|
|
74
|
+
### Memory Operations
|
|
75
|
+
|
|
76
|
+
#### Add Memory
|
|
77
|
+
|
|
78
|
+
```typescript
|
|
79
|
+
const result = await client.add({
|
|
80
|
+
content: 'User mentioned they work at Acme Corp',
|
|
81
|
+
userId: 'user_123',
|
|
82
|
+
sessionId: 'session_abc', // Optional
|
|
83
|
+
type: MemoryEntityType.FACT, // Optional
|
|
84
|
+
metadata: { source: 'chat' }, // Optional
|
|
85
|
+
});
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### Search Memories
|
|
89
|
+
|
|
90
|
+
```typescript
|
|
91
|
+
const results = await client.search({
|
|
92
|
+
query: 'where does the user work?',
|
|
93
|
+
userId: 'user_123',
|
|
94
|
+
types: [MemoryEntityType.FACT], // Optional filter
|
|
95
|
+
strategy: 'hybrid', // 'auto' | 'keyword' | 'semantic' | 'hybrid'
|
|
96
|
+
threshold: 0.7, // Minimum similarity (0-1)
|
|
97
|
+
limit: 10, // Max results
|
|
98
|
+
includeRelated: true, // Include related entities
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
#### Get/Update/Delete Memory
|
|
103
|
+
|
|
104
|
+
```typescript
|
|
105
|
+
// Get
|
|
106
|
+
const memory = await client.get(sessionId, memoryId);
|
|
107
|
+
|
|
108
|
+
// Update
|
|
109
|
+
const updated = await client.update(sessionId, memoryId, {
|
|
110
|
+
name: 'new_name',
|
|
111
|
+
properties: { updated: true },
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
// Delete
|
|
115
|
+
await client.delete(sessionId, memoryId);
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Session Operations
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
// Create session
|
|
122
|
+
const session = await client.sessions.create({
|
|
123
|
+
userId: 'user_123',
|
|
124
|
+
agentId: 'my-agent',
|
|
125
|
+
ttl: 86400, // 24 hours
|
|
126
|
+
config: {
|
|
127
|
+
autoExtract: true,
|
|
128
|
+
autoSummarize: false,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Get session
|
|
133
|
+
const session = await client.sessions.get(sessionId);
|
|
134
|
+
|
|
135
|
+
// List sessions
|
|
136
|
+
const { data, meta } = await client.sessions.list({
|
|
137
|
+
userId: 'user_123',
|
|
138
|
+
status: MemorySessionStatus.ACTIVE,
|
|
139
|
+
page: 1,
|
|
140
|
+
limit: 20,
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
// Update session
|
|
144
|
+
await client.sessions.update(sessionId, {
|
|
145
|
+
ttl: 172800, // Extend to 48 hours
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
// Delete session
|
|
149
|
+
await client.sessions.delete(sessionId);
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
### Context Generation
|
|
153
|
+
|
|
154
|
+
```typescript
|
|
155
|
+
const context = await client.getContext({
|
|
156
|
+
userId: 'user_123',
|
|
157
|
+
sessionId: 'session_abc', // Optional
|
|
158
|
+
query: 'What are the user preferences?', // Focus context on this
|
|
159
|
+
maxTokens: 4000,
|
|
160
|
+
includeProfile: true,
|
|
161
|
+
includeFacts: true,
|
|
162
|
+
includePreferences: true,
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
// Use in LLM prompt
|
|
166
|
+
const systemPrompt = `You are a helpful assistant.
|
|
167
|
+
|
|
168
|
+
Context about the user:
|
|
169
|
+
${context.context}`;
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
### Profile Operations
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
// Get profile
|
|
176
|
+
const profile = await client.profiles.get('user_123');
|
|
177
|
+
|
|
178
|
+
// Upsert profile
|
|
179
|
+
const updated = await client.profiles.upsert('user_123', {
|
|
180
|
+
staticFacts: {
|
|
181
|
+
name: 'John Doe',
|
|
182
|
+
timezone: 'America/New_York',
|
|
183
|
+
},
|
|
184
|
+
dynamicFacts: {
|
|
185
|
+
interests: ['AI', 'TypeScript'],
|
|
186
|
+
},
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
// Delete profile
|
|
190
|
+
await client.profiles.delete('user_123');
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Content Ingestion
|
|
194
|
+
|
|
195
|
+
#### URL Ingestion
|
|
196
|
+
|
|
197
|
+
```typescript
|
|
198
|
+
const result = await client.ingest.url({
|
|
199
|
+
url: 'https://example.com/article',
|
|
200
|
+
userId: 'user_123',
|
|
201
|
+
sessionId: 'session_abc', // Optional
|
|
202
|
+
options: {
|
|
203
|
+
extractEntities: true,
|
|
204
|
+
generateEmbeddings: true,
|
|
205
|
+
},
|
|
206
|
+
});
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
#### PDF Ingestion
|
|
210
|
+
|
|
211
|
+
```typescript
|
|
212
|
+
import { readFileSync } from 'fs';
|
|
213
|
+
|
|
214
|
+
const pdfBuffer = readFileSync('document.pdf');
|
|
215
|
+
|
|
216
|
+
const result = await client.ingest.pdf({
|
|
217
|
+
file: pdfBuffer,
|
|
218
|
+
filename: 'document.pdf',
|
|
219
|
+
userId: 'user_123',
|
|
220
|
+
options: {
|
|
221
|
+
extractEntities: true,
|
|
222
|
+
generateEmbeddings: true,
|
|
223
|
+
},
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
#### Text Ingestion
|
|
228
|
+
|
|
229
|
+
```typescript
|
|
230
|
+
const result = await client.ingest.text({
|
|
231
|
+
content: 'Some markdown or code content',
|
|
232
|
+
contentType: 'markdown',
|
|
233
|
+
sessionId: 'session_abc',
|
|
234
|
+
});
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Graph Operations
|
|
238
|
+
|
|
239
|
+
```typescript
|
|
240
|
+
// Get graph
|
|
241
|
+
const graph = await client.graph.get({
|
|
242
|
+
sessionId: 'session_abc',
|
|
243
|
+
depth: 2,
|
|
244
|
+
entityTypes: [MemoryEntityType.FACT, MemoryEntityType.TOPIC],
|
|
245
|
+
limit: 100,
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
// Traverse from entity
|
|
249
|
+
const related = await client.graph.traverse({
|
|
250
|
+
startEntityId: 'entity_xyz',
|
|
251
|
+
depth: 2,
|
|
252
|
+
direction: 'outbound',
|
|
253
|
+
});
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
## Error Handling
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
import {
|
|
260
|
+
MemoryError,
|
|
261
|
+
AuthenticationError,
|
|
262
|
+
ValidationError,
|
|
263
|
+
NotFoundError,
|
|
264
|
+
RateLimitError,
|
|
265
|
+
} from '@transactional/memory-sdk';
|
|
266
|
+
|
|
267
|
+
try {
|
|
268
|
+
await client.search({ query: 'test' });
|
|
269
|
+
} catch (error) {
|
|
270
|
+
if (error instanceof AuthenticationError) {
|
|
271
|
+
console.error('Invalid API key');
|
|
272
|
+
} else if (error instanceof RateLimitError) {
|
|
273
|
+
console.error(`Rate limited. Retry after ${error.retryAfter}s`);
|
|
274
|
+
} else if (error instanceof NotFoundError) {
|
|
275
|
+
console.error(`${error.resourceType} not found`);
|
|
276
|
+
} else if (error instanceof ValidationError) {
|
|
277
|
+
console.error('Validation failed:', error.fieldErrors);
|
|
278
|
+
} else if (error instanceof MemoryError) {
|
|
279
|
+
console.error(`Error ${error.code}: ${error.message}`);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
```
|
|
283
|
+
|
|
284
|
+
## TypeScript Support
|
|
285
|
+
|
|
286
|
+
This SDK is written in TypeScript and provides full type definitions:
|
|
287
|
+
|
|
288
|
+
```typescript
|
|
289
|
+
import type {
|
|
290
|
+
Memory,
|
|
291
|
+
Session,
|
|
292
|
+
SearchResult,
|
|
293
|
+
ContextResult,
|
|
294
|
+
MemoryEntityType,
|
|
295
|
+
} from '@transactional/memory-sdk';
|
|
296
|
+
```
|
|
297
|
+
|
|
298
|
+
## License
|
|
299
|
+
|
|
300
|
+
MIT
|