fix(agent): prevent empty response when no Knowledge Base is configured
- guard against empty content completion in analyzeResponse/executeLoop with retry (max 2), nudge message, and fallback response - propagate actual finish_reason through streaming pipeline instead of hardcoding "stop" in streamThinkingToEventBus - add FinishReason field to StreamResponse and streamLLMResult - add finish marker chunk in processStreamDelta for empty content scenarios - propagate lastFinishReason through EOF handler in processStream - strengthen PureAgent system prompt with MCP tool guidelines and graceful fallback instructions for unanswerable questions - add unit tests for empty response bug and finish_reason propagation
This commit is contained in:
+17
-4
@@ -15,9 +15,10 @@ import (
|
||||
|
||||
// streamLLMResult holds accumulated output from a streaming LLM call.
|
||||
type streamLLMResult struct {
|
||||
Content string
|
||||
ToolCalls []types.LLMToolCall
|
||||
Usage *types.TokenUsage
|
||||
Content string
|
||||
ToolCalls []types.LLMToolCall
|
||||
Usage *types.TokenUsage
|
||||
FinishReason string // actual finish_reason from LLM (captured from last stream chunk)
|
||||
}
|
||||
|
||||
// streamLLMToEventBus streams LLM response through EventBus (generic method)
|
||||
@@ -66,6 +67,10 @@ func (e *AgentEngine) streamLLMToEventBus(
|
||||
result.Usage = chunk.Usage
|
||||
}
|
||||
|
||||
if chunk.FinishReason != "" {
|
||||
result.FinishReason = chunk.FinishReason
|
||||
}
|
||||
|
||||
if emitFunc != nil {
|
||||
emitFunc(&chunk, result.Content)
|
||||
}
|
||||
@@ -205,10 +210,18 @@ func (e *AgentEngine) streamThinkingToEventBus(
|
||||
|
||||
fullContent := agenttools.StripThinkBlocks(llmResult.Content)
|
||||
|
||||
// Use actual finish_reason from LLM stream instead of hardcoding "stop".
|
||||
// Fallback to "stop" when the stream did not report a finish_reason
|
||||
// (e.g., certain Ollama models or providers that omit the field).
|
||||
finishReason := llmResult.FinishReason
|
||||
if finishReason == "" {
|
||||
finishReason = "stop"
|
||||
}
|
||||
|
||||
resp := &types.ChatResponse{
|
||||
Content: fullContent,
|
||||
ToolCalls: llmResult.ToolCalls,
|
||||
FinishReason: "stop",
|
||||
FinishReason: finishReason,
|
||||
}
|
||||
if llmResult.Usage != nil {
|
||||
resp.Usage = *llmResult.Usage
|
||||
|
||||
Reference in New Issue
Block a user