@@ -21,7 +21,7 @@ import { CodebuffClient } from '@codebuff/sdk'
2121
2222async function main() {
2323 const client = new CodebuffClient ({
24- // Note: You need to pass in your own API key here.
24+ // Note: You need to pass in your own API key here. Get one: https://www.codebuff.com/profile?tab=api-keys
2525 apiKey: process .env .CODEBUFF_API_KEY ,
2626 cwd: process .cwd (),
2727 onError : (e ) => console .error (' Codebuff error:' , e .message ),
@@ -52,92 +52,78 @@ async function main() {
5252main ()
5353```
5454
55- ### Advanced Example with Custom Agents, Tools, and Images
55+ ### Example 2: Custom Agents and Tools
5656
5757``` typescript
5858import { z } from ' zod'
5959import {
6060 CodebuffClient ,
61- generateInitialRunState ,
62- withAdditionalMessage ,
61+ AgentDefinition ,
6362 getCustomToolDefinition ,
6463} from ' @codebuff/sdk'
6564
6665async function main() {
6766 const client = new CodebuffClient ({
68- // Note: You need to pass in your own API key here.
67+ // Note: You need to pass in your own API key. Get it here: https://www.codebuff.com/profile?tab=api-keys
6968 apiKey: process .env .CODEBUFF_API_KEY ,
7069 cwd: process .cwd (),
7170 onError : (e ) => console .error (' Codebuff error:' , e .message ),
7271 })
7372
74- // Create a run with an image
75- const emptyRun = await generateInitialRunState ({ cwd: process .cwd () })
76- const runWithImage = withAdditionalMessage ({
77- runState: emptyRun ,
78- message: {
79- role: ' user' ,
80- content: [
73+ const myCustomAgent: AgentDefinition = {
74+ id: ' my-custom-agent' ,
75+ model: ' openai/gpt-5' ,
76+ displayName: ' Sentiment analyzer' ,
77+ instructionsPrompt: `
78+ 1. Describe the different sentiments in the given prompt.
79+ 2. Score the prompt along the following 5 dimensions:
80+ happiness, sadness, anger, fear, and surprise. ` ,
81+ // ... other AgentDefinition properties
82+ }
83+
84+ const myCustomTool = getCustomToolDefinition ({
85+ toolName: ' fetch_api_data' ,
86+ description: ' Fetch data from an API endpoint' ,
87+ inputSchema: z .object ({
88+ url: z .url (),
89+ method: z .enum ([' GET' , ' POST' ]).default (' GET' ),
90+ headers: z .record (z .string (), z .string ()).optional (),
91+ }),
92+ outputSchema: z .array (
93+ z .object ({
94+ type: z .literal (' json' ),
95+ value: z .object ({
96+ message: z .string (),
97+ }),
98+ }),
99+ ),
100+ exampleInputs: [{ url: ' https://api.example.com/data' , method: ' GET' }],
101+ handler : async ({ url , method , headers }) => {
102+ const response = await fetch (url , { method , headers })
103+ const data = await response .text ()
104+ return [
81105 {
82- type: ' image ' ,
83- image: new URL (
84- ' https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg ' ,
85- ) ,
106+ type: ' json ' as const ,
107+ value: {
108+ message: ` API Response: ${ data . slice ( 0 , 5000 )}... ` ,
109+ } ,
86110 },
87- ],
111+ ]
88112 },
89113 })
90114
91- const result = await client .run ({
115+ const { output } = await client .run ({
92116 agent: ' my-custom-agent' ,
93- prompt: ' Analyze this image and create code based on what you see' ,
94- previousRun: runWithImage ,
95-
96- // Custom agent definitions
97- agentDefinitions: [
98- {
99- id: ' my-custom-agent' ,
100- model: ' openai/gpt-5' ,
101- displayName: ' Image Analyzer' ,
102- instructionsPrompt:
103- ' 1. describe all the details in the image. 2. answer the user prompt' ,
104- // ... other AgentDefinition properties
105- },
106- ],
107-
108- // Custom tool definitions
109- customToolDefinitions: [
110- getCustomToolDefinition ({
111- toolName: ' fetch_api_data' ,
112- description: ' Fetch data from an API endpoint' ,
113- inputSchema: z .object ({
114- url: z .string ().url (),
115- method: z .enum ([' GET' , ' POST' ]).default (' GET' ),
116- headers: z .record (z .string ()).optional (),
117- }),
118- exampleInputs: [
119- { url: ' https://api.example.com/data' , method: ' GET' },
120- {
121- url: ' https://api.example.com/submit' ,
122- method: ' POST' ,
123- headers: { ' Content-Type' : ' application/json' },
124- },
125- ],
126- handler : async ({ url , method , headers }) => {
127- const response = await fetch (url , { method , headers })
128- const data = await response .text ()
129- return {
130- toolResultMessage: ` API Response: ${data .slice (0 , 5000 )}... ` ,
131- }
132- },
133- }),
134- ],
135-
117+ prompt: " Today I'm feeling very happy!" ,
118+ agentDefinitions: [myCustomAgent ],
119+ customToolDefinitions: [myCustomTool ],
136120 handleEvent : (event ) => {
137- console .log (' Agent progress: ' , event )
121+ console .log (event )
138122 },
139123 })
140124
125+ console .log (' Final output:' , output )
126+
141127 client .closeConnection ()
142128}
143129
0 commit comments