Skip to content

Commit f8fa8a5

Browse files
committed
Improve chatbot integration by adding OpenRouter reasoning effort support and refining input handling logic
- wait an additional second - once options button is clicked, a dialog appears and 'advanced settings' has to be clicked. Do it as part of [Fragment] [Pasted text] - reasoning efforts can't be seen although we visually see their dialog appears. It's html:[Pasted text] - add missing reasoning efforts [Pasted text] - disabling reasoning doesn't work, options html: [Pasted text] - setting system prompt doesn't work. Html of the dialog: [Pasted text]
1 parent 667e39e commit f8fa8a5

5 files changed

Lines changed: 98 additions & 41 deletions

File tree

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

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@ export const gemini: Chatbot = {
1010
wait_until_ready: async () => {
1111
await new Promise((resolve) => {
1212
const check_for_element = () => {
13-
if (document.querySelector('button[data-test-id="bard-mode-menu-button"]')) {
13+
if (
14+
document.querySelector('button[data-test-id="bard-mode-menu-button"]')
15+
) {
1416
resolve(null)
1517
} else {
1618
setTimeout(check_for_element, 100)
1719
}
1820
}
1921
check_for_element()
2022
})
23+
await new Promise((resolve) => setTimeout(resolve, 500))
2124
},
2225
set_model: async (chat) => {
2326
const model = chat.model
@@ -38,7 +41,8 @@ export const gemini: Chatbot = {
3841
}
3942

4043
const model_label = CHATBOTS['Gemini'].models[model].label
41-
const trigger_text = model_selector_trigger.textContent?.trim().toLowerCase() || ''
44+
const trigger_text =
45+
model_selector_trigger.textContent?.trim().toLowerCase() || ''
4246
const target_label_lower = model_label.toLowerCase()
4347
if (
4448
trigger_text.includes(target_label_lower) &&
@@ -49,16 +53,17 @@ export const gemini: Chatbot = {
4953

5054
model_selector_trigger.click()
5155
await new Promise((r) => requestAnimationFrame(r))
52-
const menu_content =
53-
document.querySelector('gem-menu')
56+
const menu_content = document.querySelector('gem-menu')
5457
if (!menu_content) {
5558
report_initialization_error({
5659
function_name: 'set_model',
5760
log_message: 'Model selector menu not found'
5861
})
5962
return
6063
}
61-
const model_options = Array.from(menu_content.querySelectorAll('[role="menuitem"]'))
64+
const model_options = Array.from(
65+
menu_content.querySelectorAll('[role="menuitem"]')
66+
)
6267
let found = false
6368
for (const option of model_options) {
6469
const label_element = option.querySelector('.label')
@@ -107,10 +112,14 @@ export const gemini: Chatbot = {
107112

108113
const thinking_level_item = Array.from(
109114
menu_content.querySelectorAll('gem-menu-item')
110-
).find((item) => item.getAttribute('value') == 'thinking_level') as HTMLElement
115+
).find(
116+
(item) => item.getAttribute('value') == 'thinking_level'
117+
) as HTMLElement
111118

112119
if (!thinking_level_item) {
113-
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
120+
document.dispatchEvent(
121+
new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })
122+
)
114123
return
115124
}
116125

@@ -119,14 +128,18 @@ export const gemini: Chatbot = {
119128
?.textContent?.trim()
120129

121130
if (current_level?.toLowerCase() == reasoning_effort.toLowerCase()) {
122-
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
131+
document.dispatchEvent(
132+
new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })
133+
)
123134
return
124135
}
125136

126137
thinking_level_item.click()
127138
await new Promise((resolve) => requestAnimationFrame(resolve))
128139

129-
const all_menu_items = Array.from(document.querySelectorAll('gem-menu-item'))
140+
const all_menu_items = Array.from(
141+
document.querySelectorAll('gem-menu-item')
142+
)
130143
let found = false
131144
for (const option of all_menu_items) {
132145
const label_element = option.querySelector('.label')
@@ -139,7 +152,9 @@ export const gemini: Chatbot = {
139152
}
140153

141154
if (!found) {
142-
document.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
155+
document.dispatchEvent(
156+
new KeyboardEvent('keydown', { key: 'Escape', bubbles: true })
157+
)
143158
}
144159

145160
await new Promise((resolve) => requestAnimationFrame(resolve))

‎apps/browser/src/content-scripts/send-prompt-content-script/chatbots/openrouter.ts‎

Lines changed: 70 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,23 @@ const show_options_modal = async (function_name: string) => {
2525
}
2626
options_button.click()
2727
await new Promise((r) => requestAnimationFrame(r))
28+
29+
const advanced_settings_button = Array.from(
30+
document.querySelectorAll('div[role="dialog"] button')
31+
).find(
32+
(button) => button.textContent?.trim() == 'Advanced Settings'
33+
) as HTMLButtonElement
34+
35+
if (!advanced_settings_button) {
36+
report_initialization_error({
37+
function_name,
38+
log_message: 'Advanced settings button not found'
39+
})
40+
return false
41+
}
42+
43+
advanced_settings_button.click()
44+
await new Promise((r) => requestAnimationFrame(r))
2845
return true
2946
}
3047

@@ -74,10 +91,11 @@ export const openrouter: Chatbot = {
7491
await close_options_modal('enter_system_instructions')
7592
return
7693
}
77-
const custom_system_instructions_button =
78-
textarea.parentElement?.parentElement?.querySelector(
79-
'div > button:last-child'
80-
)
94+
const custom_system_instructions_button = Array.from(
95+
document.querySelectorAll('div[role="dialog"] button')
96+
).find(
97+
(button) => button.textContent?.trim() == 'Custom'
98+
) as HTMLButtonElement
8199
if (!custom_system_instructions_button) {
82100
report_initialization_error({
83101
function_name: 'enter_system_instructions',
@@ -86,11 +104,29 @@ export const openrouter: Chatbot = {
86104
await close_options_modal('enter_system_instructions')
87105
return
88106
}
89-
;(custom_system_instructions_button as HTMLElement)?.click()
107+
custom_system_instructions_button.click()
90108
await new Promise((r) => requestAnimationFrame(r))
91-
textarea.focus()
92-
textarea.value = system_instructions
93-
textarea.blur()
109+
110+
const active_textarea = document.querySelector(
111+
'div[role="dialog"] textarea'
112+
) as HTMLTextAreaElement
113+
if (!active_textarea) {
114+
report_initialization_error({
115+
function_name: 'enter_system_instructions',
116+
log_message: 'System instructions textarea disappeared'
117+
})
118+
await close_options_modal('enter_system_instructions')
119+
return
120+
}
121+
122+
active_textarea.focus()
123+
const nativeTextAreaValueSetter = Object.getOwnPropertyDescriptor(
124+
window.HTMLTextAreaElement.prototype,
125+
'value'
126+
)?.set
127+
nativeTextAreaValueSetter?.call(active_textarea, system_instructions)
128+
active_textarea.dispatchEvent(new Event('input', { bubbles: true }))
129+
active_textarea.blur()
94130
await close_options_modal('enter_system_instructions')
95131
},
96132
set_options: async (chat) => {
@@ -104,7 +140,7 @@ export const openrouter: Chatbot = {
104140
// Only some models support this option
105141
if (reasoning_toggle) {
106142
if (options.includes('disable-reasoning')) {
107-
if (reasoning_toggle.getAttribute('data-state') == 'checked') {
143+
if (reasoning_toggle.hasAttribute('data-checked')) {
108144
reasoning_toggle.click()
109145
}
110146
}
@@ -156,7 +192,12 @@ export const openrouter: Chatbot = {
156192
return
157193
}
158194
temperature_input.focus()
159-
temperature_input.value = temperature.toString()
195+
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
196+
window.HTMLInputElement.prototype,
197+
'value'
198+
)?.set
199+
nativeInputValueSetter?.call(temperature_input, temperature.toString())
200+
temperature_input.dispatchEvent(new Event('input', { bubbles: true }))
160201
temperature_input.blur()
161202
await close_options_modal('set_temperature')
162203
},
@@ -202,7 +243,12 @@ export const openrouter: Chatbot = {
202243
return
203244
}
204245
top_p_input.focus()
205-
top_p_input.value = top_p.toString()
246+
const nativeInputValueSetter = Object.getOwnPropertyDescriptor(
247+
window.HTMLInputElement.prototype,
248+
'value'
249+
)?.set
250+
nativeInputValueSetter?.call(top_p_input, top_p.toString())
251+
top_p_input.dispatchEvent(new Event('input', { bubbles: true }))
206252
top_p_input.blur()
207253
await close_options_modal('set_top_p')
208254
},
@@ -221,7 +267,7 @@ export const openrouter: Chatbot = {
221267

222268
const reasoning_button = Array.from(dialog.querySelectorAll('button')).find(
223269
(button) =>
224-
['Minimal', 'Low', 'Medium', 'High'].includes(
270+
['None', 'Minimal', 'Low', 'Medium', 'High', 'XHigh', 'Max'].includes(
225271
button.textContent?.trim() || ''
226272
)
227273
) as HTMLButtonElement
@@ -231,25 +277,29 @@ export const openrouter: Chatbot = {
231277
return
232278
}
233279

234-
if (reasoning_button.textContent != reasoning_effort) {
280+
if (
281+
reasoning_button.textContent?.trim().toLowerCase() !==
282+
reasoning_effort.toLowerCase()
283+
) {
235284
reasoning_button.click()
236285
await new Promise((resolve) => setTimeout(resolve, 500)) // Opening animation must finish
237-
const dropdown = document.querySelector(
238-
'div[data-radix-popper-content-wrapper]'
239-
)
240-
if (!dropdown) {
286+
287+
const options = document.querySelectorAll('div[role="option"]')
288+
if (options.length === 0) {
241289
report_initialization_error({
242290
function_name: 'set_reasoning_effort',
243-
log_message: 'Reasoning effort dropdown not found'
291+
log_message: 'Reasoning effort options not found'
244292
})
245293
await close_options_modal('set_reasoning_effort')
246294
return
247295
}
248296

249-
const options = dropdown.querySelectorAll('div[role="option"]')
250297
let found = false
251298
for (const option of Array.from(options)) {
252-
if (option.textContent.toLowerCase() == reasoning_effort) {
299+
if (
300+
option.textContent?.trim().toLowerCase() ===
301+
reasoning_effort.toLowerCase()
302+
) {
253303
;(option as HTMLElement).click()
254304
found = true
255305
break

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,8 @@ export const CHATBOTS = {
180180
'Low',
181181
'Medium',
182182
'High',
183-
'XHigh'
183+
'XHigh',
184+
'Max'
184185
],
185186
default_system_instructions: "You're a helpful coding assistant.",
186187
supported_options: {

‎packages/ui/src/components/editor/panel/Configurations/Configurations.stories.tsx‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,6 @@ export const Default = () => {
7676
on_delete={(id) => {
7777
console.log('on_delete', id)
7878
}}
79-
on_duplicate={(id) => {
80-
console.log('on_duplicate', id)
81-
}}
8279
on_reorder={(configs) => {
8380
console.log('on_reorder', configs)
8481
}}
@@ -110,9 +107,6 @@ export const Presets = () => {
110107
on_delete={(id) => {
111108
console.log('on_delete', id)
112109
}}
113-
on_duplicate={(id) => {
114-
console.log('on_duplicate', id)
115-
}}
116110
on_reorder={(configs) => {
117111
console.log('on_reorder', configs)
118112
}}
@@ -147,9 +141,6 @@ export const Empty = () => (
147141
on_delete={(id) => {
148142
console.log('on_delete', id)
149143
}}
150-
on_duplicate={(id) => {
151-
console.log('on_duplicate', id)
152-
}}
153144
on_reorder={(configs) => {
154145
console.log('on_reorder', configs)
155146
}}

‎packages/ui/src/components/editor/panel/prompts/PromptField/PromptField.stories.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ export const WithWarning = () => (
646646
on_submit={() => console.log('Submitted')}
647647
on_copy={() => console.log('Copied')}
648648
is_connected={true}
649-
prompt_type="ask-about-context"
649+
prompt_type="ask-about-files"
650650
current_selection={null}
651651
currently_open_file_path="/path/to/file"
652652
on_caret_position_change={(pos) => console.log('Caret position:', pos)}

0 commit comments

Comments
 (0)