Skip to content

Commit 042b8e7

Browse files
committed
refactor(sandbox): migrate to object-based API parameters with backward compatibility
This commit refactors the entire sandbox API to use object-based parameter passing instead of positional arguments. All methods now accept parameters as objects with { input, config } pattern while maintaining backward compatibility through deprecation warnings for old signatures. The changes affect: - Client methods (createTemplate, deleteTemplate, etc.) - Sandbox creation and management methods - Template operations - All sandbox subclasses (AIO, Browser, CodeInterpreter, Custom) - Test suites updated for new parameter patterns Additionally, numeric field normalization is added to templates to ensure consistent data types across string and number representations. BREAKING CHANGE: Old parameter patterns are deprecated and will be removed in future versions. 此提交重构了整个沙箱 API 以使用基于对象的参数传递, 而不是位置参数。所有方法现在都接受 { input, config } 模式的对象参数, 同时通过弃用警告保留旧签名的向后兼容性。 更改影响: - 客户端方法(createTemplate、deleteTemplate 等) - 沙箱创建和管理方法 - 模板操作 - 所有沙箱子类(AIO、Browser、CodeInterpreter、Custom) - 更新测试套件以适应新的参数模式 此外,向模板添加数字字段规范化,以确保字符串和数字表示形式的数据类型一致。 重大变更:旧参数模式已被弃用,并将在未来版本中移除。 Change-Id: I73e043e97ae2c8363a0f1172eed14da27a777e7c Signed-off-by: OhYee <oyohyee@oyohyee.com>
1 parent 91f37f5 commit 042b8e7

13 files changed

Lines changed: 763 additions & 153 deletions

examples/sandbox.ts

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const client = new SandboxClient();
3939
async function listTemplates(): Promise<void> {
4040
log('枚举模板列表 / Listing templates');
4141

42-
const templates = await client.listAllTemplates();
42+
const templates = await client.listTemplates();
4343
log(`共有 ${templates.length} 个模板 / Total ${templates.length} templates:`);
4444

4545
for (const template of templates) {
@@ -76,10 +76,12 @@ async function codeInterpreterExample(): Promise<void> {
7676
// 创建模板 / Create template
7777
log('\n--- 创建模板 / Creating template ---');
7878
const template = await Template.create({
79-
templateName,
80-
templateType: TemplateType.CODE_INTERPRETER,
81-
description: 'Test template from Node.js SDK',
82-
sandboxIdleTimeoutInSeconds: 600,
79+
input: {
80+
templateName,
81+
templateType: TemplateType.CODE_INTERPRETER,
82+
description: 'Test template from Node.js SDK',
83+
sandboxIdleTimeoutInSeconds: 600,
84+
}
8385
});
8486

8587
log(`✓ 创建模板成功 / Template created: ${template.templateName}`);
@@ -158,9 +160,9 @@ async function codeInterpreterExample(): Promise<void> {
158160
});
159161
log(`✓ 上传文件成功 / File uploaded successfully`);
160162

161-
const filestat = await sandbox.fileSystem.stat(
162-
'/home/user/test-move/test_file.txt'
163-
);
163+
const filestat = await sandbox.fileSystem.stat({
164+
path: '/home/user/test-move/test_file.txt'
165+
});
164166
log(`✓ 上传文件详情 / Uploaded file stat:`, filestat);
165167

166168
const downloadPath = './downloaded_test_file.txt';
@@ -187,7 +189,7 @@ async function codeInterpreterExample(): Promise<void> {
187189
});
188190
log(`✓ 写入文件成功 / File written successfully`);
189191

190-
const readResult = await sandbox.file.read('/home/user/test/test.txt');
192+
const readResult = await sandbox.file.read({ path: '/home/user/test/test.txt' });
191193
log(`✓ 读取文件结果 / File read result:`, readResult);
192194

193195
// 测试文件移动 / File move test
@@ -198,19 +200,19 @@ async function codeInterpreterExample(): Promise<void> {
198200
});
199201
log(`✓ 移动文件成功 / File moved successfully`);
200202

201-
const movedContent = await sandbox.file.read(
202-
'/home/user/test-move/test2.txt'
203-
);
203+
const movedContent = await sandbox.file.read({
204+
path: '/home/user/test-move/test2.txt'
205+
});
204206
log(`✓ 读取移动后的文件 / Read moved file:`, movedContent);
205207

206208
// 测试文件详情 / File stat test
207209
log('\n--- 测试文件详情 / Testing file stat ---');
208-
const dirStat = await sandbox.fileSystem.stat('/home/user/test-move');
210+
const dirStat = await sandbox.fileSystem.stat({ path: '/home/user/test-move' });
209211
log(`✓ 文件详情 / File stat:`, dirStat);
210212

211213
// 测试删除文件 / Delete test
212214
log('\n--- 测试删除文件 / Testing file deletion ---');
213-
await sandbox.fileSystem.remove('/home/user/test-move');
215+
await sandbox.fileSystem.remove({ path: '/home/user/test-move' });
214216
log(`✓ 删除文件夹成功 / Directory deleted successfully`);
215217

216218
// 测试进程操作 / Process operations
@@ -221,7 +223,7 @@ async function codeInterpreterExample(): Promise<void> {
221223
const cmdResult = await sandbox.process.cmd({ command: 'ls', cwd: '/' });
222224
log(`✓ 进程执行结果 / Process execution result:`, cmdResult);
223225

224-
const processDetails = await sandbox.process.get('1');
226+
const processDetails = await sandbox.process.get({ pid: '1' });
225227
log(`✓ 进程详情 / Process details:`, processDetails);
226228

227229
// 清理上下文

src/sandbox/aio-sandbox.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -311,19 +311,19 @@ export class AioSandbox extends Sandbox {
311311
},
312312
config?: Config
313313
): Promise<AioSandbox> {
314-
const sandbox = await Sandbox.create(
315-
{
314+
const sandbox = await Sandbox.create({
315+
input: {
316316
templateName,
317317
sandboxIdleTimeoutSeconds: options?.sandboxIdleTimeoutSeconds,
318318
nasConfig: options?.nasConfig,
319319
ossMountConfig: options?.ossMountConfig,
320320
polarFsConfig: options?.polarFsConfig,
321321
},
322-
config
323-
);
322+
templateType: TemplateType.AIO,
323+
config,
324+
});
324325

325-
const aioSandbox = new AioSandbox(sandbox, config);
326-
return aioSandbox;
326+
return sandbox as AioSandbox;
327327
}
328328

329329
constructor(sandbox: Sandbox, config?: Config) {

src/sandbox/browser-sandbox.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,19 +38,19 @@ export class BrowserSandbox extends Sandbox {
3838
},
3939
config?: Config
4040
): Promise<BrowserSandbox> {
41-
const sandbox = await Sandbox.create(
42-
{
41+
const sandbox = await Sandbox.create({
42+
input: {
4343
templateName,
4444
sandboxIdleTimeoutSeconds: options?.sandboxIdleTimeoutSeconds,
4545
nasConfig: options?.nasConfig,
4646
ossMountConfig: options?.ossMountConfig,
4747
polarFsConfig: options?.polarFsConfig,
4848
},
49-
config
50-
);
49+
templateType: TemplateType.BROWSER,
50+
config,
51+
});
5152

52-
const browserSandbox = new BrowserSandbox(sandbox, config);
53-
return browserSandbox;
53+
return sandbox as BrowserSandbox;
5454
}
5555

5656
constructor(sandbox: Sandbox, config?: Config) {

0 commit comments

Comments
 (0)