Files
dobotai-platform/internal/channels/dingtalk/card_delivery_mode.go
T
姚章浩 e2b46faa05 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.
2026-09-20 18:15:04 +08:00

132 lines
6.4 KiB
Go

package dingtalk
import "strings"
// CardPartialUpdatesMode selects how much of an answer is written into the card
// while the answer is still being produced.
//
// partial (default) → the card is created with the "processing" placeholder and
// partial answer/progress frames are pushed as they arrive,
// throttled by cardUpdateInterval (historical behaviour)
// final → same placeholder card, but no partial frame at all: the
// finished answer lands in the single final update
// direct → no placeholder card; one card is created carrying the
// finished answer
//
// "final" and "direct" remove the redraw loop (and with it the frozen card and
// the end-of-answer jump) at the cost of not showing interim progress.
type CardPartialUpdatesMode string
const (
CardPartialUpdatesPartial CardPartialUpdatesMode = "partial"
CardPartialUpdatesFinal CardPartialUpdatesMode = "final"
CardPartialUpdatesDirect CardPartialUpdatesMode = "direct"
)
// ParseCardPartialUpdatesMode maps DINGTALK_CARD_PARTIAL_UPDATES onto a mode.
// Unset or unrecognised values keep the historical partial streaming so an
// environment without the variable renders exactly as before.
func ParseCardPartialUpdatesMode(raw string) CardPartialUpdatesMode {
switch strings.ToLower(strings.TrimSpace(raw)) {
case "final", "off", "0", "false", "no", "answer", "answer_only":
return CardPartialUpdatesFinal
case "direct", "once", "single":
return CardPartialUpdatesDirect
default:
return CardPartialUpdatesPartial
}
}
// streamsPartialFrames reports whether partial answer/progress frames may be
// pushed into the card. Only the explicit "final" and "direct" modes suppress
// them, so an unset or unknown mode keeps the historical streaming.
func (m CardPartialUpdatesMode) streamsPartialFrames() bool {
return m != CardPartialUpdatesFinal && m != CardPartialUpdatesDirect
}
// placeholderCardRequested reports whether the card is created up front with the
// "processing" placeholder. Only "direct" waits for the finished answer before
// creating the card.
func (m CardPartialUpdatesMode) placeholderCardRequested() bool {
return m != CardPartialUpdatesDirect
}
// CardFinalWriteMode selects how the terminal write of a finished answer lands
// in the card.
//
// streaming (default) → the finished answer is written with the card streaming
// API and isFinalize=true (historical behaviour). DingTalk
// renders content that arrives through the streaming
// endpoint with a typewriter animation driven by the
// client itself: an answer that is already complete on the
// server can still be replayed character by character, and
// a client that is not in the foreground pauses that
// animation - which reads as "the streaming stopped and it
// resumes when I switch back", long after the server has
// pushed the final frame.
// update → after the streaming finalize, the same finished answer is
// written once more through the plain card update API
// (PUT /v1.0/card/instances). That write carries no
// streaming semantics, so the terminal state is applied to
// the card without leaving a typewriter queue behind.
// NOTE: this order is the one that blanked a live card on
// 2026-09-20 (the finalize frame starts the client's
// typewriter and the plain write lands in the middle of it);
// kept only to reproduce that regression.
// refresh → the finished answer is written through the plain card
// update API FIRST, so the card shows it without any
// typewriter queue, and only then is the stream closed by a
// finalize frame that carries exactly the same text. The
// client has no delta left to animate, so the answer is
// simply there when it arrives.
// close-refresh → the stream is closed with a frame carrying the text the
// card already shows (zero delta, nothing to animate) and
// the finished answer is written through the plain update
// API afterwards. Used when the plain write must land on a
// stream that is already closed and quiet.
//
// The default stays "streaming" so an environment without the variable behaves
// exactly as before; DINGTALK_CARD_FINAL_WRITE selects the other mode.
type CardFinalWriteMode string
const (
CardFinalWriteStreaming CardFinalWriteMode = "streaming"
CardFinalWriteUpdate CardFinalWriteMode = "update"
CardFinalWriteRefresh CardFinalWriteMode = "refresh"
CardFinalWriteCloseRefresh CardFinalWriteMode = "close-refresh"
)
// ParseCardFinalWriteMode maps DINGTALK_CARD_FINAL_WRITE onto a mode. Unset or
// unrecognised values keep the historical streaming finalize.
func ParseCardFinalWriteMode(raw string) CardFinalWriteMode {
switch strings.ToLower(strings.TrimSpace(raw)) {
case "update", "plain", "plain_update", "on", "true", "1", "yes":
return CardFinalWriteUpdate
case "refresh", "refresh_first", "plain_first", "refresh_then_finalize", "refresh-first", "plain-first":
return CardFinalWriteRefresh
case "close-refresh", "close_refresh", "finalize_then_refresh", "refresh_after_finalize", "close-then-refresh":
return CardFinalWriteCloseRefresh
default:
return CardFinalWriteStreaming
}
}
// alsoPlainUpdates reports whether the finished answer must additionally be
// written through the plain (non-streaming) card update endpoint after the
// streaming finalize.
func (m CardFinalWriteMode) alsoPlainUpdates() bool {
return m == CardFinalWriteUpdate
}
// refreshBeforeFinalize reports whether the finished answer goes through the
// plain card update endpoint before the stream is closed.
func (m CardFinalWriteMode) refreshBeforeFinalize() bool {
return m == CardFinalWriteRefresh
}
// closeThenRefresh reports whether the stream is closed on the text already on
// the card and the finished answer is refreshed afterwards.
func (m CardFinalWriteMode) closeThenRefresh() bool {
return m == CardFinalWriteCloseRefresh
}