feat(backend): add RAG pipeline progress events
Emit query-understanding and knowledge-search progress over SSE, emit references before stream completion, and wire progress into session QA.
This commit is contained in:
@@ -0,0 +1,209 @@
|
||||
package chatpipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
const (
|
||||
retrievalProgressTool = "knowledge_search"
|
||||
queryUnderstandProgressTool = "query_understand"
|
||||
)
|
||||
|
||||
// StageProgress tracks an in-flight pipeline progress tool_call.
|
||||
type StageProgress struct {
|
||||
toolCallID string
|
||||
toolName string
|
||||
}
|
||||
|
||||
// ShouldEmitQueryUnderstandProgress reports whether the query-understand stage
|
||||
// will actually run (rewrite enabled or images attached).
|
||||
func ShouldEmitQueryUnderstandProgress(chatManage *types.ChatManage) bool {
|
||||
if chatManage == nil {
|
||||
return false
|
||||
}
|
||||
return chatManage.EnableRewrite || len(chatManage.Images) > 0
|
||||
}
|
||||
|
||||
// IsConsolidatedRetrievalStage reports whether a pipeline stage belongs to the
|
||||
// single user-visible "knowledge search" progress window (search → rerank → merge).
|
||||
func IsConsolidatedRetrievalStage(stage types.EventType, chatManage *types.ChatManage) bool {
|
||||
if chatManage == nil {
|
||||
return false
|
||||
}
|
||||
switch stage {
|
||||
case types.CHUNK_SEARCH_PARALLEL, types.CHUNK_RERANK, types.CHUNK_MERGE, types.FILTER_TOP_K:
|
||||
return chatManage.NeedsRetrieval()
|
||||
case types.WEB_FETCH:
|
||||
return chatManage.WebSearchEnabled
|
||||
case types.DATA_ANALYSIS:
|
||||
return chatManage.DataAnalysisEnabled && chatManage.NeedsRetrieval()
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// LastConsolidatedRetrievalStage returns the last retrieval-related stage in the
|
||||
// assembled pipeline, or empty when none apply.
|
||||
func LastConsolidatedRetrievalStage(eventList []types.EventType, chatManage *types.ChatManage) types.EventType {
|
||||
var last types.EventType
|
||||
for _, stage := range eventList {
|
||||
if IsConsolidatedRetrievalStage(stage, chatManage) {
|
||||
last = stage
|
||||
}
|
||||
}
|
||||
return last
|
||||
}
|
||||
|
||||
// BeginRetrievalProgress emits a single pending knowledge_search tool_call.
|
||||
func BeginRetrievalProgress(ctx context.Context, chatManage *types.ChatManage) *StageProgress {
|
||||
if chatManage == nil || chatManage.EventBus == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
toolCallID := uuid.New().String()
|
||||
args := map[string]any{}
|
||||
if chatManage.RewriteQuery != "" {
|
||||
args["query"] = chatManage.RewriteQuery
|
||||
} else if chatManage.Query != "" {
|
||||
args["query"] = chatManage.Query
|
||||
}
|
||||
|
||||
_ = chatManage.EventBus.Emit(ctx, types.Event{
|
||||
Type: types.EventType(event.EventAgentToolCall),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentToolCallData{
|
||||
ToolCallID: toolCallID,
|
||||
ToolName: retrievalProgressTool,
|
||||
Arguments: args,
|
||||
},
|
||||
})
|
||||
|
||||
return &StageProgress{toolCallID: toolCallID, toolName: retrievalProgressTool}
|
||||
}
|
||||
|
||||
// BeginQueryUnderstandProgress emits a pending query_understand tool_call.
|
||||
func BeginQueryUnderstandProgress(ctx context.Context, chatManage *types.ChatManage) *StageProgress {
|
||||
if chatManage == nil || chatManage.EventBus == nil || !ShouldEmitQueryUnderstandProgress(chatManage) {
|
||||
return nil
|
||||
}
|
||||
|
||||
toolCallID := uuid.New().String()
|
||||
args := map[string]any{}
|
||||
if chatManage.Query != "" {
|
||||
args["query"] = chatManage.Query
|
||||
}
|
||||
if len(chatManage.Images) > 0 {
|
||||
args["has_images"] = true
|
||||
}
|
||||
|
||||
_ = chatManage.EventBus.Emit(ctx, types.Event{
|
||||
Type: types.EventType(event.EventAgentToolCall),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentToolCallData{
|
||||
ToolCallID: toolCallID,
|
||||
ToolName: queryUnderstandProgressTool,
|
||||
Arguments: args,
|
||||
},
|
||||
})
|
||||
|
||||
return &StageProgress{toolCallID: toolCallID, toolName: queryUnderstandProgressTool}
|
||||
}
|
||||
|
||||
// EndQueryUnderstandProgress emits the matching tool_result for query understanding.
|
||||
func EndQueryUnderstandProgress(
|
||||
ctx context.Context,
|
||||
chatManage *types.ChatManage,
|
||||
progress *StageProgress,
|
||||
start time.Time,
|
||||
stageErr *PluginError,
|
||||
) {
|
||||
if progress == nil || chatManage == nil || chatManage.EventBus == nil {
|
||||
return
|
||||
}
|
||||
|
||||
success := stageErr == nil
|
||||
output := ""
|
||||
if success {
|
||||
output = "已完成问题理解"
|
||||
}
|
||||
|
||||
var errMsg string
|
||||
if !success && stageErr != nil && stageErr.Err != nil {
|
||||
errMsg = stageErr.Err.Error()
|
||||
}
|
||||
|
||||
_ = chatManage.EventBus.Emit(ctx, types.Event{
|
||||
Type: types.EventType(event.EventAgentToolResult),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentToolResultData{
|
||||
ToolCallID: progress.toolCallID,
|
||||
ToolName: queryUnderstandProgressTool,
|
||||
Output: output,
|
||||
Error: errMsg,
|
||||
Success: success,
|
||||
Duration: time.Since(start).Milliseconds(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// EndRetrievalProgress emits the matching tool_result for the consolidated retrieval window.
|
||||
func EndRetrievalProgress(
|
||||
ctx context.Context,
|
||||
chatManage *types.ChatManage,
|
||||
progress *StageProgress,
|
||||
start time.Time,
|
||||
stageErr *PluginError,
|
||||
) {
|
||||
if progress == nil || chatManage == nil || chatManage.EventBus == nil {
|
||||
return
|
||||
}
|
||||
|
||||
count := retrievalResultCount(chatManage)
|
||||
success := stageErr == nil || stageErr == ErrSearchNothing
|
||||
output := ""
|
||||
if success {
|
||||
if count == 0 {
|
||||
output = "未检索到相关内容"
|
||||
} else {
|
||||
output = fmt.Sprintf("检索到 %d 条相关内容", count)
|
||||
}
|
||||
}
|
||||
|
||||
var errMsg string
|
||||
if !success && stageErr != nil && stageErr.Err != nil {
|
||||
errMsg = stageErr.Err.Error()
|
||||
}
|
||||
|
||||
_ = chatManage.EventBus.Emit(ctx, types.Event{
|
||||
Type: types.EventType(event.EventAgentToolResult),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentToolResultData{
|
||||
ToolCallID: progress.toolCallID,
|
||||
ToolName: retrievalProgressTool,
|
||||
Output: output,
|
||||
Error: errMsg,
|
||||
Success: success,
|
||||
Duration: time.Since(start).Milliseconds(),
|
||||
Data: map[string]interface{}{
|
||||
"count": count,
|
||||
},
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
func retrievalResultCount(chatManage *types.ChatManage) int {
|
||||
switch {
|
||||
case len(chatManage.MergeResult) > 0:
|
||||
return len(chatManage.MergeResult)
|
||||
case len(chatManage.RerankResult) > 0:
|
||||
return len(chatManage.RerankResult)
|
||||
default:
|
||||
return len(chatManage.SearchResult)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package chatpipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type recordingEventBus struct {
|
||||
events []types.Event
|
||||
}
|
||||
|
||||
func (b *recordingEventBus) On(types.EventType, types.EventHandler) {}
|
||||
|
||||
func (b *recordingEventBus) Emit(_ context.Context, evt types.Event) error {
|
||||
b.events = append(b.events, evt)
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestIsConsolidatedRetrievalStage(t *testing.T) {
|
||||
cm := &types.ChatManage{}
|
||||
assert.True(t, IsConsolidatedRetrievalStage(types.CHUNK_SEARCH_PARALLEL, cm))
|
||||
assert.False(t, IsConsolidatedRetrievalStage(types.QUERY_UNDERSTAND, cm))
|
||||
assert.False(t, IsConsolidatedRetrievalStage(types.LOAD_HISTORY, cm))
|
||||
}
|
||||
|
||||
func TestLastConsolidatedRetrievalStage(t *testing.T) {
|
||||
cm := &types.ChatManage{}
|
||||
pipeline := []types.EventType{
|
||||
types.LOAD_HISTORY,
|
||||
types.QUERY_UNDERSTAND,
|
||||
types.CHUNK_SEARCH_PARALLEL,
|
||||
types.CHUNK_RERANK,
|
||||
types.CHUNK_MERGE,
|
||||
types.FILTER_TOP_K,
|
||||
types.INTO_CHAT_MESSAGE,
|
||||
types.CHAT_COMPLETION_STREAM,
|
||||
}
|
||||
assert.Equal(t, types.FILTER_TOP_K, LastConsolidatedRetrievalStage(pipeline, cm))
|
||||
}
|
||||
|
||||
func TestShouldEmitQueryUnderstandProgress(t *testing.T) {
|
||||
cm := &types.ChatManage{PipelineRequest: types.PipelineRequest{EnableRewrite: true}}
|
||||
assert.True(t, ShouldEmitQueryUnderstandProgress(cm))
|
||||
|
||||
cm.EnableRewrite = false
|
||||
assert.False(t, ShouldEmitQueryUnderstandProgress(cm))
|
||||
|
||||
cm.Images = []string{"data:image/png;base64,abc"}
|
||||
assert.True(t, ShouldEmitQueryUnderstandProgress(cm))
|
||||
}
|
||||
|
||||
func TestQueryUnderstandProgressEmitsToolCallAndResult(t *testing.T) {
|
||||
bus := &recordingEventBus{}
|
||||
cm := &types.ChatManage{
|
||||
PipelineRequest: types.PipelineRequest{SessionID: "sess-1", EnableRewrite: true},
|
||||
PipelineContext: types.PipelineContext{EventBus: bus},
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
progress := BeginQueryUnderstandProgress(context.Background(), cm)
|
||||
require.NotNil(t, progress)
|
||||
EndQueryUnderstandProgress(context.Background(), cm, progress, start, nil)
|
||||
|
||||
require.Len(t, bus.events, 2)
|
||||
callData, ok := bus.events[0].Data.(event.AgentToolCallData)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "query_understand", callData.ToolName)
|
||||
}
|
||||
|
||||
func TestRetrievalProgressEmitsSingleToolCallAndResult(t *testing.T) {
|
||||
bus := &recordingEventBus{}
|
||||
cm := &types.ChatManage{
|
||||
PipelineRequest: types.PipelineRequest{SessionID: "sess-1"},
|
||||
PipelineContext: types.PipelineContext{EventBus: bus},
|
||||
PipelineState: types.PipelineState{
|
||||
MergeResult: []*types.SearchResult{{ID: "r1"}, {ID: "r2"}, {ID: "r3"}},
|
||||
},
|
||||
}
|
||||
|
||||
start := time.Now()
|
||||
progress := BeginRetrievalProgress(context.Background(), cm)
|
||||
require.NotNil(t, progress)
|
||||
EndRetrievalProgress(context.Background(), cm, progress, start, nil)
|
||||
|
||||
require.Len(t, bus.events, 2)
|
||||
assert.Equal(t, types.EventType(event.EventAgentToolCall), bus.events[0].Type)
|
||||
assert.Equal(t, types.EventType(event.EventAgentToolResult), bus.events[1].Type)
|
||||
|
||||
callData, ok := bus.events[0].Data.(event.AgentToolCallData)
|
||||
require.True(t, ok)
|
||||
assert.Equal(t, "knowledge_search", callData.ToolName)
|
||||
|
||||
resultData, ok := bus.events[1].Data.(event.AgentToolResultData)
|
||||
require.True(t, ok)
|
||||
assert.True(t, resultData.Success)
|
||||
assert.Equal(t, 3, resultData.Data["count"])
|
||||
}
|
||||
@@ -6,14 +6,11 @@ import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/Tencent/WeKnora/internal/config"
|
||||
"github.com/Tencent/WeKnora/internal/event"
|
||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||
"github.com/Tencent/WeKnora/internal/types"
|
||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// PluginQueryUnderstand performs query rewriting and intent classification.
|
||||
@@ -115,23 +112,8 @@ func (p *PluginQueryUnderstand) OnEvent(ctx context.Context,
|
||||
maxTokens = 500
|
||||
}
|
||||
|
||||
// --- Emit progress event for image analysis ---
|
||||
var toolCallID string
|
||||
if useImages && chatManage.EventBus != nil {
|
||||
toolCallID = uuid.New().String()
|
||||
chatManage.EventBus.Emit(ctx, types.Event{
|
||||
Type: types.EventType(event.EventAgentToolCall),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentToolCallData{
|
||||
ToolCallID: toolCallID,
|
||||
ToolName: "image_analysis",
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// --- Call model ---
|
||||
thinking := false
|
||||
vlmStart := time.Now()
|
||||
response, err := rewriteModel.Chat(ctx, []chat.Message{
|
||||
{Role: "system", Content: systemContent},
|
||||
userMsg,
|
||||
@@ -141,19 +123,6 @@ func (p *PluginQueryUnderstand) OnEvent(ctx context.Context,
|
||||
Thinking: &thinking,
|
||||
})
|
||||
if err != nil {
|
||||
if toolCallID != "" && chatManage.EventBus != nil {
|
||||
chatManage.EventBus.Emit(ctx, types.Event{
|
||||
Type: types.EventType(event.EventAgentToolResult),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentToolResultData{
|
||||
ToolCallID: toolCallID,
|
||||
ToolName: "image_analysis",
|
||||
Output: "图片分析失败",
|
||||
Success: false,
|
||||
Duration: time.Since(vlmStart).Milliseconds(),
|
||||
},
|
||||
})
|
||||
}
|
||||
pipelineError(ctx, "QueryUnderstand", "model_call", map[string]interface{}{
|
||||
"session_id": chatManage.SessionID,
|
||||
"error": err.Error(),
|
||||
@@ -161,21 +130,6 @@ func (p *PluginQueryUnderstand) OnEvent(ctx context.Context,
|
||||
return next()
|
||||
}
|
||||
|
||||
// --- Emit completion event for image analysis ---
|
||||
if toolCallID != "" && chatManage.EventBus != nil {
|
||||
chatManage.EventBus.Emit(ctx, types.Event{
|
||||
Type: types.EventType(event.EventAgentToolResult),
|
||||
SessionID: chatManage.SessionID,
|
||||
Data: event.AgentToolResultData{
|
||||
ToolCallID: toolCallID,
|
||||
ToolName: "image_analysis",
|
||||
Output: "已分析图片内容",
|
||||
Success: true,
|
||||
Duration: time.Since(vlmStart).Milliseconds(),
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
// --- Parse structured output ---
|
||||
p.parseOutput(chatManage, response.Content)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user