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

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

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

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

72 lines
2.0 KiB
Go

package repositories
import (
"code.tczkiot.com/wlw/ai-agent/internal/models"
"code.tczkiot.com/wlw/ai-agent/internal/pkg/httpx/params"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AgentRunRepository = newAgentRunRepository()
func newAgentRunRepository() *agentRunRepository {
return &agentRunRepository{}
}
type agentRunRepository struct{}
func (r *agentRunRepository) Get(db *gorm.DB, id int64) *models.AgentRun {
ret := &models.AgentRun{}
if err := db.First(ret, "id = ?", id).Error; err != nil {
return nil
}
return ret
}
func (r *agentRunRepository) Create(db *gorm.DB, item *models.AgentRun) error {
return db.Create(item).Error
}
func (r *agentRunRepository) FindPageByCnd(db *gorm.DB, cnd *sqls.Cnd) (list []models.AgentRun, paging *sqls.Paging) {
cnd.Find(db, &list)
return list, &sqls.Paging{Page: cnd.Paging.Page, Limit: cnd.Paging.Limit, Total: cnd.Count(db, &models.AgentRun{})}
}
func (r *agentRunRepository) FindPageByParams(db *gorm.DB, queryParams *params.QueryParams) (list []models.AgentRun, paging *sqls.Paging) {
return r.FindPageByCnd(db, &queryParams.Cnd)
}
func (r *agentRunRepository) Updates(db *gorm.DB, id int64, columns map[string]any) error {
return db.Model(&models.AgentRun{}).Where("id = ?", id).Updates(columns).Error
}
func (r *agentRunRepository) FindRecent(db *gorm.DB, aiAgentID int64, limit int) []models.AgentRun {
if limit <= 0 || limit > 5000 {
limit = 5000
}
query := db.Order("id DESC").Limit(limit)
if aiAgentID > 0 {
query = query.Where("ai_agent_id = ?", aiAgentID)
}
var items []models.AgentRun
if err := query.Find(&items).Error; err != nil {
return nil
}
return items
}
func (r *agentRunRepository) FindRecentByConversationID(db *gorm.DB, conversationID int64, limit int) []models.AgentRun {
if conversationID <= 0 {
return nil
}
if limit <= 0 || limit > 20 {
limit = 6
}
var items []models.AgentRun
if err := db.Where("conversation_id = ?", conversationID).Order("id DESC").Limit(limit).Find(&items).Error; err != nil {
return nil
}
return items
}