refactor: remove agent ID references from AI workflow handling and related components

This commit is contained in:
mlogclub
2026-07-25 15:32:55 +08:00
parent 7b86fd1f09
commit 1663db7f24
14 changed files with 13 additions and 302 deletions
-183
View File
@@ -20,7 +20,6 @@ import (
"agent-desk/internal/repositories"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
var AIWorkflowService = newAIWorkflowService()
@@ -150,49 +149,6 @@ func appendNonZeroInt64(list []int64, value int64) []int64 {
return append(list, value)
}
func (s *aiWorkflowService) GetByAgentID(agentID int64) *models.AIWorkflow {
if agentID <= 0 {
return nil
}
return repositories.AIWorkflowRepository.Take(sqls.DB(), "agent_id = ? AND status <> ?", agentID, enums.StatusDeleted)
}
func (s *aiWorkflowService) GetOrCreateAgentWorkflow(agentID int64, operator *dto.AuthPrincipal) (*models.AIWorkflow, error) {
if operator == nil {
return nil, errorsx.UnauthorizedI18n("error.auth.expired")
}
if agentID <= 0 {
return nil, errorsx.InvalidParam("agent id is required")
}
if agent := AIAgentService.Get(agentID); agent == nil || agent.Status == enums.StatusDeleted {
return nil, errorsx.InvalidParamI18n("error.e0002")
}
if item := s.GetByAgentID(agentID); item != nil {
return item, nil
}
var item *models.AIWorkflow
err := sqls.WithTransaction(func(ctx *sqls.TxContext) error {
if current := repositories.AIWorkflowRepository.Take(ctx.Tx, "agent_id = ? AND status <> ?", agentID, enums.StatusDeleted); current != nil {
item = current
return nil
}
agent := repositories.AIAgentRepository.Get(ctx.Tx, agentID)
if agent == nil || agent.Status == enums.StatusDeleted {
return errorsx.InvalidParamI18n("error.e0002")
}
created, err := s.createDefaultAgentWorkflow(ctx.Tx, agent, operator)
if err != nil {
return err
}
item = created
return nil
})
if err != nil {
return nil, err
}
return item, nil
}
func (s *aiWorkflowService) ListNodeSpecs() []workflowregistry.NodeSpec {
return s.registry.List()
}
@@ -264,51 +220,6 @@ func (s *aiWorkflowService) CreateWorkflow(req request.CreateAIWorkflowRequest,
return item, nil
}
func (s *aiWorkflowService) SaveAgentWorkflow(req request.SaveAIWorkflowRequest, operator *dto.AuthPrincipal) (*models.AIWorkflow, error) {
if operator == nil {
return nil, errorsx.UnauthorizedI18n("error.auth.expired")
}
agent := AIAgentService.Get(req.AgentID)
if agent == nil || agent.Status == enums.StatusDeleted {
return nil, errorsx.InvalidParamI18n("error.e0002")
}
name := strings.TrimSpace(req.Name)
if name == "" {
name = defaultAgentWorkflowName(agent.Name)
}
definition, err := marshalDefinition(req.Definition)
if err != nil {
return nil, err
}
current := s.GetByAgentID(req.AgentID)
if current == nil {
item := &models.AIWorkflow{
Name: name,
Description: strings.TrimSpace(req.Description),
AgentID: req.AgentID,
Status: enums.StatusOk,
DraftDefinition: definition,
AuditFields: utils.BuildAuditFields(operator),
}
if err := repositories.AIWorkflowRepository.Create(sqls.DB(), item); err != nil {
return nil, err
}
return item, nil
}
if err := repositories.AIWorkflowRepository.Updates(sqls.DB(), current.ID, map[string]interface{}{
"name": name,
"description": strings.TrimSpace(req.Description),
"agent_id": req.AgentID,
"draft_definition": definition,
"update_user_id": operator.UserID,
"update_user_name": operator.Username,
"updated_at": time.Now(),
}); err != nil {
return nil, err
}
return s.Get(current.ID), nil
}
func (s *aiWorkflowService) UpdateWorkflow(req request.UpdateAIWorkflowRequest, operator *dto.AuthPrincipal) error {
if operator == nil {
return errorsx.UnauthorizedI18n("error.auth.expired")
@@ -353,9 +264,6 @@ func (s *aiWorkflowService) DeleteWorkflow(id int64, operator *dto.AuthPrincipal
}
func (s *aiWorkflowService) PublishWorkflow(req request.PublishAIWorkflowRequest, operator *dto.AuthPrincipal) (*models.AIWorkflowVersion, error) {
if req.AgentID > 0 {
return s.PublishAgentWorkflow(req, operator)
}
if operator == nil {
return nil, errorsx.UnauthorizedI18n("error.auth.expired")
}
@@ -403,97 +311,6 @@ func (s *aiWorkflowService) PublishWorkflow(req request.PublishAIWorkflowRequest
return version, nil
}
func (s *aiWorkflowService) PublishAgentWorkflow(req request.PublishAIWorkflowRequest, operator *dto.AuthPrincipal) (*models.AIWorkflowVersion, error) {
if operator == nil {
return nil, errorsx.UnauthorizedI18n("error.auth.expired")
}
workflow, err := s.GetOrCreateAgentWorkflow(req.AgentID, operator)
if err != nil {
return nil, err
}
req.WorkflowID = workflow.ID
result := s.ValidateDefinition(req.Definition)
if !result.Valid {
return nil, errorsx.InvalidParam("workflow definition is invalid")
}
definition, err := marshalDefinition(req.Definition)
if err != nil {
return nil, err
}
now := time.Now()
var version *models.AIWorkflowVersion
err = sqls.WithTransaction(func(ctx *sqls.TxContext) error {
current := repositories.AIWorkflowRepository.Get(ctx.Tx, workflow.ID)
if current == nil || current.AgentID != req.AgentID || current.Status == enums.StatusDeleted {
return errorsx.InvalidParamI18n("error.e0002")
}
nextVersion := repositories.AIWorkflowVersionRepository.MaxVersionByWorkflowID(ctx.Tx, current.ID) + 1
version = &models.AIWorkflowVersion{
WorkflowID: current.ID,
Version: nextVersion,
Status: enums.StatusOk,
Definition: definition,
DefinitionHash: hashDefinition(definition),
PublishedAt: &now,
PublishedByID: operator.UserID,
PublishedByName: operator.Username,
AuditFields: utils.BuildAuditFields(operator),
}
if err := repositories.AIWorkflowVersionRepository.Create(ctx.Tx, version); err != nil {
return err
}
if err := repositories.AIWorkflowRepository.Updates(ctx.Tx, current.ID, map[string]interface{}{
"draft_definition": definition,
"published_version_id": version.ID,
"update_user_id": operator.UserID,
"update_user_name": operator.Username,
"updated_at": now,
}); err != nil {
return err
}
agent := repositories.AIAgentRepository.Get(ctx.Tx, req.AgentID)
if agent == nil {
return errorsx.InvalidParamI18n("error.e0002")
}
if err := AIAgentService.validatePublishableAgent(ctx.Tx, agent); err != nil {
return err
}
revision, err := AgentRevisionService.PublishWorkflowSnapshot(ctx.Tx, agent, version, operator)
if err != nil {
return err
}
return repositories.AIAgentRepository.Updates(ctx.Tx, req.AgentID, map[string]any{
"workflow_version_id": version.ID,
"published_revision_id": revision.ID,
"update_user_id": operator.UserID,
"update_user_name": operator.Username,
"updated_at": now,
})
})
if err != nil {
return nil, err
}
return version, nil
}
func (s *aiWorkflowService) createDefaultAgentWorkflow(db *gorm.DB, agent *models.AIAgent, operator *dto.AuthPrincipal) (*models.AIWorkflow, error) {
definition, err := marshalDefinition(defaultAgentWorkflowDefinition())
if err != nil {
return nil, err
}
item := &models.AIWorkflow{
Name: defaultAgentWorkflowName(agent.Name),
AgentID: agent.ID,
Status: enums.StatusOk,
DraftDefinition: definition,
AuditFields: utils.BuildAuditFields(operator),
}
if err := repositories.AIWorkflowRepository.Create(db, item); err != nil {
return nil, err
}
return item, nil
}
func defaultAgentWorkflowDefinition() dsl.Definition {
return dsl.Definition{
SchemaVersion: dsl.SchemaVersion,