@slashml/memory 1.0.12 → 1.0.14
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 +29 -1
- package/bin/cli.js +362 -76
- package/dist/index.d.ts +188 -6
- package/dist/index.js +439 -76
- package/dist/index.mjs +1 -0
- package/package.json +2 -2
- package/src/index.ts +727 -89
package/dist/index.js
CHANGED
|
@@ -5,17 +5,40 @@
|
|
|
5
5
|
* 100% drop-in replacement for `mem0ai` (`MemoryClient`, `AsyncMemoryClient`, `Memory`, `AsyncMemory`).
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.AsyncSlashMLClient = exports.AsyncSlashMLMemory = exports.SlashMLClient = exports.SlashMLMemory = exports.AsyncMemory = exports.AsyncMemoryClient = exports.Memory = exports.MemoryClient = exports.ProjectManager = exports.SlashMLAPIError = exports.DEFAULT_TIMEOUT_MS = exports.DEFAULT_MODEL = exports.DEFAULT_BASE_URL = void 0;
|
|
8
|
+
exports.AsyncSlashMLClient = exports.AsyncSlashMLMemory = exports.SlashMLClient = exports.SlashMLMemory = exports.AsyncMemory = exports.AsyncMemoryClient = exports.Memory = exports.MemoryClient = exports.AgentsManager = exports.ProjectManager = exports.SlashMLAPIError = exports.DEFAULT_TIMEOUT_MS = exports.DEFAULT_MODEL = exports.DEFAULT_BASE_URL = void 0;
|
|
9
9
|
exports.DEFAULT_BASE_URL = "https://api.slashml.ai";
|
|
10
10
|
exports.DEFAULT_MODEL = "slashml-1.5-fast";
|
|
11
11
|
exports.DEFAULT_TIMEOUT_MS = 30_000;
|
|
12
|
+
function normalizeRedactPolicy(value) {
|
|
13
|
+
if (value === undefined || value === null) {
|
|
14
|
+
return null;
|
|
15
|
+
}
|
|
16
|
+
if (Array.isArray(value)) {
|
|
17
|
+
const cleaned = value.map((v) => String(v).trim()).filter(Boolean);
|
|
18
|
+
return cleaned.length > 0 ? cleaned : null;
|
|
19
|
+
}
|
|
20
|
+
const raw = String(value).trim();
|
|
21
|
+
if (!raw) {
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
if (raw.includes(",")) {
|
|
25
|
+
const parts = raw
|
|
26
|
+
.split(",")
|
|
27
|
+
.map((p) => p.trim())
|
|
28
|
+
.filter(Boolean);
|
|
29
|
+
return parts.length > 0 ? parts : null;
|
|
30
|
+
}
|
|
31
|
+
return raw;
|
|
32
|
+
}
|
|
12
33
|
class SlashMLAPIError extends Error {
|
|
13
34
|
statusCode;
|
|
35
|
+
status;
|
|
14
36
|
details;
|
|
15
37
|
constructor(statusCode, message, details = {}) {
|
|
16
38
|
super(`SlashMLAPIError (${statusCode}): ${message}`);
|
|
17
39
|
this.name = "SlashMLAPIError";
|
|
18
40
|
this.statusCode = statusCode;
|
|
41
|
+
this.status = statusCode;
|
|
19
42
|
this.details = details;
|
|
20
43
|
}
|
|
21
44
|
}
|
|
@@ -27,6 +50,12 @@ function getEnv(name) {
|
|
|
27
50
|
}
|
|
28
51
|
return undefined;
|
|
29
52
|
}
|
|
53
|
+
function wrapResults(res) {
|
|
54
|
+
if (Array.isArray(res)) {
|
|
55
|
+
return { results: res };
|
|
56
|
+
}
|
|
57
|
+
return res ?? {};
|
|
58
|
+
}
|
|
30
59
|
class ProjectManager {
|
|
31
60
|
client;
|
|
32
61
|
constructor(client) {
|
|
@@ -123,14 +152,68 @@ class ProjectManager {
|
|
|
123
152
|
}
|
|
124
153
|
}
|
|
125
154
|
exports.ProjectManager = ProjectManager;
|
|
155
|
+
class AgentsManager {
|
|
156
|
+
client;
|
|
157
|
+
constructor(client) {
|
|
158
|
+
this.client = client;
|
|
159
|
+
}
|
|
160
|
+
async list() {
|
|
161
|
+
const res = await this.client.request("GET", "/v1/agents/");
|
|
162
|
+
return Array.isArray(res) ? { agents: res } : (res ?? {});
|
|
163
|
+
}
|
|
164
|
+
async configure(agentIdOrOptions, options = {}) {
|
|
165
|
+
const isObj = typeof agentIdOrOptions === "object" && agentIdOrOptions !== null;
|
|
166
|
+
const opts = isObj ? agentIdOrOptions : options;
|
|
167
|
+
const resolvedAgentId = isObj
|
|
168
|
+
? (opts.agent_id ?? opts.agentId ?? "")
|
|
169
|
+
: agentIdOrOptions;
|
|
170
|
+
const payload = {
|
|
171
|
+
agent_id: String(resolvedAgentId).trim(),
|
|
172
|
+
};
|
|
173
|
+
if (opts.name !== undefined)
|
|
174
|
+
payload.name = opts.name;
|
|
175
|
+
const accessMode = opts.access_mode ?? opts.accessMode;
|
|
176
|
+
if (accessMode !== undefined)
|
|
177
|
+
payload.access_mode = accessMode;
|
|
178
|
+
const allowWrite = opts.allow_write ?? opts.allowWrite;
|
|
179
|
+
if (allowWrite !== undefined)
|
|
180
|
+
payload.allow_write = Boolean(allowWrite);
|
|
181
|
+
const redactPolicy = opts.redact_policy ?? opts.redactPolicy;
|
|
182
|
+
if (redactPolicy !== undefined) {
|
|
183
|
+
payload.redact_policy = normalizeRedactPolicy(redactPolicy);
|
|
184
|
+
}
|
|
185
|
+
const redactScope = opts.redact_scope ?? opts.redactScope;
|
|
186
|
+
if (redactScope !== undefined)
|
|
187
|
+
payload.redact_scope = redactScope;
|
|
188
|
+
if (opts.inherit_policy ?? opts.inheritPolicy)
|
|
189
|
+
payload.inherit_policy = true;
|
|
190
|
+
if (opts.inherit_access ?? opts.inheritAccess)
|
|
191
|
+
payload.inherit_access = true;
|
|
192
|
+
if (opts.metadata !== undefined)
|
|
193
|
+
payload.metadata = opts.metadata;
|
|
194
|
+
return this.client.request("POST", "/v1/agents/", { jsonBody: payload });
|
|
195
|
+
}
|
|
196
|
+
async upsert(agentIdOrOptions, options = {}) {
|
|
197
|
+
return this.configure(agentIdOrOptions, options);
|
|
198
|
+
}
|
|
199
|
+
async delete(agentId) {
|
|
200
|
+
return this.client.request("DELETE", `/v1/agents/${encodeURIComponent(String(agentId).trim())}/`);
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
exports.AgentsManager = AgentsManager;
|
|
126
204
|
class MemoryClient {
|
|
127
205
|
apiKey;
|
|
128
206
|
baseUrl;
|
|
129
207
|
orgId;
|
|
130
208
|
projectId;
|
|
209
|
+
agentId;
|
|
210
|
+
callerAgentId;
|
|
211
|
+
redactPolicy;
|
|
212
|
+
redactScope;
|
|
131
213
|
model;
|
|
132
214
|
timeoutMs;
|
|
133
215
|
project;
|
|
216
|
+
agents;
|
|
134
217
|
fetchImpl;
|
|
135
218
|
constructor(options = {}) {
|
|
136
219
|
const resolvedKey = options.apiKey ||
|
|
@@ -148,14 +231,57 @@ class MemoryClient {
|
|
|
148
231
|
const resolvedModel = options.model ||
|
|
149
232
|
getEnv("SLASHML_MODEL") ||
|
|
150
233
|
exports.DEFAULT_MODEL;
|
|
234
|
+
const rawAgentId = options.agentId ??
|
|
235
|
+
options.agent_id ??
|
|
236
|
+
options.callerAgentId ??
|
|
237
|
+
options.caller_agent_id ??
|
|
238
|
+
getEnv("SLASHML_AGENT_ID") ??
|
|
239
|
+
null;
|
|
240
|
+
const rawRedactPolicy = options.redactPolicy ??
|
|
241
|
+
options.redact_policy ??
|
|
242
|
+
getEnv("SLASHML_REDACT_POLICY") ??
|
|
243
|
+
null;
|
|
244
|
+
const rawRedactScope = options.redactScope ??
|
|
245
|
+
options.redact_scope ??
|
|
246
|
+
getEnv("SLASHML_REDACT_SCOPE") ??
|
|
247
|
+
null;
|
|
151
248
|
this.apiKey = resolvedKey;
|
|
152
249
|
this.baseUrl = resolvedUrl.replace(/\/+$/, "");
|
|
153
250
|
this.orgId = options.orgId || options.org_id || getEnv("SLASHML_ORG_ID") || null;
|
|
154
251
|
this.projectId = options.projectId || options.project_id || getEnv("SLASHML_PROJECT_ID") || null;
|
|
252
|
+
this.agentId = rawAgentId && String(rawAgentId).trim() ? String(rawAgentId).trim() : null;
|
|
253
|
+
this.callerAgentId = this.agentId;
|
|
254
|
+
this.redactPolicy = normalizeRedactPolicy(rawRedactPolicy);
|
|
255
|
+
this.redactScope =
|
|
256
|
+
rawRedactScope && String(rawRedactScope).trim()
|
|
257
|
+
? String(rawRedactScope).trim()
|
|
258
|
+
: null;
|
|
155
259
|
this.model = resolvedModel;
|
|
156
260
|
this.timeoutMs = options.timeoutMs ?? exports.DEFAULT_TIMEOUT_MS;
|
|
157
261
|
this.fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
158
262
|
this.project = new ProjectManager(this);
|
|
263
|
+
this.agents = new AgentsManager(this);
|
|
264
|
+
}
|
|
265
|
+
applyLifecycleDefaults(payload, opts = {}) {
|
|
266
|
+
const effPolicy = opts.redactPolicy !== undefined
|
|
267
|
+
? normalizeRedactPolicy(opts.redactPolicy)
|
|
268
|
+
: this.redactPolicy;
|
|
269
|
+
const effScope = opts.redactScope ?? this.redactScope;
|
|
270
|
+
const effCaller = opts.callerAgentId !== undefined && opts.callerAgentId !== null
|
|
271
|
+
? String(opts.callerAgentId).trim()
|
|
272
|
+
: this.agentId;
|
|
273
|
+
if (payload) {
|
|
274
|
+
if (effPolicy !== null && payload.redact_policy === undefined) {
|
|
275
|
+
payload.redact_policy = effPolicy;
|
|
276
|
+
}
|
|
277
|
+
if (effScope && payload.redact_scope === undefined) {
|
|
278
|
+
payload.redact_scope = effScope;
|
|
279
|
+
}
|
|
280
|
+
if (effCaller && payload.caller_agent_id === undefined) {
|
|
281
|
+
payload.caller_agent_id = effCaller;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
return effCaller ? { "X-SlashML-Agent-Id": effCaller } : undefined;
|
|
159
285
|
}
|
|
160
286
|
async request(method, path, options = {}) {
|
|
161
287
|
if (!this.apiKey) {
|
|
@@ -177,7 +303,7 @@ class MemoryClient {
|
|
|
177
303
|
const headers = {
|
|
178
304
|
Accept: "application/json",
|
|
179
305
|
"Content-Type": "application/json",
|
|
180
|
-
"User-Agent": "slashml-memory-ts/1.0.
|
|
306
|
+
"User-Agent": "slashml-memory-ts/1.0.14",
|
|
181
307
|
};
|
|
182
308
|
if (this.apiKey) {
|
|
183
309
|
headers.Authorization = `Bearer ${this.apiKey}`;
|
|
@@ -189,6 +315,15 @@ class MemoryClient {
|
|
|
189
315
|
if (this.projectId) {
|
|
190
316
|
headers["X-SlashML-Project-Id"] = this.projectId;
|
|
191
317
|
}
|
|
318
|
+
if (this.agentId) {
|
|
319
|
+
headers["X-SlashML-Agent-Id"] = this.agentId;
|
|
320
|
+
}
|
|
321
|
+
if (options.headers) {
|
|
322
|
+
for (const [hk, hv] of Object.entries(options.headers)) {
|
|
323
|
+
if (hv)
|
|
324
|
+
headers[hk] = hv;
|
|
325
|
+
}
|
|
326
|
+
}
|
|
192
327
|
const controller = new AbortController();
|
|
193
328
|
const timer = setTimeout(() => controller.abort(), this.timeoutMs);
|
|
194
329
|
try {
|
|
@@ -234,32 +369,192 @@ class MemoryClient {
|
|
|
234
369
|
// ---------------------------------------------------------------------------
|
|
235
370
|
async ingest(documents, options = {}) {
|
|
236
371
|
const docs = typeof documents === "string" ? [documents] : Array.from(documents);
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
372
|
+
const payload = {
|
|
373
|
+
namespace: options.namespace ?? "default",
|
|
374
|
+
model: options.model ?? this.model,
|
|
375
|
+
documents: docs,
|
|
376
|
+
metadata: options.metadata ?? {},
|
|
377
|
+
};
|
|
378
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
379
|
+
redactPolicy: options.redact_policy ?? options.redactPolicy,
|
|
380
|
+
redactScope: options.redact_scope ?? options.redactScope,
|
|
381
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
244
382
|
});
|
|
383
|
+
return this.request("POST", "/v1/memory/ingest", { jsonBody: payload, headers });
|
|
245
384
|
}
|
|
246
385
|
async query(prompt, options = {}) {
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
386
|
+
const payload = {
|
|
387
|
+
namespace: options.namespace ?? "default",
|
|
388
|
+
model: options.model ?? this.model,
|
|
389
|
+
prompt,
|
|
390
|
+
top_k: options.top_k ?? 5,
|
|
391
|
+
};
|
|
392
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
393
|
+
redactPolicy: options.redact_policy ?? options.redactPolicy,
|
|
394
|
+
redactScope: options.redact_scope ?? options.redactScope,
|
|
395
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
254
396
|
});
|
|
397
|
+
return this.request("POST", "/v1/memory/query", { jsonBody: payload, headers });
|
|
255
398
|
}
|
|
256
399
|
async forget(target, options = {}) {
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
}
|
|
400
|
+
if (options.purge) {
|
|
401
|
+
return this.purge(target, {
|
|
402
|
+
reason: options.reason,
|
|
403
|
+
caller_agent_id: options.caller_agent_id ?? options.callerAgentId,
|
|
404
|
+
});
|
|
405
|
+
}
|
|
406
|
+
const payload = {
|
|
407
|
+
namespace: options.namespace ?? "default",
|
|
408
|
+
target,
|
|
409
|
+
};
|
|
410
|
+
if (options.reason !== undefined) {
|
|
411
|
+
payload.reason = options.reason;
|
|
412
|
+
}
|
|
413
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
414
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
415
|
+
});
|
|
416
|
+
return this.request("POST", "/v1/memory/forget", { jsonBody: payload, headers });
|
|
417
|
+
}
|
|
418
|
+
async redact(textOrOptions, options = {}) {
|
|
419
|
+
const opts = typeof textOrOptions === "string"
|
|
420
|
+
? { ...options, text: options.text ?? textOrOptions }
|
|
421
|
+
: { ...textOrOptions, ...options };
|
|
422
|
+
const payload = {};
|
|
423
|
+
if (opts.text !== undefined) {
|
|
424
|
+
payload.text = opts.text;
|
|
425
|
+
}
|
|
426
|
+
const memId = opts.memory_id ?? opts.memoryId;
|
|
427
|
+
if (memId) {
|
|
428
|
+
payload.memory_id = memId;
|
|
429
|
+
}
|
|
430
|
+
const explicitSpans = opts.explicit_spans ?? opts.explicitSpans;
|
|
431
|
+
if (explicitSpans !== undefined) {
|
|
432
|
+
payload.explicit_spans = explicitSpans;
|
|
433
|
+
}
|
|
434
|
+
const dryRun = opts.dry_run ?? opts.dryRun;
|
|
435
|
+
if (dryRun !== undefined) {
|
|
436
|
+
payload.dry_run = Boolean(dryRun);
|
|
437
|
+
}
|
|
438
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
439
|
+
redactPolicy: opts.redact_policy ?? opts.redactPolicy,
|
|
440
|
+
callerAgentId: opts.caller_agent_id ?? opts.callerAgentId,
|
|
441
|
+
});
|
|
442
|
+
return this.request("POST", "/v1/memory/redact", { jsonBody: payload, headers });
|
|
443
|
+
}
|
|
444
|
+
async purge(memoryIds, options = {}) {
|
|
445
|
+
const ids = typeof memoryIds === "string" ? [memoryIds] : Array.from(memoryIds);
|
|
446
|
+
const payload = {
|
|
447
|
+
memory_ids: ids,
|
|
448
|
+
};
|
|
449
|
+
const dryRun = options.dry_run ?? options.dryRun;
|
|
450
|
+
if (dryRun !== undefined) {
|
|
451
|
+
payload.dry_run = Boolean(dryRun);
|
|
452
|
+
}
|
|
453
|
+
if (options.reason !== undefined) {
|
|
454
|
+
payload.reason = options.reason;
|
|
455
|
+
}
|
|
456
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
457
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
458
|
+
});
|
|
459
|
+
return this.request("POST", "/v1/memory/purge", { jsonBody: payload, headers });
|
|
460
|
+
}
|
|
461
|
+
async undo(undoToken, options = {}) {
|
|
462
|
+
const payload = {
|
|
463
|
+
undo_token: undoToken,
|
|
464
|
+
};
|
|
465
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
466
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
262
467
|
});
|
|
468
|
+
return this.request("POST", "/v1/memory/undo", { jsonBody: payload, headers });
|
|
469
|
+
}
|
|
470
|
+
async getRedactionPolicy() {
|
|
471
|
+
return this.request("GET", "/v1/memory/redact/policy");
|
|
472
|
+
}
|
|
473
|
+
async get_redaction_policy() {
|
|
474
|
+
return this.getRedactionPolicy();
|
|
475
|
+
}
|
|
476
|
+
async updateRedactionPolicy(options = {}) {
|
|
477
|
+
const payload = {};
|
|
478
|
+
const redactPolicy = options.redact_policy ?? options.redactPolicy;
|
|
479
|
+
if (redactPolicy !== undefined) {
|
|
480
|
+
payload.redact_policy = normalizeRedactPolicy(redactPolicy);
|
|
481
|
+
}
|
|
482
|
+
const redactScope = options.redact_scope ?? options.redactScope;
|
|
483
|
+
if (redactScope !== undefined) {
|
|
484
|
+
payload.redact_scope = redactScope;
|
|
485
|
+
}
|
|
486
|
+
const accessMode = options.access_mode ?? options.accessMode;
|
|
487
|
+
if (accessMode !== undefined) {
|
|
488
|
+
payload.access_mode = accessMode;
|
|
489
|
+
}
|
|
490
|
+
const allowWrite = options.allow_write ?? options.allowWrite;
|
|
491
|
+
if (allowWrite !== undefined) {
|
|
492
|
+
payload.allow_write = Boolean(allowWrite);
|
|
493
|
+
}
|
|
494
|
+
return this.request("PUT", "/v1/memory/redact/policy", { jsonBody: payload });
|
|
495
|
+
}
|
|
496
|
+
async update_redaction_policy(options = {}) {
|
|
497
|
+
return this.updateRedactionPolicy(options);
|
|
498
|
+
}
|
|
499
|
+
async previewRedaction(textOrOptions, options = {}) {
|
|
500
|
+
const opts = typeof textOrOptions === "string"
|
|
501
|
+
? { ...options, text: options.text ?? textOrOptions }
|
|
502
|
+
: { ...textOrOptions, ...options };
|
|
503
|
+
const payload = {
|
|
504
|
+
text: opts.text ?? "",
|
|
505
|
+
};
|
|
506
|
+
const agentId = opts.agent_id ?? opts.agentId;
|
|
507
|
+
if (agentId) {
|
|
508
|
+
payload.agent_id = agentId;
|
|
509
|
+
}
|
|
510
|
+
const redactPolicy = opts.redact_policy ?? opts.redactPolicy;
|
|
511
|
+
if (redactPolicy !== undefined) {
|
|
512
|
+
payload.redact_policy = normalizeRedactPolicy(redactPolicy);
|
|
513
|
+
}
|
|
514
|
+
const redactScope = opts.redact_scope ?? opts.redactScope;
|
|
515
|
+
if (redactScope !== undefined) {
|
|
516
|
+
payload.redact_scope = redactScope;
|
|
517
|
+
}
|
|
518
|
+
return this.request("POST", "/v1/memory/redact/preview", { jsonBody: payload });
|
|
519
|
+
}
|
|
520
|
+
async preview_redaction(textOrOptions, options = {}) {
|
|
521
|
+
return this.previewRedaction(textOrOptions, options);
|
|
522
|
+
}
|
|
523
|
+
async listAgents() {
|
|
524
|
+
return this.agents.list();
|
|
525
|
+
}
|
|
526
|
+
async listAgentPolicies() {
|
|
527
|
+
return this.agents.list();
|
|
528
|
+
}
|
|
529
|
+
async list_agents() {
|
|
530
|
+
return this.agents.list();
|
|
531
|
+
}
|
|
532
|
+
async list_agent_policies() {
|
|
533
|
+
return this.agents.list();
|
|
534
|
+
}
|
|
535
|
+
async configureAgent(agentIdOrOptions, options = {}) {
|
|
536
|
+
return this.agents.configure(agentIdOrOptions, options);
|
|
537
|
+
}
|
|
538
|
+
async upsertAgentPolicy(agentIdOrOptions, options = {}) {
|
|
539
|
+
return this.agents.configure(agentIdOrOptions, options);
|
|
540
|
+
}
|
|
541
|
+
async configure_agent(agentIdOrOptions, options = {}) {
|
|
542
|
+
return this.agents.configure(agentIdOrOptions, options);
|
|
543
|
+
}
|
|
544
|
+
async upsert_agent_policy(agentIdOrOptions, options = {}) {
|
|
545
|
+
return this.agents.configure(agentIdOrOptions, options);
|
|
546
|
+
}
|
|
547
|
+
async deleteAgent(agentId) {
|
|
548
|
+
return this.agents.delete(agentId);
|
|
549
|
+
}
|
|
550
|
+
async deleteAgentPolicy(agentId) {
|
|
551
|
+
return this.agents.delete(agentId);
|
|
552
|
+
}
|
|
553
|
+
async delete_agent(agentId) {
|
|
554
|
+
return this.agents.delete(agentId);
|
|
555
|
+
}
|
|
556
|
+
async delete_agent_policy(agentId) {
|
|
557
|
+
return this.agents.delete(agentId);
|
|
263
558
|
}
|
|
264
559
|
async intervene(textOrPayload, options = {}) {
|
|
265
560
|
const opts = typeof textOrPayload === "string"
|
|
@@ -272,28 +567,32 @@ class MemoryClient {
|
|
|
272
567
|
const lastAssistantMsg = opts.last_assistant_message ?? opts.lastAssistantMessage ?? undefined;
|
|
273
568
|
const stopHookActive = opts.stop_hook_active ?? opts.stopHookActive ?? undefined;
|
|
274
569
|
const whisperedIds = opts.whispered_memory_ids ?? opts.whisperedMemoryIds ?? undefined;
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
570
|
+
const payload = {
|
|
571
|
+
hook_event_name: opts.hook_event_name ?? opts.hookEventName ?? "UserPromptSubmit",
|
|
572
|
+
prompt: opts.prompt ?? resolvedText,
|
|
573
|
+
text: resolvedText,
|
|
574
|
+
...(lastAssistantMsg !== undefined ? { last_assistant_message: lastAssistantMsg } : {}),
|
|
575
|
+
...(stopHookActive !== undefined ? { stop_hook_active: Boolean(stopHookActive) } : {}),
|
|
576
|
+
...(whisperedIds && whisperedIds.length > 0 ? { whispered_memory_ids: whisperedIds } : {}),
|
|
577
|
+
tool_name: opts.tool_name ?? opts.toolName ?? null,
|
|
578
|
+
tool_input: opts.tool_input ?? opts.toolInput ?? null,
|
|
579
|
+
tool_response: opts.tool_response ?? opts.toolResponse ?? null,
|
|
580
|
+
user_id: opts.user_id ?? opts.userId ?? "default",
|
|
581
|
+
...(resolvedProjectId ? { project_id: resolvedProjectId } : {}),
|
|
582
|
+
...(resolvedSessionId ? { session_id: resolvedSessionId } : {}),
|
|
583
|
+
model: resolvedModel,
|
|
584
|
+
commit_writes: opts.commit_writes ?? opts.commitWrites ?? true,
|
|
585
|
+
top_k: opts.top_k ?? opts.topK ?? 5,
|
|
586
|
+
client_raw_tokens_delta: opts.client_raw_tokens_delta ?? opts.clientRawTokensDelta ?? 0,
|
|
587
|
+
client_dropped_tokens_delta: opts.client_dropped_tokens_delta ?? opts.clientDroppedTokensDelta ?? 0,
|
|
588
|
+
client_dropped_events_delta: opts.client_dropped_events_delta ?? opts.clientDroppedEventsDelta ?? 0,
|
|
589
|
+
};
|
|
590
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
591
|
+
redactPolicy: opts.redact_policy ?? opts.redactPolicy,
|
|
592
|
+
redactScope: opts.redact_scope ?? opts.redactScope,
|
|
593
|
+
callerAgentId: opts.caller_agent_id ?? opts.callerAgentId,
|
|
296
594
|
});
|
|
595
|
+
return this.request("POST", "/v1/memory/intervene", { jsonBody: payload, headers });
|
|
297
596
|
}
|
|
298
597
|
async status(options = {}) {
|
|
299
598
|
const uid = options.user_id ?? options.userId ?? "default";
|
|
@@ -339,48 +638,76 @@ class MemoryClient {
|
|
|
339
638
|
payload.memory_id = memId;
|
|
340
639
|
payload.id = memId;
|
|
341
640
|
}
|
|
342
|
-
|
|
641
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
642
|
+
redactPolicy: options.redact_policy ?? options.redactPolicy,
|
|
643
|
+
redactScope: options.redact_scope ?? options.redactScope,
|
|
644
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
645
|
+
});
|
|
646
|
+
return wrapResults(await this.request("POST", "/v1/memories", { jsonBody: payload, headers }));
|
|
343
647
|
}
|
|
344
648
|
async search(queryOrOptions, options = {}) {
|
|
345
649
|
const isObj = typeof queryOrOptions === "object" && queryOrOptions !== null;
|
|
346
650
|
const query = isObj ? queryOrOptions.query : queryOrOptions;
|
|
347
651
|
const opts = isObj ? queryOrOptions : options;
|
|
348
652
|
const effectiveLimit = opts.top_k ?? opts.topK ?? opts.limit ?? 5;
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
653
|
+
const payload = {
|
|
654
|
+
query,
|
|
655
|
+
user_id: opts.user_id ?? opts.userId ?? null,
|
|
656
|
+
agent_id: opts.agent_id ?? opts.agentId ?? null,
|
|
657
|
+
app_id: opts.app_id ?? opts.appId ?? null,
|
|
658
|
+
run_id: opts.run_id ?? opts.runId ?? null,
|
|
659
|
+
limit: effectiveLimit,
|
|
660
|
+
top_k: effectiveLimit,
|
|
661
|
+
filters: opts.filters ?? opts.metadata ?? null,
|
|
662
|
+
threshold: opts.threshold ?? null,
|
|
663
|
+
model: this.model,
|
|
664
|
+
};
|
|
665
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
666
|
+
redactPolicy: opts.redact_policy ?? opts.redactPolicy,
|
|
667
|
+
redactScope: opts.redact_scope ?? opts.redactScope,
|
|
668
|
+
callerAgentId: opts.caller_agent_id ?? opts.callerAgentId,
|
|
362
669
|
});
|
|
670
|
+
return wrapResults(await this.request("POST", "/v1/memories/search", { jsonBody: payload, headers }));
|
|
363
671
|
}
|
|
364
672
|
async recall(queryOrOptions, options = {}) {
|
|
365
673
|
return this.search(queryOrOptions, options);
|
|
366
674
|
}
|
|
367
675
|
async getAll(options = {}) {
|
|
368
|
-
|
|
676
|
+
const effPolicy = normalizeRedactPolicy(options.redact_policy ?? options.redactPolicy ?? this.redactPolicy);
|
|
677
|
+
const effScope = options.redact_scope ?? options.redactScope ?? this.redactScope;
|
|
678
|
+
const headers = this.applyLifecycleDefaults(undefined, {
|
|
679
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
680
|
+
});
|
|
681
|
+
return wrapResults(await this.request("GET", "/v1/memories", {
|
|
369
682
|
params: {
|
|
370
683
|
user_id: options.user_id ?? options.userId ?? undefined,
|
|
371
684
|
agent_id: options.agent_id ?? options.agentId ?? undefined,
|
|
372
685
|
app_id: options.app_id ?? options.appId ?? undefined,
|
|
373
686
|
run_id: options.run_id ?? options.runId ?? undefined,
|
|
374
687
|
limit: options.limit ?? 100,
|
|
688
|
+
redact_policy: Array.isArray(effPolicy) ? effPolicy.join(",") : (effPolicy ?? undefined),
|
|
689
|
+
redact_scope: effScope ?? undefined,
|
|
375
690
|
},
|
|
376
|
-
|
|
691
|
+
headers,
|
|
692
|
+
}));
|
|
377
693
|
}
|
|
378
694
|
async get_all(options = {}) {
|
|
379
695
|
return this.getAll(options);
|
|
380
696
|
}
|
|
381
|
-
async get(memoryId) {
|
|
697
|
+
async get(memoryId, options = {}) {
|
|
698
|
+
const effPolicy = normalizeRedactPolicy(options.redact_policy ?? options.redactPolicy ?? this.redactPolicy);
|
|
699
|
+
const effScope = options.redact_scope ?? options.redactScope ?? this.redactScope;
|
|
700
|
+
const headers = this.applyLifecycleDefaults(undefined, {
|
|
701
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
702
|
+
});
|
|
382
703
|
try {
|
|
383
|
-
return await this.request("GET", `/v1/memories/${encodeURIComponent(memoryId)}
|
|
704
|
+
return await this.request("GET", `/v1/memories/${encodeURIComponent(memoryId)}`, {
|
|
705
|
+
params: {
|
|
706
|
+
redact_policy: Array.isArray(effPolicy) ? effPolicy.join(",") : (effPolicy ?? undefined),
|
|
707
|
+
redact_scope: effScope ?? undefined,
|
|
708
|
+
},
|
|
709
|
+
headers,
|
|
710
|
+
});
|
|
384
711
|
}
|
|
385
712
|
catch (err) {
|
|
386
713
|
if (err instanceof SlashMLAPIError && err.statusCode === 404) {
|
|
@@ -389,18 +716,37 @@ class MemoryClient {
|
|
|
389
716
|
throw err;
|
|
390
717
|
}
|
|
391
718
|
}
|
|
392
|
-
async update(memoryId, data) {
|
|
393
|
-
const
|
|
719
|
+
async update(memoryId, data, options = {}) {
|
|
720
|
+
const isStr = typeof data === "string";
|
|
721
|
+
const body = isStr
|
|
394
722
|
? { text: data }
|
|
395
723
|
: { text: data.text ?? "", metadata: data.metadata };
|
|
724
|
+
const headers = this.applyLifecycleDefaults(body, {
|
|
725
|
+
redactPolicy: options.redact_policy ??
|
|
726
|
+
options.redactPolicy ??
|
|
727
|
+
(!isStr ? (data.redact_policy ?? data.redactPolicy) : undefined),
|
|
728
|
+
redactScope: options.redact_scope ??
|
|
729
|
+
options.redactScope ??
|
|
730
|
+
(!isStr ? (data.redact_scope ?? data.redactScope) : undefined),
|
|
731
|
+
callerAgentId: options.caller_agent_id ??
|
|
732
|
+
options.callerAgentId ??
|
|
733
|
+
(!isStr ? (data.caller_agent_id ?? data.callerAgentId) : undefined),
|
|
734
|
+
});
|
|
396
735
|
return this.request("PUT", `/v1/memories/${encodeURIComponent(memoryId)}`, {
|
|
397
736
|
jsonBody: body,
|
|
737
|
+
headers,
|
|
398
738
|
});
|
|
399
739
|
}
|
|
400
|
-
async delete(memoryId) {
|
|
401
|
-
|
|
740
|
+
async delete(memoryId, options = {}) {
|
|
741
|
+
const headers = this.applyLifecycleDefaults(undefined, {
|
|
742
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
743
|
+
});
|
|
744
|
+
return this.request("DELETE", `/v1/memories/${encodeURIComponent(memoryId)}`, { headers });
|
|
402
745
|
}
|
|
403
746
|
async deleteAll(options = {}) {
|
|
747
|
+
const headers = this.applyLifecycleDefaults(undefined, {
|
|
748
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
749
|
+
});
|
|
404
750
|
return this.request("DELETE", "/v1/memories", {
|
|
405
751
|
params: {
|
|
406
752
|
user_id: options.user_id ?? options.userId ?? undefined,
|
|
@@ -408,6 +754,7 @@ class MemoryClient {
|
|
|
408
754
|
app_id: options.app_id ?? options.appId ?? undefined,
|
|
409
755
|
run_id: options.run_id ?? options.runId ?? undefined,
|
|
410
756
|
},
|
|
757
|
+
headers,
|
|
411
758
|
});
|
|
412
759
|
}
|
|
413
760
|
async delete_all(options = {}) {
|
|
@@ -429,32 +776,42 @@ class MemoryClient {
|
|
|
429
776
|
}
|
|
430
777
|
return [];
|
|
431
778
|
}
|
|
432
|
-
async batchUpdate(memories) {
|
|
779
|
+
async batchUpdate(memories, options = {}) {
|
|
433
780
|
const normalized = memories.map((m) => ({
|
|
434
781
|
...m,
|
|
435
782
|
memory_id: m.memory_id ?? m.memoryId ?? m.id,
|
|
436
783
|
}));
|
|
437
|
-
|
|
784
|
+
const payload = { memories: normalized };
|
|
785
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
786
|
+
redactPolicy: options.redact_policy ?? options.redactPolicy,
|
|
787
|
+
redactScope: options.redact_scope ?? options.redactScope,
|
|
788
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
789
|
+
});
|
|
790
|
+
return this.request("PUT", "/v1/batch", { jsonBody: payload, headers });
|
|
438
791
|
}
|
|
439
|
-
async batch_update(memories) {
|
|
440
|
-
return this.batchUpdate(memories);
|
|
792
|
+
async batch_update(memories, options = {}) {
|
|
793
|
+
return this.batchUpdate(memories, options);
|
|
441
794
|
}
|
|
442
|
-
async batchDelete(memories) {
|
|
795
|
+
async batchDelete(memories, options = {}) {
|
|
443
796
|
const normalized = memories.map((m) => typeof m === "string"
|
|
444
797
|
? { memory_id: m, id: m }
|
|
445
798
|
: { ...m, memory_id: m.memory_id ?? m.memoryId ?? m.id });
|
|
446
|
-
|
|
799
|
+
const payload = { memories: normalized };
|
|
800
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
801
|
+
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
802
|
+
});
|
|
803
|
+
return this.request("DELETE", "/v1/batch", { jsonBody: payload, headers });
|
|
447
804
|
}
|
|
448
|
-
async batch_delete(memories) {
|
|
449
|
-
return this.batchDelete(memories);
|
|
805
|
+
async batch_delete(memories, options = {}) {
|
|
806
|
+
return this.batchDelete(memories, options);
|
|
450
807
|
}
|
|
451
808
|
async users() {
|
|
452
|
-
return this.request("GET", "/v1/entities", {
|
|
809
|
+
return wrapResults(await this.request("GET", "/v1/entities", {
|
|
453
810
|
params: {
|
|
454
811
|
org_id: this.orgId ?? undefined,
|
|
455
812
|
project_id: this.projectId ?? undefined,
|
|
456
813
|
},
|
|
457
|
-
});
|
|
814
|
+
}));
|
|
458
815
|
}
|
|
459
816
|
async deleteUsers(options = {}) {
|
|
460
817
|
return this.request("DELETE", "/v1/entities", {
|
|
@@ -498,9 +855,9 @@ class MemoryClient {
|
|
|
498
855
|
return this.project.update(settings);
|
|
499
856
|
}
|
|
500
857
|
async getWebhooks(projectId) {
|
|
501
|
-
return this.request("GET", "/v1/webhooks", {
|
|
858
|
+
return wrapResults(await this.request("GET", "/v1/webhooks", {
|
|
502
859
|
params: { project_id: projectId ?? this.projectId ?? undefined },
|
|
503
|
-
});
|
|
860
|
+
}));
|
|
504
861
|
}
|
|
505
862
|
async get_webhooks(projectId) {
|
|
506
863
|
return this.getWebhooks(projectId);
|
|
@@ -561,6 +918,12 @@ class Memory extends MemoryClient {
|
|
|
561
918
|
configDict.host,
|
|
562
919
|
orgId: configDict.org_id || configDict.orgId,
|
|
563
920
|
projectId: configDict.project_id || configDict.projectId,
|
|
921
|
+
agentId: configDict.agent_id ||
|
|
922
|
+
configDict.agentId ||
|
|
923
|
+
configDict.caller_agent_id ||
|
|
924
|
+
configDict.callerAgentId,
|
|
925
|
+
redactPolicy: configDict.redact_policy || configDict.redactPolicy,
|
|
926
|
+
redactScope: configDict.redact_scope || configDict.redactScope,
|
|
564
927
|
model: configDict.model,
|
|
565
928
|
timeoutMs: configDict.timeoutMs,
|
|
566
929
|
});
|