@agentrun/sdk 0.0.3 → 0.0.4
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 +20 -15
- package/dist/agent-runtime/api/index.cjs +907 -0
- package/dist/agent-runtime/api/index.cjs.map +1 -0
- package/dist/agent-runtime/api/index.d.cts +193 -0
- package/dist/agent-runtime/api/index.d.ts +193 -0
- package/dist/agent-runtime/api/index.js +882 -0
- package/dist/agent-runtime/api/index.js.map +1 -0
- package/dist/agent-runtime/index.cjs +2528 -0
- package/dist/agent-runtime/index.cjs.map +1 -0
- package/dist/agent-runtime/index.d.cts +778 -0
- package/dist/agent-runtime/index.d.ts +778 -0
- package/dist/agent-runtime/index.js +2496 -0
- package/dist/agent-runtime/index.js.map +1 -0
- package/dist/base-K1GMfJbH.d.ts +59 -0
- package/dist/base-xcWt5bua.d.cts +59 -0
- package/dist/client-DHXxjuo3.d.ts +58 -0
- package/dist/client-DPUTs69s.d.cts +58 -0
- package/dist/config-07gMelJP.d.cts +125 -0
- package/dist/config-07gMelJP.d.ts +125 -0
- package/dist/control-api-BWD4eua5.d.cts +27 -0
- package/dist/control-api-d-82Sgpr.d.ts +27 -0
- package/dist/credential/api/index.cjs +655 -0
- package/dist/credential/api/index.cjs.map +1 -0
- package/dist/credential/api/index.d.cts +93 -0
- package/dist/credential/api/index.d.ts +93 -0
- package/dist/credential/api/index.js +630 -0
- package/dist/credential/api/index.js.map +1 -0
- package/dist/credential/index.cjs +1286 -0
- package/dist/credential/index.cjs.map +1 -0
- package/dist/credential/index.d.cts +246 -0
- package/dist/credential/index.d.ts +246 -0
- package/dist/credential/index.js +1263 -0
- package/dist/credential/index.js.map +1 -0
- package/dist/data-api-B-4h9_Vf.d.ts +166 -0
- package/dist/data-api-DsG-8JRQ.d.cts +166 -0
- package/dist/exception-DM9H2Rpo.d.cts +68 -0
- package/dist/exception-DM9H2Rpo.d.ts +68 -0
- package/dist/index.cjs +29281 -1427
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +38 -4485
- package/dist/index.d.ts +38 -4485
- package/dist/index.js +29259 -1426
- package/dist/index.js.map +1 -1
- package/dist/integration/builtin/index.cjs +9399 -0
- package/dist/integration/builtin/index.cjs.map +1 -0
- package/dist/integration/builtin/index.d.cts +692 -0
- package/dist/integration/builtin/index.d.ts +692 -0
- package/dist/integration/builtin/index.js +9363 -0
- package/dist/integration/builtin/index.js.map +1 -0
- package/dist/integration/index.cjs +9524 -0
- package/dist/integration/index.cjs.map +1 -0
- package/dist/integration/index.d.cts +98 -0
- package/dist/integration/index.d.ts +98 -0
- package/dist/integration/index.js +9481 -0
- package/dist/integration/index.js.map +1 -0
- package/dist/integration/mastra/index.cjs +9383 -0
- package/dist/integration/mastra/index.cjs.map +1 -0
- package/dist/integration/mastra/index.d.cts +186 -0
- package/dist/integration/mastra/index.d.ts +186 -0
- package/dist/integration/mastra/index.js +9348 -0
- package/dist/integration/mastra/index.js.map +1 -0
- package/dist/model/index.cjs +2392 -0
- package/dist/model/index.cjs.map +1 -0
- package/dist/model/index.d.cts +97 -0
- package/dist/model/index.d.ts +97 -0
- package/dist/model/index.js +2368 -0
- package/dist/model/index.js.map +1 -0
- package/dist/model--I90nCqy.d.cts +66 -0
- package/dist/model--I90nCqy.d.ts +66 -0
- package/dist/model-BV7A6Trb.d.cts +512 -0
- package/dist/model-DGBy-o_L.d.cts +176 -0
- package/dist/model-DGBy-o_L.d.ts +176 -0
- package/dist/model-RiiiZnou.d.ts +512 -0
- package/dist/model-service-D-P2FZNi.d.ts +540 -0
- package/dist/model-service-VpzBb7rV.d.cts +540 -0
- package/dist/resource-CQovFUeh.d.cts +44 -0
- package/dist/resource-DnE_DEka.d.ts +44 -0
- package/dist/sandbox/index.cjs +3557 -0
- package/dist/sandbox/index.cjs.map +1 -0
- package/dist/sandbox/index.d.cts +1598 -0
- package/dist/sandbox/index.d.ts +1598 -0
- package/dist/sandbox/index.js +3532 -0
- package/dist/sandbox/index.js.map +1 -0
- package/dist/server/adapter/index.cjs +24886 -0
- package/dist/server/adapter/index.cjs.map +1 -0
- package/dist/server/adapter/index.d.cts +68 -0
- package/dist/server/adapter/index.d.ts +68 -0
- package/dist/server/adapter/index.js +24883 -0
- package/dist/server/adapter/index.js.map +1 -0
- package/dist/server/core/index.cjs +313 -0
- package/dist/server/core/index.cjs.map +1 -0
- package/dist/server/core/index.d.cts +58 -0
- package/dist/server/core/index.d.ts +58 -0
- package/dist/server/core/index.js +309 -0
- package/dist/server/core/index.js.map +1 -0
- package/dist/server/index.cjs +25098 -0
- package/dist/server/index.cjs.map +1 -0
- package/dist/server/index.d.cts +73 -0
- package/dist/server/index.d.ts +73 -0
- package/dist/server/index.js +25067 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/protocol/index.cjs +1057 -0
- package/dist/server/protocol/index.cjs.map +1 -0
- package/dist/server/protocol/index.d.cts +135 -0
- package/dist/server/protocol/index.d.ts +135 -0
- package/dist/server/protocol/index.js +1053 -0
- package/dist/server/protocol/index.js.map +1 -0
- package/dist/tool-CG0LY-ov.d.cts +155 -0
- package/dist/tool-JJHam0ms.d.ts +155 -0
- package/dist/toolset/api/index.cjs +754 -0
- package/dist/toolset/api/index.cjs.map +1 -0
- package/dist/toolset/api/index.d.cts +207 -0
- package/dist/toolset/api/index.d.ts +207 -0
- package/dist/toolset/api/index.js +727 -0
- package/dist/toolset/api/index.js.map +1 -0
- package/dist/toolset/index.cjs +1945 -0
- package/dist/toolset/index.cjs.map +1 -0
- package/dist/toolset/index.d.cts +182 -0
- package/dist/toolset/index.d.ts +182 -0
- package/dist/toolset/index.js +1920 -0
- package/dist/toolset/index.js.map +1 -0
- package/dist/toolset-BYDvhwRp.d.cts +394 -0
- package/dist/toolset-CSRsJxCb.d.ts +394 -0
- package/dist/utils/index.cjs +994 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +30 -0
- package/dist/utils/index.d.ts +30 -0
- package/dist/utils/index.js +951 -0
- package/dist/utils/index.js.map +1 -0
- package/package.json +65 -13
|
@@ -0,0 +1,3557 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var dotenv = require('dotenv');
|
|
4
|
+
var $AgentRun4 = require('@alicloud/agentrun20250910');
|
|
5
|
+
var $OpenApi = require('@alicloud/openapi-client');
|
|
6
|
+
var $Util = require('@alicloud/tea-util');
|
|
7
|
+
var fs2 = require('fs');
|
|
8
|
+
var path = require('path');
|
|
9
|
+
|
|
10
|
+
function _interopNamespace(e) {
|
|
11
|
+
if (e && e.__esModule) return e;
|
|
12
|
+
var n = Object.create(null);
|
|
13
|
+
if (e) {
|
|
14
|
+
Object.keys(e).forEach(function (k) {
|
|
15
|
+
if (k !== 'default') {
|
|
16
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
17
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
18
|
+
enumerable: true,
|
|
19
|
+
get: function () { return e[k]; }
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
n.default = e;
|
|
25
|
+
return Object.freeze(n);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var dotenv__namespace = /*#__PURE__*/_interopNamespace(dotenv);
|
|
29
|
+
var $AgentRun4__namespace = /*#__PURE__*/_interopNamespace($AgentRun4);
|
|
30
|
+
var $OpenApi__namespace = /*#__PURE__*/_interopNamespace($OpenApi);
|
|
31
|
+
var $Util__namespace = /*#__PURE__*/_interopNamespace($Util);
|
|
32
|
+
var fs2__namespace = /*#__PURE__*/_interopNamespace(fs2);
|
|
33
|
+
var path__namespace = /*#__PURE__*/_interopNamespace(path);
|
|
34
|
+
|
|
35
|
+
var __defProp = Object.defineProperty;
|
|
36
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
37
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
38
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
39
|
+
var __esm = (fn, res) => function __init() {
|
|
40
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
41
|
+
};
|
|
42
|
+
var __export = (target, all) => {
|
|
43
|
+
for (var name in all)
|
|
44
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
45
|
+
};
|
|
46
|
+
var __copyProps = (to, from, except, desc) => {
|
|
47
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
48
|
+
for (let key of __getOwnPropNames(from))
|
|
49
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
50
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
51
|
+
}
|
|
52
|
+
return to;
|
|
53
|
+
};
|
|
54
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
55
|
+
|
|
56
|
+
// src/utils/log.ts
|
|
57
|
+
var LOG_LEVELS, COLORS, Logger, logger;
|
|
58
|
+
var init_log = __esm({
|
|
59
|
+
"src/utils/log.ts"() {
|
|
60
|
+
LOG_LEVELS = {
|
|
61
|
+
debug: 0,
|
|
62
|
+
info: 1,
|
|
63
|
+
warn: 2,
|
|
64
|
+
error: 3
|
|
65
|
+
};
|
|
66
|
+
COLORS = {
|
|
67
|
+
reset: "\x1B[0m",
|
|
68
|
+
bright: "\x1B[1m",
|
|
69
|
+
dim: "\x1B[2m",
|
|
70
|
+
italic: "\x1B[3m",
|
|
71
|
+
blue: "\x1B[34m",
|
|
72
|
+
cyan: "\x1B[36m",
|
|
73
|
+
yellow: "\x1B[33m",
|
|
74
|
+
red: "\x1B[31m"
|
|
75
|
+
};
|
|
76
|
+
Logger = class {
|
|
77
|
+
level = "info";
|
|
78
|
+
// match Python logger name
|
|
79
|
+
prefix = "agentrun-logger";
|
|
80
|
+
setLevel(level) {
|
|
81
|
+
this.level = level;
|
|
82
|
+
}
|
|
83
|
+
shouldLog(level) {
|
|
84
|
+
return LOG_LEVELS[level] >= LOG_LEVELS[this.level];
|
|
85
|
+
}
|
|
86
|
+
getColor(level) {
|
|
87
|
+
switch (level) {
|
|
88
|
+
case "debug":
|
|
89
|
+
return COLORS.cyan;
|
|
90
|
+
case "info":
|
|
91
|
+
return COLORS.blue;
|
|
92
|
+
case "warn":
|
|
93
|
+
return COLORS.yellow;
|
|
94
|
+
case "error":
|
|
95
|
+
return COLORS.red;
|
|
96
|
+
default:
|
|
97
|
+
return COLORS.reset;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
// format timestamp like Python: YYYY-MM-DD HH:mm:ss,SSS
|
|
101
|
+
formatTimestamp(d = /* @__PURE__ */ new Date()) {
|
|
102
|
+
const pad = (n, sz = 2) => n.toString().padStart(sz, "0");
|
|
103
|
+
const year = d.getFullYear();
|
|
104
|
+
const month = pad(d.getMonth() + 1);
|
|
105
|
+
const day = pad(d.getDate());
|
|
106
|
+
const hour = pad(d.getHours());
|
|
107
|
+
const minute = pad(d.getMinutes());
|
|
108
|
+
const second = pad(d.getSeconds());
|
|
109
|
+
const ms = pad(d.getMilliseconds(), 3);
|
|
110
|
+
return `${year}-${month}-${day} ${hour}:${minute}:${second},${ms}`;
|
|
111
|
+
}
|
|
112
|
+
// attempt to infer caller file and line by parsing Error.stack
|
|
113
|
+
// helper: parse a single stack frame into {filepath, line, functionName}
|
|
114
|
+
parseFrame(frame) {
|
|
115
|
+
const m = frame.match(/^(?:at\s+)?(?:(.+?)\s+\()?(.*?):(\d+):(\d+)\)?$/);
|
|
116
|
+
if (!m) return null;
|
|
117
|
+
return {
|
|
118
|
+
functionName: m[1] ? m[1].trim() : void 0,
|
|
119
|
+
filepath: m[2],
|
|
120
|
+
line: parseInt(m[3], 10)
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
// get caller by fixed stack offset (used in public log methods)
|
|
124
|
+
getCallerByOffset() {
|
|
125
|
+
const err = new Error();
|
|
126
|
+
const stack = err.stack;
|
|
127
|
+
if (!stack) return {};
|
|
128
|
+
const lines = stack.split("\n").map((l) => l.trim());
|
|
129
|
+
for (let i = 3; i < lines.length; i++) {
|
|
130
|
+
let parsed = this.parseFrame(lines[i]);
|
|
131
|
+
if (!parsed) {
|
|
132
|
+
const m = lines[i].match(/(\/[^:\s]+:\d+:\d+)/);
|
|
133
|
+
if (m) {
|
|
134
|
+
const parts = m[1].split(":");
|
|
135
|
+
parts.pop();
|
|
136
|
+
const ln = Number(parts.pop());
|
|
137
|
+
const fp2 = parts.join(":");
|
|
138
|
+
parsed = { filepath: fp2, line: ln, functionName: void 0 };
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (!parsed) continue;
|
|
142
|
+
const fp = parsed.filepath;
|
|
143
|
+
if (fp.includes("node_modules") || fp.includes("internal") || fp.includes("<anonymous>") || fp.includes("native"))
|
|
144
|
+
continue;
|
|
145
|
+
return { filepath: parsed.filepath, line: parsed.line };
|
|
146
|
+
}
|
|
147
|
+
const cwd = process.cwd();
|
|
148
|
+
for (let i = 0; i < lines.length; i++) {
|
|
149
|
+
let parsed = this.parseFrame(lines[i]);
|
|
150
|
+
if (!parsed) {
|
|
151
|
+
const m = lines[i].match(/(\/[^:\s]+:\d+:\d+)/);
|
|
152
|
+
if (m) {
|
|
153
|
+
const parts = m[1].split(":");
|
|
154
|
+
parts.pop();
|
|
155
|
+
const ln = Number(parts.pop());
|
|
156
|
+
const fp2 = parts.join(":");
|
|
157
|
+
parsed = { filepath: fp2, line: ln, functionName: void 0 };
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
if (!parsed) continue;
|
|
161
|
+
const fp = parsed.filepath;
|
|
162
|
+
if (fp.includes("node_modules") || fp.includes("internal") || fp.includes("<anonymous>") || fp.includes("native"))
|
|
163
|
+
continue;
|
|
164
|
+
if (fp.includes("/src/utils/log.ts")) continue;
|
|
165
|
+
if (fp.startsWith(cwd)) return { filepath: parsed.filepath, line: parsed.line };
|
|
166
|
+
}
|
|
167
|
+
for (let i = 0; i < lines.length; i++) {
|
|
168
|
+
let parsed = this.parseFrame(lines[i]);
|
|
169
|
+
if (!parsed) {
|
|
170
|
+
const m = lines[i].match(/(\/[^:\s]+:\d+:\d+)/);
|
|
171
|
+
if (m) {
|
|
172
|
+
const parts = m[1].split(":");
|
|
173
|
+
parts.pop();
|
|
174
|
+
const ln = Number(parts.pop());
|
|
175
|
+
const fp2 = parts.join(":");
|
|
176
|
+
parsed = { filepath: fp2, line: ln, functionName: void 0 };
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
if (!parsed) continue;
|
|
180
|
+
const fp = parsed.filepath;
|
|
181
|
+
if (fp.includes("node_modules") || fp.includes("internal") || fp.includes("<anonymous>") || fp.includes("native"))
|
|
182
|
+
continue;
|
|
183
|
+
if (fp.includes("/src/utils/log.ts")) continue;
|
|
184
|
+
return { filepath: parsed.filepath, line: parsed.line };
|
|
185
|
+
}
|
|
186
|
+
return {};
|
|
187
|
+
}
|
|
188
|
+
formatMessage(level, message, filepath, line) {
|
|
189
|
+
const timestamp = this.formatTimestamp();
|
|
190
|
+
const color = this.getColor(level);
|
|
191
|
+
const reset = COLORS.reset;
|
|
192
|
+
const levelName = level === "warn" ? "WARNING" : level.toUpperCase();
|
|
193
|
+
const levelStr = `${COLORS.bright}${color}${levelName}${reset}`;
|
|
194
|
+
const nameStr = `${color}[${this.prefix}]${reset}`;
|
|
195
|
+
const tsStr = `${color} ${timestamp}${reset}`;
|
|
196
|
+
const pathInfo = filepath && line !== void 0 ? ` ${COLORS.dim}${COLORS.italic}${filepath}:${line}${reset}` : "";
|
|
197
|
+
const msg = level === "debug" ? `${COLORS.dim}${message}${reset}` : message;
|
|
198
|
+
return `
|
|
199
|
+
${levelStr} ${nameStr}${tsStr}${pathInfo}
|
|
200
|
+
${msg}
|
|
201
|
+
`;
|
|
202
|
+
}
|
|
203
|
+
debug(message, ...args) {
|
|
204
|
+
if (this.shouldLog("debug")) {
|
|
205
|
+
const caller = this.getCallerByOffset();
|
|
206
|
+
console.debug(this.formatMessage("debug", message, caller.filepath, caller.line), ...args);
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
info(message, ...args) {
|
|
210
|
+
if (this.shouldLog("info")) {
|
|
211
|
+
const caller = this.getCallerByOffset();
|
|
212
|
+
console.info(this.formatMessage("info", message, caller.filepath, caller.line), ...args);
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
warn(message, ...args) {
|
|
216
|
+
if (this.shouldLog("warn")) {
|
|
217
|
+
const caller = this.getCallerByOffset();
|
|
218
|
+
console.warn(this.formatMessage("warn", message, caller.filepath, caller.line), ...args);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
error(message, ...args) {
|
|
222
|
+
if (this.shouldLog("error")) {
|
|
223
|
+
const caller = this.getCallerByOffset();
|
|
224
|
+
console.error(this.formatMessage("error", message, caller.filepath, caller.line), ...args);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
};
|
|
228
|
+
logger = new Logger();
|
|
229
|
+
if (![void 0, null, "", "False", "FALSE", "false", "0"].includes(process.env["AGENTRUN_SDK_DEBUG"])) {
|
|
230
|
+
logger.setLevel("debug");
|
|
231
|
+
if (!globalThis._AGENTRUN_DEBUG_LOGGED) {
|
|
232
|
+
logger.warn("\u542F\u7528 AgentRun SDK \u8C03\u8BD5\u65E5\u5FD7\uFF0C \u79FB\u9664 AGENTRUN_SDK_DEBUG \u73AF\u5883\u53D8\u91CF\u4EE5\u5173\u95ED");
|
|
233
|
+
globalThis._AGENTRUN_DEBUG_LOGGED = true;
|
|
234
|
+
}
|
|
235
|
+
} else {
|
|
236
|
+
logger.setLevel("info");
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
// src/utils/version-check.ts
|
|
242
|
+
var VERSION;
|
|
243
|
+
var init_version_check = __esm({
|
|
244
|
+
"src/utils/version-check.ts"() {
|
|
245
|
+
init_log();
|
|
246
|
+
VERSION = "0.0.4" ;
|
|
247
|
+
if (!process.env.DISABLE_BREAKING_CHANGES_WARNING && !globalThis._AGENTRUN_VERSION_WARNING_SHOWN) {
|
|
248
|
+
globalThis._AGENTRUN_VERSION_WARNING_SHOWN = true;
|
|
249
|
+
logger.warn(
|
|
250
|
+
`\u5F53\u524D\u60A8\u6B63\u5728\u4F7F\u7528 AgentRun Python SDK \u7248\u672C ${VERSION}\u3002\u65E9\u671F\u7248\u672C\u901A\u5E38\u5305\u542B\u8BB8\u591A\u65B0\u529F\u80FD\uFF0C\u8FD9\u4E9B\u529F\u80FD\x1B[1;33m \u53EF\u80FD\u5F15\u5165\u4E0D\u517C\u5BB9\u7684\u53D8\u66F4 \x1B[0m\u3002\u4E3A\u907F\u514D\u6F5C\u5728\u95EE\u9898\uFF0C\u6211\u4EEC\u5F3A\u70C8\u5EFA\u8BAE\x1B[1;32m \u5C06\u4F9D\u8D56\u9501\u5B9A\u4E3A\u6B64\u7248\u672C \x1B[0m\u3002
|
|
251
|
+
You are currently using AgentRun Python SDK version ${VERSION}. Early versions often include many new features, which\x1B[1;33m may introduce breaking changes\x1B[0m. To avoid potential issues, we strongly recommend \x1B[1;32mpinning the dependency to this version\x1B[0m.
|
|
252
|
+
\x1B[2;3m pip install 'agentrun-sdk==${VERSION}' \x1B[0m
|
|
253
|
+
|
|
254
|
+
\u589E\u52A0\x1B[2;3m DISABLE_BREAKING_CHANGES_WARNING=1 \x1B[0m\u5230\u60A8\u7684\u73AF\u5883\u53D8\u91CF\u4EE5\u5173\u95ED\u6B64\u8B66\u544A\u3002
|
|
255
|
+
Add\x1B[2;3m DISABLE_BREAKING_CHANGES_WARNING=1 \x1B[0mto your environment variables to disable this warning.
|
|
256
|
+
|
|
257
|
+
Releases:\x1B[2;3m https://github.com/Serverless-Devs/agentrun-sdk-python/releases\x1B[0m`
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
});
|
|
262
|
+
function getEnvWithDefault(defaultValue, ...keys) {
|
|
263
|
+
for (const key of keys) {
|
|
264
|
+
const value = process.env[key];
|
|
265
|
+
if (value !== void 0 && value !== "") {
|
|
266
|
+
return value;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return defaultValue;
|
|
270
|
+
}
|
|
271
|
+
var Config;
|
|
272
|
+
var init_config = __esm({
|
|
273
|
+
"src/utils/config.ts"() {
|
|
274
|
+
dotenv__namespace.config();
|
|
275
|
+
Config = class _Config {
|
|
276
|
+
_accessKeyId;
|
|
277
|
+
_accessKeySecret;
|
|
278
|
+
_securityToken;
|
|
279
|
+
_accountId;
|
|
280
|
+
_token;
|
|
281
|
+
_regionId;
|
|
282
|
+
_timeout;
|
|
283
|
+
_readTimeout;
|
|
284
|
+
_controlEndpoint;
|
|
285
|
+
_dataEndpoint;
|
|
286
|
+
_devsEndpoint;
|
|
287
|
+
_headers;
|
|
288
|
+
constructor(options = {}) {
|
|
289
|
+
this._accessKeyId = options.accessKeyId ?? getEnvWithDefault("", "AGENTRUN_ACCESS_KEY_ID", "ALIBABA_CLOUD_ACCESS_KEY_ID");
|
|
290
|
+
this._accessKeySecret = options.accessKeySecret ?? getEnvWithDefault("", "AGENTRUN_ACCESS_KEY_SECRET", "ALIBABA_CLOUD_ACCESS_KEY_SECRET");
|
|
291
|
+
this._securityToken = options.securityToken ?? getEnvWithDefault("", "AGENTRUN_SECURITY_TOKEN", "ALIBABA_CLOUD_SECURITY_TOKEN");
|
|
292
|
+
this._accountId = options.accountId ?? getEnvWithDefault("", "AGENTRUN_ACCOUNT_ID", "FC_ACCOUNT_ID");
|
|
293
|
+
this._token = options.token;
|
|
294
|
+
this._regionId = options.regionId ?? getEnvWithDefault("cn-hangzhou", "AGENTRUN_REGION", "FC_REGION");
|
|
295
|
+
this._timeout = options.timeout ?? 6e5;
|
|
296
|
+
this._readTimeout = options.readTimeout ?? 1e8;
|
|
297
|
+
this._controlEndpoint = options.controlEndpoint ?? getEnvWithDefault("", "AGENTRUN_CONTROL_ENDPOINT");
|
|
298
|
+
this._dataEndpoint = options.dataEndpoint ?? getEnvWithDefault("", "AGENTRUN_DATA_ENDPOINT");
|
|
299
|
+
this._devsEndpoint = options.devsEndpoint ?? getEnvWithDefault("", "DEVS_ENDPOINT");
|
|
300
|
+
this._headers = options.headers ?? {};
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Create a new Config by merging multiple configs.
|
|
304
|
+
* Later configs take precedence.
|
|
305
|
+
*/
|
|
306
|
+
static withConfigs(...configs) {
|
|
307
|
+
return new _Config().update(...configs);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Update this config with values from other configs.
|
|
311
|
+
* Non-undefined values from later configs take precedence.
|
|
312
|
+
*/
|
|
313
|
+
update(...configs) {
|
|
314
|
+
for (const config2 of configs) {
|
|
315
|
+
if (!config2) continue;
|
|
316
|
+
if (config2._accessKeyId) this._accessKeyId = config2._accessKeyId;
|
|
317
|
+
if (config2._accessKeySecret) this._accessKeySecret = config2._accessKeySecret;
|
|
318
|
+
if (config2._securityToken) this._securityToken = config2._securityToken;
|
|
319
|
+
if (config2._accountId) this._accountId = config2._accountId;
|
|
320
|
+
if (config2._token) this._token = config2._token;
|
|
321
|
+
if (config2._regionId) this._regionId = config2._regionId;
|
|
322
|
+
if (config2._timeout) this._timeout = config2._timeout;
|
|
323
|
+
if (config2._readTimeout) this._readTimeout = config2._readTimeout;
|
|
324
|
+
if (config2._controlEndpoint) this._controlEndpoint = config2._controlEndpoint;
|
|
325
|
+
if (config2._dataEndpoint) this._dataEndpoint = config2._dataEndpoint;
|
|
326
|
+
if (config2._devsEndpoint) this._devsEndpoint = config2._devsEndpoint;
|
|
327
|
+
if (config2._headers && Object.keys(config2._headers).length > 0) {
|
|
328
|
+
this._headers = { ...this._headers, ...config2._headers };
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
return this;
|
|
332
|
+
}
|
|
333
|
+
get accessKeyId() {
|
|
334
|
+
return this._accessKeyId;
|
|
335
|
+
}
|
|
336
|
+
get accessKeySecret() {
|
|
337
|
+
return this._accessKeySecret;
|
|
338
|
+
}
|
|
339
|
+
get securityToken() {
|
|
340
|
+
return this._securityToken;
|
|
341
|
+
}
|
|
342
|
+
get accountId() {
|
|
343
|
+
if (!this._accountId) {
|
|
344
|
+
throw new Error(
|
|
345
|
+
"Account ID is not set. Please add AGENTRUN_ACCOUNT_ID environment variable or set it in code."
|
|
346
|
+
);
|
|
347
|
+
}
|
|
348
|
+
return this._accountId;
|
|
349
|
+
}
|
|
350
|
+
get token() {
|
|
351
|
+
return this._token;
|
|
352
|
+
}
|
|
353
|
+
get regionId() {
|
|
354
|
+
return this._regionId || "cn-hangzhou";
|
|
355
|
+
}
|
|
356
|
+
get timeout() {
|
|
357
|
+
return this._timeout || 6e5;
|
|
358
|
+
}
|
|
359
|
+
get readTimeout() {
|
|
360
|
+
return this._readTimeout || 1e8;
|
|
361
|
+
}
|
|
362
|
+
get controlEndpoint() {
|
|
363
|
+
if (this._controlEndpoint) {
|
|
364
|
+
return this._controlEndpoint;
|
|
365
|
+
}
|
|
366
|
+
return `https://agentrun.${this.regionId}.aliyuncs.com`;
|
|
367
|
+
}
|
|
368
|
+
get dataEndpoint() {
|
|
369
|
+
if (this._dataEndpoint) {
|
|
370
|
+
return this._dataEndpoint;
|
|
371
|
+
}
|
|
372
|
+
return `https://${this.accountId}.agentrun-data.${this.regionId}.aliyuncs.com`;
|
|
373
|
+
}
|
|
374
|
+
get devsEndpoint() {
|
|
375
|
+
if (this._devsEndpoint) {
|
|
376
|
+
return this._devsEndpoint;
|
|
377
|
+
}
|
|
378
|
+
return `https://devs.${this.regionId}.aliyuncs.com`;
|
|
379
|
+
}
|
|
380
|
+
get headers() {
|
|
381
|
+
return this._headers;
|
|
382
|
+
}
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
});
|
|
386
|
+
|
|
387
|
+
// src/utils/exception.ts
|
|
388
|
+
var AgentRunError, HTTPError, ClientError, ServerError, ResourceNotExistError, ResourceAlreadyExistError;
|
|
389
|
+
var init_exception = __esm({
|
|
390
|
+
"src/utils/exception.ts"() {
|
|
391
|
+
AgentRunError = class _AgentRunError extends Error {
|
|
392
|
+
constructor(message) {
|
|
393
|
+
super(message);
|
|
394
|
+
this.name = "AgentRunError";
|
|
395
|
+
Object.setPrototypeOf(this, _AgentRunError.prototype);
|
|
396
|
+
}
|
|
397
|
+
};
|
|
398
|
+
HTTPError = class _HTTPError extends AgentRunError {
|
|
399
|
+
statusCode;
|
|
400
|
+
requestId;
|
|
401
|
+
errorCode;
|
|
402
|
+
constructor(statusCode, message, options) {
|
|
403
|
+
super(message);
|
|
404
|
+
this.name = "HTTPError";
|
|
405
|
+
this.statusCode = statusCode;
|
|
406
|
+
this.requestId = options?.requestId;
|
|
407
|
+
this.errorCode = options?.errorCode;
|
|
408
|
+
Object.setPrototypeOf(this, _HTTPError.prototype);
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Convert HTTP error to resource-specific error based on status code
|
|
412
|
+
*/
|
|
413
|
+
toResourceError(resourceType, resourceId) {
|
|
414
|
+
if (this.statusCode == 404) return new ResourceNotExistError(resourceType, resourceId);
|
|
415
|
+
else if (this.statusCode == 409) return new ResourceAlreadyExistError(resourceType, resourceId);
|
|
416
|
+
else if ((this.statusCode == 400 || this.statusCode == 500) && this.message.includes("already exists"))
|
|
417
|
+
return new ResourceAlreadyExistError(resourceType, resourceId);
|
|
418
|
+
else return this;
|
|
419
|
+
}
|
|
420
|
+
};
|
|
421
|
+
ClientError = class _ClientError extends HTTPError {
|
|
422
|
+
constructor(statusCode, message, options) {
|
|
423
|
+
super(statusCode, message, options);
|
|
424
|
+
this.name = "ClientError";
|
|
425
|
+
Object.setPrototypeOf(this, _ClientError.prototype);
|
|
426
|
+
}
|
|
427
|
+
};
|
|
428
|
+
ServerError = class _ServerError extends HTTPError {
|
|
429
|
+
constructor(statusCode, message, options) {
|
|
430
|
+
super(statusCode, message, options);
|
|
431
|
+
this.name = "ServerError";
|
|
432
|
+
Object.setPrototypeOf(this, _ServerError.prototype);
|
|
433
|
+
}
|
|
434
|
+
};
|
|
435
|
+
ResourceNotExistError = class _ResourceNotExistError extends ClientError {
|
|
436
|
+
resourceType;
|
|
437
|
+
resourceId;
|
|
438
|
+
constructor(resourceType, resourceId, options) {
|
|
439
|
+
const message = resourceId ? `${resourceType} '${resourceId}' does not exist` : `${resourceType} does not exist`;
|
|
440
|
+
super(404, message, options);
|
|
441
|
+
this.name = "ResourceNotExistError";
|
|
442
|
+
this.resourceType = resourceType;
|
|
443
|
+
this.resourceId = resourceId;
|
|
444
|
+
Object.setPrototypeOf(this, _ResourceNotExistError.prototype);
|
|
445
|
+
}
|
|
446
|
+
};
|
|
447
|
+
ResourceAlreadyExistError = class _ResourceAlreadyExistError extends ClientError {
|
|
448
|
+
resourceType;
|
|
449
|
+
resourceId;
|
|
450
|
+
constructor(resourceType, resourceId, options) {
|
|
451
|
+
const message = resourceId ? `${resourceType} '${resourceId}' already exists` : `${resourceType} already exists`;
|
|
452
|
+
super(409, message, options);
|
|
453
|
+
this.name = "ResourceAlreadyExistError";
|
|
454
|
+
this.resourceType = resourceType;
|
|
455
|
+
this.resourceId = resourceId;
|
|
456
|
+
Object.setPrototypeOf(this, _ResourceAlreadyExistError.prototype);
|
|
457
|
+
}
|
|
458
|
+
};
|
|
459
|
+
}
|
|
460
|
+
});
|
|
461
|
+
var $AgentRunClient, ControlAPI;
|
|
462
|
+
var init_control_api = __esm({
|
|
463
|
+
"src/utils/control-api.ts"() {
|
|
464
|
+
init_config();
|
|
465
|
+
$AgentRunClient = // @ts-expect-error - ESM interop: default.default exists when imported as ESM namespace
|
|
466
|
+
$AgentRun4__namespace.default?.default ?? $AgentRun4__namespace.default ?? $AgentRun4__namespace;
|
|
467
|
+
ControlAPI = class {
|
|
468
|
+
config;
|
|
469
|
+
constructor(config2) {
|
|
470
|
+
this.config = config2;
|
|
471
|
+
}
|
|
472
|
+
/**
|
|
473
|
+
* Get the underlying AgentRun client instance
|
|
474
|
+
*/
|
|
475
|
+
getClient(config2) {
|
|
476
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
477
|
+
let endpoint = cfg.controlEndpoint;
|
|
478
|
+
if (endpoint.startsWith("http://") || endpoint.startsWith("https://")) {
|
|
479
|
+
endpoint = endpoint.split("://")[1];
|
|
480
|
+
}
|
|
481
|
+
const openApiConfig = new $OpenApi__namespace.Config({
|
|
482
|
+
accessKeyId: cfg.accessKeyId,
|
|
483
|
+
accessKeySecret: cfg.accessKeySecret,
|
|
484
|
+
securityToken: cfg.securityToken || void 0,
|
|
485
|
+
regionId: cfg.regionId,
|
|
486
|
+
endpoint,
|
|
487
|
+
connectTimeout: cfg.timeout,
|
|
488
|
+
readTimeout: cfg.readTimeout
|
|
489
|
+
});
|
|
490
|
+
return new $AgentRunClient(openApiConfig);
|
|
491
|
+
}
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
});
|
|
495
|
+
var SandboxControlAPI;
|
|
496
|
+
var init_control = __esm({
|
|
497
|
+
"src/sandbox/api/control.ts"() {
|
|
498
|
+
init_control_api();
|
|
499
|
+
init_exception();
|
|
500
|
+
init_log();
|
|
501
|
+
SandboxControlAPI = class extends ControlAPI {
|
|
502
|
+
constructor(config2) {
|
|
503
|
+
super(config2);
|
|
504
|
+
}
|
|
505
|
+
/**
|
|
506
|
+
* Create Sandbox Template
|
|
507
|
+
*
|
|
508
|
+
* @param params - Method parameters
|
|
509
|
+
* @param params.input - Template configuration
|
|
510
|
+
* @param params.headers - Custom request headers
|
|
511
|
+
* @param params.config - Optional config override
|
|
512
|
+
* @returns Created Template object
|
|
513
|
+
*/
|
|
514
|
+
createTemplate = async (params) => {
|
|
515
|
+
const { input, headers, config: config2 } = params;
|
|
516
|
+
try {
|
|
517
|
+
const client = this.getClient(config2);
|
|
518
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
519
|
+
const response = await client.createTemplateWithOptions(
|
|
520
|
+
new $AgentRun4__namespace.CreateTemplateRequest({ body: input }),
|
|
521
|
+
headers ?? {},
|
|
522
|
+
runtime
|
|
523
|
+
);
|
|
524
|
+
logger.debug(
|
|
525
|
+
`request api createTemplate, request Request ID: ${response.body?.requestId}
|
|
526
|
+
request: ${JSON.stringify([input])}
|
|
527
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
528
|
+
);
|
|
529
|
+
return response.body?.data;
|
|
530
|
+
} catch (error) {
|
|
531
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
532
|
+
const e = error;
|
|
533
|
+
const statusCode = e.statusCode;
|
|
534
|
+
const message = e.message || "Unknown error";
|
|
535
|
+
const requestId = e.data?.requestId;
|
|
536
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
537
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
538
|
+
} else if (statusCode >= 500) {
|
|
539
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
540
|
+
}
|
|
541
|
+
}
|
|
542
|
+
throw error;
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
/**
|
|
546
|
+
* Delete Sandbox Template
|
|
547
|
+
*
|
|
548
|
+
* @param params - Method parameters
|
|
549
|
+
* @param params.templateName - Template name
|
|
550
|
+
* @param params.headers - Custom request headers
|
|
551
|
+
* @param params.config - Optional config override
|
|
552
|
+
* @returns Deleted Template object
|
|
553
|
+
*/
|
|
554
|
+
deleteTemplate = async (params) => {
|
|
555
|
+
const { templateName, headers, config: config2 } = params;
|
|
556
|
+
try {
|
|
557
|
+
const client = this.getClient(config2);
|
|
558
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
559
|
+
const response = await client.deleteTemplateWithOptions(templateName, headers ?? {}, runtime);
|
|
560
|
+
logger.debug(
|
|
561
|
+
`request api deleteTemplate, request Request ID: ${response.body?.requestId}
|
|
562
|
+
request: ${JSON.stringify([templateName])}
|
|
563
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
564
|
+
);
|
|
565
|
+
return response.body?.data;
|
|
566
|
+
} catch (error) {
|
|
567
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
568
|
+
const e = error;
|
|
569
|
+
const statusCode = e.statusCode;
|
|
570
|
+
const message = e.message || "Unknown error";
|
|
571
|
+
const requestId = e.data?.requestId;
|
|
572
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
573
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
574
|
+
} else if (statusCode >= 500) {
|
|
575
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
throw error;
|
|
579
|
+
}
|
|
580
|
+
};
|
|
581
|
+
/**
|
|
582
|
+
* Update Sandbox Template
|
|
583
|
+
*
|
|
584
|
+
* @param params - Method parameters
|
|
585
|
+
* @param params.templateName - Template name
|
|
586
|
+
* @param params.input - Template configuration
|
|
587
|
+
* @param params.headers - Custom request headers
|
|
588
|
+
* @param params.config - Optional config override
|
|
589
|
+
* @returns Updated Template object
|
|
590
|
+
*/
|
|
591
|
+
updateTemplate = async (params) => {
|
|
592
|
+
const { templateName, input, headers, config: config2 } = params;
|
|
593
|
+
try {
|
|
594
|
+
const client = this.getClient(config2);
|
|
595
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
596
|
+
const response = await client.updateTemplateWithOptions(
|
|
597
|
+
templateName,
|
|
598
|
+
new $AgentRun4__namespace.UpdateTemplateRequest({ body: input }),
|
|
599
|
+
headers ?? {},
|
|
600
|
+
runtime
|
|
601
|
+
);
|
|
602
|
+
logger.debug(
|
|
603
|
+
`request api updateTemplate, request Request ID: ${response.body?.requestId}
|
|
604
|
+
request: ${JSON.stringify([templateName, input])}
|
|
605
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
606
|
+
);
|
|
607
|
+
return response.body?.data;
|
|
608
|
+
} catch (error) {
|
|
609
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
610
|
+
const e = error;
|
|
611
|
+
const statusCode = e.statusCode;
|
|
612
|
+
const message = e.message || "Unknown error";
|
|
613
|
+
const requestId = e.data?.requestId;
|
|
614
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
615
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
616
|
+
} else if (statusCode >= 500) {
|
|
617
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
618
|
+
}
|
|
619
|
+
}
|
|
620
|
+
throw error;
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
/**
|
|
624
|
+
* Get Sandbox Template
|
|
625
|
+
*
|
|
626
|
+
* @param params - Method parameters
|
|
627
|
+
* @param params.templateName - Template name
|
|
628
|
+
* @param params.headers - Custom request headers
|
|
629
|
+
* @param params.config - Optional config override
|
|
630
|
+
* @returns Template object
|
|
631
|
+
*/
|
|
632
|
+
getTemplate = async (params) => {
|
|
633
|
+
const { templateName, headers, config: config2 } = params;
|
|
634
|
+
try {
|
|
635
|
+
const client = this.getClient(config2);
|
|
636
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
637
|
+
const response = await client.getTemplateWithOptions(templateName, headers ?? {}, runtime);
|
|
638
|
+
logger.debug(
|
|
639
|
+
`request api getTemplate, request Request ID: ${response.body?.requestId}
|
|
640
|
+
request: ${JSON.stringify([templateName])}
|
|
641
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
642
|
+
);
|
|
643
|
+
return response.body?.data;
|
|
644
|
+
} catch (error) {
|
|
645
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
646
|
+
const e = error;
|
|
647
|
+
const statusCode = e.statusCode;
|
|
648
|
+
const message = e.message || "Unknown error";
|
|
649
|
+
const requestId = e.data?.requestId;
|
|
650
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
651
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
652
|
+
} else if (statusCode >= 500) {
|
|
653
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
throw error;
|
|
657
|
+
}
|
|
658
|
+
};
|
|
659
|
+
/**
|
|
660
|
+
* List Sandbox Templates
|
|
661
|
+
*
|
|
662
|
+
* @param params - Method parameters
|
|
663
|
+
* @param params.input - List options
|
|
664
|
+
* @param params.headers - Custom request headers
|
|
665
|
+
* @param params.config - Optional config override
|
|
666
|
+
* @returns List of Template objects
|
|
667
|
+
*/
|
|
668
|
+
listTemplates = async (params) => {
|
|
669
|
+
const { input, headers, config: config2 } = params;
|
|
670
|
+
try {
|
|
671
|
+
const client = this.getClient(config2);
|
|
672
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
673
|
+
const response = await client.listTemplatesWithOptions(input, headers ?? {}, runtime);
|
|
674
|
+
logger.debug(
|
|
675
|
+
`request api listTemplates, request Request ID: ${response.body?.requestId}
|
|
676
|
+
request: ${JSON.stringify([input])}
|
|
677
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
678
|
+
);
|
|
679
|
+
return response.body?.data;
|
|
680
|
+
} catch (error) {
|
|
681
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
682
|
+
const e = error;
|
|
683
|
+
const statusCode = e.statusCode;
|
|
684
|
+
const message = e.message || "Unknown error";
|
|
685
|
+
const requestId = e.data?.requestId;
|
|
686
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
687
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
688
|
+
} else if (statusCode >= 500) {
|
|
689
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
throw error;
|
|
693
|
+
}
|
|
694
|
+
};
|
|
695
|
+
/**
|
|
696
|
+
* Create Sandbox
|
|
697
|
+
*
|
|
698
|
+
* @param params - Method parameters
|
|
699
|
+
* @param params.input - Sandbox configuration
|
|
700
|
+
* @param params.headers - Custom request headers
|
|
701
|
+
* @param params.config - Optional config override
|
|
702
|
+
* @returns Created Sandbox object
|
|
703
|
+
*/
|
|
704
|
+
createSandbox = async (params) => {
|
|
705
|
+
const { input, headers, config: config2 } = params;
|
|
706
|
+
try {
|
|
707
|
+
const client = this.getClient(config2);
|
|
708
|
+
const runtime = new $Util__namespace.RuntimeOptions({
|
|
709
|
+
readTimeout: 3e4
|
|
710
|
+
});
|
|
711
|
+
const response = await client.createSandboxWithOptions(
|
|
712
|
+
new $AgentRun4__namespace.CreateSandboxRequest({ body: input }),
|
|
713
|
+
headers ?? {},
|
|
714
|
+
runtime
|
|
715
|
+
);
|
|
716
|
+
logger.debug(
|
|
717
|
+
`request api createSandbox, request Request ID: ${response.body?.requestId}
|
|
718
|
+
request: ${JSON.stringify([input])}
|
|
719
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
720
|
+
);
|
|
721
|
+
return response.body?.data;
|
|
722
|
+
} catch (error) {
|
|
723
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
724
|
+
const e = error;
|
|
725
|
+
const statusCode = e.statusCode;
|
|
726
|
+
const message = e.message || "Unknown error";
|
|
727
|
+
const requestId = e.data?.requestId;
|
|
728
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
729
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
730
|
+
} else if (statusCode >= 500) {
|
|
731
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
throw error;
|
|
735
|
+
}
|
|
736
|
+
};
|
|
737
|
+
/**
|
|
738
|
+
* Delete Sandbox
|
|
739
|
+
*
|
|
740
|
+
* @param params - Method parameters
|
|
741
|
+
* @param params.sandboxId - Sandbox ID
|
|
742
|
+
* @param params.headers - Custom request headers
|
|
743
|
+
* @param params.config - Optional config override
|
|
744
|
+
* @returns Deleted Sandbox object
|
|
745
|
+
*/
|
|
746
|
+
deleteSandbox = async (params) => {
|
|
747
|
+
const { sandboxId, headers, config: config2 } = params;
|
|
748
|
+
try {
|
|
749
|
+
const client = this.getClient(config2);
|
|
750
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
751
|
+
const response = await client.deleteSandboxWithOptions(sandboxId, headers ?? {}, runtime);
|
|
752
|
+
logger.debug(
|
|
753
|
+
`request api deleteSandbox, request Request ID: ${response.body?.requestId}
|
|
754
|
+
request: ${JSON.stringify([sandboxId])}
|
|
755
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
756
|
+
);
|
|
757
|
+
return response.body?.data;
|
|
758
|
+
} catch (error) {
|
|
759
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
760
|
+
const e = error;
|
|
761
|
+
const statusCode = e.statusCode;
|
|
762
|
+
const message = e.message || "Unknown error";
|
|
763
|
+
const requestId = e.data?.requestId;
|
|
764
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
765
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
766
|
+
} else if (statusCode >= 500) {
|
|
767
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
throw error;
|
|
771
|
+
}
|
|
772
|
+
};
|
|
773
|
+
/**
|
|
774
|
+
* Stop Sandbox
|
|
775
|
+
*
|
|
776
|
+
* @param params - Method parameters
|
|
777
|
+
* @param params.sandboxId - Sandbox ID
|
|
778
|
+
* @param params.headers - Custom request headers
|
|
779
|
+
* @param params.config - Optional config override
|
|
780
|
+
* @returns Stopped Sandbox object
|
|
781
|
+
*/
|
|
782
|
+
stopSandbox = async (params) => {
|
|
783
|
+
const { sandboxId, headers, config: config2 } = params;
|
|
784
|
+
try {
|
|
785
|
+
const client = this.getClient(config2);
|
|
786
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
787
|
+
const response = await client.stopSandboxWithOptions(sandboxId, headers ?? {}, runtime);
|
|
788
|
+
logger.debug(
|
|
789
|
+
`request api stopSandbox, request Request ID: ${response.body?.requestId}
|
|
790
|
+
request: ${JSON.stringify([sandboxId])}
|
|
791
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
792
|
+
);
|
|
793
|
+
return response.body?.data;
|
|
794
|
+
} catch (error) {
|
|
795
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
796
|
+
const e = error;
|
|
797
|
+
const statusCode = e.statusCode;
|
|
798
|
+
const message = e.message || "Unknown error";
|
|
799
|
+
const requestId = e.data?.requestId;
|
|
800
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
801
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
802
|
+
} else if (statusCode >= 500) {
|
|
803
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
804
|
+
}
|
|
805
|
+
}
|
|
806
|
+
throw error;
|
|
807
|
+
}
|
|
808
|
+
};
|
|
809
|
+
/**
|
|
810
|
+
* Get Sandbox
|
|
811
|
+
*
|
|
812
|
+
* @param params - Method parameters
|
|
813
|
+
* @param params.sandboxId - Sandbox ID
|
|
814
|
+
* @param params.headers - Custom request headers
|
|
815
|
+
* @param params.config - Optional config override
|
|
816
|
+
* @returns Sandbox object
|
|
817
|
+
*/
|
|
818
|
+
getSandbox = async (params) => {
|
|
819
|
+
const { sandboxId, headers, config: config2 } = params;
|
|
820
|
+
try {
|
|
821
|
+
const client = this.getClient(config2);
|
|
822
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
823
|
+
const response = await client.getSandboxWithOptions(sandboxId, headers ?? {}, runtime);
|
|
824
|
+
logger.debug(
|
|
825
|
+
`request api getSandbox, request Request ID: ${response.body?.requestId}
|
|
826
|
+
request: ${JSON.stringify([sandboxId])}
|
|
827
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
828
|
+
);
|
|
829
|
+
return response.body?.data;
|
|
830
|
+
} catch (error) {
|
|
831
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
832
|
+
const e = error;
|
|
833
|
+
const statusCode = e.statusCode;
|
|
834
|
+
const message = e.message || "Unknown error";
|
|
835
|
+
const requestId = e.data?.requestId;
|
|
836
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
837
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
838
|
+
} else if (statusCode >= 500) {
|
|
839
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
throw error;
|
|
843
|
+
}
|
|
844
|
+
};
|
|
845
|
+
/**
|
|
846
|
+
* List Sandboxes
|
|
847
|
+
*
|
|
848
|
+
* @param params - Method parameters
|
|
849
|
+
* @param params.input - List options
|
|
850
|
+
* @param params.headers - Custom request headers
|
|
851
|
+
* @param params.config - Optional config override
|
|
852
|
+
* @returns List of Sandbox objects
|
|
853
|
+
*/
|
|
854
|
+
listSandboxes = async (params) => {
|
|
855
|
+
const { input, headers, config: config2 } = params;
|
|
856
|
+
try {
|
|
857
|
+
const client = this.getClient(config2);
|
|
858
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
859
|
+
const response = await client.listSandboxesWithOptions(input, headers ?? {}, runtime);
|
|
860
|
+
logger.debug(
|
|
861
|
+
`request api listSandboxes, request Request ID: ${response.body?.requestId}
|
|
862
|
+
request: ${JSON.stringify([input])}
|
|
863
|
+
response: ${JSON.stringify(response.body?.data)}`
|
|
864
|
+
);
|
|
865
|
+
return response.body?.data;
|
|
866
|
+
} catch (error) {
|
|
867
|
+
if (error && typeof error === "object" && "statusCode" in error) {
|
|
868
|
+
const e = error;
|
|
869
|
+
const statusCode = e.statusCode;
|
|
870
|
+
const message = e.message || "Unknown error";
|
|
871
|
+
const requestId = e.data?.requestId;
|
|
872
|
+
if (statusCode >= 400 && statusCode < 500) {
|
|
873
|
+
throw new ClientError(statusCode, message, { requestId });
|
|
874
|
+
} else if (statusCode >= 500) {
|
|
875
|
+
throw new ServerError(statusCode, message, { requestId });
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
throw error;
|
|
879
|
+
}
|
|
880
|
+
};
|
|
881
|
+
};
|
|
882
|
+
}
|
|
883
|
+
});
|
|
884
|
+
exports.SandboxDataAPI = void 0;
|
|
885
|
+
var init_sandbox_data = __esm({
|
|
886
|
+
"src/sandbox/api/sandbox-data.ts"() {
|
|
887
|
+
init_config();
|
|
888
|
+
init_control_api();
|
|
889
|
+
init_exception();
|
|
890
|
+
init_log();
|
|
891
|
+
exports.SandboxDataAPI = class {
|
|
892
|
+
resourceName;
|
|
893
|
+
resourceType;
|
|
894
|
+
namespace;
|
|
895
|
+
config;
|
|
896
|
+
accessToken;
|
|
897
|
+
accessTokenMap = /* @__PURE__ */ new Map();
|
|
898
|
+
constructor(params) {
|
|
899
|
+
const { config: config2, namespace = "sandboxes" } = params;
|
|
900
|
+
this.resourceName = "";
|
|
901
|
+
this.resourceType = "template" /* Template */;
|
|
902
|
+
this.namespace = namespace;
|
|
903
|
+
this.config = config2 || new Config();
|
|
904
|
+
}
|
|
905
|
+
/**
|
|
906
|
+
* Refresh access token for the resource
|
|
907
|
+
*/
|
|
908
|
+
async refreshAccessToken(params) {
|
|
909
|
+
const { sandboxId, templateName, config: config2 } = params;
|
|
910
|
+
const cfg = Config.withConfigs(config2, this.config);
|
|
911
|
+
const cacheKey = sandboxId || templateName;
|
|
912
|
+
const cachedToken = this.accessTokenMap.get(cacheKey);
|
|
913
|
+
if (sandboxId) {
|
|
914
|
+
this.resourceName = sandboxId;
|
|
915
|
+
this.resourceType = "sandbox" /* Sandbox */;
|
|
916
|
+
this.namespace = `sandboxes/${sandboxId}`;
|
|
917
|
+
} else if (templateName) {
|
|
918
|
+
this.resourceName = templateName;
|
|
919
|
+
this.resourceType = "template" /* Template */;
|
|
920
|
+
this.namespace = "sandboxes";
|
|
921
|
+
}
|
|
922
|
+
if (cachedToken) {
|
|
923
|
+
this.accessToken = cachedToken;
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
926
|
+
this.accessToken = void 0;
|
|
927
|
+
await this.auth({ config: cfg });
|
|
928
|
+
if (this.accessToken) {
|
|
929
|
+
this.accessTokenMap.set(cacheKey, this.accessToken);
|
|
930
|
+
}
|
|
931
|
+
}
|
|
932
|
+
/**
|
|
933
|
+
* Get base URL for data API
|
|
934
|
+
*/
|
|
935
|
+
getBaseUrl() {
|
|
936
|
+
return this.config.dataEndpoint;
|
|
937
|
+
}
|
|
938
|
+
/**
|
|
939
|
+
* Construct full URL with path and query parameters
|
|
940
|
+
*/
|
|
941
|
+
withPath(path2, query) {
|
|
942
|
+
path2 = path2.replace(/^\//, "");
|
|
943
|
+
const parts = [this.getBaseUrl(), this.namespace, path2].filter((p) => p && p.length > 0);
|
|
944
|
+
let url = parts.join("/").replace(/\/+/g, "/").replace(/:\//g, "://");
|
|
945
|
+
if (query && Object.keys(query).length > 0) {
|
|
946
|
+
const urlObj = new URL(url);
|
|
947
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
948
|
+
if (Array.isArray(value)) {
|
|
949
|
+
value.forEach((v) => urlObj.searchParams.append(key, String(v)));
|
|
950
|
+
} else if (value !== void 0 && value !== null) {
|
|
951
|
+
urlObj.searchParams.append(key, String(value));
|
|
952
|
+
}
|
|
953
|
+
});
|
|
954
|
+
url = urlObj.toString();
|
|
955
|
+
}
|
|
956
|
+
return url;
|
|
957
|
+
}
|
|
958
|
+
/**
|
|
959
|
+
* Authentication hook - gets access token for the resource
|
|
960
|
+
*/
|
|
961
|
+
async auth(params) {
|
|
962
|
+
const cfg = Config.withConfigs(this.config, params.config);
|
|
963
|
+
if (cfg.token) {
|
|
964
|
+
logger.debug(`Using provided access token from config: ${this.maskToken(cfg.token)}`);
|
|
965
|
+
this.accessToken = cfg.token;
|
|
966
|
+
return;
|
|
967
|
+
}
|
|
968
|
+
if (!this.accessToken && this.resourceName && this.resourceType && !cfg.token) {
|
|
969
|
+
try {
|
|
970
|
+
const controlApi = new ControlAPI(cfg);
|
|
971
|
+
const client = controlApi.getClient(cfg);
|
|
972
|
+
const request = this.resourceType === "sandbox" /* Sandbox */ ? new $AgentRun4__namespace.GetAccessTokenRequest({
|
|
973
|
+
resourceId: this.resourceName,
|
|
974
|
+
resourceType: this.resourceType
|
|
975
|
+
}) : new $AgentRun4__namespace.GetAccessTokenRequest({
|
|
976
|
+
resourceName: this.resourceName,
|
|
977
|
+
resourceType: this.resourceType
|
|
978
|
+
});
|
|
979
|
+
const runtime = new $Util__namespace.RuntimeOptions({});
|
|
980
|
+
const response = await client.getAccessTokenWithOptions(request, {}, runtime);
|
|
981
|
+
this.accessToken = response.body?.data?.accessToken;
|
|
982
|
+
logger.debug(
|
|
983
|
+
`Fetched access token for resource ${this.resourceName} of type ${this.resourceType}: ${this.maskToken(this.accessToken || "")}`
|
|
984
|
+
);
|
|
985
|
+
} catch (error) {
|
|
986
|
+
logger.warn(
|
|
987
|
+
`Failed to get access token for ${this.resourceType}(${this.resourceName}): ${error}`
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
/**
|
|
993
|
+
* Mask token for logging
|
|
994
|
+
*/
|
|
995
|
+
maskToken(token) {
|
|
996
|
+
if (!token || token.length <= 8) {
|
|
997
|
+
return "***";
|
|
998
|
+
}
|
|
999
|
+
return `${token.substring(0, 4)}...${token.substring(token.length - 4)}`;
|
|
1000
|
+
}
|
|
1001
|
+
/**
|
|
1002
|
+
* Prepare request headers
|
|
1003
|
+
*/
|
|
1004
|
+
prepareHeaders(params) {
|
|
1005
|
+
const cfg = Config.withConfigs(this.config, params.config);
|
|
1006
|
+
const reqHeaders = {
|
|
1007
|
+
"Content-Type": "application/json",
|
|
1008
|
+
"User-Agent": "AgentRunDataClient-Node/1.0",
|
|
1009
|
+
"Agentrun-Access-Token": cfg.token || this.accessToken || "",
|
|
1010
|
+
...cfg.headers,
|
|
1011
|
+
...params.headers || {}
|
|
1012
|
+
};
|
|
1013
|
+
return reqHeaders;
|
|
1014
|
+
}
|
|
1015
|
+
/**
|
|
1016
|
+
* Make an HTTP request
|
|
1017
|
+
*/
|
|
1018
|
+
async makeRequest(params) {
|
|
1019
|
+
const { method, url, data, headers, query, config: config2 } = params;
|
|
1020
|
+
const reqHeaders = this.prepareHeaders({ headers, config: config2 });
|
|
1021
|
+
let finalUrl = url;
|
|
1022
|
+
if (query && Object.keys(query).length > 0) {
|
|
1023
|
+
const urlObj = new URL(url);
|
|
1024
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
1025
|
+
if (value !== void 0 && value !== null) {
|
|
1026
|
+
if (Array.isArray(value)) {
|
|
1027
|
+
value.forEach((v) => urlObj.searchParams.append(key, String(v)));
|
|
1028
|
+
} else {
|
|
1029
|
+
urlObj.searchParams.append(key, String(value));
|
|
1030
|
+
}
|
|
1031
|
+
}
|
|
1032
|
+
});
|
|
1033
|
+
finalUrl = urlObj.toString();
|
|
1034
|
+
}
|
|
1035
|
+
logger.debug(
|
|
1036
|
+
`${method} ${finalUrl} headers=${JSON.stringify(reqHeaders)} data=${JSON.stringify(data)}`
|
|
1037
|
+
);
|
|
1038
|
+
try {
|
|
1039
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
1040
|
+
const response = await fetch(finalUrl, {
|
|
1041
|
+
method,
|
|
1042
|
+
headers: reqHeaders,
|
|
1043
|
+
body: data ? JSON.stringify(data) : void 0,
|
|
1044
|
+
signal: AbortSignal.timeout(cfg.timeout)
|
|
1045
|
+
});
|
|
1046
|
+
const responseText = await response.text();
|
|
1047
|
+
logger.debug(`Response: ${responseText}`);
|
|
1048
|
+
if (responseText) {
|
|
1049
|
+
try {
|
|
1050
|
+
return JSON.parse(responseText);
|
|
1051
|
+
} catch (error) {
|
|
1052
|
+
const errorMsg = `Failed to parse JSON response: ${error}`;
|
|
1053
|
+
logger.error(errorMsg);
|
|
1054
|
+
if (response.status === 502 && responseText.includes("502 Bad Gateway")) {
|
|
1055
|
+
throw new ClientError(response.status, "502 Bad Gateway");
|
|
1056
|
+
}
|
|
1057
|
+
throw new ClientError(response.status, errorMsg);
|
|
1058
|
+
}
|
|
1059
|
+
}
|
|
1060
|
+
return {};
|
|
1061
|
+
} catch (error) {
|
|
1062
|
+
if (error instanceof ClientError) {
|
|
1063
|
+
throw error;
|
|
1064
|
+
}
|
|
1065
|
+
const errorMsg = `Request error: ${error}`;
|
|
1066
|
+
throw new ClientError(0, errorMsg);
|
|
1067
|
+
}
|
|
1068
|
+
}
|
|
1069
|
+
/**
|
|
1070
|
+
* GET request
|
|
1071
|
+
*/
|
|
1072
|
+
async get(params) {
|
|
1073
|
+
const url = this.withPath(params.path, params.query);
|
|
1074
|
+
return this.makeRequest({
|
|
1075
|
+
method: "GET",
|
|
1076
|
+
url,
|
|
1077
|
+
headers: params.headers,
|
|
1078
|
+
config: params.config
|
|
1079
|
+
});
|
|
1080
|
+
}
|
|
1081
|
+
/**
|
|
1082
|
+
* POST request
|
|
1083
|
+
*/
|
|
1084
|
+
async post(params) {
|
|
1085
|
+
const url = this.withPath(params.path, params.query);
|
|
1086
|
+
return this.makeRequest({
|
|
1087
|
+
method: "POST",
|
|
1088
|
+
url,
|
|
1089
|
+
data: params.data,
|
|
1090
|
+
headers: params.headers,
|
|
1091
|
+
config: params.config
|
|
1092
|
+
});
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
* PUT request
|
|
1096
|
+
*/
|
|
1097
|
+
async put(params) {
|
|
1098
|
+
const url = this.withPath(params.path, params.query);
|
|
1099
|
+
return this.makeRequest({
|
|
1100
|
+
method: "PUT",
|
|
1101
|
+
url,
|
|
1102
|
+
data: params.data,
|
|
1103
|
+
headers: params.headers,
|
|
1104
|
+
config: params.config
|
|
1105
|
+
});
|
|
1106
|
+
}
|
|
1107
|
+
/**
|
|
1108
|
+
* PATCH request
|
|
1109
|
+
*/
|
|
1110
|
+
async patch(params) {
|
|
1111
|
+
const url = this.withPath(params.path, params.query);
|
|
1112
|
+
return this.makeRequest({
|
|
1113
|
+
method: "PATCH",
|
|
1114
|
+
url,
|
|
1115
|
+
data: params.data,
|
|
1116
|
+
headers: params.headers,
|
|
1117
|
+
config: params.config
|
|
1118
|
+
});
|
|
1119
|
+
}
|
|
1120
|
+
/**
|
|
1121
|
+
* DELETE request
|
|
1122
|
+
*/
|
|
1123
|
+
async delete(params) {
|
|
1124
|
+
const url = this.withPath(params.path, params.query);
|
|
1125
|
+
return this.makeRequest({
|
|
1126
|
+
method: "DELETE",
|
|
1127
|
+
url,
|
|
1128
|
+
headers: params.headers,
|
|
1129
|
+
config: params.config
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
/**
|
|
1133
|
+
* Check sandbox health
|
|
1134
|
+
*/
|
|
1135
|
+
checkHealth = async (params) => {
|
|
1136
|
+
await this.refreshAccessToken({
|
|
1137
|
+
sandboxId: params.sandboxId,
|
|
1138
|
+
config: params.config
|
|
1139
|
+
});
|
|
1140
|
+
return this.get({ path: "/health", config: params.config });
|
|
1141
|
+
};
|
|
1142
|
+
/**
|
|
1143
|
+
* Create sandbox from template
|
|
1144
|
+
* 从模板创建沙箱 / Create Sandbox from Template
|
|
1145
|
+
*/
|
|
1146
|
+
createSandbox = async (params) => {
|
|
1147
|
+
await this.refreshAccessToken({
|
|
1148
|
+
templateName: params.templateName,
|
|
1149
|
+
config: params.config
|
|
1150
|
+
});
|
|
1151
|
+
const data = {
|
|
1152
|
+
templateName: params.templateName,
|
|
1153
|
+
sandboxIdleTimeoutSeconds: params.sandboxIdleTimeoutSeconds || 600
|
|
1154
|
+
};
|
|
1155
|
+
if (params.nasConfig !== void 0) {
|
|
1156
|
+
data.nasConfig = params.nasConfig;
|
|
1157
|
+
}
|
|
1158
|
+
if (params.ossMountConfig !== void 0) {
|
|
1159
|
+
data.ossMountConfig = params.ossMountConfig;
|
|
1160
|
+
}
|
|
1161
|
+
if (params.polarFsConfig !== void 0) {
|
|
1162
|
+
data.polarFsConfig = params.polarFsConfig;
|
|
1163
|
+
}
|
|
1164
|
+
return this.post({
|
|
1165
|
+
path: "/",
|
|
1166
|
+
data
|
|
1167
|
+
});
|
|
1168
|
+
};
|
|
1169
|
+
/**
|
|
1170
|
+
* Delete sandbox
|
|
1171
|
+
*/
|
|
1172
|
+
deleteSandbox = async (params) => {
|
|
1173
|
+
await this.refreshAccessToken({
|
|
1174
|
+
sandboxId: params.sandboxId,
|
|
1175
|
+
config: params.config
|
|
1176
|
+
});
|
|
1177
|
+
return this.delete({ path: "/" });
|
|
1178
|
+
};
|
|
1179
|
+
/**
|
|
1180
|
+
* Stop sandbox
|
|
1181
|
+
*/
|
|
1182
|
+
stopSandbox = async (params) => {
|
|
1183
|
+
await this.refreshAccessToken({
|
|
1184
|
+
sandboxId: params.sandboxId,
|
|
1185
|
+
config: params.config
|
|
1186
|
+
});
|
|
1187
|
+
return this.post({ path: "/stop" });
|
|
1188
|
+
};
|
|
1189
|
+
/**
|
|
1190
|
+
* Get sandbox info
|
|
1191
|
+
*/
|
|
1192
|
+
getSandbox = async (params) => {
|
|
1193
|
+
await this.refreshAccessToken({
|
|
1194
|
+
sandboxId: params.sandboxId,
|
|
1195
|
+
config: params.config
|
|
1196
|
+
});
|
|
1197
|
+
return this.get({ path: "/" });
|
|
1198
|
+
};
|
|
1199
|
+
};
|
|
1200
|
+
}
|
|
1201
|
+
});
|
|
1202
|
+
exports.BrowserDataAPI = void 0;
|
|
1203
|
+
var init_browser_data = __esm({
|
|
1204
|
+
"src/sandbox/api/browser-data.ts"() {
|
|
1205
|
+
init_config();
|
|
1206
|
+
init_exception();
|
|
1207
|
+
init_log();
|
|
1208
|
+
init_sandbox_data();
|
|
1209
|
+
exports.BrowserDataAPI = class extends exports.SandboxDataAPI {
|
|
1210
|
+
sandboxId;
|
|
1211
|
+
constructor(params) {
|
|
1212
|
+
super({
|
|
1213
|
+
sandboxId: params.sandboxId,
|
|
1214
|
+
config: params.config,
|
|
1215
|
+
// Set namespace to include sandboxId for CDP/VNC URL generation
|
|
1216
|
+
namespace: `sandboxes/${params.sandboxId}`
|
|
1217
|
+
});
|
|
1218
|
+
this.sandboxId = params.sandboxId;
|
|
1219
|
+
}
|
|
1220
|
+
/**
|
|
1221
|
+
* Generate the WebSocket URL for Chrome DevTools Protocol (CDP) connection
|
|
1222
|
+
*/
|
|
1223
|
+
getCdpUrl(record) {
|
|
1224
|
+
const cdpUrl = this.withPath("/ws/automation").replace("http", "ws");
|
|
1225
|
+
const url = new URL(cdpUrl);
|
|
1226
|
+
url.searchParams.set("tenantId", this.config.accountId);
|
|
1227
|
+
if (record) {
|
|
1228
|
+
url.searchParams.set("recording", "true");
|
|
1229
|
+
}
|
|
1230
|
+
return url.toString();
|
|
1231
|
+
}
|
|
1232
|
+
/**
|
|
1233
|
+
* Generate the WebSocket URL for VNC (Virtual Network Computing) live view connection
|
|
1234
|
+
*/
|
|
1235
|
+
getVncUrl(record) {
|
|
1236
|
+
const vncUrl = this.withPath("/ws/liveview").replace("http", "ws");
|
|
1237
|
+
const url = new URL(vncUrl);
|
|
1238
|
+
url.searchParams.set("tenantId", this.config.accountId);
|
|
1239
|
+
if (record) {
|
|
1240
|
+
url.searchParams.set("recording", "true");
|
|
1241
|
+
}
|
|
1242
|
+
return url.toString();
|
|
1243
|
+
}
|
|
1244
|
+
/**
|
|
1245
|
+
* List all recordings
|
|
1246
|
+
*/
|
|
1247
|
+
listRecordings = async (params) => {
|
|
1248
|
+
return this.get({ path: "/recordings", config: params?.config });
|
|
1249
|
+
};
|
|
1250
|
+
/**
|
|
1251
|
+
* Delete a recording
|
|
1252
|
+
*/
|
|
1253
|
+
deleteRecording = async (params) => {
|
|
1254
|
+
return this.delete({ path: `/recordings/${params.filename}`, config: params.config });
|
|
1255
|
+
};
|
|
1256
|
+
/**
|
|
1257
|
+
* Download a recording video file
|
|
1258
|
+
*/
|
|
1259
|
+
downloadRecording = async (params) => {
|
|
1260
|
+
return this.getVideo({
|
|
1261
|
+
path: `/recordings/${params.filename}`,
|
|
1262
|
+
savePath: params.savePath
|
|
1263
|
+
});
|
|
1264
|
+
};
|
|
1265
|
+
/**
|
|
1266
|
+
* Helper method to download video file
|
|
1267
|
+
*/
|
|
1268
|
+
async getVideo(params) {
|
|
1269
|
+
const url = this.withPath(params.path, params.query);
|
|
1270
|
+
const cfg = Config.withConfigs(this.config, params.config);
|
|
1271
|
+
const reqHeaders = this.prepareHeaders({ headers: params.headers, config: cfg });
|
|
1272
|
+
try {
|
|
1273
|
+
const response = await fetch(url, {
|
|
1274
|
+
method: "GET",
|
|
1275
|
+
headers: reqHeaders,
|
|
1276
|
+
signal: AbortSignal.timeout(cfg.timeout)
|
|
1277
|
+
});
|
|
1278
|
+
if (!response.ok) {
|
|
1279
|
+
const errorText = await response.text();
|
|
1280
|
+
throw new ClientError(response.status, errorText);
|
|
1281
|
+
}
|
|
1282
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
1283
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
1284
|
+
await fs2__namespace.promises.writeFile(params.savePath, buffer);
|
|
1285
|
+
return {
|
|
1286
|
+
savedPath: params.savePath,
|
|
1287
|
+
size: buffer.length
|
|
1288
|
+
};
|
|
1289
|
+
} catch (error) {
|
|
1290
|
+
if (error instanceof ClientError) {
|
|
1291
|
+
throw error;
|
|
1292
|
+
}
|
|
1293
|
+
logger.error(`Download video error: ${error}`);
|
|
1294
|
+
throw new ClientError(0, `Download video error: ${error}`);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1298
|
+
}
|
|
1299
|
+
});
|
|
1300
|
+
|
|
1301
|
+
// src/utils/model.ts
|
|
1302
|
+
function isFinalStatus(status) {
|
|
1303
|
+
return [
|
|
1304
|
+
void 0,
|
|
1305
|
+
null,
|
|
1306
|
+
"",
|
|
1307
|
+
...["READY", "CREATE_FAILED", "UPDATE_FAILED", "DELETE_FAILED"]
|
|
1308
|
+
].includes(status);
|
|
1309
|
+
}
|
|
1310
|
+
var Status;
|
|
1311
|
+
var init_model = __esm({
|
|
1312
|
+
"src/utils/model.ts"() {
|
|
1313
|
+
Status = {
|
|
1314
|
+
CREATING: "CREATING",
|
|
1315
|
+
CREATE_FAILED: "CREATE_FAILED",
|
|
1316
|
+
READY: "READY",
|
|
1317
|
+
UPDATING: "UPDATING",
|
|
1318
|
+
UPDATE_FAILED: "UPDATE_FAILED",
|
|
1319
|
+
DELETING: "DELETING",
|
|
1320
|
+
DELETE_FAILED: "DELETE_FAILED"
|
|
1321
|
+
};
|
|
1322
|
+
}
|
|
1323
|
+
});
|
|
1324
|
+
|
|
1325
|
+
// src/sandbox/model.ts
|
|
1326
|
+
exports.TemplateType = void 0; exports.SandboxState = void 0; exports.TemplateNetworkMode = void 0; exports.TemplateOSSPermission = void 0; exports.CodeLanguage = void 0;
|
|
1327
|
+
var init_model2 = __esm({
|
|
1328
|
+
"src/sandbox/model.ts"() {
|
|
1329
|
+
init_model();
|
|
1330
|
+
exports.TemplateType = /* @__PURE__ */ ((TemplateType2) => {
|
|
1331
|
+
TemplateType2["CODE_INTERPRETER"] = "CodeInterpreter";
|
|
1332
|
+
TemplateType2["BROWSER"] = "Browser";
|
|
1333
|
+
TemplateType2["AIO"] = "AllInOne";
|
|
1334
|
+
TemplateType2["CUSTOM"] = "CustomImage";
|
|
1335
|
+
return TemplateType2;
|
|
1336
|
+
})(exports.TemplateType || {});
|
|
1337
|
+
exports.SandboxState = /* @__PURE__ */ ((SandboxState3) => {
|
|
1338
|
+
SandboxState3["CREATING"] = "Creating";
|
|
1339
|
+
SandboxState3["RUNNING"] = "Running";
|
|
1340
|
+
SandboxState3["READY"] = "READY";
|
|
1341
|
+
SandboxState3["STOPPED"] = "Stopped";
|
|
1342
|
+
SandboxState3["FAILED"] = "Failed";
|
|
1343
|
+
SandboxState3["DELETING"] = "Deleting";
|
|
1344
|
+
return SandboxState3;
|
|
1345
|
+
})(exports.SandboxState || {});
|
|
1346
|
+
exports.TemplateNetworkMode = /* @__PURE__ */ ((TemplateNetworkMode2) => {
|
|
1347
|
+
TemplateNetworkMode2["PUBLIC"] = "PUBLIC";
|
|
1348
|
+
TemplateNetworkMode2["PRIVATE"] = "PRIVATE";
|
|
1349
|
+
TemplateNetworkMode2["PUBLIC_AND_PRIVATE"] = "PUBLIC_AND_PRIVATE";
|
|
1350
|
+
return TemplateNetworkMode2;
|
|
1351
|
+
})(exports.TemplateNetworkMode || {});
|
|
1352
|
+
exports.TemplateOSSPermission = /* @__PURE__ */ ((TemplateOSSPermission2) => {
|
|
1353
|
+
TemplateOSSPermission2["READ_WRITE"] = "READ_WRITE";
|
|
1354
|
+
TemplateOSSPermission2["READ_ONLY"] = "READ_ONLY";
|
|
1355
|
+
return TemplateOSSPermission2;
|
|
1356
|
+
})(exports.TemplateOSSPermission || {});
|
|
1357
|
+
exports.CodeLanguage = /* @__PURE__ */ ((CodeLanguage2) => {
|
|
1358
|
+
CodeLanguage2["PYTHON"] = "python";
|
|
1359
|
+
CodeLanguage2["JAVASCRIPT"] = "javascript";
|
|
1360
|
+
return CodeLanguage2;
|
|
1361
|
+
})(exports.CodeLanguage || {});
|
|
1362
|
+
}
|
|
1363
|
+
});
|
|
1364
|
+
exports.CodeInterpreterDataAPI = void 0;
|
|
1365
|
+
var init_code_interpreter_data = __esm({
|
|
1366
|
+
"src/sandbox/api/code-interpreter-data.ts"() {
|
|
1367
|
+
init_config();
|
|
1368
|
+
init_exception();
|
|
1369
|
+
init_log();
|
|
1370
|
+
init_model2();
|
|
1371
|
+
init_sandbox_data();
|
|
1372
|
+
exports.CodeInterpreterDataAPI = class extends exports.SandboxDataAPI {
|
|
1373
|
+
constructor(params) {
|
|
1374
|
+
super({
|
|
1375
|
+
sandboxId: params.sandboxId,
|
|
1376
|
+
config: params.config,
|
|
1377
|
+
// Set namespace to include sandboxId for correct API path generation
|
|
1378
|
+
namespace: `sandboxes/${params.sandboxId}`
|
|
1379
|
+
});
|
|
1380
|
+
}
|
|
1381
|
+
/**
|
|
1382
|
+
* List directory contents
|
|
1383
|
+
*/
|
|
1384
|
+
async listDirectory(params) {
|
|
1385
|
+
const query = {};
|
|
1386
|
+
if (params?.path !== void 0) {
|
|
1387
|
+
query.path = params.path;
|
|
1388
|
+
}
|
|
1389
|
+
if (params?.depth !== void 0) {
|
|
1390
|
+
query.depth = params.depth;
|
|
1391
|
+
}
|
|
1392
|
+
return this.get({ path: "/filesystem", query, config: params?.config });
|
|
1393
|
+
}
|
|
1394
|
+
/**
|
|
1395
|
+
* Get file/directory stats
|
|
1396
|
+
*/
|
|
1397
|
+
async stat(params) {
|
|
1398
|
+
const query = {
|
|
1399
|
+
path: params.path
|
|
1400
|
+
};
|
|
1401
|
+
return this.get({ path: "/filesystem/stat", query, config: params.config });
|
|
1402
|
+
}
|
|
1403
|
+
/**
|
|
1404
|
+
* Create directory
|
|
1405
|
+
*/
|
|
1406
|
+
async mkdir(params) {
|
|
1407
|
+
const data = {
|
|
1408
|
+
path: params.path,
|
|
1409
|
+
parents: params.parents !== void 0 ? params.parents : true,
|
|
1410
|
+
mode: params.mode || "0755"
|
|
1411
|
+
};
|
|
1412
|
+
return this.post({ path: "/filesystem/mkdir", data, config: params.config });
|
|
1413
|
+
}
|
|
1414
|
+
/**
|
|
1415
|
+
* Move file or directory
|
|
1416
|
+
*/
|
|
1417
|
+
async moveFile(params) {
|
|
1418
|
+
const data = {
|
|
1419
|
+
source: params.source,
|
|
1420
|
+
destination: params.destination
|
|
1421
|
+
};
|
|
1422
|
+
return this.post({ path: "/filesystem/move", data, config: params.config });
|
|
1423
|
+
}
|
|
1424
|
+
/**
|
|
1425
|
+
* Remove file or directory
|
|
1426
|
+
*/
|
|
1427
|
+
async removeFile(params) {
|
|
1428
|
+
const data = {
|
|
1429
|
+
path: params.path
|
|
1430
|
+
};
|
|
1431
|
+
return this.post({ path: "/filesystem/remove", data, config: params.config });
|
|
1432
|
+
}
|
|
1433
|
+
/**
|
|
1434
|
+
* List code execution contexts
|
|
1435
|
+
*/
|
|
1436
|
+
async listContexts(params) {
|
|
1437
|
+
return this.get({ path: "/contexts", config: params?.config });
|
|
1438
|
+
}
|
|
1439
|
+
/**
|
|
1440
|
+
* Create a new code execution context
|
|
1441
|
+
*/
|
|
1442
|
+
async createContext(params) {
|
|
1443
|
+
const language = params?.language || "python" /* PYTHON */;
|
|
1444
|
+
const cwd = params?.cwd || "/home/user";
|
|
1445
|
+
if (language !== "python" && language !== "javascript") {
|
|
1446
|
+
throw new Error(`language must be 'python' or 'javascript', got: ${language}`);
|
|
1447
|
+
}
|
|
1448
|
+
const data = {
|
|
1449
|
+
cwd,
|
|
1450
|
+
language
|
|
1451
|
+
};
|
|
1452
|
+
return this.post({ path: "/contexts", data, config: params?.config });
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* Get context details
|
|
1456
|
+
*/
|
|
1457
|
+
async getContext(params) {
|
|
1458
|
+
return this.get({ path: `/contexts/${params.contextId}`, config: params.config });
|
|
1459
|
+
}
|
|
1460
|
+
/**
|
|
1461
|
+
* Execute code in a context
|
|
1462
|
+
*/
|
|
1463
|
+
async executeCode(params) {
|
|
1464
|
+
const { code, contextId, language, timeout = 30, config: config2 } = params;
|
|
1465
|
+
if (language && language !== "python" && language !== "javascript") {
|
|
1466
|
+
throw new Error(`language must be 'python' or 'javascript', got: ${language}`);
|
|
1467
|
+
}
|
|
1468
|
+
const data = {
|
|
1469
|
+
code
|
|
1470
|
+
};
|
|
1471
|
+
if (timeout !== void 0) {
|
|
1472
|
+
data.timeout = timeout;
|
|
1473
|
+
}
|
|
1474
|
+
if (language !== void 0) {
|
|
1475
|
+
data.language = language;
|
|
1476
|
+
}
|
|
1477
|
+
if (contextId !== void 0) {
|
|
1478
|
+
data.contextId = contextId;
|
|
1479
|
+
}
|
|
1480
|
+
return this.post({ path: "/contexts/execute", data, config: config2 });
|
|
1481
|
+
}
|
|
1482
|
+
/**
|
|
1483
|
+
* Delete a context
|
|
1484
|
+
*/
|
|
1485
|
+
async deleteContext(params) {
|
|
1486
|
+
return this.delete({ path: `/contexts/${params.contextId}`, config: params.config });
|
|
1487
|
+
}
|
|
1488
|
+
/**
|
|
1489
|
+
* Read file contents
|
|
1490
|
+
*/
|
|
1491
|
+
async readFile(params) {
|
|
1492
|
+
const query = {
|
|
1493
|
+
path: params.path
|
|
1494
|
+
};
|
|
1495
|
+
return this.get({ path: "/files", query, config: params.config });
|
|
1496
|
+
}
|
|
1497
|
+
/**
|
|
1498
|
+
* Write file contents
|
|
1499
|
+
*/
|
|
1500
|
+
async writeFile(params) {
|
|
1501
|
+
const data = {
|
|
1502
|
+
path: params.path,
|
|
1503
|
+
content: params.content,
|
|
1504
|
+
mode: params.mode || "644",
|
|
1505
|
+
encoding: params.encoding || "utf-8",
|
|
1506
|
+
createDir: params.createDir !== void 0 ? params.createDir : true
|
|
1507
|
+
};
|
|
1508
|
+
return this.post({ path: "/files", data, config: params.config });
|
|
1509
|
+
}
|
|
1510
|
+
/**
|
|
1511
|
+
* Upload file to sandbox
|
|
1512
|
+
*/
|
|
1513
|
+
async uploadFile(params) {
|
|
1514
|
+
return this.postFile({
|
|
1515
|
+
path: "/filesystem/upload",
|
|
1516
|
+
localFilePath: params.localFilePath,
|
|
1517
|
+
targetFilePath: params.targetFilePath,
|
|
1518
|
+
config: params.config
|
|
1519
|
+
});
|
|
1520
|
+
}
|
|
1521
|
+
/**
|
|
1522
|
+
* Download file from sandbox
|
|
1523
|
+
*/
|
|
1524
|
+
async downloadFile(params) {
|
|
1525
|
+
const query = { path: params.path };
|
|
1526
|
+
return this.getFile({
|
|
1527
|
+
path: "/filesystem/download",
|
|
1528
|
+
savePath: params.savePath,
|
|
1529
|
+
query,
|
|
1530
|
+
config: params.config
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
/**
|
|
1534
|
+
* Execute shell command
|
|
1535
|
+
*/
|
|
1536
|
+
async cmd(params) {
|
|
1537
|
+
const data = {
|
|
1538
|
+
command: params.command,
|
|
1539
|
+
cwd: params.cwd
|
|
1540
|
+
};
|
|
1541
|
+
if (params.timeout !== void 0) {
|
|
1542
|
+
data.timeout = params.timeout;
|
|
1543
|
+
}
|
|
1544
|
+
return this.post({ path: "/processes/cmd", data, config: params.config });
|
|
1545
|
+
}
|
|
1546
|
+
/**
|
|
1547
|
+
* List running processes
|
|
1548
|
+
*/
|
|
1549
|
+
async listProcesses(params) {
|
|
1550
|
+
return this.get({ path: "/processes", config: params?.config });
|
|
1551
|
+
}
|
|
1552
|
+
/**
|
|
1553
|
+
* Get process details
|
|
1554
|
+
*/
|
|
1555
|
+
async getProcess(params) {
|
|
1556
|
+
return this.get({ path: `/processes/${params.pid}`, config: params.config });
|
|
1557
|
+
}
|
|
1558
|
+
/**
|
|
1559
|
+
* Kill a process
|
|
1560
|
+
*/
|
|
1561
|
+
async killProcess(params) {
|
|
1562
|
+
return this.delete({ path: `/processes/${params.pid}`, config: params.config });
|
|
1563
|
+
}
|
|
1564
|
+
/**
|
|
1565
|
+
* Helper method to upload file using multipart/form-data
|
|
1566
|
+
*/
|
|
1567
|
+
async postFile(params) {
|
|
1568
|
+
const filename = path__namespace.basename(params.localFilePath);
|
|
1569
|
+
const url = this.withPath(params.path, params.query);
|
|
1570
|
+
const reqHeaders = this.prepareHeaders({ headers: params.headers, config: params.config });
|
|
1571
|
+
delete reqHeaders["Content-Type"];
|
|
1572
|
+
try {
|
|
1573
|
+
const fileContent = await fs2__namespace.promises.readFile(params.localFilePath);
|
|
1574
|
+
const formData = new FormData();
|
|
1575
|
+
formData.append("file", new Blob([fileContent]), filename);
|
|
1576
|
+
const data = params.formData || {};
|
|
1577
|
+
data.path = params.targetFilePath;
|
|
1578
|
+
Object.entries(data).forEach(([key, value]) => {
|
|
1579
|
+
formData.append(key, String(value));
|
|
1580
|
+
});
|
|
1581
|
+
const cfg = Config.withConfigs(this.config, params.config);
|
|
1582
|
+
const response = await fetch(url, {
|
|
1583
|
+
method: "POST",
|
|
1584
|
+
headers: reqHeaders,
|
|
1585
|
+
body: formData,
|
|
1586
|
+
signal: AbortSignal.timeout(cfg.timeout)
|
|
1587
|
+
});
|
|
1588
|
+
if (!response.ok) {
|
|
1589
|
+
const errorText = await response.text();
|
|
1590
|
+
throw new ClientError(response.status, errorText);
|
|
1591
|
+
}
|
|
1592
|
+
return response.json();
|
|
1593
|
+
} catch (error) {
|
|
1594
|
+
if (error instanceof ClientError) {
|
|
1595
|
+
throw error;
|
|
1596
|
+
}
|
|
1597
|
+
logger.error(`Upload file error: ${error}`);
|
|
1598
|
+
throw new ClientError(0, `Upload file error: ${error}`);
|
|
1599
|
+
}
|
|
1600
|
+
}
|
|
1601
|
+
/**
|
|
1602
|
+
* Helper method to download file
|
|
1603
|
+
*/
|
|
1604
|
+
async getFile(params) {
|
|
1605
|
+
const url = this.withPath(params.path, params.query);
|
|
1606
|
+
const reqHeaders = this.prepareHeaders({ headers: params.headers, config: params.config });
|
|
1607
|
+
try {
|
|
1608
|
+
const cfg = Config.withConfigs(this.config, params.config);
|
|
1609
|
+
const response = await fetch(url, {
|
|
1610
|
+
method: "GET",
|
|
1611
|
+
headers: reqHeaders,
|
|
1612
|
+
signal: AbortSignal.timeout(cfg.timeout)
|
|
1613
|
+
});
|
|
1614
|
+
if (!response.ok) {
|
|
1615
|
+
const errorText = await response.text();
|
|
1616
|
+
throw new ClientError(response.status, errorText);
|
|
1617
|
+
}
|
|
1618
|
+
const arrayBuffer = await response.arrayBuffer();
|
|
1619
|
+
const buffer = Buffer.from(arrayBuffer);
|
|
1620
|
+
await fs2__namespace.promises.writeFile(params.savePath, buffer);
|
|
1621
|
+
return {
|
|
1622
|
+
savedPath: params.savePath,
|
|
1623
|
+
size: buffer.length
|
|
1624
|
+
};
|
|
1625
|
+
} catch (error) {
|
|
1626
|
+
if (error instanceof ClientError) {
|
|
1627
|
+
throw error;
|
|
1628
|
+
}
|
|
1629
|
+
logger.error(`Download file error: ${error}`);
|
|
1630
|
+
throw new ClientError(0, `Download file error: ${error}`);
|
|
1631
|
+
}
|
|
1632
|
+
}
|
|
1633
|
+
};
|
|
1634
|
+
}
|
|
1635
|
+
});
|
|
1636
|
+
|
|
1637
|
+
// src/sandbox/api/aio-data.ts
|
|
1638
|
+
exports.AioDataAPI = void 0;
|
|
1639
|
+
var init_aio_data = __esm({
|
|
1640
|
+
"src/sandbox/api/aio-data.ts"() {
|
|
1641
|
+
init_browser_data();
|
|
1642
|
+
init_code_interpreter_data();
|
|
1643
|
+
exports.AioDataAPI = class extends exports.CodeInterpreterDataAPI {
|
|
1644
|
+
browserAPI;
|
|
1645
|
+
constructor(params) {
|
|
1646
|
+
super({
|
|
1647
|
+
sandboxId: params.sandboxId,
|
|
1648
|
+
config: params.config
|
|
1649
|
+
});
|
|
1650
|
+
this.browserAPI = new exports.BrowserDataAPI({
|
|
1651
|
+
sandboxId: params.sandboxId,
|
|
1652
|
+
config: params.config
|
|
1653
|
+
});
|
|
1654
|
+
}
|
|
1655
|
+
// Browser API methods - delegate to browserAPI
|
|
1656
|
+
/**
|
|
1657
|
+
* Get CDP WebSocket URL for browser automation
|
|
1658
|
+
*/
|
|
1659
|
+
getCdpUrl(record) {
|
|
1660
|
+
return this.browserAPI.getCdpUrl(record);
|
|
1661
|
+
}
|
|
1662
|
+
/**
|
|
1663
|
+
* Get VNC WebSocket URL for live view
|
|
1664
|
+
*/
|
|
1665
|
+
getVncUrl(record) {
|
|
1666
|
+
return this.browserAPI.getVncUrl(record);
|
|
1667
|
+
}
|
|
1668
|
+
/**
|
|
1669
|
+
* List browser recordings
|
|
1670
|
+
*/
|
|
1671
|
+
listRecordings = async (params) => {
|
|
1672
|
+
return this.browserAPI.listRecordings(params);
|
|
1673
|
+
};
|
|
1674
|
+
/**
|
|
1675
|
+
* Delete a recording
|
|
1676
|
+
*/
|
|
1677
|
+
deleteRecording = async (params) => {
|
|
1678
|
+
return this.browserAPI.deleteRecording(params);
|
|
1679
|
+
};
|
|
1680
|
+
/**
|
|
1681
|
+
* Download a recording
|
|
1682
|
+
*/
|
|
1683
|
+
downloadRecording = async (params) => {
|
|
1684
|
+
return this.browserAPI.downloadRecording(params);
|
|
1685
|
+
};
|
|
1686
|
+
};
|
|
1687
|
+
}
|
|
1688
|
+
});
|
|
1689
|
+
|
|
1690
|
+
// src/utils/data-api.ts
|
|
1691
|
+
var init_data_api = __esm({
|
|
1692
|
+
"src/utils/data-api.ts"() {
|
|
1693
|
+
init_config();
|
|
1694
|
+
init_exception();
|
|
1695
|
+
init_log();
|
|
1696
|
+
}
|
|
1697
|
+
});
|
|
1698
|
+
|
|
1699
|
+
// src/utils/index.ts
|
|
1700
|
+
var init_utils = __esm({
|
|
1701
|
+
"src/utils/index.ts"() {
|
|
1702
|
+
init_version_check();
|
|
1703
|
+
init_config();
|
|
1704
|
+
init_data_api();
|
|
1705
|
+
init_exception();
|
|
1706
|
+
init_log();
|
|
1707
|
+
init_model();
|
|
1708
|
+
}
|
|
1709
|
+
});
|
|
1710
|
+
|
|
1711
|
+
// src/utils/resource.ts
|
|
1712
|
+
function updateObjectProperties(target, source) {
|
|
1713
|
+
for (const key in source) {
|
|
1714
|
+
if (Object.prototype.hasOwnProperty.call(source, key) && typeof source[key] !== "function" && !key.startsWith("_")) {
|
|
1715
|
+
target[key] = source[key];
|
|
1716
|
+
}
|
|
1717
|
+
}
|
|
1718
|
+
}
|
|
1719
|
+
function listAllResourcesFunction(list) {
|
|
1720
|
+
return async (params) => {
|
|
1721
|
+
const { config: config2, ...restParams } = params ?? {};
|
|
1722
|
+
const allResults = [];
|
|
1723
|
+
let page = 1;
|
|
1724
|
+
const pageSize = 50;
|
|
1725
|
+
while (true) {
|
|
1726
|
+
const pageResults = await list({
|
|
1727
|
+
input: {
|
|
1728
|
+
...restParams,
|
|
1729
|
+
pageNumber: page,
|
|
1730
|
+
pageSize
|
|
1731
|
+
},
|
|
1732
|
+
config: config2
|
|
1733
|
+
});
|
|
1734
|
+
page += 1;
|
|
1735
|
+
allResults.push(...pageResults);
|
|
1736
|
+
if (pageResults.length < pageSize) break;
|
|
1737
|
+
}
|
|
1738
|
+
const resultSet = /* @__PURE__ */ new Set();
|
|
1739
|
+
const results = [];
|
|
1740
|
+
for (const item of allResults) {
|
|
1741
|
+
const uniqId = item.uniqIdCallback() || "";
|
|
1742
|
+
if (!resultSet.has(uniqId) && uniqId) {
|
|
1743
|
+
resultSet.add(uniqId);
|
|
1744
|
+
results.push(item);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
return results;
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
var ResourceBase;
|
|
1751
|
+
var init_resource = __esm({
|
|
1752
|
+
"src/utils/resource.ts"() {
|
|
1753
|
+
init_exception();
|
|
1754
|
+
init_log();
|
|
1755
|
+
init_model();
|
|
1756
|
+
ResourceBase = class {
|
|
1757
|
+
status;
|
|
1758
|
+
_config;
|
|
1759
|
+
// static async list(params?: {
|
|
1760
|
+
// input?: PageableInput;
|
|
1761
|
+
// config?: Config;
|
|
1762
|
+
// }): Promise<WithUniqIdCallback[]> {
|
|
1763
|
+
// return [];
|
|
1764
|
+
// }
|
|
1765
|
+
waitUntil = async (params) => {
|
|
1766
|
+
const { checkFinishedCallback, intervalSeconds = 5, timeoutSeconds = 300 } = params;
|
|
1767
|
+
const startTime = Date.now();
|
|
1768
|
+
while (true) {
|
|
1769
|
+
if (await checkFinishedCallback(this)) return this;
|
|
1770
|
+
if (Date.now() - startTime >= timeoutSeconds * 1e3) {
|
|
1771
|
+
throw new Error("Timeout waiting for resource to reach desired state");
|
|
1772
|
+
}
|
|
1773
|
+
await new Promise((resolve) => setTimeout(resolve, intervalSeconds * 1e3));
|
|
1774
|
+
}
|
|
1775
|
+
};
|
|
1776
|
+
waitUntilReadyOrFailed = async (params) => {
|
|
1777
|
+
const { callback, intervalSeconds = 5, timeoutSeconds = 300 } = params ?? {};
|
|
1778
|
+
async function checkFinishedCallback(resource) {
|
|
1779
|
+
await resource.refresh();
|
|
1780
|
+
if (callback) await callback(resource);
|
|
1781
|
+
logger.debug(`Resource status: ${resource.status}`);
|
|
1782
|
+
return isFinalStatus(resource.status);
|
|
1783
|
+
}
|
|
1784
|
+
return await this.waitUntil({
|
|
1785
|
+
checkFinishedCallback,
|
|
1786
|
+
intervalSeconds,
|
|
1787
|
+
timeoutSeconds
|
|
1788
|
+
});
|
|
1789
|
+
};
|
|
1790
|
+
deleteAndWaitUntilFinished = async (params) => {
|
|
1791
|
+
const { callback, intervalSeconds = 5, timeoutSeconds = 300 } = params ?? {};
|
|
1792
|
+
try {
|
|
1793
|
+
await this.delete();
|
|
1794
|
+
} catch (error) {
|
|
1795
|
+
if (error instanceof ResourceNotExistError) return;
|
|
1796
|
+
}
|
|
1797
|
+
async function checkFinishedCallback(resource) {
|
|
1798
|
+
try {
|
|
1799
|
+
await resource.refresh();
|
|
1800
|
+
if (callback) await callback(resource);
|
|
1801
|
+
} catch (error) {
|
|
1802
|
+
if (error instanceof ResourceNotExistError) return true;
|
|
1803
|
+
if (resource.status === Status.DELETING) return false;
|
|
1804
|
+
throw Error(`Resource status is ${resource.status}`);
|
|
1805
|
+
}
|
|
1806
|
+
return false;
|
|
1807
|
+
}
|
|
1808
|
+
return await this.waitUntil({
|
|
1809
|
+
checkFinishedCallback,
|
|
1810
|
+
intervalSeconds,
|
|
1811
|
+
timeoutSeconds
|
|
1812
|
+
});
|
|
1813
|
+
};
|
|
1814
|
+
refresh = async (params) => await this.get(params);
|
|
1815
|
+
/**
|
|
1816
|
+
* 更新实例自身的属性 / Update instance properties
|
|
1817
|
+
*
|
|
1818
|
+
* @param source - 源对象 / Source object
|
|
1819
|
+
*/
|
|
1820
|
+
updateSelf(source) {
|
|
1821
|
+
updateObjectProperties(this, source);
|
|
1822
|
+
}
|
|
1823
|
+
setConfig = (config2) => {
|
|
1824
|
+
this._config = config2;
|
|
1825
|
+
return this;
|
|
1826
|
+
};
|
|
1827
|
+
};
|
|
1828
|
+
}
|
|
1829
|
+
});
|
|
1830
|
+
|
|
1831
|
+
// src/sandbox/code-interpreter-sandbox.ts
|
|
1832
|
+
var code_interpreter_sandbox_exports = {};
|
|
1833
|
+
__export(code_interpreter_sandbox_exports, {
|
|
1834
|
+
CodeInterpreterSandbox: () => exports.CodeInterpreterSandbox,
|
|
1835
|
+
ContextOperations: () => ContextOperations,
|
|
1836
|
+
FileOperations: () => FileOperations,
|
|
1837
|
+
FileSystemOperations: () => FileSystemOperations,
|
|
1838
|
+
ProcessOperations: () => ProcessOperations
|
|
1839
|
+
});
|
|
1840
|
+
var FileOperations, FileSystemOperations, ProcessOperations, ContextOperations; exports.CodeInterpreterSandbox = void 0;
|
|
1841
|
+
var init_code_interpreter_sandbox = __esm({
|
|
1842
|
+
"src/sandbox/code-interpreter-sandbox.ts"() {
|
|
1843
|
+
init_log();
|
|
1844
|
+
init_exception();
|
|
1845
|
+
init_code_interpreter_data();
|
|
1846
|
+
init_model2();
|
|
1847
|
+
init_sandbox();
|
|
1848
|
+
FileOperations = class {
|
|
1849
|
+
sandbox;
|
|
1850
|
+
constructor(sandbox) {
|
|
1851
|
+
this.sandbox = sandbox;
|
|
1852
|
+
}
|
|
1853
|
+
/**
|
|
1854
|
+
* Read a file from the code interpreter
|
|
1855
|
+
*/
|
|
1856
|
+
read = async (params) => {
|
|
1857
|
+
return this.sandbox.dataApi.readFile(params);
|
|
1858
|
+
};
|
|
1859
|
+
/**
|
|
1860
|
+
* Write a file to the code interpreter
|
|
1861
|
+
*/
|
|
1862
|
+
write = async (params) => {
|
|
1863
|
+
return this.sandbox.dataApi.writeFile(params);
|
|
1864
|
+
};
|
|
1865
|
+
};
|
|
1866
|
+
FileSystemOperations = class {
|
|
1867
|
+
sandbox;
|
|
1868
|
+
constructor(sandbox) {
|
|
1869
|
+
this.sandbox = sandbox;
|
|
1870
|
+
}
|
|
1871
|
+
/**
|
|
1872
|
+
* List directory contents
|
|
1873
|
+
*/
|
|
1874
|
+
list = async (params) => {
|
|
1875
|
+
return this.sandbox.dataApi.listDirectory(params);
|
|
1876
|
+
};
|
|
1877
|
+
/**
|
|
1878
|
+
* Move a file or directory
|
|
1879
|
+
*/
|
|
1880
|
+
move = async (params) => {
|
|
1881
|
+
return this.sandbox.dataApi.moveFile(params);
|
|
1882
|
+
};
|
|
1883
|
+
/**
|
|
1884
|
+
* Remove a file or directory
|
|
1885
|
+
*/
|
|
1886
|
+
remove = async (params) => {
|
|
1887
|
+
return this.sandbox.dataApi.removeFile(params);
|
|
1888
|
+
};
|
|
1889
|
+
/**
|
|
1890
|
+
* Get file or directory statistics
|
|
1891
|
+
*/
|
|
1892
|
+
stat = async (params) => {
|
|
1893
|
+
return this.sandbox.dataApi.stat(params);
|
|
1894
|
+
};
|
|
1895
|
+
/**
|
|
1896
|
+
* Create a directory
|
|
1897
|
+
*/
|
|
1898
|
+
mkdir = async (params) => {
|
|
1899
|
+
return this.sandbox.dataApi.mkdir(params);
|
|
1900
|
+
};
|
|
1901
|
+
/**
|
|
1902
|
+
* Upload a file to the code interpreter
|
|
1903
|
+
*/
|
|
1904
|
+
upload = async (params) => {
|
|
1905
|
+
return this.sandbox.dataApi.uploadFile(params);
|
|
1906
|
+
};
|
|
1907
|
+
/**
|
|
1908
|
+
* Download a file from the code interpreter
|
|
1909
|
+
*/
|
|
1910
|
+
download = async (params) => {
|
|
1911
|
+
return this.sandbox.dataApi.downloadFile(params);
|
|
1912
|
+
};
|
|
1913
|
+
};
|
|
1914
|
+
ProcessOperations = class {
|
|
1915
|
+
sandbox;
|
|
1916
|
+
constructor(sandbox) {
|
|
1917
|
+
this.sandbox = sandbox;
|
|
1918
|
+
}
|
|
1919
|
+
/**
|
|
1920
|
+
* Execute a command in the code interpreter
|
|
1921
|
+
*/
|
|
1922
|
+
cmd = async (params) => {
|
|
1923
|
+
return this.sandbox.dataApi.cmd(params);
|
|
1924
|
+
};
|
|
1925
|
+
/**
|
|
1926
|
+
* List all processes
|
|
1927
|
+
*/
|
|
1928
|
+
list = async (params) => {
|
|
1929
|
+
return this.sandbox.dataApi.listProcesses(params);
|
|
1930
|
+
};
|
|
1931
|
+
/**
|
|
1932
|
+
* Get a specific process by PID
|
|
1933
|
+
*/
|
|
1934
|
+
get = async (params) => {
|
|
1935
|
+
return this.sandbox.dataApi.getProcess(params);
|
|
1936
|
+
};
|
|
1937
|
+
/**
|
|
1938
|
+
* Kill a specific process by PID
|
|
1939
|
+
*/
|
|
1940
|
+
kill = async (params) => {
|
|
1941
|
+
return this.sandbox.dataApi.killProcess(params);
|
|
1942
|
+
};
|
|
1943
|
+
};
|
|
1944
|
+
ContextOperations = class {
|
|
1945
|
+
sandbox;
|
|
1946
|
+
_contextId;
|
|
1947
|
+
_language;
|
|
1948
|
+
_cwd;
|
|
1949
|
+
constructor(sandbox) {
|
|
1950
|
+
this.sandbox = sandbox;
|
|
1951
|
+
}
|
|
1952
|
+
/**
|
|
1953
|
+
* Get the current context ID
|
|
1954
|
+
*/
|
|
1955
|
+
get contextId() {
|
|
1956
|
+
return this._contextId;
|
|
1957
|
+
}
|
|
1958
|
+
/**
|
|
1959
|
+
* List all contexts
|
|
1960
|
+
*/
|
|
1961
|
+
list = async (params) => {
|
|
1962
|
+
return this.sandbox.dataApi.listContexts(params);
|
|
1963
|
+
};
|
|
1964
|
+
/**
|
|
1965
|
+
* Create a new context and save its ID
|
|
1966
|
+
*/
|
|
1967
|
+
create = async (params) => {
|
|
1968
|
+
const language = params?.language || "python" /* PYTHON */;
|
|
1969
|
+
const cwd = params?.cwd || "/home/user";
|
|
1970
|
+
const result = await this.sandbox.dataApi.createContext({
|
|
1971
|
+
language,
|
|
1972
|
+
cwd
|
|
1973
|
+
});
|
|
1974
|
+
if (result?.id && result?.cwd && result?.language) {
|
|
1975
|
+
this._contextId = result.id;
|
|
1976
|
+
this._language = result.language;
|
|
1977
|
+
this._cwd = result.cwd;
|
|
1978
|
+
return this;
|
|
1979
|
+
}
|
|
1980
|
+
throw new ServerError(500, "Failed to create context");
|
|
1981
|
+
};
|
|
1982
|
+
/**
|
|
1983
|
+
* Get a specific context by ID
|
|
1984
|
+
*/
|
|
1985
|
+
get = async (params) => {
|
|
1986
|
+
const id = params?.contextId || this._contextId;
|
|
1987
|
+
if (!id) {
|
|
1988
|
+
logger.error("context id is not set");
|
|
1989
|
+
throw new Error("context id is not set");
|
|
1990
|
+
}
|
|
1991
|
+
const result = await this.sandbox.dataApi.getContext({ contextId: id });
|
|
1992
|
+
if (result?.id && result?.cwd && result?.language) {
|
|
1993
|
+
this._contextId = result.id;
|
|
1994
|
+
this._language = result.language;
|
|
1995
|
+
this._cwd = result.cwd;
|
|
1996
|
+
return this;
|
|
1997
|
+
}
|
|
1998
|
+
throw new ServerError(500, "Failed to get context");
|
|
1999
|
+
};
|
|
2000
|
+
/**
|
|
2001
|
+
* Execute code in a context
|
|
2002
|
+
*/
|
|
2003
|
+
execute = async (params) => {
|
|
2004
|
+
const { code, timeout = 30 } = params;
|
|
2005
|
+
let { contextId, language } = params;
|
|
2006
|
+
if (!contextId) {
|
|
2007
|
+
contextId = this._contextId;
|
|
2008
|
+
}
|
|
2009
|
+
if (!contextId && !language) {
|
|
2010
|
+
logger.debug("context id is not set, use default language: python");
|
|
2011
|
+
language = "python" /* PYTHON */;
|
|
2012
|
+
}
|
|
2013
|
+
return this.sandbox.dataApi.executeCode({
|
|
2014
|
+
code,
|
|
2015
|
+
contextId,
|
|
2016
|
+
language,
|
|
2017
|
+
timeout
|
|
2018
|
+
});
|
|
2019
|
+
};
|
|
2020
|
+
/**
|
|
2021
|
+
* Delete a context
|
|
2022
|
+
*/
|
|
2023
|
+
delete = async (params) => {
|
|
2024
|
+
const id = params?.contextId || this._contextId;
|
|
2025
|
+
if (!id) {
|
|
2026
|
+
throw new Error(
|
|
2027
|
+
"context_id is required. Either pass it as parameter or create a context first."
|
|
2028
|
+
);
|
|
2029
|
+
}
|
|
2030
|
+
const result = await this.sandbox.dataApi.deleteContext({ contextId: id });
|
|
2031
|
+
this._contextId = void 0;
|
|
2032
|
+
return result;
|
|
2033
|
+
};
|
|
2034
|
+
};
|
|
2035
|
+
exports.CodeInterpreterSandbox = class extends exports.Sandbox {
|
|
2036
|
+
static templateType = "CodeInterpreter" /* CODE_INTERPRETER */;
|
|
2037
|
+
/**
|
|
2038
|
+
* Create a Code Interpreter Sandbox from template
|
|
2039
|
+
* 从模板创建代码解释器沙箱 / Create Code Interpreter Sandbox from Template
|
|
2040
|
+
*/
|
|
2041
|
+
static async createFromTemplate(templateName, options, config2) {
|
|
2042
|
+
const sandbox = await exports.Sandbox.create({
|
|
2043
|
+
input: {
|
|
2044
|
+
templateName,
|
|
2045
|
+
sandboxIdleTimeoutSeconds: options?.sandboxIdleTimeoutSeconds,
|
|
2046
|
+
nasConfig: options?.nasConfig,
|
|
2047
|
+
ossMountConfig: options?.ossMountConfig,
|
|
2048
|
+
polarFsConfig: options?.polarFsConfig
|
|
2049
|
+
},
|
|
2050
|
+
templateType: "CodeInterpreter" /* CODE_INTERPRETER */,
|
|
2051
|
+
config: config2
|
|
2052
|
+
});
|
|
2053
|
+
return sandbox;
|
|
2054
|
+
}
|
|
2055
|
+
constructor(sandbox, config2) {
|
|
2056
|
+
super(sandbox, config2);
|
|
2057
|
+
}
|
|
2058
|
+
_dataApi;
|
|
2059
|
+
_file;
|
|
2060
|
+
_fileSystem;
|
|
2061
|
+
_context;
|
|
2062
|
+
_process;
|
|
2063
|
+
/**
|
|
2064
|
+
* Get data API client
|
|
2065
|
+
*/
|
|
2066
|
+
get dataApi() {
|
|
2067
|
+
if (!this._dataApi) {
|
|
2068
|
+
this._dataApi = new exports.CodeInterpreterDataAPI({
|
|
2069
|
+
sandboxId: this.sandboxId || "",
|
|
2070
|
+
config: this._config
|
|
2071
|
+
});
|
|
2072
|
+
}
|
|
2073
|
+
return this._dataApi;
|
|
2074
|
+
}
|
|
2075
|
+
/**
|
|
2076
|
+
* Access file upload/download operations
|
|
2077
|
+
*/
|
|
2078
|
+
get file() {
|
|
2079
|
+
if (!this._file) {
|
|
2080
|
+
this._file = new FileOperations(this);
|
|
2081
|
+
}
|
|
2082
|
+
return this._file;
|
|
2083
|
+
}
|
|
2084
|
+
/**
|
|
2085
|
+
* Access file system operations
|
|
2086
|
+
*/
|
|
2087
|
+
get fileSystem() {
|
|
2088
|
+
if (!this._fileSystem) {
|
|
2089
|
+
this._fileSystem = new FileSystemOperations(this);
|
|
2090
|
+
}
|
|
2091
|
+
return this._fileSystem;
|
|
2092
|
+
}
|
|
2093
|
+
/**
|
|
2094
|
+
* Access context management operations
|
|
2095
|
+
*/
|
|
2096
|
+
get context() {
|
|
2097
|
+
if (!this._context) {
|
|
2098
|
+
this._context = new ContextOperations(this);
|
|
2099
|
+
}
|
|
2100
|
+
return this._context;
|
|
2101
|
+
}
|
|
2102
|
+
/**
|
|
2103
|
+
* Access process management operations
|
|
2104
|
+
*/
|
|
2105
|
+
get process() {
|
|
2106
|
+
if (!this._process) {
|
|
2107
|
+
this._process = new ProcessOperations(this);
|
|
2108
|
+
}
|
|
2109
|
+
return this._process;
|
|
2110
|
+
}
|
|
2111
|
+
/**
|
|
2112
|
+
* Check sandbox health
|
|
2113
|
+
*/
|
|
2114
|
+
checkHealth = async (params) => {
|
|
2115
|
+
return this.dataApi.checkHealth({
|
|
2116
|
+
sandboxId: this.sandboxId,
|
|
2117
|
+
config: params?.config
|
|
2118
|
+
});
|
|
2119
|
+
};
|
|
2120
|
+
/**
|
|
2121
|
+
* Execute code (convenience method delegating to context.execute)
|
|
2122
|
+
*/
|
|
2123
|
+
execute = async (params) => {
|
|
2124
|
+
return this.context.execute(params);
|
|
2125
|
+
};
|
|
2126
|
+
};
|
|
2127
|
+
}
|
|
2128
|
+
});
|
|
2129
|
+
|
|
2130
|
+
// src/sandbox/browser-sandbox.ts
|
|
2131
|
+
var browser_sandbox_exports = {};
|
|
2132
|
+
__export(browser_sandbox_exports, {
|
|
2133
|
+
BrowserSandbox: () => exports.BrowserSandbox
|
|
2134
|
+
});
|
|
2135
|
+
exports.BrowserSandbox = void 0;
|
|
2136
|
+
var init_browser_sandbox = __esm({
|
|
2137
|
+
"src/sandbox/browser-sandbox.ts"() {
|
|
2138
|
+
init_browser_data();
|
|
2139
|
+
init_model2();
|
|
2140
|
+
init_sandbox();
|
|
2141
|
+
exports.BrowserSandbox = class extends exports.Sandbox {
|
|
2142
|
+
static templateType = "Browser" /* BROWSER */;
|
|
2143
|
+
/**
|
|
2144
|
+
* Create a Browser Sandbox from template
|
|
2145
|
+
* 从模板创建浏览器沙箱 / Create Browser Sandbox from Template
|
|
2146
|
+
*/
|
|
2147
|
+
static async createFromTemplate(templateName, options, config2) {
|
|
2148
|
+
const sandbox = await exports.Sandbox.create({
|
|
2149
|
+
input: {
|
|
2150
|
+
templateName,
|
|
2151
|
+
sandboxIdleTimeoutSeconds: options?.sandboxIdleTimeoutSeconds,
|
|
2152
|
+
nasConfig: options?.nasConfig,
|
|
2153
|
+
ossMountConfig: options?.ossMountConfig,
|
|
2154
|
+
polarFsConfig: options?.polarFsConfig
|
|
2155
|
+
},
|
|
2156
|
+
templateType: "Browser" /* BROWSER */,
|
|
2157
|
+
config: config2
|
|
2158
|
+
});
|
|
2159
|
+
return sandbox;
|
|
2160
|
+
}
|
|
2161
|
+
constructor(sandbox, config2) {
|
|
2162
|
+
super(sandbox, config2);
|
|
2163
|
+
}
|
|
2164
|
+
_dataApi;
|
|
2165
|
+
/**
|
|
2166
|
+
* Get data API client
|
|
2167
|
+
*/
|
|
2168
|
+
get dataApi() {
|
|
2169
|
+
if (!this._dataApi) {
|
|
2170
|
+
if (!this.sandboxId) {
|
|
2171
|
+
throw new Error("Sandbox ID is not set");
|
|
2172
|
+
}
|
|
2173
|
+
this._dataApi = new exports.BrowserDataAPI({
|
|
2174
|
+
sandboxId: this.sandboxId,
|
|
2175
|
+
config: this._config
|
|
2176
|
+
});
|
|
2177
|
+
}
|
|
2178
|
+
return this._dataApi;
|
|
2179
|
+
}
|
|
2180
|
+
/**
|
|
2181
|
+
* Check sandbox health
|
|
2182
|
+
*/
|
|
2183
|
+
checkHealth = async (params) => {
|
|
2184
|
+
return this.dataApi.checkHealth({
|
|
2185
|
+
sandboxId: this.sandboxId,
|
|
2186
|
+
config: params?.config
|
|
2187
|
+
});
|
|
2188
|
+
};
|
|
2189
|
+
/**
|
|
2190
|
+
* Get CDP WebSocket URL for browser automation
|
|
2191
|
+
*/
|
|
2192
|
+
getCdpUrl(record) {
|
|
2193
|
+
return this.dataApi.getCdpUrl(record);
|
|
2194
|
+
}
|
|
2195
|
+
/**
|
|
2196
|
+
* Get VNC WebSocket URL for live view
|
|
2197
|
+
*/
|
|
2198
|
+
getVncUrl(record) {
|
|
2199
|
+
return this.dataApi.getVncUrl(record);
|
|
2200
|
+
}
|
|
2201
|
+
/**
|
|
2202
|
+
* List all recordings
|
|
2203
|
+
*/
|
|
2204
|
+
listRecordings = async (params) => {
|
|
2205
|
+
return this.dataApi.listRecordings(params);
|
|
2206
|
+
};
|
|
2207
|
+
/**
|
|
2208
|
+
* Download a recording video file
|
|
2209
|
+
*/
|
|
2210
|
+
downloadRecording = async (params) => {
|
|
2211
|
+
return this.dataApi.downloadRecording(params);
|
|
2212
|
+
};
|
|
2213
|
+
/**
|
|
2214
|
+
* Delete a recording
|
|
2215
|
+
*/
|
|
2216
|
+
deleteRecording = async (params) => {
|
|
2217
|
+
return this.dataApi.deleteRecording(params);
|
|
2218
|
+
};
|
|
2219
|
+
};
|
|
2220
|
+
}
|
|
2221
|
+
});
|
|
2222
|
+
|
|
2223
|
+
// src/sandbox/custom-sandbox.ts
|
|
2224
|
+
var custom_sandbox_exports = {};
|
|
2225
|
+
__export(custom_sandbox_exports, {
|
|
2226
|
+
CustomSandbox: () => exports.CustomSandbox
|
|
2227
|
+
});
|
|
2228
|
+
exports.CustomSandbox = void 0;
|
|
2229
|
+
var init_custom_sandbox = __esm({
|
|
2230
|
+
"src/sandbox/custom-sandbox.ts"() {
|
|
2231
|
+
init_config();
|
|
2232
|
+
init_model2();
|
|
2233
|
+
init_sandbox();
|
|
2234
|
+
init_sandbox_data();
|
|
2235
|
+
exports.CustomSandbox = class extends exports.Sandbox {
|
|
2236
|
+
static templateType = "CustomImage" /* CUSTOM */;
|
|
2237
|
+
/**
|
|
2238
|
+
* Create a Custom Sandbox from template
|
|
2239
|
+
* 从模板创建自定义沙箱 / Create Custom Sandbox from Template
|
|
2240
|
+
*/
|
|
2241
|
+
static async createFromTemplate(templateName, options, config2) {
|
|
2242
|
+
return await exports.Sandbox.create({
|
|
2243
|
+
input: {
|
|
2244
|
+
templateName,
|
|
2245
|
+
sandboxIdleTimeoutSeconds: options?.sandboxIdleTimeoutSeconds,
|
|
2246
|
+
nasConfig: options?.nasConfig,
|
|
2247
|
+
ossMountConfig: options?.ossMountConfig,
|
|
2248
|
+
polarFsConfig: options?.polarFsConfig
|
|
2249
|
+
},
|
|
2250
|
+
templateType: "CustomImage" /* CUSTOM */,
|
|
2251
|
+
config: config2
|
|
2252
|
+
});
|
|
2253
|
+
}
|
|
2254
|
+
constructor(sandbox, config2) {
|
|
2255
|
+
super(sandbox, config2);
|
|
2256
|
+
}
|
|
2257
|
+
_dataApi;
|
|
2258
|
+
/**
|
|
2259
|
+
* Get data API client
|
|
2260
|
+
*/
|
|
2261
|
+
get dataApi() {
|
|
2262
|
+
if (!this._dataApi) {
|
|
2263
|
+
this._dataApi = new exports.SandboxDataAPI({
|
|
2264
|
+
sandboxId: this.sandboxId || "",
|
|
2265
|
+
config: this._config
|
|
2266
|
+
});
|
|
2267
|
+
}
|
|
2268
|
+
return this._dataApi;
|
|
2269
|
+
}
|
|
2270
|
+
/**
|
|
2271
|
+
* Get base URL for the sandbox
|
|
2272
|
+
* 获取沙箱的基础 URL / Get base URL for the sandbox
|
|
2273
|
+
*
|
|
2274
|
+
* @returns 基础 URL / Base URL
|
|
2275
|
+
*/
|
|
2276
|
+
getBaseUrl() {
|
|
2277
|
+
const cfg = Config.withConfigs(this._config);
|
|
2278
|
+
return `${cfg.dataEndpoint}/sandboxes/${this.sandboxId}`;
|
|
2279
|
+
}
|
|
2280
|
+
};
|
|
2281
|
+
}
|
|
2282
|
+
});
|
|
2283
|
+
|
|
2284
|
+
// src/sandbox/sandbox.ts
|
|
2285
|
+
exports.Sandbox = void 0;
|
|
2286
|
+
var init_sandbox = __esm({
|
|
2287
|
+
"src/sandbox/sandbox.ts"() {
|
|
2288
|
+
init_utils();
|
|
2289
|
+
init_log();
|
|
2290
|
+
init_config();
|
|
2291
|
+
init_resource();
|
|
2292
|
+
init_model2();
|
|
2293
|
+
init_sandbox_data();
|
|
2294
|
+
exports.Sandbox = class _Sandbox extends ResourceBase {
|
|
2295
|
+
templateType;
|
|
2296
|
+
/**
|
|
2297
|
+
* 沙箱 ID / Sandbox ID
|
|
2298
|
+
*/
|
|
2299
|
+
sandboxId;
|
|
2300
|
+
/**
|
|
2301
|
+
* 沙箱名称 / Sandbox Name
|
|
2302
|
+
*/
|
|
2303
|
+
sandboxName;
|
|
2304
|
+
/**
|
|
2305
|
+
* 模板 ID / Template ID
|
|
2306
|
+
*/
|
|
2307
|
+
templateId;
|
|
2308
|
+
/**
|
|
2309
|
+
* 模板名称 / Template Name
|
|
2310
|
+
*/
|
|
2311
|
+
templateName;
|
|
2312
|
+
/**
|
|
2313
|
+
* 沙箱状态 / Sandbox State
|
|
2314
|
+
*/
|
|
2315
|
+
state;
|
|
2316
|
+
/**
|
|
2317
|
+
* 状态原因 / State Reason
|
|
2318
|
+
*/
|
|
2319
|
+
stateReason;
|
|
2320
|
+
/**
|
|
2321
|
+
* 沙箱创建时间 / Sandbox Creation Time
|
|
2322
|
+
*/
|
|
2323
|
+
createdAt;
|
|
2324
|
+
/**
|
|
2325
|
+
* 最后更新时间 / Last Updated Time
|
|
2326
|
+
*/
|
|
2327
|
+
lastUpdatedAt;
|
|
2328
|
+
/**
|
|
2329
|
+
* 沙箱空闲超时时间(秒) / Sandbox Idle Timeout (seconds)
|
|
2330
|
+
*/
|
|
2331
|
+
sandboxIdleTimeoutSeconds;
|
|
2332
|
+
/**
|
|
2333
|
+
* 沙箱结束时间 / Sandbox End Time
|
|
2334
|
+
*/
|
|
2335
|
+
endedAt;
|
|
2336
|
+
/**
|
|
2337
|
+
* 元数据 / Metadata
|
|
2338
|
+
*/
|
|
2339
|
+
metadata;
|
|
2340
|
+
/**
|
|
2341
|
+
* 沙箱全局唯一资源名称 / Sandbox ARN
|
|
2342
|
+
*/
|
|
2343
|
+
sandboxArn;
|
|
2344
|
+
/**
|
|
2345
|
+
* 沙箱空闲 TTL(秒) / Sandbox Idle TTL (seconds)
|
|
2346
|
+
*/
|
|
2347
|
+
sandboxIdleTTLInSeconds;
|
|
2348
|
+
_config;
|
|
2349
|
+
constructor(data, config2) {
|
|
2350
|
+
super();
|
|
2351
|
+
if (data) {
|
|
2352
|
+
updateObjectProperties(this, data);
|
|
2353
|
+
}
|
|
2354
|
+
this._config = config2;
|
|
2355
|
+
}
|
|
2356
|
+
/**
|
|
2357
|
+
* Create sandbox from SDK response object
|
|
2358
|
+
* 从 SDK 响应对象创建沙箱 / Create Sandbox from SDK Response Object
|
|
2359
|
+
*/
|
|
2360
|
+
static fromInnerObject(obj, config2) {
|
|
2361
|
+
return new _Sandbox(
|
|
2362
|
+
{
|
|
2363
|
+
sandboxId: obj.sandboxId,
|
|
2364
|
+
sandboxName: obj.sandboxName,
|
|
2365
|
+
templateId: obj.templateId,
|
|
2366
|
+
templateName: obj.templateName,
|
|
2367
|
+
// API returns "status" field, map it to "state"
|
|
2368
|
+
state: obj.status || obj.state,
|
|
2369
|
+
stateReason: obj.stateReason,
|
|
2370
|
+
createdAt: obj.createdAt,
|
|
2371
|
+
lastUpdatedAt: obj.lastUpdatedAt,
|
|
2372
|
+
sandboxIdleTimeoutSeconds: obj.sandboxIdleTimeoutSeconds,
|
|
2373
|
+
// New fields / 新增字段
|
|
2374
|
+
endedAt: obj.endedAt,
|
|
2375
|
+
metadata: obj.metadata,
|
|
2376
|
+
sandboxArn: obj.sandboxArn,
|
|
2377
|
+
sandboxIdleTTLInSeconds: obj.sandboxIdleTTLInSeconds
|
|
2378
|
+
},
|
|
2379
|
+
config2
|
|
2380
|
+
);
|
|
2381
|
+
}
|
|
2382
|
+
static getClient() {
|
|
2383
|
+
const { SandboxClient: SandboxClient2 } = (init_client(), __toCommonJS(client_exports));
|
|
2384
|
+
return new SandboxClient2();
|
|
2385
|
+
}
|
|
2386
|
+
static async create(arg1, arg2) {
|
|
2387
|
+
if (typeof arg1 === "object" && arg1 !== null && "input" in arg1) {
|
|
2388
|
+
return await _Sandbox.getClient().createSandbox(arg1);
|
|
2389
|
+
}
|
|
2390
|
+
logger.warn(
|
|
2391
|
+
"Deprecated: Sandbox.create(input, config) is deprecated. Use Sandbox.create({ input, config }) instead."
|
|
2392
|
+
);
|
|
2393
|
+
return await _Sandbox.getClient().createSandbox(arg1, arg2);
|
|
2394
|
+
}
|
|
2395
|
+
static async delete(arg1, arg2) {
|
|
2396
|
+
if (typeof arg1 === "string") {
|
|
2397
|
+
logger.warn(
|
|
2398
|
+
"Sandbox.delete(id, config) is deprecated. Use Sandbox.delete({ id, config }) instead."
|
|
2399
|
+
);
|
|
2400
|
+
return await _Sandbox.getClient().deleteSandbox(arg1, arg2);
|
|
2401
|
+
}
|
|
2402
|
+
return await _Sandbox.getClient().deleteSandbox(arg1);
|
|
2403
|
+
}
|
|
2404
|
+
static async stop(arg1, arg2) {
|
|
2405
|
+
if (typeof arg1 === "string") {
|
|
2406
|
+
logger.warn(
|
|
2407
|
+
"Sandbox.stop(id, config) is deprecated. Use Sandbox.stop({ id, config }) instead."
|
|
2408
|
+
);
|
|
2409
|
+
return await _Sandbox.getClient().stopSandbox(arg1, arg2);
|
|
2410
|
+
}
|
|
2411
|
+
return await _Sandbox.getClient().stopSandbox(arg1);
|
|
2412
|
+
}
|
|
2413
|
+
static async get(arg1, arg2, arg3) {
|
|
2414
|
+
let id;
|
|
2415
|
+
let templateType;
|
|
2416
|
+
let config2;
|
|
2417
|
+
if (typeof arg1 === "string") {
|
|
2418
|
+
logger.warn(
|
|
2419
|
+
"Deprecated: Sandbox.get(id, templateType?, config?) is deprecated. Use Sandbox.get({ id, templateType, config }) instead."
|
|
2420
|
+
);
|
|
2421
|
+
id = arg1;
|
|
2422
|
+
if (arg2 && typeof arg2 === "string") {
|
|
2423
|
+
templateType = arg2;
|
|
2424
|
+
config2 = arg3;
|
|
2425
|
+
} else {
|
|
2426
|
+
config2 = arg2;
|
|
2427
|
+
}
|
|
2428
|
+
} else {
|
|
2429
|
+
id = arg1.id;
|
|
2430
|
+
templateType = arg1.templateType;
|
|
2431
|
+
config2 = arg1.config;
|
|
2432
|
+
}
|
|
2433
|
+
try {
|
|
2434
|
+
const cfg = Config.withConfigs(config2);
|
|
2435
|
+
const dataApi = new exports.SandboxDataAPI({
|
|
2436
|
+
sandboxId: id,
|
|
2437
|
+
config: cfg
|
|
2438
|
+
});
|
|
2439
|
+
const result = await dataApi.getSandbox({
|
|
2440
|
+
sandboxId: id,
|
|
2441
|
+
config: cfg
|
|
2442
|
+
});
|
|
2443
|
+
if (result.code !== "SUCCESS") {
|
|
2444
|
+
throw new ClientError(0, `Failed to get sandbox: ${result.message || "Unknown error"}`);
|
|
2445
|
+
}
|
|
2446
|
+
const data = result.data || {};
|
|
2447
|
+
const baseSandbox = _Sandbox.fromInnerObject(data, config2);
|
|
2448
|
+
if (templateType) {
|
|
2449
|
+
switch (templateType) {
|
|
2450
|
+
case "CodeInterpreter" /* CODE_INTERPRETER */: {
|
|
2451
|
+
const { CodeInterpreterSandbox: CodeInterpreterSandbox2 } = await Promise.resolve().then(() => (init_code_interpreter_sandbox(), code_interpreter_sandbox_exports));
|
|
2452
|
+
const sandbox = new CodeInterpreterSandbox2(baseSandbox, config2);
|
|
2453
|
+
return sandbox;
|
|
2454
|
+
}
|
|
2455
|
+
case "Browser" /* BROWSER */: {
|
|
2456
|
+
const { BrowserSandbox: BrowserSandbox2 } = await Promise.resolve().then(() => (init_browser_sandbox(), browser_sandbox_exports));
|
|
2457
|
+
const sandbox = new BrowserSandbox2(baseSandbox, config2);
|
|
2458
|
+
return sandbox;
|
|
2459
|
+
}
|
|
2460
|
+
case "AllInOne" /* AIO */: {
|
|
2461
|
+
const { AioSandbox: AioSandbox2 } = await Promise.resolve().then(() => (init_aio_sandbox(), aio_sandbox_exports));
|
|
2462
|
+
const sandbox = new AioSandbox2(baseSandbox, config2);
|
|
2463
|
+
return sandbox;
|
|
2464
|
+
}
|
|
2465
|
+
case "CustomImage" /* CUSTOM */: {
|
|
2466
|
+
const { CustomSandbox: CustomSandbox2 } = await Promise.resolve().then(() => (init_custom_sandbox(), custom_sandbox_exports));
|
|
2467
|
+
const sandbox = new CustomSandbox2(baseSandbox, config2);
|
|
2468
|
+
return sandbox;
|
|
2469
|
+
}
|
|
2470
|
+
}
|
|
2471
|
+
}
|
|
2472
|
+
return baseSandbox;
|
|
2473
|
+
} catch (error) {
|
|
2474
|
+
if (error instanceof HTTPError) {
|
|
2475
|
+
throw error.toResourceError("Sandbox", id);
|
|
2476
|
+
}
|
|
2477
|
+
throw error;
|
|
2478
|
+
}
|
|
2479
|
+
}
|
|
2480
|
+
static async list(arg1, arg2) {
|
|
2481
|
+
if (arg2 !== void 0 || arg1 && ("maxResults" in arg1 || "nextToken" in arg1 || "status" in arg1 || "templateName" in arg1 || "templateType" in arg1)) {
|
|
2482
|
+
logger.warn(
|
|
2483
|
+
"Deprecated: Sandbox.list(input, config) is deprecated. Use Sandbox.list({ input, config }) instead."
|
|
2484
|
+
);
|
|
2485
|
+
return await _Sandbox.getClient().listSandboxes(arg1, arg2);
|
|
2486
|
+
}
|
|
2487
|
+
return await _Sandbox.getClient().listSandboxes(
|
|
2488
|
+
arg1
|
|
2489
|
+
);
|
|
2490
|
+
}
|
|
2491
|
+
get = async (params) => {
|
|
2492
|
+
const { config: config2 } = params ?? {};
|
|
2493
|
+
return await _Sandbox.get({
|
|
2494
|
+
id: this.sandboxId,
|
|
2495
|
+
templateType: this.templateType,
|
|
2496
|
+
config: Config.withConfigs(this._config, config2)
|
|
2497
|
+
});
|
|
2498
|
+
};
|
|
2499
|
+
/**
|
|
2500
|
+
* Delete this sandbox
|
|
2501
|
+
*/
|
|
2502
|
+
delete = async (params) => {
|
|
2503
|
+
const config2 = params?.config;
|
|
2504
|
+
if (!this.sandboxId) {
|
|
2505
|
+
throw new Error("sandboxId is required to delete a Sandbox");
|
|
2506
|
+
}
|
|
2507
|
+
const result = await _Sandbox.delete({
|
|
2508
|
+
id: this.sandboxId,
|
|
2509
|
+
config: config2 ?? this._config
|
|
2510
|
+
});
|
|
2511
|
+
updateObjectProperties(this, result);
|
|
2512
|
+
return this;
|
|
2513
|
+
};
|
|
2514
|
+
/**
|
|
2515
|
+
* Stop this sandbox
|
|
2516
|
+
*/
|
|
2517
|
+
stop = async (params) => {
|
|
2518
|
+
const config2 = params?.config;
|
|
2519
|
+
if (!this.sandboxId) {
|
|
2520
|
+
throw new Error("sandboxId is required to stop a Sandbox");
|
|
2521
|
+
}
|
|
2522
|
+
const result = await _Sandbox.stop({
|
|
2523
|
+
id: this.sandboxId,
|
|
2524
|
+
config: config2 ?? this._config
|
|
2525
|
+
});
|
|
2526
|
+
updateObjectProperties(this, result);
|
|
2527
|
+
return this;
|
|
2528
|
+
};
|
|
2529
|
+
/**
|
|
2530
|
+
* Refresh this sandbox's data
|
|
2531
|
+
*/
|
|
2532
|
+
refresh = async (params) => {
|
|
2533
|
+
const config2 = params?.config;
|
|
2534
|
+
if (!this.sandboxId) {
|
|
2535
|
+
throw new Error("sandboxId is required to refresh a Sandbox");
|
|
2536
|
+
}
|
|
2537
|
+
const result = await _Sandbox.get({
|
|
2538
|
+
id: this.sandboxId,
|
|
2539
|
+
config: config2 ?? this._config
|
|
2540
|
+
});
|
|
2541
|
+
updateObjectProperties(this, result);
|
|
2542
|
+
return this;
|
|
2543
|
+
};
|
|
2544
|
+
/**
|
|
2545
|
+
* Wait until the sandbox is running
|
|
2546
|
+
*/
|
|
2547
|
+
waitUntilRunning = async (options, config2) => {
|
|
2548
|
+
const timeout = (options?.timeoutSeconds ?? 300) * 1e3;
|
|
2549
|
+
const interval = (options?.intervalSeconds ?? 5) * 1e3;
|
|
2550
|
+
const startTime = Date.now();
|
|
2551
|
+
while (Date.now() - startTime < timeout) {
|
|
2552
|
+
await this.refresh({ config: config2 });
|
|
2553
|
+
if (options?.beforeCheck) {
|
|
2554
|
+
options.beforeCheck(this);
|
|
2555
|
+
}
|
|
2556
|
+
if (this.state === "Running" /* RUNNING */ || this.state === "READY" /* READY */) {
|
|
2557
|
+
return this;
|
|
2558
|
+
}
|
|
2559
|
+
if (this.state === "Failed" /* FAILED */) {
|
|
2560
|
+
throw new Error(`Sandbox failed: ${this.stateReason}`);
|
|
2561
|
+
}
|
|
2562
|
+
await new Promise((resolve) => setTimeout(resolve, interval));
|
|
2563
|
+
}
|
|
2564
|
+
throw new Error(
|
|
2565
|
+
`Timeout waiting for Sandbox to be running after ${options?.timeoutSeconds ?? 300} seconds`
|
|
2566
|
+
);
|
|
2567
|
+
};
|
|
2568
|
+
};
|
|
2569
|
+
}
|
|
2570
|
+
});
|
|
2571
|
+
|
|
2572
|
+
// src/sandbox/aio-sandbox.ts
|
|
2573
|
+
var aio_sandbox_exports = {};
|
|
2574
|
+
__export(aio_sandbox_exports, {
|
|
2575
|
+
AioContextOperations: () => AioContextOperations,
|
|
2576
|
+
AioFileOperations: () => AioFileOperations,
|
|
2577
|
+
AioFileSystemOperations: () => AioFileSystemOperations,
|
|
2578
|
+
AioProcessOperations: () => AioProcessOperations,
|
|
2579
|
+
AioSandbox: () => exports.AioSandbox
|
|
2580
|
+
});
|
|
2581
|
+
var AioFileOperations, AioFileSystemOperations, AioProcessOperations, AioContextOperations; exports.AioSandbox = void 0;
|
|
2582
|
+
var init_aio_sandbox = __esm({
|
|
2583
|
+
"src/sandbox/aio-sandbox.ts"() {
|
|
2584
|
+
init_log();
|
|
2585
|
+
init_exception();
|
|
2586
|
+
init_aio_data();
|
|
2587
|
+
init_model2();
|
|
2588
|
+
init_sandbox();
|
|
2589
|
+
AioFileOperations = class {
|
|
2590
|
+
sandbox;
|
|
2591
|
+
constructor(sandbox) {
|
|
2592
|
+
this.sandbox = sandbox;
|
|
2593
|
+
}
|
|
2594
|
+
/**
|
|
2595
|
+
* Read a file from the sandbox
|
|
2596
|
+
*/
|
|
2597
|
+
read = async (path2) => {
|
|
2598
|
+
return this.sandbox.dataApi.readFile({ path: path2 });
|
|
2599
|
+
};
|
|
2600
|
+
/**
|
|
2601
|
+
* Write a file to the sandbox
|
|
2602
|
+
*/
|
|
2603
|
+
write = async (params) => {
|
|
2604
|
+
return this.sandbox.dataApi.writeFile(params);
|
|
2605
|
+
};
|
|
2606
|
+
};
|
|
2607
|
+
AioFileSystemOperations = class {
|
|
2608
|
+
sandbox;
|
|
2609
|
+
constructor(sandbox) {
|
|
2610
|
+
this.sandbox = sandbox;
|
|
2611
|
+
}
|
|
2612
|
+
/**
|
|
2613
|
+
* List directory contents
|
|
2614
|
+
*/
|
|
2615
|
+
list = async (params) => {
|
|
2616
|
+
return this.sandbox.dataApi.listDirectory(params);
|
|
2617
|
+
};
|
|
2618
|
+
/**
|
|
2619
|
+
* Move a file or directory
|
|
2620
|
+
*/
|
|
2621
|
+
move = async (params) => {
|
|
2622
|
+
return this.sandbox.dataApi.moveFile(params);
|
|
2623
|
+
};
|
|
2624
|
+
/**
|
|
2625
|
+
* Remove a file or directory
|
|
2626
|
+
*/
|
|
2627
|
+
remove = async (params) => {
|
|
2628
|
+
return this.sandbox.dataApi.removeFile(params);
|
|
2629
|
+
};
|
|
2630
|
+
/**
|
|
2631
|
+
* Get file or directory statistics
|
|
2632
|
+
*/
|
|
2633
|
+
stat = async (params) => {
|
|
2634
|
+
return this.sandbox.dataApi.stat(params);
|
|
2635
|
+
};
|
|
2636
|
+
/**
|
|
2637
|
+
* Create a directory
|
|
2638
|
+
*/
|
|
2639
|
+
mkdir = async (params) => {
|
|
2640
|
+
return this.sandbox.dataApi.mkdir(params);
|
|
2641
|
+
};
|
|
2642
|
+
/**
|
|
2643
|
+
* Upload a file to the sandbox
|
|
2644
|
+
*/
|
|
2645
|
+
upload = async (params) => {
|
|
2646
|
+
return this.sandbox.dataApi.uploadFile(params);
|
|
2647
|
+
};
|
|
2648
|
+
/**
|
|
2649
|
+
* Download a file from the sandbox
|
|
2650
|
+
*/
|
|
2651
|
+
download = async (params) => {
|
|
2652
|
+
return this.sandbox.dataApi.downloadFile(params);
|
|
2653
|
+
};
|
|
2654
|
+
};
|
|
2655
|
+
AioProcessOperations = class {
|
|
2656
|
+
sandbox;
|
|
2657
|
+
constructor(sandbox) {
|
|
2658
|
+
this.sandbox = sandbox;
|
|
2659
|
+
}
|
|
2660
|
+
/**
|
|
2661
|
+
* Execute a command in the sandbox
|
|
2662
|
+
*/
|
|
2663
|
+
cmd = async (params) => {
|
|
2664
|
+
return this.sandbox.dataApi.cmd(params);
|
|
2665
|
+
};
|
|
2666
|
+
/**
|
|
2667
|
+
* List all processes
|
|
2668
|
+
*/
|
|
2669
|
+
list = async (params) => {
|
|
2670
|
+
return this.sandbox.dataApi.listProcesses(params);
|
|
2671
|
+
};
|
|
2672
|
+
/**
|
|
2673
|
+
* Get a specific process by PID
|
|
2674
|
+
*/
|
|
2675
|
+
get = async (params) => {
|
|
2676
|
+
return this.sandbox.dataApi.getProcess(params);
|
|
2677
|
+
};
|
|
2678
|
+
/**
|
|
2679
|
+
* Kill a specific process by PID
|
|
2680
|
+
*/
|
|
2681
|
+
kill = async (params) => {
|
|
2682
|
+
return this.sandbox.dataApi.killProcess(params);
|
|
2683
|
+
};
|
|
2684
|
+
};
|
|
2685
|
+
AioContextOperations = class {
|
|
2686
|
+
sandbox;
|
|
2687
|
+
_contextId;
|
|
2688
|
+
_language;
|
|
2689
|
+
_cwd;
|
|
2690
|
+
constructor(sandbox) {
|
|
2691
|
+
this.sandbox = sandbox;
|
|
2692
|
+
}
|
|
2693
|
+
/**
|
|
2694
|
+
* Get the current context ID
|
|
2695
|
+
*/
|
|
2696
|
+
get contextId() {
|
|
2697
|
+
return this._contextId;
|
|
2698
|
+
}
|
|
2699
|
+
/**
|
|
2700
|
+
* List all contexts
|
|
2701
|
+
*/
|
|
2702
|
+
list = async (params) => {
|
|
2703
|
+
return this.sandbox.dataApi.listContexts(params);
|
|
2704
|
+
};
|
|
2705
|
+
/**
|
|
2706
|
+
* Create a new context and save its ID
|
|
2707
|
+
*/
|
|
2708
|
+
create = async (params) => {
|
|
2709
|
+
const language = params?.language || "python" /* PYTHON */;
|
|
2710
|
+
const cwd = params?.cwd || "/home/user";
|
|
2711
|
+
const result = await this.sandbox.dataApi.createContext({
|
|
2712
|
+
language,
|
|
2713
|
+
cwd
|
|
2714
|
+
});
|
|
2715
|
+
if (result?.id && result?.cwd && result?.language) {
|
|
2716
|
+
this._contextId = result.id;
|
|
2717
|
+
this._language = result.language;
|
|
2718
|
+
this._cwd = result.cwd;
|
|
2719
|
+
return this;
|
|
2720
|
+
}
|
|
2721
|
+
throw new ServerError(500, "Failed to create context");
|
|
2722
|
+
};
|
|
2723
|
+
/**
|
|
2724
|
+
* Get a specific context by ID
|
|
2725
|
+
*/
|
|
2726
|
+
get = async (params) => {
|
|
2727
|
+
const id = params?.contextId || this._contextId;
|
|
2728
|
+
if (!id) {
|
|
2729
|
+
logger.error("context id is not set");
|
|
2730
|
+
throw new Error("context id is not set");
|
|
2731
|
+
}
|
|
2732
|
+
const result = await this.sandbox.dataApi.getContext({ contextId: id });
|
|
2733
|
+
if (result?.id && result?.cwd && result?.language) {
|
|
2734
|
+
this._contextId = result.id;
|
|
2735
|
+
this._language = result.language;
|
|
2736
|
+
this._cwd = result.cwd;
|
|
2737
|
+
return this;
|
|
2738
|
+
}
|
|
2739
|
+
throw new ServerError(500, "Failed to get context");
|
|
2740
|
+
};
|
|
2741
|
+
/**
|
|
2742
|
+
* Execute code in a context
|
|
2743
|
+
*/
|
|
2744
|
+
execute = async (params) => {
|
|
2745
|
+
const { code, timeout = 30 } = params;
|
|
2746
|
+
let { contextId, language } = params;
|
|
2747
|
+
if (!contextId) {
|
|
2748
|
+
contextId = this._contextId;
|
|
2749
|
+
}
|
|
2750
|
+
if (!contextId && !language) {
|
|
2751
|
+
logger.debug("context id is not set, use default language: python");
|
|
2752
|
+
language = "python" /* PYTHON */;
|
|
2753
|
+
}
|
|
2754
|
+
return this.sandbox.dataApi.executeCode({
|
|
2755
|
+
code,
|
|
2756
|
+
contextId,
|
|
2757
|
+
language,
|
|
2758
|
+
timeout
|
|
2759
|
+
});
|
|
2760
|
+
};
|
|
2761
|
+
/**
|
|
2762
|
+
* Delete a context
|
|
2763
|
+
*/
|
|
2764
|
+
delete = async (params) => {
|
|
2765
|
+
const id = params?.contextId || this._contextId;
|
|
2766
|
+
if (!id) {
|
|
2767
|
+
throw new Error(
|
|
2768
|
+
"context_id is required. Either pass it as parameter or create a context first."
|
|
2769
|
+
);
|
|
2770
|
+
}
|
|
2771
|
+
const result = await this.sandbox.dataApi.deleteContext({ contextId: id });
|
|
2772
|
+
this._contextId = void 0;
|
|
2773
|
+
return result;
|
|
2774
|
+
};
|
|
2775
|
+
};
|
|
2776
|
+
exports.AioSandbox = class extends exports.Sandbox {
|
|
2777
|
+
static templateType = "AllInOne" /* AIO */;
|
|
2778
|
+
/**
|
|
2779
|
+
* Create an AIO Sandbox from template
|
|
2780
|
+
* 从模板创建 AIO 沙箱 / Create AIO Sandbox from Template
|
|
2781
|
+
*/
|
|
2782
|
+
static async createFromTemplate(templateName, options, config2) {
|
|
2783
|
+
const sandbox = await exports.Sandbox.create({
|
|
2784
|
+
input: {
|
|
2785
|
+
templateName,
|
|
2786
|
+
sandboxIdleTimeoutSeconds: options?.sandboxIdleTimeoutSeconds,
|
|
2787
|
+
nasConfig: options?.nasConfig,
|
|
2788
|
+
ossMountConfig: options?.ossMountConfig,
|
|
2789
|
+
polarFsConfig: options?.polarFsConfig
|
|
2790
|
+
},
|
|
2791
|
+
templateType: "AllInOne" /* AIO */,
|
|
2792
|
+
config: config2
|
|
2793
|
+
});
|
|
2794
|
+
return sandbox;
|
|
2795
|
+
}
|
|
2796
|
+
constructor(sandbox, config2) {
|
|
2797
|
+
super(sandbox, config2);
|
|
2798
|
+
}
|
|
2799
|
+
_dataApi;
|
|
2800
|
+
_file;
|
|
2801
|
+
_fileSystem;
|
|
2802
|
+
_context;
|
|
2803
|
+
_process;
|
|
2804
|
+
/**
|
|
2805
|
+
* Get data API client
|
|
2806
|
+
*/
|
|
2807
|
+
get dataApi() {
|
|
2808
|
+
if (!this._dataApi) {
|
|
2809
|
+
this._dataApi = new exports.AioDataAPI({
|
|
2810
|
+
sandboxId: this.sandboxId || "",
|
|
2811
|
+
config: this._config
|
|
2812
|
+
});
|
|
2813
|
+
}
|
|
2814
|
+
return this._dataApi;
|
|
2815
|
+
}
|
|
2816
|
+
/**
|
|
2817
|
+
* Access file upload/download operations
|
|
2818
|
+
*/
|
|
2819
|
+
get file() {
|
|
2820
|
+
if (!this._file) {
|
|
2821
|
+
this._file = new AioFileOperations(this);
|
|
2822
|
+
}
|
|
2823
|
+
return this._file;
|
|
2824
|
+
}
|
|
2825
|
+
/**
|
|
2826
|
+
* Access file system operations
|
|
2827
|
+
*/
|
|
2828
|
+
get fileSystem() {
|
|
2829
|
+
if (!this._fileSystem) {
|
|
2830
|
+
this._fileSystem = new AioFileSystemOperations(this);
|
|
2831
|
+
}
|
|
2832
|
+
return this._fileSystem;
|
|
2833
|
+
}
|
|
2834
|
+
/**
|
|
2835
|
+
* Access context management operations
|
|
2836
|
+
*/
|
|
2837
|
+
get context() {
|
|
2838
|
+
if (!this._context) {
|
|
2839
|
+
this._context = new AioContextOperations(this);
|
|
2840
|
+
}
|
|
2841
|
+
return this._context;
|
|
2842
|
+
}
|
|
2843
|
+
/**
|
|
2844
|
+
* Access process management operations
|
|
2845
|
+
*/
|
|
2846
|
+
get process() {
|
|
2847
|
+
if (!this._process) {
|
|
2848
|
+
this._process = new AioProcessOperations(this);
|
|
2849
|
+
}
|
|
2850
|
+
return this._process;
|
|
2851
|
+
}
|
|
2852
|
+
/**
|
|
2853
|
+
* Check sandbox health
|
|
2854
|
+
*/
|
|
2855
|
+
checkHealth = async (params) => {
|
|
2856
|
+
return this.dataApi.checkHealth({
|
|
2857
|
+
sandboxId: this.sandboxId,
|
|
2858
|
+
config: params?.config
|
|
2859
|
+
});
|
|
2860
|
+
};
|
|
2861
|
+
// ========================================
|
|
2862
|
+
// Browser API Methods
|
|
2863
|
+
// ========================================
|
|
2864
|
+
/**
|
|
2865
|
+
* Get CDP WebSocket URL for browser automation
|
|
2866
|
+
*/
|
|
2867
|
+
getCdpUrl(record) {
|
|
2868
|
+
return this.dataApi.getCdpUrl(record);
|
|
2869
|
+
}
|
|
2870
|
+
/**
|
|
2871
|
+
* Get VNC WebSocket URL for live view
|
|
2872
|
+
*/
|
|
2873
|
+
getVncUrl(record) {
|
|
2874
|
+
return this.dataApi.getVncUrl(record);
|
|
2875
|
+
}
|
|
2876
|
+
/**
|
|
2877
|
+
* List all browser recordings
|
|
2878
|
+
*/
|
|
2879
|
+
listRecordings = async (params) => {
|
|
2880
|
+
return this.dataApi.listRecordings(params);
|
|
2881
|
+
};
|
|
2882
|
+
/**
|
|
2883
|
+
* Download a recording video file
|
|
2884
|
+
*/
|
|
2885
|
+
downloadRecording = async (params) => {
|
|
2886
|
+
return this.dataApi.downloadRecording(params);
|
|
2887
|
+
};
|
|
2888
|
+
/**
|
|
2889
|
+
* Delete a recording
|
|
2890
|
+
*/
|
|
2891
|
+
deleteRecording = async (params) => {
|
|
2892
|
+
return this.dataApi.deleteRecording(params);
|
|
2893
|
+
};
|
|
2894
|
+
// ========================================
|
|
2895
|
+
// Code Interpreter API Methods
|
|
2896
|
+
// ========================================
|
|
2897
|
+
/**
|
|
2898
|
+
* Execute code (convenience method delegating to context.execute)
|
|
2899
|
+
*/
|
|
2900
|
+
execute = async (params) => {
|
|
2901
|
+
return this.context.execute(params);
|
|
2902
|
+
};
|
|
2903
|
+
};
|
|
2904
|
+
}
|
|
2905
|
+
});
|
|
2906
|
+
|
|
2907
|
+
// src/sandbox/template.ts
|
|
2908
|
+
exports.Template = void 0;
|
|
2909
|
+
var init_template = __esm({
|
|
2910
|
+
"src/sandbox/template.ts"() {
|
|
2911
|
+
init_resource();
|
|
2912
|
+
exports.Template = class _Template extends ResourceBase {
|
|
2913
|
+
/**
|
|
2914
|
+
* 模板 ARN / Template ARN
|
|
2915
|
+
*/
|
|
2916
|
+
templateArn;
|
|
2917
|
+
/**
|
|
2918
|
+
* 模板 ID / Template ID
|
|
2919
|
+
*/
|
|
2920
|
+
templateId;
|
|
2921
|
+
/**
|
|
2922
|
+
* 模板名称 / Template Name
|
|
2923
|
+
*/
|
|
2924
|
+
templateName;
|
|
2925
|
+
/**
|
|
2926
|
+
* 模板类型 / Template Type
|
|
2927
|
+
*/
|
|
2928
|
+
templateType;
|
|
2929
|
+
/**
|
|
2930
|
+
* CPU 核数 / CPU Cores
|
|
2931
|
+
*/
|
|
2932
|
+
cpu;
|
|
2933
|
+
/**
|
|
2934
|
+
* 内存大小(MB) / Memory Size (MB)
|
|
2935
|
+
*/
|
|
2936
|
+
memory;
|
|
2937
|
+
/**
|
|
2938
|
+
* 创建时间 / Creation Time
|
|
2939
|
+
*/
|
|
2940
|
+
createdAt;
|
|
2941
|
+
/**
|
|
2942
|
+
* 描述 / Description
|
|
2943
|
+
*/
|
|
2944
|
+
description;
|
|
2945
|
+
/**
|
|
2946
|
+
* 执行角色 ARN / Execution Role ARN
|
|
2947
|
+
*/
|
|
2948
|
+
executionRoleArn;
|
|
2949
|
+
/**
|
|
2950
|
+
* 最后更新时间 / Last Updated Time
|
|
2951
|
+
*/
|
|
2952
|
+
lastUpdatedAt;
|
|
2953
|
+
/**
|
|
2954
|
+
* 资源名称 / Resource Name
|
|
2955
|
+
*/
|
|
2956
|
+
resourceName;
|
|
2957
|
+
/**
|
|
2958
|
+
* 沙箱空闲超时时间(秒) / Sandbox Idle Timeout (seconds)
|
|
2959
|
+
*/
|
|
2960
|
+
sandboxIdleTimeoutInSeconds;
|
|
2961
|
+
/**
|
|
2962
|
+
* 沙箱存活时间(秒) / Sandbox TTL (seconds)
|
|
2963
|
+
*/
|
|
2964
|
+
sandboxTtlInSeconds;
|
|
2965
|
+
/**
|
|
2966
|
+
* 每个沙箱的最大并发会话数 / Max Concurrency Limit Per Sandbox
|
|
2967
|
+
*/
|
|
2968
|
+
shareConcurrencyLimitPerSandbox;
|
|
2969
|
+
/**
|
|
2970
|
+
* 状态原因 / Status Reason
|
|
2971
|
+
*/
|
|
2972
|
+
statusReason;
|
|
2973
|
+
/**
|
|
2974
|
+
* 磁盘大小(GB) / Disk Size (GB)
|
|
2975
|
+
*/
|
|
2976
|
+
diskSize;
|
|
2977
|
+
/**
|
|
2978
|
+
* 是否允许匿名管理 / Whether to allow anonymous management
|
|
2979
|
+
*/
|
|
2980
|
+
allowAnonymousManage;
|
|
2981
|
+
_config;
|
|
2982
|
+
constructor(data, config2) {
|
|
2983
|
+
super();
|
|
2984
|
+
if (data) {
|
|
2985
|
+
updateObjectProperties(this, data);
|
|
2986
|
+
this.normalizeNumericFields();
|
|
2987
|
+
}
|
|
2988
|
+
this._config = config2;
|
|
2989
|
+
}
|
|
2990
|
+
uniqIdCallback = () => this.templateId;
|
|
2991
|
+
static getClient() {
|
|
2992
|
+
const { SandboxClient: SandboxClient2 } = (init_client(), __toCommonJS(client_exports));
|
|
2993
|
+
return new SandboxClient2();
|
|
2994
|
+
}
|
|
2995
|
+
/**
|
|
2996
|
+
* Create a new Template
|
|
2997
|
+
*/
|
|
2998
|
+
static async create(params) {
|
|
2999
|
+
return await _Template.getClient().createTemplate(params);
|
|
3000
|
+
}
|
|
3001
|
+
/**
|
|
3002
|
+
* Delete a Template by name
|
|
3003
|
+
*/
|
|
3004
|
+
static async delete(params) {
|
|
3005
|
+
return await _Template.getClient().deleteTemplate(params);
|
|
3006
|
+
}
|
|
3007
|
+
/**
|
|
3008
|
+
* Update a Template by name
|
|
3009
|
+
*/
|
|
3010
|
+
static async update(params) {
|
|
3011
|
+
return await _Template.getClient().updateTemplate(params);
|
|
3012
|
+
}
|
|
3013
|
+
/**
|
|
3014
|
+
* Get a Template by name
|
|
3015
|
+
*/
|
|
3016
|
+
static async get(params) {
|
|
3017
|
+
return await _Template.getClient().getTemplate(params);
|
|
3018
|
+
}
|
|
3019
|
+
/**
|
|
3020
|
+
* List Templates
|
|
3021
|
+
*/
|
|
3022
|
+
static async list(params) {
|
|
3023
|
+
return await _Template.getClient().listTemplates(params);
|
|
3024
|
+
}
|
|
3025
|
+
/**
|
|
3026
|
+
* List all Templates (with pagination)
|
|
3027
|
+
*/
|
|
3028
|
+
static listAll = listAllResourcesFunction(this.list);
|
|
3029
|
+
get = async (params = {}) => {
|
|
3030
|
+
return await _Template.get({
|
|
3031
|
+
name: this.templateName,
|
|
3032
|
+
config: params.config
|
|
3033
|
+
});
|
|
3034
|
+
};
|
|
3035
|
+
/**
|
|
3036
|
+
* Delete this template
|
|
3037
|
+
*/
|
|
3038
|
+
delete = async (params) => {
|
|
3039
|
+
const config2 = params?.config;
|
|
3040
|
+
if (!this.templateName) {
|
|
3041
|
+
throw new Error("templateName is required to delete a Template");
|
|
3042
|
+
}
|
|
3043
|
+
const result = await _Template.delete({
|
|
3044
|
+
name: this.templateName,
|
|
3045
|
+
config: config2 ?? this._config
|
|
3046
|
+
});
|
|
3047
|
+
updateObjectProperties(this, result);
|
|
3048
|
+
this.normalizeNumericFields();
|
|
3049
|
+
return this;
|
|
3050
|
+
};
|
|
3051
|
+
/**
|
|
3052
|
+
* Update this template
|
|
3053
|
+
*/
|
|
3054
|
+
update = async (params) => {
|
|
3055
|
+
const { input, config: config2 } = params;
|
|
3056
|
+
if (!this.templateName) {
|
|
3057
|
+
throw new Error("templateName is required to update a Template");
|
|
3058
|
+
}
|
|
3059
|
+
const result = await _Template.update({
|
|
3060
|
+
name: this.templateName,
|
|
3061
|
+
input,
|
|
3062
|
+
config: config2 ?? this._config
|
|
3063
|
+
});
|
|
3064
|
+
updateObjectProperties(this, result);
|
|
3065
|
+
this.normalizeNumericFields();
|
|
3066
|
+
return this;
|
|
3067
|
+
};
|
|
3068
|
+
/**
|
|
3069
|
+
* Refresh this template's data
|
|
3070
|
+
*/
|
|
3071
|
+
refresh = async (params) => {
|
|
3072
|
+
const config2 = params?.config;
|
|
3073
|
+
if (!this.templateName) {
|
|
3074
|
+
throw new Error("templateName is required to refresh a Template");
|
|
3075
|
+
}
|
|
3076
|
+
const result = await _Template.get({
|
|
3077
|
+
name: this.templateName,
|
|
3078
|
+
config: config2 ?? this._config
|
|
3079
|
+
});
|
|
3080
|
+
updateObjectProperties(this, result);
|
|
3081
|
+
this.normalizeNumericFields();
|
|
3082
|
+
return this;
|
|
3083
|
+
};
|
|
3084
|
+
normalizeNumericFields() {
|
|
3085
|
+
const toNumber = (value) => {
|
|
3086
|
+
if (typeof value === "number") return value;
|
|
3087
|
+
if (typeof value === "string") {
|
|
3088
|
+
const parsed = Number(value);
|
|
3089
|
+
return Number.isNaN(parsed) ? void 0 : parsed;
|
|
3090
|
+
}
|
|
3091
|
+
return void 0;
|
|
3092
|
+
};
|
|
3093
|
+
this.sandboxIdleTimeoutInSeconds = toNumber(this.sandboxIdleTimeoutInSeconds);
|
|
3094
|
+
this.sandboxTtlInSeconds = toNumber(this.sandboxTtlInSeconds);
|
|
3095
|
+
this.shareConcurrencyLimitPerSandbox = toNumber(this.shareConcurrencyLimitPerSandbox);
|
|
3096
|
+
this.cpu = toNumber(this.cpu);
|
|
3097
|
+
this.memory = toNumber(this.memory);
|
|
3098
|
+
this.diskSize = toNumber(this.diskSize);
|
|
3099
|
+
}
|
|
3100
|
+
};
|
|
3101
|
+
}
|
|
3102
|
+
});
|
|
3103
|
+
|
|
3104
|
+
// src/sandbox/client.ts
|
|
3105
|
+
var client_exports = {};
|
|
3106
|
+
__export(client_exports, {
|
|
3107
|
+
SandboxClient: () => exports.SandboxClient
|
|
3108
|
+
});
|
|
3109
|
+
exports.SandboxClient = void 0;
|
|
3110
|
+
var init_client = __esm({
|
|
3111
|
+
"src/sandbox/client.ts"() {
|
|
3112
|
+
init_config();
|
|
3113
|
+
init_exception();
|
|
3114
|
+
init_log();
|
|
3115
|
+
init_control();
|
|
3116
|
+
init_aio_sandbox();
|
|
3117
|
+
init_browser_sandbox();
|
|
3118
|
+
init_code_interpreter_sandbox();
|
|
3119
|
+
init_custom_sandbox();
|
|
3120
|
+
init_model2();
|
|
3121
|
+
init_sandbox();
|
|
3122
|
+
init_template();
|
|
3123
|
+
exports.SandboxClient = class {
|
|
3124
|
+
config;
|
|
3125
|
+
controlApi;
|
|
3126
|
+
constructor(config2) {
|
|
3127
|
+
this.config = config2;
|
|
3128
|
+
this.controlApi = new SandboxControlAPI(config2);
|
|
3129
|
+
}
|
|
3130
|
+
// ============ Template Operations ============
|
|
3131
|
+
/**
|
|
3132
|
+
* Create a Template
|
|
3133
|
+
*/
|
|
3134
|
+
createTemplate = async (arg1, arg2) => {
|
|
3135
|
+
let input;
|
|
3136
|
+
let config2;
|
|
3137
|
+
if ("input" in arg1) {
|
|
3138
|
+
input = arg1.input;
|
|
3139
|
+
config2 = arg1.config;
|
|
3140
|
+
} else {
|
|
3141
|
+
logger.warn(
|
|
3142
|
+
"Deprecated: createTemplate(input, config) is deprecated. Use createTemplate({ input, config }) instead."
|
|
3143
|
+
);
|
|
3144
|
+
input = arg1;
|
|
3145
|
+
config2 = arg2;
|
|
3146
|
+
}
|
|
3147
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3148
|
+
try {
|
|
3149
|
+
const finalInput = this.prepareTemplateCreateInput(input);
|
|
3150
|
+
const result = await this.controlApi.createTemplate({
|
|
3151
|
+
input: new $AgentRun4__namespace.CreateTemplateInput({
|
|
3152
|
+
...finalInput,
|
|
3153
|
+
networkConfiguration: finalInput.networkConfiguration ? new $AgentRun4__namespace.NetworkConfiguration({
|
|
3154
|
+
...finalInput.networkConfiguration
|
|
3155
|
+
}) : void 0
|
|
3156
|
+
}),
|
|
3157
|
+
config: cfg
|
|
3158
|
+
});
|
|
3159
|
+
return new exports.Template(result, cfg);
|
|
3160
|
+
} catch (error) {
|
|
3161
|
+
if (error instanceof HTTPError) {
|
|
3162
|
+
throw error.toResourceError("Template", input.templateName);
|
|
3163
|
+
}
|
|
3164
|
+
throw error;
|
|
3165
|
+
}
|
|
3166
|
+
};
|
|
3167
|
+
/**
|
|
3168
|
+
* Delete a Template
|
|
3169
|
+
*/
|
|
3170
|
+
deleteTemplate = async (arg1, arg2) => {
|
|
3171
|
+
let name;
|
|
3172
|
+
let config2;
|
|
3173
|
+
if (typeof arg1 === "string") {
|
|
3174
|
+
logger.warn(
|
|
3175
|
+
"Deprecated: deleteTemplate(name, config) is deprecated. Use deleteTemplate({ name, config }) instead."
|
|
3176
|
+
);
|
|
3177
|
+
name = arg1;
|
|
3178
|
+
config2 = arg2;
|
|
3179
|
+
} else {
|
|
3180
|
+
name = arg1.name;
|
|
3181
|
+
config2 = arg1.config;
|
|
3182
|
+
}
|
|
3183
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3184
|
+
try {
|
|
3185
|
+
const result = await this.controlApi.deleteTemplate({
|
|
3186
|
+
templateName: name,
|
|
3187
|
+
config: cfg
|
|
3188
|
+
});
|
|
3189
|
+
return new exports.Template(result, cfg);
|
|
3190
|
+
} catch (error) {
|
|
3191
|
+
if (error instanceof HTTPError) {
|
|
3192
|
+
throw error.toResourceError("Template", name);
|
|
3193
|
+
}
|
|
3194
|
+
throw error;
|
|
3195
|
+
}
|
|
3196
|
+
};
|
|
3197
|
+
/**
|
|
3198
|
+
* Update a Template
|
|
3199
|
+
*/
|
|
3200
|
+
updateTemplate = async (arg1, arg2, arg3) => {
|
|
3201
|
+
let name;
|
|
3202
|
+
let input;
|
|
3203
|
+
let config2;
|
|
3204
|
+
if (typeof arg1 === "string") {
|
|
3205
|
+
logger.warn(
|
|
3206
|
+
"Deprecated: updateTemplate(name, input, config) is deprecated. Use updateTemplate({ name, input, config }) instead."
|
|
3207
|
+
);
|
|
3208
|
+
name = arg1;
|
|
3209
|
+
input = arg2;
|
|
3210
|
+
config2 = arg3;
|
|
3211
|
+
} else {
|
|
3212
|
+
name = arg1.name;
|
|
3213
|
+
input = arg1.input;
|
|
3214
|
+
config2 = arg1.config;
|
|
3215
|
+
}
|
|
3216
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3217
|
+
try {
|
|
3218
|
+
const result = await this.controlApi.updateTemplate({
|
|
3219
|
+
templateName: name,
|
|
3220
|
+
input: new $AgentRun4__namespace.UpdateTemplateInput({
|
|
3221
|
+
...input,
|
|
3222
|
+
networkConfiguration: input.networkConfiguration ? new $AgentRun4__namespace.NetworkConfiguration({
|
|
3223
|
+
...input.networkConfiguration
|
|
3224
|
+
}) : void 0
|
|
3225
|
+
}),
|
|
3226
|
+
config: cfg
|
|
3227
|
+
});
|
|
3228
|
+
return new exports.Template(result, cfg);
|
|
3229
|
+
} catch (error) {
|
|
3230
|
+
if (error instanceof HTTPError) {
|
|
3231
|
+
throw error.toResourceError("Template", name);
|
|
3232
|
+
}
|
|
3233
|
+
throw error;
|
|
3234
|
+
}
|
|
3235
|
+
};
|
|
3236
|
+
/**
|
|
3237
|
+
* Get a Template
|
|
3238
|
+
*/
|
|
3239
|
+
getTemplate = async (arg1, arg2) => {
|
|
3240
|
+
let name;
|
|
3241
|
+
let config2;
|
|
3242
|
+
if (typeof arg1 === "string") {
|
|
3243
|
+
logger.warn(
|
|
3244
|
+
"Deprecated: getTemplate(name, config) is deprecated. Use getTemplate({ name, config }) instead."
|
|
3245
|
+
);
|
|
3246
|
+
name = arg1;
|
|
3247
|
+
config2 = arg2;
|
|
3248
|
+
} else {
|
|
3249
|
+
name = arg1.name;
|
|
3250
|
+
config2 = arg1.config;
|
|
3251
|
+
}
|
|
3252
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3253
|
+
try {
|
|
3254
|
+
const result = await this.controlApi.getTemplate({
|
|
3255
|
+
templateName: name,
|
|
3256
|
+
config: cfg
|
|
3257
|
+
});
|
|
3258
|
+
return new exports.Template(result, cfg);
|
|
3259
|
+
} catch (error) {
|
|
3260
|
+
if (error instanceof HTTPError) {
|
|
3261
|
+
throw error.toResourceError("Template", name);
|
|
3262
|
+
}
|
|
3263
|
+
throw error;
|
|
3264
|
+
}
|
|
3265
|
+
};
|
|
3266
|
+
/**
|
|
3267
|
+
* List Templates
|
|
3268
|
+
*/
|
|
3269
|
+
listTemplates = async (arg1, arg2) => {
|
|
3270
|
+
let input;
|
|
3271
|
+
let config2;
|
|
3272
|
+
if (arg2 || arg1 && ("pageNumber" in arg1 || "pageSize" in arg1 || "templateType" in arg1)) {
|
|
3273
|
+
logger.warn(
|
|
3274
|
+
"Deprecated: listTemplates(input, config) is deprecated. Use listTemplates({ input, config }) instead."
|
|
3275
|
+
);
|
|
3276
|
+
input = arg1;
|
|
3277
|
+
config2 = arg2;
|
|
3278
|
+
} else {
|
|
3279
|
+
const params = arg1 ?? {};
|
|
3280
|
+
input = params.input;
|
|
3281
|
+
config2 = params.config;
|
|
3282
|
+
}
|
|
3283
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3284
|
+
const request = new $AgentRun4__namespace.ListTemplatesRequest({
|
|
3285
|
+
...input
|
|
3286
|
+
});
|
|
3287
|
+
const result = await this.controlApi.listTemplates({
|
|
3288
|
+
input: request,
|
|
3289
|
+
config: cfg
|
|
3290
|
+
});
|
|
3291
|
+
return (result?.items || []).map((item) => new exports.Template(item, cfg));
|
|
3292
|
+
};
|
|
3293
|
+
// ============ Sandbox Operations ============
|
|
3294
|
+
/**
|
|
3295
|
+
* Create a Sandbox
|
|
3296
|
+
*/
|
|
3297
|
+
createSandbox = async (arg1, arg2) => {
|
|
3298
|
+
let input;
|
|
3299
|
+
let templateType;
|
|
3300
|
+
let config2;
|
|
3301
|
+
if ("input" in arg1) {
|
|
3302
|
+
const params = arg1;
|
|
3303
|
+
input = params.input;
|
|
3304
|
+
templateType = params.templateType;
|
|
3305
|
+
config2 = params.config;
|
|
3306
|
+
} else {
|
|
3307
|
+
logger.warn(
|
|
3308
|
+
"Deprecated: createSandbox(input, config) is deprecated. Use createSandbox({ input, config }) instead."
|
|
3309
|
+
);
|
|
3310
|
+
input = arg1;
|
|
3311
|
+
config2 = arg2;
|
|
3312
|
+
}
|
|
3313
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3314
|
+
try {
|
|
3315
|
+
const result = await this.controlApi.createSandbox({
|
|
3316
|
+
input: new $AgentRun4__namespace.CreateSandboxInput({
|
|
3317
|
+
...input
|
|
3318
|
+
}),
|
|
3319
|
+
config: cfg
|
|
3320
|
+
});
|
|
3321
|
+
const state = result.status ?? result.state;
|
|
3322
|
+
const sb = new exports.Sandbox({ ...result, state }, cfg);
|
|
3323
|
+
if (templateType === "CodeInterpreter" /* CODE_INTERPRETER */)
|
|
3324
|
+
return new exports.CodeInterpreterSandbox(sb, cfg);
|
|
3325
|
+
else if (templateType === "Browser" /* BROWSER */) return new exports.BrowserSandbox(sb, cfg);
|
|
3326
|
+
else if (templateType === "AllInOne" /* AIO */) return new exports.AioSandbox(sb, cfg);
|
|
3327
|
+
else if (templateType === "CustomImage" /* CUSTOM */) return new exports.CustomSandbox(sb, cfg);
|
|
3328
|
+
return sb;
|
|
3329
|
+
} catch (error) {
|
|
3330
|
+
if (error instanceof HTTPError) {
|
|
3331
|
+
throw error.toResourceError("Sandbox", input.templateName);
|
|
3332
|
+
}
|
|
3333
|
+
throw error;
|
|
3334
|
+
}
|
|
3335
|
+
};
|
|
3336
|
+
/**
|
|
3337
|
+
* Create a Code Interpreter Sandbox
|
|
3338
|
+
* 创建代码解释器沙箱 / Create Code Interpreter Sandbox
|
|
3339
|
+
*/
|
|
3340
|
+
createCodeInterpreterSandbox = async (params) => {
|
|
3341
|
+
const { templateName, options, config: config2 } = params;
|
|
3342
|
+
return exports.CodeInterpreterSandbox.createFromTemplate(templateName, options, config2 ?? this.config);
|
|
3343
|
+
};
|
|
3344
|
+
/**
|
|
3345
|
+
* Create a Browser Sandbox
|
|
3346
|
+
* 创建浏览器沙箱 / Create Browser Sandbox
|
|
3347
|
+
*/
|
|
3348
|
+
createBrowserSandbox = async (params) => {
|
|
3349
|
+
const { templateName, options, config: config2 } = params;
|
|
3350
|
+
return exports.BrowserSandbox.createFromTemplate(templateName, options, config2 ?? this.config);
|
|
3351
|
+
};
|
|
3352
|
+
/**
|
|
3353
|
+
* Delete a Sandbox
|
|
3354
|
+
*/
|
|
3355
|
+
deleteSandbox = async (arg1, arg2) => {
|
|
3356
|
+
let id;
|
|
3357
|
+
let config2;
|
|
3358
|
+
if (typeof arg1 === "string") {
|
|
3359
|
+
logger.warn(
|
|
3360
|
+
"Deprecated: deleteSandbox(id, config) is deprecated. Use deleteSandbox({ id, config }) instead."
|
|
3361
|
+
);
|
|
3362
|
+
id = arg1;
|
|
3363
|
+
config2 = arg2;
|
|
3364
|
+
} else {
|
|
3365
|
+
id = arg1.id;
|
|
3366
|
+
config2 = arg1.config;
|
|
3367
|
+
}
|
|
3368
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3369
|
+
try {
|
|
3370
|
+
const result = await this.controlApi.deleteSandbox({
|
|
3371
|
+
sandboxId: id,
|
|
3372
|
+
config: cfg
|
|
3373
|
+
});
|
|
3374
|
+
return exports.Sandbox.fromInnerObject(result, cfg);
|
|
3375
|
+
} catch (error) {
|
|
3376
|
+
if (error instanceof HTTPError) {
|
|
3377
|
+
throw error.toResourceError("Sandbox", id);
|
|
3378
|
+
}
|
|
3379
|
+
throw error;
|
|
3380
|
+
}
|
|
3381
|
+
};
|
|
3382
|
+
/**
|
|
3383
|
+
* Stop a Sandbox
|
|
3384
|
+
*/
|
|
3385
|
+
stopSandbox = async (arg1, arg2) => {
|
|
3386
|
+
let id;
|
|
3387
|
+
let config2;
|
|
3388
|
+
if (typeof arg1 === "string") {
|
|
3389
|
+
logger.warn(
|
|
3390
|
+
"Deprecated: stopSandbox(id, config) is deprecated. Use stopSandbox({ id, config }) instead."
|
|
3391
|
+
);
|
|
3392
|
+
id = arg1;
|
|
3393
|
+
config2 = arg2;
|
|
3394
|
+
} else {
|
|
3395
|
+
id = arg1.id;
|
|
3396
|
+
config2 = arg1.config;
|
|
3397
|
+
}
|
|
3398
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3399
|
+
try {
|
|
3400
|
+
const result = await this.controlApi.stopSandbox({
|
|
3401
|
+
sandboxId: id,
|
|
3402
|
+
config: cfg
|
|
3403
|
+
});
|
|
3404
|
+
return exports.Sandbox.fromInnerObject(result, cfg);
|
|
3405
|
+
} catch (error) {
|
|
3406
|
+
if (error instanceof HTTPError) {
|
|
3407
|
+
throw error.toResourceError("Sandbox", id);
|
|
3408
|
+
}
|
|
3409
|
+
throw error;
|
|
3410
|
+
}
|
|
3411
|
+
};
|
|
3412
|
+
/**
|
|
3413
|
+
* Get a Sandbox
|
|
3414
|
+
*
|
|
3415
|
+
* @param params.id - Sandbox ID
|
|
3416
|
+
* @param params.templateType - Template type to cast the result to the appropriate subclass
|
|
3417
|
+
* @param params.config - Configuration
|
|
3418
|
+
*/
|
|
3419
|
+
getSandbox = async (arg1, arg2, arg3) => {
|
|
3420
|
+
let id;
|
|
3421
|
+
let templateType;
|
|
3422
|
+
let config2;
|
|
3423
|
+
if (typeof arg1 === "string") {
|
|
3424
|
+
logger.warn(
|
|
3425
|
+
"Deprecated: getSandbox(id, templateType, config?) is deprecated. Use getSandbox({ id, templateType, config }) instead."
|
|
3426
|
+
);
|
|
3427
|
+
id = arg1;
|
|
3428
|
+
if (arg2 && typeof arg2 === "string") {
|
|
3429
|
+
templateType = arg2;
|
|
3430
|
+
config2 = arg3;
|
|
3431
|
+
} else {
|
|
3432
|
+
config2 = arg2;
|
|
3433
|
+
}
|
|
3434
|
+
} else {
|
|
3435
|
+
id = arg1.id;
|
|
3436
|
+
templateType = arg1.templateType;
|
|
3437
|
+
config2 = arg1.config;
|
|
3438
|
+
}
|
|
3439
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3440
|
+
try {
|
|
3441
|
+
const result = await this.controlApi.getSandbox({
|
|
3442
|
+
sandboxId: id,
|
|
3443
|
+
config: cfg
|
|
3444
|
+
});
|
|
3445
|
+
const baseSandbox = exports.Sandbox.fromInnerObject(result, cfg);
|
|
3446
|
+
if (templateType) {
|
|
3447
|
+
switch (templateType) {
|
|
3448
|
+
case "CodeInterpreter" /* CODE_INTERPRETER */: {
|
|
3449
|
+
return new exports.CodeInterpreterSandbox(baseSandbox, cfg);
|
|
3450
|
+
}
|
|
3451
|
+
case "Browser" /* BROWSER */: {
|
|
3452
|
+
return new exports.BrowserSandbox(baseSandbox, cfg);
|
|
3453
|
+
}
|
|
3454
|
+
case "AllInOne" /* AIO */: {
|
|
3455
|
+
const { AioSandbox: AioSandbox2 } = await Promise.resolve().then(() => (init_aio_sandbox(), aio_sandbox_exports));
|
|
3456
|
+
return new AioSandbox2(baseSandbox, cfg);
|
|
3457
|
+
}
|
|
3458
|
+
}
|
|
3459
|
+
}
|
|
3460
|
+
return baseSandbox;
|
|
3461
|
+
} catch (error) {
|
|
3462
|
+
if (error instanceof HTTPError) {
|
|
3463
|
+
throw error.toResourceError("Sandbox", id);
|
|
3464
|
+
}
|
|
3465
|
+
throw error;
|
|
3466
|
+
}
|
|
3467
|
+
};
|
|
3468
|
+
/**
|
|
3469
|
+
* List Sandboxes
|
|
3470
|
+
*/
|
|
3471
|
+
listSandboxes = async (arg1, arg2) => {
|
|
3472
|
+
let input;
|
|
3473
|
+
let config2;
|
|
3474
|
+
if (arg2 || arg1 && ("maxResults" in arg1 || "nextToken" in arg1 || "status" in arg1 || "templateName" in arg1 || "templateType" in arg1)) {
|
|
3475
|
+
logger.warn(
|
|
3476
|
+
"Deprecated: listSandboxes(input, config) is deprecated. Use listSandboxes({ input, config }) instead."
|
|
3477
|
+
);
|
|
3478
|
+
input = arg1;
|
|
3479
|
+
config2 = arg2;
|
|
3480
|
+
} else {
|
|
3481
|
+
const params = arg1 ?? {};
|
|
3482
|
+
input = params.input;
|
|
3483
|
+
config2 = params.config;
|
|
3484
|
+
}
|
|
3485
|
+
const cfg = Config.withConfigs(this.config, config2);
|
|
3486
|
+
const request = new $AgentRun4__namespace.ListSandboxesRequest({
|
|
3487
|
+
...input
|
|
3488
|
+
});
|
|
3489
|
+
const result = await this.controlApi.listSandboxes({
|
|
3490
|
+
input: request,
|
|
3491
|
+
config: cfg
|
|
3492
|
+
});
|
|
3493
|
+
return (result.sandboxes || []).map(
|
|
3494
|
+
(item) => exports.Sandbox.fromInnerObject(item, cfg)
|
|
3495
|
+
);
|
|
3496
|
+
};
|
|
3497
|
+
prepareTemplateCreateInput(input) {
|
|
3498
|
+
const defaults = this.getTemplateDefaults(input.templateType);
|
|
3499
|
+
const finalInput = { ...defaults, ...input };
|
|
3500
|
+
if (!finalInput.networkConfiguration) {
|
|
3501
|
+
finalInput.networkConfiguration = {
|
|
3502
|
+
networkMode: "PUBLIC" /* PUBLIC */
|
|
3503
|
+
};
|
|
3504
|
+
}
|
|
3505
|
+
this.validateTemplateCreateInput(finalInput);
|
|
3506
|
+
return finalInput;
|
|
3507
|
+
}
|
|
3508
|
+
getTemplateDefaults(templateType) {
|
|
3509
|
+
const base = {
|
|
3510
|
+
cpu: 2,
|
|
3511
|
+
memory: 4096,
|
|
3512
|
+
sandboxIdleTimeoutInSeconds: 1800,
|
|
3513
|
+
sandboxTtlInSeconds: 21600,
|
|
3514
|
+
shareConcurrencyLimitPerSandbox: 200
|
|
3515
|
+
};
|
|
3516
|
+
switch (templateType) {
|
|
3517
|
+
case "CodeInterpreter" /* CODE_INTERPRETER */:
|
|
3518
|
+
return { ...base, diskSize: 512 };
|
|
3519
|
+
case "Browser" /* BROWSER */:
|
|
3520
|
+
case "AllInOne" /* AIO */:
|
|
3521
|
+
return { ...base, cpu: 4, memory: 8192, diskSize: 10240 };
|
|
3522
|
+
default:
|
|
3523
|
+
return { ...base, diskSize: 512 };
|
|
3524
|
+
}
|
|
3525
|
+
}
|
|
3526
|
+
validateTemplateCreateInput(input) {
|
|
3527
|
+
if ((input.templateType === "Browser" /* BROWSER */ || input.templateType === "AllInOne" /* AIO */) && input.diskSize !== 10240) {
|
|
3528
|
+
throw new Error(
|
|
3529
|
+
`When templateType is BROWSER or AIO, diskSize must be 10240, got ${input.diskSize}`
|
|
3530
|
+
);
|
|
3531
|
+
}
|
|
3532
|
+
if ((input.templateType === "CodeInterpreter" /* CODE_INTERPRETER */ || input.templateType === "AllInOne" /* AIO */) && input.networkConfiguration?.networkMode === "PRIVATE" /* PRIVATE */) {
|
|
3533
|
+
throw new Error(
|
|
3534
|
+
`When templateType is CODE_INTERPRETER or AIO, networkMode cannot be PRIVATE`
|
|
3535
|
+
);
|
|
3536
|
+
}
|
|
3537
|
+
}
|
|
3538
|
+
};
|
|
3539
|
+
}
|
|
3540
|
+
});
|
|
3541
|
+
|
|
3542
|
+
// src/sandbox/index.ts
|
|
3543
|
+
init_version_check();
|
|
3544
|
+
init_client();
|
|
3545
|
+
init_sandbox();
|
|
3546
|
+
init_template();
|
|
3547
|
+
init_code_interpreter_sandbox();
|
|
3548
|
+
init_browser_sandbox();
|
|
3549
|
+
init_aio_sandbox();
|
|
3550
|
+
init_custom_sandbox();
|
|
3551
|
+
init_sandbox_data();
|
|
3552
|
+
init_code_interpreter_data();
|
|
3553
|
+
init_browser_data();
|
|
3554
|
+
init_aio_data();
|
|
3555
|
+
init_model2();
|
|
3556
|
+
//# sourceMappingURL=index.cjs.map
|
|
3557
|
+
//# sourceMappingURL=index.cjs.map
|