@watasu/sdk 0.1.24 → 0.1.25

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,24 @@ await sbx.betaPause()
31
31
  await sbx.resume({ timeoutMs: 300_000 })
32
32
  ```
33
33
 
34
+ ## Code Interpreter
35
+
36
+ ```ts
37
+ import { Sandbox } from '@watasu/sdk/code-interpreter'
38
+
39
+ const sbx = await Sandbox.create()
40
+ const execution = await sbx.runCode("print('hello')\n2 + 3", {
41
+ language: 'python',
42
+ onStdout: (message) => console.log(message.line),
43
+ })
44
+
45
+ console.log(execution.text)
46
+ await sbx.kill()
47
+ ```
48
+
49
+ `@watasu/sdk/code-interpreter` starts the `code-interpreter` template by default
50
+ and returns structured `results`, `logs`, and `error` fields for each execution.
51
+
34
52
  ## MCP Gateway
35
53
 
36
54
  ```ts
@@ -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;
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, _opts?: {
90
+ requestTimeoutMs?: number;
91
+ }): Promise<boolean>;
92
+ /** List persistent code contexts. */
93
+ listCodeContexts(_opts?: {
94
+ requestTimeoutMs?: number;
95
+ }): Promise<Context[]>;
96
+ /** Restart a persistent code context. */
97
+ restartCodeContext(_context: Context, _opts?: {
98
+ requestTimeoutMs?: number;
99
+ }): Promise<Context>;
100
+ }
@@ -0,0 +1,241 @@
1
+ import { InvalidArgumentError, NotImplementedError } 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
+ throw new NotImplementedError('code contexts are not supported by Watasu yet');
176
+ }
177
+ /** Remove a persistent code context. */
178
+ async removeCodeContext(_context, _opts = {}) {
179
+ throw new NotImplementedError('code contexts are not supported by Watasu yet');
180
+ }
181
+ /** List persistent code contexts. */
182
+ async listCodeContexts(_opts = {}) {
183
+ throw new NotImplementedError('code contexts are not supported by Watasu yet');
184
+ }
185
+ /** Restart a persistent code context. */
186
+ async restartCodeContext(_context, _opts = {}) {
187
+ throw new NotImplementedError('code contexts are not supported by Watasu yet');
188
+ }
189
+ }
190
+ function executionFromApi(payload) {
191
+ const execution = record(payload.execution ?? payload);
192
+ const logs = record(execution.logs);
193
+ return new Execution(arrayOfRecords(execution.results).map((item) => new Result(item)), {
194
+ stdout: arrayOfUnknown(logs.stdout).map((item) => outputMessageFromApi(item, false)),
195
+ stderr: arrayOfUnknown(logs.stderr).map((item) => outputMessageFromApi(item, true)),
196
+ }, executionErrorFromApi(execution.error), numberValue(execution.execution_count ?? execution.executionCount));
197
+ }
198
+ function outputMessageFromApi(value, error) {
199
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
200
+ const item = value;
201
+ return new OutputMessage(String(item.line ?? ''), numberValue(item.timestamp) ?? Date.now() / 1000, Boolean(item.error ?? error));
202
+ }
203
+ return new OutputMessage(String(value), Date.now() / 1000, error);
204
+ }
205
+ function executionErrorFromApi(value) {
206
+ if (!value || typeof value !== 'object' || Array.isArray(value))
207
+ return undefined;
208
+ const item = value;
209
+ return new ExecutionError(String(item.name ?? ''), String(item.value ?? ''), String(item.traceback ?? ''));
210
+ }
211
+ function emitCallbacks(execution, opts) {
212
+ for (const message of execution.logs.stdout)
213
+ opts.onStdout?.(message);
214
+ for (const message of execution.logs.stderr)
215
+ opts.onStderr?.(message);
216
+ for (const result of execution.results)
217
+ opts.onResult?.(result);
218
+ if (execution.error !== undefined)
219
+ opts.onError?.(execution.error);
220
+ }
221
+ function contextId(context) {
222
+ return context?.id;
223
+ }
224
+ function compactRecord(payload) {
225
+ return Object.fromEntries(Object.entries(payload).filter(([, value]) => value !== undefined));
226
+ }
227
+ function record(value) {
228
+ return value && typeof value === 'object' && !Array.isArray(value) ? value : {};
229
+ }
230
+ function arrayOfRecords(value) {
231
+ return Array.isArray(value) ? value.map((item) => record(item)) : [];
232
+ }
233
+ function arrayOfUnknown(value) {
234
+ return Array.isArray(value) ? value : [];
235
+ }
236
+ function stringValue(value) {
237
+ return typeof value === 'string' ? value : undefined;
238
+ }
239
+ function numberValue(value) {
240
+ return typeof value === 'number' ? value : undefined;
241
+ }
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 { Sandbox as CodeInterpreterSandbox } from './codeInterpreter.js';
4
5
  export type { CreateSnapshotOpts, RestoreSnapshotOpts, SandboxCreateOpts, SandboxConnectOpts, SandboxInfo, SandboxListOpts, SandboxMetrics, 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';
package/dist/index.js CHANGED
@@ -1,6 +1,8 @@
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';
package/dist/sandbox.d.ts CHANGED
@@ -142,9 +142,9 @@ export declare class SandboxPaginator {
142
142
  /** Running Watasu sandbox with ready `files` and `commands` helpers. */
143
143
  export declare class Sandbox {
144
144
  /** Default template slug used when create is called without a template. */
145
- static readonly defaultTemplate = "base";
145
+ static readonly defaultTemplate: string;
146
146
  /** Default template slug used by MCP creation once Watasu supports it. */
147
- static readonly defaultMcpTemplate = "mcp-gateway";
147
+ static readonly defaultMcpTemplate: string;
148
148
  /** Default sandbox lifetime in milliseconds. */
149
149
  static readonly defaultSandboxTimeoutMs = 300000;
150
150
  files: Filesystem;
@@ -263,5 +263,7 @@ export declare class Sandbox {
263
263
  /** Resume this sandbox and refresh its data-plane session. */
264
264
  resume(opts?: SandboxConnectOpts): Promise<boolean>;
265
265
  private fileUrl;
266
+ /** POST JSON to the sandbox data-plane runtime API. */
267
+ protected runtimePostJson(path: string, json: Record<string, unknown>, opts?: ConnectionOpts): Promise<Record<string, unknown>>;
266
268
  private configOptions;
267
269
  }
package/dist/sandbox.js CHANGED
@@ -122,7 +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);
125
+ : templateOrOpts?.template ?? (sandboxOpts.mcp === undefined ? this.defaultTemplate : undefined);
126
126
  if (sandboxOpts.volumeMounts !== undefined)
127
127
  unsupported('volumeMounts');
128
128
  const config = new ConnectionConfig(sandboxOpts);
@@ -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,
@@ -458,6 +458,13 @@ 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
+ }
461
468
  configOptions() {
462
469
  return {
463
470
  apiKey: this.config.apiKey,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@watasu/sdk",
3
- "version": "0.1.24",
3
+ "version": "0.1.25",
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",