Skip to content

Commit 1591555

Browse files
committed
Centralize diff file context construction in PromptBuilder utility
- it seems like both files have similar logic in terms of constructing prompt with files - should not use hack "as 'created' | 'deleted' | 'renamed' | 'updated'" - when building prompt for commit message, place "---" divider between instructions and files
1 parent a409dd2 commit 1591555

3 files changed

Lines changed: 56 additions & 33 deletions

File tree

‎apps/editor/src/features/commit-messages/utils/build-commit-message-prompt.ts‎

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as vscode from 'vscode'
22
import { commit_message_instructions } from '@/constants/instructions'
33
import type { GitRepository } from '@/utils/git-repository-utils'
44
import { MAX_FILE_TOKENS_FOR_COMMIT_MESSAGE } from '@/constants/values'
5+
import { PromptBuilder } from '@/utils/prompt-builder'
56

67
export const build_commit_message_prompt = async (
78
diff: string,
@@ -52,7 +53,7 @@ export const build_commit_message_prompt = async (
5253
}
5354

5455
if (file_path) {
55-
let status = 'updated'
56+
let status: 'created' | 'deleted' | 'renamed' | 'updated' = 'updated'
5657

5758
if (is_deleted) {
5859
status = 'deleted'
@@ -91,20 +92,7 @@ export const build_commit_message_prompt = async (
9192
}
9293
}
9394

94-
if (status == 'created') {
95-
changes_content += `### New file: \`${file_path}\`\n\n`
96-
} else if (status == 'deleted') {
97-
changes_content += `### Deleted file: \`${file_path}\`\n\n`
98-
} else if (status == 'renamed') {
99-
changes_content += `### Renamed file: \`${old_path_attr}\` (old) \`${file_path}\` (new)\n\n`
100-
} else {
101-
changes_content += `### Updated file: \`${file_path}\`\n\n`
102-
}
103-
104-
if (final_diff_content.trimEnd()) {
105-
changes_content += `\`\`\`diff\n${final_diff_content.trimEnd()}\n\`\`\`\n\n`
106-
}
107-
95+
let file_content_to_pass = undefined
10896
if (!is_deleted && file_path && !is_binary) {
10997
try {
11098
let full_content = ''
@@ -120,13 +108,21 @@ export const build_commit_message_prompt = async (
120108
if (full_content && !full_content.includes('\0')) {
121109
const full_content_tokens = Math.ceil(full_content.length / 4)
122110
if (full_content_tokens <= MAX_FILE_TOKENS_FOR_COMMIT_MESSAGE) {
123-
changes_content += `\`\`\`\n${full_content.trimEnd()}\n\`\`\`\n\n`
111+
file_content_to_pass = full_content
124112
}
125113
}
126114
} catch (err) {}
127115
}
116+
117+
changes_content += PromptBuilder.build_diff_file_context({
118+
status,
119+
filepath: file_path,
120+
old_filepath: old_path_attr,
121+
diff_content: final_diff_content,
122+
full_content: file_content_to_pass
123+
})
128124
}
129125
}
130126

131-
return `${commit_message_prompt}\n\n${changes_content}${commit_message_prompt}`
127+
return `${commit_message_prompt}\n\n---\n\n${changes_content}---\n\n${commit_message_prompt}`
132128
}

‎apps/editor/src/utils/git-repository-utils.ts‎

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as path from 'path'
55
import { t } from '@/i18n'
66
import { display_token_count } from './display-token-count'
77
import { MAX_FILE_TOKENS_FOR_COMMIT_MESSAGE } from '@/constants/values'
8+
import { PromptBuilder } from './prompt-builder'
89

910
export type GitRepository = {
1011
rootUri: vscode.Uri
@@ -168,7 +169,7 @@ export const prepare_staged_changes = async (params: {
168169
)
169170
const dir_name = path.dirname(relative_path)
170171

171-
let status = 'updated'
172+
let status: 'created' | 'deleted' | 'renamed' | 'updated' = 'updated'
172173
let is_deleted = false
173174
let final_diff_content = ''
174175
let full_content = ''
@@ -239,21 +240,15 @@ export const prepare_staged_changes = async (params: {
239240
)
240241
const is_too_large = file_tokens > MAX_FILE_TOKENS_FOR_COMMIT_MESSAGE
241242

242-
let file_md = ''
243-
if (status == 'created') {
244-
file_md = `### New file: \`${relative_path}\`\n\n`
245-
} else if (status == 'deleted') {
246-
file_md = `### Deleted file: \`${relative_path}\`\n\n`
247-
} else {
248-
file_md = `### Updated file: \`${relative_path}\`\n\n`
249-
}
250-
251-
if (final_diff_content.trimEnd()) {
252-
file_md += `\`\`\`diff\n${final_diff_content.trimEnd()}\n\`\`\`\n\n`
253-
}
254-
if (!is_deleted && full_content && !is_too_large) {
255-
file_md += `\`\`\`\n${full_content.trimEnd()}\n\`\`\`\n\n`
256-
}
243+
const file_md = PromptBuilder.build_diff_file_context({
244+
status,
245+
filepath: relative_path,
246+
diff_content: final_diff_content,
247+
full_content:
248+
!is_deleted && full_content && !is_too_large
249+
? full_content
250+
: undefined
251+
})
257252

258253
const token_count = Math.ceil(file_md.length / 4)
259254
const description_parts = []

‎apps/editor/src/utils/prompt-builder.ts‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@ export namespace PromptBuilder {
1111
return `### File: \`${display_path}\`\n\n\`\`\`\n${params.content}\n\`\`\`\n\n`
1212
}
1313

14+
export const build_diff_file_context = (params: {
15+
status: 'created' | 'deleted' | 'renamed' | 'updated'
16+
filepath: string
17+
old_filepath?: string
18+
diff_content?: string
19+
full_content?: string
20+
}): string => {
21+
let result = ''
22+
const display_path = params.filepath.replace(/\\/g, '/')
23+
24+
if (params.status == 'created') {
25+
result += `### New file: \`${display_path}\`\n\n`
26+
} else if (params.status == 'deleted') {
27+
result += `### Deleted file: \`${display_path}\`\n\n`
28+
} else if (params.status == 'renamed' && params.old_filepath) {
29+
const old_display_path = params.old_filepath.replace(/\\/g, '/')
30+
result += `### Renamed file: \`${old_display_path}\` (old) \`${display_path}\` (new)\n\n`
31+
} else {
32+
result += `### Updated file: \`${display_path}\`\n\n`
33+
}
34+
35+
if (params.diff_content?.trimEnd()) {
36+
result += `\`\`\`diff\n${params.diff_content.trimEnd()}\n\`\`\`\n\n`
37+
}
38+
39+
if (params.full_content?.trimEnd()) {
40+
result += `\`\`\`\n${params.full_content.trimEnd()}\n\`\`\`\n\n`
41+
}
42+
43+
return result
44+
}
45+
1446
export const build_prompt = (params: {
1547
other_files?: string
1648
recent_files?: string

0 commit comments

Comments
 (0)