From 1da1ec617a806e148d83dc431b4349e81916edd4 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Tue, 7 Apr 2026 10:23:55 -0400 Subject: [PATCH] fix: reload OpenCode config and invalidate provider caches after credential changes (#183) - Trigger reloadConfig() after API key save/delete in providers route so the OpenCode server immediately recognizes newly connected providers - Wrap reloadConfig() in try/catch in both providers.ts and oauth.ts so credential operations succeed even if the reload fails transiently - Add invalidateProviderCaches() utility covering all provider-related query keys, replacing scattered manual invalidation across ProviderSettings - Remove duplicate provider key invalidation from invalidateConfigCaches() by delegating to invalidateProviderCaches() - Remove unused invalidateAllConfigRelatedCaches() export Fixes #180 --- backend/src/routes/oauth.ts | 11 +++++++---- backend/src/routes/providers.ts | 15 +++++++++++++++ .../components/settings/ProviderSettings.tsx | 13 +++++-------- frontend/src/lib/queryInvalidation.ts | 17 ++++++++++------- 4 files changed, 37 insertions(+), 19 deletions(-) diff --git a/backend/src/routes/oauth.ts b/backend/src/routes/oauth.ts index 83831582..9dd362d3 100644 --- a/backend/src/routes/oauth.ts +++ b/backend/src/routes/oauth.ts @@ -74,10 +74,13 @@ export function createOAuthRoutes() { } const data = await response.json() - - logger.info(`OAuth callback successful for ${providerId}, reloading OpenCode configuration`) - await opencodeServerManager.reloadConfig() - + + try { + await opencodeServerManager.reloadConfig() + } catch (reloadError) { + logger.warn(`Failed to reload OpenCode config after OAuth callback for ${providerId}:`, reloadError) + } + return c.json(data) } catch (error) { logger.error('OAuth callback error:', error) diff --git a/backend/src/routes/providers.ts b/backend/src/routes/providers.ts index f40676d4..1ad00014 100644 --- a/backend/src/routes/providers.ts +++ b/backend/src/routes/providers.ts @@ -4,6 +4,7 @@ import { AuthService } from '../services/auth' import { SetCredentialRequestSchema } from '../../../shared/src/schemas/auth' import { logger } from '../utils/logger' import { setOpenCodeAuth, deleteOpenCodeAuth } from '../services/proxy' +import { opencodeServerManager } from '../services/opencode-single-server' export function createProvidersRoutes() { const app = new Hono() @@ -42,6 +43,13 @@ export function createProvidersRoutes() { } await authService.set(providerId, validated.apiKey) + + try { + await opencodeServerManager.reloadConfig() + } catch (reloadError) { + logger.warn(`Failed to reload OpenCode config after saving credentials for ${providerId}:`, reloadError) + } + return c.json({ success: true }) } catch (error) { logger.error('Failed to set provider credentials:', error) @@ -62,6 +70,13 @@ export function createProvidersRoutes() { } await authService.delete(providerId) + + try { + await opencodeServerManager.reloadConfig() + } catch (reloadError) { + logger.warn(`Failed to reload OpenCode config after deleting credentials for ${providerId}:`, reloadError) + } + return c.json({ success: true }) } catch (error) { logger.error('Failed to delete provider credentials:', error) diff --git a/frontend/src/components/settings/ProviderSettings.tsx b/frontend/src/components/settings/ProviderSettings.tsx index f32dfb0e..9774d377 100644 --- a/frontend/src/components/settings/ProviderSettings.tsx +++ b/frontend/src/components/settings/ProviderSettings.tsx @@ -1,6 +1,6 @@ import { useState, useMemo, useCallback } from 'react' import { Button } from '@/components/ui/button' -import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' +import { Card, CardContent, CardHeader, CardTitle, CardDescription } from '@/components/ui/card' import { Badge } from '@/components/ui/badge' import { Input } from '@/components/ui/input' import { DeleteDialog } from '@/components/ui/delete-dialog' @@ -12,6 +12,7 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { OAuthAuthorizeDialog } from './OAuthAuthorizeDialog' import { OAuthCallbackDialog } from './OAuthCallbackDialog' import { ApiKeyDialog } from '@/components/model/ApiKeyDialog' +import { invalidateProviderCaches } from '@/lib/queryInvalidation' export function ProviderSettings() { const [selectedProvider, setSelectedProvider] = useState(null) @@ -48,9 +49,7 @@ export function ProviderSettings() { const deleteCredentialMutation = useMutation({ mutationFn: (providerId: string) => providerCredentialsApi.delete(providerId), onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ['provider-credentials'] }) - queryClient.invalidateQueries({ queryKey: ['providers'] }) - queryClient.invalidateQueries({ queryKey: ['providers-with-models'] }) + invalidateProviderCaches(queryClient) }, }) @@ -81,7 +80,7 @@ export function ProviderSettings() { } const handleOAuthSuccess = () => { - queryClient.invalidateQueries({ queryKey: ['provider-credentials'] }) + invalidateProviderCaches(queryClient) setOauthCallbackDialogOpen(false) setOauthResponse(null) setSelectedProvider(null) @@ -138,9 +137,7 @@ export function ProviderSettings() { const handleApiKeySuccess = useCallback(() => { setApiKeyDialogOpen(false) setApiKeyProvider(null) - queryClient.invalidateQueries({ queryKey: ['provider-credentials'] }) - queryClient.invalidateQueries({ queryKey: ['providers'] }) - queryClient.invalidateQueries({ queryKey: ['providers-with-models'] }) + invalidateProviderCaches(queryClient) }, [queryClient]) const handleApiKeyDialogClose = useCallback((open: boolean) => { diff --git a/frontend/src/lib/queryInvalidation.ts b/frontend/src/lib/queryInvalidation.ts index 81f84bb5..b7493d30 100644 --- a/frontend/src/lib/queryInvalidation.ts +++ b/frontend/src/lib/queryInvalidation.ts @@ -1,15 +1,23 @@ import type { QueryClient } from '@tanstack/react-query' +export function invalidateProviderCaches(queryClient: QueryClient) { + queryClient.invalidateQueries({ queryKey: ['provider-credentials'] }) + queryClient.invalidateQueries({ queryKey: ['provider-auth-methods'] }) + queryClient.invalidateQueries({ queryKey: ['providers'] }) + queryClient.invalidateQueries({ queryKey: ['providers-with-models'] }) + queryClient.invalidateQueries({ queryKey: ['opencode', 'providers'] }) + queryClient.invalidateQueries({ queryKey: ['providers-for-execution-model'] }) +} + export function invalidateConfigCaches(queryClient: QueryClient) { queryClient.invalidateQueries({ queryKey: ['opencode', 'config'] }) queryClient.invalidateQueries({ queryKey: ['opencode', 'agents'] }) queryClient.invalidateQueries({ queryKey: ['opencode-config'] }) queryClient.invalidateQueries({ queryKey: ['health'] }) queryClient.invalidateQueries({ queryKey: ['mcp-status'] }) - queryClient.invalidateQueries({ queryKey: ['providers'] }) - queryClient.invalidateQueries({ queryKey: ['opencode', 'providers'] }) queryClient.invalidateQueries({ queryKey: ['opencode-skills'] }) queryClient.invalidateQueries({ queryKey: ['managed-skills'] }) + invalidateProviderCaches(queryClient) } export function invalidateSettingsCaches(queryClient: QueryClient, userId = 'default') { @@ -26,8 +34,3 @@ export function invalidateSessionCaches(queryClient: QueryClient) { query.queryKey[1] === 'messages'), }) } - -export function invalidateAllConfigRelatedCaches(queryClient: QueryClient, userId = 'default') { - invalidateSettingsCaches(queryClient, userId) - invalidateSessionCaches(queryClient) -}