Skip to content

Commit 7271e3b

Browse files
committed
Refactor commit message generation to use a structured XML format for diff and file content instead of plain text concatenation
1 parent e310868 commit 7271e3b

5 files changed

Lines changed: 95 additions & 55 deletions

File tree

‎packages/vscode/src/utils/git-repository-utils.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ export interface GitRepository {
1313
inputBox: {
1414
value: string
1515
}
16+
show: (ref: string, path: string) => Promise<string>
1617
}
1718

1819
export function get_git_repository(

‎packages/vscode/src/views/panel/backend/message-handlers/handle-commit-changes/utils/file-utils.ts‎

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,34 @@ export const collect_affected_files_with_metadata = async (params: {
3838
let content = ''
3939
let is_large_file = false
4040
try {
41-
const stats = await fs.promises.stat(file_path)
42-
if (stats.size > 100 * 1024) {
43-
is_large_file = true
44-
content = `File content omitted due to large size (${(
45-
stats.size / 1024
46-
).toFixed(2)} KB).`
47-
} else {
48-
content = await fs.promises.readFile(file_path, 'utf8')
41+
content = await params.repository.show('HEAD', relative_path)
42+
} catch (show_error) {
43+
// If `show` fails, it's likely a new file. Fallback to reading from disk.
44+
try {
45+
const stats = await fs.promises.stat(file_path)
46+
if (stats.size > 100 * 1024) {
47+
is_large_file = true
48+
content = `File content omitted due to large size (${(
49+
stats.size / 1024
50+
).toFixed(2)} KB).`
51+
} else {
52+
content = await fs.promises.readFile(file_path, 'utf8')
53+
}
54+
} catch (read_error) {
55+
Logger.warn({
56+
function_name: 'collect_affected_files_with_metadata',
57+
message: `Could not read file content for ${relative_path}`,
58+
data: { show_error, read_error }
59+
})
60+
content = `Could not read file content.`
4961
}
50-
} catch (read_error) {
51-
Logger.warn({
52-
function_name: 'collect_affected_files_with_metadata',
53-
message: `Could not read file content for ${relative_path}`,
54-
data: read_error
55-
})
56-
content = `Could not read file content.`
62+
}
63+
64+
if (!is_large_file && content.length > 100 * 1024) {
65+
is_large_file = true
66+
content = `File content omitted due to large size (${(
67+
content.length / 1024
68+
).toFixed(2)} KB).`
5769
}
5870

5971
// Simple token estimation: 1 token per 4 characters

‎packages/vscode/src/views/panel/backend/message-handlers/handle-commit-changes/utils/generate-commit-message-from-diff.ts‎

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@ import {
66
CommitMessageConfig
77
} from './get-commit-message-config'
88
import { collect_affected_files_with_metadata } from './file-utils'
9-
import {
10-
build_files_content,
11-
build_commit_message_prompt
12-
} from './prompt-utils'
9+
import { build_commit_message_prompt } from './prompt-utils'
1310
import axios from 'axios'
1411
import { Logger } from '@shared/utils/logger'
1512
import { dictionary } from '@shared/constants/dictionary'
@@ -188,10 +185,9 @@ export const generate_commit_message_from_diff = async (params: {
188185
repository: params.repository
189186
})
190187

191-
const affected_files = build_files_content(affected_files_data)
192188
const message = build_commit_message_prompt(
193189
commit_message_prompt!,
194-
affected_files,
190+
affected_files_data,
195191
params.diff
196192
)
197193

Lines changed: 42 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
import { FileData } from './file-utils'
22

3-
export const build_files_content = (files_data: FileData[]): string => {
4-
if (!files_data || files_data.length == 0) {
5-
return 'No relevant files to include.'
6-
}
7-
8-
return files_data
9-
.map((file) => {
10-
return `File: ${file.relative_path}\nContent:\n${file.content}`
11-
})
12-
.join('\n---\n')
13-
}
14-
153
export const strip_wrapping_quotes = (text: string): string => {
164
const trimmed = text.trim()
175

@@ -27,8 +15,48 @@ export const strip_wrapping_quotes = (text: string): string => {
2715

2816
export const build_commit_message_prompt = (
2917
commit_message_prompt: string,
30-
affected_files: string,
18+
affected_files_data: FileData[],
3119
diff: string
3220
): string => {
33-
return `${commit_message_prompt}\n${affected_files}\n${diff}\n${commit_message_prompt}`
21+
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
22+
23+
let changes_content = ''
24+
for (const file_diff_content of file_diffs) {
25+
const full_file_diff = 'diff --git ' + file_diff_content
26+
const lines = full_file_diff.split('\n')
27+
const old_path_line = lines.find((l) => l.startsWith('--- a/'))
28+
const new_path_line = lines.find((l) => l.startsWith('+++ b/'))
29+
30+
const old_path = old_path_line
31+
? old_path_line.substring('--- a/'.length)
32+
: undefined
33+
const new_path = new_path_line
34+
? new_path_line.substring('+++ b/'.length)
35+
: undefined
36+
37+
let file_path: string | undefined
38+
39+
if (new_path && new_path != '/dev/null') {
40+
file_path = new_path
41+
} else if (old_path && old_path != '/dev/null') {
42+
file_path = old_path
43+
}
44+
45+
if (file_path) {
46+
const file_data = affected_files_data.find(
47+
(f) => f.relative_path === file_path
48+
)
49+
changes_content += `<change path="${file_path}">\n`
50+
changes_content += `<diff>\n<![CDATA[\n${full_file_diff}\n]]>\n</diff>\n`
51+
if (file_data) {
52+
changes_content += `<file>\n<![CDATA[\n${file_data.content}\n]]>\n</file>\n`
53+
}
54+
changes_content += `</change>\n`
55+
}
56+
}
57+
58+
const changes_xml = `<changes>\n${changes_content}</changes>`
59+
60+
return `${commit_message_prompt}\n${changes_xml}\n${commit_message_prompt}`
3461
}
62+

‎packages/vscode/src/views/panel/backend/message-handlers/handle-send-prompt.ts‎

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { replace_selection_placeholder } from '@/views/panel/backend/utils/repla
55
import { apply_preset_affixes_to_instruction } from '@/utils/apply-preset-affixes'
66
import { replace_saved_context_placeholder } from '@/utils/replace-saved-context-placeholder'
77
import {
8-
replace_changes_placeholder, replace_context_at_commit_placeholder,
8+
replace_changes_placeholder,
9+
replace_context_at_commit_placeholder,
910
replace_commit_placeholder
1011
} from '@/views/panel/backend/utils/replace-git-placeholders'
1112
import { code_completion_instructions_for_panel } from '@/constants/instructions'
@@ -85,7 +86,10 @@ export const handle_send_prompt = async (params: {
8586
params.panel_provider.web_prompt_type
8687
)
8788
const recents =
88-
params.panel_provider.context.globalState.get<string[]>(recents_key, []) ?? []
89+
params.panel_provider.context.globalState.get<string[]>(
90+
recents_key,
91+
[]
92+
) ?? []
8993
const new_recents = [
9094
name_to_save,
9195
...recents.filter((r) => r != name_to_save)
@@ -206,19 +210,17 @@ export const handle_send_prompt = async (params: {
206210
}
207211

208212
if (pre_context_instructions.includes('#ContextAtCommit:')) {
209-
pre_context_instructions = await replace_context_at_commit_placeholder(
210-
{
213+
pre_context_instructions =
214+
await replace_context_at_commit_placeholder({
211215
instruction: pre_context_instructions,
212216
workspace_provider: params.panel_provider.workspace_provider
213-
}
214-
)
215-
post_context_instructions = await replace_context_at_commit_placeholder(
216-
{
217+
})
218+
post_context_instructions =
219+
await replace_context_at_commit_placeholder({
217220
instruction: post_context_instructions,
218221
after_context: true,
219222
workspace_provider: params.panel_provider.workspace_provider
220-
}
221-
)
223+
})
222224
}
223225
if (pre_context_instructions.includes('#SavedContext:')) {
224226
pre_context_instructions = await replace_saved_context_placeholder({
@@ -320,8 +322,8 @@ async function show_preset_quick_pick(params: {
320322
if (item && item.chatbot) {
321323
const preset = item
322324
const is_unnamed = !preset.name || /^\(\d+\)$/.test(preset.name.trim())
323-
const chatbot_models =
324-
CHATBOTS[preset.chatbot as keyof typeof CHATBOTS].models as any
325+
const chatbot_models = CHATBOTS[preset.chatbot as keyof typeof CHATBOTS]
326+
.models as any
325327
const model = preset.model
326328
? chatbot_models[preset.model]?.label || preset.model
327329
: ''
@@ -485,14 +487,19 @@ async function show_preset_quick_pick(params: {
485487
const runnable_presets = default_presets_in_group.filter(
486488
(preset) => !params.get_is_preset_disabled(preset)
487489
)
488-
const preset_names = runnable_presets.map((p) => p.name!).filter(Boolean)
490+
const preset_names = runnable_presets
491+
.map((p) => p.name!)
492+
.filter(Boolean)
489493

490494
if (preset_names.length > 0) {
491495
update_last_used_preset_or_group({ panel_provider, group_name })
492496
const recents_key = get_recently_used_presets_or_groups_key(mode)
493497
const recents =
494498
context.globalState.get<string[]>(recents_key, []) ?? []
495-
const newRecents = [group_name, ...recents.filter((r) => r !== group_name)].slice(0, 10)
499+
const newRecents = [
500+
group_name,
501+
...recents.filter((r) => r !== group_name)
502+
].slice(0, 10)
496503
context.globalState.update(recents_key, newRecents)
497504
do_resolve({ preset_names })
498505
}
@@ -521,9 +528,7 @@ async function resolve_presets(params: {
521528
context: vscode.ExtensionContext
522529
}): Promise<{ preset_names: string[]; without_submission?: boolean }> {
523530
const last_selected_preset_or_group_key =
524-
get_last_selected_preset_or_group_key(
525-
params.panel_provider.web_prompt_type
526-
)
531+
get_last_selected_preset_or_group_key(params.panel_provider.web_prompt_type)
527532
const config = vscode.workspace.getConfiguration('codeWebChat')
528533
const presets_config_key = params.panel_provider.get_presets_config_key()
529534
const all_presets = config.get<ConfigPresetFormat[]>(presets_config_key, [])
@@ -666,9 +671,7 @@ async function resolve_presets(params: {
666671
params.context.workspaceState.get<string>(
667672
last_selected_preset_or_group_key
668673
) ??
669-
params.context.globalState.get<string>(
670-
last_selected_preset_or_group_key
671-
)
674+
params.context.globalState.get<string>(last_selected_preset_or_group_key)
672675

673676
if (last_selected_name) {
674677
if (last_selected_name === 'Ungrouped') {

0 commit comments

Comments
 (0)