Skip to content

Commit 35a16b0

Browse files
committed
ai-plugin: React to cluster change
Signed-off-by: ashu8912 <aghildiyal@microsoft.com>
1 parent 30c1dcf commit 35a16b0

File tree

3 files changed

+83
-8
lines changed

3 files changed

+83
-8
lines changed

ai-assistant/src/components/settings/MCPConfigEditorDialog.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -126,18 +126,12 @@ export default function MCPConfigEditorDialog({
126126
const exampleConfig: MCPConfig = {
127127
enabled: true,
128128
servers: [
129-
{
130-
name: 'inspektor-gadget',
131-
command: 'docker',
132-
args: ['mcp', 'gateway', 'run'],
133-
enabled: true,
134-
},
135129
{
136130
name: 'flux-mcp',
137131
command: 'flux-operator-mcp',
138-
args: ['serve'],
132+
args: ['serve', '--kube-context', 'HEADLAMP_CURRENT_CLUSTER'],
139133
env: {
140-
KUBECONFIG: '/Users/ashughildiyal/.kube/config',
134+
KUBECONFIG: 'PATH_TO_KUBECONFIG',
141135
},
142136
enabled: true,
143137
},
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { getCluster } from '@kinvolk/headlamp-plugin/lib/Utils';
2+
import React from 'react';
3+
4+
// Check if we're running in Electron
5+
const isElectron = !!(window as any)?.desktopApi;
6+
7+
/**
8+
* Hook that monitors cluster changes and notifies the Electron main process
9+
* This enables MCP servers to restart when cluster context changes
10+
*/
11+
export function useClusterChangeNotifier() {
12+
const [currentCluster, setCurrentCluster] = React.useState<string | null>(null);
13+
const previousClusterRef = React.useRef<string | null>(null);
14+
15+
React.useEffect(() => {
16+
// Function to check and update cluster
17+
const checkClusterChange = () => {
18+
const cluster = getCluster() || null;
19+
20+
// Update state if cluster changed
21+
if (cluster !== currentCluster) {
22+
setCurrentCluster(cluster);
23+
}
24+
};
25+
26+
// Check initially
27+
checkClusterChange();
28+
29+
// Set up interval to check for cluster changes
30+
const interval = setInterval(checkClusterChange, 1000); // Check every second
31+
32+
return () => clearInterval(interval);
33+
}, [currentCluster]);
34+
35+
React.useEffect(() => {
36+
// Only notify if running in Electron
37+
if (!isElectron || !(window as any)?.desktopApi?.notifyClusterChange) {
38+
return;
39+
}
40+
41+
const previousCluster = previousClusterRef.current;
42+
43+
// Only notify if cluster actually changed and it's not the initial load
44+
if (currentCluster !== previousCluster && previousClusterRef.current !== undefined) {
45+
console.log('Cluster change detected, notifying electron:', {
46+
from: previousCluster,
47+
to: currentCluster,
48+
});
49+
50+
// Notify the electron main process
51+
(window as any).desktopApi.notifyClusterChange(currentCluster);
52+
}
53+
54+
// Update the ref for next comparison
55+
previousClusterRef.current = currentCluster;
56+
}, [currentCluster]);
57+
58+
return currentCluster;
59+
}
60+
61+
/**
62+
* Component that automatically monitors cluster changes and notifies Electron
63+
* This component should be included once in the app root to enable MCP server restart functionality
64+
*/
65+
export function ClusterChangeNotifier(): null {
66+
useClusterChangeNotifier();
67+
return null;
68+
}

ai-assistant/src/index.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { MCPSettings } from './components/settings';
2626
import { getDefaultConfig } from './config/modelConfig';
2727
import { PromptWidthProvider } from './contexts/PromptWidthContext';
2828
import { isTestModeCheck } from './helper';
29+
import { ClusterChangeNotifier } from './hooks/useClusterChangeNotifier';
2930
import AIPrompt from './modal';
3031
import {
3132
getAllAvailableTools,
@@ -50,6 +51,13 @@ const AIPanelComponent = React.memo(() => {
5051
const [width, setWidth] = React.useState('35vw');
5152
const [isResizing, setIsResizing] = React.useState(false);
5253

54+
// Check if models are configured
55+
const savedConfigData = React.useMemo(() => {
56+
return getSavedConfigurations(conf);
57+
}, [conf]);
58+
59+
const hasAnyValidConfig = savedConfigData.providers && savedConfigData.providers.length > 0;
60+
5361
const handleMouseDown = React.useCallback((e: React.MouseEvent) => {
5462
e.preventDefault();
5563
setIsResizing(true);
@@ -105,6 +113,8 @@ const AIPanelComponent = React.memo(() => {
105113
},
106114
}}
107115
>
116+
{/* Monitor cluster changes and notify electron - only when models are configured */}
117+
{hasAnyValidConfig && <ClusterChangeNotifier />}
108118
<Box
109119
onMouseDown={handleMouseDown}
110120
sx={{
@@ -485,3 +495,6 @@ function Settings() {
485495
}
486496

487497
registerPluginSettings(PLUGIN_NAME, Settings);
498+
499+
// Export the cluster change notifier for external use
500+
export { useClusterChangeNotifier, ClusterChangeNotifier } from './hooks/useClusterChangeNotifier';

0 commit comments

Comments
 (0)