| 1 | import { Recursiv } from '@recursiv/sdk' |
| 2 | |
| 3 | const r = new Recursiv() |
| 4 | const ORG_ID = process.env.RECURSIV_ORG_ID! |
| 5 | |
| 6 | async function main() { |
| 7 | // 1. Create a project (container for your infra) |
| 8 | const { data: project } = await r.projects.create({ |
| 9 | name: 'my-app', |
| 10 | organization_id: ORG_ID, |
| 11 | }) |
| 12 | console.log('Project:', project.id) |
| 13 | |
| 14 | // 2. Provision a Postgres database |
| 15 | const { data: db } = await r.databases.ensure({ |
| 16 | name: 'my-app-db', |
| 17 | project_id: project.id, |
| 18 | }) |
| 19 | const { data: creds } = await r.databases.getCredentials({ |
| 20 | project_id: project.id, |
| 21 | name: 'my-app-db', |
| 22 | }) |
| 23 | console.log('Database ready:', creds.connection_string) |
| 24 | |
| 25 | // 3. Run a migration in a sandbox |
| 26 | await r.projects.createSandbox(project.id) |
| 27 | await r.projects.executeCode(project.id, { |
| 28 | code: ` |
| 29 | const { Client } = require('pg'); |
| 30 | const c = new Client({ connectionString: '${creds.connection_string}' }); |
| 31 | await c.connect(); |
| 32 | await c.query(\` |
| 33 | CREATE TABLE IF NOT EXISTS items ( |
| 34 | id SERIAL PRIMARY KEY, |
| 35 | name TEXT NOT NULL, |
| 36 | category TEXT, |
| 37 | created_at TIMESTAMPTZ DEFAULT NOW() |
| 38 | ) |
| 39 | \`); |
| 40 | console.log('Migration complete'); |
| 41 | await c.end(); |
| 42 | `, |
| 43 | language: 'typescript', |
| 44 | }) |
| 45 | |
| 46 | // 4. Create an AI assistant |
| 47 | const { data: agent } = await r.agents.create({ |
| 48 | name: 'App Assistant', |
| 49 | username: 'assistant_' + Date.now(), |
| 50 | model: 'anthropic/claude-sonnet-4', |
| 51 | system_prompt: 'You help users manage their items. Be concise and helpful.', |
| 52 | organization_id: ORG_ID, |
| 53 | }) |
| 54 | console.log('Agent:', agent.id) |
| 55 | |
| 56 | // 5. Set up file storage |
| 57 | const { data: bucket } = await r.storage.ensureBucket({ |
| 58 | name: 'app-uploads', |
| 59 | project_id: project.id, |
| 60 | }) |
| 61 | console.log('Storage bucket:', bucket.name) |
| 62 | |
| 63 | // 6. Stream a chat with the agent |
| 64 | process.stdout.write('Agent: ') |
| 65 | for await (const chunk of r.agents.chatStream(agent.id, { |
| 66 | message: 'What kinds of items should I track?', |
| 67 | })) { |
| 68 | process.stdout.write(chunk.delta ?? '') |
| 69 | } |
| 70 | console.log() |
| 71 | } |
| 72 | |
| 73 | main() |