Skip to content

Commit 44cfe0f

Browse files
committed
update sdk readme
1 parent 8576b37 commit 44cfe0f

File tree

3 files changed

+139
-5
lines changed

3 files changed

+139
-5
lines changed

sdk/README.md

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ async function main() {
2828
})
2929

3030
// First run
31-
const run1 = await client.run({
31+
const runOrError1 = await client.run({
3232
// The agent id. Any agent on the store (https://codebuff.com/store)
3333
agent: 'codebuff/[email protected]',
3434
prompt: 'Create a simple calculator class',
@@ -37,16 +37,23 @@ async function main() {
3737
console.log('Codebuff Event', JSON.stringify(event))
3838
},
3939
})
40+
if (!runOrError1.success) {
41+
throw new Error('Run failed' + runOrError1.error.message)
42+
}
43+
const run1 = runOrError1.value
4044

4145
// Continue the same session with a follow-up
42-
const run2 = await client.run({
46+
const runOrError2 = await client.run({
4347
agent: 'codebuff/[email protected]',
4448
prompt: 'Add unit tests for the calculator',
4549
previousRun: run1, // <-- this is where your next run differs from the previous run
4650
handleEvent: (event) => {
4751
console.log('Codebuff Event', JSON.stringify(event))
4852
},
4953
})
54+
if (!runOrError2.success) {
55+
throw new Error('Run failed: ' + runOrError2.error.message)
56+
}
5057
}
5158

5259
main()
@@ -77,7 +84,7 @@ async function main() {
7784
id: 'my-custom-agent',
7885
model: 'x-ai/grok-4-fast',
7986
displayName: 'Sentiment analyzer',
80-
toolNames: ['fetch_api_data'] // Defined below!
87+
toolNames: ['fetch_api_data'], // Defined below!
8188
instructionsPrompt: `
8289
1. Describe the different sentiments in the given prompt.
8390
2. Score the prompt along the following 5 dimensions:
@@ -109,7 +116,7 @@ async function main() {
109116
},
110117
})
111118

112-
const { output } = await client.run({
119+
const runOrError = await client.run({
113120
// Run a custom agent by id. Must match an id in the agentDefinitions field below.
114121
agent: 'my-custom-agent',
115122
prompt: "Today I'm feeling very happy!",
@@ -123,6 +130,10 @@ async function main() {
123130
console.log('Codebuff Event', JSON.stringify(event))
124131
},
125132
})
133+
if (!runOrError.success) {
134+
throw new Error('Run failed: ' + runOrError.error.message)
135+
}
136+
const { output } = runOrError.value
126137

127138
if (output.type === 'error') {
128139
console.error(`The run failed:\n${output.message}`)
@@ -164,9 +175,13 @@ Runs a Codebuff agent with the specified options.
164175

165176
#### Returns
166177

167-
Returns a Promise that resolves to a `RunState` object which can be passed into subsequent runs via the `previousRun` parameter to resume the conversation.
178+
Returns a Promise that resolves to either a "success" or a "failure" object.
179+
180+
- The "success" object contains a `RunState` object which can be passed into subsequent runs via the `previousRun` parameter to resume the conversation.
181+
- The "failure" object contains an `Error` object with a `name`, `message`, and `stack` properties.
168182

169183
The `RunState` object contains:
184+
170185
- `sessionState`: Internal state to be passed to the next run
171186
- `output`: The agent's output (text, error, or other types)
172187

sdk/examples/readme-example-1.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { CodebuffClient } from '@codebuff/sdk'
2+
3+
async function main() {
4+
const client = new CodebuffClient({
5+
// You need to pass in your own API key here.
6+
// Get one here: https://www.codebuff.com/api-keys
7+
apiKey: process.env.CODEBUFF_API_KEY,
8+
cwd: process.cwd(),
9+
})
10+
11+
// First run
12+
const runOrError1 = await client.run({
13+
// The agent id. Any agent on the store (https://codebuff.com/store)
14+
agent: 'codebuff/[email protected]',
15+
prompt: 'Create a simple calculator class',
16+
handleEvent: (event) => {
17+
// All events that happen during the run: agent start/finish, tool calls/results, text responses, errors.
18+
console.log('Codebuff Event', JSON.stringify(event))
19+
},
20+
})
21+
if (!runOrError1.success) {
22+
throw new Error('Run failed' + runOrError1.error.message)
23+
}
24+
const run1 = runOrError1.value
25+
26+
// Continue the same session with a follow-up
27+
const runOrError2 = await client.run({
28+
agent: 'codebuff/[email protected]',
29+
prompt: 'Add unit tests for the calculator',
30+
previousRun: run1, // <-- this is where your next run differs from the previous run
31+
handleEvent: (event) => {
32+
console.log('Codebuff Event', JSON.stringify(event))
33+
},
34+
})
35+
if (!runOrError2.success) {
36+
throw new Error('Run failed: ' + runOrError2.error.message)
37+
}
38+
}
39+
40+
main()

sdk/examples/readme-example-2.ts

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import { z } from 'zod/v4'
2+
3+
import { CodebuffClient, getCustomToolDefinition } from '@codebuff/sdk'
4+
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: 'x-ai/grok-4-fast',
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 runOrError = 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))
65+
},
66+
})
67+
if (!runOrError.success) {
68+
throw new Error('Run failed: ' + runOrError.error.message)
69+
}
70+
const { output } = runOrError.value
71+
72+
if (output.type === 'error') {
73+
console.error(`The run failed:\n${output.message}`)
74+
} else {
75+
console.log('The run succeeded with output:', output)
76+
}
77+
}
78+
79+
main()

0 commit comments

Comments
 (0)