@watasu/sdk 0.1.24 → 0.1.30

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 CHANGED
@@ -31,6 +31,64 @@ await sbx.betaPause()
31
31
  await sbx.resume({ timeoutMs: 300_000 })
32
32
  ```
33
33
 
34
+ Choose what happens when the sandbox timeout expires:
35
+
36
+ ```ts
37
+ const sbx = await Sandbox.create({
38
+ lifecycle: { onTimeout: 'pause', autoResume: true },
39
+ })
40
+ ```
41
+
42
+ `onTimeout: 'kill'` is the default. `onTimeout: 'pause'` keeps the retained
43
+ disk after timeout; `autoResume` lets a later data-plane request resume that
44
+ paused sandbox automatically.
45
+
46
+ Mount a named persistent volume when the sandbox starts:
47
+
48
+ ```ts
49
+ const sbx = await Sandbox.create({
50
+ volumeMounts: {
51
+ '/workspace/cache': 'cache',
52
+ '/data/models': { name: 'models' },
53
+ },
54
+ })
55
+ ```
56
+
57
+ Create and edit a persistent volume while it is detached:
58
+
59
+ ```ts
60
+ import { Volume } from '@watasu/sdk'
61
+
62
+ const volume = await Volume.create('cache')
63
+ await volume.makeDir('/workspace')
64
+ await volume.writeFile('/workspace/status.txt', 'ready\n', { mode: '0644' })
65
+ console.log(await volume.readFile('/workspace/status.txt'))
66
+ console.log((await volume.list('/workspace')).map((entry) => entry.path))
67
+ await volume.remove('/workspace/status.txt')
68
+ await volume.destroy()
69
+ ```
70
+
71
+ ## Code Interpreter
72
+
73
+ ```ts
74
+ import { Sandbox } from '@watasu/sdk/code-interpreter'
75
+
76
+ const sbx = await Sandbox.create()
77
+ const context = await sbx.createCodeContext()
78
+ const execution = await sbx.runCode("print('hello')\n2 + 3", {
79
+ context,
80
+ onStdout: (message) => console.log(message.line),
81
+ })
82
+
83
+ console.log(execution.text)
84
+ await sbx.removeCodeContext(context)
85
+ await sbx.kill()
86
+ ```
87
+
88
+ `@watasu/sdk/code-interpreter` starts the `code-interpreter` template by default.
89
+ Code runs in persistent Python contexts and returns structured `results`, `logs`,
90
+ and `error` fields for each execution.
91
+
34
92
  ## MCP Gateway
35
93
 
36
94
  ```ts
@@ -173,7 +231,10 @@ instructions into Watasu's package-spec builder.
173
231
  import { Sandbox } from '@watasu/sdk'
174
232
 
175
233
  const sbx = await Sandbox.create()
176
- const metrics = await sbx.getMetrics()
234
+ const metrics = await sbx.getMetrics({
235
+ start: new Date(Date.now() - 5 * 60_000),
236
+ end: new Date(),
237
+ })
177
238
  const snapshot = await sbx.createSnapshot({ name: 'ready' })
178
239
  const snapshots = await sbx.listSnapshots().nextItems()
179
240
  const allSnapshots = await Sandbox.listSnapshots({ limit: 100 }).nextItems()
