import { ModalClient } from 'modal';
import { GitStorage } from '@pierre/storage';
const modal = new ModalClient();
const store = new GitStorage({
name: 'your-org',
key: process.env.PIERRE_PRIVATE_KEY,
});
async function runInModalSandbox(repoId: string) {
const repo = await store.findOne({ id: repoId });
const url = await repo.getRemoteURL({
permissions: ['git:read', 'git:write'],
ttl: 3600,
});
// Create a Modal sandbox
const app = await modal.apps.fromName('code-runner', { createIfMissing: true });
const image = modal.images.fromRegistry('python:3.12-slim');
const sb = await modal.sandboxes.create(app, image);
// Shallow clone for speed
await sb.exec(['git', 'clone', '--depth', '1', '--single-branch', url, '/code/repo']);
await sb.exec(['git', 'config', '--global', 'user.email', '[email protected]']);
await sb.exec(['git', 'config', '--global', 'user.name', 'AI Agent']);
// Run tests or make changes
const result = await sb.exec(['pytest', '/code/repo/tests']);
console.log(await result.stdout.readText());
// Commit and push changes
await sb.exec(['git', '-C', '/code/repo', 'add', '.']);
await sb.exec(['git', '-C', '/code/repo', 'commit', '-m', 'Update from sandbox']);
await sb.exec(['git', '-C', '/code/repo', 'push']);
await sb.terminate();
}