feat: initialize external DobotAI agent gateway
This commit is contained in:
@@ -0,0 +1,61 @@
|
||||
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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package session
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"gitea.zheromiku.top/Weknora/dobotai-platform/internal/core"
|
||||
)
|
||||
|
||||
func TestBuildKeyPolicies(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
in Identity
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "direct user long lived",
|
||||
in: Identity{TenantKey: "tenant-a", Channel: "dingtalk", AgentID: "agent-1", ConversationType: core.ConversationDirect, ExternalUserID: "user-7"},
|
||||
want: "v1:tenant-a:dingtalk:agent-1:user:user-7",
|
||||
},
|
||||
{
|
||||
name: "group shared",
|
||||
in: Identity{TenantKey: "tenant-a", Channel: "dingtalk", AgentID: "agent-1", ConversationType: core.ConversationGroup, ExternalChatID: "group-9", ExternalUserID: "user-7", GroupPolicy: GroupShared},
|
||||
want: "v1:tenant-a:dingtalk:agent-1:group:group-9",
|
||||
},
|
||||
{
|
||||
name: "group per user",
|
||||
in: Identity{TenantKey: "tenant-a", Channel: "dingtalk", AgentID: "agent-1", ConversationType: core.ConversationGroup, ExternalChatID: "group-9", ExternalUserID: "user-7", GroupPolicy: GroupPerUser},
|
||||
want: "v1:tenant-a:dingtalk:agent-1:group-user:group-9:user-7",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got, _, err := BuildKey(tt.in)
|
||||
if err != nil {
|
||||
t.Fatalf("BuildKey: %v", err)
|
||||
}
|
||||
if got != tt.want {
|
||||
t.Fatalf("key=%q want=%q", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user