feat: bind AI agents to workflow versions

This commit is contained in:
mlogclub
2026-06-22 00:14:36 +08:00
parent 3267a4ce5c
commit df7b199c18
7 changed files with 192 additions and 0 deletions
@@ -189,6 +189,9 @@ func buildAIAgentResponseWithLocale(item *models.AIAgent, locale string) respons
Teams: make([]response.AIAgentTeamResponse, 0),
DirectTools: make([]response.AIAgentMCPToolResponse, 0),
GraphTools: make([]string, 0),
RuntimeMode: item.RuntimeMode,
RuntimeModeName: enums.GetAIAgentRuntimeModeLabel(item.RuntimeMode),
WorkflowVersionID: item.WorkflowVersionID,
SortNo: item.SortNo,
CreatedAt: item.CreatedAt.Format("2006-01-02 15:04:05"),
UpdatedAt: item.UpdatedAt.Format("2006-01-02 15:04:05"),
+2
View File
@@ -527,6 +527,8 @@ type AIAgent struct {
SkillIDs string `gorm:"type:varchar(500);not null;default:''"` // SkillIDs 为绑定的技能ID列表,按顺序表示允许路由的范围。
AllowedMCPTools string `gorm:"type:text"` // AllowedMCPTools 为允许 direct tool 路由的 MCP 工具白名单配置JSON。
AllowedGraphTools string `gorm:"type:text"` // AllowedGraphTools 为允许 Graph Tool 的白名单配置JSON。
RuntimeMode enums.AIAgentRuntimeMode `gorm:"type:int;not null;default:1;index"` // RuntimeMode 为 Agent 执行模式,如内置 Graph 或发布的会话流程。
WorkflowVersionID int64 `gorm:"type:bigint;not null;default:0;index"` // WorkflowVersionID 为绑定的已发布会话流程版本ID。
SortNo int `gorm:"type:int;not null;default:0;index"` // SortNo 为后台展示排序号。
AuditFields
}
+2
View File
@@ -58,6 +58,8 @@ type CreateAIAgentRequest struct {
SkillIDs []int64 `json:"skillIds"`
DirectTools []AIAgentMCPToolRequest `json:"directTools"`
GraphTools []string `json:"graphTools"`
RuntimeMode enums.AIAgentRuntimeMode `json:"runtimeMode"`
WorkflowVersionID int64 `json:"workflowVersionId"`
}
type UpdateAIAgentRequest struct {
+3
View File
@@ -91,6 +91,9 @@ type AIAgentResponse struct {
Skills []AIAgentSkillResponse `json:"skills"`
DirectTools []AIAgentMCPToolResponse `json:"directTools"`
GraphTools []string `json:"graphTools"`
RuntimeMode enums.AIAgentRuntimeMode `json:"runtimeMode"`
RuntimeModeName string `json:"runtimeModeName"`
WorkflowVersionID int64 `json:"workflowVersionId"`
SortNo int `json:"sortNo"`
CreatedAt string `json:"createdAt"`
UpdatedAt string `json:"updatedAt"`
+21
View File
@@ -31,3 +31,24 @@ var aiModelTypeLabelMap = map[AIModelType]string{
func GetAIModelTypeLabel(modelType AIModelType) string {
return aiModelTypeLabelMap[modelType]
}
type AIAgentRuntimeMode int
const (
AIAgentRuntimeModeBuiltinGraph AIAgentRuntimeMode = 1
AIAgentRuntimeModeWorkflow AIAgentRuntimeMode = 2
)
var AIAgentRuntimeModeValues = []AIAgentRuntimeMode{
AIAgentRuntimeModeBuiltinGraph,
AIAgentRuntimeModeWorkflow,
}
var aiAgentRuntimeModeLabelMap = map[AIAgentRuntimeMode]string{
AIAgentRuntimeModeBuiltinGraph: "内置 Graph",
AIAgentRuntimeModeWorkflow: "会话流程",
}
func GetAIAgentRuntimeModeLabel(mode AIAgentRuntimeMode) string {
return aiAgentRuntimeModeLabelMap[mode]
}
+28
View File
@@ -108,6 +108,8 @@ func (s *aIAgentService) UpdateAIAgent(req request.UpdateAIAgentRequest, operato
"skill_ids": item.SkillIDs,
"allowed_mcp_tools": item.AllowedMCPTools,
"allowed_graph_tools": item.AllowedGraphTools,
"runtime_mode": item.RuntimeMode,
"workflow_version_id": item.WorkflowVersionID,
"update_user_id": operator.UserID,
"update_user_name": operator.Username,
"updated_at": time.Now(),
@@ -191,6 +193,10 @@ func (s *aIAgentService) buildAIAgentModel(id int64, req request.CreateAIAgentRe
if err != nil {
return nil, err
}
runtimeMode, workflowVersionID, err := s.normalizeRuntimeMode(req.RuntimeMode, req.WorkflowVersionID)
if err != nil {
return nil, err
}
directToolsJSON := ""
if len(directTools) > 0 {
buf, marshalErr := json.Marshal(directTools)
@@ -223,6 +229,8 @@ func (s *aIAgentService) buildAIAgentModel(id int64, req request.CreateAIAgentRe
SkillIDs: utils.JoinInt64s(skillIDs),
AllowedMCPTools: directToolsJSON,
AllowedGraphTools: graphToolsJSON,
RuntimeMode: runtimeMode,
WorkflowVersionID: workflowVersionID,
}, nil
}
@@ -349,6 +357,26 @@ func (s *aIAgentService) normalizeGraphTools(input []string) ([]string, error) {
return ret, nil
}
func (s *aIAgentService) normalizeRuntimeMode(input enums.AIAgentRuntimeMode, workflowVersionID int64) (enums.AIAgentRuntimeMode, int64, error) {
if input == 0 {
input = enums.AIAgentRuntimeModeBuiltinGraph
}
if !slices.Contains(enums.AIAgentRuntimeModeValues, input) {
return 0, 0, errorsx.InvalidParam("invalid ai agent runtime mode")
}
if input != enums.AIAgentRuntimeModeWorkflow {
return input, 0, nil
}
if workflowVersionID <= 0 {
return 0, 0, errorsx.InvalidParam("workflow version is required")
}
version := AIWorkflowService.GetVersion(workflowVersionID)
if version == nil || version.Status != enums.StatusOk {
return 0, 0, errorsx.InvalidParam("workflow version does not exist")
}
return input, workflowVersionID, nil
}
func (s *aIAgentService) UpdateSort(ids []int64) error {
return sqls.WithTransaction(func(ctx *sqls.TxContext) error {
for i, id := range ids {
@@ -0,0 +1,133 @@
package services
import (
"testing"
"agent-desk/internal/models"
"agent-desk/internal/pkg/dto"
"agent-desk/internal/pkg/dto/request"
"agent-desk/internal/pkg/enums"
"github.com/glebarez/sqlite"
"github.com/mlogclub/simple/sqls"
"gorm.io/gorm"
)
func TestAIAgentServiceSavesWorkflowBinding(t *testing.T) {
setupAIAgentWorkflowTestDB(t)
operator := aiAgentWorkflowTestOperator()
aiConfigID := createAIAgentWorkflowTestConfig(t)
knowledgeID := createAIAgentWorkflowTestKnowledgeBase(t)
versionID := createAIAgentWorkflowVersion(t)
item, err := AIAgentService.CreateAIAgent(request.CreateAIAgentRequest{
Name: "workflow agent",
AIConfigID: aiConfigID,
ServiceMode: enums.IMConversationServiceModeAIOnly,
HandoffMode: enums.AIAgentHandoffModeWaitPool,
FallbackMode: enums.AIAgentFallbackModeNoAnswer,
KnowledgeIDs: []int64{knowledgeID},
RuntimeMode: enums.AIAgentRuntimeModeWorkflow,
WorkflowVersionID: versionID,
}, operator)
if err != nil {
t.Fatalf("CreateAIAgent() error = %v", err)
}
if item.RuntimeMode != enums.AIAgentRuntimeModeWorkflow {
t.Fatalf("expected workflow runtime mode, got %d", item.RuntimeMode)
}
if item.WorkflowVersionID != versionID {
t.Fatalf("expected workflow version %d, got %d", versionID, item.WorkflowVersionID)
}
}
func TestAIAgentServiceRejectsWorkflowModeWithoutVersion(t *testing.T) {
setupAIAgentWorkflowTestDB(t)
operator := aiAgentWorkflowTestOperator()
aiConfigID := createAIAgentWorkflowTestConfig(t)
knowledgeID := createAIAgentWorkflowTestKnowledgeBase(t)
_, err := AIAgentService.CreateAIAgent(request.CreateAIAgentRequest{
Name: "workflow agent without version",
AIConfigID: aiConfigID,
ServiceMode: enums.IMConversationServiceModeAIOnly,
HandoffMode: enums.AIAgentHandoffModeWaitPool,
FallbackMode: enums.AIAgentFallbackModeNoAnswer,
KnowledgeIDs: []int64{knowledgeID},
RuntimeMode: enums.AIAgentRuntimeModeWorkflow,
}, operator)
if err == nil {
t.Fatalf("expected workflow runtime without version to fail")
}
}
func setupAIAgentWorkflowTestDB(t *testing.T) {
t.Helper()
db, err := gorm.Open(sqlite.Open("file:"+t.Name()+"?mode=memory&cache=shared"), &gorm.Config{})
if err != nil {
t.Fatalf("open sqlite db: %v", err)
}
if err := db.AutoMigrate(&models.AIAgent{}, &models.AIConfig{}, &models.KnowledgeBase{}, &models.AIWorkflow{}, &models.AIWorkflowVersion{}); err != nil {
t.Fatalf("auto migrate: %v", err)
}
sqls.SetDB(db)
}
func createAIAgentWorkflowTestConfig(t *testing.T) int64 {
t.Helper()
item := &models.AIConfig{
Name: "workflow-test-config",
Provider: enums.AIProviderOpenAI,
ModelType: enums.AIModelTypeLLM,
ModelName: "gpt-test",
Status: enums.StatusOk,
}
if err := sqls.DB().Create(item).Error; err != nil {
t.Fatalf("create ai config: %v", err)
}
return item.ID
}
func createAIAgentWorkflowTestKnowledgeBase(t *testing.T) int64 {
t.Helper()
item := &models.KnowledgeBase{
Name: "workflow-test-kb",
KnowledgeType: string(enums.KnowledgeBaseTypeFAQ),
Status: enums.StatusOk,
}
if err := sqls.DB().Create(item).Error; err != nil {
t.Fatalf("create knowledge base: %v", err)
}
return item.ID
}
func createAIAgentWorkflowVersion(t *testing.T) int64 {
t.Helper()
workflow := &models.AIWorkflow{
Name: "workflow-test",
OwnerType: "ai_agent",
OwnerID: 1,
Status: enums.StatusOk,
}
if err := sqls.DB().Create(workflow).Error; err != nil {
t.Fatalf("create workflow: %v", err)
}
version := &models.AIWorkflowVersion{
WorkflowID: workflow.ID,
Version: 1,
Status: enums.StatusOk,
}
if err := sqls.DB().Create(version).Error; err != nil {
t.Fatalf("create workflow version: %v", err)
}
return version.ID
}
func aiAgentWorkflowTestOperator() *dto.AuthPrincipal {
return &dto.AuthPrincipal{
UserID: 1,
Username: "agent-workflow-tester",
Nickname: "agent-workflow-tester",
}
}