@blaxel/core 0.2.49-preview.111 → 0.2.49-preview.112

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.
@@ -0,0 +1,396 @@
1
+ import { logger } from "../common/logger.js";
2
+ import { settings } from "../common/settings.js";
3
+ import { SandboxInstance } from "./sandbox.js";
4
+ export class CodeInterpreter extends SandboxInstance {
5
+ static DEFAULT_IMAGE = "blaxel/jupyter-server";
6
+ static DEFAULT_PORTS = [
7
+ { name: "jupyter", target: 8888, protocol: "HTTP" },
8
+ ];
9
+ static DEFAULT_LIFECYCLE = {
10
+ expirationPolicies: [{ type: "ttl-idle", value: "30m", action: "delete" }],
11
+ };
12
+ _sandboxConfig;
13
+ constructor(sandbox) {
14
+ super(sandbox);
15
+ this._sandboxConfig = sandbox;
16
+ }
17
+ static async get(sandboxName) {
18
+ const base = await SandboxInstance.get(sandboxName);
19
+ // Create a minimal config - the base instance already has the sandbox data
20
+ // We'll rely on the process property for URL/headers access
21
+ const config = {
22
+ metadata: base.metadata,
23
+ spec: base.spec,
24
+ status: base.status,
25
+ events: base.events,
26
+ };
27
+ return new CodeInterpreter(config);
28
+ }
29
+ static async create(sandbox, { safe = true } = {}) {
30
+ const payload = {
31
+ image: CodeInterpreter.DEFAULT_IMAGE,
32
+ ports: CodeInterpreter.DEFAULT_PORTS,
33
+ lifecycle: CodeInterpreter.DEFAULT_LIFECYCLE,
34
+ };
35
+ const allowedCopyKeys = new Set(["name", "envs", "memory", "region", "headers"]);
36
+ if (sandbox && typeof sandbox === "object") {
37
+ if (Array.isArray(sandbox)) {
38
+ // Skip arrays
39
+ }
40
+ else if ("metadata" in sandbox || "spec" in sandbox) {
41
+ // It's a Sandbox object
42
+ const sandboxObj = sandbox;
43
+ if (sandboxObj.metadata?.name) {
44
+ payload["name"] = sandboxObj.metadata.name;
45
+ }
46
+ if (sandboxObj.spec?.runtime) {
47
+ if (sandboxObj.spec.runtime.envs) {
48
+ payload["envs"] = sandboxObj.spec.runtime.envs;
49
+ }
50
+ if (sandboxObj.spec.runtime.memory) {
51
+ payload["memory"] = sandboxObj.spec.runtime.memory;
52
+ }
53
+ }
54
+ if (sandboxObj.spec?.region) {
55
+ payload["region"] = sandboxObj.spec.region;
56
+ }
57
+ }
58
+ else if ("name" in sandbox || "image" in sandbox || "memory" in sandbox) {
59
+ // It's a SandboxCreateConfiguration or dict-like object
60
+ const sandboxDict = sandbox;
61
+ for (const k of allowedCopyKeys) {
62
+ const value = sandboxDict[k];
63
+ if (value !== null && value !== undefined) {
64
+ payload[k] = value;
65
+ }
66
+ }
67
+ }
68
+ }
69
+ const baseInstance = await SandboxInstance.create(payload, { safe });
70
+ // Create config from the instance - preserve any forceUrl/headers if provided in input
71
+ const config = {
72
+ metadata: baseInstance.metadata,
73
+ spec: baseInstance.spec,
74
+ status: baseInstance.status,
75
+ events: baseInstance.events,
76
+ };
77
+ // Preserve forceUrl and headers from input if it was a dict-like object
78
+ if (sandbox && typeof sandbox === "object" && !Array.isArray(sandbox)) {
79
+ if ("forceUrl" in sandbox && typeof sandbox.forceUrl === "string") {
80
+ config.forceUrl = sandbox.forceUrl;
81
+ }
82
+ if ("headers" in sandbox && typeof sandbox.headers === "object") {
83
+ config.headers = sandbox.headers;
84
+ }
85
+ if ("params" in sandbox && typeof sandbox.params === "object") {
86
+ config.params = sandbox.params;
87
+ }
88
+ }
89
+ return new CodeInterpreter(config);
90
+ }
91
+ get _jupyterUrl() {
92
+ return this.process.url;
93
+ }
94
+ static OutputMessage = class {
95
+ text;
96
+ timestamp;
97
+ isStderr;
98
+ constructor(text, timestamp, isStderr) {
99
+ this.text = text;
100
+ this.timestamp = timestamp;
101
+ this.isStderr = isStderr;
102
+ }
103
+ };
104
+ static Result = class {
105
+ constructor(kwargs = {}) {
106
+ for (const [k, v] of Object.entries(kwargs)) {
107
+ this[k] = v;
108
+ }
109
+ }
110
+ };
111
+ static ExecutionError = class {
112
+ name;
113
+ value;
114
+ traceback;
115
+ constructor(name, value, traceback) {
116
+ this.name = name;
117
+ this.value = value;
118
+ this.traceback = traceback;
119
+ }
120
+ };
121
+ static Logs = class {
122
+ stdout = [];
123
+ stderr = [];
124
+ };
125
+ static Execution = class {
126
+ results = [];
127
+ logs = new CodeInterpreter.Logs();
128
+ error = null;
129
+ executionCount = null;
130
+ };
131
+ static Context = class {
132
+ id;
133
+ constructor(id) {
134
+ this.id = id;
135
+ }
136
+ static fromJson(data) {
137
+ return new CodeInterpreter.Context(String(data.id || data.context_id || ""));
138
+ }
139
+ };
140
+ _parseOutput(execution, output, onStdout, onStderr, onResult, onError) {
141
+ let data;
142
+ try {
143
+ data = JSON.parse(output);
144
+ }
145
+ catch {
146
+ // Fallback: treat as stdout text-only message
147
+ execution.logs.stdout.push(output);
148
+ if (onStdout) {
149
+ return onStdout(new CodeInterpreter.OutputMessage(output, null, false));
150
+ }
151
+ return null;
152
+ }
153
+ let dataType = "";
154
+ if (typeof data.type === "string") {
155
+ dataType = data.type;
156
+ }
157
+ else if (data.type !== null &&
158
+ data.type !== undefined &&
159
+ typeof data.type !== "object") {
160
+ const typeValue = data.type;
161
+ dataType = String(typeValue);
162
+ }
163
+ const restData = { ...data };
164
+ delete restData.type;
165
+ if (dataType === "result") {
166
+ const result = new CodeInterpreter.Result(restData);
167
+ execution.results.push(result);
168
+ if (onResult) {
169
+ return onResult(result);
170
+ }
171
+ }
172
+ else if (dataType === "stdout") {
173
+ let text = "";
174
+ if (typeof data.text === "string") {
175
+ text = data.text;
176
+ }
177
+ else if (data.text !== null &&
178
+ data.text !== undefined &&
179
+ typeof data.text !== "object") {
180
+ const textValue = data.text;
181
+ text = String(textValue);
182
+ }
183
+ execution.logs.stdout.push(text);
184
+ if (onStdout) {
185
+ return onStdout(new CodeInterpreter.OutputMessage(text, typeof data.timestamp === "number" ? data.timestamp : null, false));
186
+ }
187
+ }
188
+ else if (dataType === "stderr") {
189
+ let text = "";
190
+ if (typeof data.text === "string") {
191
+ text = data.text;
192
+ }
193
+ else if (data.text !== null &&
194
+ data.text !== undefined &&
195
+ typeof data.text !== "object") {
196
+ const textValue = data.text;
197
+ text = String(textValue);
198
+ }
199
+ execution.logs.stderr.push(text);
200
+ if (onStderr) {
201
+ return onStderr(new CodeInterpreter.OutputMessage(text, typeof data.timestamp === "number" ? data.timestamp : null, true));
202
+ }
203
+ }
204
+ else if (dataType === "error") {
205
+ let errorName = "";
206
+ if (typeof data.name === "string") {
207
+ errorName = data.name;
208
+ }
209
+ else if (data.name !== null &&
210
+ data.name !== undefined &&
211
+ typeof data.name !== "object") {
212
+ const nameValue = data.name;
213
+ errorName = String(nameValue);
214
+ }
215
+ execution.error = new CodeInterpreter.ExecutionError(errorName, data.value, data.traceback);
216
+ if (onError) {
217
+ return onError(execution.error);
218
+ }
219
+ }
220
+ else if (dataType === "number_of_executions") {
221
+ execution.executionCount =
222
+ typeof data.execution_count === "number" ? data.execution_count : null;
223
+ }
224
+ return null;
225
+ }
226
+ async runCode(code, options = {}) {
227
+ const { language = null, context = null, onStdout, onStderr, onResult, onError, envs = null, timeout = null, } = options;
228
+ const DEFAULT_TIMEOUT = 60.0;
229
+ if (language && context) {
230
+ throw new Error("You can provide context or language, but not both at the same time.");
231
+ }
232
+ const readTimeout = timeout === 0 ? null : timeout ?? DEFAULT_TIMEOUT;
233
+ const contextId = context?.id ?? null;
234
+ const body = {
235
+ code,
236
+ context_id: contextId,
237
+ language,
238
+ env_vars: envs,
239
+ };
240
+ const execution = new CodeInterpreter.Execution();
241
+ const headers = this._sandboxConfig.forceUrl
242
+ ? this._sandboxConfig.headers
243
+ : settings.headers;
244
+ const controller = new AbortController();
245
+ let timeoutId = null;
246
+ // Set up timeout
247
+ if (readTimeout !== null) {
248
+ timeoutId = setTimeout(() => {
249
+ controller.abort();
250
+ }, readTimeout * 1000);
251
+ }
252
+ try {
253
+ const response = await fetch(`${this._jupyterUrl}/port/8888/execute`, {
254
+ method: "POST",
255
+ headers: {
256
+ ...headers,
257
+ "Content-Type": "application/json",
258
+ },
259
+ body: JSON.stringify(body),
260
+ signal: controller.signal,
261
+ });
262
+ if (response.status >= 400) {
263
+ let bodyText = "<unavailable>";
264
+ try {
265
+ bodyText = await response.text();
266
+ }
267
+ catch {
268
+ // Ignore errors
269
+ }
270
+ const method = "POST";
271
+ const url = `${this._jupyterUrl}/port/8888/execute`;
272
+ const reason = response.statusText;
273
+ const details = "Execution failed\n" +
274
+ `- method: ${method}\n- url: ${url}\n- status: ${response.status} ${reason}\n` +
275
+ `- response-headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}\n- body:\n${bodyText}`;
276
+ logger.debug(details);
277
+ throw new Error(details);
278
+ }
279
+ if (!response.body) {
280
+ throw new Error("No response body");
281
+ }
282
+ const reader = response.body.getReader();
283
+ const decoder = new TextDecoder();
284
+ let buffer = "";
285
+ try {
286
+ while (true) {
287
+ const result = await reader.read();
288
+ if (result.done)
289
+ break;
290
+ const value = result.value;
291
+ if (value instanceof Uint8Array) {
292
+ buffer += decoder.decode(value, { stream: true });
293
+ const lines = buffer.split(/\r?\n/);
294
+ buffer = lines.pop() || "";
295
+ for (const line of lines) {
296
+ if (!line)
297
+ continue;
298
+ try {
299
+ this._parseOutput(execution, line, onStdout, onStderr, onResult, onError);
300
+ }
301
+ catch {
302
+ // Fallback: treat as stdout text-only message
303
+ execution.logs.stdout.push(line);
304
+ if (onStdout) {
305
+ onStdout(new CodeInterpreter.OutputMessage(line, null, false));
306
+ }
307
+ }
308
+ }
309
+ }
310
+ }
311
+ }
312
+ finally {
313
+ reader.releaseLock();
314
+ }
315
+ }
316
+ catch (error) {
317
+ if (error &&
318
+ typeof error === "object" &&
319
+ "name" in error &&
320
+ error.name === "AbortError") {
321
+ throw new Error("Request timeout");
322
+ }
323
+ throw error;
324
+ }
325
+ finally {
326
+ if (timeoutId) {
327
+ clearTimeout(timeoutId);
328
+ }
329
+ }
330
+ return execution;
331
+ }
332
+ async createCodeContext(options = {}) {
333
+ const { cwd = null, language = null, requestTimeout = null } = options;
334
+ const data = {};
335
+ if (language) {
336
+ data.language = language;
337
+ }
338
+ if (cwd) {
339
+ data.cwd = cwd;
340
+ }
341
+ const headers = this._sandboxConfig.forceUrl
342
+ ? this._sandboxConfig.headers
343
+ : settings.headers;
344
+ const controller = new AbortController();
345
+ let timeoutId = null;
346
+ if (requestTimeout !== null) {
347
+ timeoutId = setTimeout(() => {
348
+ controller.abort();
349
+ }, requestTimeout * 1000);
350
+ }
351
+ try {
352
+ const response = await fetch(`${this._jupyterUrl}/port/8888/contexts`, {
353
+ method: "POST",
354
+ headers: {
355
+ ...headers,
356
+ "Content-Type": "application/json",
357
+ },
358
+ body: JSON.stringify(data),
359
+ signal: controller.signal,
360
+ });
361
+ if (response.status >= 400) {
362
+ let bodyText = "<unavailable>";
363
+ try {
364
+ bodyText = await response.text();
365
+ }
366
+ catch {
367
+ // Ignore errors
368
+ }
369
+ const method = "POST";
370
+ const url = `${this._jupyterUrl}/port/8888/contexts`;
371
+ const reason = response.statusText;
372
+ const details = "Create context failed\n" +
373
+ `- method: ${method}\n- url: ${url}\n- status: ${response.status} ${reason}\n` +
374
+ `- response-headers: ${JSON.stringify(Object.fromEntries(response.headers.entries()))}\n- body:\n${bodyText}`;
375
+ logger.debug(details);
376
+ throw new Error(details);
377
+ }
378
+ const responseData = (await response.json());
379
+ return CodeInterpreter.Context.fromJson(responseData);
380
+ }
381
+ catch (error) {
382
+ if (error &&
383
+ typeof error === "object" &&
384
+ "name" in error &&
385
+ error.name === "AbortError") {
386
+ throw new Error("Request timeout");
387
+ }
388
+ throw error;
389
+ }
390
+ finally {
391
+ if (timeoutId) {
392
+ clearTimeout(timeoutId);
393
+ }
394
+ }
395
+ }
396
+ }
@@ -39,10 +39,6 @@ export class McpTool {
39
39
  this.client = new ModelContextProtocolClient({
40
40
  name: this.name,
41
41
  version: "1.0.0",
42
- }, {
43
- capabilities: {
44
- tools: {},
45
- },
46
42
  });
47
43
  }
48
44
  get fallbackUrl() {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@blaxel/core",
3
- "version": "0.2.49-preview.111",
3
+ "version": "0.2.49-preview.112",
4
4
  "description": "Blaxel Core SDK for TypeScript",
5
5
  "license": "MIT",
6
6
  "author": "Blaxel, INC (https://blaxel.ai)",
@@ -74,7 +74,7 @@
74
74
  "vite": "^5.2.0",
75
75
  "vitest": "^1.5.0"
76
76
  },
77
- "commit": "26ddeed4df5d6c810926b377dfe1a52bbea62022",
77
+ "commit": "d50dddc5a1963abd22bad3b294e9f6096c3e8287",
78
78
  "scripts": {
79
79
  "lint": "eslint src/",
80
80
  "dev": "tsc --watch",