feat(ai): add provider connection test (#38)
This commit is contained in:
@ -1,7 +1,7 @@
|
||||
import { useState, useEffect, useCallback } from 'react'
|
||||
import { Plus, Trash2, Download, HardDrive, Cpu, ChevronUp, X } from 'lucide-react'
|
||||
import { AIModelCatalog } from '@/components/AIModelCatalog'
|
||||
import { adminApi, type AIProvider, type OllamaModelInfo } from '@/api/admin'
|
||||
import { adminApi, type AIProvider, type OllamaModelInfo, type ProviderConnectionStatus } from '@/api/admin'
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import { Input } from '@/components/ui/input'
|
||||
@ -12,6 +12,16 @@ import { Spinner } from '@/components/ui/spinner'
|
||||
|
||||
const PROVIDER_NAMES = ['openai', 'anthropic', 'gemini', 'ollama', 'claudecode'] as const
|
||||
|
||||
type ConnectionState = ProviderConnectionStatus | 'testing' | 'error'
|
||||
const CONNECTION_MESSAGES: Record<ConnectionState, string> = {
|
||||
testing: 'Test en cours…',
|
||||
success: 'Connexion et authentification vérifiées.',
|
||||
auth_check_unsupported: 'Authentification non vérifiée. Aucune synthèse n’a été générée.',
|
||||
auth_rejected: 'Authentification refusée. Vérifiez les identifiants du fournisseur.',
|
||||
unavailable: 'Fournisseur indisponible. Réessayez plus tard.',
|
||||
error: 'Impossible de tester la connexion. Réessayez.',
|
||||
}
|
||||
|
||||
const CUSTOM_MODELS_KEY = 'ollama_custom_models'
|
||||
|
||||
const KNOWN_OLLAMA_MODELS: { name: string; family: string; tags: { tag: string; size: string }[] }[] = [
|
||||
@ -311,6 +321,7 @@ export function AIProviders() {
|
||||
const [editId, setEditId] = useState<string | null>(null)
|
||||
const [saving, setSaving] = useState(false)
|
||||
const [error, setError] = useState('')
|
||||
const [connectionStates, setConnectionStates] = useState<Record<string, ConnectionState>>({})
|
||||
|
||||
const isOllamaForm = form.name === 'ollama'
|
||||
const isClaudeCodeForm = form.name === 'claudecode'
|
||||
@ -337,11 +348,24 @@ export function AIProviders() {
|
||||
|
||||
useEffect(() => { load() }, [load])
|
||||
|
||||
async function testConnection(provider: AIProvider) {
|
||||
setConnectionStates(states => ({ ...states, [provider.id]: 'testing' }))
|
||||
try {
|
||||
const result = await adminApi.testProvider(provider.id)
|
||||
const status = result?.status
|
||||
const state = status === 'success' || status === 'auth_rejected' || status === 'unavailable' || status === 'auth_check_unsupported' ? status : 'error'
|
||||
setConnectionStates(states => ({ ...states, [provider.id]: state }))
|
||||
} catch {
|
||||
setConnectionStates(states => ({ ...states, [provider.id]: 'error' }))
|
||||
}
|
||||
}
|
||||
|
||||
async function save() {
|
||||
setSaving(true); setError('')
|
||||
try {
|
||||
if (editId) { await adminApi.updateProvider(editId, form) }
|
||||
else { await adminApi.createProvider(form) }
|
||||
setConnectionStates({})
|
||||
setShowForm(false); setEditId(null)
|
||||
setForm({ name: 'openai', api_key: '', model: '', endpoint: '' })
|
||||
setLoading(true); await load()
|
||||
@ -433,8 +457,10 @@ export function AIProviders() {
|
||||
<p className="text-sm text-muted-foreground text-center py-4">Aucun fournisseur configuré</p>
|
||||
)}
|
||||
|
||||
<p className="text-sm text-muted-foreground">Le test vérifie la connexion avec les identifiants enregistrés. Aucune synthèse n’est générée.</p>
|
||||
|
||||
{providers.map(p => (
|
||||
<div key={p.id} className="flex flex-wrap items-center gap-3 rounded-md border px-3 py-2.5">
|
||||
<div key={p.id} data-provider-id={p.id} className="flex flex-wrap items-center gap-3 rounded-md border px-3 py-2.5">
|
||||
<div className="flex-1 min-w-0">
|
||||
<div className="flex items-center gap-2 flex-wrap">
|
||||
<span className="font-semibold capitalize text-sm">{p.name}</span>
|
||||
@ -445,11 +471,23 @@ export function AIProviders() {
|
||||
</div>
|
||||
</div>
|
||||
<div className="flex items-center gap-1.5">
|
||||
<Button variant="ghost" size="sm" className="h-7 px-2 text-xs" onClick={() => startEdit(p)}>Modifier</Button>
|
||||
<Button variant="ghost" size="sm" className="h-7 w-7 p-0 text-destructive hover:text-destructive" onClick={() => remove(p.id)}>
|
||||
<Button variant="outline" size="sm" className="h-7 px-2 text-xs"
|
||||
aria-label={`Tester la connexion ${p.name}`}
|
||||
aria-describedby={`connection-${p.id}`}
|
||||
disabled={connectionStates[p.id] === 'testing' || (!p.has_key && p.name !== 'ollama' && p.name !== 'claudecode')}
|
||||
onClick={() => testConnection(p)}>
|
||||
{connectionStates[p.id] === 'testing' ? 'Test en cours…' : 'Tester'}
|
||||
</Button>
|
||||
<Button variant="ghost" size="sm" className="h-7 px-2 text-xs" disabled={connectionStates[p.id] === 'testing'} onClick={() => startEdit(p)}>Modifier</Button>
|
||||
<Button variant="ghost" size="sm" className="h-7 w-7 p-0 text-destructive hover:text-destructive" disabled={connectionStates[p.id] === 'testing'} onClick={() => remove(p.id)}>
|
||||
<Trash2 className="h-3 w-3" />
|
||||
</Button>
|
||||
</div>
|
||||
<p id={`connection-${p.id}`} role="status" aria-label={`Connexion ${p.name}`} aria-live="polite" aria-atomic="true"
|
||||
className="w-full text-sm">
|
||||
{connectionStates[p.id] ? CONNECTION_MESSAGES[connectionStates[p.id]] :
|
||||
(!p.has_key && p.name !== 'ollama' && p.name !== 'claudecode' ? 'Ajoutez une clé API pour tester la connexion.' : '')}
|
||||
</p>
|
||||
</div>
|
||||
))}
|
||||
</CardContent>
|
||||
|
||||
Reference in New Issue
Block a user