Skip to content

Commit 3421c10

Browse files
committed
pass in logger to gemini with fallbacks
1 parent 268f286 commit 3421c10

File tree

5 files changed

+50
-40
lines changed

5 files changed

+50
-40
lines changed

backend/src/__tests__/read-docs-tool.test.ts

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -314,12 +314,11 @@ describe('read_docs tool with researcher agent', () => {
314314
logger,
315315
})
316316

317-
expect(context7Api.fetchContext7LibraryDocumentation).toHaveBeenCalledWith(
318-
'React',
319-
{
320-
topic: 'hooks',
321-
},
322-
)
317+
expect(context7Api.fetchContext7LibraryDocumentation).toHaveBeenCalledWith({
318+
query: 'React',
319+
topic: 'hooks',
320+
logger: expect.anything(),
321+
})
323322

324323
// Check that the documentation was added to the message history
325324
const toolResultMessages = newAgentState.messageHistory.filter(
@@ -388,13 +387,12 @@ describe('read_docs tool with researcher agent', () => {
388387
logger,
389388
})
390389

391-
expect(context7Api.fetchContext7LibraryDocumentation).toHaveBeenCalledWith(
392-
'React',
393-
{
394-
topic: 'hooks',
395-
tokens: 5000,
396-
},
397-
)
390+
expect(context7Api.fetchContext7LibraryDocumentation).toHaveBeenCalledWith({
391+
query: 'React',
392+
topic: 'hooks',
393+
tokens: 5000,
394+
logger: expect.anything(),
395+
})
398396
}, 10000)
399397

