Skip to content

Commit af3f741

Browse files
committed
strongly type client tool calls
1 parent c675e57 commit af3f741

31 files changed

+121
-164
lines changed

backend/src/__tests__/subagent-streaming.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import { handleSpawnAgents } from '../tools/handlers/tool/spawn-agents'
1818
import * as loggerModule from '../util/logger'
1919

2020
import type { AgentTemplate } from '../templates/types'
21-
import type { CodebuffToolCall } from '../tools/constants'
2221
import type { SendSubagentChunk } from '../tools/handlers/tool/spawn-agents'
22+
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
2323
import type { Mock } from 'bun:test'
2424
import type { WebSocket } from 'ws'
2525

backend/src/loop-main-prompt.ts

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

backend/src/main-prompt.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { expireMessages } from './util/messages'
1111
import { requestToolCall } from './websockets/websocket-action'
1212

1313
import type { AgentTemplate } from './templates/types'
14-
import type { ClientToolCall } from './tools/constants'
1514
import type { ClientAction } from '@codebuff/common/actions'
1615
import type { CostMode } from '@codebuff/common/constants'
1716
import type { PrintModeEvent } from '@codebuff/common/types/print-mode'
@@ -35,8 +34,8 @@ export const mainPrompt = async (
3534
options: MainPromptOptions,
3635
): Promise<{
3736
sessionState: SessionState
38-
toolCalls: Array<ClientToolCall>
39-
toolResults: Array<ToolResult>
37+
toolCalls: []
38+
toolResults: ToolResult[]
4039
}> => {
4140
const { userId, clientSessionId, onResponseChunk, localAgentTemplates } =
4241
options

backend/src/run-programmatic-step.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { SandboxManager } from './util/quickjs-sandbox'
77
import { getRequestContext } from './websockets/request-context'
88
import { sendAction } from './websockets/websocket-action'
99

10-
import type { CodebuffToolCall } from './tools/constants'
10+
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
1111
import type {
1212
AgentTemplate,
1313
StepGenerator,

backend/src/tools/constants.ts

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,3 @@
11
import { endsAgentStepParam } from '@codebuff/common/tools/constants'
22

3-
import type { codebuffToolDefs } from './definitions/list'
4-
import type { FileChange } from '@codebuff/common/actions'
5-
import type { ToolName } from '@codebuff/common/tools/constants'
6-
import type { ToolCallPart } from 'ai'
7-
import type { z } from 'zod/v4'
8-
93
export const globalStopSequence = `${JSON.stringify(endsAgentStepParam)}`
10-
11-
// Tool call from LLM
12-
export type CodebuffToolCall<T extends ToolName = ToolName> = {
13-
[K in ToolName]: {
14-
toolName: K
15-
input: z.infer<(typeof codebuffToolDefs)[K]['parameters']>
16-
} & Omit<ToolCallPart, 'type'>
17-
}[T]
18-
19-
// Tool call to send to client
20-
export type ClientToolCall<T extends ToolName = ToolName> = {
21-
[K in ToolName]: {
22-
toolName: K
23-
input: K extends 'run_terminal_command'
24-
? CodebuffToolCall<'run_terminal_command'>['input'] & {
25-
mode: 'assistant' | 'user'
26-
}
27-
: K extends 'write_file' | 'str_replace' | 'create_plan'
28-
? FileChange
29-
: CodebuffToolCall<K>['input']
30-
}
31-
}[T] &
32-
Omit<ToolCallPart, 'type'>

backend/src/tools/handlers/handler-function-type.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
import type { ClientToolCall, CodebuffToolCall } from '../constants'
21
import type { ToolName } from '@codebuff/common/tools/constants'
2+
import type {
3+
ClientToolCall,
4+
ClientToolName,
5+
CodebuffToolCall,
6+
} from '@codebuff/common/tools/list'
37
import type { ProjectFileContext } from '@codebuff/common/util/file'
48

59
type PresentOrAbsent<K extends PropertyKey, V> =
@@ -24,7 +28,9 @@ export type CodebuffToolHandlerFunction<T extends ToolName = ToolName> = (
2428
state: { [K in string]?: any }
2529
} & PresentOrAbsent<
2630
'requestClientToolCall',
27-
(toolCall: ClientToolCall<T>) => Promise<string>
31+
(
32+
toolCall: ClientToolCall<T extends ClientToolName ? T : never>,
33+
) => Promise<string>
2834
>,
2935
) => {
3036
result: Promise<string | undefined>

backend/src/tools/handlers/tool/add-message.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import type { CodebuffToolCall } from '../../constants'
21
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
2+
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
33
import type { CodebuffMessage } from '@codebuff/common/types/message'
44

55
export const handleAddMessage = (({

backend/src/tools/handlers/tool/add-subgoal.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { buildArray } from '@codebuff/common/util/array'
22

3-
import type { CodebuffToolCall } from '../../constants'
43
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
4+
import type { CodebuffToolCall } from '@codebuff/common/tools/list'
55
import type { Subgoal } from '@codebuff/common/types/session-state'
66

77
export const handleAddSubgoal = ((params: {

backend/src/tools/handlers/tool/browser-logs.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import type { ClientToolCall, CodebuffToolCall } from '../../constants'
21
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
2+
import type {
3+
ClientToolCall,
4+
CodebuffToolCall,
5+
} from '@codebuff/common/tools/list'
36

47
export const handleBrowserLogs = ((params: {
58
previousToolCallFinished: Promise<void>

backend/src/tools/handlers/tool/code-search.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1-
import type { ClientToolCall, CodebuffToolCall } from '../../constants'
21
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
2+
import type {
3+
ClientToolCall,
4+
CodebuffToolCall,
5+
} from '@codebuff/common/tools/list'
36

47
export const handleCodeSearch = ((params: {
58
previousToolCallFinished: Promise<void>

0 commit comments

Comments
 (0)