fix(im): harden stream finalize and drop collapsed think header
Add agent complete wait timeout and answer fallbacks for cancel paths, restore Telegram Markdown on finalize with plain-text retry, and show answer-only final IM frames instead of the collapsed think summary.
This commit is contained in:
+65
-20
@@ -41,6 +41,9 @@ const (
|
||||
// streamFlushInterval is how often buffered stream content is flushed to the IM platform.
|
||||
// This prevents API rate-limiting while keeping perceived latency low.
|
||||
streamFlushInterval = 300 * time.Millisecond
|
||||
// agentCompleteWaitTimeout bounds how long IM waits for EventAgentComplete after
|
||||
// the final answer stream finishes, preventing indefinite hangs.
|
||||
agentCompleteWaitTimeout = 10 * time.Second
|
||||
)
|
||||
|
||||
// imCitationTagRe matches inline citation tags produced by the agent pipeline.
|
||||
@@ -586,6 +589,50 @@ func applyIMCompleteDataToMessage(msg *types.Message, data event.AgentCompleteDa
|
||||
}
|
||||
}
|
||||
|
||||
// waitForIMAgentComplete blocks until EventAgentComplete, ctx cancellation, or timeout.
|
||||
func waitForIMAgentComplete(ctx context.Context, completeDone <-chan struct{}, sessionID string) {
|
||||
timer := time.NewTimer(agentCompleteWaitTimeout)
|
||||
defer timer.Stop()
|
||||
select {
|
||||
case <-completeDone:
|
||||
case <-ctx.Done():
|
||||
logger.Warnf(ctx, "[IM] QA context ended before agent complete event: session=%s", sessionID)
|
||||
case <-timer.C:
|
||||
logger.Warnf(ctx, "[IM] Timed out waiting for agent complete event: session=%s", sessionID)
|
||||
}
|
||||
}
|
||||
|
||||
// pickIMStoredAnswer returns the best available answer text from IM stream buffers.
|
||||
func pickIMStoredAnswer(candidates ...string) string {
|
||||
for _, s := range candidates {
|
||||
if strings.TrimSpace(s) != "" {
|
||||
return s
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// mergeIMAgentAnswerBuffers copies optimistic/live answers into persistence buffers
|
||||
// when EventAgentFinalAnswer did not populate answerBuilder (e.g. cancel before complete).
|
||||
func mergeIMAgentAnswerBuffers(answerBuilder, answerOuter, agentLiveAnswer *strings.Builder, completeFinal string) {
|
||||
if answerBuilder.Len() > 0 {
|
||||
return
|
||||
}
|
||||
switch {
|
||||
case agentLiveAnswer.Len() > 0:
|
||||
live := agentLiveAnswer.String()
|
||||
answerBuilder.WriteString(live)
|
||||
if answerOuter.Len() == 0 {
|
||||
answerOuter.WriteString(live)
|
||||
}
|
||||
case answerOuter.Len() > 0:
|
||||
answerBuilder.WriteString(answerOuter.String())
|
||||
case strings.TrimSpace(completeFinal) != "":
|
||||
answerBuilder.WriteString(completeFinal)
|
||||
answerOuter.WriteString(completeFinal)
|
||||
}
|
||||
}
|
||||
|
||||
// resolveIMConfig extracts IM tuning parameters from the application config,
|
||||
// falling back to built-in defaults for any zero/nil values.
|
||||
func resolveIMConfig(appCfg *config.Config) (workers, maxQueue, maxPerUser, globalMaxWorkers int, rlWindow time.Duration, rlMax int) {
|
||||
@@ -1847,7 +1894,8 @@ func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage,
|
||||
agentToolSteps []IMToolStep
|
||||
pipelineToolSteps []IMToolStep
|
||||
|
||||
streamedAny bool
|
||||
agentCompleteFinalAnswer string
|
||||
streamedAny bool
|
||||
)
|
||||
closeDone := func() { closeOnce.Do(func() { close(done) }) }
|
||||
closeComplete := func() { completeOnce.Do(func() { close(completeDone) }) }
|
||||
@@ -1957,13 +2005,9 @@ func (s *Service) handleMessageStream(ctx context.Context, msg *IncomingMessage,
|
||||
}
|
||||
bufMu.Lock()
|
||||
agentDone = true
|
||||
agentCompleteFinalAnswer = data.FinalAnswer
|
||||
applyIMCompleteDataToMessage(assistantMsg, data)
|
||||
if answerBuilder.Len() == 0 && agentLiveAnswer.Len() > 0 {
|
||||
answerBuilder.WriteString(agentLiveAnswer.String())
|
||||
answerOuter.WriteString(agentLiveAnswer.String())
|
||||
} else if answerBuilder.Len() == 0 && answerOuter.Len() > 0 {
|
||||
answerBuilder.WriteString(answerOuter.String())
|
||||
}
|
||||
mergeIMAgentAnswerBuffers(&answerBuilder, &answerOuter, &agentLiveAnswer, data.FinalAnswer)
|
||||
bufMu.Unlock()
|
||||
closeComplete()
|
||||
return nil
|
||||
@@ -2147,21 +2191,23 @@ loop:
|
||||
}
|
||||
|
||||
if useAgent {
|
||||
select {
|
||||
case <-completeDone:
|
||||
case <-qaCtx.Done():
|
||||
logger.Warnf(ctx, "[IM] QA context ended before agent complete event: session=%s", session.ID)
|
||||
}
|
||||
waitForIMAgentComplete(qaCtx, completeDone, session.ID)
|
||||
}
|
||||
|
||||
bufMu.Lock()
|
||||
parts := getStreamParts()
|
||||
resolvedAnswer := pickIMStoredAnswer(
|
||||
answerBuilder.String(),
|
||||
answerOuter.String(),
|
||||
agentLiveAnswer.String(),
|
||||
agentCompleteFinalAnswer,
|
||||
)
|
||||
if parts.Answer == "" {
|
||||
parts.Answer = answerBuilder.String()
|
||||
parts.Answer = resolvedAnswer
|
||||
}
|
||||
answer := answerBuilder.String()
|
||||
answer := resolvedAnswer
|
||||
finalErr := qaErr
|
||||
noVisibleContent := !streamedAny
|
||||
noVisibleContent := !streamedAny && strings.TrimSpace(resolvedAnswer) == ""
|
||||
bufMu.Unlock()
|
||||
|
||||
finalDisplay := cleanIMContent(ctx, FormatIMFinalFromParts(parts), tenant, s.defaultFileSvc)
|
||||
@@ -2296,6 +2342,9 @@ func (s *Service) runQA(ctx context.Context, session *types.Session, query strin
|
||||
}
|
||||
answerMu.Lock()
|
||||
applyIMCompleteDataToMessage(assistantMsg, data)
|
||||
if answerBuilder.Len() == 0 && strings.TrimSpace(data.FinalAnswer) != "" {
|
||||
answerBuilder.WriteString(data.FinalAnswer)
|
||||
}
|
||||
answerMu.Unlock()
|
||||
closeComplete()
|
||||
return nil
|
||||
@@ -2339,11 +2388,7 @@ func (s *Service) runQA(ctx context.Context, session *types.Session, query strin
|
||||
select {
|
||||
case <-done:
|
||||
if useAgent {
|
||||
select {
|
||||
case <-completeDone:
|
||||
case <-ctx.Done():
|
||||
logger.Warnf(ctx, "[IM] QA context ended before agent complete event: session=%s", session.ID)
|
||||
}
|
||||
waitForIMAgentComplete(ctx, completeDone, session.ID)
|
||||
}
|
||||
case <-ctx.Done():
|
||||
// Mark assistant message as completed to avoid dangling incomplete records
|
||||
|
||||
@@ -2,6 +2,7 @@ package im
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
@@ -258,3 +259,35 @@ func TestApplyIMCompleteDataToMessage(t *testing.T) {
|
||||
t.Fatalf("AgentSteps = %#v, want one thinking step", msg.AgentSteps)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPickIMStoredAnswerPrefersFirstNonEmpty(t *testing.T) {
|
||||
got := pickIMStoredAnswer("", "outer", "live", "complete")
|
||||
if got != "outer" {
|
||||
t.Fatalf("pickIMStoredAnswer = %q, want outer", got)
|
||||
}
|
||||
got = pickIMStoredAnswer("", "", "live", "complete")
|
||||
if got != "live" {
|
||||
t.Fatalf("pickIMStoredAnswer = %q, want live", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestMergeIMAgentAnswerBuffersUsesLiveThenComplete(t *testing.T) {
|
||||
var builder, outer, live strings.Builder
|
||||
live.WriteString("live answer")
|
||||
|
||||
mergeIMAgentAnswerBuffers(&builder, &outer, &live, "complete final")
|
||||
if builder.String() != "live answer" {
|
||||
t.Fatalf("builder = %q, want live answer", builder.String())
|
||||
}
|
||||
if outer.String() != "live answer" {
|
||||
t.Fatalf("outer = %q, want live answer", outer.String())
|
||||
}
|
||||
|
||||
builder.Reset()
|
||||
outer.Reset()
|
||||
live.Reset()
|
||||
mergeIMAgentAnswerBuffers(&builder, &outer, &live, "complete final")
|
||||
if builder.String() != "complete final" {
|
||||
t.Fatalf("builder = %q, want complete final", builder.String())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -56,7 +56,7 @@ func (m *recordingStreamSender) snapshot() (chunks []string, final string, ended
|
||||
func TestStreamDisplayPipeline_agentScenario_redGreen(t *testing.T) {
|
||||
// Simulates the agent IM stream lifecycle aligned with Web:
|
||||
// 1) intermediate updates show styled thinking + tools
|
||||
// 2) final replace shows collapsed think summary + answer
|
||||
// 2) final replace shows answer only (no collapsed think header)
|
||||
rawIntermediate := "<think>\n分析 Civilization VI 问题\n正在调用 搜索关键词...\n搜索关键词\n</think>\n\n"
|
||||
|
||||
rec := &recordingStreamSender{}
|
||||
@@ -99,7 +99,7 @@ func TestStreamDisplayPipeline_agentScenario_redGreen(t *testing.T) {
|
||||
if chunks[0] == final {
|
||||
t.Fatal("intermediate update should differ from final answer-only content")
|
||||
}
|
||||
if finalized != "《文明6》是回合制策略游戏。" && !strings.Contains(finalized, "思考过程") {
|
||||
if finalized != "《文明6》是回合制策略游戏。" {
|
||||
t.Fatalf("FinalizeStream content = %q", finalized)
|
||||
}
|
||||
if !strings.Contains(finalized, "《文明6》是回合制策略游戏。") {
|
||||
|
||||
@@ -438,8 +438,11 @@ func (a *Adapter) FinalizeStream(ctx context.Context, incoming *im.IncomingMessa
|
||||
msgID := state.msgID
|
||||
state.mu.Unlock()
|
||||
|
||||
if err := a.editMessage(ctx, chatID, msgID, finalContent, ""); err != nil {
|
||||
logger.Warnf(ctx, "[Telegram] Failed to finalize stream: %v", err)
|
||||
if err := a.editMessage(ctx, chatID, msgID, finalContent, "Markdown"); err != nil {
|
||||
logger.Warnf(ctx, "[Telegram] Markdown finalize failed, retrying plain: %v", err)
|
||||
if retryErr := a.editMessage(ctx, chatID, msgID, finalContent, ""); retryErr != nil {
|
||||
logger.Warnf(ctx, "[Telegram] Failed to finalize stream: %v", retryErr)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
+1
-38
@@ -1,7 +1,6 @@
|
||||
package im
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
)
|
||||
@@ -122,37 +121,6 @@ func formatIMAgentIntermediate(parts IMStreamParts) string {
|
||||
return strings.Join(sections, MarkdownThinkStyle.Separator)
|
||||
}
|
||||
|
||||
// buildIMAgentCollapsedSummary returns a one-line collapsed think header (Web: tree root).
|
||||
func buildIMAgentCollapsedSummary(parts IMStreamParts) string {
|
||||
think := strings.TrimSpace(agentThinkContent(parts))
|
||||
if think == "" {
|
||||
return ""
|
||||
}
|
||||
summary := formatIMAgentStepsSummary(parts)
|
||||
line := "> 💭 **思考过程**"
|
||||
if summary != "" {
|
||||
line += " · " + summary
|
||||
}
|
||||
return line
|
||||
}
|
||||
|
||||
func formatIMAgentStepsSummary(parts IMStreamParts) string {
|
||||
toolCount := len(parts.AgentToolSteps)
|
||||
hasNarrative := strings.TrimSpace(parts.AgentInner) != ""
|
||||
|
||||
var segments []string
|
||||
if hasNarrative {
|
||||
segments = append(segments, "思考 1 轮")
|
||||
}
|
||||
if toolCount > 0 {
|
||||
segments = append(segments, fmt.Sprintf("调用 %d 次工具", toolCount))
|
||||
}
|
||||
if len(segments) == 0 {
|
||||
return ""
|
||||
}
|
||||
return strings.Join(segments, " · ")
|
||||
}
|
||||
|
||||
// formatIMQuickQAIntermediate mirrors Web RagPipelineProgress:
|
||||
// pipeline steps as plain lines, model reasoning in a separate "思考" block,
|
||||
// and once answer starts streaming the progress collapses to answer-only preview.
|
||||
@@ -180,14 +148,9 @@ func FormatIMIntermediateFromParts(parts IMStreamParts, agentInProgress bool) st
|
||||
return formatIMAgentIntermediate(parts)
|
||||
}
|
||||
|
||||
// FormatIMFinalFromParts returns the final replace frame: collapsed think summary + answer for agent mode.
|
||||
// FormatIMFinalFromParts returns the final replace frame (answer-only for all modes).
|
||||
func FormatIMFinalFromParts(parts IMStreamParts) string {
|
||||
answer := strings.TrimSpace(parts.Answer)
|
||||
if parts.Mode == IMStreamModeAgent {
|
||||
if collapsed := buildIMAgentCollapsedSummary(parts); collapsed != "" && answer != "" {
|
||||
return collapsed + MarkdownThinkStyle.Separator + answer
|
||||
}
|
||||
}
|
||||
if answer != "" {
|
||||
return answer
|
||||
}
|
||||
|
||||
@@ -236,7 +236,7 @@ func TestFormatIMQuickQA_collapsesToAnswerWhenStreaming(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestFormatIMFinalFromParts_collapsesThinkForAgent(t *testing.T) {
|
||||
func TestFormatIMFinalFromParts_agentAnswerOnly(t *testing.T) {
|
||||
parts := IMStreamParts{
|
||||
Mode: IMStreamModeAgent,
|
||||
AgentInner: "好的,让我搜索\n",
|
||||
@@ -248,17 +248,11 @@ func TestFormatIMFinalFromParts_collapsesThinkForAgent(t *testing.T) {
|
||||
Answer: "文明6是一款策略游戏。",
|
||||
}
|
||||
got := FormatIMFinalFromParts(parts)
|
||||
if !strings.Contains(got, "思考过程") {
|
||||
t.Fatalf("final should keep collapsed think summary, got: %q", got)
|
||||
if got != "文明6是一款策略游戏。" {
|
||||
t.Fatalf("final should be answer-only, got: %q", got)
|
||||
}
|
||||
if !strings.Contains(got, "调用 2 次工具") {
|
||||
t.Fatalf("final summary should mention tool count, got: %q", got)
|
||||
}
|
||||
if !strings.Contains(got, "文明6是一款策略游戏。") {
|
||||
t.Fatalf("final must include answer, got: %q", got)
|
||||
}
|
||||
if strings.Contains(got, "检索知识库") {
|
||||
t.Fatalf("final must not expand full tool lines, got: %q", got)
|
||||
if strings.Contains(got, "思考过程") {
|
||||
t.Fatalf("final must not include collapsed think header, got: %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user