Skip to content

Commit 58db758

Browse files
committed
use generic ServerAction and ClientAction instead of Extract
1 parent ff92564 commit 58db758

8 files changed

Lines changed: 29 additions & 49 deletions

File tree

backend/src/loop-main-prompt.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const DEFAULT_MAX_ITERATIONS = 20
1313

1414
export async function loopMainPrompt(
1515
ws: WebSocket,
16-
action: Extract<ClientAction, { type: 'prompt' }>,
16+
action: ClientAction<'prompt'>,
1717
options: MainPromptOptions & { maxIterations?: number },
1818
): Promise<{
1919
sessionState: SessionState
@@ -32,7 +32,7 @@ export async function loopMainPrompt(
3232
toolCalls.length > 0 &&
3333
!toolCalls.some((tc) => tc.toolName === 'end_turn')
3434
) {
35-
const nextAction: Extract<ClientAction, { type: 'prompt' }> = {
35+
const nextAction: ClientAction<'prompt'> = {
3636
...action,
3737
sessionState,
3838
toolResults,

backend/src/main-prompt.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import { uniq } from 'lodash'
2-
import type { WebSocket } from 'ws'
3-
41
import { renderToolResults } from '@codebuff/common/tools/utils'
52
import { AgentTemplateTypes } from '@codebuff/common/types/session-state'
3+
import { uniq } from 'lodash'
64

75
import { checkTerminalCommand } from './check-terminal-command'
86
import { loopAgentSteps } from './run-agent-step'
@@ -21,6 +19,7 @@ import type {
2119
ToolResult,
2220
AgentTemplateType,
2321
} from '@codebuff/common/types/session-state'
22+
import type { WebSocket } from 'ws'
2423

2524
export interface MainPromptOptions {
2625
userId: string | undefined
@@ -31,7 +30,7 @@ export interface MainPromptOptions {
3130

3231
export const mainPrompt = async (
3332
ws: WebSocket,
34-
action: Extract<ClientAction, { type: 'prompt' }>,
33+
action: ClientAction<'prompt'>,
3534
options: MainPromptOptions,
3635
): Promise<{
3736
sessionState: SessionState

backend/src/websockets/middleware.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export class WebSocketMiddleware {
5353

5454
use<T extends ClientAction['type']>(
5555
callback: (
56-
action: Extract<ClientAction, { type: T }>,
56+
action: ClientAction<T>,
5757
clientSessionId: string,
5858
ws: WebSocket,
5959
userInfo: UserInfo | undefined,
@@ -100,14 +100,14 @@ export class WebSocketMiddleware {
100100

101101
run<T extends ClientAction['type']>(
102102
baseAction: (
103-
action: Extract<ClientAction, { type: T }>,
103+
action: ClientAction<T>,
104104
clientSessionId: string,
105105
ws: WebSocket,
106106
) => void,
107107
options: { silent?: boolean } = {},
108108
) {
109109
return async (
110-
action: Extract<ClientAction, { type: T }>,
110+
action: ClientAction<T>,
111111
clientSessionId: string,
112112
ws: WebSocket,
113113
) => {

backend/src/websockets/websocket-action.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ export async function genUsageResponse(
132132
* @param ws - The WebSocket connection
133133
*/
134134
const onPrompt = async (
135-
action: Extract<ClientAction, { type: 'prompt' }>,
135+
action: ClientAction<'prompt'>,
136136
clientSessionId: string,
137137
ws: WebSocket,
138138
) => {
@@ -187,7 +187,7 @@ const onPrompt = async (
187187

188188
export const callMainPrompt = async (
189189
ws: WebSocket,
190-
action: Extract<ClientAction, { type: 'prompt' }>,
190+
action: ClientAction<'prompt'>,
191191
options: {
192192
userId: string
193193
promptId: string
@@ -246,11 +246,7 @@ export const callMainPrompt = async (
246246
* @param ws - The WebSocket connection
247247
*/
248248
const onInit = async (
249-
{
250-
fileContext,
251-
fingerprintId,
252-
authToken,
253-
}: Extract<ClientAction, { type: 'init' }>,
249+
{ fileContext, fingerprintId, authToken }: ClientAction<'init'>,
254250
clientSessionId: string,
255251
ws: WebSocket,
256252
) => {
@@ -284,7 +280,7 @@ const onInit = async (
284280
const onCancelUserInput = async ({
285281
authToken,
286282
promptId,
287-
}: Extract<ClientAction, { type: 'cancel-user-input' }>) => {
283+
}: ClientAction<'cancel-user-input'>) => {
288284
const userId = await getUserIdFromAuthToken(authToken)
289285
if (!userId) {
290286
logger.error({ authToken }, 'User id not found for authToken')
@@ -313,7 +309,7 @@ const callbacksByAction = {} as Record<
313309
export const subscribeToAction = <T extends ClientAction['type']>(
314310
type: T,
315311
callback: (
316-
action: Extract<ClientAction, { type: T }>,
312+
action: ClientAction<T>,
317313
clientSessionId: string,
318314
ws: WebSocket,
319315
) => void,

common/src/websockets/websocket-client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ export class APIRealtimeClient {
235235

236236
subscribe<T extends ServerAction['type']>(
237237
action: T,
238-
callback: (action: Extract<ServerAction, { type: T }>) => void,
238+
callback: (action: ServerAction<T>) => void,
239239
) {
240240
const currSubscribers = this.subscribers.get(action) ?? []
241241
this.subscribers.set(action, [

npm-app/src/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@ Go to https://www.codebuff.com/config for more information.`) +
15751575
this.setUsage(parsedAction.data)
15761576
})
15771577

1578-
const initAction: Extract<ClientAction, { type: 'init' }> = {
1578+
const initAction: ClientAction<'init'> = {
15791579
type: 'init',
15801580
fingerprintId: await this.fingerprintId,
15811581
authToken: this.user?.authToken,

sdk/src/client.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import {
1212
import { API_KEY_ENV_VAR } from '../../common/src/constants'
1313
import { getInitialSessionState } from '../../common/src/types/session-state'
1414

15+
import type { AgentConfig } from './types/agent-config'
1516
import type { PrintModeEvent } from '../../common/src/types/print-mode'
1617
import type { SessionState } from '../../common/src/types/session-state'
17-
import type { AgentConfig } from './types/agent-config'
1818

1919
type ClientToolName = 'write_file' | 'run_terminal_command'
2020

@@ -27,7 +27,7 @@ export type CodebuffClientOptions = {
2727
Record<
2828
ClientToolName,
2929
(
30-
args: Extract<ServerAction, { type: 'tool-call-request' }>['args'],
30+
args: ServerAction<'tool-call-request'>['args'],
3131
) => Promise<{ toolResultMessage: string }>
3232
> & {
3333
// Include read_files separately, since it has a different signature.
@@ -40,7 +40,7 @@ export type CodebuffClientOptions = {
4040

4141
type RunState = {
4242
sessionState: SessionState
43-
toolResults: Extract<ServerAction, { type: 'prompt-response' }>['toolResults']
43+
toolResults: ServerAction<'prompt-response'>['toolResults']
4444
}
4545

4646
export class CodebuffClient {
@@ -176,9 +176,7 @@ export class CodebuffClient {
176176
})
177177
}
178178

179-
private async handlePromptResponse(
180-
action: Extract<ServerAction, { type: 'prompt-response' }>,
181-
) {
179+
private async handlePromptResponse(action: ServerAction<'prompt-response'>) {
182180
const promiseActions =
183181
this.promptIdToResolveResponse[action?.promptId ?? '']
184182

@@ -217,9 +215,7 @@ export class CodebuffClient {
217215
return getFiles(filePath, this.cwd)
218216
}
219217

220-
private async handleToolCall(
221-
action: Extract<ServerAction, { type: 'tool-call-request' }>,
222-
) {
218+
private async handleToolCall(action: ServerAction<'tool-call-request'>) {
223219
const toolName = action.toolName
224220
const args = action.args
225221
let result: string

sdk/src/websocket-client.ts

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,23 @@ export type WebSocketHandlerOptions = {
88
onWebsocketError?: (error: WebSocket.ErrorEvent) => void
99
onWebsocketReconnect?: () => void
1010
onRequestReconnect?: () => Promise<void>
11-
onResponseError?: (
12-
error: Extract<ServerAction, { type: 'action-error' }>,
13-
) => Promise<void>
11+
onResponseError?: (error: ServerAction<'action-error'>) => Promise<void>
1412
readFiles: (
1513
filePath: string[],
16-
) => Promise<Extract<ClientAction, { type: 'read-files-response' }>['files']>
14+
) => Promise<ClientAction<'read-files-response'>['files']>
1715
handleToolCall: (
18-
action: Extract<ServerAction, { type: 'tool-call-request' }>,
19-
) => Promise<
20-
Omit<
21-
Extract<ClientAction, { type: 'tool-call-response' }>,
22-
'type' | 'requestId'
23-
>
24-
>
16+
action: ServerAction<'tool-call-request'>,
17+
) => Promise<Omit<ClientAction<'tool-call-response'>, 'type' | 'requestId'>>
2518
onCostResponse?: (
26-
action: Extract<ServerAction, { type: 'message-cost-response' }>,
19+
action: ServerAction<'message-cost-response'>,
2720
) => Promise<void>
2821

29-
onResponseChunk?: (
30-
action: Extract<ServerAction, { type: 'response-chunk' }>,
31-
) => Promise<void>
22+
onResponseChunk?: (action: ServerAction<'response-chunk'>) => Promise<void>
3223
onSubagentResponseChunk?: (
33-
action: Extract<ServerAction, { type: 'subagent-response-chunk' }>,
24+
action: ServerAction<'subagent-response-chunk'>,
3425
) => Promise<void>
3526

36-
onPromptResponse?: (
37-
action: Extract<ServerAction, { type: 'prompt-response' }>,
38-
) => Promise<void>
27+
onPromptResponse?: (action: ServerAction<'prompt-response'>) => Promise<void>
3928

4029
apiKey: string
4130
}
@@ -160,7 +149,7 @@ export class WebSocketHandler {
160149

161150
public sendInput(
162151
action: Omit<
163-
Extract<ClientAction, { type: 'prompt' }>,
152+
ClientAction<'prompt'>,
164153
keyof ReturnType<typeof this.getInputDefaultOptions>
165154
>,
166155
) {

0 commit comments

Comments
 (0)