Files
archie/internal/infrastructure/controlplanerpc/workflows.go
T
sam c5158edb6c feat(gateway): chat reads workflows and validates drafts
workflow_list, workflow_get and workflow_step_types read the org's
definitions and step catalog; workflow_validate runs the editor's save
check on a proposed definition without saving it.
2026-10-16 03:46:27 +11:00

73 lines
2.4 KiB
Go

package controlplanerpc
import (
"context"
"encoding/json"
"errors"
"fmt"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
pb "github.com/samcharles93/archie-core/internal/contracts/controlplane/v1"
"github.com/samcharles93/archie-core/internal/domain/workflow/task"
)
const workflowDefinitionsKind = "workflow-definitions"
// Workflows reads and validates workflow definitions in the caller's org,
// through the same control-plane calls the workflow editor makes.
type Workflows struct{ rpc pb.ControlPlaneServiceClient }
func NewWorkflows(rpc pb.ControlPlaneServiceClient) Workflows { return Workflows{rpc: rpc} }
func (w Workflows) Workflows(ctx context.Context) (task.WorkflowDefinitionCollection, error) {
var collection task.WorkflowDefinitionCollection
response, err := w.rpc.Query(ctx, &pb.QueryRequest{Kind: workflowDefinitionsKind})
if err != nil {
return collection, fmt.Errorf("%w: %w", ErrUnavailable, err)
}
if response.GetResource() == nil || len(response.GetResource().GetValueJson()) == 0 {
return collection, nil
}
if err := json.Unmarshal(response.GetResource().GetValueJson(), &collection); err != nil {
return collection, fmt.Errorf("decode workflow definitions: %w", err)
}
return collection, nil
}
func (w Workflows) StepTypes(ctx context.Context) (json.RawMessage, error) {
catalog, err := w.rpc.Catalog(ctx, &pb.CatalogRequest{})
if err != nil {
return nil, fmt.Errorf("%w: %w", ErrUnavailable, err)
}
for _, resource := range catalog.GetResources() {
if resource.GetKind() != workflowDefinitionsKind {
continue
}
var schema struct {
StepTypes json.RawMessage `json:"x-step-types"`
}
if err := json.Unmarshal([]byte(resource.GetSchemaJson()), &schema); err != nil {
return nil, fmt.Errorf("decode workflow schema: %w", err)
}
return schema.StepTypes, nil
}
return nil, errors.New("the control plane serves no workflow definitions")
}
func (w Workflows) Validate(ctx context.Context, collection task.WorkflowDefinitionCollection) (string, error) {
value, err := json.Marshal(collection)
if err != nil {
return "", err
}
_, err = w.rpc.Command(ctx, &pb.CommandRequest{Kind: workflowDefinitionsKind, Command: "validate", ValueJson: value})
if status.Code(err) == codes.InvalidArgument {
return status.Convert(err).Message(), nil
}
if err != nil {
return "", fmt.Errorf("%w: %w", ErrUnavailable, err)
}
return "", nil
}