Skip to content

Commit e411838

Browse files
Refactor processFileBlock to accept logger as parameter
Updated processFileBlock and handleLargeFile functions to receive logger via params object instead of importing it directly. Updated all callers including write-file handler and tests. 🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 89eda37 commit e411838

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

backend/src/__tests__/process-file-block.test.ts

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,17 @@ import { applyPatch } from 'diff'
99

1010
import { processFileBlock } from '../process-file-block'
1111

12+
import type { Logger } from '@codebuff/types/logger'
13+
14+
const logger: Logger = {
15+
debug: () => {},
16+
info: () => {},
17+
warn: () => {},
18+
error: () => {},
19+
}
20+
1221
describe('processFileBlockModule', () => {
1322
beforeAll(() => {
14-
// Mock logger
15-
mockModule('@codebuff/backend/util/logger', () => ({
16-
logger: {
17-
debug: () => {},
18-
error: () => {},
19-
info: () => {},
20-
warn: () => {},
21-
},
22-
withLoggerContext: async (context: any, fn: () => Promise<any>) => fn(),
23-
}))
24-
2523
// Mock database interactions
2624
mockModule('pg-pool', () => ({
2725
Pool: class {
@@ -87,6 +85,7 @@ describe('processFileBlockModule', () => {
8785
fingerprintId: 'fingerprintId',
8886
userInputId: 'userInputId',
8987
userId: TEST_USER_ID,
88+
logger,
9089
})
9190

9291
expect(result).not.toBeNull()
@@ -123,6 +122,7 @@ describe('processFileBlockModule', () => {
123122
fingerprintId: 'fingerprintId',
124123
userInputId: 'userInputId',
125124
userId: TEST_USER_ID,
125+
logger,
126126
})
127127

128128
expect(result).not.toBeNull()
@@ -155,6 +155,7 @@ describe('processFileBlockModule', () => {
155155
fingerprintId: 'fingerprintId',
156156
userInputId: 'userInputId',
157157
userId: TEST_USER_ID,
158+
logger,
158159
})
159160

160161
expect(result).not.toBeNull()
@@ -180,6 +181,7 @@ describe('processFileBlockModule', () => {
180181
fingerprintId: 'fingerprintId',
181182
userInputId: 'userInputId',
182183
userId: TEST_USER_ID,
184+
logger,
183185
})
184186

185187
expect(result).not.toBeNull()
@@ -226,6 +228,7 @@ describe('processFileBlockModule', () => {
226228
fingerprintId: 'fingerprintId',
227229
userInputId: 'userInputId',
228230
userId: TEST_USER_ID,
231+
logger,
229232
})
230233

231234
expect(result).not.toBeNull()

backend/src/process-file-block.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import {
99
retryDiffBlocksPrompt,
1010
} from './generate-diffs-prompt'
1111
import { promptAiSdk } from './llm-apis/vercel-ai-sdk/ai-sdk'
12-
import { logger } from './util/logger'
1312
import { countTokens } from './util/token-counter'
1413

14+
import type { Logger } from '@codebuff/types/logger'
15+
1516
import type { Message } from '@codebuff/common/types/messages/codebuff-message'
1617

1718
export async function processFileBlock(params: {
@@ -26,6 +27,7 @@ export async function processFileBlock(params: {
2627
fingerprintId: string
2728
userInputId: string
2829
userId: string | undefined
30+
logger: Logger
2931
}): Promise<
3032
| {
3133
tool: 'write_file'
@@ -52,6 +54,7 @@ export async function processFileBlock(params: {
5254
fingerprintId,
5355
userInputId,
5456
userId,
57+
logger,
5558
} = params
5659
const initialContent = await initialContentPromise
5760

@@ -118,6 +121,7 @@ export async function processFileBlock(params: {
118121
userInputId,
119122
userId,
120123
filePath: path,
124+
logger,
121125
})
122126

123127
if (!largeFileContent) {
@@ -232,6 +236,7 @@ export async function handleLargeFile(params: {
232236
userInputId: string
233237
userId: string | undefined
234238
filePath: string
239+
logger: Logger
235240
}): Promise<string | null> {
236241
const {
237242
oldContent,
@@ -241,6 +246,7 @@ export async function handleLargeFile(params: {
241246
userInputId,
242247
userId,
243248
filePath,
249+
logger,
244250
} = params
245251
const startTime = Date.now()
246252

backend/src/tools/handlers/tool/write-file.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { partition } from 'lodash'
22

33
import { processFileBlock } from '../../../process-file-block'
4-
import { logger } from '../../../util/logger'
54
import { requestOptionalFile } from '../../../websockets/websocket-action'
65

6+
import type { Logger } from '@codebuff/types/logger'
7+
78
import type { CodebuffToolHandlerFunction } from '../handler-function-type'
89
import type {
910
ClientToolCall,
@@ -94,13 +95,14 @@ export const handleWriteFile = (({
9495
fullResponse?: string
9596
prompt?: string
9697
messages?: Message[]
98+
logger?: Logger
9799
} & OptionalFileProcessingState
98100
}): {
99101
result: Promise<CodebuffToolOutput<'write_file'>>
100102
state: FileProcessingState
101103
} => {
102104
const { path, instructions, content } = toolCall.input
103-
const { ws, fingerprintId, userId, fullResponse, prompt } = state
105+
const { ws, fingerprintId, userId, fullResponse, prompt, logger } = state
104106
if (!ws) {
105107
throw new Error('Internal error for write_file: Missing WebSocket in state')
106108
}
@@ -117,6 +119,9 @@ export const handleWriteFile = (({
117119
if (!agentMessagesUntruncated) {
118120
throw new Error('Internal error for write_file: Missing messages in state')
119121
}
122+
if (!logger) {
123+
throw new Error('Internal error for write_file: Missing logger in state')
124+
}
120125

121126
// Initialize state for this file path if needed
122127
if (!fileProcessingPromisesByPath[path]) {
@@ -151,6 +156,7 @@ export const handleWriteFile = (({
151156
fingerprintId,
152157
userInputId,
153158
userId,
159+
logger,
154160
})
155161
.catch((error) => {
156162
logger.error(error, 'Error processing write_file block')

plans/logger-refactor-plan.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
## Plan: Refactor Logger to Pass as Parameter
22

33
### Step 1: Search for logger imports
4+
45
- Use `code_search` with pattern `import \{ logger` (escape curly braces)
56
- Set `cwd: "backend"` to limit search scope
67
- Exclude websocket-action.ts files
78

89
### Step 2: For each file (except websocket-action.ts)
10+
911
- Remove the `import { logger }` line
1012
- Refactor function signature to use single `params` object containing all arguments including `logger: Logger`
1113
- Import proper type: `import type { Logger } from '@codebuff/types/logger'`
1214
- Don't manually type as `{ debug: Function; ... }` - will fail typecheck
1315
- Add destructuring at top of function body to extract params
1416

1517
### Step 3: Update all callers
18+
1619
- **Always run full `bun run typecheck`** (not head/tail!) to find ALL errors
1720
- Update function calls to pass object with named properties
18-
- For tests: create mock logger constant with all 5 methods (debug, info, warn, error, fatal)
21+
- For tests: create mock logger constant called `logger` with all 4 methods (debug, info, warn, error)
1922
- Use `allowMultiple: true` in str_replace when updating multiple calls in same file
2023
- **Check carefully** - there may be multiple call sites in the same file!
2124
- Repeat typecheck until ALL errors resolved
2225

2326
### Step 4: Commit changes
27+
2428
- **Do NOT use git-committer agent** - it's too slow
2529
- Instead, manually run: `git add <files>` then `git commit -m "<message>"`
2630
- Keep commit message concise but descriptive

0 commit comments

Comments
 (0)