Skip to content

Self-improving AI Employees for everyone

fix(client): refresh the global model list when endpoints change

trohitg13f4746bc88e
diff --git a/client/src/components/ui/TopToolbar.tsx b/client/src/components/ui/TopToolbar.tsx
index 85683a36..7f0026ea 100644
--- a/client/src/components/ui/TopToolbar.tsx
+++ b/client/src/components/ui/TopToolbar.tsx
@@ -60,7 +60,7 @@ import { cn } from '@/lib/utils';
import { useCanvasDockStore } from '../../stores/canvasDockStore';
import { useAuth } from '../../contexts/AuthContext';
import { useApiKeys, GlobalModelState } from '../../hooks/useApiKeys';
-import { useStoredProviderCount } from '../../hooks/useCatalogueQuery';
+import { useStoredCredentialSignature } from '../../hooks/useCatalogueQuery';
import { AI_PROVIDER_META } from '../icons/AIProviderIcons';
// New-contract token: --border-default ↔ Tailwind utility `bg-border-default`.
@@ -146,15 +146,15 @@ const TopToolbar: React.FC<TopToolbarProps> = ({
const { getValidatedAiProviders, saveGlobalModel, isConnected: apiKeysConnected } = useApiKeys();
const [globalModelState, setGlobalModelState] = useState<GlobalModelState>({ providers: [], global_provider: null, global_model: null });
- // Re-fetch validated providers whenever the count of stored
- // credentials changes. Read from the catalogue (single source of
- // truth — `provider.stored` flag); the retired
- // `apiKeyStatuses[id].hasKey` mirror duplicated this answer.
- const apiKeyCount = useStoredProviderCount();
+ // Re-fetch validated providers whenever the stored credentials change:
+ // a provider added or removed, or a named endpoint added, removed or
+ // refreshed. Read from the catalogue (single source of truth); a count of
+ // stored providers would miss a second endpoint.
+ const storedSignature = useStoredCredentialSignature();
useEffect(() => {
if (!apiKeysConnected) return;
getValidatedAiProviders().then(state => setGlobalModelState(state));
- }, [apiKeysConnected, apiKeyCount, getValidatedAiProviders]);
+ }, [apiKeysConnected, storedSignature, getValidatedAiProviders]);
const handleSelectGlobalModel = useCallback((value: string) => {
const [provider, ...rest] = value.split('::');
diff --git a/client/src/hooks/__tests__/storedCredentialSignature.test.ts b/client/src/hooks/__tests__/storedCredentialSignature.test.ts
new file mode 100644
index 00000000..dd220340
--- /dev/null
+++ b/client/src/hooks/__tests__/storedCredentialSignature.test.ts
@@ -0,0 +1,57 @@
+/**
+ * The toolbar re-fetches its global model list when this signature changes,
+ * so it must change for every edit to what the picker offers, including a
+ * second endpoint, which leaves the count of stored providers unchanged.
+ */
+
+import { describe, expect, it } from 'vitest';
+
+import { storedCredentialSignature, type ServerProviderConfig } from '../useCatalogueQuery';
+
+const provider = (id: string, stored: boolean, endpoints?: ServerProviderConfig['endpoints']): ServerProviderConfig => ({
+ id,
+ name: id,
+ category: 'ai',
+ category_label: 'AI',
+ color: '',
+ kind: 'apiKey',
+ stored,
+ endpoints,
+});
+
+const endpoint = (slug: string, modelCount: number) => ({
+ ref: `openai_compatible:${slug}`,
+ label: slug,
+ base_url: `http://${slug}:8000/v1`,
+ kind: 'generic',
+ model_count: modelCount,
+});
+
+describe('storedCredentialSignature', () => {
+ it('changes when a second endpoint is added', () => {
+ const one = [provider('openai', true), provider('openai_compatible', true, [endpoint('home', 2)])];
+ const two = [provider('openai', true), provider('openai_compatible', true, [endpoint('home', 2), endpoint('lab', 1)])];
+
+ expect(storedCredentialSignature(two)).not.toBe(storedCredentialSignature(one));
+ });
+
+ it('changes when a refresh changes an endpoint model count', () => {
+ const before = [provider('openai_compatible', true, [endpoint('home', 2)])];
+ const after = [provider('openai_compatible', true, [endpoint('home', 3)])];
+
+ expect(storedCredentialSignature(after)).not.toBe(storedCredentialSignature(before));
+ });
+
+ it('changes when a provider is stored or removed, and ignores unstored ones', () => {
+ const base = [provider('openai', true), provider('anthropic', false)];
+
+ expect(storedCredentialSignature([provider('openai', true), provider('anthropic', true)])).not.toBe(
+ storedCredentialSignature(base),
+ );
+ expect(storedCredentialSignature(base)).toBe(storedCredentialSignature([provider('openai', true)]));
+ });
+
+ it('is empty before the catalogue loads', () => {
+ expect(storedCredentialSignature(undefined)).toBe('');
+ });
+});
diff --git a/client/src/hooks/useCatalogueQuery.ts b/client/src/hooks/useCatalogueQuery.ts
index fae1906a..2c82e5ec 100644
--- a/client/src/hooks/useCatalogueQuery.ts
+++ b/client/src/hooks/useCatalogueQuery.ts
@@ -393,3 +393,22 @@ export function useStoredProviderCount(): number {
if (!data?.providers) return 0;
return data.providers.filter((p) => p.stored).length;
}
+
+/**
+ * A key that changes whenever what a model picker can offer changes: which
+ * providers have a stored credential, and each saved named endpoint with its
+ * model count. A count of stored providers misses a second endpoint, because
+ * the provider holding it is already stored.
+ */
+export function storedCredentialSignature(providers: ReadonlyArray<ServerProviderConfig> | undefined): string {
+ if (!providers) return '';
+ return providers
+ .filter((p) => p.stored)
+ .map((p) => [p.id, ...(p.endpoints ?? []).map((e) => `${e.ref}:${e.model_count}`)].join(','))
+ .join('|');
+}
+
+export function useStoredCredentialSignature(): string {
+ const { data } = useCatalogueQuery();
+ return storedCredentialSignature(data?.providers);
+}