Skip to content

armgit / Pulse

Public Imported from GitHub
0

Real-time monitoring dashboard for Proxmox VE, PBS, Docker, Kubernetes, TrueNAS and vSphere. Self-hosted, with alerts for failed backups, full disks, unhealthy containers and offline nodes.

fix(ai): fix ollama streaming timeouts and ensure consistent tool call responses

rcourtman9d83e4e1d14a
diff --git a/internal/ai/chat/agentic.go b/internal/ai/chat/agentic.go
index ca97a20805..52e319b122 100644
--- a/internal/ai/chat/agentic.go
+++ b/internal/ai/chat/agentic.go
@@ -1131,22 +1131,9 @@ func (a *AgenticLoop) executeWithTools(ctx context.Context, sessionID string, me
}
if singleToolEnforced && len(toolCalls) > 0 {
- summary := firstToolResultText
- if strings.TrimSpace(summary) == "" {
- if preferredToolName != "" {
- summary = fmt.Sprintf("Tool %s completed.", preferredToolName)
- } else {
- summary = "Tool call completed."
- }
- }
- if len(resultMessages) > 0 {
- lastIdx := len(resultMessages) - 1
- if resultMessages[lastIdx].Role == "assistant" && strings.TrimSpace(resultMessages[lastIdx].Content) == "" {
- resultMessages[lastIdx].Content = summary
- }
- }
- jsonData, _ := json.Marshal(ContentData{Text: summary})
- callback(StreamEvent{Type: "content", Data: jsonData})
+ // Single tool request completed - ensure we have a proper response
+ // Don't just return raw tool output, let ensureFinalTextResponse synthesize if needed
+ resultMessages = a.ensureFinalTextResponse(ctx, sessionID, resultMessages, providerMessages, callback)
return resultMessages, nil
}
diff --git a/internal/ai/providers/ollama.go b/internal/ai/providers/ollama.go
index 6fb500dac0..6b892b611f 100644
--- a/internal/ai/providers/ollama.go
+++ b/internal/ai/providers/ollama.go
@@ -13,9 +13,10 @@ import (
// OllamaClient implements the Provider interface for Ollama's local API
type OllamaClient struct {
- model string
- baseURL string
- client *http.Client
+ model string
+ baseURL string
+ client *http.Client // For non-streaming requests (has overall timeout)
+ streamClient *http.Client // For streaming requests (no overall timeout — relies on context)
}
// NewOllamaClient creates a new Ollama API client
@@ -38,6 +39,15 @@ func NewOllamaClient(model, baseURL string, timeout time.Duration) *OllamaClient
client: &http.Client{
Timeout: timeout,
},
+ streamClient: &http.Client{
+ // No Timeout set — http.Client.Timeout covers the entire request lifecycle
+ // including reading the response body, which kills streaming responses from
+ // slow models (e.g. CPU-only inference). Context cancellation handles timeouts
+ // for streaming instead.
+ Transport: &http.Transport{
+ ResponseHeaderTimeout: timeout, // Still timeout if server never starts responding
+ },
+ },
}
}
@@ -379,7 +389,10 @@ func (c *OllamaClient) ChatStream(ctx context.Context, req ChatRequest, callback
httpReq.Header.Set("Content-Type", "application/json")
- resp, err := c.client.Do(httpReq)
+ // Use streamClient which has no overall timeout — http.Client.Timeout
+ // includes response body reading time, which kills slow streaming responses.
+ // Context cancellation (from the agentic loop) handles timeouts instead.
+ resp, err := c.streamClient.Do(httpReq)
if err != nil {
return fmt.Errorf("request failed: %w", err)
}