types.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. package service
  2. import "time"
  3. // BroadcastHub 描述 WebSocket Hub 的广播能力。
  4. type BroadcastHub interface {
  5. BroadcastMessage(conversationID uint, messageType string, data interface{})
  6. BroadcastToAllAgents(messageType string, data interface{})
  7. }
  8. // InitConversationInput 对话初始化需要的输入数据。
  9. type InitConversationInput struct {
  10. VisitorID uint
  11. Website string
  12. Referrer string
  13. Browser string
  14. OS string
  15. Language string
  16. IPAddress string
  17. ChatMode string // 对话模式:human(人工客服)、ai(AI客服)
  18. AIConfigID *uint // AI 配置 ID(访客选择的模型配置,AI 模式时必需)
  19. }
  20. // InitConversationResult 对话初始化后的返回结果。
  21. type InitConversationResult struct {
  22. ConversationID uint
  23. Status string
  24. AccessToken string
  25. }
  26. // UpdateConversationContactInput 更新访客联系信息时需要的参数。
  27. type UpdateConversationContactInput struct {
  28. ConversationID uint
  29. Email *string
  30. Phone *string
  31. Notes *string
  32. }
  33. // ConversationListResult 分页会话列表。
  34. type ConversationListResult struct {
  35. Items []ConversationSummary
  36. Total int64
  37. Page int
  38. PageSize int
  39. HasMore bool
  40. TotalUnread int64
  41. }
  42. type ConversationSummary struct {
  43. ID uint
  44. ConversationType string // visitor | internal
  45. VisitorID uint
  46. AgentID uint
  47. Status string
  48. ChatMode string // human | ai
  49. CreatedAt time.Time
  50. UpdatedAt time.Time
  51. LastMessage *LastMessageSummary
  52. UnreadCount int64
  53. LastSeenAt *time.Time // 最后活跃时间,用于判断在线状态
  54. HasParticipated bool // 当前用户是否参与过该会话(是否发送过消息)
  55. }
  56. // LastMessageSummary 会话最后一条消息的摘要信息。
  57. type LastMessageSummary struct {
  58. ID uint
  59. Content string
  60. SenderIsAgent bool
  61. MessageType string
  62. IsRead bool
  63. ReadAt *time.Time
  64. CreatedAt time.Time
  65. }
  66. // ConversationDetail 在会话概要基础上附加访客信息。
  67. type ConversationDetail struct {
  68. ConversationSummary
  69. Website string
  70. Referrer string
  71. Browser string
  72. OS string
  73. Language string
  74. IPAddress string
  75. Location string
  76. Email string
  77. Phone string
  78. Notes string
  79. LastSeen *time.Time
  80. }
  81. // CreateMessageInput 创建消息时需要的参数。
  82. type CreateMessageInput struct {
  83. ConversationID uint
  84. Content string
  85. SenderID uint
  86. SenderIsAgent bool
  87. // 文件相关字段(可选)
  88. FileURL *string // 文件URL
  89. FileType *string // 文件类型:image, document
  90. FileName *string // 原始文件名
  91. FileSize *int64 // 文件大小(字节)
  92. MimeType *string // MIME类型
  93. // 回复数据源开关(仅 AI 模式有效):不传或 nil 时使用默认(知识库+大模型开,联网关)
  94. UseKnowledgeBase *bool // 是否使用知识库检索,默认 true
  95. UseLLM *bool // 无知识库匹配时是否用大模型回复,默认 true
  96. UseWebSearch *bool // 是否允许联网搜索(需本回合 NeedWebSearch 或策略触发),默认 false
  97. NeedWebSearch bool // 本回合是否请求联网搜索(如用户点击「联网搜索」),默认 false
  98. }
  99. // CreateAgentInput 创建客服或管理员账号需要的参数。
  100. type CreateAgentInput struct {
  101. Username string
  102. Password string
  103. Role string
  104. }
  105. // MarkMessagesReadResult 消息标记已读后的返回信息。
  106. type MarkMessagesReadResult struct {
  107. ConversationID uint
  108. MessageIDs []uint
  109. UnreadCount int64
  110. ReadAt time.Time
  111. }
  112. // UpdateProfileInput 更新个人资料时需要的参数。
  113. type UpdateProfileInput struct {
  114. UserID uint
  115. Nickname *string
  116. Email *string
  117. ReceiveAIConversations *bool // 是否接收 AI 对话(可选)
  118. }
  119. // ProfileResult 个人资料信息。
  120. type ProfileResult struct {
  121. ID uint `json:"id"`
  122. Username string `json:"username"`
  123. Role string `json:"role"`
  124. Permissions []string `json:"permissions"`
  125. AvatarURL string `json:"avatar_url"`
  126. Nickname string `json:"nickname"`
  127. Email string `json:"email"`
  128. ReceiveAIConversations bool `json:"receive_ai_conversations"` // 是否接收 AI 对话
  129. }
  130. // UserSummary 用户列表摘要信息(不包含密码)。
  131. type UserSummary struct {
  132. ID uint `json:"id"`
  133. Username string `json:"username"`
  134. Role string `json:"role"`
  135. Permissions []string `json:"permissions"`
  136. Nickname string `json:"nickname"`
  137. Email string `json:"email"`
  138. AvatarURL string `json:"avatar_url"`
  139. ReceiveAIConversations bool `json:"receive_ai_conversations"`
  140. CreatedAt time.Time `json:"created_at"`
  141. UpdatedAt time.Time `json:"updated_at"`
  142. }
  143. // CreateUserInput 创建用户输入。
  144. type CreateUserInput struct {
  145. Username string // 用户名(必需)
  146. Password string // 密码(必需)
  147. Role string // 角色:"admin" 或 "agent"(必需)
  148. Permissions []string // 功能权限(可选;role=admin 时忽略)
  149. Nickname *string // 昵称(可选)
  150. Email *string // 邮箱(可选)
  151. }
  152. // UpdateUserInput 更新用户输入。
  153. type UpdateUserInput struct {
  154. UserID uint // 用户ID(必需)
  155. Role *string // 角色(可选)
  156. Permissions *[]string // 功能权限(可选;role=admin 时忽略)
  157. Nickname *string // 昵称(可选)
  158. Email *string // 邮箱(可选)
  159. ReceiveAIConversations *bool // 是否接收 AI 对话(可选)
  160. }
  161. // UpdatePasswordInput 更新密码输入。
  162. type UpdatePasswordInput struct {
  163. UserID uint // 用户ID(必需)
  164. OldPassword *string // 旧密码(可选,管理员修改其他用户密码时不需要)
  165. NewPassword string // 新密码(必需)
  166. IsAdmin bool // 是否是管理员操作(必需)
  167. }
  168. // FAQSummary FAQ(常见问题)摘要信息。
  169. type FAQSummary struct {
  170. ID uint `json:"id"`
  171. Question string `json:"question"` // 问题
  172. Answer string `json:"answer"` // 答案
  173. Keywords string `json:"keywords"` // 关键词(用于搜索)
  174. CreatedAt time.Time `json:"created_at"` // 创建时间
  175. UpdatedAt time.Time `json:"updated_at"` // 更新时间
  176. }
  177. // CreateFAQInput 创建 FAQ 输入。
  178. type CreateFAQInput struct {
  179. Question string // 问题(必需)
  180. Answer string // 答案(必需)
  181. Keywords string // 关键词(可选,用逗号或空格分隔)
  182. }
  183. // UpdateFAQInput 更新 FAQ 输入。
  184. type UpdateFAQInput struct {
  185. Question *string // 问题(可选)
  186. Answer *string // 答案(可选)
  187. Keywords *string // 关键词(可选)
  188. }
  189. // OnlineAgent 在线客服信息(供访客查看)。
  190. type OnlineAgent struct {
  191. ID uint `json:"id"` // 客服ID
  192. Nickname string `json:"nickname"` // 昵称
  193. AvatarURL string `json:"avatar_url"` // 头像URL
  194. }
  195. // DocumentSummary 文档摘要信息。
  196. type DocumentSummary struct {
  197. ID uint `json:"id"`
  198. KnowledgeBaseID uint `json:"knowledge_base_id"`
  199. Title string `json:"title"`
  200. Content string `json:"content"`
  201. Summary string `json:"summary"`
  202. Type string `json:"type"`
  203. Status string `json:"status"`
  204. EmbeddingStatus string `json:"embedding_status"`
  205. CreatedAt time.Time `json:"created_at"`
  206. UpdatedAt time.Time `json:"updated_at"`
  207. }
  208. // CreateDocumentInput 创建文档输入。
  209. type CreateDocumentInput struct {
  210. KnowledgeBaseID uint // 知识库 ID(必需)
  211. Title string // 文档标题(必需)
  212. Content string // 文档内容(必需)
  213. Summary string // 文档摘要(可选)
  214. Type string // 文档类型(可选,默认:document)
  215. Status string // 文档状态(可选,默认:draft)
  216. Metadata map[string]interface{} // 元数据(可选)
  217. }
  218. // UpdateDocumentInput 更新文档输入。
  219. type UpdateDocumentInput struct {
  220. Title *string // 文档标题(可选)
  221. Content *string // 文档内容(可选)
  222. Summary *string // 文档摘要(可选)
  223. Type *string // 文档类型(可选)
  224. Status *string // 文档状态(可选)
  225. Metadata *map[string]interface{} // 元数据(可选)
  226. }
  227. // DocumentListResult 文档列表查询结果。
  228. type DocumentListResult struct {
  229. Documents []DocumentSummary `json:"documents"` // 文档列表
  230. Total int64 `json:"total"` // 总记录数
  231. Page int `json:"page"` // 当前页码
  232. PageSize int `json:"page_size"` // 每页大小
  233. TotalPage int `json:"total_page"` // 总页数
  234. }
  235. // KnowledgeBaseSummary 知识库摘要信息。
  236. type KnowledgeBaseSummary struct {
  237. ID uint `json:"id"`
  238. Name string `json:"name"`
  239. Description string `json:"description"`
  240. DocumentCount int64 `json:"document_count"` // 文档数量(统计信息)
  241. RAGEnabled bool `json:"rag_enabled"` // 是否参与 RAG(对 AI 开放)
  242. CreatedAt time.Time `json:"created_at"`
  243. UpdatedAt time.Time `json:"updated_at"`
  244. }
  245. // CreateKnowledgeBaseInput 创建知识库输入。
  246. type CreateKnowledgeBaseInput struct {
  247. Name string // 知识库名称(必需)
  248. Description string // 知识库描述(可选)
  249. }
  250. // UpdateKnowledgeBaseInput 更新知识库输入。
  251. type UpdateKnowledgeBaseInput struct {
  252. Name *string // 知识库名称(可选)
  253. Description *string // 知识库描述(可选)
  254. RAGEnabled *bool // 是否参与 RAG(可选)
  255. }
  256. // MessageAttachment 当前用户消息的附件(用于多模态:识图等)
  257. type MessageAttachment struct {
  258. FileURL string // 文件 URL(创建消息时返回的 file_url)
  259. FileType string // image / document
  260. MimeType string // 如 image/jpeg
  261. }
  262. // GenerateAIResponseInput 生成 AI 回复时的选项(数据源开关等)。
  263. type GenerateAIResponseInput struct {
  264. UseKnowledgeBase *bool // 是否使用知识库,默认 true
  265. UseLLM *bool // 无知识库时是否用大模型回复,默认 true
  266. UseWebSearch *bool // 是否允许联网,默认 false
  267. NeedWebSearch bool // 本回合是否请求联网(如用户点击按钮),默认 false
  268. Attachment *MessageAttachment // 当前条消息的附件(如图片),用于多模态识图
  269. }
  270. // GenerateAIResponseResult 生成 AI 回复的结果(内容 + 使用的数据源标记)。
  271. type GenerateAIResponseResult struct {
  272. Content string // 合成的一条回复
  273. SourcesUsed string // 逗号分隔,如 "knowledge_base" / "knowledge_base,llm" / "llm,web",供前端展示
  274. // 生图时返回生成图片的 URL,写入 AI 消息的 file_url
  275. GeneratedFileURL *string
  276. // GenerationFailed 为 true 表示大模型调用失败,内容为兜底话术(仍返回 err==nil 时由 message 层写入 is_ai_generation_failed)
  277. GenerationFailed bool
  278. }