Skip to content

Commit e9bc924

Browse files
committed
sdk: update readme, bump version, handle passed in apiKey
1 parent 349a140 commit e9bc924

File tree

3 files changed

+42
-97
lines changed

3 files changed

+42
-97
lines changed

sdk/README.md

Lines changed: 30 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -26,97 +26,40 @@ npm install @codebuff/sdk
2626
```typescript
2727
import * as fs from 'fs'
2828
import * as os from 'os'
29+
import { CodebuffClient } from '@codebuff/sdk'
2930

30-
import { WebSocketHandler, getInitialSessionState } from '@codebuff/sdk'
31+
// Available after running `codebuff login`
32+
const apiKey = JSON.parse(
33+
fs
34+
.readFileSync(os.homedir() + '/.config/manicode/credentials.json')
35+
.toString(),
36+
).default.authToken
3137

32-
const client = new WebSocketHandler({
33-
onWebsocketError: (error) => {
34-
console.log({ error }, 'onWebsocketError')
35-
},
36-
onWebsocketReconnect: () => {
37-
console.log('onWebsocketReconnect')
38-
},
39-
onResponseError: async (error) => {
40-
console.log({ error }, 'onResponseError')
41-
},
42-
onRequestReconnect: async () => {
43-
console.log('onRequestReconnect')
44-
},
45-
readFiles: async (input) => {
46-
console.log({ input }, 'readFiles')
47-
return {}
48-
},
49-
handleToolCall: async ({
50-
type,
51-
toolName,
52-
requestId,
53-
userInputId,
54-
args,
55-
timeout,
56-
}) => {
57-
console.log(
58-
{ type, toolName, requestId, userInputId, args, timeout },
59-
'handleToolCall',
60-
)
61-
return { success: true, toolCallResult: 'asdf' }
62-
},
63-
onCostResponse: async (action) => {
64-
console.log({ action }, 'onCostResponse')
65-
},
66-
onUsageResponse: async (action) => {
67-
console.log({ action }, 'onUsageResponse')
68-
},
69-
onResponseChunk: async (action) => {
70-
console.log({ action }, 'onResponseChunk')
71-
},
72-
onSubagentResponseChunk: async (action) => {
73-
console.log({ action }, 'onSubagentResponseChunk')
74-
},
75-
onPromptResponse: async (response) => {
76-
console.log({ response }, 'onPromptResponse')
77-
},
78-
// Available after running `codebuff login`
79-
apiKey: JSON.parse(
80-
fs
81-
.readFileSync(os.homedir() + '/.config/manicode/credentials.json')
82-
.toString(),
83-
).default.authToken,
38+
const client = new CodebuffClient({
39+
apiKey,
40+
cwd: process.cwd(),
41+
onError: (e) => console.error('Codebuff error:', e.message),
42+
// Optional: Override the implementation of specific tools.
43+
overrideTools: {},
8444
})
8545

86-
console.log('connecting')
87-
await client.connect()
88-
console.log('connected')
89-
client.sendInput({
90-
prompt: 'can you run echo "hi" for me?',
91-
promptId: 'some-prompt-id-12345',
92-
costMode: 'normal',
93-
sessionState: getInitialSessionState({
94-
projectRoot: os.homedir() + '/github/codebuff',
95-
cwd: os.homedir() + '/github/codebuff',
96-
fileTree: [],
97-
fileTokenScores: {},
98-
tokenCallers: {},
99-
knowledgeFiles: {},
100-
userKnowledgeFiles: {},
101-
agentTemplates: {},
102-
gitChanges: {
103-
status: '',
104-
diff: '',
105-
diffCached: '',
106-
lastCommitMessages: '',
107-
},
108-
changesSinceLastChat: {},
109-
shellConfigFiles: {},
110-
systemInfo: {
111-
platform: process.platform,
112-
shell: 'bash',
113-
nodeVersion: process.version,
114-
arch: process.arch,
115-
homedir: os.homedir() + '/github/codebuff',
116-
cpus: 16,
117-
},
118-
}),
119-
toolResults: [],
46+
// Single run
47+
const run1 = await client.run({
48+
agent: 'base',
49+
prompt: 'Add console.log("Hello from Codebuff") to src/index.ts',
50+
})
51+
52+
// Continue same session with follow‑up
53+
const run2 = await client.run({
54+
agent: 'base',
55+
prompt: 'Create a basic test file for it',
56+
previousRun: run1,
57+
58+
// Stream events (optional)
59+
handleEvent: (event) => {
60+
// event includes streamed updates like assistant messages and tool calls
61+
console.log('event:', event)
62+
},
12063
})
12164
```
12265

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.2",
5+
"version": "0.1.3",
66
"description": "Official SDK for Codebuff — AI coding agent & framework",
77
"license": "MIT",
88
"type": "module",

