1313 * bun run examples/sandbox.ts
1414 */
1515
16- import * as fs from "fs/promises" ;
17- import * as path from "path" ;
18-
19- import { CodeInterpreterSandbox , CodeLanguage , Sandbox , SandboxClient , Template , TemplateType } from "../src/index" ;
20- import { logger } from "../src/utils/log" ;
16+ import * as fs from 'fs/promises' ;
17+ import * as path from 'path' ;
18+
19+ import {
20+ CodeInterpreterSandbox ,
21+ CodeLanguage ,
22+ Sandbox ,
23+ SandboxClient ,
24+ Template ,
25+ TemplateType ,
26+ } from '../src/index' ;
27+ import { logger } from '../src/utils/log' ;
2128
2229// Logger helper
2330function log ( message : string , ...args : unknown [ ] ) {
@@ -30,21 +37,23 @@ const client = new SandboxClient();
3037 * 列出模板 / List templates
3138 */
3239async function listTemplates ( ) : Promise < void > {
33- log ( " 枚举模板列表 / Listing templates" ) ;
40+ log ( ' 枚举模板列表 / Listing templates' ) ;
3441
3542 const templates = await client . listAllTemplates ( ) ;
3643 log ( `共有 ${ templates . length } 个模板 / Total ${ templates . length } templates:` ) ;
3744
3845 for ( const template of templates ) {
39- log ( ` - ${ template . templateName } (${ template . templateType } ) [${ template . status } ]` ) ;
46+ log (
47+ ` - ${ template . templateName } (${ template . templateType } ) [${ template . status } ]`
48+ ) ;
4049 }
4150}
4251
4352/**
4453 * 列出沙箱 / List sandboxes
4554 */
4655async function listSandboxes ( ) : Promise < void > {
47- log ( " 枚举沙箱列表 / Listing sandboxes" ) ;
56+ log ( ' 枚举沙箱列表 / Listing sandboxes' ) ;
4857
4958 const sandboxes = await client . listSandboxes ( ) ;
5059 log ( `共有 ${ sandboxes . length } 个沙箱 / Total ${ sandboxes . length } sandboxes:` ) ;
@@ -58,18 +67,18 @@ async function listSandboxes(): Promise<void> {
5867 * Code Interpreter 测试 / Code Interpreter test
5968 */
6069async function codeInterpreterExample ( ) : Promise < void > {
61- log ( "=" . repeat ( 60 ) ) ;
62- log ( " 开始测试 Code Interpreter / Starting Code Interpreter test" ) ;
63- log ( "=" . repeat ( 60 ) ) ;
70+ log ( '=' . repeat ( 60 ) ) ;
71+ log ( ' 开始测试 Code Interpreter / Starting Code Interpreter test' ) ;
72+ log ( '=' . repeat ( 60 ) ) ;
6473
6574 const templateName = `sdk-nodejs-template-${ Date . now ( ) } ` ;
6675
6776 // 创建模板 / Create template
68- log ( " \n--- 创建模板 / Creating template ---" ) ;
77+ log ( ' \n--- 创建模板 / Creating template ---' ) ;
6978 const template = await Template . create ( {
7079 templateName,
7180 templateType : TemplateType . CODE_INTERPRETER ,
72- description : " Test template from Node.js SDK" ,
81+ description : ' Test template from Node.js SDK' ,
7382 sandboxIdleTimeoutInSeconds : 600 ,
7483 } ) ;
7584
@@ -78,35 +87,39 @@ async function codeInterpreterExample(): Promise<void> {
7887 log ( ` - 模板状态: ${ template . status } ` ) ;
7988
8089 // 等待模板就绪 / Wait for template to be ready
81- log ( " \n--- 等待模板就绪 / Waiting for template to be ready ---" ) ;
82- await template . waitUntilReady ( {
83- beforeCheck : ( t ) => log ( ` 当前状态 / Current status: ${ t . status } ` ) ,
90+ log ( ' \n--- 等待模板就绪 / Waiting for template to be ready ---' ) ;
91+ await template . waitUntilReadyOrFailed ( {
92+ callback : ( t ) => log ( ` 当前状态 / Current status: ${ t . status } ` ) ,
8493 } ) ;
85- log ( " ✓ 模板已就绪 / Template is ready" ) ;
94+ log ( ' ✓ 模板已就绪 / Template is ready' ) ;
8695
8796 // 创建沙箱 / Create sandbox
88- log ( "\n--- 创建 Code Interpreter 沙箱 / Creating Code Interpreter sandbox ---" ) ;
97+ log (
98+ '\n--- 创建 Code Interpreter 沙箱 / Creating Code Interpreter sandbox ---'
99+ ) ;
89100 const sandbox = await CodeInterpreterSandbox . createFromTemplate ( templateName ) ;
90101 log ( `✓ 创建沙箱成功 / Sandbox created: ${ sandbox . sandboxId } ` ) ;
91102
92103 // 等待沙箱运行 / Wait for sandbox to be running
93- log ( " \n--- 等待沙箱运行 / Waiting for sandbox to be running ---" ) ;
104+ log ( ' \n--- 等待沙箱运行 / Waiting for sandbox to be running ---' ) ;
94105 await sandbox . waitUntilRunning ( {
95106 beforeCheck : ( s ) => log ( ` 当前状态 / Current state: ${ s . state } ` ) ,
96107 } ) ;
97- log ( " ✓ 沙箱已运行 / Sandbox is running" ) ;
108+ log ( ' ✓ 沙箱已运行 / Sandbox is running' ) ;
98109
99110 // 等待沙箱健康检查通过
100- log ( " \n--- 等待沙箱就绪 / Waiting for sandbox to be ready ---" ) ;
101- await sandbox . waitUntilReady ( ) ;
102- log ( " ✓ 沙箱健康检查通过 / Sandbox is healthy" ) ;
111+ log ( ' \n--- 等待沙箱就绪 / Waiting for sandbox to be ready ---' ) ;
112+ await sandbox . waitUntilReadyOrFailed ( ) ;
113+ log ( ' ✓ 沙箱健康检查通过 / Sandbox is healthy' ) ;
103114
104115 // 测试代码执行上下文
105- log ( " \n--- 测试代码执行上下文 / Testing code execution context ---" ) ;
116+ log ( ' \n--- 测试代码执行上下文 / Testing code execution context ---' ) ;
106117 const ctx = await sandbox . context . create ( { language : CodeLanguage . PYTHON } ) ;
107118 log ( `✓ 创建上下文成功 / Context created: ${ ctx . contextId } ` ) ;
108119
109- const execResult = await ctx . execute ( { code : "print('Hello from Node.js SDK!')" } ) ;
120+ const execResult = await ctx . execute ( {
121+ code : "print('Hello from Node.js SDK!')" ,
122+ } ) ;
110123 log ( `✓ 执行代码结果 / Code execution result:` , execResult ) ;
111124
112125 const contexts = await ctx . list ( ) ;
@@ -116,104 +129,118 @@ async function codeInterpreterExample(): Promise<void> {
116129 log ( `✓ 获取上下文详情 / Context details: ${ contextDetails . contextId } ` ) ;
117130
118131 // 测试文件系统操作 / File system operations
119- log ( " \n--- 测试文件系统操作 / Testing file system operations ---" ) ;
120- const rootFiles = await sandbox . fileSystem . list ( { path : "/" } ) ;
132+ log ( ' \n--- 测试文件系统操作 / Testing file system operations ---' ) ;
133+ const rootFiles = await sandbox . fileSystem . list ( { path : '/' } ) ;
121134 log ( `✓ 根目录文件列表 / Root directory listing:` , rootFiles ) ;
122135
123- await sandbox . fileSystem . mkdir ( { path : " /home/user/test" } ) ;
136+ await sandbox . fileSystem . mkdir ( { path : ' /home/user/test' } ) ;
124137 log ( `✓ 创建文件夹 /home/user/test / Created directory /home/user/test` ) ;
125138
126- await sandbox . fileSystem . mkdir ( { path : "/home/user/test-move" } ) ;
127- log ( `✓ 创建文件夹 /home/user/test-move / Created directory /home/user/test-move` ) ;
139+ await sandbox . fileSystem . mkdir ( { path : '/home/user/test-move' } ) ;
140+ log (
141+ `✓ 创建文件夹 /home/user/test-move / Created directory /home/user/test-move`
142+ ) ;
128143
129144 // 测试上传下载 / Upload/Download test
130- log ( " \n--- 测试上传下载 / Testing upload/download ---" ) ;
131- const testFilePath = " ./temp_test_file.txt" ;
145+ log ( ' \n--- 测试上传下载 / Testing upload/download ---' ) ;
146+ const testFilePath = ' ./temp_test_file.txt' ;
132147 const testContent =
133- " 这是一个测试文件,用于验证 Sandbox 文件上传下载功能。\n" +
134- " This is a test file for validating Sandbox file upload/download.\n" +
148+ ' 这是一个测试文件,用于验证 Sandbox 文件上传下载功能。\n' +
149+ ' This is a test file for validating Sandbox file upload/download.\n' +
135150 `创建时间 / Created at: ${ new Date ( ) . toISOString ( ) } \n` ;
136151
137152 await fs . writeFile ( testFilePath , testContent ) ;
138153 log ( `✓ 创建临时测试文件 / Created temp test file: ${ testFilePath } ` ) ;
139154
140155 await sandbox . fileSystem . upload ( {
141156 localFilePath : testFilePath ,
142- targetFilePath : " /home/user/test-move/test_file.txt" ,
157+ targetFilePath : ' /home/user/test-move/test_file.txt' ,
143158 } ) ;
144159 log ( `✓ 上传文件成功 / File uploaded successfully` ) ;
145160
146- const filestat = await sandbox . fileSystem . stat ( "/home/user/test-move/test_file.txt" ) ;
161+ const filestat = await sandbox . fileSystem . stat (
162+ '/home/user/test-move/test_file.txt'
163+ ) ;
147164 log ( `✓ 上传文件详情 / Uploaded file stat:` , filestat ) ;
148165
149- const downloadPath = " ./downloaded_test_file.txt" ;
166+ const downloadPath = ' ./downloaded_test_file.txt' ;
150167 const downloadResult = await sandbox . fileSystem . download ( {
151- path : " /home/user/test-move/test_file.txt" ,
168+ path : ' /home/user/test-move/test_file.txt' ,
152169 savePath : downloadPath ,
153170 } ) ;
154171 log ( `✓ 下载文件结果 / Downloaded file:` , downloadResult ) ;
155172
156173 // 验证下载的文件内容
157- const downloadedContent = await fs . readFile ( downloadPath , "utf-8" ) ;
158- log ( `✓ 验证下载文件内容 / Verify downloaded content: ${ downloadedContent . slice ( 0 , 50 ) } ...` ) ;
174+ const downloadedContent = await fs . readFile ( downloadPath , 'utf-8' ) ;
175+ log (
176+ `✓ 验证下载文件内容 / Verify downloaded content: ${ downloadedContent . slice (
177+ 0 ,
178+ 50
179+ ) } ...`
180+ ) ;
159181
160182 // 测试文件读写 / File read/write test
161- log ( "\n--- 测试文件读写 / Testing file read/write ---" ) ;
162- await sandbox . file . write ( { path : "/home/user/test/test.txt" , content : "hello world" } ) ;
183+ log ( '\n--- 测试文件读写 / Testing file read/write ---' ) ;
184+ await sandbox . file . write ( {
185+ path : '/home/user/test/test.txt' ,
186+ content : 'hello world' ,
187+ } ) ;
163188 log ( `✓ 写入文件成功 / File written successfully` ) ;
164189
165- const readResult = await sandbox . file . read ( " /home/user/test/test.txt" ) ;
190+ const readResult = await sandbox . file . read ( ' /home/user/test/test.txt' ) ;
166191 log ( `✓ 读取文件结果 / File read result:` , readResult ) ;
167192
168193 // 测试文件移动 / File move test
169- log ( " \n--- 测试文件移动 / Testing file move ---" ) ;
194+ log ( ' \n--- 测试文件移动 / Testing file move ---' ) ;
170195 await sandbox . fileSystem . move ( {
171- source : " /home/user/test/test.txt" ,
172- destination : " /home/user/test-move/test2.txt" ,
196+ source : ' /home/user/test/test.txt' ,
197+ destination : ' /home/user/test-move/test2.txt' ,
173198 } ) ;
174199 log ( `✓ 移动文件成功 / File moved successfully` ) ;
175200
176- const movedContent = await sandbox . file . read ( "/home/user/test-move/test2.txt" ) ;
201+ const movedContent = await sandbox . file . read (
202+ '/home/user/test-move/test2.txt'
203+ ) ;
177204 log ( `✓ 读取移动后的文件 / Read moved file:` , movedContent ) ;
178205
179206 // 测试文件详情 / File stat test
180- log ( " \n--- 测试文件详情 / Testing file stat ---" ) ;
181- const dirStat = await sandbox . fileSystem . stat ( " /home/user/test-move" ) ;
207+ log ( ' \n--- 测试文件详情 / Testing file stat ---' ) ;
208+ const dirStat = await sandbox . fileSystem . stat ( ' /home/user/test-move' ) ;
182209 log ( `✓ 文件详情 / File stat:` , dirStat ) ;
183210
184211 // 测试删除文件 / Delete test
185- log ( " \n--- 测试删除文件 / Testing file deletion ---" ) ;
186- await sandbox . fileSystem . remove ( " /home/user/test-move" ) ;
212+ log ( ' \n--- 测试删除文件 / Testing file deletion ---' ) ;
213+ await sandbox . fileSystem . remove ( ' /home/user/test-move' ) ;
187214 log ( `✓ 删除文件夹成功 / Directory deleted successfully` ) ;
188215
189216 // 测试进程操作 / Process operations
190- log ( " \n--- 测试进程操作 / Testing process operations ---" ) ;
217+ log ( ' \n--- 测试进程操作 / Testing process operations ---' ) ;
191218 const processes = await sandbox . process . list ( ) ;
192219 log ( `✓ 进程列表 / Process list:` , processes ) ;
193220
194- const cmdResult = await sandbox . process . cmd ( { command : "ls" , cwd : "/" } ) ;
221+ const cmdResult = await sandbox . process . cmd ( { command : 'ls' , cwd : '/' } ) ;
195222 log ( `✓ 进程执行结果 / Process execution result:` , cmdResult ) ;
196223
197- const processDetails = await sandbox . process . get ( "1" ) ;
224+ const processDetails = await sandbox . process . get ( '1' ) ;
198225 log ( `✓ 进程详情 / Process details:` , processDetails ) ;
199226
200227 // 清理上下文
201228 await ctx . delete ( ) ;
202229 log ( `✓ 删除上下文成功 / Context deleted` ) ;
203230
204231 // 停止沙箱 / Stop sandbox
205- log ( " \n--- 停止沙箱 / Stopping sandbox ---" ) ;
232+ log ( ' \n--- 停止沙箱 / Stopping sandbox ---' ) ;
206233 await sandbox . stop ( ) ;
207- log ( " ✓ 沙箱已停止 / Sandbox stopped" ) ;
234+ log ( ' ✓ 沙箱已停止 / Sandbox stopped' ) ;
208235
209236 // 清理资源 / Cleanup
210- log ( " \n--- 清理资源 / Cleaning up ---" ) ;
237+ log ( ' \n--- 清理资源 / Cleaning up ---' ) ;
211238
212239 await sandbox . delete ( ) ;
213- log ( " ✓ 沙箱已删除 / Sandbox deleted" ) ;
240+ log ( ' ✓ 沙箱已删除 / Sandbox deleted' ) ;
214241
215242 await template . delete ( ) ;
216- log ( " ✓ 模板已删除 / Template deleted" ) ;
243+ log ( ' ✓ 模板已删除 / Template deleted' ) ;
217244
218245 // 清理临时测试文件 / Clean up temp files
219246 try {
@@ -230,14 +257,14 @@ async function codeInterpreterExample(): Promise<void> {
230257 // Ignore if file doesn't exist
231258 }
232259
233- log ( " \n✓ Code Interpreter 测试完成 / Code Interpreter test complete\n" ) ;
260+ log ( ' \n✓ Code Interpreter 测试完成 / Code Interpreter test complete\n' ) ;
234261}
235262
236263/**
237264 * 主函数 / Main function
238265 */
239266async function main ( ) {
240- log ( " ==== 沙箱模块基本功能示例 / Sandbox Module Example ====" ) ;
267+ log ( ' ==== 沙箱模块基本功能示例 / Sandbox Module Example ====' ) ;
241268
242269 try {
243270 // List existing templates and sandboxes
@@ -251,9 +278,9 @@ async function main() {
251278 await listTemplates ( ) ;
252279 await listSandboxes ( ) ;
253280
254- log ( " ==== 示例完成 / Example Complete ====" ) ;
281+ log ( ' ==== 示例完成 / Example Complete ====' ) ;
255282 } catch ( error ) {
256- logger . error ( " Error:" , error ) ;
283+ logger . error ( ' Error:' , error ) ;
257284 process . exit ( 1 ) ;
258285 }
259286}
0 commit comments