feat(dingtalk): add card terminal-write order modes (refresh, close-refresh)
A DingTalk AI card replays streamed content with a typewriter animation the
client drives itself, so a finished answer can still be typed out character
by character - and a card that is not in the foreground pauses that replay.
DINGTALK_CARD_FINAL_WRITE now sequences the two terminal writes explicitly:
streaming (default) streaming finalize only (unchanged)
update finalize, then a plain write (observed to blank the card)
refresh plain write of the finished answer, then a zero-delta
finalize carrying the same text
close-refresh zero-delta finalize on the text the card already shows,
then a plain write of the finished answer
The sender remembers the text each live card shows so a terminal write can
close the stream with a frame the client has nothing to animate; a failing
plain write is logged and never fails the delivery.
This commit is contained in:
@@ -28,6 +28,11 @@ type finalWriteTestServer struct {
|
||||
lastUpdateMethod string
|
||||
lastUpdateHeader string
|
||||
|
||||
// callSeq records the endpoint order ("create"/"stream"/"update") so the
|
||||
// write ORDER of a terminal answer can be asserted, not just the counts.
|
||||
callSeq []string
|
||||
streamBodies []map[string]any
|
||||
|
||||
streamStatus int
|
||||
streamResponse any
|
||||
updateStatus int
|
||||
@@ -50,13 +55,16 @@ func newFinalWriteTestServer() *finalWriteTestServer {
|
||||
json.NewEncoder(w).Encode(map[string]any{"accessToken": "test-access-token", "expireIn": 3600})
|
||||
case "/create":
|
||||
ts.createCalls++
|
||||
ts.callSeq = append(ts.callSeq, "create")
|
||||
ts.lastCreateBody = make(map[string]any)
|
||||
json.NewDecoder(r.Body).Decode(&ts.lastCreateBody)
|
||||
json.NewEncoder(w).Encode(ts.createResponse)
|
||||
case "/stream":
|
||||
ts.streamCalls++
|
||||
ts.callSeq = append(ts.callSeq, "stream")
|
||||
ts.lastStreamBody = make(map[string]any)
|
||||
json.NewDecoder(r.Body).Decode(&ts.lastStreamBody)
|
||||
ts.streamBodies = append(ts.streamBodies, ts.lastStreamBody)
|
||||
if ts.streamStatus != http.StatusOK {
|
||||
w.WriteHeader(ts.streamStatus)
|
||||
return
|
||||
@@ -64,6 +72,7 @@ func newFinalWriteTestServer() *finalWriteTestServer {
|
||||
json.NewEncoder(w).Encode(ts.streamResponse)
|
||||
case "/update":
|
||||
ts.updateCalls++
|
||||
ts.callSeq = append(ts.callSeq, "update")
|
||||
ts.lastUpdateMethod = r.Method
|
||||
ts.lastUpdateHeader = r.Header.Get("x-acs-dingtalk-access-token")
|
||||
ts.lastUpdateBody = make(map[string]any)
|
||||
@@ -291,3 +300,162 @@ func TestCardFinalWriteUpdateModeAppliesToSendFinalCard(t *testing.T) {
|
||||
t.Fatalf("plain update content=%v", ts.paramMap(t)["content"])
|
||||
}
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Order modes: refresh (plain first) and close-refresh (zero-delta finalize
|
||||
// first). Both exist because the client replays streamed text with its own
|
||||
// typewriter animation, so the ORDER of the two terminal writes decides what
|
||||
// the user sees when the answer is ready.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
func wantCallSeq(t *testing.T, got, want []string) {
|
||||
t.Helper()
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("call sequence=%v, want %v", got, want)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("call sequence=%v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardFinalWriteRefreshModeRefreshesBeforeFinalize(t *testing.T) {
|
||||
ts := newFinalWriteTestServer()
|
||||
defer ts.Close()
|
||||
sender := ts.sender(t, CardFinalWriteRefresh)
|
||||
|
||||
if _, err := sender.StartCard(context.Background(), CardRequest{
|
||||
ConversationType: core.ConversationDirect,
|
||||
ExternalUserID: "user-1",
|
||||
Content: "占位",
|
||||
}); err != nil {
|
||||
t.Fatalf("StartCard: %v", err)
|
||||
}
|
||||
if err := sender.UpdateCard(context.Background(), "track-1", "部分", false, false); err != nil {
|
||||
t.Fatalf("partial UpdateCard: %v", err)
|
||||
}
|
||||
if err := sender.UpdateCard(context.Background(), "track-1", "完整答案", true, false); err != nil {
|
||||
t.Fatalf("terminal UpdateCard: %v", err)
|
||||
}
|
||||
|
||||
// The finished answer goes out through the plain update BEFORE the frame
|
||||
// that closes the stream.
|
||||
wantCallSeq(t, ts.callSeq, []string{"create", "stream", "update", "stream"})
|
||||
if got := ts.paramMap(t)["content"]; got != "完整答案" {
|
||||
t.Fatalf("plain refresh content=%v, want the finished answer", got)
|
||||
}
|
||||
if len(ts.streamBodies) != 2 {
|
||||
t.Fatalf("stream frames=%d, want 2", len(ts.streamBodies))
|
||||
}
|
||||
closing := ts.streamBodies[1]
|
||||
if closing["content"] != "完整答案" || closing["isFinalize"] != true {
|
||||
t.Fatalf("closing frame=%v, want the finished answer with isFinalize=true", closing)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardFinalWriteRefreshModeKeepsDeliveryWhenPlainRefreshFails(t *testing.T) {
|
||||
ts := newFinalWriteTestServer()
|
||||
defer ts.Close()
|
||||
ts.updateStatus = http.StatusInternalServerError
|
||||
sender := ts.sender(t, CardFinalWriteRefresh)
|
||||
|
||||
if err := sender.UpdateCard(context.Background(), "track-1", "完整答案", true, false); err != nil {
|
||||
t.Fatalf("a failed plain refresh must not fail the delivery, got %v", err)
|
||||
}
|
||||
if ts.updateCalls != 1 || ts.streamCalls != 1 {
|
||||
t.Fatalf("update/stream=%d/%d, want 1/1 (the closing frame still goes out)", ts.updateCalls, ts.streamCalls)
|
||||
}
|
||||
if ts.lastStreamBody["isFinalize"] != true || ts.lastStreamBody["content"] != "完整答案" {
|
||||
t.Fatalf("closing frame=%v", ts.lastStreamBody)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardFinalWriteCloseRefreshModeClosesOnShownTextThenRefreshes(t *testing.T) {
|
||||
ts := newFinalWriteTestServer()
|
||||
defer ts.Close()
|
||||
sender := ts.sender(t, CardFinalWriteCloseRefresh)
|
||||
|
||||
if _, err := sender.StartCard(context.Background(), CardRequest{
|
||||
ConversationType: core.ConversationDirect,
|
||||
ExternalUserID: "user-1",
|
||||
Content: "占位",
|
||||
}); err != nil {
|
||||
t.Fatalf("StartCard: %v", err)
|
||||
}
|
||||
if err := sender.UpdateCard(context.Background(), "track-1", "部分", false, false); err != nil {
|
||||
t.Fatalf("partial UpdateCard: %v", err)
|
||||
}
|
||||
if err := sender.UpdateCard(context.Background(), "track-1", "完整答案", true, false); err != nil {
|
||||
t.Fatalf("terminal UpdateCard: %v", err)
|
||||
}
|
||||
|
||||
// The closing frame carries the text the card already shows (zero delta, so
|
||||
// the client has nothing to animate) and the finished answer follows through
|
||||
// the plain update.
|
||||
wantCallSeq(t, ts.callSeq, []string{"create", "stream", "stream", "update"})
|
||||
if len(ts.streamBodies) != 2 {
|
||||
t.Fatalf("stream frames=%d, want 2", len(ts.streamBodies))
|
||||
}
|
||||
closing := ts.streamBodies[1]
|
||||
if closing["content"] != "部分" || closing["isFinalize"] != true {
|
||||
t.Fatalf("closing frame=%v, want the previously shown text with isFinalize=true", closing)
|
||||
}
|
||||
if got := ts.paramMap(t)["content"]; got != "完整答案" {
|
||||
t.Fatalf("plain refresh content=%v, want the finished answer", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardFinalWriteCloseRefreshModeRefreshesFirstWhenNothingStreamed(t *testing.T) {
|
||||
ts := newFinalWriteTestServer()
|
||||
defer ts.Close()
|
||||
sender := ts.sender(t, CardFinalWriteCloseRefresh)
|
||||
|
||||
// A card that never received a partial frame has no zero-delta frame to
|
||||
// close on, so the refresh has to go first.
|
||||
if err := sender.UpdateCard(context.Background(), "track-1", "完整答案", true, false); err != nil {
|
||||
t.Fatalf("terminal UpdateCard: %v", err)
|
||||
}
|
||||
wantCallSeq(t, ts.callSeq, []string{"update", "stream"})
|
||||
if ts.lastStreamBody["isFinalize"] != true || ts.lastStreamBody["content"] != "完整答案" {
|
||||
t.Fatalf("closing frame=%v", ts.lastStreamBody)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardFinalWriteCloseRefreshModeKeepsDeliveryWhenPlainRefreshFails(t *testing.T) {
|
||||
ts := newFinalWriteTestServer()
|
||||
defer ts.Close()
|
||||
ts.updateStatus = http.StatusInternalServerError
|
||||
sender := ts.sender(t, CardFinalWriteCloseRefresh)
|
||||
|
||||
if err := sender.UpdateCard(context.Background(), "track-1", "部分", false, false); err != nil {
|
||||
t.Fatalf("partial UpdateCard: %v", err)
|
||||
}
|
||||
if err := sender.UpdateCard(context.Background(), "track-1", "完整答案", true, false); err != nil {
|
||||
t.Fatalf("a failed plain refresh must not fail the delivery, got %v", err)
|
||||
}
|
||||
wantCallSeq(t, ts.callSeq, []string{"stream", "stream", "update"})
|
||||
if ts.lastStreamBody["isFinalize"] != true || ts.lastStreamBody["content"] != "部分" {
|
||||
t.Fatalf("closing frame=%v", ts.lastStreamBody)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCardFinalWriteParserAcceptsOrderValues(t *testing.T) {
|
||||
for raw, want := range map[string]CardFinalWriteMode{
|
||||
"": CardFinalWriteStreaming,
|
||||
"streaming": CardFinalWriteStreaming,
|
||||
"off": CardFinalWriteStreaming,
|
||||
"nonsense": CardFinalWriteStreaming,
|
||||
"update": CardFinalWriteUpdate,
|
||||
"refresh": CardFinalWriteRefresh,
|
||||
"plain_first": CardFinalWriteRefresh,
|
||||
"refresh-first": CardFinalWriteRefresh,
|
||||
"close_refresh": CardFinalWriteCloseRefresh,
|
||||
"close-refresh": CardFinalWriteCloseRefresh,
|
||||
"CLOSE-REFRESH ": CardFinalWriteCloseRefresh,
|
||||
} {
|
||||
if got := ParseCardFinalWriteMode(raw); got != want {
|
||||
t.Fatalf("ParseCardFinalWriteMode(%q)=%q, want %q", raw, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user