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:
wizardchen
2026-07-16 16:38:22 +08:00
committed by lyingbug
parent 92ceb3bd7d
commit 9ae17be43c
7 changed files with 308 additions and 10 deletions
@@ -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))