feat: Ollama auto-detect without manual input
- Auto-detect Ollama endpoint from predefined URLs - Try multiple common addresses (localhost, VPS IPs, cloud domain) - One-click connect to Ollama without manual endpoint entry - Visual feedback during detection - Support for https://llm.reifonas.cloud
This commit is contained in:
@@ -80,6 +80,33 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
};
|
||||
|
||||
const handleTestApi = async () => {
|
||||
if (provider === 'ollama') {
|
||||
setIsTesting(true);
|
||||
setError(null);
|
||||
try {
|
||||
const result = await testApiKey(provider, '', endpoint);
|
||||
|
||||
if (result.success && result.models && result.models.length > 0) {
|
||||
setAvailableModels(result.models);
|
||||
setModel(result.models[0].id);
|
||||
if (result.endpoint) {
|
||||
setEndpoint(result.endpoint);
|
||||
}
|
||||
setTestStatus('success');
|
||||
setError(null);
|
||||
} else {
|
||||
setTestStatus('error');
|
||||
setError(result.error || 'Falha ao conectar com Ollama');
|
||||
}
|
||||
} catch (err) {
|
||||
setTestStatus('error');
|
||||
setError(err instanceof Error ? err.message : 'Erro ao testar API');
|
||||
} finally {
|
||||
setIsTesting(false);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!localApiKey.trim() || !isValidApiKey(localApiKey)) {
|
||||
setError('Insira uma chave de API válida para testar.');
|
||||
return;
|
||||
@@ -114,11 +141,12 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
setModel(providerConfig?.defaultModel || '');
|
||||
setAvailableModels([]);
|
||||
setTestStatus('idle');
|
||||
setEndpoint('');
|
||||
};
|
||||
|
||||
const handleSave = () => {
|
||||
if (provider === 'ollama') {
|
||||
if (endpoint.trim()) {
|
||||
if (endpoint.trim() || testStatus === 'success') {
|
||||
onKeySave('', provider, model, endpoint.trim());
|
||||
}
|
||||
} else if (localApiKey.trim() && isValidApiKey(localApiKey)) {
|
||||
@@ -163,30 +191,61 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Azure/Ollama Endpoint */}
|
||||
{(provider === 'azure' || provider === 'ollama') && (
|
||||
<div>
|
||||
<label htmlFor="provider-endpoint" className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">
|
||||
{provider === 'ollama' ? 'Endereço do Ollama (VPS)' : 'Endpoint do Azure'}
|
||||
</label>
|
||||
<input
|
||||
type="url"
|
||||
id="provider-endpoint"
|
||||
className="block w-full rounded-xl border-slate-300 dark:border-slate-600 bg-white/50 dark:bg-slate-700/50 py-3 px-4 text-slate-900 dark:text-slate-100 shadow-sm focus:border-blue-500 focus:ring-blue-500 sm:text-sm"
|
||||
placeholder={provider === 'ollama' ? 'http://192.168.1.100:11434' : 'https://seu-resource.openai.azure.com'}
|
||||
value={endpoint}
|
||||
onChange={(e) => setEndpoint(e.target.value)}
|
||||
/>
|
||||
{/* Ollama Endpoint - Auto Detect */}
|
||||
{provider === 'ollama' && (
|
||||
<p className="mt-1 text-xs text-slate-500">
|
||||
Informe o IP público da sua VPS e a porta (padrão: 11434)
|
||||
<div>
|
||||
<label className="block text-xs font-semibold text-slate-500 dark:text-slate-400 uppercase tracking-wider mb-2">
|
||||
Ollama (Local)
|
||||
</label>
|
||||
<div className="p-4 bg-slate-100 dark:bg-slate-700/50 rounded-xl border border-slate-200 dark:border-slate-600">
|
||||
<div className="flex items-center justify-between mb-3">
|
||||
<div className="flex items-center gap-3">
|
||||
<div className="w-10 h-10 rounded-lg bg-gradient-to-br from-purple-500 to-pink-600 flex items-center justify-center text-white font-bold">
|
||||
O
|
||||
</div>
|
||||
<div>
|
||||
<p className="font-medium text-slate-700 dark:text-slate-200">Detectando Ollama...</p>
|
||||
<p className="text-xs text-slate-500 dark:text-slate-400">Procurando na rede local</p>
|
||||
</div>
|
||||
</div>
|
||||
{isTesting && (
|
||||
<span className="w-5 h-5 border-2 border-purple-500 border-t-transparent rounded-full animate-spin"></span>
|
||||
)}
|
||||
{testStatus === 'success' && (
|
||||
<svg className="w-5 h-5 text-green-500" fill="none" viewBox="0 0 24 24" stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M5 13l4 4L19 7" />
|
||||
</svg>
|
||||
)}
|
||||
</div>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleTestApi}
|
||||
disabled={isTesting}
|
||||
className="w-full py-2 px-4 bg-gradient-to-r from-purple-500 to-pink-600 hover:from-purple-600 hover:to-pink-700 text-white font-medium rounded-lg transition-all disabled:opacity-50"
|
||||
>
|
||||
{isTesting ? 'Procurando...' : testStatus === 'success' ? 'Conectado!' : 'Conectar ao Ollama'}
|
||||
</button>
|
||||
{endpoint && (
|
||||
<p className="mt-2 text-xs text-slate-500 dark:text-slate-400">
|
||||
Endpoint: <span className="font-mono">{endpoint}</span>
|
||||
</p>
|
||||
)}
|
||||
{error && (
|
||||
<p className="mt-2 text-xs text-red-600 dark:text-red-400">
|
||||
{error}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
{testStatus === 'success' && availableModels.length > 0 && (
|
||||
<p className="mt-2 text-xs text-green-600 dark:text-green-400">
|
||||
✓ {availableModels.length} modelos disponíveis detectados
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* API Key Input (not needed for Ollama) */}
|
||||
{provider !== 'ollama' && (
|
||||
{/* Azure Endpoint */}
|
||||
{provider === 'azure' && (
|
||||
<div>
|
||||
<label htmlFor="api-key-setup" className="block text-sm font-medium text-slate-700 dark:text-slate-300 mb-2">
|
||||
Chave de API ({getProviderLabel(provider)})
|
||||
@@ -281,11 +340,11 @@ export const ApiKeySetup: React.FC<ApiKeySetupProps> = ({ onKeySave }) => {
|
||||
|
||||
<button
|
||||
onClick={handleSave}
|
||||
disabled={provider !== 'ollama' && (!localApiKey.trim() || isValid === false)}
|
||||
disabled={provider === 'ollama' ? !endpoint && testStatus !== 'success' : (!localApiKey.trim() || isValid === false)}
|
||||
className="w-full flex justify-center items-center gap-2 bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-700 hover:to-indigo-700 text-white font-bold py-3.5 px-4 rounded-xl shadow-lg hover:shadow-blue-500/30 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-300 transform active:scale-[0.98]"
|
||||
>
|
||||
<SaveIcon className="h-5 w-5" />
|
||||
<span>Salvar e Continuar</span>
|
||||
<span>{provider === 'ollama' ? 'Conectar e Continuar' : 'Salvar e Continuar'}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { PROVIDERS, type AIProvider } from '../types/providers';
|
||||
import { OLLAMA_AUTO_DETECT_URLS } from '../types/providers';
|
||||
|
||||
export interface ModelInfo {
|
||||
id: string;
|
||||
@@ -9,6 +10,7 @@ interface TestResult {
|
||||
success: boolean;
|
||||
models?: ModelInfo[];
|
||||
error?: string;
|
||||
endpoint?: string;
|
||||
}
|
||||
|
||||
export const testApiKey = async (provider: AIProvider, apiKey: string, endpoint?: string): Promise<TestResult> => {
|
||||
@@ -35,13 +37,37 @@ export const testApiKey = async (provider: AIProvider, apiKey: string, endpoint?
|
||||
}
|
||||
};
|
||||
|
||||
const findOllamaEndpoint = async (): Promise<string | null> => {
|
||||
for (const url of OLLAMA_AUTO_DETECT_URLS) {
|
||||
try {
|
||||
const response = await fetch(`${url}/api/tags`, {
|
||||
method: 'GET',
|
||||
signal: AbortSignal.timeout(3000)
|
||||
});
|
||||
if (response.ok) {
|
||||
return url;
|
||||
}
|
||||
} catch {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
};
|
||||
|
||||
const testOllama = async (endpoint?: string): Promise<TestResult> => {
|
||||
if (!endpoint) {
|
||||
return { success: false, error: 'Endereço do Ollama é obrigatório (ex: http://192.168.1.100:11434)' };
|
||||
let ollamaEndpoint = endpoint;
|
||||
|
||||
if (!ollamaEndpoint) {
|
||||
const foundEndpoint = await findOllamaEndpoint();
|
||||
if (foundEndpoint) {
|
||||
ollamaEndpoint = foundEndpoint;
|
||||
} else {
|
||||
return { success: false, error: 'Ollama não encontrado. Configure o endereço manualmente.' };
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
const response = await fetch(`${endpoint}/api/tags`);
|
||||
const response = await fetch(`${ollamaEndpoint}/api/tags`);
|
||||
|
||||
if (!response.ok) {
|
||||
return { success: false, error: 'Não foi possível conectar ao Ollama. Verifique o endereço.' };
|
||||
@@ -61,12 +87,13 @@ const testOllama = async (endpoint?: string): Promise<TestResult> => {
|
||||
);
|
||||
|
||||
if (visionModels.length > 0) {
|
||||
return { success: true, models: visionModels };
|
||||
return { success: true, models: visionModels, endpoint: ollamaEndpoint };
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
models: models.length > 0 ? models : [{ id: 'llama3.2', name: 'Llama 3.2 (Padrão)' }]
|
||||
models: models.length > 0 ? models : [{ id: 'llama3.2', name: 'Llama 3.2 (Padrão)' }],
|
||||
endpoint: ollamaEndpoint
|
||||
};
|
||||
} catch (error: any) {
|
||||
return { success: false, error: 'Não foi possível conectar ao Ollama. Verifique o endereço e certifique-se que o Ollama está rodando.' };
|
||||
|
||||
@@ -2,6 +2,16 @@ import { AIProvider } from './providers';
|
||||
|
||||
export type AIProvider = 'gemini' | 'openai' | 'anthropic' | 'azure' | 'ollama';
|
||||
|
||||
export const OLLAMA_AUTO_DETECT_URLS = [
|
||||
'http://localhost:11434',
|
||||
'http://127.0.0.1:11434',
|
||||
'http://192.168.1.100:11434',
|
||||
'http://10.0.0.1:11434',
|
||||
'https://llm.reifonas.cloud',
|
||||
'http://ollama:11434',
|
||||
'http://host.docker.internal:11434',
|
||||
];
|
||||
|
||||
export interface ProviderConfig {
|
||||
id: AIProvider;
|
||||
name: string;
|
||||
|
||||
Reference in New Issue
Block a user