fix(outbound): keep single-run answers whole and show formulas
The DingTalk binding can point at the quick-answer agent, which publishes the whole answer as one answer run plus tool_call/tool_result steps. The narration boundary was rejected for that shape (start=0), so the text-only heuristic ran and dropped the answer lead-in together with its image. - answer_observation: a single answer run means there is no narration to cut. - outboundtext: rewrite LaTeX formulas into plain Unicode text, because DingTalk cards render no math typesetting (r = sqrt(X^2 + Y^2) -> r = √(X² + Y²)). - dingtalk handler: normalize formulas on both the partial and final card paths.
This commit is contained in:
@@ -0,0 +1,107 @@
|
||||
package outboundtext
|
||||
|
||||
import "strings"
|
||||
|
||||
func NormalizeMath(markdown string) string {
|
||||
if !carriesMath(markdown) {
|
||||
return markdown
|
||||
}
|
||||
content := markdown
|
||||
for from, to := range escapedPunctuation {
|
||||
content = strings.ReplaceAll(content, from, to)
|
||||
}
|
||||
content = mathBlockDelimRE.ReplaceAllString(content, "\n$1\n")
|
||||
content = parenDelimRE.ReplaceAllString(content, "$1")
|
||||
content = bracketDelimRE.ReplaceAllString(content, "$1")
|
||||
content = mathInlineDelimRE.ReplaceAllString(content, "$1")
|
||||
for i := 0; i < 3; i++ {
|
||||
next := sqrtRE.ReplaceAllStringFunc(content, expandRoot)
|
||||
next = fracRE.ReplaceAllString(next, "($1)/($2)")
|
||||
next = supBracedRE.ReplaceAllStringFunc(next, convertSuperscript)
|
||||
next = subBracedRE.ReplaceAllStringFunc(next, convertSubscript)
|
||||
next = supCharRE.ReplaceAllStringFunc(next, convertSuperscript)
|
||||
next = subCharRE.ReplaceAllStringFunc(next, convertSubscript)
|
||||
if next == content {
|
||||
break
|
||||
}
|
||||
content = next
|
||||
}
|
||||
content = commandRE.ReplaceAllStringFunc(content, replaceCommand)
|
||||
content = braceDelimRE.ReplaceAllString(content, "$1")
|
||||
content = blankRunRE.ReplaceAllString(content, " ")
|
||||
return content
|
||||
}
|
||||
|
||||
func carriesMath(markdown string) bool {
|
||||
if !strings.Contains(markdown, "$") && !strings.Contains(markdown, `\(`) &&
|
||||
!strings.Contains(markdown, `\[`) && !strings.Contains(markdown, `\sqrt`) &&
|
||||
!strings.Contains(markdown, `\frac`) && !strings.Contains(markdown, `\times`) &&
|
||||
!strings.Contains(markdown, `\times`) {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func expandRoot(match string) string {
|
||||
parts := sqrtRE.FindStringSubmatch(match)
|
||||
degree, radicand := strings.TrimSpace(parts[1]), strings.TrimSpace(parts[2])
|
||||
if degree != "" {
|
||||
return "√[" + degree + "](" + radicand + ")"
|
||||
}
|
||||
return "√(" + radicand + ")"
|
||||
}
|
||||
|
||||
func convertSuperscript(match string) string {
|
||||
body := strings.TrimPrefix(match, "^")
|
||||
body = strings.TrimSuffix(strings.TrimPrefix(body, "{"), "}")
|
||||
if mapped, ok := mapRun(body, superscripts); ok {
|
||||
return mapped
|
||||
}
|
||||
return "^(" + body + ")"
|
||||
}
|
||||
|
||||
func convertSubscript(match string) string {
|
||||
body := strings.TrimPrefix(match, "_")
|
||||
body = strings.TrimSuffix(strings.TrimPrefix(body, "{"), "}")
|
||||
if mapped, ok := mapRun(body, subscripts); ok {
|
||||
return mapped
|
||||
}
|
||||
return "_(" + body + ")"
|
||||
}
|
||||
|
||||
func mapRun(body string, table map[rune]string) (string, bool) {
|
||||
if body == "" {
|
||||
return "", false
|
||||
}
|
||||
var b strings.Builder
|
||||
for _, r := range body {
|
||||
mapped, ok := table[r]
|
||||
if !ok {
|
||||
return "", false
|
||||
}
|
||||
b.WriteString(mapped)
|
||||
}
|
||||
return b.String(), true
|
||||
}
|
||||
|
||||
func replaceCommand(match string) string {
|
||||
name := strings.TrimPrefix(match, `\`)
|
||||
if mapped, ok := mathCommands[name]; ok {
|
||||
return mapped
|
||||
}
|
||||
return name
|
||||
}
|
||||
|
||||
var superscripts = map[rune]string{
|
||||
'0': "⁰", '1': "¹", '2': "²", '3': "³", '4': "⁴", '5': "⁵", '6': "⁶",
|
||||
'7': "⁷", '8': "⁸", '9': "⁹", '+': "⁺", '-': "⁻", '=': "⁼",
|
||||
'(': "⁽", ')': "⁾", 'n': "ⁿ", 'i': "ⁱ", 'T': "ᵀ",
|
||||
}
|
||||
|
||||
var subscripts = map[rune]string{
|
||||
'0': "₀", '1': "₁", '2': "₂", '3': "₃", '4': "₄", '5': "₅", '6': "₆",
|
||||
'7': "₇", '8': "₈", '9': "₉", '+': "₊", '-': "₋", '=': "₌",
|
||||
'(': "₍", ')': "₎", 'a': "ₐ", 'e': "ₑ", 'i': "ᵢ", 'j': "ⱼ", 'k': "ₖ",
|
||||
'n': "ₙ", 'o': "ₒ", 'p': "ₚ", 'r': "ᵣ", 's': "ₛ", 't': "ₜ", 'u': "ᵤ",
|
||||
'v': "ᵥ", 'x': "ₓ",
|
||||
}
|
||||
Reference in New Issue
Block a user