Skip to content

Commit ede830a

Browse files
committed
Replace usePruneContextInstructions hook with direct state management and onBlur synchronization in PromptField component
1 parent 7ea379d commit ede830a

6 files changed

Lines changed: 99 additions & 154 deletions

File tree

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

Lines changed: 50 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import { use_ghost_text } from './hooks/use-ghost-text'
1010
import { use_drag_drop } from './hooks/use-drag-drop'
1111
import { use_keyboard_shortcuts } from './hooks/use-keyboard-shortcuts'
1212
import { use_edit_format_compacting } from './hooks/use-edit-format-compacting'
13-
import { use_prune_context_instructions } from './hooks/use-prune-context-instructions'
1413
import { DropdownMenu } from '../../common/DropdownMenu'
1514
import { use_is_mac } from '@shared/hooks'
1615
import {
@@ -73,13 +72,13 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
7372
const [hovered_edit_format, set_hovered_edit_format] =
7473
useState<EditFormat | null>(null)
7574

76-
const { prune_instructions, set_prune_instructions } =
77-
use_prune_context_instructions({
78-
prune_context_instructions_prefix:
79-
props.prune_context_instructions_prefix,
80-
on_prune_context_instructions_prefix_change:
81-
props.on_prune_context_instructions_prefix_change
82-
})
75+
const [prune_instructions, set_prune_instructions] = useState(
76+
props.prune_context_instructions_prefix
77+
)
78+
79+
useEffect(() => {
80+
set_prune_instructions(props.prune_context_instructions_prefix)
81+
}, [props.prune_context_instructions_prefix])
8382

8483
const toggle_invocation_dropdown = useCallback(() => {
8584
set_is_invocation_dropdown_open((prev) => !prev)
@@ -330,6 +329,14 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
330329
onChange={(e) => {
331330
set_prune_instructions(e.target.value)
332331
}}
332+
onBlur={(e) => {
333+
props.on_prune_context_instructions_prefix_change(
334+
prune_instructions
335+
)
336+
if (!e.target.value) {
337+
set_prune_instructions(props.prune_context_instructions_prefix)
338+
}
339+
}}
333340
onClick={(e) => e.stopPropagation()}
334341
/>
335342
)}
@@ -541,38 +548,42 @@ export const PromptField: React.FC<PromptFieldProps> = (props) => {
541548
{(!props.is_web_mode ||
542549
(props.is_web_mode && props.is_connected)) && (
543550
<>
544-
<div
545-
className={styles['footer__right__invocation-count']}
546-
ref={invocation_dropdown_ref}
547-
>
548-
<button
549-
className={cn(
550-
styles['footer__right__submit__button'],
551-
styles['footer__right__invocation-count__button']
552-
)}
553-
onClick={(e) => {
554-
e.stopPropagation()
555-
set_is_invocation_dropdown_open((prev) => !prev)
556-
close_dropdown()
557-
}}
558-
title={`Invocation count ${is_mac ? '(⌥X 1-5)' : '(Alt+X 1-5)'}`}
551+
{!(
552+
props.prompt_type == 'prune-context' && !props.is_web_mode
553+
) && (
554+
<div
555+
className={styles['footer__right__invocation-count']}
556+
ref={invocation_dropdown_ref}
559557
>
560-
{props.invocation_count}×
561-
</button>
562-
{is_invocation_dropdown_open && (
563-
<DropdownMenu
564-
items={[1, 2, 3, 4, 5].map((count) => ({
565-
label: `${count}×`,
566-
checked: count == props.invocation_count,
567-
shortcut: is_mac ? `⌥X ${count}` : `Alt+X ${count}`,
568-
on_click: () => {
569-
props.on_invocation_count_change(count)
570-
set_is_invocation_dropdown_open(false)
571-
}
572-
}))}
573-
/>
574-
)}
575-
</div>
558+
<button
559+
className={cn(
560+
styles['footer__right__submit__button'],
561+
styles['footer__right__invocation-count__button']
562+
)}
563+
onClick={(e) => {
564+
e.stopPropagation()
565+
set_is_invocation_dropdown_open((prev) => !prev)
566+
close_dropdown()
567+
}}
568+
title={`Invocation count ${is_mac ? '(⌥X 1-5)' : '(Alt+X 1-5)'}`}
569+
>
570+
{props.invocation_count}×
571+
</button>
572+
{is_invocation_dropdown_open && (
573+
<DropdownMenu
574+
items={[1, 2, 3, 4, 5].map((count) => ({
575+
label: `${count}×`,
576+
checked: count == props.invocation_count,
577+
shortcut: is_mac ? `⌥X ${count}` : `Alt+X ${count}`,
578+
on_click: () => {
579+
props.on_invocation_count_change(count)
580+
set_is_invocation_dropdown_open(false)
581+
}
582+
}))}
583+
/>
584+
)}
585+
</div>
586+
)}
576587
<button
577588
className={cn(
578589
styles['footer__right__submit__button'],

‎packages/ui/src/components/editor/panel/PromptField/hooks/use-prune-context-instructions.ts‎

Lines changed: 0 additions & 52 deletions
This file was deleted.

‎packages/vscode/src/views/panel/backend/message-handlers/handle-prune-context.ts‎

Lines changed: 40 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -400,71 +400,51 @@ export const handle_prune_context = async (
400400
}
401401
]
402402

403-
let error_occurred = false
404-
let was_cancelled = false
405-
406-
const promises = Array.from({ length: message.invocation_count }).map(
407-
async () => {
408-
const body: { [key: string]: any } = {
409-
messages,
410-
model: prune_context_config.model,
411-
temperature: prune_context_config.temperature
412-
}
413-
414-
apply_reasoning_effort(
415-
body,
416-
provider,
417-
prune_context_config.reasoning_effort
418-
)
419-
420-
try {
421-
const result = await panel_provider.api_manager.get({
422-
endpoint_url,
423-
api_key: provider.api_key,
424-
body,
425-
provider_name: prune_context_config.provider_name,
426-
model: prune_context_config.model,
427-
reasoning_effort: prune_context_config.reasoning_effort
428-
})
403+
const body: { [key: string]: any } = {
404+
messages,
405+
model: prune_context_config.model,
406+
temperature: prune_context_config.temperature
407+
}
429408

430-
if (result) {
431-
vscode.commands.executeCommand('codeWebChat.applyChatResponse', {
432-
response: result.response,
433-
raw_instructions: instructions
434-
})
435-
return true
436-
}
437-
} catch (error) {
438-
if (axios.isCancel(error)) {
439-
was_cancelled = true
440-
return false
441-
}
442-
Logger.error({
443-
function_name: 'handle_prune_context',
444-
message: 'prune context task error',
445-
data: error
446-
})
447-
if (!error_occurred) {
448-
vscode.window.showErrorMessage(
449-
'Prune context error. See console for details.'
450-
)
451-
error_occurred = true
452-
}
453-
return false
454-
}
455-
return false
456-
}
409+
apply_reasoning_effort(
410+
body,
411+
provider,
412+
prune_context_config.reasoning_effort
457413
)
458414

459-
const results = await Promise.all(promises)
460-
461-
if (error_occurred || was_cancelled) return
415+
try {
416+
const result = await panel_provider.api_manager.get({
417+
endpoint_url,
418+
api_key: provider.api_key,
419+
body,
420+
provider_name: prune_context_config.provider_name,
421+
model: prune_context_config.model,
422+
reasoning_effort: prune_context_config.reasoning_effort
423+
})
462424

463-
if (results.some((r) => r)) {
425+
if (result) {
426+
vscode.commands.executeCommand('codeWebChat.applyChatResponse', {
427+
response: result.response,
428+
raw_instructions: instructions
429+
})
430+
return
431+
}
432+
} catch (error) {
433+
if (axios.isCancel(error)) {
434+
return
435+
}
436+
Logger.error({
437+
function_name: 'handle_prune_context',
438+
message: 'prune context task error',
439+
data: error
440+
})
441+
vscode.window.showErrorMessage(
442+
'Prune context error. See console for details.'
443+
)
464444
return
465-
} else {
466-
should_show_quick_pick = true
467-
current_config_id = undefined
468445
}
446+
447+
should_show_quick_pick = true
448+
current_config_id = undefined
469449
}
470450
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1+
import { prune_context_instructions_prefix } from '@/constants/instructions'
12
import * as vscode from 'vscode'
23

34
export const handle_save_prune_context_instructions_prefix = async (
45
prefix: string
56
): Promise<void> => {
67
const config = vscode.workspace.getConfiguration('codeWebChat')
7-
await config.update('pruneContextInstructionsPrefix', prefix)
8+
await config.update(
9+
'pruneContextInstructionsPrefix',
10+
prefix && prefix != prune_context_instructions_prefix ? prefix : undefined,
11+
vscode.ConfigurationTarget.Global
12+
)
813
}

‎packages/vscode/src/views/panel/frontend/Main/MainView/MainView.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { use_last_choice_button_title } from './hooks/use-last-choice-button-tit
1616
import { ContextUtilisation as UiContextUtilisation } from '@ui/components/editor/panel/ContextUtilisation'
1717
import { Header } from './components/Header'
1818
import { use_invocation_counts } from './hooks/use-invocation-counts'
19+
import { prune_context_instructions_prefix } from '@/constants/instructions'
1920

2021
type Props = {
2122
scroll_reset_key: number

‎packages/vscode/src/views/panel/frontend/hooks/use-panel.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ export const use_panel = (vscode: any) => {
190190
}
191191

192192
const handle_prune_context_instructions_prefix_change = (prefix: string) => {
193-
set_prune_context_instructions_prefix(prefix)
194193
post_message(vscode, {
195194
command: 'SAVE_PRUNE_CONTEXT_INSTRUCTIONS_PREFIX',
196195
prefix
@@ -485,7 +484,8 @@ export const use_panel = (vscode: any) => {
485484
if (new_mode == MODE.API && web_prompt_type) {
486485
if (
487486
web_prompt_type == 'edit-context' ||
488-
web_prompt_type == 'code-at-cursor'
487+
web_prompt_type == 'code-at-cursor' ||
488+
web_prompt_type == 'prune-context'
489489
) {
490490
handle_api_prompt_type_change(web_prompt_type)
491491
}

0 commit comments

Comments
 (0)