feat(session): persist scope metadata and aliases

This commit is contained in:
Hoshina
2026-04-01 16:25:05 +08:00
parent bb2167e3f3
commit 3957e2cc72
7 changed files with 585 additions and 104 deletions
+41
View File
@@ -74,6 +74,7 @@ type AgentLoop struct {
// processOptions configures how a message is processed
type processOptions struct {
SessionKey string // Session identifier for history/context
SessionAliases []string // Compatibility aliases for the session key
Channel string // Target channel for tool execution
ChatID string // Target chat ID for tool execution
MessageID string // Current inbound platform message ID
@@ -1475,6 +1476,7 @@ func (al *AgentLoop) processMessage(ctx context.Context, msg bus.InboundMessage)
opts := processOptions{
SessionKey: sessionKey,
SessionAliases: buildSessionAliases(sessionKey, allocation.SessionKey, msg.SessionKey),
Channel: msg.Channel,
ChatID: msg.ChatID,
MessageID: msg.MessageID,
@@ -1547,6 +1549,43 @@ func resolveScopeKey(routeSessionKey, msgSessionKey string) string {
return routeSessionKey
}
func buildSessionAliases(canonicalKey string, keys ...string) []string {
if len(keys) == 0 {
return nil
}
aliases := make([]string, 0, len(keys))
seen := make(map[string]struct{}, len(keys))
canonicalKey = strings.TrimSpace(canonicalKey)
for _, key := range keys {
key = strings.TrimSpace(key)
if key == "" || key == canonicalKey {
continue
}
if _, ok := seen[key]; ok {
continue
}
seen[key] = struct{}{}
aliases = append(aliases, key)
}
if len(aliases) == 0 {
return nil
}
return aliases
}
func ensureSessionMetadata(store session.SessionStore, key string, scope *session.SessionScope, aliases []string) {
if key == "" || scope == nil {
return
}
metaStore, ok := store.(interface {
EnsureSessionMetadata(sessionKey string, scope *session.SessionScope, aliases []string)
})
if !ok {
return
}
metaStore.EnsureSessionMetadata(key, scope, aliases)
}
func (al *AgentLoop) allocateRouteSession(route routing.ResolvedRoute, msg bus.InboundMessage) session.Allocation {
return session.AllocateRouteSession(session.AllocationInput{
AgentID: route.AgentID,
@@ -1668,6 +1707,8 @@ func (al *AgentLoop) runAgentLoop(
}
}
ensureSessionMetadata(agent.Sessions, opts.SessionKey, opts.SessionScope, opts.SessionAliases)
turnScope := al.newTurnEventScope(
agent.ID,
opts.SessionKey,