| 290 | * @returns The file content |
| 291 | */ |
| 292 | export async function readFile( |
| 293 | path: string, |
| 294 | options: CodeInterpreterOptions = {}, |
| 295 | existingSandbox?: Sandbox, |
| 296 | ): Promise<string> { |
| 297 | const sandbox = existingSandbox || await createSandbox(options); |
| 298 | |
| 299 | try { |
| 300 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 301 | const content = await sandbox.files.read(path); |
| 302 | await logMessage("info", `File read from sandbox: ${path}`, { contentLength: content.length }); |
| 303 | return content; |
| 304 | } catch (error: unknown) { |
| 305 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 306 | await logMessage("error", `Failed to read file from 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 | * List files in the sandbox |