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:
姚章浩
2026-09-20 18:15:04 +08:00
parent ba35e251be
commit e2b46faa05
4 changed files with 311 additions and 20 deletions
+99 -15
View File
@@ -67,6 +67,13 @@ type CardSender struct {
mu sync.Mutex
token string
tokenTill time.Time
// streamed keeps the text every live card currently shows, so a terminal
// write can close the stream with a frame the client has nothing to animate
// (see CardFinalWriteMode). Bounded by the number of cards in flight in one
// process; entries are dropped once the card is finalized.
contentMu sync.Mutex
streamed map[string]string
}
// CardSenderOption configures optional CardSender behavior.
@@ -247,23 +254,100 @@ func (s *CardSender) UpdateCard(ctx context.Context, outTrackID, content string,
return err
}
if err := s.finalizeCard(ctx, outTrackID, content, token, isFinalize, isError); err != nil {
return err
}
// Optionally let the terminal content land through the plain card update API
// as well: content written through the streaming endpoint is replayed by the
// client with a typewriter animation, while a plain write has no streaming
// semantics. The streaming finalize above already closed the card, so a
// failing follow-up write is logged and swallowed - it must never turn a
// delivered answer into an error.
if isFinalize && !isError && s.finalWrite.alsoPlainUpdates() {
if err := s.replaceCardContent(ctx, outTrackID, content, token); err != nil {
log.Printf("DingTalk card final write: plain update failed error=%v", err)
} else {
log.Printf("DingTalk card final write: plain update ok bytes=%d", len(content))
if !isFinalize || isError {
if err := s.finalizeCard(ctx, outTrackID, content, token, isFinalize, isError); err != nil {
return err
}
if !isFinalize {
// Remember what the card now shows: a terminal write can close the
// stream with a frame that carries no new text.
s.rememberStreamedContent(outTrackID, content)
}
return nil
}
return nil
// Terminal write. The order of the two available writes decides whether the
// client replays the finished answer with its own typewriter animation, so
// each mode sequences them explicitly - see CardFinalWriteMode.
defer s.forgetStreamedContent(outTrackID)
switch {
case s.finalWrite.refreshBeforeFinalize():
// Show the finished answer first (plain write, no streaming semantics)
// and close the stream with the very same text afterwards: the client
// has no delta left to animate, so the answer is simply there.
s.refreshCardPlain(ctx, outTrackID, content, token)
return s.finalizeCard(ctx, outTrackID, content, token, true, false)
case s.finalWrite.closeThenRefresh():
shown := s.lastStreamedContent(outTrackID)
if shown == "" {
// Nothing was streamed into this card: there is no zero-delta frame
// to close on, so refresh first and finalize after.
s.refreshCardPlain(ctx, outTrackID, content, token)
return s.finalizeCard(ctx, outTrackID, content, token, true, false)
}
// Close the stream on the text the card already shows (zero delta,
// nothing to animate), then refresh with the finished answer.
if err := s.finalizeCard(ctx, outTrackID, shown, token, true, false); err != nil {
return err
}
s.refreshCardPlain(ctx, outTrackID, content, token)
return nil
default:
if err := s.finalizeCard(ctx, outTrackID, content, token, isFinalize, isError); err != nil {
return err
}
// Optionally let the terminal content land through the plain card update
// API as well: content written through the streaming endpoint is replayed
// by the client with a typewriter animation, while a plain write has no
// streaming semantics. The streaming finalize above already closed the
// card, so a failing follow-up write is logged and swallowed - it must
// never turn a delivered answer into an error.
if s.finalWrite.alsoPlainUpdates() {
if err := s.replaceCardContent(ctx, outTrackID, content, token); err != nil {
log.Printf("DingTalk card final write: plain update failed error=%v", err)
} else {
log.Printf("DingTalk card final write: plain update ok bytes=%d", len(content))
}
}
return nil
}
}
// refreshCardPlain writes a finished answer through the plain card update API
// and logs the outcome. The write is best effort: the answer is already on its
// way through the streaming channel, so a failure here is never turned into an
// error for the caller.
func (s *CardSender) refreshCardPlain(ctx context.Context, outTrackID, content, token string) {
if err := s.replaceCardContent(ctx, outTrackID, content, token); err != nil {
log.Printf("DingTalk card final write: plain refresh failed error=%v", err)
return
}
log.Printf("DingTalk card final write: plain refresh ok bytes=%d", len(content))
}
// rememberStreamedContent records the text a card currently shows.
func (s *CardSender) rememberStreamedContent(outTrackID, content string) {
s.contentMu.Lock()
defer s.contentMu.Unlock()
if s.streamed == nil {
s.streamed = make(map[string]string)
}
s.streamed[outTrackID] = content
}
// lastStreamedContent returns the text a card currently shows, or "" when the
// card was created but never received a partial frame.
func (s *CardSender) lastStreamedContent(outTrackID string) string {
s.contentMu.Lock()
defer s.contentMu.Unlock()
return s.streamed[outTrackID]
}
// forgetStreamedContent drops the remembered text of a finalized card.
func (s *CardSender) forgetStreamedContent(outTrackID string) {
s.contentMu.Lock()
defer s.contentMu.Unlock()
delete(s.streamed, outTrackID)
}
// cardParamMap builds the template variables for a given answer. The plain card