package runs import ( "context" "slices" "gopkg.in/yaml.v3" ) // modelReference is the part of a workflow body that names a model: an agent // step's settings. type modelReference struct { Steps []struct { Type string `yaml:"type"` Settings struct { Model string `yaml:"model"` } `yaml:"settings"` } `yaml:"steps"` } // defaultModelAlias is what a step that names no model uses. It is also the // name the operator sees for it, so the two spell the same model. const defaultModelAlias = "default" // sameModelAlias reports whether two names mean one model. func sameModelAlias(a, b string) bool { norm := func(s string) string { if s == "" { return defaultModelAlias } return s } return norm(a) == norm(b) } // namesModel reports whether a workflow body gives an agent step the named // model. A body that isn't a workflow names nothing. func namesModel(body, alias string) bool { var doc modelReference if err := yaml.Unmarshal([]byte(body), &doc); err != nil { return false } for _, step := range doc.Steps { if step.Type == typeAgent && sameModelAlias(step.Settings.Model, alias) { return true } } return false } // WorkflowsUsingModel returns the names of the saved workflows that give an // agent step the named model, in name order. The caller is a setting that // would otherwise make them fail at their agent step. func (e *Engine) WorkflowsUsingModel(ctx context.Context, orgID, alias string) ([]string, error) { values, err := e.db.Workflows(ctx, orgID) if err != nil { return nil, err } var out []string for _, w := range values { if namesModel(w.Body, alias) { out = append(out, w.Name) } } slices.Sort(out) return out, nil }