Skip to content

Commit a9fe09f

Browse files
committed
sdk v0.1
1 parent e79f36b commit a9fe09f

File tree

8 files changed

+221
-144
lines changed

8 files changed

+221
-144
lines changed

common/src/actions.ts

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
toolCallSchema,
99
toolResultSchema,
1010
} from './types/session-state'
11-
import { FileVersionSchema, ProjectFileContextSchema } from './util/file'
11+
import { ProjectFileContextSchema } from './util/file'
1212

1313
export const FileChangeSchema = z.object({
1414
type: z.enum(['patch', 'file']),
@@ -96,22 +96,6 @@ export const InitResponseSchema = z
9696
)
9797
export type InitResponse = z.infer<typeof InitResponseSchema>
9898

99-
export const ResponseCompleteSchema = z
100-
.object({
101-
type: z.literal('response-complete'),
102-
userInputId: z.string(),
103-
response: z.string(),
104-
changes: CHANGES,
105-
changesAlreadyApplied: CHANGES,
106-
addedFileVersions: z.array(FileVersionSchema),
107-
resetFileVersions: z.boolean(),
108-
})
109-
.merge(
110-
UsageReponseSchema.omit({
111-
type: true,
112-
}).partial(),
113-
)
114-
11599
export const MessageCostResponseSchema = z.object({
116100
type: z.literal('message-cost-response'),
117101
promptId: z.string(),
@@ -142,7 +126,6 @@ export const SERVER_ACTION_SCHEMA = z.discriminatedUnion('type', [
142126
chunk: z.string(),
143127
prompt: z.string().optional(),
144128
}),
145-
ResponseCompleteSchema,
146129
PromptResponseSchema,
147130
z.object({
148131
type: z.literal('read-files'),
@@ -157,26 +140,6 @@ export const SERVER_ACTION_SCHEMA = z.discriminatedUnion('type', [
157140
args: z.record(z.any()),
158141
timeout: z.number().optional(),
159142
}),
160-
z.object({
161-
type: z.literal('tool-call'),
162-
userInputId: z.string(),
163-
response: z.string(),
164-
data: toolCallSchema,
165-
changes: CHANGES,
166-
changesAlreadyApplied: CHANGES,
167-
addedFileVersions: z.array(FileVersionSchema),
168-
resetFileVersions: z.boolean(),
169-
}),
170-
z.object({
171-
type: z.literal('terminal-command-result'),
172-
userInputId: z.string(),
173-
result: z.string(),
174-
}),
175-
z.object({
176-
type: z.literal('npm-version-status'),
177-
isUpToDate: z.boolean(),
178-
latestVersion: z.string(),
179-
}),
180143
InitResponseSchema,
181144
UsageReponseSchema,
182145
MessageCostResponseSchema,
@@ -186,10 +149,6 @@ export const SERVER_ACTION_SCHEMA = z.discriminatedUnion('type', [
186149
error: z.string().optional(),
187150
remainingBalance: z.number().optional(),
188151
}),
189-
z.object({
190-
type: z.literal('commit-message-response'),
191-
commitMessage: z.string(),
192-
}),
193152
z.object({
194153
// The server is imminently going to shutdown, and the client should reconnect
195154
type: z.literal('request-reconnect'),

common/src/websockets/websocket-client.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,14 @@ export class APIRealtimeClient {
6060
connectTimeout?: any
6161
heartbeat?: any
6262
hadError = false
63-
onError: () => void
63+
onError: (event: WebSocket.ErrorEvent) => void
6464
onReconnect: () => void
6565

66-
constructor(url: string, onError: () => void, onReconnect: () => void) {
66+
constructor(
67+
url: string,
68+
onError: (event: WebSocket.ErrorEvent) => void,
69+
onReconnect: () => void,
70+
) {
6771
this.url = url
6872
this.txid = 0
6973
this.txns = new Map()
@@ -94,7 +98,7 @@ export class APIRealtimeClient {
9498
}
9599
this.ws.onerror = (ev) => {
96100
if (!this.hadError) {
97-
this.onError()
101+
this.onError(ev)
98102
this.hadError = true
99103
}
100104
// this can fire without an onclose if this is the first time we ever try

npm-app/src/client.ts

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -832,17 +832,6 @@ export class Client {
832832
}
833833
})
834834

835-
this.webSocket.subscribe('npm-version-status', (action) => {
836-
const { isUpToDate } = action
837-
if (!isUpToDate) {
838-
console.warn(
839-
yellow(
840-
`\nThere's a new version of Codebuff! Please update to ensure proper functionality.\nUpdate now by running: npm install -g codebuff`,
841-
),
842-
)
843-
}
844-
})
845-
846835
this.webSocket.subscribe('message-cost-response', (action) => {
847836
const parsedAction = MessageCostResponseSchema.safeParse(action)
848837
if (!parsedAction.success) return
@@ -934,25 +923,6 @@ export class Client {
934923
}
935924
}
936925

937-
async generateCommitMessage(stagedChanges: string): Promise<string> {
938-
return new Promise(async (resolve, reject) => {
939-
const unsubscribe = this.webSocket.subscribe(
940-
'commit-message-response',
941-
(action) => {
942-
unsubscribe()
943-
resolve(action.commitMessage)
944-
},
945-
)
946-
947-
this.webSocket.sendAction({
948-
type: 'generate-commit-message',
949-
fingerprintId: await this.fingerprintId,
950-
authToken: this.user?.authToken,
951-
stagedChanges,
952-
})
953-
})
954-
}
955-
956926
async sendUserInput(prompt: string): Promise<{
957927
responsePromise: Promise<
958928
ServerAction & { type: 'prompt-response' | 'manager-prompt-response' } & {
@@ -1576,7 +1546,7 @@ Go to https://www.codebuff.com/config for more information.`) +
15761546
this.setUsage(parsedAction.data)
15771547
})
15781548

1579-
const initAction: ClientAction = {
1549+
const initAction: Extract<ClientAction, { type: 'init' }> = {
15801550
type: 'init',
15811551
fingerprintId: await this.fingerprintId,
15821552
authToken: this.user?.authToken,

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

sdk/src/client.ts

Lines changed: 11 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import { execFileSync } from 'child_process'
22

3-
import { API_KEY_ENV_VAR } from '../../common/src/constants'
43
import { CODEBUFF_BINARY } from './constants'
54
import { processStream } from './process-stream'
5+
import { API_KEY_ENV_VAR } from '../../common/src/constants'
66

7-
import type {
8-
CodebuffClientOptions,
9-
ChatContext,
10-
ContinueChatOptions,
11-
NewChatOptions,
12-
} from './types'
13-
7+
/** @deprecated Migrate to WebSocketHandler */
148
export class CodebuffClient {
159
public cwd: string
1610

17-
constructor({ cwd }: CodebuffClientOptions) {
11+
constructor({ cwd }: { cwd: string }) {
1812
// TODO: download binary automatically
1913
if (execFileSync('which', [CODEBUFF_BINARY]).toString().trim() === '') {
2014
throw new Error(
@@ -35,37 +29,14 @@ export class CodebuffClient {
3529
prompt,
3630
params,
3731
handleEvent,
38-
}: NewChatOptions): Promise<ChatContext> {
39-
const args = [prompt, '-p', '--agent', agent]
40-
if (prompt) {
41-
args.push(prompt)
42-
}
43-
if (params) {
44-
args.push('--params', JSON.stringify(params))
45-
}
46-
if (this.cwd) {
47-
args.push('--cwd', this.cwd)
48-
}
49-
50-
await processStream({
51-
codebuffArgs: args,
52-
handleEvent,
53-
})
54-
55-
return {
56-
agentId: agent,
57-
}
58-
}
59-
60-
// WIP
61-
private async continueChat({
62-
agent,
63-
prompt,
64-
params,
65-
context,
66-
handleEvent,
67-
}: ContinueChatOptions): Promise<ChatContext> {
68-
agent = agent ?? context.agentId
32+
}: {
33+
agent: string
34+
prompt: string
35+
params?: Record<string, any>
36+
handleEvent: (event: any) => void
37+
}): Promise<{
38+
agentId: string
39+
}> {
6940
const args = [prompt, '-p', '--agent', agent]
7041
if (prompt) {
7142
args.push(prompt)

sdk/src/constants.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,15 @@
11
export const CODEBUFF_BINARY = 'codebuff'
2+
3+
export const IS_DEV = process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'dev'
4+
export const IS_TEST = process.env.NEXT_PUBLIC_CB_ENVIRONMENT === 'test'
5+
export const IS_PROD = !IS_DEV && !IS_TEST
6+
7+
export const WEBSOCKET_URL = IS_PROD
8+
? 'wss://manicode-backend.onrender.com/ws'
9+
: 'ws://localhost:4242/ws'
10+
export const WEBSITE_URL = IS_PROD
11+
? 'https://codebuff.com'
12+
: 'http://localhost:3000'
13+
export const BACKEND_URL = IS_PROD
14+
? 'https://manicode-backend.onrender.com'
15+
: 'http://localhost:4242'

sdk/src/types.ts

Lines changed: 0 additions & 27 deletions
This file was deleted.

0 commit comments

Comments
 (0)