Skip to content

Commit 0505902

Browse files
committed
Remove invocation count feature and add browser focus handling for chat processing
- remove 'invocation count' feature - we no longer send 'invocation count', nevertheless we continue to allow sending many messages in very short succession - should not progress with the queue unless the browser window has focus - when browser restores focus, before you open next item in the queue, wait 2 seconds - the timeout we run when window is back in focus should not apply if the queue is full (it's a browser's first focus ever for the current queue) ├── apps │ ├── browser │ │ └── src │ │ ├── background │ │ │ ├── clear-chat-init-data.ts │ │ │ ├── config.ts │ │ │ ├── keep-alive.ts │ │ │ ├── main.ts │ │ │ ├── message-handler.ts │ │ │ └── websocket.ts │ │ ├── content-scripts │ │ │ └── send-prompt-content-script │ │ │ ├── send-prompt-content-script.ts │ │ │ └── utils │ │ │ ├── add-apply-response-button.ts │ │ │ ├── apply-response-styles.ts │ │ │ ├── logger.ts │ │ │ ├── report-initialization-error.ts │ │ │ └── show-response-ready-notification.ts │ │ └── manifest.json │ └── editor │ └── src │ ├── commands │ │ └── generate-commit-message-commands │ │ └── actions │ │ └── run-generate-action.ts │ ├── features │ │ └── search-files │ │ └── search-modes │ │ └── perform-intelligent-search-mode.ts │ ├── services │ │ └── websocket-manager.ts │ └── views │ └── prompt │ ├── backend │ │ ├── message-handlers │ │ │ ├── handle-autofill.ts │ │ │ └── handle-make-api-call.ts │ │ └── prompt-view-provider.ts │ ├── frontend │ │ ├── Main │ │ │ ├── Main.tsx │ │ │ └── MainView │ │ │ ├── MainView.tsx │ │ │ └── hooks │ │ │ └── use-invocation-counts.ts │ │ └── i18n │ │ └── translations │ │ └── prompt-field.ts │ └── types │ └── messages.ts └── packages ├── shared │ └── src │ └── types │ └── websocket-message.ts └── ui └── src └── components └── editor └── common └── prompts ├── PromptField │ ├── PromptField.module.scss │ ├── PromptField.stories.tsx │ ├── PromptField.tsx │ ├── components │ │ ├── Tooltip.module.scss │ │ ├── Tooltip.tsx │ │ └── index.ts │ ├── hooks │ │ ├── use-drag-drop.ts │ │ ├── use-dropdown.ts │ │ ├── use-ghost-text.ts │ │ ├── use-handlers │ │ │ ├── handle-copy.ts │ │ │ ├── handle-cut.ts │ │ │ ├── handle-input-change.ts │ │ │ ├── handle-input-click.ts │ │ │ ├── handle-key-down.ts │ │ │ ├── handle-paste.ts │ │ │ ├── handle-submit.ts │ │ │ ├── index.ts │ │ │ ├── types.ts │ │ │ ├── use-handlers.ts │ │ │ └── utils.ts │ │ └── use-keyboard-shortcuts.ts │ └── index.ts ├── Template │ ├── Template.module.scss │ ├── Template.tsx │ └── index.ts └── shared └── symbols ├── caret.ts ├── get-highlighted-text.tsx ├── index.ts ├── position-mapping.ts ├── prompt-symbols.module.scss ├── reconstruct-raw-value.ts └── use-symbol-deletion.ts
1 parent 95f1c0f commit 0505902

16 files changed

Lines changed: 104 additions & 345 deletions

File tree

