Skip to content

Commit a2d3bdc

Browse files
committed
Refactor commit message generation to use hardcoded default instructions while preserving customizable settings
1 parent 032d35a commit a2d3bdc

10 files changed

Lines changed: 66 additions & 95 deletions

File tree

‎packages/vscode/package.json‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -536,8 +536,7 @@
536536
"codeWebChat.commitMessageInstructions": {
537537
"type": "string",
538538
"scope": "resource",
539-
"description": "Style and conventions (e.g. Conventional Commits, gitmoji) for generated commit messages.",
540-
"default": "Write a brief and precise summary for the changes, limited to a single sentence, if possible and nothing else. Use an imperative tone to ensure clarity and focus on the primary change or purpose."
539+
"description": "Style and conventions (e.g. Conventional Commits, gitmoji) for generated commit messages."
541540
},
542541
"codeWebChat.commitMessageAutoAcceptAfter": {
543542
"type": "number",

‎packages/vscode/src/commands/generate-commit-message-command.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@ export function generate_commit_message_command(
1414
const repository = get_git_repository(source_control)
1515
if (!repository) return
1616

17+
await repository.status()
18+
const was_empty_stage = (repository.state.indexChanges || []).length === 0
19+
1720
const diff = await prepare_staged_changes(repository)
1821

1922
if (!diff) return
@@ -25,6 +28,8 @@ export function generate_commit_message_command(
2528
})
2629
if (commit_message) {
2730
repository.inputBox.value = commit_message
31+
} else if (was_empty_stage) {
32+
await vscode.commands.executeCommand('git.unstageAll')
2833
}
2934
}
3035
)

‎packages/vscode/src/constants/instructions.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ The variable is possibly not defined.
2121

