Skip to content

Commit db9a299

Browse files
committed
inject fetch into backend
1 parent f2bad53 commit db9a299

File tree

9 files changed

+62
-50
lines changed

9 files changed

+62
-50
lines changed

backend/src/get-documentation-for-query.ts

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,20 @@ const DELIMITER = `\n\n----------------------------------------\n\n`
2727
export async function getDocumentationForQuery(
2828
params: {
2929
query: string
30-
tokens?: number
3130
clientSessionId: string
3231
userInputId: string
3332
fingerprintId: string
3433
userId?: string
3534
logger: Logger
3635
} & ParamsOf<typeof suggestLibraries> &
37-
ParamsExcluding<typeof filterRelevantChunks, 'allChunks'>,
36+
ParamsExcluding<typeof filterRelevantChunks, 'allChunks'> &
37+
ParamsExcluding<
38+
typeof fetchContext7LibraryDocumentation,
39+
'query' | 'topic'
40+
>,
3841
): Promise<string | null> {
39-
const {
40-
query,
41-
tokens,
42-
clientSessionId,
43-
userInputId,
44-
fingerprintId,
45-
userId,
46-
logger,
47-
} = params
42+
const { query, clientSessionId, userInputId, fingerprintId, userId, logger } =
43+
params
4844
const startTime = Date.now()
4945

5046
// 1. Search for relevant libraries
@@ -70,10 +66,9 @@ export async function getDocumentationForQuery(
7066
await Promise.all(
7167
libraries.map(({ libraryName, topic }) =>
7268
fetchContext7LibraryDocumentation({
69+
...params,
7370
query: libraryName,
74-
tokens,
7571
topic,
76-
logger,
7772
}),
7873
),
7974
)

backend/src/impl/agent-runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ export const BACKEND_AGENT_RUNTIME_IMPL: AgentRuntimeDeps = Object.freeze({
3131

3232
// Other
3333
logger,
34+
fetch: globalThis.fetch,
3435
})

backend/src/llm-apis/context7-api.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { withTimeout } from '@codebuff/common/util/promise'
22
import { env } from '@codebuff/internal/env'
33

4-
import type { ParamsOf } from '@codebuff/common/types/function-params'
54
import type { Logger } from '@codebuff/common/types/contracts/logger'
5+
import type { ParamsOf } from '@codebuff/common/types/function-params'
66

77
const CONTEXT7_API_BASE_URL = 'https://context7.com/api/v1'
88
const DEFAULT_TYPE = 'txt'
@@ -46,8 +46,9 @@ export interface SearchResult {
4646
export async function searchLibraries(params: {
4747
query: string
4848
logger: Logger
49+
fetch: typeof globalThis.fetch
4950
}): Promise<SearchResult[] | null> {
50-
const { query, logger } = params
51+
const { query, logger, fetch } = params
5152

5253
const searchStartTime = Date.now()
5354
const searchContext = {
@@ -137,9 +138,10 @@ export async function fetchContext7LibraryDocumentation(
137138
topic?: string
138139
folders?: string
139140
logger: Logger
141+
fetch: typeof globalThis.fetch
140142
} & ParamsOf<typeof searchLibraries>,
141143
): Promise<string | null> {
142-
const { query, tokens, topic, folders, logger } = params
144+
const { query, tokens, topic, folders, logger, fetch } = params
143145

144146
const apiStartTime = Date.now()
145147
const apiContext = {

backend/src/tools/handlers/tool/read-docs.ts

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,40 @@ import type {
66
CodebuffToolOutput,
77
} from '@codebuff/common/tools/list'
88
import type { Logger } from '@codebuff/common/types/contracts/logger'
9+
import type { ParamsExcluding } from '@codebuff/common/types/function-params'
910

10-
export const handleReadDocs = (({
11-
previousToolCallFinished,
12-
toolCall,
13-
logger,
14-
agentStepId,
15-
clientSessionId,
16-
userInputId,
17-
state,
18-
}: {
19-
previousToolCallFinished: Promise<void>
20-
toolCall: CodebuffToolCall<'read_docs'>
21-
logger: Logger
11+
export const handleReadDocs = ((
12+
params: {
13+
previousToolCallFinished: Promise<void>
14+
toolCall: CodebuffToolCall<'read_docs'>
15+
logger: Logger
2216

23-
agentStepId: string
24-
clientSessionId: string
25-
userInputId: string
17+
agentStepId: string
18+
clientSessionId: string
19+
userInputId: string
2620

27-
state: {
28-
userId?: string
29-
fingerprintId?: string
30-
repoId?: string
31-
}
32-
}): {
21+
state: {
22+
userId?: string
23+
fingerprintId?: string
24+
repoId?: string
25+
}
26+
} & ParamsExcluding<
27+
typeof fetchContext7LibraryDocumentation,
28+
'query' | 'topic' | 'tokens'
29+
>,
30+
): {
3331
result: Promise<CodebuffToolOutput<'read_docs'>>
3432
state: {}
3533
} => {
34+
const {
35+
previousToolCallFinished,
36+
toolCall,
37+
logger,
38+
agentStepId,
39+
clientSessionId,
40+
userInputId,
41+
state,
42+
} = params
3643
const { libraryTitle, topic, max_tokens } = toolCall.input
3744
const { userId, fingerprintId, repoId } = state
3845
if (!userId) {
@@ -61,10 +68,10 @@ export const handleReadDocs = (({
6168
const documentationPromise = (async () => {
6269
try {
6370
const documentation = await fetchContext7LibraryDocumentation({
71+
...params,
6472
query: libraryTitle,
6573
topic,
6674
tokens: max_tokens,
67-
logger,
6875
})
6976

7077
const docsDuration = Date.now() - docsStartTime

backend/src/tools/tool-executor.ts

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { checkLiveUserInput } from '@codebuff/agent-runtime/live-user-inputs'
22
import { getMCPToolData } from '@codebuff/agent-runtime/mcp'
3+
import { codebuffToolDefs } from '@codebuff/agent-runtime/tools/definitions/list'
34
import { endsAgentStepParam } from '@codebuff/common/tools/constants'
45
import { generateCompactId } from '@codebuff/common/util/string'
56
import { type ToolCallPart } from 'ai'
67
import { cloneDeep } from 'lodash'
78
import z from 'zod/v4'
89
import { convertJsonSchemaToZod } from 'zod-from-json-schema'
910

10-
import { codebuffToolDefs } from '@codebuff/agent-runtime/tools/definitions/list'
1111
import { codebuffToolHandlers } from './handlers/list'
1212

1313
import type { AgentTemplate } from '@codebuff/agent-runtime/templates/types'
@@ -133,6 +133,7 @@ export type ExecuteToolCallParams<T extends string = ToolName> = {
133133
userId: string | undefined
134134
autoInsertEndStepParam?: boolean
135135
excludeToolFromMessageHistory?: boolean
136+
fetch: typeof globalThis.fetch
136137
fromHandleSteps?: boolean
137138
} & AgentRuntimeDeps &
138139
AgentRuntimeScopedDeps
@@ -231,19 +232,14 @@ export function executeToolCall<T extends ToolName>(
231232
}
232233

233234
// Cast to any to avoid type errors
234-
const handler = codebuffToolHandlers[toolName] as any
235+
const handler = codebuffToolHandlers[
236+
toolName
237+
] as unknown as CodebuffToolHandlerFunction<T>
235238
const { result: toolResultPromise, state: stateUpdate } = handler({
236239
...params,
237240
previousToolCallFinished,
238-
fileContext,
239-
agentStepId,
240-
clientSessionId,
241-
userInputId,
242-
repoUrl,
243-
repoId,
244-
fullResponse,
245241
writeToClient: onResponseChunk,
246-
requestClientToolCall: async (
242+
requestClientToolCall: (async (
247243
clientToolCall: ClientToolCall<T extends ClientToolName ? T : never>,
248244
) => {
249245
if (!checkLiveUserInput(params)) {
@@ -256,11 +252,11 @@ export function executeToolCall<T extends ToolName>(
256252
input: clientToolCall.input,
257253
})
258254
return clientToolResult.output as CodebuffToolOutput<T>
259-
},
255+
}) as any,
260256
toolCall,
261257
getLatestState: () => state,
262258
state,
263-
}) as ReturnType<CodebuffToolHandlerFunction<T>>
259+
})
264260

265261
for (const [key, value] of Object.entries(stateUpdate ?? {})) {
266262
if (key === 'agentState' && typeof value === 'object' && value !== null) {

common/src/testing/impl/agent-runtime.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ export const testLogger: Logger = {
1212
warn: () => {},
1313
}
1414

15+
export const testFetch = async () => {
16+
throw new Error('fetch not implemented in test runtime')
17+
}
18+
testFetch.preconnect = async () => {
19+
throw new Error('fetch.preconnect not implemented in test runtime')
20+
}
21+
1522
export const TEST_AGENT_RUNTIME_IMPL = Object.freeze<AgentRuntimeDeps>({
1623
// Database
1724
getUserInfoFromApiKey: async () => ({
@@ -42,6 +49,7 @@ export const TEST_AGENT_RUNTIME_IMPL = Object.freeze<AgentRuntimeDeps>({
4249

4350
// Other
4451
logger: testLogger,
52+
fetch: testFetch,
4553
})
4654

4755
export const TEST_AGENT_RUNTIME_SCOPED_IMPL =

common/src/types/contracts/agent-runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export type AgentRuntimeDeps = {
4343

4444
// Other
4545
logger: Logger
46+
fetch: typeof globalThis.fetch
4647
}
4748

4849
export type AgentRuntimeScopedDeps = {

evals/impl/agent-runtime.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,5 @@ export const EVALS_AGENT_RUNTIME_IMPL = Object.freeze<AgentRuntimeDeps>({
3131

3232
// Other
3333
logger: console,
34+
fetch: globalThis.fetch,
3435
})

packages/agent-runtime/src/tools/handlers/handler-function-type.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ export type CodebuffToolHandlerFunction<T extends ToolName = ToolName> = (
3030
fileContext: ProjectFileContext
3131

3232
fullResponse: string
33+
fetch: typeof globalThis.fetch
3334

3435
writeToClient: (chunk: string | PrintModeEvent) => void
3536

0 commit comments

Comments
 (0)