fix(providers): register Ollama in-memory on HTTP create, Docker localhost rewrite (#483)

Closes #470. registerInMemory() skipped Ollama because APIKey was empty.
Add Ollama special case before the key guard (mirrors startup code).

When running in Docker, rewrite localhost → host.docker.internal so the
container can reach the host Ollama instance. Extract InDocker() and
DockerLocalhost() into config/runtime.go for reuse.

Add extra_hosts to docker-compose.yml for Linux compatibility.
This commit is contained in:
Duc Nguyen authored and GitHub committed 2026-03-26 12:45:52 +07:00
1 parent 2445192819
commit 19498bff79
5 files changed
+44 -4

No files matched your search

+1 -1
View File
@@ -279,7 +279,7 @@ func registerProvidersFromDB(registry *providers.Registry, provStore store.Provi
if host == "" {
host = "http://localhost:11434"
}
registry.RegisterForTenant(p.TenantID, providers.NewOpenAIProvider(p.Name, "ollama", host+"/v1", "llama3.3"))
registry.RegisterForTenant(p.TenantID, providers.NewOpenAIProvider(p.Name, "ollama", config.DockerLocalhost(host+"/v1"), "llama3.3"))
slog.Info("registered provider from DB", "name", p.Name)
continue
}
+2
View File
@@ -41,6 +41,8 @@ services:
volumes:
- goclaw-data:/app/data
- goclaw-workspace:/app/workspace
extra_hosts:
- "host.docker.internal:host-gateway"
security_opt:
- no-new-privileges:true
init: true
+32
View File
@@ -0,0 +1,32 @@
package config
import (
"os"
"strings"
"sync"
)
var (
dockerOnce sync.Once
dockerCached bool
)
// InDocker returns true when running inside a Docker container.
// Result is cached after the first call.
func InDocker() bool {
dockerOnce.Do(func() {
_, err := os.Stat("/.dockerenv")
dockerCached = err == nil
})
return dockerCached
}
// DockerLocalhost rewrites localhost 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.
func DockerLocalhost(url string) string {
if InDocker() && strings.Contains(url, "localhost") {
return strings.Replace(url, "localhost", "host.docker.internal", 1)
}
return url
}
+2 -3
View File
@@ -5,13 +5,13 @@ import (
"encoding/json"
"errors"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
"github.com/google/uuid"
"github.com/nextlevelbuilder/goclaw/internal/config"
"github.com/nextlevelbuilder/goclaw/internal/i18n"
"github.com/nextlevelbuilder/goclaw/internal/providers"
"github.com/nextlevelbuilder/goclaw/internal/store"
@@ -139,8 +139,7 @@ func (h *ProvidersHandler) handleClaudeCLIAuthStatus(w http.ResponseWriter, r *h
}
}
_, dockerErr := os.Stat("/.dockerenv")
inDocker := dockerErr == nil
inDocker := config.InDocker()
status, err := providers.CheckClaudeAuthStatus(ctx, cliPath)
if err != nil {
+7
View File
@@ -9,6 +9,7 @@ import (
"github.com/google/uuid"
"github.com/nextlevelbuilder/goclaw/internal/bus"
"github.com/nextlevelbuilder/goclaw/internal/config"
"github.com/nextlevelbuilder/goclaw/internal/i18n"
"github.com/nextlevelbuilder/goclaw/internal/oauth"
"github.com/nextlevelbuilder/goclaw/internal/providers"
@@ -141,6 +142,12 @@ 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.
// 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"))
return
}
if p.APIKey == "" {
return
}