Skip to content

Commit 7029390

Browse files
committed
Update sdk readme and docs
1 parent 3420707 commit 7029390

File tree

3 files changed

+85
-33
lines changed

3 files changed

+85
-33
lines changed

sdk/README.md

Lines changed: 83 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,52 @@ npm install @codebuff/sdk
2323

2424
## Usage
2525

26+
### Basic Example
27+
2628
```typescript
2729
import * as fs from 'fs'
2830
import * as os from 'os'
31+
import { CodebuffClient } from '@codebuff/sdk'
32+
33+
// Available after running `codebuff login`
34+
const apiKey = JSON.parse(
35+
fs
36+
.readFileSync(os.homedir() + '/.config/manicode/credentials.json')
37+
.toString(),
38+
).default.authToken
39+
40+
const client = new CodebuffClient({
41+
apiKey,
42+
cwd: process.cwd(),
43+
onError: (e) => console.error('Codebuff error:', e.message),
44+
})
45+
46+
// First run
47+
const run1 = await client.run({
48+
agent: 'base',
49+
prompt: 'Create a simple calculator class',
50+
})
51+
52+
// Continue the same session with a follow-up
53+
const run2 = await client.run({
54+
agent: 'base',
55+
prompt: 'Add unit tests for the calculator',
56+
previousRun: run1,
57+
handleEvent: (event) => {
58+
// Log all events
59+
console.log('Progress:', event)
60+
},
61+
})
62+
63+
client.closeConnection()
64+
```
2965

66+
### Advanced Example with Custom Agents, Tools, and Images
67+
68+
```typescript
69+
import * as fs from 'fs'
70+
import * as os from 'os'
71+
import { z } from 'zod'
3072
import {
3173
CodebuffClient,
3274
generateInitialRunState,
@@ -39,20 +81,16 @@ const apiKey = JSON.parse(
3981
.readFileSync(os.homedir() + '/.config/manicode/credentials.json')
4082
.toString(),
4183
).default.authToken
42-
const cwd = process.cwd()
4384

4485
const client = new CodebuffClient({
4586
apiKey,
46-
cwd,
87+
cwd: process.cwd(),
4788
onError: (e) => console.error('Codebuff error:', e.message),
48-
// Optional: Override the implementation of specific tools.
49-
overrideTools: {},
5089
})
5190

52-
// Single run
53-
const emptyRun = generateInitialRunState({ cwd })
54-
55-
const runWithExampleImage = withAdditionalMessage({
91+
// Create a run with an image
92+
const emptyRun = await generateInitialRunState({ cwd: process.cwd() })
93+
const runWithImage = withAdditionalMessage({
5694
runState: emptyRun,
5795
message: {
5896
role: 'user',
@@ -67,37 +105,47 @@ const runWithExampleImage = withAdditionalMessage({
67105
},
68106
})
69107

70-
const run1 = await client.run({
71-
agent: 'base',
72-
prompt: 'What is depicted in the attached image?',
73-
previousRun: runWithExampleImage,
74-
handleEvent: (event) => {
75-
console.log('event from run1:', { event })
76-
},
77-
})
78-
79-
// Continue same session with follow‑up
80-
const run2 = await client.run({
81-
agent: 'base',
82-
prompt: 'What about the text? (ignoring the pictures)',
83-
previousRun: run1,
108+
const result = await client.run({
109+
agent: 'my-custom-agent',
110+
prompt: 'Analyze this image and create code based on what you see',
111+
previousRun: runWithImage,
84112

85-
// Stream events (optional)
86-
handleEvent: (event) => {
87-
// event includes streamed updates like assistant messages and tool calls
88-
console.log('event from run2:', event)
89-
},
90-
91-
// Custom agents (optional)
113+
// Custom agent definitions
92114
agentDefinitions: [
93115
{
94-
id: 'my-awesome-agent',
116+
id: 'my-custom-agent',
95117
model: 'openai/gpt-5',
96-
displayName: 'My awesome agent',
97-
instructionsPrompt: 'Do something awesome',
118+
displayName: 'Image Analyzer',
119+
instructionsPrompt:
120+
'1. describe all the details in the image. 2. answer the user prompt',
98121
// ... other AgentDefinition properties
99122
},
100123
],
124+
125+
// Custom tool definitions
126+
customToolDefinitions: [
127+
{
128+
toolName: 'fetch_api_data',
129+
zodSchema: z
130+
.object({
131+
url: z.string().url(),
132+
method: z.enum(['GET', 'POST']).default('GET'),
133+
headers: z.record(z.string()).optional(),
134+
})
135+
.describe('Fetch data from an API endpoint'),
136+
handler: async ({ url, method, headers }) => {
137+
const response = await fetch(url, { method, headers })
138+
const data = await response.text()
139+
return {
140+
toolResultMessage: `API Response: ${data.slice(0, 500)}...`,
141+
}
142+
},
143+
},
144+
],
145+
146+
handleEvent: (event) => {
147+
console.log('Agent progress:', event)
148+
},
101149
})
102150

103151
client.closeConnection()
@@ -126,6 +174,9 @@ Runs a Codebuff agent with the specified options.
126174
- **`knowledgeFiles`** (object, optional): Knowledge files to inject into every `run()` call. Uses the same schema as `projectFiles` - keys are file paths and values are file contents. These files are added directly to the agent's context.
127175

128176
- **`agentDefinitions`** (array, optional): Array of custom agent configurations. Each object should satisfy the AgentConfig type.
177+
178+
- **`customToolDefinitions`** (array, optional): Array of custom tool definitions that extend the agent's capabilities. Each tool definition includes a name, Zod schema for input validation, and a handler function. These tools can be called by the agent during execution.
179+
129180
- **`maxAgentSteps`** (number, optional): Maximum number of steps the agent can take before stopping. Use this as a safety measure in case your agent starts going off the rails. A reasonable number is around 20.
130181

131182
#### Returns

sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "@codebuff/sdk",
33
"private": false,
44
"access": "public",
5-
"version": "0.1.14",
5+
"version": "0.1.15",
66
"description": "Official SDK for Codebuff — AI coding agent & framework",
77
"license": "MIT",
88
"type": "module",

sdk/src/client.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export class CodebuffClient {
112112
* @param projectFiles - (Optional) All the files in your project as a plain JavaScript object. Keys should be the full path from your current directory to each file, and values should be the string contents of the file. Example: { "src/index.ts": "console.log('hi')" }. This helps Codebuff pick good source files for context.
113113
* @param knowledgeFiles - (Optional) Knowledge files to inject into every run() call. Uses the same schema as projectFiles - keys are file paths and values are file contents. These files are added directly to the agent's context.
114114
* @param agentDefinitions - (Optional) Array of custom agent definitions. Each object should satisfy the AgentDefinition type. You can input the agent's id field into the agent parameter to run that agent.
115+
* @param customToolDefinitions - (Optional) Array of custom tool definitions that extend the agent's capabilities. Each tool definition includes a name, Zod schema for input validation, and a handler function. These tools can be called by the agent during execution.
115116
* @param maxAgentSteps - (Optional) Maximum number of steps the agent can take before stopping. Use this as a safety measure in case your agent starts going off the rails. A reasonable number is around 20.
116117
*
117118
* @returns A Promise that resolves to a RunState JSON object which you can pass to a subsequent run() call to continue the run.

0 commit comments

Comments
 (0)