From ae2edbde204f9b2a6d1ab2afa3270c5817b73aef Mon Sep 17 00:00:00 2001 From: rcourtman Date: Fri, 13 Mar 2026 12:06:08 +0000 Subject: [PATCH] fix(ai): complete wiring on first-time configure; guard Ollama fallback MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three follow-up fixes: 1. RestartAIChat() now performs the full post-start wiring (MCP providers, patrol adapter, investigation orchestrator) when the service starts for the first time via Restart(). Previously these were only wired via StartAIChat(), leaving first-time configure with a partially wired service. 2. The Ollama→OpenAI-compatible fallback in createProviderForModel is now guarded by !strings.HasPrefix(modelStr, "ollama:") so explicit "ollama:llama3" models are never silently rerouted to a different provider. 3. Windows install script registration check now uses the $Hostname override (if set) instead of always looking up $env:COMPUTERNAME, so post-install verification works correctly when a custom hostname is specified. --- frontend-modern/public/install-host-agent.ps1 | 2 +- internal/ai/chat/service.go | 7 +++--- internal/api/router.go | 25 +++++++++++++++---- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/frontend-modern/public/install-host-agent.ps1 b/frontend-modern/public/install-host-agent.ps1 index c0c14509c..d212c1c65 100644 --- a/frontend-modern/public/install-host-agent.ps1 +++ b/frontend-modern/public/install-host-agent.ps1 @@ -473,7 +473,7 @@ if (-not $NoService) { for ($i = 1; $i -le $maxRetries; $i++) { Start-Sleep -Seconds $retryDelay - $hostname = $env:COMPUTERNAME + $hostname = if ($Hostname) { $Hostname } else { $env:COMPUTERNAME } $lookupHost = Test-AgentRegistration -PulseUrl $PulseUrl -Hostname $hostname -Token $Token if ($lookupHost) { diff --git a/internal/ai/chat/service.go b/internal/ai/chat/service.go index 3a5f3309e..6b69080a7 100644 --- a/internal/ai/chat/service.go +++ b/internal/ai/chat/service.go @@ -773,10 +773,9 @@ func (s *Service) createProviderForModel(modelStr string) (providers.StreamingPr case "ollama": baseURL := s.cfg.OllamaBaseURL if baseURL == "" { - // Ollama not explicitly configured. If the user has a custom OpenAI-compatible - // base URL set, route there instead — this handles bare model names (e.g. - // "qwen3-omni") typed manually for self-hosted OpenAI-compatible endpoints. - if s.cfg.OpenAIAPIKey != "" && s.cfg.OpenAIBaseURL != "" { + // Only fall back for bare/unrecognized model names (no explicit "ollama:" prefix). + // An explicit "ollama:llama3" must not be silently rerouted to a different provider. + if !strings.HasPrefix(modelStr, "ollama:") && s.cfg.OpenAIAPIKey != "" && s.cfg.OpenAIBaseURL != "" { return providers.NewOpenAIClient(s.cfg.OpenAIAPIKey, modelName, s.cfg.OpenAIBaseURL, timeout), nil } baseURL = "http://localhost:11434" diff --git a/internal/api/router.go b/internal/api/router.go index 0181da78e..62e6abaa6 100644 --- a/internal/api/router.go +++ b/internal/api/router.go @@ -3447,11 +3447,26 @@ func (r *Router) StopAIChat(ctx context.Context) { // RestartAIChat restarts the AI chat service with updated configuration // Call this when AI settings change that affect the service (e.g., model selection) func (r *Router) RestartAIChat(ctx context.Context) { - if r.aiHandler != nil { - if err := r.aiHandler.Restart(ctx); err != nil { - log.Error().Err(err).Msg("Failed to restart AI chat service") - } else { - log.Info().Msg("AI chat service restarted with new configuration") + if r.aiHandler == nil { + return + } + + wasRunning := r.aiHandler.IsRunning(ctx) + + if err := r.aiHandler.Restart(ctx); err != nil { + log.Error().Err(err).Msg("Failed to restart AI chat service") + return + } + + log.Info().Msg("AI chat service restarted with new configuration") + + // If the service just started for the first time (was not running before), + // perform the full post-start wiring that StartAIChat normally handles. + if !wasRunning && r.aiHandler.IsRunning(ctx) { + r.wireAIChatProviders() + r.wireChatServiceToAI() + if r.aiSettingsHandler != nil { + r.aiSettingsHandler.WireOrchestratorAfterChatStart() } } }