refactor: remove agent ID references from AI workflow handling and related components
This commit is contained in:
@@ -214,10 +214,6 @@ func registerDashboardAgentTeamScheduleRoutes(group *gin.RouterGroup) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func registerDashboardAIAgentRoutes(group *gin.RouterGroup) {
|
func registerDashboardAIAgentRoutes(group *gin.RouterGroup) {
|
||||||
group.GET("/:id/workflow", dashboard.AIWorkflowGetByAgent)
|
|
||||||
group.POST("/workflow/save", dashboard.AIWorkflowPostSaveAgent)
|
|
||||||
group.POST("/workflow/validate", dashboard.AIWorkflowPostValidate)
|
|
||||||
group.POST("/workflow/publish", dashboard.AIWorkflowPostPublishAgent)
|
|
||||||
group.POST("/publish", dashboard.AIAgentPostPublish)
|
group.POST("/publish", dashboard.AIAgentPostPublish)
|
||||||
group.POST("/rollback", dashboard.AIAgentPostRollback)
|
group.POST("/rollback", dashboard.AIAgentPostRollback)
|
||||||
group.POST("/rollback_rollout", dashboard.AIAgentPostRollback_rollout)
|
group.POST("/rollback_rollout", dashboard.AIAgentPostRollback_rollout)
|
||||||
|
|||||||
@@ -19,7 +19,6 @@ func BuildAIWorkflow(item *models.AIWorkflow) response.AIWorkflowResponse {
|
|||||||
ID: item.ID,
|
ID: item.ID,
|
||||||
Name: item.Name,
|
Name: item.Name,
|
||||||
Description: item.Description,
|
Description: item.Description,
|
||||||
AgentID: item.AgentID,
|
|
||||||
Status: item.Status,
|
Status: item.Status,
|
||||||
DraftDefinition: parseWorkflowDefinition(item.DraftDefinition),
|
DraftDefinition: parseWorkflowDefinition(item.DraftDefinition),
|
||||||
PublishedVersionID: item.PublishedVersionID,
|
PublishedVersionID: item.PublishedVersionID,
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ func AIWorkflowAnyList(ctx *gin.Context) {
|
|||||||
cnd := params.NewPagedSqlCnd(ctx,
|
cnd := params.NewPagedSqlCnd(ctx,
|
||||||
params.QueryFilter{ParamName: "status"},
|
params.QueryFilter{ParamName: "status"},
|
||||||
params.QueryFilter{ParamName: "name", Op: params.Like},
|
params.QueryFilter{ParamName: "name", Op: params.Like},
|
||||||
params.QueryFilter{ParamName: "agentId"},
|
|
||||||
).Desc("id")
|
).Desc("id")
|
||||||
list, paging := services.AIWorkflowService.FindPageByCnd(cnd)
|
list, paging := services.AIWorkflowService.FindPageByCnd(cnd)
|
||||||
httpx.WriteJSON(ctx, &web.PageResult{Results: builders.BuildAIWorkflowList(list), Page: paging})
|
httpx.WriteJSON(ctx, &web.PageResult{Results: builders.BuildAIWorkflowList(list), Page: paging})
|
||||||
@@ -160,62 +159,6 @@ func AIWorkflowPostPublish(ctx *gin.Context) {
|
|||||||
httpx.WriteJSON(ctx, builders.BuildAIWorkflowVersion(item))
|
httpx.WriteJSON(ctx, builders.BuildAIWorkflowVersion(item))
|
||||||
}
|
}
|
||||||
|
|
||||||
func AIWorkflowGetByAgent(ctx *gin.Context) {
|
|
||||||
agentID, ok := httpx.GetPathInt64(ctx, "id")
|
|
||||||
if !ok {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentView)
|
|
||||||
if err != nil {
|
|
||||||
httpx.WriteJSON(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
item, err := services.AIWorkflowService.GetOrCreateAgentWorkflow(agentID, operator)
|
|
||||||
if err != nil {
|
|
||||||
httpx.WriteJSON(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
httpx.WriteJSON(ctx, builders.BuildAIWorkflow(item))
|
|
||||||
}
|
|
||||||
|
|
||||||
func AIWorkflowPostSaveAgent(ctx *gin.Context) {
|
|
||||||
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentUpdate)
|
|
||||||
if err != nil {
|
|
||||||
httpx.WriteJSON(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req := request.SaveAIWorkflowRequest{}
|
|
||||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
||||||
httpx.WriteJSON(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
item, err := services.AIWorkflowService.SaveAgentWorkflow(req, operator)
|
|
||||||
if err != nil {
|
|
||||||
httpx.WriteJSON(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
httpx.WriteJSON(ctx, builders.BuildAIWorkflow(item))
|
|
||||||
}
|
|
||||||
|
|
||||||
func AIWorkflowPostPublishAgent(ctx *gin.Context) {
|
|
||||||
operator, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentUpdate)
|
|
||||||
if err != nil {
|
|
||||||
httpx.WriteJSON(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
req := request.PublishAIWorkflowRequest{}
|
|
||||||
if err := params.ReadJSON(ctx, &req); err != nil {
|
|
||||||
httpx.WriteJSON(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
item, err := services.AIWorkflowService.PublishAgentWorkflow(req, operator)
|
|
||||||
if err != nil {
|
|
||||||
httpx.WriteJSON(ctx, err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
httpx.WriteJSON(ctx, builders.BuildAIWorkflowVersion(item))
|
|
||||||
}
|
|
||||||
|
|
||||||
func AIWorkflowAnyVersionList(ctx *gin.Context) {
|
func AIWorkflowAnyVersionList(ctx *gin.Context) {
|
||||||
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentView); err != nil {
|
if _, err := services.AuthService.RequirePermission(ctx, constants.PermissionAIAgentView); err != nil {
|
||||||
httpx.WriteJSON(ctx, err)
|
httpx.WriteJSON(ctx, err)
|
||||||
|
|||||||
@@ -553,7 +553,7 @@ type AIAgent struct {
|
|||||||
KnowledgeIDs string `gorm:"type:varchar(500);not null;default:''"` // KnowledgeIDs 为绑定的知识库ID列表,按顺序表示优先级。
|
KnowledgeIDs string `gorm:"type:varchar(500);not null;default:''"` // KnowledgeIDs 为绑定的知识库ID列表,按顺序表示优先级。
|
||||||
SkillIDs string `gorm:"type:varchar(500);not null;default:''"` // SkillIDs 为绑定的技能ID列表,按顺序表示允许路由的范围。
|
SkillIDs string `gorm:"type:varchar(500);not null;default:''"` // SkillIDs 为绑定的技能ID列表,按顺序表示允许路由的范围。
|
||||||
AllowedMCPTools string `gorm:"type:text"` // AllowedMCPTools 为允许 direct tool 路由的 MCP 工具白名单配置JSON。
|
AllowedMCPTools string `gorm:"type:text"` // AllowedMCPTools 为允许 direct tool 路由的 MCP 工具白名单配置JSON。
|
||||||
WorkflowVersionID int64 `gorm:"type:bigint;not null;default:0;index"` // WorkflowVersionID 为绑定的已发布会话流程版本ID。
|
WorkflowVersionID int64 `gorm:"type:bigint;not null;default:0;index"` // WorkflowVersionID is retained as the single-workflow runtime pointer.
|
||||||
PublishedRevisionID int64 `gorm:"type:bigint;not null;default:0;index"` // PublishedRevisionID 为当前已发布 Agent 配置快照ID。
|
PublishedRevisionID int64 `gorm:"type:bigint;not null;default:0;index"` // PublishedRevisionID 为当前已发布 Agent 配置快照ID。
|
||||||
SortNo int `gorm:"type:int;not null;default:0;index"` // SortNo 为后台展示排序号。
|
SortNo int `gorm:"type:int;not null;default:0;index"` // SortNo 为后台展示排序号。
|
||||||
AuditFields
|
AuditFields
|
||||||
@@ -643,7 +643,6 @@ type AIWorkflow struct {
|
|||||||
ID int64 `gorm:"primaryKey;autoIncrement"`
|
ID int64 `gorm:"primaryKey;autoIncrement"`
|
||||||
Name string `gorm:"type:varchar(100);not null;default:'';index"`
|
Name string `gorm:"type:varchar(100);not null;default:'';index"`
|
||||||
Description string `gorm:"type:text"`
|
Description string `gorm:"type:text"`
|
||||||
AgentID int64 `gorm:"type:bigint;not null;default:0;index"`
|
|
||||||
Status enums.Status `gorm:"type:int;not null;default:0;index"`
|
Status enums.Status `gorm:"type:int;not null;default:0;index"`
|
||||||
DraftDefinition string `gorm:"type:longtext"`
|
DraftDefinition string `gorm:"type:longtext"`
|
||||||
PublishedVersionID int64 `gorm:"type:bigint;not null;default:0;index"`
|
PublishedVersionID int64 `gorm:"type:bigint;not null;default:0;index"`
|
||||||
|
|||||||
@@ -5,12 +5,9 @@ import "agent-desk/internal/ai/workflow/dsl"
|
|||||||
type CreateAIWorkflowRequest struct {
|
type CreateAIWorkflowRequest struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
AgentID int64 `json:"agentId"`
|
|
||||||
Definition dsl.Definition `json:"definition"`
|
Definition dsl.Definition `json:"definition"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type SaveAIWorkflowRequest = CreateAIWorkflowRequest
|
|
||||||
|
|
||||||
type UpdateAIWorkflowRequest struct {
|
type UpdateAIWorkflowRequest struct {
|
||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
CreateAIWorkflowRequest
|
CreateAIWorkflowRequest
|
||||||
@@ -26,7 +23,6 @@ type ValidateAIWorkflowRequest struct {
|
|||||||
|
|
||||||
type PublishAIWorkflowRequest struct {
|
type PublishAIWorkflowRequest struct {
|
||||||
WorkflowID int64 `json:"workflowId"`
|
WorkflowID int64 `json:"workflowId"`
|
||||||
AgentID int64 `json:"agentId"`
|
|
||||||
Definition dsl.Definition `json:"definition"`
|
Definition dsl.Definition `json:"definition"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ type AIWorkflowResponse struct {
|
|||||||
ID int64 `json:"id"`
|
ID int64 `json:"id"`
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
Description string `json:"description"`
|
Description string `json:"description"`
|
||||||
AgentID int64 `json:"agentId"`
|
|
||||||
Status enums.Status `json:"status"`
|
Status enums.Status `json:"status"`
|
||||||
DraftDefinition dsl.Definition `json:"draftDefinition"`
|
DraftDefinition dsl.Definition `json:"draftDefinition"`
|
||||||
PublishedVersionID int64 `json:"publishedVersionId"`
|
PublishedVersionID int64 `json:"publishedVersionId"`
|
||||||
|
|||||||
@@ -40,8 +40,6 @@ func (s *agentRevisionService) FindByAgentID(agentID int64) []models.AgentRevisi
|
|||||||
type agentRevisionDefinition struct {
|
type agentRevisionDefinition struct {
|
||||||
Agent agentRevisionAgent `json:"agent"`
|
Agent agentRevisionAgent `json:"agent"`
|
||||||
Model agentRevisionModel `json:"model"`
|
Model agentRevisionModel `json:"model"`
|
||||||
WorkflowVersionID int64 `json:"workflowVersionId"`
|
|
||||||
WorkflowDefinition string `json:"workflowDefinition"`
|
|
||||||
WorkflowBindings []agentRevisionWorkflowBinding `json:"workflowBindings"`
|
WorkflowBindings []agentRevisionWorkflowBinding `json:"workflowBindings"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -123,9 +121,6 @@ func (s *agentRevisionService) ResolvePublishedSnapshot(agent models.AIAgent, co
|
|||||||
}
|
}
|
||||||
applyRevisionAgentSnapshot(&snapshot.Agent, definition.Agent)
|
applyRevisionAgentSnapshot(&snapshot.Agent, definition.Agent)
|
||||||
snapshot.WorkflowBindings = append([]agentRevisionWorkflowBinding(nil), definition.WorkflowBindings...)
|
snapshot.WorkflowBindings = append([]agentRevisionWorkflowBinding(nil), definition.WorkflowBindings...)
|
||||||
if definition.WorkflowVersionID > 0 {
|
|
||||||
snapshot.Agent.WorkflowVersionID = definition.WorkflowVersionID
|
|
||||||
}
|
|
||||||
applyRevisionModelSnapshot(&snapshot.AIConfig, definition.Model)
|
applyRevisionModelSnapshot(&snapshot.AIConfig, definition.Model)
|
||||||
return snapshot, nil
|
return snapshot, nil
|
||||||
}
|
}
|
||||||
@@ -169,17 +164,11 @@ func applyRevisionModelSnapshot(config *models.AIConfig, definition agentRevisio
|
|||||||
config.MaxRetryCount = definition.MaxRetryCount
|
config.MaxRetryCount = definition.MaxRetryCount
|
||||||
}
|
}
|
||||||
|
|
||||||
// PublishWorkflowSnapshot keeps the Agent settings and its referenced
|
|
||||||
// workflow definition together as an immutable, reproducible revision.
|
|
||||||
func (s *agentRevisionService) PublishWorkflowSnapshot(db *gorm.DB, agent *models.AIAgent, version *models.AIWorkflowVersion, operator *dto.AuthPrincipal) (*models.AgentRevision, error) {
|
|
||||||
return s.publishSnapshot(db, agent, version, operator)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (s *agentRevisionService) PublishSnapshot(db *gorm.DB, agent *models.AIAgent, operator *dto.AuthPrincipal) (*models.AgentRevision, error) {
|
func (s *agentRevisionService) PublishSnapshot(db *gorm.DB, agent *models.AIAgent, operator *dto.AuthPrincipal) (*models.AgentRevision, error) {
|
||||||
return s.publishSnapshot(db, agent, nil, operator)
|
return s.publishSnapshot(db, agent, operator)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *agentRevisionService) publishSnapshot(db *gorm.DB, agent *models.AIAgent, version *models.AIWorkflowVersion, operator *dto.AuthPrincipal) (*models.AgentRevision, error) {
|
func (s *agentRevisionService) publishSnapshot(db *gorm.DB, agent *models.AIAgent, operator *dto.AuthPrincipal) (*models.AgentRevision, error) {
|
||||||
model := agentRevisionModel{ConfigID: agent.AIConfigID}
|
model := agentRevisionModel{ConfigID: agent.AIConfigID}
|
||||||
if config := repositories.AIConfigRepository.Get(db, agent.AIConfigID); config != nil {
|
if config := repositories.AIConfigRepository.Get(db, agent.AIConfigID); config != nil {
|
||||||
model = agentRevisionModel{
|
model = agentRevisionModel{
|
||||||
@@ -188,12 +177,6 @@ func (s *agentRevisionService) publishSnapshot(db *gorm.DB, agent *models.AIAgen
|
|||||||
TimeoutMS: config.TimeoutMS, MaxRetryCount: config.MaxRetryCount,
|
TimeoutMS: config.TimeoutMS, MaxRetryCount: config.MaxRetryCount,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
workflowVersionID := int64(0)
|
|
||||||
workflowDefinition := ""
|
|
||||||
if version != nil {
|
|
||||||
workflowVersionID = version.ID
|
|
||||||
workflowDefinition = version.Definition
|
|
||||||
}
|
|
||||||
definition := agentRevisionDefinition{
|
definition := agentRevisionDefinition{
|
||||||
Agent: agentRevisionAgent{
|
Agent: agentRevisionAgent{
|
||||||
Name: agent.Name, Description: agent.Description, AIConfigID: agent.AIConfigID,
|
Name: agent.Name, Description: agent.Description, AIConfigID: agent.AIConfigID,
|
||||||
@@ -204,8 +187,6 @@ func (s *agentRevisionService) publishSnapshot(db *gorm.DB, agent *models.AIAgen
|
|||||||
SkillIDs: agent.SkillIDs, AllowedMCPTools: agent.AllowedMCPTools,
|
SkillIDs: agent.SkillIDs, AllowedMCPTools: agent.AllowedMCPTools,
|
||||||
},
|
},
|
||||||
Model: model,
|
Model: model,
|
||||||
WorkflowVersionID: workflowVersionID,
|
|
||||||
WorkflowDefinition: workflowDefinition,
|
|
||||||
}
|
}
|
||||||
for _, binding := range repositories.AIAgentWorkflowBindingRepository.FindEnabledByAgentID(db, agent.ID) {
|
for _, binding := range repositories.AIAgentWorkflowBindingRepository.FindEnabledByAgentID(db, agent.ID) {
|
||||||
definition.WorkflowBindings = append(definition.WorkflowBindings, agentRevisionWorkflowBinding{WorkflowID: binding.WorkflowID, WorkflowVersionID: binding.WorkflowVersionID, ToolName: binding.ToolName, TriggerInstruction: binding.TriggerInstruction, Priority: binding.Priority})
|
definition.WorkflowBindings = append(definition.WorkflowBindings, agentRevisionWorkflowBinding{WorkflowID: binding.WorkflowID, WorkflowVersionID: binding.WorkflowVersionID, ToolName: binding.ToolName, TriggerInstruction: binding.TriggerInstruction, Priority: binding.Priority})
|
||||||
@@ -218,7 +199,7 @@ func (s *agentRevisionService) publishSnapshot(db *gorm.DB, agent *models.AIAgen
|
|||||||
hash := sha256.Sum256(data)
|
hash := sha256.Sum256(data)
|
||||||
item := &models.AgentRevision{
|
item := &models.AgentRevision{
|
||||||
AgentID: agent.ID, Revision: repositories.AgentRevisionRepository.MaxRevisionByAgentID(db, agent.ID) + 1,
|
AgentID: agent.ID, Revision: repositories.AgentRevisionRepository.MaxRevisionByAgentID(db, agent.ID) + 1,
|
||||||
WorkflowVersionID: workflowVersionID, Status: enums.StatusOk, Definition: string(data), DefinitionHash: hex.EncodeToString(hash[:]),
|
Status: enums.StatusOk, Definition: string(data), DefinitionHash: hex.EncodeToString(hash[:]),
|
||||||
PublishedAt: &now, PublishedByID: operator.UserID, PublishedByName: operator.Username, AuditFields: utils.BuildAuditFields(operator),
|
PublishedAt: &now, PublishedByID: operator.UserID, PublishedByName: operator.Username, AuditFields: utils.BuildAuditFields(operator),
|
||||||
}
|
}
|
||||||
if err := repositories.AgentRevisionRepository.Create(db, item); err != nil {
|
if err := repositories.AgentRevisionRepository.Create(db, item); err != nil {
|
||||||
|
|||||||
@@ -87,13 +87,6 @@ func (s *aIAgentService) CreateAIAgent(req request.CreateAIAgentRequest, operato
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(bindings) == 0 && (item.RuntimeMode == enums.AIAgentRuntimeModeWorkflow || item.RuntimeMode == enums.AIAgentRuntimeModeHybrid) {
|
|
||||||
_, createErr := AIWorkflowService.createDefaultAgentWorkflow(ctx.Tx, item, operator)
|
|
||||||
if createErr != nil {
|
|
||||||
return createErr
|
|
||||||
}
|
|
||||||
return nil // Legacy create calls remain compatible until clients send explicit bindings.
|
|
||||||
}
|
|
||||||
return s.validateWorkflowBindingMode(ctx.Tx, item, bindings)
|
return s.validateWorkflowBindingMode(ctx.Tx, item, bindings)
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@@ -285,12 +278,6 @@ func (s *aIAgentService) RollbackAIAgent(id, revisionID int64, operator *dto.Aut
|
|||||||
"update_user_name": operator.Username,
|
"update_user_name": operator.Username,
|
||||||
"updated_at": time.Now(),
|
"updated_at": time.Now(),
|
||||||
}
|
}
|
||||||
if agent.RuntimeMode == enums.AIAgentRuntimeModeWorkflow || agent.RuntimeMode == enums.AIAgentRuntimeModeHybrid {
|
|
||||||
if revision.WorkflowVersionID <= 0 || repositories.AIWorkflowVersionRepository.Get(ctx.Tx, revision.WorkflowVersionID) == nil {
|
|
||||||
return errorsx.InvalidParam("workflow revision does not contain a published workflow version")
|
|
||||||
}
|
|
||||||
updates["workflow_version_id"] = revision.WorkflowVersionID
|
|
||||||
}
|
|
||||||
return repositories.AIAgentRepository.Updates(ctx.Tx, agent.ID, updates)
|
return repositories.AIAgentRepository.Updates(ctx.Tx, agent.ID, updates)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
@@ -438,7 +425,6 @@ func (s *aIAgentService) buildAIAgentModel(id int64, req request.CreateAIAgentRe
|
|||||||
KnowledgeIDs: utils.JoinInt64s(knowledgeBaseIDs),
|
KnowledgeIDs: utils.JoinInt64s(knowledgeBaseIDs),
|
||||||
SkillIDs: utils.JoinInt64s(skillIDs),
|
SkillIDs: utils.JoinInt64s(skillIDs),
|
||||||
AllowedMCPTools: directToolsJSON,
|
AllowedMCPTools: directToolsJSON,
|
||||||
WorkflowVersionID: 0,
|
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,5 @@
|
|||||||
|
//go:build legacy
|
||||||
|
|
||||||
package services
|
package services
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
|||||||
@@ -20,7 +20,6 @@ import (
|
|||||||
"agent-desk/internal/repositories"
|
"agent-desk/internal/repositories"
|
||||||
|
|
||||||
"github.com/mlogclub/simple/sqls"
|
"github.com/mlogclub/simple/sqls"
|
||||||
"gorm.io/gorm"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
var AIWorkflowService = newAIWorkflowService()
|
var AIWorkflowService = newAIWorkflowService()
|
||||||
@@ -150,49 +149,6 @@ func appendNonZeroInt64(list []int64, value int64) []int64 {
|
|||||||
return append(list, value)
|
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 {
|
func (s *aiWorkflowService) ListNodeSpecs() []workflowregistry.NodeSpec {
|
||||||
return s.registry.List()
|
return s.registry.List()
|
||||||
}
|
}
|
||||||
@@ -264,51 +220,6 @@ func (s *aiWorkflowService) CreateWorkflow(req request.CreateAIWorkflowRequest,
|
|||||||
return item, nil
|
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 {
|
func (s *aiWorkflowService) UpdateWorkflow(req request.UpdateAIWorkflowRequest, operator *dto.AuthPrincipal) error {
|
||||||
if operator == nil {
|
if operator == nil {
|
||||||
return errorsx.UnauthorizedI18n("error.auth.expired")
|
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) {
|
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 {
|
if operator == nil {
|
||||||
return nil, errorsx.UnauthorizedI18n("error.auth.expired")
|
return nil, errorsx.UnauthorizedI18n("error.auth.expired")
|
||||||
}
|
}
|
||||||
@@ -403,97 +311,6 @@ func (s *aiWorkflowService) PublishWorkflow(req request.PublishAIWorkflowRequest
|
|||||||
return version, nil
|
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 {
|
func defaultAgentWorkflowDefinition() dsl.Definition {
|
||||||
return dsl.Definition{
|
return dsl.Definition{
|
||||||
SchemaVersion: dsl.SchemaVersion,
|
SchemaVersion: dsl.SchemaVersion,
|
||||||
|
|||||||
@@ -46,7 +46,6 @@ func TestAIWorkflowServicePublishCreatesImmutableVersion(t *testing.T) {
|
|||||||
workflow, err := AIWorkflowService.CreateWorkflow(request.CreateAIWorkflowRequest{
|
workflow, err := AIWorkflowService.CreateWorkflow(request.CreateAIWorkflowRequest{
|
||||||
Name: "support flow",
|
Name: "support flow",
|
||||||
Description: "customer service flow",
|
Description: "customer service flow",
|
||||||
AgentID: 12,
|
|
||||||
Definition: validAIWorkflowDefinition(),
|
Definition: validAIWorkflowDefinition(),
|
||||||
}, operator)
|
}, operator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -108,7 +107,6 @@ func TestAIWorkflowServicePublishIncrementsVersion(t *testing.T) {
|
|||||||
operator := aiWorkflowTestOperator()
|
operator := aiWorkflowTestOperator()
|
||||||
workflow, err := AIWorkflowService.CreateWorkflow(request.CreateAIWorkflowRequest{
|
workflow, err := AIWorkflowService.CreateWorkflow(request.CreateAIWorkflowRequest{
|
||||||
Name: "support flow versions",
|
Name: "support flow versions",
|
||||||
AgentID: 99,
|
|
||||||
Definition: validAIWorkflowDefinition(),
|
Definition: validAIWorkflowDefinition(),
|
||||||
}, operator)
|
}, operator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -140,7 +138,6 @@ func TestAIWorkflowServicePublishRejectsInvalidDSL(t *testing.T) {
|
|||||||
operator := aiWorkflowTestOperator()
|
operator := aiWorkflowTestOperator()
|
||||||
workflow, err := AIWorkflowService.CreateWorkflow(request.CreateAIWorkflowRequest{
|
workflow, err := AIWorkflowService.CreateWorkflow(request.CreateAIWorkflowRequest{
|
||||||
Name: "invalid publish flow",
|
Name: "invalid publish flow",
|
||||||
AgentID: 23,
|
|
||||||
Definition: validAIWorkflowDefinition(),
|
Definition: validAIWorkflowDefinition(),
|
||||||
}, operator)
|
}, operator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -177,7 +174,7 @@ func TestAIWorkflowServiceRunListAndDetail(t *testing.T) {
|
|||||||
if err := sqls.DB().Create(&agent).Error; err != nil {
|
if err := sqls.DB().Create(&agent).Error; err != nil {
|
||||||
t.Fatalf("create agent: %v", err)
|
t.Fatalf("create agent: %v", err)
|
||||||
}
|
}
|
||||||
workflow := models.AIWorkflow{Name: "售后流程", AgentID: agent.ID, Status: enums.StatusOk}
|
workflow := models.AIWorkflow{Name: "售后流程", Status: enums.StatusOk}
|
||||||
if err := sqls.DB().Create(&workflow).Error; err != nil {
|
if err := sqls.DB().Create(&workflow).Error; err != nil {
|
||||||
t.Fatalf("create workflow: %v", err)
|
t.Fatalf("create workflow: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -437,7 +437,7 @@ func (s *channelService) buildChannelModel(id int64, req request.CreateChannelRe
|
|||||||
return nil, errorsx.InvalidParamI18n("error.e0004")
|
return nil, errorsx.InvalidParamI18n("error.e0004")
|
||||||
}
|
}
|
||||||
if aiAgent.RuntimeMode == "" || aiAgent.RuntimeMode == enums.AIAgentRuntimeModeWorkflow {
|
if aiAgent.RuntimeMode == "" || aiAgent.RuntimeMode == enums.AIAgentRuntimeModeWorkflow {
|
||||||
if aiAgent.WorkflowVersionID <= 0 {
|
if len(AIAgentService.ListEnabledWorkflowBindings(sqls.DB(), aiAgent.ID)) != 1 {
|
||||||
return nil, errorsx.InvalidParam("ai agent workflow must be published before binding channel")
|
return nil, errorsx.InvalidParam("ai agent workflow must be published before binding channel")
|
||||||
}
|
}
|
||||||
} else if aiAgent.RuntimeMode == enums.AIAgentRuntimeModeAutonomous {
|
} else if aiAgent.RuntimeMode == enums.AIAgentRuntimeModeAutonomous {
|
||||||
@@ -445,7 +445,7 @@ func (s *channelService) buildChannelModel(id int64, req request.CreateChannelRe
|
|||||||
return nil, errorsx.InvalidParam("autonomous ai agent must be published before binding channel")
|
return nil, errorsx.InvalidParam("autonomous ai agent must be published before binding channel")
|
||||||
}
|
}
|
||||||
} else if aiAgent.RuntimeMode == enums.AIAgentRuntimeModeHybrid {
|
} else if aiAgent.RuntimeMode == enums.AIAgentRuntimeModeHybrid {
|
||||||
if aiAgent.PublishedRevisionID <= 0 || aiAgent.WorkflowVersionID <= 0 {
|
if aiAgent.PublishedRevisionID <= 0 {
|
||||||
return nil, errorsx.InvalidParam("hybrid ai agent and workflow must be published before binding channel")
|
return nil, errorsx.InvalidParam("hybrid ai agent and workflow must be published before binding channel")
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
@@ -478,7 +478,6 @@ export function AIAgentConfigWorkbench({
|
|||||||
setSavingWorkflow(true)
|
setSavingWorkflow(true)
|
||||||
try {
|
try {
|
||||||
await saveAIAgentWorkflow({
|
await saveAIAgentWorkflow({
|
||||||
agentId: currentAgentId,
|
|
||||||
name: "",
|
name: "",
|
||||||
description: "",
|
description: "",
|
||||||
definition,
|
definition,
|
||||||
@@ -561,7 +560,6 @@ export function AIAgentConfigWorkbench({
|
|||||||
setSavingWorkflow(true)
|
setSavingWorkflow(true)
|
||||||
try {
|
try {
|
||||||
const saved = await saveAIAgentWorkflow({
|
const saved = await saveAIAgentWorkflow({
|
||||||
agentId: currentAgentId,
|
|
||||||
name: "",
|
name: "",
|
||||||
description: "",
|
description: "",
|
||||||
definition,
|
definition,
|
||||||
|
|||||||
@@ -402,7 +402,6 @@ export type AIWorkflow = {
|
|||||||
id: number
|
id: number
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
agentId: number
|
|
||||||
status: number
|
status: number
|
||||||
draftDefinition: AIWorkflowDefinition
|
draftDefinition: AIWorkflowDefinition
|
||||||
publishedVersionId: number
|
publishedVersionId: number
|
||||||
@@ -459,7 +458,6 @@ export type AIWorkflowValidationResult = {
|
|||||||
export type CreateAIWorkflowPayload = {
|
export type CreateAIWorkflowPayload = {
|
||||||
name: string
|
name: string
|
||||||
description: string
|
description: string
|
||||||
agentId?: number
|
|
||||||
definition: AIWorkflowDefinition
|
definition: AIWorkflowDefinition
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user