refactor: 后端仓库层收口(A4:任务/项目/笔记/专员/动作定义)
把 B 档(此前尚无仓库的对象)的 api 裸查询收进仓库,api 层裸 store.DB 从 162 降到 96(口径:internal/ 下非测试 .go,不含 internal/repository/ 自身)。 新增 7 个仓库:TaskRecordRepo / TaskArtifactRepo / TaskRunRepo / ProjectRepo / StudyNoteRepo / SpecialistRepo / ActionDefinitionRepo。 按对象补齐的方法: - TaskRecordRepo:GetByID / GetByIDForOwners / ListBySpecialistKey / ListByOwners / ListByProject / CountBySpecialistKey / ClearProject / DeleteCascade - ProjectRepo:GetByIDForOwners / ListByOwners - StudyNoteRepo:ListByUser / GetByID - SpecialistRepo:GetByKey / GetByID / Query / List / CountByKey - TaskArtifactRepo / TaskRunRepo:GetByID / ListByTask - ActionDefinitionRepo:List / GetByID / GetByKey 几条口径改由仓库单点持有,避免各处手写漂移: - 归属过滤抽成 ownerScope:owners 为空时套恒假条件(空集),绝不退化成全表。 specialists/runtime 的 MyTaskQuery / ProjectQuery 随之删除,调用方改用 MyTaskOwners + 仓库方法 —— 任务与项目共用同一套归属口径。 - 「任务 + 交付物 + 运行记录三张表同事务级联删除」从 handler 收进 DeleteCascade, 不留没有任务的孤儿交付物;删项目只解除其下任务的 project_id 归属(置 NULL, 不是 0),不删任务本身 —— 任务是「做过的事」,删一个分组不该把它一起抹掉。 - 专员目录的 state 档位(默认 active / all 仅管理员 / system / 其它精确匹配, 且除显式要 system 外一律排除 system 记录)收进 SpecialistRepo.List。 - GetByKey 有意不过滤 state:调用方口径不同(建任务时要能查到,对话取 prompt 时要拒绝 inactive),口径留在调用方,仓库只负责取数。顺带把「按 key 找专员」 从 4 个文件里各写一遍收敛成一处。 - ActionDefinitionRepo.List 不替调用方定 state 默认值 ——「不传 state 就只看 active」是列表接口的契约,由 handler 解析 query 后传入。 - StudyNoteRepo.GetByID 不判归属,越权检查留在 handler(那里能把「不存在」与 「不是你的」分别回成 404 / 403)。 保留未动:skills/api/office_handlers.go 里两处 store.DB.Transaction —— 运行记录 与交付物要在同一个事务里落库,QueryBuilder 不带事务,维持原样并就地注明。 验证:go build ./... 与 go vet ./... 干净,go test ./... 6 个包全绿。 另用独立验证程序走真实路由 + 真实 HTTP,对数据库副本跑 148 项断言全绿 (覆盖跨用户越权 404、空 owners 退化成空集、级联删除、解除归属置 NULL、 笔记按用户隔离、专员 key 唯一性排除自身、管理员 state=all 仍排除 system 等)。 另对其中 10 条关键语义做了变异测试:逐条注入反向实现,确认断言确实会失败, 捕获 10 / 漏掉 0。变异测试同时暴露并修掉了验证体系自身的两个漏洞: - 「state=all 排除 system」这条规则此前没有任何断言能观察到 —— 默认档被 state=active 挡着、system 档被 state=system 挡着,删掉实现也不会红; - 变异驱动只跑 HTTP 断言、不跑 go test,导致针对单元测试注入的变异 (拒绝已下线专员的 prompt)永远逮不到。 验证程序为一次性脚手架,验证完成后已删除(tmp_vfy_b/)。 原始 data/eai_agentplatform.db 全程未触碰(mtime 仍为 2026-09-17 15:14)。 Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -7,10 +7,17 @@ import (
|
||||
|
||||
"eai_agentplatform/backend/internal/jsonutil"
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/repository"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
)
|
||||
|
||||
// actionDefinitionRepo 动作定义仓库(便于测试时覆写),包内共享。
|
||||
var actionDefinitionRepo repository.ActionDefinitionRepo
|
||||
|
||||
func init() {
|
||||
actionDefinitionRepo = repository.ActionDefinitionRepo{}
|
||||
}
|
||||
|
||||
type actionDefinitionReq struct {
|
||||
Key string `json:"key"`
|
||||
Label string `json:"label"`
|
||||
@@ -79,18 +86,12 @@ func validateActionDefinitionReq(req *actionDefinitionReq) *web.AppError {
|
||||
}
|
||||
|
||||
func ListActionDefinitions(c *gin.Context) {
|
||||
q := store.DB.Model(&model.ActionDefinition{})
|
||||
if c.Query("state") == "" {
|
||||
q = q.Where("state = ?", "active")
|
||||
} else {
|
||||
q = q.Where("state = ?", c.Query("state"))
|
||||
// 不传 state 默认只看 active —— 这是列表接口的契约,放在这里解析。
|
||||
state := c.Query("state")
|
||||
if state == "" {
|
||||
state = "active"
|
||||
}
|
||||
var items []model.ActionDefinition
|
||||
if err := q.Order("sort_order ASC, id ASC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询 Action 定义失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
web.OK(c, actionDefinitionRepo.List(state))
|
||||
}
|
||||
|
||||
func GetActionDefinitionByKey(c *gin.Context) {
|
||||
@@ -99,8 +100,8 @@ func GetActionDefinitionByKey(c *gin.Context) {
|
||||
web.Fail(c, web.NewBadRequest("action key 不能为空"))
|
||||
return
|
||||
}
|
||||
var item model.ActionDefinition
|
||||
if err := store.DB.Where("key = ?", key).First(&item).Error; err != nil {
|
||||
item, found := actionDefinitionRepo.GetByKey(key)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("Action 定义不存在"))
|
||||
return
|
||||
}
|
||||
@@ -133,7 +134,7 @@ func CreateActionDefinition(c *gin.Context) {
|
||||
State: req.State,
|
||||
SortOrder: req.SortOrder,
|
||||
}
|
||||
if err := store.DB.Create(&item).Error; err != nil {
|
||||
if !actionDefinitionRepo.Insert(&item) {
|
||||
web.Fail(c, web.NewBadRequest("创建 Action 定义失败"))
|
||||
return
|
||||
}
|
||||
@@ -145,8 +146,8 @@ func UpdateActionDefinition(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var item model.ActionDefinition
|
||||
if err := store.DB.First(&item, id).Error; err != nil {
|
||||
item, found := actionDefinitionRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("Action 定义不存在"))
|
||||
return
|
||||
}
|
||||
@@ -173,7 +174,7 @@ func UpdateActionDefinition(c *gin.Context) {
|
||||
item.OntologyBindingJSON = req.OntologyBindingJSON
|
||||
item.State = req.State
|
||||
item.SortOrder = req.SortOrder
|
||||
if err := store.DB.Save(&item).Error; err != nil {
|
||||
if !actionDefinitionRepo.Update(&item) {
|
||||
web.Fail(c, web.NewBadRequest("更新 Action 定义失败"))
|
||||
return
|
||||
}
|
||||
@@ -185,12 +186,12 @@ func DeleteActionDefinition(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var item model.ActionDefinition
|
||||
if err := store.DB.First(&item, id).Error; err != nil {
|
||||
item, found := actionDefinitionRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("Action 定义不存在"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Delete(&item).Error; err != nil {
|
||||
if !actionDefinitionRepo.Delete(&item) {
|
||||
web.Fail(c, web.NewBadRequest("删除 Action 定义失败"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -9,9 +9,7 @@ import (
|
||||
|
||||
"eai_agentplatform/backend/internal/middleware"
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
specialistmodel "eai_agentplatform/backend/internal/specialists/model"
|
||||
specialistruntime "eai_agentplatform/backend/internal/specialists/runtime"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
)
|
||||
|
||||
@@ -39,16 +37,8 @@ func ListMyTasks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var items []model.TaskRecord
|
||||
if err := specialistruntime.MyTaskQuery(user).
|
||||
// 置顶的排在前面,其余按最近动过的排 —— 置顶只是一个排序偏好。
|
||||
Order("pinned DESC, updated_at DESC, id DESC").
|
||||
Limit(50).
|
||||
Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询我的任务失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
// 置顶的排在前面,其余按最近动过的排 —— 置顶只是一个排序偏好。
|
||||
web.OK(c, taskRecordRepo.ListByOwners(specialistruntime.MyTaskOwners(user), 50))
|
||||
}
|
||||
|
||||
// DeleteMyTask 彻底删掉一条任务。task_record 没有软删字段,删了就是删了 ——
|
||||
@@ -64,12 +54,12 @@ func DeleteMyTask(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var task model.TaskRecord
|
||||
if err := specialistruntime.MyTaskQuery(user).Where("id = ?", id).First(&task).Error; err != nil {
|
||||
task, found := taskRecordRepo.GetByIDForOwners(id, specialistruntime.MyTaskOwners(user))
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("任务不存在"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Delete(&task).Error; err != nil {
|
||||
if !taskRecordRepo.Delete(&task) {
|
||||
web.Fail(c, web.NewBadRequest("删除任务失败"))
|
||||
return
|
||||
}
|
||||
@@ -95,8 +85,8 @@ func CreateMyTask(c *gin.Context) {
|
||||
if specialistKey == "" {
|
||||
specialistKey = generalAssistantKey
|
||||
}
|
||||
var specialist specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", specialistKey).First(&specialist).Error; err != nil {
|
||||
specialist, found := specialistRepo.GetByKey(specialistKey)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("专员不存在"))
|
||||
return
|
||||
}
|
||||
@@ -123,7 +113,7 @@ func CreateMyTask(c *gin.Context) {
|
||||
task.Status = "待处理"
|
||||
}
|
||||
|
||||
if err := store.DB.Create(&task).Error; err != nil {
|
||||
if !taskRecordRepo.Insert(&task) {
|
||||
web.Fail(c, web.NewBadRequest("创建任务失败"))
|
||||
return
|
||||
}
|
||||
@@ -142,8 +132,9 @@ func UpdateMyTask(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var task model.TaskRecord
|
||||
if err := specialistruntime.MyTaskQuery(user).Where("id = ?", id).First(&task).Error; err != nil {
|
||||
owners := specialistruntime.MyTaskOwners(user)
|
||||
task, found := taskRecordRepo.GetByIDForOwners(id, owners)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("任务不存在"))
|
||||
return
|
||||
}
|
||||
@@ -158,8 +149,8 @@ func UpdateMyTask(c *gin.Context) {
|
||||
task.Title = title
|
||||
}
|
||||
if key := strings.TrimSpace(req.SpecialistKey); key != "" && key != task.SpecialistKey {
|
||||
var specialist specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", key).First(&specialist).Error; err != nil {
|
||||
specialist, ok := specialistRepo.GetByKey(key)
|
||||
if !ok {
|
||||
web.Fail(c, web.NewNotFoundError("目标专员不存在"))
|
||||
return
|
||||
}
|
||||
@@ -173,8 +164,8 @@ func UpdateMyTask(c *gin.Context) {
|
||||
if *req.ProjectID == 0 {
|
||||
task.ProjectID = nil
|
||||
} else {
|
||||
var project model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).Where("id = ?", *req.ProjectID).First(&project).Error; err != nil {
|
||||
project, ok := projectRepo.GetByIDForOwners(*req.ProjectID, owners)
|
||||
if !ok {
|
||||
web.Fail(c, web.NewNotFoundError("项目不存在"))
|
||||
return
|
||||
}
|
||||
@@ -185,7 +176,7 @@ func UpdateMyTask(c *gin.Context) {
|
||||
task.Pinned = *req.Pinned
|
||||
}
|
||||
|
||||
if err := store.DB.Save(&task).Error; err != nil {
|
||||
if !taskRecordRepo.Update(&task) {
|
||||
web.Fail(c, web.NewBadRequest("更新任务失败"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -8,10 +8,17 @@ import (
|
||||
|
||||
"eai_agentplatform/backend/internal/middleware"
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/repository"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
)
|
||||
|
||||
// noteRepo 学习笔记仓库(便于测试时覆写),包内共享。
|
||||
var noteRepo repository.StudyNoteRepo
|
||||
|
||||
func init() {
|
||||
noteRepo = repository.StudyNoteRepo{}
|
||||
}
|
||||
|
||||
var validNoteItemTypes = map[string]bool{"company": true, "product": true, "course": true}
|
||||
|
||||
// ListNotes GET /api/notes?item_type=&item_id= —— 我的学习笔记
|
||||
@@ -21,21 +28,16 @@ func ListNotes(c *gin.Context) {
|
||||
web.Fail(c, web.NewAuthError("未登录"))
|
||||
return
|
||||
}
|
||||
q := store.DB.Where("user_id = ?", u.ID)
|
||||
if t := c.Query("item_type"); t != "" {
|
||||
q = q.Where("item_type = ?", t)
|
||||
}
|
||||
// item_id 解析失败(不是数字)就当没传 —— 不给一个错参数回 400,
|
||||
// 笔记列表本来就是按内容可筛可不筛。
|
||||
var itemID *uint
|
||||
if s := c.Query("item_id"); s != "" {
|
||||
if n, err := strconv.ParseUint(s, 10, 64); err == nil {
|
||||
q = q.Where("item_id = ?", n)
|
||||
id := uint(n)
|
||||
itemID = &id
|
||||
}
|
||||
}
|
||||
var items []model.StudyNote
|
||||
if err := q.Order("updated_at DESC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询笔记失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
web.OK(c, noteRepo.ListByUser(u.ID, c.Query("item_type"), itemID))
|
||||
}
|
||||
|
||||
// CreateNote POST /api/notes —— 新增学习笔记
|
||||
@@ -63,7 +65,7 @@ func CreateNote(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
n := model.StudyNote{UserID: u.ID, ItemType: req.ItemType, ItemID: req.ItemID, Content: strings.TrimSpace(req.Content)}
|
||||
if err := store.DB.Create(&n).Error; err != nil {
|
||||
if !noteRepo.Insert(&n) {
|
||||
web.Fail(c, web.NewBadRequest("保存笔记失败"))
|
||||
return
|
||||
}
|
||||
@@ -81,8 +83,8 @@ func UpdateNote(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var n model.StudyNote
|
||||
if err := store.DB.First(&n, id).Error; err != nil {
|
||||
n, found := noteRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("笔记不存在"))
|
||||
return
|
||||
}
|
||||
@@ -102,7 +104,7 @@ func UpdateNote(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
n.Content = strings.TrimSpace(req.Content)
|
||||
if err := store.DB.Save(&n).Error; err != nil {
|
||||
if !noteRepo.Update(&n) {
|
||||
web.Fail(c, web.NewBadRequest("更新笔记失败"))
|
||||
return
|
||||
}
|
||||
@@ -120,8 +122,8 @@ func DeleteNote(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var n model.StudyNote
|
||||
if err := store.DB.First(&n, id).Error; err != nil {
|
||||
n, found := noteRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("笔记不存在"))
|
||||
return
|
||||
}
|
||||
@@ -129,7 +131,7 @@ func DeleteNote(c *gin.Context) {
|
||||
web.Fail(c, web.NewForbiddenError("无权操作他人笔记"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Delete(&n).Error; err != nil {
|
||||
if !noteRepo.Delete(&n) {
|
||||
web.Fail(c, web.NewBadRequest("删除笔记失败"))
|
||||
return
|
||||
}
|
||||
|
||||
@@ -10,9 +10,7 @@ import (
|
||||
|
||||
"eai_agentplatform/backend/internal/middleware"
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
specialistmodel "eai_agentplatform/backend/internal/specialists/model"
|
||||
specialistruntime "eai_agentplatform/backend/internal/specialists/runtime"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
)
|
||||
|
||||
@@ -33,8 +31,6 @@ type projectReq struct {
|
||||
Pinned *bool `json:"pinned"`
|
||||
}
|
||||
|
||||
|
||||
|
||||
// encodeKeys 把 key 列表存成 JSON 字符串。空列表存空串而不是 "[]",
|
||||
// 读的时候一眼能看出「没配」和「配了但为空」的区别不大,但空串更省。
|
||||
func encodeKeys(keys []string) string {
|
||||
@@ -62,15 +58,7 @@ func ListProjects(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var items []model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).
|
||||
Order("pinned DESC, updated_at DESC, id DESC").
|
||||
Limit(50).
|
||||
Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询项目失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
web.OK(c, projectRepo.ListByOwners(specialistruntime.MyTaskOwners(user), 50))
|
||||
}
|
||||
|
||||
// CreateProject 建一个项目。空 body 也收(跟「新建任务」一样,全走默认值),
|
||||
@@ -117,7 +105,7 @@ func CreateProject(c *gin.Context) {
|
||||
SkillKeys: encodeKeys(req.SkillKeys),
|
||||
ConnectorKeys: encodeKeys(req.ConnectorKeys),
|
||||
}
|
||||
if err := store.DB.Create(&project).Error; err != nil {
|
||||
if !projectRepo.Insert(&project) {
|
||||
web.Fail(c, web.NewBadRequest("创建项目失败"))
|
||||
return
|
||||
}
|
||||
@@ -137,8 +125,8 @@ func UpdateProject(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var project model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).Where("id = ?", id).First(&project).Error; err != nil {
|
||||
project, found := projectRepo.GetByIDForOwners(id, specialistruntime.MyTaskOwners(user))
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("项目不存在"))
|
||||
return
|
||||
}
|
||||
@@ -181,7 +169,7 @@ func UpdateProject(c *gin.Context) {
|
||||
project.ConnectorKeys = encodeKeys(req.ConnectorKeys)
|
||||
}
|
||||
|
||||
if err := store.DB.Save(&project).Error; err != nil {
|
||||
if !projectRepo.Update(&project) {
|
||||
web.Fail(c, web.NewBadRequest("更新项目失败"))
|
||||
return
|
||||
}
|
||||
@@ -204,18 +192,16 @@ func DeleteProject(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var project model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).Where("id = ?", id).First(&project).Error; err != nil {
|
||||
project, found := projectRepo.GetByIDForOwners(id, specialistruntime.MyTaskOwners(user))
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("项目不存在"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Model(&model.TaskRecord{}).
|
||||
Where("project_id = ?", project.ID).
|
||||
Update("project_id", nil).Error; err != nil {
|
||||
if !taskRecordRepo.ClearProject(project.ID) {
|
||||
web.Fail(c, web.NewBadRequest("解除任务归属失败"))
|
||||
return
|
||||
}
|
||||
if err := store.DB.Delete(&project).Error; err != nil {
|
||||
if !projectRepo.Delete(&project) {
|
||||
web.Fail(c, web.NewBadRequest("删除项目失败"))
|
||||
return
|
||||
}
|
||||
@@ -235,21 +221,13 @@ func ListProjectTasks(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var project model.Project
|
||||
if err := specialistruntime.ProjectQuery(user).Where("id = ?", id).First(&project).Error; err != nil {
|
||||
project, found := projectRepo.GetByIDForOwners(id, specialistruntime.MyTaskOwners(user))
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("项目不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
var items []model.TaskRecord
|
||||
if err := store.DB.Where("project_id = ?", project.ID).
|
||||
Order("updated_at DESC, id DESC").
|
||||
Limit(100).
|
||||
Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询项目任务失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
web.OK(c, taskRecordRepo.ListByProject(project.ID, 100))
|
||||
}
|
||||
|
||||
// validateSpecialistKeys 专员 key 得真实存在才让存 —— 项目卡片上要显示专员名,
|
||||
@@ -262,8 +240,7 @@ func validateSpecialistKeys(keys []string) error {
|
||||
if trimmed == "" {
|
||||
continue
|
||||
}
|
||||
var specialist specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", trimmed).First(&specialist).Error; err != nil {
|
||||
if _, found := specialistRepo.GetByKey(trimmed); !found {
|
||||
return errors.New("专员不存在:" + trimmed)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,10 +4,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
skillcore "eai_agentplatform/backend/internal/skills/core"
|
||||
specialistmodel "eai_agentplatform/backend/internal/specialists/model"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
)
|
||||
|
||||
// 本文件回答一个问题:一次对话/一次动作,到底该以哪个专员的身份说话。
|
||||
@@ -25,12 +23,10 @@ func loadSpecialistByKey(key string) *specialistmodel.Specialist {
|
||||
if key == "" {
|
||||
return nil
|
||||
}
|
||||
var s specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", key).First(&s).Error; err != nil {
|
||||
return nil
|
||||
}
|
||||
// state = inactive 的专员不再接新会话,但 system(通用助手)要能查到。
|
||||
if s.State == "inactive" {
|
||||
// state = inactive 的专员不再接新会话,但 system(通用助手)要能查到 ——
|
||||
// 「要不要拒绝 inactive」这个口径只在这里,不去污染仓库的取数口径。
|
||||
s, found := specialistRepo.GetByKey(key)
|
||||
if !found || s.State == "inactive" {
|
||||
return nil
|
||||
}
|
||||
return &s
|
||||
@@ -48,8 +44,7 @@ func resolveSpecialist(req ChatMessageRequest) *specialistmodel.Specialist {
|
||||
if req.TaskID > 0 {
|
||||
// 与 task_runtime.go 其余读路径一致,不按 created_by 收窄:
|
||||
// 专员目录本身就是所有登录用户可读的,这里不构成新的信息暴露。
|
||||
var task model.TaskRecord
|
||||
if err := store.DB.First(&task, req.TaskID).Error; err == nil {
|
||||
if task, found := taskRecordRepo.GetByID(req.TaskID); found {
|
||||
if s := loadSpecialistByKey(task.SpecialistKey); s != nil {
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"gorm.io/gorm/logger"
|
||||
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
"eai_agentplatform/backend/internal/repository"
|
||||
specialistmodel "eai_agentplatform/backend/internal/specialists/model"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
)
|
||||
@@ -124,6 +125,12 @@ func setupAPITestDB(t *testing.T) {
|
||||
store.DB = db
|
||||
t.Cleanup(func() { store.DB = prev })
|
||||
|
||||
// 取数现在走仓库层,零值仓库(repository.XxxRepo{})的 base() 会回落到
|
||||
// 包级 repository.DB —— 这根线不接,第一个查询就是 nil 解引用。
|
||||
prevRepo := repository.DB
|
||||
repository.SetDB(db)
|
||||
t.Cleanup(func() { repository.SetDB(prevRepo) })
|
||||
|
||||
for _, s := range []specialistmodel.Specialist{
|
||||
{Key: "contract-review", Label: "合同审查专员", State: "active", Tier: "industry", ObjectEntryRoute: "/apps/contract-review"},
|
||||
{Key: "report-generation", Label: "报告生成专员", State: "active", Tier: "generic", ObjectEntryRoute: "/apps/report-generation"},
|
||||
|
||||
@@ -7,30 +7,43 @@ import (
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"eai_agentplatform/backend/internal/middleware"
|
||||
"eai_agentplatform/backend/internal/model"
|
||||
specialistmodel "eai_agentplatform/backend/internal/specialists/model"
|
||||
"eai_agentplatform/backend/internal/repository"
|
||||
wechatofficialaccountapi "eai_agentplatform/backend/internal/specialists/packages/wechat_official_account/api"
|
||||
specialistruntime "eai_agentplatform/backend/internal/specialists/runtime"
|
||||
"eai_agentplatform/backend/internal/store"
|
||||
"eai_agentplatform/backend/internal/web"
|
||||
)
|
||||
|
||||
// 任务域仓库,包内共享。
|
||||
//
|
||||
// specialistRepo 还被 my_task.go、project.go、specialist_prompt.go 共用,
|
||||
// projectRepo 还被 my_task.go 共用 —— 同一个仓库只声明一处:
|
||||
// 两份变量持有同一个仓库时,测试里覆写一份、另一份照旧,行为会静默分叉。
|
||||
var (
|
||||
specialistRepo repository.SpecialistRepo
|
||||
taskRecordRepo repository.TaskRecordRepo
|
||||
taskRunRepo repository.TaskRunRepo
|
||||
taskArtifactRepo repository.TaskArtifactRepo
|
||||
projectRepo repository.ProjectRepo
|
||||
)
|
||||
|
||||
func init() {
|
||||
specialistRepo = repository.SpecialistRepo{}
|
||||
taskRecordRepo = repository.TaskRecordRepo{}
|
||||
taskRunRepo = repository.TaskRunRepo{}
|
||||
taskArtifactRepo = repository.TaskArtifactRepo{}
|
||||
projectRepo = repository.ProjectRepo{}
|
||||
}
|
||||
|
||||
func ListTasks(c *gin.Context) {
|
||||
specialistKey := strings.TrimSpace(c.Query("specialist_key"))
|
||||
if specialistKey == "" {
|
||||
web.Fail(c, web.NewBadRequest("specialist_key 不能为空"))
|
||||
return
|
||||
}
|
||||
|
||||
var items []model.TaskRecord
|
||||
if err := store.DB.Where("specialist_key = ?", specialistKey).Order("updated_at DESC, id DESC").Find(&items).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询事项失败"))
|
||||
return
|
||||
}
|
||||
web.OK(c, items)
|
||||
web.OK(c, taskRecordRepo.ListBySpecialistKey(specialistKey))
|
||||
}
|
||||
|
||||
func GetTaskDetail(c *gin.Context) {
|
||||
@@ -39,23 +52,14 @@ func GetTaskDetail(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var task model.TaskRecord
|
||||
if err := store.DB.First(&task, id).Error; err != nil {
|
||||
task, found := taskRecordRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("事项不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
var artifacts []model.TaskArtifact
|
||||
if err := store.DB.Where("task_id = ?", id).Order("created_at DESC, id DESC").Find(&artifacts).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询交付物失败"))
|
||||
return
|
||||
}
|
||||
|
||||
var runs []model.TaskRun
|
||||
if err := store.DB.Where("task_id = ?", id).Order("started_at DESC, id DESC").Find(&runs).Error; err != nil {
|
||||
web.Fail(c, web.NewBadRequest("查询运行记录失败"))
|
||||
return
|
||||
}
|
||||
artifacts := taskArtifactRepo.ListByTask(id)
|
||||
runs := taskRunRepo.ListByTask(id)
|
||||
if task.SpecialistKey == wechatofficialaccountapi.OfficialAccountSpecialistKey {
|
||||
artifacts = wechatofficialaccountapi.CompactOfficialAccountArtifactsForResponse(artifacts)
|
||||
runs = wechatofficialaccountapi.CompactOfficialAccountRunsForResponse(runs)
|
||||
@@ -75,22 +79,21 @@ func GetArtifactDetail(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var artifact model.TaskArtifact
|
||||
if err := store.DB.First(&artifact, id).Error; err != nil {
|
||||
artifact, found := taskArtifactRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("交付物不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
var task model.TaskRecord
|
||||
if err := store.DB.First(&task, artifact.TaskID).Error; err != nil {
|
||||
task, found := taskRecordRepo.GetByID(artifact.TaskID)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("事项不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
var run model.TaskRun
|
||||
var runData any
|
||||
if artifact.CreatedByRunID != nil {
|
||||
if err := store.DB.First(&run, *artifact.CreatedByRunID).Error; err == nil {
|
||||
if run, ok := taskRunRepo.GetByID(*artifact.CreatedByRunID); ok {
|
||||
runData = run
|
||||
}
|
||||
}
|
||||
@@ -113,8 +116,8 @@ func CreateTask(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var specialist specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", strings.TrimSpace(req.SpecialistKey)).First(&specialist).Error; err != nil {
|
||||
specialist, found := specialistRepo.GetByKey(strings.TrimSpace(req.SpecialistKey))
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("专员不存在"))
|
||||
return
|
||||
}
|
||||
@@ -132,7 +135,7 @@ func CreateTask(c *gin.Context) {
|
||||
task.Status = "待处理"
|
||||
}
|
||||
|
||||
if err := store.DB.Create(&task).Error; err != nil {
|
||||
if !taskRecordRepo.Insert(&task) {
|
||||
web.Fail(c, web.NewBadRequest("创建事项失败"))
|
||||
return
|
||||
}
|
||||
@@ -145,8 +148,8 @@ func UpdateTask(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var task model.TaskRecord
|
||||
if err := store.DB.First(&task, id).Error; err != nil {
|
||||
task, found := taskRecordRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("事项不存在"))
|
||||
return
|
||||
}
|
||||
@@ -163,9 +166,9 @@ func UpdateTask(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if strings.TrimSpace(req.SpecialistKey) != "" && strings.TrimSpace(req.SpecialistKey) != task.SpecialistKey {
|
||||
var specialist specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", strings.TrimSpace(req.SpecialistKey)).First(&specialist).Error; err != nil {
|
||||
if key := strings.TrimSpace(req.SpecialistKey); key != "" && key != task.SpecialistKey {
|
||||
specialist, ok := specialistRepo.GetByKey(key)
|
||||
if !ok {
|
||||
web.Fail(c, web.NewNotFoundError("目标专员不存在"))
|
||||
return
|
||||
}
|
||||
@@ -178,7 +181,7 @@ func UpdateTask(c *gin.Context) {
|
||||
task.Status = specialistruntime.NormalizeTaskStatus(updated.Status)
|
||||
task.ContextJSON = updated.ContextJSON
|
||||
task.DueAt = updated.DueAt
|
||||
if err := store.DB.Save(&task).Error; err != nil {
|
||||
if !taskRecordRepo.Update(&task) {
|
||||
web.Fail(c, web.NewBadRequest("更新事项失败"))
|
||||
return
|
||||
}
|
||||
@@ -191,8 +194,8 @@ func UpdateTaskStatus(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var task model.TaskRecord
|
||||
if err := store.DB.First(&task, id).Error; err != nil {
|
||||
task, found := taskRecordRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("事项不存在"))
|
||||
return
|
||||
}
|
||||
@@ -208,7 +211,7 @@ func UpdateTaskStatus(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
task.Status = status
|
||||
if err := store.DB.Save(&task).Error; err != nil {
|
||||
if !taskRecordRepo.Update(&task) {
|
||||
web.Fail(c, web.NewBadRequest("更新事项状态失败"))
|
||||
return
|
||||
}
|
||||
@@ -221,24 +224,14 @@ func DeleteTask(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var task model.TaskRecord
|
||||
if err := store.DB.First(&task, id).Error; err != nil {
|
||||
task, found := taskRecordRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("事项不存在"))
|
||||
return
|
||||
}
|
||||
|
||||
if err := store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if err := tx.Where("task_id = ?", task.ID).Delete(&model.TaskArtifact{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Where("task_id = ?", task.ID).Delete(&model.TaskRun{}).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tx.Delete(&task).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}); err != nil {
|
||||
// 交付物与运行记录跟着任务一起走,三张表在同一个事务里。
|
||||
if !taskRecordRepo.DeleteCascade(task.ID) {
|
||||
web.Fail(c, web.NewBadRequest("删除事项失败"))
|
||||
return
|
||||
}
|
||||
@@ -252,13 +245,13 @@ func UpdateArtifactStatus(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var artifact model.TaskArtifact
|
||||
if err := store.DB.First(&artifact, id).Error; err != nil {
|
||||
artifact, found := taskArtifactRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("交付物不存在"))
|
||||
return
|
||||
}
|
||||
var task model.TaskRecord
|
||||
if err := store.DB.First(&task, artifact.TaskID).Error; err != nil {
|
||||
task, found := taskRecordRepo.GetByID(artifact.TaskID)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("事项不存在"))
|
||||
return
|
||||
}
|
||||
@@ -274,14 +267,14 @@ func UpdateArtifactStatus(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
artifact.Status = nextStatus
|
||||
if err := store.DB.Save(&artifact).Error; err != nil {
|
||||
if !taskArtifactRepo.Update(&artifact) {
|
||||
web.Fail(c, web.NewBadRequest("更新交付物状态失败"))
|
||||
return
|
||||
}
|
||||
|
||||
task.Status = specialistruntime.TaskStatusFromArtifactStatus(nextStatus)
|
||||
task.CurrentResult = specialistruntime.BuildArtifactStatusSummary(artifact, strings.TrimSpace(req.Remark))
|
||||
if err := store.DB.Save(&task).Error; err != nil {
|
||||
if !taskRecordRepo.Update(&task) {
|
||||
web.Fail(c, web.NewBadRequest("更新事项状态失败"))
|
||||
return
|
||||
}
|
||||
@@ -298,13 +291,13 @@ func ExecuteTaskAction(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var task model.TaskRecord
|
||||
if err := store.DB.First(&task, id).Error; err != nil {
|
||||
task, found := taskRecordRepo.GetByID(id)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("事项不存在"))
|
||||
return
|
||||
}
|
||||
var specialist specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", task.SpecialistKey).First(&specialist).Error; err != nil {
|
||||
specialist, found := specialistRepo.GetByKey(task.SpecialistKey)
|
||||
if !found {
|
||||
web.Fail(c, web.NewNotFoundError("专员不存在"))
|
||||
return
|
||||
}
|
||||
@@ -350,7 +343,7 @@ func ExecuteTaskAction(c *gin.Context) {
|
||||
StartedAt: now,
|
||||
FinishedAt: &now,
|
||||
}
|
||||
if err := store.DB.Create(&run).Error; err != nil {
|
||||
if !taskRunRepo.Insert(&run) {
|
||||
web.Fail(c, web.NewBadRequest("执行动作失败"))
|
||||
return
|
||||
}
|
||||
@@ -369,7 +362,7 @@ func ExecuteTaskAction(c *gin.Context) {
|
||||
SourceRefsJSON: string(sourceRefsJSON),
|
||||
CreatedByRunID: &run.ID,
|
||||
}
|
||||
if err := store.DB.Create(artifact).Error; err != nil {
|
||||
if !taskArtifactRepo.Insert(artifact) {
|
||||
web.Fail(c, web.NewBadRequest("保存交付物失败"))
|
||||
return
|
||||
}
|
||||
@@ -379,7 +372,7 @@ func ExecuteTaskAction(c *gin.Context) {
|
||||
task.CurrentRunID = &run.ID
|
||||
task.CurrentResult = runOutput.Summary
|
||||
task.LastTriggeredAt = &now
|
||||
if err := store.DB.Save(&task).Error; err != nil {
|
||||
if !taskRecordRepo.Update(&task) {
|
||||
web.Fail(c, web.NewBadRequest("更新事项状态失败"))
|
||||
return
|
||||
}
|
||||
@@ -391,18 +384,17 @@ func ExecuteTaskAction(c *gin.Context) {
|
||||
})
|
||||
}
|
||||
|
||||
// ensureBootstrapTask 某专员名下一条任务都没有时,铺一条默认任务。
|
||||
//
|
||||
// 注意:目前全仓没有调用方(保留原样迁到仓库层,未删)。
|
||||
func ensureBootstrapTask(specialistKey string, user *model.User) error {
|
||||
var count int64
|
||||
if err := store.DB.Model(&model.TaskRecord{}).Where("specialist_key = ?", specialistKey).Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
if count > 0 {
|
||||
if taskRecordRepo.CountBySpecialistKey(specialistKey) > 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
var specialist specialistmodel.Specialist
|
||||
if err := store.DB.Where("key = ?", specialistKey).First(&specialist).Error; err != nil {
|
||||
return err
|
||||
specialist, found := specialistRepo.GetByKey(specialistKey)
|
||||
if !found {
|
||||
return fmt.Errorf("专员不存在:%s", specialistKey)
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
@@ -427,7 +419,10 @@ func ensureBootstrapTask(specialistKey string, user *model.User) error {
|
||||
if user != nil {
|
||||
task.CreatedBy = &user.ID
|
||||
}
|
||||
return store.DB.Create(&task).Error
|
||||
if !taskRecordRepo.Insert(&task) {
|
||||
return fmt.Errorf("创建铺底任务失败")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildBottomPanel(runs []model.TaskRun) gin.H {
|
||||
|
||||
Reference in New Issue
Block a user