Skip to content

Commit 98e2179

Browse files
authored
Fix duplicate message, settings and agent deselect bug (#9)
Fix duplicate message, settings and agent deselect bug
1 parent d018a6a commit 98e2179

File tree

5 files changed

+36
-28
lines changed

5 files changed

+36
-28
lines changed

config/gni/devtools_grd_files.gni

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,8 @@ grd_files_release_sources = [
601601
"front_end/panels/ai_chat/ui/AIChatPanel.js",
602602
"front_end/panels/ai_chat/ui/ChatView.js",
603603
"front_end/panels/ai_chat/ui/chatView.css.js",
604+
"front_end/panels/ai_chat/ui/HelpDialog.js",
605+
"front_end/panels/ai_chat/ui/SettingsDialog.js",
604606
"front_end/panels/ai_chat/core/AgentService.js",
605607
"front_end/panels/ai_chat/core/State.js",
606608
"front_end/panels/ai_chat/core/Graph.js",

front_end/panels/ai_chat/core/BaseOrchestratorAgent.ts

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -314,30 +314,36 @@ export function renderAgentTypeButtons(
314314
export function createAgentTypeSelectionHandler(
315315
element: HTMLElement,
316316
textInputElement: HTMLTextAreaElement | undefined,
317-
onAgentTypeSelected: ((agentType: string) => void) | undefined,
318-
setSelectedAgentType: (type: string) => void
317+
onAgentTypeSelected: ((agentType: string | null) => void) | undefined,
318+
setSelectedAgentType: (type: string | null) => void,
319+
getCurrentSelectedType: () => string | null
319320
): (event: Event) => void {
320321
return (event: Event): void => {
321322
const button = event.currentTarget as HTMLButtonElement;
322323
const agentType = button.dataset.agentType;
323324
if (agentType && onAgentTypeSelected) {
325+
const currentSelected = getCurrentSelectedType();
326+
324327
// Remove selected class from all agent type buttons
325-
const allButtons = element.shadowRoot?.querySelectorAll('.agent-type-button');
328+
const allButtons = element.shadowRoot?.querySelectorAll('.prompt-button');
326329
allButtons?.forEach(btn => btn.classList.remove('selected'));
327330

328-
// Add selected class to the clicked button
329-
button.classList.add('selected');
330-
331-
// Update the selected agent type
332-
setSelectedAgentType(agentType);
333-
334-
// Call the handler passed via props
335-
onAgentTypeSelected(agentType);
336-
337-
// Focus the input after selecting an agent type
331+
// Check if we're clicking on the currently selected button (toggle off)
332+
if (currentSelected === agentType) {
333+
// Deselect - set to null and don't add selected class
334+
setSelectedAgentType(null);
335+
onAgentTypeSelected(null);
336+
console.log('Deselected agent type, returning to default');
337+
} else {
338+
// Select new agent type - add selected class to clicked button
339+
button.classList.add('selected');
340+
setSelectedAgentType(agentType);
341+
onAgentTypeSelected(agentType);
342+
console.log('Selected agent type:', agentType);
343+
}
344+
345+
// Focus the input after selecting/deselecting an agent type
338346
textInputElement?.focus();
339-
340-
console.log('Selected agent type:', agentType);
341347
}
342348
};
343349
}

front_end/panels/ai_chat/ui/AIChatPanel.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -871,11 +871,9 @@ export class AIChatPanel extends UI.Panel.Panel {
871871
return;
872872
}
873873

874-
// Add user message first regardless of whether we can process it
875-
this.#addUserMessage(text, imageInput);
876-
877874
// If we can't send messages due to missing credentials, add error message and return
878875
if (!this.#canSendMessages) {
876+
this.#addUserMessage(text, imageInput);
879877
this.#addCredentialErrorMessage();
880878
return;
881879
}
@@ -1021,13 +1019,17 @@ export class AIChatPanel extends UI.Panel.Panel {
10211019

10221020
/**
10231021
* Handles prompt type selection from ChatView
1024-
* @param promptType The selected prompt type
1022+
* @param promptType The selected prompt type (null means deselected)
10251023
*/
1026-
#handlePromptSelected(promptType: string): void {
1024+
#handlePromptSelected(promptType: string | null): void {
10271025
console.log('Prompt selected in AIChatPanel:', promptType);
10281026
this.#selectedAgentType = promptType;
10291027
// Save selection for future sessions
1030-
localStorage.setItem('ai_chat_agent_type', promptType);
1028+
if (promptType) {
1029+
localStorage.setItem('ai_chat_agent_type', promptType);
1030+
} else {
1031+
localStorage.removeItem('ai_chat_agent_type');
1032+
}
10311033
}
10321034

10331035
#onNewChatClick(): void {

front_end/panels/ai_chat/ui/ChatView.ts

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ export enum State {
130130
export interface Props {
131131
messages: ChatMessage[];
132132
onSendMessage: (text: string, imageInput?: ImageInputData) => void;
133-
onPromptSelected: (promptType: string) => void;
133+
onPromptSelected: (promptType: string | null) => void;
134134
state: State;
135135
isTextInputEmpty: boolean;
136136
imageInput?: ImageInputData;
@@ -160,7 +160,7 @@ export class ChatView extends HTMLElement {
160160
#imageInput?: ImageInputData;
161161
#onSendMessage?: (text: string, imageInput?: ImageInputData) => void;
162162
#onImageInputClear?: () => void;
163-
#onPromptSelected?: (promptType: string) => void;
163+
#onPromptSelected?: (promptType: string | null) => void;
164164
#textInputElement?: HTMLTextAreaElement;
165165
#markdownRenderer = new MarkdownRenderer();
166166
#isFirstMessageView = true; // Track if we're in the centered first-message view
@@ -264,10 +264,11 @@ export class ChatView extends HTMLElement {
264264
this,
265265
this.#textInputElement,
266266
this.#onPromptSelected,
267-
(type: string) => {
267+
(type: string | null) => {
268268
this.#selectedPromptType = type;
269269
void ComponentHelpers.ScheduledRender.scheduleRender(this, this.#boundRender);
270-
}
270+
},
271+
() => this.#selectedPromptType || null
271272
);
272273
}
273274

front_end/panels/ai_chat/ui/SettingsDialog.ts

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,6 @@ export class SettingsDialog {
288288

289289
console.log(`Provider changed to: ${selectedProvider}`);
290290

291-
// Store the change immediately in localStorage to ensure getModelOptions sees the new provider
292-
localStorage.setItem(PROVIDER_SELECTION_KEY, selectedProvider);
293-
294291
// If switching to LiteLLM, fetch the latest models if endpoint is configured
295292
if (selectedProvider === 'litellm') {
296293
const endpoint = litellmEndpointInput.value.trim();

0 commit comments

Comments
 (0)