@plimsollmark/client 0.19.0 → 0.20.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/mastra.d.ts CHANGED
@@ -1,5 +1,16 @@
1
+ import { type ToolExecutionContext } from "@mastra/core/tools";
1
2
  import { CodeSandboxes, type CodeSandboxesOptions } from "./sandboxes.ts";
2
- export type PlimsollExecuteCodeOptions = CodeSandboxesOptions;
3
+ export type PlimsollExecuteCodeOptions = CodeSandboxesOptions & {
4
+ /**
5
+ * The user a call belongs to, from your app's own authentication (a value your server
6
+ * put in Mastra's requestContext, say). Default: the call's resourceId. Set it for an
7
+ * agent network: Mastra fills a missing resourceId with the network's name, so every
8
+ * user of the network would count as one owner under maxSessionsPerOwner and the
9
+ * daemon's SANDBOX_MAX_SESSIONS_PER_OWNER, and close each other's sandboxes. Pass the
10
+ * same owner to dispose.
11
+ */
12
+ owner?: (ctx: ToolExecutionContext) => string | undefined;
13
+ };
3
14
  export declare function plimsollExecuteCode(opts: PlimsollExecuteCodeOptions): {
4
15
  executeCode: import("@mastra/core/tools").Tool<{
5
16
  code: string;
@@ -21,8 +32,8 @@ export declare function plimsollExecuteCode(opts: PlimsollExecuteCodeOptions): {
21
32
  freshSandbox?: true | undefined;
22
33
  isolation: string;
23
34
  recordSha256: string;
24
- }, unknown, unknown, import("@mastra/core/tools").ToolExecutionContext<unknown, unknown, unknown>, "executeCode", unknown>;
25
- /** Closes a user's thread's sandbox, e.g. when the conversation ends. */
26
- dispose: (threadId: string, resourceId: string) => Promise<void>;
35
+ }, unknown, unknown, ToolExecutionContext<unknown, unknown, unknown>, "executeCode", unknown>;
36
+ /** Closes a user's thread's sandbox, e.g. when the conversation ends; owner is the one the option gave. */
37
+ dispose: (threadId: string, resourceId: string, owner?: string) => Promise<void>;
27
38
  sandboxes: CodeSandboxes;
28
39
  };
package/dist/mastra.js CHANGED
@@ -5,12 +5,17 @@
5
5
  import { createTool } from "@mastra/core/tools";
6
6
  import { CodeSandboxes } from "./sandboxes.js";
7
7
  import { executeCodeDescription, executeCodeInput, executeCodeOutput } from "./tool.js";
