Files
WeiCHEN ad96f67442 refactor(workflowdef): make definition/runtime deps unidirectional
Keep registry key validation local to workflowdef with composition-root
injection, adapt FlatCatalog for projectworkflow, and drive the
orchestrator through LiveInstancePort views instead of projectinstance.
2026-09-22 21:44:57 +08:00

132 lines
3.7 KiB
Go

package bizdev
import (
"context"
"chat-agent-system/internal/projectworkflow"
"chat-agent-system/internal/workflowdef"
)
// flatCatalogAdapter maps workflowdef.FlatCatalog onto projectworkflow.Catalog at the composition root.
type flatCatalogAdapter struct {
inner workflowdef.FlatCatalog
}
func adaptFlatCatalog(c workflowdef.FlatCatalog) projectworkflow.Catalog {
if c == nil {
return nil
}
return flatCatalogAdapter{inner: c}
}
func (a flatCatalogAdapter) Get(workflowKey string) (*projectworkflow.Definition, bool) {
d, ok := a.inner.Get(workflowKey)
if !ok || d == nil {
return nil, false
}
return &projectworkflow.Definition{
Key: d.Key,
Name: d.Name,
Description: d.Description,
StageKey: d.StageKey,
TaskKey: d.TaskKey,
RequiredUpstreamWorkflow: d.RequiredUpstreamWorkflow,
OutputDocType: d.OutputDocType,
OutputDocTitle: d.OutputDocTitle,
Enabled: d.Enabled,
}, true
}
func (a flatCatalogAdapter) List() []*projectworkflow.Definition {
items := a.inner.List()
out := make([]*projectworkflow.Definition, 0, len(items))
for _, d := range items {
if d == nil {
continue
}
out = append(out, &projectworkflow.Definition{
Key: d.Key,
Name: d.Name,
Description: d.Description,
StageKey: d.StageKey,
TaskKey: d.TaskKey,
RequiredUpstreamWorkflow: d.RequiredUpstreamWorkflow,
OutputDocType: d.OutputDocType,
OutputDocTitle: d.OutputDocTitle,
Enabled: d.Enabled,
})
}
return out
}
func (a flatCatalogAdapter) Validate() error {
err := a.inner.Validate()
if err == nil {
return nil
}
if err == workflowdef.ErrCatalogUpstreamMissing {
return projectworkflow.ErrWorkflowNotFound
}
return err
}
func (a flatCatalogAdapter) Refresh(ctx context.Context) error {
if r, ok := a.inner.(workflowdef.CatalogRefresher); ok {
return r.Refresh(ctx)
}
return nil
}
// defaultFlatCatalog wraps projectworkflow.NewDefaultCatalog as a workflowdef.FlatCatalog fallback.
type defaultFlatCatalog struct {
inner projectworkflow.Catalog
}
func newDefaultFlatCatalog() workflowdef.FlatCatalog {
return defaultFlatCatalog{inner: projectworkflow.NewDefaultCatalog()}
}
func (d defaultFlatCatalog) Get(workflowKey string) (*workflowdef.FlatDefinition, bool) {
def, ok := d.inner.Get(workflowKey)
if !ok || def == nil {
return nil, false
}
return &workflowdef.FlatDefinition{
Key: def.Key,
Name: def.Name,
Description: def.Description,
StageKey: def.StageKey,
TaskKey: def.TaskKey,
RequiredUpstreamWorkflow: def.RequiredUpstreamWorkflow,
OutputDocType: def.OutputDocType,
OutputDocTitle: def.OutputDocTitle,
Enabled: def.Enabled,
}, true
}
func (d defaultFlatCatalog) List() []*workflowdef.FlatDefinition {
items := d.inner.List()
out := make([]*workflowdef.FlatDefinition, 0, len(items))
for _, def := range items {
if def == nil {
continue
}
out = append(out, &workflowdef.FlatDefinition{
Key: def.Key,
Name: def.Name,
Description: def.Description,
StageKey: def.StageKey,
TaskKey: def.TaskKey,
RequiredUpstreamWorkflow: def.RequiredUpstreamWorkflow,
OutputDocType: def.OutputDocType,
OutputDocTitle: def.OutputDocTitle,
Enabled: def.Enabled,
})
}
return out
}
func (d defaultFlatCatalog) Validate() error {
return d.inner.Validate()
}