@@ -0,0 +1,100 @@
1
+ import { Sandbox as BaseSandbox, SandboxConnectOpts, SandboxCreateOpts } from './sandbox.js';
2
+ export type RunCodeLanguage = 'python' | 'python3' | string;
3
+ export interface RunCodeOpts {
4
+ language?: RunCodeLanguage;
5
+ context?: Context | string;
6
+ onStdout?: (message: OutputMessage) => void;
7
+ onStderr?: (message: OutputMessage) => void;
8
+ onResult?: (result: Result) => void;
9
+ onError?: (error: ExecutionError) => void;
10
+ envs?: Record<string, string>;
11
+ timeout?: number;
12
+ requestTimeoutMs?: number;
13
+ }
14
+ export interface CreateCodeContextOpts {
15
+ cwd?: string;
16
+ language?: RunCodeLanguage;
17
+ requestTimeoutMs?: number;
18
+ }
19
+ /** One stdout or stderr line emitted by code execution. */
20
+ export declare class OutputMessage {
21
+ readonly line: string;
22
+ readonly timestamp: number;
23
+ readonly error: boolean;
24
+ constructor(line: string, timestamp?: number, error?: boolean);
25
+ toString(): string;
26
+ toJSON(): Record<string, unknown>;
27
+ }
28
+ /** Structured exception raised by user code inside the sandbox. */
29
+ export declare class ExecutionError {
30
+ readonly name: string;
31
+ readonly value: string;
32
+ readonly traceback: string;
33
+ constructor(name: string, value: string, traceback: string);
34
+ toJSON(): Record<string, unknown>;
35
+ }
36
+ /** Rich result produced by the last expression of a code execution. */
37
+ export declare class Result {
38
+ readonly text?: string;
39
+ readonly html?: string;
40
+ readonly markdown?: string;
41
+ readonly svg?: string;
42
+ readonly png?: string;
43
+ readonly jpeg?: string;
44
+ readonly pdf?: string;
45
+ readonly latex?: string;
46
+ readonly json?: unknown;
47
+ readonly javascript?: string;
48
+ readonly data?: unknown;
49
+ readonly chart?: unknown;
50
+ readonly extra: Record<string, unknown>;
51
+ readonly isMainResult: boolean;
52
+ constructor(payload?: Record<string, unknown>);
53
+ formats(): string[];
54
+ toJSON(): Record<string, unknown>;
55
+ }
56
+ export interface Logs {
57
+ stdout: OutputMessage[];
58
+ stderr: OutputMessage[];
59
+ }
60
+ /** Complete result of a sandbox code execution. */
61
+ export declare class Execution {
62
+ readonly results: Result[];
63
+ readonly logs: Logs;
64
+ readonly error: ExecutionError | undefined;
65
+ readonly executionCount: number | undefined;
66
+ constructor(results?: Result[], logs?: Logs, error?: ExecutionError | undefined, executionCount?: number | undefined);
67
+ get text(): string | undefined;
68
+ toJSON(): Record<string, unknown>;
69
+ }
70
+ /** Code execution context metadata. */
71
+ export declare class Context {
72
+ readonly id: string;
73
+ readonly language?: string | undefined;
74
+ readonly cwd?: string | undefined;
75
+ constructor(id: string, language?: string | undefined, cwd?: string | undefined);
76
+ toJSON(): Record<string, unknown>;
77
+ }
78
+ /** Sandbox specialized for running Python code. */
79
+ export declare class Sandbox extends BaseSandbox {
80
+ static readonly defaultTemplate = "code-interpreter";
81
+ static create(opts?: SandboxCreateOpts): Promise<Sandbox>;
82
+ static create(template: string, opts?: SandboxCreateOpts): Promise<Sandbox>;
83
+ static connect(sandboxId: string, opts?: SandboxConnectOpts): Promise<Sandbox>;
84
+ /** Run Python code in the sandbox and return structured execution output. */
85
+ runCode(code: string, opts?: RunCodeOpts): Promise<Execution>;
86
+ /** Create a persistent code context. */
87
+ createCodeContext(_opts?: CreateCodeContextOpts): Promise<Context>;
88
+ /** Remove a persistent code context. */
89
+ removeCodeContext(context: Context | string, opts?: {
90
+ requestTimeoutMs?: number;
91
+ }): Promise<void>;
92
+ /** List persistent code contexts. */
93
+ listCodeContexts(opts?: {
94
+ requestTimeoutMs?: number;
95
+ }): Promise<Context[]>;
96
+ /** Restart a persistent code context. */
97
+ restartCodeContext(context: Context | string, opts?: {
98
+ requestTimeoutMs?: number;
99
+ }): Promise<void>;
100
+ }
@@ -0,0 +1,265 @@
1
+ import { InvalidArgumentError } from './errors.js';
2
+ import { Sandbox as BaseSandbox } from './sandbox.js';
3
+ /** One stdout or stderr line emitted by code execution. */
4
+ export class OutputMessage {
5
+ line;
6
+ timestamp;
7
+ error;
8
+ constructor(line, timestamp = Date.now() / 1000, error = false) {
9
+ this.line = line;
10
+ this.timestamp = timestamp;
11
+ this.error = error;
12
+ }
13
+ toString() {
14
+ return this.line;
15
+ }
16
+ toJSON() {
17
+ return {
18
+ line: this.line,
19
+ timestamp: this.timestamp,
20
+ error: this.error,
21
+ };
22
+ }
23
+ }
24
+ /** Structured exception raised by user code inside the sandbox. */
25
+ export class ExecutionError {
26
+ name;
27
+ value;
28
+ traceback;
29
+ constructor(name, value, traceback) {
30
+ this.name = name;
31
+ this.value = value;
32
+ this.traceback = traceback;
33
+ }
34
+ toJSON() {
35
+ return {
36
+ name: this.name,
37
+ value: this.value,
38
+ traceback: this.traceback,
39
+ };
40
+ }
41
+ }
42
+ /** Rich result produced by the last expression of a code execution. */
43
+ export class Result {
44
+ text;
45
+ html;
46
+ markdown;
47
+ svg;
48
+ png;
49
+ jpeg;
50
+ pdf;
51
+ latex;
52
+ json;
53
+ javascript;
54
+ data;
55
+ chart;
56
+ extra;
57
+ isMainResult;
58
+ constructor(payload = {}) {
59
+ this.text = stringValue(payload.text);
60
+ this.html = stringValue(payload.html);
61
+ this.markdown = stringValue(payload.markdown);
62
+ this.svg = stringValue(payload.svg);
63
+ this.png = stringValue(payload.png);
64
+ this.jpeg = stringValue(payload.jpeg);
65
+ this.pdf = stringValue(payload.pdf);
66
+ this.latex = stringValue(payload.latex);
67
+ this.json = payload.json;
68
+ this.javascript = stringValue(payload.javascript);
69
+ this.data = payload.data;
70
+ this.chart = payload.chart;
71
+ this.extra = record(payload.extra);
72
+ this.isMainResult = Boolean(payload.is_main_result ?? payload.isMainResult);
73
+ }
74
+ formats() {
75
+ const names = ['text', 'html', 'markdown', 'svg', 'png', 'jpeg', 'pdf', 'latex', 'json', 'javascript', 'data', 'chart'];
76
+ return names.filter((name) => this[name] !== undefined);
77
+ }
78
+ toJSON() {
79
+ return compactRecord({
80
+ text: this.text,
81
+ html: this.html,
82
+ markdown: this.markdown,
83
+ svg: this.svg,
84
+ png: this.png,
85
+ jpeg: this.jpeg,
86
+ pdf: this.pdf,
87
+ latex: this.latex,
88
+ json: this.json,
89
+ javascript: this.javascript,
90
+ data: this.data,
91
+ chart: this.chart,
92
+ extra: Object.keys(this.extra).length === 0 ? undefined : this.extra,
93
+ is_main_result: this.isMainResult,
94
+ });
95
+ }
96
+ }
97
+ /** Complete result of a sandbox code execution. */
98
+ export class Execution {
99
+ results;
100
+ logs;
101
+ error;
102
+ executionCount;
103
+ constructor(results = [], logs = { stdout: [], stderr: [] }, error = undefined, executionCount = undefined) {
104
+ this.results = results;
105
+ this.logs = logs;
106
+ this.error = error;
107
+ this.executionCount = executionCount;
108
+ }
109
+ get text() {
110
+ return this.results.find((result) => result.isMainResult && result.text !== undefined)?.text ??
111
+ this.results.find((result) => result.text !== undefined)?.text;
112
+ }
113
+ toJSON() {
114
+ return {
115
+ results: this.results.map((result) => result.toJSON()),
116
+ logs: {
117
+ stdout: this.logs.stdout.map((message) => message.toJSON()),
118
+ stderr: this.logs.stderr.map((message) => message.toJSON()),
119
+ },
120
+ error: this.error?.toJSON() ?? null,
121
+ execution_count: this.executionCount,
122
+ };
123
+ }
124
+ }
125
+ /** Code execution context metadata. */
126
+ export class Context {
127
+ id;
128
+ language;
129
+ cwd;
130
+ constructor(id, language, cwd) {
131
+ this.id = id;
132
+ this.language = language;
133
+ this.cwd = cwd;
134
+ }
135
+ toJSON() {
136
+ return compactRecord({
137
+ id: this.id,
138
+ language: this.language,
139
+ cwd: this.cwd,
140
+ });
141
+ }
142
+ }
143
+ /** Sandbox specialized for running Python code. */
144
+ export class Sandbox extends BaseSandbox {
145
+ static defaultTemplate = 'code-interpreter';
146
+ static async create(templateOrOpts, opts = {}) {
147
+ return await super.create(templateOrOpts, opts);
148
+ }
149
+ static async connect(sandboxId, opts = {}) {
150
+ return await super.connect(sandboxId, opts);
151
+ }
152
+ /** Run Python code in the sandbox and return structured execution output. */
153
+ async runCode(code, opts = {}) {
154
+ if (typeof code !== 'string')
155
+ throw new InvalidArgumentError('code must be a string');
156
+ if (opts.language !== undefined && opts.context !== undefined) {
157
+ throw new InvalidArgumentError('language and context cannot both be set');
158
+ }
159
+ const payload = compactRecord({
160
+ code,
161
+ language: opts.language,
162
+ context_id: contextId(opts.context),
163
+ env_vars: opts.envs,
164
+ timeout_seconds: opts.timeout,
165
+ });
166
+ const response = await this.runtimePostJson('/runtime/v1/code/run', payload, {
167
+ requestTimeoutMs: opts.requestTimeoutMs,
168
+ });
169
+ const execution = executionFromApi(response);
170
+ emitCallbacks(execution, opts);
171
+ return execution;
172
+ }
173
+ /** Create a persistent code context. */
174
+ async createCodeContext(_opts = {}) {
175
+ const response = await this.runtimePostJson('/runtime/v1/code/contexts', compactRecord({
176
+ cwd: _opts.cwd,
177
+ language: _opts.language,
178
+ }), {
179
+ requestTimeoutMs: _opts.requestTimeoutMs,
180
+ });
181
+ return contextFromApi(response);
182
+ }
183
+ /** Remove a persistent code context. */
184
+ async removeCodeContext(context, opts = {}) {
185
+ await this.runtimeDeleteJson(`/runtime/v1/code/contexts/${encodeURIComponent(requireContextId(context))}`, {
186
+ requestTimeoutMs: opts.requestTimeoutMs,
187
+ });
188
+ }
189
+ /** List persistent code contexts. */
190
+ async listCodeContexts(opts = {}) {
191
+ const response = await this.runtimeGetJson('/runtime/v1/code/contexts', {
192
+ requestTimeoutMs: opts.requestTimeoutMs,
193
+ });
194
+ const contexts = Array.isArray(response) ? response : arrayOfUnknown(response.contexts);
195
+ return contexts.map((item) => contextFromApi(record(item)));
196
+ }
197
+ /** Restart a persistent code context. */
198
+ async restartCodeContext(context, opts = {}) {
199
+ await this.runtimePostJson(`/runtime/v1/code/contexts/${encodeURIComponent(requireContextId(context))}/restart`, {}, {
200
+ requestTimeoutMs: opts.requestTimeoutMs,
201
+ });
202
+ }
203
+ }
204
+ function executionFromApi(payload) {
205
+ const execution = record(payload.execution ?? payload);
206
+ const logs = record(execution.logs);
207
+ return new Execution(arrayOfRecords(execution.results).map((item) => new Result(item)), {
208
+ stdout: arrayOfUnknown(logs.stdout).map((item) => outputMessageFromApi(item, false)),
209
+ stderr: arrayOfUnknown(logs.stderr).map((item) => outputMessageFromApi(item, true)),
210
+ }, executionErrorFromApi(execution.error), numberValue(execution.execution_count ?? execution.executionCount));
211
+ }
212
+ function outputMessageFromApi(value, error) {
213
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
214
+ const item = value;
215
+ return new OutputMessage(String(item.line ?? ''), numberValue(item.timestamp) ?? Date.now() / 1000, Boolean(item.error ?? error));
216
+ }
217
+ return new OutputMessage(String(value), Date.now() / 1000, error);
218
+ }
219
+ function executionErrorFromApi(value) {
220
+ if (!value || typeof value !== 'object' || Array.isArray(value))
221
+ return undefined;
222
+ const item = value;
223
+ return new ExecutionError(String(item.name ?? ''), String(item.value ?? ''), String(item.traceback ?? ''));
224
+ }
225
+ function emitCallbacks(execution, opts) {
226
+ for (const message of execution.logs.stdout)
227
+ opts.onStdout?.(message);
228
+ for (const message of execution.logs.stderr)
229
+ opts.onStderr?.(message);
230
+ for (const result of execution.results)
231
+ opts.onResult?.(result);
232
+ if (execution.error !== undefined)
233
+ opts.onError?.(execution.error);
234
+ }
235
+ function contextId(context) {
236
+ if (context === undefined)
237
+ return undefined;
238
+ return requireContextId(context);
239
+ }
240
+ function requireContextId(context) {
241
+ if (typeof context === 'string')
242
+ return context;
243
+ return context.id;
244
+ }
245
+ function contextFromApi(payload) {
246
+ return new Context(String(payload.id ?? ''), stringValue(payload.language), stringValue(payload.cwd));
247
+ }
248
+ function compactRecord(payload) {
249
+ return Object.fromEntries(Object.entries(payload).filter(([, value]) => value !== undefined));
250
+ }
251
+ function record(value) {
252
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
253
+ }
254
+ function arrayOfRecords(value) {
255
+ return Array.isArray(value) ? value.map((item) => record(item)) : [];
256
+ }
257
+ function arrayOfUnknown(value) {
258
+ return Array.isArray(value) ? value : [];
259
+ }
260
+ function stringValue(value) {
261
+ return typeof value === 'string' ? value : undefined;
262
+ }
263
+ function numberValue(value) {
264
+ return typeof value === 'number' ? value : undefined;
265
+ }
package/dist/index.d.ts CHANGED
@@ -1,7 +1,10 @@
1
1
  export { ApiError, AuthenticationError, ConflictError, FileNotFoundError, InvalidArgumentError, NotEnoughSpaceError, NotFoundError, NotImplementedError, RateLimitError, SandboxError, TimeoutError, } from './errors.js';
