-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflows.go
More file actions
1227 lines (990 loc) · 42.1 KB
/
Copy pathworkflows.go
File metadata and controls
1227 lines (990 loc) · 42.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2026 AxonFlow
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package axonflow
import (
"context"
"fmt"
"net/url"
"strconv"
"time"
)
// WorkflowStatus represents the status of a workflow.
type WorkflowStatus string
const (
WorkflowStatusInProgress WorkflowStatus = "in_progress"
WorkflowStatusCompleted WorkflowStatus = "completed"
WorkflowStatusAborted WorkflowStatus = "aborted"
WorkflowStatusFailed WorkflowStatus = "failed"
)
// IsTerminal returns true if the workflow status is terminal (completed, aborted, or failed).
func (s WorkflowStatus) IsTerminal() bool {
return s == WorkflowStatusCompleted || s == WorkflowStatusAborted || s == WorkflowStatusFailed
}
// WorkflowSource represents the source orchestrator running the workflow.
type WorkflowSource string
const (
WorkflowSourceLangGraph WorkflowSource = "langgraph"
WorkflowSourceLangChain WorkflowSource = "langchain"
WorkflowSourceCrewAI WorkflowSource = "crewai"
WorkflowSourceExternal WorkflowSource = "external"
)
// GateDecision represents the decision returned by a step gate check.
type GateDecision string
const (
GateDecisionAllow GateDecision = "allow"
GateDecisionBlock GateDecision = "block"
GateDecisionRequireApproval GateDecision = "require_approval"
)
// IsAllowed returns true if the gate decision allows the step to proceed.
func (d GateDecision) IsAllowed() bool {
return d == GateDecisionAllow
}
// IsBlocked returns true if the gate decision blocks the step.
func (d GateDecision) IsBlocked() bool {
return d == GateDecisionBlock
}
// RequiresApproval returns true if the gate decision requires human approval.
func (d GateDecision) RequiresApproval() bool {
return d == GateDecisionRequireApproval
}
// ApprovalStatus represents the approval status for steps requiring human approval.
type ApprovalStatus string
const (
ApprovalStatusPending ApprovalStatus = "pending"
ApprovalStatusApproved ApprovalStatus = "approved"
ApprovalStatusRejected ApprovalStatus = "rejected"
)
// StepType indicates what kind of operation the step performs.
type StepType string
const (
StepTypeLLMCall StepType = "llm_call"
StepTypeToolCall StepType = "tool_call"
StepTypeConnectorCall StepType = "connector_call"
StepTypeHumanTask StepType = "human_task"
)
// ToolContext provides tool-level context for per-tool governance within tool_call steps.
type ToolContext struct {
ToolName string `json:"tool_name"`
ToolType string `json:"tool_type,omitempty"`
ToolInput map[string]interface{} `json:"tool_input,omitempty"`
}
// CreateWorkflowRequest is the request to create a new workflow.
type CreateWorkflowRequest struct {
// WorkflowName is the human-readable name for the workflow (required)
WorkflowName string `json:"workflow_name"`
// Source is the orchestrator running the workflow (optional)
Source WorkflowSource `json:"source,omitempty"`
// Metadata contains additional key-value metadata for the workflow (optional)
Metadata map[string]interface{} `json:"metadata,omitempty"`
// TraceID is an optional trace ID for correlating workflows with external tracing systems
TraceID string `json:"trace_id,omitempty"`
}
// CreateWorkflowResponse is the response from creating a workflow.
type CreateWorkflowResponse struct {
// WorkflowID is the unique identifier for the workflow
WorkflowID string `json:"workflow_id"`
// WorkflowName is the name of the workflow
WorkflowName string `json:"workflow_name"`
// Status is the current status (always "in_progress" for new workflows)
Status WorkflowStatus `json:"status"`
// StartedAt is when the workflow started executing (canonical wire field).
StartedAt time.Time `json:"started_at"`
// TraceID is the trace ID for correlating with external tracing systems
TraceID string `json:"trace_id,omitempty"`
// Deprecated: the wire emits started_at, not created_at. This field
// has always read zero against JSON-decoded server responses. Use
// StartedAt. Removed in v6.
CreatedAt time.Time `json:"created_at,omitempty"`
// Deprecated: not emitted on the create response. The wire shape
// for CreateWorkflowResponse does not include `source`; this
// field has always read empty. Read Source from a subsequent
// GetWorkflow() call instead. Removed in v6.
Source WorkflowSource `json:"source,omitempty"`
}
// RetryPolicy controls how step gate decisions behave on repeated calls for the
// same (workflow_id, step_id) pair.
type RetryPolicy string
const (
// RetryPolicyIdempotent returns the cached decision if the step was already evaluated.
// This is the default behavior when RetryPolicy is empty.
RetryPolicyIdempotent RetryPolicy = "idempotent"
// RetryPolicyReevaluate forces a fresh policy evaluation even if the step was
// previously evaluated. Use when external state has changed.
RetryPolicyReevaluate RetryPolicy = "reevaluate"
)
// StepGateRequest is the request to check if a step is allowed to proceed.
type StepGateRequest struct {
// StepName is the human-readable name for the step (optional)
StepName string `json:"step_name,omitempty"`
// StepType is the type of step being executed (required)
StepType StepType `json:"step_type"`
// StepInput contains input data for the step, used for policy evaluation (optional)
StepInput map[string]interface{} `json:"step_input,omitempty"`
// Model is the LLM model being used, if applicable (optional)
Model string `json:"model,omitempty"`
// Provider is the LLM provider, if applicable (optional)
Provider string `json:"provider,omitempty"`
// ToolContext provides tool-level context for per-tool governance within tool_call steps (optional)
ToolContext *ToolContext `json:"tool_context,omitempty"`
// RetryPolicy controls behavior on repeated calls for the same (workflow_id, step_id).
// Default (empty or "idempotent"): return cached decision. "reevaluate": force fresh evaluation.
RetryPolicy RetryPolicy `json:"retry_policy,omitempty"`
// IdempotencyKey is a caller-supplied opaque business-level key (max 255 chars).
// Once set on the first gate call for a (workflow_id, step_id), it is immutable —
// subsequent gate/complete calls must pass the same key or receive IdempotencyKeyMismatchError.
// The key is echoed on RetryContext.IdempotencyKey in every subsequent gate response.
IdempotencyKey string `json:"idempotency_key,omitempty"`
// TokensIn is the estimated input tokens for the step at gate time
// (used by budget-based policies).
TokensIn int `json:"tokens_in,omitempty"`
// TokensOut is the estimated output tokens for the step at gate time.
TokensOut int `json:"tokens_out,omitempty"`
// CostUSD is the estimated cost in USD for the step at gate time.
CostUSD float64 `json:"cost_usd,omitempty"`
}
// StepGateOptions controls gate-call behavior that lives outside the request body.
type StepGateOptions struct {
// IncludePriorOutput opts into populating RetryContext.PriorOutput on the
// response when a prior /complete exists. Default false because prior output
// may be large and/or contain sensitive data. Sent as ?include_prior_output=true.
IncludePriorOutput bool
}
// StepGateResponse is the response from a step gate check.
type StepGateResponse struct {
// Decision is the gate decision: allow, block, or require_approval
Decision GateDecision `json:"decision"`
// StepID is the unique step ID assigned by the system
StepID string `json:"step_id"`
// Reason explains the decision (especially for block/require_approval)
Reason string `json:"reason,omitempty"`
// PolicyIDs contains IDs of policies that matched and influenced the decision
PolicyIDs []string `json:"policy_ids,omitempty"`
// ApprovalURL is the URL to the approval portal (if decision is require_approval)
ApprovalURL string `json:"approval_url,omitempty"`
// DecisionID is the unique audit correlator for this gate decision.
DecisionID string `json:"decision_id,omitempty"`
// PoliciesEvaluated contains all policies that were evaluated for this step (Issue #1021)
PoliciesEvaluated []PolicyMatch `json:"policies_evaluated,omitempty"`
// PoliciesMatched contains policies that matched and influenced the decision (Issue #1021)
PoliciesMatched []PolicyMatch `json:"policies_matched,omitempty"`
// Cached indicates whether this response was served from a prior decision
// rather than a fresh policy evaluation.
//
// Deprecated: use RetryContext.GateCount > 1 instead. Will be removed in a future major version.
Cached bool `json:"cached"`
// DecisionSource indicates how the decision was produced: "fresh" or "cached".
//
// Deprecated: use RetryContext.PriorCompletionStatus instead. Will be removed in a future major version.
DecisionSource string `json:"decision_source"`
// RetryContext is the first-class state signal for (workflow_id, step_id). Always
// present on every gate response, including the first call. See RetryContext for
// field semantics.
RetryContext RetryContext `json:"retry_context"`
}
// IsAllowed returns true if the step is allowed to proceed.
func (r *StepGateResponse) IsAllowed() bool {
return r.Decision.IsAllowed()
}
// IsBlocked returns true if the step is blocked.
func (r *StepGateResponse) IsBlocked() bool {
return r.Decision.IsBlocked()
}
// RequiresApproval returns true if the step requires human approval.
func (r *StepGateResponse) RequiresApproval() bool {
return r.Decision.RequiresApproval()
}
// WorkflowStepInfo contains information about a workflow step.
type WorkflowStepInfo struct {
// StepID is the unique step identifier
StepID string `json:"step_id"`
// StepIndex is the step index in the workflow (0-based)
StepIndex int `json:"step_index"`
// StepName is the step name
StepName string `json:"step_name,omitempty"`
// StepType is the step type
StepType StepType `json:"step_type"`
// Decision is the gate decision for this step
Decision GateDecision `json:"decision"`
// DecisionReason explains the decision
DecisionReason string `json:"decision_reason,omitempty"`
// ApprovalStatus is the approval status (if require_approval decision)
ApprovalStatus ApprovalStatus `json:"approval_status,omitempty"`
// ApprovedBy indicates who approved the step (if approved)
ApprovedBy string `json:"approved_by,omitempty"`
// GateCheckedAt is when the gate was checked
GateCheckedAt time.Time `json:"gate_checked_at"`
// CompletedAt is when the step was completed
CompletedAt *time.Time `json:"completed_at,omitempty"`
}
// WorkflowStatusResponse contains the status of a workflow.
type WorkflowStatusResponse struct {
// WorkflowID is the workflow ID
WorkflowID string `json:"workflow_id"`
// WorkflowName is the workflow name
WorkflowName string `json:"workflow_name"`
// Source is the source orchestrator
Source WorkflowSource `json:"source"`
// Status is the current status
Status WorkflowStatus `json:"status"`
// CurrentStepIndex is the current step index (0-based)
CurrentStepIndex int `json:"current_step_index"`
// TotalSteps is the total steps in the workflow
TotalSteps int `json:"total_steps,omitempty"`
// StartedAt is when the workflow started
StartedAt time.Time `json:"started_at"`
// CompletedAt is when the workflow completed (if completed)
CompletedAt *time.Time `json:"completed_at,omitempty"`
// TraceID is the trace ID for correlating with external tracing systems
TraceID string `json:"trace_id,omitempty"`
// Metadata is arbitrary workflow metadata, opaque to the platform.
Metadata map[string]interface{} `json:"metadata,omitempty"`
// Steps contains the list of steps in the workflow
Steps []WorkflowStepInfo `json:"steps,omitempty"`
}
// IsTerminal returns true if the workflow is in a terminal state.
func (r *WorkflowStatusResponse) IsTerminal() bool {
return r.Status.IsTerminal()
}
// ListWorkflowsOptions contains options for listing workflows.
type ListWorkflowsOptions struct {
// Status filters by workflow status
Status WorkflowStatus
// Source filters by source orchestrator
Source WorkflowSource
// Limit is the maximum number of results to return (default 50, max 100)
Limit int
// Offset is the offset for pagination
Offset int
// TraceID filters by trace ID
TraceID string
}
// ListWorkflowsResponse is the response from listing workflows.
type ListWorkflowsResponse struct {
// Workflows is the list of workflows
Workflows []WorkflowStatusResponse `json:"workflows"`
// Total is the total count for pagination
Total int `json:"total"`
// Limit echoes the limit query parameter (for pagination clients).
Limit int `json:"limit,omitempty"`
// Offset echoes the offset query parameter (for pagination clients).
Offset int `json:"offset,omitempty"`
}
// AbortWorkflowRequest is the request to abort a workflow.
type AbortWorkflowRequest struct {
// Reason is the reason for aborting the workflow
Reason string `json:"reason,omitempty"`
}
// FailWorkflowRequest is the request to fail a workflow.
type FailWorkflowRequest struct {
// Reason is the reason for failing the workflow
Reason string `json:"reason,omitempty"`
}
// MarkStepCompletedRequest is the request to mark a step as completed.
type MarkStepCompletedRequest struct {
// Output is the output of the completed step
Output map[string]interface{} `json:"output,omitempty"`
// TokensIn is the number of input tokens consumed by this step
TokensIn *int `json:"tokens_in,omitempty"`
// TokensOut is the number of output tokens produced by this step
TokensOut *int `json:"tokens_out,omitempty"`
// CostUSD is the estimated cost in USD for this step's LLM usage
CostUSD *float64 `json:"cost_usd,omitempty"`
// IdempotencyKey must match the key passed on the corresponding gate call, if any.
// Mismatch (including empty vs set on either side) yields IdempotencyKeyMismatchError.
IdempotencyKey string `json:"idempotency_key,omitempty"`
}
// CreateWorkflow creates a new workflow for governance tracking.
//
// Call this at the start of your external orchestrator workflow (LangChain, LangGraph, CrewAI, etc.)
// to register it with AxonFlow for governance tracking.
//
// Example:
//
// workflow, err := client.CreateWorkflow(CreateWorkflowRequest{
// WorkflowName: "customer-support-agent",
// Source: WorkflowSourceLangGraph,
// Metadata: map[string]interface{}{"customer_id": "cust-123"},
// })
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Workflow created: %s\n", workflow.WorkflowID)
func (c *AxonFlowClient) CreateWorkflow(req CreateWorkflowRequest) (*CreateWorkflowResponse, error) {
fullURL := c.config.Endpoint + "/api/v1/workflows"
var result CreateWorkflowResponse
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, req, &result); err != nil {
return nil, fmt.Errorf("failed to create workflow: %w", err)
}
return &result, nil
}
// GetWorkflow retrieves the status of a workflow.
//
// Example:
//
// status, err := client.GetWorkflow("wf_123")
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Status: %s, Current Step: %d\n", status.Status, status.CurrentStepIndex)
func (c *AxonFlowClient) GetWorkflow(workflowID string) (*WorkflowStatusResponse, error) {
if workflowID == "" {
return nil, fmt.Errorf("workflow ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s", c.config.Endpoint, workflowID)
var result WorkflowStatusResponse
if err := c.makeJSONRequest(context.Background(), "GET", fullURL, nil, &result); err != nil {
return nil, fmt.Errorf("failed to get workflow: %w", err)
}
return &result, nil
}
// StepGate checks if a workflow step is allowed to proceed.
//
// This is the core governance method. Call this before executing each step
// in your workflow to check if the step is allowed based on policies.
//
// Example:
//
// gate, err := client.StepGate("wf_123", "step-generate-code", StepGateRequest{
// StepName: "Generate Code",
// StepType: StepTypeLLMCall,
// Model: "gpt-4",
// Provider: "openai",
// StepInput: map[string]interface{}{"prompt": "Generate a hello world function"},
// })
// if err != nil {
// log.Fatal(err)
// }
//
// if gate.IsBlocked() {
// log.Fatalf("Step blocked: %s", gate.Reason)
// }
// if gate.RequiresApproval() {
// fmt.Printf("Approval required: %s\n", gate.ApprovalURL)
// return
// }
// // Step is allowed, proceed with execution
func (c *AxonFlowClient) StepGate(workflowID, stepID string, req StepGateRequest) (*StepGateResponse, error) {
return c.StepGateWithOptions(workflowID, stepID, req, StepGateOptions{})
}
// StepGateWithOptions is StepGate with explicit call-level options (e.g. IncludePriorOutput).
//
// Use this variant when you need RetryContext.PriorOutput populated for retry/replay flows.
// The opts.IncludePriorOutput flag is sent as the ?include_prior_output=true query param.
//
// Example:
//
// gate, err := client.StepGateWithOptions("wf_123", "step-1",
// StepGateRequest{StepType: StepTypeLLMCall, IdempotencyKey: "payment:wire:acct4471:invoice-7721"},
// StepGateOptions{IncludePriorOutput: true})
// if err != nil {
// var idemErr *IdempotencyKeyMismatchError
// if errors.As(err, &idemErr) {
// // expected_idempotency_key / received_idempotency_key available on idemErr
// }
// return err
// }
// if gate.RetryContext.PriorCompletionStatus == PriorCompletionStatusCompleted {
// // prior result is in gate.RetryContext.PriorOutput
// }
func (c *AxonFlowClient) StepGateWithOptions(workflowID, stepID string, req StepGateRequest, opts StepGateOptions) (*StepGateResponse, error) {
if workflowID == "" {
return nil, fmt.Errorf("workflow ID is required")
}
if stepID == "" {
return nil, fmt.Errorf("step ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/steps/%s/gate", c.config.Endpoint, workflowID, stepID)
if opts.IncludePriorOutput {
fullURL += "?include_prior_output=true"
}
var result StepGateResponse
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, req, &result); err != nil {
if idemErr := parseIdempotencyKeyMismatch(err); idemErr != nil {
return nil, idemErr
}
return nil, fmt.Errorf("failed to check step gate: %w", err)
}
return &result, nil
}
// CompleteWorkflow marks a workflow as completed successfully.
//
// Call this when your workflow has completed all steps successfully.
//
// Example:
//
// err := client.CompleteWorkflow("wf_123")
// if err != nil {
// log.Fatal(err)
// }
func (c *AxonFlowClient) CompleteWorkflow(workflowID string) error {
if workflowID == "" {
return fmt.Errorf("workflow ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/complete", c.config.Endpoint, workflowID)
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, struct{}{}, nil); err != nil {
return fmt.Errorf("failed to complete workflow: %w", err)
}
return nil
}
// AbortWorkflow aborts a workflow with an optional reason.
//
// Call this when you need to stop a workflow due to an error or user request.
//
// Example:
//
// err := client.AbortWorkflow("wf_123", "User cancelled the operation")
// if err != nil {
// log.Fatal(err)
// }
func (c *AxonFlowClient) AbortWorkflow(workflowID string, reason string) error {
if workflowID == "" {
return fmt.Errorf("workflow ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/abort", c.config.Endpoint, workflowID)
req := AbortWorkflowRequest{Reason: reason}
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, req, nil); err != nil {
return fmt.Errorf("failed to abort workflow: %w", err)
}
return nil
}
// FailWorkflow fails a workflow with a reason.
//
// Call this when a workflow encounters an unrecoverable error and cannot continue.
//
// Example:
//
// err := client.FailWorkflow("wf_123", "Step 3 encountered an unrecoverable error")
// if err != nil {
// log.Fatal(err)
// }
func (c *AxonFlowClient) FailWorkflow(workflowID string, reason string) error {
if workflowID == "" {
return fmt.Errorf("workflow ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/fail", c.config.Endpoint, workflowID)
req := FailWorkflowRequest{Reason: reason}
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, req, nil); err != nil {
return fmt.Errorf("failed to fail workflow: %w", err)
}
return nil
}
// ResumeWorkflow resumes a workflow after approval.
//
// Call this after a step has been approved to continue the workflow.
//
// Example:
//
// // After approval received via webhook or polling
// err := client.ResumeWorkflow("wf_123")
// if err != nil {
// log.Fatal(err)
// }
func (c *AxonFlowClient) ResumeWorkflow(workflowID string) error {
if workflowID == "" {
return fmt.Errorf("workflow ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/resume", c.config.Endpoint, workflowID)
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, struct{}{}, nil); err != nil {
return fmt.Errorf("failed to resume workflow: %w", err)
}
return nil
}
// GetCheckpoints returns all step-gate checkpoints for a workflow.
//
// Checkpoints are created automatically at each step gate evaluation and capture
// the decision and policy context. Available in all tiers.
//
// Example:
//
// resp, err := client.GetCheckpoints("wf_123")
// for _, cp := range resp.Checkpoints {
// fmt.Printf("Step %s: %s (resumable=%v)\n", cp.StepID, cp.GateDecision, cp.IsResumable)
// }
func (c *AxonFlowClient) GetCheckpoints(workflowID string) (*CheckpointListResponse, error) {
if workflowID == "" {
return nil, fmt.Errorf("workflow ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/checkpoints", c.config.Endpoint, workflowID)
var resp CheckpointListResponse
if err := c.makeJSONRequest(context.Background(), "GET", fullURL, nil, &resp); err != nil {
return nil, fmt.Errorf("failed to get checkpoints: %w", err)
}
return &resp, nil
}
// ResumeFromLastCheckpoint resumes a workflow from its last resumable checkpoint
// with fresh policy evaluation. Evaluation+ tier.
//
// Example:
//
// resp, err := client.ResumeFromLastCheckpoint("wf_123")
// fmt.Printf("Resumed from %s, new decision: %s\n", resp.ResumedFromCheckpoint, resp.NewDecision)
func (c *AxonFlowClient) ResumeFromLastCheckpoint(workflowID string) (*ResumeFromCheckpointResponse, error) {
if workflowID == "" {
return nil, fmt.Errorf("workflow ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/checkpoints/resume", c.config.Endpoint, workflowID)
var resp ResumeFromCheckpointResponse
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, struct{}{}, &resp); err != nil {
return nil, fmt.Errorf("failed to resume from last checkpoint: %w", err)
}
return &resp, nil
}
// ResumeFromCheckpoint resumes a workflow from a specific checkpoint with fresh
// policy evaluation. Enterprise only.
//
// The step gate at the checkpoint boundary is re-evaluated with current policies,
// so any policy changes since the checkpoint was created are reflected.
//
// Example:
//
// resp, err := client.ResumeFromCheckpoint("wf_123", 42)
// fmt.Printf("Resumed from %s, new decision: %s\n", resp.ResumedFromCheckpoint, resp.NewDecision)
func (c *AxonFlowClient) ResumeFromCheckpoint(workflowID string, checkpointID int64) (*ResumeFromCheckpointResponse, error) {
if workflowID == "" {
return nil, fmt.Errorf("workflow ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/checkpoints/%d/resume", c.config.Endpoint, workflowID, checkpointID)
var resp ResumeFromCheckpointResponse
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, struct{}{}, &resp); err != nil {
return nil, fmt.Errorf("failed to resume from checkpoint: %w", err)
}
return &resp, nil
}
// MarkStepCompleted marks a workflow step as completed.
//
// Call this after a step has been executed successfully.
//
// Example:
//
// err := client.MarkStepCompleted("wf_123", "step-1", &MarkStepCompletedRequest{
// Output: map[string]interface{}{"result": "success"},
// })
// if err != nil {
// log.Fatal(err)
// }
func (c *AxonFlowClient) MarkStepCompleted(workflowID, stepID string, req *MarkStepCompletedRequest) error {
if workflowID == "" {
return fmt.Errorf("workflow ID is required")
}
if stepID == "" {
return fmt.Errorf("step ID is required")
}
fullURL := fmt.Sprintf("%s/api/v1/workflows/%s/steps/%s/complete", c.config.Endpoint, workflowID, stepID)
var payload interface{} = struct{}{}
if req != nil {
payload = req
}
if err := c.makeJSONRequest(context.Background(), "POST", fullURL, payload, nil); err != nil {
if idemErr := parseIdempotencyKeyMismatch(err); idemErr != nil {
return idemErr
}
return fmt.Errorf("failed to mark step completed: %w", err)
}
return nil
}
// ListWorkflows lists workflows with optional filters.
//
// Example:
//
// result, err := client.ListWorkflows(&ListWorkflowsOptions{
// Status: WorkflowStatusInProgress,
// Source: WorkflowSourceLangGraph,
// Limit: 10,
// })
// if err != nil {
// log.Fatal(err)
// }
// fmt.Printf("Found %d workflows\n", result.Total)
func (c *AxonFlowClient) ListWorkflows(opts *ListWorkflowsOptions) (*ListWorkflowsResponse, error) {
params := url.Values{}
if opts != nil {
if opts.Status != "" {
params.Set("status", string(opts.Status))
}
if opts.Source != "" {
params.Set("source", string(opts.Source))
}
if opts.Limit > 0 {
params.Set("limit", strconv.Itoa(opts.Limit))
}
if opts.Offset > 0 {
params.Set("offset", strconv.Itoa(opts.Offset))
}
if opts.TraceID != "" {
params.Set("trace_id", opts.TraceID)
}
}
fullURL := c.config.Endpoint + "/api/v1/workflows"
if len(params) > 0 {
fullURL += "?" + params.Encode()
}
var result ListWorkflowsResponse
if err := c.makeJSONRequest(context.Background(), "GET", fullURL, nil, &result); err != nil {
return nil, fmt.Errorf("failed to list workflows: %w", err)
}
return &result, nil
}
// ============================================================================
// WCP Approval Types (Feature 5)
// ============================================================================
// --- Checkpoint Types ---
// Checkpoint represents a governance-aware resume boundary at a step-gate evaluation.
type Checkpoint struct {
// ID is the database identifier for this checkpoint
ID int64 `json:"id"`
// WorkflowID is the workflow this checkpoint belongs to
WorkflowID string `json:"workflow_id"`
// StepID is the step this checkpoint was created at
StepID string `json:"step_id"`
// StepIndex is the position of the step in the workflow
StepIndex int `json:"step_index"`
// StepType is the type of step (llm_call, tool_call, etc.)
StepType string `json:"step_type,omitempty"`
// CheckpointType classifies the checkpoint: "step_gate" or "approval_boundary"
CheckpointType string `json:"checkpoint_type"`
// GateDecision is the decision recorded at this checkpoint
GateDecision string `json:"gate_decision"`
// GateReason explains the decision
GateReason string `json:"gate_reason,omitempty"`
// IsResumable indicates whether the workflow can be resumed from this checkpoint
IsResumable bool `json:"is_resumable"`
// ResumeCount is how many times the workflow has been resumed from this checkpoint
ResumeCount int `json:"resume_count"`
// CreatedAt is when the checkpoint was created
CreatedAt string `json:"created_at"`
}
// CheckpointListResponse is the response from listing checkpoints.
type CheckpointListResponse struct {
// Checkpoints is the ordered list of checkpoints for the workflow
Checkpoints []Checkpoint `json:"checkpoints"`
// WorkflowID is the workflow the checkpoints belong to
WorkflowID string `json:"workflow_id"`
}
// ResumeFromCheckpointResponse is the response after resuming from a checkpoint.
type ResumeFromCheckpointResponse struct {
// WorkflowID is the workflow that was resumed
WorkflowID string `json:"workflow_id"`
// ResumedFromCheckpoint is the step_id of the checkpoint that was resumed from
ResumedFromCheckpoint string `json:"resumed_from_checkpoint"`
// ResumedFromIndex is the step_index of the checkpoint
ResumedFromIndex int `json:"resumed_from_index"`
// NewDecision is the fresh policy decision after re-evaluation
NewDecision string `json:"new_decision"`
// DecisionSource is always "fresh" since resume forces re-evaluation
DecisionSource string `json:"decision_source"`
// ResumeCount is the updated resume count for this checkpoint
ResumeCount int `json:"resume_count"`
// Message is a human-readable summary
Message string `json:"message"`
}
// ApproveStepRequest is the request to approve a workflow step.
//
// The server requires `comment` with a minimum of 10 characters — it's the
// audit-trail justification that every approval carries into the workflow
// history. Callers SHOULD always supply a meaningful comment; the SDK will
// pass it through unchanged.
type ApproveStepRequest struct {
// ApprovedBy identifies who approved the step (optional; defaults to
// the X-User-ID header on the server side if omitted).
ApprovedBy string `json:"approved_by,omitempty"`
// Comment is the audit justification for the approval. Required by the
// server (min 10 chars).
Comment string `json:"comment,omitempty"`
}
// ApproveStepResponse is the response from approving a workflow step.
//
// Starting with v5.6.0 the server returns the same rich shape as the step-gate
// response — decision resolves to "allow" once approved, retry_context carries
// the first-class state signal, approved_by / approved_at track the reviewer,
// and policies_matched reconstructs the governance trail. The legacy
// workflow_id / step_id / status fields remain for back-compat (status mirrors
// approval_status so older code keeps working).
//
// See ADR-046 (HITL response parity) for why the same shape is returned by
// both the WCP endpoint and the MAP plan-scoped equivalent, and ADR-045 for
// the retry_context wire contract.
type ApproveStepResponse struct {
// WorkflowID is the workflow containing the step
WorkflowID string `json:"workflow_id"`
// PlanID is the MAP plan id on MAP-plane responses. Empty on WCP-plane
// responses.
PlanID string `json:"plan_id,omitempty"`
// StepID is the step that was approved
StepID string `json:"step_id"`
// Status is the new status of the step after approval (approved/rejected/pending).
// Mirrors ApprovalStatus — retained for v5.x back-compat.
Status string `json:"status,omitempty"`
// Decision resolves to "allow" on a successful approval — the agent that
// re-calls /gate will see the step cleared.
Decision string `json:"decision,omitempty"`
// Reason is the decision reason text. On the approve path, prefixed with
// "Approved: ".
Reason string `json:"reason,omitempty"`
// ApprovalStatus is the terminal approval status: pending / approved / rejected.
ApprovalStatus string `json:"approval_status,omitempty"`
// ApprovalID is the deterministic HITL queue entry UUID.
ApprovalID string `json:"approval_id,omitempty"`
// ApprovedBy is the identity that approved the step.
ApprovedBy string `json:"approved_by,omitempty"`
// ApprovedAt is when the approval was persisted (RFC3339).
ApprovedAt string `json:"approved_at,omitempty"`
// PoliciesMatched are the policies that triggered the original require_approval decision.
PoliciesMatched []PolicyMatch `json:"policies_matched,omitempty"`
// RetryContext mirrors the gate response retry_context block.
RetryContext RetryContext `json:"retry_context"`
// Message is a human-readable status summary.
Message string `json:"message,omitempty"`
}
// RejectStepRequest is the request to reject a workflow step.
//
// The server requires `reason` with a minimum of 10 characters — it's the
// audit justification for the rejection. Callers SHOULD always supply a
// meaningful reason.
type RejectStepRequest struct {
// Reason explains why the step was rejected. Required by the server
// (min 10 chars).
Reason string `json:"reason,omitempty"`
// RejectedBy identifies who rejected the step (optional; defaults to
// X-User-ID header server-side if omitted).
RejectedBy string `json:"rejected_by,omitempty"`
}
// RejectStepResponse is the response from rejecting a workflow step.
//
// Starting with v5.6.0 the server returns the same rich shape as ApproveStepResponse
// with rejected_by / rejected_at populated instead of approved_by / approved_at.
// See ADR-046.
type RejectStepResponse struct {
WorkflowID string `json:"workflow_id"`
// PlanID is the MAP plan id on MAP-plane responses. Empty on WCP-plane responses.
PlanID string `json:"plan_id,omitempty"`
StepID string `json:"step_id"`
// Status mirrors ApprovalStatus (legacy back-compat field).
Status string `json:"status,omitempty"`
// Decision resolves to "block" on a successful rejection; the workflow is aborted.
Decision string `json:"decision,omitempty"`
// Reason is the decision reason text, prefixed with "Rejected: " on the reject path.
Reason string `json:"reason,omitempty"`
// ApprovalStatus is the terminal approval status.
ApprovalStatus string `json:"approval_status,omitempty"`
// ApprovalID is the deterministic HITL queue entry UUID.
ApprovalID string `json:"approval_id,omitempty"`
// RejectedBy is the identity that rejected the step.
RejectedBy string `json:"rejected_by,omitempty"`
// RejectedAt is when the rejection was persisted (RFC3339).
RejectedAt string `json:"rejected_at,omitempty"`
// PoliciesMatched are the policies that triggered the require_approval decision.
PoliciesMatched []PolicyMatch `json:"policies_matched,omitempty"`
// RetryContext mirrors the gate response retry_context block.
RetryContext RetryContext `json:"retry_context"`
// Message is a human-readable status summary.
Message string `json:"message,omitempty"`
}
// PendingApproval represents a workflow step awaiting human approval.
//
// Populated by both `GetPendingApprovals` (WCP plane) and
// `GetPendingPlanApprovals` (MAP plane). The `PlanID` field is the intentional
// asymmetry between the two planes — populated on MAP-plane entries, empty on
// WCP-plane entries. Mirrors the server-side ADR-046 parity rule.
type PendingApproval struct {
// WorkflowID is the workflow containing the pending step
WorkflowID string `json:"workflow_id"`