@keska/sandbox 0.48.0 → 0.50.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/index.d.ts +27 -13
- package/dist/index.js +86 -29
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -5315,6 +5315,16 @@ declare class GuestHttp$1 {
|
|
|
5315
5315
|
websocket(path?: string): Promise<WebSocket>;
|
|
5316
5316
|
}
|
|
5317
5317
|
|
|
5318
|
+
/** Exchange a login JWT for one Matrix device. The homeserver follows the control plane. */
|
|
5319
|
+
/** One Matrix access token. It is one device for one person. */
|
|
5320
|
+
type MatrixToken = {
|
|
5321
|
+
accessToken: string;
|
|
5322
|
+
userId: string;
|
|
5323
|
+
deviceId: string;
|
|
5324
|
+
expiresAt: string;
|
|
5325
|
+
homeserver: string;
|
|
5326
|
+
};
|
|
5327
|
+
|
|
5318
5328
|
type TimeoutOpts = {
|
|
5319
5329
|
timeout?: number;
|
|
5320
5330
|
signal?: AbortSignal;
|
|
@@ -6232,14 +6242,6 @@ declare class Agent {
|
|
|
6232
6242
|
newSandbox(opts?: Omit<SandboxCreateParams, "agentId" | "image"> & {
|
|
6233
6243
|
image?: string;
|
|
6234
6244
|
}): Promise<Sandbox>;
|
|
6235
|
-
/** Chat goes through agent-kit. This client creates conversations and login tokens. */
|
|
6236
|
-
sendMessage(_body: string): Promise<string>;
|
|
6237
|
-
/** Chat goes through agent-kit. */
|
|
6238
|
-
subscribe(): Promise<never>;
|
|
6239
|
-
/** Chat goes through agent-kit. */
|
|
6240
|
-
history(): AsyncGenerator<never>;
|
|
6241
|
-
/** Messaging off is reported first. A messaging agent still chats through agent-kit. */
|
|
6242
|
-
private chatElsewhere;
|
|
6243
6245
|
}
|
|
6244
6246
|
/** Copied onto an agent at create. Later edits do not change existing agents. */
|
|
6245
6247
|
declare class Template {
|
|
@@ -6307,6 +6309,7 @@ declare class Agents {
|
|
|
6307
6309
|
create(name: string, opts?: {
|
|
6308
6310
|
template?: string;
|
|
6309
6311
|
messaging?: boolean;
|
|
6312
|
+
user?: User | string;
|
|
6310
6313
|
userId?: string;
|
|
6311
6314
|
displayName?: string;
|
|
6312
6315
|
timeout?: Timeout;
|
|
@@ -6368,6 +6371,13 @@ declare class User {
|
|
|
6368
6371
|
newToken(opts?: {
|
|
6369
6372
|
timeout?: Timeout;
|
|
6370
6373
|
}): Promise<LoginToken>;
|
|
6374
|
+
/**
|
|
6375
|
+
* Matrix access token for this person.
|
|
6376
|
+
* Omit `deviceId` to create a device. Pass it back to keep that device.
|
|
6377
|
+
*/
|
|
6378
|
+
matrixToken(opts?: {
|
|
6379
|
+
deviceId?: string;
|
|
6380
|
+
}): Promise<MatrixToken>;
|
|
6371
6381
|
/** Sign this person out of every device. */
|
|
6372
6382
|
revoke(opts?: {
|
|
6373
6383
|
timeout?: Timeout;
|
|
@@ -6439,6 +6449,13 @@ declare class Client {
|
|
|
6439
6449
|
newToken(opts?: {
|
|
6440
6450
|
timeout?: Timeout;
|
|
6441
6451
|
}): Promise<LoginToken>;
|
|
6452
|
+
/**
|
|
6453
|
+
* Matrix access token for the API caller.
|
|
6454
|
+
* Omit `deviceId` to create a device. Pass it back to keep that device.
|
|
6455
|
+
*/
|
|
6456
|
+
matrixToken(opts?: {
|
|
6457
|
+
deviceId?: string;
|
|
6458
|
+
}): Promise<MatrixToken>;
|
|
6442
6459
|
/** Close the connection. */
|
|
6443
6460
|
close(): void;
|
|
6444
6461
|
/** `using` calls this. */
|
|
@@ -6875,14 +6892,11 @@ declare class Sandbox {
|
|
|
6875
6892
|
private exec;
|
|
6876
6893
|
/** Run one shell string with `/bin/sh -c`. */
|
|
6877
6894
|
sh(command: string, opts?: Parameters<Sandbox["run"]>[1]): Promise<ExecResult>;
|
|
6878
|
-
/** Start a command and return its handle.
|
|
6895
|
+
/** Start a command and return its handle. The process runs until it exits. */
|
|
6879
6896
|
start(argv: string[], opts?: {
|
|
6880
6897
|
env?: Record<string, string>;
|
|
6881
6898
|
cwd?: string;
|
|
6882
|
-
timeout?: Timeout;
|
|
6883
6899
|
name?: string;
|
|
6884
|
-
keepAlive?: boolean;
|
|
6885
|
-
endsSandbox?: boolean;
|
|
6886
6900
|
}): Promise<ProcessHandle>;
|
|
6887
6901
|
/** Copy this sandbox's writable layer into new sandboxes. */
|
|
6888
6902
|
clone(opts?: {
|
|
@@ -7053,4 +7067,4 @@ type GuestResponse = GuestResponse$1;
|
|
|
7053
7067
|
|
|
7054
7068
|
declare const publicExports: readonly ["Agent", "Template", "AuthenticationError", "CapacityError", "Client", "CommandFailedError", "CommandTimeoutError", "CommandTooLargeError", "ConflictError", "DirectoryNotEmptyError", "ExecutableNotFoundError", "FileExistsError", "FileTooLargeError", "GuestRuntimeDeadError", "ImageTimeoutError", "InvalidArgumentError", "InvalidPathError", "Mkdir", "UNLIMITED", "NameTakenError", "NotFoundError", "Page", "PathNotFoundError", "PageExhaustedError", "PermissionDeniedError", "PortNotOpenError", "ProcessHandle", "Profile", "ResourceInUseError", "ResourcePolicy", "SecurityPolicy", "ProcessNotFoundError", "ProcessWaitTimeoutError", "Remove", "Sandbox", "SandboxConflictError", "SandboxError", "SandboxNotFoundError", "SandboxNotReadyError", "SandboxSleepingError", "SandboxStateError", "LogStream", "SandboxStatus", "SandboxWaitTimeoutError", "SandboxTimeoutError", "TimeoutError", "UnavailableError", "Write", "mkdir", "publicExports", "remove", "write"];
|
|
7055
7069
|
|
|
7056
|
-
export { Agent, AuthenticationError, CapacityError, Client, type ClientOptions, CommandFailedError, CommandTimeoutError, CommandTooLargeError, ConflictError, DirectoryNotEmptyError, type ExecBytesResult, type ExecResult, ExecutableNotFoundError, FileExistsError, type FileInfo, type FileList, FileTooLargeError, type GuestHttp, type GuestResponse, GuestRuntimeDeadError, type HTTPGrant, type Image, type ImageKind, ImageTimeoutError, InvalidArgumentError, InvalidPathError, type LogEntry, LogStream, Mkdir, NameTakenError, NotFoundError, Page, PageExhaustedError, PathNotFoundError, PermissionDeniedError, PortNotOpenError, type Process, ProcessHandle, ProcessNotFoundError, ProcessWaitTimeoutError, Profile, Remove, ResourceInUseError, ResourcePolicy, type Resources, Sandbox, SandboxConflictError, type SandboxCreateParams, SandboxError, type SandboxInfo, SandboxNotFoundError, SandboxNotReadyError, type SandboxRow, SandboxSleepingError, SandboxStateError, SandboxStatus, SandboxTimeoutError, SandboxWaitTimeoutError, type Secret, SecurityPolicy, type Snapshot, type StreamChunk, Template, TimeoutError, UNLIMITED, UnavailableError, type Volume, Write, mkdir, publicExports, remove, write };
|
|
7070
|
+
export { Agent, AuthenticationError, CapacityError, Client, type ClientOptions, CommandFailedError, CommandTimeoutError, CommandTooLargeError, ConflictError, DirectoryNotEmptyError, type ExecBytesResult, type ExecResult, ExecutableNotFoundError, FileExistsError, type FileInfo, type FileList, FileTooLargeError, type GuestHttp, type GuestResponse, GuestRuntimeDeadError, type HTTPGrant, type Image, type ImageKind, ImageTimeoutError, InvalidArgumentError, InvalidPathError, type LogEntry, LogStream, type MatrixToken, Mkdir, NameTakenError, NotFoundError, Page, PageExhaustedError, PathNotFoundError, PermissionDeniedError, PortNotOpenError, type Process, ProcessHandle, ProcessNotFoundError, ProcessWaitTimeoutError, Profile, Remove, ResourceInUseError, ResourcePolicy, type Resources, Sandbox, SandboxConflictError, type SandboxCreateParams, SandboxError, type SandboxInfo, SandboxNotFoundError, SandboxNotReadyError, type SandboxRow, SandboxSleepingError, SandboxStateError, SandboxStatus, SandboxTimeoutError, SandboxWaitTimeoutError, type Secret, SecurityPolicy, type Snapshot, type StreamChunk, Template, TimeoutError, UNLIMITED, UnavailableError, type Volume, Write, mkdir, publicExports, remove, write };
|
package/dist/index.js
CHANGED
|
@@ -4122,6 +4122,73 @@ var Client = class {
|
|
|
4122
4122
|
}
|
|
4123
4123
|
};
|
|
4124
4124
|
|
|
4125
|
+
// src/matrixToken.ts
|
|
4126
|
+
var TOKEN_MS = 36e5;
|
|
4127
|
+
var HOMESERVERS = {
|
|
4128
|
+
"numenbox.keska.dev": "https://numen-matrix.keska.dev",
|
|
4129
|
+
"numenbox.keska.io": "https://numen-matrix.keska.io"
|
|
4130
|
+
};
|
|
4131
|
+
function matrixHomeserver(baseUrl) {
|
|
4132
|
+
let host = "";
|
|
4133
|
+
try {
|
|
4134
|
+
host = new URL(baseUrl).hostname;
|
|
4135
|
+
} catch {
|
|
4136
|
+
host = "";
|
|
4137
|
+
}
|
|
4138
|
+
const homeserver = HOMESERVERS[host];
|
|
4139
|
+
if (!homeserver) {
|
|
4140
|
+
throw new InvalidArgumentError(`no matrix homeserver for ${host || baseUrl}`, {
|
|
4141
|
+
code: "invalid_argument",
|
|
4142
|
+
reason: "HOMESERVER_UNKNOWN"
|
|
4143
|
+
});
|
|
4144
|
+
}
|
|
4145
|
+
return homeserver;
|
|
4146
|
+
}
|
|
4147
|
+
async function openMatrixToken(baseUrl, mint, opts, doFetch = fetch) {
|
|
4148
|
+
const homeserver = matrixHomeserver(baseUrl);
|
|
4149
|
+
const deviceId = opts?.deviceId || crypto.randomUUID();
|
|
4150
|
+
const jwt = await mint();
|
|
4151
|
+
const url = `${homeserver}/_matrix/client/v3/login`;
|
|
4152
|
+
let res;
|
|
4153
|
+
try {
|
|
4154
|
+
res = await doFetch(url, {
|
|
4155
|
+
method: "POST",
|
|
4156
|
+
headers: { "content-type": "application/json" },
|
|
4157
|
+
body: JSON.stringify({ type: "org.matrix.login.jwt", token: jwt, device_id: deviceId })
|
|
4158
|
+
});
|
|
4159
|
+
} catch (error) {
|
|
4160
|
+
throw new SandboxError(`matrix login ${error instanceof Error ? error.message : "failed"}`, {
|
|
4161
|
+
code: "unavailable",
|
|
4162
|
+
reason: "UNAVAILABLE"
|
|
4163
|
+
});
|
|
4164
|
+
}
|
|
4165
|
+
if (res.status === 401 || res.status === 403) {
|
|
4166
|
+
throw new AuthenticationError("matrix login refused", { code: "unauthenticated", reason: "UNAUTHENTICATED" });
|
|
4167
|
+
}
|
|
4168
|
+
if (!res.ok) throw new SandboxError(`matrix login ${res.status}`);
|
|
4169
|
+
const body = await res.json();
|
|
4170
|
+
if (!body.access_token || !body.user_id) {
|
|
4171
|
+
throw new AuthenticationError("matrix login returned no token", {
|
|
4172
|
+
code: "unauthenticated",
|
|
4173
|
+
reason: "UNAUTHENTICATED"
|
|
4174
|
+
});
|
|
4175
|
+
}
|
|
4176
|
+
if (body.device_id && body.device_id !== deviceId) {
|
|
4177
|
+
throw new AuthenticationError("matrix login changed the device", {
|
|
4178
|
+
code: "unauthenticated",
|
|
4179
|
+
reason: "UNAUTHENTICATED"
|
|
4180
|
+
});
|
|
4181
|
+
}
|
|
4182
|
+
const expiresInMs = body.expires_in_ms ?? TOKEN_MS;
|
|
4183
|
+
return {
|
|
4184
|
+
accessToken: body.access_token,
|
|
4185
|
+
userId: body.user_id,
|
|
4186
|
+
deviceId,
|
|
4187
|
+
expiresAt: new Date(Date.now() + expiresInMs).toISOString(),
|
|
4188
|
+
homeserver
|
|
4189
|
+
};
|
|
4190
|
+
}
|
|
4191
|
+
|
|
4125
4192
|
// src/sandbox.ts
|
|
4126
4193
|
import { createWriteStream as createWriteStream2, lstatSync } from "fs";
|
|
4127
4194
|
import { rename as rename2, unlink as unlink2 } from "fs/promises";
|
|
@@ -5554,29 +5621,6 @@ var Agent = class {
|
|
|
5554
5621
|
profile: opts.profile ?? (this.profileId || void 0)
|
|
5555
5622
|
});
|
|
5556
5623
|
}
|
|
5557
|
-
/** Chat goes through agent-kit. This client creates conversations and login tokens. */
|
|
5558
|
-
async sendMessage(_body) {
|
|
5559
|
-
this.chatElsewhere();
|
|
5560
|
-
}
|
|
5561
|
-
/** Chat goes through agent-kit. */
|
|
5562
|
-
async subscribe() {
|
|
5563
|
-
this.chatElsewhere();
|
|
5564
|
-
}
|
|
5565
|
-
/** Chat goes through agent-kit. */
|
|
5566
|
-
// biome-ignore lint/correctness/useYield: this stub only rejects; chat is agent-kit
|
|
5567
|
-
async *history() {
|
|
5568
|
-
this.chatElsewhere();
|
|
5569
|
-
}
|
|
5570
|
-
/** Messaging off is reported first. A messaging agent still chats through agent-kit. */
|
|
5571
|
-
chatElsewhere() {
|
|
5572
|
-
if (!this.messaging) {
|
|
5573
|
-
throw new SandboxError("messaging is off for this agent", {
|
|
5574
|
-
code: "failed_precondition",
|
|
5575
|
-
reason: "AGENT_MESSAGING_OFF"
|
|
5576
|
-
});
|
|
5577
|
-
}
|
|
5578
|
-
throw new SandboxError("chat with agent-kit", { code: "failed_precondition", reason: "USE_AGENT_KIT" });
|
|
5579
|
-
}
|
|
5580
5624
|
};
|
|
5581
5625
|
var Template = class {
|
|
5582
5626
|
constructor(client, info) {
|
|
@@ -5692,10 +5736,12 @@ var Agents2 = class {
|
|
|
5692
5736
|
client;
|
|
5693
5737
|
templates;
|
|
5694
5738
|
create(name, opts) {
|
|
5739
|
+
const bound = opts?.user;
|
|
5740
|
+
const userId = typeof bound === "string" ? bound : bound instanceof User ? bound.id : opts?.userId;
|
|
5695
5741
|
return this.client.wire.agents.create(name, {
|
|
5696
5742
|
template: opts?.template,
|
|
5697
5743
|
messaging: opts?.messaging,
|
|
5698
|
-
userId
|
|
5744
|
+
userId,
|
|
5699
5745
|
displayName: opts?.displayName,
|
|
5700
5746
|
timeout: timeoutSeconds(opts?.timeout)
|
|
5701
5747
|
}).then((info) => new Agent(this.client, info));
|
|
@@ -5818,6 +5864,13 @@ var User = class {
|
|
|
5818
5864
|
() => this.client.wire.tokens.create(this.id, { timeout: timeoutSeconds(opts?.timeout) })
|
|
5819
5865
|
);
|
|
5820
5866
|
}
|
|
5867
|
+
/**
|
|
5868
|
+
* Matrix access token for this person.
|
|
5869
|
+
* Omit `deviceId` to create a device. Pass it back to keep that device.
|
|
5870
|
+
*/
|
|
5871
|
+
matrixToken(opts) {
|
|
5872
|
+
return openMatrixToken(this.client.wire.baseUrl, async () => (await this.newToken()).token, opts);
|
|
5873
|
+
}
|
|
5821
5874
|
/** Sign this person out of every device. */
|
|
5822
5875
|
revoke(opts) {
|
|
5823
5876
|
return retry(
|
|
@@ -5932,6 +5985,13 @@ var Client2 = class {
|
|
|
5932
5985
|
newToken(opts) {
|
|
5933
5986
|
return retry(this, () => this.wire.newToken({ timeout: timeoutSeconds(opts?.timeout) }));
|
|
5934
5987
|
}
|
|
5988
|
+
/**
|
|
5989
|
+
* Matrix access token for the API caller.
|
|
5990
|
+
* Omit `deviceId` to create a device. Pass it back to keep that device.
|
|
5991
|
+
*/
|
|
5992
|
+
matrixToken(opts) {
|
|
5993
|
+
return openMatrixToken(this.wire.baseUrl, async () => (await this.newToken()).token, opts);
|
|
5994
|
+
}
|
|
5935
5995
|
/** Close the connection. */
|
|
5936
5996
|
close() {
|
|
5937
5997
|
this.wire.close();
|
|
@@ -6670,15 +6730,12 @@ var Sandbox2 = class _Sandbox {
|
|
|
6670
6730
|
sh(command, opts) {
|
|
6671
6731
|
return this.run(["/bin/sh", "-c", command], opts);
|
|
6672
6732
|
}
|
|
6673
|
-
/** Start a command and return its handle.
|
|
6733
|
+
/** Start a command and return its handle. The process runs until it exits. */
|
|
6674
6734
|
async start(argv, opts) {
|
|
6675
6735
|
const inner = await this.inner.start(argv, {
|
|
6676
6736
|
env: opts?.env,
|
|
6677
6737
|
cwd: opts?.cwd,
|
|
6678
|
-
|
|
6679
|
-
serviceName: opts?.name,
|
|
6680
|
-
holdsIdle: opts?.keepAlive,
|
|
6681
|
-
endsSandbox: opts?.endsSandbox
|
|
6738
|
+
serviceName: opts?.name
|
|
6682
6739
|
});
|
|
6683
6740
|
return new ProcessHandle2(this, inner);
|
|
6684
6741
|
}
|