Skip to content

Commit 6f34fbd

Browse files
committed
Update sdk example to wrap in async function
1 parent 03b5574 commit 6f34fbd

File tree

1 file changed

+102
-94
lines changed

1 file changed

+102
-94
lines changed

sdk/README.md

Lines changed: 102 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,35 @@ Create a Codebuff account and get your [Codebuff API key here](https://www.codeb
1919
```typescript
2020
import { CodebuffClient } from '@codebuff/sdk'
2121

22-
const client = new CodebuffClient({
23-
// Note: You need to pass in your own API key here.
24-
apiKey: process.env.CODEBUFF_API_KEY,
25-
cwd: process.cwd(),
26-
onError: (e) => console.error('Codebuff error:', e.message),
27-
})
28-
29-
// First run
30-
const run1 = await client.run({
31-
agent: 'base',
32-
prompt: 'Create a simple calculator class',
33-
})
34-
35-
// Continue the same session with a follow-up
36-
const run2 = await client.run({
37-
agent: 'base',
38-
prompt: 'Add unit tests for the calculator',
39-
previousRun: run1,
40-
handleEvent: (event) => {
41-
// Log all events
42-
console.log('Progress:', event)
43-
},
44-
})
45-
46-
client.closeConnection()
22+
async function main() {
23+
const client = new CodebuffClient({
24+
// Note: You need to pass in your own API key here.
25+
apiKey: process.env.CODEBUFF_API_KEY,
26+
cwd: process.cwd(),
27+
onError: (e) => console.error('Codebuff error:', e.message),
28+
})
29+
30+
// First run
31+
const run1 = await client.run({
32+
agent: 'base',
33+
prompt: 'Create a simple calculator class',
34+
})
35+
36+
// Continue the same session with a follow-up
37+
const run2 = await client.run({
38+
agent: 'base',
39+
prompt: 'Add unit tests for the calculator',
40+
previousRun: run1,
41+
handleEvent: (event) => {
42+
// Log all events
43+
console.log('Progress:', event)
44+
},
45+
})
46+
47+
client.closeConnection()
48+
}
49+
50+
main()
4751
```
4852

4953
### Advanced Example with Custom Agents, Tools, and Images
@@ -57,81 +61,85 @@ import {
5761
getCustomToolDefinition,
5862
} from '@codebuff/sdk'
5963

60-
const client = new CodebuffClient({
61-
// Note: You need to pass in your own API key here.
62-
apiKey: process.env.CODEBUFF_API_KEY,
63-
cwd: process.cwd(),
64-
onError: (e) => console.error('Codebuff error:', e.message),
65-
})
66-
67-
// Create a run with an image
68-
const emptyRun = await generateInitialRunState({ cwd: process.cwd() })
69-
const runWithImage = withAdditionalMessage({
70-
runState: emptyRun,
71-
message: {
72-
role: 'user',
73-
content: [
74-
{
75-
type: 'image',
76-
image: new URL(
77-
'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg',
78-
),
79-
},
80-
],
81-
},
82-
})
83-
84-
const result = await client.run({
85-
agent: 'my-custom-agent',
86-
prompt: 'Analyze this image and create code based on what you see',
87-
previousRun: runWithImage,
88-
89-
// Custom agent definitions
90-
agentDefinitions: [
91-
{
92-
id: 'my-custom-agent',
93-
model: 'openai/gpt-5',
94-
displayName: 'Image Analyzer',
95-
instructionsPrompt:
96-
'1. describe all the details in the image. 2. answer the user prompt',
97-
// ... other AgentDefinition properties
98-
},
99-
],
100-
101-
// Custom tool definitions
102-
customToolDefinitions: [
103-
getCustomToolDefinition({
104-
toolName: 'fetch_api_data',
105-
description: 'Fetch data from an API endpoint',
106-
inputSchema: z.object({
107-
url: z.string().url(),
108-
method: z.enum(['GET', 'POST']).default('GET'),
109-
headers: z.record(z.string()).optional(),
110-
}),
111-
exampleInputs: [
112-
{ url: 'https://api.example.com/data', method: 'GET' },
64+
async function main() {
65+
const client = new CodebuffClient({
66+
// Note: You need to pass in your own API key here.
67+
apiKey: process.env.CODEBUFF_API_KEY,
68+
cwd: process.cwd(),
69+
onError: (e) => console.error('Codebuff error:', e.message),
70+
})
71+
72+
// Create a run with an image
73+
const emptyRun = await generateInitialRunState({ cwd: process.cwd() })
74+
const runWithImage = withAdditionalMessage({
75+
runState: emptyRun,
76+
message: {
77+
role: 'user',
78+
content: [
11379
{
114-
url: 'https://api.example.com/submit',
115-
method: 'POST',
116-
headers: { 'Content-Type': 'application/json' },
80+
type: 'image',
81+
image: new URL(
82+
'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg',
83+
),
11784
},
11885
],
119-
handler: async ({ url, method, headers }) => {
120-
const response = await fetch(url, { method, headers })
121-
const data = await response.text()
122-
return {
123-
toolResultMessage: `API Response: ${data.slice(0, 5000)}...`,
124-
}
86+
},
87+
})
88+
89+
const result = await client.run({
90+
agent: 'my-custom-agent',
91+
prompt: 'Analyze this image and create code based on what you see',
92+
previousRun: runWithImage,
93+
94+
// Custom agent definitions
95+
agentDefinitions: [
96+
{
97+
id: 'my-custom-agent',
98+
model: 'openai/gpt-5',
99+
displayName: 'Image Analyzer',
100+
instructionsPrompt:
101+
'1. describe all the details in the image. 2. answer the user prompt',
102+
// ... other AgentDefinition properties
125103
},
126-
}),
127-
],
104+
],
105+
106+
// Custom tool definitions
107+
customToolDefinitions: [
108+
getCustomToolDefinition({
109+
toolName: 'fetch_api_data',
110+
description: 'Fetch data from an API endpoint',
111+
inputSchema: z.object({
112+
url: z.string().url(),
113+
method: z.enum(['GET', 'POST']).default('GET'),
114+
headers: z.record(z.string()).optional(),
115+
}),
116+
exampleInputs: [
117+
{ url: 'https://api.example.com/data', method: 'GET' },
118+
{
119+
url: 'https://api.example.com/submit',
120+
method: 'POST',
121+
headers: { 'Content-Type': 'application/json' },
122+
},
123+
],
124+
handler: async ({ url, method, headers }) => {
125+
const response = await fetch(url, { method, headers })
126+
const data = await response.text()
127+
return {
128+
toolResultMessage: `API Response: ${data.slice(0, 5000)}...`,
129+
}
130+
},
131+
}),
132+
],
133+
134+
handleEvent: (event) => {
135+
console.log('Agent progress:', event)
136+
},
137+
})
128138

129-
handleEvent: (event) => {
130-
console.log('Agent progress:', event)
131-
},
132-
})
139+
client.closeConnection()
140+
}
133141

134-
client.closeConnection()
142+
main()
135143
```
136144

137145
## API Reference

0 commit comments

Comments
 (0)