diff --git a/internal/config/runtime.go b/internal/config/runtime.go index 7661faaa..75e94b32 100644 --- a/internal/config/runtime.go +++ b/internal/config/runtime.go @@ -21,12 +21,18 @@ func InDocker() bool { return dockerCached } -// DockerLocalhost rewrites localhost in url to host.docker.internal +// DockerLocalhost rewrites localhost or 127.0.0.1 in url to host.docker.internal // when running inside Docker, so the container can reach host services. -// Returns the url unchanged when not in Docker or when it doesn't contain localhost. +// Returns the url unchanged when not in Docker or when it doesn't reference loopback. func DockerLocalhost(url string) string { - if InDocker() && strings.Contains(url, "localhost") { + if !InDocker() { + return url + } + if strings.Contains(url, "localhost") { return strings.Replace(url, "localhost", "host.docker.internal", 1) } + if strings.Contains(url, "127.0.0.1") { + return strings.Replace(url, "127.0.0.1", "host.docker.internal", 1) + } return url } diff --git a/internal/http/providers.go b/internal/http/providers.go index fcb6c6c5..28297411 100644 --- a/internal/http/providers.go +++ b/internal/http/providers.go @@ -142,10 +142,14 @@ func (h *ProvidersHandler) registerInMemory(p *store.LLMProviderData) { h.providerReg.RegisterForTenant(p.TenantID, providers.NewClaudeCLIProvider(cliPath, cliOpts...)) return } - // Ollama doesn't need an API key — inject a dummy value like startup does. + // Ollama doesn't need an API key — handle before the key guard (same as startup). // In Docker, swap localhost → host.docker.internal so the container can reach the host. if p.ProviderType == store.ProviderOllama { - h.providerReg.RegisterForTenant(p.TenantID, providers.NewOpenAIProvider(p.Name, "ollama", config.DockerLocalhost(p.APIBase), "llama3.3")) + host := p.APIBase + if host == "" { + host = "http://localhost:11434" + } + h.providerReg.RegisterForTenant(p.TenantID, providers.NewOpenAIProvider(p.Name, "ollama", config.DockerLocalhost(host+"/v1"), "llama3.3")) return } if p.APIKey == "" {