400398
test('should handle case when no documentation is found', async () => {

backend/src/__tests__/request-files-prompt.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,6 @@ describe('requestRelevantFiles', () => {
228228
expect(
229229
geminiWithFallbacksModule.promptFlashWithFallbacks,
230230
).toHaveBeenCalledWith(
231-
expect.anything(),
232231
expect.objectContaining({
233232
useFinetunedModel: finetunedVertexModels.ft_filepicker_010,
234233
}),
@@ -259,7 +258,6 @@ describe('requestRelevantFiles', () => {
259258
expect(
260259
geminiWithFallbacksModule.promptFlashWithFallbacks,
261260
).toHaveBeenCalledWith(
262-
expect.anything(),
263261
expect.objectContaining({
264262
useFinetunedModel: expectedModel,
265263
}),

backend/src/fast-rewrite.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import { promptFlashWithFallbacks } from './llm-apis/gemini-with-fallbacks'
77
import { promptRelaceAI } from './llm-apis/relace-api'
88
import { promptAiSdk } from './llm-apis/vercel-ai-sdk/ai-sdk'
99

10-
import type { Logger } from '@codebuff/types/logger'
1110
import type { CodebuffToolMessage } from '@codebuff/common/tools/list'
1211
import type {
1312
Message,
1413
ToolMessage,
1514
} from '@codebuff/common/types/messages/codebuff-message'
15+
import type { Logger } from '@codebuff/types/logger'
1616

1717
export async function fastRewrite(params: {
1818
initialContent: string
@@ -243,12 +243,14 @@ Do not write anything else.
243243
content: prompt,
244244
},
245245
)
246-
const response = await promptFlashWithFallbacks(messages, {
246+
const response = await promptFlashWithFallbacks({
247+
messages,
247248
clientSessionId,
248249
fingerprintId,
249250
userInputId,
250251
model: models.openrouter_gemini2_5_flash,
251252
userId,
253+
logger,
252254
})
253255
const shouldAddPlaceholderComments = response.includes('LOCAL_CHANGE_ONLY')
254256
logger.debug(

backend/src/find-files/request-files-prompt.ts

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import { range, shuffle, uniq } from 'lodash'
1515
import { CustomFilePickerConfigSchema } from './custom-file-picker-config'
1616
import { promptFlashWithFallbacks } from '../llm-apis/gemini-with-fallbacks'
1717
import { promptAiSdk } from '../llm-apis/vercel-ai-sdk/ai-sdk'
18-
import type { Logger } from '@codebuff/types/logger'
1918
import {
2019
castAssistantMessage,
2120
messagesWithSystem,
@@ -31,6 +30,7 @@ import type {
3130
} from '@codebuff/bigquery'
3231
import type { Message } from '@codebuff/common/types/messages/codebuff-message'
3332
import type { ProjectFileContext } from '@codebuff/common/util/file'
33+
import type { Logger } from '@codebuff/types/logger'
3434

3535
const NUMBER_OF_EXAMPLE_FILES = 100
3636
const MAX_FILES_PER_REQUEST = 30
@@ -356,7 +356,10 @@ async function getRelevantFiles(params: {
356356
logger,
357357
})
358358
const start = performance.now()
359-
let codebuffMessages = messagesWithSystem({ messages: messagesWithPrompt, system })
359+
let codebuffMessages = messagesWithSystem({
360+
messages: messagesWithPrompt,
361+
system,
362+
})
360363

361364
// Converts assistant messages to user messages for finetuned model
362365
codebuffMessages = codebuffMessages
@@ -370,13 +373,15 @@ async function getRelevantFiles(params: {
370373
.filter((msg) => msg !== null)
371374
const finetunedModel = modelId ?? finetunedVertexModels.ft_filepicker_010
372375

373-
let response = await promptFlashWithFallbacks(codebuffMessages, {
376+
let response = await promptFlashWithFallbacks({
377+
messages: codebuffMessages,
374378
clientSessionId,
375379
userInputId,
376380
model: models.openrouter_gemini2_5_flash,
377381
userId,
378382
useFinetunedModel: finetunedModel,
379383
fingerprintId,
384+
logger,
380385
})
381386
const end = performance.now()
382387
const duration = end - start
@@ -528,7 +533,10 @@ function generateNonObviousRequestFilesPrompt(
528533
fileContext: ProjectFileContext,
529534
count: number,
530535
): string {
531-
const exampleFiles = getExampleFileList({ fileContext, count: NUMBER_OF_EXAMPLE_FILES })
536+
const exampleFiles = getExampleFileList({
537+
fileContext,
538+
count: NUMBER_OF_EXAMPLE_FILES,
539+
})
532540
return `
533541
Your task is to find the second-order relevant files for the following user request (in quotes).
534542
@@ -583,7 +591,10 @@ function generateKeyRequestFilesPrompt(
583591
fileContext: ProjectFileContext,
584592
count: number,
585593
): string {
586-
const exampleFiles = getExampleFileList({ fileContext, count: NUMBER_OF_EXAMPLE_FILES })
594+
const exampleFiles = getExampleFileList({
595+
fileContext,
596+
count: NUMBER_OF_EXAMPLE_FILES,
597+
})
587598

588599
return `
589600
Your task is to find the most relevant files for the following user request (in quotes).

backend/src/llm-apis/gemini-with-fallbacks.ts

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { openaiModels, openrouterModels } from '@codebuff/common/old-constants'
22

3-
import { logger } from '../util/logger'
43
import { promptAiSdk } from './vercel-ai-sdk/ai-sdk'
54

65
import type {
@@ -9,6 +8,7 @@ import type {
98
Model,
109
} from '@codebuff/common/old-constants'
1110
import type { Message } from '@codebuff/common/types/messages/codebuff-message'
11+
import type { Logger } from '@codebuff/types/logger'
1212

1313
/**
1414
* Prompts a Gemini model with fallback logic.
@@ -35,28 +35,29 @@ import type { Message } from '@codebuff/common/types/messages/codebuff-message'
3535
* @returns A promise that resolves to the complete response string from the successful API call.
3636
* @throws If all API calls (primary and fallbacks) fail.
3737
*/
38-
export async function promptFlashWithFallbacks(
39-
messages: Message[],
40-
options: {
41-
clientSessionId: string
42-
fingerprintId: string
43-
userInputId: string
44-
model: Model
45-
userId: string | undefined
46-
maxTokens?: number
47-
temperature?: number
48-
costMode?: CostMode
49-
useGPT4oInsteadOfClaude?: boolean
50-
thinkingBudget?: number
51-
useFinetunedModel?: FinetunedVertexModel | undefined
52-
},
53-
): Promise<string> {
38+
export async function promptFlashWithFallbacks(params: {
39+
messages: Message[]
40+
clientSessionId: string
41+
fingerprintId: string
42+
userInputId: string
43+
model: Model
44+
userId: string | undefined
45+
maxTokens?: number
46+
temperature?: number
47+
costMode?: CostMode
48+
useGPT4oInsteadOfClaude?: boolean
49+
thinkingBudget?: number
50+
useFinetunedModel?: FinetunedVertexModel | undefined
51+
logger: Logger
52+
}): Promise<string> {
5453
const {
54+
messages,
5555
costMode,
5656
useGPT4oInsteadOfClaude,
5757
useFinetunedModel,
58+
logger,
5859
...geminiOptions
59-
} = options
60+
} = params
6061

6162
// Try finetuned model first if enabled
6263
if (useFinetunedModel) {

0 commit comments

Comments
 (0)