()
| 5 | import type { AgentDefinition } from '@codebuff/sdk' |
| 6 | |
| 7 | async function main() { |
| 8 | const client = new CodebuffClient({ |
| 9 | // Note: You need to pass in your own API key. |
| 10 | // Get it here: https://www.codebuff.com/profile?tab=api-keys |
| 11 | apiKey: process.env.CODEBUFF_API_KEY, |
| 12 | // Optional directory agent runs from (if applicable). |
| 13 | cwd: process.cwd(), |
| 14 | }) |
| 15 | |
| 16 | // Define your own custom agents! |
| 17 | const myCustomAgent: AgentDefinition = { |
| 18 | id: 'my-custom-agent', |
| 19 | model: 'google/gemini-3.1-flash-lite-preview', |
| 20 | displayName: 'Sentiment analyzer', |
| 21 | toolNames: ['fetch_api_data'], // Defined below! |
| 22 | instructionsPrompt: ` |
| 23 | 1. Describe the different sentiments in the given prompt. |
| 24 | 2. Score the prompt along the following 5 dimensions: |
| 25 | happiness, sadness, anger, fear, and surprise.`, |
| 26 | // ... other AgentDefinition properties |
| 27 | } |
| 28 | |
| 29 | // And define your own custom tools! |
| 30 | const myCustomTool = getCustomToolDefinition({ |
| 31 | toolName: 'fetch_api_data', |
| 32 | description: 'Fetch data from an API endpoint', |
| 33 | inputSchema: z.object({ |
| 34 | url: z.url(), |
| 35 | method: z.enum(['GET', 'POST']).default('GET'), |
| 36 | headers: z.record(z.string(), z.string()).optional(), |
| 37 | }), |
| 38 | exampleInputs: [{ url: 'https://api.example.com/data', method: 'GET' }], |
| 39 | execute: async ({ url, method, headers }) => { |
| 40 | const response = await fetch(url, { method, headers }) |
| 41 | const data = await response.text() |
| 42 | return [ |
| 43 | { |
| 44 | type: 'json' as const, |
| 45 | value: { |
| 46 | message: `API Response: ${data.slice(0, 5000)}...`, |
| 47 | }, |
| 48 | }, |
| 49 | ] |
| 50 | }, |
| 51 | }) |
| 52 | |
| 53 | const { output } = await client.run({ |
| 54 | // Run a custom agent by id. Must match an id in the agentDefinitions field below. |
| 55 | agent: 'my-custom-agent', |
| 56 | prompt: "Today I'm feeling very happy!", |
| 57 | |
| 58 | // Provide custom agent and tool definitions: |
| 59 | agentDefinitions: [myCustomAgent], |
| 60 | customToolDefinitions: [myCustomTool], |
| 61 | |
| 62 | handleEvent: (event) => { |
| 63 | // All events that happen during the run: agent start/finish, tool calls/results, text responses, errors. |
| 64 | console.log('Codebuff Event', JSON.stringify(event)) |
no test coverage detected