sdk/src/client.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ import { getFiles } from './tools/read-files'
1919
type ClientToolName = 'write_file' | 'run_terminal_command'
2020

2121
export type CodebuffClientOptions = {
22+
// Provide an API key or set the CODEBUFF_API_KEY environment variable.
23+
apiKey?: string
2224
cwd: string
2325
onError: (error: { message: string }) => void
2426
overrideTools: Partial<
@@ -57,7 +59,7 @@ export class CodebuffClient {
5759
{ resolve: (response: any) => void; reject: (error: any) => void }
5860
> = {}
5961

60-
constructor({ cwd, onError, overrideTools }: CodebuffClientOptions) {
62+
constructor({ apiKey, cwd, onError, overrideTools }: CodebuffClientOptions) {
6163
// TODO: download binary automatically
6264
const isWindows = process.platform === 'win32'
6365
if (
@@ -69,17 +71,17 @@ export class CodebuffClient {
6971
`Could not find ${CODEBUFF_BINARY} in PATH. Please run "npm i -g codebuff" to install codebuff.`,
7072
)
7173
}
72-
if (!process.env[API_KEY_ENV_VAR]) {
74+
const foundApiKey = apiKey ?? process.env[API_KEY_ENV_VAR]
75+
if (!foundApiKey) {
7376
throw new Error(
74-
`Codebuff API key not found. Please set the ${API_KEY_ENV_VAR} environment variable.`,
77+
`Codebuff API key not found. Please provide an apiKey in the constructor of CodebuffClient or set the ${API_KEY_ENV_VAR} environment variable.`,
7578
)
7679
}
77-
const apiKey = process.env[API_KEY_ENV_VAR]
7880

7981
this.cwd = cwd
8082
this.overrideTools = overrideTools
8183
this.websocketHandler = new WebSocketHandler({
82-
apiKey,
84+
apiKey: foundApiKey,
8385
onWebsocketError: (error) => {
8486
onError({ message: error.message })
8587
},
@@ -130,7 +132,7 @@ export class CodebuffClient {
130132
prompt,
131133
params,
132134
handleEvent,
133-
previousState,
135+
previousRun,
134136
allFiles,
135137
knowledgeFiles,
136138
agentConfig,
@@ -140,7 +142,7 @@ export class CodebuffClient {
140142
prompt: string
141143
params?: Record<string, any>
142144
handleEvent?: (event: PrintModeEvent) => void
143-
previousState?: RunState
145+
previousRun?: RunState
144146
allFiles?: Record<string, string>
145147
knowledgeFiles?: Record<string, string>
146148
agentConfig?: Record<string, any>
@@ -150,14 +152,14 @@ export class CodebuffClient {
150152

151153
const promptId = Math.random().toString(36).substring(2, 15)
152154
const sessionState =
153-
previousState?.sessionState ??
155+
previousRun?.sessionState ??
154156
initialSessionState(this.cwd, {
155157
knowledgeFiles,
156158
agentConfig,
157159
allFiles,
158160
maxAgentSteps,
159161
})
160-
const toolResults = previousState?.toolResults ?? []
162+
const toolResults = previousRun?.toolResults ?? []
161163
if (handleEvent) {
162164
this.promptIdToHandleEvent[promptId] = handleEvent
163165
}

0 commit comments

Comments
 (0)