|
| 1 | +# @us4/den |
| 2 | + |
| 3 | +TypeScript SDK for [Den](https://github.com/us/den) — the self-hosted sandbox runtime for AI agents. |
| 4 | + |
| 5 | +> **100 sandboxes on E2B = ~$600/hour. 100 sandboxes on Den = one $5/month server.** |
| 6 | +
|
| 7 | +## Installation |
| 8 | + |
| 9 | +```bash |
| 10 | +bun add @us4/den |
| 11 | +# or |
| 12 | +npm install @us4/den |
| 13 | +``` |
| 14 | + |
| 15 | +## Quick Start |
| 16 | + |
| 17 | +```typescript |
| 18 | +import { Den } from "@us4/den"; |
| 19 | + |
| 20 | +const client = new Den("http://localhost:8080", { apiKey: "your-key" }); |
| 21 | + |
| 22 | +// Create a sandbox |
| 23 | +const sandbox = await client.create({ image: "ubuntu:22.04" }); |
| 24 | + |
| 25 | +// Execute a command |
| 26 | +const result = await sandbox.exec(["python3", "-c", "print('Hello from Den!')"]); |
| 27 | +console.log(result.stdout); // Hello from Den! |
| 28 | + |
| 29 | +// Read/write files |
| 30 | +await sandbox.writeFile("/tmp/hello.py", "print('hello world')"); |
| 31 | +const content = await sandbox.readFile("/tmp/hello.py"); |
| 32 | + |
| 33 | +// Clean up |
| 34 | +await sandbox.destroy(); |
| 35 | +``` |
| 36 | + |
| 37 | +## Storage |
| 38 | + |
| 39 | +```typescript |
| 40 | +import { Den } from "@us4/den"; |
| 41 | + |
| 42 | +const client = new Den("http://localhost:8080"); |
| 43 | + |
| 44 | +// Persistent volume |
| 45 | +const sandbox = await client.create({ |
| 46 | + image: "ubuntu:22.04", |
| 47 | + storage: { |
| 48 | + volumes: [{ name: "my-data", mountPath: "/data" }], |
| 49 | + }, |
| 50 | +}); |
| 51 | + |
| 52 | +// S3 import/export |
| 53 | +await sandbox.s3Import({ |
| 54 | + bucket: "my-bucket", |
| 55 | + key: "data/input.csv", |
| 56 | + destPath: "/home/sandbox/input.csv", |
| 57 | +}); |
| 58 | + |
| 59 | +await sandbox.s3Export({ |
| 60 | + sourcePath: "/home/sandbox/output.csv", |
| 61 | + bucket: "my-bucket", |
| 62 | + key: "results/output.csv", |
| 63 | +}); |
| 64 | +``` |
| 65 | + |
| 66 | +## Snapshots |
| 67 | + |
| 68 | +```typescript |
| 69 | +// Save state |
| 70 | +const snapshot = await sandbox.snapshot("after-setup"); |
| 71 | + |
| 72 | +// Restore later |
| 73 | +const restored = await client.restoreSnapshot(snapshot.id); |
| 74 | +``` |
| 75 | + |
| 76 | +## WebSocket Streaming |
| 77 | + |
| 78 | +```typescript |
| 79 | +// Stream command output in real-time |
| 80 | +const stream = await sandbox.execStream(["python3", "long_script.py"]); |
| 81 | + |
| 82 | +for await (const message of stream) { |
| 83 | + if (message.type === "stdout") process.stdout.write(message.data); |
| 84 | + if (message.type === "stderr") process.stderr.write(message.data); |
| 85 | + if (message.type === "exit") console.log(`Exit code: ${message.data}`); |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +## Features |
| 90 | + |
| 91 | +- Sandbox lifecycle management (create, list, get, stop, destroy) |
| 92 | +- Command execution with timeout and environment variables |
| 93 | +- WebSocket streaming for real-time output |
| 94 | +- File operations (read, write, list, mkdir, delete, upload, download) |
| 95 | +- Persistent volumes, shared volumes, tmpfs configuration |
| 96 | +- S3 integration (hooks, on-demand, FUSE) |
| 97 | +- Snapshot/restore |
| 98 | +- Port forwarding |
| 99 | +- Full TypeScript types |
| 100 | + |
| 101 | +## Requirements |
| 102 | + |
| 103 | +- Node.js >= 18 or Bun |
| 104 | +- Den server running (see [Den repo](https://github.com/us/den)) |
| 105 | + |
| 106 | +## License |
| 107 | + |
| 108 | +MIT |
0 commit comments