Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 70b144f1e5 | |||
| 1bbaf2f07a | |||
| 48b741984f | |||
| 5ccaeb3b96 | |||
| dfce8cb9f6 | |||
| 149e94b34b |
+4
-1
@@ -71,7 +71,10 @@ func main() {
|
||||
if strings.TrimSpace(settingsRuntime.clientID) == "" || strings.TrimSpace(settingsRuntime.secret) == "" {
|
||||
log.Fatal("DingTalk credentials are required; configure DINGTALK_CLIENT_ID and DINGTALK_CLIENT_SECRET in the protected runtime environment")
|
||||
}
|
||||
client, err := weknora.NewClient(os.Getenv("WEKNORA_BASE_URL"), os.Getenv("WEKNORA_API_KEY"), 120*time.Second)
|
||||
// 0 = no client-side total timeout. WeKnora's smart-reasoning agent can run
|
||||
// 20+ tool rounds and stream far longer than 120s; the total-time cap cut the
|
||||
// SSE mid-answer and the failure branch overwrote the streamed card.
|
||||
client, err := weknora.NewClient(os.Getenv("WEKNORA_BASE_URL"), os.Getenv("WEKNORA_API_KEY"), 0)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,118 @@
|
||||
// 临时探针(不入库,用完即删):用真实 WeKnora + OSS 端点跑
|
||||
// internal/outboundimage 的改写链路,量化"一条含 N 张真实图的答案"在
|
||||
// 并发取图 + 预算可配下的出图结果与耗时。
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"context"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/outboundimage"
|
||||
)
|
||||
|
||||
func main() {
|
||||
refsFile := flag.String("refs-file", "", "文件:每行一个 resource:// 句柄")
|
||||
budget := flag.Duration("budget", 180*time.Second, "改写预算")
|
||||
maxImages := flag.Int("max", 50, "图片上限")
|
||||
compress := flag.Bool("compress", true, "上传前压缩截图")
|
||||
passes := flag.Int("passes", 1, "重复改写轮数(第二轮应命中已存在对象)")
|
||||
flag.Parse()
|
||||
|
||||
refs := readRefs(*refsFile)
|
||||
if len(refs) == 0 {
|
||||
log.Fatal("probe: no references")
|
||||
}
|
||||
|
||||
fetcher, err := outboundimage.NewWeKnoraFetcher(
|
||||
os.Getenv("WEKNORA_BASE_URL"), os.Getenv("WEKNORA_API_KEY"),
|
||||
&http.Client{Timeout: 30 * time.Second}, 10<<20)
|
||||
if err != nil {
|
||||
log.Fatalf("probe: fetcher: %v", err)
|
||||
}
|
||||
store, err := outboundimage.NewAliyunOSSStore(
|
||||
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_ENDPOINT"),
|
||||
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_REGION"),
|
||||
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_ACCESS_KEY_ID"),
|
||||
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_ACCESS_KEY_SECRET"),
|
||||
os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_BUCKET"))
|
||||
if err != nil {
|
||||
log.Fatalf("probe: store: %v", err)
|
||||
}
|
||||
publicBase := os.Getenv("DOBOTAI_OUTBOUND_IMAGE_PUBLIC_BASE_URL")
|
||||
publisher, err := outboundimage.NewStableHashPublisher(store, "dobotai/im-images", publicBase)
|
||||
if err != nil {
|
||||
log.Fatalf("probe: publisher: %v", err)
|
||||
}
|
||||
rewriter := outboundimage.NewRewriter(fetcher, publisher, *maxImages, outboundimage.WithImageCompression(*compress))
|
||||
|
||||
var answer strings.Builder
|
||||
answer.WriteString("## 探针答案\n")
|
||||
for i, ref := range refs {
|
||||
fmt.Fprintf(&answer, "\n\n", i+1, ref)
|
||||
}
|
||||
|
||||
log.Printf("probe: refs=%d budget=%s max=%d compress=%v passes=%d bucket=%s", len(refs), *budget, *maxImages, *compress, *passes, os.Getenv("DOBOTAI_OUTBOUND_IMAGE_OSS_BUCKET"))
|
||||
body := answer.String()
|
||||
for pass := 1; pass <= *passes; pass++ {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), *budget)
|
||||
started := time.Now()
|
||||
out := rewriter.Rewrite(ctx, body)
|
||||
elapsed := time.Since(started)
|
||||
cancel()
|
||||
|
||||
published := regexp.MustCompile(`!\[图\d+\]\(https://`).FindAllString(out, -1)
|
||||
leaked := strings.Count(out, "resource://")
|
||||
fmt.Printf("RESULT pass=%d elapsed=%s refs=%d published=%d dropped=%d residual_resource_refs=%d out_bytes=%d\n",
|
||||
pass, elapsed.Round(time.Millisecond), len(refs), len(published), len(refs)-len(published), leaked, len(out))
|
||||
if len(published) > 0 {
|
||||
fmt.Printf("FIRST_URL_%d=%s\nLAST_URL_%d=%s\n", pass, firstImageURL(out), pass, lastImageURL(out))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func readRefs(path string) []string {
|
||||
if path == "" {
|
||||
return nil
|
||||
}
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
log.Fatalf("probe: refs file: %v", err)
|
||||
}
|
||||
defer file.Close()
|
||||
seen := map[string]bool{}
|
||||
refs := make([]string, 0, 64)
|
||||
scanner := bufio.NewScanner(file)
|
||||
for scanner.Scan() {
|
||||
ref := strings.TrimSpace(scanner.Text())
|
||||
if !strings.HasPrefix(ref, "resource://") || seen[ref] {
|
||||
continue
|
||||
}
|
||||
seen[ref] = true
|
||||
refs = append(refs, ref)
|
||||
}
|
||||
return refs
|
||||
}
|
||||
|
||||
var imageURLRE = regexp.MustCompile(`!\[图\d+\]\((https://[^)]+)\)`)
|
||||
|
||||
func firstImageURL(content string) string {
|
||||
if match := imageURLRE.FindStringSubmatch(content); len(match) > 1 {
|
||||
return match[1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func lastImageURL(content string) string {
|
||||
if matches := imageURLRE.FindAllStringSubmatch(content, -1); len(matches) > 0 {
|
||||
return matches[len(matches)-1][1]
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -30,4 +30,4 @@ func TestEffectiveDownloadCodesEmpty(t *testing.T) {
|
||||
if codes := effectiveDownloadCodes(BotCallback{}); len(codes) != 0 {
|
||||
t.Fatalf("codes=%v, want empty", codes)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/directory"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/outboundtext"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
||||
)
|
||||
@@ -675,6 +676,7 @@ func (h *Handler) finalizeCardOrMarkdown(ctx context.Context, webhookURL string,
|
||||
}
|
||||
|
||||
func (h *Handler) rewriteOutboundContent(ctx context.Context, content string) string {
|
||||
content = outboundtext.NormalizeMath(content)
|
||||
options := tableNormalizeOptions{groupWideTables: h.groupTablesFinal}
|
||||
if h.outboundRewriter == nil {
|
||||
return normalizeMarkdownTables(sanitizeDisplayAnswer(content), options)
|
||||
@@ -692,6 +694,7 @@ func (h *Handler) rewriteOutboundContent(ctx context.Context, content string) st
|
||||
}
|
||||
|
||||
func (h *Handler) sanitizeOutboundPartial(ctx context.Context, content string) string {
|
||||
content = outboundtext.NormalizeMath(content)
|
||||
sanitizer, ok := h.outboundRewriter.(OutboundPartialSanitizer)
|
||||
if ok {
|
||||
content = sanitizer.SanitizePartial(content)
|
||||
|
||||
@@ -77,4 +77,4 @@ func TestPublishQuestionImageFailsClosedWithoutURL(t *testing.T) {
|
||||
if got := h2.publishQuestionImage(context.Background(), image); got != "" {
|
||||
t.Fatalf("expected empty URL on publish error, got %q", got)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
package dingtalk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/outboundimage"
|
||||
)
|
||||
|
||||
type diagFetcher struct{}
|
||||
|
||||
func (diagFetcher) Fetch(ctx context.Context, ref string) ([]byte, string, error) {
|
||||
return []byte("PNGDATA"), "image/png", nil
|
||||
}
|
||||
|
||||
type diagPublisher struct{}
|
||||
|
||||
func (diagPublisher) Publish(ctx context.Context, data []byte, contentType string) (string, error) {
|
||||
return "https://weknora-api-test.dobot.cn/dingtalk/i/a87f73b8-15cb-546e-ba53-bf93dade916f", nil
|
||||
}
|
||||
|
||||
// The outbound rewrite must publish a resource image and keep it in the card
|
||||
// body. A reference whose handle contains "_x" (…ZM_pltQ) used to be mangled by
|
||||
// the math normalizer, which made the image unpublishable and the sanitizer
|
||||
// drop the whole line.
|
||||
func TestRewriteOutboundContentKeepsPublishedImage(t *testing.T) {
|
||||
content := "\n\n公式:$r = \\sqrt{X^2 + Y^2}$\n"
|
||||
h := &Handler{
|
||||
outboundRewriter: outboundimage.NewRewriter(diagFetcher{}, diagPublisher{}, 50),
|
||||
groupTablesFinal: true,
|
||||
}
|
||||
out := h.rewriteOutboundContent(context.Background(), content)
|
||||
if !strings.Contains(out, " {
|
||||
t.Fatalf("published image missing from card content: %q", out)
|
||||
}
|
||||
if !strings.Contains(out, "r = √(X² + Y²)") {
|
||||
t.Fatalf("formula not converted: %q", out)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package outboundtext
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// DingTalk cards render a small Markdown subset and no math typesetting, so a
|
||||
// LaTeX formula reaches the user as raw source ($r = \sqrt{X^2 + Y^2}$).
|
||||
// NormalizeMath rewrites such formulas into plain Unicode text that the card
|
||||
// can show. It only touches content that actually carries math delimiters or
|
||||
// well-known math commands, so ordinary prose is left byte-identical.
|
||||
|
||||
var (
|
||||
mathBlockDelimRE = regexp.MustCompile(`\$\$([\s\S]+?)\$\$`)
|
||||
mathSpanRE = regexp.MustCompile(`(?s)\$\$.+?\$\$|\\\[.+?\\\]|\\\(.+?\\\)|\$[^$\n]{1,400}\$`)
|
||||
parenDelimRE = regexp.MustCompile(`\\\(([\s\S]+?)\\\)`)
|
||||
bracketDelimRE = regexp.MustCompile(`\\\[([\s\S]+?)\\\]`)
|
||||
sqrtRE = regexp.MustCompile(`\\sqrt\s*(?:\[([^\]]{1,20})\])?\s*\{([^{}]{1,200})\}`)
|
||||
fracRE = regexp.MustCompile(`\\frac\s*\{([^{}]{1,200})\}\s*\{([^{}]{1,200})\}`)
|
||||
supBracedRE = regexp.MustCompile(`\^\s*\{([^{}]{1,40})\}`)
|
||||
subBracedRE = regexp.MustCompile(`_\s*\{([^{}]{1,40})\}`)
|
||||
supCharRE = regexp.MustCompile(`\^([0-9A-Za-z+\-=()])`)
|
||||
subCharRE = regexp.MustCompile(`_([0-9A-Za-z+\-=()])`)
|
||||
commandRE = regexp.MustCompile(`\\([A-Za-z]+)`)
|
||||
braceDelimRE = regexp.MustCompile(`\{([^{}]{0,200})\}`)
|
||||
blankRunRE = regexp.MustCompile(`[ \t]{2,}`)
|
||||
)
|
||||
|
||||
var mathCommands = map[string]string{
|
||||
"times": "×", "cdot": "·", "div": "÷", "pm": "±", "mp": "∓",
|
||||
"leq": "≤", "le": "≤", "geq": "≥", "ge": "≥", "neq": "≠", "ne": "≠",
|
||||
"approx": "≈", "sim": "∼", "simeq": "≃", "equiv": "≡", "propto": "∝",
|
||||
"infty": "∞", "partial": "∂", "nabla": "∇",
|
||||
"alpha": "α", "beta": "β", "gamma": "γ", "delta": "δ", "Delta": "Δ",
|
||||
"epsilon": "ε", "theta": "θ", "lambda": "λ", "mu": "μ", "rho": "ρ",
|
||||
"sigma": "σ", "tau": "τ", "phi": "φ", "omega": "ω", "Omega": "Ω",
|
||||
"eta": "η", "kappa": "κ", "nu": "ν", "xi": "ξ", "zeta": "ζ", "psi": "ψ",
|
||||
"sum": "Σ", "prod": "Π", "int": "∫", "iint": "∬", "oint": "∮",
|
||||
"in": "∈", "notin": "∉", "subset": "⊂", "subseteq": "⊆", "cup": "∪", "cap": "∩",
|
||||
"rightarrow": "→", "to": "→", "longrightarrow": "→", "leftarrow": "←",
|
||||
"Rightarrow": "⇒", "leftrightarrow": "↔", "mapsto": "↦",
|
||||
"forall": "∀", "exists": "∃", "angle": "∠", "degree": "°", "circ": "°",
|
||||
"perp": "⊥", "parallel": "∥", "therefore": "∴", "because": "∵",
|
||||
"ldots": "…", "cdots": "…", "dots": "…", "prime": "′",
|
||||
"quad": " ", "qquad": " ", "limits": "", "displaystyle": "",
|
||||
"left": "", "right": "", "bmod": "mod ", "pmod": "mod ",
|
||||
"text": "", "mathrm": "", "mathit": "", "operatorname": "",
|
||||
"overline": "", "underline": "",
|
||||
}
|
||||
|
||||
var escapedPunctuation = map[string]string{
|
||||
`\\`: "\n", `\%`: "%", `\&`: "&", `\#`: "#", `\_`: "_", `\$`: "$",
|
||||
`\{`: "{", `\}`: "}", `\,`: " ", `\;`: " ", `\!`: "",
|
||||
`\(`: "", `\)`: "", `\[`: "", `\]`: "",
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
package outboundtext
|
||||
|
||||
import "strings"
|
||||
|
||||
func NormalizeMath(markdown string) string {
|
||||
if !carriesMath(markdown) {
|
||||
return markdown
|
||||
}
|
||||
content := mathSpanRE.ReplaceAllStringFunc(markdown, convertMathSpan)
|
||||
// Outside math spans only LaTeX commands are converted. Identifiers and URLs
|
||||
// (resource://…_pltQ) must never be rewritten: a mangled reference can no
|
||||
// longer be published, and the outbound sanitizer then drops the whole image.
|
||||
content = commandRE.ReplaceAllStringFunc(content, replaceCommand)
|
||||
content = blankRunRE.ReplaceAllString(content, " ")
|
||||
return content
|
||||
}
|
||||
|
||||
func convertMathSpan(span string) string {
|
||||
inner := span
|
||||
switch {
|
||||
case strings.HasPrefix(inner, "$$"):
|
||||
inner = strings.TrimSuffix(strings.TrimPrefix(inner, "$$"), "$$")
|
||||
return "\n" + convertMath(inner) + "\n"
|
||||
case strings.HasPrefix(inner, `\[`):
|
||||
inner = strings.TrimSuffix(strings.TrimPrefix(inner, `\[`), `\]`)
|
||||
case strings.HasPrefix(inner, `\(`):
|
||||
inner = strings.TrimSuffix(strings.TrimPrefix(inner, `\(`), `\)`)
|
||||
default:
|
||||
inner = strings.TrimSuffix(strings.TrimPrefix(inner, "$"), "$")
|
||||
}
|
||||
return convertMath(inner)
|
||||
}
|
||||
|
||||
func convertMath(inner string) string {
|
||||
content := inner
|
||||
for from, to := range escapedPunctuation {
|
||||
content = strings.ReplaceAll(content, from, to)
|
||||
}
|
||||
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")
|
||||
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': "ₓ",
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package outboundtext
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestNormalizeMath(t *testing.T) {
|
||||
cases := []struct{ name, in, want string }{
|
||||
{"inline sqrt", `$r = \sqrt{X^2 + Y^2}$`, `r = √(X² + Y²)`},
|
||||
{"inline inside prose", `计算公式为 $r = \sqrt{X^2 + Y^2}$(单位:cm)`, `计算公式为 r = √(X² + Y²)(单位:cm)`},
|
||||
{"block frac times", `$$\frac{a}{b} \times c$$`, "\n(a)/(b) × c\n"},
|
||||
{"paren delimiters", `\(x \leq y\)`, `x ≤ y`},
|
||||
{"bracket delimiters", `\[ \sum_{i=1}^{n} x_i \]`, ` Σᵢ₌₁ⁿ xᵢ `},
|
||||
{"nth root", `$\sqrt[3]{V}$`, `√[3](V)`},
|
||||
{"no math untouched", `C:\new\folder 与 100% 覆盖`, `C:\new\folder 与 100% 覆盖`},
|
||||
{"dollar free prose", `价格是 5 元, 无公式`, `价格是 5 元, 无公式`},
|
||||
{"resource url untouched", ` 公式 $r = \sqrt{X^2 + Y^2}$`, ` 公式 r = √(X² + Y²)`},
|
||||
{"code span untouched", "`x_1` 与 $x_1$", "`x_1` 与 x₁"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := NormalizeMath(tc.in)
|
||||
if got != tc.want {
|
||||
t.Fatalf("NormalizeMath(%q) = %q, want %q", tc.in, got, tc.want)
|
||||
}
|
||||
if again := NormalizeMath(got); again != got {
|
||||
t.Fatalf("NormalizeMath is not idempotent: %q -> %q", got, again)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeMathKeepsPlainMarkdown(t *testing.T) {
|
||||
plain := "### 标题\n\n- 项目一\n- 项目二\n\n普通段落,没有公式。"
|
||||
if got := NormalizeMath(plain); got != plain {
|
||||
t.Fatalf("plain markdown changed: %q", got)
|
||||
}
|
||||
}
|
||||
@@ -68,3 +68,86 @@ func TestAskEventsQuickKeepsFullAnswer(t *testing.T) {
|
||||
t.Fatalf("quick answer must be untouched, got %q", result.Answer)
|
||||
}
|
||||
}
|
||||
|
||||
// Production shape (turn efae317e, 给我CRA5的负载图): the agent narrates its
|
||||
// steps, then publishes the answer — lead-in, image and all — as its own answer
|
||||
// run. Cutting at the first structural line threw the lead-in and the image
|
||||
// away; the run boundary keeps them.
|
||||
func cra5Fixture() (string, string, string) {
|
||||
narration := "用户想要 CRA5(CR5A)的末端负载曲线图。让我先在知识库中搜索相关的产品手册和 FAQ。"
|
||||
body := "我已经找到了 CRA5(CR5A)的末端负载曲线图,并确认了负载曲线的读法说明。下面给出完整回答。\n\n" +
|
||||
"CRA5(即 CR5A)的末端负载曲线如下:\n\n" +
|
||||
"\n\n" +
|
||||
"---\n\n" +
|
||||
"## 一、负载曲线怎么读\n\n" +
|
||||
"负载曲线反映的是:末端负载能力与负载质心偏心距离成负相关。"
|
||||
return narration, body, narration + body
|
||||
}
|
||||
|
||||
func TestAskEventsReasoningKeepsLeadInAndImageFromAnswerRun(t *testing.T) {
|
||||
narration, body, answer := cra5Fixture()
|
||||
// The heuristic on its own drops the lead-in and the image; the observation
|
||||
// must win over it.
|
||||
if heuristic := reasoningmode.TrimReasoningNarration(answer); heuristic == body {
|
||||
t.Fatal("fixture no longer exposes the heuristic defect")
|
||||
}
|
||||
repo := newMemoryRepository()
|
||||
client := &fakeEventStreamingWeKnora{
|
||||
fakeWeKnora: &fakeWeKnora{},
|
||||
events: []weknora.StreamEvent{
|
||||
{ResponseType: "answer", FullAnswer: narration, AnswerRunID: "step-1-answer", AnswerRunStart: 0},
|
||||
{ResponseType: "tool_call", ToolName: "knowledge_search"},
|
||||
{ResponseType: "tool_result", ToolName: "knowledge_search"},
|
||||
{ResponseType: "thinking"},
|
||||
{ResponseType: "answer", FullAnswer: answer, Done: true, AnswerRunID: "final-answer", AnswerRunStart: len(narration)},
|
||||
},
|
||||
streamResult: weknora.ChatResult{RequestID: "request-cra5", Answer: answer},
|
||||
}
|
||||
service, err := New(repo, fixedResolver{}, client, session.GroupShared)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := streamQARequest("message-cra5")
|
||||
req.ReasoningExecution = string(reasoningmode.ExecutionReasoning)
|
||||
result, err := service.AskEvents(context.Background(), req, func(weknora.StreamEvent) {})
|
||||
if err != nil {
|
||||
t.Fatalf("AskEvents: %v", err)
|
||||
}
|
||||
if result.Answer != body {
|
||||
t.Fatalf("delivered answer must keep the answer body (lead-in + image), got %q", result.Answer)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAskEventsReasoningFallsBackWhenFinalRunInterrupted(t *testing.T) {
|
||||
_, _, answer := reasoningAnswerFixture()
|
||||
narration := "我需要先确认这台设备的型号和手册。"
|
||||
repo := newMemoryRepository()
|
||||
client := &fakeEventStreamingWeKnora{
|
||||
fakeWeKnora: &fakeWeKnora{},
|
||||
events: []weknora.StreamEvent{
|
||||
{ResponseType: "answer", FullAnswer: narration, AnswerRunID: "step-1-answer", AnswerRunStart: 0},
|
||||
{ResponseType: "tool_call", ToolName: "knowledge_search"},
|
||||
{ResponseType: "tool_result", ToolName: "knowledge_search"},
|
||||
// The last answer run keeps running across another tool call, so the
|
||||
// stream cannot prove where the body starts: fall back to the heuristic.
|
||||
{ResponseType: "answer", FullAnswer: answer, AnswerRunID: "step-2-answer", AnswerRunStart: len(narration)},
|
||||
{ResponseType: "tool_call", ToolName: "knowledge_search"},
|
||||
{ResponseType: "tool_result", ToolName: "knowledge_search"},
|
||||
{ResponseType: "answer", FullAnswer: answer, Done: true, AnswerRunID: "step-2-answer", AnswerRunStart: len(narration)},
|
||||
},
|
||||
streamResult: weknora.ChatResult{RequestID: "request-interrupted", Answer: answer},
|
||||
}
|
||||
service, err := New(repo, fixedResolver{}, client, session.GroupShared)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req := streamQARequest("message-interrupted")
|
||||
req.ReasoningExecution = string(reasoningmode.ExecutionReasoning)
|
||||
result, err := service.AskEvents(context.Background(), req, func(weknora.StreamEvent) {})
|
||||
if err != nil {
|
||||
t.Fatalf("AskEvents: %v", err)
|
||||
}
|
||||
if want := reasoningmode.TrimReasoningNarration(answer); result.Answer != want {
|
||||
t.Fatalf("interrupted run must fall back to the heuristic: want %q, got %q", want, result.Answer)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
package qa
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/reasoningmode"
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
||||
)
|
||||
|
||||
// TestReplayRecordedStreamDiagnostics replays a recorded WeKnora SSE stream
|
||||
// (path in WK_RAW_STREAM) through the real client and prints what the narration
|
||||
// observation sees. Temporary diagnostic, removed after the investigation.
|
||||
func TestReplayRecordedStreamDiagnostics(t *testing.T) {
|
||||
path := os.Getenv("WK_RAW_STREAM")
|
||||
if path == "" {
|
||||
t.Skip("WK_RAW_STREAM not set")
|
||||
}
|
||||
raw, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
t.Fatalf("read stream: %v", err)
|
||||
}
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
_, _ = w.Write(raw)
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
client, err := weknora.NewClient(server.URL, "test-key", 30*time.Second)
|
||||
if err != nil {
|
||||
t.Fatalf("client: %v", err)
|
||||
}
|
||||
|
||||
var observation reasoningmode.NarrationObservation
|
||||
runStarts := []string{}
|
||||
eventTypes := map[string]int{}
|
||||
observer := func(e weknora.StreamEvent) {
|
||||
eventTypes[e.ResponseType]++
|
||||
switch e.ResponseType {
|
||||
case "answer":
|
||||
if e.AnswerRunID != "" {
|
||||
observation.Observed = true
|
||||
}
|
||||
if e.AnswerRunID != "" && e.AnswerRunID != lastRunID(runStarts) {
|
||||
runStarts = append(runStarts, fmt.Sprintf("%s@%d", e.AnswerRunID, e.AnswerRunStart))
|
||||
observation.AnswerRuns++
|
||||
observation.LastRunStart = e.AnswerRunStart
|
||||
observation.LastRunClean = true
|
||||
}
|
||||
case "thinking", "tool_call", "tool_result":
|
||||
observation.ToolEvents++
|
||||
observation.LastRunClean = false
|
||||
}
|
||||
}
|
||||
|
||||
result, err := client.AgentChatEvents(context.Background(), "session", weknora.AgentChatRequest{AgentID: "agent", Query: "q"}, observer)
|
||||
if err != nil {
|
||||
t.Fatalf("agent chat events: %v", err)
|
||||
}
|
||||
|
||||
t.Logf("eventTypes=%v", eventTypes)
|
||||
t.Logf("runs=%v", runStarts)
|
||||
t.Logf("observation: observed=%v runs=%d tools=%d lastRunStart=%d lastRunClean=%v",
|
||||
observation.Observed, observation.AnswerRuns, observation.ToolEvents, observation.LastRunStart, observation.LastRunClean)
|
||||
|
||||
content := result.Answer
|
||||
observed := reasoningmode.TrimReasoningNarrationObserved(content, observation)
|
||||
heuristic := reasoningmode.TrimReasoningNarration(content)
|
||||
t.Logf("contentLen=%d observedKept=%d heuristicKept=%d", len(content), len(observed), len(heuristic))
|
||||
if n := len(content) - len(observed); n > 0 {
|
||||
t.Logf("observedDropPrefix=%q", firstRunes(content[:n], 160))
|
||||
}
|
||||
if n := len(content) - len(heuristic); n > 0 {
|
||||
t.Logf("heuristicDropPrefix=%q", firstRunes(content[:n], 160))
|
||||
}
|
||||
t.Logf("observedKeptHead=%q", firstRunes(observed, 160))
|
||||
}
|
||||
|
||||
func lastRunID(runs []string) string {
|
||||
if len(runs) == 0 {
|
||||
return ""
|
||||
}
|
||||
id := runs[len(runs)-1]
|
||||
if i := strings.LastIndex(id, "@"); i >= 0 {
|
||||
return id[:i]
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func firstRunes(s string, n int) string {
|
||||
r := []rune(s)
|
||||
if len(r) <= n {
|
||||
return string(r)
|
||||
}
|
||||
return string(r[:n]) + "…"
|
||||
}
|
||||
+24
-1
@@ -248,10 +248,33 @@ func (s *Service) ask(ctx context.Context, request core.QARequest, answerObserve
|
||||
// observing knowledge_search tool-call events. The flag feeds coverage
|
||||
// classification (image turns) and is persisted with the turn.
|
||||
var knowledgeSearched bool
|
||||
// The stream itself separates the agent's step narration from the final
|
||||
// answer body: every step publishes its answer chunks under its own answer
|
||||
// run id (data.event_id), and the last run that no tool/thinking event
|
||||
// interrupts is the final body. Track that structure so the delivered
|
||||
// answer keeps the body's lead-in — and any image reference inside it —
|
||||
// instead of guessing the boundary from the text.
|
||||
var narrationObservation reasoningmode.NarrationObservation
|
||||
lastAnswerRunID := ""
|
||||
effectiveEventObserver := weknora.StreamObserver(func(e weknora.StreamEvent) {
|
||||
if isKnowledgeTool(e.ToolName) {
|
||||
knowledgeSearched = true
|
||||
}
|
||||
switch e.ResponseType {
|
||||
case "answer":
|
||||
if e.AnswerRunID != "" {
|
||||
narrationObservation.Observed = true
|
||||
if e.AnswerRunID != lastAnswerRunID {
|
||||
lastAnswerRunID = e.AnswerRunID
|
||||
narrationObservation.AnswerRuns++
|
||||
narrationObservation.LastRunStart = e.AnswerRunStart
|
||||
narrationObservation.LastRunClean = true
|
||||
}
|
||||
}
|
||||
case "thinking", "tool_call", "tool_result":
|
||||
narrationObservation.ToolEvents++
|
||||
narrationObservation.LastRunClean = false
|
||||
}
|
||||
if eventObserver != nil {
|
||||
eventObserver(e)
|
||||
}
|
||||
@@ -334,7 +357,7 @@ func (s *Service) ask(ctx context.Context, request core.QARequest, answerObserve
|
||||
// narration stays visible while the card streams (line by line) and is
|
||||
// dropped once the final body starts.
|
||||
if reasoningmode.IsReasoningExecution(request.ReasoningExecution) {
|
||||
if trimmed := reasoningmode.TrimReasoningNarration(chat.Answer); trimmed != chat.Answer {
|
||||
if trimmed := reasoningmode.TrimReasoningNarrationObserved(chat.Answer, narrationObservation); trimmed != chat.Answer {
|
||||
turn.Answer, chat.Answer = trimmed, trimmed
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,127 @@
|
||||
package reasoningmode
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// The production case this observation fixes: the agent narrates its steps,
|
||||
// then the final answer opens with a lead-in ("我已经找到了…下面给出完整回答。"),
|
||||
// carries the requested load-curve image, and only then reaches its first
|
||||
// heading. The text heuristic started the body at the heading and dropped the
|
||||
// lead-in plus the image reference with it.
|
||||
const craLoadCurveAnswer = "我需要先确认 CRA5 与 CR5A 是同一型号,然后检索负载曲线。\n" +
|
||||
"知识库检索命中 3 篇文档,其中用户手册含末端负载曲线图。\n" +
|
||||
"我已经找到了 CRA5(CR5A)的末端负载曲线图,并确认了负载曲线的读法说明。下面给出完整回答。\n\n" +
|
||||
"CRA5(即 CR5A)的末端负载曲线如下:\n\n" +
|
||||
"\n\n" +
|
||||
"---\n\n" +
|
||||
"## 一、负载曲线怎么读\n\n" +
|
||||
"负载曲线反映的是:末端负载能力与负载质心偏心距离成负相关。\n"
|
||||
|
||||
func TestNarrationObservationKeepsAnswerLeadInAndImage(t *testing.T) {
|
||||
narration := strings.Index(craLoadCurveAnswer, "我已经找到了")
|
||||
observation := NarrationObservation{
|
||||
Observed: true, AnswerRuns: 3, ToolEvents: 5,
|
||||
LastRunStart: narration, LastRunClean: true,
|
||||
}
|
||||
|
||||
body := TrimReasoningNarrationObserved(craLoadCurveAnswer, observation)
|
||||
if !strings.HasPrefix(body, "我已经找到了") {
|
||||
t.Fatalf("body must start at the answer lead-in, got %q", firstLine(body))
|
||||
}
|
||||
if !strings.Contains(body, "") {
|
||||
t.Fatalf("body must keep the image reference, got %q", firstLine(body))
|
||||
}
|
||||
if strings.Contains(body, "我需要先确认") {
|
||||
t.Fatalf("body must not contain the step narration, got %q", firstLine(body))
|
||||
}
|
||||
|
||||
// The text-only heuristic is what dropped the lead-in and the image.
|
||||
heuristic := TrimReasoningNarration(craLoadCurveAnswer)
|
||||
if !strings.HasPrefix(heuristic, "## 一、负载曲线怎么读") {
|
||||
t.Fatalf("heuristic is expected to start at the heading, got %q", firstLine(heuristic))
|
||||
}
|
||||
if strings.Contains(heuristic, "resource://") {
|
||||
t.Fatalf("heuristic is expected to drop the image reference")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNarrationObservationFallsBackWithoutUsableBoundary(t *testing.T) {
|
||||
narration := strings.Index(craLoadCurveAnswer, "我已经找到了")
|
||||
tests := []struct {
|
||||
name string
|
||||
observation NarrationObservation
|
||||
}{
|
||||
{"not observed", NarrationObservation{AnswerRuns: 3, ToolEvents: 5, LastRunStart: narration, LastRunClean: true}},
|
||||
{"last run interrupted by a tool", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: narration}},
|
||||
{"multi run starting at zero", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: 0, LastRunClean: true}},
|
||||
{"run start beyond the answer", NarrationObservation{Observed: true, AnswerRuns: 3, ToolEvents: 5, LastRunStart: len(craLoadCurveAnswer) + 10, LastRunClean: true}},
|
||||
}
|
||||
for _, tc := range tests {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
got := TrimReasoningNarrationObserved(craLoadCurveAnswer, tc.observation)
|
||||
if want := TrimReasoningNarration(craLoadCurveAnswer); got != want {
|
||||
t.Fatalf("expected heuristic fallback %q, got %q", firstLine(want), firstLine(got))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNarrationObservationKeepsImageLeadIn(t *testing.T) {
|
||||
// The reported production shape: the lead-in carries the answer prose plus
|
||||
// the picture, and the boundary observed by the stream only lands after it.
|
||||
content := "根据知识库中《末端负载曲线-CRA系列》文档,以下是 **CR5A 的末端负载曲线图**:\n\n" +
|
||||
"\n\n" +
|
||||
"### 如何查看该负载曲线\n负载能力与偏心距离成负相关。\n"
|
||||
offset := strings.Index(content, "### 如何查看该负载曲线")
|
||||
observation := NarrationObservation{Observed: true, AnswerRuns: 4, ToolEvents: 6, LastRunStart: offset, LastRunClean: true}
|
||||
if got := TrimReasoningNarrationObserved(content, observation); got != content {
|
||||
t.Fatalf("image lead-in must survive the trim, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNarrationObservationKeepsSingleRunAnswer(t *testing.T) {
|
||||
// The quick-answer agent publishes the whole answer as a single answer run
|
||||
// (with tool_call/tool_result steps around it). Nothing in that stream is
|
||||
// narration, so the lead-in and the image must survive.
|
||||
observation := NarrationObservation{Observed: true, AnswerRuns: 1, ToolEvents: 4, LastRunStart: 0, LastRunClean: true}
|
||||
got := TrimReasoningNarrationObserved(craLoadCurveAnswer, observation)
|
||||
if got != craLoadCurveAnswer {
|
||||
t.Fatalf("single answer run must keep the whole answer, got %q", firstLine(got))
|
||||
}
|
||||
if !strings.Contains(got, "resource://") {
|
||||
t.Fatalf("the image reference must survive for a single answer run")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNarrationObservationKeepsWholeAnswerWithoutToolSteps(t *testing.T) {
|
||||
answer := "本机提供以下三种接线方式:\n\n| 方式 | 说明 |\n| --- | --- |\n| A | 直连 |\n"
|
||||
observation := NarrationObservation{Observed: true, AnswerRuns: 1, ToolEvents: 0}
|
||||
|
||||
body, ok := SplitReasoningAnswerObserved(answer, observation)
|
||||
if !ok {
|
||||
t.Fatalf("a run without tool steps has no narration, so the whole answer is the body")
|
||||
}
|
||||
if body != answer {
|
||||
t.Fatalf("expected the whole answer, got %q", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNarrationObservationTrimsBlankLineAtRunStart(t *testing.T) {
|
||||
content := "叙述步骤。\n\n\n## 标题\n\n正文。\n"
|
||||
observation := NarrationObservation{Observed: true, AnswerRuns: 2, ToolEvents: 1,
|
||||
LastRunStart: strings.Index(content, "\n\n\n##"), LastRunClean: true}
|
||||
|
||||
body := TrimReasoningNarrationObserved(content, observation)
|
||||
if body != "## 标题\n\n正文。\n" {
|
||||
t.Fatalf("expected the blank lines before the run content to be trimmed, got %q", body)
|
||||
}
|
||||
}
|
||||
|
||||
func firstLine(s string) string {
|
||||
if i := strings.IndexByte(s, '\n'); i >= 0 {
|
||||
return s[:i]
|
||||
}
|
||||
return s
|
||||
}
|
||||
@@ -34,6 +34,12 @@ type StreamEvent struct {
|
||||
FullAnswer string
|
||||
ReferenceCount int
|
||||
ToolName string
|
||||
// AnswerRunID / AnswerRunStart describe the answer run the event belongs
|
||||
// to. WeKnora tags every agent step's answer chunks with its own
|
||||
// data.event_id, so a change of AnswerRunID marks the start of a new run
|
||||
// and AnswerRunStart is the accumulated answer offset where it began.
|
||||
AnswerRunID string
|
||||
AnswerRunStart int
|
||||
}
|
||||
|
||||
// StreamObserver is called with a safe StreamEvent for each SSE event.
|
||||
@@ -80,9 +86,8 @@ func NewClient(baseURL, apiKey string, timeout time.Duration) (*Client, error) {
|
||||
if strings.TrimSpace(apiKey) == "" {
|
||||
return nil, errors.New("WEKNORA_API_KEY is required")
|
||||
}
|
||||
if timeout <= 0 {
|
||||
timeout = 120 * time.Second
|
||||
}
|
||||
// timeout <= 0 keeps Go's default: no client-side total timeout on the HTTP
|
||||
// client, so long-lived agent SSE streams are never cut by a duration cap.
|
||||
return &Client{baseURL: parsed.String(), apiKey: apiKey, http: &http.Client{Timeout: timeout}}, nil
|
||||
}
|
||||
|
||||
@@ -227,6 +232,12 @@ func parseSSEWithEventObserver(reader io.Reader, answerObserver AnswerObserver,
|
||||
var result ChatResult
|
||||
var dataLines []string
|
||||
terminal := false
|
||||
// A reasoning agent publishes every step narrative and the final answer
|
||||
// body through the same answer channel, but each step is its own answer
|
||||
// run (data.event_id). A change of run id marks a new run start, which is
|
||||
// what lets callers separate narration from the final body instead of
|
||||
// guessing from the text.
|
||||
answerRunID, answerRunStart := "", 0
|
||||
|
||||
notify := func(e StreamEvent) {
|
||||
if eventObserver != nil {
|
||||
@@ -262,15 +273,20 @@ func parseSSEWithEventObserver(reader io.Reader, answerObserver AnswerObserver,
|
||||
ReferenceCount: len(event.KnowledgeReferences),
|
||||
})
|
||||
case "answer":
|
||||
if runID := extractAnswerRunID(event.Data); runID != "" && runID != answerRunID {
|
||||
answerRunID, answerRunStart = runID, len(result.Answer)
|
||||
}
|
||||
hasContent := strings.TrimSpace(event.Content) != ""
|
||||
result.Answer += event.Content
|
||||
if hasContent && answerObserver != nil {
|
||||
answerObserver(result.Answer)
|
||||
}
|
||||
notify(StreamEvent{
|
||||
ResponseType: "answer",
|
||||
Done: event.Done,
|
||||
FullAnswer: result.Answer,
|
||||
ResponseType: "answer",
|
||||
Done: event.Done,
|
||||
FullAnswer: result.Answer,
|
||||
AnswerRunID: answerRunID,
|
||||
AnswerRunStart: answerRunStart,
|
||||
})
|
||||
if event.Done {
|
||||
terminal = true
|
||||
@@ -348,6 +364,30 @@ func parseSSEWithEventObserver(reader io.Reader, answerObserver AnswerObserver,
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// extractAnswerRunID extracts data.event_id, the identifier of the answer run
|
||||
// an answer chunk belongs to. A reasoning agent gives every step its own id,
|
||||
// so a change of id marks the start of a new run and, once no tool event
|
||||
// follows, of the final answer body. Older deployments that do not tag answer
|
||||
// events yield an empty id, which callers treat as "no observation".
|
||||
func extractAnswerRunID(data json.RawMessage) string {
|
||||
if len(data) == 0 {
|
||||
return ""
|
||||
}
|
||||
var m map[string]json.RawMessage
|
||||
if err := json.Unmarshal(data, &m); err != nil {
|
||||
return ""
|
||||
}
|
||||
v, ok := m["event_id"]
|
||||
if !ok {
|
||||
return ""
|
||||
}
|
||||
var s string
|
||||
if err := json.Unmarshal(v, &s); err != nil {
|
||||
return ""
|
||||
}
|
||||
return strings.TrimSpace(s)
|
||||
}
|
||||
|
||||
// extractToolName extracts the tool_name string from a JSON Data field.
|
||||
// The Data may be any JSON shape; we look for the "tool_name" key and return
|
||||
// its string value. Returns empty string if not found or not a string.
|
||||
|
||||
@@ -267,3 +267,69 @@ func TestAgentChatEventsErrorTurn(t *testing.T) {
|
||||
t.Fatalf("expected error done event, got %+v", events[0])
|
||||
}
|
||||
}
|
||||
|
||||
// TestStreamEventAnswerRuns verifies answer chunks are attributed to the answer
|
||||
// run that produced them: a reasoning agent publishes every step narrative and
|
||||
// the final answer body through the same answer channel, each under its own
|
||||
// data.event_id, so the run start marks where the final body begins.
|
||||
func TestStreamEventAnswerRuns(t *testing.T) {
|
||||
stream := "data: {\"response_type\":\"thinking\",\"content\":\"inner monologue\",\"done\":false}\n\n" +
|
||||
"data: {\"response_type\":\"answer\",\"content\":\"我先检索知识库。\",\"data\":{\"event_id\":\"step-1-answer\"},\"done\":false}\n\n" +
|
||||
"data: {\"response_type\":\"tool_call\",\"data\":{\"tool_name\":\"knowledge_search\",\"event_id\":\"step-1-tool\"},\"done\":false}\n\n" +
|
||||
"data: {\"response_type\":\"tool_result\",\"data\":{\"tool_name\":\"knowledge_search\",\"event_id\":\"step-1-tool\"},\"done\":false}\n\n" +
|
||||
"data: {\"response_type\":\"answer\",\"content\":\"我已经找到了负载曲线图:\",\"data\":{\"event_id\":\"final-answer\"},\"done\":false}\n\n" +
|
||||
"data: {\"response_type\":\"answer\",\"content\":\"\",\"data\":{\"event_id\":\"final-answer\"},\"done\":true}\n\n"
|
||||
|
||||
var events []StreamEvent
|
||||
result, err := parseSSEWithEventObserver(strings.NewReader(stream), nil, func(e StreamEvent) {
|
||||
events = append(events, e)
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if got := len([]rune("我先检索知识库。")); got == 0 {
|
||||
t.Fatal("unreachable")
|
||||
}
|
||||
narration := len("我先检索知识库。")
|
||||
if result.Answer != "我先检索知识库。我已经找到了负载曲线图:" {
|
||||
t.Fatalf("unexpected answer: %q", result.Answer)
|
||||
}
|
||||
|
||||
var answers []StreamEvent
|
||||
for _, e := range events {
|
||||
if e.ResponseType == "answer" {
|
||||
answers = append(answers, e)
|
||||
}
|
||||
}
|
||||
if len(answers) != 3 {
|
||||
t.Fatalf("expected 3 answer events, got %d: %+v", len(answers), answers)
|
||||
}
|
||||
if answers[0].AnswerRunID != "step-1-answer" || answers[0].AnswerRunStart != 0 {
|
||||
t.Fatalf("narration run: want id 'step-1-answer' start 0, got %+v", answers[0])
|
||||
}
|
||||
if answers[1].AnswerRunID != "final-answer" || answers[1].AnswerRunStart != narration {
|
||||
t.Fatalf("final run must start at %d, got %+v", narration, answers[1])
|
||||
}
|
||||
if answers[2].AnswerRunID != "final-answer" || answers[2].AnswerRunStart != narration {
|
||||
t.Fatalf("chunks of one run must share its start, got %+v", answers[2])
|
||||
}
|
||||
}
|
||||
|
||||
// TestStreamEventAnswerRunWithoutID verifies streams that carry no run id leave
|
||||
// the run fields empty, so consumers fall back to the text heuristic.
|
||||
func TestStreamEventAnswerRunWithoutID(t *testing.T) {
|
||||
stream := "data: {\"response_type\":\"answer\",\"content\":\"hello \",\"done\":false}\n\n" +
|
||||
"data: {\"response_type\":\"answer\",\"content\":\"world\",\"done\":true}\n\n"
|
||||
|
||||
var events []StreamEvent
|
||||
if _, err := parseSSEWithEventObserver(strings.NewReader(stream), nil, func(e StreamEvent) {
|
||||
events = append(events, e)
|
||||
}); err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
for i, e := range events {
|
||||
if e.AnswerRunID != "" || e.AnswerRunStart != 0 {
|
||||
t.Fatalf("event[%d] must carry no run info, got %+v", i, e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user