forked from longtao.wu/droboticflow
275 lines
11 KiB
Go
275 lines
11 KiB
Go
package domain
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
const MaxInlinePayloadBytes = 64 * 1024
|
|
|
|
func NormalizeWorkflowDefinitionMetadata(workflow *WorkflowRecord) {
|
|
if workflow == nil {
|
|
return
|
|
}
|
|
if workflow.DefinitionState == "" {
|
|
workflow.DefinitionState = WorkflowDefinitionStateDraft
|
|
}
|
|
if workflow.DefinitionVersion <= 0 {
|
|
workflow.DefinitionVersion = 1
|
|
}
|
|
if strings.TrimSpace(workflow.DefinitionFamilyID) == "" {
|
|
workflow.DefinitionFamilyID = workflow.ID
|
|
}
|
|
}
|
|
|
|
type WorkflowInputContractIssue struct {
|
|
Field string
|
|
Code string
|
|
Message string
|
|
}
|
|
|
|
func ValidateArtifactPayload(payload ArtifactPayload, expectedType PortType) error {
|
|
if payload.Type == "" {
|
|
payload.Type = expectedType
|
|
}
|
|
if expectedType != "" && payload.Type != expectedType {
|
|
return fmt.Errorf("payload type %q does not match input type %q", payload.Type, expectedType)
|
|
}
|
|
switch payload.Mode {
|
|
case ArtifactTransferModeInline:
|
|
if payload.Type != PortTypeJSON {
|
|
return fmt.Errorf("inline payloads are only supported for json inputs")
|
|
}
|
|
if payload.Inline == nil {
|
|
return fmt.Errorf("inline payload value is required")
|
|
}
|
|
if payload.Ref != nil {
|
|
return fmt.Errorf("inline payload must not include ref")
|
|
}
|
|
encoded, err := json.Marshal(payload.Inline)
|
|
if err != nil {
|
|
return fmt.Errorf("inline payload must be valid json: %w", err)
|
|
}
|
|
if len(encoded) > MaxInlinePayloadBytes {
|
|
return fmt.Errorf("inline payload exceeds %d bytes; use ref mode", MaxInlinePayloadBytes)
|
|
}
|
|
case ArtifactTransferModeRef:
|
|
if payload.Ref == nil {
|
|
return fmt.Errorf("ref payload requires ref")
|
|
}
|
|
if payload.Inline != nil {
|
|
return fmt.Errorf("ref payload must not include inline")
|
|
}
|
|
ref := payload.Ref
|
|
if ref.Provider != StorageProviderBOS {
|
|
return fmt.Errorf("ref.provider must be BOS")
|
|
}
|
|
if strings.TrimSpace(ref.ConnectionID) == "" {
|
|
return fmt.Errorf("ref.connectionId is required")
|
|
}
|
|
if strings.TrimSpace(ref.Bucket) == "" {
|
|
return fmt.Errorf("ref.bucket is required")
|
|
}
|
|
switch ref.Kind {
|
|
case ArtifactRefKindObject:
|
|
if strings.TrimSpace(ref.Key) == "" {
|
|
return fmt.Errorf("object ref.key is required")
|
|
}
|
|
case ArtifactRefKindDirectory:
|
|
if payload.Type != PortTypeFile {
|
|
return fmt.Errorf("directory refs require a file input")
|
|
}
|
|
if strings.TrimSpace(ref.Prefix) == "" {
|
|
return fmt.Errorf("directory ref.prefix is required")
|
|
}
|
|
default:
|
|
return fmt.Errorf("ref.kind must be object or directory")
|
|
}
|
|
default:
|
|
return fmt.Errorf("payload mode must be inline or ref")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func WorkflowRootNodeIDs(nodes []WorkflowNode, edges []WorkflowEdge) map[string]struct{} {
|
|
rootIDs := make(map[string]struct{}, len(nodes))
|
|
for _, node := range nodes {
|
|
rootIDs[node.ID] = struct{}{}
|
|
}
|
|
for _, edge := range edges {
|
|
delete(rootIDs, edge.Target)
|
|
}
|
|
return rootIDs
|
|
}
|
|
|
|
func workflowInputPort(node *WorkflowNode, portID string) *PortDefinition {
|
|
if node == nil {
|
|
return nil
|
|
}
|
|
for index := range node.Data.Inputs {
|
|
if strings.TrimSpace(node.Data.Inputs[index].ID) == strings.TrimSpace(portID) {
|
|
return &node.Data.Inputs[index]
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func edgeTargetsPort(edge WorkflowEdge, node WorkflowNode, port PortDefinition) bool {
|
|
if edge.Target != node.ID {
|
|
return false
|
|
}
|
|
targetHandle := strings.TrimSpace(edge.TargetHandle)
|
|
if targetHandle != "" {
|
|
return targetHandle == strings.TrimSpace(port.ID)
|
|
}
|
|
return len(node.Data.Inputs) == 1
|
|
}
|
|
|
|
func ValidateWorkflowInputContract(nodes []WorkflowNode, edges []WorkflowEdge, definitions []WorkflowInputDefinition, release bool) []WorkflowInputContractIssue {
|
|
issues := make([]WorkflowInputContractIssue, 0)
|
|
rootIDs := WorkflowRootNodeIDs(nodes, edges)
|
|
nodeByID := make(map[string]*WorkflowNode, len(nodes))
|
|
for index := range nodes {
|
|
nodeByID[nodes[index].ID] = &nodes[index]
|
|
}
|
|
definitionIDs := make(map[string]struct{}, len(definitions))
|
|
boundTargets := map[string]string{}
|
|
for index, definition := range definitions {
|
|
field := fmt.Sprintf("workflowInputs[%d]", index)
|
|
id := strings.TrimSpace(definition.ID)
|
|
if id == "" {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: field + ".id", Code: "workflow_input_id_required", Message: "workflow input id is required"})
|
|
} else if _, exists := definitionIDs[id]; exists {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: field + ".id", Code: "workflow_input_id_duplicate", Message: "workflow input id must be unique"})
|
|
} else {
|
|
definitionIDs[id] = struct{}{}
|
|
}
|
|
if strings.TrimSpace(definition.Name) == "" {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: field + ".name", Code: "workflow_input_name_required", Message: "workflow input name is required"})
|
|
}
|
|
if definition.DefaultPayload != nil {
|
|
if err := ValidateArtifactPayload(*definition.DefaultPayload, definition.Type); err != nil {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: field + ".defaultPayload", Code: "workflow_input_payload_invalid", Message: err.Error()})
|
|
}
|
|
if ValidPortPayloadMode(definition.PayloadMode) && definition.DefaultPayload.Mode != definition.PayloadMode {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: field + ".defaultPayload", Code: "workflow_input_payload_mode_mismatch", Message: "workflow input default payload mode must match payloadMode"})
|
|
}
|
|
}
|
|
if len(definition.Bindings) == 0 {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: field + ".bindings", Code: "workflow_input_binding_required", Message: "workflow input requires at least one root-node binding"})
|
|
}
|
|
for bindingIndex, binding := range definition.Bindings {
|
|
bindingField := fmt.Sprintf("%s.bindings[%d]", field, bindingIndex)
|
|
node := nodeByID[strings.TrimSpace(binding.NodeID)]
|
|
if node == nil {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: bindingField + ".nodeId", Code: "workflow_input_node_missing", Message: "workflow input binding node does not exist"})
|
|
continue
|
|
}
|
|
if _, root := rootIDs[node.ID]; !root {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: bindingField + ".nodeId", Code: "workflow_input_node_not_root", Message: "workflow inputs may only bind nodes without incoming edges"})
|
|
}
|
|
port := workflowInputPort(node, binding.TargetPortID)
|
|
if port == nil {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: bindingField + ".targetPortId", Code: "workflow_input_port_missing", Message: "workflow input binding port does not exist"})
|
|
continue
|
|
}
|
|
if port.Type != definition.Type {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: bindingField + ".targetPortId", Code: "workflow_input_type_mismatch", Message: "workflow input type must match target port type"})
|
|
}
|
|
if ValidPortContainer(definition.Container) && !EffectivePortContract(*port).Legacy && definition.Container != EffectivePortContract(*port).Container {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: bindingField + ".targetPortId", Code: "workflow_input_container_mismatch", Message: "workflow input container must match target port container"})
|
|
}
|
|
if ValidPortPayloadMode(definition.PayloadMode) && !EffectivePortContract(*port).Legacy && definition.PayloadMode != EffectivePortContract(*port).PayloadMode {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: bindingField + ".targetPortId", Code: "workflow_input_payload_mode_mismatch", Message: "workflow input payloadMode must match target port payloadMode"})
|
|
}
|
|
if strings.TrimSpace(definition.Role) != "" && strings.TrimSpace(port.Role) != "" && !strings.EqualFold(strings.TrimSpace(definition.Role), strings.TrimSpace(port.Role)) {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: bindingField + ".targetPortId", Code: "workflow_input_role_mismatch", Message: "workflow input role must match target port role"})
|
|
}
|
|
if port.Required && !definition.Required {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: field + ".required", Code: "workflow_input_required_mismatch", Message: "a required target port cannot be exposed as optional"})
|
|
}
|
|
targetKey := node.ID + "\x00" + port.ID
|
|
if previous, exists := boundTargets[targetKey]; exists && previous != id {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: bindingField, Code: "workflow_input_binding_duplicate", Message: "a root input port may only be bound by one workflow input"})
|
|
} else {
|
|
boundTargets[targetKey] = id
|
|
}
|
|
}
|
|
}
|
|
if !release {
|
|
return issues
|
|
}
|
|
for _, node := range nodes {
|
|
_, root := rootIDs[node.ID]
|
|
for _, port := range node.Data.Inputs {
|
|
if !port.Required {
|
|
continue
|
|
}
|
|
if root {
|
|
if _, bound := boundTargets[node.ID+"\x00"+port.ID]; !bound {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: "workflowInputs", Code: "required_root_input_unbound", Message: fmt.Sprintf("required root input %s.%s must be exposed as a workflow input", node.ID, port.ID)})
|
|
}
|
|
continue
|
|
}
|
|
connected := false
|
|
for _, edge := range edges {
|
|
if edgeTargetsPort(edge, node, port) {
|
|
connected = true
|
|
break
|
|
}
|
|
}
|
|
if !connected {
|
|
issues = append(issues, WorkflowInputContractIssue{Field: "edges", Code: "required_input_unconnected", Message: fmt.Sprintf("required input %s.%s must have an upstream edge", node.ID, port.ID)})
|
|
}
|
|
}
|
|
}
|
|
return issues
|
|
}
|
|
|
|
func ResolveWorkflowRunInputs(workflow WorkflowRecord, provided map[string]ArtifactPayload) (map[string]ArtifactPayload, []ResolvedInputBinding, error) {
|
|
definitions := make(map[string]WorkflowInputDefinition, len(workflow.WorkflowInputs))
|
|
for _, definition := range workflow.WorkflowInputs {
|
|
definitions[definition.ID] = definition
|
|
}
|
|
for id := range provided {
|
|
if _, ok := definitions[id]; !ok {
|
|
return nil, nil, fmt.Errorf("unknown workflow input %q", id)
|
|
}
|
|
}
|
|
resolvedInputs := make(map[string]ArtifactPayload)
|
|
bindings := make([]ResolvedInputBinding, 0)
|
|
for _, definition := range workflow.WorkflowInputs {
|
|
payload, ok := provided[definition.ID]
|
|
if !ok && definition.DefaultPayload != nil {
|
|
payload = *definition.DefaultPayload
|
|
ok = true
|
|
}
|
|
if !ok {
|
|
if definition.Required {
|
|
return nil, nil, fmt.Errorf("required workflow input %q is missing", definition.ID)
|
|
}
|
|
continue
|
|
}
|
|
if payload.Type == "" {
|
|
payload.Type = definition.Type
|
|
}
|
|
if err := ValidateArtifactPayload(payload, definition.Type); err != nil {
|
|
return nil, nil, fmt.Errorf("workflow input %q: %w", definition.ID, err)
|
|
}
|
|
if ValidPortPayloadMode(definition.PayloadMode) && payload.Mode != definition.PayloadMode {
|
|
return nil, nil, fmt.Errorf("workflow input %q: payload mode %s must match %s", definition.ID, payload.Mode, definition.PayloadMode)
|
|
}
|
|
resolvedInputs[definition.ID] = payload
|
|
for _, target := range definition.Bindings {
|
|
bindings = append(bindings, ResolvedInputBinding{
|
|
WorkflowInputID: definition.ID,
|
|
NodeID: target.NodeID,
|
|
TargetPortID: target.TargetPortID,
|
|
Payload: payload,
|
|
})
|
|
}
|
|
}
|
|
return resolvedInputs, bindings, nil
|
|
}
|