@langchain/langgraph-checkpoint 0.0.12 → 0.0.13
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/dist/base.cjs +2 -0
- package/dist/base.js +3 -1
- package/dist/serde/types.cjs +3 -1
- package/dist/serde/types.d.ts +2 -0
- package/dist/serde/types.js +2 -0
- package/dist/store/base.cjs +144 -58
- package/dist/store/base.d.ts +262 -54
- package/dist/store/base.js +141 -57
- package/dist/store/memory.cjs +273 -28
- package/dist/store/memory.d.ts +48 -3
- package/dist/store/memory.js +273 -28
- package/dist/store/utils.cjs +261 -0
- package/dist/store/utils.d.ts +26 -0
- package/dist/store/utils.js +254 -0
- package/dist/types.d.ts +2 -1
- package/package.json +2 -2
package/dist/store/base.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { Embeddings } from "@langchain/core/embeddings";
|
|
1
2
|
/**
|
|
2
3
|
* Error thrown when an invalid namespace is provided.
|
|
3
4
|
*/
|
|
@@ -31,16 +32,39 @@ export interface Item {
|
|
|
31
32
|
*/
|
|
32
33
|
updatedAt: Date;
|
|
33
34
|
}
|
|
35
|
+
/**
|
|
36
|
+
* Represents a search result item with relevance score.
|
|
37
|
+
* Extends the base Item interface with an optional similarity score.
|
|
38
|
+
*/
|
|
39
|
+
export interface SearchItem extends Item {
|
|
40
|
+
/**
|
|
41
|
+
* Relevance/similarity score if from a ranked operation.
|
|
42
|
+
* Higher scores indicate better matches.
|
|
43
|
+
*
|
|
44
|
+
* This is typically a cosine similarity score between -1 and 1,
|
|
45
|
+
* where 1 indicates identical vectors and -1 indicates opposite vectors.
|
|
46
|
+
*/
|
|
47
|
+
score?: number;
|
|
48
|
+
}
|
|
34
49
|
/**
|
|
35
50
|
* Operation to retrieve an item by namespace and ID.
|
|
36
51
|
*/
|
|
37
52
|
export interface GetOperation {
|
|
38
53
|
/**
|
|
39
54
|
* Hierarchical path for the item.
|
|
55
|
+
*
|
|
56
|
+
* @example
|
|
57
|
+
* // Get a user profile
|
|
58
|
+
* namespace: ["users", "profiles"]
|
|
40
59
|
*/
|
|
41
60
|
namespace: string[];
|
|
42
61
|
/**
|
|
43
62
|
* Unique identifier within the namespace.
|
|
63
|
+
* Together with namespace forms the complete path to the item.
|
|
64
|
+
*
|
|
65
|
+
* @example
|
|
66
|
+
* key: "user123" // For a user profile
|
|
67
|
+
* key: "doc456" // For a document
|
|
44
68
|
*/
|
|
45
69
|
key: string;
|
|
46
70
|
}
|
|
@@ -50,10 +74,39 @@ export interface GetOperation {
|
|
|
50
74
|
export interface SearchOperation {
|
|
51
75
|
/**
|
|
52
76
|
* Hierarchical path prefix to search within.
|
|
77
|
+
* Only items under this prefix will be searched.
|
|
78
|
+
*
|
|
79
|
+
* @example
|
|
80
|
+
* // Search all user documents
|
|
81
|
+
* namespacePrefix: ["users", "documents"]
|
|
82
|
+
*
|
|
83
|
+
* // Search everything
|
|
84
|
+
* namespacePrefix: []
|
|
53
85
|
*/
|
|
54
86
|
namespacePrefix: string[];
|
|
55
87
|
/**
|
|
56
|
-
* Key-value pairs to filter results.
|
|
88
|
+
* Key-value pairs to filter results based on exact matches or comparison operators.
|
|
89
|
+
*
|
|
90
|
+
* Supports both exact matches and operator-based comparisons:
|
|
91
|
+
* - $eq: Equal to (same as direct value comparison)
|
|
92
|
+
* - $ne: Not equal to
|
|
93
|
+
* - $gt: Greater than
|
|
94
|
+
* - $gte: Greater than or equal to
|
|
95
|
+
* - $lt: Less than
|
|
96
|
+
* - $lte: Less than or equal to
|
|
97
|
+
*
|
|
98
|
+
* @example
|
|
99
|
+
* // Exact match
|
|
100
|
+
* filter: { status: "active" }
|
|
101
|
+
*
|
|
102
|
+
* // With operators
|
|
103
|
+
* filter: { score: { $gt: 4.99 } }
|
|
104
|
+
*
|
|
105
|
+
* // Multiple conditions
|
|
106
|
+
* filter: {
|
|
107
|
+
* score: { $gte: 3.0 },
|
|
108
|
+
* color: "red"
|
|
109
|
+
* }
|
|
57
110
|
*/
|
|
58
111
|
filter?: Record<string, any>;
|
|
59
112
|
/**
|
|
@@ -63,9 +116,23 @@ export interface SearchOperation {
|
|
|
63
116
|
limit?: number;
|
|
64
117
|
/**
|
|
65
118
|
* Number of items to skip before returning results.
|
|
119
|
+
* Useful for pagination.
|
|
66
120
|
* @default 0
|
|
67
121
|
*/
|
|
68
122
|
offset?: number;
|
|
123
|
+
/**
|
|
124
|
+
* Natural language search query for semantic search.
|
|
125
|
+
* When provided, results will be ranked by relevance to this query
|
|
126
|
+
* using vector similarity search.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* // Find technical documentation about APIs
|
|
130
|
+
* query: "technical documentation about REST APIs"
|
|
131
|
+
*
|
|
132
|
+
* // Find recent ML papers
|
|
133
|
+
* query: "machine learning papers from 2023"
|
|
134
|
+
*/
|
|
135
|
+
query?: string;
|
|
69
136
|
}
|
|
70
137
|
/**
|
|
71
138
|
* Operation to store, update, or delete an item.
|
|
@@ -73,96 +140,237 @@ export interface SearchOperation {
|
|
|
73
140
|
export interface PutOperation {
|
|
74
141
|
/**
|
|
75
142
|
* Hierarchical path for the item.
|
|
76
|
-
*
|
|
77
|
-
*
|
|
143
|
+
* Acts as a folder-like structure to organize items.
|
|
144
|
+
* Each element represents one level in the hierarchy.
|
|
145
|
+
*
|
|
146
|
+
* @example
|
|
147
|
+
* // Root level documents
|
|
148
|
+
* namespace: ["documents"]
|
|
149
|
+
*
|
|
150
|
+
* // User-specific documents
|
|
151
|
+
* namespace: ["documents", "user123"]
|
|
152
|
+
*
|
|
153
|
+
* // Nested cache structure
|
|
154
|
+
* namespace: ["cache", "docs", "v1"]
|
|
78
155
|
*/
|
|
79
156
|
namespace: string[];
|
|
80
157
|
/**
|
|
81
|
-
* Unique identifier for the document.
|
|
82
|
-
*
|
|
158
|
+
* Unique identifier for the document within its namespace.
|
|
159
|
+
* Together with namespace forms the complete path to the item.
|
|
160
|
+
*
|
|
161
|
+
* Example: If namespace is ["documents", "user123"] and key is "report1",
|
|
162
|
+
* the full path would effectively be "documents/user123/report1"
|
|
83
163
|
*/
|
|
84
164
|
key: string;
|
|
85
165
|
/**
|
|
86
|
-
* Data to be stored, or
|
|
87
|
-
*
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
91
|
-
*
|
|
166
|
+
* Data to be stored, or null to delete the item.
|
|
167
|
+
* Must be a JSON-serializable object with string keys.
|
|
168
|
+
* Setting to null signals that the item should be deleted.
|
|
169
|
+
*
|
|
170
|
+
* @example
|
|
171
|
+
* {
|
|
172
|
+
* field1: "string value",
|
|
173
|
+
* field2: 123,
|
|
174
|
+
* nested: { can: "contain", any: "serializable data" }
|
|
175
|
+
* }
|
|
92
176
|
*/
|
|
93
177
|
value: Record<string, any> | null;
|
|
178
|
+
/**
|
|
179
|
+
* Controls how the item's fields are indexed for search operations.
|
|
180
|
+
*
|
|
181
|
+
* - undefined: Uses store's default indexing configuration
|
|
182
|
+
* - false: Disables indexing for this item
|
|
183
|
+
* - string[]: List of field paths to index
|
|
184
|
+
*
|
|
185
|
+
* Path syntax supports:
|
|
186
|
+
* - Nested fields: "metadata.title"
|
|
187
|
+
* - Array access: "chapters[*].content" (each indexed separately)
|
|
188
|
+
* - Specific indices: "authors[0].name"
|
|
189
|
+
*
|
|
190
|
+
* @example
|
|
191
|
+
* // Index specific fields
|
|
192
|
+
* index: ["metadata.title", "chapters[*].content"]
|
|
193
|
+
*
|
|
194
|
+
* // Disable indexing
|
|
195
|
+
* index: false
|
|
196
|
+
*/
|
|
197
|
+
index?: false | string[];
|
|
94
198
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
matchType: NamespaceMatchType;
|
|
99
|
-
path: NameSpacePath;
|
|
100
|
-
}
|
|
199
|
+
/**
|
|
200
|
+
* Operation to list and filter namespaces in the store.
|
|
201
|
+
*/
|
|
101
202
|
export interface ListNamespacesOperation {
|
|
102
203
|
matchConditions?: MatchCondition[];
|
|
103
204
|
maxDepth?: number;
|
|
104
205
|
limit: number;
|
|
105
206
|
offset: number;
|
|
106
207
|
}
|
|
208
|
+
export type NameSpacePath = (string | "*")[];
|
|
209
|
+
export type NamespaceMatchType = "prefix" | "suffix";
|
|
210
|
+
export interface MatchCondition {
|
|
211
|
+
matchType: NamespaceMatchType;
|
|
212
|
+
path: NameSpacePath;
|
|
213
|
+
}
|
|
107
214
|
export type Operation = GetOperation | SearchOperation | PutOperation | ListNamespacesOperation;
|
|
108
215
|
export type OperationResults<Tuple extends readonly Operation[]> = {
|
|
109
|
-
[K in keyof Tuple]: Tuple[K] extends PutOperation ? void : Tuple[K] extends SearchOperation ?
|
|
216
|
+
[K in keyof Tuple]: Tuple[K] extends PutOperation ? void : Tuple[K] extends SearchOperation ? SearchItem[] : Tuple[K] extends GetOperation ? Item | null : Tuple[K] extends ListNamespacesOperation ? string[][] : never;
|
|
110
217
|
};
|
|
111
218
|
/**
|
|
112
|
-
*
|
|
219
|
+
* Configuration for indexing documents for semantic search in the store.
|
|
220
|
+
*
|
|
221
|
+
* This configures how documents are embedded and indexed for vector similarity search.
|
|
222
|
+
*/
|
|
223
|
+
export interface IndexConfig {
|
|
224
|
+
/**
|
|
225
|
+
* Number of dimensions in the embedding vectors.
|
|
226
|
+
*
|
|
227
|
+
* Common embedding model dimensions:
|
|
228
|
+
* - OpenAI text-embedding-3-large: 256, 1024, or 3072
|
|
229
|
+
* - OpenAI text-embedding-3-small: 512 or 1536
|
|
230
|
+
* - OpenAI text-embedding-ada-002: 1536
|
|
231
|
+
* - Cohere embed-english-v3.0: 1024
|
|
232
|
+
* - Cohere embed-english-light-v3.0: 384
|
|
233
|
+
* - Cohere embed-multilingual-v3.0: 1024
|
|
234
|
+
* - Cohere embed-multilingual-light-v3.0: 384
|
|
235
|
+
*/
|
|
236
|
+
dims: number;
|
|
237
|
+
/**
|
|
238
|
+
* The embeddings model to use for generating vectors.
|
|
239
|
+
* This should be a LangChain Embeddings implementation.
|
|
240
|
+
*/
|
|
241
|
+
embeddings: Embeddings;
|
|
242
|
+
/**
|
|
243
|
+
* Fields to extract text from for embedding generation.
|
|
244
|
+
*
|
|
245
|
+
* Path syntax supports:
|
|
246
|
+
* - Simple field access: "field"
|
|
247
|
+
* - Nested fields: "metadata.title"
|
|
248
|
+
* - Array indexing:
|
|
249
|
+
* - All elements: "chapters[*].content"
|
|
250
|
+
* - Specific index: "authors[0].name"
|
|
251
|
+
* - Last element: "array[-1]"
|
|
252
|
+
*
|
|
253
|
+
* @default ["$"] Embeds the entire document as one vector
|
|
254
|
+
*/
|
|
255
|
+
fields?: string[];
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Utility function to get text at a specific JSON path
|
|
259
|
+
*/
|
|
260
|
+
export declare function getTextAtPath(obj: any, path: string): string[];
|
|
261
|
+
/**
|
|
262
|
+
* Tokenizes a JSON path into parts
|
|
263
|
+
*/
|
|
264
|
+
export declare function tokenizePath(path: string): string[];
|
|
265
|
+
/**
|
|
266
|
+
* Abstract base class for persistent key-value stores.
|
|
267
|
+
*
|
|
268
|
+
* Stores enable persistence and memory that can be shared across threads,
|
|
269
|
+
* scoped to user IDs, assistant IDs, or other arbitrary namespaces.
|
|
270
|
+
*
|
|
271
|
+
* Features:
|
|
272
|
+
* - Hierarchical namespaces for organization
|
|
273
|
+
* - Key-value storage with metadata
|
|
274
|
+
* - Vector similarity search (if configured)
|
|
275
|
+
* - Filtering and pagination
|
|
113
276
|
*/
|
|
114
277
|
export declare abstract class BaseStore {
|
|
115
278
|
/**
|
|
116
|
-
* Execute a batch
|
|
117
|
-
*
|
|
118
|
-
*
|
|
279
|
+
* Execute multiple operations in a single batch.
|
|
280
|
+
* This is more efficient than executing operations individually.
|
|
281
|
+
*
|
|
282
|
+
* @param operations Array of operations to execute
|
|
283
|
+
* @returns Promise resolving to results matching the operations
|
|
119
284
|
*/
|
|
120
|
-
abstract batch<Op extends Operation[]>(
|
|
285
|
+
abstract batch<Op extends Operation[]>(operations: Op): Promise<OperationResults<Op>>;
|
|
121
286
|
/**
|
|
122
|
-
* Retrieve a single item.
|
|
123
|
-
*
|
|
124
|
-
* @param
|
|
125
|
-
* @
|
|
287
|
+
* Retrieve a single item by its namespace and key.
|
|
288
|
+
*
|
|
289
|
+
* @param namespace Hierarchical path for the item
|
|
290
|
+
* @param key Unique identifier within the namespace
|
|
291
|
+
* @returns Promise resolving to the item or null if not found
|
|
126
292
|
*/
|
|
127
293
|
get(namespace: string[], key: string): Promise<Item | null>;
|
|
128
294
|
/**
|
|
129
295
|
* Search for items within a namespace prefix.
|
|
130
|
-
*
|
|
131
|
-
*
|
|
132
|
-
* @param
|
|
133
|
-
* @param options
|
|
134
|
-
* @
|
|
135
|
-
*
|
|
296
|
+
* Supports both metadata filtering and vector similarity search.
|
|
297
|
+
*
|
|
298
|
+
* @param namespacePrefix Hierarchical path prefix to search within
|
|
299
|
+
* @param options Search options for filtering and pagination
|
|
300
|
+
* @returns Promise resolving to list of matching items with relevance scores
|
|
301
|
+
*
|
|
302
|
+
* @example
|
|
303
|
+
* // Search with filters
|
|
304
|
+
* await store.search(["documents"], {
|
|
305
|
+
* filter: { type: "report", status: "active" },
|
|
306
|
+
* limit: 5,
|
|
307
|
+
* offset: 10
|
|
308
|
+
* });
|
|
309
|
+
*
|
|
310
|
+
* // Vector similarity search
|
|
311
|
+
* await store.search(["users", "content"], {
|
|
312
|
+
* query: "technical documentation about APIs",
|
|
313
|
+
* limit: 20
|
|
314
|
+
* });
|
|
136
315
|
*/
|
|
137
316
|
search(namespacePrefix: string[], options?: {
|
|
138
317
|
filter?: Record<string, any>;
|
|
139
318
|
limit?: number;
|
|
140
319
|
offset?: number;
|
|
141
|
-
|
|
320
|
+
query?: string;
|
|
321
|
+
}): Promise<SearchItem[]>;
|
|
142
322
|
/**
|
|
143
323
|
* Store or update an item.
|
|
144
|
-
*
|
|
145
|
-
* @param
|
|
146
|
-
* @param
|
|
324
|
+
*
|
|
325
|
+
* @param namespace Hierarchical path for the item
|
|
326
|
+
* @param key Unique identifier within the namespace
|
|
327
|
+
* @param value Object containing the item's data
|
|
328
|
+
* @param index Optional indexing configuration
|
|
329
|
+
*
|
|
330
|
+
* @example
|
|
331
|
+
* // Simple storage
|
|
332
|
+
* await store.put(["docs"], "report", { title: "Annual Report" });
|
|
333
|
+
*
|
|
334
|
+
* // With specific field indexing
|
|
335
|
+
* await store.put(
|
|
336
|
+
* ["docs"],
|
|
337
|
+
* "report",
|
|
338
|
+
* {
|
|
339
|
+
* title: "Q4 Report",
|
|
340
|
+
* chapters: [{ content: "..." }, { content: "..." }]
|
|
341
|
+
* },
|
|
342
|
+
* ["title", "chapters[*].content"]
|
|
343
|
+
* );
|
|
147
344
|
*/
|
|
148
|
-
put(namespace: string[], key: string, value: Record<string, any
|
|
345
|
+
put(namespace: string[], key: string, value: Record<string, any>, index?: false | string[]): Promise<void>;
|
|
149
346
|
/**
|
|
150
|
-
* Delete an item.
|
|
151
|
-
*
|
|
152
|
-
* @param
|
|
347
|
+
* Delete an item from the store.
|
|
348
|
+
*
|
|
349
|
+
* @param namespace Hierarchical path for the item
|
|
350
|
+
* @param key Unique identifier within the namespace
|
|
153
351
|
*/
|
|
154
352
|
delete(namespace: string[], key: string): Promise<void>;
|
|
155
353
|
/**
|
|
156
354
|
* List and filter namespaces in the store.
|
|
157
|
-
*
|
|
158
|
-
*
|
|
159
|
-
* @param options
|
|
160
|
-
* @
|
|
161
|
-
*
|
|
162
|
-
* @
|
|
163
|
-
*
|
|
164
|
-
|
|
165
|
-
|
|
355
|
+
* Used to explore data organization and navigate the namespace hierarchy.
|
|
356
|
+
*
|
|
357
|
+
* @param options Options for listing namespaces
|
|
358
|
+
* @returns Promise resolving to list of namespace paths
|
|
359
|
+
*
|
|
360
|
+
* @example
|
|
361
|
+
* // List all namespaces under "documents"
|
|
362
|
+
* await store.listNamespaces({
|
|
363
|
+
* prefix: ["documents"],
|
|
364
|
+
* maxDepth: 2
|
|
365
|
+
* });
|
|
366
|
+
*
|
|
367
|
+
* // List namespaces ending with "v1"
|
|
368
|
+
* await store.listNamespaces({
|
|
369
|
+
* suffix: ["v1"],
|
|
370
|
+
* limit: 50
|
|
371
|
+
* });
|
|
372
|
+
*/
|
|
373
|
+
listNamespaces(options?: {
|
|
166
374
|
prefix?: string[];
|
|
167
375
|
suffix?: string[];
|
|
168
376
|
maxDepth?: number;
|
|
@@ -170,11 +378,11 @@ export declare abstract class BaseStore {
|
|
|
170
378
|
offset?: number;
|
|
171
379
|
}): Promise<string[][]>;
|
|
172
380
|
/**
|
|
173
|
-
*
|
|
381
|
+
* Start the store. Override if initialization is needed.
|
|
174
382
|
*/
|
|
175
|
-
|
|
383
|
+
start(): void;
|
|
176
384
|
/**
|
|
177
|
-
*
|
|
385
|
+
* Stop the store. Override if cleanup is needed.
|
|
178
386
|
*/
|
|
179
|
-
|
|
387
|
+
stop(): void;
|
|
180
388
|
}
|
package/dist/store/base.js
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
1
|
/**
|
|
3
2
|
* Error thrown when an invalid namespace is provided.
|
|
4
3
|
*/
|
|
@@ -34,69 +33,158 @@ function validateNamespace(namespace) {
|
|
|
34
33
|
}
|
|
35
34
|
}
|
|
36
35
|
/**
|
|
37
|
-
*
|
|
36
|
+
* Utility function to get text at a specific JSON path
|
|
37
|
+
*/
|
|
38
|
+
export function getTextAtPath(obj, path) {
|
|
39
|
+
const parts = path.split(".");
|
|
40
|
+
let current = obj;
|
|
41
|
+
for (const part of parts) {
|
|
42
|
+
if (part.includes("[")) {
|
|
43
|
+
const [arrayName, indexStr] = part.split("[");
|
|
44
|
+
const index = indexStr.replace("]", "");
|
|
45
|
+
if (!current[arrayName])
|
|
46
|
+
return [];
|
|
47
|
+
if (index === "*") {
|
|
48
|
+
const results = [];
|
|
49
|
+
for (const item of current[arrayName]) {
|
|
50
|
+
if (typeof item === "string")
|
|
51
|
+
results.push(item);
|
|
52
|
+
}
|
|
53
|
+
return results;
|
|
54
|
+
}
|
|
55
|
+
const idx = parseInt(index, 10);
|
|
56
|
+
if (Number.isNaN(idx))
|
|
57
|
+
return [];
|
|
58
|
+
current = current[arrayName][idx];
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
current = current[part];
|
|
62
|
+
}
|
|
63
|
+
if (current === undefined)
|
|
64
|
+
return [];
|
|
65
|
+
}
|
|
66
|
+
return typeof current === "string" ? [current] : [];
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Tokenizes a JSON path into parts
|
|
70
|
+
*/
|
|
71
|
+
export function tokenizePath(path) {
|
|
72
|
+
return path.split(".");
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Abstract base class for persistent key-value stores.
|
|
76
|
+
*
|
|
77
|
+
* Stores enable persistence and memory that can be shared across threads,
|
|
78
|
+
* scoped to user IDs, assistant IDs, or other arbitrary namespaces.
|
|
79
|
+
*
|
|
80
|
+
* Features:
|
|
81
|
+
* - Hierarchical namespaces for organization
|
|
82
|
+
* - Key-value storage with metadata
|
|
83
|
+
* - Vector similarity search (if configured)
|
|
84
|
+
* - Filtering and pagination
|
|
38
85
|
*/
|
|
39
86
|
export class BaseStore {
|
|
40
|
-
// convenience methods
|
|
41
87
|
/**
|
|
42
|
-
* Retrieve a single item.
|
|
43
|
-
*
|
|
44
|
-
* @param
|
|
45
|
-
* @
|
|
88
|
+
* Retrieve a single item by its namespace and key.
|
|
89
|
+
*
|
|
90
|
+
* @param namespace Hierarchical path for the item
|
|
91
|
+
* @param key Unique identifier within the namespace
|
|
92
|
+
* @returns Promise resolving to the item or null if not found
|
|
46
93
|
*/
|
|
47
94
|
async get(namespace, key) {
|
|
48
|
-
|
|
49
|
-
return batchResult[0];
|
|
95
|
+
return (await this.batch([{ namespace, key }]))[0];
|
|
50
96
|
}
|
|
51
97
|
/**
|
|
52
98
|
* Search for items within a namespace prefix.
|
|
53
|
-
*
|
|
54
|
-
*
|
|
55
|
-
* @param
|
|
56
|
-
* @param options
|
|
57
|
-
* @
|
|
58
|
-
*
|
|
99
|
+
* Supports both metadata filtering and vector similarity search.
|
|
100
|
+
*
|
|
101
|
+
* @param namespacePrefix Hierarchical path prefix to search within
|
|
102
|
+
* @param options Search options for filtering and pagination
|
|
103
|
+
* @returns Promise resolving to list of matching items with relevance scores
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* // Search with filters
|
|
107
|
+
* await store.search(["documents"], {
|
|
108
|
+
* filter: { type: "report", status: "active" },
|
|
109
|
+
* limit: 5,
|
|
110
|
+
* offset: 10
|
|
111
|
+
* });
|
|
112
|
+
*
|
|
113
|
+
* // Vector similarity search
|
|
114
|
+
* await store.search(["users", "content"], {
|
|
115
|
+
* query: "technical documentation about APIs",
|
|
116
|
+
* limit: 20
|
|
117
|
+
* });
|
|
59
118
|
*/
|
|
60
|
-
async search(namespacePrefix, options) {
|
|
61
|
-
const
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
119
|
+
async search(namespacePrefix, options = {}) {
|
|
120
|
+
const { filter, limit = 10, offset = 0, query } = options;
|
|
121
|
+
return (await this.batch([
|
|
122
|
+
{
|
|
123
|
+
namespacePrefix,
|
|
124
|
+
filter,
|
|
125
|
+
limit,
|
|
126
|
+
offset,
|
|
127
|
+
query,
|
|
128
|
+
},
|
|
129
|
+
]))[0];
|
|
70
130
|
}
|
|
71
131
|
/**
|
|
72
132
|
* Store or update an item.
|
|
73
|
-
*
|
|
74
|
-
* @param
|
|
75
|
-
* @param
|
|
133
|
+
*
|
|
134
|
+
* @param namespace Hierarchical path for the item
|
|
135
|
+
* @param key Unique identifier within the namespace
|
|
136
|
+
* @param value Object containing the item's data
|
|
137
|
+
* @param index Optional indexing configuration
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* // Simple storage
|
|
141
|
+
* await store.put(["docs"], "report", { title: "Annual Report" });
|
|
142
|
+
*
|
|
143
|
+
* // With specific field indexing
|
|
144
|
+
* await store.put(
|
|
145
|
+
* ["docs"],
|
|
146
|
+
* "report",
|
|
147
|
+
* {
|
|
148
|
+
* title: "Q4 Report",
|
|
149
|
+
* chapters: [{ content: "..." }, { content: "..." }]
|
|
150
|
+
* },
|
|
151
|
+
* ["title", "chapters[*].content"]
|
|
152
|
+
* );
|
|
76
153
|
*/
|
|
77
|
-
async put(namespace, key, value) {
|
|
154
|
+
async put(namespace, key, value, index) {
|
|
78
155
|
validateNamespace(namespace);
|
|
79
|
-
await this.batch([{ namespace, key, value }]);
|
|
156
|
+
await this.batch([{ namespace, key, value, index }]);
|
|
80
157
|
}
|
|
81
158
|
/**
|
|
82
|
-
* Delete an item.
|
|
83
|
-
*
|
|
84
|
-
* @param
|
|
159
|
+
* Delete an item from the store.
|
|
160
|
+
*
|
|
161
|
+
* @param namespace Hierarchical path for the item
|
|
162
|
+
* @param key Unique identifier within the namespace
|
|
85
163
|
*/
|
|
86
164
|
async delete(namespace, key) {
|
|
87
165
|
await this.batch([{ namespace, key, value: null }]);
|
|
88
166
|
}
|
|
89
167
|
/**
|
|
90
168
|
* List and filter namespaces in the store.
|
|
91
|
-
*
|
|
92
|
-
*
|
|
93
|
-
* @param options
|
|
94
|
-
* @
|
|
95
|
-
*
|
|
96
|
-
* @
|
|
97
|
-
*
|
|
169
|
+
* Used to explore data organization and navigate the namespace hierarchy.
|
|
170
|
+
*
|
|
171
|
+
* @param options Options for listing namespaces
|
|
172
|
+
* @returns Promise resolving to list of namespace paths
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* // List all namespaces under "documents"
|
|
176
|
+
* await store.listNamespaces({
|
|
177
|
+
* prefix: ["documents"],
|
|
178
|
+
* maxDepth: 2
|
|
179
|
+
* });
|
|
180
|
+
*
|
|
181
|
+
* // List namespaces ending with "v1"
|
|
182
|
+
* await store.listNamespaces({
|
|
183
|
+
* suffix: ["v1"],
|
|
184
|
+
* limit: 50
|
|
185
|
+
* });
|
|
98
186
|
*/
|
|
99
|
-
async listNamespaces(options) {
|
|
187
|
+
async listNamespaces(options = {}) {
|
|
100
188
|
const { prefix, suffix, maxDepth, limit = 100, offset = 0 } = options;
|
|
101
189
|
const matchConditions = [];
|
|
102
190
|
if (prefix) {
|
|
@@ -105,25 +193,21 @@ export class BaseStore {
|
|
|
105
193
|
if (suffix) {
|
|
106
194
|
matchConditions.push({ matchType: "suffix", path: suffix });
|
|
107
195
|
}
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
196
|
+
return (await this.batch([
|
|
197
|
+
{
|
|
198
|
+
matchConditions: matchConditions.length ? matchConditions : undefined,
|
|
199
|
+
maxDepth,
|
|
200
|
+
limit,
|
|
201
|
+
offset,
|
|
202
|
+
},
|
|
203
|
+
]))[0];
|
|
116
204
|
}
|
|
117
205
|
/**
|
|
118
|
-
*
|
|
206
|
+
* Start the store. Override if initialization is needed.
|
|
119
207
|
*/
|
|
120
|
-
|
|
121
|
-
// no-op if not implemented.
|
|
122
|
-
}
|
|
208
|
+
start() { }
|
|
123
209
|
/**
|
|
124
|
-
*
|
|
210
|
+
* Stop the store. Override if cleanup is needed.
|
|
125
211
|
*/
|
|
126
|
-
|
|
127
|
-
// no-op if not implemented.
|
|
128
|
-
}
|
|
212
|
+
stop() { }
|
|
129
213
|
}
|