import { useState } from 'react' import { Button } from '@/components/ui/button' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Dialog, DialogContent, DialogDescription, DialogHeader, DialogTitle, DialogFooter } from '@/components/ui/dialog' import { Badge } from '@/components/ui/badge' import { Loader2, Key, Check, X, Plus } from 'lucide-react' import { providerCredentialsApi } from '@/api/providers' import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query' import { AddProviderDialog } from './AddProviderDialog' import { getProviders } from '@/api/providers' export function ProviderSettings() { const [selectedProvider, setSelectedProvider] = useState(null) const [apiKey, setApiKey] = useState('') const [showApiKey, setShowApiKey] = useState(false) const [addDialogOpen, setAddDialogOpen] = useState(false) const queryClient = useQueryClient() const { data: providers, isLoading: providersLoading } = useQuery({ queryKey: ['providers'], queryFn: () => getProviders(), staleTime: 300000, }) const { data: credentialsList, isLoading: credentialsLoading } = useQuery({ queryKey: ['provider-credentials'], queryFn: () => providerCredentialsApi.list(), }) const setCredentialMutation = useMutation({ mutationFn: ({ providerId, apiKey }: { providerId: string; apiKey: string }) => providerCredentialsApi.set(providerId, apiKey), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['provider-credentials'] }) setSelectedProvider(null) setApiKey('') }, }) const deleteCredentialMutation = useMutation({ mutationFn: (providerId: string) => providerCredentialsApi.delete(providerId), onSuccess: () => { queryClient.invalidateQueries({ queryKey: ['provider-credentials'] }) }, }) const handleSetCredential = () => { if (selectedProvider && apiKey) { setCredentialMutation.mutate({ providerId: selectedProvider, apiKey }) } } const handleDeleteCredential = (providerId: string) => { if (confirm(`Remove credentials for ${providerId}?`)) { deleteCredentialMutation.mutate(providerId) } } const hasCredentials = (providerId: string) => { return credentialsList?.includes(providerId) || false } if (providersLoading || credentialsLoading) { return (
) } return (

Provider Credentials

Manage API keys for AI providers. Keys are stored securely in your workspace.

{!providers || providers.length === 0 ? (

No providers configured. Add providers in your OpenCode config.

) : (
{providers.map((provider) => { const hasKey = hasCredentials(provider.id) const modelCount = Object.keys(provider.models || {}).length return (
{provider.name || provider.id} {hasKey ? ( Configured ) : ( No Key )} {provider.npm && Package: {provider.npm}} {provider.options?.baseURL && ( {provider.options.baseURL} )} {modelCount > 0 && ( {modelCount} model{modelCount !== 1 ? 's' : ''} )}
{hasKey && ( )}
) })}
)} !open && setSelectedProvider(null)}> Set API Key for {selectedProvider} Enter your API key. It will be stored securely in your workspace.
setApiKey(e.target.value)} placeholder="sk-..." className="bg-background border-border pr-20" />
) }