import { Sandbox } from "@vercel/sandbox";
import { Mesa } from "@mesadev/sdk";
const mesa = new Mesa({ apiKey: process.env.MESA_API_KEY });
// --- Outside the sandbox: set up Mesa resources ---
// Create a repo (or use an existing one)
const repo = await mesa.repos.create({ name: "agent-workspace" });
// Sign a scoped, self-expiring access token for the sandbox. Signed locally
// from your API key with no network call — your API key never enters the
// sandbox. Scope it to the repos the session needs, as full `org/repo` names.
const { token } = await mesa.tokens.create({
scopes: ["read", "write"],
repos: ["my-org/agent-workspace"],
ttl_seconds: 3600, // 1 hour
});
// --- Inside the sandbox: install and mount Mesa ---
const sandbox = await Sandbox.create({
teamId: process.env.VERCEL_TEAM_ID,
projectId: process.env.VERCEL_PROJECT_ID,
token: process.env.VERCEL_TOKEN,
});
// Install the Mesa CLI.
await sandbox.runCommand({
cmd: "sh",
args: ["-c", "curl -fsSL https://mesa.dev/install.sh | sh"],
});
// Install FUSE and enable non-root access to the FUSE mount.
await sandbox.runCommand({
cmd: "dnf",
args: ["install", "-y", "fuse3"],
sudo: true,
});
await sandbox.runCommand({
cmd: "sh",
args: [
"-c",
[
"echo user_allow_other >> /etc/fuse.conf",
"chmod 666 /dev/fuse",
].join("\n"),
],
sudo: true,
});
// Start Mesa as a detached command so it keeps the mount process alive.
// MESA_API_KEY accepts an API key or an access token; we pass the scoped token
// so the raw API key never enters the sandbox.
await sandbox.runCommand({
cmd: "mesa",
args: ["mount", "-y"],
detached: true,
env: {
MESA_ORG: "my-org",
MESA_API_KEY: token,
},
});
// --- Run your agent ---
await sandbox.runCommand({
cmd: "sh",
args: ["-c", 'claude "Implement the feature described in TODO.md"'],
cwd: "/home/user/.local/share/mesa/mnt/my-org/agent-workspace",
});