fix(chat): persist attachment content and fix fallback multi-turn context
Persist resolved temporary-attachment content onto the stored user message so later turns can replay it in history. Close the retrieval progress spinner on early pipeline exits, and reshape model-fallback messages with a proper system role plus trailing user question.
This commit is contained in:
@@ -65,6 +65,17 @@ func LastConsolidatedRetrievalStage(eventList []types.EventType, chatManage *typ
|
||||
return last
|
||||
}
|
||||
|
||||
// ShouldCloseRetrievalProgress reports whether the consolidated retrieval
|
||||
// progress window must be closed after a pipeline stage. It returns true when
|
||||
// either the planned last retrieval stage completed, or a retrieval stage
|
||||
// short-circuited the pipeline with an error — including ErrSearchNothing,
|
||||
// which routes into the fallback response. Closing on the error paths prevents
|
||||
// the frontend "knowledge_search" spinner from hanging forever when the
|
||||
// pipeline early-returns before reaching the last retrieval stage.
|
||||
func ShouldCloseRetrievalProgress(stage, lastRetrievalStage types.EventType, stageErr *PluginError) bool {
|
||||
return stage == lastRetrievalStage || stageErr != nil
|
||||
}
|
||||
|
||||
// BeginRetrievalProgress emits a single pending knowledge_search tool_call.
|
||||
func BeginRetrievalProgress(ctx context.Context, chatManage *types.ChatManage) *StageProgress {
|
||||
if chatManage == nil || chatManage.EventBus == nil {
|
||||
|
||||
@@ -44,6 +44,21 @@ func TestLastConsolidatedRetrievalStage(t *testing.T) {
|
||||
assert.Equal(t, types.FILTER_TOP_K, LastConsolidatedRetrievalStage(pipeline, cm))
|
||||
}
|
||||
|
||||
func TestShouldCloseRetrievalProgress(t *testing.T) {
|
||||
last := types.FILTER_TOP_K
|
||||
|
||||
// Normal completion: only the last retrieval stage closes the window.
|
||||
assert.True(t, ShouldCloseRetrievalProgress(types.FILTER_TOP_K, last, nil))
|
||||
assert.False(t, ShouldCloseRetrievalProgress(types.CHUNK_SEARCH_PARALLEL, last, nil))
|
||||
|
||||
// ErrSearchNothing at an earlier retrieval stage must still close the
|
||||
// window so the frontend stops spinning before the fallback answer streams.
|
||||
assert.True(t, ShouldCloseRetrievalProgress(types.CHUNK_SEARCH_PARALLEL, last, ErrSearchNothing))
|
||||
|
||||
// A hard error at any retrieval stage must also close the window.
|
||||
assert.True(t, ShouldCloseRetrievalProgress(types.CHUNK_RERANK, last, &PluginError{}))
|
||||
}
|
||||
|
||||
func TestShouldEmitQueryUnderstandProgress(t *testing.T) {
|
||||
cm := &types.ChatManage{PipelineRequest: types.PipelineRequest{EnableRewrite: true}}
|
||||
assert.True(t, ShouldEmitQueryUnderstandProgress(cm))
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestBuildFallbackMessages_PrependsSystemAndEndsWithUser guards the model
|
||||
// fallback path: the LLM input must start with a system message (the fallback
|
||||
// instruction) and end with the user's question, with history in between.
|
||||
// Previously the fallback dropped the system role entirely, producing a
|
||||
// [user, assistant, user] input with no system message.
|
||||
func TestBuildFallbackMessages_PrependsSystemAndEndsWithUser(t *testing.T) {
|
||||
cm := &types.ChatManage{}
|
||||
cm.Query = "这个文件内容"
|
||||
cm.History = []*types.History{
|
||||
{Query: "上一个问题", Answer: "上一个回答"},
|
||||
}
|
||||
|
||||
msgs := buildFallbackMessages(cm, "No content directly matched...\n\nUser question: 这个文件内容")
|
||||
|
||||
require.GreaterOrEqual(t, len(msgs), 2)
|
||||
assert.Equal(t, "system", msgs[0].Role, "fallback input must start with a system message")
|
||||
assert.Contains(t, msgs[0].Content, "No content directly matched")
|
||||
|
||||
// History is replayed between system and the trailing user turn.
|
||||
assert.Equal(t, "user", msgs[1].Role)
|
||||
assert.Equal(t, "上一个问题", msgs[1].Content)
|
||||
assert.Equal(t, "assistant", msgs[2].Role)
|
||||
assert.Equal(t, "上一个回答", msgs[2].Content)
|
||||
|
||||
last := msgs[len(msgs)-1]
|
||||
assert.Equal(t, "user", last.Role, "generation must be prompted by a trailing user turn")
|
||||
assert.Equal(t, "这个文件内容", last.Content)
|
||||
}
|
||||
|
||||
// TestBuildFallbackMessages_PrefersRewriteQuery verifies the trailing user turn
|
||||
// uses the rewritten query when available.
|
||||
func TestBuildFallbackMessages_PrefersRewriteQuery(t *testing.T) {
|
||||
cm := &types.ChatManage{}
|
||||
cm.Query = "它怎么样"
|
||||
cm.RewriteQuery = "混元大模型性能怎么样"
|
||||
|
||||
msgs := buildFallbackMessages(cm, "fallback instruction")
|
||||
|
||||
last := msgs[len(msgs)-1]
|
||||
assert.Equal(t, "user", last.Role)
|
||||
assert.Equal(t, "混元大模型性能怎么样", last.Content)
|
||||
}
|
||||
|
||||
// TestBuildFallbackMessages_EmptyPromptSkipsSystem ensures we don't inject an
|
||||
// empty system message when there is no fallback instruction to carry.
|
||||
func TestBuildFallbackMessages_EmptyPromptSkipsSystem(t *testing.T) {
|
||||
cm := &types.ChatManage{}
|
||||
cm.Query = "hello"
|
||||
|
||||
msgs := buildFallbackMessages(cm, " ")
|
||||
|
||||
require.Len(t, msgs, 1)
|
||||
assert.Equal(t, "user", msgs[0].Role)
|
||||
assert.Equal(t, "hello", msgs[0].Content)
|
||||
}
|
||||
|
||||
// TestBuildFallbackMessages_AttachesImagesToUserTurn confirms images ride on the
|
||||
// trailing user message only when the chat model supports vision.
|
||||
func TestBuildFallbackMessages_AttachesImagesToUserTurn(t *testing.T) {
|
||||
cm := &types.ChatManage{}
|
||||
cm.Query = "看图"
|
||||
cm.Images = []string{"https://example.com/a.png"}
|
||||
|
||||
cm.ChatModelSupportsVision = false
|
||||
noVision := buildFallbackMessages(cm, "fallback")
|
||||
assert.Empty(t, noVision[len(noVision)-1].Images)
|
||||
|
||||
cm.ChatModelSupportsVision = true
|
||||
withVision := buildFallbackMessages(cm, "fallback")
|
||||
assert.Equal(t, cm.Images, withVision[len(withVision)-1].Images)
|
||||
}
|
||||
@@ -718,7 +718,14 @@ func (s *sessionService) KnowledgeQAByEvent(ctx context.Context,
|
||||
chatpipeline.EndQueryUnderstandProgress(stageCtx, chatManage, understandProgress, understandStart, err)
|
||||
understandProgress = nil
|
||||
}
|
||||
if retrievalProgress != nil && eventType == lastRetrievalStage {
|
||||
// Close the consolidated retrieval progress window as soon as retrieval
|
||||
// is done: either the planned last retrieval stage completed, or a
|
||||
// retrieval stage short-circuited the pipeline (ErrSearchNothing or a
|
||||
// hard error). The early returns below (fallback / stage_failed) would
|
||||
// otherwise skip EndRetrievalProgress, leaving the "knowledge_search"
|
||||
// tool_call pending — so the frontend keeps spinning on "正在检索知识库"
|
||||
// forever even though the fallback answer has already streamed.
|
||||
if retrievalProgress != nil && chatpipeline.ShouldCloseRetrievalProgress(eventType, lastRetrievalStage, err) {
|
||||
chatpipeline.EndRetrievalProgress(stageCtx, chatManage, retrievalProgress, retrievalStart, err)
|
||||
retrievalProgress = nil
|
||||
}
|
||||
@@ -982,10 +989,29 @@ func (s *sessionService) handleModelFallback(ctx context.Context, chatManage *ty
|
||||
}
|
||||
|
||||
func buildFallbackMessages(chatManage *types.ChatManage, promptContent string) []chat.Message {
|
||||
messages := make([]chat.Message, 0, len(chatManage.History)*2+1)
|
||||
messages := make([]chat.Message, 0, len(chatManage.History)*2+2)
|
||||
|
||||
// The model-fallback prompt is a system-style instruction (KB document
|
||||
// listing + "use general knowledge when nothing matched" guidance). Carry
|
||||
// it in the system role so the LLM input keeps a proper system message
|
||||
// instead of starting with a bare user turn. We deliberately do NOT reuse
|
||||
// the RAG summary system prompt (SummaryConfig.Prompt) here: that template
|
||||
// forbids prior knowledge ("reply ONLY based on retrieved information"),
|
||||
// which directly contradicts the fallback's purpose.
|
||||
if strings.TrimSpace(promptContent) != "" {
|
||||
messages = append(messages, chat.Message{Role: "system", Content: promptContent})
|
||||
}
|
||||
|
||||
messages = chatpipeline.AppendHistoryMessages(messages, chatManage.History)
|
||||
|
||||
userMsg := chat.Message{Role: "user", Content: promptContent}
|
||||
// End on the user's actual question so generation is prompted by a user
|
||||
// turn (the query is also embedded in the system instruction above, but a
|
||||
// trailing user message keeps the chat shape valid for all providers).
|
||||
query := chatManage.Query
|
||||
if rq := strings.TrimSpace(chatManage.RewriteQuery); rq != "" {
|
||||
query = rq
|
||||
}
|
||||
userMsg := chat.Message{Role: "user", Content: query}
|
||||
if chatManage.ChatModelSupportsVision && len(chatManage.Images) > 0 {
|
||||
userMsg.Images = chatManage.Images
|
||||
}
|
||||
|
||||
@@ -139,11 +139,16 @@ func TestHandleModelFallback_IncludesHistoryMessages(t *testing.T) {
|
||||
|
||||
svc.handleModelFallback(context.Background(), cm)
|
||||
|
||||
require.Len(t, chatModel.lastMessages, 3)
|
||||
assert.Equal(t, "user", chatModel.lastMessages[0].Role)
|
||||
assert.Equal(t, "先介绍一下 WeKnora", chatModel.lastMessages[0].Content)
|
||||
assert.Equal(t, "assistant", chatModel.lastMessages[1].Role)
|
||||
assert.Equal(t, "WeKnora 是一个知识库问答系统。", chatModel.lastMessages[1].Content)
|
||||
assert.Equal(t, "user", chatModel.lastMessages[2].Role)
|
||||
assert.Contains(t, chatModel.lastMessages[2].Content, "现在还能继续讲吗?")
|
||||
// Corrected fallback shape: a system message carries the fallback
|
||||
// instruction, history is replayed in the middle, and the turn ends on the
|
||||
// user's question. Previously the system message was dropped entirely.
|
||||
require.Len(t, chatModel.lastMessages, 4)
|
||||
assert.Equal(t, "system", chatModel.lastMessages[0].Role)
|
||||
assert.Contains(t, chatModel.lastMessages[0].Content, "Answer the latest user question")
|
||||
assert.Equal(t, "user", chatModel.lastMessages[1].Role)
|
||||
assert.Equal(t, "先介绍一下 WeKnora", chatModel.lastMessages[1].Content)
|
||||
assert.Equal(t, "assistant", chatModel.lastMessages[2].Role)
|
||||
assert.Equal(t, "WeKnora 是一个知识库问答系统。", chatModel.lastMessages[2].Content)
|
||||
assert.Equal(t, "user", chatModel.lastMessages[3].Role)
|
||||
assert.Contains(t, chatModel.lastMessages[3].Content, "现在还能继续讲吗?")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user