| 259 | * @returns The file content |
| 260 | */ |
| 261 | export async function readFile( |
| 262 | path: string, |
| 263 | options: CodeInterpreterOptions = {}, |
| 264 | existingSandbox?: Sandbox |
| 265 | ): Promise<string> { |
| 266 | const sandbox = existingSandbox || await createSandbox(options); |
| 267 | |
| 268 | try { |
| 269 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 270 | const content = await sandbox.files.read(path); |
| 271 | console.log(`File read from sandbox: ${path}`, { contentLength: content.length }); |
| 272 | return content; |
| 273 | } catch (error: unknown) { |
| 274 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 275 | console.error(`Failed to read file from sandbox: ${path}`, { error: errorMessage }); |
| 276 | throw error; |
| 277 | } finally { |
| 278 | if (!existingSandbox) { |
| 279 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 280 | await sandbox.kill(); |
| 281 | } |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | /** |
| 286 | * List files in the sandbox |