Files
dobotai-platform/internal/reasoningmode/answer_observation.go
T
姚章浩 149e94b34b fix(reasoning): keep the answer body's lead-in and images when trimming narration
The DingTalk card dropped the answer's opening (and any image reference in
it) whenever the agent narrated its steps before answering: the text-only
boundary heuristic (reasoningmode.BodyStartOffset) treats everything before
the first structural line as narration, so a lead-in like "我已经找到了…"
plus the image that follows it disappeared, leaving the card to start at
the first heading.

The event stream already knows the boundary: every agent step publishes its
answer chunks under its own answer run id (data.event_id), and the final
body is the last run that no tool/thinking event interrupts. WeKnora
StreamEvent now carries the run id and run start offset; qa.Service tracks
the observation and prefers it over the heuristic, falling back to the
heuristic when the stream carries no run information.

Verified: production turn efae317e (给我CRA5的负载图) lost 131 chars —
lead-in + the load-curve image + separator — and now keeps them.
2026-09-18 15:53:43 +08:00

80 lines
3.2 KiB
Go

package reasoningmode
import "strings"
// NarrationObservation carries what the WeKnora event stream knows about the
// boundary between an agent step's narration and the final answer body.
//
// The text-only heuristic (BodyStartOffset) cannot tell a step narrative from
// an answer lead-in such as "我已经找到了 CRA5 的末端负载曲线图…下面给出完整回答。":
// both are plain prose, so the heuristic starts the body at the first
// structural line and silently drops everything before it — including answer
// prose, image references and table rows that sit in that lead-in.
//
// The stream knows the boundary: every agent step writes its narrative to the
// answer channel as its own answer run (WeKnora's data.event_id), and the
// final answer body is the last run that no tool/thinking event interrupts.
type NarrationObservation struct {
// Observed reports that the stream carried answer run information at all
// (WeKnora tags answer chunks with data.event_id). Without it the text
// heuristic stays in charge, so older deployments keep their behaviour.
Observed bool
// AnswerRuns counts distinct answer runs (consecutive chunks sharing one
// event id count as one run).
AnswerRuns int
// ToolEvents counts thinking / tool_call / tool_result events.
ToolEvents int
// LastRunStart is the byte offset in the accumulated answer where the
// most recent answer run started.
LastRunStart int
// LastRunClean reports that no tool/thinking event arrived after that run
// started, which makes the run the final answer body rather than another
// narrative step.
LastRunClean bool
}
// BodyStartOffset returns the observed body start and whether the observation
// is usable. An answer with no tool event carries no narration at all, so the
// whole answer is the body.
func (o NarrationObservation) BodyStartOffset(contentLength int) (int, bool) {
if !o.Observed {
return 0, false
}
if o.ToolEvents == 0 {
return 0, true
}
if o.LastRunClean && o.LastRunStart > 0 && o.LastRunStart < contentLength {
return o.LastRunStart, true
}
return 0, false
}
// SplitReasoningAnswerObserved returns the final answer body using the stream
// observation when it is usable and the text heuristic otherwise. ok is false
// when the content carries no body boundary (nothing must be hidden).
func SplitReasoningAnswerObserved(content string, observation NarrationObservation) (string, bool) {
if offset, ok := observation.BodyStartOffset(len(content)); ok {
if offset <= 0 {
// The stream showed no narration at all: the whole answer is body.
return content, true
}
body := strings.TrimLeft(content[offset:], "\r\n")
if strings.TrimSpace(body) != "" {
return body, true
}
}
return SplitReasoningAnswer(content)
}
// TrimReasoningNarrationObserved drops the step narration using the stream
// observation, falling back to the text heuristic when the stream carried no
// usable boundary. Unlike the heuristic it never drops the answer lead-in or
// an image reference that belongs to the body.
func TrimReasoningNarrationObserved(content string, observation NarrationObservation) string {
body, ok := SplitReasoningAnswerObserved(content, observation)
if !ok {
return content
}
return body
}