feat(dingtalk): per-frame wide-table grouping (stream keeps tables flowing while streaming)

This commit is contained in:
hermes
2026-09-18 11:55:45 +08:00
parent 92324b4242
commit dd229ec0ad
6 changed files with 117 additions and 50 deletions
+20 -13
View File
@@ -168,10 +168,11 @@ type HandlerOptions struct {
// delivery timeout, otherwise the remaining images are dropped. Zero keeps
// finalDeliveryTimeout.
OutboundRewriteTimeout time.Duration
// WideTableGroupingDisabled stops the final-frame normalizer from rewriting
// over-wide tables into grouped lists. Leave false on clients whose cards
// cannot lay out wide tables.
WideTableGroupingDisabled bool
// WideTableGrouping selects how over-wide answer tables are adapted, per
// frame: "" or "on" groups on every frame (historical), "off" never groups,
// "stream" groups only while the answer is still streaming and keeps the
// real table on the final frame. See tableGroupingForMode.
WideTableGrouping string
}
type Handler struct {
@@ -193,10 +194,12 @@ type Handler struct {
// outboundRewriteTimeout applies to the image rewrite stage only; zero
// falls back to finalDeliveryTimeout.
outboundRewriteTimeout time.Duration
// wideTableGroupingDisabled keeps the final frame's tables as tables
// instead of rewriting over-wide ones into grouped lists. Used while the
// client renders wide tables itself (PC wide-screen cards).
wideTableGroupingDisabled bool
// groupTablesPartial / groupTablesFinal decide per frame whether an
// over-wide table becomes a grouped list. Grouped lists survive the
// streaming frames where a table is still incomplete; the final frame can
// keep the real table, which the PC wide-screen card lays out itself.
groupTablesPartial bool
groupTablesFinal bool
}
// NewHandler accepts at most one image downloader. A nil/omitted downloader
@@ -228,6 +231,8 @@ func NewHandlerWithOptions(cfg TenantConfig, qa QAAsker, reply MarkdownSender, o
if reply == nil {
return nil, errors.New("dingtalk handler: markdown sender is required")
}
groupPartial, groupFinal := tableGroupingForMode(opts.WideTableGrouping)
return &Handler{
cfg: cfg,
qa: qa,
@@ -245,8 +250,9 @@ func NewHandlerWithOptions(cfg TenantConfig, qa QAAsker, reply MarkdownSender, o
cardUpdateInterval: 500 * time.Millisecond,
finalDeliveryTimeout: defaultHTTPTimeout,
outboundRewriteTimeout: opts.OutboundRewriteTimeout,
wideTableGroupingDisabled: opts.WideTableGroupingDisabled,
outboundRewriteTimeout: opts.OutboundRewriteTimeout,
groupTablesPartial: groupPartial,
groupTablesFinal: groupFinal,
}, nil
}
@@ -669,8 +675,7 @@ func (h *Handler) finalizeCardOrMarkdown(ctx context.Context, webhookURL string,
}
func (h *Handler) rewriteOutboundContent(ctx context.Context, content string) string {
options := defaultTableNormalizeOptions()
options.groupWideTables = !h.wideTableGroupingDisabled
options := tableNormalizeOptions{groupWideTables: h.groupTablesFinal}
if h.outboundRewriter == nil {
return normalizeMarkdownTables(sanitizeDisplayAnswer(content), options)
}
@@ -691,7 +696,9 @@ func (h *Handler) sanitizeOutboundPartial(ctx context.Context, content string) s
if ok {
content = sanitizer.SanitizePartial(content)
}
return h.rewriteCardImages(ctx, stableMarkdownPrefix(sanitizeDisplayAnswer(content)), false)
options := tableNormalizeOptions{groupWideTables: h.groupTablesPartial}
content = normalizeMarkdownTables(sanitizeDisplayAnswer(content), options)
return h.rewriteCardImages(ctx, stableMarkdownPrefix(content), false)
}
// rewriteCardImages converts image references in card content into DingTalk