2025-05-21 18:21:20 -07:00
|
|
|
import { Sandbox } from "@e2b/code-interpreter";
|
2025-05-22 19:05:29 -07:00
|
|
|
import { TIMEOUT_MS } from "../constants.js";
|
2025-05-23 15:56:06 -07:00
|
|
|
import { getSandboxErrorFields } from "./sandbox-error-fields.js";
|
2025-05-21 18:21:20 -07:00
|
|
|
|
|
|
|
|
export async function readFile(
|
|
|
|
|
sandbox: Sandbox,
|
|
|
|
|
filePath: string,
|
|
|
|
|
): Promise<{
|
|
|
|
|
success: boolean;
|
|
|
|
|
output: string;
|
|
|
|
|
}> {
|
|
|
|
|
try {
|
|
|
|
|
const readOutput = await sandbox.commands.run(`cat "${filePath}"`);
|
2025-05-22 19:05:29 -07:00
|
|
|
// Add an extra 5 min timeout to the sandbox.
|
|
|
|
|
await sandbox.setTimeout(TIMEOUT_MS);
|
|
|
|
|
|
2025-05-21 18:21:20 -07:00
|
|
|
if (readOutput.exitCode !== 0) {
|
|
|
|
|
console.error(
|
2025-05-23 15:56:06 -07:00
|
|
|
`\nError reading file '${filePath}' from sandbox via cat:`,
|
2025-05-21 18:21:20 -07:00
|
|
|
readOutput,
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
output: `FAILED TO READ FILE from sandbox '${filePath}'. Exit code: ${readOutput.exitCode}.\nStderr: ${readOutput.stderr}.\nStdout: ${readOutput.stdout}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (readOutput.stderr) {
|
|
|
|
|
console.warn(
|
2025-05-23 15:56:06 -07:00
|
|
|
`\nStderr while reading file '${filePath}' from sandbox via cat: ${readOutput.stderr}`,
|
2025-05-21 18:21:20 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
output: readOutput.stdout,
|
|
|
|
|
};
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
console.error(
|
2025-05-23 15:56:06 -07:00
|
|
|
`\nException while trying to read file '${filePath}' from sandbox via cat:`,
|
2025-05-21 18:21:20 -07:00
|
|
|
e,
|
|
|
|
|
);
|
2025-05-23 15:56:06 -07:00
|
|
|
let outputMessage = `FAILED TO EXECUTE READ COMMAND for sandbox '${filePath}'.`;
|
|
|
|
|
const errorFields = getSandboxErrorFields(e);
|
|
|
|
|
if (errorFields) {
|
|
|
|
|
outputMessage += `\nExit code: ${errorFields.exitCode}.\nStderr: ${errorFields.stderr}.\nStdout: ${errorFields.stdout}`;
|
|
|
|
|
} else {
|
|
|
|
|
outputMessage += ` Error: ${(e as Error).message || String(e)}`;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-21 18:21:20 -07:00
|
|
|
return {
|
|
|
|
|
success: false,
|
2025-05-23 15:56:06 -07:00
|
|
|
output: outputMessage,
|
2025-05-21 18:21:20 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export async function writeFile(
|
|
|
|
|
sandbox: Sandbox,
|
|
|
|
|
filePath: string,
|
|
|
|
|
content: string,
|
|
|
|
|
): Promise<{
|
|
|
|
|
success: boolean;
|
|
|
|
|
output: string;
|
|
|
|
|
}> {
|
|
|
|
|
try {
|
|
|
|
|
const writeCommand = `printf '%s' '${content}' > "${filePath}"`;
|
|
|
|
|
const writeOutput = await sandbox.commands.run(writeCommand);
|
2025-05-22 19:05:29 -07:00
|
|
|
// Add an extra 5 min timeout to the sandbox.
|
|
|
|
|
await sandbox.setTimeout(TIMEOUT_MS);
|
2025-05-21 18:21:20 -07:00
|
|
|
|
|
|
|
|
if (writeOutput.exitCode !== 0) {
|
|
|
|
|
console.error(
|
2025-05-23 15:56:06 -07:00
|
|
|
`\nError writing file '${filePath}' to sandbox via printf:`,
|
2025-05-21 18:21:20 -07:00
|
|
|
writeOutput,
|
|
|
|
|
);
|
|
|
|
|
return {
|
|
|
|
|
success: false,
|
|
|
|
|
output: `FAILED TO WRITE FILE to sandbox '${filePath}'. Exit code: ${writeOutput.exitCode}. Stderr: ${writeOutput.stderr}. Stdout: ${writeOutput.stdout}`,
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (writeOutput.stderr) {
|
|
|
|
|
console.warn(
|
2025-05-23 15:56:06 -07:00
|
|
|
`\nStderr while writing file '${filePath}' to sandbox via printf: ${writeOutput.stderr}`,
|
2025-05-21 18:21:20 -07:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
success: true,
|
|
|
|
|
output: `Successfully wrote file '${filePath}' to sandbox via printf.`,
|
|
|
|
|
};
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
console.error(
|
2025-05-23 15:56:06 -07:00
|
|
|
`\nException while trying to write file '${filePath}' to sandbox via printf:`,
|
2025-05-21 18:21:20 -07:00
|
|
|
e,
|
|
|
|
|
);
|
2025-05-23 15:56:06 -07:00
|
|
|
|
|
|
|
|
let outputMessage = `FAILED TO EXECUTE WRITE COMMAND for sandbox '${filePath}'.`;
|
|
|
|
|
const errorFields = getSandboxErrorFields(e);
|
|
|
|
|
if (errorFields) {
|
|
|
|
|
outputMessage += `\nExit code: ${errorFields.exitCode}.\nStderr: ${errorFields.stderr}.\nStdout: ${errorFields.stdout}`;
|
|
|
|
|
} else {
|
|
|
|
|
outputMessage += ` Error: ${(e as Error).message || String(e)}`;
|
|
|
|
|
}
|
|
|
|
|
|
2025-05-21 18:21:20 -07:00
|
|
|
return {
|
|
|
|
|
success: false,
|
2025-05-23 15:56:06 -07:00
|
|
|
output: outputMessage,
|
2025-05-21 18:21:20 -07:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|