Skip to content

Commit 268f286

Browse files
committed
pass in logger to searchLibraries
1 parent dfb32f6 commit 268f286

File tree

4 files changed

+49
-57
lines changed

4 files changed

+49
-57
lines changed

backend/src/__tests__/prompt-caching-subagents.test.ts

Lines changed: 18 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
import { TEST_USER_ID } from '@codebuff/common/old-constants'
2-
import {
3-
clearMockedModules,
4-
mockModule,
5-
} from '@codebuff/common/testing/mock-modules'
62
import { getInitialSessionState } from '@codebuff/common/types/session-state'
73
import {
84
spyOn,
95
beforeEach,
106
afterEach,
11-
beforeAll,
12-
afterAll,
137
describe,
148
expect,
159
it,
@@ -23,8 +17,16 @@ import * as websocketAction from '../websockets/websocket-action'
2317
import type { AgentTemplate } from '../templates/types'
2418
import type { Message } from '@codebuff/common/types/messages/codebuff-message'
2519
import type { ProjectFileContext } from '@codebuff/common/util/file'
20+
import type { Logger } from '@codebuff/types/logger'
2621
import type { WebSocket } from 'ws'
2722

23+
const logger: Logger = {
24+
debug: () => {},
25+
info: () => {},
26+
warn: () => {},
27+
error: () => {},
28+
}
29+
2830
const mockFileContext: ProjectFileContext = {
2931
projectRoot: '/test',
3032
cwd: '/test',
@@ -62,19 +64,6 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
6264
let mockLocalAgentTemplates: Record<string, AgentTemplate>
6365
let capturedMessages: Message[] = []
6466

65-
beforeAll(() => {
66-
// Mock logger
67-
mockModule('@codebuff/backend/util/logger', () => ({
68-
logger: {
69-
debug: () => {},
70-
error: () => {},
71-
info: () => {},
72-
warn: () => {},
73-
},
74-
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
75-
}))
76-
})
77-
7867
beforeEach(() => {
7968
capturedMessages = []
8069

@@ -167,10 +156,6 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
167156
mock.restore()
168157
})
169158

170-
afterAll(() => {
171-
clearMockedModules()
172-
})
173-
174159
it('should inherit parent system prompt when inheritParentSystemPrompt is true', async () => {
175160
const sessionState = getInitialSessionState(mockFileContext)
176161
const ws = new MockWebSocket() as unknown as WebSocket
@@ -189,7 +174,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
189174
userId: TEST_USER_ID,
190175
clientSessionId: 'test-session',
191176
onResponseChunk: () => {},
192-
logger: console as any,
177+
logger,
193178
})
194179

195180
// Capture parent's messages which include the system prompt
@@ -224,7 +209,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
224209
clientSessionId: 'test-session',
225210
onResponseChunk: () => {},
226211
parentSystemPrompt: parentSystemPrompt,
227-
logger: console as any,
212+
logger,
228213
})
229214

230215
// Verify child uses parent's system prompt
@@ -272,7 +257,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
272257
userId: TEST_USER_ID,
273258
clientSessionId: 'test-session',
274259
onResponseChunk: () => {},
275-
logger: console as any,
260+
logger,
276261
})
277262

278263
const parentMessages = capturedMessages
@@ -301,7 +286,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
301286
clientSessionId: 'test-session',
302287
onResponseChunk: () => {},
303288
parentSystemPrompt: parentSystemPrompt,
304-
logger: console as any,
289+
logger,
305290
})
306291

307292
const childMessages = capturedMessages
@@ -350,7 +335,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
350335
userId: TEST_USER_ID,
351336
clientSessionId: 'test-session',
352337
onResponseChunk: () => {},
353-
logger: console as any,
338+
logger,
354339
})
355340

356341
const parentMessages = capturedMessages
@@ -382,7 +367,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
382367
clientSessionId: 'test-session',
383368
onResponseChunk: () => {},
384369
parentSystemPrompt: parentSystemPrompt,
385-
logger: console as any,
370+
logger,
386371
})
387372

388373
const childMessages = capturedMessages
@@ -457,7 +442,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
457442
userId: TEST_USER_ID,
458443
clientSessionId: 'test-session',
459444
onResponseChunk: () => {},
460-
logger: console as any,
445+
logger,
461446
})
462447

463448
const parentMessages = capturedMessages
@@ -486,7 +471,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
486471
clientSessionId: 'test-session',
487472
onResponseChunk: () => {},
488473
parentSystemPrompt: parentSystemPrompt,
489-
logger: console as any,
474+
logger,
490475
})
491476

492477
const childMessages = capturedMessages
@@ -545,7 +530,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
545530
userId: TEST_USER_ID,
546531
clientSessionId: 'test-session',
547532
onResponseChunk: () => {},
548-
logger: console as any,
533+
logger,
549534
})
550535

