Skip to content

Commit

Permalink
chore: enhance sandbox creation logic with template fallback and deta…
Browse files Browse the repository at this point in the history
…iled response message
  • Loading branch information
jmaddington committed Dec 21, 2024
1 parent d4d46e9 commit 7ad7f50
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions api/app/clients/tools/structured/E2BCode.js
Original file line number Diff line number Diff line change
Expand Up @@ -533,10 +533,32 @@ class E2BCode extends Tool {
};
}

// If template is not provided, use a default template, e.g. 'ubuntu-latest'
const sandboxTemplate = template || 'ubuntu-latest';
let sandboxCreate;
let skippedTemplate = false;
//Try to use a template if provided:
if (template) {
try {
sandboxCreate = await Sandbox.create(template, sandboxCreateOptions);
} catch (error) {
//Try without a template:
try {
sandboxCreate = await Sandbox.create(sandboxCreateOptions);
skippedTemplate = true;
} catch (error) {
logger.error('[E2BCode] Error creating sandbox', { sessionId, error: error.message });
throw new Error(`Error creating sandbox: ${error.message}`);
}
}
} else {
//Try without a template:
try {
sandboxCreate = await Sandbox.create(sandboxCreateOptions);
} catch (error) {
logger.error('[E2BCode] Error creating sandbox', { sessionId, error: error.message });
throw new Error(`Error creating sandbox: ${error.message}`);
}
}

const sandboxCreate = await Sandbox.create(sandboxTemplate, sandboxCreateOptions);
sandboxes.set(sessionId, {
sandbox: sandboxCreate,
lastAccessed: Date.now(),
Expand All @@ -553,13 +575,23 @@ class E2BCode extends Tool {
// Get sandbox ID
const createdSandboxId = sandboxCreate.sandboxId;

let message;
if (!skippedTemplate) {
message = `Sandbox created with sandboxId ${createdSandboxId} from template '${template}' with timeout ${adjustedTimeout} minutes.`;
} else {
message = `Sandbox created with sandboxId ${createdSandboxId} with timeout ${adjustedTimeout} minutes. There was an error attempting to use the template so none was used.`;
}

//Add the current user and current directory to the response
message += ` You are user ${currentUser} and current directory is ${currentDirectory}.`;

return JSON.stringify({
sessionId,
sandboxId: createdSandboxId,
currentUser,
currentDirectory,
success: true,
message: `Sandbox created from template '${sandboxTemplate}' with timeout ${adjustedTimeout} minutes.`,
message: message,
});
}

Expand Down

0 comments on commit 7ad7f50

Please sign in to comment.