Skip to content

Commit c19649b

Browse files
committed
Add setting and prompt option to use context files in commit message generation
- below 'attach ascii tree of context' implement similar ask/always/never setting "Use context files" (useContextFilesInCommitMessagePrompt), desc: "Improved accuracy at higher token usage." - place 'use context files' above 'attach ascii...' - place 'commit message instructions' above 'use context files' - translate in [Fragment] - right after user selects files to stage, determine if there are any context files to be attached, if so respect 'use Context Files In Commit Message Prompt' setting - change 'Attach X context files to the prompt?' to 'Should use context files to improve accuracy?'. options to: Use (desc e.g.: 20K tokens)/Skip (desc e.g.: 10K tokens). - place 'skip' above 'use'. Skip should tell total token count, Use should tell additional token count,e.g "+10K" - should remember last chosen use/skip - rename 'last attach context files' to 'last use context files' - allow 'go back' from 'commit message actions to the selector use/skip (when shown) - add the missing close button to 'use/skip' quick pick - cancelling api call should show list of configurations, not the initial use/skip quick pick - change place holder 'choose how to proceed' to 'choose how to proceed with [x] tokens' - rename 'select configuration to process X tokens' to 'select configuration' - move [Fragment] to `apps/editor/src/i18n/translations/commands/search-files.ts` ├── .github │ └── workflows │ ├── deploy-vscode-extension.yml │ └── deploy-website.yml └── apps └── editor ├── package.json └── src ├── commands │ └── generate-commit-message-commands │ ├── actions │ │ ├── get-prompt-data.ts │ │ └── run-generate-action.ts │ ├── generate-commit-message-commands.ts │ ├── index.ts │ └── utils │ ├── build-commit-message-prompt.ts │ ├── generate-commit-message-with-api.ts │ └── strip-wrapping-quotes.ts ├── constants │ └── state-keys.ts ├── features │ └── search-files │ ├── search-modes │ │ └── perform-intelligent-search-mode.ts │ └── utils │ ├── prompt-for-config.ts │ ├── prompt-for-intelligent-search-results.ts │ └── prompt-for-search-results.ts ├── i18n │ └── translations │ ├── commands │ │ ├── generate-commit-message.ts │ │ └── search-files.ts │ ├── common.ts │ └── views │ └── settings │ ├── handlers │ │ ├── handle-create-template.ts │ │ ├── handle-delete-model-provider.ts │ │ ├── handle-delete-template.ts │ │ ├── handle-pick-api-reasoning-effort.ts │ │ ├── index.ts │ │ ├── open-allow-patterns-settings.ts │ │ ├── open-ignore-patterns-settings.ts │ │ └── select-default-api-configuration.ts │ └── index.ts ├── utils │ ├── display-token-count.ts │ └── git-repository-utils.ts └── views └── settings ├── backend │ ├── message-handlers │ │ ├── handle-get-attach-ascii-tree-of-context.ts │ │ ├── handle-get-use-context-files-in-commit-message-prompt.ts │ │ ├── handle-update-attach-ascii-tree-of-context.ts │ │ ├── handle-update-use-context-files-in-commit-message-prompt.ts │ │ └── index.ts │ └── settings-view-provider.ts ├── frontend │ ├── Home │ │ ├── Home.tsx │ │ └── sections │ │ └── GeneralSection.tsx │ ├── Settings.tsx │ ├── hooks │ │ └── use-settings.ts │ └── i18n │ └── translations │ └── general.ts └── types └── messages.ts
1 parent 34d693e commit c19649b

19 files changed

Lines changed: 907 additions & 350 deletions

File tree

