Files
t 18c9354095 refactor: 将客服后端重构为宿主可嵌入模块
- 注入数据库、运行时配置、统一响应、文件存储和平台 AI 能力,补充业务读写工具与客户快捷操作契约。

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

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

- 统一 HTTP、DTO 与 WebSocket 的 snake_case 协议,补齐模块初始化、业务动作和公共载荷等回归测试。
2026-08-28 22:23:13 +08:00

658 lines
54 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package models
import (
"code.tczkiot.com/wlw/ai-agent/internal/pkg/enums"
"net/http"
"time"
)
// Models 注册所有需要迁移和代码生成的模型。
var Models = []any{
&Asset{},
&Conversation{},
&ConversationParticipant{},
&ConversationReadState{},
&Message{},
&WxWorkKFSyncState{},
&WxWorkKFConversation{},
&WxWorkKFMessageRef{},
&ChannelMessageOutbox{},
&ConversationAssignment{},
&QuickReply{},
&ConversationEventLog{},
&Notification{},
&AIAgent{},
&Channel{},
&AgentProfile{},
&AgentTeam{},
&AgentTeamSchedule{},
&AIConfig{},
&KnowledgeBase{},
&KnowledgeDirectory{},
&KnowledgeDocument{},
&KnowledgeFAQ{},
&KnowledgeChunk{},
&KnowledgeRetrieveLog{},
&KnowledgeRetrieveHit{},
&KnowledgeFeedback{},
&AgentRevision{},
&AgentRun{},
&AgentStep{},
&AgentToolCall{},
&AgentToolInvocation{},
&AgentRunQualityFeedback{},
&ConversationInterrupt{},
}
// AgentToolInvocation persists the idempotency boundary for a business tool.
// It is intentionally independent of AgentRun audit rows so a retry after a
// process interruption cannot repeat an external write.
type AgentToolInvocation struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;index;uniqueIndex:uk_agent_tool_invocation"`
AIAgentID int64 `gorm:"type:bigint;not null;default:0;index"`
ToolCode string `gorm:"type:varchar(128);not null;default:'';uniqueIndex:uk_agent_tool_invocation"`
IdempotencyKey string `gorm:"type:varchar(160);not null;default:'';uniqueIndex:uk_agent_tool_invocation"`
Status string `gorm:"type:varchar(20);not null;default:'running';index"`
ResultData string `gorm:"type:text"`
ErrorMessage string `gorm:"type:text"`
AuditFields
}
// Notification 站内通知。
type Notification struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
RecipientUserID int64 `gorm:"type:bigint;not null;default:0;index"`
Title string `gorm:"type:varchar(255);not null;default:''"`
Content string `gorm:"type:text"`
NotificationType string `gorm:"type:varchar(50);not null;default:'';index"`
BizType string `gorm:"type:varchar(50);not null;default:'';index"`
BizID int64 `gorm:"type:bigint;not null;default:0;index"`
ActionURL string `gorm:"type:varchar(255);not null;default:''"`
ReadAt *time.Time `gorm:"index"`
Status enums.Status `gorm:"type:int;not null;default:0;index"`
CreatedAt time.Time `gorm:"not null;index"`
}
// AuditFields 定义涉及用户操作数据的统一审计字段。
// 该结构记录数据创建与更新的时间、操作者ID和操作者名称。
type AuditFields struct {
CreatedAt time.Time `gorm:"not null;index"` // CreatedAt 记录数据创建时间。
CreateUserID int64 `gorm:"type:bigint;not null;default:0;index"` // CreateUserID 记录创建人用户ID;系统任务写0。
CreateUserName string `gorm:"type:varchar(100);not null;default:''"` // CreateUserName 记录创建人名称;系统任务写system。
UpdatedAt time.Time `gorm:"not null;index"` // UpdatedAt 记录数据最近更新时间。
UpdateUserID int64 `gorm:"type:bigint;not null;default:0;index"` // UpdateUserID 记录最后更新人用户ID;系统任务写0。
UpdateUserName string `gorm:"type:varchar(100);not null;default:''"` // UpdateUserName 记录最后更新人名称;系统任务写system。
}
// Asset 存储的文件资源,如上传的附件等。
type Asset struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;default:0;index"`
AssetID string `gorm:"type:varchar(64);not null;uniqueIndex"`
Provider enums.AssetProvider `gorm:"type:varchar(50);not null;default:'';index"`
StorageKey string `gorm:"type:varchar(255);not null;default:'';uniqueIndex:uk_storage_key"`
Filename string `gorm:"type:varchar(255);not null;default:''"`
FileSize int64 `gorm:"type:bigint;not null;default:0"`
MimeType string `gorm:"type:varchar(100);not null;default:''"`
Status enums.AssetStatus `gorm:"type:int;not null;default:1;index"`
AuditFields
}
// Conversation 客服会话。
type Conversation struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为会话主键。
AIAgentID int64 `gorm:"type:bigint;not null;default:0;index"` // AIAgentID 为当前会话绑定的 AI Agent ID。
ChannelID int64 `gorm:"type:bigint;not null;default:0;index"` // ChannelID 为该会话来源接入渠道ID。
CustomerType string `gorm:"type:varchar(30);not null;default:'';index"` // CustomerType 为 be-system 用户类型;匿名渠道使用来源类型。
CustomerID int64 `gorm:"type:bigint;not null;default:0;index"` // CustomerID 为 be-system 用户 ID;匿名访客为 0。
CustomerExternalID string `gorm:"type:varchar(128);not null;default:'';index"` // CustomerExternalID 为宿主用户或匿名渠道的稳定外部标识。
CustomerName string `gorm:"type:varchar(100);not null;default:'';index"` // CustomerName 为显示名称快照,用于历史会话展示和搜索。
Status enums.IMConversationStatus `gorm:"type:int;not null;default:1;index"` // Status 为会话状态,如待接入、处理中、已关闭。
ServiceMode enums.IMConversationServiceMode `gorm:"type:int;not null;default:3;index"` // ServiceMode 为服务模式,如仅AI、仅人工、AI优先人工接管。
Priority int `gorm:"type:int;not null;default:0;index"` // Priority 为会话优先级。
CurrentAssigneeID int64 `gorm:"type:bigint;not null;default:0;index"` // CurrentAssigneeID 为当前接待客服ID。
CurrentTeamID int64 `gorm:"type:bigint;not null;default:0;index"` // CurrentTeamID 为当前处理客服组ID。
LastMessageID int64 `gorm:"type:bigint;not null;default:0;index"` // LastMessageID 为最后一条消息ID。
LastMessageAt time.Time `gorm:"index"` // LastMessageAt 为最后消息时间。
LastActiveAt time.Time `gorm:"index"` // LastActiveAt 为会话最近活跃时间。
LastMessageSummary string `gorm:"type:varchar(255);not null;default:''"` // LastMessageSummary 为最后一条消息摘要。
CustomerUnreadCount int `gorm:"type:int;not null;default:0"` // CustomerUnreadCount 为用户侧未读数。
AgentUnreadCount int `gorm:"type:int;not null;default:0"` // AgentUnreadCount 为客服侧未读数。
HandoffAt *time.Time `gorm:"index"` // HandoffAt 为最近一次转人工时间。
HandoffReason string `gorm:"type:varchar(255);not null;default:''"` // HandoffReason 为最近一次转人工原因。
QueueEnteredAt *time.Time `gorm:"index"` // QueueEnteredAt 为本次进入人工待接入队列的时间。
AIReplyRounds int `gorm:"type:int;not null;default:0"` // AIReplyRounds 为当前会话内 AI 已成功回复次数。
ClosedAt *time.Time `gorm:"index"` // ClosedAt 为会话关闭时间。
ClosedBy int64 `gorm:"type:bigint;not null;default:0;index"` // ClosedBy 为关闭人用户ID,访客关闭时写0。
CloseReason string `gorm:"type:varchar(255);not null;default:''"` // CloseReason 为关闭原因。
AuditFields
}
// ConversationParticipant 会话参与方。
type ConversationParticipant struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;index;uniqueIndex:uk_conversation_participant"`
ParticipantType string `gorm:"type:varchar(30);not null;default:'';index;uniqueIndex:uk_conversation_participant"`
ParticipantID int64 `gorm:"type:bigint;not null;default:0;uniqueIndex:uk_conversation_participant"`
ExternalParticipantID string `gorm:"type:varchar(128);not null;default:''"`
JoinedAt *time.Time
LeftAt *time.Time
Status enums.Status `gorm:"type:int;not null;default:0;index"`
AuditFields
}
// ConversationReadState 会话读游标。
type ConversationReadState struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;index;uniqueIndex:uk_conversation_reader"`
ReaderType enums.IMSenderType `gorm:"type:varchar(30);not null;default:'';index;uniqueIndex:uk_conversation_reader"`
ReaderID int64 `gorm:"type:bigint;not null;default:0;uniqueIndex:uk_conversation_reader"`
ExternalReaderID string `gorm:"type:varchar(128);not null;default:'';uniqueIndex:uk_conversation_reader"`
LastReadMessageID int64 `gorm:"type:bigint;not null;default:0;index"`
LastReadAt *time.Time
AuditFields
}
// Message 会话消息。
type Message struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;index;uniqueIndex:uk_conversation_client_msg"`
RequestID string `gorm:"type:varchar(128);not null;default:'';index"`
ClientMsgID string `gorm:"type:varchar(128);not null;default:'';uniqueIndex:uk_conversation_client_msg"`
SenderType enums.IMSenderType `gorm:"type:varchar(30);not null;default:'';index"`
SenderID int64 `gorm:"type:bigint;not null;default:0;index"`
ReceiverType string `gorm:"type:varchar(30);not null;default:'';index"`
MessageType enums.IMMessageType `gorm:"type:varchar(30);not null;default:'';index"`
Content string `gorm:"type:text"`
Payload string `gorm:"type:text"`
SendStatus enums.IMMessageStatus `gorm:"type:int;not null;default:2;index"`
SentAt *time.Time `gorm:"index"`
DeliveredAt *time.Time
ReadAt *time.Time
RecalledAt *time.Time
QuotedMessageID int64 `gorm:"type:bigint;not null;default:0;index"`
AuditFields
}
// WxWorkKFSyncState 企业微信客服消息同步状态表。
//
// 按 open_kfid 记录企业微信客服消息同步游标,用于 SyncMsg 增量拉取。
type WxWorkKFSyncState struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为同步状态主键。
OpenKfID string `gorm:"type:varchar(64);not null;default:'';uniqueIndex"` // OpenKfID 为企业微信客服账号ID。
NextCursor string `gorm:"type:varchar(128);not null;default:''"` // NextCursor 为下一次增量同步使用的游标。
LastSyncAt *time.Time `gorm:"index"` // LastSyncAt 为最近一次成功同步时间。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 为同步状态记录状态。
Remark string `gorm:"type:text"` // Remark 为同步异常、人工备注等补充信息。
AuditFields
}
// WxWorkKFConversation 企业微信客服渠道会话映射表。
//
// 维护平台会话与企业微信客服会话上下文的对应关系,供入站同步和下行发送复用。
type WxWorkKFConversation struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为渠道会话映射主键。
ConversationID int64 `gorm:"type:bigint;not null;uniqueIndex"` // ConversationID 为平台会话ID,一条平台会话仅对应一条当前有效渠道映射。
ChannelID int64 `gorm:"type:bigint;not null;default:0;index"` // ChannelID 为所属接入渠道ID,用于标识该会话来自哪个企业微信渠道配置。
OpenKfID string `gorm:"type:varchar(64);not null;default:'';index:idx_openkf_ext"` // OpenKfID 为企业微信客服账号ID。
ExternalUserID string `gorm:"type:varchar(128);not null;default:'';index:idx_openkf_ext"` // ExternalUserID 为企业微信客户ID。
ServicerUserID string `gorm:"type:varchar(128);not null;default:'';index"` // ServicerUserID 为企业微信当前接待客服成员UserID。
SessionStatus string `gorm:"type:varchar(30);not null;default:'';index"` // SessionStatus 为微信侧会话状态快照,如接入中、转接中、已结束。
LastWxMsgID string `gorm:"type:varchar(64);not null;default:'';index"` // LastWxMsgID 为最近一次同步到的微信消息ID。
LastWxMsgTime *time.Time `gorm:"index"` // LastWxMsgTime 为最近一次微信消息时间。
RawProfile string `gorm:"type:text"` // RawProfile 为微信侧原始会话补充信息JSON。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 为渠道会话映射状态。
AuditFields
}
// WxWorkKFMessageRef 企业微信客服消息映射表。
//
// 用于实现微信消息幂等消费,并保存平台消息与微信消息的双向映射关系。
type WxWorkKFMessageRef struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为消息映射主键。
ConversationID int64 `gorm:"type:bigint;not null;default:0;index"` // ConversationID 为所属平台会话ID。
MessageID int64 `gorm:"type:bigint;not null;default:0;index"` // MessageID 为所属平台消息ID;仅渠道消息尚未生成平台消息时可暂为0。
WxMsgID string `gorm:"type:varchar(64);not null;default:'';uniqueIndex"` // WxMsgID 为企业微信消息ID,用于幂等去重。
Direction string `gorm:"type:varchar(20);not null;default:'';index"` // Direction 为消息方向,如 in/out。
Origin int `gorm:"type:int;not null;default:0;index"` // Origin 为企业微信消息来源值,如客户发送、系统事件、企微客户端发送。
OpenKfID string `gorm:"type:varchar(64);not null;default:'';index"` // OpenKfID 为发送或接收该消息的客服账号ID。
ExternalUserID string `gorm:"type:varchar(128);not null;default:'';index"` // ExternalUserID 为消息对应的企业微信客户ID。
SendStatus string `gorm:"type:varchar(30);not null;default:'';index"` // SendStatus 为渠道发送状态快照,如 sent、failed。
FailReason string `gorm:"type:text"` // FailReason 为渠道发送失败原因或补偿说明。
RawPayload string `gorm:"type:text"` // RawPayload 为企业微信原始消息JSON。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 为消息映射状态。
AuditFields
}
// ChannelMessageOutbox 外部渠道消息投递任务表。
//
// 用于记录平台消息提交后的渠道发送任务,保证第三方发送动作与主事务解耦。
type ChannelMessageOutbox struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为投递任务主键。
ChannelType string `gorm:"type:varchar(30);not null;default:'';index;uniqueIndex:uk_channel_message"` // ChannelType 为目标渠道类型,如 wxwork_kf。
ConversationID int64 `gorm:"type:bigint;not null;default:0;index"` // ConversationID 为所属平台会话ID。
MessageID int64 `gorm:"type:bigint;not null;default:0;uniqueIndex:uk_channel_message"` // MessageID 为待投递的平台消息ID。
Payload string `gorm:"type:text"` // Payload 为渠道发送所需的标准化请求数据JSON。
SendStatus string `gorm:"type:varchar(30);not null;default:'';index"` // SendStatus 为当前投递状态,如 pending、sending、sent、failed。
RetryCount int `gorm:"type:int;not null;default:0"` // RetryCount 为已重试次数。
NextRetryAt *time.Time `gorm:"index"` // NextRetryAt 为下一次允许重试时间。
LastError string `gorm:"type:text"` // LastError 为最近一次发送失败信息。
SentAt *time.Time `gorm:"index"` // SentAt 为最终发送成功时间。
AuditFields
}
// ConversationAssignment 会话接待关系。
type ConversationAssignment struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;index"`
FromUserID int64 `gorm:"type:bigint;not null;default:0;index"`
ToUserID int64 `gorm:"type:bigint;not null;default:0;index"`
AssignType string `gorm:"type:varchar(30);not null;default:'';index"`
Reason string `gorm:"type:varchar(255);not null;default:''"`
Status enums.IMAssignmentStatus `gorm:"type:int;not null;index"`
CreatedAt time.Time `gorm:"not null;index"`
FinishedAt *time.Time
OperatorID int64 `gorm:"type:bigint;not null;default:0;index"`
}
// QuickReply 快捷回复。
type QuickReply struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
GroupName string `gorm:"type:varchar(50);not null;default:'';index"`
Title string `gorm:"type:varchar(100);not null;default:'';index"`
Content string `gorm:"type:text"`
Status enums.Status `gorm:"type:int;not null;index"`
SortNo int `gorm:"type:int;not null;index"`
AuditFields
}
// AIAgent AI 接待实例。
type AIAgent struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为 AI Agent 主键。
Name string `gorm:"type:varchar(100);not null;default:'';index"` // Name 为 AI Agent 名称。
Avatar string `gorm:"type:varchar(1024);not null;default:''"` // Avatar 为 AI Agent 在客服会话中展示的头像。
Description string `gorm:"type:varchar(255);not null;default:''"` // Description 为 AI Agent 描述。
Status enums.Status `gorm:"type:int;not null;index"` // Status 为 AI Agent
AIConfigID int64 `gorm:"type:bigint;not null;default:0;index"` // AIConfigID 为关联的 AI 配置ID。
MaxSteps int `gorm:"type:int;not null;default:6"` // MaxSteps 为一次 Agent Loop 允许的最大推理步骤数。
ContextWindow int `gorm:"type:int;not null;default:0"` // ContextWindow 为会话上下文消息窗口,0 表示使用运行时默认值。
ToolPolicy string `gorm:"type:text"` // ToolPolicy 为工具风险与确认策略JSON。
KnowledgePolicy string `gorm:"type:text"` // KnowledgePolicy 为知识检索与无依据回答策略JSON。
ServiceMode enums.IMConversationServiceMode `gorm:"type:int;not null;default:3;index"` // ServiceMode 为服务模式,如仅AI、仅人工、AI优先人工接管。
SystemPrompt string `gorm:"type:text"` // SystemPrompt 为该 Agent 的系统提示词。
WelcomeMessage string `gorm:"type:text"` // WelcomeMessage 为该 Agent 的欢迎语或首响模板。
ReplyTimeoutSeconds int `gorm:"type:int;not null;default:180"` // ReplyTimeoutSeconds 为异步自动回复超时秒数。
RolloutPercent int `gorm:"type:int;not null;default:100"` // RolloutPercent 为该 Agent 的会话灰度百分比,100 表示全量。
PreviousRolloutPercent int `gorm:"type:int;not null;default:0"` // PreviousRolloutPercent 保存上一次生效的灰度比例,0 表示尚无可回滚值。
TeamIDs string `gorm:"type:varchar(500);not null;default:''"` // TeamIDs 为转人工时可路由的客服组ID列表,多个之间使用逗号分隔。
HandoffMode enums.AIAgentHandoffMode `gorm:"type:int;not null;default:1"` // HandoffMode 为转人工执行方式,如进入待接入池、进入默认客服组待接入池。
FallbackMode enums.AIAgentFallbackMode `gorm:"type:int;not null;default:1"` // FallbackMode 为知识不足时的回复策略。
FallbackMessage string `gorm:"type:text"` // FallbackMessage 为知识不足回复文案。
KnowledgeIDs string `gorm:"type:varchar(500);not null;default:''"` // KnowledgeIDs 为绑定的知识库ID列表,按顺序表示优先级。
PublishedRevisionID int64 `gorm:"type:bigint;not null;default:0;index"` // PublishedRevisionID 为当前已发布 Agent 配置快照ID。
SortNo int `gorm:"type:int;not null;default:0;index"` // SortNo 为后台展示排序号。
AuditFields
}
// AgentRevision stores an immutable published Agent configuration snapshot.
type AgentRevision struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
AgentID int64 `gorm:"type:bigint;not null;index;uniqueIndex:uk_agent_revision"`
Revision int `gorm:"type:int;not null;uniqueIndex:uk_agent_revision"`
Status enums.Status `gorm:"type:int;not null;default:0;index"`
Definition string
DefinitionHash string `gorm:"type:varchar(64);not null;default:'';index"`
PublishedAt *time.Time `gorm:"index"`
PublishedByID int64 `gorm:"type:bigint;not null;default:0;index"`
PublishedByName string `gorm:"type:varchar(100);not null;default:''"`
AuditFields
}
// AgentRun is the parent audit record for one Agent Loop execution.
type AgentRun struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;default:0;index"`
AIAgentID int64 `gorm:"type:bigint;not null;default:0;index"`
AgentRevisionID int64 `gorm:"type:bigint;not null;default:0;index"`
SourceMessageID int64 `gorm:"type:bigint;not null;default:0;index"`
Status string `gorm:"type:varchar(30);not null;default:'';index"`
PromptTokens int `gorm:"type:int;not null;default:0"`
CompletionTokens int `gorm:"type:int;not null;default:0"`
StartedAt time.Time `gorm:"not null;index"`
EndedAt *time.Time `gorm:"index"`
ErrorMessage string `gorm:"type:text"`
TraceData string `gorm:"type:text"`
CreatedAt time.Time `gorm:"not null;index"`
UpdatedAt time.Time `gorm:"not null;index"`
}
// AgentStep records a normalized model, tool, workflow, or policy transition.
type AgentStep struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
AgentRunID int64 `gorm:"type:bigint;not null;index"`
StepType string `gorm:"type:varchar(50);not null;default:'';index"`
StepCode string `gorm:"type:varchar(100);not null;default:'';index"`
Status string `gorm:"type:varchar(30);not null;default:'';index"`
InputPreview string `gorm:"type:text"`
OutputPreview string `gorm:"type:text"`
ErrorMessage string `gorm:"type:text"`
StartedAt time.Time `gorm:"not null;index"`
EndedAt *time.Time `gorm:"index"`
DurationMS int `gorm:"type:int;not null;default:0"`
CreatedAt time.Time `gorm:"not null;index"`
}
// AgentToolCall records the safety-relevant details of a normalized tool call.
type AgentToolCall struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
AgentRunID int64 `gorm:"type:bigint;not null;index"`
AgentStepID int64 `gorm:"type:bigint;not null;default:0;index"`
ToolCode string `gorm:"type:varchar(150);not null;default:'';index"`
RiskLevel string `gorm:"type:varchar(30);not null;default:'';index"`
RequireConfirm bool `gorm:"not null;default:false"`
Status string `gorm:"type:varchar(30);not null;default:'';index"`
ArgumentsPreview string `gorm:"type:text"`
ResultPreview string `gorm:"type:text"`
ErrorMessage string `gorm:"type:text"`
DurationMS int `gorm:"type:int;not null;default:0"`
CreatedAt time.Time `gorm:"not null;index"`
}
// AgentRunQualityFeedback is an operator-provided quality review for one
// normalized Agent run. Runtime completion must not be treated as resolution.
type AgentRunQualityFeedback struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
AgentRunID int64 `gorm:"type:bigint;not null;uniqueIndex"`
ResolutionStatus enums.AgentRunResolutionStatus `gorm:"type:varchar(20);not null;default:'unknown';index"`
EvidenceStatus enums.AgentRunEvidenceStatus `gorm:"type:varchar(20);not null;default:'unknown';index"`
Comment string `gorm:"type:text"`
AuditFields
}
// Channel 接入渠道配置。
//
// 用于统一描述系统的外部接入入口。不同渠道类型共享统一的接入配置骨架,
// 例如网页客服渠道(web)和企业微信客服渠道(wxwork_kf)。
// 渠道本身负责定义“入口如何识别、默认接入哪个 AI Agent、渠道专属配置是什么”,
// 而具体消息收发、会话映射等运行时数据由各自的渠道业务表承载。
type Channel struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为渠道主键。
Name string `gorm:"type:varchar(100);not null;default:'';index"` // Name 为渠道名称,用于后台展示和业务识别,例如“官网客服”“企业微信主客服”。
ChannelType string `gorm:"type:varchar(30);not null;default:'';index"` // ChannelType 为渠道类型,决定该渠道的接入方式和配置解释规则。当前规划的典型取值包括:web、wxwork_kf。
ChannelID string `gorm:"type:varchar(64);not null;default:'';uniqueIndex"` // ChannelID 为渠道入口标识,由系统自动生成。对 web 渠道,该字段用于前端通过 X-Channel-Id 标识接入来源;对其他渠道,作为统一的系统内稳定渠道标识保留。
AIAgentID int64 `gorm:"type:bigint;not null;default:0;index"` // AIAgentID 为该渠道默认接入的 AI Agent。 当外部客户通过该渠道首次进入系统且尚未命中现有未结束会话时,系统会使用该 AI Agent 作为会话默认接待实例。
AIAgentRolloutPercent int `gorm:"type:int;not null;default:100"` // AIAgentRolloutPercent 为该渠道对 AI 自动回复施加的灰度百分比,100 表示不额外限制。
PreviousAIAgentRolloutPercent int `gorm:"type:int;not null;default:0"` // PreviousAIAgentRolloutPercent 保存渠道上一次生效的 Agent 灰度比例,0 表示尚无可回滚值。
// ConfigJSON 为渠道专属扩展配置,使用 JSON 存储。
// 例如:
// 1. web 渠道可记录允许域名、品牌配置等;
// 2. wxwork_kf 渠道可记录 openKfId、欢迎语策略等。
// 该字段只存储渠道类型私有配置,不承载通用主字段。
ConfigJSON string `gorm:"type:text"`
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 为渠道状态。禁用后,该渠道不再允许新会话接入;删除时采用软删除状态保留历史关联数据。
Remark string `gorm:"type:text"` // Remark 为渠道备注,用于记录接入说明、维护说明和内部运维信息。
AuditFields
}
// ConversationEventLog 会话事件日志。
type ConversationEventLog struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;index"`
RequestID string `gorm:"type:varchar(128);not null;default:'';index"`
EventType enums.IMEventType `gorm:"type:varchar(50);not null;default:'';index"`
OperatorType enums.IMSenderType `gorm:"type:varchar(30);not null;default:'';index"`
OperatorID int64 `gorm:"type:bigint;not null;default:0;index"`
Content string `gorm:"type:text"`
Payload string `gorm:"type:text"`
CreatedAt time.Time `gorm:"not null;index"`
}
// AgentProfile 客服档案。
type AgentProfile struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为客服档案主键。
UserID int64 `gorm:"type:bigint;not null;uniqueIndex"` // UserID 关联后台用户,一名用户只允许一份客服档案。
TeamID int64 `gorm:"type:bigint;not null;default:0;index"` // TeamID 为客服所属客服组。
AgentCode string `gorm:"type:varchar(64);not null;default:'';uniqueIndex"` // AgentCode 为客服工号,用于业务侧识别客服。
DisplayName string `gorm:"type:varchar(100);not null;default:'';index"` // DisplayName 为客服展示名,可区别于后台昵称。
Avatar string `gorm:"type:varchar(1024);not null;default:''"` // Avatar 为客服头像 URL。
ServiceStatus enums.ServiceStatus `gorm:"type:int;not null;default:0;index"` // ServiceStatus 表示客服服务状态:0空闲 1忙碌。
MaxConcurrentCount int `gorm:"type:int;not null;default:0"` // MaxConcurrentCount 表示客服最大并发接待数。
PriorityLevel int `gorm:"type:int;not null;default:0;index"` // PriorityLevel 表示自动分配优先级,值越大越优先。
AutoAssignEnabled bool `gorm:"not null;default:true;index"` // AutoAssignEnabled 表示是否参与自动分配。
ReceiveOfflineMessage bool `gorm:"not null;default:false"` // ReceiveOfflineMessage 表示离线时是否仍接收离线消息或转接消息。
LastOnlineAt *time.Time `gorm:"index"` // LastOnlineAt 记录最近一次在线时间。
LastStatusAt *time.Time `gorm:"index"` // LastStatusAt 记录最近一次状态变更时间。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 表示客服档案状态
Remark string `gorm:"type:text"` // Remark 记录客服备注信息。
AuditFields
}
// AgentTeam 客服组。
type AgentTeam struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为客服组主键。
Name string `gorm:"type:varchar(100);not null;default:'';index"` // Name 为客服组名称。
LeaderUserID int64 `gorm:"type:bigint;not null;default:0;index"` // LeaderUserID 为组长用户ID0 表示暂未设置。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 表示客服组状态
Description string `gorm:"type:varchar(255);not null;default:''"` // Description 为客服组简介,用于说明职责边界。
Remark string `gorm:"type:text"` // Remark 记录客服组内部备注。
AuditFields
}
// AgentTeamSchedule 客服组排班。
type AgentTeamSchedule struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为组排班主键。
TeamID int64 `gorm:"type:bigint;not null;index"` // TeamID 为被排班的客服组ID。
StartAt time.Time `gorm:"not null;index"` // StartAt 为班次开始时间。
EndAt time.Time `gorm:"not null;index"` // EndAt 为班次结束时间。
Remark string `gorm:"type:varchar(255);not null;default:''"` // Remark 记录排班备注。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 表示组排班记录状态。
AuditFields
}
// AIConfig AI 统一配置。
// 一条记录表示一个可直接调用的 AI 配置实例,
// 同时包含厂商接入信息、模型信息和调用参数,不再拆分 endpoint/model 两层概念。
type AIConfig struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为配置主键。
Name string `gorm:"type:varchar(100);not null;default:'';index"` // Name 为配置名称,用于后台识别和展示。
Provider enums.AIProvider `gorm:"type:varchar(50);not null;default:'';index"` // Provider 为供应商标识,例如 openai、azure_openai、dashscope。
BaseURL string `gorm:"type:varchar(255);not null;default:''"` // BaseURL 为模型服务基础地址,例如 https://api.openai.com/v1。
APIKey string `gorm:"type:varchar(255);not null;default:''"` // APIKey 为服务端请求模型接口所需密钥。
ModelType enums.AIModelType `gorm:"type:varchar(30);not null;default:'';index"` // ModelType 为模型类型,例如 llm、embedding、rerank。
ModelName string `gorm:"type:varchar(100);not null;default:'';index"` // ModelName 为实际请求时传给上游的模型名。
Dimension int `gorm:"type:int;not null;default:0"` // Dimension 为向量维度,仅 embedding 模型通常需要填写。
MaxContextTokens int `gorm:"type:int;not null;default:0"` // MaxContextTokens 为模型支持的最大上下文 token 数。
MaxOutputTokens int `gorm:"type:int;not null;default:0"` // MaxOutputTokens 为模型建议的最大输出 token 数。
TimeoutMS int `gorm:"type:int;not null;default:30000"` // TimeoutMS 为调用该配置的默认超时时间,单位毫秒。
MaxRetryCount int `gorm:"type:int;not null;default:0"` // MaxRetryCount 为默认最大重试次数。
RPMLimit int `gorm:"type:int;not null;default:0"` // RPMLimit 为每分钟请求数限制,0 表示未显式配置。
TPMLimit int `gorm:"type:int;not null;default:0"` // TPMLimit 为每分钟 token 数限制,0 表示未显式配置。
Status enums.Status `gorm:"type:int;not null;index"` // Status 状态;同一 modelType 仅允许一条启用记录。
SortNo int `gorm:"type:int;not null;index"` // SortNo 为排序号,用于后台展示和人工调整顺序。
Remark string `gorm:"type:text"` // Remark 为备注,用于记录用途、成本、限制和切换说明等补充信息。
Platform bool `gorm:"-" json:"-"` // Platform 标记该配置来自宿主平台代理,不写入数据库。
ChatEnabled bool `gorm:"-" json:"-"` // ChatEnabled 标记平台代理文本模型能力已启用。
VisionEnabled bool `gorm:"-" json:"-"` // VisionEnabled 标记平台代理视觉模型能力已启用。
VisionModel string `gorm:"-" json:"-"` // VisionModel 为当前图片消息调用使用的平台代理模型名。
EmbeddingEnabled bool `gorm:"-" json:"-"` // EmbeddingEnabled 标记平台代理向量模型能力已启用。
HTTPClient *http.Client `gorm:"-" json:"-"` // HTTPClient 为宿主提供的授权签名客户端。
AuditFields
}
// KnowledgeBase 知识库主表。
type KnowledgeBase struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为知识库主键。
Name string `gorm:"type:varchar(100);not null;default:'';index"` // Name 为知识库名称。
Description string `gorm:"type:text"` // Description 为知识库描述。
KnowledgeType string `gorm:"type:varchar(20);not null;default:'document';index"` // KnowledgeType 为知识库类型:document/faq。
Status enums.Status `gorm:"type:int;not null;index"` // Status 为状态
DefaultTopK int `gorm:"type:int;not null;default:10"` // DefaultTopK 为默认召回数量。
DefaultScoreThreshold float64 `gorm:"type:decimal(5,4);not null;default:0.5"` // DefaultScoreThreshold 为默认相似度阈值。
DefaultRerankLimit int `gorm:"type:int;not null;default:5"` // DefaultRerankLimit 为默认重排后保留数量。
ChunkProvider string `gorm:"type:varchar(30);not null;default:'structured'"` // ChunkProvider 为知识库分块策略 provider。
ChunkTargetTokens int `gorm:"type:int;not null;default:300"` // ChunkTargetTokens 为目标 chunk token 数。
ChunkMaxTokens int `gorm:"type:int;not null;default:400"` // ChunkMaxTokens 为单 chunk 最大 token 数。
ChunkOverlapTokens int `gorm:"type:int;not null;default:40"` // ChunkOverlapTokens 为相邻 chunk 重叠 token 数。
AnswerMode int `gorm:"type:int;not null;default:1"` // AnswerMode 为回答模式:1严格知识库模式 2辅助解释模式。
SortNo int `gorm:"type:int;not null;default:0;index"` // SortNo 为排序号,用于后台展示和知识库的人工排序管理。
Remark string `gorm:"type:text"` // Remark 为备注。
AuditFields
}
// KnowledgeDirectory 知识库内部目录表。
type KnowledgeDirectory struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为目录主键。
KnowledgeBaseID int64 `gorm:"type:bigint;not null;index:idx_kb_parent_sort"` // KnowledgeBaseID 为所属知识库 ID。
ParentID int64 `gorm:"type:bigint;not null;default:0;index:idx_kb_parent_sort"` // ParentID 为父目录 ID0 表示一级目录。
Name string `gorm:"type:varchar(100);not null;default:'';index"` // Name 为目录名称。
SortNo int `gorm:"type:int;not null;default:0;index:idx_kb_parent_sort"` // SortNo 为同级排序号。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 为状态。
Remark string `gorm:"type:text"` // Remark 为备注。
AuditFields
}
// KnowledgeDocument 知识文档主表。
type KnowledgeDocument struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为文档主键。
KnowledgeBaseID int64 `gorm:"type:bigint;not null;index"` // KnowledgeBaseID 为所属知识库ID。
DirectoryID int64 `gorm:"type:bigint;not null;default:0;index"` // DirectoryID 为所属知识库内部目录 ID,0 表示根目录。
Title string `gorm:"type:varchar(255);not null;default:'';index"` // Title 为文档标题。
ContentType enums.KnowledgeDocumentContentType `gorm:"type:varchar(20);not null;default:'html'"` // ContentType 为内容类型:html/markdown。
Content string `gorm:"type:text"` // Content 为文档内容。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 为状态
IndexStatus enums.KnowledgeDocumentIndexStatus `gorm:"type:varchar(20);not null;default:'pending';index"` // IndexStatus 为索引状态:pending/indexed/failed。
IndexedAt *time.Time `gorm:"index"` // IndexedAt 为最近一次索引成功时间。
IndexError string `gorm:"type:text"` // IndexError 为最近一次索引失败信息。
ContentHash string `gorm:"type:varchar(64);not null;default:'';index"` // ContentHash 为内容哈希,用于变更检测。
AuditFields
}
// KnowledgeFAQ FAQ 条目主表。
type KnowledgeFAQ struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为 FAQ 主键。
KnowledgeBaseID int64 `gorm:"type:bigint;not null;index"` // KnowledgeBaseID 为所属 FAQ 知识库 ID。
DirectoryID int64 `gorm:"type:bigint;not null;default:0;index"` // DirectoryID 为所属知识库内部目录 ID,0 表示根目录。
Question string `gorm:"type:varchar(500);not null;default:'';index"` // Question 为标准问题。
Answer string `gorm:"type:text"` // Answer 为标准答案。
SimilarQuestions string `gorm:"type:text"` // SimilarQuestions 为相似问 JSON 数组。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 为状态。
IndexStatus enums.KnowledgeDocumentIndexStatus `gorm:"type:varchar(20);not null;default:'pending';index"` // IndexStatus 为索引状态:pending/indexed/failed。
IndexedAt *time.Time `gorm:"index"` // IndexedAt 为最近一次索引成功时间。
IndexError string `gorm:"type:text"` // IndexError 为最近一次索引失败信息。
Remark string `gorm:"type:text"` // Remark 为备注。
AuditFields
}
// KnowledgeChunk 切片元数据表。
type KnowledgeChunk struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为切片主键。
KnowledgeBaseID int64 `gorm:"type:bigint;not null;index"` // KnowledgeBaseID 为知识库ID。
DocumentID int64 `gorm:"type:bigint;not null;default:0;index"` // DocumentID 为文档ID。
FaqID int64 `gorm:"type:bigint;not null;default:0;index"` // FaqID 为 FAQ ID。
ChunkNo int `gorm:"type:int;not null;default:0;index"` // ChunkNo 为切片序号。
Title string `gorm:"type:varchar(255);not null;default:''"` // Title 为切片标题。
Content string `gorm:"type:text"` // Content 为切片内容。
ContentHash string `gorm:"type:varchar(64);not null;default:'';index"` // ContentHash 为内容哈希。
CharCount int `gorm:"type:int;not null;default:0"` // CharCount 为字符数。
TokenCount int `gorm:"type:int;not null;default:0"` // TokenCount 为token数。
ChunkType string `gorm:"type:varchar(30);not null;default:''"` // ChunkType 为切片类型。
SectionPath string `gorm:"type:text"` // SectionPath 为章节路径。
Provider string `gorm:"type:varchar(30);not null;default:''"` // Provider 为分块 provider。
Status enums.Status `gorm:"type:int;not null;default:0;index"` // Status 为状态:1有效 2已删除。
VectorID string `gorm:"type:varchar(100);not null;default:'';index"` // VectorID 为向量库中的point ID。
CreatedAt time.Time `gorm:"not null;index"`
UpdatedAt time.Time `gorm:"not null;index"`
}
// KnowledgeRetrieveLog 检索日志表。
type KnowledgeRetrieveLog struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为日志主键。
KnowledgeBaseID int64 `gorm:"type:bigint;not null;index"` // KnowledgeBaseID 为知识库ID。
Channel string `gorm:"type:varchar(30);not null;default:'';index"` // Channel 为渠道:im会话, agent_assist坐席辅助, api开放接口, debug调试。
Scene string `gorm:"type:varchar(50);not null;default:'';index"` // Scene 为场景:first_response首响, assist辅助, qa问答。
SessionID string `gorm:"type:varchar(64);not null;default:'';index"` // SessionID 为会话ID。
ConversationID int64 `gorm:"type:bigint;not null;default:0;index"` // ConversationID 为会话ID。
RequestID string `gorm:"type:varchar(64);not null;default:'';index"` // RequestID 为请求ID。
Question string `gorm:"type:text"` // Question 为原始问题。
RewriteQuestion string `gorm:"type:text"` // RewriteQuestion 为改写后问题。
Answer string `gorm:"type:text"` // Answer 为生成的答案。
AnswerStatus int `gorm:"type:int;not null;default:1;index"` // AnswerStatus 为答案状态:1正常 2无答案 3兜底 4风控拦截。
HitCount int `gorm:"type:int;not null;default:0"` // HitCount 为命中数量。
TopScore float64 `gorm:"type:decimal(5,4);not null;default:0"` // TopScore 为最高相似度分数。
ChunkProvider string `gorm:"type:varchar(30);not null;default:'';index"` // ChunkProvider 为分块 provider。
ChunkTargetTokens int `gorm:"type:int;not null;default:0"` // ChunkTargetTokens 为目标 token 数。
ChunkMaxTokens int `gorm:"type:int;not null;default:0"` // ChunkMaxTokens 为最大 token 数。
ChunkOverlapTokens int `gorm:"type:int;not null;default:0"` // ChunkOverlapTokens 为重叠 token 数。
RerankEnabled bool `gorm:"not null;default:false;index"` // RerankEnabled 是否启用 rerank。
RerankLimit int `gorm:"type:int;not null;default:0"` // RerankLimit 为 rerank 条数。
CitationCount int `gorm:"type:int;not null;default:0"` // CitationCount 为最终引用条数。
UsedChunkCount int `gorm:"type:int;not null;default:0"` // UsedChunkCount 为进入上下文的 chunk 数。
LatencyMs int64 `gorm:"type:bigint;not null;default:0"` // LatencyMs 为总耗时毫秒。
RetrieveMs int64 `gorm:"type:bigint;not null;default:0"` // RetrieveMs 为检索耗时毫秒。
GenerateMs int64 `gorm:"type:bigint;not null;default:0"` // GenerateMs 为生成耗时毫秒。
PromptTokens int `gorm:"type:int;not null;default:0"` // PromptTokens 为prompt token数。
CompletionTokens int `gorm:"type:int;not null;default:0"` // CompletionTokens 为completion token数。
ModelName string `gorm:"type:varchar(100);not null;default:''"` // ModelName 为使用的模型名称。
TraceData string `gorm:"type:text"` // TraceData 为链路追踪数据JSON。
CreatedAt time.Time `gorm:"not null;index"`
}
// KnowledgeRetrieveHit 检索命中详情表。
type KnowledgeRetrieveHit struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为命中记录主键。
RetrieveLogID int64 `gorm:"type:bigint;not null;index"` // RetrieveLogID 为检索日志ID。
KnowledgeBaseID int64 `gorm:"type:bigint;not null;default:0;index"` // KnowledgeBaseID 为命中来源知识库ID。
ChunkID int64 `gorm:"type:bigint;not null;index"` // ChunkID 为切片ID。
DocumentID int64 `gorm:"type:bigint;not null;index"` // DocumentID 为文档ID。
DocumentTitle string `gorm:"type:varchar(255);not null;default:''"` // DocumentTitle 为文档标题。
FaqID int64 `gorm:"type:bigint;not null;default:0;index"` // FaqID 为 FAQ ID。
FaqQuestion string `gorm:"type:varchar(500);not null;default:''"` // FaqQuestion 为 FAQ 问题。
ChunkNo int `gorm:"type:int;not null;default:0"` // ChunkNo 为切片序号。
Title string `gorm:"type:varchar(255);not null;default:''"` // Title 为切片标题。
SectionPath string `gorm:"type:text"` // SectionPath 为章节路径。
ChunkType string `gorm:"type:varchar(30);not null;default:''"` // ChunkType 为切片类型。
Provider string `gorm:"type:varchar(30);not null;default:''"` // Provider 为分块 provider。
RankNo int `gorm:"type:int;not null;default:0"` // RankNo 为排名。
Score float64 `gorm:"type:decimal(5,4);not null;default:0"` // Score 为相似度分数。
RerankScore float64 `gorm:"type:decimal(5,4);not null;default:0"` // RerankScore 为重排分数。
UsedInAnswer bool `gorm:"not null;default:false"` // UsedInAnswer 是否用于生成答案。
IsCitation bool `gorm:"not null;default:false"` // IsCitation 是否作为引用返回。
Snippet string `gorm:"type:text"` // Snippet 为内容片段。
CreatedAt time.Time `gorm:"not null;index"`
}
// KnowledgeFeedback 问答反馈表。
type KnowledgeFeedback struct {
ID int64 `gorm:"primaryKey;autoIncrement"` // ID 为反馈主键。
RetrieveLogID int64 `gorm:"type:bigint;not null;index"` // RetrieveLogID 为检索日志ID。
FeedbackType int `gorm:"type:int;not null;default:1;index"` // FeedbackType 为反馈类型:1点赞 2点踩 3无帮助 4引用错误 5其他。
FeedbackReason string `gorm:"type:varchar(500);not null;default:''"` // FeedbackReason 为反馈原因。
UserID int64 `gorm:"type:bigint;not null;default:0;index"` // UserID 为用户ID。
AgentID int64 `gorm:"type:bigint;not null;default:0;index"` // AgentID 为坐席ID。
Remark string `gorm:"type:text"` // Remark 为备注。
CreatedAt time.Time `gorm:"not null;index"`
}
// ConversationInterrupt 表示会话级待恢复中断记录。
type ConversationInterrupt struct {
ID int64 `gorm:"primaryKey;autoIncrement"`
ConversationID int64 `gorm:"type:bigint;not null;default:0;index"`
AIAgentID int64 `gorm:"type:bigint;not null;default:0;index"`
AgentRunID int64 `gorm:"type:bigint;not null;default:0;index"`
AgentStepID int64 `gorm:"type:bigint;not null;default:0;index"`
SourceMessageID int64 `gorm:"type:bigint;not null;default:0;index"`
LastResumeMessageID int64 `gorm:"type:bigint;not null;default:0;index"`
CheckPointID string `gorm:"type:varchar(128);not null;default:'';uniqueIndex"`
InterruptID string `gorm:"type:varchar(255);not null;default:'';index"`
InterruptType string `gorm:"type:varchar(50);not null;default:'';index"`
Status string `gorm:"type:varchar(30);not null;default:'';index"`
PromptText string `gorm:"type:text"`
RequestData string `gorm:"type:text"`
CheckPointData string
ResumeCount int `gorm:"type:int;not null;default:0"`
ExpiresAt *time.Time `gorm:"index"`
CreatedAt time.Time `gorm:"not null;index"`
UpdatedAt time.Time `gorm:"not null;index"`
}