Skip to content

Commit ba842fe

Browse files
Refactor file processing API to a single-parameter object; improve maintainability and testability. Align tests and write-file flow with the new signature.
🤖 Generated with Codebuff Co-Authored-By: Codebuff <noreply@codebuff.com>
1 parent 2b1a39a commit ba842fe

File tree

4 files changed

+119
-98
lines changed

4 files changed

+119
-98
lines changed

backend/src/__tests__/main-prompt.test.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,11 @@ describe('mainPrompt', () => {
9696

9797
// Mock processFileBlock
9898
spyOn(processFileBlockModule, 'processFileBlock').mockImplementation(
99-
async (path, instructions, contentPromise, newContent) => {
99+
async (params) => {
100100
return {
101101
tool: 'write_file' as const,
102-
path,
103-
instructions,
104-
content: newContent,
102+
path: params.path,
103+
content: params.newContent,
105104
patch: undefined,
106105
messages: [],
107106
}

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

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -75,19 +75,19 @@ describe('processFileBlockModule', () => {
7575
'```typescript\nfunction test() {\n return true;\n}\n```'
7676
const expectedContent = 'function test() {\n return true;\n}'
7777

78-
const result = await processFileBlock(
79-
'test.ts',
80-
undefined,
81-
Promise.resolve(null),
78+
const result = await processFileBlock({
79+
path: 'test.ts',
80+
instructions: undefined,
81+
initialContentPromise: Promise.resolve(null),
8282
newContent,
83-
[],
84-
'',
85-
undefined,
86-
'clientSessionId',
87-
'fingerprintId',
88-
'userInputId',
89-
TEST_USER_ID,
90-
)
83+
messages: [],
84+
fullResponse: '',
85+
lastUserPrompt: undefined,
86+
clientSessionId: 'clientSessionId',
87+
fingerprintId: 'fingerprintId',
88+
userInputId: 'userInputId',
89+
userId: TEST_USER_ID,
90+
})
9191

9292
expect(result).not.toBeNull()
9393
if ('error' in result) {
@@ -111,19 +111,19 @@ describe('processFileBlockModule', () => {
111111
' return "See you later!";\r\n' +
112112
'}\r\n'
113113

114-
const result = await processFileBlock(
115-
'test.ts',
116-
undefined,
117-
Promise.resolve(oldContent),
114+
const result = await processFileBlock({
115+
path: 'test.ts',
116+
instructions: undefined,
117+
initialContentPromise: Promise.resolve(oldContent),
118118
newContent,
119-
[],
120-
'',
121-
undefined,
122-
'clientSessionId',
123-
'fingerprintId',
124-
'userInputId',
125-
TEST_USER_ID,
126-
)
119+
messages: [],
120+
fullResponse: '',
121+
lastUserPrompt: undefined,
122+
clientSessionId: 'clientSessionId',
123+
fingerprintId: 'fingerprintId',
124+
userInputId: 'userInputId',
125+
userId: TEST_USER_ID,
126+
})
127127

128128
expect(result).not.toBeNull()
129129
if ('error' in result) {
@@ -143,19 +143,19 @@ describe('processFileBlockModule', () => {
143143
const oldContent = 'function test() {\n return true;\n}\n'
144144
const newContent = 'function test() {\n return true;\n}\n'
145145

146-
const result = await processFileBlock(
147-
'test.ts',
148-
undefined,
149-
Promise.resolve(oldContent),
146+
const result = await processFileBlock({
147+
path: 'test.ts',
148+
instructions: undefined,
149+
initialContentPromise: Promise.resolve(oldContent),
150150
newContent,
151-
[],
152-
'',
153-
undefined,
154-
'clientSessionId',
155-
'fingerprintId',
156-
'userInputId',
157-
TEST_USER_ID,
158-
)
151+
messages: [],
152+
fullResponse: '',
153+
lastUserPrompt: undefined,
154+
clientSessionId: 'clientSessionId',
155+
fingerprintId: 'fingerprintId',
156+
userInputId: 'userInputId',
157+
userId: TEST_USER_ID,
158+
})
159159

160160
expect(result).not.toBeNull()
161161
expect('error' in result).toBe(true)
@@ -168,19 +168,19 @@ describe('processFileBlockModule', () => {
168168
const oldContent = 'const x = 1;\r\nconst y = 2;\r\n'
169169
const newContent = 'const x = 1;\r\nconst z = 3;\r\n'
170170

171-
const result = await processFileBlock(
172-
'test.ts',
173-
undefined,
174-
Promise.resolve(oldContent),
171+
const result = await processFileBlock({
172+
path: 'test.ts',
173+
instructions: undefined,
174+
initialContentPromise: Promise.resolve(oldContent),
175175
newContent,
176-
[],
177-
'',
178-
undefined,
179-
'clientSessionId',
180-
'fingerprintId',
181-
'userInputId',
182-
TEST_USER_ID,
183-
)
176+
messages: [],
177+
fullResponse: '',
178+
lastUserPrompt: undefined,
179+
clientSessionId: 'clientSessionId',
180+
fingerprintId: 'fingerprintId',
181+
userInputId: 'userInputId',
182+
userId: TEST_USER_ID,
183+
})
184184

185185
expect(result).not.toBeNull()
186186
if ('error' in result) {
@@ -214,19 +214,19 @@ describe('processFileBlockModule', () => {
214214
const newContent =
215215
'// ... existing code ...\nconst x = 1;\n// ... existing code ...'
216216

217-
const result = await processFileBlock(
218-
'test.ts',
219-
undefined,
220-
Promise.resolve(null),
217+
const result = await processFileBlock({
218+
path: 'test.ts',
219+
instructions: undefined,
220+
initialContentPromise: Promise.resolve(null),
221221
newContent,
222-
[],
223-
'',
224-
undefined,
225-
'clientSessionId',
226-
'fingerprintId',
227-
'userInputId',
228-
TEST_USER_ID,
229-
)
222+
messages: [],
223+
fullResponse: '',
224+
lastUserPrompt: undefined,
225+
clientSessionId: 'clientSessionId',
226+
fingerprintId: 'fingerprintId',
227+
userInputId: 'userInputId',
228+
userId: TEST_USER_ID,
229+
})
230230

231231
expect(result).not.toBeNull()
232232
expect('error' in result).toBe(true)

backend/src/process-file-block.ts

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,19 @@ import { countTokens } from './util/token-counter'
1414

1515
import type { Message } from '@codebuff/common/types/messages/codebuff-message'
1616

17-
export async function processFileBlock(
18-
path: string,
19-
instructions: string | undefined,
20-
initialContentPromise: Promise<string | null>,
21-
newContent: string,
22-
messages: Message[],
23-
fullResponse: string,
24-
lastUserPrompt: string | undefined,
25-
clientSessionId: string,
26-
fingerprintId: string,
27-
userInputId: string,
28-
userId: string | undefined,
29-
): Promise<
17+
export async function processFileBlock(params: {
18+
path: string
19+
instructions: string | undefined
20+
initialContentPromise: Promise<string | null>
21+
newContent: string
22+
messages: Message[]
23+
fullResponse: string
24+
lastUserPrompt: string | undefined
25+
clientSessionId: string
26+
fingerprintId: string
27+
userInputId: string
28+
userId: string | undefined
29+
}): Promise<
3030
| {
3131
tool: 'write_file'
3232
path: string
@@ -40,6 +40,19 @@ export async function processFileBlock(
4040
error: string // Error message if the file could not be updated
4141
}
4242
> {
43+
const {
44+
path,
45+
instructions,
46+
initialContentPromise,
47+
newContent,
48+
messages,
49+
fullResponse,
50+
lastUserPrompt,
51+
clientSessionId,
52+
fingerprintId,
53+
userInputId,
54+
userId,
55+
} = params
4356
const initialContent = await initialContentPromise
4457

4558
if (initialContent === null) {
@@ -97,15 +110,15 @@ export async function processFileBlock(
97110
'Write diff created by fast-apply model. May contain errors. Make sure to double check!',
98111
)
99112
if (tokenCount > LARGE_FILE_TOKEN_LIMIT) {
100-
const largeFileContent = await handleLargeFile(
101-
normalizedInitialContent,
102-
normalizedEditSnippet,
113+
const largeFileContent = await handleLargeFile({
114+
oldContent: normalizedInitialContent,
115+
editSnippet: normalizedEditSnippet,
103116
clientSessionId,
104117
fingerprintId,
105118
userInputId,
106119
userId,
107-
path,
108-
)
120+
filePath: path,
121+
})
109122

110123
if (!largeFileContent) {
111124
return {
@@ -211,15 +224,24 @@ export async function processFileBlock(
211224

212225
const LARGE_FILE_TOKEN_LIMIT = 64_000
213226

214-
export async function handleLargeFile(
215-
oldContent: string,
216-
editSnippet: string,
217-
clientSessionId: string,
218-
fingerprintId: string,
219-
userInputId: string,
220-
userId: string | undefined,
221-
filePath: string,
222-
): Promise<string | null> {
227+
export async function handleLargeFile(params: {
228+
oldContent: string
229+
editSnippet: string
230+
clientSessionId: string
231+
fingerprintId: string
232+
userInputId: string
233+
userId: string | undefined
234+
filePath: string
235+
}): Promise<string | null> {
236+
const {
237+
oldContent,
238+
editSnippet,
239+
clientSessionId,
240+
fingerprintId,
241+
userInputId,
242+
userId,
243+
filePath,
244+
} = params
223245
const startTime = Date.now()
224246

225247
// If the whole file is rewritten, we can just return the new content.

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -139,19 +139,19 @@ export const handleWriteFile = (({
139139

140140
logger.debug({ path, content }, `write_file ${path}`)
141141

142-
const newPromise = processFileBlock(
142+
const newPromise = processFileBlock({
143143
path,
144144
instructions,
145-
latestContentPromise,
146-
fileContentWithoutStartNewline,
147-
agentMessagesUntruncated,
148-
fullResponse ?? '',
149-
prompt,
145+
initialContentPromise: latestContentPromise,
146+
newContent: fileContentWithoutStartNewline,
147+
messages: agentMessagesUntruncated,
148+
fullResponse: fullResponse ?? '',
149+
lastUserPrompt: prompt,
150150
clientSessionId,
151151
fingerprintId,
152152
userInputId,
153153
userId,
154-
)
154+
})
155155
.catch((error) => {
156156
logger.error(error, 'Error processing write_file block')
157157
return {

0 commit comments

Comments
 (0)