package reasoningmode import ( "strings" "testing" ) // The production case this observation fixes: the agent narrates its steps, // then the final answer opens with a lead-in ("我已经找到了…下面给出完整回答。"), // carries the requested load-curve image, and only then reaches its first // heading. The text heuristic started the body at the heading and dropped the // lead-in plus the image reference with it. const craLoadCurveAnswer = "我需要先确认 CRA5 与 CR5A 是同一型号,然后检索负载曲线。\n" + "知识库检索命中 3 篇文档,其中用户手册含末端负载曲线图。\n" + "我已经找到了 CRA5(CR5A)的末端负载曲线图,并确认了负载曲线的读法说明。下面给出完整回答。\n\n" + "CRA5(即 CR5A)的末端负载曲线如下:\n\n" + "![CR5A 末端负载曲线](resource://FsVe56UYMz64sm8NsUZNrg)\n\n" + "---\n\n" + "## 一、负载曲线怎么读\n\n" + "负载曲线反映的是:末端负载能力与负载质心偏心距离成负相关。\n" func TestNarrationObservationKeepsAnswerLeadInAndImage(t *testing.T) { narration := strings.Index(craLoadCurveAnswer, "我已经找到了") observation := NarrationObservation{ Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: narration, LastRunClean: true, } body := TrimReasoningNarrationObserved(craLoadCurveAnswer, observation) if !strings.HasPrefix(body, "我已经找到了") { t.Fatalf("body must start at the answer lead-in, got %q", firstLine(body)) } if !strings.Contains(body, "![CR5A 末端负载曲线](resource://FsVe56UYMz64sm8NsUZNrg)") { t.Fatalf("body must keep the image reference, got %q", firstLine(body)) } if strings.Contains(body, "我需要先确认") { t.Fatalf("body must not contain the step narration, got %q", firstLine(body)) } // The text-only heuristic is what dropped the lead-in and the image. heuristic := TrimReasoningNarration(craLoadCurveAnswer) if !strings.HasPrefix(heuristic, "## 一、负载曲线怎么读") { t.Fatalf("heuristic is expected to start at the heading, got %q", firstLine(heuristic)) } if strings.Contains(heuristic, "resource://") { t.Fatalf("heuristic is expected to drop the image reference") } } func TestNarrationObservationFallsBackWithoutUsableBoundary(t *testing.T) { narration := strings.Index(craLoadCurveAnswer, "我已经找到了") tests := []struct { name string observation NarrationObservation }{ {"not observed", NarrationObservation{AnswerRuns: 3, ToolEvents: 5, LastRunStart: narration, LastRunClean: true}}, {"last run interrupted by a tool", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: narration}}, {"multi run starting at zero", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: 0, LastRunClean: true}}, {"run start beyond the answer", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: len(craLoadCurveAnswer) + 10, LastRunClean: true}}, } for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { got := TrimReasoningNarrationObserved(craLoadCurveAnswer, tc.observation) if want := TrimReasoningNarration(craLoadCurveAnswer); got != want { t.Fatalf("expected heuristic fallback %q, got %q", firstLine(want), firstLine(got)) } }) } } func TestNarrationObservationKeepsImageLeadIn(t *testing.T) { // The reported production shape: the lead-in carries the answer prose plus // the picture, and the boundary observed by the stream only lands after it. content := "根据知识库中《末端负载曲线-CRA系列》文档,以下是 **CR5A 的末端负载曲线图**:\n\n" + "![CR5A 末端负载曲线](resource://FsVe56UYMz64sm8NsUZNrg)\n\n" + "### 如何查看该负载曲线\n负载能力与偏心距离成负相关。\n" offset := strings.Index(content, "### 如何查看该负载曲线") observation := NarrationObservation{Observed: true, AnswerRuns: 4, ToolEvents: 6, LastRunStart: offset, LastRunClean: true} if got := TrimReasoningNarrationObserved(content, observation); got != content { t.Fatalf("image lead-in must survive the trim, got %q", got) } } func TestNarrationObservationKeepsSingleRunAnswer(t *testing.T) { // The quick-answer agent publishes the whole answer as a single answer run // (with tool_call/tool_result steps around it). Nothing in that stream is // narration, so the lead-in and the image must survive. observation := NarrationObservation{Observed: true, AnswerRuns: 1, ToolEvents: 4, LastRunStart: 0, LastRunClean: true} got := TrimReasoningNarrationObserved(craLoadCurveAnswer, observation) if got != craLoadCurveAnswer { t.Fatalf("single answer run must keep the whole answer, got %q", firstLine(got)) } if !strings.Contains(got, "resource://") { t.Fatalf("the image reference must survive for a single answer run") } } func TestNarrationObservationKeepsWholeAnswerWithoutToolSteps(t *testing.T) { answer := "本机提供以下三种接线方式:\n\n| 方式 | 说明 |\n| --- | --- |\n| A | 直连 |\n" observation := NarrationObservation{Observed: true, AnswerRuns: 1, ToolEvents: 0} body, ok := SplitReasoningAnswerObserved(answer, observation) if !ok { t.Fatalf("a run without tool steps has no narration, so the whole answer is the body") } if body != answer { t.Fatalf("expected the whole answer, got %q", body) } } func TestNarrationObservationTrimsBlankLineAtRunStart(t *testing.T) { content := "叙述步骤。\n\n\n## 标题\n\n正文。\n" observation := NarrationObservation{Observed: true, AnswerRuns: 2, ToolEvents: 1, LastRunStart: strings.Index(content, "\n\n\n##"), LastRunClean: true} body := TrimReasoningNarrationObserved(content, observation) if body != "## 标题\n\n正文。\n" { t.Fatalf("expected the blank lines before the run content to be trimmed, got %q", body) } } func firstLine(s string) string { if i := strings.IndexByte(s, '\n'); i >= 0 { return s[:i] } return s }