package session import ( "errors" "fmt" "strings" "gitea.zheromiku.top/Weknora/dobotai-platform/internal/core" ) type GroupPolicy string const ( GroupShared GroupPolicy = "shared" GroupPerUser GroupPolicy = "per_user" ) type Identity struct { TenantKey string Channel string AgentID string ConversationType core.ConversationType ExternalChatID string ExternalUserID string GroupPolicy GroupPolicy } func BuildKey(identity Identity) (key string, subjectID string, err error) { parts := []string{identity.TenantKey, identity.Channel, identity.AgentID} for _, part := range parts { if strings.TrimSpace(part) == "" { return "", "", errors.New("tenant, channel and agent are required") } } switch identity.ConversationType { case core.ConversationDirect: subjectID = strings.TrimSpace(identity.ExternalUserID) if subjectID == "" { return "", "", errors.New("direct conversation requires external user id") } return fmt.Sprintf("v1:%s:%s:%s:user:%s", identity.TenantKey, identity.Channel, identity.AgentID, subjectID), subjectID, nil case core.ConversationGroup: chatID := strings.TrimSpace(identity.ExternalChatID) if chatID == "" { return "", "", errors.New("group conversation requires external chat id") } if identity.GroupPolicy == GroupPerUser { userID := strings.TrimSpace(identity.ExternalUserID) if userID == "" { return "", "", errors.New("per-user group policy requires external user id") } subjectID = chatID + ":" + userID return fmt.Sprintf("v1:%s:%s:%s:group-user:%s:%s", identity.TenantKey, identity.Channel, identity.AgentID, chatID, userID), subjectID, nil } subjectID = chatID return fmt.Sprintf("v1:%s:%s:%s:group:%s", identity.TenantKey, identity.Channel, identity.AgentID, chatID), subjectID, nil default: return "", "", fmt.Errorf("unsupported conversation type %q", identity.ConversationType) } }