feat(dingtalk): stabilize multimodal answer workflows
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -38,6 +39,15 @@ type RecordInserter interface {
|
||||
InsertRecord(ctx context.Context, row Row) (string, error)
|
||||
}
|
||||
|
||||
// AnswerRewriter optionally transforms the answer before it is written to the
|
||||
// question table, applying the same outbound conversion used for DingTalk
|
||||
// delivery (resource images to stable HTTPS, document links, anchor cleanup).
|
||||
// When nil or when Rewrite returns an error, the original answer is written so
|
||||
// the record is never lost.
|
||||
type AnswerRewriter interface {
|
||||
Rewrite(ctx context.Context, content string) (string, error)
|
||||
}
|
||||
|
||||
// DispatcherStats holds the outcome counters for a single RunOnce invocation.
|
||||
type DispatcherStats struct {
|
||||
Claimed int
|
||||
@@ -51,6 +61,7 @@ type DispatcherOptions struct {
|
||||
MaxAttempts int
|
||||
ClaimLease time.Duration
|
||||
SendTimeout time.Duration
|
||||
Rewriter AnswerRewriter
|
||||
}
|
||||
|
||||
// Dispatcher runs the background dispatch loop that claims pending exports
|
||||
@@ -58,6 +69,7 @@ type DispatcherOptions struct {
|
||||
type Dispatcher struct {
|
||||
repo ExportRepository
|
||||
inserter RecordInserter
|
||||
rewriter AnswerRewriter
|
||||
|
||||
batch int
|
||||
maxAttempts int
|
||||
@@ -96,8 +108,10 @@ func NewDispatcher(repo ExportRepository, inserter RecordInserter, opts *Dispatc
|
||||
maxAttempts := 5
|
||||
claimLease := 5 * time.Minute
|
||||
sendTimeout := 10 * time.Second
|
||||
var rewriter AnswerRewriter
|
||||
|
||||
if opts != nil {
|
||||
rewriter = opts.Rewriter
|
||||
if opts.Batch < 0 {
|
||||
return nil, fmt.Errorf("questiontable dispatcher: batch must be >= 0, got %d", opts.Batch)
|
||||
}
|
||||
@@ -130,6 +144,7 @@ func NewDispatcher(repo ExportRepository, inserter RecordInserter, opts *Dispatc
|
||||
return &Dispatcher{
|
||||
repo: repo,
|
||||
inserter: inserter,
|
||||
rewriter: rewriter,
|
||||
batch: batch,
|
||||
maxAttempts: maxAttempts,
|
||||
claimLease: claimLease,
|
||||
@@ -165,8 +180,18 @@ func (d *Dispatcher) RunOnce(ctx context.Context) (DispatcherStats, error) {
|
||||
return stats, fmt.Errorf("questiontable dispatcher: context cancelled during dispatch: %w", err)
|
||||
}
|
||||
|
||||
row := item.Row
|
||||
if err := d.rewriteAnswer(ctx, &row); err != nil {
|
||||
// Rewriting is best-effort: the original answer is written so the
|
||||
// question-table record is never lost. Only the row is skipped when
|
||||
// the parent context is cancelled.
|
||||
if ctx.Err() != nil {
|
||||
return stats, fmt.Errorf("questiontable dispatcher: context cancelled during rewrite: %w", ctx.Err())
|
||||
}
|
||||
}
|
||||
|
||||
sendCtx, cancel := context.WithTimeout(ctx, d.sendTimeout)
|
||||
recordID, insertErr := d.inserter.InsertRecord(sendCtx, item.Row)
|
||||
recordID, insertErr := d.inserter.InsertRecord(sendCtx, row)
|
||||
cancel()
|
||||
|
||||
if insertErr != nil {
|
||||
@@ -189,3 +214,20 @@ func (d *Dispatcher) RunOnce(ctx context.Context) (DispatcherStats, error) {
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
||||
// rewriteAnswer transforms the outbound answer when an AnswerRewriter is
|
||||
// configured. A nil rewriter or a rewriter error leaves the original answer
|
||||
// intact; the row is never skipped for a rewriter error.
|
||||
func (d *Dispatcher) rewriteAnswer(ctx context.Context, row *Row) error {
|
||||
if d.rewriter == nil {
|
||||
return nil
|
||||
}
|
||||
rewritten, err := d.rewriter.Rewrite(ctx, row.Answer)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if strings.TrimSpace(rewritten) != "" {
|
||||
row.Answer = rewritten
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user