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

This commit is contained in:
ciel authored and GitHub committed 2026-11-06 15:14:36 +08:00
1 parent f41da2e813
commit 832db8a5de
3 files changed
+31

No files matched your search

+23
View File
@@ -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,
+1
View File
@@ -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
@@ -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" {