Skip to content

Commit 2c3f094

Browse files
committed
Move commit message api configuration onto multi provider setup
1 parent 5ebe2f6 commit 2c3f094

12 files changed

Lines changed: 455 additions & 493 deletions

File tree

packages/vscode/src/commands/commit-changes-command.ts

Lines changed: 4 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,9 @@ import { execSync } from 'child_process'
33
import { Logger } from '@/utils/logger'
44
import {
55
get_git_repository,
6-
prepare_staged_changes,
7-
GitRepository
6+
prepare_staged_changes
87
} from '../utils/git-repository-utils'
9-
import {
10-
get_commit_message_config,
11-
get_ignored_extensions,
12-
collect_affected_files_with_metadata,
13-
handle_file_selection_if_needed,
14-
build_files_content,
15-
generate_commit_message_with_api,
16-
build_commit_message_prompt
17-
} from '../utils/commit-message-generator'
8+
import { generate_commit_message_from_diff } from '../utils/commit-message-generator'
189

1910
export function commit_changes_command(context: vscode.ExtensionContext) {
2011
return vscode.commands.registerCommand(
@@ -27,9 +18,10 @@ export function commit_changes_command(context: vscode.ExtensionContext) {
2718
const diff = await prepare_staged_changes(repository)
2819
if (!diff) return
2920

30-
const commit_message = await generate_commit_message(
21+
const commit_message = await generate_commit_message_from_diff(
3122
context,
3223
repository,
24+
'Generating commit message and committing...',
3325
diff
3426
)
3527

@@ -66,42 +58,3 @@ export function commit_changes_command(context: vscode.ExtensionContext) {
6658
}
6759
)
6860
}
69-
70-
async function generate_commit_message(
71-
context: vscode.ExtensionContext,
72-
repository: GitRepository,
73-
diff: string
74-
): Promise<string | null> {
75-
const config = vscode.workspace.getConfiguration('codeWebChat')
76-
const commit_message_prompt = config.get<string>('commitMessageInstructions')
77-
const all_ignored_extensions = get_ignored_extensions()
78-
79-
const api_config = await get_commit_message_config(context)
80-
if (!api_config) return null
81-
82-
const affected_files_data = await collect_affected_files_with_metadata(
83-
repository,
84-
all_ignored_extensions
85-
)
86-
87-
const selected_files = await handle_file_selection_if_needed(
88-
context,
89-
affected_files_data
90-
)
91-
if (!selected_files) return null
92-
93-
const affected_files = build_files_content(selected_files)
94-
const message = build_commit_message_prompt(
95-
commit_message_prompt!,
96-
affected_files,
97-
diff
98-
)
99-
100-
return await generate_commit_message_with_api(
101-
api_config.endpoint_url,
102-
api_config.provider,
103-
api_config.config,
104-
message,
105-
'Generating commit message and committing...'
106-
)
107-
}

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

Lines changed: 4 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,7 @@ import {
44
get_git_repository,
55
prepare_staged_changes
66
} from '../utils/git-repository-utils'
7-
import {
8-
get_commit_message_config,
9-
get_ignored_extensions,
10-
collect_affected_files_with_metadata,
11-
handle_file_selection_if_needed,
12-
build_files_content,
13-
generate_commit_message_with_api,
14-
build_commit_message_prompt
15-
} from '../utils/commit-message-generator'
7+
import { generate_commit_message_from_diff } from '../utils/commit-message-generator'
168

179
export function generate_commit_message_command(
1810
context: vscode.ExtensionContext
@@ -27,41 +19,13 @@ export function generate_commit_message_command(
2719
const diff = await prepare_staged_changes(repository)
2820
if (!diff) return
2921

30-
const config = vscode.workspace.getConfiguration('codeWebChat')
31-
const commit_message_prompt = config.get<string>(
32-
'commitMessageInstructions'
33-
)
34-
const all_ignored_extensions = get_ignored_extensions()
35-
36-
const api_config = await get_commit_message_config(context)
37-
if (!api_config) return
38-
39-
const affected_files_data = await collect_affected_files_with_metadata(
40-
repository,
41-
all_ignored_extensions
42-
)
43-
44-
const selected_files = await handle_file_selection_if_needed(
22+
const commit_message = await generate_commit_message_from_diff(
4523
context,
46-
affected_files_data
47-
)
48-
if (!selected_files) return
49-
50-
const affected_files = build_files_content(selected_files)
51-
const message = build_commit_message_prompt(
52-
commit_message_prompt!,
53-
affected_files,
24+
repository,
25+
'Waiting for a commit message...',
5426
diff
5527
)
5628

57-
const commit_message = await generate_commit_message_with_api(
58-
api_config.endpoint_url,
59-
api_config.provider,
60-
api_config.config,
61-
message,
62-
'Waiting for a commit message...'
63-
)
64-
6529
if (commit_message) {
6630
repository.inputBox.value = commit_message
6731
}

packages/vscode/src/commands/open-settings-command/open-settings-command.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from 'vscode'
22
import { api_providers } from './api-providers'
33
import { setup_api_tool_multi_config } from './setup-api-tool-multi-config'
4-
import { setup_api_tool } from './setup-api-tool'
54

65
const LABEL_PROVIDERS = '$(key) API Providers'
76
const LABEL_CODE_COMPLETIONS = '$(tools) Code Completions'
@@ -88,16 +87,12 @@ export const open_settings_command = (context: vscode.ExtensionContext) => {
8887
tool: 'intelligent-update'
8988
})
9089
break
91-
case LABEL_COMMIT_MESSAGES: {
92-
const exit_menu = await setup_api_tool({
90+
case LABEL_COMMIT_MESSAGES:
91+
await setup_api_tool_multi_config({
9392
context,
9493
tool: 'commit-messages'
9594
})
96-
if (exit_menu) {
97-
show_menu = false
98-
}
9995
break
100-
}
10196
}
10297
}
10398
})

packages/vscode/src/commands/open-settings-command/setup-api-tool-multi-config.ts

Lines changed: 121 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {
44
Provider,
55
ToolConfig,
66
CodeCompletionsConfigs,
7+
CommitMessagesConfigs,
78
EditContextConfigs,
89
IntelligentUpdateConfigs,
910
ReasoningEffort
@@ -12,6 +13,9 @@ import { ModelFetcher } from '@/services/model-fetcher'
1213
import { PROVIDERS } from '@shared/constants/providers'
1314
import { Logger } from '@/utils/logger'
1415
import { DEFAULT_TEMPERATURE, SupportedTool } from '@shared/constants/api-tools'
16+
import { COMMIT_MESSAGES_CONFIRMATION_THRESHOLD_STATE_KEY } from '@/constants/state-keys'
17+
18+
const DEFAULT_COMMIT_MESSAGE_MAX_TOKENS_BEFORE_ASK = 20000
1519

1620
interface ToolMethods {
1721
get_configs: () => Promise<ToolConfig[]>
@@ -28,6 +32,9 @@ export const setup_api_tool_multi_config = async (params: {
2832
const providers_manager = new ApiProvidersManager(params.context)
2933
const model_fetcher = new ModelFetcher()
3034

35+
const EDIT_INSTRUCTIONS_LABEL = 'Instructions'
36+
const CONFIRMATION_THRESHOLD_LABEL = 'Show file picker threshold'
37+
3138
const BACK_LABEL = '$(arrow-left) Back'
3239
const ADD_CONFIGURATION_LABEL = '$(add) Add configuration...'
3340
const SET_AS_DEFAULT_LABEL = '$(star) Set as default'
@@ -78,6 +85,18 @@ export const setup_api_tool_multi_config = async (params: {
7885
),
7986
get_display_name: () => 'Intelligent Update'
8087
}
88+
case 'commit-messages':
89+
return {
90+
get_configs: () =>
91+
providers_manager.get_commit_messages_tool_configs(),
92+
save_configs: (configs: CommitMessagesConfigs) =>
93+
providers_manager.save_commit_messages_tool_configs(configs),
94+
get_default_config: () =>
95+
providers_manager.get_default_commit_messages_config(),
96+
set_default_config: (config: ToolConfig | null) =>
97+
providers_manager.set_default_commit_messages_config(config as any),
98+
get_display_name: () => 'Commit Messages'
99+
}
81100
default:
82101
throw new Error(`Unsupported tool: ${tool}`)
83102
}
@@ -371,7 +390,10 @@ export const setup_api_tool_multi_config = async (params: {
371390

372391
async function edit_configuration(config: ToolConfig): Promise<boolean> {
373392
const create_edit_options = () => {
374-
const options = [
393+
const options: (vscode.QuickPickItem & {
394+
config?: ToolConfig
395+
index?: number
396+
})[] = [
375397
{ label: BACK_LABEL },
376398
{ label: '', kind: vscode.QuickPickItemKind.Separator },
377399
{
@@ -396,6 +418,32 @@ export const setup_api_tool_multi_config = async (params: {
396418
}
397419
]
398420

421+
if (params.tool == 'commit-messages') {
422+
const current_threshold = params.context.globalState.get<number>(
423+
COMMIT_MESSAGES_CONFIRMATION_THRESHOLD_STATE_KEY,
424+
DEFAULT_COMMIT_MESSAGE_MAX_TOKENS_BEFORE_ASK
425+
)
426+
427+
const current_prompt = vscode.workspace
428+
.getConfiguration('codeWebChat')
429+
.get<string>('commitMessageInstructions', '')
430+
431+
options.push(
432+
{
433+
label: EDIT_INSTRUCTIONS_LABEL,
434+
detail: current_prompt
435+
},
436+
{
437+
label: CONFIRMATION_THRESHOLD_LABEL,
438+
description: `${current_threshold.toString()} tokens${
439+
current_threshold == DEFAULT_COMMIT_MESSAGE_MAX_TOKENS_BEFORE_ASK
440+
? ' (default)'
441+
: ''
442+
}`
443+
}
444+
)
445+
}
446+
399447
const current_is_default =
400448
default_config &&
401449
default_config.provider_type == config.provider_type &&
@@ -434,6 +482,28 @@ export const setup_api_tool_multi_config = async (params: {
434482
return
435483
}
436484

485+
if (
486+
params.tool == 'commit-messages' &&
487+
selected_option.label == EDIT_INSTRUCTIONS_LABEL
488+
) {
489+
await vscode.commands.executeCommand(
490+
'workbench.action.openSettings',
491+
'codeWebChat.commitMessageInstructions'
492+
)
493+
resolve(await edit_configuration(config))
494+
return
495+
}
496+
497+
if (
498+
params.tool == 'commit-messages' &&
499+
selected_option.label == CONFIRMATION_THRESHOLD_LABEL
500+
) {
501+
quick_pick.hide()
502+
await handle_confirmation_threshold_update()
503+
resolve(await edit_configuration(config))
504+
return
505+
}
506+
437507
if (selected_option.label == SET_AS_DEFAULT_LABEL) {
438508
default_config = { ...config }
439509
await tool_methods.set_default_config(default_config)
@@ -580,6 +650,26 @@ export const setup_api_tool_multi_config = async (params: {
580650
})
581651
}
582652

653+
async function handle_confirmation_threshold_update() {
654+
const current_threshold = params.context.globalState.get<number>(
655+
COMMIT_MESSAGES_CONFIRMATION_THRESHOLD_STATE_KEY,
656+
DEFAULT_COMMIT_MESSAGE_MAX_TOKENS_BEFORE_ASK
657+
)
658+
const new_threshold = await set_confirmation_threshold(current_threshold)
659+
if (new_threshold !== undefined) {
660+
await params.context.globalState.update(
661+
COMMIT_MESSAGES_CONFIRMATION_THRESHOLD_STATE_KEY,
662+
new_threshold
663+
)
664+
665+
if (current_threshold != new_threshold) {
666+
vscode.window.showInformationMessage(
667+
`Confirmation threshold updated to ${new_threshold} tokens.`
668+
)
669+
}
670+
}
671+
}
672+
583673
async function select_provider(): Promise<
584674
Pick<Provider, 'type' | 'name'> | undefined
585675
> {
@@ -730,5 +820,35 @@ export const setup_api_tool_multi_config = async (params: {
730820
return { value: selected.effort, cancelled: false }
731821
}
732822

823+
async function set_confirmation_threshold(
824+
current_threshold: number
825+
): Promise<number | undefined> {
826+
const threshold_input = await vscode.window.showInputBox({
827+
title: 'Set Confirmation Threshold',
828+
prompt: 'Enter token count above which to show affected files picker',
829+
value: current_threshold.toString(),
830+
placeHolder: 'Leave empty to restore default',
831+
validateInput: (value) => {
832+
if (value == '') return null
833+
834+
const num = Number(value)
835+
if (isNaN(num)) return 'Please enter a valid number'
836+
if (num < 0) return 'Threshold must be 0 or greater'
837+
if (num > 1000000) return 'Threshold seems too large'
838+
return null
839+
}
840+
})
841+
842+
if (threshold_input === undefined) {
843+
return undefined
844+
}
845+
846+
if (threshold_input === '') {
847+
return DEFAULT_COMMIT_MESSAGE_MAX_TOKENS_BEFORE_ASK
848+
}
849+
850+
return Number(threshold_input)
851+
}
852+
733853
return await show_configs_quick_pick()
734854
}

0 commit comments

Comments
 (0)