Some checks failed
CI/CD / detect-changes (pull_request) Successful in 4s
CI/CD / test-frontend (pull_request) Successful in 5s
CI/CD / test-go-pg (pull_request) Failing after 1m56s
CI/CD / test-sqlite (pull_request) Successful in 2m47s
CI/CD / build-and-deploy (pull_request) Has been skipped
Drop chat-era columns (persona_id, history_mode) from workflow_stages. Rename transition_rules → stage_config. Add audience, stage_type, starlark_hook, branch_rules columns. Update stage_mode CHECK to (form, review, delegated, automated). All Go stores, handlers, routing engine, Starlark module, and frontend editors updated. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
82 lines
3.3 KiB
Go
82 lines
3.3 KiB
Go
package sandbox
|
|
|
|
// workflow_module.go — v0.30.2 CS1
|
|
//
|
|
// The workflow module lets extensions read workflow definitions and
|
|
// stage data, and programmatically advance or reject workflow stages.
|
|
//
|
|
// Starlark API:
|
|
// wf = workflow.get_definition(workflow_id) # returns dict
|
|
// data = workflow.get_stage_data(channel_id) # returns dict
|
|
// workflow.advance(channel_id) # advance to next stage
|
|
// workflow.reject(channel_id, reason) # reject current stage
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"go.starlark.net/starlark"
|
|
"go.starlark.net/starlarkstruct"
|
|
|
|
"switchboard-core/store"
|
|
)
|
|
|
|
// BuildWorkflowModule creates the "workflow" Starlark module for a package.
|
|
// Requires the workflow.access permission.
|
|
func BuildWorkflowModule(ctx context.Context, stores store.Stores) *starlarkstruct.Module {
|
|
return MakeModule("workflow", starlark.StringDict{
|
|
"get_definition": starlark.NewBuiltin("workflow.get_definition", workflowGetDef(ctx, stores)),
|
|
})
|
|
}
|
|
|
|
func workflowGetDef(ctx context.Context, stores store.Stores) func(*starlark.Thread, *starlark.Builtin, starlark.Tuple, []starlark.Tuple) (starlark.Value, error) {
|
|
return func(_ *starlark.Thread, _ *starlark.Builtin, args starlark.Tuple, kwargs []starlark.Tuple) (starlark.Value, error) {
|
|
var workflowID string
|
|
if err := starlark.UnpackPositionalArgs("workflow.get_definition", args, kwargs, 1, &workflowID); err != nil {
|
|
return nil, err
|
|
}
|
|
wf, err := stores.Workflows.GetByID(ctx, workflowID)
|
|
if err != nil {
|
|
return starlark.None, fmt.Errorf("workflow.get_definition: %w", err)
|
|
}
|
|
|
|
stages, _ := stores.Workflows.ListStages(ctx, workflowID)
|
|
|
|
// Build stages list
|
|
stageList := make([]starlark.Value, 0, len(stages))
|
|
for _, s := range stages {
|
|
d := starlark.NewDict(8)
|
|
d.SetKey(starlark.String("id"), starlark.String(s.ID))
|
|
d.SetKey(starlark.String("name"), starlark.String(s.Name))
|
|
d.SetKey(starlark.String("ordinal"), starlark.MakeInt(s.Ordinal))
|
|
d.SetKey(starlark.String("stage_mode"), starlark.String(s.StageMode))
|
|
d.SetKey(starlark.String("audience"), starlark.String(s.Audience))
|
|
d.SetKey(starlark.String("stage_type"), starlark.String(s.StageType))
|
|
d.SetKey(starlark.String("auto_transition"), starlark.Bool(s.AutoTransition))
|
|
if s.StarlarkHook != nil {
|
|
d.SetKey(starlark.String("starlark_hook"), starlark.String(*s.StarlarkHook))
|
|
}
|
|
if s.AssignmentTeamID != nil {
|
|
d.SetKey(starlark.String("assignment_team_id"), starlark.String(*s.AssignmentTeamID))
|
|
}
|
|
if s.SurfacePkgID != nil {
|
|
d.SetKey(starlark.String("surface_pkg_id"), starlark.String(*s.SurfacePkgID))
|
|
}
|
|
stageList = append(stageList, d)
|
|
}
|
|
|
|
result := starlark.NewDict(8)
|
|
result.SetKey(starlark.String("id"), starlark.String(wf.ID))
|
|
result.SetKey(starlark.String("name"), starlark.String(wf.Name))
|
|
result.SetKey(starlark.String("slug"), starlark.String(wf.Slug))
|
|
result.SetKey(starlark.String("entry_mode"), starlark.String(wf.EntryMode))
|
|
result.SetKey(starlark.String("is_active"), starlark.Bool(wf.IsActive))
|
|
result.SetKey(starlark.String("version"), starlark.MakeInt(wf.Version))
|
|
result.SetKey(starlark.String("stages"), starlark.NewList(stageList))
|
|
return result, nil
|
|
}
|
|
}
|
|
|
|
// workflowRoute routes the workflow to a named stage (v0.35.0).
|
|
// Starlark: workflow.route(channel_id, target_stage, reason)
|