Skip to content

Commit b7522cb

Browse files
committed
Refactor commit message generation to support file selection persistence and token count display
- move 'simplify prompt symbols' to shared package - display prompt formatted with `packages/shared/src/utils/simplify-prompt-symbols/simplify-prompt-symbols.ts` keep title attribute raw - display prompt formatted with `packages/shared/src/utils/simplify-prompt-symbols/simplify-prompt-symbols.ts` keep title attribute raw in `packages/ui/src/components/editor/panel/Responses/Responses.tsx` - display description formatted with `packages/shared/src/utils/simplify-prompt-symbols/simplify-prompt-symbols.ts` keep title attribute raw in `packages/ui/src/components/editor/panel/Timeline/Timeline.tsx` - change placeholder "Select files to commit" -> "Select files to commit (XK tokens)" - fill translation 'command.commit-message.select-files' - on the quick pick Configurations, in placeholder change (create new translation key) "Select configuration" to "Select configuration to send XK tokens". - rename [Fragment] to 'Select files to commit', adjust code to not send tokens variable - when going back to "Unstaged Files" quick pick, file selection is not persisted—all files are always checked.
1 parent 3fe6b14 commit b7522cb

11 files changed

Lines changed: 92 additions & 26 deletions

File tree

‎apps/editor/src/commands/generate-commit-message-command/generate-commit-message-command.ts‎

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,27 @@ import { ModelProvidersManager } from '@/services/model-providers-manager'
1212
import { t } from '@/i18n'
1313
import axios from 'axios'
1414
import { PromptsForCommitMessagesUtils } from '../../utils/prompts-for-commit-messages-utils'
15-
import { simplify_prompt_symbols } from '../../utils/simplify-prompt-symbols'
15+
import { simplify_prompt_symbols } from '@shared/utils/simplify-prompt-symbols'
1616