8
- // A thread id is unique only within its resource (the user), so the key is both, and a
9
- // call without either has no key and runs fresh. An empty resource would put every user
10
- // without one whose thread id collided into one sandbox, with each other's files and
11
- // variables. The pair is encoded as JSON, not joined with a separator: a resource ending
12
- // in the separator and a thread starting with it would name another user's key.
13
- const threadKey = (threadId, resourceId) => threadId && resourceId ? JSON.stringify([resourceId, threadId]) : undefined;
8
+ // A thread id is unique only within its resource (the user), so the key is both, the
9
+ // resource as the owner the per-user cap counts, and a call without either has no key
10
+ // and runs fresh. An empty resource would put every user without one whose thread id
11
+ // collided into one sandbox, with each other's files and variables. An owner of the
12
+ // app's own replaces the resource as what the caps count, and the conversation keeps
13
+ // both IDs, so the sandbox is still the resource's thread's.
14
+ const threadKey = (threadId, resourceId, owner) => !threadId || !resourceId
15
+ ? undefined
16
+ : owner === undefined
17
+ ? { owner: resourceId, conversation: threadId }
18
+ : { owner, conversation: JSON.stringify([resourceId, threadId]) };
14
19
  export function plimsollExecuteCode(opts) {
15
20
  const sandboxes = new CodeSandboxes(opts);
16
21
  const executeCode = createTool({
@@ -20,14 +25,15 @@ export function plimsollExecuteCode(opts) {
20
25
  outputSchema: executeCodeOutput,
21
26
  // The user's thread scopes the sandbox. A call outside a thread, or without a
22
27
  // resource, runs fresh: one sandbox shared across conversations would show one user
23
- // another's files.
24
- execute: async (input, ctx) => sandboxes.run(threadKey(ctx?.agent?.threadId, ctx?.agent?.resourceId), input, ctx?.abortSignal),
28
+ // another's files. The key is read inside run, so an owner resolver that throws is
29
+ // refused as nothing ran.
30
+ execute: async (input, ctx) => sandboxes.run(() => threadKey(ctx?.agent?.threadId, ctx?.agent?.resourceId, ctx && opts.owner ? opts.owner(ctx) : undefined), input, ctx?.abortSignal),
25
31
  });
26
32
  return {
27
33
  executeCode,
28
- /** Closes a user's thread's sandbox, e.g. when the conversation ends. */
29
- dispose: async (threadId, resourceId) => {
30
- const key = threadKey(threadId, resourceId);
34
+ /** Closes a user's thread's sandbox, e.g. when the conversation ends; owner is the one the option gave. */
35
+ dispose: async (threadId, resourceId, owner) => {
36
+ const key = threadKey(threadId, resourceId, owner);
31
37
  if (key !== undefined)
32
38
  await sandboxes.dispose(key);
33
39
  },
@@ -3,6 +3,18 @@ export type SessionMode = "auto" | "always" | "never";
3
3
  /** The languages a tool offers, the first being the default. */
4
4
  export type ToolLanguages = readonly [Language, ...Language[]];
5
5
  export declare const DEFAULT_LANGUAGES: ToolLanguages;
6
+ /**
7
+ * Which sandbox a call uses, from the application, never from a model's tool call. A
8
+ * conversation names its owner, the user it belongs to, whom maxSessionsPerOwner
9
+ * counts; a string (a Trigger.dev run id) names no owner and counts only against
10
+ * maxSessions.
11
+ */
12
+ export type SandboxKey = string | {
13
+ owner: string;
14
+ conversation: string;
15
+ };
16
+ /** @internal The first 12 hex digits of the key's SHA-256: names it in a log line without its IDs. */
17
+ export declare function keyDigest(key: SandboxKey): string;
6
18
  export type CodeSandboxesOptions = {
7
19
  /**
8
20
  * The daemon's client, or a function that builds it on first use: a
@@ -18,7 +30,11 @@ export type CodeSandboxesOptions = {
18
30
  sessions?: SessionMode;
19
31
  /** The languages the tool offers, the first being the default. Default python, then javascript. */
20
32
  languages?: ToolLanguages;
21
- /** The weakest tier a run accepts; checked before dispatch and on the answer. */
33
+ /**
34
+ * The weakest tier a run accepts; checked before dispatch and on the answer. Default
35
+ * kernel (a verified gVisor runtime), the weakest tier fit for hostile code; set
36
+ * "container" for local development against an ordinary docker daemon.
37
+ */
22
38
  minimumIsolation?: Exclude<Isolation, "none">;
23
39
  /** Per-call budget. */
24
40
  timeoutMs?: number;
@@ -31,16 +47,35 @@ export type CodeSandboxesOptions = {
31
47
  * session's lifetime ends. Default 10 minutes; 0 never.
32
48
  */
33
49
  idleCloseMs?: number;
50
+ /**
51
+ * Sandboxes one owner may hold in this instance. Default 3: a person rarely works in
52
+ * more than a few conversations at once, and the conversation whose sandbox is closed
53
+ * loses its variables and files, never an answer (its next call says freshSandbox).
54
+ * 0 = no cap. It counts this instance only; for an app running as many processes
55
+ * (serverless functions, Trigger.dev workers), set the daemon's
56
+ * SANDBOX_MAX_SESSIONS_PER_OWNER, which counts them all.
57
+ */
58
+ maxSessionsPerOwner?: number;
59
+ /**
60
+ * Sandboxes this instance holds across owners, closed the same way: the share of
61
+ * the daemon's SANDBOX_MAX_SESSIONS this app may take, which only the operator knows.
62
+ * Default 0, no cap of its own. A daemon at its cap refuses the open (not dispatched,
63
+ * reason capacity); closing an idle sandbox then is not done for you, because the
64
+ * daemon gives the same refusal for its rate limit, which a close does not help.
65
+ */
66
+ maxSessions?: number;
34
67
  /** Characters of stdout and of stderr returned to the model; the rest is cut and marked. */
35
68
  maxOutputChars?: number;
36
69
  /**
37
70
  * Called when closing a sandbox fails. A "data_loss" error here means the
38
71
  * daemon counted calls this client has no answer for: a call that ended
39
72
  * without one (an abort, a timeout, a dropped connection, a proxy's error),
40
- * or, if none did, someone else holding the session ID. Default: console.warn.
41
- * The error never contains the session ID.
73
+ * or, if none did, someone else holding the session ID. Default: console.warn, naming
74
+ * the key only by a digest, since a key can hold a user's ID. The error never
75
+ * contains the session ID. A callback that throws, or returns a promise that
76
+ * rejects, is logged the same way; it never stops a close or the next open.
42
77
  */
43
- onCloseError?: (key: string, err: unknown) => void;
78
+ onCloseError?: (key: SandboxKey, err: unknown) => void;
44
79
  };
45
80
  /** What the executeCode tool takes. */
46
81
  export type ExecuteCodeInput = {
@@ -114,13 +149,29 @@ export declare class CodeSandboxes {
114
149
  persistent(): Promise<boolean>;
115
150
  /**
116
151
  * Starts creating the key's sandbox and returns at once. A failure here is
117
- * not lost: the next run under the key opens a session again.
152
+ * not lost: the next run under the key opens a session again. A warm is a guess
153
+ * (a new chat the user may never type in), so it never closes another sandbox to
154
+ * make room: with the key's owner, or this instance, at its cap it does nothing,
155
+ * and the key's first call opens the sandbox, closing the least recently used idle
156
+ * one then. On a daemon with SANDBOX_MAX_SESSIONS_PER_OWNER a warm that opens is an
157
+ * open like any other: the daemon counts it, and at its cap closes that user's least
158
+ * recently used session there.
159
+ */
160
+ warm(key: SandboxKey): void;
161
+ /**
162
+ * Runs one call of the tool under the key, or under the key a function returns (an
163
+ * add-on reading it from its framework's context, so a failure to find it is this
164
+ * call's refusal). Every error it throws is a PlimsollError whose message starts with
165
+ * whether the code ran: "Nothing ran (<reason>): " when it carries a not-dispatched
166
+ * mark, "The code may have run, and there is no checked answer for it: " otherwise. A
167
+ * failure before the code was sent (the key, the client, the Describe, the sandbox's
168
+ * open) is marked, reason environment unless the daemon gave one. A framework hands
169
+ * its model only the message (Mastra's error-text, the AI SDK's tool error), and that
170
+ * is the one thing the model must not guess.
118
171
  */
119
- warm(key: string): void;
120
- /** Runs one call of the tool under the key. */
121
- run(key: string | undefined, input: ExecuteCodeInput, signal?: AbortSignal): Promise<ExecuteCodeOutput>;
172
+ run(key: SandboxKey | undefined | (() => SandboxKey | undefined), input: ExecuteCodeInput, signal?: AbortSignal): Promise<ExecuteCodeOutput>;
122
173
  /** Closes the key's sandbox, if it has one. Safe to call more than once. */
123
- dispose(key: string): Promise<void>;
174
+ dispose(key: SandboxKey): Promise<void>;
124
175
  /** Closes every sandbox this instance holds, for shutdown. */
125
176
  disposeAll(): Promise<void>;
126
177
  }