@plimsollmark/client 0.16.0 → 0.17.1
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 +4 -2
- package/dist/client.d.ts +8 -0
- package/dist/client.js +81 -35
- package/dist/errors.d.ts +0 -1
- package/dist/errors.js +11 -3
- package/dist/mastra.js +3 -2
- package/dist/sandboxes.js +3 -0
- package/dist/wire.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -200,7 +200,9 @@ project with the plan it received, for the project a fresh call sends) and runs
|
|
|
200
200
|
suites against them: the
|
|
201
201
|
client's (needs only node), the add-ons' (needs `npm install` here), and the Trigger.dev
|
|
202
202
|
example's (needs `npm install` in `examples/trigger-chat`; it waits out Trigger.dev's
|
|
203
|
-
30-second idle window to prove the suspend closes the sandbox).
|
|
204
|
-
|
|
203
|
+
30-second idle window to prove the suspend closes the sandbox). An ordinary Go test
|
|
204
|
+
skips a suite whose runtime or dependencies are absent. CI runs `make clients-suite`,
|
|
205
|
+
which installs both npm lockfiles and sets `PLIMSOLL_REQUIRE_CLIENTS=1`, so a missing
|
|
206
|
+
prerequisite or skipped Go test fails. The client's digests are also checked against the
|
|
205
207
|
golden vectors of `record/record_test.go`, and the fresh-run runners are run on this
|
|
206
208
|
machine's `node` and `python3` to check they print the last expression.
|
package/dist/client.d.ts
CHANGED
|
@@ -179,6 +179,12 @@ export type SessionOptions = {
|
|
|
179
179
|
lifetimeMs?: number;
|
|
180
180
|
idleTimeoutMs?: number;
|
|
181
181
|
traceId?: string;
|
|
182
|
+
/**
|
|
183
|
+
* The languages the session's cells will use: a hint, so a daemon with a warm pool
|
|
184
|
+
* hands over a sandbox with those interpreters already running. A cell in any
|
|
185
|
+
* language the daemon states still runs.
|
|
186
|
+
*/
|
|
187
|
+
languages?: Language[];
|
|
182
188
|
signal?: AbortSignal;
|
|
183
189
|
};
|
|
184
190
|
export type SessionSummary = {
|
|
@@ -219,6 +225,7 @@ export declare class PlimsollClient {
|
|
|
219
225
|
call<T>(method: string, body: unknown, signal?: AbortSignal): Promise<T>;
|
|
220
226
|
/** The daemon's own statement of its provider, tier and what it supports. */
|
|
221
227
|
describe(signal?: AbortSignal): Promise<Info>;
|
|
228
|
+
private info;
|
|
222
229
|
/** Runs a JavaScript snippet in a fresh sandbox. */
|
|
223
230
|
runJavaScript(code: string, opts?: RunOptions): Promise<JavaScriptResult>;
|
|
224
231
|
/** Writes a multi-file project into a fresh sandbox and runs its steps in order. */
|
|
@@ -232,6 +239,7 @@ export declare class PlimsollClient {
|
|
|
232
239
|
/** @internal */
|
|
233
240
|
envelope(opts: RunOptions, rule: WireSoftwareRule | undefined): WireEnvelope;
|
|
234
241
|
private exchange;
|
|
242
|
+
private checked;
|
|
235
243
|
}
|
|
236
244
|
/** @internal */
|
|
237
245
|
export declare function projectPayload(req: ProjectRequest, grantProfile: string): Payload;
|
package/dist/client.js
CHANGED
|
@@ -173,6 +173,36 @@ function cellResult(resp, record) {
|
|
|
173
173
|
interpreterEnded: c.interpreterEnded ?? false,
|
|
174
174
|
};
|
|
175
175
|
}
|
|
176
|
+
// A lone surrogate has no UTF-8 form, so no protobuf string can hold one, though JSON
|
|
177
|
+
// carries it as an escape: the daemon would refuse the request after it arrived, with
|
|
178
|
+
// no not-dispatched mark. Every request is checked here before it is sent instead.
|
|
179
|
+
function refuseIllFormed(v, at) {
|
|
180
|
+
if (typeof v === "string") {
|
|
181
|
+
if (!v.isWellFormed()) {
|
|
182
|
+
throw new PlimsollError("invalid_argument", `plimsoll: ${at} is not well-formed text (a lone surrogate)`, { notDispatched: "request" });
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
else if (Array.isArray(v)) {
|
|
186
|
+
v.forEach((x, i) => refuseIllFormed(x, `${at}[${i}]`));
|
|
187
|
+
}
|
|
188
|
+
else if (v !== null && typeof v === "object") {
|
|
189
|
+
for (const [k, x] of Object.entries(v))
|
|
190
|
+
refuseIllFormed(x, `${at}.${k}`);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
// Reads an answer the daemon gave. A broken or hostile daemon can answer in any shape, so
|
|
194
|
+
// anything but a PlimsollError thrown while it is checked or decoded (a TypeError from a
|
|
195
|
+
// number where bytes belong, say) is data loss: the call may have run.
|
|
196
|
+
function decoded(read) {
|
|
197
|
+
try {
|
|
198
|
+
return read();
|
|
199
|
+
}
|
|
200
|
+
catch (e) {
|
|
201
|
+
if (e instanceof PlimsollError)
|
|
202
|
+
throw e;
|
|
203
|
+
throw new PlimsollError("data_loss", `plimsoll: the daemon's answer is malformed (${e instanceof Error ? e.message : String(e)})`, { cause: e });
|
|
204
|
+
}
|
|
205
|
+
}
|
|
176
206
|
function digestPayload(p) {
|
|
177
207
|
if ("javascript" in p)
|
|
178
208
|
return { kind: "javascript", code: p.javascript.code, grantProfile: p.javascript.grantProfile ?? "" };
|
|
@@ -205,6 +235,7 @@ export class PlimsollClient {
|
|
|
205
235
|
// this process, so it is a refusal, not a call that may have run.
|
|
206
236
|
if (signal?.aborted)
|
|
207
237
|
throw new PlimsollError("canceled", `plimsoll: ${method} was canceled before it was sent`, { notDispatched: "request", cause: signal.reason });
|
|
238
|
+
refuseIllFormed(body, "the request");
|
|
208
239
|
const headers = { "Content-Type": "application/json", "Connect-Protocol-Version": "1" };
|
|
209
240
|
if (this.token)
|
|
210
241
|
headers["Authorization"] = `Bearer ${this.token}`;
|
|
@@ -257,6 +288,9 @@ export class PlimsollClient {
|
|
|
257
288
|
/** The daemon's own statement of its provider, tier and what it supports. */
|
|
258
289
|
async describe(signal) {
|
|
259
290
|
const m = await this.call("Describe", {}, signal);
|
|
291
|
+
return decoded(() => this.info(m));
|
|
292
|
+
}
|
|
293
|
+
info(m) {
|
|
260
294
|
const env = (e) => ({
|
|
261
295
|
identity: e?.identity ?? "",
|
|
262
296
|
softwareIdentity: e?.softwareIdentity ?? "",
|
|
@@ -292,12 +326,12 @@ export class PlimsollClient {
|
|
|
292
326
|
/** Runs a JavaScript snippet in a fresh sandbox. */
|
|
293
327
|
async runJavaScript(code, opts = {}) {
|
|
294
328
|
const resp = await this.exchange("Run", this.envelope(opts, validateRule(opts.software)), { javascript: { code, grantProfile: this.jsGrant || undefined } }, opts);
|
|
295
|
-
return finish(resp.run, resp.record, javascriptResult, "javascript", opts.minimumIsolation);
|
|
329
|
+
return decoded(() => finish(resp.run, resp.record, javascriptResult, "javascript", opts.minimumIsolation));
|
|
296
330
|
}
|
|
297
331
|
/** Writes a multi-file project into a fresh sandbox and runs its steps in order. */
|
|
298
332
|
async runProject(req, opts = {}) {
|
|
299
333
|
const resp = await this.exchange("Run", this.envelope(opts, validateRule(opts.software)), projectPayload(req, this.projectGrant), opts);
|
|
300
|
-
return finish(resp.run, resp.record, projectResult, "project", opts.minimumIsolation);
|
|
334
|
+
return decoded(() => finish(resp.run, resp.record, projectResult, "project", opts.minimumIsolation));
|
|
301
335
|
}
|
|
302
336
|
/**
|
|
303
337
|
* Opens a session: one sandbox kept for many calls, files persisting, and of
|
|
@@ -306,6 +340,11 @@ export class PlimsollClient {
|
|
|
306
340
|
*/
|
|
307
341
|
async openSession(opts = {}) {
|
|
308
342
|
const rule = validateRule(opts.software);
|
|
343
|
+
for (const l of opts.languages ?? []) {
|
|
344
|
+
if (l !== "javascript" && l !== "python") {
|
|
345
|
+
throw new PlimsollError("invalid_argument", `plimsoll: unknown language ${JSON.stringify(l)} in the hint`, { notDispatched: "request" });
|
|
346
|
+
}
|
|
347
|
+
}
|
|
309
348
|
const m = await this.call("OpenSession", {
|
|
310
349
|
protocol: PROTOCOL,
|
|
311
350
|
minimumIsolation: opts.minimumIsolation,
|
|
@@ -313,9 +352,10 @@ export class PlimsollClient {
|
|
|
313
352
|
lifetimeMs: durationMs(opts.lifetimeMs) || undefined,
|
|
314
353
|
idleTimeoutMs: durationMs(opts.idleTimeoutMs) || undefined,
|
|
315
354
|
softwareRule: rule,
|
|
355
|
+
languages: opts.languages?.length ? [...new Set(opts.languages)] : undefined,
|
|
316
356
|
}, opts.signal);
|
|
317
|
-
const s = new Session(this, m, rule, opts.minimumIsolation);
|
|
318
|
-
if (s.fingerprint !== sessionFingerprint(m.sessionId ?? "")) {
|
|
357
|
+
const s = decoded(() => new Session(this, m, rule, opts.minimumIsolation));
|
|
358
|
+
if (decoded(() => s.fingerprint !== sessionFingerprint(m.sessionId ?? ""))) {
|
|
319
359
|
// The session exists on the daemon either way, and nothing else holds its ID.
|
|
320
360
|
await s.close().catch(() => undefined);
|
|
321
361
|
throw new PlimsollError("data_loss", "plimsoll: the daemon's session fingerprint does not match its session ID; the session was closed");
|
|
@@ -343,6 +383,9 @@ export class PlimsollClient {
|
|
|
343
383
|
// Sends one Run and checks its record.
|
|
344
384
|
async exchange(method, env, payload, opts) {
|
|
345
385
|
const resp = await this.call(method, { ...env, ...payload }, opts.signal);
|
|
386
|
+
return decoded(() => this.checked(env, payload, resp));
|
|
387
|
+
}
|
|
388
|
+
checked(env, payload, resp) {
|
|
346
389
|
const checked = checkRecord(requestDigest({ protocol: env.protocol, minimumIsolation: env.minimumIsolation ?? "", timeoutMs: env.timeoutMs ?? 0, softwareRule: env.softwareRule, payload: digestPayload(payload) }), env.protocol, env.softwareRule, resp);
|
|
347
390
|
if ("problem" in checked)
|
|
348
391
|
throw new PlimsollError("data_loss", `plimsoll: ${checked.problem}`, { result: resp });
|
|
@@ -440,13 +483,13 @@ export class Session {
|
|
|
440
483
|
async runJavaScript(code, opts = {}) {
|
|
441
484
|
opts = this.#withFloor(opts);
|
|
442
485
|
const run = await this.#call({ javascript: { code, grantProfile: this.#client.jsGrant || undefined } }, opts);
|
|
443
|
-
return finish(run.run, run.record, javascriptResult, "javascript", opts.minimumIsolation);
|
|
486
|
+
return decoded(() => finish(run.run, run.record, javascriptResult, "javascript", opts.minimumIsolation));
|
|
444
487
|
}
|
|
445
488
|
/** Runs a project in the session; its files persist for later calls. */
|
|
446
489
|
async runProject(req, opts = {}) {
|
|
447
490
|
opts = this.#withFloor(opts);
|
|
448
491
|
const run = await this.#call(projectPayload(req, this.#client.projectGrant), opts);
|
|
449
|
-
return finish(run.run, run.record, projectResult, "project", opts.minimumIsolation);
|
|
492
|
+
return decoded(() => finish(run.run, run.record, projectResult, "project", opts.minimumIsolation));
|
|
450
493
|
}
|
|
451
494
|
/**
|
|
452
495
|
* Runs code in the session's interpreter for req.language, which keeps what
|
|
@@ -461,7 +504,7 @@ export class Session {
|
|
|
461
504
|
opts = this.#withFloor(opts);
|
|
462
505
|
const files = (req.files ?? []).map((f) => ({ path: f.path, content: f.content }));
|
|
463
506
|
const run = await this.#call({ cell: { language: req.language, code: req.code, files } }, opts);
|
|
464
|
-
return finish(run.run, run.record, cellResult, "cell", opts.minimumIsolation);
|
|
507
|
+
return decoded(() => finish(run.run, run.record, cellResult, "cell", opts.minimumIsolation));
|
|
465
508
|
}
|
|
466
509
|
// The call's options with the session's floor, unless the call asks for more.
|
|
467
510
|
#withFloor(opts) {
|
|
@@ -483,6 +526,8 @@ export class Session {
|
|
|
483
526
|
throw new PlimsollError("failed_precondition", `plimsoll: an earlier call of this session ended without an answer this client could check (${this.#unknown}); it may have run, so this client sends nothing more on the session: open a new one`, { notDispatched: "request" });
|
|
484
527
|
}
|
|
485
528
|
const env = this.#client.envelope(opts, rule);
|
|
529
|
+
// Before the digest, which cannot encode such text either.
|
|
530
|
+
refuseIllFormed({ ...env, ...payload }, "the request");
|
|
486
531
|
const digest = requestDigest({ protocol: env.protocol, minimumIsolation: env.minimumIsolation ?? "", timeoutMs: env.timeoutMs ?? 0, softwareRule: rule, payload: digestPayload(payload) });
|
|
487
532
|
try {
|
|
488
533
|
return await this.#exchange(payload, opts, rule, env, digest);
|
|
@@ -519,33 +564,34 @@ export class Session {
|
|
|
519
564
|
});
|
|
520
565
|
}
|
|
521
566
|
async #exchange(payload, opts, rule, env, digest) {
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
this.#calls
|
|
546
|
-
this.#last = r.sha256;
|
|
547
|
-
return { run, record: r };
|
|
567
|
+
let m;
|
|
568
|
+
try {
|
|
569
|
+
m = await this.#client.call("SessionRun", { ...env, sessionId: this.#id, ...payload }, opts.signal);
|
|
570
|
+
}
|
|
571
|
+
catch (e) {
|
|
572
|
+
if (e instanceof PlimsollError && e.sessionEnded)
|
|
573
|
+
this.#end = e.sessionEnded;
|
|
574
|
+
throw e;
|
|
575
|
+
}
|
|
576
|
+
return decoded(() => this.#chained(m, env, rule, digest));
|
|
577
|
+
}
|
|
578
|
+
#chained(m, env, rule, digest) {
|
|
579
|
+
const run = m.run;
|
|
580
|
+
if (!run)
|
|
581
|
+
throw new PlimsollError("data_loss", "plimsoll: the session call's answer carries no run");
|
|
582
|
+
const end = sessionEndFromWire(m.ended);
|
|
583
|
+
if (end !== "open")
|
|
584
|
+
this.#end = { reason: end, detail: m.endDetail ?? "" };
|
|
585
|
+
const checked = checkRecord(digest, env.protocol, rule, run);
|
|
586
|
+
if ("problem" in checked)
|
|
587
|
+
throw new PlimsollError("data_loss", `plimsoll: ${checked.problem}`, { result: run });
|
|
588
|
+
const r = checked.record;
|
|
589
|
+
if (r.session !== this.fingerprint || r.sequence !== this.#calls + 1n || r.previousSha256 !== this.#last) {
|
|
590
|
+
throw new PlimsollError("data_loss", `plimsoll: the session's chain is broken: call ${r.sequence} after "${r.previousSha256}", this client's last was call ${this.#calls}, "${this.#last}"`, { result: run });
|
|
548
591
|
}
|
|
592
|
+
this.#calls = r.sequence;
|
|
593
|
+
this.#last = r.sha256;
|
|
594
|
+
return { run, record: r };
|
|
549
595
|
}
|
|
550
596
|
/**
|
|
551
597
|
* Ends the session (or collects one that ended by itself) and checks the
|
|
@@ -554,12 +600,12 @@ export class Session {
|
|
|
554
600
|
close(signal) {
|
|
555
601
|
return this.#serial(async () => {
|
|
556
602
|
const m = await this.#client.call("CloseSession", { protocol: PROTOCOL, sessionId: this.#id }, signal);
|
|
557
|
-
const sum = {
|
|
603
|
+
const sum = decoded(() => ({
|
|
558
604
|
session: m.session ?? "",
|
|
559
605
|
calls: BigInt(m.calls ?? 0),
|
|
560
606
|
lastRecordSha256: m.lastRecordSha256 ?? "",
|
|
561
607
|
end: sessionEndFromWire(m.ended),
|
|
562
|
-
};
|
|
608
|
+
}));
|
|
563
609
|
this.#end ??= { reason: sum.end, detail: "" };
|
|
564
610
|
if (sum.session !== this.fingerprint || sum.calls !== this.#calls || sum.lastRecordSha256 !== this.#last) {
|
|
565
611
|
throw new PlimsollError("data_loss", `plimsoll: the daemon counts ${sum.calls} calls ending "${sum.lastRecordSha256}", this client saw ${this.#calls} ending "${this.#last}"`, { result: sum });
|
package/dist/errors.d.ts
CHANGED
|
@@ -51,5 +51,4 @@ export declare class PlimsollError extends Error {
|
|
|
51
51
|
});
|
|
52
52
|
}
|
|
53
53
|
export declare function sessionEndFromWire(v: string | number | undefined): SessionEnd;
|
|
54
|
-
/** The error a Connect error body describes, with its plimsoll details restored. */
|
|
55
54
|
export declare function errorFromWire(httpStatus: number, body: WireError | undefined): PlimsollError;
|
package/dist/errors.js
CHANGED
|
@@ -172,12 +172,20 @@ function decodeRecord(b) {
|
|
|
172
172
|
return m;
|
|
173
173
|
}
|
|
174
174
|
/** The error a Connect error body describes, with its plimsoll details restored. */
|
|
175
|
+
const CODES = new Set([
|
|
176
|
+
"canceled", "unknown", "invalid_argument", "deadline_exceeded", "not_found", "already_exists", "permission_denied", "resource_exhausted",
|
|
177
|
+
"failed_precondition", "aborted", "out_of_range", "unimplemented", "internal", "unavailable", "data_loss", "unauthenticated",
|
|
178
|
+
]);
|
|
179
|
+
// The body is whatever the daemon sent, so each field is used only in the shape Connect
|
|
180
|
+
// gives it, and a code outside Connect's set is "unknown".
|
|
175
181
|
export function errorFromWire(httpStatus, body) {
|
|
176
|
-
const
|
|
182
|
+
const wire = body !== null && typeof body === "object" && !Array.isArray(body) ? body : undefined;
|
|
183
|
+
const code = wire?.code === undefined ? httpStatusCode(httpStatus) : CODES.has(wire.code) ? wire.code : "unknown";
|
|
184
|
+
const message = typeof wire?.message === "string" ? wire.message : `HTTP ${httpStatus}`;
|
|
177
185
|
let notDispatched;
|
|
178
186
|
let sessionEnded;
|
|
179
187
|
let unanswered;
|
|
180
|
-
for (const d of
|
|
188
|
+
for (const d of Array.isArray(wire?.details) ? wire.details : []) {
|
|
181
189
|
try {
|
|
182
190
|
const bytes = Buffer.from(d.value ?? "", "base64");
|
|
183
191
|
if (d.type === "plimsoll.v1.NotDispatched") {
|
|
@@ -195,7 +203,7 @@ export function errorFromWire(httpStatus, body) {
|
|
|
195
203
|
// An undecodable detail states nothing; in particular not that nothing ran.
|
|
196
204
|
}
|
|
197
205
|
}
|
|
198
|
-
return new PlimsollError(code,
|
|
206
|
+
return new PlimsollError(code, message, { notDispatched, sessionEnded, unanswered: notDispatched ? undefined : unanswered });
|
|
199
207
|
}
|
|
200
208
|
// Connect's mapping for a response without a Connect error body.
|
|
201
209
|
function httpStatusCode(status) {
|
package/dist/mastra.js
CHANGED
|
@@ -8,8 +8,9 @@ import { executeCodeDescription, executeCodeInput, executeCodeOutput } from "./t
|
|
|
8
8
|
// A thread id is unique only within its resource (the user), so the key is both, and a
|
|
9
9
|
// call without either has no key and runs fresh. An empty resource would put every user
|
|
10
10
|
// without one whose thread id collided into one sandbox, with each other's files and
|
|
11
|
-
// variables.
|
|
12
|
-
|
|
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;
|
|
13
14
|
export function plimsollExecuteCode(opts) {
|
|
14
15
|
const sandboxes = new CodeSandboxes(opts);
|
|
15
16
|
const executeCode = createTool({
|
package/dist/sandboxes.js
CHANGED
|
@@ -296,10 +296,13 @@ export class CodeSandboxes {
|
|
|
296
296
|
const session = this.persistent().then((ok) => {
|
|
297
297
|
if (!ok)
|
|
298
298
|
throw new PlimsollError("unimplemented", "plimsoll: the daemon does not keep sessions", { notDispatched: "unsupported" });
|
|
299
|
+
// The tool's languages are the hint: a daemon with a warm pool hands over a
|
|
300
|
+
// sandbox with their interpreters running, and drops any it does not run.
|
|
299
301
|
return this.client.openSession({
|
|
300
302
|
minimumIsolation: this.#opts.minimumIsolation,
|
|
301
303
|
lifetimeMs: this.#opts.sessionLifetimeMs,
|
|
302
304
|
idleTimeoutMs: this.#opts.sessionIdleTimeoutMs,
|
|
305
|
+
languages: [...this.languages],
|
|
303
306
|
});
|
|
304
307
|
});
|
|
305
308
|
const e = { session, answered: false, busy: 0 };
|
package/dist/wire.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@plimsollmark/client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.17.1",
|
|
4
4
|
"description": "TypeScript client for plimsolld, with Trigger.dev and Mastra executeCode add-ons",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"homepage": "https://github.com/plimsollmark/plimsoll/tree/main/clients/typescript#readme",
|