@slashml/memory 1.0.16 → 1.1.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 -1
- package/bin/cli.js +1149 -18
- package/dist/index.d.ts +92 -0
- package/dist/index.js +342 -4
- package/dist/index.mjs +3 -0
- package/package.json +3 -4
- package/src/index.ts +485 -3
package/dist/index.d.ts
CHANGED
|
@@ -22,6 +22,8 @@ export interface MemoryClientOptions {
|
|
|
22
22
|
org_id?: string;
|
|
23
23
|
projectId?: string;
|
|
24
24
|
project_id?: string;
|
|
25
|
+
runId?: string;
|
|
26
|
+
run_id?: string;
|
|
25
27
|
agentId?: string;
|
|
26
28
|
agent_id?: string;
|
|
27
29
|
callerAgentId?: string;
|
|
@@ -43,6 +45,10 @@ export interface MemoryFilters {
|
|
|
43
45
|
appId?: string | null;
|
|
44
46
|
run_id?: string | null;
|
|
45
47
|
runId?: string | null;
|
|
48
|
+
project_id?: string | null;
|
|
49
|
+
projectId?: string | null;
|
|
50
|
+
inherit_scopes?: boolean;
|
|
51
|
+
inheritScopes?: boolean;
|
|
46
52
|
metadata?: Record<string, unknown> | null;
|
|
47
53
|
filters?: Record<string, unknown> | null;
|
|
48
54
|
redact_policy?: RedactPolicy;
|
|
@@ -153,6 +159,60 @@ export interface ConfigureAgentOptions {
|
|
|
153
159
|
inheritAccess?: boolean;
|
|
154
160
|
metadata?: Record<string, unknown>;
|
|
155
161
|
}
|
|
162
|
+
export interface MintWorkerTokenOptions {
|
|
163
|
+
tier?: string;
|
|
164
|
+
run_id?: string | null;
|
|
165
|
+
runId?: string | null;
|
|
166
|
+
project_id?: string | null;
|
|
167
|
+
projectId?: string | null;
|
|
168
|
+
access_mode?: AccessMode;
|
|
169
|
+
accessMode?: AccessMode;
|
|
170
|
+
redact_policy?: RedactPolicy;
|
|
171
|
+
redactPolicy?: RedactPolicy;
|
|
172
|
+
redact_scope?: RedactScope;
|
|
173
|
+
redactScope?: RedactScope;
|
|
174
|
+
ttl_seconds?: number;
|
|
175
|
+
ttlSeconds?: number;
|
|
176
|
+
can_delegate?: boolean;
|
|
177
|
+
canDelegate?: boolean;
|
|
178
|
+
}
|
|
179
|
+
export interface PromoteRunOptions {
|
|
180
|
+
run_id?: string | null;
|
|
181
|
+
runId?: string | null;
|
|
182
|
+
target_project_id?: string | null;
|
|
183
|
+
targetProjectId?: string | null;
|
|
184
|
+
target_user_id?: string | null;
|
|
185
|
+
targetUserId?: string | null;
|
|
186
|
+
memory_ids?: string[] | null;
|
|
187
|
+
memoryIds?: string[] | null;
|
|
188
|
+
discard_unpromoted?: boolean;
|
|
189
|
+
discardUnpromoted?: boolean;
|
|
190
|
+
caller_agent_id?: string | null;
|
|
191
|
+
callerAgentId?: string | null;
|
|
192
|
+
}
|
|
193
|
+
export interface GuardToolsOptions {
|
|
194
|
+
client?: MemoryClient;
|
|
195
|
+
apiKey?: string;
|
|
196
|
+
api_key?: string;
|
|
197
|
+
baseUrl?: string;
|
|
198
|
+
base_url?: string;
|
|
199
|
+
userId?: string;
|
|
200
|
+
user_id?: string;
|
|
201
|
+
projectId?: string | null;
|
|
202
|
+
project_id?: string | null;
|
|
203
|
+
runId?: string | null;
|
|
204
|
+
run_id?: string | null;
|
|
205
|
+
callerAgentId?: string | null;
|
|
206
|
+
caller_agent_id?: string | null;
|
|
207
|
+
redactOutputs?: boolean;
|
|
208
|
+
redact_outputs?: boolean;
|
|
209
|
+
redactPolicy?: RedactPolicy;
|
|
210
|
+
redact_policy?: RedactPolicy;
|
|
211
|
+
onBlock?: "return_error" | "return_object" | "raise";
|
|
212
|
+
on_block?: "return_error" | "return_object" | "raise";
|
|
213
|
+
failMode?: "open" | "closed";
|
|
214
|
+
fail_mode?: "open" | "closed";
|
|
215
|
+
}
|
|
156
216
|
export interface InterveneOptions {
|
|
157
217
|
hook_event_name?: string;
|
|
158
218
|
hookEventName?: string;
|
|
@@ -174,8 +234,14 @@ export interface InterveneOptions {
|
|
|
174
234
|
userId?: string | null;
|
|
175
235
|
project_id?: string | null;
|
|
176
236
|
projectId?: string | null;
|
|
237
|
+
run_id?: string | null;
|
|
238
|
+
runId?: string | null;
|
|
177
239
|
session_id?: string | null;
|
|
178
240
|
sessionId?: string | null;
|
|
241
|
+
strict_pretool_block?: boolean;
|
|
242
|
+
strictPretoolBlock?: boolean;
|
|
243
|
+
inherit_scopes?: boolean;
|
|
244
|
+
inheritScopes?: boolean;
|
|
179
245
|
model?: string;
|
|
180
246
|
commit_writes?: boolean;
|
|
181
247
|
commitWrites?: boolean;
|
|
@@ -210,6 +276,12 @@ export declare class SlashMLAPIError extends Error {
|
|
|
210
276
|
readonly details: Record<string, unknown>;
|
|
211
277
|
constructor(statusCode: number, message: string, details?: Record<string, unknown>);
|
|
212
278
|
}
|
|
279
|
+
export declare class SlashMLToolBlockedError extends Error {
|
|
280
|
+
readonly toolName: string;
|
|
281
|
+
readonly ruleIds: string[];
|
|
282
|
+
readonly rawResponse: Record<string, unknown>;
|
|
283
|
+
constructor(toolName: string, message: string, ruleIds?: string[], rawResponse?: Record<string, unknown>);
|
|
284
|
+
}
|
|
213
285
|
export declare class ProjectManager {
|
|
214
286
|
private readonly client;
|
|
215
287
|
constructor(client: MemoryClient);
|
|
@@ -247,12 +319,18 @@ export declare class AgentsManager {
|
|
|
247
319
|
configure(agentIdOrOptions: string | ConfigureAgentOptions, options?: ConfigureAgentOptions): Promise<Record<string, unknown>>;
|
|
248
320
|
upsert(agentIdOrOptions: string | ConfigureAgentOptions, options?: ConfigureAgentOptions): Promise<Record<string, unknown>>;
|
|
249
321
|
delete(agentId: string): Promise<Record<string, unknown>>;
|
|
322
|
+
mintToken(agentId: string, options?: MintWorkerTokenOptions): Promise<Record<string, unknown>>;
|
|
323
|
+
mint_token(agentId: string, options?: MintWorkerTokenOptions): Promise<Record<string, unknown>>;
|
|
324
|
+
revoke(agentId: string): Promise<Record<string, unknown>>;
|
|
325
|
+
revokeAll(): Promise<Record<string, unknown>>;
|
|
326
|
+
revoke_all(): Promise<Record<string, unknown>>;
|
|
250
327
|
}
|
|
251
328
|
export declare class MemoryClient {
|
|
252
329
|
readonly apiKey: string;
|
|
253
330
|
readonly baseUrl: string;
|
|
254
331
|
readonly orgId: string | null;
|
|
255
332
|
readonly projectId: string | null;
|
|
333
|
+
readonly runId: string | null;
|
|
256
334
|
readonly agentId: string | null;
|
|
257
335
|
readonly callerAgentId: string | null;
|
|
258
336
|
readonly redactPolicy: string | string[] | null;
|
|
@@ -261,6 +339,7 @@ export declare class MemoryClient {
|
|
|
261
339
|
readonly timeoutMs: number;
|
|
262
340
|
readonly project: ProjectManager;
|
|
263
341
|
readonly agents: AgentsManager;
|
|
342
|
+
lastVaultRevision: number;
|
|
264
343
|
private readonly fetchImpl;
|
|
265
344
|
constructor(options?: MemoryClientOptions);
|
|
266
345
|
private applyLifecycleDefaults;
|
|
@@ -293,6 +372,14 @@ export declare class MemoryClient {
|
|
|
293
372
|
deleteAgentPolicy(agentId: string): Promise<Record<string, unknown>>;
|
|
294
373
|
delete_agent(agentId: string): Promise<Record<string, unknown>>;
|
|
295
374
|
delete_agent_policy(agentId: string): Promise<Record<string, unknown>>;
|
|
375
|
+
mintWorkerToken(agentId: string, options?: MintWorkerTokenOptions): Promise<Record<string, unknown>>;
|
|
376
|
+
mint_worker_token(agentId: string, options?: MintWorkerTokenOptions): Promise<Record<string, unknown>>;
|
|
377
|
+
revokeAgentTokens(agentId: string): Promise<Record<string, unknown>>;
|
|
378
|
+
revoke_agent_tokens(agentId: string): Promise<Record<string, unknown>>;
|
|
379
|
+
revokeAllTokens(): Promise<Record<string, unknown>>;
|
|
380
|
+
revoke_all_tokens(): Promise<Record<string, unknown>>;
|
|
381
|
+
promoteRun(runIdOrOptions?: string | PromoteRunOptions, options?: PromoteRunOptions): Promise<Record<string, unknown>>;
|
|
382
|
+
promote_run(runIdOrOptions?: string | PromoteRunOptions, options?: PromoteRunOptions): Promise<Record<string, unknown>>;
|
|
296
383
|
intervene(textOrPayload: string | InterveneOptions, options?: InterveneOptions): Promise<Record<string, unknown>>;
|
|
297
384
|
status(options?: StatusOptions): Promise<Record<string, unknown>>;
|
|
298
385
|
add(messages: string | Array<Record<string, unknown>> | Record<string, unknown>, options?: AddMemoryOptions): Promise<Record<string, unknown>>;
|
|
@@ -459,4 +546,9 @@ export declare class AsyncSlashMLMemory extends MemoryClient {
|
|
|
459
546
|
}
|
|
460
547
|
export declare class AsyncSlashMLClient extends MemoryClient {
|
|
461
548
|
}
|
|
549
|
+
export declare function guardCallable<T extends (...args: any[]) => any>(fn: T, toolName?: string, options?: GuardToolsOptions): (...args: Parameters<T>) => Promise<unknown>;
|
|
550
|
+
/**
|
|
551
|
+
* 1-line runtime guardrail wrapper for Vercel AI SDK tool maps, LangChain.js tools, or functions.
|
|
552
|
+
*/
|
|
553
|
+
export declare function guardTools<T>(tools: T, options?: GuardToolsOptions): T;
|
|
462
554
|
export default Memory;
|
package/dist/index.js
CHANGED
|
@@ -5,7 +5,9 @@
|
|
|
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.AgentsManager = 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.SlashMLToolBlockedError = exports.SlashMLAPIError = exports.DEFAULT_TIMEOUT_MS = exports.DEFAULT_MODEL = exports.DEFAULT_BASE_URL = void 0;
|
|
9
|
+
exports.guardCallable = guardCallable;
|
|
10
|
+
exports.guardTools = guardTools;
|
|
9
11
|
exports.DEFAULT_BASE_URL = "https://api.slashml.ai";
|
|
10
12
|
exports.DEFAULT_MODEL = "slashml-1.5-fast";
|
|
11
13
|
exports.DEFAULT_TIMEOUT_MS = 30_000;
|
|
@@ -43,6 +45,19 @@ class SlashMLAPIError extends Error {
|
|
|
43
45
|
}
|
|
44
46
|
}
|
|
45
47
|
exports.SlashMLAPIError = SlashMLAPIError;
|
|
48
|
+
class SlashMLToolBlockedError extends Error {
|
|
49
|
+
toolName;
|
|
50
|
+
ruleIds;
|
|
51
|
+
rawResponse;
|
|
52
|
+
constructor(toolName, message, ruleIds = [], rawResponse = {}) {
|
|
53
|
+
super(message);
|
|
54
|
+
this.name = "SlashMLToolBlockedError";
|
|
55
|
+
this.toolName = toolName;
|
|
56
|
+
this.ruleIds = ruleIds;
|
|
57
|
+
this.rawResponse = rawResponse;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
exports.SlashMLToolBlockedError = SlashMLToolBlockedError;
|
|
46
61
|
function getEnv(name) {
|
|
47
62
|
const g = globalThis;
|
|
48
63
|
if (typeof g.process !== "undefined" && g.process.env) {
|
|
@@ -199,6 +214,42 @@ class AgentsManager {
|
|
|
199
214
|
async delete(agentId) {
|
|
200
215
|
return this.client.request("DELETE", `/v1/agents/${encodeURIComponent(String(agentId).trim())}/`);
|
|
201
216
|
}
|
|
217
|
+
async mintToken(agentId, options = {}) {
|
|
218
|
+
const effRunId = options.run_id ?? options.runId ?? this.client.runId;
|
|
219
|
+
const effProjectId = options.project_id ?? options.projectId ?? this.client.projectId;
|
|
220
|
+
const payload = {
|
|
221
|
+
tier: options.tier ?? "worker",
|
|
222
|
+
ttl_seconds: options.ttl_seconds ?? options.ttlSeconds ?? 7200,
|
|
223
|
+
can_delegate: Boolean(options.can_delegate ?? options.canDelegate ?? false),
|
|
224
|
+
};
|
|
225
|
+
if (effRunId)
|
|
226
|
+
payload.run_id = effRunId;
|
|
227
|
+
if (effProjectId)
|
|
228
|
+
payload.project_id = effProjectId;
|
|
229
|
+
const accessMode = options.access_mode ?? options.accessMode;
|
|
230
|
+
if (accessMode !== undefined)
|
|
231
|
+
payload.access_mode = accessMode;
|
|
232
|
+
const redactPolicy = options.redact_policy ?? options.redactPolicy;
|
|
233
|
+
if (redactPolicy !== undefined) {
|
|
234
|
+
payload.redact_policy = normalizeRedactPolicy(redactPolicy);
|
|
235
|
+
}
|
|
236
|
+
const redactScope = options.redact_scope ?? options.redactScope;
|
|
237
|
+
if (redactScope !== undefined)
|
|
238
|
+
payload.redact_scope = redactScope;
|
|
239
|
+
return this.client.request("POST", `/v1/agents/${encodeURIComponent(String(agentId).trim())}/tokens`, { jsonBody: payload });
|
|
240
|
+
}
|
|
241
|
+
async mint_token(agentId, options = {}) {
|
|
242
|
+
return this.mintToken(agentId, options);
|
|
243
|
+
}
|
|
244
|
+
async revoke(agentId) {
|
|
245
|
+
return this.client.request("POST", `/v1/agents/${encodeURIComponent(String(agentId).trim())}/revoke`, { jsonBody: {} });
|
|
246
|
+
}
|
|
247
|
+
async revokeAll() {
|
|
248
|
+
return this.client.request("POST", "/v1/agents/revoke-all", { jsonBody: {} });
|
|
249
|
+
}
|
|
250
|
+
async revoke_all() {
|
|
251
|
+
return this.revokeAll();
|
|
252
|
+
}
|
|
202
253
|
}
|
|
203
254
|
exports.AgentsManager = AgentsManager;
|
|
204
255
|
class MemoryClient {
|
|
@@ -206,6 +257,7 @@ class MemoryClient {
|
|
|
206
257
|
baseUrl;
|
|
207
258
|
orgId;
|
|
208
259
|
projectId;
|
|
260
|
+
runId;
|
|
209
261
|
agentId;
|
|
210
262
|
callerAgentId;
|
|
211
263
|
redactPolicy;
|
|
@@ -214,6 +266,7 @@ class MemoryClient {
|
|
|
214
266
|
timeoutMs;
|
|
215
267
|
project;
|
|
216
268
|
agents;
|
|
269
|
+
lastVaultRevision;
|
|
217
270
|
fetchImpl;
|
|
218
271
|
constructor(options = {}) {
|
|
219
272
|
const resolvedKey = options.apiKey ||
|
|
@@ -237,6 +290,10 @@ class MemoryClient {
|
|
|
237
290
|
options.caller_agent_id ??
|
|
238
291
|
getEnv("SLASHML_AGENT_ID") ??
|
|
239
292
|
null;
|
|
293
|
+
const rawRunId = options.runId ??
|
|
294
|
+
options.run_id ??
|
|
295
|
+
getEnv("SLASHML_RUN_ID") ??
|
|
296
|
+
null;
|
|
240
297
|
const rawRedactPolicy = options.redactPolicy ??
|
|
241
298
|
options.redact_policy ??
|
|
242
299
|
getEnv("SLASHML_REDACT_POLICY") ??
|
|
@@ -249,6 +306,7 @@ class MemoryClient {
|
|
|
249
306
|
this.baseUrl = resolvedUrl.replace(/\/+$/, "");
|
|
250
307
|
this.orgId = options.orgId || options.org_id || getEnv("SLASHML_ORG_ID") || null;
|
|
251
308
|
this.projectId = options.projectId || options.project_id || getEnv("SLASHML_PROJECT_ID") || null;
|
|
309
|
+
this.runId = rawRunId && String(rawRunId).trim() ? String(rawRunId).trim() : null;
|
|
252
310
|
this.agentId = rawAgentId && String(rawAgentId).trim() ? String(rawAgentId).trim() : null;
|
|
253
311
|
this.callerAgentId = this.agentId;
|
|
254
312
|
this.redactPolicy = normalizeRedactPolicy(rawRedactPolicy);
|
|
@@ -258,6 +316,7 @@ class MemoryClient {
|
|
|
258
316
|
: null;
|
|
259
317
|
this.model = resolvedModel;
|
|
260
318
|
this.timeoutMs = options.timeoutMs ?? exports.DEFAULT_TIMEOUT_MS;
|
|
319
|
+
this.lastVaultRevision = 0;
|
|
261
320
|
this.fetchImpl = options.fetchImpl ?? globalThis.fetch;
|
|
262
321
|
this.project = new ProjectManager(this);
|
|
263
322
|
this.agents = new AgentsManager(this);
|
|
@@ -315,6 +374,9 @@ class MemoryClient {
|
|
|
315
374
|
if (this.projectId) {
|
|
316
375
|
headers["X-SlashML-Project-Id"] = this.projectId;
|
|
317
376
|
}
|
|
377
|
+
if (this.runId) {
|
|
378
|
+
headers["X-SlashML-Run-Id"] = this.runId;
|
|
379
|
+
}
|
|
318
380
|
if (this.agentId) {
|
|
319
381
|
headers["X-SlashML-Agent-Id"] = this.agentId;
|
|
320
382
|
}
|
|
@@ -333,6 +395,16 @@ class MemoryClient {
|
|
|
333
395
|
body: options.jsonBody !== undefined ? JSON.stringify(options.jsonBody) : undefined,
|
|
334
396
|
signal: controller.signal,
|
|
335
397
|
});
|
|
398
|
+
const revHdr = response.headers && typeof response.headers.get === "function"
|
|
399
|
+
? response.headers.get("x-slashml-vault-revision") ||
|
|
400
|
+
response.headers.get("X-SlashML-Vault-Revision")
|
|
401
|
+
: null;
|
|
402
|
+
if (revHdr) {
|
|
403
|
+
const parsedRev = Number.parseInt(String(revHdr).trim(), 10);
|
|
404
|
+
if (Number.isFinite(parsedRev) && parsedRev > this.lastVaultRevision) {
|
|
405
|
+
this.lastVaultRevision = parsedRev;
|
|
406
|
+
}
|
|
407
|
+
}
|
|
336
408
|
const rawText = await response.text();
|
|
337
409
|
let parsed = {};
|
|
338
410
|
if (rawText) {
|
|
@@ -556,6 +628,54 @@ class MemoryClient {
|
|
|
556
628
|
async delete_agent_policy(agentId) {
|
|
557
629
|
return this.agents.delete(agentId);
|
|
558
630
|
}
|
|
631
|
+
async mintWorkerToken(agentId, options = {}) {
|
|
632
|
+
return this.agents.mintToken(agentId, options);
|
|
633
|
+
}
|
|
634
|
+
async mint_worker_token(agentId, options = {}) {
|
|
635
|
+
return this.agents.mintToken(agentId, options);
|
|
636
|
+
}
|
|
637
|
+
async revokeAgentTokens(agentId) {
|
|
638
|
+
return this.agents.revoke(agentId);
|
|
639
|
+
}
|
|
640
|
+
async revoke_agent_tokens(agentId) {
|
|
641
|
+
return this.agents.revoke(agentId);
|
|
642
|
+
}
|
|
643
|
+
async revokeAllTokens() {
|
|
644
|
+
return this.agents.revokeAll();
|
|
645
|
+
}
|
|
646
|
+
async revoke_all_tokens() {
|
|
647
|
+
return this.agents.revokeAll();
|
|
648
|
+
}
|
|
649
|
+
async promoteRun(runIdOrOptions, options = {}) {
|
|
650
|
+
const isStr = typeof runIdOrOptions === "string";
|
|
651
|
+
const opts = isStr
|
|
652
|
+
? { ...options, run_id: runIdOrOptions }
|
|
653
|
+
: { ...(runIdOrOptions ?? {}), ...options };
|
|
654
|
+
const effRunId = opts.run_id ?? opts.runId ?? this.runId;
|
|
655
|
+
if (!effRunId) {
|
|
656
|
+
throw new SlashMLAPIError(400, "runId is required for promoteRun (pass runId or set SLASHML_RUN_ID).");
|
|
657
|
+
}
|
|
658
|
+
const payload = {
|
|
659
|
+
run_id: effRunId,
|
|
660
|
+
discard_unpromoted: Boolean(opts.discard_unpromoted ?? opts.discardUnpromoted ?? false),
|
|
661
|
+
};
|
|
662
|
+
const targetProject = opts.target_project_id ?? opts.targetProjectId ?? this.projectId;
|
|
663
|
+
if (targetProject)
|
|
664
|
+
payload.target_project_id = targetProject;
|
|
665
|
+
const targetUser = opts.target_user_id ?? opts.targetUserId;
|
|
666
|
+
if (targetUser)
|
|
667
|
+
payload.target_user_id = targetUser;
|
|
668
|
+
const memoryIds = opts.memory_ids ?? opts.memoryIds;
|
|
669
|
+
if (memoryIds !== undefined && memoryIds !== null)
|
|
670
|
+
payload.memory_ids = memoryIds;
|
|
671
|
+
const headers = this.applyLifecycleDefaults(payload, {
|
|
672
|
+
callerAgentId: opts.caller_agent_id ?? opts.callerAgentId,
|
|
673
|
+
});
|
|
674
|
+
return this.request("POST", "/v1/memory/promote", { jsonBody: payload, headers });
|
|
675
|
+
}
|
|
676
|
+
async promote_run(runIdOrOptions, options = {}) {
|
|
677
|
+
return this.promoteRun(runIdOrOptions, options);
|
|
678
|
+
}
|
|
559
679
|
async intervene(textOrPayload, options = {}) {
|
|
560
680
|
const opts = typeof textOrPayload === "string"
|
|
561
681
|
? { ...options, prompt: options.prompt ?? textOrPayload, text: options.text ?? textOrPayload }
|
|
@@ -563,7 +683,10 @@ class MemoryClient {
|
|
|
563
683
|
const resolvedText = opts.text ?? opts.prompt ?? "";
|
|
564
684
|
const resolvedModel = opts.model ?? this.model;
|
|
565
685
|
const resolvedProjectId = opts.project_id ?? opts.projectId ?? this.projectId ?? null;
|
|
686
|
+
const resolvedRunId = opts.run_id ?? opts.runId ?? this.runId ?? null;
|
|
566
687
|
const resolvedSessionId = opts.session_id ?? opts.sessionId ?? null;
|
|
688
|
+
const strictBlock = opts.strict_pretool_block ?? opts.strictPretoolBlock;
|
|
689
|
+
const inheritScopes = opts.inherit_scopes ?? opts.inheritScopes;
|
|
567
690
|
const lastAssistantMsg = opts.last_assistant_message ?? opts.lastAssistantMessage ?? undefined;
|
|
568
691
|
const stopHookActive = opts.stop_hook_active ?? opts.stopHookActive ?? undefined;
|
|
569
692
|
const whisperedIds = opts.whispered_memory_ids ?? opts.whisperedMemoryIds ?? undefined;
|
|
@@ -579,7 +702,10 @@ class MemoryClient {
|
|
|
579
702
|
tool_response: opts.tool_response ?? opts.toolResponse ?? null,
|
|
580
703
|
user_id: opts.user_id ?? opts.userId ?? "default",
|
|
581
704
|
...(resolvedProjectId ? { project_id: resolvedProjectId } : {}),
|
|
705
|
+
...(resolvedRunId ? { run_id: resolvedRunId } : {}),
|
|
582
706
|
...(resolvedSessionId ? { session_id: resolvedSessionId } : {}),
|
|
707
|
+
...(strictBlock !== undefined ? { strict_pretool_block: Boolean(strictBlock) } : {}),
|
|
708
|
+
...(inheritScopes !== undefined ? { inherit_scopes: Boolean(inheritScopes) } : {}),
|
|
583
709
|
model: resolvedModel,
|
|
584
710
|
commit_writes: opts.commit_writes ?? opts.commitWrites ?? true,
|
|
585
711
|
top_k: opts.top_k ?? opts.topK ?? 5,
|
|
@@ -629,7 +755,10 @@ class MemoryClient {
|
|
|
629
755
|
user_id: options.user_id ?? options.userId ?? null,
|
|
630
756
|
agent_id: options.agent_id ?? options.agentId ?? null,
|
|
631
757
|
app_id: options.app_id ?? options.appId ?? null,
|
|
632
|
-
run_id: options.run_id ?? options.runId ?? null,
|
|
758
|
+
run_id: options.run_id ?? options.runId ?? this.runId ?? null,
|
|
759
|
+
...(options.project_id ?? options.projectId ?? this.projectId
|
|
760
|
+
? { project_id: options.project_id ?? options.projectId ?? this.projectId }
|
|
761
|
+
: {}),
|
|
633
762
|
metadata: options.metadata ?? {},
|
|
634
763
|
infer: options.infer ?? true,
|
|
635
764
|
model: this.model,
|
|
@@ -650,12 +779,16 @@ class MemoryClient {
|
|
|
650
779
|
const query = isObj ? queryOrOptions.query : queryOrOptions;
|
|
651
780
|
const opts = isObj ? queryOrOptions : options;
|
|
652
781
|
const effectiveLimit = opts.top_k ?? opts.topK ?? opts.limit ?? 5;
|
|
782
|
+
const inheritScopes = opts.inherit_scopes ?? opts.inheritScopes;
|
|
783
|
+
const effProj = opts.project_id ?? opts.projectId ?? this.projectId;
|
|
653
784
|
const payload = {
|
|
654
785
|
query,
|
|
655
786
|
user_id: opts.user_id ?? opts.userId ?? null,
|
|
656
787
|
agent_id: opts.agent_id ?? opts.agentId ?? null,
|
|
657
788
|
app_id: opts.app_id ?? opts.appId ?? null,
|
|
658
|
-
run_id: opts.run_id ?? opts.runId ?? null,
|
|
789
|
+
run_id: opts.run_id ?? opts.runId ?? this.runId ?? null,
|
|
790
|
+
...(effProj ? { project_id: effProj } : {}),
|
|
791
|
+
...(inheritScopes !== undefined ? { inherit_scopes: Boolean(inheritScopes) } : {}),
|
|
659
792
|
limit: effectiveLimit,
|
|
660
793
|
top_k: effectiveLimit,
|
|
661
794
|
filters: opts.filters ?? opts.metadata ?? null,
|
|
@@ -675,6 +808,7 @@ class MemoryClient {
|
|
|
675
808
|
async getAll(options = {}) {
|
|
676
809
|
const effPolicy = normalizeRedactPolicy(options.redact_policy ?? options.redactPolicy ?? this.redactPolicy);
|
|
677
810
|
const effScope = options.redact_scope ?? options.redactScope ?? this.redactScope;
|
|
811
|
+
const inheritScopes = options.inherit_scopes ?? options.inheritScopes;
|
|
678
812
|
const headers = this.applyLifecycleDefaults(undefined, {
|
|
679
813
|
callerAgentId: options.caller_agent_id ?? options.callerAgentId,
|
|
680
814
|
});
|
|
@@ -683,7 +817,9 @@ class MemoryClient {
|
|
|
683
817
|
user_id: options.user_id ?? options.userId ?? undefined,
|
|
684
818
|
agent_id: options.agent_id ?? options.agentId ?? undefined,
|
|
685
819
|
app_id: options.app_id ?? options.appId ?? undefined,
|
|
686
|
-
run_id: options.run_id ?? options.runId ?? undefined,
|
|
820
|
+
run_id: options.run_id ?? options.runId ?? this.runId ?? undefined,
|
|
821
|
+
project_id: options.project_id ?? options.projectId ?? this.projectId ?? undefined,
|
|
822
|
+
inherit_scopes: inheritScopes !== undefined ? Boolean(inheritScopes) : undefined,
|
|
687
823
|
limit: options.limit ?? 100,
|
|
688
824
|
redact_policy: Array.isArray(effPolicy) ? effPolicy.join(",") : (effPolicy ?? undefined),
|
|
689
825
|
redact_scope: effScope ?? undefined,
|
|
@@ -918,6 +1054,7 @@ class Memory extends MemoryClient {
|
|
|
918
1054
|
configDict.host,
|
|
919
1055
|
orgId: configDict.org_id || configDict.orgId,
|
|
920
1056
|
projectId: configDict.project_id || configDict.projectId,
|
|
1057
|
+
runId: configDict.run_id || configDict.runId,
|
|
921
1058
|
agentId: configDict.agent_id ||
|
|
922
1059
|
configDict.agentId ||
|
|
923
1060
|
configDict.caller_agent_id ||
|
|
@@ -951,4 +1088,205 @@ exports.AsyncSlashMLMemory = AsyncSlashMLMemory;
|
|
|
951
1088
|
class AsyncSlashMLClient extends MemoryClient {
|
|
952
1089
|
}
|
|
953
1090
|
exports.AsyncSlashMLClient = AsyncSlashMLClient;
|
|
1091
|
+
function resolveGuardClient(options = {}) {
|
|
1092
|
+
if (options.client instanceof MemoryClient) {
|
|
1093
|
+
return options.client;
|
|
1094
|
+
}
|
|
1095
|
+
return new MemoryClient({
|
|
1096
|
+
apiKey: options.apiKey ?? options.api_key,
|
|
1097
|
+
baseUrl: options.baseUrl ?? options.base_url,
|
|
1098
|
+
projectId: options.projectId ?? options.project_id ?? undefined,
|
|
1099
|
+
runId: options.runId ?? options.run_id ?? undefined,
|
|
1100
|
+
agentId: options.callerAgentId ?? options.caller_agent_id ?? undefined,
|
|
1101
|
+
redactPolicy: options.redactPolicy ?? options.redact_policy,
|
|
1102
|
+
});
|
|
1103
|
+
}
|
|
1104
|
+
function normalizeGuardInput(args) {
|
|
1105
|
+
if (args.length === 0)
|
|
1106
|
+
return {};
|
|
1107
|
+
const first = args[0];
|
|
1108
|
+
if (first && typeof first === "object" && !Array.isArray(first)) {
|
|
1109
|
+
return { ...first };
|
|
1110
|
+
}
|
|
1111
|
+
if (args.length === 1) {
|
|
1112
|
+
return { input: first };
|
|
1113
|
+
}
|
|
1114
|
+
return { args };
|
|
1115
|
+
}
|
|
1116
|
+
async function evaluateTsPretoolGuard(client, toolName, toolInput, options) {
|
|
1117
|
+
const failMode = String(options.failMode ?? options.fail_mode ?? "open").toLowerCase();
|
|
1118
|
+
const promptText = `${toolName} ${JSON.stringify(toolInput)}`;
|
|
1119
|
+
try {
|
|
1120
|
+
const res = await client.intervene(promptText, {
|
|
1121
|
+
hook_event_name: "PreToolUse",
|
|
1122
|
+
tool_name: toolName,
|
|
1123
|
+
tool_input: toolInput,
|
|
1124
|
+
user_id: options.userId ?? options.user_id ?? "default",
|
|
1125
|
+
project_id: options.projectId ?? options.project_id ?? client.projectId,
|
|
1126
|
+
run_id: options.runId ?? options.run_id ?? client.runId,
|
|
1127
|
+
strict_pretool_block: true,
|
|
1128
|
+
commit_writes: false,
|
|
1129
|
+
redact_policy: options.redactPolicy ?? options.redact_policy,
|
|
1130
|
+
caller_agent_id: options.callerAgentId ?? options.caller_agent_id ?? client.agentId,
|
|
1131
|
+
});
|
|
1132
|
+
const decision = String(res.action ?? res.intervention_severity ?? res.decision ?? "").toUpperCase();
|
|
1133
|
+
const sev = String(res.intervention_severity ?? "").toUpperCase();
|
|
1134
|
+
const hookEnv = res.hook_output && typeof res.hook_output === "object"
|
|
1135
|
+
? res.hook_output
|
|
1136
|
+
: res;
|
|
1137
|
+
const hookOut = hookEnv.hookSpecificOutput && typeof hookEnv.hookSpecificOutput === "object"
|
|
1138
|
+
? hookEnv.hookSpecificOutput
|
|
1139
|
+
: res.hookSpecificOutput && typeof res.hookSpecificOutput === "object"
|
|
1140
|
+
? res.hookSpecificOutput
|
|
1141
|
+
: {};
|
|
1142
|
+
const perm = String(hookOut.permissionDecision ?? "").toLowerCase();
|
|
1143
|
+
const blocked = decision === "BLOCK" || sev === "BLOCK" || perm === "deny";
|
|
1144
|
+
if (!blocked) {
|
|
1145
|
+
return { blocked: false, message: "", ruleIds: [], raw: res };
|
|
1146
|
+
}
|
|
1147
|
+
let msg = String(hookOut.permissionDecisionReason ?? "").trim() ||
|
|
1148
|
+
String(res.intervention_message ?? "").trim() ||
|
|
1149
|
+
String(res.whisper ?? "").trim() ||
|
|
1150
|
+
String(res.reason ?? "").trim() ||
|
|
1151
|
+
`[SLASHML GUARDRAIL BLOCK] Tool '${toolName}' blocked by active policy.`;
|
|
1152
|
+
if (!msg.includes("[SLASHML GUARDRAIL BLOCK]")) {
|
|
1153
|
+
msg = `[SLASHML GUARDRAIL BLOCK] ${msg}`;
|
|
1154
|
+
}
|
|
1155
|
+
const rawIds = Array.isArray(res.violated_memory_ids)
|
|
1156
|
+
? res.violated_memory_ids
|
|
1157
|
+
: Array.isArray(res.whispered_memory_ids)
|
|
1158
|
+
? res.whispered_memory_ids
|
|
1159
|
+
: [];
|
|
1160
|
+
const ruleIds = rawIds.map((r) => String(r)).filter(Boolean);
|
|
1161
|
+
return { blocked: true, message: msg, ruleIds, raw: res };
|
|
1162
|
+
}
|
|
1163
|
+
catch (err) {
|
|
1164
|
+
if (err instanceof SlashMLAPIError && (err.statusCode === 401 || err.statusCode === 403)) {
|
|
1165
|
+
const msg = `[SLASHML GUARDRAIL BLOCK] Authorization rejected (${err.statusCode}) before executing '${toolName}': ${err.message}`;
|
|
1166
|
+
return { blocked: true, message: msg, ruleIds: [], raw: err.details };
|
|
1167
|
+
}
|
|
1168
|
+
if (failMode === "closed") {
|
|
1169
|
+
const msg = `[SLASHML GUARDRAIL BLOCK] Guardrail error in fail-closed mode for '${toolName}': ${err instanceof Error ? err.message : String(err)}`;
|
|
1170
|
+
return { blocked: true, message: msg, ruleIds: [], raw: {} };
|
|
1171
|
+
}
|
|
1172
|
+
return { blocked: false, message: "", ruleIds: [], raw: {} };
|
|
1173
|
+
}
|
|
1174
|
+
}
|
|
1175
|
+
async function scrubTsToolOutput(client, output, options) {
|
|
1176
|
+
const redactOutputs = options.redactOutputs ?? options.redact_outputs ?? true;
|
|
1177
|
+
if (!redactOutputs || output === null || output === undefined) {
|
|
1178
|
+
return output;
|
|
1179
|
+
}
|
|
1180
|
+
if (typeof output === "boolean" || typeof output === "number") {
|
|
1181
|
+
return output;
|
|
1182
|
+
}
|
|
1183
|
+
const effPolicy = normalizeRedactPolicy(options.redactPolicy ?? options.redact_policy ?? client.redactPolicy);
|
|
1184
|
+
if (effPolicy === "off") {
|
|
1185
|
+
return output;
|
|
1186
|
+
}
|
|
1187
|
+
const isStructured = typeof output === "object";
|
|
1188
|
+
let rawText = "";
|
|
1189
|
+
if (typeof output === "string") {
|
|
1190
|
+
rawText = output;
|
|
1191
|
+
}
|
|
1192
|
+
else if (isStructured) {
|
|
1193
|
+
try {
|
|
1194
|
+
rawText = JSON.stringify(output);
|
|
1195
|
+
}
|
|
1196
|
+
catch {
|
|
1197
|
+
rawText = String(output);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
else {
|
|
1201
|
+
return output;
|
|
1202
|
+
}
|
|
1203
|
+
if (!rawText.trim()) {
|
|
1204
|
+
return output;
|
|
1205
|
+
}
|
|
1206
|
+
try {
|
|
1207
|
+
const res = await client.redact({
|
|
1208
|
+
text: rawText,
|
|
1209
|
+
redact_policy: effPolicy ?? undefined,
|
|
1210
|
+
caller_agent_id: options.callerAgentId ?? options.caller_agent_id ?? client.agentId ?? undefined,
|
|
1211
|
+
});
|
|
1212
|
+
const redactedText = res.redacted_text;
|
|
1213
|
+
if (typeof redactedText !== "string" || redactedText === rawText) {
|
|
1214
|
+
return output;
|
|
1215
|
+
}
|
|
1216
|
+
if (isStructured) {
|
|
1217
|
+
try {
|
|
1218
|
+
return JSON.parse(redactedText);
|
|
1219
|
+
}
|
|
1220
|
+
catch {
|
|
1221
|
+
return redactedText;
|
|
1222
|
+
}
|
|
1223
|
+
}
|
|
1224
|
+
return redactedText;
|
|
1225
|
+
}
|
|
1226
|
+
catch {
|
|
1227
|
+
const failMode = String(options.failMode ?? options.fail_mode ?? "open").toLowerCase();
|
|
1228
|
+
if (failMode === "closed") {
|
|
1229
|
+
return "[SLASHML REDACTION ERROR: OUTPUT WITHHELD]";
|
|
1230
|
+
}
|
|
1231
|
+
return output;
|
|
1232
|
+
}
|
|
1233
|
+
}
|
|
1234
|
+
function guardCallable(fn, toolName = "tool", options = {}) {
|
|
1235
|
+
const client = resolveGuardClient(options);
|
|
1236
|
+
const onBlock = options.onBlock ?? options.on_block ?? "return_error";
|
|
1237
|
+
const resolvedName = toolName || fn.name || "tool";
|
|
1238
|
+
return async (...args) => {
|
|
1239
|
+
const toolInput = normalizeGuardInput(args);
|
|
1240
|
+
const verdict = await evaluateTsPretoolGuard(client, resolvedName, toolInput, options);
|
|
1241
|
+
if (verdict.blocked) {
|
|
1242
|
+
if (onBlock === "raise") {
|
|
1243
|
+
throw new SlashMLToolBlockedError(resolvedName, verdict.message, verdict.ruleIds, verdict.raw);
|
|
1244
|
+
}
|
|
1245
|
+
if (onBlock === "return_object") {
|
|
1246
|
+
return { error: verdict.message, blocked: true, ruleIds: verdict.ruleIds };
|
|
1247
|
+
}
|
|
1248
|
+
return verdict.message;
|
|
1249
|
+
}
|
|
1250
|
+
const result = await fn(...args);
|
|
1251
|
+
return scrubTsToolOutput(client, result, options);
|
|
1252
|
+
};
|
|
1253
|
+
}
|
|
1254
|
+
/**
|
|
1255
|
+
* 1-line runtime guardrail wrapper for Vercel AI SDK tool maps, LangChain.js tools, or functions.
|
|
1256
|
+
*/
|
|
1257
|
+
function guardTools(tools, options = {}) {
|
|
1258
|
+
const client = resolveGuardClient(options);
|
|
1259
|
+
const mergedOpts = { ...options, client };
|
|
1260
|
+
const wrapItem = (item, explicitName) => {
|
|
1261
|
+
if (typeof item === "function") {
|
|
1262
|
+
return guardCallable(item, explicitName || item.name || "tool", mergedOpts);
|
|
1263
|
+
}
|
|
1264
|
+
if (item && typeof item === "object") {
|
|
1265
|
+
const name = String(explicitName || item.name || "tool");
|
|
1266
|
+
const wrappedObj = { ...item };
|
|
1267
|
+
for (const methodKey of ["execute", "invoke", "func", "call"]) {
|
|
1268
|
+
if (typeof item[methodKey] === "function") {
|
|
1269
|
+
const origFn = item[methodKey].bind(item);
|
|
1270
|
+
wrappedObj[methodKey] = guardCallable(origFn, name, mergedOpts);
|
|
1271
|
+
}
|
|
1272
|
+
}
|
|
1273
|
+
return wrappedObj;
|
|
1274
|
+
}
|
|
1275
|
+
return item;
|
|
1276
|
+
};
|
|
1277
|
+
if (typeof tools === "function") {
|
|
1278
|
+
return wrapItem(tools);
|
|
1279
|
+
}
|
|
1280
|
+
if (Array.isArray(tools)) {
|
|
1281
|
+
return tools.map((t) => wrapItem(t));
|
|
1282
|
+
}
|
|
1283
|
+
if (tools && typeof tools === "object") {
|
|
1284
|
+
const out = {};
|
|
1285
|
+
for (const [key, val] of Object.entries(tools)) {
|
|
1286
|
+
out[key] = wrapItem(val, key);
|
|
1287
|
+
}
|
|
1288
|
+
return out;
|
|
1289
|
+
}
|
|
1290
|
+
return tools;
|
|
1291
|
+
}
|
|
954
1292
|
exports.default = Memory;
|
package/dist/index.mjs
CHANGED
|
@@ -7,6 +7,7 @@ export const {
|
|
|
7
7
|
DEFAULT_MODEL,
|
|
8
8
|
DEFAULT_TIMEOUT_MS,
|
|
9
9
|
SlashMLAPIError,
|
|
10
|
+
SlashMLToolBlockedError,
|
|
10
11
|
ProjectManager,
|
|
11
12
|
AgentsManager,
|
|
12
13
|
MemoryClient,
|
|
@@ -17,6 +18,8 @@ export const {
|
|
|
17
18
|
SlashMLClient,
|
|
18
19
|
AsyncSlashMLMemory,
|
|
19
20
|
AsyncSlashMLClient,
|
|
21
|
+
guardCallable,
|
|
22
|
+
guardTools,
|
|
20
23
|
} = cjs;
|
|
21
24
|
|
|
22
25
|
export default cjs;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@slashml/memory",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Official HTTP TypeScript & Node.js SDK for / ml (https://api.slashml.ai)",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://slashml.ai",
|
|
@@ -31,8 +31,8 @@
|
|
|
31
31
|
"LICENSE"
|
|
32
32
|
],
|
|
33
33
|
"scripts": {
|
|
34
|
-
"build": "tsc && node -e \"const fs = require('node:fs'); const code = 'import { createRequire } from \\\"node:module\\\";\\nconst require = createRequire(import.meta.url);\\nconst cjs = require(\\\"./index.js\\\");\\n\\nexport const {\\n DEFAULT_BASE_URL,\\n DEFAULT_MODEL,\\n DEFAULT_TIMEOUT_MS,\\n SlashMLAPIError,\\n ProjectManager,\\n AgentsManager,\\n MemoryClient,\\n Memory,\\n AsyncMemoryClient,\\n AsyncMemory,\\n SlashMLMemory,\\n SlashMLClient,\\n AsyncSlashMLMemory,\\n AsyncSlashMLClient,\\n} = cjs;\\n\\nexport default cjs;\\n'; fs.writeFileSync('dist/index.mjs', code);\"",
|
|
35
|
-
"test": "node --test test/sdk.test.js"
|
|
34
|
+
"build": "tsc && node -e \"const fs = require('node:fs'); const code = 'import { createRequire } from \\\"node:module\\\";\\nconst require = createRequire(import.meta.url);\\nconst cjs = require(\\\"./index.js\\\");\\n\\nexport const {\\n DEFAULT_BASE_URL,\\n DEFAULT_MODEL,\\n DEFAULT_TIMEOUT_MS,\\n SlashMLAPIError,\\n SlashMLToolBlockedError,\\n ProjectManager,\\n AgentsManager,\\n MemoryClient,\\n Memory,\\n AsyncMemoryClient,\\n AsyncMemory,\\n SlashMLMemory,\\n SlashMLClient,\\n AsyncSlashMLMemory,\\n AsyncSlashMLClient,\\n guardCallable,\\n guardTools,\\n} = cjs;\\n\\nexport default cjs;\\n'; fs.writeFileSync('dist/index.mjs', code);\"",
|
|
35
|
+
"test": "node --test test/sdk.test.js test/swarm_guardrails.test.js"
|
|
36
36
|
},
|
|
37
37
|
"engines": {
|
|
38
38
|
"node": ">=18.0.0"
|
|
@@ -41,7 +41,6 @@
|
|
|
41
41
|
"slashml",
|
|
42
42
|
"memory",
|
|
43
43
|
"agent-memory",
|
|
44
|
-
"mem0",
|
|
45
44
|
"mcp",
|
|
46
45
|
"langgraph",
|
|
47
46
|
"semantic-memory"
|