package agent import ( "context" "time" agenttools "github.com/Tencent/WeKnora/internal/agent/tools" "github.com/Tencent/WeKnora/internal/common" "github.com/Tencent/WeKnora/internal/event" "github.com/Tencent/WeKnora/internal/logger" "github.com/Tencent/WeKnora/internal/models/chat" "github.com/Tencent/WeKnora/internal/types" ) // streamFinalAnswerToEventBus streams the final answer generation through EventBus func (e *AgentEngine) streamFinalAnswerToEventBus( ctx context.Context, query string, state *types.AgentState, sessionID string, conversation []chat.Message, ) error { totalToolCalls := countTotalToolCalls(state.RoundSteps) logger.Infof(ctx, "[Agent][FinalAnswer] Synthesizing from %d steps, %d tool calls", len(state.RoundSteps), totalToolCalls) common.PipelineInfo(ctx, "Agent", "final_answer_start", map[string]interface{}{ "session_id": sessionID, "query": query, "steps": len(state.RoundSteps), "tool_results": totalToolCalls, }) // Reuse the live transcript, including history, images, compaction and steer // messages. Tool output must retain its role and call ID; never promote it // into user instructions during error recovery or iteration-limit synthesis. messages := append([]chat.Message(nil), conversation...) runtimeNotice := "{\"source\":\"runtime\",\"type\":\"final_synthesis\",\"message\":\"Tool " + "execution has ended for this run. Respond to the current task, including the " + "latest user corrections and source restrictions in the conversation. Base " + "claims on the evidence actually obtained; distinguish completed work from " + "remaining work and explain any missing evidence. Use the user's requested " + "language and format. Do not claim that an unperformed action " + "succeeded.\"}" // Generate a single ID for this entire final answer stream answerID := generateEventID("answer") logger.Debugf(ctx, "[Agent][FinalAnswer] AnswerID: %s", answerID) answerDoneEmitted := false splitter := agenttools.NewThinkStreamSplitter() budget := e.clampCompletionBudgetToContext( e.tokenEstimator.EstimateMessages( append(append([]chat.Message(nil), messages...), chat.Message{Role: "system", Content: runtimeNotice}), ), ) llmResult, err := e.streamLLMToEventBus( ctx, messages, &chat.ChatOptions{ RuntimeNotice: runtimeNotice, Temperature: e.config.Temperature, MaxCompletionTokens: budget, PromptCacheKey: sessionID, ToolChoice: "none", }, // Thinking disabled for final answer synthesis func(chunk *types.StreamResponse, fullContent string) { // Defensive filter: only emit answer content, skip thinking chunks if chunk.ResponseType == types.ResponseTypeThinking { return } _, answerContent := splitter.Feed(chunk.Content) if splitter.TakeAnswerReset() { _ = e.eventBus.Emit( ctx, event.Event{ ID: answerID, Type: event.EventAgentFinalAnswer, SessionID: sessionID, Data: event.AgentFinalAnswerData{Replace: true}, }, ) } if chunk.Done { _, tail := splitter.Flush() answerContent += tail } if answerContent != "" { logger.Debugf(ctx, "[Agent][FinalAnswer] Emitting answer chunk: %d chars", len(answerContent)) e.eventBus.Emit(ctx, event.Event{ ID: answerID, Type: event.EventAgentFinalAnswer, SessionID: sessionID, Data: event.AgentFinalAnswerData{ Content: answerContent, Done: chunk.Done, }, }) if chunk.Done { answerDoneEmitted = true } } }, ) if err != nil { logger.Errorf(ctx, "[Agent][FinalAnswer] Final answer generation failed: %v", err) common.PipelineError(ctx, "Agent", "final_answer_stream_failed", map[string]interface{}{ "session_id": sessionID, "error": err.Error(), }) return err } if _, tail := splitter.Flush(); tail != "" { _ = e.eventBus.Emit(ctx, event.Event{ ID: answerID, Type: event.EventAgentFinalAnswer, SessionID: sessionID, Data: event.AgentFinalAnswerData{Content: tail}, }) } if !answerDoneEmitted { e.eventBus.Emit(ctx, event.Event{ ID: answerID, Type: event.EventAgentFinalAnswer, SessionID: sessionID, Data: event.AgentFinalAnswerData{ Content: "", Done: true, }, }) } // The synthesis call is often the largest of the turn — fold its usage // into the turn aggregate like every ReAct round. if llmResult.Usage != nil { state.TurnUsage.Accumulate(*llmResult.Usage) } // Safety net: strip any residual blocks that may have leaked through fullAnswer := agenttools.StripThinkBlocks(llmResult.Content) logger.Infof(ctx, "[Agent][FinalAnswer] Final answer generated: %d characters", len(fullAnswer)) common.PipelineInfo(ctx, "Agent", "final_answer_done", map[string]interface{}{ "session_id": sessionID, "answer_len": len(fullAnswer), }) // Delegated callers must distinguish a synthesized partial answer from a // completed deliverable too, including the iteration-budget fallback path. if e.collaboration != nil && e.collaboration.Child != nil && isLengthFinishReason(llmResult.FinishReason) { state.RoundSteps = append(state.RoundSteps, types.AgentStep{ Iteration: state.CurrentRound, Thought: fullAnswer, Truncated: true, Timestamp: time.Now(), }) } state.FinalAnswer = fullAnswer return nil } // handleMaxIterations generates a final answer when the agent loop exhausted all iterations // without the LLM producing a natural stop. It marks state.IsComplete = true. func (e *AgentEngine) handleMaxIterations( ctx context.Context, query string, state *types.AgentState, sessionID string, messages []chat.Message, ) { logger.Info(ctx, "Reached max iterations, generating final answer") common.PipelineWarn(ctx, "Agent", "max_iterations_reached", map[string]interface{}{ "iterations": state.CurrentRound, "max": e.config.MaxIterations, }) // Stream final answer generation through EventBus if err := e.streamFinalAnswerToEventBus(ctx, query, state, sessionID, messages); err != nil { logger.Errorf(ctx, "Failed to synthesize final answer: %v", err) common.PipelineError(ctx, "Agent", "final_answer_failed", map[string]interface{}{ "error": err.Error(), }) state.FinalAnswer = "Sorry, I was unable to generate a complete answer." } state.IsComplete = true } // emitCompletionEvent emits the EventAgentComplete event with execution summary. func (e *AgentEngine) emitCompletionEvent( ctx context.Context, state *types.AgentState, sessionID, messageID string, startTime time.Time, ) { steps := state.RoundSteps if state.SuspendedStep != nil { steps = append(append([]types.AgentStep(nil), steps...), *state.SuspendedStep) } if len(state.PendingSteerMessages) > 0 { // A stop or model failure can arrive after delivery but before the next // response exists. Preserve that boundary without inventing an answer. steps = append(append([]types.AgentStep(nil), steps...), types.AgentStep{ Iteration: state.CurrentRound, UserMessagesBefore: state.PendingSteerMessages, }) } // Convert knowledge refs to interface{} slice for event data knowledgeRefsInterface := make([]interface{}, 0, len(state.KnowledgeRefs)) for _, ref := range state.KnowledgeRefs { knowledgeRefsInterface = append(knowledgeRefsInterface, ref) } data := event.AgentCompleteData{ Artifacts: e.collaborationArtifacts(), FinalAnswer: state.FinalAnswer, KnowledgeRefs: knowledgeRefsInterface, AgentSteps: steps, Usage: turnUsage(state), TotalSteps: len(state.RoundSteps), TotalDurationMs: time.Since(startTime).Milliseconds(), MessageID: messageID, // Include message ID for proper message update } // The turn's usage is aggregated only from calls made through this chat // model (each round and the final answer), so it is the model the usage // belongs to. The message row keeps the requested binding, which can differ // when a request override falls back to the agent's model. if e.chatModel != nil { data.ModelID = e.chatModel.GetModelID() } if state.Suspended != nil { data.Suspended = state.Suspended } e.eventBus.Emit(ctx, event.Event{ ID: generateEventID("complete"), Type: event.EventAgentComplete, SessionID: sessionID, Data: data, }) logger.Infof(ctx, "Agent execution completed in %d rounds", state.CurrentRound) } // turnUsage returns the turn's aggregated LLM usage, or nil when no round // reported usage so the field stays absent from the completion event and the // persisted message alike. func turnUsage(state *types.AgentState) *types.TokenUsage { // A token scale is worth persisting on its own: the next turn's history // loading is calibrated by it even when this provider reported no total. if state == nil || (state.TurnUsage.TotalTokens == 0 && state.TurnUsage.ContextTokenScale <= 0) { return nil } usage := state.TurnUsage return &usage }