| 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 |
| 8 | const { data: project } = await r.projects.create({ |
| 9 | name: 'code-playground', |
| 10 | organization_id: ORG_ID, |
| 11 | }) |
| 12 | console.log('Project:', project.id) |
| 13 | |
| 14 | // 2. Start a sandbox |
| 15 | const { data: sandbox } = await r.projects.createSandbox(project.id) |
| 16 | console.log('Sandbox ready:', sandbox.sandbox_id) |
| 17 | |
| 18 | // 3. Create an AI coding assistant |
| 19 | const { data: agent } = await r.agents.create({ |
| 20 | name: 'Code Assistant', |
| 21 | username: 'coder_' + Date.now(), |
| 22 | model: 'anthropic/claude-sonnet-4', |
| 23 | system_prompt: `You are a coding assistant. When the user asks you to write code, |
| 24 | respond with working TypeScript code. Be concise.`, |
| 25 | tool_mode: 'autonomous', |
| 26 | organization_id: ORG_ID, |
| 27 | }) |
| 28 | |
| 29 | // 4. Ask the agent to write code |
| 30 | process.stdout.write('Agent: ') |
| 31 | let generatedCode = '' |
| 32 | for await (const chunk of r.agents.chatStream(agent.id, { |
| 33 | message: 'Write a function that checks if a string is a palindrome', |
| 34 | })) { |
| 35 | const text = chunk.delta ?? '' |
| 36 | process.stdout.write(text) |
| 37 | generatedCode += text |
| 38 | } |
| 39 | console.log('\n') |
| 40 | |
| 41 | // 5. Execute code in the sandbox |
| 42 | const { data: result } = await r.projects.executeCode(project.id, { |
| 43 | code: ` |
| 44 | function isPalindrome(s: string): boolean { |
| 45 | const cleaned = s.toLowerCase().replace(/[^a-z0-9]/g, ''); |
| 46 | return cleaned === cleaned.split('').reverse().join(''); |
| 47 | } |
| 48 | console.log(isPalindrome('racecar')); // true |
| 49 | console.log(isPalindrome('hello')); // false |
| 50 | console.log(isPalindrome('A man a plan a canal Panama')); // true |
| 51 | `, |
| 52 | language: 'typescript', |
| 53 | }) |
| 54 | console.log('Output:', result.output) |
| 55 | |
| 56 | // 6. Deploy to production |
| 57 | const { data: deployment } = await r.projects.deploy(project.id, { |
| 58 | branch: 'main', |
| 59 | type: 'production', |
| 60 | }) |
| 61 | console.log('Deployed:', deployment.id) |
| 62 | |
| 63 | // 7. Check deployment logs |
| 64 | const { data: logs } = await r.projects.deploymentLogs(project.id, deployment.id) |
| 65 | console.log('Logs:', logs) |
| 66 | |
| 67 | // 8. Clean up sandbox when done |
| 68 | await r.projects.stopSandbox(project.id) |
| 69 | } |
| 70 | |
| 71 | main() |