agent-memory-client 0.2.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/dist/index.js ADDED
@@ -0,0 +1,646 @@
1
+ 'use strict';
2
+
3
+ // src/errors.ts
4
+ var MemoryClientError = class _MemoryClientError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ this.name = "MemoryClientError";
8
+ if (Error.captureStackTrace) {
9
+ Error.captureStackTrace(this, _MemoryClientError);
10
+ }
11
+ }
12
+ };
13
+ var MemoryValidationError = class extends MemoryClientError {
14
+ constructor(message) {
15
+ super(message);
16
+ this.name = "MemoryValidationError";
17
+ }
18
+ };
19
+ var MemoryNotFoundError = class extends MemoryClientError {
20
+ constructor(message) {
21
+ super(message);
22
+ this.name = "MemoryNotFoundError";
23
+ }
24
+ };
25
+ var MemoryServerError = class extends MemoryClientError {
26
+ statusCode;
27
+ constructor(message, statusCode) {
28
+ super(message);
29
+ this.name = "MemoryServerError";
30
+ this.statusCode = statusCode;
31
+ }
32
+ };
33
+
34
+ // src/filters.ts
35
+ var SessionId = class {
36
+ eq;
37
+ in_;
38
+ not_eq;
39
+ not_in;
40
+ ne;
41
+ constructor(options = {}) {
42
+ this.eq = options.eq;
43
+ this.in_ = options.in_;
44
+ this.not_eq = options.not_eq;
45
+ this.not_in = options.not_in;
46
+ this.ne = options.ne;
47
+ }
48
+ toJSON() {
49
+ const result = {};
50
+ if (this.eq !== void 0) result.eq = this.eq;
51
+ if (this.in_ !== void 0) result.in_ = this.in_;
52
+ if (this.not_eq !== void 0) result.not_eq = this.not_eq;
53
+ if (this.not_in !== void 0) result.not_in = this.not_in;
54
+ if (this.ne !== void 0) result.ne = this.ne;
55
+ return result;
56
+ }
57
+ };
58
+ var Namespace = class {
59
+ eq;
60
+ in_;
61
+ not_eq;
62
+ not_in;
63
+ constructor(options = {}) {
64
+ this.eq = options.eq;
65
+ this.in_ = options.in_;
66
+ this.not_eq = options.not_eq;
67
+ this.not_in = options.not_in;
68
+ }
69
+ toJSON() {
70
+ const result = {};
71
+ if (this.eq !== void 0) result.eq = this.eq;
72
+ if (this.in_ !== void 0) result.in_ = this.in_;
73
+ if (this.not_eq !== void 0) result.not_eq = this.not_eq;
74
+ if (this.not_in !== void 0) result.not_in = this.not_in;
75
+ return result;
76
+ }
77
+ };
78
+ var UserId = class {
79
+ eq;
80
+ in_;
81
+ not_eq;
82
+ not_in;
83
+ constructor(options = {}) {
84
+ this.eq = options.eq;
85
+ this.in_ = options.in_;
86
+ this.not_eq = options.not_eq;
87
+ this.not_in = options.not_in;
88
+ }
89
+ toJSON() {
90
+ const result = {};
91
+ if (this.eq !== void 0) result.eq = this.eq;
92
+ if (this.in_ !== void 0) result.in_ = this.in_;
93
+ if (this.not_eq !== void 0) result.not_eq = this.not_eq;
94
+ if (this.not_in !== void 0) result.not_in = this.not_in;
95
+ return result;
96
+ }
97
+ };
98
+ var Topics = class {
99
+ any;
100
+ all;
101
+ none;
102
+ constructor(options = {}) {
103
+ this.any = options.any;
104
+ this.all = options.all;
105
+ this.none = options.none;
106
+ }
107
+ toJSON() {
108
+ const result = {};
109
+ if (this.any !== void 0) result.any = this.any;
110
+ if (this.all !== void 0) result.all = this.all;
111
+ if (this.none !== void 0) result.none = this.none;
112
+ return result;
113
+ }
114
+ };
115
+ var Entities = class {
116
+ any;
117
+ all;
118
+ none;
119
+ constructor(options = {}) {
120
+ this.any = options.any;
121
+ this.all = options.all;
122
+ this.none = options.none;
123
+ }
124
+ toJSON() {
125
+ const result = {};
126
+ if (this.any !== void 0) result.any = this.any;
127
+ if (this.all !== void 0) result.all = this.all;
128
+ if (this.none !== void 0) result.none = this.none;
129
+ return result;
130
+ }
131
+ };
132
+ var CreatedAt = class {
133
+ gte;
134
+ lte;
135
+ eq;
136
+ constructor(options = {}) {
137
+ this.gte = options.gte;
138
+ this.lte = options.lte;
139
+ this.eq = options.eq;
140
+ }
141
+ toJSON() {
142
+ const result = {};
143
+ if (this.gte !== void 0)
144
+ result.gte = this.gte instanceof Date ? this.gte.toISOString() : this.gte;
145
+ if (this.lte !== void 0)
146
+ result.lte = this.lte instanceof Date ? this.lte.toISOString() : this.lte;
147
+ if (this.eq !== void 0)
148
+ result.eq = this.eq instanceof Date ? this.eq.toISOString() : this.eq;
149
+ return result;
150
+ }
151
+ };
152
+ var LastAccessed = class {
153
+ gte;
154
+ lte;
155
+ eq;
156
+ constructor(options = {}) {
157
+ this.gte = options.gte;
158
+ this.lte = options.lte;
159
+ this.eq = options.eq;
160
+ }
161
+ toJSON() {
162
+ const result = {};
163
+ if (this.gte !== void 0)
164
+ result.gte = this.gte instanceof Date ? this.gte.toISOString() : this.gte;
165
+ if (this.lte !== void 0)
166
+ result.lte = this.lte instanceof Date ? this.lte.toISOString() : this.lte;
167
+ if (this.eq !== void 0)
168
+ result.eq = this.eq instanceof Date ? this.eq.toISOString() : this.eq;
169
+ return result;
170
+ }
171
+ };
172
+ var EventDate = class {
173
+ gte;
174
+ lte;
175
+ eq;
176
+ constructor(options = {}) {
177
+ this.gte = options.gte;
178
+ this.lte = options.lte;
179
+ this.eq = options.eq;
180
+ }
181
+ toJSON() {
182
+ const result = {};
183
+ if (this.gte !== void 0)
184
+ result.gte = this.gte instanceof Date ? this.gte.toISOString() : this.gte;
185
+ if (this.lte !== void 0)
186
+ result.lte = this.lte instanceof Date ? this.lte.toISOString() : this.lte;
187
+ if (this.eq !== void 0)
188
+ result.eq = this.eq instanceof Date ? this.eq.toISOString() : this.eq;
189
+ return result;
190
+ }
191
+ };
192
+ var MemoryType = class {
193
+ eq;
194
+ in_;
195
+ not_eq;
196
+ not_in;
197
+ constructor(options = {}) {
198
+ this.eq = options.eq;
199
+ this.in_ = options.in_;
200
+ this.not_eq = options.not_eq;
201
+ this.not_in = options.not_in;
202
+ }
203
+ toJSON() {
204
+ const result = {};
205
+ if (this.eq !== void 0) result.eq = this.eq;
206
+ if (this.in_ !== void 0) result.in_ = this.in_;
207
+ if (this.not_eq !== void 0) result.not_eq = this.not_eq;
208
+ if (this.not_in !== void 0) result.not_in = this.not_in;
209
+ return result;
210
+ }
211
+ };
212
+
213
+ // src/client.ts
214
+ var VERSION = "0.1.0";
215
+ var MemoryAPIClient = class {
216
+ config;
217
+ fetchFn;
218
+ constructor(config) {
219
+ this.config = {
220
+ timeout: 3e4,
221
+ ...config,
222
+ baseUrl: config.baseUrl.replace(/\/$/, "")
223
+ // Remove trailing slash
224
+ };
225
+ this.fetchFn = config.fetch ?? fetch;
226
+ }
227
+ /**
228
+ * Get default headers for requests
229
+ */
230
+ getHeaders() {
231
+ const headers = {
232
+ "Content-Type": "application/json",
233
+ "User-Agent": `agent-memory-client-js/${VERSION}`,
234
+ "X-Client-Version": VERSION
235
+ };
236
+ if (this.config.apiKey) {
237
+ headers["X-API-Key"] = this.config.apiKey;
238
+ }
239
+ if (this.config.bearerToken) {
240
+ headers["Authorization"] = `Bearer ${this.config.bearerToken}`;
241
+ }
242
+ return headers;
243
+ }
244
+ /**
245
+ * Make an HTTP request with error handling
246
+ */
247
+ async request(method, path, options = {}) {
248
+ const url = new URL(path, this.config.baseUrl);
249
+ if (options.params) {
250
+ for (const [key, value] of Object.entries(options.params)) {
251
+ if (value !== void 0) {
252
+ if (Array.isArray(value)) {
253
+ for (const item of value) {
254
+ url.searchParams.append(key, String(item));
255
+ }
256
+ } else {
257
+ url.searchParams.set(key, String(value));
258
+ }
259
+ }
260
+ }
261
+ }
262
+ const controller = new AbortController();
263
+ const timeoutId = setTimeout(() => controller.abort(), this.config.timeout);
264
+ try {
265
+ const response = await this.fetchFn(url.toString(), {
266
+ method,
267
+ headers: this.getHeaders(),
268
+ body: options.body ? JSON.stringify(options.body) : void 0,
269
+ signal: controller.signal
270
+ });
271
+ clearTimeout(timeoutId);
272
+ if (!response.ok) {
273
+ return this.handleHttpError(response);
274
+ }
275
+ return await response.json();
276
+ } catch (error) {
277
+ clearTimeout(timeoutId);
278
+ if (error instanceof MemoryClientError) {
279
+ throw error;
280
+ }
281
+ if (error instanceof Error && error.name === "AbortError") {
282
+ throw new MemoryClientError(`Request timeout after ${this.config.timeout}ms`);
283
+ }
284
+ throw new MemoryClientError(`Request failed: ${String(error)}`);
285
+ }
286
+ }
287
+ /**
288
+ * Handle HTTP error responses
289
+ */
290
+ async handleHttpError(response) {
291
+ let message;
292
+ try {
293
+ const body = await response.json();
294
+ message = body.detail || body.message || JSON.stringify(body);
295
+ } catch {
296
+ message = await response.text();
297
+ }
298
+ if (response.status === 404) {
299
+ throw new MemoryNotFoundError(message);
300
+ }
301
+ throw new MemoryServerError(message, response.status);
302
+ }
303
+ // ==================== Health ====================
304
+ /**
305
+ * Check server health
306
+ */
307
+ async healthCheck() {
308
+ return this.request("GET", "/v1/health");
309
+ }
310
+ // ==================== Working Memory ====================
311
+ /**
312
+ * List all session IDs
313
+ */
314
+ async listSessions(options = {}) {
315
+ return this.request("GET", "/v1/working-memory/", {
316
+ params: {
317
+ namespace: options.namespace ?? this.config.defaultNamespace,
318
+ limit: options.limit,
319
+ offset: options.offset
320
+ }
321
+ });
322
+ }
323
+ /**
324
+ * Get working memory for a session
325
+ */
326
+ async getWorkingMemory(sessionId, options = {}) {
327
+ try {
328
+ return await this.request(
329
+ "GET",
330
+ `/v1/working-memory/${encodeURIComponent(sessionId)}`,
331
+ {
332
+ params: {
333
+ namespace: options.namespace ?? this.config.defaultNamespace,
334
+ model_name: options.modelName ?? this.config.defaultModelName,
335
+ context_window_max: options.contextWindowMax ?? this.config.defaultContextWindowMax
336
+ }
337
+ }
338
+ );
339
+ } catch (error) {
340
+ if (error instanceof MemoryNotFoundError) {
341
+ return null;
342
+ }
343
+ throw error;
344
+ }
345
+ }
346
+ /**
347
+ * Get or create working memory for a session
348
+ */
349
+ async getOrCreateWorkingMemory(sessionId, options = {}) {
350
+ const existing = await this.getWorkingMemory(sessionId, options);
351
+ if (existing) {
352
+ return existing;
353
+ }
354
+ const workingMemory = {
355
+ session_id: sessionId,
356
+ namespace: options.namespace ?? this.config.defaultNamespace,
357
+ user_id: options.userId,
358
+ messages: [],
359
+ memories: [],
360
+ ttl_seconds: options.ttlSeconds,
361
+ long_term_memory_strategy: options.longTermMemoryStrategy
362
+ };
363
+ return this.putWorkingMemory(sessionId, workingMemory, options);
364
+ }
365
+ /**
366
+ * Create or update working memory for a session
367
+ */
368
+ async putWorkingMemory(sessionId, workingMemory, options = {}) {
369
+ const body = {
370
+ session_id: sessionId,
371
+ ...workingMemory,
372
+ namespace: workingMemory.namespace ?? options.namespace ?? this.config.defaultNamespace
373
+ };
374
+ return this.request(
375
+ "PUT",
376
+ `/v1/working-memory/${encodeURIComponent(sessionId)}`,
377
+ {
378
+ body,
379
+ params: {
380
+ model_name: options.modelName ?? this.config.defaultModelName,
381
+ context_window_max: options.contextWindowMax ?? this.config.defaultContextWindowMax,
382
+ background: options.background
383
+ }
384
+ }
385
+ );
386
+ }
387
+ /**
388
+ * Delete working memory for a session
389
+ */
390
+ async deleteWorkingMemory(sessionId, options = {}) {
391
+ return this.request(
392
+ "DELETE",
393
+ `/v1/working-memory/${encodeURIComponent(sessionId)}`,
394
+ {
395
+ params: {
396
+ namespace: options.namespace ?? this.config.defaultNamespace
397
+ }
398
+ }
399
+ );
400
+ }
401
+ // ==================== Long-term Memory ====================
402
+ /**
403
+ * Create long-term memory records
404
+ */
405
+ async createLongTermMemory(memories, options = {}) {
406
+ return this.request("POST", "/v1/long-term-memory/", {
407
+ body: { memories },
408
+ params: {
409
+ namespace: options.namespace ?? this.config.defaultNamespace
410
+ }
411
+ });
412
+ }
413
+ /**
414
+ * Search long-term memory
415
+ */
416
+ async searchLongTermMemory(options) {
417
+ const body = {
418
+ text: options.text,
419
+ limit: options.limit,
420
+ offset: options.offset,
421
+ distance_threshold: options.distanceThreshold
422
+ };
423
+ if (options.sessionId) {
424
+ body.session_id = options.sessionId instanceof SessionId ? options.sessionId.toJSON() : options.sessionId;
425
+ }
426
+ if (options.namespace) {
427
+ body.namespace = options.namespace instanceof Namespace ? options.namespace.toJSON() : options.namespace;
428
+ }
429
+ if (options.topics) {
430
+ body.topics = options.topics instanceof Topics ? options.topics.toJSON() : options.topics;
431
+ }
432
+ if (options.entities) {
433
+ body.entities = options.entities instanceof Entities ? options.entities.toJSON() : options.entities;
434
+ }
435
+ if (options.createdAt) {
436
+ body.created_at = options.createdAt instanceof CreatedAt ? options.createdAt.toJSON() : options.createdAt;
437
+ }
438
+ if (options.lastAccessed) {
439
+ body.last_accessed = options.lastAccessed instanceof LastAccessed ? options.lastAccessed.toJSON() : options.lastAccessed;
440
+ }
441
+ if (options.userId) {
442
+ body.user_id = options.userId instanceof UserId ? options.userId.toJSON() : options.userId;
443
+ }
444
+ if (options.memoryType) {
445
+ body.memory_type = options.memoryType instanceof MemoryType ? options.memoryType.toJSON() : options.memoryType;
446
+ }
447
+ if (options.eventDate) {
448
+ body.event_date = options.eventDate instanceof EventDate ? options.eventDate.toJSON() : options.eventDate;
449
+ }
450
+ if (options.recency) {
451
+ body.recency_boost = options.recency.recency_boost;
452
+ body.recency_semantic_weight = options.recency.semantic_weight;
453
+ body.recency_recency_weight = options.recency.recency_weight;
454
+ body.recency_freshness_weight = options.recency.freshness_weight;
455
+ body.recency_novelty_weight = options.recency.novelty_weight;
456
+ body.recency_half_life_last_access_days = options.recency.half_life_last_access_days;
457
+ body.recency_half_life_created_days = options.recency.half_life_created_days;
458
+ body.server_side_recency = options.recency.server_side_recency;
459
+ }
460
+ return this.request("POST", "/v1/long-term-memory/search", {
461
+ body
462
+ });
463
+ }
464
+ /**
465
+ * Get a long-term memory by ID
466
+ */
467
+ async getLongTermMemory(memoryId, options = {}) {
468
+ try {
469
+ return await this.request(
470
+ "GET",
471
+ `/v1/long-term-memory/${encodeURIComponent(memoryId)}`,
472
+ {
473
+ params: {
474
+ namespace: options.namespace ?? this.config.defaultNamespace
475
+ }
476
+ }
477
+ );
478
+ } catch (error) {
479
+ if (error instanceof MemoryNotFoundError) {
480
+ return null;
481
+ }
482
+ throw error;
483
+ }
484
+ }
485
+ /**
486
+ * Delete long-term memories by IDs
487
+ */
488
+ async deleteLongTermMemories(memoryIds, options = {}) {
489
+ return this.request("DELETE", "/v1/long-term-memory", {
490
+ params: {
491
+ memory_ids: memoryIds,
492
+ namespace: options.namespace ?? this.config.defaultNamespace
493
+ }
494
+ });
495
+ }
496
+ // ==================== Memory Prompt ====================
497
+ /**
498
+ * Get memory-enhanced prompt
499
+ */
500
+ async memoryPrompt(request) {
501
+ return this.request("POST", "/v1/memory/prompt", {
502
+ body: request
503
+ });
504
+ }
505
+ // ==================== Edit Long-term Memory ====================
506
+ /**
507
+ * Edit a long-term memory by ID
508
+ */
509
+ async editLongTermMemory(memoryId, updates) {
510
+ return this.request(
511
+ "PATCH",
512
+ `/v1/long-term-memory/${encodeURIComponent(memoryId)}`,
513
+ { body: updates }
514
+ );
515
+ }
516
+ // ==================== Forget ====================
517
+ /**
518
+ * Run a forgetting pass with the provided policy
519
+ */
520
+ async forgetLongTermMemories(options) {
521
+ return this.request("POST", "/v1/long-term-memory/forget", {
522
+ params: {
523
+ namespace: options.namespace,
524
+ user_id: options.userId,
525
+ session_id: options.sessionId,
526
+ limit: options.limit,
527
+ dry_run: options.dryRun
528
+ },
529
+ body: {
530
+ policy: options.policy,
531
+ pinned_ids: options.pinnedIds
532
+ }
533
+ });
534
+ }
535
+ // ==================== Summary Views ====================
536
+ /**
537
+ * List all summary views
538
+ */
539
+ async listSummaryViews() {
540
+ return this.request("GET", "/v1/summary-views");
541
+ }
542
+ /**
543
+ * Create a new summary view
544
+ */
545
+ async createSummaryView(request) {
546
+ return this.request("POST", "/v1/summary-views", {
547
+ body: request
548
+ });
549
+ }
550
+ /**
551
+ * Get a summary view by ID
552
+ */
553
+ async getSummaryView(viewId) {
554
+ try {
555
+ return await this.request(
556
+ "GET",
557
+ `/v1/summary-views/${encodeURIComponent(viewId)}`
558
+ );
559
+ } catch (error) {
560
+ if (error instanceof MemoryNotFoundError) {
561
+ return null;
562
+ }
563
+ throw error;
564
+ }
565
+ }
566
+ /**
567
+ * Delete a summary view
568
+ */
569
+ async deleteSummaryView(viewId) {
570
+ return this.request(
571
+ "DELETE",
572
+ `/v1/summary-views/${encodeURIComponent(viewId)}`
573
+ );
574
+ }
575
+ /**
576
+ * Run a summary view partition
577
+ */
578
+ async runSummaryViewPartition(viewId, group) {
579
+ return this.request(
580
+ "POST",
581
+ `/v1/summary-views/${encodeURIComponent(viewId)}/partitions/run`,
582
+ { body: { group } }
583
+ );
584
+ }
585
+ /**
586
+ * List summary view partitions
587
+ */
588
+ async listSummaryViewPartitions(viewId, options = {}) {
589
+ return this.request(
590
+ "GET",
591
+ `/v1/summary-views/${encodeURIComponent(viewId)}/partitions`,
592
+ {
593
+ params: {
594
+ namespace: options.namespace,
595
+ user_id: options.userId,
596
+ session_id: options.sessionId,
597
+ memory_type: options.memoryType
598
+ }
599
+ }
600
+ );
601
+ }
602
+ /**
603
+ * Run a full summary view (async task)
604
+ */
605
+ async runSummaryView(viewId, options = {}) {
606
+ return this.request(
607
+ "POST",
608
+ `/v1/summary-views/${encodeURIComponent(viewId)}/run`,
609
+ { body: options }
610
+ );
611
+ }
612
+ // ==================== Tasks ====================
613
+ /**
614
+ * Get a task by ID
615
+ */
616
+ async getTask(taskId) {
617
+ try {
618
+ return await this.request(
619
+ "GET",
620
+ `/v1/tasks/${encodeURIComponent(taskId)}`
621
+ );
622
+ } catch (error) {
623
+ if (error instanceof MemoryNotFoundError) {
624
+ return null;
625
+ }
626
+ throw error;
627
+ }
628
+ }
629
+ };
630
+
631
+ exports.CreatedAt = CreatedAt;
632
+ exports.Entities = Entities;
633
+ exports.EventDate = EventDate;
634
+ exports.LastAccessed = LastAccessed;
635
+ exports.MemoryAPIClient = MemoryAPIClient;
636
+ exports.MemoryClientError = MemoryClientError;
637
+ exports.MemoryNotFoundError = MemoryNotFoundError;
638
+ exports.MemoryServerError = MemoryServerError;
639
+ exports.MemoryType = MemoryType;
640
+ exports.MemoryValidationError = MemoryValidationError;
641
+ exports.Namespace = Namespace;
642
+ exports.SessionId = SessionId;
643
+ exports.Topics = Topics;
644
+ exports.UserId = UserId;
645
+ //# sourceMappingURL=index.js.map
646
+ //# sourceMappingURL=index.js.map