Skip to content

Commit 01a4aff

Browse files
committed
pass in promptAiSdkStructured
1 parent 89ef32f commit 01a4aff

File tree

1 file changed

+31
-20
lines changed

1 file changed

+31
-20
lines changed

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

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@ import { uniq } from 'lodash'
44
import { z } from 'zod/v4'
55

66
import { fetchContext7LibraryDocumentation } from './llm-apis/context7-api'
7-
import { promptAiSdkStructured } from './llm-apis/vercel-ai-sdk/ai-sdk'
87

8+
import type { PromptAiSdkStructuredFn } from '@codebuff/common/types/contracts/llm'
99
import type { Logger } from '@codebuff/common/types/contracts/logger'
10+
import type {
11+
ParamsExcluding,
12+
ParamsOf,
13+
} from '@codebuff/common/types/function-params'
1014

1115
const DELIMITER = `\n\n----------------------------------------\n\n`
1216

@@ -20,15 +24,18 @@ const DELIMITER = `\n\n----------------------------------------\n\n`
2024
* @param options.userId The ID of the user making the request
2125
* @returns The documentation text chunks or null if no relevant docs found
2226
*/
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> {
27+
export async function getDocumentationForQuery(
28+
params: {
29+
query: string
30+
tokens?: number
31+
clientSessionId: string
32+
userInputId: string
33+
fingerprintId: string
34+
userId?: string
35+
logger: Logger
36+
} & ParamsOf<typeof suggestLibraries> &
37+
ParamsExcluding<typeof filterRelevantChunks, 'allChunks'>,
38+
): Promise<string | null> {
3239
const {
3340
query,
3441
tokens,
@@ -41,14 +48,7 @@ export async function getDocumentationForQuery(params: {
4148
const startTime = Date.now()
4249

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

5353
if (!libraryResults || libraryResults.libraries.length === 0) {
5454
logger.info(
@@ -104,6 +104,7 @@ export async function getDocumentationForQuery(params: {
104104

105105
// 3. Filter relevant chunks using another LLM call
106106
const filterResults = await filterRelevantChunks({
107+
...params,
107108
query,
108109
allChunks: allUniqueChunks,
109110
clientSessionId,
@@ -163,10 +164,18 @@ const suggestLibraries = async (params: {
163164
userInputId: string
164165
fingerprintId: string
165166
userId?: string
167+
promptAiSdkStructured: PromptAiSdkStructuredFn
166168
logger: Logger
167169
}) => {
168-
const { query, clientSessionId, userInputId, fingerprintId, userId, logger } =
169-
params
170+
const {
171+
query,
172+
clientSessionId,
173+
userInputId,
174+
fingerprintId,
175+
userId,
176+
promptAiSdkStructured,
177+
logger,
178+
} = params
170179
const prompt =
171180
`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.
172181
@@ -231,6 +240,7 @@ async function filterRelevantChunks(params: {
231240
userInputId: string
232241
fingerprintId: string
233242
userId?: string
243+
promptAiSdkStructured: PromptAiSdkStructuredFn
234244
logger: Logger
235245
}): Promise<{ relevantChunks: string[]; geminiDuration: number } | null> {
236246
const {
@@ -240,6 +250,7 @@ async function filterRelevantChunks(params: {
240250
userInputId,
241251
fingerprintId,
242252
userId,
253+
promptAiSdkStructured,
243254
logger,
244255
} = params
245256
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.

0 commit comments

Comments
 (0)