refactor(workflow): Move the plugin component in the Workflow package into the common crossdomain package (#717)

This commit is contained in:
Ryo
2025-08-13 11:06:53 +08:00
committed by GitHub
parent b38ab95623
commit 99c759addc
47 changed files with 1330 additions and 1407 deletions

View File

@@ -22,6 +22,7 @@ import (
einoCompose "github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/schema"
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
"github.com/coze-dev/coze-studio/backend/domain/workflow"
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
@@ -36,7 +37,7 @@ func (a *asToolImpl) WithMessagePipe() (einoCompose.Option, *schema.StreamReader
return execute.WithMessagePipe()
}
func (a *asToolImpl) WithExecuteConfig(cfg vo.ExecuteConfig) einoCompose.Option {
func (a *asToolImpl) WithExecuteConfig(cfg plugin.ExecuteConfig) einoCompose.Option {
return einoCompose.WithToolsNodeOption(einoCompose.WithToolOption(execute.WithExecuteConfig(cfg)))
}

View File

@@ -25,6 +25,8 @@ import (
einoCompose "github.com/cloudwego/eino/compose"
"github.com/cloudwego/eino/schema"
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
model "github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
"github.com/coze-dev/coze-studio/backend/domain/workflow"
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity"
"github.com/coze-dev/coze-studio/backend/domain/workflow/entity/vo"
@@ -44,7 +46,7 @@ type executableImpl struct {
repo workflow.Repository
}
func (i *impl) SyncExecute(ctx context.Context, config vo.ExecuteConfig, input map[string]any) (*entity.WorkflowExecution, vo.TerminatePlan, error) {
func (i *impl) SyncExecute(ctx context.Context, config model.ExecuteConfig, input map[string]any) (*entity.WorkflowExecution, vo.TerminatePlan, error) {
var (
err error
wfEntity *entity.Workflow
@@ -62,7 +64,7 @@ func (i *impl) SyncExecute(ctx context.Context, config vo.ExecuteConfig, input m
}
isApplicationWorkflow := wfEntity.AppID != nil
if isApplicationWorkflow && config.Mode == vo.ExecuteModeRelease {
if isApplicationWorkflow && config.Mode == model.ExecuteModeRelease {
err = i.checkApplicationWorkflowReleaseVersion(ctx, *wfEntity.AppID, config.ConnectorID, config.ID, config.Version)
if err != nil {
return nil, "", err
@@ -189,7 +191,7 @@ func (i *impl) SyncExecute(ctx context.Context, config vo.ExecuteConfig, input m
// AsyncExecute executes the specified workflow asynchronously, returning the execution ID.
// Intermediate results are not emitted on the fly.
// The caller is expected to poll the execution status using the GetExecution method and the returned execution ID.
func (i *impl) AsyncExecute(ctx context.Context, config vo.ExecuteConfig, input map[string]any) (int64, error) {
func (i *impl) AsyncExecute(ctx context.Context, config plugin.ExecuteConfig, input map[string]any) (int64, error) {
var (
err error
wfEntity *entity.Workflow
@@ -207,7 +209,7 @@ func (i *impl) AsyncExecute(ctx context.Context, config vo.ExecuteConfig, input
}
isApplicationWorkflow := wfEntity.AppID != nil
if isApplicationWorkflow && config.Mode == vo.ExecuteModeRelease {
if isApplicationWorkflow && config.Mode == plugin.ExecuteModeRelease {
err = i.checkApplicationWorkflowReleaseVersion(ctx, *wfEntity.AppID, config.ConnectorID, config.ID, config.Version)
if err != nil {
return 0, err
@@ -264,7 +266,7 @@ func (i *impl) AsyncExecute(ctx context.Context, config vo.ExecuteConfig, input
return 0, err
}
if config.Mode == vo.ExecuteModeDebug {
if config.Mode == plugin.ExecuteModeDebug {
if err = i.repo.SetTestRunLatestExeID(ctx, wfEntity.ID, config.Operator, executeID); err != nil {
logs.CtxErrorf(ctx, "failed to set test run latest exe id: %v", err)
}
@@ -275,7 +277,7 @@ func (i *impl) AsyncExecute(ctx context.Context, config vo.ExecuteConfig, input
return executeID, nil
}
func (i *impl) AsyncExecuteNode(ctx context.Context, nodeID string, config vo.ExecuteConfig, input map[string]any) (int64, error) {
func (i *impl) AsyncExecuteNode(ctx context.Context, nodeID string, config plugin.ExecuteConfig, input map[string]any) (int64, error) {
var (
err error
wfEntity *entity.Workflow
@@ -292,7 +294,7 @@ func (i *impl) AsyncExecuteNode(ctx context.Context, nodeID string, config vo.Ex
}
isApplicationWorkflow := wfEntity.AppID != nil
if isApplicationWorkflow && config.Mode == vo.ExecuteModeRelease {
if isApplicationWorkflow && config.Mode == plugin.ExecuteModeRelease {
err = i.checkApplicationWorkflowReleaseVersion(ctx, *wfEntity.AppID, config.ConnectorID, config.ID, config.Version)
if err != nil {
return 0, err
@@ -343,7 +345,7 @@ func (i *impl) AsyncExecuteNode(ctx context.Context, nodeID string, config vo.Ex
return 0, err
}
if config.Mode == vo.ExecuteModeNodeDebug {
if config.Mode == plugin.ExecuteModeNodeDebug {
if err = i.repo.SetNodeDebugLatestExeID(ctx, wfEntity.ID, nodeID, config.Operator, executeID); err != nil {
logs.CtxErrorf(ctx, "failed to set node debug latest exe id: %v", err)
}
@@ -356,7 +358,7 @@ func (i *impl) AsyncExecuteNode(ctx context.Context, nodeID string, config vo.Ex
// StreamExecute executes the specified workflow, returning a stream of execution events.
// The caller is expected to receive from the returned stream immediately.
func (i *impl) StreamExecute(ctx context.Context, config vo.ExecuteConfig, input map[string]any) (*schema.StreamReader[*entity.Message], error) {
func (i *impl) StreamExecute(ctx context.Context, config plugin.ExecuteConfig, input map[string]any) (*schema.StreamReader[*entity.Message], error) {
var (
err error
wfEntity *entity.Workflow
@@ -375,7 +377,7 @@ func (i *impl) StreamExecute(ctx context.Context, config vo.ExecuteConfig, input
}
isApplicationWorkflow := wfEntity.AppID != nil
if isApplicationWorkflow && config.Mode == vo.ExecuteModeRelease {
if isApplicationWorkflow && config.Mode == plugin.ExecuteModeRelease {
err = i.checkApplicationWorkflowReleaseVersion(ctx, *wfEntity.AppID, config.ConnectorID, config.ID, config.Version)
if err != nil {
return nil, err
@@ -545,7 +547,7 @@ func (i *impl) GetNodeExecution(ctx context.Context, exeID int64, nodeID string)
return nil, nil, fmt.Errorf("try getting workflow exe for exeID : %d, but not found", exeID)
}
if wfExe.Mode != vo.ExecuteModeNodeDebug {
if wfExe.Mode != plugin.ExecuteModeNodeDebug {
return nodeExe, nil, nil
}
@@ -671,7 +673,7 @@ func mergeCompositeInnerNodes(nodeExes map[int]*entity.NodeExecution, maxIndex i
// AsyncResume resumes a workflow execution asynchronously, using the passed in executionID and eventID.
// Intermediate results during the resuming run are not emitted on the fly.
// Caller is expected to poll the execution status using the GetExecution method.
func (i *impl) AsyncResume(ctx context.Context, req *entity.ResumeRequest, config vo.ExecuteConfig) error {
func (i *impl) AsyncResume(ctx context.Context, req *entity.ResumeRequest, config plugin.ExecuteConfig) error {
wfExe, found, err := i.repo.GetWorkflowExecution(ctx, req.ExecuteID)
if err != nil {
return err
@@ -689,11 +691,11 @@ func (i *impl) AsyncResume(ctx context.Context, req *entity.ResumeRequest, confi
return fmt.Errorf("workflow execution %d is not interrupted, status is %v, cannot resume", req.ExecuteID, wfExe.Status)
}
var from vo.Locator
var from plugin.Locator
if wfExe.Version == "" {
from = vo.FromDraft
from = plugin.FromDraft
} else {
from = vo.FromSpecificVersion
from = plugin.FromSpecificVersion
}
wfEntity, err := i.Get(ctx, &vo.GetPolicy{
@@ -722,7 +724,7 @@ func (i *impl) AsyncResume(ctx context.Context, req *entity.ResumeRequest, confi
config.ConnectorID = wfExe.ConnectorID
}
if wfExe.Mode == vo.ExecuteModeNodeDebug {
if wfExe.Mode == plugin.ExecuteModeNodeDebug {
nodeExes, err := i.repo.GetNodeExecutionsByWfExeID(ctx, wfExe.ID)
if err != nil {
return err
@@ -751,7 +753,7 @@ func (i *impl) AsyncResume(ctx context.Context, req *entity.ResumeRequest, confi
return fmt.Errorf("failed to create workflow: %w", err)
}
config.Mode = vo.ExecuteModeNodeDebug
config.Mode = plugin.ExecuteModeNodeDebug
cancelCtx, _, opts, _, err := compose.NewWorkflowRunner(
wfEntity.GetBasic(), workflowSC, config, compose.WithResumeReq(req)).Prepare(ctx)
@@ -793,7 +795,7 @@ func (i *impl) AsyncResume(ctx context.Context, req *entity.ResumeRequest, confi
// StreamResume resumes a workflow execution, using the passed in executionID and eventID.
// Intermediate results during the resuming run are emitted using the returned StreamReader.
// Caller is expected to poll the execution status using the GetExecution method.
func (i *impl) StreamResume(ctx context.Context, req *entity.ResumeRequest, config vo.ExecuteConfig) (
func (i *impl) StreamResume(ctx context.Context, req *entity.ResumeRequest, config plugin.ExecuteConfig) (
*schema.StreamReader[*entity.Message], error) {
// must get the interrupt event
// generate the state modifier
@@ -814,11 +816,11 @@ func (i *impl) StreamResume(ctx context.Context, req *entity.ResumeRequest, conf
return nil, fmt.Errorf("workflow execution %d is not interrupted, status is %v, cannot resume", req.ExecuteID, wfExe.Status)
}
var from vo.Locator
var from plugin.Locator
if wfExe.Version == "" {
from = vo.FromDraft
from = plugin.FromDraft
} else {
from = vo.FromSpecificVersion
from = plugin.FromSpecificVersion
}
wfEntity, err := i.Get(ctx, &vo.GetPolicy{

View File

@@ -30,6 +30,7 @@ import (
"golang.org/x/sync/errgroup"
"gorm.io/gorm"
"github.com/coze-dev/coze-studio/backend/api/model/crossdomain/plugin"
cloudworkflow "github.com/coze-dev/coze-studio/backend/api/model/workflow"
"github.com/coze-dev/coze-studio/backend/application/base/ctxutil"
"github.com/coze-dev/coze-studio/backend/domain/workflow"
@@ -386,7 +387,7 @@ func (i *impl) ValidateTree(ctx context.Context, id int64, validateConfig vo.Val
MetaQuery: vo.MetaQuery{
IDs: ids,
},
QType: vo.FromDraft,
QType: plugin.FromDraft,
})
if err != nil {
return nil, err
@@ -719,7 +720,7 @@ func (i *impl) UpdateMeta(ctx context.Context, id int64, metaUpdate *vo.MetaUpda
return nil
}
func (i *impl) CopyWorkflow(ctx context.Context, workflowID int64, policy vo.CopyWorkflowPolicy) (*entity.Workflow, error) {
func (i *impl) CopyWorkflow(ctx context.Context, workflowID int64, policy plugin.CopyWorkflowPolicy) (*entity.Workflow, error) {
wf, err := i.repo.CopyWorkflow(ctx, workflowID, policy)
if err != nil {
return nil, err
@@ -754,13 +755,13 @@ func (i *impl) ReleaseApplicationWorkflows(ctx context.Context, appID int64, con
MetaQuery: vo.MetaQuery{
AppID: &appID,
},
QType: vo.FromDraft,
QType: plugin.FromDraft,
})
if err != nil {
return nil, err
}
relatedPlugins := make(map[int64]*vo.PluginEntity, len(config.PluginIDs))
relatedPlugins := make(map[int64]*plugin.PluginEntity, len(config.PluginIDs))
relatedWorkflow := make(map[int64]entity.IDVersionPair, len(wfs))
for _, wf := range wfs {
@@ -770,7 +771,7 @@ func (i *impl) ReleaseApplicationWorkflows(ctx context.Context, appID int64, con
}
}
for _, id := range config.PluginIDs {
relatedPlugins[id] = &vo.PluginEntity{
relatedPlugins[id] = &plugin.PluginEntity{
PluginID: id,
PluginVersion: &config.Version,
}
@@ -803,7 +804,7 @@ func (i *impl) ReleaseApplicationWorkflows(ctx context.Context, appID int64, con
return nil, err
}
err = replaceRelatedWorkflowOrExternalResourceInWorkflowNodes(c.Nodes, relatedWorkflow, vo.ExternalResourceRelated{
err = replaceRelatedWorkflowOrExternalResourceInWorkflowNodes(c.Nodes, relatedWorkflow, plugin.ExternalResourceRelated{
PluginMap: relatedPlugins,
})
@@ -870,7 +871,7 @@ func (i *impl) ReleaseApplicationWorkflows(ctx context.Context, appID int64, con
return nil, nil
}
func (i *impl) CopyWorkflowFromAppToLibrary(ctx context.Context, workflowID int64, appID int64, related vo.ExternalResourceRelated) (map[int64]entity.IDVersionPair, []*vo.ValidateIssue, error) {
func (i *impl) CopyWorkflowFromAppToLibrary(ctx context.Context, workflowID int64, appID int64, related plugin.ExternalResourceRelated) (map[int64]entity.IDVersionPair, []*vo.ValidateIssue, error) {
type copiedWorkflow struct {
id int64
@@ -1084,7 +1085,7 @@ func (i *impl) CopyWorkflowFromAppToLibrary(ctx context.Context, workflowID int6
return err
}
cwf, err := i.repo.CopyWorkflow(ctx, wf.id, vo.CopyWorkflowPolicy{
cwf, err := i.repo.CopyWorkflow(ctx, wf.id, plugin.CopyWorkflowPolicy{
TargetAppID: ptr.Of(int64(0)),
ModifiedCanvasSchema: ptr.Of(modifiedCanvasString),
})
@@ -1144,7 +1145,7 @@ func (i *impl) CopyWorkflowFromAppToLibrary(ctx context.Context, workflowID int6
}
func (i *impl) DuplicateWorkflowsByAppID(ctx context.Context, sourceAppID, targetAppID int64, related vo.ExternalResourceRelated) error {
func (i *impl) DuplicateWorkflowsByAppID(ctx context.Context, sourceAppID, targetAppID int64, related plugin.ExternalResourceRelated) error {
type copiedWorkflow struct {
id int64
@@ -1276,7 +1277,7 @@ func (i *impl) DuplicateWorkflowsByAppID(ctx context.Context, sourceAppID, targe
return err
}
cwf, err := i.CopyWorkflow(ctx, wf.id, vo.CopyWorkflowPolicy{
cwf, err := i.CopyWorkflow(ctx, wf.id, plugin.CopyWorkflowPolicy{
TargetAppID: ptr.Of(targetAppID),
ModifiedCanvasSchema: ptr.Of(modifiedCanvasString),
})
@@ -1331,7 +1332,7 @@ func (i *impl) DuplicateWorkflowsByAppID(ctx context.Context, sourceAppID, targe
}
func (i *impl) SyncRelatedWorkflowResources(ctx context.Context, appID int64, relatedWorkflows map[int64]entity.IDVersionPair, related vo.ExternalResourceRelated) error {
func (i *impl) SyncRelatedWorkflowResources(ctx context.Context, appID int64, relatedWorkflows map[int64]entity.IDVersionPair, related plugin.ExternalResourceRelated) error {
draftVersions, _, err := i.repo.GetDraftWorkflowsByAppID(ctx, appID)
if err != nil {
return err
@@ -1380,10 +1381,10 @@ func (i *impl) SyncRelatedWorkflowResources(ctx context.Context, appID int64, re
}
func (i *impl) GetWorkflowDependenceResource(ctx context.Context, workflowID int64) (*vo.DependenceResource, error) {
func (i *impl) GetWorkflowDependenceResource(ctx context.Context, workflowID int64) (*plugin.DependenceResource, error) {
wf, err := i.Get(ctx, &vo.GetPolicy{
ID: workflowID,
QType: vo.FromDraft,
QType: plugin.FromDraft,
})
if err != nil {
return nil, err
@@ -1394,7 +1395,7 @@ func (i *impl) GetWorkflowDependenceResource(ctx context.Context, workflowID int
return nil, err
}
ds := &vo.DependenceResource{
ds := &plugin.DependenceResource{
PluginIDs: make([]int64, 0),
KnowledgeIDs: make([]int64, 0),
DatabaseIDs: make([]int64, 0),
@@ -1483,7 +1484,7 @@ func (i *impl) GetWorkflowDependenceResource(ctx context.Context, workflowID int
subWorkflow, err := i.repo.GetEntity(ctx, &vo.GetPolicy{
ID: wfID,
QType: vo.FromDraft,
QType: plugin.FromDraft,
})
if err != nil {
return err
@@ -1558,9 +1559,9 @@ func (i *impl) MGet(ctx context.Context, policy *vo.MGetPolicy) ([]*entity.Workf
}
switch policy.QType {
case vo.FromDraft:
case plugin.FromDraft:
return i.repo.MGetDrafts(ctx, policy)
case vo.FromSpecificVersion:
case plugin.FromSpecificVersion:
if len(policy.IDs) == 0 || len(policy.Versions) != len(policy.IDs) {
return nil, 0, fmt.Errorf("ids and versions are required when MGet from specific versions")
}
@@ -1602,7 +1603,7 @@ func (i *impl) MGet(ctx context.Context, policy *vo.MGetPolicy) ([]*entity.Workf
}
return result, total, nil
case vo.FromLatestVersion:
case plugin.FromLatestVersion:
return i.repo.MGetLatestVersion(ctx, policy)
default:
panic("not implemented")
@@ -1637,7 +1638,7 @@ func (i *impl) calculateTestRunSuccess(ctx context.Context, c *vo.Canvas, wid in
return existedDraft.TestRunSuccess, nil // inherit previous draft snapshot's test run success flag
}
func replaceRelatedWorkflowOrExternalResourceInWorkflowNodes(nodes []*vo.Node, relatedWorkflows map[int64]entity.IDVersionPair, related vo.ExternalResourceRelated) error {
func replaceRelatedWorkflowOrExternalResourceInWorkflowNodes(nodes []*vo.Node, relatedWorkflows map[int64]entity.IDVersionPair, related plugin.ExternalResourceRelated) error {
var (
hasWorkflowRelated = len(relatedWorkflows) > 0
hasPluginRelated = len(related.PluginMap) > 0