341b7e3990
DINGTALK_CARD_PARTIAL_UPDATES selects how much of an answer is written into
the card while it is still being produced:
partial (default, unset) - placeholder card plus throttled partial answer
and progress frames, then the final update
final - placeholder card, no partial frames at all; the
finished answer lands in the single final update
direct - no placeholder card; one card with the answer
Both non-default modes drop the 500ms redraw pump for the turn, so the card
only ever shows content that is actually final. handleNonStreaming and the
two new modes share one delivery helper, and the markdown fallback keeps the
existing behaviour when the card cannot be delivered.
187 lines
6.5 KiB
Go
187 lines
6.5 KiB
Go
package dingtalk
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
|
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/weknora"
|
|
)
|
|
|
|
// pacedEventQA replays the answer events of a real turn and sleeps between them
|
|
// so the asynchronous 500ms frame pump gets a chance to fire. It is the
|
|
// sensitivity control for the frame assertions below: with mode "partial" the
|
|
// very same fixture must produce partial frames.
|
|
type pacedEventQA struct {
|
|
fakeStreamingQA
|
|
events []weknora.StreamEvent
|
|
hold time.Duration
|
|
eventsCalled bool
|
|
}
|
|
|
|
func (f *pacedEventQA) AskEvents(_ context.Context, req core.QARequest, observer weknora.StreamObserver) (core.QAResult, error) {
|
|
f.eventsCalled = true
|
|
f.lastReq = req
|
|
for _, event := range f.events {
|
|
if observer != nil {
|
|
observer(event)
|
|
}
|
|
time.Sleep(f.hold)
|
|
}
|
|
return f.result, f.err
|
|
}
|
|
|
|
func TestStreamingModesControlPartialCardFrames(t *testing.T) {
|
|
rawAnswer := "最终答案内容"
|
|
tests := []struct {
|
|
name string
|
|
mode CardPartialUpdatesMode
|
|
wantPartials bool
|
|
}{
|
|
{"partial streams frames", CardPartialUpdatesPartial, true},
|
|
{"final suppresses frames", CardPartialUpdatesFinal, false},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
card := &fakeStreamingCard{startID: "track-mode"}
|
|
qa := &pacedEventQA{
|
|
fakeStreamingQA: fakeStreamingQA{result: core.QAResult{Answer: rawAnswer}},
|
|
events: []weknora.StreamEvent{
|
|
{ResponseType: "tool_call", ToolName: "knowledge_search"},
|
|
{ResponseType: "references", ReferenceCount: 2},
|
|
{ResponseType: "answer", FullAnswer: "最终"},
|
|
{ResponseType: "answer", FullAnswer: rawAnswer, Done: true},
|
|
},
|
|
hold: 5 * time.Millisecond,
|
|
}
|
|
reply := &fakeReply{}
|
|
h, err := NewHandlerWithOptions(
|
|
TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"},
|
|
qa, reply, HandlerOptions{CardSender: card, PartialUpdates: tt.mode})
|
|
if err != nil {
|
|
t.Fatalf("NewHandlerWithOptions: %v", err)
|
|
}
|
|
h.cardUpdateInterval = time.Millisecond
|
|
if err := h.Handle(context.Background(), directTextCallback()); err != nil {
|
|
t.Fatalf("Handle: %v", err)
|
|
}
|
|
|
|
if !card.startCalled || card.startReq.Content != streamingPlaceholder {
|
|
t.Fatalf("placeholder card must be created: called=%v content=%q", card.startCalled, card.startReq.Content)
|
|
}
|
|
var partials, finals int
|
|
for _, update := range card.updates {
|
|
if update.isFinalize {
|
|
finals++
|
|
continue
|
|
}
|
|
partials++
|
|
}
|
|
if got := partials > 0; got != tt.wantPartials {
|
|
t.Fatalf("partial frames=%d want frames=%v updates=%+v", partials, tt.wantPartials, card.updates)
|
|
}
|
|
if finals != 1 {
|
|
t.Fatalf("final updates=%d want 1 updates=%+v", finals, card.updates)
|
|
}
|
|
last := card.updates[len(card.updates)-1]
|
|
if !last.isFinalize || last.isError || last.content != rawAnswer {
|
|
t.Fatalf("final update=%+v want the complete answer", last)
|
|
}
|
|
if reply.called {
|
|
t.Fatal("SendMarkdown must not be called")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDirectModeDeliversOneCardWithoutPlaceholder(t *testing.T) {
|
|
rawAnswer := "直接输出答案"
|
|
card := &fakeStreamingCard{finalID: "track-direct"}
|
|
qa := &fakeEventQA{
|
|
fakeStreamingQA: fakeStreamingQA{result: core.QAResult{Answer: rawAnswer, TurnID: "turn-1", AnswerMessageID: "msg-1"}},
|
|
events: []weknora.StreamEvent{
|
|
{ResponseType: "tool_call", ToolName: "query_understand"},
|
|
{ResponseType: "answer", FullAnswer: "直接"},
|
|
{ResponseType: "answer", FullAnswer: rawAnswer, Done: true},
|
|
},
|
|
}
|
|
reply := &fakeReply{}
|
|
h, err := NewHandlerWithOptions(
|
|
TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"},
|
|
qa, reply, HandlerOptions{CardSender: card, PartialUpdates: CardPartialUpdatesDirect})
|
|
if err != nil {
|
|
t.Fatalf("NewHandlerWithOptions: %v", err)
|
|
}
|
|
h.cardUpdateInterval = time.Millisecond
|
|
if err := h.Handle(context.Background(), directTextCallback()); err != nil {
|
|
t.Fatalf("Handle: %v", err)
|
|
}
|
|
|
|
if card.startCalled {
|
|
t.Fatalf("no placeholder card may be created in direct mode: %q", card.startReq.Content)
|
|
}
|
|
if len(card.updates) != 0 {
|
|
t.Fatalf("no card update may be sent before the answer is complete: %+v", card.updates)
|
|
}
|
|
if !card.finalCalled || card.finalReq.Content != rawAnswer {
|
|
t.Fatalf("final card=%v content=%q want %q", card.finalCalled, card.finalReq.Content, rawAnswer)
|
|
}
|
|
if reply.called {
|
|
t.Fatal("SendMarkdown must not be called")
|
|
}
|
|
if len(qa.deliveries) == 0 || qa.deliveries[len(qa.deliveries)-1].DeliveryMethod != core.DeliveryMethodCard {
|
|
t.Fatalf("delivery audit=%+v", qa.deliveries)
|
|
}
|
|
}
|
|
|
|
func TestDirectModeFallsBackToMarkdownWhenTheAnswerFails(t *testing.T) {
|
|
card := &fakeStreamingCard{finalID: "track-direct-error"}
|
|
qa := &fakeEventQA{fakeStreamingQA: fakeStreamingQA{err: errors.New("weknora down")}}
|
|
reply := &fakeReply{}
|
|
h, err := NewHandlerWithOptions(
|
|
TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"},
|
|
qa, reply, HandlerOptions{CardSender: card, PartialUpdates: CardPartialUpdatesDirect})
|
|
if err != nil {
|
|
t.Fatalf("NewHandlerWithOptions: %v", err)
|
|
}
|
|
if err := h.Handle(context.Background(), directTextCallback()); err != nil {
|
|
t.Fatalf("Handle: %v", err)
|
|
}
|
|
if card.finalCalled || card.startCalled {
|
|
t.Fatal("a failed answer must not create a card")
|
|
}
|
|
if !reply.called || reply.lastText != qaErrorReply {
|
|
t.Fatalf("markdown fallback: called=%v text=%q", reply.called, reply.lastText)
|
|
}
|
|
}
|
|
|
|
func TestDirectModeAppliesToTheStreamingAnswerPath(t *testing.T) {
|
|
rawAnswer := "流式接口直接输出"
|
|
card := &fakeStreamingCard{finalID: "track-direct-stream"}
|
|
qa := &fakeStreamingQA{chunks: []string{"流式", "接口直接输出"}, result: core.QAResult{Answer: rawAnswer}}
|
|
reply := &fakeReply{}
|
|
h, err := NewHandlerWithOptions(
|
|
TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"},
|
|
qa, reply, HandlerOptions{CardSender: card, PartialUpdates: CardPartialUpdatesDirect})
|
|
if err != nil {
|
|
t.Fatalf("NewHandlerWithOptions: %v", err)
|
|
}
|
|
if err := h.Handle(context.Background(), directTextCallback()); err != nil {
|
|
t.Fatalf("Handle: %v", err)
|
|
}
|
|
if !qa.streamCalled {
|
|
t.Fatal("the streaming asker must be used")
|
|
}
|
|
if qa.observerCaptured == nil {
|
|
t.Fatal("the streaming asker must still receive an observer")
|
|
}
|
|
if card.startCalled || len(card.updates) != 0 {
|
|
t.Fatalf("no placeholder/partial card may be created: start=%v updates=%+v", card.startCalled, card.updates)
|
|
}
|
|
if !card.finalCalled || card.finalReq.Content != rawAnswer {
|
|
t.Fatalf("final card=%v content=%q", card.finalCalled, card.finalReq.Content)
|
|
}
|
|
}
|