‎apps/browser/src/background/message-handler.ts‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ interface ChatQueueItem {
1414

1515
const chat_queue: ChatQueueItem[] = []
1616
let is_processing = false
17+
let is_waiting_for_focus = false
18+
let is_first_focus_for_queue = true
1719
let last_opened_tab_id: number | undefined
1820
let is_finished_responding = false
1921

@@ -49,12 +51,25 @@ const generate_alphanumeric_id = async (
4951
throw new Error('Unable to generate a unique ID after maximum attempts')
5052
}
5153

54+
const check_browser_focus = async (): Promise<boolean> => {
55+
const windows = await browser.windows.getAll()
56+
return windows.some((win) => win.focused)
57+
}
58+
5259
const process_next_chat = async () => {
5360
if (chat_queue.length == 0 || !is_processing) {
5461
is_processing = false
5562
return
5663
}
5764

65+
const has_focus = await check_browser_focus()
66+
if (!has_focus) {
67+
is_waiting_for_focus = true
68+
return
69+
}
70+
71+
is_first_focus_for_queue = false
72+
5873
const current_queue_item = chat_queue[0]
5974

6075
if (current_queue_item.timeout_id) {
@@ -145,17 +160,15 @@ const process_next_chat = async () => {
145160
const start_processing = async () => {
146161
if (!is_processing && chat_queue.length > 0) {
147162
is_processing = true
163+
is_first_focus_for_queue = true
148164
await process_next_chat()
149165
}
150166
}
151167

152168
const handle_initialize_chat_message = async (
153169
message: InitializeChatMessage
154170
) => {
155-
const count = message.invocation_count || 1
156-
for (let i = 0; i < count; i++) {
157-
chat_queue.push({ message })
158-
}
171+
chat_queue.push({ message })
159172
await start_processing()
160173
}
161174

@@ -170,6 +183,22 @@ const handle_chat_initialized = async () => {
170183
}
171184

172185
export const setup_message_listeners = () => {
186+
browser.windows.onFocusChanged.addListener((window_id) => {
187+
if (window_id !== browser.windows.WINDOW_ID_NONE) {
188+
if (is_waiting_for_focus) {
189+
is_waiting_for_focus = false
190+
if (is_first_focus_for_queue) {
191+
is_first_focus_for_queue = false
192+
process_next_chat()
193+
} else {
194+
setTimeout(() => {
195+
process_next_chat()
196+
}, 2000)
197+
}
198+
}
199+
}
200+
})
201+
173202
browser.runtime.onMessage.addListener(
174203
(message: any, sender: any, _: any): any => {
175204
if (is_message(message)) {

‎apps/editor/src/commands/generate-commit-message-commands/actions/run-generate-action.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,6 @@ export const run_generate_action = async (params: {
357357
const sent = await params.websocket_manager.initialize_chat({
358358
text: chatbot_prompt,
359359
web_configuration_name: selected_web_configuration_name,
360-
invocation_count: 1,
361360
inject_apply_response_button: true
362361
})
363362
if (sent) {

‎apps/editor/src/features/search-files/search-modes/perform-intelligent-search-mode.ts‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,6 @@ export const perform_intelligent_search_mode = async (params: {
374374
const sent = await params.websocket_manager.initialize_chat({
375375
text: chatbot_prompt,
376376
web_configuration_name: selected_web_configuration_name,
377-
invocation_count: 1,
378377
inject_apply_response_button: true
379378
})
380379
if (sent) {

‎apps/editor/src/services/websocket-manager.ts‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,6 @@ export class WebSocketManager {
365365
text: string
366366
web_configuration_name: string
367367
raw_instructions?: string
368-
invocation_count: number
369368
inject_apply_response_button?: boolean
370369
}): Promise<boolean> {
371370
if (!this.has_connected_browsers) {
@@ -377,10 +376,7 @@ export class WebSocketManager {
377376
config.get<ConfigWebConfigurationFormat[]>('webConfigurations') ?? []
378377
const gemini_user_id = config.get<number | null>('geminiUserId')
379378
const ai_studio_user_id = config.get<number | null>('aiStudioUserId')
380-
const reuse_last_tab =
381-
params.invocation_count > 1
382-
? false
383-
: config.get<boolean>('reuseLastTab', false)
379+
const reuse_last_tab = config.get<boolean>('reuseLastTab', false)
384380

385381
const target_browser_id = await this._select_browser()
386382
if (target_browser_id === undefined) {
@@ -456,7 +452,6 @@ export class WebSocketManager {
456452
client_id: this.client_id || 0, // 0 is a temporary fallback and should be removed few weeks from 28.03.25
457453
raw_instructions: params.raw_instructions,
458454
reuse_last_tab,
459-
invocation_count: params.invocation_count,
460455
inject_apply_response_button: params.inject_apply_response_button
461456
}
462457

‎apps/editor/src/views/prompt/backend/message-handlers/handle-autofill.ts‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import { PromptBuilder } from '@/utils/prompt-builder'
2020

2121
export const handle_autofill = async (params: {
2222
prompt_view_provider: PromptViewProvider
23-
invocation_count: number
2423
web_configuration_name?: string
2524
show_quick_pick?: boolean
2625
}): Promise<void> => {
@@ -106,7 +105,6 @@ export const handle_autofill = async (params: {
106105
text,
107106
web_configuration_name: resolved_web_configuration_name,
108107
raw_instructions: current_instructions,
109-
invocation_count: params.invocation_count,
110108
inject_apply_response_button
111109
}
112110
)

‎apps/editor/src/views/prompt/backend/message-handlers/handle-make-api-call.ts‎

Lines changed: 55 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -254,84 +254,69 @@ export const handle_make_api_call = async (
254254
]
255255

256256
let error_occurred = false
257-
let was_cancelled = false
258257

259-
const promises = Array.from({ length: message.invocation_count }).map(
260-
async () => {
261-
const body: { [key: string]: any } = {
262-
messages,
263-
model: api_configuration.model
264-
}
258+
const body: { [key: string]: any } = {
259+
messages,
260+
model: api_configuration.model
261+
}
265262

266-
apply_reasoning_effort({
267-
body,
268-
model_provider,
269-
reasoning_effort: api_configuration.reasoning_effort
270-
})
263+
apply_reasoning_effort({
264+
body,
265+
model_provider,
266+
reasoning_effort: api_configuration.reasoning_effort
267+
})
271268

272-
try {
273-
let result: { response: string; thoughts?: string } | null = null
274-
275-
result =
276-
await prompt_view_provider.prompt_view_api_calls_manager.send_llm_message(
277-
{
278-
base_url: model_provider.base_url,
279-
api_key: model_provider.api_key,
280-
body,
281-
provider_name: api_configuration.model_provider_name,
282-
model: api_configuration.model,
283-
reasoning_effort: api_configuration.reasoning_effort,
284-
raw_instructions: instructions
285-
}
286-
)
287-
288-
if (result) {
289-
const recent_api_configuration = {
290-
model_provider: api_configuration.model_provider_name,
291-
model: api_configuration.model,
292-
reasoning_effort: api_configuration.reasoning_effort
293-
}
294-
295-
if (prompt_type == 'edit-files') {
296-
vscode.commands.executeCommand('codeWebChat.applyResponse', {
297-
response: result.response,
298-
raw_instructions: instructions,
299-
edit_format,
300-
recent_api_configuration
301-
})
302-
}
303-
return true
269+
try {
270+
let result: { response: string; thoughts?: string } | null = null
271+
272+
result =
273+
await prompt_view_provider.prompt_view_api_calls_manager.send_llm_message(
274+
{
275+
base_url: model_provider.base_url,
276+
api_key: model_provider.api_key,
277+
body,
278+
provider_name: api_configuration.model_provider_name,
279+
model: api_configuration.model,
280+
reasoning_effort: api_configuration.reasoning_effort,
281+
raw_instructions: instructions
304282
}
305-
} catch (error) {
306-
if (axios.isCancel(error)) {
307-
was_cancelled = true
308-
return false
309-
}
310-
Logger.error({
311-
function_name: 'handle_make_api_call',
312-
message: `${prompt_type} task error`,
313-
data: error
283+
)
284+
285+
if (result) {
286+
const recent_api_configuration = {
287+
model_provider: api_configuration.model_provider_name,
288+
model: api_configuration.model,
289+
reasoning_effort: api_configuration.reasoning_effort
290+
}
291+
292+
if (prompt_type == 'edit-files') {
293+
vscode.commands.executeCommand('codeWebChat.applyResponse', {
294+
response: result.response,
295+
raw_instructions: instructions,
296+
edit_format,
297+
recent_api_configuration
314298
})
315-
if (!error_occurred) {
316-
const err_msg = dictionary.error_message.EDIT_FILES_ERROR
317-
vscode.window.showErrorMessage(err_msg)
318-
error_occurred = true
319-
}
320-
return false
321299
}
322-
return false
300+
return
301+
} else {
302+
should_show_quick_pick = true
303+
current_api_configuration_id = undefined
304+
}
305+
} catch (error) {
306+
if (axios.isCancel(error)) {
307+
return
308+
}
309+
Logger.error({
310+
function_name: 'handle_make_api_call',
311+
message: `${prompt_type} task error`,
312+
data: error
313+
})
314+
if (!error_occurred) {
315+
const err_msg = dictionary.error_message.EDIT_FILES_ERROR
316+
vscode.window.showErrorMessage(err_msg)
317+
error_occurred = true
323318
}
324-
)
325-
326-
const results = await Promise.all(promises)
327-
328-
if (error_occurred || was_cancelled) return
329-
330-
if (results.some((r) => r)) {
331319
return
332-
} else {
333-
should_show_quick_pick = true
334-
current_api_configuration_id = undefined
335320
}
336321
}
337322
}

‎apps/editor/src/views/prompt/backend/prompt-view-provider.ts‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,8 +620,7 @@ export class PromptViewProvider implements vscode.WebviewViewProvider {
620620
await handle_autofill({
621621
prompt_view_provider: this,
622622
web_configuration_name: message.web_configuration_name,
623-
show_quick_pick: message.show_quick_pick,
624-
invocation_count: message.invocation_count
623+
show_quick_pick: message.show_quick_pick
625624
})
626625
} else if (message.command == 'PREVIEW_WEB_CONFIGURATION') {
627626
await handle_preview_web_configuration(this, message)

‎apps/editor/src/views/prompt/frontend/Main/Main.tsx‎

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,11 @@ export const Main: React.FC<Props> = (props) => {
214214
const handle_initialize_chats = (params: {
215215
web_configuration_name?: string
216216
show_quick_pick?: boolean
217-
invocation_count: number
218217
}) => {
219218
post_message(props.vscode, {
220219
command: 'AUTOFILL',
221220
web_configuration_name: params.web_configuration_name,
222-
show_quick_pick: params.show_quick_pick,
223-
invocation_count: params.invocation_count
221+
show_quick_pick: params.show_quick_pick
224222
})
225223

226224
update_chat_history(instructions)
@@ -369,16 +367,14 @@ export const Main: React.FC<Props> = (props) => {
369367
}
370368

371369
const handle_make_api_call = (
372-
use_quick_pick: boolean,
373-
invocation_count: number
370+
use_quick_pick: boolean
374371
) => {
375372
const instruction = get_current_instructions()
376373

377374
post_message(props.vscode, {
378375
command: 'MAKE_API_CALL',
379376
prompt_type: current_prompt_type as ApiPromptType,
380-
use_quick_pick,
381-
invocation_count
377+
use_quick_pick
382378
})
383379

384380
if (instruction.trim()) {
@@ -411,8 +407,7 @@ export const Main: React.FC<Props> = (props) => {
411407
command: 'MAKE_API_CALL',
412408
prompt_type: props.api_prompt_type,
413409
use_quick_pick: false,
414-
api_configuration_id: id,
415-
invocation_count: 1
410+
api_configuration_id: id
416411
})
417412

418413
update_chat_history(instruction)

0 commit comments

Comments
 (0)