| 320 | * @returns Array of file names |
| 321 | */ |
| 322 | export async function listFiles( |
| 323 | path: string, |
| 324 | options: CodeInterpreterOptions = {}, |
| 325 | existingSandbox?: Sandbox, |
| 326 | ): Promise<string[]> { |
| 327 | const sandbox = existingSandbox || await createSandbox(options); |
| 328 | |
| 329 | try { |
| 330 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 331 | const entries = await sandbox.files.list(path); |
| 332 | const files = entries.map((entry: any) => entry.name || entry.path || String(entry)); |
| 333 | await logMessage("info", `Listed files in sandbox: ${path}`, { fileCount: entries.length }); |
| 334 | return files; |
| 335 | } catch (error: unknown) { |
| 336 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 337 | await logMessage("error", `Failed to list files in sandbox: ${path}`, { error: errorMessage }); |
| 338 | throw error; |
| 339 | } finally { |
| 340 | if (!existingSandbox) { |
| 341 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 342 | await sandbox.kill(); |
| 343 | } |
| 344 | } |
| 345 | } |
| 346 | |
| 347 | /** |
| 348 | * Install packages in the sandbox |