2
2
  export { ConnectionConfig, KEEPALIVE_PING_INTERVAL_SEC } from './connectionConfig.js';
3
3
  export { Sandbox, SandboxPaginator, SnapshotPaginator } from './sandbox.js';
4
- export type { CreateSnapshotOpts, RestoreSnapshotOpts, SandboxCreateOpts, SandboxConnectOpts, SandboxInfo, SandboxListOpts, SandboxMetrics, McpServer, McpServerName, SandboxNetworkSelector, SandboxNetworkUpdate, SandboxNetworkUpdateOpts, SandboxUrlOpts, SnapshotInfo, FileUrlInfo, } from './sandbox.js';
4
+ export { Sandbox as CodeInterpreterSandbox } from './codeInterpreter.js';
5
+ export type { CreateSnapshotOpts, RestoreSnapshotOpts, SandboxCreateOpts, SandboxConnectOpts, SandboxInfo, SandboxInfoLifecycle, SandboxLifecycle, SandboxListOpts, SandboxMetrics, SandboxMetricsOpts, McpServer, McpServerName, SandboxNetworkSelector, SandboxNetworkUpdate, SandboxNetworkUpdateOpts, SandboxUrlOpts, SnapshotInfo, FileUrlInfo, } from './sandbox.js';
6
+ export type { CreateCodeContextOpts, RunCodeLanguage, RunCodeOpts, } from './codeInterpreter.js';
7
+ export { Context as CodeInterpreterContext, Execution as CodeInterpreterExecution, ExecutionError as CodeInterpreterExecutionError, OutputMessage as CodeInterpreterOutputMessage, Result as CodeInterpreterResult, } from './codeInterpreter.js';
5
8
  export { CommandExitError, CommandHandle, Commands } from './commands.js';
6
9
  export type { CommandResult, CommandStartOpts, ProcessInfo } from './commands.js';
7
10
  export { Process, ProcessManager, ProcessMessage, ProcessOutput } from './process.js';
@@ -14,6 +17,8 @@ export { Pty } from './pty.js';
14
17
  export type { PtyConnectOpts, PtyCreateOpts, PtySize } from './pty.js';
15
18
  export { Terminal, TerminalManager, TerminalOutput } from './terminal.js';
16
19
  export type { TerminalOpts } from './terminal.js';
