Files
dobotai-platform/internal/qa/replay_diagnostics_test.go
T
姚章浩 5ccaeb3b96 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.
2026-09-18 16:21:42 +08:00

103 lines
3.0 KiB
Go

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]) + "…"
}