Skip to content

Commit 04671ae

Browse files
committed
Refactor chatbot configuration constants and implement a reusable PresetOption component to support model-specific and URL-dependent option disabling in the preset editor
1 parent 0efa6f8 commit 04671ae

14 files changed

Lines changed: 298 additions & 246 deletions

File tree

‎packages/shared/src/constants/chatbots.ts‎

Lines changed: 33 additions & 197 deletions
Large diffs are not rendered by default.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.option {
2+
display: flex;
3+
align-items: center;
4+
gap: 5px;
5+
cursor: pointer;
6+
7+
input {
8+
margin: 0;
9+
}
10+
11+
&.disabled {
12+
cursor: default;
13+
14+
span {
15+
opacity: var(--disabled-opacity);
16+
}
17+
}
18+
}
19+
20+
.disabled_reason {
21+
font-size: 11px;
22+
color: var(--cwc-text-color-dimmed);
23+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useState } from 'react'
2+
import { PresetOption } from './PresetOption'
3+
4+
export default {
5+
component: PresetOption
6+
}
7+
8+
export const Default = () => {
9+
const [checked, set_checked] = useState(false)
10+
11+
return (
12+
<PresetOption
13+
label="Temporary chat"
14+
checked={checked}
15+
on_change={set_checked}
16+
/>
17+
)
18+
}
19+
20+
export const Checked = () => {
21+
const [checked, set_checked] = useState(true)
22+
23+
return (
24+
<PresetOption
25+
label="Temporary chat"
26+
checked={checked}
27+
on_change={set_checked}
28+
/>
29+
)
30+
}
31+
32+
export const Disabled = () => (
33+
<PresetOption
34+
label="Temporary chat"
35+
checked={false}
36+
on_change={() => {}}
37+
disabled
38+
disabled_reason="This option is disabled for a reason."
39+
/>
40+
)
41+
42+
export const DisabledChecked = () => (
43+
<PresetOption
44+
label="Temporary chat"
45+
checked={true}
46+
on_change={() => {}}
47+
disabled
48+
disabled_reason="This option is disabled for a reason."
49+
/>
50+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { FC } from 'react'
2+
import { Checkbox } from '../../common/Checkbox'
3+
import styles from './PresetOption.module.scss'
4+
5+
type Props = {
6+
label: string
7+
checked: boolean
8+
on_change: (checked: boolean) => void
9+
disabled?: boolean
10+
title?: string
11+
disabled_reason?: string
12+
}
13+
14+
export const PresetOption: FC<Props> = ({
15+
label,
16+
checked,
17+
on_change,
18+
disabled,
19+
title,
20+
disabled_reason
21+
}) => {
22+
return (
23+
<div>
24+
<label
25+
className={`${styles.option} ${disabled ? styles.disabled : ''}`}
26+
title={title}
27+
>
28+
<Checkbox
29+
checked={checked}
30+
on_change={on_change}
31+
disabled={disabled}
32+
/>
33+
<span>{label}</span>
34+
</label>
35+
{disabled && disabled_reason && (
36+
<div className={styles.disabled_reason}>{disabled_reason}</div>
37+
)}
38+
</div>
39+
)
40+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './PresetOption'

‎packages/vscode/src/views/panel/backend/message-handlers/handle-pick-open-router-model.ts‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as vscode from 'vscode'
22
import { PanelProvider } from '@/views/panel/backend/panel-provider'
33
import { dictionary } from '@shared/constants/dictionary'
4+
import { Logger } from '@shared/utils/logger'
45
import axios from 'axios'
56

67
export const handle_pick_open_router_model = async (
@@ -17,8 +18,8 @@ export const handle_pick_open_router_model = async (
1718
}))
1819

1920
const selected = await vscode.window.showQuickPick(items, {
20-
title: 'Select Open Router Model',
21-
placeHolder: 'Choose a model from Open Router'
21+
title: 'Select Model',
22+
placeHolder: 'Choose a model'
2223
})
2324

2425
if (selected) {
@@ -28,7 +29,11 @@ export const handle_pick_open_router_model = async (
2829
})
2930
}
3031
} catch (error) {
31-
console.error('Error fetching Open Router models:', error)
32+
Logger.error({
33+
function_name: 'handle_pick_open_router_model',
34+
message: 'Error fetching Open Router models',
35+
data: error
36+
})
3237
vscode.window.showErrorMessage(
3338
dictionary.error_message.FAILED_TO_FETCH_OPEN_ROUTER_MODELS
3439
)

‎packages/vscode/src/views/panel/frontend/components/edit-preset-form/EditPresetForm/EditPresetForm.module.scss‎

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,4 @@
1010
flex-direction: column;
1111
align-items: flex-start;
1212
gap: 4px;
13-
14-
&__item {
15-
display: flex;
16-
align-items: center;
17-
gap: 5px;
18-
cursor: pointer;
19-
20-
input {
21-
margin: 0;
22-
}
23-
}
2413
}

‎packages/vscode/src/views/panel/frontend/components/edit-preset-form/EditPresetForm/EditPresetForm.tsx‎

