Skip to content

Commit 3fe64a2

Browse files
committed
Refactor tool type handling to use a shared ToolType enum and replace switch statements with if-else chains
1 parent 295b391 commit 3fe64a2

12 files changed

Lines changed: 237 additions & 282 deletions

File tree

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
11
import { SettingsProvider } from '@/views/settings/backend/settings-provider'
22
import { delete_configuration } from '../../../utils/delete-configuration'
3+
import { ToolType } from '@/views/settings/types/tools'
34

45
export const handle_delete_configuration = async (
56
provider: SettingsProvider,
67
configuration_id: string,
7-
type:
8-
| 'code-at-cursor'
9-
| 'edit-context'
10-
| 'intelligent-update'
11-
| 'commit-messages'
12-
| 'prune-context'
13-
| 'voice-input'
8+
type: ToolType
149
): Promise<void> => {
1510
await delete_configuration(provider.context, configuration_id, type)
1611
}

‎apps/editor/src/views/settings/backend/message-handlers/handle-get-configurations.ts‎

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
get_tool_config_id
66
} from '@/services/model-providers-manager'
77
import { ConfigurationForClient } from '@/views/settings/types/messages'
8+
import { ToolType } from '@/views/settings/types/tools'
89

910
const create_description = (config: ToolConfig): string => {
1011
const description_parts = [config.provider_name]
@@ -19,54 +20,42 @@ const create_description = (config: ToolConfig): string => {
1920

2021
export const handle_get_configurations = async (params: {
2122
provider: SettingsProvider
22-
type:
23-
| 'code-at-cursor'
24-
| 'edit-context'
25-
| 'intelligent-update'
26-
| 'commit-messages'
27-
| 'prune-context'
28-
| 'voice-input'
23+
type: ToolType
2924
}): Promise<void> => {
3025
const providers_manager = new ModelProvidersManager(params.provider.context)
3126

3227
let saved_configs: ToolConfig[] = []
3328
let default_config: ToolConfig | undefined
3429
let command: string
3530

36-
switch (params.type) {
37-
case 'code-at-cursor':
38-
saved_configs =
39-
await providers_manager.get_code_completions_tool_configs()
40-
default_config =
41-
await providers_manager.get_default_code_completions_config()
42-
command = 'CODE_AT_CURSOR_CONFIGURATIONS'
43-
break
44-
case 'edit-context':
45-
saved_configs = await providers_manager.get_edit_context_tool_configs()
46-
command = 'EDIT_CONTEXT_CONFIGURATIONS'
47-
break
48-
case 'intelligent-update':
49-
saved_configs =
50-
await providers_manager.get_intelligent_update_tool_configs()
51-
default_config =
52-
await providers_manager.get_default_intelligent_update_config()
53-
command = 'INTELLIGENT_UPDATE_CONFIGURATIONS'
54-
break
55-
case 'commit-messages':
56-
saved_configs = await providers_manager.get_commit_messages_tool_configs()
57-
default_config =
58-
await providers_manager.get_default_commit_messages_config()
59-
command = 'COMMIT_MESSAGES_CONFIGURATIONS'
60-
break
61-
case 'prune-context':
62-
saved_configs = await providers_manager.get_prune_context_tool_configs()
63-
command = 'PRUNE_CONTEXT_CONFIGURATIONS'
64-
break
65-
case 'voice-input':
66-
saved_configs = await providers_manager.get_voice_input_tool_configs()
67-
default_config = await providers_manager.get_default_voice_input_config()
68-
command = 'VOICE_INPUT_CONFIGURATIONS'
69-
break
31+
if (params.type == 'code-at-cursor') {
32+
saved_configs = await providers_manager.get_code_completions_tool_configs()
33+
default_config =
34+
await providers_manager.get_default_code_completions_config()
35+
command = 'CODE_AT_CURSOR_CONFIGURATIONS'
36+
} else if (params.type == 'edit-context') {
37+
saved_configs = await providers_manager.get_edit_context_tool_configs()
38+
command = 'EDIT_CONTEXT_CONFIGURATIONS'
39+
} else if (params.type == 'intelligent-update') {
40+
saved_configs =
41+
await providers_manager.get_intelligent_update_tool_configs()
42+
default_config =
43+
await providers_manager.get_default_intelligent_update_config()
44+
command = 'INTELLIGENT_UPDATE_CONFIGURATIONS'
45+
} else if (params.type == 'commit-messages') {
46+
saved_configs = await providers_manager.get_commit_messages_tool_configs()
47+
default_config =
48+
await providers_manager.get_default_commit_messages_config()
49+
command = 'COMMIT_MESSAGES_CONFIGURATIONS'
50+
} else if (params.type == 'prune-context') {
51+
saved_configs = await providers_manager.get_prune_context_tool_configs()
52+
command = 'PRUNE_CONTEXT_CONFIGURATIONS'
53+
} else if (params.type == 'voice-input') {
54+
saved_configs = await providers_manager.get_voice_input_tool_configs()
55+
default_config = await providers_manager.get_default_voice_input_config()
56+
command = 'VOICE_INPUT_CONFIGURATIONS'
57+
} else {
58+
throw new Error(`Unknown tool type: ${params.type}`)
7059
}
7160

7261
const default_config_id = default_config

‎apps/editor/src/views/settings/backend/message-handlers/handle-reorder-configuration.ts‎

Lines changed: 24 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -4,52 +4,40 @@ import {
44
ToolConfig,
55
get_tool_config_id
66
} from '@/services/model-providers-manager'
7+
import { ToolType } from '@/views/settings/types/tools'
78

89
export const handle_reorder_configuration = async (
910
provider: SettingsProvider,
1011
configurations: { id: string }[],
11-
type:
12-
| 'code-at-cursor'
13-
| 'edit-context'
14-
| 'intelligent-update'
15-
| 'commit-messages'
16-
| 'prune-context'
17-
| 'voice-input'
12+
type: ToolType
1813
): Promise<void> => {
1914
const providers_manager = new ModelProvidersManager(provider.context)
2015

2116
let get_configs: () => Promise<ToolConfig[]>
2217
let save_configs: (configs: ToolConfig[]) => Promise<void>
2318

24-
switch (type) {
25-
case 'code-at-cursor':
26-
get_configs = () => providers_manager.get_code_completions_tool_configs()
27-
save_configs = (c) =>
28-
providers_manager.save_code_completions_tool_configs(c)
29-
break
30-
case 'edit-context':
31-
get_configs = () => providers_manager.get_edit_context_tool_configs()
32-
save_configs = (c) => providers_manager.save_edit_context_tool_configs(c)
33-
break
34-
case 'intelligent-update':
35-
get_configs = () =>
36-
providers_manager.get_intelligent_update_tool_configs()
37-
save_configs = (c) =>
38-
providers_manager.save_intelligent_update_tool_configs(c)
39-
break
40-
case 'commit-messages':
41-
get_configs = () => providers_manager.get_commit_messages_tool_configs()
42-
save_configs = (c) =>
43-
providers_manager.save_commit_messages_tool_configs(c)
44-
break
45-
case 'prune-context':
46-
get_configs = () => providers_manager.get_prune_context_tool_configs()
47-
save_configs = (c) => providers_manager.save_prune_context_tool_configs(c)
48-
break
49-
case 'voice-input':
50-
get_configs = () => providers_manager.get_voice_input_tool_configs()
51-
save_configs = (c) => providers_manager.save_voice_input_tool_configs(c)
52-
break
19+
if (type == 'code-at-cursor') {
20+
get_configs = () => providers_manager.get_code_completions_tool_configs()
21+
save_configs = (c) =>
22+
providers_manager.save_code_completions_tool_configs(c)
23+
} else if (type == 'edit-context') {
24+
get_configs = () => providers_manager.get_edit_context_tool_configs()
25+
save_configs = (c) => providers_manager.save_edit_context_tool_configs(c)
26+
} else if (type == 'intelligent-update') {
27+
get_configs = () => providers_manager.get_intelligent_update_tool_configs()
28+
save_configs = (c) =>
29+
providers_manager.save_intelligent_update_tool_configs(c)
30+
} else if (type == 'commit-messages') {
31+
get_configs = () => providers_manager.get_commit_messages_tool_configs()
32+
save_configs = (c) => providers_manager.save_commit_messages_tool_configs(c)
33+
} else if (type == 'prune-context') {
34+
get_configs = () => providers_manager.get_prune_context_tool_configs()
35+
save_configs = (c) => providers_manager.save_prune_context_tool_configs(c)
36+
} else if (type == 'voice-input') {
37+
get_configs = () => providers_manager.get_voice_input_tool_configs()
38+
save_configs = (c) => providers_manager.save_voice_input_tool_configs(c)
39+
} else {
40+
throw new Error(`Unknown tool type: ${type}`)
5341
}
5442

5543
const current_configs = await get_configs()

‎apps/editor/src/views/settings/backend/message-handlers/handle-set-default-configuration.ts‎

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,40 @@ import {
44
get_tool_config_id,
55
ToolConfig
66
} from '@/services/model-providers-manager'
7+
import { ToolType } from '@/views/settings/types/tools'
78

89
export const handle_set_default_configuration = async (
910
provider: SettingsProvider,
1011
configuration_id: string | null,
11-
type:
12-
| 'code-at-cursor'
13-
| 'intelligent-update'
14-
| 'commit-messages'
15-
| 'prune-context'
16-
| 'voice-input'
12+
type: ToolType
1713
): Promise<void> => {
1814
const providers_manager = new ModelProvidersManager(provider.context)
1915

2016
let get_configs: () => Promise<ToolConfig[]>
2117
let set_default_config: (config: ToolConfig | null) => Promise<void>
2218

23-
switch (type) {
24-
case 'code-at-cursor':
25-
get_configs = () => providers_manager.get_code_completions_tool_configs()
26-
set_default_config = (c) =>
27-
providers_manager.set_default_code_completions_config(c)
28-
break
29-
case 'intelligent-update':
30-
get_configs = () =>
31-
providers_manager.get_intelligent_update_tool_configs()
32-
set_default_config = (c) =>
33-
providers_manager.set_default_intelligent_update_config(c)
34-
break
35-
case 'commit-messages':
36-
get_configs = () => providers_manager.get_commit_messages_tool_configs()
37-
set_default_config = (c) =>
38-
providers_manager.set_default_commit_messages_config(c)
39-
break
40-
case 'prune-context':
41-
get_configs = () => providers_manager.get_prune_context_tool_configs()
42-
set_default_config = (c) =>
43-
providers_manager.set_default_prune_context_config(c)
44-
break
45-
case 'voice-input':
46-
get_configs = () => providers_manager.get_voice_input_tool_configs()
47-
set_default_config = (c) =>
48-
providers_manager.set_default_voice_input_config(c)
49-
break
19+
if (type == 'code-at-cursor') {
20+
get_configs = () => providers_manager.get_code_completions_tool_configs()
21+
set_default_config = (c) =>
22+
providers_manager.set_default_code_completions_config(c)
23+
} else if (type == 'intelligent-update') {
24+
get_configs = () => providers_manager.get_intelligent_update_tool_configs()
25+
set_default_config = (c) =>
26+
providers_manager.set_default_intelligent_update_config(c)
27+
} else if (type == 'commit-messages') {
28+
get_configs = () => providers_manager.get_commit_messages_tool_configs()
29+
set_default_config = (c) =>
30+
providers_manager.set_default_commit_messages_config(c)
31+
} else if (type == 'prune-context') {
32+
get_configs = () => providers_manager.get_prune_context_tool_configs()
33+
set_default_config = (c) =>
34+
providers_manager.set_default_prune_context_config(c)
35+
} else if (type == 'voice-input') {
36+
get_configs = () => providers_manager.get_voice_input_tool_configs()
37+
set_default_config = (c) =>
38+
providers_manager.set_default_voice_input_config(c)
39+
} else {
40+
throw new Error(`Unknown tool type: ${type}`)
5041
}
5142

5243
if (configuration_id === null) {

‎apps/editor/src/views/settings/frontend/Home/Home.tsx‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,26 @@ export const Home: React.FC<Props> = (props) => {
221221
[handle_stuck_change]
222222
)
223223

224+
const get_has_warning = (id: NavItem): boolean => {
225+
if (id == 'model-providers') {
226+
return props.providers.length == 0
227+
} else if (id == 'edit-context') {
228+
return props.edit_context_configs.length == 0
229+
} else if (id == 'intelligent-update') {
230+
return props.intelligent_update_configs.length == 0
231+
} else if (id == 'prune-context') {
232+
return props.prune_context_configs.length == 0
233+
} else if (id == 'code-at-cursor') {
234+
return props.code_at_cursor_configs.length == 0
235+
} else if (id == 'voice-input') {
236+
return props.voice_input_configs.length == 0
237+
} else if (id == 'commit-messages') {
238+
return props.commit_messages_configs.length == 0
239+
} else {
240+
return false
241+
}
242+
}
243+
224244
const nav_item_ids = NAV_ITEMS_CONFIG.filter(
225245
(item): item is Extract<NavConfigItem, { type: 'item' }> =>
226246
item.type == 'item'
@@ -309,6 +329,7 @@ export const Home: React.FC<Props> = (props) => {
309329
href={`#${item.id}`}
310330
label={t(item.label)}
311331
is_active={item.id === active_nav_item_id}
332+
has_warning={get_has_warning(item.id)}
312333
on_click={(e) => handle_nav_click(e, item.id)}
313334
/>
314335
)

0 commit comments

Comments
 (0)