Skip to content

Commit 1e56d70

Browse files
committed
Refactor AI Studio chatbot implementation to remove the redundant set_model function and integrate model-specific reasoning effort support
1 parent e4b20e8 commit 1e56d70

30 files changed

Lines changed: 339 additions & 620 deletions

File tree

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

Lines changed: 71 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@ import { Chatbot } from '../types/chatbot'
22
import { CHATBOTS } from '@shared/constants/chatbots'
33
import { show_response_ready_notification } from '../utils/show-response-ready-notification'
44
import { add_apply_response_button } from '../utils/add-apply-response-button'
5-
import {
6-
InitializationError,
7-
report_initialization_error
8-
} from '../utils/report-initialization-error'
5+
import { report_initialization_error } from '../utils/report-initialization-error'
96

107
export const ai_studio: Chatbot = {
118
wait_until_ready: async () => {
@@ -32,63 +29,6 @@ export const ai_studio: Chatbot = {
3229
check_for_element()
3330
})
3431
},
35-
set_model: async (model?: string) => {
36-
if (!model) return
37-
await open_panel()
38-
const model_selector = document.querySelector(
39-
'button.model-selector-card'
40-
) as HTMLButtonElement
41-
if (!model_selector) {
42-
report_initialization_error({
43-
function_name: 'set_model',
44-
log_message: 'Model selector button not found',
45-
alert_message: InitializationError.UNABLE_TO_SET_MODEL
46-
})
47-
return
48-
}
49-
const model_name = model_selector.querySelector(
50-
'span.title'
51-
) as HTMLSpanElement
52-
if (!model_name) {
53-
report_initialization_error({
54-
function_name: 'set_model',
55-
log_message: 'Model name element not found',
56-
alert_message: InitializationError.UNABLE_TO_SET_MODEL
57-
})
58-
return
59-
}
60-
if (
61-
model_name.textContent ==
62-
(CHATBOTS['AI Studio'].models as any)[model]?.label
63-
) {
64-
return
65-
}
66-
model_selector.click()
67-
await new Promise((r) => requestAnimationFrame(r))
68-
// Click "All" filter option
69-
;(
70-
document.querySelector(
71-
'div.model-categories-container button'
72-
) as HTMLButtonElement
73-
)?.click()
74-
await new Promise((r) => requestAnimationFrame(r))
75-
const model_options = Array.from(
76-
document.querySelectorAll('ms-model-carousel-row button.content-button')
77-
)
78-
for (const option of model_options) {
79-
const model_name_element = option.querySelector(
80-
'span.model-title-text'
81-
) as HTMLElement
82-
if (
83-
model_name_element?.textContent?.trim() ==
84-
(CHATBOTS['AI Studio'].models as any)[model]?.label
85-
) {
86-
;(option as HTMLElement).click()
87-
break
88-
}
89-
}
90-
await new Promise((resolve) => setTimeout(resolve, 500)) // System instructions use the same sliding panel so we need to wait for it complete closure
91-
},
9232
enter_system_instructions: async (system_instructions?: string) => {
9333
if (!system_instructions) return
9434
await open_panel()
@@ -98,8 +38,7 @@ export const ai_studio: Chatbot = {
9838
if (!system_instructions_button) {
9939
report_initialization_error({
10040
function_name: 'enter_system_instructions',
101-
log_message: 'System instructions button not found',
102-
alert_message: InitializationError.UNABLE_TO_SET_SYSTEM_INSTRUCTIONS
41+
log_message: 'System instructions button not found'
10342
})
10443
return
10544
}
@@ -113,8 +52,7 @@ export const ai_studio: Chatbot = {
11352
if (!close_button) {
11453
report_initialization_error({
11554
function_name: 'enter_system_instructions',
116-
log_message: 'Close button for system instructions dialog not found',
117-
alert_message: InitializationError.UNABLE_TO_SET_SYSTEM_INSTRUCTIONS
55+
log_message: 'Close button for system instructions dialog not found'
11856
})
11957
return
12058
}
@@ -125,8 +63,7 @@ export const ai_studio: Chatbot = {
12563
if (!panel) {
12664
report_initialization_error({
12765
function_name: 'enter_system_instructions',
128-
log_message: 'System instructions panel not found',
129-
alert_message: InitializationError.UNABLE_TO_SET_SYSTEM_INSTRUCTIONS
66+
log_message: 'System instructions panel not found'
13067
})
13168
return
13269
}
@@ -135,8 +72,7 @@ export const ai_studio: Chatbot = {
13572
if (!textarea) {
13673
report_initialization_error({
13774
function_name: 'enter_system_instructions',
138-
log_message: 'System instructions textarea not found',
139-
alert_message: InitializationError.UNABLE_TO_SET_SYSTEM_INSTRUCTIONS
75+
log_message: 'System instructions textarea not found'
14076
})
14177
return
14278
}
@@ -169,8 +105,7 @@ export const ai_studio: Chatbot = {
169105
if (!thinking_toggle) {
170106
report_initialization_error({
171107
function_name: 'set_options',
172-
log_message: 'Thinking toggle not found',
173-
alert_message: InitializationError.UNABLE_TO_SET_OPTIONS
108+
log_message: 'Thinking toggle not found'
174109
})
175110
return
176111
}
@@ -202,8 +137,7 @@ export const ai_studio: Chatbot = {
202137
if (!temp_toggle) {
203138
report_initialization_error({
204139
function_name: 'set_options',
205-
log_message: 'Temporary chat toggle not found',
206-
alert_message: InitializationError.UNABLE_TO_SET_OPTIONS
140+
log_message: 'Temporary chat toggle not found'
207141
})
208142
return
209143
}
@@ -219,8 +153,7 @@ export const ai_studio: Chatbot = {
219153
if (!grounding_button) {
220154
report_initialization_error({
221155
function_name: 'set_options',
222-
log_message: 'Grounding with Google Search button not found',
223-
alert_message: InitializationError.UNABLE_TO_SET_OPTIONS
156+
log_message: 'Grounding with Google Search button not found'
224157
})
225158
return
226159
}
@@ -238,8 +171,7 @@ export const ai_studio: Chatbot = {
238171
if (!url_context_button) {
239172
report_initialization_error({
240173
function_name: 'set_options',
241-
log_message: 'URL context button not found',
242-
alert_message: InitializationError.UNABLE_TO_SET_OPTIONS
174+
log_message: 'URL context button not found'
243175
})
244176
return
245177
}
@@ -255,14 +187,61 @@ export const ai_studio: Chatbot = {
255187
if (!temperature_element) {
256188
report_initialization_error({
257189
function_name: 'set_temperature',
258-
log_message: 'Temperature input not found',
259-
alert_message: InitializationError.UNABLE_TO_SET_TEMPERATURE
190+
log_message: 'Temperature input not found'
260191
})
261192
return
262193
}
263194
temperature_element.value = temperature.toString()
264195
temperature_element.dispatchEvent(new Event('change', { bubbles: true }))
265196
},
197+
set_reasoning_effort: async (reasoning_effort?: string) => {
198+
if (!reasoning_effort) return
199+
200+
await open_panel()
201+
202+
const thinking_level_setting = document.querySelector(
203+
'ms-thinking-level-setting mat-form-field > div'
204+
) as HTMLElement
205+
206+
if (!thinking_level_setting) {
207+
report_initialization_error({
208+
function_name: 'set_reasoning_effort',
209+
log_message: 'Thinking level setting element not found'
210+
})
211+
return
212+
}
213+
214+
thinking_level_setting.click()
215+
await new Promise((r) => requestAnimationFrame(r))
216+
217+
const options_container = document.querySelector(
218+
'.cdk-overlay-container div[role="listbox"]'
219+
)
220+
if (!options_container) {
221+
report_initialization_error({
222+
function_name: 'set_reasoning_effort',
223+
log_message: 'Reasoning effort options container not found'
224+
})
225+
return
226+
}
227+
228+
const target_label =
229+
reasoning_effort.charAt(0).toUpperCase() + reasoning_effort.slice(1)
230+
231+
const options = Array.from(
232+
options_container.querySelectorAll('mat-option')
233+
) as HTMLElement[]
234+
235+
for (const option of options) {
236+
const label = option.textContent?.trim()
237+
if (label == target_label) {
238+
option.click()
239+
break
240+
}
241+
}
242+
243+
await new Promise((r) => requestAnimationFrame(r))
244+
},
266245
set_thinking_budget: async (thinking_budget?: number) => {
267246
if (thinking_budget === undefined) {
268247
const thinking_toggle = document.querySelector(
@@ -278,8 +257,7 @@ export const ai_studio: Chatbot = {
278257
if (!manual_budget_toggle) {
279258
report_initialization_error({
280259
function_name: 'set_thinking_budget',
281-
log_message: 'Manual budget toggle not found',
282-
alert_message: InitializationError.UNABLE_TO_SET_THINKING_BUDGET
260+
log_message: 'Manual budget toggle not found'
283261
})
284262
return
285263
}
@@ -294,8 +272,7 @@ export const ai_studio: Chatbot = {
294272
if (!thinking_toggle) {
295273
report_initialization_error({
296274
function_name: 'set_thinking_budget',
297-
log_message: 'Thinking toggle not found',
298-
alert_message: InitializationError.UNABLE_TO_SET_THINKING_BUDGET
275+
log_message: 'Thinking toggle not found'
299276
})
300277
return
301278
}
@@ -309,8 +286,7 @@ export const ai_studio: Chatbot = {
309286
if (!manual_budget_toggle) {
310287
report_initialization_error({
311288
function_name: 'set_thinking_budget',
312-
log_message: 'Manual budget toggle not found',
313-
alert_message: InitializationError.UNABLE_TO_SET_THINKING_BUDGET
289+
log_message: 'Manual budget toggle not found'
314290
})
315291
return
316292
}
@@ -324,8 +300,7 @@ export const ai_studio: Chatbot = {
324300
if (!budget_input) {
325301
report_initialization_error({
326302
function_name: 'set_thinking_budget',
327-
log_message: 'Budget input not found',
328-
alert_message: InitializationError.UNABLE_TO_SET_THINKING_BUDGET
303+
log_message: 'Budget input not found'
329304
})
330305
return
331306
}
@@ -359,8 +334,7 @@ export const ai_studio: Chatbot = {
359334
if (!top_p_element) {
360335
report_initialization_error({
361336
function_name: 'set_top_p',
362-
log_message: 'Top-p input not found',
363-
alert_message: InitializationError.UNABLE_TO_SET_TOP_P
337+
log_message: 'Top-p input not found'
364338
})
365339
return
366340
}
@@ -374,8 +348,7 @@ export const ai_studio: Chatbot = {
374348
if (!input_element) {
375349
report_initialization_error({
376350
function_name: 'enter_message_and_send',
377-
log_message: 'Message input textarea not found',
378-
alert_message: InitializationError.UNABLE_TO_SEND_MESSAGE
351+
log_message: 'Message input textarea not found'
379352
})
380353
return
381354
}
@@ -408,8 +381,7 @@ export const ai_studio: Chatbot = {
408381
if (!send_button) {
409382
report_initialization_error({
410383
function_name: 'enter_message_and_send',
411-
log_message: 'Send button not found',
412-
alert_message: InitializationError.UNABLE_TO_SEND_MESSAGE
384+
log_message: 'Send button not found'
413385
})
414386
return
415387
}
@@ -432,8 +404,7 @@ export const ai_studio: Chatbot = {
432404
if (!chat_turn_container) {
433405
report_initialization_error({
434406
function_name: 'ai_studio.perform_copy',
435-
log_message: 'Chat turn container not found',
436-
alert_message: InitializationError.UNABLE_TO_COPY_RESPONSE
407+
log_message: 'Chat turn container not found'
437408
})
438409
return
439410
}
@@ -443,8 +414,7 @@ export const ai_studio: Chatbot = {
443414
if (!options) {
444415
report_initialization_error({
445416
function_name: 'ai_studio.perform_copy',
446-
log_message: 'Options button not found',
447-
alert_message: InitializationError.UNABLE_TO_COPY_RESPONSE
417+
log_message: 'Options button not found'
448418
})
449419
return
450420
}
@@ -457,8 +427,7 @@ export const ai_studio: Chatbot = {
457427
if (!markdown_copy_button) {
458428
report_initialization_error({
459429
function_name: 'ai_studio.perform_copy',
460-
log_message: 'Markdown copy button not found',
461-
alert_message: InitializationError.UNABLE_TO_COPY_RESPONSE
430+
log_message: 'Markdown copy button not found'
462431
})
463432
return
464433
}
@@ -505,8 +474,7 @@ const open_panel = async () => {
505474
if (!tune_button) {
506475
report_initialization_error({
507476
function_name: 'open_panel',
508-
log_message: 'Tune button not found',
509-
alert_message: InitializationError.UNABLE_TO_OPEN_SETTINGS_PANEL
477+
log_message: 'Tune button not found'
510478
})
511479
return
512480
}
@@ -524,8 +492,7 @@ const close_panel = async () => {
524492
if (!close_button) {
525493
report_initialization_error({
526494
function_name: 'close_panel',
527-
log_message: 'Close button for settings panel not found',
528-
alert_message: InitializationError.UNABLE_TO_CLOSE_SETTINGS_PANEL
495+
log_message: 'Close button for settings panel not found'
529496
})
530497
return
531498
}
@@ -538,8 +505,7 @@ const close_panel = async () => {
538505
if (!close_button) {
539506
report_initialization_error({
540507
function_name: 'close_panel',
541-
log_message: 'Close button for settings panel not found',
542-
alert_message: InitializationError.UNABLE_TO_CLOSE_SETTINGS_PANEL
508+
log_message: 'Close button for settings panel not found'
543509
})
544510
return
545511
}

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

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@ import {
44
add_apply_response_button,
55
observe_for_responses
66
} from '../utils/add-apply-response-button'
7-
import {
8-
InitializationError,
9-
report_initialization_error
10-
} from '../utils/report-initialization-error'
7+
import { report_initialization_error } from '../utils/report-initialization-error'
118

129
export const chatgpt: Chatbot = {
1310
wait_until_ready: async () => {
@@ -105,15 +102,13 @@ export const chatgpt: Chatbot = {
105102
if (!found) {
106103
report_initialization_error({
107104
function_name: 'set_options',
108-
log_message: 'Think longer button not found',
109-
alert_message: InitializationError.UNABLE_TO_SET_OPTIONS
105+
log_message: 'Think longer button not found'
110106
})
111107
}
112108
} else {
113109
report_initialization_error({
114110
function_name: 'set_options',
115-
log_message: 'Plus button for "Think longer" not found',
116-
alert_message: InitializationError.UNABLE_TO_SET_OPTIONS
111+
log_message: 'Plus button for "Think longer" not found'
117112
})
118113
}
119114
}
@@ -138,8 +133,7 @@ export const chatgpt: Chatbot = {
138133
if (!copy_button) {
139134
report_initialization_error({
140135
function_name: 'chatgpt.perform_copy',
141-
log_message: 'Copy button not found',
142-
alert_message: InitializationError.UNABLE_TO_COPY_RESPONSE
136+
log_message: 'Copy button not found'
143137
})
144138
return
145139
}

0 commit comments

Comments
 (0)