|
| 1 | +import { readFileSync, existsSync } from 'fs'; |
| 2 | +import { resolve, join } from 'path'; |
| 3 | + |
| 4 | +const CLOUD_API = 'https://agentbot.sh'; |
| 5 | + |
| 6 | +export async function deployCommand(args: string[]) { |
| 7 | + const targetDir = args[0] || '.'; |
| 8 | + const resolved = resolve(targetDir); |
| 9 | + const bundleDir = join(resolved, '.agentbot/dist'); |
| 10 | + |
| 11 | + console.log(''); |
| 12 | + console.log(' 🚀 Agentbot Deploy'); |
| 13 | + console.log(' ──────────────────'); |
| 14 | + |
| 15 | + // Check for bundled agents |
| 16 | + const indexFile = join(bundleDir, 'index.json'); |
| 17 | + if (!existsSync(indexFile)) { |
| 18 | + console.log(' ⚠️ No bundle found. Run `agentbot bundle` first.'); |
| 19 | + console.log(''); |
| 20 | + console.log(' Or bundle and deploy in one step:'); |
| 21 | + console.log(' agentbot bundle && agentbot deploy'); |
| 22 | + console.log(''); |
| 23 | + process.exit(1); |
| 24 | + } |
| 25 | + |
| 26 | + const index = JSON.parse(readFileSync(indexFile, 'utf-8')); |
| 27 | + console.log(` Found ${index.total} agent(s) to deploy.`); |
| 28 | + console.log(''); |
| 29 | + |
| 30 | + // Check for API key |
| 31 | + const apiKey = process.env.AGENTBOT_API_KEY; |
| 32 | + if (!apiKey) { |
| 33 | + console.log(' ❌ AGENTBOT_API_KEY not set.'); |
| 34 | + console.log(''); |
| 35 | + console.log(' Get your API key:'); |
| 36 | + console.log(' 1. Sign up at https://agentbot.sh'); |
| 37 | + console.log(' 2. Go to Settings → API Keys'); |
| 38 | + console.log(' 3. Create a new key'); |
| 39 | + console.log(''); |
| 40 | + console.log(' Then run:'); |
| 41 | + console.log(' AGENTBOT_API_KEY=sk-... agentbot deploy'); |
| 42 | + console.log(''); |
| 43 | + process.exit(1); |
| 44 | + } |
| 45 | + |
| 46 | + // Deploy each agent |
| 47 | + for (const agent of index.agents) { |
| 48 | + const agentFile = join(bundleDir, `${agent.name}.md`); |
| 49 | + const content = readFileSync(agentFile, 'utf-8'); |
| 50 | + |
| 51 | + console.log(` 📤 Deploying ${agent.name}...`); |
| 52 | + |
| 53 | + try { |
| 54 | + const res = await fetch(`${CLOUD_API}/api/agents/provision`, { |
| 55 | + method: 'POST', |
| 56 | + headers: { |
| 57 | + 'Content-Type': 'application/json', |
| 58 | + 'Authorization': `Bearer ${apiKey}`, |
| 59 | + }, |
| 60 | + body: JSON.stringify({ |
| 61 | + name: agent.name, |
| 62 | + description: agent.description, |
| 63 | + definition: content, |
| 64 | + model: agent.model, |
| 65 | + tools: agent.tools, |
| 66 | + }), |
| 67 | + }); |
| 68 | + |
| 69 | + if (res.ok) { |
| 70 | + const data = await res.json() as { url?: string }; |
| 71 | + console.log(` ✅ ${agent.name} deployed!`); |
| 72 | + if (data.url) { |
| 73 | + console.log(` ${data.url}`); |
| 74 | + } |
| 75 | + } else { |
| 76 | + const err = await res.text(); |
| 77 | + console.log(` ❌ ${agent.name} failed: ${res.status} ${err}`); |
| 78 | + } |
| 79 | + } catch (err) { |
| 80 | + console.log(` ❌ ${agent.name} error: ${err instanceof Error ? err.message : String(err)}`); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + console.log(''); |
| 85 | + console.log(' 🎉 Deploy complete!'); |
| 86 | + console.log(` Dashboard: ${CLOUD_API}/dashboard`); |
| 87 | + console.log(''); |
| 88 | +} |
0 commit comments