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.

Surface native Ollama thinking tokens instead of silently dropping them

rcourtman52469f5cbc20
diff --git a/internal/ai/providers/ollama.go b/internal/ai/providers/ollama.go
index af0ebee0ba..df3019d299 100644
--- a/internal/ai/providers/ollama.go
+++ b/internal/ai/providers/ollama.go
@@ -145,6 +145,7 @@ func ollamaKeepAliveRequestValue(value string) any {
type ollamaMessage struct {
Role string `json:"role"`
Content string `json:"content"`
+ Thinking string `json:"thinking,omitempty"` // Prior-turn reasoning for assistant messages
ToolCalls []ollamaToolCall `json:"tool_calls,omitempty"` // For assistant messages with tool calls
}
@@ -193,6 +194,7 @@ type ollamaResponse struct {
type ollamaMessageResp struct {
Role string `json:"role"`
Content string `json:"content"`
+ Thinking string `json:"thinking,omitempty"` // Reasoning tokens from thinking models (e.g. qwen3)
ToolCalls []ollamaToolCall `json:"tool_calls,omitempty"`
}
@@ -211,8 +213,9 @@ func (c *OllamaClient) Chat(ctx context.Context, req ChatRequest) (*ChatResponse
for _, m := range req.Messages {
msg := ollamaMessage{
- Role: m.Role,
- Content: m.Content,
+ Role: m.Role,
+ Content: m.Content,
+ Thinking: m.ReasoningContent,
}
// Include tool calls for assistant messages (for multi-turn with tool use)
if len(m.ToolCalls) > 0 {
@@ -325,11 +328,12 @@ func (c *OllamaClient) Chat(ctx context.Context, req ChatRequest) (*ChatResponse
// Build response with tool calls if present
chatResp := &ChatResponse{
- Content: ollamaResp.Message.Content,
- Model: ollamaResp.Model,
- StopReason: ollamaResp.DoneReason,
- InputTokens: ollamaResp.PromptEvalCount,
- OutputTokens: ollamaResp.EvalCount,
+ Content: ollamaResp.Message.Content,
+ ReasoningContent: ollamaResp.Message.Thinking,
+ Model: ollamaResp.Model,
+ StopReason: ollamaResp.DoneReason,
+ InputTokens: ollamaResp.PromptEvalCount,
+ OutputTokens: ollamaResp.EvalCount,
}
// Convert Ollama tool calls to our format
@@ -354,8 +358,10 @@ func (c *OllamaClient) Chat(ctx context.Context, req ChatRequest) (*ChatResponse
// SupportsThinking returns true if the model supports extended thinking
func (c *OllamaClient) SupportsThinking(model string) bool {
- // Ollama models don't currently support extended thinking in stream output
- return false
+ // Ollama's native /api/chat streams reasoning in message.thinking for
+ // thinking models (e.g. qwen3). Whether a given model actually thinks is
+ // decided by the model itself; the provider passes through whatever it emits.
+ return true
}
// ollamaStreamResponse is a single chunk from the Ollama streaming API
@@ -385,8 +391,9 @@ func (c *OllamaClient) ChatStream(ctx context.Context, req ChatRequest, callback
for _, m := range req.Messages {
msg := ollamaMessage{
- Role: m.Role,
- Content: m.Content,
+ Role: m.Role,
+ Content: m.Content,
+ Thinking: m.ReasoningContent,
}
if len(m.ToolCalls) > 0 {
for _, tc := range m.ToolCalls {
@@ -499,6 +506,15 @@ func (c *OllamaClient) ChatStream(ctx context.Context, req ChatRequest, callback
return fmt.Errorf("stream decode error: %w", err)
}
+ // Reasoning tokens arrive in message.thinking before any content for
+ // thinking models (e.g. qwen3); surface them live instead of dead air.
+ if chunk.Message.Thinking != "" {
+ callback(StreamEvent{
+ Type: "thinking",
+ Data: ThinkingEvent{Text: chunk.Message.Thinking},
+ })
+ }
+
// Regular content
if chunk.Message.Content != "" {
callback(StreamEvent{
diff --git a/internal/ai/providers/ollama_test.go b/internal/ai/providers/ollama_test.go
index cbd23030f5..5d0beb08ff 100644
--- a/internal/ai/providers/ollama_test.go
+++ b/internal/ai/providers/ollama_test.go
@@ -78,6 +78,48 @@ func TestOllamaClient_ChatStream_Success(t *testing.T) {
assert.True(t, done)
}
+func TestOllamaClient_ChatStream_Thinking(t *testing.T) {
+ // Thinking models (e.g. qwen3) stream reasoning in message.thinking
+ // before any content arrives — Pulse must surface it live.
+ mockResponse := `{"model":"qwen3:8b","message":{"role":"assistant","content":"","thinking":"Let me"},"done":false}
+{"model":"qwen3:8b","message":{"role":"assistant","content":"","thinking":" think."},"done":false}
+{"model":"qwen3:8b","message":{"role":"assistant","content":"Answer"},"done":false}
+{"model":"qwen3:8b","message":{"role":"assistant","content":""},"done":true,"done_reason":"stop","prompt_eval_count":10,"eval_count":5}
+`
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ w.Header().Set("Content-Type", "application/x-ndjson")
+ w.Write([]byte(mockResponse))
+ }))
+ defer server.Close()
+
+ client := newTestOllamaClient(t, "qwen3:8b", server.URL, "", "", 0)
+
+ var thinking, content string
+ var events []string
+
+ err := client.ChatStream(context.Background(), ChatRequest{
+ Model: "qwen3:8b",
+ Messages: []Message{{Role: "user", Content: "Hi"}},
+ }, func(event StreamEvent) {
+ events = append(events, event.Type)
+ switch event.Type {
+ case "thinking":
+ if data, ok := event.Data.(ThinkingEvent); ok {
+ thinking += data.Text
+ }
+ case "content":
+ if data, ok := event.Data.(ContentEvent); ok {
+ content += data.Text
+ }
+ }
+ })
+
+ require.NoError(t, err)
+ assert.Equal(t, "Let me think.", thinking)
+ assert.Equal(t, "Answer", content)
+ assert.Equal(t, []string{"thinking", "thinking", "content", "done"}, events)
+}
+
func TestOllamaClient_ChatStream_ToolCall(t *testing.T) {
// Mock Ollama Tool Call Streaming (NDJSON)
// Note: Ollama sends tool calls in the message object, often in one chunk or accumulated.
@@ -194,8 +236,9 @@ func TestOllamaClient_Chat_Success(t *testing.T) {
_ = json.NewEncoder(w).Encode(ollamaResponse{
Model: "llama3",
Message: ollamaMessageResp{
- Role: "assistant",
- Content: "Hello",
+ Role: "assistant",
+ Content: "Hello",
+ Thinking: "pondering",
ToolCalls: []ollamaToolCall{
{Function: ollamaFunctionCall{Name: "get_time", Arguments: map[string]interface{}{"tz": "UTC"}}},
},
@@ -224,6 +267,7 @@ func TestOllamaClient_Chat_Success(t *testing.T) {
})
require.NoError(t, err)
assert.Equal(t, "Hello", resp.Content)
+ assert.Equal(t, "pondering", resp.ReasoningContent)
assert.Equal(t, "tool_use", resp.StopReason)
require.Len(t, resp.ToolCalls, 1)
assert.Equal(t, "get_time", resp.ToolCalls[0].Name)
@@ -328,9 +372,9 @@ func TestOllamaClient_TestConnection(t *testing.T) {
}
func TestOllamaClient_SupportsThinking(t *testing.T) {
- client := newTestOllamaClient(t, "llama3", "http://localhost:11434", "", "", 0)
- if client.SupportsThinking("llama3") {
- t.Fatal("expected SupportsThinking to be false")
+ client := newTestOllamaClient(t, "qwen3:8b", "http://localhost:11434", "", "", 0)
+ if !client.SupportsThinking("qwen3:8b") {
+ t.Fatal("expected SupportsThinking to be true")
}
}