| 229 | * @param options Options for the sandbox |
| 230 | */ |
| 231 | export async function writeFile( |
| 232 | path: string, |
| 233 | content: string, |
| 234 | options: CodeInterpreterOptions = {}, |
| 235 | existingSandbox?: Sandbox |
| 236 | ): Promise<void> { |
| 237 | const sandbox = existingSandbox || await createSandbox(options); |
| 238 | |
| 239 | try { |
| 240 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 241 | await sandbox.files.write(path, content); |
| 242 | console.log(`File written to sandbox: ${path}`, { contentLength: content.length }); |
| 243 | } catch (error: unknown) { |
| 244 | const errorMessage = error instanceof Error ? error.message : String(error); |
| 245 | console.error(`Failed to write file to sandbox: ${path}`, { error: errorMessage }); |
| 246 | throw error; |
| 247 | } finally { |
| 248 | if (!existingSandbox) { |
| 249 | // @ts-ignore - Ignore TypeScript errors for API compatibility |
| 250 | await sandbox.kill(); |
| 251 | } |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Read a file from the sandbox |