Skip to content

Commit 70ca4fe

Browse files
committed
Refactor preset creation logic to correctly insert new presets after trailing groups or separators, and standardize comparison operators in the Presets UI component
1 parent e3ae0c2 commit 70ca4fe

3 files changed

Lines changed: 39 additions & 28 deletions

File tree

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

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
310310
list={sortable_list}
311311
setList={(new_state) => {
312312
const ungrouped_item = new_state.find(
313-
(item) => item.id === UNGROUPED_ID
313+
(item) => item.id == UNGROUPED_ID
314314
)
315315
const ungrouped_was_moved = ungrouped_item
316316
? new_state[0].id !== UNGROUPED_ID
@@ -319,10 +319,10 @@ export const Presets: React.FC<Presets.Props> = (props) => {
319319
// Handle ungrouped item drag
320320
if (ungrouped_item && ungrouped_was_moved) {
321321
const ungrouped_item_index = new_state.findIndex(
322-
(item) => item.id === UNGROUPED_ID
322+
(item) => item.id == UNGROUPED_ID
323323
)
324324
const new_state_without_ungrouped = new_state.filter(
325-
(item) => item.id !== UNGROUPED_ID
325+
(item) => item.id != UNGROUPED_ID
326326
)
327327

328328
// Determine where to insert the new group
@@ -337,11 +337,11 @@ export const Presets: React.FC<Presets.Props> = (props) => {
337337

338338
if (preset_at_drop_location) {
339339
target_full_index = props.presets.findIndex(
340-
(p) => p.name === preset_at_drop_location.name
340+
(p) => p.name == preset_at_drop_location.name
341341
)
342342

343343
// Fallback if preset not found
344-
if (target_full_index === -1) {
344+
if (target_full_index == -1) {
345345
target_full_index = props.presets.length
346346
}
347347
} else {
@@ -362,23 +362,23 @@ export const Presets: React.FC<Presets.Props> = (props) => {
362362

363363
// Handle trailing separator item drag
364364
const trailing_separator_item = new_state.find(
365-
(item) => item.id === TRAILING_SEPARATOR_ID
365+
(item) => item.id == TRAILING_SEPARATOR_ID
366366
)
367367
const trailing_separator_was_moved =
368368
trailing_separator_item &&
369369
sortable_list.findIndex(
370-
(item) => item.id === TRAILING_SEPARATOR_ID
371-
) !==
370+
(item) => item.id == TRAILING_SEPARATOR_ID
371+
) !=
372372
new_state.findIndex(
373-
(item) => item.id === TRAILING_SEPARATOR_ID
373+
(item) => item.id == TRAILING_SEPARATOR_ID
374374
)
375375

376376
if (trailing_separator_item && trailing_separator_was_moved) {
377377
const trailing_separator_item_index = new_state.findIndex(
378-
(item) => item.id === TRAILING_SEPARATOR_ID
378+
(item) => item.id == TRAILING_SEPARATOR_ID
379379
)
380380
const new_state_without_trailing_separator = new_state.filter(
381-
(item) => item.id !== TRAILING_SEPARATOR_ID
381+
(item) => item.id != TRAILING_SEPARATOR_ID
382382
)
383383

384384
// Determine where to insert the new separator
@@ -395,16 +395,16 @@ export const Presets: React.FC<Presets.Props> = (props) => {
395395
]
396396

397397
if (preset_at_drop_location) {
398-
if (preset_at_drop_location.id === UNGROUPED_ID) {
398+
if (preset_at_drop_location.id == UNGROUPED_ID) {
399399
target_full_index = 0
400400
} else {
401401
target_full_index = props.presets.findIndex(
402-
(p) => p.name === preset_at_drop_location.name
402+
(p) => p.name == preset_at_drop_location.name
403403
)
404404
}
405405

406406
// Fallback if preset not found
407-
if (target_full_index === -1) {
407+
if (target_full_index == -1) {
408408
target_full_index = props.presets.length
409409
}
410410
} else {
@@ -423,18 +423,18 @@ export const Presets: React.FC<Presets.Props> = (props) => {
423423

424424
// Handle trailing group item drag
425425
const trailing_group_item = new_state.find(
426-
(item) => item.id === TRAILING_GROUP_ID
426+
(item) => item.id == TRAILING_GROUP_ID
427427
)
428428
const trailing_group_was_moved =
429429
trailing_group_item &&
430430
sortable_list.findIndex(
431-
(item) => item.id === TRAILING_GROUP_ID
431+
(item) => item.id == TRAILING_GROUP_ID
432432
) !==
433-
new_state.findIndex((item) => item.id === TRAILING_GROUP_ID)
433+
new_state.findIndex((item) => item.id == TRAILING_GROUP_ID)
434434

435435
if (trailing_group_item && trailing_group_was_moved) {
436436
const trailing_group_item_index = new_state.findIndex(
437-
(item) => item.id === TRAILING_GROUP_ID
437+
(item) => item.id == TRAILING_GROUP_ID
438438
)
439439
const item_after_tg = new_state[trailing_group_item_index + 1]
440440
const item_after_that =
@@ -443,7 +443,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
443443
// A preset was dragged to the end to create a new group
444444
if (
445445
item_after_tg?.chatbot &&
446-
item_after_that?.id === TRAILING_SEPARATOR_ID
446+
item_after_that?.id == TRAILING_SEPARATOR_ID
447447
) {
448448
props.on_create_group({
449449
instant: true,
@@ -470,16 +470,16 @@ export const Presets: React.FC<Presets.Props> = (props) => {
470470
]
471471

472472
if (preset_at_drop_location) {
473-
if (preset_at_drop_location.id === UNGROUPED_ID) {
473+
if (preset_at_drop_location.id == UNGROUPED_ID) {
474474
target_full_index = 0
475475
} else {
476476
target_full_index = props.presets.findIndex(
477-
(p) => p.name === preset_at_drop_location.name
477+
(p) => p.name == preset_at_drop_location.name
478478
)
479479
}
480480

481481
// Fallback if preset not found
482-
if (target_full_index === -1) {
482+
if (target_full_index == -1) {
483483
target_full_index = props.presets.length
484484
}
485485
} else {
@@ -545,7 +545,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
545545
handle={`.${styles.presets__item__left__drag_handle}`}
546546
>
547547
{sortable_list.map((preset) => {
548-
if (preset.id === TRAILING_SEPARATOR_ID) {
548+
if (preset.id == TRAILING_SEPARATOR_ID) {
549549
return (
550550
<div
551551
key={TRAILING_SEPARATOR_ID}

‎packages/vscode/src/constants/instructions.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ export const code_completion_instructions_for_panel = (
66
row: number,
77
column: number
88
) =>
9-
`Find correct replacement text for the <missing_text> symbol. Your response must begin with a Markdown Code Block containing the replacement text, followed by an explanation. The first line of the Markdown Code Block must be a commented-out text: "${file_path}" ${
9+
`Find correct replacement text for the <missing_text> symbol. Your response must begin with a Markdown Code Block containing the replacement text, followed by an explanation. The first line of the Markdown Code Block must be: "// ${file_path} ${
1010
row + 1
1111
}:${
1212
column + 1
13-
}. Always refer to the symbol "<missing_text>" as "cursor position" and "replacement" as "completion".`
13+
}". Always refer to the symbol "<missing_text>" as "cursor position" and "replacement" as "completion".`
1414

1515
export const intelligent_update_instructions =
1616
"User requested refactor of a file. Print file in full because I have a disability which means I can't type and need to be able to just copy and paste."

‎packages/vscode/src/views/panel/backend/message-handlers/handle-create-preset.ts‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,20 @@ export const handle_create_preset = async (
3232
systemInstructions: CHATBOTS['AI Studio'].default_system_instructions
3333
}
3434

35-
const updated_presets = message.add_on_top
36-
? [new_preset, ...current_presets]
37-
: [...current_presets, new_preset]
35+
let updated_presets: ConfigPresetFormat[]
36+
if (message.add_on_top) {
37+
updated_presets = [new_preset, ...current_presets]
38+
} else {
39+
const last_group_or_separator = [...current_presets]
40+
.reverse()
41+
.find((p) => (!p.chatbot && p.name) || !p.name)
42+
43+
if (last_group_or_separator?.name && !last_group_or_separator.chatbot) {
44+
updated_presets = [...current_presets, {}, new_preset]
45+
} else {
46+
updated_presets = [...current_presets, new_preset]
47+
}
48+
}
3849

3950
try {
4051
panel_provider.send_message({

0 commit comments

Comments
 (0)