Files
dobotai-platform/internal/reasoningmode/answer_split.go
T
姚章浩 15f2a11162 fix(dingtalk): 推理模式卡片隐藏推理并只留正文
- reasoningmode: 新增正文边界识别(结构行 + 前置空行)与流式 PartialFeed:
  分步叙述按行拼接显示,正文开始时只输出正文,卡片更新即刷新清掉推理区
- dingtalk: 事件流与流式回调统一改走 PartialFeed
- qa: 推理执行的最终答案在入库/投递前去掉步骤叙述(快速模式行为不变)

语料回归:生产 118 条内建智能推理答案中 106 条识别出正文边界(被隐藏的
均为 59-164 字过渡句),12 条无推理的问候类答案保持原样;go vet 与
go test ./... 全绿。
2026-09-17 10:58:59 +08:00

99 lines
3.2 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 only when it is a structural Markdown
// line (heading, bold-only line, table row or blockquote), directly follows a
// blank line and is not inside a fenced code block.
func BodyStartOffset(content string) (int, bool) {
lines := strings.Split(content, "\n")
fence := ""
offset := 0
previousBlank := 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 = ""
}
previousBlank = false
offset += len(raw) + 1
continue
}
if i > 0 && fence == "" && previousBlank && isAnswerBodyLine(trimmed) {
return offset, 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)
}