Skip to content

Commit 105fcac

Browse files
committed
Refactor chatbot model accessors to use optional chaining instead of explicit type casting to any
1 parent 96b1825 commit 105fcac

12 files changed

Lines changed: 32 additions & 33 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export const claude: Chatbot = {
3232
return
3333
}
3434

35-
const model_name_to_find = (CHATBOTS['Claude'].models as any)[model]?.label
35+
const model_name_to_find = CHATBOTS['Claude'].models?.[model]?.label
3636
if (!model_name_to_find) {
3737
report_initialization_error({
3838
function_name: 'set_model',

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const copilot: Chatbot = {
3333
return
3434
}
3535

36-
const model_label_to_find = (CHATBOTS['Copilot'].models as any)[model]
36+
const model_label_to_find = CHATBOTS['Copilot'].models?.[model]
3737
?.label
3838
if (!model_label_to_find) return
3939

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

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,6 @@ import {
77
import { report_initialization_error } from '../utils/report-initialization-error'
88

99
export const deepseek: Chatbot = {
10-
wait_until_ready: async () => {
11-
await new Promise((resolve) => {
12-
const check_for_element = () => {
13-
if (document.querySelector('div.intercom-lightweight-app')) {
14-
resolve(null)
15-
} else {
16-
setTimeout(check_for_element, 100)
17-
}
18-
}
19-
check_for_element()
20-
})
21-
await new Promise((resolve) => setTimeout(resolve, 500))
22-
},
2310
set_options: async (options?: string[]) => {
2411
if (!options) return
2512

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

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ export const gemini: Chatbot = {
2020
})
2121
},
2222
set_model: async (model?: string) => {
23-
if (model && CHATBOTS['Gemini'].models && model in CHATBOTS['Gemini'].models) {
23+
if (
24+
model &&
25+
CHATBOTS['Gemini'].models &&
26+
model in CHATBOTS['Gemini'].models
27+
) {
2428
const model_selector_trigger = document.querySelector(
2529
'bard-mode-switcher button'
2630
) as HTMLButtonElement
@@ -32,7 +36,7 @@ export const gemini: Chatbot = {
3236
return
3337
}
3438

35-
const model_label = (CHATBOTS['Gemini'].models as any)[model].label
39+
const model_label = CHATBOTS['Gemini'].models[model].label
3640
if (model_selector_trigger.textContent == model_label) {
3741
return
3842
}
@@ -50,16 +54,24 @@ export const gemini: Chatbot = {
5054
return
5155
}
5256
const model_options = Array.from(menu_content.querySelectorAll('button'))
57+
const target_data_test_id = model_label.toLowerCase()
58+
let found = false
5359
for (const option of model_options) {
54-
const name_element = option.querySelector(
55-
'.title-and-description > span:first-child'
56-
)
57-
if (name_element && name_element.textContent?.trim() == model_label) {
58-
;(option as HTMLElement).click()
59-
await new Promise((r) => requestAnimationFrame(r))
60+
if (
61+
option.getAttribute('data-test-id')?.includes(target_data_test_id)
62+
) {
63+
option.click()
64+
found = true
6065
break
6166
}
6267
}
68+
if (!found) {
69+
report_initialization_error({
70+
function_name: 'set_model',
71+
log_message: `Model option for "${model_label}" not found`
72+
})
73+
}
74+
await new Promise((r) => requestAnimationFrame(r))
6375
}
6476
},
6577
set_options: async (options?: string[]) => {

‎packages/browser/src/content-scripts/send-prompt-content-script/chatbots/github-copilot.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export const github_copilot: Chatbot = {
3333
return
3434
}
3535

36-
const model_label_to_find = (CHATBOTS['GitHub Copilot'].models as any)[
36+
const model_label_to_find = CHATBOTS['GitHub Copilot'].models?.[
3737
model
3838
]?.label
3939
if (!model_label_to_find) return

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export const grok: Chatbot = {
5252
return
5353
}
5454

55-
const model_label_to_find = (CHATBOTS['Grok'].models as any)[model]?.label
55+
const model_label_to_find = CHATBOTS['Grok'].models?.[model]?.label
5656
if (!model_label_to_find) return
5757

5858
if (model_selector_button.textContent?.includes(model_label_to_find)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ export const qwen: Chatbot = {
4444

4545
if (
4646
model_selector_button.textContent?.trim() ==
47-
(CHATBOTS['Qwen'].models as any)[model]?.label
47+
CHATBOTS['Qwen'].models?.[model]?.label
4848
) {
4949
return
5050
}
@@ -73,7 +73,7 @@ export const qwen: Chatbot = {
7373
if (
7474
model_name_element &&
7575
model_name_element.textContent ===
76-
(CHATBOTS['Qwen'].models as any)[model]?.label
76+
CHATBOTS['Qwen'].models?.[model]?.label
7777
) {
7878
button.click()
7979
break

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export const together: Chatbot = {
2222
set_model: async (model?: string) => {
2323
if (!model) return
2424

25-
const model_label_to_find = (CHATBOTS['Together'].models as any)[model]
25+
const model_label_to_find = CHATBOTS['Together'].models?.[model]
2626
?.label
2727
if (!model_label_to_find) return
2828

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export const yuanbao: Chatbot = {
4343
) as HTMLDivElement
4444
if (
4545
model_name_element?.textContent ==
46-
(CHATBOTS['Yuanbao'].models as any)[model]?.label
46+
CHATBOTS['Yuanbao'].models?.[model]?.label
4747
) {
4848
button.click()
4949
break

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
182182

183183
const model_display_name =
184184
model && chatbot
185-
? (CHATBOTS[chatbot].models as any)[model]?.label || model
185+
? CHATBOTS[chatbot].models?.[model]?.label || model
186186
: null
187187

188188
if (is_unnamed) {
@@ -774,7 +774,7 @@ export const Presets: React.FC<Presets.Props> = (props) => {
774774
}
775775

776776
const model_display_name = model
777-
? (CHATBOTS[chatbot].models as any)[model]?.label || model
777+
? CHATBOTS[chatbot].models?.[model]?.label || model
778778
: null
779779

780780
if (is_unnamed) {

0 commit comments

Comments
 (0)