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:
@@ -12,98 +12,11 @@ import (
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// newFinalAnswerResponse builds a ChatResponse that carries a single
|
||||
// final_answer tool call with the given raw JSON arguments.
|
||||
func newFinalAnswerResponse(rawArgs string) *types.ChatResponse {
|
||||
return &types.ChatResponse{
|
||||
FinishReason: "tool_calls",
|
||||
ToolCalls: []types.LLMToolCall{
|
||||
{
|
||||
ID: "call-1",
|
||||
Type: "function",
|
||||
Function: types.FunctionCall{
|
||||
Name: agenttools.ToolFinalAnswer,
|
||||
Arguments: rawArgs,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
// TestAnalyzeResponse_FinalAnswer_ValidArgs guards the happy path: well-formed
|
||||
// arguments must be extracted into the final answer and terminate the loop.
|
||||
func TestAnalyzeResponse_FinalAnswer_ValidArgs(t *testing.T) {
|
||||
engine := newTestEngine(t, &mockChat{})
|
||||
resp := newFinalAnswerResponse(`{"answer": "Here is the answer."}`)
|
||||
|
||||
verdict := engine.analyzeResponse(
|
||||
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
|
||||
)
|
||||
|
||||
assert.True(t, verdict.isDone, "final_answer must terminate the loop")
|
||||
assert.Equal(t, "Here is the answer.", verdict.finalAnswer)
|
||||
}
|
||||
|
||||
// TestAnalyzeResponse_FinalAnswer_MalformedJSON_RecoveredViaRepair covers the
|
||||
// common case reported in issue #1008: the LLM emits final_answer with a
|
||||
// trailing comma / missing brace. RepairJSON should recover the answer and
|
||||
// the loop must still terminate in this single round (not re-invoke
|
||||
// final_answer in the next round).
|
||||
func TestAnalyzeResponse_FinalAnswer_MalformedJSON_RecoveredViaRepair(t *testing.T) {
|
||||
engine := newTestEngine(t, &mockChat{})
|
||||
resp := newFinalAnswerResponse(`{"answer": "repaired"`) // missing closing brace
|
||||
|
||||
verdict := engine.analyzeResponse(
|
||||
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
|
||||
)
|
||||
|
||||
assert.True(t, verdict.isDone,
|
||||
"final_answer must terminate the loop even when JSON repair is needed")
|
||||
assert.Equal(t, "repaired", verdict.finalAnswer)
|
||||
}
|
||||
|
||||
// TestAnalyzeResponse_FinalAnswer_UnrecoverableArgs_StillTerminates is the
|
||||
// direct regression test for issue #1008: when the arguments are so malformed
|
||||
// that even RepairJSON + regex cannot recover an answer, the loop MUST still
|
||||
// terminate (with a user-visible fallback message) rather than continuing and
|
||||
// letting the LLM re-emit final_answer on the next round.
|
||||
func TestAnalyzeResponse_FinalAnswer_UnrecoverableArgs_StillTerminates(t *testing.T) {
|
||||
engine := newTestEngine(t, &mockChat{})
|
||||
// No `answer` key at all — strict parse succeeds (returns zero-value
|
||||
// answer), RepairJSON is a no-op on already-valid JSON, regex finds
|
||||
// nothing. All three tiers fail to recover an answer.
|
||||
resp := newFinalAnswerResponse(`{"unexpected": "field"}`)
|
||||
|
||||
verdict := engine.analyzeResponse(
|
||||
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
|
||||
)
|
||||
|
||||
assert.True(t, verdict.isDone,
|
||||
"final_answer must terminate the loop even when args are unrecoverable — "+
|
||||
"otherwise the LLM re-emits final_answer and duplicates the answer (issue #1008)")
|
||||
assert.Equal(t, finalAnswerParseFallback, verdict.finalAnswer,
|
||||
"unrecoverable final_answer should surface the parse-failure fallback message")
|
||||
}
|
||||
|
||||
// TestAnalyzeResponse_FinalAnswer_Garbage_StillTerminates exercises the most
|
||||
// hostile case: completely non-JSON arguments. The loop must still terminate
|
||||
// — protecting against the duplicate-answer loop reported in issue #1008.
|
||||
func TestAnalyzeResponse_FinalAnswer_Garbage_StillTerminates(t *testing.T) {
|
||||
engine := newTestEngine(t, &mockChat{})
|
||||
resp := newFinalAnswerResponse(`not json at all`)
|
||||
|
||||
verdict := engine.analyzeResponse(
|
||||
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
|
||||
)
|
||||
|
||||
assert.True(t, verdict.isDone)
|
||||
assert.Equal(t, finalAnswerParseFallback, verdict.finalAnswer)
|
||||
}
|
||||
|
||||
// TestAnalyzeResponse_NonFinalAnswerTool_DoesNotTerminate is a regression
|
||||
// guard: only final_answer is terminal. Other tool calls (e.g. thinking,
|
||||
// knowledge_search) must keep the loop running.
|
||||
func TestAnalyzeResponse_NonFinalAnswerTool_DoesNotTerminate(t *testing.T) {
|
||||
// TestAnalyzeResponse_ToolCall_DoesNotTerminate is a regression guard: the
|
||||
// agent has no dedicated terminal tool — any round that requests tool calls is
|
||||
// non-terminal and must keep the loop running. The agent ends only by stopping
|
||||
// naturally with its answer as plain text.
|
||||
func TestAnalyzeResponse_ToolCall_DoesNotTerminate(t *testing.T) {
|
||||
engine := newTestEngine(t, &mockChat{})
|
||||
resp := &types.ChatResponse{
|
||||
FinishReason: "tool_calls",
|
||||
@@ -127,6 +40,24 @@ func TestAnalyzeResponse_NonFinalAnswerTool_DoesNotTerminate(t *testing.T) {
|
||||
"non-terminal tool calls must keep the loop running")
|
||||
}
|
||||
|
||||
// TestAnalyzeResponse_NaturalStop_Terminates guards the sole termination path:
|
||||
// finish_reason == "stop" with no tool calls ends the loop and surfaces the
|
||||
// plain content as the final answer.
|
||||
func TestAnalyzeResponse_NaturalStop_Terminates(t *testing.T) {
|
||||
engine := newTestEngine(t, &mockChat{})
|
||||
resp := &types.ChatResponse{
|
||||
FinishReason: "stop",
|
||||
Content: "Here is the answer.",
|
||||
}
|
||||
|
||||
verdict := engine.analyzeResponse(
|
||||
context.Background(), resp, types.AgentStep{}, 0, "sess-1", time.Now(),
|
||||
)
|
||||
|
||||
assert.True(t, verdict.isDone, "a natural stop with no tool calls must terminate the loop")
|
||||
assert.Equal(t, "Here is the answer.", verdict.finalAnswer)
|
||||
}
|
||||
|
||||
// TestAppendToolResults_PreservesReasoningContent verifies that the assistant
|
||||
// message produced by appendToolResults carries the reasoning_content emitted
|
||||
// by the model in the same round. Without this, MiMo and DeepSeek V3.2+
|
||||
|
||||
Reference in New Issue
Block a user