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) }