Lines changed: 16 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { useState, useEffect, useRef, useMemo } from 'react'
22
import cn from 'classnames'
3+
import { useChatgpt } from '../chatbot-hooks'
34
import styles from './EditPresetForm.module.scss'
45
import dropdown_styles from '@ui/components/editor/common/Dropdown/Dropdown.module.scss'
56
import { Preset } from '@shared/types/preset'
67
import { CHATBOTS } from '@shared/constants/chatbots'
78
import { Field } from '@ui/components/editor/panel/Field'
89
import { Slider } from '@ui/components/editor/panel/Slider'
9-
import { Checkbox } from '@ui/components/editor/common/Checkbox'
1010
import { Input } from '@ui/components/editor/common/Input'
1111
import { Textarea } from '@ui/components/editor/common/Textarea'
1212
import { Dropdown } from '@ui/components/editor/common/Dropdown'
1313
import { BackendMessage } from '@/views/panel/types/messages'
1414
import { Scrollable } from '@ui/components/editor/panel/Scrollable'
15+
import { Options } from '../chatbot-overrides/options'
1516

1617
type Props = {
1718
preset: Preset
@@ -78,8 +79,10 @@ export const EditPresetForm: React.FC<Props> = (props) => {
7879
}
7980
}, [model_info])
8081

82+
useChatgpt({ chatbot, new_url, set_options })
83+
8184
const supports_temperature = chatbot_config?.supports_custom_temperature
82-
const supports_top_p = chatbot_config?.supports_custom_top_p
85+
const supports_top_p = chatbot_config?.default_top_p !== undefined
8386
const supports_thinking_budget = chatbot_config?.supports_thinking_budget
8487
const supports_reasoning_effort =
8588
chatbot_config?.supports_reasoning_effort ||
@@ -90,7 +93,6 @@ export const EditPresetForm: React.FC<Props> = (props) => {
9093
const supports_url_override = chatbot_config?.supports_url_override
9194
const supports_user_provided_model =
9295
chatbot_config?.supports_user_provided_model
93-
const supported_options = chatbot_config?.supported_options || {}
9496

9597
useEffect(() => {
9698
if (chatbot) {
@@ -100,7 +102,9 @@ export const EditPresetForm: React.FC<Props> = (props) => {
100102
...(prompt_prefix ? { prompt_prefix } : {}),
101103
...(prompt_suffix ? { prompt_suffix } : {}),
102104
...(temperature !== undefined ? { temperature } : {}),
103-
...(top_p !== CHATBOTS[chatbot].default_top_p ? { top_p } : {}),
105+
...(!supports_top_p || top_p !== chatbot_config.default_top_p
106+
? { top_p }
107+
: {}),
104108
...(thinking_budget !== undefined ? { thinking_budget } : {}),
105109
...(reasoning_effort ? { reasoning_effort } : {}),
106110
...(model ? { model } : {}),
@@ -134,16 +138,6 @@ export const EditPresetForm: React.FC<Props> = (props) => {
134138
options
135139
])
136140

137-
let url_override_label = 'New URL'
138-
let url_override_info = 'Enter link of a project, space, etc.'
139-
if (chatbot == 'ChatGPT') {
140-
url_override_label = 'Project URL'
141-
url_override_info = 'Enter link of a project'
142-
} else if (chatbot == 'Perplexity') {
143-
url_override_label = 'Space URL'
144-
url_override_info = 'Enter link of a space'
145-
}
146-
147141
const handle_chatbot_change = (new_chatbot: keyof typeof CHATBOTS) => {
148142
set_chatbot(new_chatbot)
149143
set_model(Object.keys(CHATBOTS[new_chatbot].models)[0] || undefined)
@@ -327,9 +321,8 @@ export const EditPresetForm: React.FC<Props> = (props) => {
327321

328322
{supports_url_override && (
329323
<Field
330-
label={url_override_label}
324+
label={chatbot_config?.url_override_label || 'URL override'}
331325
html_for="new-url"
332-
info={url_override_info}
333326
>
334327
<Input
335328
id="new-url"
@@ -358,25 +351,13 @@ export const EditPresetForm: React.FC<Props> = (props) => {
358351
</Field>
359352
)}
360353

361-
{Object.keys(supported_options).length > 0 && (
362-
<Field label="Options">
363-
<div className={styles.options}>
364-
{Object.entries(supported_options).map(([key, label]) => {
365-
const model_info = model ? (models as any)[model] : undefined
366-
if (model_info?.disabled_options?.includes(key)) return null
367-
return (
368-
<label key={key} className={styles.options__item}>
369-
<Checkbox
370-
checked={options.includes(key)}
371-
on_change={() => handle_option_toggle(key)}
372-
/>
373-
{label as any}
374-
</label>
375-
)
376-
})}
377-
</div>
378-
</Field>
379-
)}
354+
<Options
355+
chatbot={chatbot}
356+
model={model}
357+
options={options}
358+
new_url={new_url}
359+
on_option_toggle={handle_option_toggle}
360+
/>
380361

381362
{supports_temperature && (
382363
<Field
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { useEffect } from 'react'
2+
3+
type Props = {
4+
chatbot?: string
5+
new_url?: string
6+
set_options: React.Dispatch<React.SetStateAction<string[]>>
7+
}
8+
9+
export const useChatgpt = ({ chatbot, new_url, set_options }: Props) => {
10+
useEffect(() => {
11+
if (chatbot == 'ChatGPT' && new_url) {
12+
set_options((prev) =>
13+
prev.filter((o) => o != 'temporary' && o != 'thinking')
14+
)
15+
}
16+
}, [chatbot, new_url, set_options])
17+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './chatgpt'

0 commit comments

Comments
 (0)