Files
sam ce70d4f3ee feat(runs): workflow documents take today's step shape, references and when (#2039)
A step is id/type/settings with when and on_failure; the five kinds become
the types agent.run (inline mission), connector.action, approval.wait,
run.notify and workflow.sleep. Documents declare inputs, repository,
instructions, identity and profile; references are {{ task|inputs|steps }}
checked at save. Any number of approval steps, each asking only about what
was held since; actions still held at the end are put to people before the
run finishes. Triggers are refused for a workflow they can't start, and the
workflows API lists, reads and saves documents beside a step-type catalog.

Copied from internal/domain/workflow: definition.go, step_refs.go,
step_flow.go's checks, control_steps.go's when parser, vocabulary.go and
task/interface.go. stableid moves to internal/platform.
2026-11-17 18:43:45 +11:00

60 lines
1.3 KiB
Go

package runs
import (
"errors"
"strings"
)
// condition is a step's when: a reference that must be truthy, optionally
// negated with !, or compared with == or != to a literal.
type condition struct {
path string
negate bool
operator string
operand string
}
func parseCondition(source string) (condition, error) {
source = strings.TrimSpace(source)
for _, operator := range []string{"==", "!="} {
if left, right, ok := strings.Cut(source, operator); ok {
return condition{path: strings.TrimSpace(left), operator: operator, operand: strings.Trim(strings.TrimSpace(right), `"'`)}, nil
}
}
c := condition{path: source}
if rest, ok := strings.CutPrefix(source, "!"); ok {
c = condition{path: strings.TrimSpace(rest), negate: true}
}
if c.path == "" {
return condition{}, errors.New("when names nothing")
}
return c, nil
}
func (c condition) holds(values map[string]any) bool {
value := lookupPath(values, c.path)
switch c.operator {
case "==":
return stringify(value) == c.operand
case "!=":
return stringify(value) != c.operand
}
return truthy(value) != c.negate
}
func truthy(value any) bool {
switch v := value.(type) {
case nil:
return false
case bool:
return v
case string:
return v != "" && v != "false"
case float64:
return v != 0
case int:
return v != 0
}
return true
}