‎apps/editor/package.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,16 @@
950950
],
951951
"default": "ask"
952952
},
953+
"codeWebChat.useContextFilesInCommitMessagePrompt": {
954+
"type": "string",
955+
"scope": "resource",
956+
"enum": [
957+
"ask",
958+
"always",
959+
"never"
960+
],
961+
"default": "ask"
962+
},
953963
"codeWebChat.selectAllPromptsInCommitMessagesByDefault": {
954964
"type": "boolean",
955965
"scope": "resource",

‎apps/editor/src/commands/generate-commit-message-commands/actions/get-prompt-data.ts‎

Lines changed: 184 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,29 @@ import { CommitMessageDetails } from '../../../utils/commit-message-details'
99
import { normalize_path } from '@/utils/normalize-path'
1010
import { WorkspaceProvider } from '@/context/providers/workspace/workspace-provider'
1111
import { FilesCollector } from '@/utils/files-collector'
12+
import { t } from '@/i18n'
13+
import { display_token_count } from '@/utils/display-token-count'
14+
import { LAST_USE_CONTEXT_FILES_STATE_KEY } from '@/constants/state-keys'
1215

1316
export const get_prompt_data = async (params: {
1417
repository: GitRepository
1518
stage_all_if_none_staged: boolean
1619
selection_state?: { files?: string[] }
1720
extension_context: vscode.ExtensionContext
1821
workspace_provider: WorkspaceProvider
22+
files_staged_by_action?: boolean
23+
is_single_change_flow?: boolean
1924
}) => {
2025
await vscode.workspace.saveAll()
2126
await params.repository.status()
22-
const was_empty_stage =
27+
const current_empty_stage =
2328
(params.repository.state.indexChanges || []).length == 0
29+
const was_empty_stage = current_empty_stage || !!params.files_staged_by_action
2430
const working_tree_changes = params.repository.state.workingTreeChanges || []
25-
const is_single_change = was_empty_stage && working_tree_changes.length == 1
31+
const is_single_change =
32+
params.is_single_change_flow !== undefined
33+
? params.is_single_change_flow
34+
: current_empty_stage && working_tree_changes.length == 1
2635
const diff = await prepare_staged_changes({
2736
repository: params.repository,
2837
stage_all_if_none_staged: params.stage_all_if_none_staged,
@@ -107,18 +116,185 @@ export const get_prompt_data = async (params: {
107116
return normalize_path(path.relative(workspace_root, f))
108117
})
109118

110-
const { api_prompt, chatbot_prompt } = await build_commit_message_prompt(
111-
diff,
112-
params.repository,
113-
ordered_context_files
114-
)
119+
let api_prompt = ''
120+
let chatbot_prompt = ''
121+
let was_context_prompt_shown = false
122+
123+
if (ordered_context_files.length > 0) {
124+
const setting = vscode.workspace
125+
.getConfiguration('codeWebChat')
126+
.get<string>('useContextFilesInCommitMessagePrompt', 'ask')
127+
128+
if (setting === 'always') {
129+
const prompt = await build_commit_message_prompt(
130+
diff,
131+
params.repository,
132+
ordered_context_files
133+
)
134+
api_prompt = prompt.api_prompt
135+
chatbot_prompt = prompt.chatbot_prompt
136+
} else if (setting === 'ask') {
137+
const prompt_with_context = await build_commit_message_prompt(
138+
diff,
139+
params.repository,
140+
ordered_context_files
141+
)
142+
143+
const prompt_without_context = await build_commit_message_prompt(
144+
diff,
145+
params.repository,
146+
[]
147+
)
148+
149+
const skip_tokens = Math.ceil(
150+
prompt_without_context.api_prompt.length / 4
151+
)
152+
const additional_tokens =
153+
Math.ceil(prompt_with_context.api_prompt.length / 4) - skip_tokens
154+
155+
const attach_label = t(
156+
'command.generate-commit-message.attach-context-files.attach'
157+
)
158+
const skip_label = t(
159+
'command.generate-commit-message.attach-context-files.skip'
160+
)
161+
const last_selected_id =
162+
params.extension_context.workspaceState.get<string>(
163+
LAST_USE_CONTEXT_FILES_STATE_KEY,
164+
'attach'
165+
)
166+
167+
const show_back_button =
168+
was_empty_stage && !is_single_change && !params.stage_all_if_none_staged
169+
170+
was_context_prompt_shown = true
171+
172+
const answer = await new Promise<string | undefined | 'back'>(
173+
(resolve) => {
174+
const quick_pick = vscode.window.createQuickPick<
175+
vscode.QuickPickItem & { id: string }
176+
>()
177+
quick_pick.items = [
178+
{
179+
label: skip_label,
180+
description: `${display_token_count(skip_tokens)} tokens`,
181+
id: 'skip'
182+
},
183+
{
184+
label: attach_label,
185+
description: `+${display_token_count(additional_tokens)} tokens`,
186+
id: 'attach'
187+
}
188+
]
189+
quick_pick.activeItems = [
190+
quick_pick.items.find((i) => i.id === last_selected_id) ||
191+
quick_pick.items[1]
192+
]
193+
quick_pick.title = t(
194+
'command.generate-commit-message.attach-context-files.title'
195+
)
196+
quick_pick.placeholder = t(
197+
'command.generate-commit-message.attach-context-files.placeholder'
198+
)
199+
quick_pick.ignoreFocusOut = true
200+
const close_button = {
201+
iconPath: new vscode.ThemeIcon('close'),
202+
tooltip: t('common.close')
203+
}
204+
quick_pick.buttons = [
205+
...(show_back_button ? [vscode.QuickInputButtons.Back] : []),
206+
close_button
207+
]
208+
209+
let is_resolved = false
210+
211+
quick_pick.onDidTriggerButton((button) => {
212+
if (button === vscode.QuickInputButtons.Back) {
213+
is_resolved = true
214+
resolve('back')
215+
quick_pick.hide()
216+
} else if (button === close_button) {
217+
is_resolved = true
218+
resolve(undefined)
219+
quick_pick.hide()
220+
}
221+
})
222+
223+
quick_pick.onDidAccept(() => {
224+
is_resolved = true
225+
resolve(quick_pick.selectedItems[0]?.id)
226+
quick_pick.hide()
227+
})
228+
229+
quick_pick.onDidHide(() => {
230+
if (!is_resolved) {
231+
resolve(undefined)
232+
}
233+
quick_pick.dispose()
234+
})
235+
236+
quick_pick.show()
237+
}
238+
)
239+
240+
if (answer === 'back') {
241+
if (was_empty_stage) {
242+
await vscode.commands.executeCommand(
243+
'git.unstageAll',
244+
params.repository
245+
)
246+
}
247+
return 'back'
248+
}
249+
250+
if (answer === undefined) {
251+
if (was_empty_stage) {
252+
await vscode.commands.executeCommand(
253+
'git.unstageAll',
254+
params.repository
255+
)
256+
}
257+
return null
258+
}
259+
260+
params.extension_context.workspaceState.update(
261+
LAST_USE_CONTEXT_FILES_STATE_KEY,
262+
answer
263+
)
264+
265+
if (answer === 'attach') {
266+
api_prompt = prompt_with_context.api_prompt
267+
chatbot_prompt = prompt_with_context.chatbot_prompt
268+
} else {
269+
api_prompt = prompt_without_context.api_prompt
270+
chatbot_prompt = prompt_without_context.chatbot_prompt
271+
}
272+
} else {
273+
const prompt = await build_commit_message_prompt(
274+
diff,
275+
params.repository,
276+
[]
277+
)
278+
api_prompt = prompt.api_prompt
279+
chatbot_prompt = prompt.chatbot_prompt
280+
}
281+
} else {
282+
const prompt = await build_commit_message_prompt(
283+
diff,
284+
params.repository,
285+
[]
286+
)
287+
api_prompt = prompt.api_prompt
288+
chatbot_prompt = prompt.chatbot_prompt
289+
}
115290

116291
return {
117292
repository: params.repository,
118293
was_empty_stage,
119294
api_prompt,
120295
chatbot_prompt,
121296
is_single_change,
122-
staged_files: diff_file_paths
297+
staged_files: diff_file_paths,
298+
was_context_prompt_shown
123299
}
124300
}

0 commit comments

Comments
 (0)