| 289 | * @returns Array of file names |
| 290 | */ |
| 291 | export async function listFiles( |
| 292 | path: string, |
| 293 | options: CodeInterpreterOptions = {}, |
| 294 | existingSandbox?: Sandbox |
| 295 | ): Promise<string[]> { |
| 296 | const sandbox = existingSandbox || await createSandbox(options); |
| 297 | |
| 298 | try { |
| 299 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 300 | const entries = await sandbox.files.list(path); |
| 301 | const files = entries.map((entry: any) => entry.name || entry.path || String(entry)); |
| 302 | console.log(`Listed files in sandbox: ${path}`, { fileCount: entries.length }); |
| 303 | return files; |
| 304 | } catch (error) { |
| 305 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 306 | console.error(`Failed to list files in sandbox: ${path}`, { error: errorMessage }); |
| 307 | throw error; |
| 308 | } finally { |
| 309 | if (!existingSandbox) { |
| 310 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 311 | await sandbox.kill(); |
| 312 | } |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | /** |
| 317 | * Install packages in the sandbox |