@memorylayerai/sdk 0.3.0 → 0.4.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 +43 -53
- package/dist/index.cjs +18 -0
- package/dist/index.d.cts +32 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +18 -0
- package/package.json +1 -1
- package/src/resources/search.ts +25 -0
- package/src/types.ts +32 -0
package/README.md
CHANGED
|
@@ -28,30 +28,30 @@ yarn add @memorylayerai/sdk
|
|
|
28
28
|
|
|
29
29
|
## Quick Start
|
|
30
30
|
|
|
31
|
-
### Option 1: Transparent Router (
|
|
31
|
+
### Option 1: Transparent Router (Beta) - Drop-in OpenAI Proxy ⚡
|
|
32
32
|
|
|
33
|
-
|
|
33
|
+
Change your baseURL to add automatic memory injection:
|
|
34
34
|
|
|
35
35
|
```typescript
|
|
36
36
|
import OpenAI from 'openai';
|
|
37
37
|
|
|
38
38
|
const openai = new OpenAI({
|
|
39
|
-
baseURL: 'https://api.memorylayer.ai/v1', // ←
|
|
39
|
+
baseURL: 'https://api.memorylayer.ai/v1', // ← Point to MemoryLayer
|
|
40
40
|
apiKey: 'ml_your_memorylayer_key' // ← Use your MemoryLayer key
|
|
41
41
|
});
|
|
42
42
|
|
|
43
|
-
//
|
|
43
|
+
// Memory is automatically retrieved and injected
|
|
44
44
|
const response = await openai.chat.completions.create({
|
|
45
45
|
model: 'gpt-4',
|
|
46
46
|
messages: [{ role: 'user', content: 'What are my preferences?' }]
|
|
47
47
|
});
|
|
48
48
|
```
|
|
49
49
|
|
|
50
|
-
**
|
|
51
|
-
- ✅
|
|
52
|
-
- ✅
|
|
53
|
-
- ✅
|
|
54
|
-
-
|
|
50
|
+
**Current Status:**
|
|
51
|
+
- ✅ Works with `/v1/chat/completions` (non-streaming)
|
|
52
|
+
- ✅ OpenAI-compatible responses
|
|
53
|
+
- ✅ Configurable via headers (use `fetch`/`axios` for guaranteed header support)
|
|
54
|
+
- ⏳ Streaming support coming soon
|
|
55
55
|
|
|
56
56
|
See [Transparent Router Guide](#transparent-router) for details.
|
|
57
57
|
|
|
@@ -115,7 +115,12 @@ results.forEach(result => {
|
|
|
115
115
|
|
|
116
116
|
## Transparent Router
|
|
117
117
|
|
|
118
|
-
The transparent router is an OpenAI-compatible proxy that automatically injects memory context into your requests.
|
|
118
|
+
The transparent router is an OpenAI-compatible proxy that automatically injects memory context into your requests.
|
|
119
|
+
|
|
120
|
+
**Current Status:**
|
|
121
|
+
- ✅ Works with `/v1/chat/completions` (non-streaming)
|
|
122
|
+
- ✅ OpenAI-compatible responses
|
|
123
|
+
- ⏳ Streaming support coming soon
|
|
119
124
|
|
|
120
125
|
### Basic Usage
|
|
121
126
|
|
|
@@ -135,67 +140,52 @@ const response = await openai.chat.completions.create({
|
|
|
135
140
|
|
|
136
141
|
### Configuration Headers
|
|
137
142
|
|
|
138
|
-
Control memory injection with optional headers:
|
|
143
|
+
Control memory injection with optional headers. **Note:** For guaranteed header support, use `fetch` or `axios` directly:
|
|
139
144
|
|
|
140
145
|
```typescript
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
146
|
+
// Using fetch for guaranteed header support
|
|
147
|
+
const response = await fetch('https://api.memorylayer.ai/v1/chat/completions', {
|
|
148
|
+
method: 'POST',
|
|
144
149
|
headers: {
|
|
145
|
-
'
|
|
146
|
-
'
|
|
147
|
-
'x-memory-
|
|
148
|
-
'x-memory-
|
|
149
|
-
'x-memory-
|
|
150
|
-
'x-memory-
|
|
151
|
-
|
|
150
|
+
'Authorization': `Bearer ${process.env.MEMORYLAYER_API_KEY}`,
|
|
151
|
+
'Content-Type': 'application/json',
|
|
152
|
+
'x-memory-user-id': 'user_123', // User scope (required for multi-user apps)
|
|
153
|
+
'x-memory-session-id': 'sess_abc', // Session scope (persist from response)
|
|
154
|
+
'x-memory-limit': '10', // Max memories to inject
|
|
155
|
+
'x-memory-injection-mode': 'safe', // safe|full (balanced coming soon)
|
|
156
|
+
'x-memory-disabled': 'false' // Enable/disable memory
|
|
157
|
+
},
|
|
158
|
+
body: JSON.stringify({
|
|
159
|
+
model: 'gpt-4',
|
|
160
|
+
messages: [{ role: 'user', content: 'Hello!' }]
|
|
161
|
+
})
|
|
152
162
|
});
|
|
153
163
|
```
|
|
154
164
|
|
|
155
165
|
### Injection Modes
|
|
156
166
|
|
|
157
|
-
- **safe
|
|
158
|
-
- **
|
|
159
|
-
- **
|
|
167
|
+
- **safe** (default): Only fact + preference types (minimal risk, structured data)
|
|
168
|
+
- **full**: All memory types including snippets (maximum context, higher token usage)
|
|
169
|
+
- **balanced**: Trusted summaries + facts + preferences (coming soon)
|
|
160
170
|
|
|
161
171
|
### Diagnostic Headers
|
|
162
172
|
|
|
163
|
-
Every response includes diagnostic headers:
|
|
173
|
+
Every response includes diagnostic headers showing what happened:
|
|
164
174
|
|
|
165
175
|
```typescript
|
|
166
|
-
const response = await
|
|
167
|
-
|
|
168
|
-
console.log('Memories retrieved:', response.headers?.['x-memory-hit-count']);
|
|
169
|
-
console.log('Tokens injected:', response.headers?.['x-memory-injected-tokens']);
|
|
170
|
-
console.log('Max score:', response.headers?.['x-memory-max-score']);
|
|
171
|
-
console.log('Query rewriting:', response.headers?.['x-memory-rewrite']);
|
|
172
|
-
console.log('Memory status:', response.headers?.['x-memory-status']);
|
|
173
|
-
console.log('Session ID:', response.headers?.['x-memory-session-id']);
|
|
174
|
-
```
|
|
175
|
-
|
|
176
|
-
### Streaming Support
|
|
176
|
+
const response = await fetch('https://api.memorylayer.ai/v1/chat/completions', { ... });
|
|
177
177
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
stream: true,
|
|
185
|
-
headers: {
|
|
186
|
-
'x-memory-user-id': 'user_123'
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
for await (const chunk of stream) {
|
|
191
|
-
const content = chunk.choices[0]?.delta?.content || '';
|
|
192
|
-
process.stdout.write(content);
|
|
193
|
-
}
|
|
178
|
+
console.log('Memories retrieved:', response.headers.get('x-memory-hit-count'));
|
|
179
|
+
console.log('Tokens injected:', response.headers.get('x-memory-injected-tokens'));
|
|
180
|
+
console.log('Max score:', response.headers.get('x-memory-max-score'));
|
|
181
|
+
console.log('Query rewriting:', response.headers.get('x-memory-rewrite'));
|
|
182
|
+
console.log('Memory status:', response.headers.get('x-memory-status'));
|
|
183
|
+
console.log('Session ID:', response.headers.get('x-memory-session-id')); // Persist this!
|
|
194
184
|
```
|
|
195
185
|
|
|
196
186
|
### Session Management
|
|
197
187
|
|
|
198
|
-
|
|
188
|
+
For chat applications, persist `x-memory-session-id` from response headers and pass it in subsequent requests:
|
|
199
189
|
|
|
200
190
|
```typescript
|
|
201
191
|
const response = await openai.chat.completions.create({ ... });
|
package/dist/index.cjs
CHANGED
|
@@ -251,6 +251,24 @@ var init_search = __esm({
|
|
|
251
251
|
if (request.filter) {
|
|
252
252
|
query.filter = JSON.stringify(request.filter);
|
|
253
253
|
}
|
|
254
|
+
if (request.enableQueryRewriting !== void 0) {
|
|
255
|
+
query.enableQueryRewriting = request.enableQueryRewriting.toString();
|
|
256
|
+
}
|
|
257
|
+
if (request.enableEntityExpansion !== void 0) {
|
|
258
|
+
query.enableEntityExpansion = request.enableEntityExpansion.toString();
|
|
259
|
+
}
|
|
260
|
+
if (request.enableGraphConnectivity !== void 0) {
|
|
261
|
+
query.enableGraphConnectivity = request.enableGraphConnectivity.toString();
|
|
262
|
+
}
|
|
263
|
+
if (request.enableSemanticDedup !== void 0) {
|
|
264
|
+
query.enableSemanticDedup = request.enableSemanticDedup.toString();
|
|
265
|
+
}
|
|
266
|
+
if (request.rerankingStrategy) {
|
|
267
|
+
query.rerankingStrategy = request.rerankingStrategy;
|
|
268
|
+
}
|
|
269
|
+
if (request.fusionWeights) {
|
|
270
|
+
query.fusionWeights = JSON.stringify(request.fusionWeights);
|
|
271
|
+
}
|
|
254
272
|
return this.httpClient.request({
|
|
255
273
|
method: "GET",
|
|
256
274
|
path: "/v1/search",
|
package/dist/index.d.cts
CHANGED
|
@@ -169,6 +169,24 @@ interface SearchRequest {
|
|
|
169
169
|
filter?: Record<string, any>;
|
|
170
170
|
/** Minimum relevance score threshold (0-1) */
|
|
171
171
|
threshold?: number;
|
|
172
|
+
/** Enable query rewriting (default: true) */
|
|
173
|
+
enableQueryRewriting?: boolean;
|
|
174
|
+
/** Enable entity expansion search (default: false) */
|
|
175
|
+
enableEntityExpansion?: boolean;
|
|
176
|
+
/** Enable graph connectivity search (default: false) */
|
|
177
|
+
enableGraphConnectivity?: boolean;
|
|
178
|
+
/** Enable semantic deduplication (default: false) */
|
|
179
|
+
enableSemanticDedup?: boolean;
|
|
180
|
+
/** Reranking strategy: 'none', 'cross-encoder', 'llm' (default: 'none') */
|
|
181
|
+
rerankingStrategy?: 'none' | 'cross-encoder' | 'llm';
|
|
182
|
+
/** Custom fusion weights for multi-method retrieval */
|
|
183
|
+
fusionWeights?: {
|
|
184
|
+
vector?: number;
|
|
185
|
+
bm25?: number;
|
|
186
|
+
recency?: number;
|
|
187
|
+
entity?: number;
|
|
188
|
+
graph?: number;
|
|
189
|
+
};
|
|
172
190
|
}
|
|
173
191
|
/**
|
|
174
192
|
* Search result
|
|
@@ -180,6 +198,20 @@ interface SearchResult {
|
|
|
180
198
|
score: number;
|
|
181
199
|
/** Highlighted text snippets */
|
|
182
200
|
highlights?: string[];
|
|
201
|
+
/** Score breakdown by retrieval method */
|
|
202
|
+
scoreBreakdown?: {
|
|
203
|
+
vectorScore?: number;
|
|
204
|
+
bm25Score?: number;
|
|
205
|
+
recencyScore?: number;
|
|
206
|
+
entityScore?: number;
|
|
207
|
+
graphScore?: number;
|
|
208
|
+
};
|
|
209
|
+
/** Connected memories (if graph enhancement enabled) */
|
|
210
|
+
connections?: Array<{
|
|
211
|
+
memoryId: string;
|
|
212
|
+
connectionType: 'updates' | 'extends' | 'derives' | 'similarity';
|
|
213
|
+
connectionStrength: number;
|
|
214
|
+
}>;
|
|
183
215
|
}
|
|
184
216
|
/**
|
|
185
217
|
* Search response
|
package/dist/index.d.ts
CHANGED
|
@@ -169,6 +169,24 @@ interface SearchRequest {
|
|
|
169
169
|
filter?: Record<string, any>;
|
|
170
170
|
/** Minimum relevance score threshold (0-1) */
|
|
171
171
|
threshold?: number;
|
|
172
|
+
/** Enable query rewriting (default: true) */
|
|
173
|
+
enableQueryRewriting?: boolean;
|
|
174
|
+
/** Enable entity expansion search (default: false) */
|
|
175
|
+
enableEntityExpansion?: boolean;
|
|
176
|
+
/** Enable graph connectivity search (default: false) */
|
|
177
|
+
enableGraphConnectivity?: boolean;
|
|
178
|
+
/** Enable semantic deduplication (default: false) */
|
|
179
|
+
enableSemanticDedup?: boolean;
|
|
180
|
+
/** Reranking strategy: 'none', 'cross-encoder', 'llm' (default: 'none') */
|
|
181
|
+
rerankingStrategy?: 'none' | 'cross-encoder' | 'llm';
|
|
182
|
+
/** Custom fusion weights for multi-method retrieval */
|
|
183
|
+
fusionWeights?: {
|
|
184
|
+
vector?: number;
|
|
185
|
+
bm25?: number;
|
|
186
|
+
recency?: number;
|
|
187
|
+
entity?: number;
|
|
188
|
+
graph?: number;
|
|
189
|
+
};
|
|
172
190
|
}
|
|
173
191
|
/**
|
|
174
192
|
* Search result
|
|
@@ -180,6 +198,20 @@ interface SearchResult {
|
|
|
180
198
|
score: number;
|
|
181
199
|
/** Highlighted text snippets */
|
|
182
200
|
highlights?: string[];
|
|
201
|
+
/** Score breakdown by retrieval method */
|
|
202
|
+
scoreBreakdown?: {
|
|
203
|
+
vectorScore?: number;
|
|
204
|
+
bm25Score?: number;
|
|
205
|
+
recencyScore?: number;
|
|
206
|
+
entityScore?: number;
|
|
207
|
+
graphScore?: number;
|
|
208
|
+
};
|
|
209
|
+
/** Connected memories (if graph enhancement enabled) */
|
|
210
|
+
connections?: Array<{
|
|
211
|
+
memoryId: string;
|
|
212
|
+
connectionType: 'updates' | 'extends' | 'derives' | 'similarity';
|
|
213
|
+
connectionStrength: number;
|
|
214
|
+
}>;
|
|
183
215
|
}
|
|
184
216
|
/**
|
|
185
217
|
* Search response
|
package/dist/index.js
CHANGED
|
@@ -250,6 +250,24 @@ var init_search = __esm({
|
|
|
250
250
|
if (request.filter) {
|
|
251
251
|
query.filter = JSON.stringify(request.filter);
|
|
252
252
|
}
|
|
253
|
+
if (request.enableQueryRewriting !== void 0) {
|
|
254
|
+
query.enableQueryRewriting = request.enableQueryRewriting.toString();
|
|
255
|
+
}
|
|
256
|
+
if (request.enableEntityExpansion !== void 0) {
|
|
257
|
+
query.enableEntityExpansion = request.enableEntityExpansion.toString();
|
|
258
|
+
}
|
|
259
|
+
if (request.enableGraphConnectivity !== void 0) {
|
|
260
|
+
query.enableGraphConnectivity = request.enableGraphConnectivity.toString();
|
|
261
|
+
}
|
|
262
|
+
if (request.enableSemanticDedup !== void 0) {
|
|
263
|
+
query.enableSemanticDedup = request.enableSemanticDedup.toString();
|
|
264
|
+
}
|
|
265
|
+
if (request.rerankingStrategy) {
|
|
266
|
+
query.rerankingStrategy = request.rerankingStrategy;
|
|
267
|
+
}
|
|
268
|
+
if (request.fusionWeights) {
|
|
269
|
+
query.fusionWeights = JSON.stringify(request.fusionWeights);
|
|
270
|
+
}
|
|
253
271
|
return this.httpClient.request({
|
|
254
272
|
method: "GET",
|
|
255
273
|
path: "/v1/search",
|
package/package.json
CHANGED
package/src/resources/search.ts
CHANGED
|
@@ -46,6 +46,31 @@ export class SearchResource {
|
|
|
46
46
|
query.filter = JSON.stringify(request.filter);
|
|
47
47
|
}
|
|
48
48
|
|
|
49
|
+
// New advanced retrieval options
|
|
50
|
+
if (request.enableQueryRewriting !== undefined) {
|
|
51
|
+
query.enableQueryRewriting = request.enableQueryRewriting.toString();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
if (request.enableEntityExpansion !== undefined) {
|
|
55
|
+
query.enableEntityExpansion = request.enableEntityExpansion.toString();
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (request.enableGraphConnectivity !== undefined) {
|
|
59
|
+
query.enableGraphConnectivity = request.enableGraphConnectivity.toString();
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
if (request.enableSemanticDedup !== undefined) {
|
|
63
|
+
query.enableSemanticDedup = request.enableSemanticDedup.toString();
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
if (request.rerankingStrategy) {
|
|
67
|
+
query.rerankingStrategy = request.rerankingStrategy;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (request.fusionWeights) {
|
|
71
|
+
query.fusionWeights = JSON.stringify(request.fusionWeights);
|
|
72
|
+
}
|
|
73
|
+
|
|
49
74
|
return this.httpClient.request<SearchResponse>({
|
|
50
75
|
method: 'GET',
|
|
51
76
|
path: '/v1/search',
|
package/src/types.ts
CHANGED
|
@@ -66,6 +66,24 @@ export interface SearchRequest {
|
|
|
66
66
|
filter?: Record<string, any>;
|
|
67
67
|
/** Minimum relevance score threshold (0-1) */
|
|
68
68
|
threshold?: number;
|
|
69
|
+
/** Enable query rewriting (default: true) */
|
|
70
|
+
enableQueryRewriting?: boolean;
|
|
71
|
+
/** Enable entity expansion search (default: false) */
|
|
72
|
+
enableEntityExpansion?: boolean;
|
|
73
|
+
/** Enable graph connectivity search (default: false) */
|
|
74
|
+
enableGraphConnectivity?: boolean;
|
|
75
|
+
/** Enable semantic deduplication (default: false) */
|
|
76
|
+
enableSemanticDedup?: boolean;
|
|
77
|
+
/** Reranking strategy: 'none', 'cross-encoder', 'llm' (default: 'none') */
|
|
78
|
+
rerankingStrategy?: 'none' | 'cross-encoder' | 'llm';
|
|
79
|
+
/** Custom fusion weights for multi-method retrieval */
|
|
80
|
+
fusionWeights?: {
|
|
81
|
+
vector?: number;
|
|
82
|
+
bm25?: number;
|
|
83
|
+
recency?: number;
|
|
84
|
+
entity?: number;
|
|
85
|
+
graph?: number;
|
|
86
|
+
};
|
|
69
87
|
}
|
|
70
88
|
|
|
71
89
|
/**
|
|
@@ -78,6 +96,20 @@ export interface SearchResult {
|
|
|
78
96
|
score: number;
|
|
79
97
|
/** Highlighted text snippets */
|
|
80
98
|
highlights?: string[];
|
|
99
|
+
/** Score breakdown by retrieval method */
|
|
100
|
+
scoreBreakdown?: {
|
|
101
|
+
vectorScore?: number;
|
|
102
|
+
bm25Score?: number;
|
|
103
|
+
recencyScore?: number;
|
|
104
|
+
entityScore?: number;
|
|
105
|
+
graphScore?: number;
|
|
106
|
+
};
|
|
107
|
+
/** Connected memories (if graph enhancement enabled) */
|
|
108
|
+
connections?: Array<{
|
|
109
|
+
memoryId: string;
|
|
110
|
+
connectionType: 'updates' | 'extends' | 'derives' | 'similarity';
|
|
111
|
+
connectionStrength: number;
|
|
112
|
+
}>;
|
|
81
113
|
}
|
|
82
114
|
|
|
83
115
|
/**
|