Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix: prefix agent channel sessions with the chat channel name (#22090)

ciel832db8a5de77
diff --git a/internal/service/agent.go b/internal/service/agent.go
index 8bb091d673..82536be4c0 100644
--- a/internal/service/agent.go
+++ b/internal/service/agent.go
@@ -98,6 +98,8 @@ func (s *AgentService) RunAgentWithWebhook(
type agentSessionIDContextKey struct{}
+type agentChannelNameContextKey struct{}
+
type openAICompatMessagesContextKey struct{}
// WithAgentSessionID lets an HTTP boundary allocate the session identity while
@@ -115,6 +117,24 @@ func AgentSessionIDFromContext(ctx context.Context) string {
return sessionID
}
+// withAgentChannelName lets a chat-channel boundary record the channel name so
+// the session RunAgent creates carries it in its display name.
+func withAgentChannelName(ctx context.Context, channelName string) context.Context {
+ name := strings.TrimSpace(channelName)
+ if name == "" {
+ return ctx
+ }
+ return context.WithValue(ctx, agentChannelNameContextKey{}, name)
+}
+
+func agentChannelNameFromContext(ctx context.Context) string {
+ if ctx == nil {
+ return ""
+ }
+ name, _ := ctx.Value(agentChannelNameContextKey{}).(string)
+ return name
+}
+
// WithOpenAICompatMessages attaches the complete OpenAI messages list to an
// agent run without changing RunAgent's public argument list. The latest user
// message remains the run input; the service uses the earlier messages to seed
@@ -2644,6 +2664,9 @@ func (s *AgentService) createAgentRunSession(
}
source := "agent"
name := deriveAgentSessionName(userInput)
+ if channelName := agentChannelNameFromContext(ctx); channelName != "" {
+ name = channelSessionName(name, channelName)
+ }
session := &entity.API4Conversation{
ID: sessionID,
Name: &name,
diff --git a/internal/service/chat_channel_agent.go b/internal/service/chat_channel_agent.go
index 9ab333e911..8b4059b210 100644
--- a/internal/service/chat_channel_agent.go
+++ b/internal/service/chat_channel_agent.go
@@ -59,6 +59,7 @@ func (s *ChatChannelService) handleChannelAgentMessage(ctx context.Context, chan
// RunAgent creates and authorizes the persisted session; this boundary only
// supplies its stable identity for the first message and subsequent turns.
ctx = WithAgentSessionID(ctx, sessionID)
+ ctx = withAgentChannelName(ctx, channel.Name)
events, err := s.agentRunner.RunAgent(ctx, *channel.AgentID, sessionID, "", msg.Text, nil)
if err != nil {
return "", err
diff --git a/internal/service/chat_channel_agent_test.go b/internal/service/chat_channel_agent_test.go
index 1e33ca2316..055ddf1887 100644
--- a/internal/service/chat_channel_agent_test.go
+++ b/internal/service/chat_channel_agent_test.go
@@ -135,6 +135,13 @@ func TestChatChannelAgentRealEngineContinuationAndResume(t *testing.T) {
if err != nil || !strings.Contains(answer, "Your answer?") {
t.Fatalf("waiting: %q %v", answer, err)
}
+ var session entity.API4Conversation
+ if err := db.First(&session, "id = ?", channelSessionID("workflow", channel.ID, "external-one")).Error; err != nil {
+ t.Fatalf("load session: %v", err)
+ }
+ if session.Name == nil || *session.Name != "(channel: channel) start" {
+ t.Fatalf("session name = %v", session.Name)
+ }
msg.Text = "hello"
answer, err = svc.HandleIncomingMessage(t.Context(), msg)
if err != nil || answer != "got: hello" {