diff --git a/internal/agent/think.go b/internal/agent/think.go index 95ab1fbd..6c199a27 100644 --- a/internal/agent/think.go +++ b/internal/agent/think.go @@ -41,9 +41,15 @@ func (e *AgentEngine) streamLLMToEventBus( result := &streamLLMResult{} chunkCount := 0 + responseTypeCounts := make(map[string]int) + firstChunkTime := time.Time{} for chunk := range stream { chunkCount++ + if chunkCount == 1 { + firstChunkTime = time.Now() + } + responseTypeCounts[string(chunk.ResponseType)]++ if chunk.Content != "" { isExtracted := chunk.Data != nil && chunk.Data["source"] != nil @@ -65,6 +71,16 @@ func (e *AgentEngine) streamLLMToEventBus( } } + // Stream diagnostic summary: helps identify non-streaming patterns + streamDuration := time.Duration(0) + if !firstChunkTime.IsZero() { + streamDuration = time.Since(firstChunkTime) + } + logger.Infof(ctx, "[Agent][Stream] Completed: chunks=%d, content_len=%d, tool_calls=%d, "+ + "stream_duration=%dms, type_distribution=%v", + chunkCount, len(result.Content), len(result.ToolCalls), + streamDuration.Milliseconds(), responseTypeCounts) + return result, nil } @@ -88,6 +104,9 @@ func (e *AgentEngine) streamThinkingToEventBus( pendingToolCalls := make(map[string]bool) thinkingToolIDs := make(map[string]string) // tool_call_id -> event ID for thinking tool streams + // Track which event types we emitted for diagnostics + emittedEventTypes := make(map[string]int) + // Generate IDs for this stream thinkingID := generateEventID("thinking") answerID := generateEventID("answer") @@ -103,6 +122,7 @@ func (e *AgentEngine) streamThinkingToEventBus( if toolCallID != "" && toolName != "" && !pendingToolCalls[toolCallID] { pendingToolCalls[toolCallID] = true + emittedEventTypes["tool_call_pending"]++ e.eventBus.Emit(ctx, event.Event{ ID: fmt.Sprintf("%s-tool-call-pending", toolCallID), Type: event.EventAgentToolCall, @@ -119,6 +139,7 @@ func (e *AgentEngine) streamThinkingToEventBus( // Handle final_answer tool's streaming answer content if chunk.ResponseType == types.ResponseTypeAnswer { if source, _ := chunk.Data["source"].(string); source == "final_answer_tool" { + emittedEventTypes["final_answer_chunk"]++ e.eventBus.Emit(ctx, event.Event{ ID: answerID, Type: event.EventAgentFinalAnswer, @@ -141,6 +162,7 @@ func (e *AgentEngine) streamThinkingToEventBus( eventID = generateEventID("thinking-tool") thinkingToolIDs[toolCallID] = eventID } + emittedEventTypes["thinking_tool_chunk"]++ e.eventBus.Emit(ctx, event.Event{ ID: eventID, Type: event.EventAgentThought, @@ -156,6 +178,7 @@ func (e *AgentEngine) streamThinkingToEventBus( } if chunk.Content != "" { + emittedEventTypes["thought_chunk"]++ e.eventBus.Emit(ctx, event.Event{ ID: thinkingID, // Same ID for all chunks in this stream Type: event.EventAgentThought, @@ -174,8 +197,9 @@ func (e *AgentEngine) streamThinkingToEventBus( return nil, err } - logger.Debugf(ctx, "[Agent][Thinking] Iteration-%d completed: content=%d chars, tool_calls=%d", - iteration+1, len(llmResult.Content), len(llmResult.ToolCalls)) + // Emit diagnostics: helps identify when answer content went to "thought" vs "final_answer" events + logger.Infof(ctx, "[Agent][Thinking] Iteration-%d completed: content=%d chars, tool_calls=%d, emitted_events=%v", + iteration+1, len(llmResult.Content), len(llmResult.ToolCalls), emittedEventTypes) fullContent := agenttools.StripThinkBlocks(llmResult.Content) diff --git a/internal/handler/session/agent_stream_handler.go b/internal/handler/session/agent_stream_handler.go index f53c3b23..780c96db 100644 --- a/internal/handler/session/agent_stream_handler.go +++ b/internal/handler/session/agent_stream_handler.go @@ -450,7 +450,11 @@ func (h *AgentStreamHandler) handleComplete(ctx context.Context, evt event.Event // This guards against edge cases where the LLM stops without calling final_answer. if h.finalAnswer == "" && data.FinalAnswer != "" { logger.GetLogger(h.ctx).Warnf( - "No answer events were streamed, emitting fallback answer (len=%d)", len(data.FinalAnswer), + "No answer events were streamed, emitting fallback answer (len=%d). "+ + "This typically happens when: (1) model stopped naturally and content was sent as thought events, "+ + "or (2) Ollama model returned tool calls non-incrementally. "+ + "total_steps=%d, total_duration_ms=%d", + len(data.FinalAnswer), data.TotalSteps, data.TotalDurationMs, ) fallbackID := fmt.Sprintf("answer-fallback-%d", time.Now().UnixMilli()) if err := h.streamManager.AppendEvent(h.ctx, h.sessionID, h.assistantMessageID, interfaces.StreamEvent{ diff --git a/internal/models/chat/ollama.go b/internal/models/chat/ollama.go index 3265abc2..0e00b059 100644 --- a/internal/models/chat/ollama.go +++ b/internal/models/chat/ollama.go @@ -227,7 +227,17 @@ func (c *OllamaChat) ChatStream( Done: false, } - // Extract and stream content from special tools (complete, not incremental) + // Ollama returns tool calls as complete objects (not incremental deltas). + // Log this so we can trace non-streaming answer delivery. + for _, tc := range resp.Message.ToolCalls { + if tc.Function.Name == "final_answer" || tc.Function.Name == "thinking" { + argsBytes, _ := json.Marshal(tc.Function.Arguments) + logger.Warnf(ctx, "[Ollama Stream] Tool %q arrived non-incrementally (%d bytes args), "+ + "answer will not be token-streamed to frontend", + tc.Function.Name, len(argsBytes)) + } + } + for _, tc := range resp.Message.ToolCalls { switch tc.Function.Name { case "final_answer": diff --git a/internal/models/chat/remote_api.go b/internal/models/chat/remote_api.go index 446b5dfc..02150c96 100644 --- a/internal/models/chat/remote_api.go +++ b/internal/models/chat/remote_api.go @@ -6,8 +6,10 @@ import ( "encoding/json" "fmt" "io" + "net" "net/http" "strings" + "time" "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/models/provider" @@ -16,6 +18,17 @@ import ( "github.com/sashabaranov/go-openai" ) +// rawHTTPClient is a shared HTTP client for raw HTTP LLM calls with connection-level timeouts. +// No overall Timeout is set so streaming calls are controlled by context cancellation only. +var rawHTTPClient = &http.Client{ + Transport: &http.Transport{ + DialContext: (&net.Dialer{Timeout: 15 * time.Second}).DialContext, + TLSHandshakeTimeout: 10 * time.Second, + IdleConnTimeout: 90 * time.Second, + MaxIdleConnsPerHost: 5, + }, +} + // RemoteAPIChat 实现了基于 OpenAI 兼容 API 的聊天 // 这是一个通用实现,不包含任何 provider 特定的逻辑 type RemoteAPIChat struct { @@ -285,8 +298,7 @@ func (c *RemoteAPIChat) chatWithRawHTTP(ctx context.Context, endpoint string, cu httpReq.Header.Set("Content-Type", "application/json") httpReq.Header.Set("Authorization", "Bearer "+c.apiKey) - client := &http.Client{} - resp, err := client.Do(httpReq) + resp, err := rawHTTPClient.Do(httpReq) if err != nil { return nil, fmt.Errorf("send request: %w", err) } @@ -430,8 +442,7 @@ func (c *RemoteAPIChat) chatStreamWithRawHTTP(ctx context.Context, endpoint stri httpReq.Header.Set("Authorization", "Bearer "+c.apiKey) httpReq.Header.Set("Accept", "text/event-stream") - client := &http.Client{} - resp, err := client.Do(httpReq) + resp, err := rawHTTPClient.Do(httpReq) if err != nil { return nil, fmt.Errorf("send request: %w", err) } @@ -620,7 +631,7 @@ func (c *RemoteAPIChat) processStreamDelta(ctx context.Context, choice *openai.C // 处理 tool calls if len(delta.ToolCalls) > 0 { - c.processToolCallsDelta(delta.ToolCalls, state, streamChan) + c.processToolCallsDelta(ctx, delta.ToolCalls, state, streamChan) } // 发送思考内容(ReasoningContent,支持 DeepSeek 等模型) @@ -675,7 +686,7 @@ func (c *RemoteAPIChat) processStreamDelta(ctx context.Context, choice *openai.C } // processToolCallsDelta 处理 tool calls 的增量更新 -func (c *RemoteAPIChat) processToolCallsDelta(toolCalls []openai.ToolCall, state *streamState, streamChan chan types.StreamResponse) { +func (c *RemoteAPIChat) processToolCallsDelta(ctx context.Context, toolCalls []openai.ToolCall, state *streamState, streamChan chan types.StreamResponse) { for _, tc := range toolCalls { var toolCallIndex int if tc.Index != nil { @@ -735,6 +746,12 @@ func (c *RemoteAPIChat) processToolCallsDelta(toolCalls []openai.ToolCall, state if !exists { extractor = newJSONFieldExtractor("answer") state.fieldExtractors[toolCallIndex] = extractor + // Detect non-incremental arrival: if the first args chunk is large, + // the model likely returned all arguments at once (non-streaming tool call) + if len(tc.Function.Arguments) > 200 { + logger.Warnf(ctx, "[LLM Stream] final_answer args arrived in large chunk (%d bytes), "+ + "model may not support incremental tool call streaming", len(tc.Function.Arguments)) + } } answerChunk := extractor.Feed(tc.Function.Arguments) if answerChunk != "" { diff --git a/internal/models/utils/ollama/ollama.go b/internal/models/utils/ollama/ollama.go index 5665aacf..f0dba310 100644 --- a/internal/models/utils/ollama/ollama.go +++ b/internal/models/utils/ollama/ollama.go @@ -4,6 +4,7 @@ import ( "context" "encoding/json" "fmt" + "net" "net/http" "net/url" "os" @@ -40,8 +41,18 @@ func GetOllamaService() (*OllamaService, error) { return nil, fmt.Errorf("invalid Ollama service URL: %w", err) } - // Create official client - client := api.NewClient(parsedURL, http.DefaultClient) + // Dedicated HTTP client for Ollama instead of http.DefaultClient. + // - Dial timeout prevents hanging when Ollama process is down or port unreachable + // - No overall Timeout so long-running streaming calls are controlled by context cancellation + ollamaHTTPClient := &http.Client{ + Transport: &http.Transport{ + DialContext: (&net.Dialer{Timeout: 10 * time.Second}).DialContext, + TLSHandshakeTimeout: 10 * time.Second, + IdleConnTimeout: 90 * time.Second, + MaxIdleConns: 10, + }, + } + client := api.NewClient(parsedURL, ollamaHTTPClient) // Check if Ollama is set as optional isOptional := false