fix: give card fallback a fresh deadline
This commit is contained in:
@@ -88,13 +88,14 @@ type HandlerOptions struct {
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
cfg TenantConfig
|
||||
qa QAAsker
|
||||
reply MarkdownSender
|
||||
downloader ImageDownloader
|
||||
card FinalCardSender
|
||||
now func() time.Time
|
||||
cardUpdateInterval time.Duration
|
||||
cfg TenantConfig
|
||||
qa QAAsker
|
||||
reply MarkdownSender
|
||||
downloader ImageDownloader
|
||||
card FinalCardSender
|
||||
now func() time.Time
|
||||
cardUpdateInterval time.Duration
|
||||
finalDeliveryTimeout time.Duration
|
||||
}
|
||||
|
||||
// NewHandler accepts at most one image downloader. A nil/omitted downloader
|
||||
@@ -127,13 +128,14 @@ func NewHandlerWithOptions(cfg TenantConfig, qa QAAsker, reply MarkdownSender, o
|
||||
return nil, errors.New("dingtalk handler: markdown sender is required")
|
||||
}
|
||||
return &Handler{
|
||||
cfg: cfg,
|
||||
qa: qa,
|
||||
reply: reply,
|
||||
downloader: opts.ImageDownloader,
|
||||
card: opts.CardSender,
|
||||
now: time.Now,
|
||||
cardUpdateInterval: 500 * time.Millisecond,
|
||||
cfg: cfg,
|
||||
qa: qa,
|
||||
reply: reply,
|
||||
downloader: opts.ImageDownloader,
|
||||
card: opts.CardSender,
|
||||
now: time.Now,
|
||||
cardUpdateInterval: 500 * time.Millisecond,
|
||||
finalDeliveryTimeout: defaultHTTPTimeout,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@@ -291,7 +293,7 @@ func (h *Handler) handleStreaming(ctx context.Context, webhookURL string, req co
|
||||
return h.sendReply(ctx, webhookURL, result.Answer)
|
||||
}
|
||||
|
||||
// Streaming observer with 750ms throttle
|
||||
// Streaming observer with the configured update throttle (500ms by default).
|
||||
var lastUpdate time.Time
|
||||
observer := func(fullContent string) {
|
||||
if strings.TrimSpace(fullContent) == "" {
|
||||
@@ -310,24 +312,10 @@ func (h *Handler) handleStreaming(ctx context.Context, webhookURL string, req co
|
||||
}
|
||||
|
||||
result, err := streamingQA.AskStream(ctx, req, observer)
|
||||
// Final card convergence must still be attempted when the QA context was
|
||||
// canceled or timed out; otherwise the card can remain "processing"
|
||||
// forever. Keep the independent delivery window tightly bounded.
|
||||
finalCtx, cancelFinal := context.WithTimeout(context.WithoutCancel(ctx), defaultHTTPTimeout)
|
||||
defer cancelFinal()
|
||||
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
||||
// Error or empty answer: converge to error card
|
||||
if updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, qaErrorReply, true, true); updateErr != nil {
|
||||
return h.sendReply(finalCtx, webhookURL, qaErrorReply)
|
||||
}
|
||||
return nil
|
||||
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, qaErrorReply, true)
|
||||
}
|
||||
|
||||
// Success: finalize with answer
|
||||
if updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, result.Answer, true, false); updateErr != nil {
|
||||
return h.sendReply(finalCtx, webhookURL, result.Answer)
|
||||
}
|
||||
return nil
|
||||
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, result.Answer, false)
|
||||
}
|
||||
|
||||
func (h *Handler) handleEventStreaming(ctx context.Context, webhookURL string, req core.QARequest, streamingCard StreamingCardSender, eventQA EventQAAsker) error {
|
||||
@@ -368,18 +356,32 @@ func (h *Handler) handleEventStreaming(ctx context.Context, webhookURL string, r
|
||||
// so no stale progress/partial answer can overwrite the terminal card state.
|
||||
pump.Stop()
|
||||
|
||||
finalCtx, cancelFinal := context.WithTimeout(context.WithoutCancel(ctx), defaultHTTPTimeout)
|
||||
defer cancelFinal()
|
||||
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
||||
if updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, qaErrorReply, true, true); updateErr != nil {
|
||||
return h.sendReply(finalCtx, webhookURL, qaErrorReply)
|
||||
}
|
||||
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, qaErrorReply, true)
|
||||
}
|
||||
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, result.Answer, false)
|
||||
}
|
||||
|
||||
func (h *Handler) finalizeCardOrMarkdown(ctx context.Context, webhookURL string, streamingCard StreamingCardSender, outTrackID, content string, isError bool) error {
|
||||
timeout := h.finalDeliveryTimeout
|
||||
if timeout <= 0 {
|
||||
timeout = defaultHTTPTimeout
|
||||
}
|
||||
|
||||
// Final card convergence must still be attempted when the QA context was
|
||||
// canceled or timed out; otherwise the card can remain "processing".
|
||||
finalCtx, cancelFinal := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
||||
updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, content, true, isError)
|
||||
cancelFinal()
|
||||
if updateErr == nil {
|
||||
return nil
|
||||
}
|
||||
if updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, result.Answer, true, false); updateErr != nil {
|
||||
return h.sendReply(finalCtx, webhookURL, result.Answer)
|
||||
}
|
||||
return nil
|
||||
|
||||
// The card attempt may have consumed its entire deadline. Markdown must get
|
||||
// a fresh delivery window rather than reusing the expired finalCtx.
|
||||
fallbackCtx, cancelFallback := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
||||
defer cancelFallback()
|
||||
return h.sendReply(fallbackCtx, webhookURL, content)
|
||||
}
|
||||
|
||||
func renderProgressEvent(event weknora.StreamEvent) string {
|
||||
|
||||
Reference in New Issue
Block a user