Skip to content

Commit 97540cb

Browse files
Refactor get-documentation-for-query.ts to pass logger as parameter
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent d45a2a5 commit 97540cb

File tree

1 file changed

+73
-42
lines changed

1 file changed

+73
-42
lines changed

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

Lines changed: 73 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { z } from 'zod/v4'
55

66
import { fetchContext7LibraryDocumentation } from './llm-apis/context7-api'
77
import { promptAiSdkStructured } from './llm-apis/vercel-ai-sdk/ai-sdk'
8-
import { logger } from './util/logger'
8+
9+
import type { Logger } from '@codebuff/types/logger'
910

1011
const DELIMITER = `\n\n----------------------------------------\n\n`
1112

@@ -19,20 +20,35 @@ const DELIMITER = `\n\n----------------------------------------\n\n`
1920
* @param options.userId The ID of the user making the request
2021
* @returns The documentation text chunks or null if no relevant docs found
2122
*/
22-
export async function getDocumentationForQuery(
23-
query: string,
24-
options: {
25-
tokens?: number
26-
clientSessionId: string
27-
userInputId: string
28-
fingerprintId: string
29-
userId?: string
30-
},
31-
): Promise<string | null> {
23+
export async function getDocumentationForQuery(params: {
24+
query: string
25+
tokens?: number
26+
clientSessionId: string
27+
userInputId: string
28+
fingerprintId: string
29+
userId?: string
30+
logger: Logger
31+
}): Promise<string | null> {
32+
const {
33+
query,
34+
tokens,
35+
clientSessionId,
36+
userInputId,
37+
fingerprintId,
38+
userId,
39+
logger,
40+
} = params
3241
const startTime = Date.now()
3342

3443
// 1. Search for relevant libraries
35-
const libraryResults = await suggestLibraries(query, options)
44+
const libraryResults = await suggestLibraries({
45+
query,
46+
clientSessionId,
47+
userInputId,
48+
fingerprintId,
49+
userId,
50+
logger,
51+
})
3652

3753
if (!libraryResults || libraryResults.libraries.length === 0) {
3854
logger.info(
@@ -54,7 +70,7 @@ export async function getDocumentationForQuery(
5470
await Promise.all(
5571
libraries.map(({ libraryName, topic }) =>
5672
fetchContext7LibraryDocumentation(libraryName, {
57-
tokens: options.tokens,
73+
tokens,
5874
topic,
5975
}),
6076
),
@@ -85,11 +101,15 @@ export async function getDocumentationForQuery(
85101
}
86102

87103
// 3. Filter relevant chunks using another LLM call
88-
const filterResults = await filterRelevantChunks(
104+
const filterResults = await filterRelevantChunks({
89105
query,
90-
allUniqueChunks,
91-
options,
92-
)
106+
allChunks: allUniqueChunks,
107+
clientSessionId,
108+
userInputId,
109+
fingerprintId,
110+
userId,
111+
logger,
112+
})
93113

94114
const totalDuration = Date.now() - startTime
95115

@@ -135,15 +155,16 @@ export async function getDocumentationForQuery(
135155
return relevantChunks.join(DELIMITER)
136156
}
137157

138-
const suggestLibraries = async (
139-
query: string,
140-
options: {
141-
clientSessionId: string
142-
userInputId: string
143-
fingerprintId: string
144-
userId?: string
145-
},
146-
) => {
158+
const suggestLibraries = async (params: {
159+
query: string
160+
clientSessionId: string
161+
userInputId: string
162+
fingerprintId: string
163+
userId?: string
164+
logger: Logger
165+
}) => {
166+
const { query, clientSessionId, userInputId, fingerprintId, userId, logger } =
167+
params
147168
const prompt =
148169
`You are an expert at documentation for libraries. Given a user's query return a list of (library name, topic) where each library name is the name of a library and topic is a keyword or phrase that specifies a topic within the library that is most relevant to the user's query.
149170
@@ -163,9 +184,11 @@ ${closeXml('user_query')}
163184
const geminiStartTime = Date.now()
164185
try {
165186
const response = await promptAiSdkStructured({
166-
...options,
167187
messages: [{ role: 'user', content: prompt }],
168-
userId: options.userId,
188+
clientSessionId,
189+
userInputId,
190+
fingerprintId,
191+
userId,
169192
model: models.openrouter_gemini2_5_flash,
170193
temperature: 0,
171194
schema: z.object({
@@ -198,16 +221,24 @@ ${closeXml('user_query')}
198221
* @param options Common request options including session and user identifiers.
199222
* @returns A promise that resolves to an object containing the relevant chunks and Gemini call duration, or null if an error occurs.
200223
*/
201-
async function filterRelevantChunks(
202-
query: string,
203-
allChunks: string[],
204-
options: {
205-
clientSessionId: string
206-
userInputId: string
207-
fingerprintId: string
208-
userId?: string
209-
},
210-
): Promise<{ relevantChunks: string[]; geminiDuration: number } | null> {
224+
async function filterRelevantChunks(params: {
225+
query: string
226+
allChunks: string[]
227+
clientSessionId: string
228+
userInputId: string
229+
fingerprintId: string
230+
userId?: string
231+
logger: Logger
232+
}): Promise<{ relevantChunks: string[]; geminiDuration: number } | null> {
233+
const {
234+
query,
235+
allChunks,
236+
clientSessionId,
237+
userInputId,
238+
fingerprintId,
239+
userId,
240+
logger,
241+
} = params
211242
const prompt = `You are an expert at analyzing documentation queries. Given a user's query and a list of documentation chunks, determine which chunks are relevant to the query. Choose as few chunks as possible, likely none. Only include chunks if they are relevant to the user query.
212243
213244
<user_query>
@@ -223,10 +254,10 @@ ${closeXml('documentation_chunks')}
223254
try {
224255
const response = await promptAiSdkStructured({
225256
messages: [{ role: 'user', content: prompt }],
226-
clientSessionId: options.clientSessionId,
227-
userInputId: options.userInputId,
228-
fingerprintId: options.fingerprintId,
229-
userId: options.userId,
257+
clientSessionId,
258+
userInputId,
259+
fingerprintId,
260+
userId,
230261
model: models.openrouter_gemini2_5_flash,
231262
temperature: 0,
232263
schema: z.object({

0 commit comments

Comments
 (0)