Files
ai-agent/workflow/dao/session/exec_chat_dao.go
T

88 lines
2.7 KiB
Go

package session
import (
"ai-agent/workflow/consts/public"
sessionDto "ai-agent/workflow/model/dto/session"
"ai-agent/workflow/model/entity"
"context"
"gitea.redpowerfuture.com/red-future/common/beans"
"gitea.redpowerfuture.com/red-future/common/db/gfdb"
"github.com/gogf/gf/v2/util/gconv"
)
var ExecChatDao = &execChatDao{}
type execChatDao struct{}
func (d *execChatDao) Insert(ctx context.Context, req *sessionDto.CreateExecChatReq) (id int64, err error) {
var s = new(entity.ExecChat)
if err = gconv.Struct(req, &s); err != nil {
return
}
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecChat).Insert(s)
if err != nil {
return
}
return r.LastInsertId()
}
func (d *execChatDao) Update(ctx context.Context, req *sessionDto.UpdateExecChatReq) (rows int64, err error) {
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecChat).OmitEmpty().Data(&req).Where(entity.ExecChatCol.Id, req.Id).Update()
if err != nil {
return
}
return r.RowsAffected()
}
// ClearError 清空对话执行记录的报错信息(重新执行成功后调用,OmitEmpty 的 Update 会跳过空串,需显式写空)
func (d *execChatDao) ClearError(ctx context.Context, id int64) (rows int64, err error) {
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecChat).
Where(entity.ExecChatCol.Id, id).
Data(map[string]any{
entity.ExecChatCol.ErrorMessage: "",
entity.ExecChatCol.Error: "",
}).
Update()
if err != nil {
return
}
return r.RowsAffected()
}
func (d *execChatDao) Delete(ctx context.Context, req *sessionDto.DeleteExecChatReq) (rows int64, err error) {
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecChat).Where(entity.ExecChatCol.Id, req.Id).Delete()
if err != nil {
return
}
return r.RowsAffected()
}
func (d *execChatDao) List(ctx context.Context, creator string, page *beans.Page) (res []*entity.ExecChat, total int, err error) {
m := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecChat).
Where(entity.ExecChatCol.Creator, creator)
m.OrderDesc(entity.ExecChatCol.CreatedAt)
if page != nil {
m.Page(int(page.PageNum), int(page.PageSize))
}
r, total, err := m.AllAndCount(false)
if err != nil {
return
}
err = r.Structs(&res)
return
}
// ListBySession 查询会话下普通对话执行记录(按创建时间倒序)
func (d *execChatDao) ListBySession(ctx context.Context, sessionId string) (res []*entity.ExecChat, err error) {
r, err := gfdb.DB(ctx, public.DbNameBlackDeacon).Model(ctx, public.TableNameExecChat).
Where(entity.ExecChatCol.SessionId, sessionId).
OrderDesc(entity.ExecChatCol.CreatedAt).
All()
if err != nil {
return
}
err = r.Structs(&res)
return
}