Skip to content

Commit b89c30a

Browse files
committed
refactor: convert fastRewrite, rewriteWithOpenAI, and shouldAddFilePlaceholders to use object parameters
1 parent b9bd7e3 commit b89c30a

File tree

3 files changed

+94
-62
lines changed

3 files changed

+94
-62
lines changed

backend/src/__tests__/fast-rewrite.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ describe.skip('rewriteWithOpenAI', () => {
4343
const editSnippet = await Bun.file(`${testDataDir}/edit-snippet.go`).text()
4444
const expectedResult = await Bun.file(`${testDataDir}/expected.go`).text()
4545

46-
const result = await rewriteWithOpenAI(
47-
originalContent,
46+
const result = await rewriteWithOpenAI({
47+
oldContent: originalContent,
4848
editSnippet,
49-
'taskruntoolcall.go',
50-
'clientSessionId',
51-
'fingerprintId',
52-
'userInputId',
53-
TEST_USER_ID,
54-
undefined,
55-
)
49+
filePath: 'taskruntoolcall.go',
50+
clientSessionId: 'clientSessionId',
51+
fingerprintId: 'fingerprintId',
52+
userInputId: 'userInputId',
53+
userId: TEST_USER_ID,
54+
userMessage: undefined,
55+
})
5656

5757
const patch = createPatch('test.ts', expectedResult, result)
5858
const patchLines = patch.split('\n').slice(4)

backend/src/fast-rewrite.ts

Lines changed: 67 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,28 @@ import type {
1414
ToolMessage,
1515
} from '@codebuff/common/types/messages/codebuff-message'
1616

17-
export async function fastRewrite(
18-
initialContent: string,
19-
editSnippet: string,
20-
filePath: string,
21-
instructions: string | undefined,
22-
clientSessionId: string,
23-
fingerprintId: string,
24-
userInputId: string,
25-
userId: string | undefined,
26-
userMessage: string | undefined,
27-
) {
17+
export async function fastRewrite(params: {
18+
initialContent: string
19+
editSnippet: string
20+
filePath: string
21+
instructions: string | undefined
22+
clientSessionId: string
23+
fingerprintId: string
24+
userInputId: string
25+
userId: string | undefined
26+
userMessage: string | undefined
27+
}) {
28+
const {
29+
initialContent,
30+
editSnippet,
31+
filePath,
32+
instructions,
33+
clientSessionId,
34+
fingerprintId,
35+
userInputId,
36+
userId,
37+
userMessage,
38+
} = params
2839
const relaceStartTime = Date.now()
2940
const messageId = generateCompactId('cb-')
3041
let response = await promptRelaceAI(
@@ -49,16 +60,16 @@ export async function fastRewrite(
4960
hasLazyEdit(response)
5061
) {
5162
const relaceResponse = response
52-
response = await rewriteWithOpenAI(
53-
initialContent,
63+
response = await rewriteWithOpenAI({
64+
oldContent: initialContent,
5465
editSnippet,
5566
filePath,
5667
clientSessionId,
5768
fingerprintId,
5869
userInputId,
5970
userId,
6071
userMessage,
61-
)
72+
})
6273
logger.debug(
6374
{ filePath, relaceResponse, openaiResponse: response, messageId },
6475
`Relace output contained lazy edits, trying GPT-4o-mini ${filePath}`,
@@ -81,16 +92,26 @@ export async function fastRewrite(
8192
}
8293

8394
// Gemini flash can only output 8k tokens, openai models can do at least 16k tokens.
84-
export async function rewriteWithOpenAI(
85-
oldContent: string,
86-
editSnippet: string,
87-
filePath: string,
88-
clientSessionId: string,
89-
fingerprintId: string,
90-
userInputId: string,
91-
userId: string | undefined,
92-
userMessage: string | undefined,
93-
): Promise<string> {
95+
export async function rewriteWithOpenAI(params: {
96+
oldContent: string
97+
editSnippet: string
98+
filePath: string
99+
clientSessionId: string
100+
fingerprintId: string
101+
userInputId: string
102+
userId: string | undefined
103+
userMessage: string | undefined
104+
}): Promise<string> {
105+
const {
106+
oldContent,
107+
editSnippet,
108+
filePath,
109+
clientSessionId,
110+
fingerprintId,
111+
userInputId,
112+
userId,
113+
userMessage,
114+
} = params
94115
const prompt = `You are an expert programmer tasked with implementing changes to a file. Please rewrite the file to implement the changes shown in the edit snippet, while preserving the original formatting and behavior of unchanged parts.
95116
96117
Old file content:
@@ -132,17 +153,28 @@ Please output just the complete updated file content with the edit applied and n
132153
* sketches an update to a single function, but forgets to add ... existing code ...
133154
* above and below the function.
134155
*/
135-
export const shouldAddFilePlaceholders = async (
136-
filePath: string,
137-
oldContent: string,
138-
rewrittenNewContent: string,
139-
messageHistory: Message[],
140-
fullResponse: string,
141-
userId: string | undefined,
142-
clientSessionId: string,
143-
fingerprintId: string,
144-
userInputId: string,
145-
) => {
156+
export const shouldAddFilePlaceholders = async (params: {
157+
filePath: string
158+
oldContent: string
159+
rewrittenNewContent: string
160+
messageHistory: Message[]
161+
fullResponse: string
162+
userId: string | undefined
163+
clientSessionId: string
164+
fingerprintId: string
165+
userInputId: string
166+
}) => {
167+
const {
168+
filePath,
169+
oldContent,
170+
rewrittenNewContent,
171+
messageHistory,
172+
fullResponse,
173+
userId,
174+
clientSessionId,
175+
fingerprintId,
176+
userInputId,
177+
} = params
146178
const fileWasPreviouslyEdited = messageHistory
147179
.filter(
148180
(

backend/src/process-file-block.ts

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -131,43 +131,43 @@ export async function processFileBlock(params: {
131131

132132
updatedContent = largeFileContent
133133
} else {
134-
updatedContent = await fastRewrite(
135-
normalizedInitialContent,
136-
normalizedEditSnippet,
137-
path,
134+
updatedContent = await fastRewrite({
135+
initialContent: normalizedInitialContent,
136+
editSnippet: normalizedEditSnippet,
137+
filePath: path,
138138
instructions,
139139
clientSessionId,
140140
fingerprintId,
141141
userInputId,
142142
userId,
143-
lastUserPrompt,
144-
)
145-
const shouldAddPlaceholders = await shouldAddFilePlaceholders(
146-
path,
147-
normalizedInitialContent,
148-
updatedContent,
149-
messages,
143+
userMessage: lastUserPrompt,
144+
})
145+
const shouldAddPlaceholders = await shouldAddFilePlaceholders({
146+
filePath: path,
147+
oldContent: normalizedInitialContent,
148+
rewrittenNewContent: updatedContent,
149+
messageHistory: messages,
150150
fullResponse,
151151
userId,
152152
clientSessionId,
153153
fingerprintId,
154154
userInputId,
155-
)
155+
})
156156

157157
if (shouldAddPlaceholders) {
158158
const placeholderComment = `... existing code ...`
159159
const updatedEditSnippet = `${placeholderComment}\n${updatedContent}\n${placeholderComment}`
160-
updatedContent = await fastRewrite(
161-
normalizedInitialContent,
162-
updatedEditSnippet,
163-
path,
160+
updatedContent = await fastRewrite({
161+
initialContent: normalizedInitialContent,
162+
editSnippet: updatedEditSnippet,
163+
filePath: path,
164164
instructions,
165165
clientSessionId,
166166
fingerprintId,
167167
userInputId,
168168
userId,
169-
lastUserPrompt,
170-
)
169+
userMessage: lastUserPrompt,
170+
})
171171
}
172172
}
173173

0 commit comments

Comments
 (0)