@douglas-agent/sandbank-cli 0.5.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +229 -0
- package/dist/cli/api.d.ts +6 -0
- package/dist/cli/api.d.ts.map +1 -0
- package/dist/cli/api.js +8 -0
- package/dist/cli/api.test.d.ts +2 -0
- package/dist/cli/api.test.d.ts.map +1 -0
- package/dist/cli/api.test.js +31 -0
- package/dist/cli/auth.d.ts +25 -0
- package/dist/cli/auth.d.ts.map +1 -0
- package/dist/cli/auth.js +34 -0
- package/dist/cli/auth.test.d.ts +2 -0
- package/dist/cli/auth.test.d.ts.map +1 -0
- package/dist/cli/auth.test.js +89 -0
- package/dist/cli/commands/addons.d.ts +3 -0
- package/dist/cli/commands/addons.d.ts.map +1 -0
- package/dist/cli/commands/addons.js +54 -0
- package/dist/cli/commands/clone.d.ts +3 -0
- package/dist/cli/commands/clone.d.ts.map +1 -0
- package/dist/cli/commands/clone.js +18 -0
- package/dist/cli/commands/commands.test.d.ts +2 -0
- package/dist/cli/commands/commands.test.d.ts.map +1 -0
- package/dist/cli/commands/commands.test.js +439 -0
- package/dist/cli/commands/config.d.ts +3 -0
- package/dist/cli/commands/config.d.ts.map +1 -0
- package/dist/cli/commands/config.js +57 -0
- package/dist/cli/commands/create.d.ts +3 -0
- package/dist/cli/commands/create.d.ts.map +1 -0
- package/dist/cli/commands/create.js +30 -0
- package/dist/cli/commands/destroy.d.ts +3 -0
- package/dist/cli/commands/destroy.d.ts.map +1 -0
- package/dist/cli/commands/destroy.js +16 -0
- package/dist/cli/commands/exec.d.ts +3 -0
- package/dist/cli/commands/exec.d.ts.map +1 -0
- package/dist/cli/commands/exec.js +22 -0
- package/dist/cli/commands/get.d.ts +3 -0
- package/dist/cli/commands/get.d.ts.map +1 -0
- package/dist/cli/commands/get.js +21 -0
- package/dist/cli/commands/help.d.ts +2 -0
- package/dist/cli/commands/help.d.ts.map +1 -0
- package/dist/cli/commands/help.js +39 -0
- package/dist/cli/commands/keep.d.ts +3 -0
- package/dist/cli/commands/keep.d.ts.map +1 -0
- package/dist/cli/commands/keep.js +25 -0
- package/dist/cli/commands/list.d.ts +3 -0
- package/dist/cli/commands/list.d.ts.map +1 -0
- package/dist/cli/commands/list.js +14 -0
- package/dist/cli/commands/login.d.ts +3 -0
- package/dist/cli/commands/login.d.ts.map +1 -0
- package/dist/cli/commands/login.js +25 -0
- package/dist/cli/commands/snapshot.d.ts +3 -0
- package/dist/cli/commands/snapshot.d.ts.map +1 -0
- package/dist/cli/commands/snapshot.js +78 -0
- package/dist/cli/config.d.ts +9 -0
- package/dist/cli/config.d.ts.map +1 -0
- package/dist/cli/config.js +27 -0
- package/dist/cli/config.test.d.ts +2 -0
- package/dist/cli/config.test.d.ts.map +1 -0
- package/dist/cli/config.test.js +60 -0
- package/dist/cli/index.d.ts +8 -0
- package/dist/cli/index.d.ts.map +1 -0
- package/dist/cli/index.js +78 -0
- package/dist/cli/index.test.d.ts +2 -0
- package/dist/cli/index.test.d.ts.map +1 -0
- package/dist/cli/index.test.js +126 -0
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/package.json +53 -0
package/README.md
ADDED
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# Sandbank
|
|
2
|
+
|
|
3
|
+
> Unified sandbox SDK for AI agents — write once, run on any cloud.
|
|
4
|
+
|
|
5
|
+
**[Website](https://sandbank.dev)** | **[中文文档](./README.zh-CN.md)** | **[日本語ドキュメント](./README.ja.md)**
|
|
6
|
+
|
|
7
|
+
Sandbank provides a single TypeScript interface for creating, managing, and orchestrating cloud sandboxes. Switch between providers without changing your application code.
|
|
8
|
+
|
|
9
|
+
## Why Sandbank?
|
|
10
|
+
|
|
11
|
+
AI agents need isolated execution environments. But every cloud provider has a different API — Daytona, Fly.io, Cloudflare Workers all speak different languages. Sandbank unifies them behind one interface:
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
import { createProvider } from '@douglas-agent/sandbank-core'
|
|
15
|
+
import { DaytonaAdapter } from '@douglas-agent/sandbank-daytona'
|
|
16
|
+
|
|
17
|
+
const provider = createProvider(new DaytonaAdapter({ apiKey: '...' }))
|
|
18
|
+
const sandbox = await provider.create({ image: 'node:22' })
|
|
19
|
+
|
|
20
|
+
const result = await sandbox.exec('echo "Hello from the sandbox"')
|
|
21
|
+
console.log(result.stdout) // Hello from the sandbox
|
|
22
|
+
|
|
23
|
+
await provider.destroy(sandbox.id)
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
Swap `DaytonaAdapter` for `FlyioAdapter` or `CloudflareAdapter` — zero code changes.
|
|
27
|
+
|
|
28
|
+
## Architecture
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
┌──────────────────────────────────────────────────────┐
|
|
32
|
+
│ Your Application / AI Agent │
|
|
33
|
+
├──────────────────────────────────────────────────────┤
|
|
34
|
+
│ @douglas-agent/sandbank-core Unified Provider Interface │
|
|
35
|
+
│ @douglas-agent/sandbank-skills Skill Registry & Injection │
|
|
36
|
+
│ @douglas-agent/sandbank-agent In-sandbox Agent Client │
|
|
37
|
+
│ @douglas-agent/sandbank-relay Multi-agent Communication │
|
|
38
|
+
├──────────────────────────────────────────────────────┤
|
|
39
|
+
│ @douglas-agent/sandbank-daytona @douglas-agent/sandbank-flyio @douglas-agent/sandbank-cloudflare │
|
|
40
|
+
│ @douglas-agent/sandbank-boxlite │
|
|
41
|
+
│ Provider Adapters │
|
|
42
|
+
├──────────────────────────────────────────────────────┤
|
|
43
|
+
│ Daytona Fly.io Machines Cloudflare Workers │
|
|
44
|
+
│ BoxLite (self-hosted Docker) │
|
|
45
|
+
└──────────────────────────────────────────────────────┘
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
## Packages
|
|
49
|
+
|
|
50
|
+
| Package | Description |
|
|
51
|
+
|---------|-------------|
|
|
52
|
+
| [`@douglas-agent/sandbank-core`](./packages/core) | Provider abstraction, capability system, error types |
|
|
53
|
+
| [`@douglas-agent/sandbank-skills`](./packages/skills) | Skill registry and local filesystem loader |
|
|
54
|
+
| [`@douglas-agent/sandbank-daytona`](./packages/daytona) | Daytona cloud sandbox adapter |
|
|
55
|
+
| [`@douglas-agent/sandbank-flyio`](./packages/flyio) | Fly.io Machines adapter |
|
|
56
|
+
| [`@douglas-agent/sandbank-cloudflare`](./packages/cloudflare) | Cloudflare Workers adapter |
|
|
57
|
+
| [`@douglas-agent/sandbank-boxlite`](./packages/boxlite) | BoxLite self-hosted Docker adapter |
|
|
58
|
+
| [`@douglas-agent/sandbank-relay`](./packages/relay) | WebSocket relay for multi-agent communication |
|
|
59
|
+
| [`@douglas-agent/sandbank-agent`](./packages/agent) | Lightweight client for agents running inside sandboxes |
|
|
60
|
+
|
|
61
|
+
## Provider Support
|
|
62
|
+
|
|
63
|
+
### Core Operations
|
|
64
|
+
|
|
65
|
+
All providers implement these — the minimum contract:
|
|
66
|
+
|
|
67
|
+
| Operation | Daytona | Fly.io | Cloudflare | BoxLite |
|
|
68
|
+
|-----------|:-------:|:------:|:----------:|:-------:|
|
|
69
|
+
| Create / Destroy | ✅ | ✅ | ✅ | ✅ |
|
|
70
|
+
| List sandboxes | ✅ | ✅ | ✅ | ✅ |
|
|
71
|
+
| Execute commands | ✅ | ✅ | ✅ | ✅ |
|
|
72
|
+
| Read / Write files | ✅ | ✅ | ✅ | ✅ |
|
|
73
|
+
| Skill injection | ✅ | ✅ | ✅ | ✅ |
|
|
74
|
+
|
|
75
|
+
### Extended Capabilities
|
|
76
|
+
|
|
77
|
+
Capabilities are opt-in. Use `withVolumes(provider)`, `withPortExpose(sandbox)`, etc. to safely check and access them at runtime.
|
|
78
|
+
|
|
79
|
+
| Capability | Daytona | Fly.io | Cloudflare | BoxLite | Description |
|
|
80
|
+
|------------|:-------:|:------:|:----------:|:-------:|-------------|
|
|
81
|
+
| `volumes` | ✅ | ✅ | ⚠️* | ❌ | Persistent volume management |
|
|
82
|
+
| `port.expose` | ✅ | ✅ | ⚠️** | ✅ | Expose sandbox ports to the internet |
|
|
83
|
+
| `exec.stream` | ❌ | ❌ | ✅ | ✅ | Stream stdout/stderr in real-time |
|
|
84
|
+
| `snapshot` | ❌ | ❌ | ✅ | ✅ | Snapshot and restore sandbox state |
|
|
85
|
+
| `terminal` | ✅ | ✅ | ✅ | ✅ | Interactive web terminal (ttyd) |
|
|
86
|
+
| `sleep` | ❌ | ❌ | ❌ | ✅ | Hibernate and wake sandboxes |
|
|
87
|
+
| `skills` | ✅ | ✅ | ✅ | ✅ | Load and inject skill definitions into sandboxes |
|
|
88
|
+
|
|
89
|
+
\* Cloudflare `volumes` requires `storage` option in adapter config.
|
|
90
|
+
|
|
91
|
+
\*\* Cloudflare reserves port 3000 for its sandbox control plane. Use any port in 1024–65535 except 3000.
|
|
92
|
+
|
|
93
|
+
### Provider Characteristics
|
|
94
|
+
|
|
95
|
+
| | Daytona | Fly.io | Cloudflare | BoxLite |
|
|
96
|
+
|---|---------|--------|------------|---------|
|
|
97
|
+
| **Runtime** | Full VM | Firecracker microVM | V8 isolate + container | Docker container |
|
|
98
|
+
| **Cold start** | ~10s | ~3-5s | ~1s | ~2-5s |
|
|
99
|
+
| **File I/O** | Native SDK | Via exec (base64) | Native SDK | Via exec (base64) |
|
|
100
|
+
| **Regions** | Multi | Multi | Global edge | Self-hosted |
|
|
101
|
+
| **External deps** | `@daytonaio/sdk` | None (pure fetch) | `@cloudflare/sandbox` | BoxLite API |
|
|
102
|
+
|
|
103
|
+
## Multi-Agent Sessions
|
|
104
|
+
|
|
105
|
+
Sandbank includes a built-in orchestration layer for multi-agent workflows. The **Relay** handles real-time messaging and shared context between sandboxes.
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
import { createSession } from '@douglas-agent/sandbank-core'
|
|
109
|
+
|
|
110
|
+
const session = await createSession({
|
|
111
|
+
provider,
|
|
112
|
+
relay: { type: 'memory' },
|
|
113
|
+
})
|
|
114
|
+
|
|
115
|
+
// Spawn agents in isolated sandboxes
|
|
116
|
+
const architect = await session.spawn('architect', {
|
|
117
|
+
image: 'node:22',
|
|
118
|
+
env: { ROLE: 'architect' },
|
|
119
|
+
})
|
|
120
|
+
|
|
121
|
+
const developer = await session.spawn('developer', {
|
|
122
|
+
image: 'node:22',
|
|
123
|
+
env: { ROLE: 'developer' },
|
|
124
|
+
})
|
|
125
|
+
|
|
126
|
+
// Shared context — all agents can read/write
|
|
127
|
+
await session.context.set('spec', { endpoints: ['/users', '/posts'] })
|
|
128
|
+
|
|
129
|
+
// Wait for all agents to complete
|
|
130
|
+
await session.waitForAll()
|
|
131
|
+
await session.close()
|
|
132
|
+
```
|
|
133
|
+
|
|
134
|
+
Inside the sandbox, agents use `@douglas-agent/sandbank-agent`:
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
import { connect } from '@douglas-agent/sandbank-agent'
|
|
138
|
+
|
|
139
|
+
const session = await connect() // reads SANDBANK_* env vars
|
|
140
|
+
|
|
141
|
+
session.on('message', async (msg) => {
|
|
142
|
+
if (msg.type === 'task') {
|
|
143
|
+
// do work...
|
|
144
|
+
await session.send(msg.from, 'done', result)
|
|
145
|
+
}
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
await session.complete({ status: 'success', summary: 'Built 5 API endpoints' })
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Quick Start
|
|
152
|
+
|
|
153
|
+
```bash
|
|
154
|
+
# Install
|
|
155
|
+
pnpm add @douglas-agent/sandbank-core @douglas-agent/sandbank-daytona # or @douglas-agent/sandbank-flyio, @douglas-agent/sandbank-cloudflare
|
|
156
|
+
|
|
157
|
+
# Set up provider
|
|
158
|
+
export DAYTONA_API_KEY=your-key
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
```typescript
|
|
162
|
+
import { createProvider } from '@douglas-agent/sandbank-core'
|
|
163
|
+
import { DaytonaAdapter } from '@douglas-agent/sandbank-daytona'
|
|
164
|
+
|
|
165
|
+
const provider = createProvider(
|
|
166
|
+
new DaytonaAdapter({ apiKey: process.env.DAYTONA_API_KEY! })
|
|
167
|
+
)
|
|
168
|
+
|
|
169
|
+
// Create a sandbox
|
|
170
|
+
const sandbox = await provider.create({
|
|
171
|
+
image: 'node:22',
|
|
172
|
+
resources: { cpu: 2, memory: 2048 },
|
|
173
|
+
autoDestroyMinutes: 30,
|
|
174
|
+
})
|
|
175
|
+
|
|
176
|
+
// Run commands
|
|
177
|
+
const { stdout } = await sandbox.exec('node --version')
|
|
178
|
+
|
|
179
|
+
// File operations
|
|
180
|
+
await sandbox.writeFile('/app/index.js', 'console.log("hi")')
|
|
181
|
+
await sandbox.exec('node /app/index.js')
|
|
182
|
+
|
|
183
|
+
// Clean up
|
|
184
|
+
await provider.destroy(sandbox.id)
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
## Development
|
|
188
|
+
|
|
189
|
+
```bash
|
|
190
|
+
git clone https://github.com/chekusu/sandbank.git
|
|
191
|
+
cd sandbank
|
|
192
|
+
pnpm install
|
|
193
|
+
|
|
194
|
+
# Run all unit tests
|
|
195
|
+
pnpm test
|
|
196
|
+
|
|
197
|
+
# Run cross-provider conformance tests
|
|
198
|
+
pnpm test:conformance
|
|
199
|
+
|
|
200
|
+
# Typecheck
|
|
201
|
+
pnpm typecheck
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
### Running Integration Tests
|
|
205
|
+
|
|
206
|
+
Integration tests hit real APIs and are gated by environment variables:
|
|
207
|
+
|
|
208
|
+
```bash
|
|
209
|
+
# Daytona
|
|
210
|
+
DAYTONA_API_KEY=... pnpm test
|
|
211
|
+
|
|
212
|
+
# Fly.io
|
|
213
|
+
FLY_API_TOKEN=... FLY_APP_NAME=... pnpm test
|
|
214
|
+
|
|
215
|
+
# Cloudflare
|
|
216
|
+
E2E_WORKER_URL=... pnpm test
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Design Principles
|
|
220
|
+
|
|
221
|
+
1. **Minimal interface, maximum interop** — only the true common denominator (exec + files + lifecycle)
|
|
222
|
+
2. **Explicit over implicit** — no auto-fallback, no caching, no hidden retries
|
|
223
|
+
3. **Capability detection, not fake implementations** — if a provider doesn't support it, it errors
|
|
224
|
+
4. **Idempotent operations** — destroying an already-destroyed sandbox is a no-op
|
|
225
|
+
5. **Full decoupling** — provider layer and session layer are independent, compose freely
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
MIT
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { createX402Fetch } from '@douglas-agent/sandbank-cloud';
|
|
2
|
+
import type { CliFlags } from './auth.js';
|
|
3
|
+
export type ApiClient = ReturnType<typeof createX402Fetch>;
|
|
4
|
+
export declare function createApiClient(flags: CliFlags): ApiClient;
|
|
5
|
+
export declare function printJson(value: unknown): void;
|
|
6
|
+
//# sourceMappingURL=api.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.d.ts","sourceRoot":"","sources":["../../src/cli/api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,+BAA+B,CAAA;AAC/D,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,WAAW,CAAA;AAGzC,MAAM,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,eAAe,CAAC,CAAA;AAE1D,wBAAgB,eAAe,CAAC,KAAK,EAAE,QAAQ,GAAG,SAAS,CAE1D;AAED,wBAAgB,SAAS,CAAC,KAAK,EAAE,OAAO,GAAG,IAAI,CAE9C"}
|
package/dist/cli/api.js
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { createX402Fetch } from '@douglas-agent/sandbank-cloud';
|
|
2
|
+
import { resolveCloudConfig } from './auth.js';
|
|
3
|
+
export function createApiClient(flags) {
|
|
4
|
+
return createX402Fetch(resolveCloudConfig(flags));
|
|
5
|
+
}
|
|
6
|
+
export function printJson(value) {
|
|
7
|
+
console.log(JSON.stringify(value, null, 2));
|
|
8
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"api.test.d.ts","sourceRoot":"","sources":["../../src/cli/api.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from 'vitest';
|
|
2
|
+
vi.mock('@douglas-agent/sandbank-cloud', () => ({
|
|
3
|
+
createX402Fetch: vi.fn((config) => ({
|
|
4
|
+
x402Fetch: vi.fn(),
|
|
5
|
+
x402FetchRaw: vi.fn(),
|
|
6
|
+
baseUrl: config.url || 'https://cloud.sandbank.dev',
|
|
7
|
+
})),
|
|
8
|
+
}));
|
|
9
|
+
vi.mock('./auth.js', () => ({
|
|
10
|
+
resolveCloudConfig: vi.fn((flags) => ({
|
|
11
|
+
url: flags.url || 'https://cloud.sandbank.dev',
|
|
12
|
+
apiToken: flags.apiKey,
|
|
13
|
+
})),
|
|
14
|
+
}));
|
|
15
|
+
import { createApiClient, printJson } from './api.js';
|
|
16
|
+
describe('createApiClient', () => {
|
|
17
|
+
it('creates a client with resolved config', () => {
|
|
18
|
+
const client = createApiClient({ apiKey: 'test' });
|
|
19
|
+
expect(client).toHaveProperty('x402Fetch');
|
|
20
|
+
expect(client).toHaveProperty('x402FetchRaw');
|
|
21
|
+
expect(client).toHaveProperty('baseUrl');
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
describe('printJson', () => {
|
|
25
|
+
it('prints JSON to stdout', () => {
|
|
26
|
+
const spy = vi.spyOn(console, 'log').mockImplementation(() => { });
|
|
27
|
+
printJson({ foo: 'bar' });
|
|
28
|
+
expect(spy).toHaveBeenCalledWith(JSON.stringify({ foo: 'bar' }, null, 2));
|
|
29
|
+
spy.mockRestore();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import type { SandbankCloudConfig } from '@douglas-agent/sandbank-cloud';
|
|
2
|
+
export interface CliFlags {
|
|
3
|
+
apiKey?: string;
|
|
4
|
+
walletKey?: string;
|
|
5
|
+
url?: string;
|
|
6
|
+
json?: boolean;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Resolve SandbankCloudConfig from CLI flags, env vars, and saved credentials.
|
|
10
|
+
*
|
|
11
|
+
* Priority for API token:
|
|
12
|
+
* 1. --api-key flag
|
|
13
|
+
* 2. SANDBANK_API_KEY env
|
|
14
|
+
* 3. SANDBANK_AGENT_TOKEN env (inside sandbox)
|
|
15
|
+
* 4. ~/.sandbank/credentials.json
|
|
16
|
+
*
|
|
17
|
+
* Priority for wallet key:
|
|
18
|
+
* 1. --wallet-key flag
|
|
19
|
+
* 2. SANDBANK_WALLET_KEY env
|
|
20
|
+
* 3. ~/.sandbank/credentials.json
|
|
21
|
+
*/
|
|
22
|
+
export declare function resolveCloudConfig(flags: CliFlags): SandbankCloudConfig;
|
|
23
|
+
/** Get current box ID (only available inside a sandbox) */
|
|
24
|
+
export declare function currentBoxId(): string | undefined;
|
|
25
|
+
//# sourceMappingURL=auth.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.d.ts","sourceRoot":"","sources":["../../src/cli/auth.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAA;AAGxE,MAAM,WAAW,QAAQ;IACvB,MAAM,CAAC,EAAE,MAAM,CAAA;IACf,SAAS,CAAC,EAAE,MAAM,CAAA;IAClB,GAAG,CAAC,EAAE,MAAM,CAAA;IACZ,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,QAAQ,GAAG,mBAAmB,CAkBvE;AAED,2DAA2D;AAC3D,wBAAgB,YAAY,IAAI,MAAM,GAAG,SAAS,CAEjD"}
|
package/dist/cli/auth.js
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { loadCredentials } from './config.js';
|
|
2
|
+
/**
|
|
3
|
+
* Resolve SandbankCloudConfig from CLI flags, env vars, and saved credentials.
|
|
4
|
+
*
|
|
5
|
+
* Priority for API token:
|
|
6
|
+
* 1. --api-key flag
|
|
7
|
+
* 2. SANDBANK_API_KEY env
|
|
8
|
+
* 3. SANDBANK_AGENT_TOKEN env (inside sandbox)
|
|
9
|
+
* 4. ~/.sandbank/credentials.json
|
|
10
|
+
*
|
|
11
|
+
* Priority for wallet key:
|
|
12
|
+
* 1. --wallet-key flag
|
|
13
|
+
* 2. SANDBANK_WALLET_KEY env
|
|
14
|
+
* 3. ~/.sandbank/credentials.json
|
|
15
|
+
*/
|
|
16
|
+
export function resolveCloudConfig(flags) {
|
|
17
|
+
const creds = loadCredentials();
|
|
18
|
+
const apiToken = flags.apiKey
|
|
19
|
+
|| process.env['SANDBANK_API_KEY']
|
|
20
|
+
|| process.env['SANDBANK_AGENT_TOKEN']
|
|
21
|
+
|| creds.apiKey;
|
|
22
|
+
const walletPrivateKey = flags.walletKey
|
|
23
|
+
|| process.env['SANDBANK_WALLET_KEY']
|
|
24
|
+
|| creds.walletKey;
|
|
25
|
+
const url = flags.url
|
|
26
|
+
|| process.env['SANDBANK_API_URL']
|
|
27
|
+
|| creds.url
|
|
28
|
+
|| 'https://cloud.sandbank.dev';
|
|
29
|
+
return { url, apiToken, walletPrivateKey };
|
|
30
|
+
}
|
|
31
|
+
/** Get current box ID (only available inside a sandbox) */
|
|
32
|
+
export function currentBoxId() {
|
|
33
|
+
return process.env['SANDBANK_BOX_ID'];
|
|
34
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auth.test.d.ts","sourceRoot":"","sources":["../../src/cli/auth.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
|
|
2
|
+
vi.mock('./config.js', () => ({
|
|
3
|
+
loadCredentials: vi.fn(() => ({})),
|
|
4
|
+
}));
|
|
5
|
+
import { resolveCloudConfig, currentBoxId } from './auth.js';
|
|
6
|
+
import { loadCredentials } from './config.js';
|
|
7
|
+
describe('resolveCloudConfig', () => {
|
|
8
|
+
const origEnv = { ...process.env };
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
vi.mocked(loadCredentials).mockReturnValue({});
|
|
11
|
+
delete process.env['SANDBANK_API_KEY'];
|
|
12
|
+
delete process.env['SANDBANK_AGENT_TOKEN'];
|
|
13
|
+
delete process.env['SANDBANK_WALLET_KEY'];
|
|
14
|
+
delete process.env['SANDBANK_API_URL'];
|
|
15
|
+
});
|
|
16
|
+
afterEach(() => {
|
|
17
|
+
Object.assign(process.env, origEnv);
|
|
18
|
+
});
|
|
19
|
+
it('uses default URL when nothing configured', () => {
|
|
20
|
+
const config = resolveCloudConfig({});
|
|
21
|
+
expect(config.url).toBe('https://cloud.sandbank.dev');
|
|
22
|
+
expect(config.apiToken).toBeUndefined();
|
|
23
|
+
expect(config.walletPrivateKey).toBeUndefined();
|
|
24
|
+
});
|
|
25
|
+
it('prioritizes --api-key flag over env', () => {
|
|
26
|
+
process.env['SANDBANK_API_KEY'] = 'env-key';
|
|
27
|
+
const config = resolveCloudConfig({ apiKey: 'flag-key' });
|
|
28
|
+
expect(config.apiToken).toBe('flag-key');
|
|
29
|
+
});
|
|
30
|
+
it('uses SANDBANK_API_KEY env var', () => {
|
|
31
|
+
process.env['SANDBANK_API_KEY'] = 'env-key';
|
|
32
|
+
const config = resolveCloudConfig({});
|
|
33
|
+
expect(config.apiToken).toBe('env-key');
|
|
34
|
+
});
|
|
35
|
+
it('uses SANDBANK_AGENT_TOKEN as fallback', () => {
|
|
36
|
+
process.env['SANDBANK_AGENT_TOKEN'] = 'agent-token';
|
|
37
|
+
const config = resolveCloudConfig({});
|
|
38
|
+
expect(config.apiToken).toBe('agent-token');
|
|
39
|
+
});
|
|
40
|
+
it('prefers SANDBANK_API_KEY over SANDBANK_AGENT_TOKEN', () => {
|
|
41
|
+
process.env['SANDBANK_API_KEY'] = 'api-key';
|
|
42
|
+
process.env['SANDBANK_AGENT_TOKEN'] = 'agent-token';
|
|
43
|
+
const config = resolveCloudConfig({});
|
|
44
|
+
expect(config.apiToken).toBe('api-key');
|
|
45
|
+
});
|
|
46
|
+
it('falls back to saved credentials', () => {
|
|
47
|
+
vi.mocked(loadCredentials).mockReturnValue({ apiKey: 'saved-key', url: 'https://custom.dev' });
|
|
48
|
+
const config = resolveCloudConfig({});
|
|
49
|
+
expect(config.apiToken).toBe('saved-key');
|
|
50
|
+
expect(config.url).toBe('https://custom.dev');
|
|
51
|
+
});
|
|
52
|
+
it('resolves wallet key from flag', () => {
|
|
53
|
+
const config = resolveCloudConfig({ walletKey: '0xabc' });
|
|
54
|
+
expect(config.walletPrivateKey).toBe('0xabc');
|
|
55
|
+
});
|
|
56
|
+
it('resolves wallet key from env', () => {
|
|
57
|
+
process.env['SANDBANK_WALLET_KEY'] = '0xdef';
|
|
58
|
+
const config = resolveCloudConfig({});
|
|
59
|
+
expect(config.walletPrivateKey).toBe('0xdef');
|
|
60
|
+
});
|
|
61
|
+
it('resolves wallet key from saved credentials', () => {
|
|
62
|
+
vi.mocked(loadCredentials).mockReturnValue({ walletKey: '0x123' });
|
|
63
|
+
const config = resolveCloudConfig({});
|
|
64
|
+
expect(config.walletPrivateKey).toBe('0x123');
|
|
65
|
+
});
|
|
66
|
+
it('uses --url flag', () => {
|
|
67
|
+
const config = resolveCloudConfig({ url: 'http://localhost:3000' });
|
|
68
|
+
expect(config.url).toBe('http://localhost:3000');
|
|
69
|
+
});
|
|
70
|
+
it('uses SANDBANK_API_URL env', () => {
|
|
71
|
+
process.env['SANDBANK_API_URL'] = 'http://local:4000';
|
|
72
|
+
const config = resolveCloudConfig({});
|
|
73
|
+
expect(config.url).toBe('http://local:4000');
|
|
74
|
+
});
|
|
75
|
+
});
|
|
76
|
+
describe('currentBoxId', () => {
|
|
77
|
+
const origEnv = { ...process.env };
|
|
78
|
+
afterEach(() => {
|
|
79
|
+
Object.assign(process.env, origEnv);
|
|
80
|
+
});
|
|
81
|
+
it('returns SANDBANK_BOX_ID when set', () => {
|
|
82
|
+
process.env['SANDBANK_BOX_ID'] = 'box-123';
|
|
83
|
+
expect(currentBoxId()).toBe('box-123');
|
|
84
|
+
});
|
|
85
|
+
it('returns undefined when not set', () => {
|
|
86
|
+
delete process.env['SANDBANK_BOX_ID'];
|
|
87
|
+
expect(currentBoxId()).toBeUndefined();
|
|
88
|
+
});
|
|
89
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"addons.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/addons.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAY1C,wBAAsB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAiDlF"}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { currentBoxId } from '../auth.js';
|
|
2
|
+
import { createApiClient, printJson } from '../api.js';
|
|
3
|
+
function takeOption(args, name) {
|
|
4
|
+
const idx = args.indexOf(name);
|
|
5
|
+
if (idx === -1)
|
|
6
|
+
return undefined;
|
|
7
|
+
const value = args[idx + 1];
|
|
8
|
+
args.splice(idx, 2);
|
|
9
|
+
return value;
|
|
10
|
+
}
|
|
11
|
+
export async function addonsCommand(args, flags) {
|
|
12
|
+
const sub = args[0];
|
|
13
|
+
const boxId = takeOption(args, '--box') || currentBoxId();
|
|
14
|
+
if (sub === 'create') {
|
|
15
|
+
const type = args[1];
|
|
16
|
+
if (!type) {
|
|
17
|
+
console.error('Usage: sandbank addons create <type> [--intent "..."] [--box <id>]');
|
|
18
|
+
process.exit(1);
|
|
19
|
+
}
|
|
20
|
+
if (!boxId) {
|
|
21
|
+
console.error('No box ID. Use --box <id> or run inside a sandbox.');
|
|
22
|
+
process.exit(1);
|
|
23
|
+
}
|
|
24
|
+
const intent = takeOption(args, '--intent') || args.slice(2).join(' ') || undefined;
|
|
25
|
+
const api = createApiClient(flags);
|
|
26
|
+
const result = await api.x402Fetch(`/boxes/${boxId}/addons`, { method: 'POST', body: JSON.stringify({ type, intent }) });
|
|
27
|
+
if (flags.json)
|
|
28
|
+
return printJson(result);
|
|
29
|
+
console.log(`${result.type} ${result.id} ${result.status}`);
|
|
30
|
+
if (result.relay_name)
|
|
31
|
+
console.log(`relay: ${result.relay_name}`);
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
if (sub === 'list' || sub === 'ls') {
|
|
35
|
+
if (!boxId) {
|
|
36
|
+
console.error('No box ID. Use --box <id> or run inside a sandbox.');
|
|
37
|
+
process.exit(1);
|
|
38
|
+
}
|
|
39
|
+
const api = createApiClient(flags);
|
|
40
|
+
const addons = await api.x402Fetch(`/boxes/${boxId}/addons`);
|
|
41
|
+
if (flags.json)
|
|
42
|
+
return printJson(addons);
|
|
43
|
+
if (addons.length === 0) {
|
|
44
|
+
console.log('No addons');
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
for (const a of addons) {
|
|
48
|
+
console.log(`${a.id} ${a.type.padEnd(12)} ${a.status.padEnd(8)} ${a.created_at}`);
|
|
49
|
+
}
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
console.error('Usage: sandbank addons <create|list> ...');
|
|
53
|
+
process.exit(1);
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"clone.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/clone.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAI1C,wBAAsB,YAAY,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,KAAK,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC,CAgBjF"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { currentBoxId } from '../auth.js';
|
|
2
|
+
import { createApiClient, printJson } from '../api.js';
|
|
3
|
+
export async function cloneCommand(args, flags) {
|
|
4
|
+
const id = args[0] || currentBoxId();
|
|
5
|
+
if (!id) {
|
|
6
|
+
console.error('Usage: sandbank clone <id>');
|
|
7
|
+
console.error('Inside a sandbox, <id> defaults to the current box.');
|
|
8
|
+
process.exit(1);
|
|
9
|
+
}
|
|
10
|
+
const api = createApiClient(flags);
|
|
11
|
+
const result = await api.x402Fetch(`/boxes/${id}/clone`, {
|
|
12
|
+
method: 'POST',
|
|
13
|
+
body: JSON.stringify({}),
|
|
14
|
+
});
|
|
15
|
+
if (flags.json)
|
|
16
|
+
return printJson(result);
|
|
17
|
+
console.log(`Cloned ${id} → ${result.id} (${result.status})`);
|
|
18
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commands.test.d.ts","sourceRoot":"","sources":["../../../src/cli/commands/commands.test.ts"],"names":[],"mappings":""}
|