Skip to content

Commit 7b8abc7

Browse files
committed
Centralize state key definitions to improve code organization
1 parent 43c5762 commit 7b8abc7

3 files changed

Lines changed: 43 additions & 28 deletions

File tree

packages/browser/src/content-scripts/send-prompt-content-script/chatbots/together.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,10 @@ export const together: Chatbot = {
117117
const observer = new MutationObserver((mutations) => {
118118
mutations.forEach(() => {
119119
if (
120-
document.querySelector('button[data-testid="stop-button"]') &&
121-
document.querySelector('div[data-testid="assistant-message-toolbar"]')
120+
document.querySelector('button[data-testid="stop-button"]') ||
121+
!document.querySelector(
122+
'div[data-testid="assistant-message-toolbar"]'
123+
)
122124
) {
123125
return
124126
}

packages/vscode/src/constants/state-keys.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Use kebab-case for new keys
1+
// Note: Use kebab-case for new keys
22
export const LAST_APPLIED_CHANGES_STATE_KEY = 'last-applied-changes-state'
33
export const LAST_APPLIED_CLIPBOARD_CONTENT_STATE_KEY =
44
'last-applied-clipboard-content'
@@ -45,6 +45,15 @@ export const PINNED_HISTORY_CODE_COMPLETIONS_STATE_KEY =
4545
'pinned-history-code-completions'
4646
export const PINNED_HISTORY_NO_CONTEXT_STATE_KEY = 'pinned-history-no-context'
4747

48+
export const get_last_group_or_preset_choice_state_key = (web_mode: string) =>
49+
`last-group-or-preset-choice-${web_mode}`
50+
51+
export const get_last_selected_preset_key = (web_mode: string) =>
52+
`last-selected-preset-${web_mode}`
53+
54+
export const get_last_selected_group_state_key = (web_mode: string) =>
55+
`last-selected-group-${web_mode}`
56+
4857
export interface HistoryEntry {
4958
text: string
5059
createdAt: number

packages/vscode/src/view/backend/message-handlers/handle-send-prompt.ts

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ import { apply_preset_affixes_to_instruction } from '@/utils/apply-preset-affixe
66
import { replace_saved_context_placeholder } from '@/utils/replace-saved-context-placeholder'
77
import { replace_changes_placeholder } from '@/view/backend/utils/replace-changes-placeholder'
88
import { chat_code_completion_instructions } from '@/constants/instructions'
9+
import {
10+
get_last_group_or_preset_choice_state_key,
11+
get_last_selected_group_state_key,
12+
get_last_selected_preset_key
13+
} from '@/constants/state-keys'
914
import { ConfigPresetFormat } from '../utils/preset-format-converters'
1015
import { extract_file_paths_from_instruction } from '@/utils/extract-file-paths-from-instruction'
1116
import { CHATBOTS } from '@shared/constants/chatbots'
@@ -20,26 +25,22 @@ export const handle_send_prompt = async (params: {
2025
group_name?: string
2126
show_quick_pick?: boolean
2227
}): Promise<void> => {
23-
const LAST_GROUP_OR_PRESET_CHOICE_STATE_KEY = `last-group-or-preset-choice-${params.provider.web_mode}`
24-
const LAST_SELECTED_PRESET_KEY = `last-selected-preset-${params.provider.web_mode}`
25-
const LAST_SELECTED_GROUP_STATE_KEY = `last-selected-group-${params.provider.web_mode}`
26-
2728
if (params.preset_name !== undefined) {
2829
params.provider.context.workspaceState.update(
29-
LAST_GROUP_OR_PRESET_CHOICE_STATE_KEY,
30+
get_last_group_or_preset_choice_state_key(params.provider.web_mode),
3031
'Preset'
3132
)
3233
params.provider.context.workspaceState.update(
33-
LAST_SELECTED_PRESET_KEY,
34+
get_last_selected_preset_key(params.provider.web_mode),
3435
params.preset_name
3536
)
3637
} else if (params.group_name) {
3738
params.provider.context.workspaceState.update(
38-
LAST_GROUP_OR_PRESET_CHOICE_STATE_KEY,
39+
get_last_group_or_preset_choice_state_key(params.provider.web_mode),
3940
'Group'
4041
)
4142
params.provider.context.workspaceState.update(
42-
LAST_SELECTED_GROUP_STATE_KEY,
43+
get_last_selected_group_state_key(params.provider.web_mode),
4344
params.group_name
4445
)
4546
}
@@ -210,10 +211,8 @@ async function show_preset_quick_pick(
210211
context: vscode.ExtensionContext,
211212
mode: string
212213
): Promise<string[]> {
213-
const LAST_SELECTED_PRESET_KEY = `last-selected-preset-${mode}`
214-
215214
const last_selected_item = context.workspaceState.get<string | undefined>(
216-
LAST_SELECTED_PRESET_KEY,
215+
get_last_selected_preset_key(mode),
217216
undefined
218217
)
219218

@@ -277,7 +276,7 @@ async function show_preset_quick_pick(
277276

278277
if (selected && selected.name !== undefined) {
279278
const selected_name = selected.name
280-
context.workspaceState.update(LAST_SELECTED_PRESET_KEY, selected_name)
279+
context.workspaceState.update(get_last_selected_preset_key(mode), selected_name)
281280
resolve([selected_name])
282281
} else {
283282
resolve([])
@@ -302,9 +301,14 @@ async function resolve_presets(params: {
302301
show_quick_pick?: boolean
303302
context: vscode.ExtensionContext
304303
}): Promise<string[]> {
305-
const LAST_GROUP_OR_PRESET_CHOICE_STATE_KEY = `last-group-or-preset-choice-${params.provider.web_mode}`
306-
const LAST_SELECTED_PRESET_KEY = `last-selected-preset-${params.provider.web_mode}`
307-
const LAST_SELECTED_GROUP_STATE_KEY = `last-selected-group-${params.provider.web_mode}`
304+
const last_group_or_preset_choice_state_key =
305+
get_last_group_or_preset_choice_state_key(params.provider.web_mode)
306+
const last_selected_preset_key = get_last_selected_preset_key(
307+
params.provider.web_mode
308+
)
309+
const last_selected_group_state_key = get_last_selected_group_state_key(
310+
params.provider.web_mode
311+
)
308312

309313
const PRESET = 'Preset'
310314
const GROUP = 'Group'
@@ -381,12 +385,12 @@ async function resolve_presets(params: {
381385
if (params.preset_name === undefined && params.group_name === undefined) {
382386
// Try to use last selection if "Send" button is clicked without specific preset/group
383387
const last_choice = params.context.workspaceState.get<string>(
384-
LAST_GROUP_OR_PRESET_CHOICE_STATE_KEY
388+
last_group_or_preset_choice_state_key
385389
)
386390

387391
if (last_choice == PRESET) {
388392
const last_preset = params.context.workspaceState.get<string>(
389-
LAST_SELECTED_PRESET_KEY
393+
last_selected_preset_key
390394
)
391395
if (
392396
last_preset !== undefined &&
@@ -396,7 +400,7 @@ async function resolve_presets(params: {
396400
}
397401
} else if (last_choice == GROUP) {
398402
const last_group = params.context.workspaceState.get<string>(
399-
LAST_SELECTED_GROUP_STATE_KEY
403+
last_selected_group_state_key
400404
)
401405
if (last_group) {
402406
if (last_group == 'Ungrouped') {
@@ -431,12 +435,12 @@ async function resolve_presets(params: {
431435
}
432436
} else {
433437
const last_choice = params.context.workspaceState.get<string>(
434-
LAST_GROUP_OR_PRESET_CHOICE_STATE_KEY
438+
last_group_or_preset_choice_state_key
435439
)
436440

437441
if (last_choice == PRESET) {
438442
const last_preset = params.context.workspaceState.get<string>(
439-
LAST_SELECTED_PRESET_KEY
443+
last_selected_preset_key
440444
)
441445
if (
442446
last_preset !== undefined &&
@@ -446,7 +450,7 @@ async function resolve_presets(params: {
446450
}
447451
} else if (last_choice == GROUP) {
448452
const last_group = params.context.workspaceState.get<string>(
449-
LAST_SELECTED_GROUP_STATE_KEY
453+
last_selected_group_state_key
450454
)
451455
if (last_group) {
452456
if (last_group == 'Ungrouped') {
@@ -497,7 +501,7 @@ async function resolve_presets(params: {
497501
quick_pick.items = items
498502
quick_pick.placeholder = 'Select what to initialize'
499503
const last_choice = params.context.workspaceState.get<string>(
500-
LAST_GROUP_OR_PRESET_CHOICE_STATE_KEY
504+
last_group_or_preset_choice_state_key
501505
)
502506
if (last_choice) {
503507
const last_item = items.find((item) => item.label == last_choice)
@@ -522,7 +526,7 @@ async function resolve_presets(params: {
522526
}
523527

524528
params.context.workspaceState.update(
525-
LAST_GROUP_OR_PRESET_CHOICE_STATE_KEY,
529+
last_group_or_preset_choice_state_key,
526530
choice
527531
)
528532

@@ -583,7 +587,7 @@ async function resolve_presets(params: {
583587
quick_pick.placeholder = 'Select a group'
584588

585589
const last_selected_group = params.context.workspaceState.get<string>(
586-
LAST_SELECTED_GROUP_STATE_KEY,
590+
last_selected_group_state_key,
587591
''
588592
)
589593
if (last_selected_group) {
@@ -609,7 +613,7 @@ async function resolve_presets(params: {
609613

610614
const group_name = selected.name
611615
params.context.workspaceState.update(
612-
LAST_SELECTED_GROUP_STATE_KEY,
616+
last_selected_group_state_key,
613617
group_name
614618
)
615619

0 commit comments

Comments
 (0)