20
+ export { Volume } from './volume.js';
21
+ export type { VolumeApiParams, VolumeConnectionConfig, VolumeEntryStat, VolumeFileType, VolumeInfo, VolumeListFilesOpts, VolumeListOpts, VolumeMetadataOpts, VolumeReadFileOpts, VolumeReadFormat, VolumeWriteData, VolumeWriteFileOpts, } from './volume.js';
17
22
  export { ProcessSocket, base64DecodeBytes, base64DecodeText, base64Encode } from './processSocket.js';
18
23
  export { ReadyCmd, Template, TemplateBase, waitForFile, waitForPort, waitForProcess, waitForTimeout, waitForURL, waitForUrl, } from './template.js';
19
24
  export type { BuildInfo, BuildOptions, BuildStatusReason, CopyItem, GetBuildStatusOptions, LogEntry, ReadyCommand, TemplateBuildStatus, TemplateBuildStatusResponse, TemplateBuilder, TemplateClass, TemplateFactory, TemplateFinal, TemplateFromImage, TemplateOptions, TemplateTag, TemplateTagInfo, } from './template.js';
package/dist/index.js CHANGED
@@ -1,11 +1,14 @@
1
1
  export { ApiError, AuthenticationError, ConflictError, FileNotFoundError, InvalidArgumentError, NotEnoughSpaceError, NotFoundError, NotImplementedError, RateLimitError, SandboxError, TimeoutError, } from './errors.js';
2
2
  export { ConnectionConfig, KEEPALIVE_PING_INTERVAL_SEC } from './connectionConfig.js';
3
3
  export { Sandbox, SandboxPaginator, SnapshotPaginator } from './sandbox.js';
4
+ export { Sandbox as CodeInterpreterSandbox } from './codeInterpreter.js';
5
+ export { Context as CodeInterpreterContext, Execution as CodeInterpreterExecution, ExecutionError as CodeInterpreterExecutionError, OutputMessage as CodeInterpreterOutputMessage, Result as CodeInterpreterResult, } from './codeInterpreter.js';
4
6
  export { CommandExitError, CommandHandle, Commands } from './commands.js';
5
7
  export { Process, ProcessManager, ProcessMessage, ProcessOutput } from './process.js';
6
8
  export { FileType, Filesystem, FilesystemWatcher, WatchHandle } from './filesystem.js';
7
9
  export { Git } from './git.js';
8
10
  export { Pty } from './pty.js';
9
11
  export { Terminal, TerminalManager, TerminalOutput } from './terminal.js';
12
+ export { Volume } from './volume.js';
10
13
  export { ProcessSocket, base64DecodeBytes, base64DecodeText, base64Encode } from './processSocket.js';
11
14
  export { ReadyCmd, Template, TemplateBase, waitForFile, waitForPort, waitForProcess, waitForTimeout, waitForURL, waitForUrl, } from './template.js';
package/dist/sandbox.d.ts CHANGED
@@ -19,7 +19,16 @@ export interface SandboxCreateOpts extends ConnectionOpts {
19
19
  team?: string;
20
20
  /** MCP gateway configuration to launch inside an `mcp-gateway` sandbox. */
21
21
  mcp?: McpServer;
22
- volumeMounts?: unknown;
22
+ /** Timeout lifecycle policy. Defaults to killing the sandbox at timeout. */
23
+ lifecycle?: SandboxLifecycle;
24
+ /** Persistent volumes to mount, keyed by guest path. */
25
+ volumeMounts?: Record<string, string | {
26
+ name: string;
27
+ }>;
28
+ }
29
+ export interface SandboxLifecycle {
30
+ onTimeout: 'kill' | 'pause';
31
+ autoResume?: boolean;
23
32
  }
24
33
  export type SandboxNetworkSelector = string | string[];
