Files
dobotai-platform/internal/reasoningmode/answer_observation.go
T

91 lines
3.6 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
}
// A single answer run carries the whole answer: the agent published no
// separate step narration, so there is nothing that can be cut safely.
if o.AnswerRuns <= 1 {
return 0, true
}
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
}
// An image reference in front of the boundary is answer content (the
// agent published a picture in its lead-in), never step narration, so
// the safe answer is to keep everything.
if strings.Contains(content[:offset], "![") {
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
}