diff --git a/cmd/dobotai/card_tables.go b/cmd/dobotai/card_tables.go index 5a45e78..dbbd5bd 100644 --- a/cmd/dobotai/card_tables.go +++ b/cmd/dobotai/card_tables.go @@ -12,13 +12,15 @@ import ( // unset / on → grouped list on every frame (historical behaviour) // off → real tables on every frame // stream → grouped list while streaming, real table on the final frame +// smooth → like "on", and every table is grouped while streaming so a +// table forming mid-answer no longer freezes the card // // The default stays "on" so an environment without the variable renders exactly // as before; DINGTALK_CARD_WIDE_TABLE_GROUP selects the other modes. func cardWideTableGroupingMode() string { mode := strings.ToLower(strings.TrimSpace(os.Getenv("DINGTALK_CARD_WIDE_TABLE_GROUP"))) switch mode { - case "off", "0", "false", "no", "stream", "streaming", "partial": + case "off", "0", "false", "no", "stream", "streaming", "partial", "smooth": log.Printf("DingTalk wide-table grouping mode=%s", mode) return mode default: diff --git a/cmd/dobotai/main_test.go b/cmd/dobotai/main_test.go index e48b876..713135c 100644 --- a/cmd/dobotai/main_test.go +++ b/cmd/dobotai/main_test.go @@ -232,6 +232,10 @@ func TestCardWideTableGroupingMode(t *testing.T) { if got := cardWideTableGroupingMode(); got != "" { t.Fatalf("on is the default and must not be forwarded as a mode, got %q", got) } + t.Setenv("DINGTALK_CARD_WIDE_TABLE_GROUP", "SMOOTH") + if got := cardWideTableGroupingMode(); got != "smooth" { + t.Fatalf("smooth must be normalised, got %q", got) + } } func TestMainDoesNotExposeWebAdministrationOrSetupOnlyMode(t *testing.T) { diff --git a/internal/channels/dingtalk/handler.go b/internal/channels/dingtalk/handler.go index d3576a0..fdf257b 100644 --- a/internal/channels/dingtalk/handler.go +++ b/internal/channels/dingtalk/handler.go @@ -169,10 +169,12 @@ type HandlerOptions struct { // delivery timeout, otherwise the remaining images are dropped. Zero keeps // finalDeliveryTimeout. OutboundRewriteTimeout time.Duration - // 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 selects how answer tables are adapted, per frame: "" + // or "on" groups over-wide tables 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, "smooth" also groups every + // table on streaming frames so a forming table never freezes the card. + // See tableGroupingForMode. WideTableGrouping string } @@ -201,6 +203,9 @@ type Handler struct { // keep the real table, which the PC wide-screen card lays out itself. groupTablesPartial bool groupTablesFinal bool + // groupAllTablesPartial groups every table on streaming frames (mode + // "smooth"): the card keeps growing while a table is still forming. + groupAllTablesPartial bool } // NewHandler accepts at most one image downloader. A nil/omitted downloader @@ -232,7 +237,7 @@ 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) + groupPartial, groupFinal, groupAllPartial := tableGroupingForMode(opts.WideTableGrouping) return &Handler{ cfg: cfg, @@ -254,6 +259,7 @@ func NewHandlerWithOptions(cfg TenantConfig, qa QAAsker, reply MarkdownSender, o outboundRewriteTimeout: opts.OutboundRewriteTimeout, groupTablesPartial: groupPartial, groupTablesFinal: groupFinal, + groupAllTablesPartial: groupAllPartial, }, nil } @@ -699,7 +705,7 @@ func (h *Handler) sanitizeOutboundPartial(ctx context.Context, content string) s if ok { content = sanitizer.SanitizePartial(content) } - options := tableNormalizeOptions{groupWideTables: h.groupTablesPartial} + options := tableNormalizeOptions{groupWideTables: h.groupTablesPartial, groupAllTables: h.groupAllTablesPartial} content = normalizeMarkdownTables(sanitizeDisplayAnswer(content), options) return h.rewriteCardImages(ctx, stableMarkdownPrefix(content), false) } diff --git a/internal/channels/dingtalk/stream_table_fold_test.go b/internal/channels/dingtalk/stream_table_fold_test.go new file mode 100644 index 0000000..d302e38 --- /dev/null +++ b/internal/channels/dingtalk/stream_table_fold_test.go @@ -0,0 +1,66 @@ +package dingtalk + +import ( + "context" + "strings" + "testing" +) + +// smoothAnswer mirrors a live answer shape: prose, a compact table, more prose, +// a second table, then a long tail. With the historical modes the streaming +// card froze at the first table and the rest arrived in one final frame. +func smoothAnswer() string { + return "Nova 是新一代工业协作机器人平台。\n\n" + + "| Model | Payload | Reach |\n| :--- | :--- | :--- |\n| Nova 3 | 3 kg | 626 mm |\n| Nova 5 | 5 kg | 900 mm |\n\n" + + "选型时先看负载与臂展,再看控制柜与接口。\n\n" + + "| Interface | Count |\n| :--- | :--- |\n| DI | 16 |\n| DO | 16 |\n\n" + + strings.Repeat("最后一段补充说明。", 30) +} + +func streamedFrames(t *testing.T, mode, answer string) (frames, lastPartial int) { + t.Helper() + h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "t", Channel: "dingtalk"}, &fakeQA{}, &fakeReply{}, HandlerOptions{WideTableGrouping: mode}) + if err != nil { + t.Fatalf("handler: %v", err) + } + prev := "" + for n := 1; n <= len(answer); n += 7 { // ~model chunk granularity + p := h.sanitizeOutboundPartial(context.Background(), answer[:n]) + if p != prev { + frames++ + prev = p + } + } + return frames, len(prev) +} + +// TestSmoothModeKeepsStreamingThroughFormingTable guards the regression where a +// half-formed table truncated every streaming frame (stableMarkdownPrefix), so +// the card appeared to stop and dumped the remaining bytes at finalize. +func TestSmoothModeKeepsStreamingThroughFormingTable(t *testing.T) { + answer := smoothAnswer() + smoothFrames, smoothLast := streamedFrames(t, "smooth", answer) + streamFrames, streamLast := streamedFrames(t, "stream", answer) + if smoothFrames <= streamFrames { + t.Fatalf("smooth mode must deliver more streaming frames: smooth=%d stream=%d", smoothFrames, streamFrames) + } + if smoothLast < len(answer)*9/10 { + t.Fatalf("smooth streaming must deliver the growing answer: last=%d answer=%d", smoothLast, len(answer)) + } + if streamLast >= len(answer)*9/10 { + t.Fatalf("stream mode is expected to hold the table tail back, got last=%d", streamLast) + } +} + +// TestSmoothModeKeepsRealCompactTableOnFinalFrame pins that the fix only +// changes streaming frames: a compact table still lands as a real table. +func TestSmoothModeKeepsRealCompactTableOnFinalFrame(t *testing.T) { + h, err := NewHandlerWithOptions(TenantConfig{TenantKey: "t", Channel: "dingtalk"}, &fakeQA{}, &fakeReply{}, HandlerOptions{WideTableGrouping: "smooth"}) + if err != nil { + t.Fatalf("handler: %v", err) + } + final := h.rewriteOutboundContent(context.Background(), smoothAnswer()) + if !strings.Contains(final, "| Model | Payload | Reach |") { + t.Fatalf("final frame must keep the real compact table, got %q", final) + } +} diff --git a/internal/channels/dingtalk/table_normalize.go b/internal/channels/dingtalk/table_normalize.go index e8f4ed2..3c0e0c9 100644 --- a/internal/channels/dingtalk/table_normalize.go +++ b/internal/channels/dingtalk/table_normalize.go @@ -17,6 +17,11 @@ type tableNormalizeOptions struct { // lists (bold row heading + "- header:value" lines). Disable it when the // client lays out wide tables itself, so the answer keeps its real table. groupWideTables bool + // groupAllTables rewrites every table into a grouped list, compact ones + // included. Streaming frames use it so a table that is still forming never + // freezes the card; the final frame keeps the real table whenever it lays + // out safely. + groupAllTables bool } func defaultTableNormalizeOptions() tableNormalizeOptions { @@ -30,14 +35,19 @@ func defaultTableNormalizeOptions() tableNormalizeOptions { // ""/"on" group on every frame // "off" never group: real tables everywhere // "stream" group while streaming, real table on the final frame -func tableGroupingForMode(mode string) (partial, final bool) { +// "smooth" group on every frame and group every table (compact ones too) +// while streaming, so a table forming mid-answer keeps the card +// growing instead of freezing it until the final frame +func tableGroupingForMode(mode string) (partial, final, allPartial bool) { switch strings.ToLower(strings.TrimSpace(mode)) { case "off", "0", "false", "no": - return false, false + return false, false, false case "stream", "streaming", "partial": - return true, false + return true, false, false + case "smooth": + return true, true, true default: - return true, true + return true, true, false } } @@ -98,7 +108,7 @@ func normalizeTableBlock(rows []markdownStreamLine, options tableNormalizeOption } } header := cells[0] - if maxWidth <= maxTableCellVisualWidth || !options.groupWideTables { + if !options.groupAllTables && (maxWidth <= maxTableCellVisualWidth || !options.groupWideTables) { // Compact tables keep their exact original bytes unless a cell carries //
, which the card renderer mishandles inside cells. The same path // serves over-wide tables when grouping is disabled: the table stays a diff --git a/internal/channels/dingtalk/table_normalize_test.go b/internal/channels/dingtalk/table_normalize_test.go index c0a7a5c..14a5d0a 100644 --- a/internal/channels/dingtalk/table_normalize_test.go +++ b/internal/channels/dingtalk/table_normalize_test.go @@ -83,9 +83,10 @@ func TestNormalizeKeepsWideTableWhenGroupingDisabled(t *testing.T) { func TestTableGroupingForMode(t *testing.T) { cases := []struct { - mode string - wantPartial bool - wantFinal bool + mode string + wantPartial bool + wantFinal bool + wantAllPartial bool }{ {mode: "", wantPartial: true, wantFinal: true}, {mode: "on", wantPartial: true, wantFinal: true}, @@ -94,11 +95,13 @@ func TestTableGroupingForMode(t *testing.T) { {mode: "NO", wantPartial: false, wantFinal: false}, {mode: "stream", wantPartial: true, wantFinal: false}, {mode: " Stream ", wantPartial: true, wantFinal: false}, + {mode: "smooth", wantPartial: true, wantFinal: true, wantAllPartial: true}, + {mode: " Smooth ", wantPartial: true, wantFinal: true, wantAllPartial: true}, } for _, tc := range cases { - partial, final := tableGroupingForMode(tc.mode) - if partial != tc.wantPartial || final != tc.wantFinal { - t.Fatalf("tableGroupingForMode(%q) = (%v,%v), want (%v,%v)", tc.mode, partial, final, tc.wantPartial, tc.wantFinal) + partial, final, allPartial := tableGroupingForMode(tc.mode) + if partial != tc.wantPartial || final != tc.wantFinal || allPartial != tc.wantAllPartial { + t.Fatalf("tableGroupingForMode(%q) = (%v,%v,%v), want (%v,%v,%v)", tc.mode, partial, final, allPartial, tc.wantPartial, tc.wantFinal, tc.wantAllPartial) } } }