fix: give card fallback a fresh deadline
This commit is contained in:
@@ -88,13 +88,14 @@ type HandlerOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Handler struct {
|
type Handler struct {
|
||||||
cfg TenantConfig
|
cfg TenantConfig
|
||||||
qa QAAsker
|
qa QAAsker
|
||||||
reply MarkdownSender
|
reply MarkdownSender
|
||||||
downloader ImageDownloader
|
downloader ImageDownloader
|
||||||
card FinalCardSender
|
card FinalCardSender
|
||||||
now func() time.Time
|
now func() time.Time
|
||||||
cardUpdateInterval time.Duration
|
cardUpdateInterval time.Duration
|
||||||
|
finalDeliveryTimeout time.Duration
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewHandler accepts at most one image downloader. A nil/omitted downloader
|
// 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 nil, errors.New("dingtalk handler: markdown sender is required")
|
||||||
}
|
}
|
||||||
return &Handler{
|
return &Handler{
|
||||||
cfg: cfg,
|
cfg: cfg,
|
||||||
qa: qa,
|
qa: qa,
|
||||||
reply: reply,
|
reply: reply,
|
||||||
downloader: opts.ImageDownloader,
|
downloader: opts.ImageDownloader,
|
||||||
card: opts.CardSender,
|
card: opts.CardSender,
|
||||||
now: time.Now,
|
now: time.Now,
|
||||||
cardUpdateInterval: 500 * time.Millisecond,
|
cardUpdateInterval: 500 * time.Millisecond,
|
||||||
|
finalDeliveryTimeout: defaultHTTPTimeout,
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -291,7 +293,7 @@ func (h *Handler) handleStreaming(ctx context.Context, webhookURL string, req co
|
|||||||
return h.sendReply(ctx, webhookURL, result.Answer)
|
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
|
var lastUpdate time.Time
|
||||||
observer := func(fullContent string) {
|
observer := func(fullContent string) {
|
||||||
if strings.TrimSpace(fullContent) == "" {
|
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)
|
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) == "" {
|
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
||||||
// Error or empty answer: converge to error card
|
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, qaErrorReply, true)
|
||||||
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, result.Answer, false)
|
||||||
// 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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *Handler) handleEventStreaming(ctx context.Context, webhookURL string, req core.QARequest, streamingCard StreamingCardSender, eventQA EventQAAsker) error {
|
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.
|
// so no stale progress/partial answer can overwrite the terminal card state.
|
||||||
pump.Stop()
|
pump.Stop()
|
||||||
|
|
||||||
finalCtx, cancelFinal := context.WithTimeout(context.WithoutCancel(ctx), defaultHTTPTimeout)
|
|
||||||
defer cancelFinal()
|
|
||||||
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
if err != nil || strings.TrimSpace(result.Answer) == "" {
|
||||||
if updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, qaErrorReply, true, true); updateErr != nil {
|
return h.finalizeCardOrMarkdown(ctx, webhookURL, streamingCard, outTrackID, qaErrorReply, true)
|
||||||
return h.sendReply(finalCtx, webhookURL, qaErrorReply)
|
}
|
||||||
}
|
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
|
return nil
|
||||||
}
|
}
|
||||||
if updateErr := streamingCard.UpdateCard(finalCtx, outTrackID, result.Answer, true, false); updateErr != nil {
|
|
||||||
return h.sendReply(finalCtx, webhookURL, result.Answer)
|
// The card attempt may have consumed its entire deadline. Markdown must get
|
||||||
}
|
// a fresh delivery window rather than reusing the expired finalCtx.
|
||||||
return nil
|
fallbackCtx, cancelFallback := context.WithTimeout(context.WithoutCancel(ctx), timeout)
|
||||||
|
defer cancelFallback()
|
||||||
|
return h.sendReply(fallbackCtx, webhookURL, content)
|
||||||
}
|
}
|
||||||
|
|
||||||
func renderProgressEvent(event weknora.StreamEvent) string {
|
func renderProgressEvent(event weknora.StreamEvent) string {
|
||||||
|
|||||||
@@ -1596,3 +1596,61 @@ func TestHandleEventStreamingErrorConvergesCard(t *testing.T) {
|
|||||||
t.Fatalf("error leaked or markdown duplicated: content=%q markdown=%v", last.content, reply.called)
|
t.Fatalf("error leaked or markdown duplicated: content=%q markdown=%v", last.content, reply.called)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type deadlineFinalCard struct{}
|
||||||
|
|
||||||
|
func (deadlineFinalCard) SendFinalCard(context.Context, CardRequest) (string, error) {
|
||||||
|
return "track-deadline", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (deadlineFinalCard) StartCard(context.Context, CardRequest) (string, error) {
|
||||||
|
return "track-deadline", nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (deadlineFinalCard) UpdateCard(ctx context.Context, _ string, _ string, isFinalize, _ bool) error {
|
||||||
|
if !isFinalize {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
<-ctx.Done()
|
||||||
|
return ctx.Err()
|
||||||
|
}
|
||||||
|
|
||||||
|
type contextCheckingReply struct {
|
||||||
|
called bool
|
||||||
|
contextErr error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *contextCheckingReply) SendMarkdown(ctx context.Context, _, _, _ string) error {
|
||||||
|
r.called = true
|
||||||
|
r.contextErr = ctx.Err()
|
||||||
|
return r.contextErr
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleEventStreamingFinalTimeoutUsesFreshMarkdownContext(t *testing.T) {
|
||||||
|
qa := &fakeEventQA{
|
||||||
|
fakeStreamingQA: fakeStreamingQA{result: core.QAResult{Answer: "完整答案"}},
|
||||||
|
events: []weknora.StreamEvent{{ResponseType: "answer", FullAnswer: "完整答案", Done: true}},
|
||||||
|
}
|
||||||
|
reply := &contextCheckingReply{}
|
||||||
|
h, err := NewHandlerWithOptions(
|
||||||
|
TenantConfig{TenantKey: "tenant-a", Channel: "dingtalk"},
|
||||||
|
qa,
|
||||||
|
reply,
|
||||||
|
HandlerOptions{CardSender: deadlineFinalCard{}},
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("NewHandlerWithOptions: %v", err)
|
||||||
|
}
|
||||||
|
h.cardUpdateInterval = time.Hour
|
||||||
|
h.finalDeliveryTimeout = 5 * time.Millisecond
|
||||||
|
|
||||||
|
if err := h.Handle(context.Background(), directTextCallback()); err != nil {
|
||||||
|
t.Fatalf("Markdown fallback must receive a fresh context: %v", err)
|
||||||
|
}
|
||||||
|
if !reply.called {
|
||||||
|
t.Fatal("Markdown fallback was not called")
|
||||||
|
}
|
||||||
|
if reply.contextErr != nil {
|
||||||
|
t.Fatalf("Markdown fallback received expired context: %v", reply.contextErr)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user