Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/10999.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [BUG][Chat] Unregister tools when component unmount ([#10999](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/10999))
1 change: 1 addition & 0 deletions src/plugins/chat/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -188,5 +188,6 @@ export class ChatPlugin implements Plugin<ChatPluginSetup, ChatPluginStart> {
public stop() {
this.paddingSizeSubscription?.unsubscribe();
this.unsubscribeWindowStateChange?.();
this.chatService?.destroy();
}
}
22 changes: 21 additions & 1 deletion src/plugins/chat/public/services/chat_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Observable } from 'rxjs';
import { Observable, Subscription } from 'rxjs';
import { AgUiAgent } from './ag_ui_agent';
import { RunAgentInput, Message, UserMessage, ToolMessage } from '../../common/types';
import type { ToolDefinition } from '../../../context_provider/public';
import { AssistantActionService } from '../../../context_provider/public';
import { ChatLayoutMode } from '../components/chat_header_button';
import type { ChatWindowInstance } from '../components/chat_window';
import {
Expand Down Expand Up @@ -51,6 +52,9 @@ export class ChatService {
// ChatWindow ref for delegating sendMessage calls to proper timeline management
private chatWindowRef: React.RefObject<ChatWindowInstance> | null = null;

// Subscription to assistant action service for tool updates
private toolSubscription?: Subscription;

constructor(
uiSettings: IUiSettingsClient,
coreChatService?: ChatServiceStart,
Expand All @@ -69,6 +73,12 @@ export class ChatService {
this.coreChatService.setThreadId(currentChatState.threadId);
}
this.currentMessages = currentChatState?.messages || [];

// Subscribe to assistant action service to keep tools in sync
const assistantActionService = AssistantActionService.getInstance();
this.toolSubscription = assistantActionService.getState$().subscribe((state) => {
this.availableTools = state.toolDefinitions;
});
}

public getThreadId = () => {
Expand Down Expand Up @@ -514,4 +524,14 @@ export class ChatService {
// Clear dynamic context from global store for fresh chat session
this.clearDynamicContextFromStore();
}

/**
* Cleanup method to properly dispose of subscriptions
*/
public destroy(): void {
if (this.toolSubscription) {
this.toolSubscription.unsubscribe();
this.toolSubscription = undefined;
}
}
}
Loading