@@ -26,97 +26,40 @@ npm install @codebuff/sdk
2626``` typescript
2727import * as fs from ' fs'
2828import * 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
0 commit comments