feat: 新增Chat会话和群聊API
- 新增chat_session相关模型、仓库和服务 - 新增chat_group相关模型、仓库和服务 - 新增session_handler和chat_group_handler - 实现会话管理和群聊功能 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
67
server/internal/model/chat_group.go
Normal file
67
server/internal/model/chat_group.go
Normal file
@@ -0,0 +1,67 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// ChatGroup 群聊
|
||||
type ChatGroup struct {
|
||||
ID string `json:"id" gorm:"primaryKey;type:varchar(36)"`
|
||||
UserID string `json:"user_id" gorm:"type:varchar(36);index"`
|
||||
Name string `json:"name" gorm:"type:varchar(100)"`
|
||||
Description string `json:"description" gorm:"type:text"`
|
||||
AgentIDs string `json:"agent_ids" gorm:"type:text"` // JSON数组,存储群聊中的Agent ID列表
|
||||
Status string `json:"status" gorm:"type:varchar(20);default:'active'"` // active/archived
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (ChatGroup) TableName() string {
|
||||
return "chat_groups"
|
||||
}
|
||||
|
||||
// CreateGroupRequest 创建群聊请求
|
||||
type CreateGroupRequest struct {
|
||||
UserID string `json:"user_id"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Description string `json:"description"`
|
||||
AgentIDs string `json:"agent_ids" binding:"required"` // JSON数组格式
|
||||
}
|
||||
|
||||
// UpdateGroupRequest 更新群聊请求
|
||||
type UpdateGroupRequest struct {
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
AgentIDs string `json:"agent_ids"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// GroupChatRequest 群聊对话请求
|
||||
type GroupChatRequest struct {
|
||||
Message string `json:"message" binding:"required"`
|
||||
AgentIDs string `json:"agent_ids"` // 可选,覆盖群聊中配置的Agent
|
||||
SessionID string `json:"session_id"` // 可选,关联的会话ID
|
||||
}
|
||||
|
||||
// GroupChatResponse 群聊对话响应
|
||||
type GroupChatResponse struct {
|
||||
SessionID string `json:"session_id"`
|
||||
Reply string `json:"reply"`
|
||||
DurationMs int `json:"duration_ms"`
|
||||
TokensUsed int `json:"tokens_used"`
|
||||
Strategy string `json:"strategy"`
|
||||
SubtaskResults []SubtaskResult `json:"subtask_results"`
|
||||
}
|
||||
|
||||
// SubtaskResult 子任务结果
|
||||
type SubtaskResult struct {
|
||||
AgentID string `json:"agent_id"`
|
||||
AgentName string `json:"agent_name"`
|
||||
Reply string `json:"reply"`
|
||||
TokensUsed int `json:"tokens_used"`
|
||||
DurationMs int `json:"duration_ms"`
|
||||
}
|
||||
|
||||
// GroupListResponse 群聊列表响应
|
||||
type GroupListResponse struct {
|
||||
List []ChatGroup `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
71
server/internal/model/chat_session.go
Normal file
71
server/internal/model/chat_session.go
Normal file
@@ -0,0 +1,71 @@
|
||||
package model
|
||||
|
||||
import "time"
|
||||
|
||||
// ChatSession 会话
|
||||
type ChatSession struct {
|
||||
ID string `json:"id" gorm:"primaryKey;type:varchar(36)"`
|
||||
UserID string `json:"user_id" gorm:"type:varchar(36);index"`
|
||||
AgentID string `json:"agent_id" gorm:"type:varchar(36);index"`
|
||||
Title string `json:"title" gorm:"type:varchar(255)"`
|
||||
ModelID string `json:"model_id" gorm:"type:varchar(36)"`
|
||||
Status string `json:"status" gorm:"type:varchar(20);default:'active'"` // active/archived
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func (ChatSession) TableName() string {
|
||||
return "chat_sessions"
|
||||
}
|
||||
|
||||
// ChatMessage 消息
|
||||
type ChatMessage struct {
|
||||
ID string `json:"id" gorm:"primaryKey;type:varchar(36)"`
|
||||
SessionID string `json:"session_id" gorm:"type:varchar(36);index"`
|
||||
Role string `json:"role" gorm:"type:varchar(20)"` // user/assistant/system
|
||||
Content string `json:"content" gorm:"type:text"`
|
||||
TokensUsed int `json:"tokens_used" gorm:"default:0"`
|
||||
DurationMs int `json:"duration_ms" gorm:"default:0"`
|
||||
Metadata string `json:"metadata" gorm:"type:text"` // JSON格式存储额外信息
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
func (ChatMessage) TableName() string {
|
||||
return "chat_messages"
|
||||
}
|
||||
|
||||
// CreateSessionRequest 创建会话请求
|
||||
type CreateSessionRequest struct {
|
||||
UserID string `json:"user_id"`
|
||||
AgentID string `json:"agent_id"`
|
||||
Title string `json:"title"`
|
||||
ModelID string `json:"model_id"`
|
||||
}
|
||||
|
||||
// UpdateSessionRequest 更新会话请求
|
||||
type UpdateSessionRequest struct {
|
||||
Title string `json:"title"`
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// CreateMessageRequest 创建消息请求
|
||||
type CreateMessageRequest struct {
|
||||
SessionID string `json:"session_id" binding:"required"`
|
||||
Role string `json:"role" binding:"required"` // user/assistant
|
||||
Content string `json:"content" binding:"required"`
|
||||
TokensUsed int `json:"tokens_used"`
|
||||
DurationMs int `json:"duration_ms"`
|
||||
Metadata string `json:"metadata"`
|
||||
}
|
||||
|
||||
// SessionListResponse 会话列表响应
|
||||
type SessionListResponse struct {
|
||||
List []ChatSession `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
|
||||
// MessageListResponse 消息列表响应
|
||||
type MessageListResponse struct {
|
||||
List []ChatMessage `json:"list"`
|
||||
Total int64 `json:"total"`
|
||||
}
|
||||
Reference in New Issue
Block a user