refactor: 将客服后端重构为宿主可嵌入模块

- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。

- 移除模块内重复的组织、客户、工单、标签、技能、旧工作流、MCP 和迁移实现,将身份权限与业务主体交由宿主管理。

- 使用 libSQL 重构向量存储,并完善图片消息、访客身份、排队调度、企业微信和支持聊天页面。

- 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
This commit is contained in:
t
2026-08-28 22:23:13 +08:00
parent 6845c728f8
commit 18c9354095
377 changed files with 13199 additions and 22881 deletions
@@ -2,6 +2,7 @@ package services_test
import (
"strings"
"sync"
"testing"
"time"
@@ -181,6 +182,120 @@ func TestConversationAutoAssignManualDispatchFallsBackToTeamPool(t *testing.T) {
}
}
func TestConversationQueueOrdersByEffectivePriorityThenFIFO(t *testing.T) {
setupConversationHumanDispatchTestDB(t)
now := time.Now().Truncate(time.Second)
oldest := now.Add(-6 * time.Minute)
middle := now.Add(-2 * time.Minute)
newest := now.Add(-time.Minute)
queue := []models.Conversation{
{ID: 1, Status: enums.IMConversationStatusPending, Priority: 0, QueueEnteredAt: &middle},
{ID: 2, Status: enums.IMConversationStatusPending, Priority: 1, QueueEnteredAt: &newest},
{ID: 3, Status: enums.IMConversationStatusPending, Priority: 0, QueueEnteredAt: &oldest},
}
services.ConversationQueueService.Sort(queue, now)
if queue[0].ID != 3 || queue[1].ID != 2 || queue[2].ID != 1 {
t.Fatalf("unexpected queue order: %d, %d, %d", queue[0].ID, queue[1].ID, queue[2].ID)
}
if level := services.ConversationQueueService.EscalationLevel(&queue[0], now); level != 1 {
t.Fatalf("expected timeout escalation level 1, got %d", level)
}
}
func TestConversationQueueSnapshotSeparatesTeamPools(t *testing.T) {
db := setupConversationHumanDispatchTestDB(t)
now := time.Now().Truncate(time.Second)
firstEnteredAt := now.Add(-3 * time.Minute)
secondEnteredAt := now.Add(-2 * time.Minute)
otherPoolEnteredAt := now.Add(-10 * time.Minute)
first := models.Conversation{Status: enums.IMConversationStatusPending, CurrentTeamID: 1, QueueEnteredAt: &firstEnteredAt}
second := models.Conversation{Status: enums.IMConversationStatusPending, CurrentTeamID: 1, QueueEnteredAt: &secondEnteredAt}
otherPool := models.Conversation{Status: enums.IMConversationStatusPending, CurrentTeamID: 2, QueueEnteredAt: &otherPoolEnteredAt}
for _, item := range []*models.Conversation{&first, &second, &otherPool} {
if err := db.Create(item).Error; err != nil {
t.Fatalf("create queued conversation: %v", err)
}
}
firstSnapshot := services.ConversationQueueService.GetSnapshotAt(&first, now)
secondSnapshot := services.ConversationQueueService.GetSnapshotAt(&second, now)
otherSnapshot := services.ConversationQueueService.GetSnapshotAt(&otherPool, now)
if firstSnapshot.Position != 1 || firstSnapshot.WaitingCount != 2 {
t.Fatalf("unexpected first snapshot: %+v", firstSnapshot)
}
if secondSnapshot.Position != 2 || secondSnapshot.AheadCount != 1 {
t.Fatalf("unexpected second snapshot: %+v", secondSnapshot)
}
if otherSnapshot.Position != 1 || otherSnapshot.WaitingCount != 1 {
t.Fatalf("unexpected other-pool snapshot: %+v", otherSnapshot)
}
}
func TestConversationPureHumanGlobalQueueDispatchesFIFOAndHonorsCapacity(t *testing.T) {
db := setupConversationHumanDispatchTestDB(t)
createHumanDispatchTeam(t, db, 1, "售后支持组")
createHumanDispatchActiveSchedule(t, db, 1)
createHumanDispatchAgentProfile(t, db, 101, 1, enums.ServiceStatusIdle, 1, true, enums.StatusOk)
now := time.Now()
olderEnteredAt := now.Add(-2 * time.Minute)
newerEnteredAt := now.Add(-time.Minute)
older := createHumanDispatchConversation(t, db, 0, enums.IMConversationStatusPending)
newer := createHumanDispatchConversation(t, db, 0, enums.IMConversationStatusPending)
if err := db.Model(&models.Conversation{}).Where("id = ?", older.ID).Update("queue_entered_at", olderEnteredAt).Error; err != nil {
t.Fatalf("set older queue time: %v", err)
}
if err := db.Model(&models.Conversation{}).Where("id = ?", newer.ID).Update("queue_entered_at", newerEnteredAt).Error; err != nil {
t.Fatalf("set newer queue time: %v", err)
}
count, err := services.ConversationDispatchService.DispatchPendingConversations(10)
if err != nil {
t.Fatalf("DispatchPendingConversations() error = %v", err)
}
if count != 1 {
t.Fatalf("expected one dispatch at capacity, got %d", count)
}
olderCurrent := services.ConversationService.Get(older.ID)
newerCurrent := services.ConversationService.Get(newer.ID)
if olderCurrent.Status != enums.IMConversationStatusActive || olderCurrent.CurrentAssigneeID != 101 {
t.Fatalf("expected oldest conversation assigned first, got %+v", olderCurrent)
}
if newerCurrent.Status != enums.IMConversationStatusPending || newerCurrent.CurrentAssigneeID != 0 {
t.Fatalf("expected newer conversation to remain queued, got %+v", newerCurrent)
}
}
func TestConversationConcurrentAutoDispatchCreatesSingleAssignment(t *testing.T) {
db := setupConversationHumanDispatchTestDB(t)
createHumanDispatchTeam(t, db, 1, "售后支持组")
createHumanDispatchActiveSchedule(t, db, 1)
createHumanDispatchAgentProfile(t, db, 101, 1, enums.ServiceStatusIdle, 3, true, enums.StatusOk)
conversation := createHumanDispatchConversation(t, db, 0, enums.IMConversationStatusPending)
now := time.Now()
if err := db.Model(&models.Conversation{}).Where("id = ?", conversation.ID).Update("queue_entered_at", now).Error; err != nil {
t.Fatalf("set queue time: %v", err)
}
var waitGroup sync.WaitGroup
for range 8 {
waitGroup.Add(1)
go func() {
defer waitGroup.Done()
_, _ = services.ConversationDispatchService.DispatchConversation(conversation.ID)
}()
}
waitGroup.Wait()
var assignmentCount int64
if err := db.Model(&models.ConversationAssignment{}).Where("conversation_id = ?", conversation.ID).Count(&assignmentCount).Error; err != nil {
t.Fatalf("count assignments: %v", err)
}
if assignmentCount != 1 {
t.Fatalf("expected exactly one assignment, got %d", assignmentCount)
}
}
func setupConversationHumanDispatchTestDB(t *testing.T) *gorm.DB {
t.Helper()
dbName := strings.NewReplacer("/", "_", " ", "_").Replace(t.Name())
@@ -200,8 +315,6 @@ func setupConversationHumanDispatchTestDB(t *testing.T) *gorm.DB {
}
})
if err := db.AutoMigrate(
&models.Customer{},
&models.CustomerIdentity{},
&models.AIAgent{},
&models.AgentTeam{},
&models.AgentTeamSchedule{},