refactor(agent): rebuild multi-turn history from DB, drop llmcontext layer
The Agent ran a parallel context cache (Redis/in-memory) on top of the messages table to feed multi-turn history into the LLM. That dual-write caused subtle drift (e.g. compression diverging from DB, system-prompt swaps lost on restart) and required a separate ClearContext path on session/IM clear. Make the messages table the single source of truth: - Add service.LoadAgentHistory: rebuilds chat.Message history per turn from the persisted messages, expanding AgentSteps into proper OpenAI assistant_with_tool_calls + tool messages and replaying the canonical final answer (with <think> blocks stripped). final_answer tool calls are filtered to avoid duplicating the trailing answer. - Make AgentEngine stateless across turns: drop ContextManager / sessionID cache plumbing from the engine, agent service, and CreateAgentEngine signature. The engine only uses sessionID for logging/event emission. - Wire AgentQA to load history from DB on demand using HistoryTurns (default 5) when MultiTurnEnabled, otherwise run with empty history. - Delete the llmcontext package (ContextManager interface, Redis/memory storage, factory) and the SessionService.ClearContext API path; IM /clear and session message clear no longer need to invalidate cache. Behavior preserved: KnowledgeQA-mode replay is unchanged (turns with empty AgentSteps just produce the single canonical assistant message), and Agent-mode turns now consistently see prior tool calls and results.
This commit is contained in:
@@ -18,14 +18,19 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||||
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
|
"github.com/Tencent/WeKnora/internal/tracing/langfuse"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// langfuseQueryPreview caps the query length we ship as the agent.execute
|
// langfuseQueryPreview caps the query length we ship as the agent.execute
|
||||||
// span input — long quoted-context queries can be many KB of prose.
|
// span input — long quoted-context queries can be many KB of prose.
|
||||||
const langfuseQueryPreview = 2000
|
const langfuseQueryPreview = 2000
|
||||||
|
|
||||||
// AgentEngine is the core engine for running ReAct agents
|
// AgentEngine is the core engine for running ReAct agents.
|
||||||
|
//
|
||||||
|
// History persistence note: the engine is stateless across turns. Conversation
|
||||||
|
// history is rebuilt from the DB once per turn by the caller
|
||||||
|
// (see service.LoadAgentHistory) and passed into Execute as llmContext. The
|
||||||
|
// engine therefore does not maintain its own cache, system-prompt store, or
|
||||||
|
// cross-turn buffer.
|
||||||
type AgentEngine struct {
|
type AgentEngine struct {
|
||||||
config *types.AgentConfig
|
config *types.AgentConfig
|
||||||
toolRegistry *agenttools.ToolRegistry
|
toolRegistry *agenttools.ToolRegistry
|
||||||
@@ -33,8 +38,7 @@ type AgentEngine struct {
|
|||||||
eventBus *event.EventBus
|
eventBus *event.EventBus
|
||||||
knowledgeBasesInfo []*KnowledgeBaseInfo // Detailed knowledge base information for prompt
|
knowledgeBasesInfo []*KnowledgeBaseInfo // Detailed knowledge base information for prompt
|
||||||
selectedDocs []*SelectedDocumentInfo // User-selected documents (via @ mention)
|
selectedDocs []*SelectedDocumentInfo // User-selected documents (via @ mention)
|
||||||
contextManager interfaces.ContextManager // Context manager for writing agent conversation to LLM context
|
sessionID string // Session ID for logging and event emission
|
||||||
sessionID string // Session ID for context management
|
|
||||||
systemPromptTemplate string // System prompt template (optional, uses default if empty)
|
systemPromptTemplate string // System prompt template (optional, uses default if empty)
|
||||||
skillsManager *skills.Manager // Skills manager for Progressive Disclosure (optional)
|
skillsManager *skills.Manager // Skills manager for Progressive Disclosure (optional)
|
||||||
appConfig *appconfig.Config // Application config for prompt template resolution (optional)
|
appConfig *appconfig.Config // Application config for prompt template resolution (optional)
|
||||||
@@ -57,7 +61,6 @@ func NewAgentEngine(
|
|||||||
eventBus *event.EventBus,
|
eventBus *event.EventBus,
|
||||||
knowledgeBasesInfo []*KnowledgeBaseInfo,
|
knowledgeBasesInfo []*KnowledgeBaseInfo,
|
||||||
selectedDocs []*SelectedDocumentInfo,
|
selectedDocs []*SelectedDocumentInfo,
|
||||||
contextManager interfaces.ContextManager,
|
|
||||||
sessionID string,
|
sessionID string,
|
||||||
systemPromptTemplate string,
|
systemPromptTemplate string,
|
||||||
) *AgentEngine {
|
) *AgentEngine {
|
||||||
@@ -75,7 +78,6 @@ func NewAgentEngine(
|
|||||||
eventBus: eventBus,
|
eventBus: eventBus,
|
||||||
knowledgeBasesInfo: knowledgeBasesInfo,
|
knowledgeBasesInfo: knowledgeBasesInfo,
|
||||||
selectedDocs: selectedDocs,
|
selectedDocs: selectedDocs,
|
||||||
contextManager: contextManager,
|
|
||||||
sessionID: sessionID,
|
sessionID: sessionID,
|
||||||
systemPromptTemplate: systemPromptTemplate,
|
systemPromptTemplate: systemPromptTemplate,
|
||||||
tokenEstimator: tokenEst,
|
tokenEstimator: tokenEst,
|
||||||
@@ -99,7 +101,6 @@ func NewAgentEngineWithSkills(
|
|||||||
eventBus *event.EventBus,
|
eventBus *event.EventBus,
|
||||||
knowledgeBasesInfo []*KnowledgeBaseInfo,
|
knowledgeBasesInfo []*KnowledgeBaseInfo,
|
||||||
selectedDocs []*SelectedDocumentInfo,
|
selectedDocs []*SelectedDocumentInfo,
|
||||||
contextManager interfaces.ContextManager,
|
|
||||||
sessionID string,
|
sessionID string,
|
||||||
systemPromptTemplate string,
|
systemPromptTemplate string,
|
||||||
skillsManager *skills.Manager,
|
skillsManager *skills.Manager,
|
||||||
@@ -111,7 +112,6 @@ func NewAgentEngineWithSkills(
|
|||||||
eventBus,
|
eventBus,
|
||||||
knowledgeBasesInfo,
|
knowledgeBasesInfo,
|
||||||
selectedDocs,
|
selectedDocs,
|
||||||
contextManager,
|
|
||||||
sessionID,
|
sessionID,
|
||||||
systemPromptTemplate,
|
systemPromptTemplate,
|
||||||
)
|
)
|
||||||
@@ -630,7 +630,7 @@ func (e *AgentEngine) runReActIteration(
|
|||||||
|
|
||||||
// 4. Observe: Add tool results to messages and write to context
|
// 4. Observe: Add tool results to messages and write to context
|
||||||
state.RoundSteps = append(state.RoundSteps, step)
|
state.RoundSteps = append(state.RoundSteps, step)
|
||||||
*messagesPtr = e.appendToolResults(ctx, *messagesPtr, step)
|
*messagesPtr = e.appendToolResults(*messagesPtr, step)
|
||||||
common.PipelineInfo(ctx, "Agent", "round_end", map[string]interface{}{
|
common.PipelineInfo(ctx, "Agent", "round_end", map[string]interface{}{
|
||||||
"iteration": state.CurrentRound,
|
"iteration": state.CurrentRound,
|
||||||
"round": round,
|
"round": round,
|
||||||
|
|||||||
@@ -79,7 +79,6 @@ func newTestEngine(t *testing.T, chatModel chat.Chat, opts ...testEngineOption)
|
|||||||
event.NewEventBus(),
|
event.NewEventBus(),
|
||||||
nil,
|
nil,
|
||||||
nil,
|
nil,
|
||||||
nil,
|
|
||||||
"test-session",
|
"test-session",
|
||||||
"",
|
"",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -368,10 +368,11 @@ func (e *AgentEngine) buildToolsForLLM() []chat.Tool {
|
|||||||
return tools
|
return tools
|
||||||
}
|
}
|
||||||
|
|
||||||
// appendToolResults adds tool results to the message history following OpenAI's tool calling format
|
// appendToolResults adds tool results to the in-turn message history following
|
||||||
// Also writes these messages to the context manager for persistence
|
// OpenAI's tool-calling format. Cross-turn persistence is handled separately:
|
||||||
|
// the final AgentSteps are written to the assistant message by the SSE handler,
|
||||||
|
// and rebuilt from DB on the next turn by service.LoadAgentHistory.
|
||||||
func (e *AgentEngine) appendToolResults(
|
func (e *AgentEngine) appendToolResults(
|
||||||
ctx context.Context,
|
|
||||||
messages []chat.Message,
|
messages []chat.Message,
|
||||||
step types.AgentStep,
|
step types.AgentStep,
|
||||||
) []chat.Message {
|
) []chat.Message {
|
||||||
@@ -401,15 +402,6 @@ func (e *AgentEngine) appendToolResults(
|
|||||||
}
|
}
|
||||||
|
|
||||||
messages = append(messages, assistantMsg)
|
messages = append(messages, assistantMsg)
|
||||||
|
|
||||||
// Write assistant message to context
|
|
||||||
if e.contextManager != nil {
|
|
||||||
if err := e.contextManager.AddMessage(ctx, e.sessionID, assistantMsg); err != nil {
|
|
||||||
logger.Warnf(ctx, "[Agent] Failed to add assistant message to context: %v", err)
|
|
||||||
} else {
|
|
||||||
logger.Debugf(ctx, "[Agent] Added assistant message to context (session: %s)", e.sessionID)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add tool result messages (role: "tool", following OpenAI format)
|
// Add tool result messages (role: "tool", following OpenAI format)
|
||||||
@@ -427,15 +419,6 @@ func (e *AgentEngine) appendToolResults(
|
|||||||
}
|
}
|
||||||
|
|
||||||
messages = append(messages, toolMsg)
|
messages = append(messages, toolMsg)
|
||||||
|
|
||||||
// Write tool message to context
|
|
||||||
if e.contextManager != nil {
|
|
||||||
if err := e.contextManager.AddMessage(ctx, e.sessionID, toolMsg); err != nil {
|
|
||||||
logger.Warnf(ctx, "[Agent] Failed to add tool message to context: %v", err)
|
|
||||||
} else {
|
|
||||||
logger.Debugf(ctx, "[Agent] Added tool message to context (session: %s, tool: %s)", e.sessionID, toolCall.Name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return messages
|
return messages
|
||||||
|
|||||||
@@ -0,0 +1,229 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
|
"regexp"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
||||||
|
)
|
||||||
|
|
||||||
|
// agentHistoryFetchMultiplier controls how many raw DB messages to fetch
|
||||||
|
// when assembling history. Each turn contributes ~2 rows (user + assistant);
|
||||||
|
// we ask for a generous multiple so we never under-fetch when some pairs are
|
||||||
|
// incomplete (e.g. an in-flight turn).
|
||||||
|
const agentHistoryFetchMultiplier = 4
|
||||||
|
|
||||||
|
// agentHistoryFetchMin is the floor for the DB fetch limit, used when
|
||||||
|
// maxRounds is small or unset.
|
||||||
|
const agentHistoryFetchMin = 50
|
||||||
|
|
||||||
|
var agentHistoryThinkTagRegex = regexp.MustCompile(`(?s)<think>.*?</think>`)
|
||||||
|
|
||||||
|
// LoadAgentHistory rebuilds the multi-turn LLM context for an Agent-mode
|
||||||
|
// session directly from the persistent messages table. The result is a
|
||||||
|
// chronologically ordered list of chat.Message entries suitable for prepending
|
||||||
|
// to the current turn (without system prompt; the engine adds that itself).
|
||||||
|
//
|
||||||
|
// For each historical turn it emits:
|
||||||
|
// 1. A user message (RenderedContent if present, else Content, plus any
|
||||||
|
// image captions appended).
|
||||||
|
// 2. For each AgentStep with non-terminal tool calls (i.e. excluding
|
||||||
|
// final_answer), an assistant message carrying the step's thought and
|
||||||
|
// tool_calls, followed by one tool message per tool result.
|
||||||
|
// 3. A final assistant message with the canonical answer (msg.Content with
|
||||||
|
// <think> blocks stripped).
|
||||||
|
//
|
||||||
|
// Turns lacking either user or assistant content are skipped. The newest
|
||||||
|
// maxRounds turns are returned in chronological order.
|
||||||
|
//
|
||||||
|
// DB is treated as the single source of truth — there is no Redis/in-memory
|
||||||
|
// cache layer above this function. Callers are expected to invoke it once
|
||||||
|
// per turn before handing the messages to the agent engine.
|
||||||
|
func LoadAgentHistory(
|
||||||
|
ctx context.Context,
|
||||||
|
messageRepo interfaces.MessageRepository,
|
||||||
|
sessionID string,
|
||||||
|
maxRounds int,
|
||||||
|
) ([]chat.Message, error) {
|
||||||
|
if maxRounds <= 0 {
|
||||||
|
return []chat.Message{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
fetchLimit := maxRounds * agentHistoryFetchMultiplier
|
||||||
|
if fetchLimit < agentHistoryFetchMin {
|
||||||
|
fetchLimit = agentHistoryFetchMin
|
||||||
|
}
|
||||||
|
|
||||||
|
rows, err := messageRepo.GetRecentMessagesBySession(ctx, sessionID, fetchLimit)
|
||||||
|
if err != nil {
|
||||||
|
return nil, fmt.Errorf("load agent history: %w", err)
|
||||||
|
}
|
||||||
|
if len(rows) == 0 {
|
||||||
|
return []chat.Message{}, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type pair struct {
|
||||||
|
user *types.Message
|
||||||
|
assistant *types.Message
|
||||||
|
createdAt time.Time
|
||||||
|
}
|
||||||
|
pairs := make(map[string]*pair)
|
||||||
|
for _, msg := range rows {
|
||||||
|
p, ok := pairs[msg.RequestID]
|
||||||
|
if !ok {
|
||||||
|
p = &pair{}
|
||||||
|
pairs[msg.RequestID] = p
|
||||||
|
}
|
||||||
|
switch msg.Role {
|
||||||
|
case "user":
|
||||||
|
p.user = msg
|
||||||
|
if p.createdAt.IsZero() || msg.CreatedAt.Before(p.createdAt) {
|
||||||
|
p.createdAt = msg.CreatedAt
|
||||||
|
}
|
||||||
|
case "assistant":
|
||||||
|
p.assistant = msg
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
completePairs := make([]*pair, 0, len(pairs))
|
||||||
|
for _, p := range pairs {
|
||||||
|
if p.user != nil && p.assistant != nil && p.assistant.IsCompleted {
|
||||||
|
completePairs = append(completePairs, p)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(completePairs, func(i, j int) bool {
|
||||||
|
return completePairs[i].createdAt.Before(completePairs[j].createdAt)
|
||||||
|
})
|
||||||
|
|
||||||
|
if len(completePairs) > maxRounds {
|
||||||
|
completePairs = completePairs[len(completePairs)-maxRounds:]
|
||||||
|
}
|
||||||
|
|
||||||
|
out := make([]chat.Message, 0, len(completePairs)*4)
|
||||||
|
for _, p := range completePairs {
|
||||||
|
out = append(out, buildUserHistoryMessage(p.user))
|
||||||
|
out = append(out, buildAssistantHistoryMessages(p.assistant)...)
|
||||||
|
}
|
||||||
|
return out, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildUserHistoryMessage converts a stored user message into the chat.Message
|
||||||
|
// form that should appear in LLM history. It prefers RenderedContent (which
|
||||||
|
// already contains any retrieval-context augmentation from prior turns) and
|
||||||
|
// falls back to Content. When the user uploaded images, the captions extracted
|
||||||
|
// during ingestion are appended so subsequent turns retain a textual reference
|
||||||
|
// to those images even after the binary payload is gone.
|
||||||
|
func buildUserHistoryMessage(m *types.Message) chat.Message {
|
||||||
|
content := m.RenderedContent
|
||||||
|
if content == "" {
|
||||||
|
content = m.Content
|
||||||
|
}
|
||||||
|
if captions := extractImageCaptionsFromMessage(m.Images); captions != "" && m.RenderedContent == "" {
|
||||||
|
content += "\n\n[用户上传图片内容]\n" + captions
|
||||||
|
}
|
||||||
|
return chat.Message{Role: "user", Content: content}
|
||||||
|
}
|
||||||
|
|
||||||
|
// buildAssistantHistoryMessages reconstructs the assistant side of one
|
||||||
|
// historical turn. It walks AgentSteps to expand intermediate tool calls into
|
||||||
|
// proper OpenAI-shaped assistant + tool messages, then emits the canonical
|
||||||
|
// final answer as a trailing assistant message.
|
||||||
|
//
|
||||||
|
// AgentSteps from KnowledgeQA-mode turns are empty, in which case the result
|
||||||
|
// is just the single final-answer assistant message — exactly mirroring how
|
||||||
|
// the KnowledgeQA pipeline replays history today.
|
||||||
|
func buildAssistantHistoryMessages(m *types.Message) []chat.Message {
|
||||||
|
msgs := make([]chat.Message, 0, len(m.AgentSteps)*2+1)
|
||||||
|
for _, step := range m.AgentSteps {
|
||||||
|
nonTerminalCalls := filterNonTerminalToolCalls(step.ToolCalls)
|
||||||
|
if len(nonTerminalCalls) == 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
assistantMsg := chat.Message{
|
||||||
|
Role: "assistant",
|
||||||
|
Content: step.Thought,
|
||||||
|
ToolCalls: make([]chat.ToolCall, 0, len(nonTerminalCalls)),
|
||||||
|
}
|
||||||
|
for _, tc := range nonTerminalCalls {
|
||||||
|
argsJSON, _ := json.Marshal(tc.Args)
|
||||||
|
assistantMsg.ToolCalls = append(assistantMsg.ToolCalls, chat.ToolCall{
|
||||||
|
ID: tc.ID,
|
||||||
|
Type: "function",
|
||||||
|
Function: chat.FunctionCall{
|
||||||
|
Name: tc.Name,
|
||||||
|
Arguments: string(argsJSON),
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
msgs = append(msgs, assistantMsg)
|
||||||
|
for _, tc := range nonTerminalCalls {
|
||||||
|
msgs = append(msgs, chat.Message{
|
||||||
|
Role: "tool",
|
||||||
|
Content: toolCallOutput(tc),
|
||||||
|
ToolCallID: tc.ID,
|
||||||
|
Name: tc.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
finalContent := agentHistoryThinkTagRegex.ReplaceAllString(m.Content, "")
|
||||||
|
finalContent = strings.TrimSpace(finalContent)
|
||||||
|
if finalContent != "" {
|
||||||
|
msgs = append(msgs, chat.Message{Role: "assistant", Content: finalContent})
|
||||||
|
}
|
||||||
|
return msgs
|
||||||
|
}
|
||||||
|
|
||||||
|
// filterNonTerminalToolCalls drops final_answer entries since those are
|
||||||
|
// terminal signals — the canonical answer text is replayed via the trailing
|
||||||
|
// assistant message instead, so re-injecting final_answer here would either
|
||||||
|
// duplicate the answer or confuse the model into thinking the previous turn
|
||||||
|
// is still mid-flight.
|
||||||
|
func filterNonTerminalToolCalls(calls []types.ToolCall) []types.ToolCall {
|
||||||
|
out := make([]types.ToolCall, 0, len(calls))
|
||||||
|
for _, tc := range calls {
|
||||||
|
if tc.Name == agenttools.ToolFinalAnswer {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
out = append(out, tc)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
// toolCallOutput returns the textual content to use for a historical tool
|
||||||
|
// message: the recorded Output on success, or an "Error: …" line otherwise so
|
||||||
|
// the model can tell that an earlier tool call failed.
|
||||||
|
func toolCallOutput(tc types.ToolCall) string {
|
||||||
|
if tc.Result == nil {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
if !tc.Result.Success {
|
||||||
|
if tc.Result.Error != "" {
|
||||||
|
return "Error: " + tc.Result.Error
|
||||||
|
}
|
||||||
|
return "Error: tool call failed"
|
||||||
|
}
|
||||||
|
return tc.Result.Output
|
||||||
|
}
|
||||||
|
|
||||||
|
// extractImageCaptionsFromMessage concatenates non-empty Caption fields from
|
||||||
|
// stored message images. Mirrors the helper used in chat_pipeline so both
|
||||||
|
// modes surface previous-turn image descriptions identically.
|
||||||
|
func extractImageCaptionsFromMessage(images types.MessageImages) string {
|
||||||
|
var parts []string
|
||||||
|
for _, img := range images {
|
||||||
|
if img.Caption != "" {
|
||||||
|
parts = append(parts, img.Caption)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return strings.Join(parts, "\n")
|
||||||
|
}
|
||||||
@@ -0,0 +1,190 @@
|
|||||||
|
package service
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
agenttools "github.com/Tencent/WeKnora/internal/agent/tools"
|
||||||
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||||
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestBuildUserHistoryMessage_PrefersRenderedContent verifies the user side of
|
||||||
|
// history rebuild: RenderedContent (the RAG-augmented version) wins over the
|
||||||
|
// raw Content, and image captions are appended only when there's no rendered
|
||||||
|
// override (the rendered version already carries any retrieval context that
|
||||||
|
// would have included the caption upstream).
|
||||||
|
func TestBuildUserHistoryMessage_PrefersRenderedContent(t *testing.T) {
|
||||||
|
msg := &types.Message{
|
||||||
|
Role: "user",
|
||||||
|
Content: "what about the chart?",
|
||||||
|
RenderedContent: "what about the chart? [augmented]",
|
||||||
|
Images: types.MessageImages{
|
||||||
|
{Caption: "a bar chart"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got := buildUserHistoryMessage(msg)
|
||||||
|
assert.Equal(t, "user", got.Role)
|
||||||
|
assert.Equal(t, "what about the chart? [augmented]", got.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestBuildUserHistoryMessage_FallsBackToContentWithCaptions(t *testing.T) {
|
||||||
|
msg := &types.Message{
|
||||||
|
Role: "user",
|
||||||
|
Content: "look at this",
|
||||||
|
Images: types.MessageImages{
|
||||||
|
{Caption: "a bar chart"},
|
||||||
|
{Caption: "a pie chart"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got := buildUserHistoryMessage(msg)
|
||||||
|
assert.Equal(t, "user", got.Role)
|
||||||
|
assert.Equal(t, "look at this\n\n[用户上传图片内容]\na bar chart\na pie chart", got.Content)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBuildAssistantHistoryMessages_NaturalFinishEmitsSingleAnswer covers the
|
||||||
|
// most common path: a turn with no tool calls (model answered directly). The
|
||||||
|
// result must be a single assistant message holding the canonical answer —
|
||||||
|
// duplicates would inflate token usage every turn.
|
||||||
|
func TestBuildAssistantHistoryMessages_NaturalFinishEmitsSingleAnswer(t *testing.T) {
|
||||||
|
msg := &types.Message{
|
||||||
|
Role: "assistant",
|
||||||
|
Content: "Hello, nice to meet you!",
|
||||||
|
AgentSteps: types.AgentSteps{
|
||||||
|
{Iteration: 0, Thought: "Hello, nice to meet you!", ToolCalls: nil},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got := buildAssistantHistoryMessages(msg)
|
||||||
|
if assert.Len(t, got, 1) {
|
||||||
|
assert.Equal(t, "assistant", got[0].Role)
|
||||||
|
assert.Equal(t, "Hello, nice to meet you!", got[0].Content)
|
||||||
|
assert.Empty(t, got[0].ToolCalls)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBuildAssistantHistoryMessages_StripsThinkBlocks ensures the trailing
|
||||||
|
// final-answer assistant message has any <think>…</think> blocks stripped, so
|
||||||
|
// internal-reasoning text doesn't leak into the next turn's context.
|
||||||
|
func TestBuildAssistantHistoryMessages_StripsThinkBlocks(t *testing.T) {
|
||||||
|
msg := &types.Message{
|
||||||
|
Role: "assistant",
|
||||||
|
Content: "<think>plotting...</think>The answer is 42.",
|
||||||
|
}
|
||||||
|
got := buildAssistantHistoryMessages(msg)
|
||||||
|
if assert.Len(t, got, 1) {
|
||||||
|
assert.Equal(t, "The answer is 42.", got[0].Content)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBuildAssistantHistoryMessages_ToolCallsExpandIntoOpenAIShape covers the
|
||||||
|
// option-B replay: non-terminal tool calls from AgentSteps become proper
|
||||||
|
// assistant_with_tool_calls + tool messages, and the canonical final answer is
|
||||||
|
// appended last. final_answer entries are filtered because they're terminal
|
||||||
|
// signals — the trailing assistant message already carries the answer.
|
||||||
|
func TestBuildAssistantHistoryMessages_ToolCallsExpandIntoOpenAIShape(t *testing.T) {
|
||||||
|
msg := &types.Message{
|
||||||
|
Role: "assistant",
|
||||||
|
Content: "Found 3 matches in the docs.",
|
||||||
|
AgentSteps: types.AgentSteps{
|
||||||
|
{
|
||||||
|
Iteration: 0,
|
||||||
|
Thought: "Let me search.",
|
||||||
|
ToolCalls: []types.ToolCall{
|
||||||
|
{
|
||||||
|
ID: "call_1",
|
||||||
|
Name: agenttools.ToolKnowledgeSearch,
|
||||||
|
Args: map[string]interface{}{"query": "foo"},
|
||||||
|
Result: &types.ToolResult{
|
||||||
|
Success: true,
|
||||||
|
Output: "doc A, doc B, doc C",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Iteration: 1,
|
||||||
|
Thought: "",
|
||||||
|
ToolCalls: []types.ToolCall{
|
||||||
|
{
|
||||||
|
ID: "call_2",
|
||||||
|
Name: agenttools.ToolFinalAnswer,
|
||||||
|
Args: map[string]interface{}{"answer": "Found 3 matches in the docs."},
|
||||||
|
Result: &types.ToolResult{Success: true},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got := buildAssistantHistoryMessages(msg)
|
||||||
|
if !assert.Len(t, got, 3) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
// 1. assistant message announcing the tool call
|
||||||
|
assert.Equal(t, "assistant", got[0].Role)
|
||||||
|
assert.Equal(t, "Let me search.", got[0].Content)
|
||||||
|
if assert.Len(t, got[0].ToolCalls, 1) {
|
||||||
|
assert.Equal(t, "call_1", got[0].ToolCalls[0].ID)
|
||||||
|
assert.Equal(t, agenttools.ToolKnowledgeSearch, got[0].ToolCalls[0].Function.Name)
|
||||||
|
assert.Contains(t, got[0].ToolCalls[0].Function.Arguments, "foo")
|
||||||
|
}
|
||||||
|
// 2. tool result paired with the call ID
|
||||||
|
assert.Equal(t, "tool", got[1].Role)
|
||||||
|
assert.Equal(t, "call_1", got[1].ToolCallID)
|
||||||
|
assert.Equal(t, "doc A, doc B, doc C", got[1].Content)
|
||||||
|
// 3. canonical final answer (final_answer tool call itself was filtered)
|
||||||
|
assert.Equal(t, "assistant", got[2].Role)
|
||||||
|
assert.Equal(t, "Found 3 matches in the docs.", got[2].Content)
|
||||||
|
assert.Empty(t, got[2].ToolCalls)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestBuildAssistantHistoryMessages_ToolFailureSurfacesAsError ensures a
|
||||||
|
// historical failed tool call is replayed as an "Error: …" tool message so the
|
||||||
|
// model can see (and avoid retrying) the same failure path.
|
||||||
|
func TestBuildAssistantHistoryMessages_ToolFailureSurfacesAsError(t *testing.T) {
|
||||||
|
msg := &types.Message{
|
||||||
|
Role: "assistant",
|
||||||
|
Content: "Sorry, I could not complete the search.",
|
||||||
|
AgentSteps: types.AgentSteps{
|
||||||
|
{
|
||||||
|
Iteration: 0,
|
||||||
|
Thought: "Trying search.",
|
||||||
|
ToolCalls: []types.ToolCall{
|
||||||
|
{
|
||||||
|
ID: "call_err",
|
||||||
|
Name: agenttools.ToolKnowledgeSearch,
|
||||||
|
Args: map[string]interface{}{"query": "x"},
|
||||||
|
Result: &types.ToolResult{
|
||||||
|
Success: false,
|
||||||
|
Error: "kb unreachable",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got := buildAssistantHistoryMessages(msg)
|
||||||
|
if !assert.Len(t, got, 3) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
assert.Equal(t, chat.Message{
|
||||||
|
Role: "tool",
|
||||||
|
Content: "Error: kb unreachable",
|
||||||
|
ToolCallID: "call_err",
|
||||||
|
Name: agenttools.ToolKnowledgeSearch,
|
||||||
|
}, got[1])
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestFilterNonTerminalToolCalls confirms only final_answer is dropped — every
|
||||||
|
// other tool (KB search, web search, MCP tools…) must survive the filter.
|
||||||
|
func TestFilterNonTerminalToolCalls(t *testing.T) {
|
||||||
|
in := []types.ToolCall{
|
||||||
|
{Name: agenttools.ToolKnowledgeSearch},
|
||||||
|
{Name: agenttools.ToolFinalAnswer},
|
||||||
|
{Name: agenttools.ToolWebSearch},
|
||||||
|
}
|
||||||
|
out := filterNonTerminalToolCalls(in)
|
||||||
|
if assert.Len(t, out, 2) {
|
||||||
|
assert.Equal(t, agenttools.ToolKnowledgeSearch, out[0].Name)
|
||||||
|
assert.Equal(t, agenttools.ToolWebSearch, out[1].Name)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -102,14 +102,15 @@ func NewAgentService(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// CreateAgentEngineWithEventBus creates an agent engine with the given configuration and EventBus
|
// CreateAgentEngine creates an agent engine with the given configuration and EventBus.
|
||||||
|
// History is loaded once per turn by the caller (see service.LoadAgentHistory)
|
||||||
|
// and handed to AgentEngine.Execute as llmContext; the engine is stateless across turns.
|
||||||
func (s *agentService) CreateAgentEngine(
|
func (s *agentService) CreateAgentEngine(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
config *types.AgentConfig,
|
config *types.AgentConfig,
|
||||||
chatModel chat.Chat,
|
chatModel chat.Chat,
|
||||||
rerankModel rerank.Reranker,
|
rerankModel rerank.Reranker,
|
||||||
eventBus *event.EventBus,
|
eventBus *event.EventBus,
|
||||||
contextManager interfaces.ContextManager,
|
|
||||||
sessionID string,
|
sessionID string,
|
||||||
) (interfaces.AgentEngine, error) {
|
) (interfaces.AgentEngine, error) {
|
||||||
logger.Infof(ctx, "Creating agent engine with custom EventBus")
|
logger.Infof(ctx, "Creating agent engine with custom EventBus")
|
||||||
@@ -144,7 +145,7 @@ func (s *agentService) CreateAgentEngine(
|
|||||||
// 5. Create engine
|
// 5. Create engine
|
||||||
engine := agent.NewAgentEngine(
|
engine := agent.NewAgentEngine(
|
||||||
config, chatModel, toolRegistry, eventBus,
|
config, chatModel, toolRegistry, eventBus,
|
||||||
kbInfos, selectedDocs, contextManager, sessionID,
|
kbInfos, selectedDocs, sessionID,
|
||||||
systemPromptTemplate,
|
systemPromptTemplate,
|
||||||
)
|
)
|
||||||
engine.SetAppConfig(s.cfg)
|
engine.SetAppConfig(s.cfg)
|
||||||
|
|||||||
@@ -1,221 +0,0 @@
|
|||||||
package llmcontext
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"regexp"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
|
||||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
||||||
)
|
|
||||||
|
|
||||||
// dbFallbackFetchCount is the number of raw DB messages to fetch when
|
|
||||||
// rebuilding context from persistent storage. This should be generous
|
|
||||||
// because user+assistant messages are paired by RequestID and some
|
|
||||||
// incomplete pairs are discarded.
|
|
||||||
const dbFallbackFetchCount = 200
|
|
||||||
|
|
||||||
var regThinkTags = regexp.MustCompile(`(?s)<think>.*?</think>`)
|
|
||||||
|
|
||||||
// contextManager implements the ContextManager interface.
|
|
||||||
// It is a cache-backed storage layer: messages are persisted per session in
|
|
||||||
// a fast store (Redis / memory). When the cache is empty (e.g. TTL expired),
|
|
||||||
// it falls back to the persistent messages table via MessageService to
|
|
||||||
// rebuild context.
|
|
||||||
//
|
|
||||||
// All LLM-aware compression (summarisation, tool-boundary-aware truncation)
|
|
||||||
// is handled by the Agent Engine's Consolidator before messages are sent to
|
|
||||||
// the model.
|
|
||||||
type contextManager struct {
|
|
||||||
storage ContextStorage
|
|
||||||
messageRepo interfaces.MessageRepository // optional; enables DB fallback
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewContextManager creates a context manager.
|
|
||||||
// messageRepo is optional — when provided, GetContext will reconstruct
|
|
||||||
// history from the DB if the cache is empty.
|
|
||||||
func NewContextManager(storage ContextStorage, messageRepo interfaces.MessageRepository) interfaces.ContextManager {
|
|
||||||
return &contextManager{
|
|
||||||
storage: storage,
|
|
||||||
messageRepo: messageRepo,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// AddMessage appends a message to the session context and persists it.
|
|
||||||
func (cm *contextManager) AddMessage(ctx context.Context, sessionID string, message chat.Message) error {
|
|
||||||
messages, err := cm.storage.Load(ctx, sessionID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to load context: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
messages = append(messages, message)
|
|
||||||
|
|
||||||
if err := cm.storage.Save(ctx, sessionID, messages); err != nil {
|
|
||||||
return fmt.Errorf("failed to save context: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debugf(ctx, "[ContextManager][Session-%s] Message saved (total: %d)", sessionID, len(messages))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetContext retrieves the stored context for a session.
|
|
||||||
// If the cache is empty and a MessageService is available, it rebuilds
|
|
||||||
// the context from the persistent messages table and warms the cache.
|
|
||||||
func (cm *contextManager) GetContext(ctx context.Context, sessionID string) ([]chat.Message, error) {
|
|
||||||
messages, err := cm.storage.Load(ctx, sessionID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to load context: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(messages) > 0 {
|
|
||||||
logger.Debugf(ctx, "[ContextManager][Session-%s] Cache hit: %d messages", sessionID, len(messages))
|
|
||||||
return messages, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if cm.messageRepo == nil {
|
|
||||||
return messages, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cache miss — rebuild from DB
|
|
||||||
rebuilt, err := cm.rebuildFromDB(ctx, sessionID)
|
|
||||||
if err != nil {
|
|
||||||
logger.Warnf(ctx, "[ContextManager][Session-%s] Failed to rebuild context from DB: %v", sessionID, err)
|
|
||||||
return []chat.Message{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(rebuilt) > 0 {
|
|
||||||
if saveErr := cm.storage.Save(ctx, sessionID, rebuilt); saveErr != nil {
|
|
||||||
logger.Warnf(ctx, "[ContextManager][Session-%s] Failed to warm cache: %v", sessionID, saveErr)
|
|
||||||
}
|
|
||||||
logger.Infof(ctx, "[ContextManager][Session-%s] Rebuilt %d messages from DB", sessionID, len(rebuilt))
|
|
||||||
}
|
|
||||||
|
|
||||||
return rebuilt, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// rebuildFromDB loads recent messages from the persistent messages table
|
|
||||||
// and converts them into chat.Message pairs (user + assistant).
|
|
||||||
func (cm *contextManager) rebuildFromDB(ctx context.Context, sessionID string) ([]chat.Message, error) {
|
|
||||||
dbMessages, err := cm.messageRepo.GetRecentMessagesBySession(ctx, sessionID, dbFallbackFetchCount)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to load messages from DB: %w", err)
|
|
||||||
}
|
|
||||||
if len(dbMessages) == 0 {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Group by RequestID into Q&A pairs, same logic as chat_pipeline/common.go
|
|
||||||
type pair struct {
|
|
||||||
query string
|
|
||||||
answer string
|
|
||||||
createdAt time.Time
|
|
||||||
}
|
|
||||||
pairMap := make(map[string]*pair)
|
|
||||||
for _, msg := range dbMessages {
|
|
||||||
p, ok := pairMap[msg.RequestID]
|
|
||||||
if !ok {
|
|
||||||
p = &pair{}
|
|
||||||
pairMap[msg.RequestID] = p
|
|
||||||
}
|
|
||||||
switch msg.Role {
|
|
||||||
case "user":
|
|
||||||
if msg.RenderedContent != "" {
|
|
||||||
p.query = msg.RenderedContent
|
|
||||||
} else {
|
|
||||||
p.query = msg.Content
|
|
||||||
}
|
|
||||||
p.createdAt = msg.CreatedAt
|
|
||||||
if desc := extractImageCaptions(msg.Images); desc != "" && msg.RenderedContent == "" {
|
|
||||||
p.query += "\n\n[用户上传图片内容]\n" + desc
|
|
||||||
}
|
|
||||||
case "assistant":
|
|
||||||
p.answer = regThinkTags.ReplaceAllString(msg.Content, "")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pairs := make([]*pair, 0, len(pairMap))
|
|
||||||
for _, p := range pairMap {
|
|
||||||
if p.query != "" && p.answer != "" {
|
|
||||||
pairs = append(pairs, p)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(pairs, func(i, j int) bool {
|
|
||||||
return pairs[i].createdAt.Before(pairs[j].createdAt)
|
|
||||||
})
|
|
||||||
|
|
||||||
result := make([]chat.Message, 0, len(pairs)*2)
|
|
||||||
for _, p := range pairs {
|
|
||||||
result = append(result,
|
|
||||||
chat.Message{Role: "user", Content: p.query},
|
|
||||||
chat.Message{Role: "assistant", Content: p.answer},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// extractImageCaptions concatenates non-empty Caption fields from message
|
|
||||||
// images so that previous turns' image descriptions are included in context.
|
|
||||||
func extractImageCaptions(images types.MessageImages) string {
|
|
||||||
var parts []string
|
|
||||||
for _, img := range images {
|
|
||||||
if img.Caption != "" {
|
|
||||||
parts = append(parts, img.Caption)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return strings.Join(parts, "\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ClearContext removes all context for a session.
|
|
||||||
func (cm *contextManager) ClearContext(ctx context.Context, sessionID string) error {
|
|
||||||
if err := cm.storage.Delete(ctx, sessionID); err != nil {
|
|
||||||
return fmt.Errorf("failed to clear context: %w", err)
|
|
||||||
}
|
|
||||||
logger.Infof(ctx, "[ContextManager][Session-%s] Context cleared", sessionID)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetContextStats returns statistics about the stored context.
|
|
||||||
func (cm *contextManager) GetContextStats(ctx context.Context, sessionID string) (*interfaces.ContextStats, error) {
|
|
||||||
messages, err := cm.storage.Load(ctx, sessionID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to load context: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return &interfaces.ContextStats{
|
|
||||||
MessageCount: len(messages),
|
|
||||||
OriginalMessageCount: len(messages),
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetSystemPrompt sets or updates the system prompt for a session.
|
|
||||||
func (cm *contextManager) SetSystemPrompt(ctx context.Context, sessionID string, systemPrompt string) error {
|
|
||||||
messages, err := cm.storage.Load(ctx, sessionID)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to load context: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
systemMessage := chat.Message{
|
|
||||||
Role: "system",
|
|
||||||
Content: systemPrompt,
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(messages) > 0 && messages[0].Role == "system" {
|
|
||||||
messages[0] = systemMessage
|
|
||||||
} else {
|
|
||||||
messages = append([]chat.Message{systemMessage}, messages...)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := cm.storage.Save(ctx, sessionID, messages); err != nil {
|
|
||||||
return fmt.Errorf("failed to save context: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debugf(ctx, "[ContextManager][Session-%s] System prompt set (length=%d)", sessionID, len(systemPrompt))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,18 +0,0 @@
|
|||||||
package llmcontext
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
||||||
)
|
|
||||||
|
|
||||||
// NewContextManagerFromConfig creates a ContextManager.
|
|
||||||
// messageRepo is optional — when provided, context will be rebuilt from
|
|
||||||
// the persistent messages table if the cache (Redis/memory) is empty.
|
|
||||||
func NewContextManagerFromConfig(
|
|
||||||
storage ContextStorage,
|
|
||||||
messageRepo interfaces.MessageRepository,
|
|
||||||
) interfaces.ContextManager {
|
|
||||||
if storage == nil {
|
|
||||||
storage = NewMemoryStorage()
|
|
||||||
}
|
|
||||||
return NewContextManager(storage, messageRepo)
|
|
||||||
}
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
package llmcontext
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"sync"
|
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
|
||||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
||||||
)
|
|
||||||
|
|
||||||
// memoryStorage implements ContextStorage using in-memory storage
|
|
||||||
type memoryStorage struct {
|
|
||||||
sessions map[string][]chat.Message
|
|
||||||
mu sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewMemoryStorage creates a new memory-based storage
|
|
||||||
func NewMemoryStorage() ContextStorage {
|
|
||||||
return &memoryStorage{
|
|
||||||
sessions: make(map[string][]chat.Message),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save saves messages for a session to memory
|
|
||||||
func (ms *memoryStorage) Save(ctx context.Context, sessionID string, messages []chat.Message) error {
|
|
||||||
ms.mu.Lock()
|
|
||||||
defer ms.mu.Unlock()
|
|
||||||
|
|
||||||
// Make a copy to avoid external modifications
|
|
||||||
messageCopy := make([]chat.Message, len(messages))
|
|
||||||
copy(messageCopy, messages)
|
|
||||||
|
|
||||||
ms.sessions[sessionID] = messageCopy
|
|
||||||
logger.Debugf(ctx, "[MemoryStorage][Session-%s] Saved %d messages to memory", sessionID, len(messages))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load loads messages for a session from memory
|
|
||||||
func (ms *memoryStorage) Load(ctx context.Context, sessionID string) ([]chat.Message, error) {
|
|
||||||
ms.mu.RLock()
|
|
||||||
defer ms.mu.RUnlock()
|
|
||||||
|
|
||||||
messages, exists := ms.sessions[sessionID]
|
|
||||||
if !exists {
|
|
||||||
logger.Debugf(ctx, "[MemoryStorage][Session-%s] No context found in memory", sessionID)
|
|
||||||
return []chat.Message{}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Return a copy to avoid external modifications
|
|
||||||
messageCopy := make([]chat.Message, len(messages))
|
|
||||||
copy(messageCopy, messages)
|
|
||||||
|
|
||||||
logger.Debugf(ctx, "[MemoryStorage][Session-%s] Loaded %d messages from memory", sessionID, len(messages))
|
|
||||||
return messageCopy, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete deletes all messages for a session from memory
|
|
||||||
func (ms *memoryStorage) Delete(ctx context.Context, sessionID string) error {
|
|
||||||
ms.mu.Lock()
|
|
||||||
defer ms.mu.Unlock()
|
|
||||||
|
|
||||||
delete(ms.sessions, sessionID)
|
|
||||||
logger.Debugf(ctx, "[MemoryStorage][Session-%s] Deleted context from memory", sessionID)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,112 +0,0 @@
|
|||||||
package llmcontext
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
|
||||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
)
|
|
||||||
|
|
||||||
// redisStorage implements ContextStorage using Redis
|
|
||||||
type redisStorage struct {
|
|
||||||
client *redis.Client
|
|
||||||
ttl time.Duration
|
|
||||||
prefix string
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRedisStorage creates a new Redis-based storage
|
|
||||||
func NewRedisStorage(client *redis.Client, ttl time.Duration, prefix string) (ContextStorage, error) {
|
|
||||||
// Validate connection
|
|
||||||
_, err := client.Ping(context.Background()).Result()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to connect to Redis: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if ttl == 0 {
|
|
||||||
ttl = 24 * time.Hour // Default TTL 24 hours
|
|
||||||
}
|
|
||||||
|
|
||||||
if prefix == "" {
|
|
||||||
prefix = "context:" // Default prefix
|
|
||||||
}
|
|
||||||
|
|
||||||
return &redisStorage{
|
|
||||||
client: client,
|
|
||||||
ttl: ttl,
|
|
||||||
prefix: prefix,
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// buildKey builds the Redis key for a session
|
|
||||||
func (rs *redisStorage) buildKey(sessionID string) string {
|
|
||||||
return fmt.Sprintf("%s%s", rs.prefix, sessionID)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save saves messages for a session to Redis
|
|
||||||
func (rs *redisStorage) Save(ctx context.Context, sessionID string, messages []chat.Message) error {
|
|
||||||
key := rs.buildKey(sessionID)
|
|
||||||
|
|
||||||
// Marshal messages to JSON
|
|
||||||
data, err := json.Marshal(messages)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to marshal messages: %v", sessionID, err)
|
|
||||||
return fmt.Errorf("failed to marshal messages: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save to Redis with TTL
|
|
||||||
err = rs.client.Set(ctx, key, data, rs.ttl).Err()
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to save to Redis: %v", sessionID, err)
|
|
||||||
return fmt.Errorf("failed to save to Redis: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debugf(ctx, "[RedisStorage][Session-%s] Saved %d messages to Redis (TTL: %s)",
|
|
||||||
sessionID, len(messages), rs.ttl)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load loads messages for a session from Redis
|
|
||||||
func (rs *redisStorage) Load(ctx context.Context, sessionID string) ([]chat.Message, error) {
|
|
||||||
key := rs.buildKey(sessionID)
|
|
||||||
|
|
||||||
// Get from Redis
|
|
||||||
data, err := rs.client.Get(ctx, key).Bytes()
|
|
||||||
if err != nil {
|
|
||||||
if err == redis.Nil {
|
|
||||||
// No context exists yet, return empty slice
|
|
||||||
logger.Debugf(ctx, "[RedisStorage][Session-%s] No context found in Redis", sessionID)
|
|
||||||
return []chat.Message{}, nil
|
|
||||||
}
|
|
||||||
logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to get from Redis: %v", sessionID, err)
|
|
||||||
return nil, fmt.Errorf("failed to get from Redis: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Unmarshal messages
|
|
||||||
var messages []chat.Message
|
|
||||||
err = json.Unmarshal(data, &messages)
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to unmarshal messages: %v", sessionID, err)
|
|
||||||
return nil, fmt.Errorf("failed to unmarshal messages: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debugf(ctx, "[RedisStorage][Session-%s] Loaded %d messages from Redis", sessionID, len(messages))
|
|
||||||
return messages, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete deletes all messages for a session from Redis
|
|
||||||
func (rs *redisStorage) Delete(ctx context.Context, sessionID string) error {
|
|
||||||
key := rs.buildKey(sessionID)
|
|
||||||
|
|
||||||
err := rs.client.Del(ctx, key).Err()
|
|
||||||
if err != nil {
|
|
||||||
logger.Errorf(ctx, "[RedisStorage][Session-%s] Failed to delete from Redis: %v", sessionID, err)
|
|
||||||
return fmt.Errorf("failed to delete from Redis: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Debugf(ctx, "[RedisStorage][Session-%s] Deleted context from Redis", sessionID)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
package llmcontext
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ContextStorage defines the interface for storing and retrieving conversation context
|
|
||||||
// This separates storage implementation from business logic
|
|
||||||
type ContextStorage interface {
|
|
||||||
// Save saves messages for a session
|
|
||||||
Save(ctx context.Context, sessionID string, messages []chat.Message) error
|
|
||||||
|
|
||||||
// Load loads messages for a session
|
|
||||||
Load(ctx context.Context, sessionID string) ([]chat.Message, error)
|
|
||||||
|
|
||||||
// Delete deletes all messages for a session
|
|
||||||
Delete(ctx context.Context, sessionID string) error
|
|
||||||
}
|
|
||||||
@@ -15,7 +15,6 @@ import (
|
|||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
|
||||||
chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline"
|
chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline"
|
||||||
llmcontext "github.com/Tencent/WeKnora/internal/application/service/llmcontext"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// generateEventID generates a unique event ID with type suffix for better traceability
|
// generateEventID generates a unique event ID with type suffix for better traceability
|
||||||
@@ -23,7 +22,10 @@ func generateEventID(suffix string) string {
|
|||||||
return fmt.Sprintf("%s-%s", uuid.New().String()[:8], suffix)
|
return fmt.Sprintf("%s-%s", uuid.New().String()[:8], suffix)
|
||||||
}
|
}
|
||||||
|
|
||||||
// sessionService implements the SessionService interface for managing conversation sessions
|
// sessionService implements the SessionService interface for managing conversation sessions.
|
||||||
|
// History for multi-turn conversations is rebuilt from the messages table on demand
|
||||||
|
// (see service.LoadAgentHistory and chat_pipeline history loading) — there is no
|
||||||
|
// separate cross-turn cache layer.
|
||||||
type sessionService struct {
|
type sessionService struct {
|
||||||
cfg *config.Config // Application configuration
|
cfg *config.Config // Application configuration
|
||||||
sessionRepo interfaces.SessionRepository // Repository for session data
|
sessionRepo interfaces.SessionRepository // Repository for session data
|
||||||
@@ -33,7 +35,6 @@ type sessionService struct {
|
|||||||
tenantService interfaces.TenantService // Service for tenant operations
|
tenantService interfaces.TenantService // Service for tenant operations
|
||||||
eventManager *chatpipeline.EventManager // Event manager for chat pipeline
|
eventManager *chatpipeline.EventManager // Event manager for chat pipeline
|
||||||
agentService interfaces.AgentService // Service for agent operations
|
agentService interfaces.AgentService // Service for agent operations
|
||||||
sessionStorage llmcontext.ContextStorage // Session storage
|
|
||||||
knowledgeService interfaces.KnowledgeService // Service for knowledge operations
|
knowledgeService interfaces.KnowledgeService // Service for knowledge operations
|
||||||
chunkService interfaces.ChunkService // Service for chunk operations
|
chunkService interfaces.ChunkService // Service for chunk operations
|
||||||
webSearchStateRepo interfaces.WebSearchStateService // Service for web search state
|
webSearchStateRepo interfaces.WebSearchStateService // Service for web search state
|
||||||
@@ -53,7 +54,6 @@ func NewSessionService(cfg *config.Config,
|
|||||||
tenantService interfaces.TenantService,
|
tenantService interfaces.TenantService,
|
||||||
eventManager *chatpipeline.EventManager,
|
eventManager *chatpipeline.EventManager,
|
||||||
agentService interfaces.AgentService,
|
agentService interfaces.AgentService,
|
||||||
sessionStorage llmcontext.ContextStorage,
|
|
||||||
webSearchStateRepo interfaces.WebSearchStateService,
|
webSearchStateRepo interfaces.WebSearchStateService,
|
||||||
webSearchProviderRepo interfaces.WebSearchProviderRepository,
|
webSearchProviderRepo interfaces.WebSearchProviderRepository,
|
||||||
kbShareService interfaces.KBShareService,
|
kbShareService interfaces.KBShareService,
|
||||||
@@ -70,7 +70,6 @@ func NewSessionService(cfg *config.Config,
|
|||||||
tenantService: tenantService,
|
tenantService: tenantService,
|
||||||
eventManager: eventManager,
|
eventManager: eventManager,
|
||||||
agentService: agentService,
|
agentService: agentService,
|
||||||
sessionStorage: sessionStorage,
|
|
||||||
webSearchStateRepo: webSearchStateRepo,
|
webSearchStateRepo: webSearchStateRepo,
|
||||||
webSearchProviderRepo: webSearchProviderRepo,
|
webSearchProviderRepo: webSearchProviderRepo,
|
||||||
kbShareService: kbShareService,
|
kbShareService: kbShareService,
|
||||||
@@ -266,11 +265,6 @@ func (s *sessionService) DeleteSession(ctx context.Context, id string) error {
|
|||||||
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", id, err)
|
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", id, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cleanup conversation context stored in Redis for this session
|
|
||||||
if err := s.sessionStorage.Delete(ctx, id); err != nil {
|
|
||||||
logger.Warnf(ctx, "Failed to cleanup conversation context for session %s: %v", id, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete session from repository
|
// Delete session from repository
|
||||||
err := s.sessionRepo.Delete(ctx, tenantID, id)
|
err := s.sessionRepo.Delete(ctx, tenantID, id)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -314,9 +308,6 @@ func (s *sessionService) BatchDeleteSessions(ctx context.Context, ids []string)
|
|||||||
if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, id); err != nil {
|
if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, id); err != nil {
|
||||||
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", id, err)
|
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", id, err)
|
||||||
}
|
}
|
||||||
if err := s.sessionStorage.Delete(ctx, id); err != nil {
|
|
||||||
logger.Warnf(ctx, "Failed to cleanup conversation context for session %s: %v", id, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Batch delete sessions from repository
|
// Batch delete sessions from repository
|
||||||
@@ -359,9 +350,6 @@ func (s *sessionService) DeleteAllSessions(ctx context.Context) error {
|
|||||||
if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, session.ID); err != nil {
|
if err := s.webSearchStateRepo.DeleteWebSearchTempKBState(ctx, session.ID); err != nil {
|
||||||
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", session.ID, err)
|
logger.Warnf(ctx, "Failed to cleanup temporary KB for session %s: %v", session.ID, err)
|
||||||
}
|
}
|
||||||
if err := s.sessionStorage.Delete(ctx, session.ID); err != nil {
|
|
||||||
logger.Warnf(ctx, "Failed to cleanup conversation context for session %s: %v", session.ID, err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -560,10 +548,3 @@ func (s *sessionService) GenerateTitleAsync(
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ClearContext clears the LLM context for a session
|
|
||||||
// This is useful when switching knowledge bases or agent modes to prevent context contamination
|
|
||||||
func (s *sessionService) ClearContext(ctx context.Context, sessionID string) error {
|
|
||||||
logger.Infof(ctx, "Clearing context for session: %s", sessionID)
|
|
||||||
return s.sessionStorage.Delete(ctx, sessionID)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -8,13 +8,11 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/agent/tools"
|
"github.com/Tencent/WeKnora/internal/agent/tools"
|
||||||
llmcontext "github.com/Tencent/WeKnora/internal/application/service/llmcontext"
|
|
||||||
"github.com/Tencent/WeKnora/internal/event"
|
"github.com/Tencent/WeKnora/internal/event"
|
||||||
"github.com/Tencent/WeKnora/internal/logger"
|
"github.com/Tencent/WeKnora/internal/logger"
|
||||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
"github.com/Tencent/WeKnora/internal/models/chat"
|
||||||
"github.com/Tencent/WeKnora/internal/models/rerank"
|
"github.com/Tencent/WeKnora/internal/models/rerank"
|
||||||
"github.com/Tencent/WeKnora/internal/types"
|
"github.com/Tencent/WeKnora/internal/types"
|
||||||
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// AgentQA performs agent-based question answering with conversation history and streaming support
|
// AgentQA performs agent-based question answering with conversation history and streaming support
|
||||||
@@ -117,38 +115,29 @@ func (s *sessionService) AgentQA(
|
|||||||
logger.Infof(ctx, "knowledge_search tool not enabled, skipping rerank model initialization")
|
logger.Infof(ctx, "knowledge_search tool not enabled, skipping rerank model initialization")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get or create contextManager for this session
|
// Load multi-turn history directly from DB (the single source of truth).
|
||||||
contextManager := s.getContextManagerForSession()
|
// AgentSteps on each historical assistant message are expanded into proper
|
||||||
|
// assistant_with_tool_calls + tool messages so the model can see what was
|
||||||
// Set system prompt for the current agent in context manager
|
// tried last turn — except final_answer, which is replayed as the trailing
|
||||||
// This ensures the context uses the correct system prompt when switching agents
|
// canonical assistant message.
|
||||||
systemPrompt := agentConfig.ResolveSystemPrompt(agentConfig.WebSearchEnabled)
|
var llmContext []chat.Message
|
||||||
if systemPrompt != "" {
|
if agentConfig.MultiTurnEnabled {
|
||||||
if err := contextManager.SetSystemPrompt(ctx, sessionID, systemPrompt); err != nil {
|
historyTurns := agentConfig.HistoryTurns
|
||||||
logger.Warnf(ctx, "Failed to set system prompt in context manager: %v", err)
|
if historyTurns <= 0 {
|
||||||
} else {
|
historyTurns = 5
|
||||||
logger.Infof(ctx, "System prompt updated in context manager for agent")
|
|
||||||
}
|
}
|
||||||
}
|
llmContext, err = LoadAgentHistory(ctx, s.messageRepo, sessionID, historyTurns)
|
||||||
|
if err != nil {
|
||||||
// Get LLM context from context manager
|
logger.Warnf(ctx, "Failed to load agent history from DB: %v, continuing without history", err)
|
||||||
llmContext, err := s.getContextForSession(ctx, contextManager, sessionID)
|
llmContext = []chat.Message{}
|
||||||
if err != nil {
|
}
|
||||||
logger.Warnf(ctx, "Failed to get LLM context: %v, continuing without history", err)
|
logger.Infof(ctx, "Loaded %d history messages from DB (turns=%d)", len(llmContext), historyTurns)
|
||||||
llmContext = []chat.Message{}
|
} else {
|
||||||
}
|
logger.Infof(ctx, "Multi-turn disabled for this agent, running without history")
|
||||||
logger.Infof(ctx, "Loaded %d messages from LLM context manager", len(llmContext))
|
|
||||||
|
|
||||||
// Apply multi-turn configuration for Agent mode
|
|
||||||
// Note: In Agent mode, context is managed by contextManager with compression strategies,
|
|
||||||
// so we don't apply HistoryTurns limit here. HistoryTurns is used in normal (KnowledgeQA) mode.
|
|
||||||
if !agentConfig.MultiTurnEnabled {
|
|
||||||
// Multi-turn disabled, clear history
|
|
||||||
logger.Infof(ctx, "Multi-turn disabled for this agent, clearing history context")
|
|
||||||
llmContext = []chat.Message{}
|
llmContext = []chat.Message{}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create agent engine with EventBus and ContextManager
|
// Create agent engine with EventBus
|
||||||
logger.Info(ctx, "Creating agent engine")
|
logger.Info(ctx, "Creating agent engine")
|
||||||
engine, err := s.agentService.CreateAgentEngine(
|
engine, err := s.agentService.CreateAgentEngine(
|
||||||
ctx,
|
ctx,
|
||||||
@@ -156,7 +145,6 @@ func (s *sessionService) AgentQA(
|
|||||||
summaryModel,
|
summaryModel,
|
||||||
rerankModel,
|
rerankModel,
|
||||||
eventBus,
|
eventBus,
|
||||||
contextManager,
|
|
||||||
sessionID,
|
sessionID,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -357,29 +345,3 @@ func (s *sessionService) configureSkillsFromAgent(
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// getContextManagerForSession creates a context manager for the session.
|
|
||||||
func (s *sessionService) getContextManagerForSession() interfaces.ContextManager {
|
|
||||||
return llmcontext.NewContextManagerFromConfig(s.sessionStorage, s.messageRepo)
|
|
||||||
}
|
|
||||||
|
|
||||||
// getContextForSession retrieves LLM context for a session
|
|
||||||
func (s *sessionService) getContextForSession(
|
|
||||||
ctx context.Context,
|
|
||||||
contextManager interfaces.ContextManager,
|
|
||||||
sessionID string,
|
|
||||||
) ([]chat.Message, error) {
|
|
||||||
history, err := contextManager.GetContext(ctx, sessionID)
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get context: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Log context statistics
|
|
||||||
stats, _ := contextManager.GetContextStats(ctx, sessionID)
|
|
||||||
if stats != nil {
|
|
||||||
logger.Infof(ctx, "LLM context stats for session %s: messages=%d, tokens=~%d, compressed=%v",
|
|
||||||
sessionID, stats.MessageCount, stats.TokenCount, stats.IsCompressed)
|
|
||||||
}
|
|
||||||
|
|
||||||
return history, nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -48,7 +48,6 @@ import (
|
|||||||
"github.com/Tencent/WeKnora/internal/application/service"
|
"github.com/Tencent/WeKnora/internal/application/service"
|
||||||
chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline"
|
chatpipeline "github.com/Tencent/WeKnora/internal/application/service/chat_pipeline"
|
||||||
"github.com/Tencent/WeKnora/internal/application/service/file"
|
"github.com/Tencent/WeKnora/internal/application/service/file"
|
||||||
"github.com/Tencent/WeKnora/internal/application/service/llmcontext"
|
|
||||||
memoryService "github.com/Tencent/WeKnora/internal/application/service/memory"
|
memoryService "github.com/Tencent/WeKnora/internal/application/service/memory"
|
||||||
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
"github.com/Tencent/WeKnora/internal/application/service/retriever"
|
||||||
"github.com/Tencent/WeKnora/internal/config"
|
"github.com/Tencent/WeKnora/internal/config"
|
||||||
@@ -110,7 +109,6 @@ func BuildContainer(container *dig.Container) *dig.Container {
|
|||||||
must(container.Provide(initFileService))
|
must(container.Provide(initFileService))
|
||||||
must(container.Provide(initRedisClient))
|
must(container.Provide(initRedisClient))
|
||||||
must(container.Provide(initAntsPool))
|
must(container.Provide(initAntsPool))
|
||||||
must(container.Provide(initContextStorage))
|
|
||||||
|
|
||||||
// Register tracer cleanup handler (tracer needs to be available for cleanup registration)
|
// Register tracer cleanup handler (tracer needs to be available for cleanup registration)
|
||||||
must(container.Invoke(registerTracerCleanup))
|
must(container.Invoke(registerTracerCleanup))
|
||||||
@@ -382,18 +380,6 @@ func initRedisClient() (*redis.Client, error) {
|
|||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func initContextStorage(redisClient *redis.Client) (llmcontext.ContextStorage, error) {
|
|
||||||
if redisClient == nil {
|
|
||||||
logger.Infof(context.Background(), "[ContextStorage] Redis not available, using in-memory storage")
|
|
||||||
return llmcontext.NewMemoryStorage(), nil
|
|
||||||
}
|
|
||||||
storage, err := llmcontext.NewRedisStorage(redisClient, 24*time.Hour, "context:")
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return storage, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// initDatabase initializes database connection
|
// initDatabase initializes database connection
|
||||||
// Creates and configures database connection based on environment configuration
|
// Creates and configures database connection based on environment configuration
|
||||||
// Supports multiple database backends (PostgreSQL)
|
// Supports multiple database backends (PostgreSQL)
|
||||||
|
|||||||
@@ -374,10 +374,6 @@ func (h *Handler) ClearSessionMessages(c *gin.Context) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := h.sessionService.ClearContext(ctx, id); err != nil {
|
|
||||||
logger.Warnf(ctx, "Failed to clear LLM context for session %s: %v", id, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.Infof(ctx, "Session messages cleared successfully, ID: %s", id)
|
logger.Infof(ctx, "Session messages cleared successfully, ID: %s", id)
|
||||||
c.JSON(http.StatusOK, gin.H{
|
c.JSON(http.StatusOK, gin.H{
|
||||||
"success": true,
|
"success": true,
|
||||||
|
|||||||
@@ -1230,16 +1230,15 @@ func (s *Service) handleCommand(
|
|||||||
// Handle service-level side effects.
|
// Handle service-level side effects.
|
||||||
switch result.Action {
|
switch result.Action {
|
||||||
case ActionClear:
|
case ActionClear:
|
||||||
// Soft-delete the current ChannelSession and clear the LLM context
|
// Soft-delete the current ChannelSession so the next IM message
|
||||||
// so the next message creates a completely fresh conversation.
|
// starts a completely fresh WeKnora session. Conversation history
|
||||||
|
// is keyed by session ID and rebuilt from DB on demand, so no
|
||||||
|
// separate cache invalidation step is needed.
|
||||||
if err := s.db.Model(&ChannelSession{}).
|
if err := s.db.Model(&ChannelSession{}).
|
||||||
Where("id = ?", channelSession.ID).
|
Where("id = ?", channelSession.ID).
|
||||||
Update("deleted_at", time.Now()).Error; err != nil {
|
Update("deleted_at", time.Now()).Error; err != nil {
|
||||||
logger.Warnf(ctx, "[IM] Failed to soft-delete channel session: %v", err)
|
logger.Warnf(ctx, "[IM] Failed to soft-delete channel session: %v", err)
|
||||||
}
|
}
|
||||||
if err := s.sessionService.ClearContext(ctx, channelSession.SessionID); err != nil {
|
|
||||||
logger.Warnf(ctx, "[IM] Failed to clear session context: %v", err)
|
|
||||||
}
|
|
||||||
case ActionStop:
|
case ActionStop:
|
||||||
stopThreadID := ""
|
stopThreadID := ""
|
||||||
if channel.SessionMode == string(SessionModeThread) {
|
if channel.SessionMode == string(SessionModeThread) {
|
||||||
|
|||||||
@@ -32,14 +32,15 @@ type AgentEngine interface {
|
|||||||
|
|
||||||
// AgentService defines the interface for agent-related operations
|
// AgentService defines the interface for agent-related operations
|
||||||
type AgentService interface {
|
type AgentService interface {
|
||||||
// CreateAgentEngine creates an agent engine with the given configuration, EventBus, and ContextManager
|
// CreateAgentEngine creates an agent engine with the given configuration and EventBus.
|
||||||
|
// Conversation history is loaded by the caller (see service.LoadAgentHistory) and
|
||||||
|
// passed into AgentEngine.Execute; the engine itself is stateless across turns.
|
||||||
CreateAgentEngine(
|
CreateAgentEngine(
|
||||||
ctx context.Context,
|
ctx context.Context,
|
||||||
config *types.AgentConfig,
|
config *types.AgentConfig,
|
||||||
chatModel chat.Chat,
|
chatModel chat.Chat,
|
||||||
rerankModel rerank.Reranker,
|
rerankModel rerank.Reranker,
|
||||||
eventBus *event.EventBus,
|
eventBus *event.EventBus,
|
||||||
contextManager ContextManager,
|
|
||||||
sessionID string,
|
sessionID string,
|
||||||
) (AgentEngine, error)
|
) (AgentEngine, error)
|
||||||
|
|
||||||
|
|||||||
@@ -1,44 +0,0 @@
|
|||||||
package interfaces
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
|
|
||||||
"github.com/Tencent/WeKnora/internal/models/chat"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ContextManager manages LLM context for sessions
|
|
||||||
// It maintains conversation context separately from message storage
|
|
||||||
// and provides context compression when context window is exceeded
|
|
||||||
type ContextManager interface {
|
|
||||||
// AddMessage adds a message to the session context
|
|
||||||
// The message will be added to the context window for LLM
|
|
||||||
AddMessage(ctx context.Context, sessionID string, message chat.Message) error
|
|
||||||
|
|
||||||
// GetContext retrieves the current context for a session
|
|
||||||
// Returns messages that fit within the context window
|
|
||||||
// May apply compression if context is too large
|
|
||||||
GetContext(ctx context.Context, sessionID string) ([]chat.Message, error)
|
|
||||||
|
|
||||||
// ClearContext clears all context for a session
|
|
||||||
ClearContext(ctx context.Context, sessionID string) error
|
|
||||||
|
|
||||||
// GetContextStats returns statistics about the context
|
|
||||||
GetContextStats(ctx context.Context, sessionID string) (*ContextStats, error)
|
|
||||||
|
|
||||||
// SetSystemPrompt sets or updates the system prompt for a session
|
|
||||||
// If a system message exists, it will be replaced; otherwise, a new one will be added at the beginning
|
|
||||||
SetSystemPrompt(ctx context.Context, sessionID string, systemPrompt string) error
|
|
||||||
}
|
|
||||||
|
|
||||||
// ContextStats contains statistics about session context
|
|
||||||
type ContextStats struct {
|
|
||||||
// Total number of messages in context
|
|
||||||
MessageCount int `json:"message_count"`
|
|
||||||
// Estimated token count
|
|
||||||
TokenCount int `json:"token_count"`
|
|
||||||
// Whether context was compressed
|
|
||||||
IsCompressed bool `json:"is_compressed"`
|
|
||||||
// Number of original messages before compression
|
|
||||||
OriginalMessageCount int `json:"original_message_count"`
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -49,8 +49,6 @@ type SessionService interface {
|
|||||||
SearchKnowledge(ctx context.Context, knowledgeBaseIDs []string, knowledgeIDs []string, query string) ([]*types.SearchResult, error)
|
SearchKnowledge(ctx context.Context, knowledgeBaseIDs []string, knowledgeIDs []string, query string) ([]*types.SearchResult, error)
|
||||||
// AgentQA performs agent-based question answering with conversation history and streaming support.
|
// AgentQA performs agent-based question answering with conversation history and streaming support.
|
||||||
AgentQA(ctx context.Context, req *types.QARequest, eventBus *event.EventBus) error
|
AgentQA(ctx context.Context, req *types.QARequest, eventBus *event.EventBus) error
|
||||||
// ClearContext clears the LLM context for a session
|
|
||||||
ClearContext(ctx context.Context, sessionID string) error
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// SessionRepository defines the session repository interface
|
// SessionRepository defines the session repository interface
|
||||||
|
|||||||
Reference in New Issue
Block a user