Files
dobotai-platform/internal/reasoningmode/answer_split.go
T
姚章浩 a1a2aabab4 fix(dingtalk): 推理正文边界支持「首行即正文」并补齐卡片结构诊断
- BodyStartOffset:内容首个非空行即结构行时正文从头开始,避免收尾裁剪
  把标题+首段一起删掉(118 条生产样本中 31 条、最多 526 字)
- 语料回归守卫:隐藏前缀不得含结构行、上限 600 字;单测补 suffix 断言
- 卡片诊断新增 body_ok/body_line/body_prefix_runes/banner_pos/first_heading_line
  (定位流式残留推理末句,只看结构不看文本)
2026-09-17 14:01:10 +08:00

117 lines
3.9 KiB
Go

package reasoningmode
import (
"regexp"
"strings"
)
// Reasoning agent runs report every intermediate step through WeKnora's
// response_type=answer channel: the step narratives are concatenated without
// any separator and the final answer body is appended to that same
// accumulation. A card update therefore shows one run-on "narration + body"
// blob. These helpers locate the boundary so callers can hide the narration
// and keep the body only.
//
// Calibration against 118 production answers of the built-in smart-reasoning
// agent: 106 had a structural first body line (97 heading, 4 bold-only line,
// 3 table row, 2 blockquote) and every one of them was directly preceded by a
// blank line; the remaining 12 answers were greetings without narration. The
// hidden narration was a short lead-in (median 111 chars, max 526 chars).
var (
answerHeadingLineRE = regexp.MustCompile(`^#{1,6}[\t ]+\S`)
answerBoldLineRE = regexp.MustCompile(`^\*\*[^*\n]+\*\*[\t ]*$`)
answerTableRowRE = regexp.MustCompile(`^\|.*\|[\t ]*$`)
answerQuoteLineRE = regexp.MustCompile(`^>[\t ]*\S`)
answerFenceLineRE = regexp.MustCompile("^(`{3,}|~{3,})")
)
// IsReasoningExecution reports whether execution runs the multi-step reasoning
// agent whose answer stream mixes step narration with the final answer body.
func IsReasoningExecution(execution string) bool {
switch ExecutionMode(strings.TrimSpace(execution)) {
case ExecutionReasoning, ExecutionReasoningSingle:
return true
}
return false
}
// SplitReasoningAnswer returns the final answer body. ok is false when the
// content carries no body boundary yet (nothing must be hidden in that case).
func SplitReasoningAnswer(content string) (string, bool) {
offset, ok := BodyStartOffset(content)
if !ok {
return content, false
}
return content[offset:], true
}
// TrimReasoningNarration drops the step narration that precedes the final
// answer body. Content without a body boundary is returned unchanged.
func TrimReasoningNarration(content string) string {
body, ok := SplitReasoningAnswer(content)
if !ok {
return content
}
return body
}
// BodyStartOffset returns the byte offset where the final answer body starts.
// A line qualifies as the body start when it is a structural Markdown line
// (heading, bold-only line, table row or blockquote) and is not inside a
// fenced code block, either directly following a blank line or being the very
// first non-blank line of the content.
//
// The first-line rule matters: 31 of 118 production reasoning answers open
// directly with their body ("# 标题" first). Without it the scan would skip
// that opening line and fire on the *next* structural line, so finalize
// deleted the answer's title plus its opening paragraph (up to 526 chars) while
// the streaming feed, whose display inserts blank lines between steps, kept
// them - mid-stream and finalize text disagreed.
func BodyStartOffset(content string) (int, bool) {
lines := strings.Split(content, "\n")
fence := ""
offset := 0
previousBlank := false
seenContent := false
for i, raw := range lines {
trimmed := strings.TrimSpace(raw)
if marker := answerFenceLineRE.FindString(trimmed); marker != "" &&
(fence == "" || strings.HasPrefix(trimmed, fence)) {
if fence == "" {
fence = marker
} else {
fence = ""
}
seenContent = true
previousBlank = false
offset += len(raw) + 1
continue
}
if fence == "" {
if !seenContent && isAnswerBodyLine(trimmed) {
return offset, true
}
if i > 0 && previousBlank && isAnswerBodyLine(trimmed) {
return offset, true
}
}
if trimmed != "" {
seenContent = true
}
previousBlank = trimmed == ""
offset += len(raw) + 1
}
return 0, false
}
func isAnswerBodyLine(trimmed string) bool {
if trimmed == "" {
return false
}
return answerHeadingLineRE.MatchString(trimmed) ||
answerBoldLineRE.MatchString(trimmed) ||
answerTableRowRE.MatchString(trimmed) ||
answerQuoteLineRE.MatchString(trimmed)
}