-
Notifications
You must be signed in to change notification settings - Fork 515
Expand file tree
/
Copy pathclient-wrapper.ts
More file actions
250 lines (231 loc) · 6.53 KB
/
client-wrapper.ts
File metadata and controls
250 lines (231 loc) · 6.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { toOptionalFile } from '@codebuff/common/old-constants'
import { ensureEndsWithNewline } from '@codebuff/common/util/file'
import { generateCompactId } from '@codebuff/common/util/string'
import { subscribeToAction } from './websockets/websocket-action'
import type { ServerAction } from '@codebuff/common/actions'
import type {
HandleStepsLogChunkFn,
RequestFilesFn,
RequestMcpToolDataFn,
RequestOptionalFileFn,
SendSubagentChunkFn,
} from '@codebuff/common/types/contracts/client'
import type { ParamsOf } from '@codebuff/common/types/function-params'
import type { MCPConfig } from '@codebuff/common/types/mcp'
import type { ToolResultOutput } from '@codebuff/common/types/messages/content-part'
import type { ServerMessage } from '@codebuff/common/websockets/websocket-schema'
import type { WebSocket } from 'ws'
function sendMessage(ws: WebSocket, server: ServerMessage) {
ws.send(JSON.stringify(server))
}
/**
* Sends an action to the client via WebSocket
* @param ws - The WebSocket connection to send the action to
* @param action - The server action to send
*/
export function sendActionWs(params: { ws: WebSocket; action: ServerAction }) {
const { ws, action } = params
sendMessage(ws, {
type: 'action',
data: action,
})
}
/**
* Requests a tool call execution from the client with timeout support
* @param ws - The WebSocket connection
* @param toolName - Name of the tool to execute
* @param input - Arguments for the tool (can include timeout)
* @returns Promise resolving to the tool execution result
*/
export async function requestToolCallWs(params: {
ws: WebSocket
userInputId: string
toolName: string
input: Record<string, any> & { timeout_seconds?: number }
mcpConfig?: MCPConfig
}): Promise<{
output: ToolResultOutput[]
}> {
const { ws, userInputId, toolName, input, mcpConfig } = params
return new Promise((resolve) => {
const requestId = generateCompactId()
const timeoutInSeconds =
(input.timeout_seconds || 30) < 0
? undefined
: input.timeout_seconds || 30
// Set up timeout
const timeoutHandle =
timeoutInSeconds === undefined
? undefined
: setTimeout(
() => {
unsubscribe()
resolve({
output: [
{
type: 'json',
value: {
errorMessage: `Tool call '${toolName}' timed out after ${timeoutInSeconds}s`,
},
},
],
})
},
timeoutInSeconds * 1000 + 5000, // Convert to ms and add a small buffer
)
// Subscribe to response
const unsubscribe = subscribeToAction('tool-call-response', (action) => {
if (action.requestId === requestId) {
clearTimeout(timeoutHandle)
unsubscribe()
resolve({
output: action.output,
})
}
})
// Send the request
sendActionWs({
ws,
action: {
type: 'tool-call-request',
requestId,
userInputId,
toolName,
input,
timeout:
timeoutInSeconds === undefined ? undefined : timeoutInSeconds * 1000, // Send timeout in milliseconds
mcpConfig,
},
})
})
}
/**
* Requests a tool call execution from the client with timeout support
* @param ws - The WebSocket connection
* @param mcpConfig - The configuration for the MCP server
* @param input - Arguments for the tool (can include timeout)
* @returns Promise resolving to the tool execution result
*/
export async function requestMcpToolDataWs(
params: ParamsOf<RequestMcpToolDataFn> & {
ws: WebSocket
},
): ReturnType<RequestMcpToolDataFn> {
const { ws, mcpConfig, toolNames } = params
return new Promise((resolve) => {
const requestId = generateCompactId()
// Set up timeout
const timeoutHandle = setTimeout(
() => {
unsubscribe()
resolve([])
},
45_000 + 5000, // Convert to ms and add a small buffer
)
// Subscribe to response
const unsubscribe = subscribeToAction('mcp-tool-data', (action) => {
if (action.requestId === requestId) {
clearTimeout(timeoutHandle)
unsubscribe()
resolve(action.tools)
}
})
// Send the request
sendActionWs({
ws,
action: {
type: 'request-mcp-tool-data',
mcpConfig,
requestId,
...(toolNames && { toolNames }),
},
})
})
}
/**
* Requests multiple files from the client
* @param ws - The WebSocket connection
* @param filePaths - Array of file paths to request
* @returns Promise resolving to an object mapping file paths to their contents
*/
export async function requestFilesWs(
params: {
ws: WebSocket
} & ParamsOf<RequestFilesFn>,
): ReturnType<RequestFilesFn> {
const { ws, filePaths } = params
return new Promise<Record<string, string | null>>((resolve) => {
const requestId = generateCompactId()
const unsubscribe = subscribeToAction('read-files-response', (action) => {
for (const [filename, contents] of Object.entries(action.files)) {
action.files[filename] = ensureEndsWithNewline(contents)
}
if (action.requestId === requestId) {
unsubscribe()
resolve(action.files)
}
})
sendActionWs({
ws,
action: {
type: 'read-files',
filePaths,
requestId,
},
})
})
}
export async function requestOptionalFileWs(
params: {
ws: WebSocket
} & ParamsOf<RequestOptionalFileFn>,
): ReturnType<RequestOptionalFileFn> {
const { ws, filePath } = params
const files = await requestFilesWs({ ws, filePaths: [filePath] })
return toOptionalFile(files[filePath] ?? null)
}
export function sendSubagentChunkWs(
params: {
ws: WebSocket
} & ParamsOf<SendSubagentChunkFn>,
): ReturnType<SendSubagentChunkFn> {
const {
ws,
userInputId,
agentId,
agentType,
chunk,
prompt,
forwardToPrompt = true,
} = params
return sendActionWs({
ws,
action: {
type: 'subagent-response-chunk',
userInputId,
agentId,
agentType,
chunk,
prompt,
forwardToPrompt,
},
})
}
export function handleStepsLogChunkWs(
params: {
ws: WebSocket
} & ParamsOf<HandleStepsLogChunkFn>,
): ReturnType<HandleStepsLogChunkFn> {
const { ws, userInputId, runId, level, data, message } = params
return sendActionWs({
ws,
action: {
type: 'handlesteps-log-chunk',
userInputId,
agentId: runId,
level,
data,
message,
},
})
}