feat(workflows): iterate on a workflow's draft with Archie from the canvas

This commit is contained in:
sam committed 2026-10-18 02:27:41 +11:00
1 parent ee8c418cb2
commit 7725cf1c1f
9 files changed
+330 -87

No files matched your search

+45 -6
View File
@@ -19,6 +19,8 @@ type WorkflowSource interface {
Workflows(ctx context.Context) (task.WorkflowDefinitionCollection, error)
StepTypes(ctx context.Context) (json.RawMessage, error)
Validate(ctx context.Context, collection task.WorkflowDefinitionCollection) (problem string, err error)
Draft(ctx context.Context, id string) (yaml string, ok bool, err error)
PutDraft(ctx context.Context, id, yaml string) error
}
// WorkflowSummary is one workflow as workflow_list shows it.
@@ -35,8 +37,9 @@ type WorkflowValidation struct {
Problem string `json:"problem,omitempty"`
}
// WorkflowTools returns the read and validate tools over source. Saving is
// left to the operator in the workflow editor. Nil source registers none.
// WorkflowTools returns the workflow tools over source. The model writes only
// drafts; publishing is left to the operator in the workflow editor. Nil
// source registers none.
func WorkflowTools(source WorkflowSource) []tools.ToolEntry {
if source == nil {
return nil
@@ -46,6 +49,7 @@ func WorkflowTools(source WorkflowSource) []tools.ToolEntry {
workflowGetTool(source),
workflowStepTypesTool(source),
workflowValidateTool(source),
workflowDraftTool(source),
}
}
@@ -74,7 +78,7 @@ func workflowGetTool(source WorkflowSource) tools.ToolEntry {
return tools.ToolEntry{
Name: "workflow_get",
Toolset: "workflows",
Description: "Return one workflow's YAML definition. Read a similar workflow before writing a new one.",
Description: "Return one workflow's YAML: its draft when the operator has one open, else the published definition. Read a similar workflow before writing a new one.",
Classification: tools.ClassIdempotent,
Schema: tools.JSONSchema{
"type": "object",
@@ -83,6 +87,13 @@ func workflowGetTool(source WorkflowSource) tools.ToolEntry {
},
Handler: func(ctx context.Context, input map[string]any) (any, error) {
id := strings.TrimSpace(asString(input["id"]))
draft, ok, err := source.Draft(ctx, id)
if err != nil {
return nil, fmt.Errorf("workflow_get: %w", err)
}
if ok {
return map[string]any{"id": id, "yaml": draft, "draft": true}, nil
}
collection, err := source.Workflows(ctx)
if err != nil {
return nil, fmt.Errorf("workflow_get: %w", err)
@@ -91,7 +102,7 @@ func workflowGetTool(source WorkflowSource) tools.ToolEntry {
if !ok {
return nil, fmt.Errorf("workflow_get: no workflow %q", id)
}
return map[string]string{"id": entry.ID, "yaml": entry.YAML}, nil
return map[string]any{"id": entry.ID, "yaml": entry.YAML, "draft": false}, nil
},
}
}
@@ -118,8 +129,7 @@ func workflowValidateTool(source WorkflowSource) tools.ToolEntry {
Name: "workflow_validate",
Toolset: "workflows",
Description: "Check a workflow YAML definition exactly as saving it would, without saving. " +
"Fix every problem it reports and validate again before giving the operator the YAML, " +
"who saves it from the workflow editor.",
"Fix every problem it reports and validate again before writing the draft.",
Classification: tools.ClassIdempotent,
Schema: tools.JSONSchema{
"type": "object",
@@ -146,6 +156,35 @@ func workflowValidateTool(source WorkflowSource) tools.ToolEntry {
}
}
func workflowDraftTool(source WorkflowSource) tools.ToolEntry {
return tools.ToolEntry{
Name: "workflow_draft",
Toolset: "workflows",
Description: "Write the complete YAML as a workflow's draft. The operator sees it in the workflow editor, " +
"keeps or reverts each change, and publishes it; nothing runs from a draft. " +
"Validate first. id is the workflow being edited, even when the YAML renames it.",
Classification: tools.ClassMutating,
Schema: tools.JSONSchema{
"type": "object",
"properties": map[string]any{
"id": map[string]any{"type": "string", "description": "The workflow being edited."},
"yaml": map[string]any{"type": "string", "description": "The complete workflow definition."},
},
"required": []any{"id", "yaml"},
},
Handler: func(ctx context.Context, input map[string]any) (any, error) {
id := strings.TrimSpace(asString(input["id"]))
if id == "" {
return nil, fmt.Errorf("workflow_draft: id is required")
}
if err := source.PutDraft(ctx, id, asString(input["yaml"])); err != nil {
return nil, fmt.Errorf("workflow_draft: %w", err)
}
return map[string]any{"id": id, "saved": true}, nil
},
}
}
// withDefinition returns collection with src as workflow id, replacing an
// existing definition of that id or appending a new one.
func withDefinition(collection task.WorkflowDefinitionCollection, id, src string) (task.WorkflowDefinitionCollection, bool) {
@@ -70,3 +70,61 @@ func (w Workflows) Validate(ctx context.Context, collection task.WorkflowDefinit
}
return "", nil
}
const workflowDraftsKind = "workflow-drafts"
type workflowDrafts struct {
Drafts []task.WorkflowDefinitionEntry `json:"drafts"`
}
func (w Workflows) drafts(ctx context.Context) (workflowDrafts, int64, error) {
var drafts workflowDrafts
response, err := w.rpc.Query(ctx, &pb.QueryRequest{Kind: workflowDraftsKind})
if err != nil {
return drafts, 0, fmt.Errorf("%w: %w", ErrUnavailable, err)
}
resource := response.GetResource()
if resource == nil || len(resource.GetValueJson()) == 0 {
return drafts, resource.GetVersion(), nil
}
if err := json.Unmarshal(resource.GetValueJson(), &drafts); err != nil {
return drafts, 0, fmt.Errorf("decode workflow drafts: %w", err)
}
return drafts, resource.GetVersion(), nil
}
// Draft returns a workflow's unpublished YAML, and false when it has none.
func (w Workflows) Draft(ctx context.Context, id string) (string, bool, error) {
drafts, _, err := w.drafts(ctx)
for _, draft := range drafts.Drafts {
if draft.ID == id {
return draft.YAML, true, err
}
}
return "", false, err
}
// PutDraft writes a workflow's draft, the same document the editor saves to,
// so the operator reviews it there before publishing.
func (w Workflows) PutDraft(ctx context.Context, id, src string) error {
drafts, version, err := w.drafts(ctx)
if err != nil {
return err
}
next := workflowDrafts{Drafts: make([]task.WorkflowDefinitionEntry, 0, len(drafts.Drafts)+1)}
for _, draft := range drafts.Drafts {
if draft.ID != id {
next.Drafts = append(next.Drafts, draft)
}
}
next.Drafts = append(next.Drafts, task.WorkflowDefinitionEntry{ID: id, YAML: src})
value, err := json.Marshal(next)
if err != nil {
return err
}
_, err = w.rpc.Command(ctx, &pb.CommandRequest{Kind: workflowDraftsKind, Command: "replace", ValueJson: value, ExpectedVersion: version, Source: "chat"})
if err != nil {
return fmt.Errorf("save workflow draft: %w", err)
}
return nil
}