1717
export const generate_commit_message_command = (
1818
context: vscode.ExtensionContext
1919
) => {
20-
const get_prompt_data = async (source_control?: vscode.SourceControl) => {
20+
const get_prompt_data = async (
21+
source_control?: vscode.SourceControl,
22+
selection_state?: { files?: string[] }
23+
) => {
2124
const repository = await get_git_repository(source_control)
2225
if (!repository) return null
2326
await vscode.workspace.saveAll()
2427
await repository.status()
2528
const was_empty_stage = (repository.state.indexChanges || []).length == 0
2629
const working_tree_changes = repository.state.workingTreeChanges || []
2730
const is_single_change = was_empty_stage && working_tree_changes.length == 1
28-
const diff = await prepare_staged_changes(repository, !!source_control)
31+
const diff = await prepare_staged_changes(
32+
repository,
33+
!!source_control,
34+
selection_state
35+
)
2936
if (!diff) return null
3037
const message_prompt = await build_commit_message_prompt(diff, repository)
3138
return { repository, was_empty_stage, message_prompt, is_single_change }
@@ -38,14 +45,18 @@ export const generate_commit_message_command = (
3845
let files_staged_by_action = false
3946
let is_single_change_flow = false
4047
const api_providers_manager = new ModelProvidersManager(context)
41-
let force_show_quick_pick = false
48+
let force_quick_pick = false
49+
const selection_state: { files?: string[] } = {}
4250

4351
while (true) {
44-
const data = await get_prompt_data(source_control)
52+
const data = await get_prompt_data(source_control, selection_state)
4553
if (!data) return
4654
const { repository, message_prompt, is_single_change } = data
4755
let { was_empty_stage } = data
4856

57+
// token count for the prompt, used in the config UI
58+
const token_count = Math.ceil(message_prompt.length / 4)
59+
4960
if (was_empty_stage) {
5061
files_staged_by_action = true
5162
is_single_change_flow = is_single_change
@@ -63,10 +74,11 @@ export const generate_commit_message_command = (
6374
const api_config_data = await get_commit_message_config(
6475
context,
6576
show_back_button,
66-
force_show_quick_pick
77+
force_quick_pick,
78+
token_count
6779
)
6880

69-
force_show_quick_pick = false
81+
force_quick_pick = false
7082

7183
if (api_config_data === 'back') {
7284
if (was_empty_stage) {
@@ -99,10 +111,10 @@ export const generate_commit_message_command = (
99111
})
100112
} catch (error) {
101113
if (axios.isCancel(error)) {
102-
force_show_quick_pick = true
114+
force_quick_pick = true
103115
continue
104116
} else {
105-
force_show_quick_pick = true
117+
force_quick_pick = true
106118
continue
107119
}
108120
}
@@ -205,10 +217,12 @@ export const generate_commit_message_command = (
205217
resolve(quick_pick.selectedItems.map((i: any) => i.prompt))
206218
quick_pick.hide()
207219
})
220+
208221
quick_pick.onDidHide(() => {
209222
resolve(undefined)
210223
quick_pick.dispose()
211224
})
225+
212226
quick_pick.show()
213227
})
214228

‎apps/editor/src/commands/generate-commit-message-command/utils/get-commit-message-config.ts‎

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {
88
import { dictionary } from '@shared/constants/dictionary'
99
import { Logger } from '@shared/utils/logger'
1010
import { PROVIDERS } from '@shared/constants/providers'
11+
import { display_token_count } from '@/utils/display-token-count'
1112
import { RECENTLY_USED_COMMIT_MESSAGES_CONFIG_IDS_STATE_KEY } from '@/constants/state-keys'
1213
import { t } from '@/i18n'
1314

@@ -21,7 +22,8 @@ export interface CommitMessageConfig {
2122
export const get_commit_message_config = async (
2223
context: vscode.ExtensionContext,
2324
show_back_button: boolean = true,
24-
force_quick_pick: boolean = false
25+
force_quick_pick: boolean = false,
26+
token_count?: number
2527
): Promise<
2628
| {
2729
config: CommitMessageConfig
@@ -137,7 +139,12 @@ export const get_commit_message_config = async (
137139

138140
quick_pick.items = create_items()
139141
quick_pick.title = t('common.config.title')
140-
quick_pick.placeholder = t('common.config.placeholder')
142+
quick_pick.placeholder =
143+
token_count != null
144+
? t('common.config.placeholder-with-tokens', {
145+
tokens: display_token_count(token_count)
146+
})
147+
: t('common.config.placeholder')
141148
quick_pick.matchOnDescription = true
142149

143150
const items = quick_pick.items as (vscode.QuickPickItem & {

‎apps/editor/src/i18n/translations/commands/generate-commit-message.ts‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,20 +110,20 @@ export const generate_commit_message = {
110110
},
111111
'command.commit-message.select-files': {
112112
en: 'Select files to commit',
113-
pl: 'Wybierz pliki do zatwierdzenia',
113+
pl: 'Wybierz pliki do commita',
114114
'zh-cn': '选择要提交的文件',
115-
ja: 'コミットするファイルを選択',
115+
ja: 'コミットするファイルを選択してください',
116116
'zh-tw': '選擇要提交的檔案',
117-
de: 'Dateien zum Committen auswählen',
118-
es: 'Seleccionar archivos para el commit',
119-
fr: 'Sélectionner les fichiers à commiter',
120-
'pt-br': 'Selecione os arquivos para commit',
117+
de: 'Wählen Sie Dateien zum Committen aus',
118+
es: 'Selecciona los archivos para el commit',
119+
fr: 'Sélectionnez les fichiers à valider',
120+
'pt-br': 'Selecione os arquivos para o commit',
121121
ru: 'Выберите файлы для коммита',
122122
ko: '커밋할 파일 선택',
123123
it: 'Seleziona i file per il commit',
124124
tr: 'İşlenecek dosyaları seçin',
125-
cs: 'Vyberte soubory k commitnutí',
126-
hu: 'Válassza ki a commitolandó fájlokat',
125+
cs: 'Vyberte soubory pro commit',
126+
hu: 'Válassza ki a commitálandó fájlokat',
127127
bg: 'Изберете файлове за commit'
128128
},
129129
'command.commit-message.show-diff': {

‎apps/editor/src/i18n/translations/common.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,24 @@ export const common = {
197197
hu: 'Válasszon konfigurációt',
198198
bg: 'Изберете конфигурация'
199199
},
200+
'common.config.placeholder-with-tokens': {
201+
en: 'Select configuration to send {tokens} tokens',
202+
pl: 'Wybierz konfigurację, aby wysłać {tokens} tokenów',
203+
'zh-cn': '选择配置以发送 {tokens} 个 token',
204+
ja: '設定を選択して {tokens} トークンを送信',
205+
'zh-tw': '選擇設定以發送 {tokens} 個 token',
206+
de: 'Konfiguration auswählen, um {tokens} Tokens zu senden',
207+
es: 'Seleccionar configuración para enviar {tokens} tokens',
208+
fr: 'Sélectionner la configuration pour envoyer {tokens} tokens',
209+
'pt-br': 'Selecionar configuração para enviar {tokens} tokens',
210+
ru: 'Выберите конфигурацию для отправки {tokens} токенов',
211+
ko: '구성 선택 ({tokens} 토큰 전송)',
212+
it: 'Seleziona la configurazione per inviare {tokens} token',
213+
tr: '{tokens} token göndermek için yapılandırma seçin',
214+
cs: 'Vyberte konfiguraci pro odeslání {tokens} tokenů',
215+
hu: 'Válasszon konfigurációt {tokens} token elküldéséhez',
216+
bg: 'Изберете конфигурация за изпращане на {tokens} токена'
217+
},
200218
'common.separator.recently-created': {
201219
en: 'recently created',
202220
pl: 'ostatnio utworzone',

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

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ export const get_git_repository = async (
9090

9191
export const prepare_staged_changes = async (
9292
repository: GitRepository,
93-
stage_all_if_none_staged: boolean = false
93+
stage_all_if_none_staged: boolean = false,
94+
selection_state?: { files?: string[] }
9495
): Promise<string | null> => {
9596
await repository.status()
9697
const staged_changes = repository.state.indexChanges || []
@@ -183,6 +184,7 @@ export const prepare_staged_changes = async (
183184
description: description_parts.join(' · '),
184185
picked: true,
185186
fsPath: change.uri.fsPath,
187+
token_count,
186188
buttons: [
187189
{
188190
iconPath: new vscode.ThemeIcon(
@@ -202,7 +204,15 @@ export const prepare_staged_changes = async (
202204
const selected = await new Promise<any[] | undefined>((resolve) => {
203205
const quick_pick = vscode.window.createQuickPick<any>()
204206
quick_pick.items = items
205-
quick_pick.selectedItems = items
207+
208+
if (selection_state?.files) {
209+
quick_pick.selectedItems = items.filter((i) =>
210+
selection_state.files!.includes(i.fsPath)
211+
)
212+
} else {
213+
quick_pick.selectedItems = items
214+
}
215+
206216
quick_pick.canSelectMany = true
207217
quick_pick.title = t('command.commit-message.unstaged-files')
208218
quick_pick.placeholder = t('command.commit-message.select-files')
@@ -234,7 +244,11 @@ export const prepare_staged_changes = async (
234244
})
235245

236246
quick_pick.onDidAccept(() => {
237-
resolve(Array.from(quick_pick.selectedItems))
247+
const selected_items = Array.from(quick_pick.selectedItems)
248+
if (selection_state) {
249+
selection_state.files = selected_items.map((i: any) => i.fsPath)
250+
}
251+
resolve(selected_items)
238252
quick_pick.hide()
239253
})
240254

apps/editor/src/utils/simplify-prompt-symbols/index.ts renamed to packages/shared/src/utils/simplify-prompt-symbols/index.ts

File renamed without changes.

apps/editor/src/utils/simplify-prompt-symbols/simplify-prompt-symbols.spec.ts renamed to packages/shared/src/utils/simplify-prompt-symbols/simplify-prompt-symbols.spec.ts

File renamed without changes.

apps/editor/src/utils/simplify-prompt-symbols/simplify-prompt-symbols.ts renamed to packages/shared/src/utils/simplify-prompt-symbols/simplify-prompt-symbols.ts

File renamed without changes.

‎packages/ui/src/components/editor/panel/ResponsePreview/ResponsePreview.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { FC, useRef, useState, useMemo, useEffect } from 'react'
22
import { FileInPreview, ItemInPreview } from '@shared/types/file-in-preview'
3+
import { simplify_prompt_symbols } from '@shared/utils/simplify-prompt-symbols'
34
import cn from 'classnames'
45
import styles from './ResponsePreview.module.scss'
56
import { TextItem, InlineFileItem, FileItem } from './components'
@@ -180,6 +181,14 @@ export const ResponsePreview: FC<Props> = (props) => {
180181
return ''
181182
}
182183

184+
const simplified_instructions = useMemo(
185+
() =>
186+
props.raw_instructions
187+
? simplify_prompt_symbols({ prompt: props.raw_instructions })
188+
: '',
189+
[props.raw_instructions]
190+
)
191+
183192
return (
184193
<Scrollable
185194
ref={scrollable_ref}
@@ -194,11 +203,11 @@ export const ResponsePreview: FC<Props> = (props) => {
194203
<div
195204
className={cn(
196205
styles.instructions,
197-
get_instructions_font_size_class(props.raw_instructions)
206+
get_instructions_font_size_class(simplified_instructions)
198207
)}
199208
title={props.raw_instructions}
200209
>
201-
{props.raw_instructions}
210+
{simplified_instructions}
202211
</div>
203212
)}
204213

‎packages/ui/src/components/editor/panel/Responses/Responses.tsx‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import relativeTime from 'dayjs/plugin/relativeTime'
44
import { ResponseHistoryItem } from '@shared/types/response-history-item'
55
import { use_periodic_re_render } from '../../../../hooks/use-periodic-re-render'
66
import { use_dayjs_locale } from '../../../../hooks/use-dayjs-locale'
7+
import { simplify_prompt_symbols } from '@shared/utils/simplify-prompt-symbols'
78
import styles from './Responses.module.scss'
89

910
dayjs.extend(relativeTime)
@@ -61,7 +62,9 @@ export const Responses: React.FC<Props> = (props) => {
6162
/>
6263
)}
6364
{item.raw_instructions ? (
64-
<span>{item.raw_instructions}</span>
65+
<span>
66+
{simplify_prompt_symbols({ prompt: item.raw_instructions })}
67+
</span>
6568
) : (
6669
<span
6770
className={

0 commit comments

Comments
 (0)