@alibaba-group/opensandbox-code-interpreter 0.1.0-dev1
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 +187 -0
- package/dist/adapters/codesAdapter.d.ts +31 -0
- package/dist/adapters/codesAdapter.d.ts.map +1 -0
- package/dist/adapters/codesAdapter.js +146 -0
- package/dist/adapters/openapiError.d.ts +5 -0
- package/dist/adapters/openapiError.d.ts.map +1 -0
- package/dist/adapters/openapiError.js +35 -0
- package/dist/adapters/sse.d.ts +9 -0
- package/dist/adapters/sse.d.ts.map +1 -0
- package/dist/adapters/sse.js +83 -0
- package/dist/factory/adapterFactory.d.ts +13 -0
- package/dist/factory/adapterFactory.d.ts.map +1 -0
- package/dist/factory/adapterFactory.js +14 -0
- package/dist/factory/defaultAdapterFactory.d.ts +7 -0
- package/dist/factory/defaultAdapterFactory.d.ts.map +1 -0
- package/dist/factory/defaultAdapterFactory.js +33 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/interpreter.d.ts +26 -0
- package/dist/interpreter.d.ts.map +1 -0
- package/dist/interpreter.js +50 -0
- package/dist/models.d.ts +18 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +21 -0
- package/dist/services/codes.d.ts +31 -0
- package/dist/services/codes.d.ts.map +1 -0
- package/dist/services/codes.js +14 -0
- package/package.json +45 -0
package/README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
# Alibaba Code Interpreter SDK for JavaScript/TypeScript
|
|
2
|
+
|
|
3
|
+
English | [中文](README_zh.md)
|
|
4
|
+
|
|
5
|
+
A TypeScript/JavaScript SDK for executing code in secure, isolated sandboxes. It provides a high-level API for running Python, Java, Go, TypeScript, and other languages safely, with support for code execution contexts.
|
|
6
|
+
|
|
7
|
+
## Prerequisites
|
|
8
|
+
|
|
9
|
+
This SDK requires a Docker image containing the Code Interpreter runtime environment. You must use the `opensandbox/code-interpreter` image (or a derivative) which includes pre-installed runtimes for Python, Java, Go, Node.js, etc.
|
|
10
|
+
|
|
11
|
+
For detailed information about supported languages and versions, please refer to the [Environment Documentation](../../../sandboxes/code-interpreter/README.md).
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
### npm
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @alibaba-group/opensandbox-code-interpreter
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### pnpm
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
pnpm add @alibaba-group/opensandbox-code-interpreter
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### yarn
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
yarn add @alibaba-group/opensandbox-code-interpreter
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
## Quick Start
|
|
34
|
+
|
|
35
|
+
The following example demonstrates how to create a sandbox with a specific runtime configuration and execute a simple script.
|
|
36
|
+
|
|
37
|
+
> **Note**: Before running this example, ensure the OpenSandbox service is running. See the root [README.md](../../../README.md) for startup instructions.
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import { ConnectionConfig, Sandbox } from "@alibaba-group/opensandbox";
|
|
41
|
+
import { CodeInterpreter, SupportedLanguages } from "@alibaba-group/opensandbox-code-interpreter";
|
|
42
|
+
|
|
43
|
+
// 1. Configure connection
|
|
44
|
+
const config = new ConnectionConfig({
|
|
45
|
+
domain: "api.opensandbox.io",
|
|
46
|
+
apiKey: "your-api-key",
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
// 2. Create a Sandbox with the code-interpreter image + runtime versions
|
|
50
|
+
const sandbox = await Sandbox.create({
|
|
51
|
+
connectionConfig: config,
|
|
52
|
+
image: "opensandbox/code-interpreter:latest",
|
|
53
|
+
entrypoint: ["/opt/opensandbox/code-interpreter.sh"],
|
|
54
|
+
env: {
|
|
55
|
+
PYTHON_VERSION: "3.11",
|
|
56
|
+
JAVA_VERSION: "17",
|
|
57
|
+
NODE_VERSION: "20",
|
|
58
|
+
GO_VERSION: "1.24",
|
|
59
|
+
},
|
|
60
|
+
timeoutSeconds: 15 * 60,
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
// 3. Create CodeInterpreter wrapper
|
|
64
|
+
const ci = await CodeInterpreter.create(sandbox);
|
|
65
|
+
|
|
66
|
+
// 4. Create an execution context (Python)
|
|
67
|
+
const ctx = await ci.codes.createContext(SupportedLanguages.PYTHON);
|
|
68
|
+
|
|
69
|
+
// 5. Run code
|
|
70
|
+
const result = await ci.codes.run("import sys\nprint(sys.version)\nresult = 2 + 2\nresult", {
|
|
71
|
+
context: ctx,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// 6. Print output
|
|
75
|
+
console.log(result.result[0]?.text);
|
|
76
|
+
|
|
77
|
+
// 7. Cleanup remote instance (optional but recommended)
|
|
78
|
+
await sandbox.kill();
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Runtime Configuration
|
|
82
|
+
|
|
83
|
+
### Docker Image
|
|
84
|
+
|
|
85
|
+
The Code Interpreter SDK relies on a specialized environment. Ensure your sandbox provider has the `opensandbox/code-interpreter` image available.
|
|
86
|
+
|
|
87
|
+
### Language Version Selection
|
|
88
|
+
|
|
89
|
+
You can specify the desired version of a programming language by setting the corresponding environment variable when creating the `Sandbox`.
|
|
90
|
+
|
|
91
|
+
| Language | Environment Variable | Example Value | Default (if unset) |
|
|
92
|
+
| --- | --- | --- | --- |
|
|
93
|
+
| Python | `PYTHON_VERSION` | `3.11` | Image default |
|
|
94
|
+
| Java | `JAVA_VERSION` | `17` | Image default |
|
|
95
|
+
| Node.js | `NODE_VERSION` | `20` | Image default |
|
|
96
|
+
| Go | `GO_VERSION` | `1.24` | Image default |
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
const sandbox = await Sandbox.create({
|
|
100
|
+
connectionConfig: config,
|
|
101
|
+
image: "opensandbox/code-interpreter:latest",
|
|
102
|
+
entrypoint: ["/opt/opensandbox/code-interpreter.sh"],
|
|
103
|
+
env: {
|
|
104
|
+
JAVA_VERSION: "17",
|
|
105
|
+
GO_VERSION: "1.24",
|
|
106
|
+
},
|
|
107
|
+
});
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
## Usage Examples
|
|
111
|
+
|
|
112
|
+
### 0. Run with `language` (default language context)
|
|
113
|
+
|
|
114
|
+
If you don't need to manage explicit context IDs, you can run code by specifying only `language`.
|
|
115
|
+
When `context.id` is omitted, execd can create/reuse a default session for that language, so state can persist across runs.
|
|
116
|
+
|
|
117
|
+
```ts
|
|
118
|
+
import { SupportedLanguages } from "@alibaba-group/opensandbox-code-interpreter";
|
|
119
|
+
|
|
120
|
+
await ci.codes.run("x = 42", { language: SupportedLanguages.PYTHON });
|
|
121
|
+
const execution = await ci.codes.run("result = x\nresult", { language: SupportedLanguages.PYTHON });
|
|
122
|
+
console.log(execution.result[0]?.text); // "42"
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### 0.1 Context management (list/get/delete)
|
|
126
|
+
|
|
127
|
+
You can manage contexts explicitly (aligned with Python/Kotlin SDKs):
|
|
128
|
+
|
|
129
|
+
```ts
|
|
130
|
+
const ctx = await ci.codes.createContext(SupportedLanguages.PYTHON);
|
|
131
|
+
|
|
132
|
+
const same = await ci.codes.getContext(ctx.id!);
|
|
133
|
+
console.log(same.id, same.language);
|
|
134
|
+
|
|
135
|
+
const all = await ci.codes.listContexts();
|
|
136
|
+
const pyOnly = await ci.codes.listContexts(SupportedLanguages.PYTHON);
|
|
137
|
+
|
|
138
|
+
await ci.codes.deleteContext(ctx.id!);
|
|
139
|
+
await ci.codes.deleteContexts(SupportedLanguages.PYTHON); // bulk cleanup
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
### 1. Java Code Execution
|
|
143
|
+
|
|
144
|
+
```ts
|
|
145
|
+
import { SupportedLanguages } from "@alibaba-group/opensandbox-code-interpreter";
|
|
146
|
+
|
|
147
|
+
const javaCtx = await ci.codes.createContext(SupportedLanguages.JAVA);
|
|
148
|
+
const execution = await ci.codes.run(
|
|
149
|
+
[
|
|
150
|
+
'System.out.println("Calculating sum...");',
|
|
151
|
+
"int a = 10;",
|
|
152
|
+
"int b = 20;",
|
|
153
|
+
"int sum = a + b;",
|
|
154
|
+
'System.out.println("Sum: " + sum);',
|
|
155
|
+
"sum",
|
|
156
|
+
].join("\n"),
|
|
157
|
+
{ context: javaCtx },
|
|
158
|
+
);
|
|
159
|
+
console.log(execution.logs.stdout.map((m) => m.text));
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
### 2. Streaming Output Handling
|
|
163
|
+
|
|
164
|
+
Handle stdout/stderr and execution events in real-time.
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
import type { ExecutionHandlers } from "@alibaba-group/opensandbox";
|
|
168
|
+
import { SupportedLanguages } from "@alibaba-group/opensandbox-code-interpreter";
|
|
169
|
+
|
|
170
|
+
const handlers: ExecutionHandlers = {
|
|
171
|
+
onStdout: (m) => console.log("STDOUT:", m.text),
|
|
172
|
+
onStderr: (m) => console.error("STDERR:", m.text),
|
|
173
|
+
onResult: (r) => console.log("RESULT:", r.text),
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
const pyCtx = await ci.codes.createContext(SupportedLanguages.PYTHON);
|
|
177
|
+
await ci.codes.run("import time\nfor i in range(5):\n print(i)\n time.sleep(0.2)", {
|
|
178
|
+
context: pyCtx,
|
|
179
|
+
handlers,
|
|
180
|
+
});
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
## Notes
|
|
184
|
+
|
|
185
|
+
- **Lifecycle**: `CodeInterpreter` wraps an existing `Sandbox` instance and reuses its connection configuration.
|
|
186
|
+
- **Default context**: `codes.run(..., { language })` uses a language default context (state can persist across runs).
|
|
187
|
+
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ExecdClient, ExecdPaths } from "@alibaba-group/opensandbox/internal";
|
|
2
|
+
import type { ServerStreamEvent } from "@alibaba-group/opensandbox";
|
|
3
|
+
import type { Execution, ExecutionHandlers } from "@alibaba-group/opensandbox";
|
|
4
|
+
import type { Codes } from "../services/codes.js";
|
|
5
|
+
import type { CodeContext, SupportedLanguage } from "../models.js";
|
|
6
|
+
type ApiRunCodeRequest = ExecdPaths["/code"]["post"]["requestBody"]["content"]["application/json"];
|
|
7
|
+
export declare class CodesAdapter implements Codes {
|
|
8
|
+
private readonly client;
|
|
9
|
+
private readonly opts;
|
|
10
|
+
private readonly fetch;
|
|
11
|
+
constructor(client: ExecdClient, opts: {
|
|
12
|
+
baseUrl: string;
|
|
13
|
+
fetch?: typeof fetch;
|
|
14
|
+
headers?: Record<string, string>;
|
|
15
|
+
});
|
|
16
|
+
createContext(language: SupportedLanguage): Promise<CodeContext>;
|
|
17
|
+
getContext(contextId: string): Promise<CodeContext>;
|
|
18
|
+
listContexts(language?: SupportedLanguage): Promise<CodeContext[]>;
|
|
19
|
+
deleteContext(contextId: string): Promise<void>;
|
|
20
|
+
deleteContexts(language: SupportedLanguage): Promise<void>;
|
|
21
|
+
interrupt(contextId: string): Promise<void>;
|
|
22
|
+
runStream(req: ApiRunCodeRequest, signal?: AbortSignal): AsyncIterable<ServerStreamEvent>;
|
|
23
|
+
run(code: string, opts?: {
|
|
24
|
+
context?: CodeContext;
|
|
25
|
+
language?: SupportedLanguage;
|
|
26
|
+
handlers?: ExecutionHandlers;
|
|
27
|
+
signal?: AbortSignal;
|
|
28
|
+
}): Promise<Execution>;
|
|
29
|
+
}
|
|
30
|
+
export {};
|
|
31
|
+
//# sourceMappingURL=codesAdapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codesAdapter.d.ts","sourceRoot":"","sources":["../../src/adapters/codesAdapter.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,WAAW,EAAE,UAAU,EAAE,MAAM,qCAAqC,CAAC;AACnF,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAM/E,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAClD,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAYnE,KAAK,iBAAiB,GACpB,UAAU,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC,aAAa,CAAC,CAAC,SAAS,CAAC,CAAC,kBAAkB,CAAC,CAAC;AAc5E,qBAAa,YAAa,YAAW,KAAK;IAItC,OAAO,CAAC,QAAQ,CAAC,MAAM;IACvB,OAAO,CAAC,QAAQ,CAAC,IAAI;IAJvB,OAAO,CAAC,QAAQ,CAAC,KAAK,CAAe;gBAGlB,MAAM,EAAE,WAAW,EACnB,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;KAAE;IAK9F,aAAa,CAAC,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,CAAC;IAgBhE,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC;IAkBnD,YAAY,CAAC,QAAQ,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAclE,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAU/C,cAAc,CAAC,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC;IAO1D,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAO1C,SAAS,CAAC,GAAG,EAAE,iBAAiB,EAAE,MAAM,CAAC,EAAE,WAAW,GAAG,aAAa,CAAC,iBAAiB,CAAC;IAmB1F,GAAG,CACP,IAAI,EAAE,MAAM,EACZ,IAAI,GAAE;QAAE,OAAO,CAAC,EAAE,WAAW,CAAC;QAAC,QAAQ,CAAC,EAAE,iBAAiB,CAAC;QAAC,QAAQ,CAAC,EAAE,iBAAiB,CAAC;QAAC,MAAM,CAAC,EAAE,WAAW,CAAA;KAAO,GACrH,OAAO,CAAC,SAAS,CAAC;CAiCtB"}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { ExecutionEventDispatcher, InvalidArgumentException, } from "@alibaba-group/opensandbox";
|
|
15
|
+
import { throwOnOpenApiFetchError } from "./openapiError.js";
|
|
16
|
+
import { parseJsonEventStream } from "./sse.js";
|
|
17
|
+
/**
|
|
18
|
+
* Single-layer codes adapter for the Code Interpreter SDK.
|
|
19
|
+
*
|
|
20
|
+
* - Handles HTTP/SSE streaming via the underlying execd adapter
|
|
21
|
+
* - Builds the structured {@link Execution} result for `run(...)`
|
|
22
|
+
*/
|
|
23
|
+
function joinUrl(baseUrl, pathname) {
|
|
24
|
+
const base = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
25
|
+
const path = pathname.startsWith("/") ? pathname : `/${pathname}`;
|
|
26
|
+
return `${base}${path}`;
|
|
27
|
+
}
|
|
28
|
+
export class CodesAdapter {
|
|
29
|
+
client;
|
|
30
|
+
opts;
|
|
31
|
+
fetch;
|
|
32
|
+
constructor(client, opts) {
|
|
33
|
+
this.client = client;
|
|
34
|
+
this.opts = opts;
|
|
35
|
+
this.fetch = opts.fetch ?? fetch;
|
|
36
|
+
}
|
|
37
|
+
async createContext(language) {
|
|
38
|
+
const body = { language };
|
|
39
|
+
const { data, error, response } = await this.client.POST("/code/context", {
|
|
40
|
+
body,
|
|
41
|
+
});
|
|
42
|
+
throwOnOpenApiFetchError({ error, response }, "Create code context failed");
|
|
43
|
+
const ok = data;
|
|
44
|
+
if (!ok || typeof ok !== "object") {
|
|
45
|
+
throw new Error("Create code context failed: unexpected response shape");
|
|
46
|
+
}
|
|
47
|
+
if (typeof ok.language !== "string" || !ok.language) {
|
|
48
|
+
throw new Error("Create code context failed: missing language");
|
|
49
|
+
}
|
|
50
|
+
return { id: ok.id, language: ok.language };
|
|
51
|
+
}
|
|
52
|
+
async getContext(contextId) {
|
|
53
|
+
if (!contextId?.trim()) {
|
|
54
|
+
throw new InvalidArgumentException({ message: "contextId cannot be empty" });
|
|
55
|
+
}
|
|
56
|
+
const { data, error, response } = await this.client.GET("/code/contexts/{context_id}", {
|
|
57
|
+
params: { path: { context_id: contextId } },
|
|
58
|
+
});
|
|
59
|
+
throwOnOpenApiFetchError({ error, response }, "Get code context failed");
|
|
60
|
+
const ok = data;
|
|
61
|
+
if (!ok || typeof ok !== "object") {
|
|
62
|
+
throw new Error("Get code context failed: unexpected response shape");
|
|
63
|
+
}
|
|
64
|
+
if (typeof ok.language !== "string" || !ok.language) {
|
|
65
|
+
throw new Error("Get code context failed: missing language");
|
|
66
|
+
}
|
|
67
|
+
return { id: ok.id, language: ok.language };
|
|
68
|
+
}
|
|
69
|
+
async listContexts(language) {
|
|
70
|
+
const { data, error, response } = await this.client.GET("/code/contexts", {
|
|
71
|
+
params: language ? { query: { language } } : undefined,
|
|
72
|
+
});
|
|
73
|
+
throwOnOpenApiFetchError({ error, response }, "List code contexts failed");
|
|
74
|
+
const ok = data;
|
|
75
|
+
if (!Array.isArray(ok)) {
|
|
76
|
+
throw new Error("List code contexts failed: unexpected response shape");
|
|
77
|
+
}
|
|
78
|
+
return ok
|
|
79
|
+
.filter((c) => c && typeof c === "object")
|
|
80
|
+
.map((c) => ({ id: c.id, language: c.language }));
|
|
81
|
+
}
|
|
82
|
+
async deleteContext(contextId) {
|
|
83
|
+
if (!contextId?.trim()) {
|
|
84
|
+
throw new InvalidArgumentException({ message: "contextId cannot be empty" });
|
|
85
|
+
}
|
|
86
|
+
const { error, response } = await this.client.DELETE("/code/contexts/{context_id}", {
|
|
87
|
+
params: { path: { context_id: contextId } },
|
|
88
|
+
});
|
|
89
|
+
throwOnOpenApiFetchError({ error, response }, "Delete code context failed");
|
|
90
|
+
}
|
|
91
|
+
async deleteContexts(language) {
|
|
92
|
+
const { error, response } = await this.client.DELETE("/code/contexts", {
|
|
93
|
+
params: { query: { language } },
|
|
94
|
+
});
|
|
95
|
+
throwOnOpenApiFetchError({ error, response }, "Delete code contexts failed");
|
|
96
|
+
}
|
|
97
|
+
async interrupt(contextId) {
|
|
98
|
+
const { error, response } = await this.client.DELETE("/code", {
|
|
99
|
+
params: { query: { id: contextId } },
|
|
100
|
+
});
|
|
101
|
+
throwOnOpenApiFetchError({ error, response }, "Interrupt code failed");
|
|
102
|
+
}
|
|
103
|
+
async *runStream(req, signal) {
|
|
104
|
+
const url = joinUrl(this.opts.baseUrl, "/code");
|
|
105
|
+
const body = JSON.stringify(req);
|
|
106
|
+
const res = await this.fetch(url, {
|
|
107
|
+
method: "POST",
|
|
108
|
+
headers: {
|
|
109
|
+
"accept": "text/event-stream",
|
|
110
|
+
"content-type": "application/json",
|
|
111
|
+
...(this.opts.headers ?? {}),
|
|
112
|
+
},
|
|
113
|
+
body,
|
|
114
|
+
signal,
|
|
115
|
+
});
|
|
116
|
+
for await (const ev of parseJsonEventStream(res, { fallbackErrorMessage: "Run code failed" })) {
|
|
117
|
+
yield ev;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
async run(code, opts = {}) {
|
|
121
|
+
if (!code.trim()) {
|
|
122
|
+
throw new InvalidArgumentException({ message: "Code cannot be empty" });
|
|
123
|
+
}
|
|
124
|
+
if (opts.context && opts.language) {
|
|
125
|
+
throw new InvalidArgumentException({ message: "Provide either opts.context or opts.language, not both" });
|
|
126
|
+
}
|
|
127
|
+
const context = opts.context ??
|
|
128
|
+
(opts.language
|
|
129
|
+
? { language: opts.language }
|
|
130
|
+
: { language: "python" });
|
|
131
|
+
// Make the OpenAPI contract explicit so backend schema changes surface quickly.
|
|
132
|
+
const req = {
|
|
133
|
+
code,
|
|
134
|
+
context: { id: context.id, language: context.language },
|
|
135
|
+
};
|
|
136
|
+
const execution = {
|
|
137
|
+
logs: { stdout: [], stderr: [] },
|
|
138
|
+
result: [],
|
|
139
|
+
};
|
|
140
|
+
const dispatcher = new ExecutionEventDispatcher(execution, opts.handlers);
|
|
141
|
+
for await (const ev of this.runStream(req, opts.signal)) {
|
|
142
|
+
await dispatcher.dispatch(ev);
|
|
143
|
+
}
|
|
144
|
+
return execution;
|
|
145
|
+
}
|
|
146
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openapiError.d.ts","sourceRoot":"","sources":["../../src/adapters/openapiError.ts"],"names":[],"mappings":"AAgBA,wBAAgB,wBAAwB,CACtC,MAAM,EAAE;IAAE,KAAK,CAAC,EAAE,OAAO,CAAC;IAAC,QAAQ,EAAE,QAAQ,CAAA;CAAE,EAC/C,eAAe,EAAE,MAAM,GACtB,IAAI,CAwBN"}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { SandboxApiException, SandboxError } from "@alibaba-group/opensandbox";
|
|
15
|
+
export function throwOnOpenApiFetchError(result, fallbackMessage) {
|
|
16
|
+
if (!result.error)
|
|
17
|
+
return;
|
|
18
|
+
const requestId = result.response.headers.get("x-request-id") ?? undefined;
|
|
19
|
+
const status = result.response.status ?? 0;
|
|
20
|
+
const err = result.error;
|
|
21
|
+
const message = err?.message ??
|
|
22
|
+
err?.error?.message ??
|
|
23
|
+
fallbackMessage;
|
|
24
|
+
const code = err?.code ?? err?.error?.code;
|
|
25
|
+
const msg = err?.message ?? err?.error?.message ?? message;
|
|
26
|
+
throw new SandboxApiException({
|
|
27
|
+
message: msg,
|
|
28
|
+
statusCode: status,
|
|
29
|
+
requestId,
|
|
30
|
+
error: code
|
|
31
|
+
? new SandboxError(String(code), String(msg ?? ""))
|
|
32
|
+
: new SandboxError(SandboxError.UNEXPECTED_RESPONSE, String(msg ?? "")),
|
|
33
|
+
rawBody: result.error,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parses an SSE-like stream that may be either:
|
|
3
|
+
* - standard SSE frames (`data: {...}\n\n`)
|
|
4
|
+
* - newline-delimited JSON (one JSON object per line)
|
|
5
|
+
*/
|
|
6
|
+
export declare function parseJsonEventStream<T>(res: Response, opts?: {
|
|
7
|
+
fallbackErrorMessage?: string;
|
|
8
|
+
}): AsyncIterable<T>;
|
|
9
|
+
//# sourceMappingURL=sse.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"sse.d.ts","sourceRoot":"","sources":["../../src/adapters/sse.ts"],"names":[],"mappings":"AAwBA;;;;GAIG;AACH,wBAAuB,oBAAoB,CAAC,CAAC,EAC3C,GAAG,EAAE,QAAQ,EACb,IAAI,CAAC,EAAE;IAAE,oBAAoB,CAAC,EAAE,MAAM,CAAA;CAAE,GACvC,aAAa,CAAC,CAAC,CAAC,CAyDlB"}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { SandboxApiException, SandboxError } from "@alibaba-group/opensandbox";
|
|
15
|
+
function tryParseJson(line) {
|
|
16
|
+
try {
|
|
17
|
+
return JSON.parse(line);
|
|
18
|
+
}
|
|
19
|
+
catch {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Parses an SSE-like stream that may be either:
|
|
25
|
+
* - standard SSE frames (`data: {...}\n\n`)
|
|
26
|
+
* - newline-delimited JSON (one JSON object per line)
|
|
27
|
+
*/
|
|
28
|
+
export async function* parseJsonEventStream(res, opts) {
|
|
29
|
+
if (!res.ok) {
|
|
30
|
+
const text = await res.text().catch(() => "");
|
|
31
|
+
const parsed = tryParseJson(text);
|
|
32
|
+
const err = parsed && typeof parsed === "object" ? parsed : undefined;
|
|
33
|
+
const requestId = res.headers.get("x-request-id") ?? undefined;
|
|
34
|
+
const message = err?.message ?? opts?.fallbackErrorMessage ?? `Stream request failed (status=${res.status})`;
|
|
35
|
+
const code = err?.code ? String(err.code) : SandboxError.UNEXPECTED_RESPONSE;
|
|
36
|
+
throw new SandboxApiException({
|
|
37
|
+
message,
|
|
38
|
+
statusCode: res.status,
|
|
39
|
+
requestId,
|
|
40
|
+
error: new SandboxError(code, err?.message ? String(err.message) : message),
|
|
41
|
+
rawBody: parsed ?? text,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (!res.body)
|
|
45
|
+
return;
|
|
46
|
+
const reader = res.body.getReader();
|
|
47
|
+
const decoder = new TextDecoder("utf-8");
|
|
48
|
+
let buf = "";
|
|
49
|
+
while (true) {
|
|
50
|
+
const { value, done } = await reader.read();
|
|
51
|
+
if (done)
|
|
52
|
+
break;
|
|
53
|
+
buf += decoder.decode(value, { stream: true });
|
|
54
|
+
let idx;
|
|
55
|
+
while ((idx = buf.indexOf("\n")) >= 0) {
|
|
56
|
+
const rawLine = buf.slice(0, idx);
|
|
57
|
+
buf = buf.slice(idx + 1);
|
|
58
|
+
const line = rawLine.trim();
|
|
59
|
+
if (!line)
|
|
60
|
+
continue;
|
|
61
|
+
// Support standard SSE "data:" prefix
|
|
62
|
+
if (line.startsWith(":"))
|
|
63
|
+
continue;
|
|
64
|
+
if (line.startsWith("event:") || line.startsWith("id:") || line.startsWith("retry:"))
|
|
65
|
+
continue;
|
|
66
|
+
const jsonLine = line.startsWith("data:") ? line.slice("data:".length).trim() : line;
|
|
67
|
+
if (!jsonLine)
|
|
68
|
+
continue;
|
|
69
|
+
const parsed = tryParseJson(jsonLine);
|
|
70
|
+
if (!parsed)
|
|
71
|
+
continue;
|
|
72
|
+
yield parsed;
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
// flush last line if exists
|
|
76
|
+
const last = buf.trim();
|
|
77
|
+
if (last) {
|
|
78
|
+
const jsonLine = last.startsWith("data:") ? last.slice("data:".length).trim() : last;
|
|
79
|
+
const parsed = tryParseJson(jsonLine);
|
|
80
|
+
if (parsed)
|
|
81
|
+
yield parsed;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Sandbox } from "@alibaba-group/opensandbox";
|
|
2
|
+
import type { Codes } from "../services/codes.js";
|
|
3
|
+
export interface CreateCodesStackOptions {
|
|
4
|
+
sandbox: Sandbox;
|
|
5
|
+
execdBaseUrl: string;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Factory abstraction for Code Interpreter SDK to decouple from concrete adapters/clients.
|
|
9
|
+
*/
|
|
10
|
+
export interface AdapterFactory {
|
|
11
|
+
createCodes(opts: CreateCodesStackOptions): Codes;
|
|
12
|
+
}
|
|
13
|
+
//# sourceMappingURL=adapterFactory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"adapterFactory.d.ts","sourceRoot":"","sources":["../../src/factory/adapterFactory.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAC1D,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAElD,MAAM,WAAW,uBAAuB;IACtC,OAAO,EAAE,OAAO,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;CACtB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,WAAW,CAAC,IAAI,EAAE,uBAAuB,GAAG,KAAK,CAAC;CACnD"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export {};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { AdapterFactory, CreateCodesStackOptions } from "./adapterFactory.js";
|
|
2
|
+
import type { Codes } from "../services/codes.js";
|
|
3
|
+
export declare class DefaultAdapterFactory implements AdapterFactory {
|
|
4
|
+
createCodes(opts: CreateCodesStackOptions): Codes;
|
|
5
|
+
}
|
|
6
|
+
export declare function createDefaultAdapterFactory(): AdapterFactory;
|
|
7
|
+
//# sourceMappingURL=defaultAdapterFactory.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"defaultAdapterFactory.d.ts","sourceRoot":"","sources":["../../src/factory/defaultAdapterFactory.ts"],"names":[],"mappings":"AAgBA,OAAO,KAAK,EAAE,cAAc,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAEnF,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,sBAAsB,CAAC;AAElD,qBAAa,qBAAsB,YAAW,cAAc;IAC1D,WAAW,CAAC,IAAI,EAAE,uBAAuB,GAAG,KAAK;CAclD;AAED,wBAAgB,2BAA2B,IAAI,cAAc,CAE5D"}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { createExecdClient } from "@alibaba-group/opensandbox/internal";
|
|
15
|
+
import { CodesAdapter } from "../adapters/codesAdapter.js";
|
|
16
|
+
export class DefaultAdapterFactory {
|
|
17
|
+
createCodes(opts) {
|
|
18
|
+
const client = createExecdClient({
|
|
19
|
+
baseUrl: opts.execdBaseUrl,
|
|
20
|
+
headers: opts.sandbox.connectionConfig.headers,
|
|
21
|
+
fetch: opts.sandbox.connectionConfig.fetch,
|
|
22
|
+
});
|
|
23
|
+
return new CodesAdapter(client, {
|
|
24
|
+
baseUrl: opts.execdBaseUrl,
|
|
25
|
+
headers: opts.sandbox.connectionConfig.headers,
|
|
26
|
+
// Streaming calls (SSE) use a dedicated fetch, aligned with Kotlin/Python SDKs.
|
|
27
|
+
fetch: opts.sandbox.connectionConfig.sseFetch,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
export function createDefaultAdapterFactory() {
|
|
32
|
+
return new DefaultAdapterFactory();
|
|
33
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export { CodeInterpreter } from "./interpreter.js";
|
|
2
|
+
export type { CodeInterpreterCreateOptions } from "./interpreter.js";
|
|
3
|
+
export type { AdapterFactory } from "./factory/adapterFactory.js";
|
|
4
|
+
export { DefaultAdapterFactory, createDefaultAdapterFactory } from "./factory/defaultAdapterFactory.js";
|
|
5
|
+
export type { CodeContext, SupportedLanguage } from "./models.js";
|
|
6
|
+
export { SupportedLanguage as SupportedLanguages } from "./models.js";
|
|
7
|
+
export type { Codes } from "./services/codes.js";
|
|
8
|
+
export type { Execution, ExecutionComplete, ExecutionError, ExecutionHandlers, ExecutionInit, ExecutionResult, OutputMessage, } from "@alibaba-group/opensandbox";
|
|
9
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AACnD,YAAY,EAAE,4BAA4B,EAAE,MAAM,kBAAkB,CAAC;AAErE,YAAY,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,EAAE,qBAAqB,EAAE,2BAA2B,EAAE,MAAM,oCAAoC,CAAC;AAExG,YAAY,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAClE,OAAO,EAAE,iBAAiB,IAAI,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAEtE,YAAY,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEjD,YAAY,EACV,SAAS,EACT,iBAAiB,EACjB,cAAc,EACd,iBAAiB,EACjB,aAAa,EACb,eAAe,EACf,aAAa,GACd,MAAM,4BAA4B,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export { CodeInterpreter } from "./interpreter.js";
|
|
15
|
+
export { DefaultAdapterFactory, createDefaultAdapterFactory } from "./factory/defaultAdapterFactory.js";
|
|
16
|
+
export { SupportedLanguage as SupportedLanguages } from "./models.js";
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { Sandbox } from "@alibaba-group/opensandbox";
|
|
2
|
+
import type { AdapterFactory } from "./factory/adapterFactory.js";
|
|
3
|
+
import type { Codes } from "./services/codes.js";
|
|
4
|
+
export interface CodeInterpreterCreateOptions {
|
|
5
|
+
adapterFactory?: AdapterFactory;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Code interpreter facade (JS/TS).
|
|
9
|
+
*
|
|
10
|
+
* This class wraps an existing {@link Sandbox} and provides a high-level API for code execution.
|
|
11
|
+
*
|
|
12
|
+
* - Use {@link codes} to create contexts and run code.
|
|
13
|
+
* - {@link files}, {@link commands}, and {@link metrics} are exposed for convenience and are
|
|
14
|
+
* the same instances as on the underlying {@link Sandbox}.
|
|
15
|
+
*/
|
|
16
|
+
export declare class CodeInterpreter {
|
|
17
|
+
readonly sandbox: Sandbox;
|
|
18
|
+
readonly codes: Codes;
|
|
19
|
+
private constructor();
|
|
20
|
+
static create(sandbox: Sandbox, opts?: CodeInterpreterCreateOptions): Promise<CodeInterpreter>;
|
|
21
|
+
get id(): string;
|
|
22
|
+
get files(): import("@alibaba-group/opensandbox").SandboxFiles;
|
|
23
|
+
get commands(): import("@alibaba-group/opensandbox").ExecdCommands;
|
|
24
|
+
get metrics(): import("@alibaba-group/opensandbox").ExecdMetrics;
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=interpreter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interpreter.d.ts","sourceRoot":"","sources":["../src/interpreter.ts"],"names":[],"mappings":"AAeA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,4BAA4B,CAAC;AAG1D,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,6BAA6B,CAAC;AAClE,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,qBAAqB,CAAC;AAEjD,MAAM,WAAW,4BAA4B;IAC3C,cAAc,CAAC,EAAE,cAAc,CAAC;CACjC;AAED;;;;;;;;GAQG;AACH,qBAAa,eAAe;IAExB,QAAQ,CAAC,OAAO,EAAE,OAAO;IACzB,QAAQ,CAAC,KAAK,EAAE,KAAK;IAFvB,OAAO;WAKM,MAAM,CAAC,OAAO,EAAE,OAAO,EAAE,IAAI,GAAE,4BAAiC,GAAG,OAAO,CAAC,eAAe,CAAC;IAQxG,IAAI,EAAE,WAEL;IAED,IAAI,KAAK,sDAER;IAED,IAAI,QAAQ,uDAEX;IAED,IAAI,OAAO,sDAEV;CACF"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
import { DEFAULT_EXECD_PORT } from "@alibaba-group/opensandbox";
|
|
15
|
+
import { createDefaultAdapterFactory } from "./factory/defaultAdapterFactory.js";
|
|
16
|
+
/**
|
|
17
|
+
* Code interpreter facade (JS/TS).
|
|
18
|
+
*
|
|
19
|
+
* This class wraps an existing {@link Sandbox} and provides a high-level API for code execution.
|
|
20
|
+
*
|
|
21
|
+
* - Use {@link codes} to create contexts and run code.
|
|
22
|
+
* - {@link files}, {@link commands}, and {@link metrics} are exposed for convenience and are
|
|
23
|
+
* the same instances as on the underlying {@link Sandbox}.
|
|
24
|
+
*/
|
|
25
|
+
export class CodeInterpreter {
|
|
26
|
+
sandbox;
|
|
27
|
+
codes;
|
|
28
|
+
constructor(sandbox, codes) {
|
|
29
|
+
this.sandbox = sandbox;
|
|
30
|
+
this.codes = codes;
|
|
31
|
+
}
|
|
32
|
+
static async create(sandbox, opts = {}) {
|
|
33
|
+
const execdBaseUrl = await sandbox.getEndpointUrl(DEFAULT_EXECD_PORT);
|
|
34
|
+
const adapterFactory = opts.adapterFactory ?? createDefaultAdapterFactory();
|
|
35
|
+
const codes = adapterFactory.createCodes({ sandbox, execdBaseUrl });
|
|
36
|
+
return new CodeInterpreter(sandbox, codes);
|
|
37
|
+
}
|
|
38
|
+
get id() {
|
|
39
|
+
return this.sandbox.id;
|
|
40
|
+
}
|
|
41
|
+
get files() {
|
|
42
|
+
return this.sandbox.files;
|
|
43
|
+
}
|
|
44
|
+
get commands() {
|
|
45
|
+
return this.sandbox.commands;
|
|
46
|
+
}
|
|
47
|
+
get metrics() {
|
|
48
|
+
return this.sandbox.metrics;
|
|
49
|
+
}
|
|
50
|
+
}
|
package/dist/models.d.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export declare const SupportedLanguage: {
|
|
2
|
+
readonly PYTHON: "python";
|
|
3
|
+
readonly JAVA: "java";
|
|
4
|
+
readonly GO: "go";
|
|
5
|
+
readonly TYPESCRIPT: "typescript";
|
|
6
|
+
readonly JAVASCRIPT: "javascript";
|
|
7
|
+
readonly BASH: "bash";
|
|
8
|
+
};
|
|
9
|
+
export type SupportedLanguage = (typeof SupportedLanguage)[keyof typeof SupportedLanguage];
|
|
10
|
+
export interface CodeContext {
|
|
11
|
+
id?: string;
|
|
12
|
+
language: SupportedLanguage | (string & {});
|
|
13
|
+
}
|
|
14
|
+
export interface RunCodeRequest {
|
|
15
|
+
code: string;
|
|
16
|
+
context: CodeContext;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=models.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"models.d.ts","sourceRoot":"","sources":["../src/models.ts"],"names":[],"mappings":"AAcA,eAAO,MAAM,iBAAiB;;;;;;;CAOpB,CAAC;AAEX,MAAM,MAAM,iBAAiB,GAC3B,CAAC,OAAO,iBAAiB,CAAC,CAAC,MAAM,OAAO,iBAAiB,CAAC,CAAC;AAE7D,MAAM,WAAW,WAAW;IAC1B,EAAE,CAAC,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,iBAAiB,GAAG,CAAC,MAAM,GAAG,EAAE,CAAC,CAAC;CAC7C;AAED,MAAM,WAAW,cAAc;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,WAAW,CAAC;CACtB"}
|
package/dist/models.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export const SupportedLanguage = {
|
|
15
|
+
PYTHON: "python",
|
|
16
|
+
JAVA: "java",
|
|
17
|
+
GO: "go",
|
|
18
|
+
TYPESCRIPT: "typescript",
|
|
19
|
+
JAVASCRIPT: "javascript",
|
|
20
|
+
BASH: "bash",
|
|
21
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { ServerStreamEvent } from "@alibaba-group/opensandbox";
|
|
2
|
+
import type { Execution, ExecutionHandlers } from "@alibaba-group/opensandbox";
|
|
3
|
+
import type { CodeContext, RunCodeRequest, SupportedLanguage } from "../models.js";
|
|
4
|
+
export interface Codes {
|
|
5
|
+
createContext(language: SupportedLanguage): Promise<CodeContext>;
|
|
6
|
+
/**
|
|
7
|
+
* Get an existing context by id.
|
|
8
|
+
*/
|
|
9
|
+
getContext(contextId: string): Promise<CodeContext>;
|
|
10
|
+
/**
|
|
11
|
+
* List active contexts. If language is provided, filters by language/runtime.
|
|
12
|
+
*/
|
|
13
|
+
listContexts(language?: SupportedLanguage): Promise<CodeContext[]>;
|
|
14
|
+
/**
|
|
15
|
+
* Delete a context by id.
|
|
16
|
+
*/
|
|
17
|
+
deleteContext(contextId: string): Promise<void>;
|
|
18
|
+
/**
|
|
19
|
+
* Delete all contexts under the specified language/runtime.
|
|
20
|
+
*/
|
|
21
|
+
deleteContexts(language: SupportedLanguage): Promise<void>;
|
|
22
|
+
run(code: string, opts?: {
|
|
23
|
+
context?: CodeContext;
|
|
24
|
+
language?: SupportedLanguage;
|
|
25
|
+
handlers?: ExecutionHandlers;
|
|
26
|
+
signal?: AbortSignal;
|
|
27
|
+
}): Promise<Execution>;
|
|
28
|
+
runStream(req: RunCodeRequest, signal?: AbortSignal): AsyncIterable<ServerStreamEvent>;
|
|
29
|
+
interrupt(contextId: string): Promise<void>;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=codes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codes.d.ts","sourceRoot":"","sources":["../../src/services/codes.ts"],"names":[],"mappings":"AAcA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AACpE,OAAO,KAAK,EAAE,SAAS,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAC/E,OAAO,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,cAAc,CAAC;AAEnF,MAAM,WAAW,KAAK;IACpB,aAAa,CAAC,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IACjE;;OAEG;IACH,UAAU,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,CAAC,CAAC;IACpD;;OAEG;IACH,YAAY,CAAC,QAAQ,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC,CAAC;IACnE;;OAEG;IACH,aAAa,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChD;;OAEG;IACH,cAAc,CAAC,QAAQ,EAAE,iBAAiB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAE3D,GAAG,CACD,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE;QAAE,OAAO,CAAC,EAAE,WAAW,CAAC;QAAC,QAAQ,CAAC,EAAE,iBAAiB,CAAC;QAAC,QAAQ,CAAC,EAAE,iBAAiB,CAAC;QAAC,MAAM,CAAC,EAAE,WAAW,CAAA;KAAE,GACjH,OAAO,CAAC,SAAS,CAAC,CAAC;IAEtB,SAAS,CACP,GAAG,EAAE,cAAc,EACnB,MAAM,CAAC,EAAE,WAAW,GACnB,aAAa,CAAC,iBAAiB,CAAC,CAAC;IAEpC,SAAS,CAAC,SAAS,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC7C"}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
// Copyright 2026 Alibaba Group Holding Ltd.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
export {};
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@alibaba-group/opensandbox-code-interpreter",
|
|
3
|
+
"version": "0.1.0-dev1",
|
|
4
|
+
"description": "OpenSandbox Code Interpreter TypeScript/JavaScript SDK",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"types": "./dist/index.d.ts",
|
|
12
|
+
"default": "./dist/index.js"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"browser": "./dist/index.js",
|
|
16
|
+
"sideEffects": false,
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/alibaba/OpenSandbox.git"
|
|
20
|
+
},
|
|
21
|
+
"bugs": {
|
|
22
|
+
"url": "https://github.com/alibaba/OpenSandbox/issues"
|
|
23
|
+
},
|
|
24
|
+
"homepage": "https://github.com/alibaba/OpenSandbox",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=20"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@alibaba-group/opensandbox": "^0.1.0-dev1"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@eslint/js": "^9.39.2",
|
|
36
|
+
"eslint": "^9.39.2",
|
|
37
|
+
"typescript": "^5.7.2",
|
|
38
|
+
"typescript-eslint": "^8.52.0"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsc -p tsconfig.json",
|
|
42
|
+
"lint": "eslint src --max-warnings 0",
|
|
43
|
+
"clean": "rm -rf dist"
|
|
44
|
+
}
|
|
45
|
+
}
|