refactor(agent): remove final_answer tool references and update related logic
- Eliminated all instances of the final_answer tool from the agent's configuration and logic, transitioning to a model where the agent concludes responses with plain text instead. - Updated event handling and response analysis to reflect this change, ensuring that the agent's output is now directly delivered as text without relying on a dedicated final answer tool. - Adjusted localization strings across multiple languages to remove references to final_answer, enhancing clarity and consistency in user-facing messages. - Improved the overall structure of the agent's response handling to streamline the process and reduce potential confusion regarding tool usage. refactor(agent): streamline answer handling and preamble logic - Updated the agent's response handling to treat any text streamed before a tool call as a preamble, ensuring it is not included in the final answer. - Removed the superseded marker logic from the event emission process, simplifying the flow of answer content and enhancing clarity in the UI. - Adjusted the event handling to ensure that preamble segments are correctly retracted and relocated into the steps tree, improving the overall user experience. - Enhanced the agent's internal logic to better manage answer segments and their states during streaming, leading to a more coherent response structure.
This commit is contained in:
@@ -9,7 +9,6 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
|
||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
@@ -193,15 +192,20 @@ func buildAssistantHistoryMessages(m *types.Message) []chat.Message {
|
||||
return msgs
|
||||
}
|
||||
|
||||
// filterNonTerminalToolCalls drops final_answer entries since those are
|
||||
// terminal signals — the canonical answer text is replayed via the trailing
|
||||
// assistant message instead, so re-injecting final_answer here would either
|
||||
// duplicate the answer or confuse the model into thinking the previous turn
|
||||
// is still mid-flight.
|
||||
// legacyFinalAnswerToolName is the name of the now-removed final_answer tool.
|
||||
// It is retained here only to filter such calls out of OLD persisted agent
|
||||
// histories: pre-existing conversations recorded a final_answer tool call as
|
||||
// the terminal step, and the canonical answer text is replayed via the
|
||||
// trailing assistant message instead. Re-injecting it would duplicate the
|
||||
// answer or confuse the model into thinking the previous turn is mid-flight.
|
||||
const legacyFinalAnswerToolName = "final_answer"
|
||||
|
||||
// filterNonTerminalToolCalls drops legacy final_answer entries from historical
|
||||
// tool calls (see legacyFinalAnswerToolName). New turns never produce them.
|
||||
func filterNonTerminalToolCalls(calls []types.ToolCall) []types.ToolCall {
|
||||
out := make([]types.ToolCall, 0, len(calls))
|
||||
for _, tc := range calls {
|
||||
if tc.Name == agenttools.ToolFinalAnswer {
|
||||
if tc.Name == legacyFinalAnswerToolName {
|
||||
continue
|
||||
}
|
||||
out = append(out, tc)
|
||||
|
||||
@@ -148,8 +148,10 @@ func TestBuildAssistantHistoryMessages_ToolCallsExpandIntoOpenAIShape(t *testing
|
||||
Thought: "",
|
||||
ToolCalls: []types.ToolCall{
|
||||
{
|
||||
// Legacy persisted data: old conversations recorded a
|
||||
// final_answer terminal tool call. The filter still drops it.
|
||||
ID: "call_2",
|
||||
Name: agenttools.ToolFinalAnswer,
|
||||
Name: "final_answer",
|
||||
Args: map[string]interface{}{"answer": "Found 3 matches in the docs."},
|
||||
Result: &types.ToolResult{Success: true},
|
||||
},
|
||||
@@ -216,12 +218,12 @@ func TestBuildAssistantHistoryMessages_ToolFailureSurfacesAsError(t *testing.T)
|
||||
}, got[1])
|
||||
}
|
||||
|
||||
// TestFilterNonTerminalToolCalls confirms only final_answer is dropped — every
|
||||
// other tool (KB search, web search, MCP tools…) must survive the filter.
|
||||
// TestFilterNonTerminalToolCalls confirms a legacy final_answer entry is
|
||||
// dropped — every other tool (KB search, web search, MCP tools…) must survive.
|
||||
func TestFilterNonTerminalToolCalls(t *testing.T) {
|
||||
in := []types.ToolCall{
|
||||
{Name: agenttools.ToolKnowledgeSearch},
|
||||
{Name: agenttools.ToolFinalAnswer},
|
||||
{Name: "final_answer"},
|
||||
{Name: agenttools.ToolWebSearch},
|
||||
}
|
||||
out := filterNonTerminalToolCalls(in)
|
||||
|
||||
@@ -503,8 +503,7 @@ func (s *agentService) registerTools(
|
||||
// Deduplicate while preserving original order.
|
||||
allowedTools = dedupStrings(allowedTools)
|
||||
|
||||
logger.Infof(ctx, "Registering tools: %v, webSearchEnabled: %v", allowedTools, config.WebSearchEnabled)
|
||||
allowedTools = append(allowedTools, tools.ToolFinalAnswer)
|
||||
// logger.Infof(ctx, "Registering tools: %v, webSearchEnabled: %v", allowedTools, config.WebSearchEnabled)
|
||||
// Register each allowed tool
|
||||
for _, toolName := range allowedTools {
|
||||
var toolToRegister types.Tool
|
||||
@@ -559,10 +558,6 @@ func (s *agentService) registerTools(
|
||||
toolToRegister = tools.NewDataSchemaTool(s.knowledgeService, s.chunkService.GetRepository())
|
||||
logger.Infof(ctx, "Registered data_schema tool")
|
||||
|
||||
case tools.ToolFinalAnswer:
|
||||
toolToRegister = tools.NewFinalAnswerTool()
|
||||
logger.Infof(ctx, "Registered final_answer tool")
|
||||
|
||||
// Wiki tools — only registered when wiki KBs are detected
|
||||
case tools.ToolWikiReadPage:
|
||||
toolToRegister = tools.NewWikiReadPageTool(s.wikiPageService, wikiScopes)
|
||||
|
||||
Reference in New Issue
Block a user