25
34
  export interface SandboxNetworkUpdate {
@@ -58,10 +67,19 @@ export interface SandboxInfo {
58
67
  templateId?: string;
59
68
  name?: string;
60
69
  state?: string;
70
+ lifecycle?: SandboxInfoLifecycle;
71
+ volumeMounts?: Array<{
72
+ name: string;
73
+ path: string;
74
+ }>;
61
75
  metadata: Record<string, string>;
62
76
  startedAt?: string;
63
77
  endAt?: string;
64
78
  }
79
+ export interface SandboxInfoLifecycle {
80
+ onTimeout: 'kill' | 'pause' | string;
81
+ autoResume: boolean;
82
+ }
65
83
  export interface SandboxMetrics {
66
84
  sandboxId?: string;
67
85
  state?: string;
@@ -71,6 +89,12 @@ export interface SandboxMetrics {
71
89
  memoryMb?: number;
72
90
  raw: Record<string, unknown>;
73
91
  }
92
+ export interface SandboxMetricsOpts extends ConnectionOpts {
93
+ /** Start time for the metrics. Defaults to the sandbox start time. */
94
+ start?: Date;
95
+ /** End time for the metrics. Defaults to the current time. */
96
+ end?: Date;
97
+ }
74
98
  export interface SnapshotInfo {
75
99
  snapshotId: string;
76
100
  sandboxId?: string;
@@ -142,9 +166,9 @@ export declare class SandboxPaginator {
142
166
  /** Running Watasu sandbox with ready `files` and `commands` helpers. */
143
167
  export declare class Sandbox {
144
168
  /** Default template slug used when create is called without a template. */
145
- static readonly defaultTemplate = "base";
169
+ static readonly defaultTemplate: string;
146
170
  /** Default template slug used by MCP creation once Watasu supports it. */
147
- static readonly defaultMcpTemplate = "mcp-gateway";
171
+ static readonly defaultMcpTemplate: string;
148
172
  /** Default sandbox lifetime in milliseconds. */
149
173
  static readonly defaultSandboxTimeoutMs = 300000;
150
174
  files: Filesystem;
@@ -194,7 +218,7 @@ export declare class Sandbox {
194
218
  /** Destroy a sandbox by id. */
195
219
  static kill(sandboxId: string, opts?: ConnectionOpts | string): Promise<boolean>;
196
220
  /** Fetch sandbox metrics by id. */
197
- static getMetrics(sandboxId: string, opts?: ConnectionOpts): Promise<SandboxMetrics[]>;
221
+ static getMetrics(sandboxId: string, opts?: SandboxMetricsOpts): Promise<SandboxMetrics[]>;
198
222
  /** Atomically replace a sandbox's network egress policy by id. */
199
223
  static updateNetwork(sandboxId: string, network: SandboxNetworkUpdate, opts?: SandboxNetworkUpdateOpts): Promise<void>;
200
224
  private static putNetwork;
@@ -221,7 +245,7 @@ export declare class Sandbox {
221
245
  /** Fetch the latest control-plane metadata for this sandbox. */
222
246
  getInfo(): Promise<SandboxInfo>;
223
247
  /** Fetch latest sandbox metrics. */
224
- getMetrics(opts?: ConnectionOpts): Promise<SandboxMetrics[]>;
248
+ getMetrics(opts?: SandboxMetricsOpts): Promise<SandboxMetrics[]>;
225
249
  /** Create a Watasu checkpoint using snapshot naming. */
226
250
  createSnapshot(opts?: CreateSnapshotOpts): Promise<SnapshotInfo>;
227
251
  /** Delete a snapshot by id. */
@@ -263,5 +287,11 @@ export declare class Sandbox {
263
287
  /** Resume this sandbox and refresh its data-plane session. */
264
288
  resume(opts?: SandboxConnectOpts): Promise<boolean>;
265
289
  private fileUrl;
290
+ /** POST JSON to the sandbox data-plane runtime API. */
291
+ protected runtimePostJson(path: string, json: Record<string, unknown>, opts?: ConnectionOpts): Promise<Record<string, unknown>>;
292
+ /** GET JSON from the sandbox data-plane runtime API. */
293
+ protected runtimeGetJson(path: string, opts?: ConnectionOpts): Promise<Record<string, unknown>>;
294
+ /** DELETE JSON from the sandbox data-plane runtime API. */
295
+ protected runtimeDeleteJson(path: string, opts?: ConnectionOpts): Promise<Record<string, unknown>>;
266
296
  private configOptions;
267
297
  }
package/dist/sandbox.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { Commands } from './commands.js';
2
2
  import { ConnectionConfig, SESSION_OPERATION_REQUEST_TIMEOUT_MS } from './connectionConfig.js';
3
- import { DataPlaneClient, ControlClient } from './transport.js';
3
+ import { DataPlaneClient, ControlClient, withQuery } from './transport.js';
4
4
  import { ConflictError, FileNotFoundError, NotFoundError, SandboxError, unsupported } from './errors.js';
5
5
  import { Filesystem } from './filesystem.js';
6
6
  import { Git } from './git.js';
@@ -122,9 +122,7 @@ export class Sandbox {
122
122
  const sandboxOpts = typeof templateOrOpts === 'string' ? opts : templateOrOpts ?? {};
123
123
  const template = typeof templateOrOpts === 'string'
124
124
  ? templateOrOpts
125
- : templateOrOpts?.template ?? (sandboxOpts.mcp === undefined ? Sandbox.defaultTemplate : undefined);
126
- if (sandboxOpts.volumeMounts !== undefined)
127
- unsupported('volumeMounts');
125
+ : templateOrOpts?.template ?? (sandboxOpts.mcp === undefined ? this.defaultTemplate : undefined);
128
126
  const config = new ConnectionConfig(sandboxOpts);
129
127
  const control = new ControlClient(config);
130
128
  const sandboxPayload = {
@@ -136,6 +134,8 @@ export class Sandbox {
136
134
  };
137
135
  putIfPresent(sandboxPayload, 'template_id', template);
138
136
  putIfPresent(sandboxPayload, 'mcp', sandboxOpts.mcp);
137
+ putIfPresent(sandboxPayload, 'lifecycle', lifecyclePayload(sandboxOpts.lifecycle));
138
+ putIfPresent(sandboxPayload, 'volume_mounts', volumeMountsPayload(sandboxOpts.volumeMounts));
139
139
  Object.assign(sandboxPayload, networkUpdatePayload(sandboxOpts.network));
140
140
  putIfPresent(sandboxPayload, 'team', sandboxOpts.team);
141
141
  const response = await control.post('/sandboxes', {
@@ -146,7 +146,7 @@ export class Sandbox {
146
146
  const sandboxId = sandbox.id ?? sandbox.sandbox_id;
147
147
  if (sandboxId === undefined)
148
148
  throw new SandboxError('create response did not include sandbox id');
149
- const sandboxInstance = new Sandbox({
149
+ const sandboxInstance = new this({
150
150
  sandboxId: String(sandboxId),
151
151
  connectionConfig: config,
152
152
  control,
@@ -165,7 +165,7 @@ export class Sandbox {
165
165
  json: opts.timeoutMs ? { timeout: Math.ceil(opts.timeoutMs / 1000) } : {},
166
166
  requestTimeoutMs: sessionOperationRequestTimeout(config, opts),
167
167
  });
168
- return new Sandbox({
168
+ return new this({
169
169
  sandboxId,
170
170
  connectionConfig: config,
171
171
  control,
@@ -229,7 +229,7 @@ export class Sandbox {
229
229
  /** Fetch sandbox metrics by id. */
230
230
  static async getMetrics(sandboxId, opts = {}) {
231
231
  const control = new ControlClient(new ConnectionConfig(opts));
232
- const payload = await control.get(`/sandboxes/${sandboxId}/metrics`, {
232
+ const payload = await control.get(metricsPath(sandboxId, opts), {
233
233
  requestTimeoutMs: opts.requestTimeoutMs,
234
234
  });
235
235
  return metricsList(payload.metrics ?? payload);
@@ -458,6 +458,25 @@ export class Sandbox {
458
458
  });
459
459
  return fileUrlInfo(record(payload.file_url ?? payload));
460
460
  }
461
+ /** POST JSON to the sandbox data-plane runtime API. */
462
+ async runtimePostJson(path, json, opts = {}) {
463
+ return this.dataPlane.postJson(path, {
464
+ json,
465
+ requestTimeoutMs: opts.requestTimeoutMs,
466
+ });
467
+ }
468
+ /** GET JSON from the sandbox data-plane runtime API. */
469
+ async runtimeGetJson(path, opts = {}) {
470
+ return this.dataPlane.getJson(path, {
471
+ requestTimeoutMs: opts.requestTimeoutMs,
472
+ });
473
+ }
474
+ /** DELETE JSON from the sandbox data-plane runtime API. */
475
+ async runtimeDeleteJson(path, opts = {}) {
476
+ return this.dataPlane.deleteJson(path, {
477
+ requestTimeoutMs: opts.requestTimeoutMs,
478
+ });
479
+ }
461
480
  configOptions() {
462
481
  return {
463
482
  apiKey: this.config.apiKey,
@@ -508,6 +527,15 @@ function snapshotListPath(opts, nextToken) {
508
527
  const query = params.toString();
509
528
  return query ? `/sandbox_snapshots?${query}` : '/sandbox_snapshots';
510
529
  }
530
+ function metricsPath(sandboxId, opts) {
531
+ return withQuery(`/sandboxes/${sandboxId}/metrics`, {
532
+ start: dateTimestampSeconds(opts.start),
533
+ end: dateTimestampSeconds(opts.end),
534
+ });
535
+ }
536
+ function dateTimestampSeconds(value) {
537
+ return value === undefined ? undefined : Math.round(value.getTime() / 1000);
538
+ }
511
539
  function fileUrlInfo(payload) {
512
540
  return {
513
541
  method: String(payload.method ?? ''),
@@ -531,6 +559,8 @@ function sandboxInfo(payload) {
531
559
  templateId: typeof payload.template_id === 'string' ? payload.template_id : templateSlug(payload.template),
532
560
  name: typeof payload.name === 'string' ? payload.name : undefined,
533
561
  state: typeof payload.state === 'string' ? payload.state : undefined,
562
+ lifecycle: sandboxLifecycleInfo(payload.lifecycle),
563
+ volumeMounts: volumeMountsInfo(payload.volume_mounts ?? payload.volumeMounts),
534
564
  metadata: recordOfStrings(payload.metadata),
535
565
  startedAt: typeof payload.started_at === 'string'
536
566
  ? payload.started_at
@@ -540,6 +570,45 @@ function sandboxInfo(payload) {
540
570
  : typeof payload.deadline_at === 'string' ? payload.deadline_at : undefined,
541
571
  };
542
572
  }
573
+ function lifecyclePayload(lifecycle) {
574
+ if (lifecycle === undefined)
575
+ return undefined;
576
+ const onTimeout = lifecycle.onTimeout ?? 'kill';
577
+ const autoResume = lifecycle.autoResume ?? false;
578
+ if (autoResume && onTimeout !== 'pause') {
579
+ throw new SandboxError("lifecycle.autoResume can only be true when lifecycle.onTimeout is 'pause'");
580
+ }
581
+ return { on_timeout: onTimeout, auto_resume: autoResume };
582
+ }
583
+ function volumeMountsPayload(volumeMounts) {
584
+ if (volumeMounts === undefined)
585
+ return undefined;
586
+ return Object.entries(volumeMounts).map(([path, volume]) => ({
587
+ path,
588
+ name: typeof volume === 'string' ? volume : volume.name,
589
+ }));
590
+ }
591
+ function volumeMountsInfo(value) {
592
+ if (!Array.isArray(value))
593
+ return undefined;
594
+ return value
595
+ .map((item) => {
596
+ const entry = record(item);
597
+ return { name: String(entry.name ?? ''), path: String(entry.path ?? '') };
598
+ })
599
+ .filter((entry) => entry.name !== '' && entry.path !== '');
600
+ }
601
+ function sandboxLifecycleInfo(value) {
602
+ const lifecycle = record(value);
603
+ const onTimeout = stringValue(lifecycle.on_timeout ?? lifecycle.onTimeout);
604
+ const autoResume = booleanValue(lifecycle.auto_resume ?? lifecycle.autoResume);
605
+ if (onTimeout === undefined && autoResume === undefined)
606
+ return undefined;
607
+ return {
608
+ onTimeout: onTimeout ?? 'kill',
609
+ autoResume: autoResume ?? false,
610
+ };
611
+ }
543
612
  function metricsList(value) {
544
613
  if (Array.isArray(value))
545
614
  return value.map((item) => metricsInfo(record(item)));
@@ -589,6 +658,15 @@ function stringValue(value) {
589
658
  function numberValue(value) {
590
659
  return typeof value === 'number' ? value : undefined;
591
660
  }
661
+ function booleanValue(value) {
662
+ if (typeof value === 'boolean')
663
+ return value;
664
+ if (value === 'true' || value === '1')
665
+ return true;
666
+ if (value === 'false' || value === '0')
667
+ return false;
668
+ return undefined;
669
+ }
592
670
  function templateSlug(value) {
593
671
  const template = record(value);
594
672
  return typeof template.slug === 'string' ? template.slug : undefined;
@@ -0,0 +1,120 @@
1
+ import { Blob } from 'node:buffer';
2
+ import { ConnectionConfig, type ConnectionOpts } from './connectionConfig.js';
3
+ import { ControlClient } from './transport.js';
4
+ export type VolumeFileType = 'file' | 'directory' | 'symlink' | string;
5
+ export type VolumeReadFormat = 'text' | 'bytes' | 'blob' | 'stream';
6
+ export type VolumeWriteData = string | Uint8Array | ArrayBuffer | Blob;
7
+ /** Control-plane metadata for a persistent Watasu volume. */
8
+ export interface VolumeInfo {
9
+ volumeId: string;
10
+ id: string;
11
+ name: string;
12
+ state?: string;
13
+ token?: string;
14
+ sizeMb?: number;
15
+ sizeBytes?: number;
16
+ node?: string;
17
+ metadata: Record<string, string>;
18
+ createdAt?: string;
19
+ updatedAt?: string;
20
+ raw: Record<string, unknown>;
21
+ }
22
+ /** File or directory metadata returned by volume content operations. */
23
+ export interface VolumeEntryStat {
24
+ path: string;
25
+ name: string;
26
+ type: VolumeFileType;
27
+ size?: number;
28
+ mode?: number;
29
+ uid?: number;
30
+ gid?: number;
31
+ atime?: unknown;
32
+ mtime?: unknown;
33
+ ctime?: unknown;
34
+ raw: Record<string, unknown>;
35
+ }
36
+ export interface VolumeApiParams extends ConnectionOpts {
37
+ team?: string;
38
+ }
39
+ export interface VolumeConnectionConfig extends ConnectionOpts {
40
+ }
41
+ export interface VolumeListOpts extends ConnectionOpts {
42
+ team?: string;
43
+ }
44
+ export interface VolumeListFilesOpts extends ConnectionOpts {
45
+ depth?: number;
46
+ }
47
+ export interface VolumeReadFileOpts extends ConnectionOpts {
48
+ format?: VolumeReadFormat;
49
+ }
50
+ export interface VolumeWriteFileOpts extends ConnectionOpts {
51
+ uid?: number;
52
+ gid?: number;
53
+ mode?: number | string;
54
+ force?: boolean;
55
+ }
56
+ export interface VolumeMetadataOpts extends ConnectionOpts {
57
+ uid?: number;
58
+ gid?: number;
59
+ mode?: number | string;
60
+ }
61
+ /** Persistent volume that can be mounted into sandboxes and edited while detached. */
62
+ export declare class Volume {
63
+ readonly volumeId: string;
64
+ readonly id: string;
65
+ readonly name: string;
66
+ readonly token?: string;
67
+ private readonly config;
68
+ private readonly control;
69
+ constructor(opts: {
70
+ volumeId: string;
71
+ name?: string;
72
+ token?: string;
73
+ connectionConfig: ConnectionConfig;
74
+ control?: ControlClient;
75
+ });
76
+ /** Create a persistent volume and return a connected SDK object. */
77
+ static create(name: string, opts?: VolumeApiParams): Promise<Volume>;
78
+ /** Connect to an existing volume by id or name. */
79
+ static connect(volumeId: string, opts?: VolumeConnectionConfig): Promise<Volume>;
80
+ /** Fetch metadata for an existing volume by id or name. */
81
+ static getInfo(volumeId: string, opts?: VolumeConnectionConfig): Promise<VolumeInfo>;
82
+ /** List volumes visible to the configured API key. */
83
+ static list(opts?: VolumeListOpts): Promise<VolumeInfo[]>;
84
+ /** Destroy a volume by id or name. Returns false when it does not exist. */
85
+ static destroy(volumeId: string, opts?: VolumeConnectionConfig): Promise<boolean>;
86
+ /** Alias for `destroy`. */
87
+ static delete(volumeId: string, opts?: VolumeConnectionConfig): Promise<boolean>;
88
+ /** Fetch this volume's latest metadata. */
89
+ getInfo(): Promise<VolumeInfo>;
90
+ /** Fetch metadata for a path inside this volume. */
91
+ getInfo(path: string, opts?: ConnectionOpts): Promise<VolumeEntryStat>;
92
+ /** List files and directories under `path`. */
93
+ list(path?: string, opts?: VolumeListFilesOpts): Promise<VolumeEntryStat[]>;
94
+ /** Create a directory inside the detached volume. */
95
+ makeDir(path: string, opts?: VolumeWriteFileOpts): Promise<VolumeEntryStat>;
96
+ /** Return whether a path exists inside the detached volume. */
97
+ exists(path: string, opts?: ConnectionOpts): Promise<boolean>;
98
+ /** Update ownership or mode metadata for a path. */
99
+ updateMetadata(path: string, opts?: VolumeMetadataOpts): Promise<VolumeEntryStat>;
100
+ /** Read a file from the detached volume. */
101
+ readFile(path: string, opts: VolumeReadFileOpts & {
102
+ format: 'bytes';
103
+ }): Promise<Uint8Array>;
104
+ readFile(path: string, opts: VolumeReadFileOpts & {
105
+ format: 'blob';
106
+ }): Promise<Blob>;
107
+ readFile(path: string, opts: VolumeReadFileOpts & {
108
+ format: 'stream';
109
+ }): Promise<ReadableStream<Uint8Array>>;
110
+ readFile(path: string, opts?: VolumeReadFileOpts): Promise<string>;
111
+ /** Write a file into the detached volume. */
112
+ writeFile(path: string, data: VolumeWriteData, opts?: VolumeWriteFileOpts): Promise<VolumeEntryStat>;
113
+ /** Remove a file or directory from the detached volume. */
114
+ remove(path: string, opts?: ConnectionOpts): Promise<boolean>;
115
+ /** Destroy this volume. Returns false when it no longer exists. */
116
+ destroy(opts?: ConnectionOpts): Promise<boolean>;
117
+ /** Alias for `destroy`. */
118
+ delete(opts?: ConnectionOpts): Promise<boolean>;
119
+ private configOptions;
120
+ }
package/dist/volume.js ADDED
@@ -0,0 +1,267 @@
1
+ import { Blob } from 'node:buffer';
2
+ import { ConnectionConfig } from './connectionConfig.js';
3
+ import { NotFoundError, SandboxError } from './errors.js';
4
+ import { base64DecodeBytes, base64DecodeText, base64Encode } from './processSocket.js';
5
+ import { ControlClient, withQuery } from './transport.js';
6
+ /** Persistent volume that can be mounted into sandboxes and edited while detached. */
7
+ export class Volume {
8
+ volumeId;
9
+ id;
10
+ name;
11
+ token;
12
+ config;
13
+ control;
14
+ constructor(opts) {
15
+ this.volumeId = String(opts.volumeId);
16
+ this.id = this.volumeId;
17
+ this.name = opts.name ?? this.volumeId;
18
+ this.token = opts.token;
19
+ this.config = opts.connectionConfig;
20
+ this.control = opts.control ?? new ControlClient(this.config);
21
+ }
22
+ /** Create a persistent volume and return a connected SDK object. */
23
+ static async create(name, opts = {}) {
24
+ const config = new ConnectionConfig(opts);
25
+ const control = new ControlClient(config);
26
+ const payload = await control.post('/volumes', {
27
+ json: compactRecord({ name, team: opts.team }),
28
+ requestTimeoutMs: opts.requestTimeoutMs,
29
+ });
30
+ return volumeFromPayload(payload, config, control);
31
+ }
32
+ /** Connect to an existing volume by id or name. */
33
+ static async connect(volumeId, opts = {}) {
34
+ const config = new ConnectionConfig(opts);
35
+ const control = new ControlClient(config);
36
+ const payload = await control.get(`/volumes/${encodeURIComponent(volumeId)}`, {
37
+ requestTimeoutMs: opts.requestTimeoutMs,
38
+ });
39
+ return volumeFromPayload(payload, config, control);
40
+ }
41
+ /** Fetch metadata for an existing volume by id or name. */
42
+ static async getInfo(volumeId, opts = {}) {
43
+ const config = new ConnectionConfig(opts);
44
+ const control = new ControlClient(config);
45
+ const payload = await control.get(`/volumes/${encodeURIComponent(volumeId)}`, {
46
+ requestTimeoutMs: opts.requestTimeoutMs,
47
+ });
48
+ return volumeInfo(record(payload.volume ?? payload));
49
+ }
50
+ /** List volumes visible to the configured API key. */
51
+ static async list(opts = {}) {
52
+ const config = new ConnectionConfig(opts);
53
+ const control = new ControlClient(config);
54
+ const path = withQuery('/volumes', { team: opts.team });
55
+ const payload = await control.get(path, { requestTimeoutMs: opts.requestTimeoutMs });
56
+ const volumes = Array.isArray(payload.volumes) ? payload.volumes : [];
57
+ return volumes.map((item) => volumeInfo(record(item)));
58
+ }
59
+ /** Destroy a volume by id or name. Returns false when it does not exist. */
60
+ static async destroy(volumeId, opts = {}) {
61
+ const config = new ConnectionConfig(opts);
62
+ const control = new ControlClient(config);
63
+ try {
64
+ await control.delete(`/volumes/${encodeURIComponent(volumeId)}`, {
65
+ requestTimeoutMs: opts.requestTimeoutMs,
66
+ });
67
+ return true;
68
+ }
69
+ catch (error) {
70
+ if (error instanceof NotFoundError)
71
+ return false;
72
+ throw error;
73
+ }
74
+ }
75
+ /** Alias for `destroy`. */
76
+ static async delete(volumeId, opts = {}) {
77
+ return this.destroy(volumeId, opts);
78
+ }
79
+ async getInfo(path, opts = {}) {
80
+ if (path === undefined) {
81
+ return Volume.getInfo(this.volumeId, this.configOptions(opts));
82
+ }
83
+ const payload = await this.control.get(withQuery(`/volumes/${this.volumeId}/path`, { path }), {
84
+ requestTimeoutMs: opts.requestTimeoutMs,
85
+ });
86
+ return volumeEntry(record(payload.file ?? payload));
87
+ }
88
+ /** List files and directories under `path`. */
89
+ async list(path = '/', opts = {}) {
90
+ const payload = await this.control.get(withQuery(`/volumes/${this.volumeId}/directories`, {
91
+ path,
92
+ depth: opts.depth,
93
+ }), {
94
+ requestTimeoutMs: opts.requestTimeoutMs,
95
+ });
96
+ const entries = Array.isArray(payload.entries) ? payload.entries : [];
97
+ return entries.map((item) => volumeEntry(record(item)));
98
+ }
99
+ /** Create a directory inside the detached volume. */
100
+ async makeDir(path, opts = {}) {
101
+ const payload = await this.control.post(`/volumes/${this.volumeId}/directories`, {
102
+ json: compactRecord({ path, ...metadataPayload(opts) }),
103
+ requestTimeoutMs: opts.requestTimeoutMs,
104
+ });
105
+ return volumeEntry(record(payload.file ?? payload));
106
+ }
107
+ /** Return whether a path exists inside the detached volume. */
108
+ async exists(path, opts = {}) {
109
+ try {
110
+ await this.getInfo(path, opts);
111
+ return true;
112
+ }
113
+ catch (error) {
114
+ if (error instanceof NotFoundError)
115
+ return false;
116
+ throw error;
117
+ }
118
+ }
119
+ /** Update ownership or mode metadata for a path. */
120
+ async updateMetadata(path, opts = {}) {
121
+ const payload = await this.control.patch(`/volumes/${this.volumeId}/path`, {
122
+ json: compactRecord({ path, ...metadataPayload(opts) }),
123
+ requestTimeoutMs: opts.requestTimeoutMs,
124
+ });
125
+ return volumeEntry(record(payload.file ?? payload));
126
+ }
127
+ async readFile(path, opts = {}) {
128
+ const payload = await this.control.get(withQuery(`/volumes/${this.volumeId}/files`, { path }), {
129
+ requestTimeoutMs: opts.requestTimeoutMs,
130
+ });
131
+ const file = record(payload.file ?? payload);
132
+ const content = file.content_b64 ?? file.contentBase64 ?? file.content ?? '';
133
+ switch (opts.format ?? 'text') {
134
+ case 'bytes':
135
+ return base64DecodeBytes(content);
136
+ case 'blob':
137
+ return new Blob([base64DecodeBytes(content)]);
138
+ case 'stream':
139
+ return new Blob([base64DecodeBytes(content)]).stream();
140
+ case 'text':
141
+ return file.content_b64 || file.contentBase64 ? base64DecodeText(content) : String(content);
142
+ default:
143
+ throw new SandboxError(`unsupported volume read format: ${String(opts.format)}`);
144
+ }
145
+ }
146
+ /** Write a file into the detached volume. */
147
+ async writeFile(path, data, opts = {}) {
148
+ const bytes = await bytesFromWriteData(data);
149
+ const payload = await this.control.put(`/volumes/${this.volumeId}/files`, {
150
+ json: compactRecord({
151
+ path,
152
+ content_b64: base64Encode(bytes),
153
+ ...metadataPayload(opts),
154
+ force: opts.force,
155
+ }),
156
+ requestTimeoutMs: opts.requestTimeoutMs,
157
+ });
158
+ return volumeEntry(record(payload.file ?? payload));
159
+ }
160
+ /** Remove a file or directory from the detached volume. */
161
+ async remove(path, opts = {}) {
162
+ await this.control.delete(withQuery(`/volumes/${this.volumeId}/path`, { path }), {
163
+ requestTimeoutMs: opts.requestTimeoutMs,
164
+ });
165
+ return true;
166
+ }
167
+ /** Destroy this volume. Returns false when it no longer exists. */
168
+ async destroy(opts = {}) {
169
+ return Volume.destroy(this.volumeId, this.configOptions(opts));
170
+ }
171
+ /** Alias for `destroy`. */
172
+ async delete(opts = {}) {
173
+ return this.destroy(opts);
174
+ }
175
+ configOptions(opts = {}) {
176
+ return {
177
+ apiKey: this.config.apiKey,
178
+ apiUrl: this.config.apiUrl,
179
+ dataPlaneDomain: this.config.dataPlaneDomain,
180
+ requestTimeoutMs: this.config.requestTimeoutMs,
181
+ ...opts,
182
+ };
183
+ }
184
+ }
185
+ function volumeFromPayload(payload, config, control) {
186
+ const info = volumeInfo(record(payload.volume ?? payload));
187
+ return new Volume({
188
+ volumeId: info.volumeId,
189
+ name: info.name,
190
+ token: info.token,
191
+ connectionConfig: config,
192
+ control,
193
+ });
194
+ }
195
+ function volumeInfo(payload) {
196
+ const id = payload.volume_id ?? payload.volumeId ?? payload.id;
197
+ if (id === undefined)
198
+ throw new SandboxError('volume response did not include id');
199
+ return {
200
+ volumeId: String(id),
201
+ id: String(id),
202
+ name: String(payload.name ?? id),
203
+ state: stringValue(payload.state),
204
+ token: stringValue(payload.token),
205
+ sizeMb: numberValue(payload.size_mb ?? payload.sizeMb),
206
+ sizeBytes: numberValue(payload.size_bytes ?? payload.sizeBytes),
207
+ node: stringValue(payload.node ?? payload.node_name ?? payload.nodeName),
208
+ metadata: recordOfStrings(payload.metadata),
209
+ createdAt: stringValue(payload.created_at ?? payload.createdAt),
210
+ updatedAt: stringValue(payload.updated_at ?? payload.updatedAt),
211
+ raw: payload,
212
+ };
213
+ }
214
+ function volumeEntry(payload) {
215
+ return {
216
+ path: String(payload.path ?? ''),
217
+ name: String(payload.name ?? ''),
218
+ type: String(payload.type ?? 'file'),
219
+ size: numberValue(payload.size ?? payload.bytes),
220
+ mode: numberValue(payload.mode),
221
+ uid: numberValue(payload.uid),
222
+ gid: numberValue(payload.gid),
223
+ atime: payload.atime,
224
+ mtime: payload.mtime,
225
+ ctime: payload.ctime,
226
+ raw: payload,
227
+ };
228
+ }
229
+ async function bytesFromWriteData(data) {
230
+ if (typeof data === 'string')
231
+ return new TextEncoder().encode(data);
232
+ if (data instanceof Uint8Array)
233
+ return data;
234
+ if (data instanceof ArrayBuffer)
235
+ return new Uint8Array(data);
236
+ if (data instanceof Blob)
237
+ return new Uint8Array(await data.arrayBuffer());
238
+ throw new SandboxError('unsupported volume write data');
239
+ }
240
+ function metadataPayload(opts) {
241
+ return compactRecord({
242
+ uid: opts.uid,
243
+ gid: opts.gid,
244
+ mode: opts.mode,
245
+ });
246
+ }
247
+ function compactRecord(payload) {
248
+ return Object.fromEntries(Object.entries(payload).filter(([, value]) => value !== undefined));
249
+ }
250
+ function record(value) {
251
+ return value && typeof value === 'object' ? value : {};
252
+ }
253
+ function recordOfStrings(value) {
254
+ if (!value || typeof value !== 'object')
255
+ return {};
256
+ return Object.fromEntries(Object.entries(value).map(([key, item]) => [key, String(item)]));
257
+ }
258
+ function stringValue(value) {
259
+ if (typeof value === 'string')
260
+ return value;
261
+ if (typeof value === 'number')
262
+ return String(value);
263
+ return undefined;
264
+ }
265
+ function numberValue(value) {
266
+ return typeof value === 'number' ? value : undefined;
267
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watasu/sdk",
3
- "version": "0.1.24",
3
+ "version": "0.1.30",
4
4
  "type": "module",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "description": "TypeScript SDK for Watasu",
@@ -11,6 +11,10 @@
11
11
  ".": {
12
12
  "types": "./dist/index.d.ts",
13
13
  "import": "./dist/index.js"
14
+ },
15
+ "./code-interpreter": {
16
+ "types": "./dist/codeInterpreter.d.ts",
17
+ "import": "./dist/codeInterpreter.js"
14
18
  }
15
19
  },
16
20
  "types": "./dist/index.d.ts",