@plimsollmark/client 0.14.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/LICENSE +201 -0
- package/README.md +177 -0
- package/dist/client.d.ts +278 -0
- package/dist/client.js +528 -0
- package/dist/errors.d.ts +42 -0
- package/dist/errors.js +132 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +3 -0
- package/dist/mastra.d.ts +27 -0
- package/dist/mastra.js +30 -0
- package/dist/record.d.ts +78 -0
- package/dist/record.js +250 -0
- package/dist/sandboxes.d.ts +116 -0
- package/dist/sandboxes.js +316 -0
- package/dist/tool.d.ts +31 -0
- package/dist/tool.js +49 -0
- package/dist/trigger.d.ts +17 -0
- package/dist/trigger.js +29 -0
- package/dist/wire.d.ts +187 -0
- package/dist/wire.js +6 -0
- package/package.json +76 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright 2026 Carroll Guertin
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
# @plimsollmark/client
|
|
2
|
+
|
|
3
|
+
A TypeScript client for `plimsolld`, plus two add-ons that give a chat agent an
|
|
4
|
+
`executeCode` tool backed by plimsoll: one for [Trigger.dev](https://trigger.dev) chat
|
|
5
|
+
agents and one for [Mastra](https://mastra.ai) agents. Both add-ons sit on the same
|
|
6
|
+
client and the same per-conversation sandbox layer, so they behave the same way.
|
|
7
|
+
|
|
8
|
+
The client has no runtime dependencies (Node >= 22.18). It speaks Connect's JSON
|
|
9
|
+
protocol over `fetch` and keeps the promises the Go client keeps:
|
|
10
|
+
|
|
11
|
+
- it states the protocol number on every request;
|
|
12
|
+
- it checks every answer's **run record**: it recomputes the request and result digests
|
|
13
|
+
from what it sent and received ([run-records.md](https://github.com/plimsollmark/plimsoll/blob/main/docs/run-records.md)), and an
|
|
14
|
+
answer that does not check, or has no record, is a `data_loss` error with the result
|
|
15
|
+
attached, because the code may have run;
|
|
16
|
+
- it checks the reported isolation tier against the caller's floor the same way;
|
|
17
|
+
- in a **session** it tracks the chain of records, so a call made by anyone else who holds
|
|
18
|
+
the session ID is caught at the next call, and at close;
|
|
19
|
+
- an error says whether anything ran: `notDispatched` is set only when the daemon stated
|
|
20
|
+
that nothing did, and only then is a retry safe.
|
|
21
|
+
|
|
22
|
+
The tier it reports is configuration and provider evidence, never runtime attestation
|
|
23
|
+
(the README's provider table says what each tier means).
|
|
24
|
+
|
|
25
|
+
## Install
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npm install @plimsollmark/client
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
The package's version is the plimsoll release it ships in: version X.Y.Z was built and
|
|
32
|
+
tested against plimsolld vX.Y.Z. The add-ons need their framework installed beside it:
|
|
33
|
+
`@trigger.dev/sdk`, `ai` and `zod` for `/trigger`, `@mastra/core` and `zod` for `/mastra`.
|
|
34
|
+
|
|
35
|
+
## The client
|
|
36
|
+
|
|
37
|
+
```ts
|
|
38
|
+
import { PlimsollClient } from "@plimsollmark/client";
|
|
39
|
+
|
|
40
|
+
const plimsoll = new PlimsollClient({ baseUrl: "https://plimsoll.internal:8443", token: process.env.PLIMSOLL_TOKEN });
|
|
41
|
+
|
|
42
|
+
const info = await plimsoll.describe(); // provider, tier, supportsSessions, ...
|
|
43
|
+
const r = await plimsoll.runJavaScript("console.log(6 * 7)", { minimumIsolation: "container", timeoutMs: 10_000 });
|
|
44
|
+
r.stdout; // "42\n"; a non-zero r.exitCode is a normal result, not an error
|
|
45
|
+
|
|
46
|
+
const s = await plimsoll.openSession({ minimumIsolation: "container" }); // docker or openshell
|
|
47
|
+
await s.runProject({ files: [{ path: "a.js", content: "console.log(1)" }], steps: ["node a.js"] });
|
|
48
|
+
await s.runJavaScript("console.log(require('fs').readdirSync('.'))");
|
|
49
|
+
// A cell runs in an interpreter the session keeps alive: what it defines is there next time.
|
|
50
|
+
await s.runCell({ language: "python", code: "import csv\nrows = list(csv.reader(open('t.csv')))", files: [{ path: "t.csv", content: "a,b\n1,2\n" }] });
|
|
51
|
+
(await s.runCell({ language: "python", code: "len(rows)" })).stdout; // "2\n": the last expression is printed
|
|
52
|
+
await s.close(); // checks the daemon's call count against the chain this client saw
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
A cell's `files` are written into the session's work directory, the interpreter's working
|
|
56
|
+
directory, before the code runs. Its result adds `interpreterStarted` (this call began with
|
|
57
|
+
a fresh interpreter, so nothing defined earlier exists) and `interpreterEnded`; its exit code
|
|
58
|
+
is 0 when the code ran, 1 when it raised and 124 at the deadline. Python needs `python3` in
|
|
59
|
+
the daemon's image (`plimsoll/sandbox-python` has it, with NumPy and SciPy);
|
|
60
|
+
`describe()` lists the languages the daemon proved on `environments.project.languages`.
|
|
61
|
+
|
|
62
|
+
Cleartext HTTP is accepted only to a loopback address unless you pass `insecureHttp: true`.
|
|
63
|
+
|
|
64
|
+
## `executeCode` for an agent: `CodeSandboxes`
|
|
65
|
+
|
|
66
|
+
The `executeCode` tool takes `code`, a `language` (`"python"` or `"javascript"`; the
|
|
67
|
+
`languages` option sets which the tool offers and the default, Python first unless you
|
|
68
|
+
say otherwise) and optional `files`: text files written into the working directory before
|
|
69
|
+
the code runs, so the model can hand the code another tool's output by path instead of
|
|
70
|
+
pasting it into the source. The value of the code's last expression is printed, as in a
|
|
71
|
+
notebook.
|
|
72
|
+
|
|
73
|
+
Every call of a conversation runs in the same sandbox, so code one call ran can change what
|
|
74
|
+
later calls see: a call's output is only as trustworthy as the calls before it in that
|
|
75
|
+
conversation
|
|
76
|
+
([what a session gives up](https://github.com/plimsollmark/plimsoll/blob/main/docs/sessions.md#what-a-session-gives-up)).
|
|
77
|
+
The tool's calls are cells, which carry no grant, so no API access is shared between them.
|
|
78
|
+
|
|
79
|
+
`CodeSandboxes` keeps one sandbox per conversation key. With `sessions: "auto"` (the
|
|
80
|
+
default) it asks the daemon once whether it keeps sessions:
|
|
81
|
+
|
|
82
|
+
- **It does** (`docker` with a project image, or `openshell`, with `SANDBOX_MAX_SESSIONS`
|
|
83
|
+
set): a key holds one plimsoll session, and every call is a cell in that session's
|
|
84
|
+
interpreter. Variables, imports and loaded data from earlier calls are still defined, and
|
|
85
|
+
files stay in the working directory. On one laptop on 2026-10-01, a call that used an
|
|
86
|
+
80 MB array an earlier call had loaded took 10 ms on OpenShell and 23 ms on docker
|
|
87
|
+
under gVisor (medians), against 939 ms and 714 ms for a fresh sandbox that rebuilt it.
|
|
88
|
+
- **It does not** (`e2b`, `dockercloud`, `wasm`, or `sessions: "never"`): every call runs in
|
|
89
|
+
a fresh sandbox, as a small project whose runner prints the last expression the same way,
|
|
90
|
+
and nothing persists. `wasm` runs no projects, so there a call is a JavaScript snippet
|
|
91
|
+
printing its last value; Python and files are refused before anything is sent.
|
|
92
|
+
|
|
93
|
+
The tool's output says which: `stateKept` (this call's interpreter was still running when
|
|
94
|
+
it answered, so what it defined can be there for the next call; false when no interpreter is
|
|
95
|
+
kept, and when the call's deadline or the sandbox's end ended it) and `filesPersist` (the same
|
|
96
|
+
for its sandbox and files). Neither promises the next call anything: the sandbox can still end
|
|
97
|
+
between calls, at the end of its lifetime or when the disk check after a call finds it over
|
|
98
|
+
its limit. So the output also says what is new, rather than what was lost, because
|
|
99
|
+
only the sandbox knows: `freshInterpreter` whenever the call's interpreter had just started
|
|
100
|
+
(the first call in a language, a deadline, a crash), so nothing earlier calls defined
|
|
101
|
+
exists, and `freshSandbox` whenever the call is the first answered one in a newly opened
|
|
102
|
+
sandbox, so no earlier file is there either. That covers a sandbox that `dispose` or the
|
|
103
|
+
idle close let go, one that ended by itself (its lifetime, its disk budget), and a
|
|
104
|
+
conversation resumed in another process, which this instance cannot tell from a first
|
|
105
|
+
call. A call without a key (no thread, no run) always runs fresh: one sandbox shared
|
|
106
|
+
across conversations would show one user another's files. A language the daemon's image
|
|
107
|
+
cannot run is refused before anything is sent.
|
|
108
|
+
|
|
109
|
+
Closing a sandbox checks the daemon's count of calls against the chain the client saw; a
|
|
110
|
+
mismatch means someone else held the session ID, and it goes to `onCloseError` (by default
|
|
111
|
+
a warning) rather than being dropped at teardown.
|
|
112
|
+
|
|
113
|
+
The session ID is a capability. It lives in the client's memory and nowhere else: not in
|
|
114
|
+
a log line, not in a record, not in Trigger.dev's `chat.local` (which is serialized into
|
|
115
|
+
subtask metadata). Records carry its SHA-256.
|
|
116
|
+
|
|
117
|
+
## Trigger.dev
|
|
118
|
+
|
|
119
|
+
The add-on follows Trigger.dev's own
|
|
120
|
+
[code-sandbox recipe](https://trigger.dev/docs/ai-chat/patterns/code-sandbox): warm in
|
|
121
|
+
`onTurnStart`, reuse in `executeCode`, dispose in `onChatSuspend` (right before the run
|
|
122
|
+
sleeps) and in `onComplete`.
|
|
123
|
+
|
|
124
|
+
```ts
|
|
125
|
+
import { chat } from "@trigger.dev/sdk/ai";
|
|
126
|
+
import { PlimsollClient } from "@plimsollmark/client";
|
|
127
|
+
import { plimsollCodeSandbox } from "@plimsollmark/client/trigger";
|
|
128
|
+
|
|
129
|
+
const sandbox = plimsollCodeSandbox({
|
|
130
|
+
client: () => new PlimsollClient({ baseUrl: process.env.PLIMSOLL_URL!, token: process.env.PLIMSOLL_TOKEN }),
|
|
131
|
+
minimumIsolation: "container",
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
export const codeChat = chat.agent({
|
|
135
|
+
id: "code-chat",
|
|
136
|
+
tools: { executeCode: sandbox.executeCode },
|
|
137
|
+
onTurnStart: async ({ runId }) => sandbox.warm(runId),
|
|
138
|
+
onChatSuspend: async ({ runId }) => sandbox.dispose(runId),
|
|
139
|
+
onComplete: async ({ ctx }) => sandbox.dispose(ctx.run.id),
|
|
140
|
+
run: async ({ messages, tools, signal, streamText }) => streamText({ model, messages, tools, abortSignal: signal }),
|
|
141
|
+
});
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
The full example, with a test that drives real turns through Trigger.dev's `mockChatAgent`:
|
|
145
|
+
[examples/trigger-chat](https://github.com/plimsollmark/plimsoll/tree/main/examples/trigger-chat/).
|
|
146
|
+
|
|
147
|
+
## Mastra
|
|
148
|
+
|
|
149
|
+
```ts
|
|
150
|
+
import { Agent } from "@mastra/core/agent";
|
|
151
|
+
import { PlimsollClient } from "@plimsollmark/client";
|
|
152
|
+
import { plimsollExecuteCode } from "@plimsollmark/client/mastra";
|
|
153
|
+
|
|
154
|
+
const { executeCode, dispose } = plimsollExecuteCode({
|
|
155
|
+
client: new PlimsollClient({ baseUrl: process.env.PLIMSOLL_URL!, token: process.env.PLIMSOLL_TOKEN }),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
export const analyst = new Agent({ id: "analyst", name: "analyst", instructions: "...", model, tools: { executeCode } });
|
|
159
|
+
// When a conversation ends: await dispose(threadId, resourceId);
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
The sandbox is keyed by Mastra's thread and resource (a thread ID is unique only within
|
|
163
|
+
its user). Mastra has no hook for "this conversation is going to sleep", so a thread's
|
|
164
|
+
sandbox closes on `dispose`, or after `idleCloseMs` (default 10 minutes) without a call.
|
|
165
|
+
|
|
166
|
+
## Tests
|
|
167
|
+
|
|
168
|
+
`go test ./clients/typescript/` serves the real RPC handler (the wasm provider for single
|
|
169
|
+
runs, an in-memory session provider for sessions and cells, and a provider that answers a
|
|
170
|
+
project with the plan it received, for the project a fresh call sends) and runs three
|
|
171
|
+
suites against them: the
|
|
172
|
+
client's (needs only node), the add-ons' (needs `npm install` here), and the Trigger.dev
|
|
173
|
+
example's (needs `npm install` in `examples/trigger-chat`; it waits out Trigger.dev's
|
|
174
|
+
30-second idle window to prove the suspend closes the sandbox). A suite whose
|
|
175
|
+
dependencies are absent is skipped. The client's digests are also checked against the
|
|
176
|
+
golden vectors of `record/record_test.go`, and the fresh-run runners are run on this
|
|
177
|
+
machine's `node` and `python3` to check they print the last expression.
|
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
import { type RunRecord } from "./record.ts";
|
|
2
|
+
import { type SessionEnd } from "./errors.ts";
|
|
3
|
+
import type { WireCellRun, WireEnvelope, WireOpenSessionResponse, WireSoftwareRule } from "./wire.ts";
|
|
4
|
+
/** The wire protocol number this client speaks (protocol.Number in Go). */
|
|
5
|
+
export declare const PROTOCOL = 2;
|
|
6
|
+
/** How strong the boundary is, weakest first. */
|
|
7
|
+
export type Isolation = "none" | "process" | "container" | "kernel" | "vm";
|
|
8
|
+
/** Whether a tier the daemon reported meets a floor. An unknown tier meets nothing. */
|
|
9
|
+
export declare function meets(actual: string, floor: Isolation): boolean;
|
|
10
|
+
/** A caller's rule for the selected software: one exact identity, or 1 to 32 approved ones. */
|
|
11
|
+
export type SoftwareRule = {
|
|
12
|
+
mode: "exact" | "approved";
|
|
13
|
+
identities: string[];
|
|
14
|
+
};
|
|
15
|
+
export type ClientOptions = {
|
|
16
|
+
/** The daemon's absolute http(s) URL. Cleartext to a non-loopback host needs insecureHttp. */
|
|
17
|
+
baseUrl: string;
|
|
18
|
+
/** Bearer token with the code:run scope. Omit for an open development daemon. */
|
|
19
|
+
token?: string;
|
|
20
|
+
/** Explicitly permits cleartext HTTP to a non-loopback host. Development only. */
|
|
21
|
+
insecureHttp?: boolean;
|
|
22
|
+
/** Named server-side grant profile for snippets (PLIMSOLL_GRANTS_FILE). Empty = no network. */
|
|
23
|
+
javascriptGrantProfile?: string;
|
|
24
|
+
/** Named server-side grant profile for projects. */
|
|
25
|
+
projectGrantProfile?: string;
|
|
26
|
+
/** A fetch implementation; defaults to the global one. */
|
|
27
|
+
fetch?: typeof fetch;
|
|
28
|
+
/**
|
|
29
|
+
* Bounds each HTTP exchange, sending and reading the answer, in milliseconds.
|
|
30
|
+
* Default 6 minutes, as the Go and Python clients. A run's timeoutMs bounds what
|
|
31
|
+
* the daemon executes, not a stalled connection; a call cut off here may have run.
|
|
32
|
+
*/
|
|
33
|
+
requestTimeoutMs?: number;
|
|
34
|
+
};
|
|
35
|
+
export type RunOptions = {
|
|
36
|
+
/** Whole-run budget; defaulted and clamped by the daemon. */
|
|
37
|
+
timeoutMs?: number;
|
|
38
|
+
/** The weakest tier this run accepts, checked before dispatch and again on the answer. */
|
|
39
|
+
minimumIsolation?: Exclude<Isolation, "none">;
|
|
40
|
+
software?: SoftwareRule;
|
|
41
|
+
/** An opaque correlation id for the daemon's audit line: [A-Za-z0-9._:-]{1,64}, else dropped. */
|
|
42
|
+
traceId?: string;
|
|
43
|
+
signal?: AbortSignal;
|
|
44
|
+
};
|
|
45
|
+
/** Evidence every answered run carries: configuration and provider evidence, never attestation. */
|
|
46
|
+
export type Evidence = {
|
|
47
|
+
sandbox: string;
|
|
48
|
+
isolation: string;
|
|
49
|
+
softwareIdentity: string;
|
|
50
|
+
environment: string;
|
|
51
|
+
durationMs: number;
|
|
52
|
+
/** The checked run record. */
|
|
53
|
+
record: RunRecord;
|
|
54
|
+
};
|
|
55
|
+
export type AdviceFinding = {
|
|
56
|
+
pattern: string;
|
|
57
|
+
severity: string;
|
|
58
|
+
remedy: string;
|
|
59
|
+
method: string;
|
|
60
|
+
route: string;
|
|
61
|
+
detail: string;
|
|
62
|
+
suggestedMethod: string;
|
|
63
|
+
suggestedRoute: string;
|
|
64
|
+
extraCalls: number;
|
|
65
|
+
addedLatencyMs: number;
|
|
66
|
+
bytesMoved: number;
|
|
67
|
+
};
|
|
68
|
+
export type JavaScriptResult = Evidence & {
|
|
69
|
+
/** Guest output decoded as UTF-8 (invalid sequences replaced); the raw bytes are beside it. */
|
|
70
|
+
stdout: string;
|
|
71
|
+
stderr: string;
|
|
72
|
+
stdoutBytes: Uint8Array;
|
|
73
|
+
stderrBytes: Uint8Array;
|
|
74
|
+
/** A non-zero exit code is a normal result: the guest's code failed. */
|
|
75
|
+
exitCode: number;
|
|
76
|
+
timedOut: boolean;
|
|
77
|
+
stdoutTruncated: boolean;
|
|
78
|
+
stderrTruncated: boolean;
|
|
79
|
+
advice: AdviceFinding[];
|
|
80
|
+
};
|
|
81
|
+
export type ProjectRequest = {
|
|
82
|
+
files: {
|
|
83
|
+
path: string;
|
|
84
|
+
content: string;
|
|
85
|
+
}[];
|
|
86
|
+
/** Shell commands run in order; the run stops at the first that fails. */
|
|
87
|
+
steps: string[];
|
|
88
|
+
/** Relative paths to capture and return. */
|
|
89
|
+
artifacts?: string[];
|
|
90
|
+
};
|
|
91
|
+
export type ProjectOutcome = "unspecified" | "completed" | "setup_failed" | "timed_out" | "protocol_error";
|
|
92
|
+
export type StepResult = {
|
|
93
|
+
command: string;
|
|
94
|
+
stdout: string;
|
|
95
|
+
stderr: string;
|
|
96
|
+
exitCode: number;
|
|
97
|
+
timedOut: boolean;
|
|
98
|
+
durationMs: number;
|
|
99
|
+
stdoutTruncated: boolean;
|
|
100
|
+
stderrTruncated: boolean;
|
|
101
|
+
};
|
|
102
|
+
export type ProjectResult = Evidence & {
|
|
103
|
+
outcome: ProjectOutcome;
|
|
104
|
+
detail: string;
|
|
105
|
+
steps: StepResult[];
|
|
106
|
+
artifacts: {
|
|
107
|
+
path: string;
|
|
108
|
+
content: Uint8Array;
|
|
109
|
+
}[];
|
|
110
|
+
artifactsTruncated: boolean;
|
|
111
|
+
advice: AdviceFinding[];
|
|
112
|
+
};
|
|
113
|
+
/** An interpreter language: a cell's, and one an environment states. */
|
|
114
|
+
export type Language = "javascript" | "python";
|
|
115
|
+
/**
|
|
116
|
+
* Where one payload kind runs. languages are the interpreters the daemon's startup
|
|
117
|
+
* checks proved: what a project's steps can invoke, and in a session what a cell may use.
|
|
118
|
+
*/
|
|
119
|
+
export type PayloadEnvironment = {
|
|
120
|
+
identity: string;
|
|
121
|
+
softwareIdentity: string;
|
|
122
|
+
maxTimeoutMs: number;
|
|
123
|
+
languages: Language[];
|
|
124
|
+
};
|
|
125
|
+
/** A cell: code for the session's interpreter, with files written into the work directory first. */
|
|
126
|
+
export type CellRequest = {
|
|
127
|
+
language: Language;
|
|
128
|
+
code: string;
|
|
129
|
+
files?: {
|
|
130
|
+
path: string;
|
|
131
|
+
content: string;
|
|
132
|
+
}[];
|
|
133
|
+
};
|
|
134
|
+
export type CellResult = Evidence & {
|
|
135
|
+
stdout: string;
|
|
136
|
+
stderr: string;
|
|
137
|
+
stdoutBytes: Uint8Array;
|
|
138
|
+
stderrBytes: Uint8Array;
|
|
139
|
+
/** 0: the code ran; 1: it raised (the error is on stderr); 124: the deadline ended it. */
|
|
140
|
+
exitCode: number;
|
|
141
|
+
timedOut: boolean;
|
|
142
|
+
stdoutTruncated: boolean;
|
|
143
|
+
stderrTruncated: boolean;
|
|
144
|
+
/** This call started a fresh interpreter: nothing an earlier cell defined exists. */
|
|
145
|
+
interpreterStarted: boolean;
|
|
146
|
+
/** The interpreter ended during this call; the next cell starts a fresh one. */
|
|
147
|
+
interpreterEnded: boolean;
|
|
148
|
+
};
|
|
149
|
+
export type Info = {
|
|
150
|
+
sandbox: string;
|
|
151
|
+
isolation: string;
|
|
152
|
+
protocol: number;
|
|
153
|
+
supportsProject: boolean;
|
|
154
|
+
supportsModule: boolean;
|
|
155
|
+
supportsJavaScriptGrants: boolean;
|
|
156
|
+
supportsProjectGrants: boolean;
|
|
157
|
+
supportsSessions: boolean;
|
|
158
|
+
sessionLifetimeMs: number;
|
|
159
|
+
sessionIdleTimeoutMs: number;
|
|
160
|
+
environments: {
|
|
161
|
+
javascript: PayloadEnvironment;
|
|
162
|
+
project: PayloadEnvironment;
|
|
163
|
+
module: PayloadEnvironment;
|
|
164
|
+
policy: string;
|
|
165
|
+
};
|
|
166
|
+
resources: {
|
|
167
|
+
memoryMb: number;
|
|
168
|
+
cpus: number;
|
|
169
|
+
pids: number;
|
|
170
|
+
diskMb: number;
|
|
171
|
+
};
|
|
172
|
+
};
|
|
173
|
+
export type SessionOptions = {
|
|
174
|
+
minimumIsolation?: Exclude<Isolation, "none">;
|
|
175
|
+
software?: SoftwareRule;
|
|
176
|
+
/** Shorter than the daemon's own, or omitted for the daemon's. */
|
|
177
|
+
lifetimeMs?: number;
|
|
178
|
+
idleTimeoutMs?: number;
|
|
179
|
+
traceId?: string;
|
|
180
|
+
signal?: AbortSignal;
|
|
181
|
+
};
|
|
182
|
+
export type SessionSummary = {
|
|
183
|
+
session: string;
|
|
184
|
+
calls: bigint;
|
|
185
|
+
lastRecordSha256: string;
|
|
186
|
+
end: SessionEnd;
|
|
187
|
+
};
|
|
188
|
+
type Payload = {
|
|
189
|
+
javascript: {
|
|
190
|
+
code: string;
|
|
191
|
+
grantProfile?: string;
|
|
192
|
+
};
|
|
193
|
+
} | {
|
|
194
|
+
project: {
|
|
195
|
+
files: ProjectRequest["files"];
|
|
196
|
+
steps: string[];
|
|
197
|
+
artifacts: string[];
|
|
198
|
+
grantProfile?: string;
|
|
199
|
+
};
|
|
200
|
+
} | {
|
|
201
|
+
cell: WireCellRun & {
|
|
202
|
+
files: {
|
|
203
|
+
path: string;
|
|
204
|
+
content: string;
|
|
205
|
+
}[];
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
export declare class PlimsollClient {
|
|
209
|
+
private readonly base;
|
|
210
|
+
private readonly token;
|
|
211
|
+
private readonly fetchImpl;
|
|
212
|
+
private readonly requestTimeoutMs;
|
|
213
|
+
/** @internal */ readonly jsGrant: string;
|
|
214
|
+
/** @internal */ readonly projectGrant: string;
|
|
215
|
+
constructor(opts: ClientOptions);
|
|
216
|
+
/** @internal One unary Connect call with the JSON codec. */
|
|
217
|
+
call<T>(method: string, body: unknown, signal?: AbortSignal): Promise<T>;
|
|
218
|
+
/** The daemon's own statement of its provider, tier and what it supports. */
|
|
219
|
+
describe(signal?: AbortSignal): Promise<Info>;
|
|
220
|
+
/** Runs a JavaScript snippet in a fresh sandbox. */
|
|
221
|
+
runJavaScript(code: string, opts?: RunOptions): Promise<JavaScriptResult>;
|
|
222
|
+
/** Writes a multi-file project into a fresh sandbox and runs its steps in order. */
|
|
223
|
+
runProject(req: ProjectRequest, opts?: RunOptions): Promise<ProjectResult>;
|
|
224
|
+
/**
|
|
225
|
+
* Opens a session: one sandbox kept for many calls, files persisting, and of
|
|
226
|
+
* processes only the interpreters runCell keeps. Only on a daemon whose Describe
|
|
227
|
+
* states supportsSessions.
|
|
228
|
+
*/
|
|
229
|
+
openSession(opts?: SessionOptions): Promise<Session>;
|
|
230
|
+
/** @internal */
|
|
231
|
+
envelope(opts: RunOptions, rule: WireSoftwareRule | undefined): WireEnvelope;
|
|
232
|
+
private exchange;
|
|
233
|
+
}
|
|
234
|
+
/** @internal */
|
|
235
|
+
export declare function projectPayload(req: ProjectRequest, grantProfile: string): Payload;
|
|
236
|
+
/**
|
|
237
|
+
* An open session. Calls are serialized here as they are on the daemon, so the
|
|
238
|
+
* chain this client tracks follows the daemon's; a record that does not continue
|
|
239
|
+
* it means someone else holding the session ID made a call.
|
|
240
|
+
*/
|
|
241
|
+
export declare class Session {
|
|
242
|
+
#private;
|
|
243
|
+
/** The SHA-256 of the session ID, as the session's records carry it. */
|
|
244
|
+
readonly fingerprint: string;
|
|
245
|
+
/** The tier the daemon measured when the session opened. */
|
|
246
|
+
readonly isolation: string;
|
|
247
|
+
readonly sandbox: string;
|
|
248
|
+
readonly expiresAtMs: number;
|
|
249
|
+
readonly idleTimeoutMs: number;
|
|
250
|
+
/** @internal */
|
|
251
|
+
constructor(client: PlimsollClient, m: WireOpenSessionResponse, rule: WireSoftwareRule | undefined);
|
|
252
|
+
/** The session's end once a response reported it, else undefined. */
|
|
253
|
+
get ended(): {
|
|
254
|
+
reason: SessionEnd;
|
|
255
|
+
detail: string;
|
|
256
|
+
} | undefined;
|
|
257
|
+
/** The number of calls this client has seen executed, and the last record's digest. */
|
|
258
|
+
get chain(): {
|
|
259
|
+
calls: bigint;
|
|
260
|
+
lastRecordSha256: string;
|
|
261
|
+
};
|
|
262
|
+
runJavaScript(code: string, opts?: RunOptions): Promise<JavaScriptResult>;
|
|
263
|
+
/** Runs a project in the session; its files persist for later calls. */
|
|
264
|
+
runProject(req: ProjectRequest, opts?: RunOptions): Promise<ProjectResult>;
|
|
265
|
+
/**
|
|
266
|
+
* Runs code in the session's interpreter for req.language, which keeps what
|
|
267
|
+
* earlier cells defined unless the result says a fresh one started. req.files are
|
|
268
|
+
* written into the session's work directory, the interpreter's working directory,
|
|
269
|
+
* before the code runs. A cell carries no grant.
|
|
270
|
+
*/
|
|
271
|
+
runCell(req: CellRequest, opts?: RunOptions): Promise<CellResult>;
|
|
272
|
+
/**
|
|
273
|
+
* Ends the session (or collects one that ended by itself) and checks the
|
|
274
|
+
* daemon's count of executed calls against the chain this client saw.
|
|
275
|
+
*/
|
|
276
|
+
close(signal?: AbortSignal): Promise<SessionSummary>;
|
|
277
|
+
}
|
|
278
|
+
export {};
|