551536
const parentMessages = capturedMessages
@@ -577,7 +562,7 @@ describe('Prompt Caching for Subagents with inheritParentSystemPrompt', () => {
577562
clientSessionId: 'test-session',
578563
onResponseChunk: () => {},
579564
parentSystemPrompt: parentSystemPrompt,
580-
logger: console as any,
565+
logger,
581566
})
582567

583568
const childMessages = capturedMessages

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,11 @@ export async function getDocumentationForQuery(params: {
6969
const allRawChunks = (
7070
await Promise.all(
7171
libraries.map(({ libraryName, topic }) =>
72-
fetchContext7LibraryDocumentation(libraryName, {
72+
fetchContext7LibraryDocumentation({
73+
query: libraryName,
7374
tokens,
7475
topic,
76+
logger,
7577
}),
7678
),
7779
)

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

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

4-
import { logger } from '../util/logger'
4+
import type { ParamsOf } from '@codebuff/types/common'
5+
import type { Logger } from '@codebuff/types/logger'
56

67
const CONTEXT7_API_BASE_URL = 'https://context7.com/api/v1'
78
const DEFAULT_TYPE = 'txt'
@@ -42,9 +43,12 @@ export interface SearchResult {
4243
* Lists all available documentation projects from Context7
4344
* @returns Array of projects with their metadata, or null if the request fails
4445
*/
45-
export async function searchLibraries(
46-
query: string,
47-
): Promise<SearchResult[] | null> {
46+
export async function searchLibraries(params: {
47+
query: string
48+
logger: Logger
49+
}): Promise<SearchResult[] | null> {
50+
const { query, logger } = params
51+
4852
const searchStartTime = Date.now()
4953
const searchContext = {
5054
query,
@@ -127,23 +131,26 @@ export async function searchLibraries(
127131
* @returns The documentation text or null if the request fails
128132
*/
129133
export async function fetchContext7LibraryDocumentation(
130-
query: string,
131-
options: {
134+
params: {
135+
query: string
132136
tokens?: number
133137
topic?: string
134138
folders?: string
135-
} = {},
139+
logger: Logger
140+
} & ParamsOf<typeof searchLibraries>,
136141
): Promise<string | null> {
142+
const { query, tokens, topic, folders, logger } = params
143+
137144
const apiStartTime = Date.now()
138145
const apiContext = {
139146
query,
140-
requestedTokens: options.tokens,
141-
topic: options.topic,
142-
folders: options.folders,
147+
requestedTokens: tokens,
148+
topic,
149+
folders,
143150
}
144151

145152
const searchStartTime = Date.now()
146-
const libraries = await searchLibraries(query)
153+
const libraries = await searchLibraries(params)
147154
const searchDuration = Date.now() - searchStartTime
148155

149156
if (!libraries || libraries.length === 0) {
@@ -179,10 +186,9 @@ export async function fetchContext7LibraryDocumentation(
179186

180187
try {
181188
const url = new URL(`${CONTEXT7_API_BASE_URL}/${libraryId}`)
182-
if (options.tokens)
183-
url.searchParams.set('tokens', options.tokens.toString())
184-
if (options.topic) url.searchParams.set('topic', options.topic)
185-
if (options.folders) url.searchParams.set('folders', options.folders)
189+
if (tokens) url.searchParams.set('tokens', tokens.toString())
190+
if (topic) url.searchParams.set('topic', topic)
191+
if (folders) url.searchParams.set('folders', folders)
186192
url.searchParams.set('type', DEFAULT_TYPE)
187193

188194
const fetchStartTime = Date.now()

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

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { fetchContext7LibraryDocumentation } from '../../../llm-apis/context7-api'
22

33
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
4-
import type { Logger } from '@codebuff/types/logger'
54
import type {
65
CodebuffToolCall,
76
CodebuffToolOutput,
87
} from '@codebuff/common/tools/list'
8+
import type { Logger } from '@codebuff/types/logger'
99

1010
export const handleReadDocs = (({
1111
previousToolCallFinished,
@@ -60,13 +60,12 @@ export const handleReadDocs = (({
6060

6161
const documentationPromise = (async () => {
6262
try {
63-
const documentation = await fetchContext7LibraryDocumentation(
64-
libraryTitle,
65-
{
66-
topic,
67-
tokens: max_tokens,
68-
},
69-
)
63+
const documentation = await fetchContext7LibraryDocumentation({
64+
query: libraryTitle,
65+
topic,
66+
tokens: max_tokens,
67+
logger,
68+
})
7069

7170
const docsDuration = Date.now() - docsStartTime
7271
const resultLength = documentation?.length || 0

0 commit comments

Comments
 (0)