2222
export const intelligent_update_instructions =
2323
"Refactor the file according to the attached changes without explanations or any other text. Print the file in full because I have a disability which means I can't type and need to be able to just copy and paste."
24+
25+
export const commit_message_instructions =
26+
"Write a brief and precise summary for the changes, limited to a single sentence, if possible and nothing else. Because the summary will be used for a commit message, don't use any markdown formatting. Use an imperative tone to ensure clarity and focus on the primary change or purpose."

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@ async function proceed_with_commit_generation(
1919
try {
2020
const api_config = await get_commit_message_config(panel_provider.context)
2121
if (!api_config) {
22+
if (was_empty_stage) {
23+
await vscode.commands.executeCommand('git.unstageAll')
24+
}
2225
panel_provider.send_message({ command: 'COMMIT_PROCESS_CANCELLED' })
2326
return
2427
}
@@ -31,6 +34,9 @@ async function proceed_with_commit_generation(
3134
vscode.window.showInformationMessage(
3235
dictionary.information_message.NO_CHANGES_TO_COMMIT
3336
)
37+
if (was_empty_stage) {
38+
await vscode.commands.executeCommand('git.unstageAll')
39+
}
3440
panel_provider.send_message({ command: 'COMMIT_PROCESS_CANCELLED' })
3541
return
3642
}

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,9 +173,6 @@ export const generate_commit_message_from_diff = async (params: {
173173
}
174174
panel_provider?: PanelProvider
175175
}): Promise<string | null> => {
176-
const config = vscode.workspace.getConfiguration('codeWebChat')
177-
const commit_message_prompt = config.get<string>('commitMessageInstructions')
178-
179176
// Use provided config or get it if not provided (for backward compatibility)
180177
const resolved_api_config =
181178
params.api_config || (await get_commit_message_config(params.context))
@@ -185,11 +182,7 @@ export const generate_commit_message_from_diff = async (params: {
185182
repository: params.repository
186183
})
187184

188-
const message = build_commit_message_prompt(
189-
commit_message_prompt!,
190-
affected_files_data,
191-
params.diff
192-
)
185+
const message = build_commit_message_prompt(affected_files_data, params.diff)
193186

194187
return await generate_commit_message_with_api({
195188
endpoint_url: resolved_api_config.endpoint_url,

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

Lines changed: 26 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -31,60 +31,36 @@ export const get_commit_message_config = async (
3131
if (!commit_message_config) {
3232
const configs =
3333
await api_providers_manager.get_commit_messages_tool_configs()
34+
35+
if (configs.length == 0) {
36+
vscode.commands.executeCommand('codeWebChat.settings')
37+
vscode.window.showInformationMessage(
38+
dictionary.information_message.NO_COMMIT_MESSAGES_CONFIGURATIONS_FOUND
39+
)
40+
return null
41+
}
42+
3443
if (configs.length == 1) {
3544
commit_message_config = configs[0]
3645
} else if (configs.length > 1) {
37-
const set_default_button = {
38-
iconPath: new vscode.ThemeIcon('star'),
39-
tooltip: 'Set as default'
40-
}
41-
42-
const unset_default_button = {
43-
iconPath: new vscode.ThemeIcon('star-full'),
44-
tooltip: 'Unset default'
45-
}
46-
47-
const create_items = async () => {
48-
const default_config =
49-
await api_providers_manager.get_default_commit_messages_config()
50-
51-
return configs.map((config, index) => {
52-
const buttons = []
53-
54-
const is_default =
55-
default_config &&
56-
default_config.provider_name == config.provider_name &&
57-
default_config.model == config.model &&
58-
default_config.temperature == config.temperature &&
59-
default_config.reasoning_effort == config.reasoning_effort
60-
61-
if (is_default) {
62-
buttons.push(unset_default_button)
63-
} else {
64-
buttons.push(set_default_button)
65-
}
66-
67-
const description_parts = [config.provider_name]
68-
if (config.reasoning_effort) {
69-
description_parts.push(`${config.reasoning_effort}`)
70-
}
71-
if (config.temperature != DEFAULT_TEMPERATURE['commit-messages']) {
72-
description_parts.push(`${config.temperature}`)
73-
}
74-
75-
return {
76-
label: is_default ? `$(pass-filled) ${config.model}` : config.model,
77-
description: description_parts.join(' · '),
78-
config,
79-
index,
80-
id: get_tool_config_id(config),
81-
buttons
82-
}
83-
})
84-
}
85-
8646
const quick_pick = vscode.window.createQuickPick()
87-
quick_pick.items = await create_items()
47+
quick_pick.items = configs.map((config, index) => {
48+
const description_parts = [config.provider_name]
49+
if (config.reasoning_effort) {
50+
description_parts.push(`${config.reasoning_effort}`)
51+
}
52+
if (config.temperature != DEFAULT_TEMPERATURE['commit-messages']) {
53+
description_parts.push(`${config.temperature}`)
54+
}
55+
56+
return {
57+
label: config.model,
58+
description: description_parts.join(' · '),
59+
config,
60+
index,
61+
id: get_tool_config_id(config)
62+
}
63+
})
8864
quick_pick.placeholder = 'Select configuration for commit message'
8965
quick_pick.matchOnDescription = true
9066

@@ -108,23 +84,6 @@ export const get_commit_message_config = async (
10884
commit_message_config = await new Promise<
10985
CommitMessageConfig | undefined
11086
>((resolve) => {
111-
quick_pick.onDidTriggerItemButton(async (event) => {
112-
const item = event.item as any
113-
const button = event.button
114-
const index = item.index
115-
116-
if (button === set_default_button) {
117-
await api_providers_manager.set_default_commit_messages_config(
118-
configs[index]
119-
)
120-
} else if (button === unset_default_button) {
121-
await api_providers_manager.set_default_commit_messages_config(
122-
null as any
123-
)
124-
}
125-
quick_pick.items = await create_items()
126-
})
127-
12887
quick_pick.onDidAccept(async () => {
12988
const selected = quick_pick.selectedItems[0] as any
13089
quick_pick.hide()
@@ -157,10 +116,6 @@ export const get_commit_message_config = async (
157116
}
158117

159118
if (!commit_message_config) {
160-
vscode.commands.executeCommand('codeWebChat.settings')
161-
vscode.window.showInformationMessage(
162-
dictionary.information_message.NO_COMMIT_MESSAGES_CONFIGURATIONS_FOUND
163-
)
164119
return null
165120
}
166121

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

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import { FileData } from './file-utils'
2+
import * as vscode from 'vscode'
3+
import { commit_message_instructions } from '@/constants/instructions'
24

35
export const strip_wrapping_quotes = (text: string): string => {
46
const trimmed = text.trim()
@@ -13,11 +15,18 @@ export const strip_wrapping_quotes = (text: string): string => {
1315
return trimmed
1416
}
1517

18+
const get_commit_message_instructions = (): string => {
19+
const config = vscode.workspace.getConfiguration('codeWebChat')
20+
const instructions = config.get<string>('commitMessageInstructions')
21+
return instructions || commit_message_instructions
22+
}
23+
1624
export const build_commit_message_prompt = (
17-
commit_message_prompt: string,
1825
affected_files_data: FileData[],
1926
diff: string
2027
): string => {
28+
const commit_message_prompt = get_commit_message_instructions()
29+
2130
const file_diffs = diff.split(/^diff --git /m).filter((d) => d.trim() != '')
2231

2332
let changes_content = ''
@@ -44,18 +53,15 @@ export const build_commit_message_prompt = (
4453

4554
if (file_path) {
4655
const file_data = affected_files_data.find(
47-
(f) => f.relative_path === file_path
56+
(f) => f.relative_path == file_path
4857
)
49-
changes_content += `<change path="${file_path}">\n`
50-
changes_content += `<diff>\n<![CDATA[\n${full_file_diff}\n]]>\n</diff>\n`
58+
changes_content += `\n### File: \`${file_path}\`\n\n`
5159
if (file_data) {
52-
changes_content += `<file>\n<![CDATA[\n${file_data.content}\n]]>\n</file>\n`
60+
changes_content += `The original state of the file for reference:\n<![CDATA[\n${file_data.content}\n]]>\n`
5361
}
54-
changes_content += `</change>\n`
62+
changes_content += `**New changes:**\n<![CDATA[\n${full_file_diff}\n]]>\n`
5563
}
5664
}
5765

58-
const changes_xml = `<changes>\n${changes_content}</changes>`
59-
60-
return `${commit_message_prompt}\n${changes_xml}\n${commit_message_prompt}`
66+
return `${commit_message_prompt}\n${changes_content}\n${commit_message_prompt}`
6167
}

‎packages/vscode/src/views/settings/backend/message-handlers/handle-get-commit-message-instructions.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import * as vscode from 'vscode'
22
import { SettingsProvider } from '@/views/settings/backend/settings-provider'
3+
import { commit_message_instructions } from '@/constants/instructions'
34

45
export const handle_get_commit_message_instructions = async (
56
provider: SettingsProvider
67
): Promise<void> => {
78
const config = vscode.workspace.getConfiguration('codeWebChat')
8-
const instructions = config.get<string>('commitMessageInstructions') || ''
9+
const instructions =
10+
config.get<string>('commitMessageInstructions') ||
11+
commit_message_instructions
912
provider.postMessage({
1013
command: 'COMMIT_MESSAGE_INSTRUCTIONS',
1114
instructions
Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import * as vscode from 'vscode'
2-
import { SettingsProvider } from '@/views/settings/backend/settings-provider'
32
import { UpdateCommitMessageInstructionsMessage } from '@/views/settings/types/messages'
3+
import { commit_message_instructions } from '@/constants/instructions'
44

55
export const handle_update_commit_message_instructions = async (
6-
provider: SettingsProvider,
76
message: UpdateCommitMessageInstructionsMessage
87
): Promise<void> => {
98
await vscode.workspace
109
.getConfiguration('codeWebChat')
1110
.update(
1211
'commitMessageInstructions',
13-
message.instructions,
12+
message.instructions == commit_message_instructions
13+
? undefined
14+
: message.instructions,
1415
vscode.ConfigurationTarget.Global
1516
)
1617
}

‎packages/vscode/src/views/settings/backend/settings-provider.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ export class SettingsProvider {
196196
} else if (message.command == 'GET_COMMIT_MESSAGE_INSTRUCTIONS') {
197197
await handle_get_commit_message_instructions(this)
198198
} else if (message.command == 'UPDATE_COMMIT_MESSAGE_INSTRUCTIONS') {
199-
await handle_update_commit_message_instructions(this, message)
199+
await handle_update_commit_message_instructions(message)
200200
} else if (message.command == 'GET_COMMIT_MESSAGE_AUTO_ACCEPT_AFTER') {
201201
await handle_get_commit_message_auto_accept_after(this)
202202
} else if (

0 commit comments

Comments
 (0)