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:
2026-04-04 19:51:34 +00:00
parent a395f0d696
commit 075f6ae0bc
3 changed files with 121 additions and 25 deletions

View File

@@ -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.' };