@contractspec/lib.contracts 0.0.0-canary-20260119225944 → 0.0.0-canary-20260202053150
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/app-config/{contracts.d.ts → app-config.contracts.d.ts} +52 -52
- package/dist/app-config/{contracts.js → app-config.contracts.js} +1 -1
- package/dist/app-config/lifecycle-contracts.d.ts +55 -55
- package/dist/app-config/runtime.d.ts +4 -4
- package/dist/app-config/spec.d.ts +2 -2
- package/dist/capabilities/capabilities.d.ts +64 -5
- package/dist/capabilities/capabilities.js +125 -0
- package/dist/capabilities/context.d.ts +88 -0
- package/dist/capabilities/context.js +87 -0
- package/dist/capabilities/docs/capabilities.docblock.js +191 -2
- package/dist/capabilities/guards.d.ts +110 -0
- package/dist/capabilities/guards.js +146 -0
- package/dist/capabilities/index.d.ts +4 -1
- package/dist/capabilities/index.js +4 -1
- package/dist/capabilities/validation.d.ts +76 -0
- package/dist/capabilities/validation.js +141 -0
- package/dist/client/react/feature-render.d.ts +2 -2
- package/dist/data-views/data-views.d.ts +2 -1
- package/dist/data-views/index.d.ts +2 -1
- package/dist/data-views/registry.d.ts +1 -1
- package/dist/data-views/report/contractVerificationTable.d.ts +10 -0
- package/dist/data-views/report/contractVerificationTable.js +95 -0
- package/dist/data-views/runtime.d.ts +2 -2
- package/dist/data-views/spec.d.ts +2 -8
- package/dist/data-views/types.d.ts +1 -1
- package/dist/docs/capabilities/documentationSystem.capability.d.ts +7 -0
- package/dist/docs/capabilities/documentationSystem.capability.js +71 -0
- package/dist/docs/capabilities/index.d.ts +2 -0
- package/dist/docs/capabilities/index.js +3 -0
- package/dist/docs/commands/docsGenerate.command.d.ts +95 -0
- package/dist/docs/commands/docsGenerate.command.js +142 -0
- package/dist/docs/commands/docsPublish.command.d.ts +64 -0
- package/dist/docs/commands/docsPublish.command.js +107 -0
- package/dist/docs/commands/index.d.ts +3 -0
- package/dist/docs/commands/index.js +4 -0
- package/dist/docs/constants.d.ts +7 -0
- package/dist/docs/constants.js +10 -0
- package/dist/docs/contracts.d.ts +442 -0
- package/dist/docs/contracts.js +58 -0
- package/dist/docs/ensure-docblocks.d.ts +1 -0
- package/dist/docs/ensure-docblocks.js +1 -0
- package/dist/docs/events/docsGenerated.event.d.ts +62 -0
- package/dist/docs/events/docsGenerated.event.js +53 -0
- package/dist/docs/events/docsPublished.event.d.ts +70 -0
- package/dist/docs/events/docsPublished.event.js +57 -0
- package/dist/docs/events/index.d.ts +3 -0
- package/dist/docs/events/index.js +4 -0
- package/dist/docs/forms/docsSearch.form.d.ts +22 -0
- package/dist/docs/forms/docsSearch.form.js +113 -0
- package/dist/docs/forms/index.d.ts +2 -0
- package/dist/docs/forms/index.js +3 -0
- package/dist/docs/index.d.ts +23 -2
- package/dist/docs/index.js +25 -1
- package/dist/docs/presentations/docsLayout.presentation.d.ts +7 -0
- package/dist/docs/presentations/docsLayout.presentation.js +35 -0
- package/dist/docs/presentations/docsReferencePage.presentation.d.ts +7 -0
- package/dist/docs/presentations/docsReferencePage.presentation.js +35 -0
- package/dist/docs/presentations/index.d.ts +3 -0
- package/dist/docs/presentations/index.js +4 -0
- package/dist/docs/queries/contractReference.query.d.ts +217 -0
- package/dist/docs/queries/contractReference.query.js +125 -0
- package/dist/docs/queries/docsIndex.query.d.ts +272 -0
- package/dist/docs/queries/docsIndex.query.js +133 -0
- package/dist/docs/queries/index.d.ts +3 -0
- package/dist/docs/queries/index.js +4 -0
- package/dist/docs/tech/cli.docblock.js +10 -0
- package/dist/docs/tech/docs-system.docblock.d.ts +1 -0
- package/dist/docs/tech/docs-system.docblock.js +128 -0
- package/dist/docs/tech/report-verification-table.docblock.d.ts +1 -0
- package/dist/docs/tech/report-verification-table.docblock.js +50 -0
- package/dist/docs/views/contractReference.dataView.d.ts +7 -0
- package/dist/docs/views/contractReference.dataView.js +80 -0
- package/dist/docs/views/docsIndex.dataView.d.ts +7 -0
- package/dist/docs/views/docsIndex.dataView.js +136 -0
- package/dist/docs/views/exampleCatalog.dataView.d.ts +7 -0
- package/dist/docs/views/exampleCatalog.dataView.js +91 -0
- package/dist/docs/views/index.d.ts +4 -0
- package/dist/docs/views/index.js +5 -0
- package/dist/events.d.ts +80 -14
- package/dist/events.js +33 -3
- package/dist/examples/schema.d.ts +12 -12
- package/dist/examples/types.d.ts +1 -1
- package/dist/experiments/spec.d.ts +9 -6
- package/dist/features/index.d.ts +2 -2
- package/dist/features/install.d.ts +4 -4
- package/dist/features/types.d.ts +28 -32
- package/dist/forms/forms.d.ts +1 -1
- package/dist/index.d.ts +60 -31
- package/dist/index.js +32 -4
- package/dist/install.d.ts +1 -1
- package/dist/integrations/openbanking/contracts/accounts.d.ts +67 -67
- package/dist/integrations/openbanking/contracts/balances.d.ts +35 -35
- package/dist/integrations/openbanking/contracts/transactions.d.ts +49 -49
- package/dist/integrations/openbanking/models.d.ts +55 -55
- package/dist/integrations/operations.d.ts +103 -103
- package/dist/integrations/spec.d.ts +2 -2
- package/dist/jsonschema.d.ts +1 -1
- package/dist/knowledge/operations.d.ts +67 -67
- package/dist/knowledge/spec.d.ts +1 -1
- package/dist/llm/exporters.d.ts +4 -4
- package/dist/llm/types.d.ts +1 -1
- package/dist/markdown.d.ts +2 -2
- package/dist/operations/index.d.ts +4 -1
- package/dist/operations/index.js +4 -1
- package/dist/operations/operation.d.ts +8 -2
- package/dist/operations/registry.d.ts +1 -1
- package/dist/operations/report/getContractVerificationStatus.d.ts +75 -0
- package/dist/operations/report/getContractVerificationStatus.js +96 -0
- package/dist/operations/report/index.d.ts +13 -0
- package/dist/operations/report/index.js +45 -0
- package/dist/ownership.d.ts +133 -8
- package/dist/ownership.js +25 -0
- package/dist/policy/context.d.ts +237 -0
- package/dist/policy/context.js +227 -0
- package/dist/policy/guards.d.ts +145 -0
- package/dist/policy/guards.js +254 -0
- package/dist/policy/index.d.ts +12 -1
- package/dist/policy/index.js +11 -1
- package/dist/policy/spec.d.ts +7 -4
- package/dist/policy/validation.d.ts +67 -0
- package/dist/policy/validation.js +307 -0
- package/dist/presentations/presentations.d.ts +6 -0
- package/dist/presentations/registry.d.ts +1 -1
- package/dist/registry.d.ts +1 -1
- package/dist/serialization/index.d.ts +3 -0
- package/dist/serialization/index.js +3 -0
- package/dist/serialization/serializers.d.ts +40 -0
- package/dist/serialization/serializers.js +148 -0
- package/dist/serialization/types.d.ts +103 -0
- package/dist/serialization/types.js +0 -0
- package/dist/server/rest-elysia.d.ts +1 -1
- package/dist/server/rest-express.d.ts +1 -1
- package/dist/server/rest-generic.d.ts +1 -1
- package/dist/server/rest-next-app.d.ts +1 -1
- package/dist/server/rest-next-mcp.d.ts +1 -1
- package/dist/server/rest-next-pages.d.ts +1 -1
- package/dist/telemetry/spec.d.ts +1 -1
- package/dist/telemetry/tracker.d.ts +3 -2
- package/dist/tests/runner.d.ts +1 -1
- package/dist/tests/spec.d.ts +17 -12
- package/dist/themes.d.ts +8 -5
- package/dist/translations/index.d.ts +6 -0
- package/dist/translations/index.js +5 -0
- package/dist/translations/registry.d.ts +144 -0
- package/dist/translations/registry.js +223 -0
- package/dist/translations/spec.d.ts +126 -0
- package/dist/translations/spec.js +31 -0
- package/dist/translations/validation.d.ts +85 -0
- package/dist/translations/validation.js +328 -0
- package/dist/types.d.ts +142 -16
- package/dist/versioning/index.d.ts +2 -1
- package/dist/versioning/index.js +2 -1
- package/dist/versioning/refs.d.ts +179 -0
- package/dist/versioning/refs.js +161 -0
- package/dist/workflow/context.d.ts +191 -0
- package/dist/workflow/context.js +227 -0
- package/dist/workflow/index.d.ts +6 -3
- package/dist/workflow/index.js +4 -2
- package/dist/workflow/spec.d.ts +4 -11
- package/dist/workflow/validation.d.ts +64 -2
- package/dist/workflow/validation.js +194 -1
- package/dist/workspace-config/contractsrc-schema.d.ts +3 -3
- package/dist/workspace-config/contractsrc-schema.js +1 -0
- package/dist/workspace-config/contractsrc-types.d.ts +7 -8
- package/dist/workspace-config/index.d.ts +2 -2
- package/package.json +52 -9
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
import { Step, WorkflowSpec } from "./spec.js";
|
|
2
|
+
import { StepExecution, WorkflowState } from "./state.js";
|
|
3
|
+
|
|
4
|
+
//#region src/workflow/context.d.ts
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Error thrown when a workflow operation fails.
|
|
8
|
+
*/
|
|
9
|
+
declare class WorkflowContextError extends Error {
|
|
10
|
+
readonly errorType: WorkflowContextErrorType;
|
|
11
|
+
readonly workflowId: string;
|
|
12
|
+
readonly details?: Record<string, unknown>;
|
|
13
|
+
constructor(type: WorkflowContextErrorType, workflowId: string, message: string, details?: Record<string, unknown>);
|
|
14
|
+
}
|
|
15
|
+
type WorkflowContextErrorType = 'invalid_transition' | 'workflow_completed' | 'workflow_failed' | 'step_not_found' | 'guard_rejected' | 'sla_violated';
|
|
16
|
+
interface WorkflowTransitionResult {
|
|
17
|
+
success: boolean;
|
|
18
|
+
previousStep: string;
|
|
19
|
+
currentStep: string;
|
|
20
|
+
status: WorkflowState['status'];
|
|
21
|
+
error?: string;
|
|
22
|
+
}
|
|
23
|
+
interface AvailableTransition {
|
|
24
|
+
from: string;
|
|
25
|
+
to: string;
|
|
26
|
+
label?: string;
|
|
27
|
+
condition?: string;
|
|
28
|
+
}
|
|
29
|
+
interface SlaStatus {
|
|
30
|
+
key: string;
|
|
31
|
+
type: 'workflow' | 'step';
|
|
32
|
+
stepId?: string;
|
|
33
|
+
limitMs: number;
|
|
34
|
+
elapsedMs: number;
|
|
35
|
+
remainingMs: number;
|
|
36
|
+
violated: boolean;
|
|
37
|
+
percentUsed: number;
|
|
38
|
+
}
|
|
39
|
+
interface WorkflowEvent {
|
|
40
|
+
timestamp: Date;
|
|
41
|
+
type: 'step_started' | 'step_completed' | 'step_failed' | 'step_retried' | 'transition';
|
|
42
|
+
stepId?: string;
|
|
43
|
+
fromStep?: string;
|
|
44
|
+
toStep?: string;
|
|
45
|
+
input?: unknown;
|
|
46
|
+
output?: unknown;
|
|
47
|
+
error?: string;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Runtime context for interacting with a workflow instance.
|
|
51
|
+
*
|
|
52
|
+
* Provides a simplified interface over WorkflowRunner for common
|
|
53
|
+
* workflow operations like state access, transitions, and SLA tracking.
|
|
54
|
+
*/
|
|
55
|
+
interface WorkflowContext<TData = Record<string, unknown>> {
|
|
56
|
+
/** Unique workflow instance identifier. */
|
|
57
|
+
readonly workflowId: string;
|
|
58
|
+
/** Workflow spec key. */
|
|
59
|
+
readonly workflowKey: string;
|
|
60
|
+
/** Workflow spec version. */
|
|
61
|
+
readonly workflowVersion: string;
|
|
62
|
+
/** Current step identifier. */
|
|
63
|
+
readonly currentStep: string;
|
|
64
|
+
/** Current workflow status. */
|
|
65
|
+
readonly status: WorkflowState['status'];
|
|
66
|
+
/** Workflow data/state. */
|
|
67
|
+
readonly data: TData;
|
|
68
|
+
/** Execution history. */
|
|
69
|
+
readonly history: readonly StepExecution[];
|
|
70
|
+
/**
|
|
71
|
+
* Get the current workflow state.
|
|
72
|
+
* @returns Full workflow state object
|
|
73
|
+
*/
|
|
74
|
+
getState(): WorkflowState;
|
|
75
|
+
/**
|
|
76
|
+
* Get a value from workflow data.
|
|
77
|
+
* @param key - Data key to retrieve
|
|
78
|
+
* @returns Value or undefined
|
|
79
|
+
*/
|
|
80
|
+
getData<T = unknown>(key: string): T | undefined;
|
|
81
|
+
/**
|
|
82
|
+
* Check if workflow is in a terminal state.
|
|
83
|
+
* @returns True if completed, failed, or cancelled
|
|
84
|
+
*/
|
|
85
|
+
isTerminal(): boolean;
|
|
86
|
+
/**
|
|
87
|
+
* Check if workflow is running.
|
|
88
|
+
* @returns True if status is 'running'
|
|
89
|
+
*/
|
|
90
|
+
isRunning(): boolean;
|
|
91
|
+
/**
|
|
92
|
+
* Get the current step definition.
|
|
93
|
+
* @returns Step definition or undefined
|
|
94
|
+
*/
|
|
95
|
+
getCurrentStepDef(): Step | undefined;
|
|
96
|
+
/**
|
|
97
|
+
* Get the retry count for a step.
|
|
98
|
+
* @param stepId - Step to check (defaults to current step)
|
|
99
|
+
* @returns Number of retry attempts
|
|
100
|
+
*/
|
|
101
|
+
getRetryCount(stepId?: string): number;
|
|
102
|
+
/**
|
|
103
|
+
* Check if a transition to a target step is valid.
|
|
104
|
+
* @param toStepId - Target step identifier
|
|
105
|
+
* @returns True if transition is valid from current step
|
|
106
|
+
*/
|
|
107
|
+
canTransition(toStepId: string): boolean;
|
|
108
|
+
/**
|
|
109
|
+
* Get all available transitions from current step.
|
|
110
|
+
* @returns Array of available transitions
|
|
111
|
+
*/
|
|
112
|
+
getAvailableTransitions(): AvailableTransition[];
|
|
113
|
+
/**
|
|
114
|
+
* Check if the current step has any outgoing transitions.
|
|
115
|
+
* @returns True if there are outgoing transitions
|
|
116
|
+
*/
|
|
117
|
+
hasNextStep(): boolean;
|
|
118
|
+
/**
|
|
119
|
+
* Get remaining time for an SLA constraint.
|
|
120
|
+
* @param slaKey - 'totalDuration' or step ID
|
|
121
|
+
* @returns Remaining milliseconds, or null if no SLA configured
|
|
122
|
+
*/
|
|
123
|
+
getRemainingTime(slaKey: string): number | null;
|
|
124
|
+
/**
|
|
125
|
+
* Check if an SLA constraint has been violated.
|
|
126
|
+
* @param slaKey - 'totalDuration' or step ID
|
|
127
|
+
* @returns True if SLA has been violated
|
|
128
|
+
*/
|
|
129
|
+
isSlaViolated(slaKey: string): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Get status of all SLA constraints.
|
|
132
|
+
* @returns Array of SLA statuses
|
|
133
|
+
*/
|
|
134
|
+
getAllSlaStatuses(): SlaStatus[];
|
|
135
|
+
/**
|
|
136
|
+
* Check if compensation/rollback is available.
|
|
137
|
+
* @returns True if workflow has compensation strategy
|
|
138
|
+
*/
|
|
139
|
+
hasCompensation(): boolean;
|
|
140
|
+
/**
|
|
141
|
+
* Get steps that have compensation handlers.
|
|
142
|
+
* @returns Array of step IDs with compensation
|
|
143
|
+
*/
|
|
144
|
+
getCompensableSteps(): string[];
|
|
145
|
+
/**
|
|
146
|
+
* Convert execution history to workflow events.
|
|
147
|
+
* @returns Array of workflow events
|
|
148
|
+
*/
|
|
149
|
+
getEvents(): WorkflowEvent[];
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Creates a workflow context from state and spec.
|
|
153
|
+
*
|
|
154
|
+
* @param state - Current workflow state
|
|
155
|
+
* @param spec - Workflow specification
|
|
156
|
+
* @returns WorkflowContext for interacting with the workflow
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const state = await runner.getState(workflowId);
|
|
161
|
+
* const spec = registry.get(state.workflowName, state.workflowVersion);
|
|
162
|
+
* const ctx = createWorkflowContext(state, spec);
|
|
163
|
+
*
|
|
164
|
+
* console.log('Current step:', ctx.currentStep);
|
|
165
|
+
* console.log('Available transitions:', ctx.getAvailableTransitions());
|
|
166
|
+
* ```
|
|
167
|
+
*/
|
|
168
|
+
declare function createWorkflowContext<TData = Record<string, unknown>>(state: WorkflowState, spec: WorkflowSpec): WorkflowContext<TData>;
|
|
169
|
+
/**
|
|
170
|
+
* Calculate workflow progress as a percentage.
|
|
171
|
+
*
|
|
172
|
+
* @param ctx - Workflow context
|
|
173
|
+
* @returns Progress percentage (0-100)
|
|
174
|
+
*/
|
|
175
|
+
declare function calculateWorkflowProgress(ctx: WorkflowContext): number;
|
|
176
|
+
/**
|
|
177
|
+
* Get the duration of a workflow in milliseconds.
|
|
178
|
+
*
|
|
179
|
+
* @param ctx - Workflow context
|
|
180
|
+
* @returns Duration in milliseconds
|
|
181
|
+
*/
|
|
182
|
+
declare function getWorkflowDuration(ctx: WorkflowContext): number;
|
|
183
|
+
/**
|
|
184
|
+
* Get the average step duration in milliseconds.
|
|
185
|
+
*
|
|
186
|
+
* @param ctx - Workflow context
|
|
187
|
+
* @returns Average duration or 0 if no completed steps
|
|
188
|
+
*/
|
|
189
|
+
declare function getAverageStepDuration(ctx: WorkflowContext): number;
|
|
190
|
+
//#endregion
|
|
191
|
+
export { AvailableTransition, SlaStatus, WorkflowContext, WorkflowContextError, WorkflowContextErrorType, WorkflowEvent, WorkflowTransitionResult, calculateWorkflowProgress, createWorkflowContext, getAverageStepDuration, getWorkflowDuration };
|
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
//#region src/workflow/context.ts
|
|
2
|
+
/**
|
|
3
|
+
* Error thrown when a workflow operation fails.
|
|
4
|
+
*/
|
|
5
|
+
var WorkflowContextError = class extends Error {
|
|
6
|
+
errorType;
|
|
7
|
+
workflowId;
|
|
8
|
+
details;
|
|
9
|
+
constructor(type, workflowId, message, details) {
|
|
10
|
+
super(message);
|
|
11
|
+
this.name = "WorkflowContextError";
|
|
12
|
+
this.errorType = type;
|
|
13
|
+
this.workflowId = workflowId;
|
|
14
|
+
this.details = details;
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var WorkflowContextImpl = class {
|
|
18
|
+
constructor(state, spec) {
|
|
19
|
+
this.state = state;
|
|
20
|
+
this.spec = spec;
|
|
21
|
+
}
|
|
22
|
+
get workflowId() {
|
|
23
|
+
return this.state.workflowId;
|
|
24
|
+
}
|
|
25
|
+
get workflowKey() {
|
|
26
|
+
return this.state.workflowName;
|
|
27
|
+
}
|
|
28
|
+
get workflowVersion() {
|
|
29
|
+
return this.state.workflowVersion;
|
|
30
|
+
}
|
|
31
|
+
get currentStep() {
|
|
32
|
+
return this.state.currentStep;
|
|
33
|
+
}
|
|
34
|
+
get status() {
|
|
35
|
+
return this.state.status;
|
|
36
|
+
}
|
|
37
|
+
get data() {
|
|
38
|
+
return this.state.data;
|
|
39
|
+
}
|
|
40
|
+
get history() {
|
|
41
|
+
return this.state.history;
|
|
42
|
+
}
|
|
43
|
+
getState() {
|
|
44
|
+
return this.state;
|
|
45
|
+
}
|
|
46
|
+
getData(key) {
|
|
47
|
+
return this.state.data[key];
|
|
48
|
+
}
|
|
49
|
+
isTerminal() {
|
|
50
|
+
return this.state.status === "completed" || this.state.status === "failed" || this.state.status === "cancelled";
|
|
51
|
+
}
|
|
52
|
+
isRunning() {
|
|
53
|
+
return this.state.status === "running";
|
|
54
|
+
}
|
|
55
|
+
getCurrentStepDef() {
|
|
56
|
+
return this.spec.definition.steps.find((s) => s.id === this.state.currentStep);
|
|
57
|
+
}
|
|
58
|
+
getRetryCount(stepId) {
|
|
59
|
+
const id = stepId ?? this.state.currentStep;
|
|
60
|
+
return this.state.retryCounts?.[id] ?? 0;
|
|
61
|
+
}
|
|
62
|
+
canTransition(toStepId) {
|
|
63
|
+
return this.spec.definition.transitions.filter((t) => t.from === this.state.currentStep && t.to === toStepId).length > 0;
|
|
64
|
+
}
|
|
65
|
+
getAvailableTransitions() {
|
|
66
|
+
return this.spec.definition.transitions.filter((t) => t.from === this.state.currentStep).map((t) => ({
|
|
67
|
+
from: t.from,
|
|
68
|
+
to: t.to,
|
|
69
|
+
label: t.label,
|
|
70
|
+
condition: t.condition
|
|
71
|
+
}));
|
|
72
|
+
}
|
|
73
|
+
hasNextStep() {
|
|
74
|
+
return this.spec.definition.transitions.some((t) => t.from === this.state.currentStep);
|
|
75
|
+
}
|
|
76
|
+
getRemainingTime(slaKey) {
|
|
77
|
+
const sla = this.spec.definition.sla;
|
|
78
|
+
if (!sla) return null;
|
|
79
|
+
const now = Date.now();
|
|
80
|
+
if (slaKey === "totalDuration") {
|
|
81
|
+
if (!sla.totalDurationMs) return null;
|
|
82
|
+
const elapsed$1 = now - this.state.createdAt.getTime();
|
|
83
|
+
return Math.max(0, sla.totalDurationMs - elapsed$1);
|
|
84
|
+
}
|
|
85
|
+
const stepLimit = sla.stepDurationMs?.[slaKey];
|
|
86
|
+
if (!stepLimit) return null;
|
|
87
|
+
const stepExecution = this.state.history.find((h) => h.stepId === slaKey && h.status === "running");
|
|
88
|
+
if (!stepExecution) return stepLimit;
|
|
89
|
+
const elapsed = now - stepExecution.startedAt.getTime();
|
|
90
|
+
return Math.max(0, stepLimit - elapsed);
|
|
91
|
+
}
|
|
92
|
+
isSlaViolated(slaKey) {
|
|
93
|
+
const remaining = this.getRemainingTime(slaKey);
|
|
94
|
+
return remaining !== null && remaining <= 0;
|
|
95
|
+
}
|
|
96
|
+
getAllSlaStatuses() {
|
|
97
|
+
const sla = this.spec.definition.sla;
|
|
98
|
+
if (!sla) return [];
|
|
99
|
+
const statuses = [];
|
|
100
|
+
const now = Date.now();
|
|
101
|
+
if (sla.totalDurationMs) {
|
|
102
|
+
const elapsed = now - this.state.createdAt.getTime();
|
|
103
|
+
const remaining = Math.max(0, sla.totalDurationMs - elapsed);
|
|
104
|
+
statuses.push({
|
|
105
|
+
key: "totalDuration",
|
|
106
|
+
type: "workflow",
|
|
107
|
+
limitMs: sla.totalDurationMs,
|
|
108
|
+
elapsedMs: elapsed,
|
|
109
|
+
remainingMs: remaining,
|
|
110
|
+
violated: remaining <= 0,
|
|
111
|
+
percentUsed: elapsed / sla.totalDurationMs * 100
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
if (sla.stepDurationMs) for (const [stepId, limitMs] of Object.entries(sla.stepDurationMs)) {
|
|
115
|
+
const stepExecution = this.state.history.find((h) => h.stepId === stepId && h.status === "running");
|
|
116
|
+
const elapsed = stepExecution ? now - stepExecution.startedAt.getTime() : 0;
|
|
117
|
+
const remaining = Math.max(0, limitMs - elapsed);
|
|
118
|
+
statuses.push({
|
|
119
|
+
key: stepId,
|
|
120
|
+
type: "step",
|
|
121
|
+
stepId,
|
|
122
|
+
limitMs,
|
|
123
|
+
elapsedMs: elapsed,
|
|
124
|
+
remainingMs: remaining,
|
|
125
|
+
violated: stepExecution !== void 0 && remaining <= 0,
|
|
126
|
+
percentUsed: elapsed / limitMs * 100
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
return statuses;
|
|
130
|
+
}
|
|
131
|
+
hasCompensation() {
|
|
132
|
+
return this.spec.definition.compensation !== void 0;
|
|
133
|
+
}
|
|
134
|
+
getCompensableSteps() {
|
|
135
|
+
const compensation = this.spec.definition.compensation;
|
|
136
|
+
if (!compensation) return [];
|
|
137
|
+
return compensation.steps.map((s) => s.stepId);
|
|
138
|
+
}
|
|
139
|
+
getEvents() {
|
|
140
|
+
const events = [];
|
|
141
|
+
for (const execution of this.state.history) {
|
|
142
|
+
events.push({
|
|
143
|
+
timestamp: execution.startedAt,
|
|
144
|
+
type: "step_started",
|
|
145
|
+
stepId: execution.stepId,
|
|
146
|
+
input: execution.input
|
|
147
|
+
});
|
|
148
|
+
if (execution.completedAt) {
|
|
149
|
+
if (execution.status === "completed") events.push({
|
|
150
|
+
timestamp: execution.completedAt,
|
|
151
|
+
type: "step_completed",
|
|
152
|
+
stepId: execution.stepId,
|
|
153
|
+
output: execution.output
|
|
154
|
+
});
|
|
155
|
+
else if (execution.status === "failed") events.push({
|
|
156
|
+
timestamp: execution.completedAt,
|
|
157
|
+
type: "step_failed",
|
|
158
|
+
stepId: execution.stepId,
|
|
159
|
+
error: execution.error
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
events.sort((a, b) => a.timestamp.getTime() - b.timestamp.getTime());
|
|
164
|
+
return events;
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Creates a workflow context from state and spec.
|
|
169
|
+
*
|
|
170
|
+
* @param state - Current workflow state
|
|
171
|
+
* @param spec - Workflow specification
|
|
172
|
+
* @returns WorkflowContext for interacting with the workflow
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```typescript
|
|
176
|
+
* const state = await runner.getState(workflowId);
|
|
177
|
+
* const spec = registry.get(state.workflowName, state.workflowVersion);
|
|
178
|
+
* const ctx = createWorkflowContext(state, spec);
|
|
179
|
+
*
|
|
180
|
+
* console.log('Current step:', ctx.currentStep);
|
|
181
|
+
* console.log('Available transitions:', ctx.getAvailableTransitions());
|
|
182
|
+
* ```
|
|
183
|
+
*/
|
|
184
|
+
function createWorkflowContext(state, spec) {
|
|
185
|
+
return new WorkflowContextImpl(state, spec);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Calculate workflow progress as a percentage.
|
|
189
|
+
*
|
|
190
|
+
* @param ctx - Workflow context
|
|
191
|
+
* @returns Progress percentage (0-100)
|
|
192
|
+
*/
|
|
193
|
+
function calculateWorkflowProgress(ctx) {
|
|
194
|
+
const state = ctx.getState();
|
|
195
|
+
const completedSteps = new Set(state.history.filter((h) => h.status === "completed").map((h) => h.stepId));
|
|
196
|
+
completedSteps.size + (ctx.isTerminal() ? 0 : 1);
|
|
197
|
+
if (ctx.status === "completed") return 100;
|
|
198
|
+
if (ctx.status === "failed" || ctx.status === "cancelled") return completedSteps.size > 0 ? Math.min(99, completedSteps.size * 20) : 0;
|
|
199
|
+
return Math.min(99, completedSteps.size * 20);
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Get the duration of a workflow in milliseconds.
|
|
203
|
+
*
|
|
204
|
+
* @param ctx - Workflow context
|
|
205
|
+
* @returns Duration in milliseconds
|
|
206
|
+
*/
|
|
207
|
+
function getWorkflowDuration(ctx) {
|
|
208
|
+
const state = ctx.getState();
|
|
209
|
+
return (ctx.isTerminal() ? state.updatedAt.getTime() : Date.now()) - state.createdAt.getTime();
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Get the average step duration in milliseconds.
|
|
213
|
+
*
|
|
214
|
+
* @param ctx - Workflow context
|
|
215
|
+
* @returns Average duration or 0 if no completed steps
|
|
216
|
+
*/
|
|
217
|
+
function getAverageStepDuration(ctx) {
|
|
218
|
+
const completedSteps = ctx.history.filter((h) => h.status === "completed" && h.completedAt);
|
|
219
|
+
if (completedSteps.length === 0) return 0;
|
|
220
|
+
return completedSteps.reduce((sum, step) => {
|
|
221
|
+
if (!step.completedAt) return sum;
|
|
222
|
+
return sum + (step.completedAt.getTime() - step.startedAt.getTime());
|
|
223
|
+
}, 0) / completedSteps.length;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
//#endregion
|
|
227
|
+
export { WorkflowContextError, calculateWorkflowProgress, createWorkflowContext, getAverageStepDuration, getWorkflowDuration };
|
package/dist/workflow/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { FormRef } from "../features/types.js";
|
|
2
|
+
import { CompensationStep, CompensationStrategy, GuardCondition, GuardConditionKind, RetryPolicy, SLA, Step, StepAction, StepType, Transition, WorkflowDefinition, WorkflowMeta, WorkflowRegistry, WorkflowSpec, WorkflowStatus } from "./spec.js";
|
|
3
|
+
import { ValidateWorkflowSpecOptions, WorkflowConsistencyDeps, WorkflowValidationError, WorkflowValidationIssue, WorkflowValidationLevel, WorkflowValidationResult, assertWorkflowConsistency, assertWorkflowSpecValid, validateCompensation, validateRetryConfig, validateSlaConfig, validateWorkflowComprehensive, validateWorkflowConsistency, validateWorkflowSpec } from "./validation.js";
|
|
3
4
|
import { StateStore, StepExecution, WorkflowState, WorkflowStateFilters } from "./state.js";
|
|
4
5
|
import { GuardContext, GuardEvaluator, OperationExecutor, OperationExecutorContext, WorkflowPreFlightError, WorkflowPreFlightIssue, WorkflowPreFlightIssueSeverity, WorkflowPreFlightIssueType, WorkflowPreFlightResult, WorkflowRunner, WorkflowRunnerConfig } from "./runner.js";
|
|
5
6
|
import { ExpressionContext, evaluateExpression } from "./expression.js";
|
|
@@ -7,6 +8,8 @@ import { InMemoryStateStore } from "./adapters/memory-store.js";
|
|
|
7
8
|
import { PrismaStateStore } from "./adapters/db-adapter.js";
|
|
8
9
|
import { FileStateStoreOptions, createFileStateStore } from "./adapters/file-adapter.js";
|
|
9
10
|
import "./adapters/index.js";
|
|
11
|
+
import { AvailableTransition, SlaStatus, WorkflowContext, WorkflowContextError, WorkflowContextErrorType, WorkflowEvent, WorkflowTransitionResult, calculateWorkflowProgress, createWorkflowContext, getAverageStepDuration, getWorkflowDuration } from "./context.js";
|
|
12
|
+
import { SLABreachEvent, SLAMonitor } from "./sla-monitor.js";
|
|
10
13
|
|
|
11
14
|
//#region src/workflow/index.d.ts
|
|
12
15
|
/**
|
|
@@ -14,4 +17,4 @@ import "./adapters/index.js";
|
|
|
14
17
|
*/
|
|
15
18
|
declare const defineWorkflow: (spec: WorkflowSpec) => WorkflowSpec;
|
|
16
19
|
//#endregion
|
|
17
|
-
export { CompensationStep, CompensationStrategy, ExpressionContext, FileStateStoreOptions, FormRef, GuardCondition, GuardConditionKind, GuardContext, GuardEvaluator, InMemoryStateStore, OperationExecutor, OperationExecutorContext, PrismaStateStore, RetryPolicy, SLA, StateStore, Step, StepAction, StepExecution, StepType, Transition, ValidateWorkflowSpecOptions, WorkflowDefinition, WorkflowMeta, WorkflowPreFlightError, WorkflowPreFlightIssue, WorkflowPreFlightIssueSeverity, WorkflowPreFlightIssueType, WorkflowPreFlightResult, WorkflowRegistry, WorkflowRunner, WorkflowRunnerConfig, WorkflowSpec, WorkflowState, WorkflowStateFilters, WorkflowStatus, WorkflowValidationError, WorkflowValidationIssue, WorkflowValidationLevel, assertWorkflowSpecValid, createFileStateStore, defineWorkflow, evaluateExpression, validateWorkflowSpec };
|
|
20
|
+
export { AvailableTransition, CompensationStep, CompensationStrategy, ExpressionContext, FileStateStoreOptions, FormRef, GuardCondition, GuardConditionKind, GuardContext, GuardEvaluator, InMemoryStateStore, OperationExecutor, OperationExecutorContext, PrismaStateStore, RetryPolicy, SLA, SLABreachEvent, SLAMonitor, SlaStatus, StateStore, Step, StepAction, StepExecution, StepType, Transition, ValidateWorkflowSpecOptions, WorkflowConsistencyDeps, WorkflowContext, WorkflowContextError, WorkflowContextErrorType, WorkflowDefinition, WorkflowEvent, WorkflowMeta, WorkflowPreFlightError, WorkflowPreFlightIssue, WorkflowPreFlightIssueSeverity, WorkflowPreFlightIssueType, WorkflowPreFlightResult, WorkflowRegistry, WorkflowRunner, WorkflowRunnerConfig, WorkflowSpec, WorkflowState, WorkflowStateFilters, WorkflowStatus, WorkflowTransitionResult, WorkflowValidationError, WorkflowValidationIssue, WorkflowValidationLevel, WorkflowValidationResult, assertWorkflowConsistency, assertWorkflowSpecValid, calculateWorkflowProgress, createFileStateStore, createWorkflowContext, defineWorkflow, evaluateExpression, getAverageStepDuration, getWorkflowDuration, validateCompensation, validateRetryConfig, validateSlaConfig, validateWorkflowComprehensive, validateWorkflowConsistency, validateWorkflowSpec };
|
package/dist/workflow/index.js
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import { WorkflowRegistry } from "./spec.js";
|
|
2
|
-
import { WorkflowValidationError, assertWorkflowSpecValid, validateWorkflowSpec } from "./validation.js";
|
|
2
|
+
import { WorkflowValidationError, assertWorkflowConsistency, assertWorkflowSpecValid, validateCompensation, validateRetryConfig, validateSlaConfig, validateWorkflowComprehensive, validateWorkflowConsistency, validateWorkflowSpec } from "./validation.js";
|
|
3
3
|
import { evaluateExpression } from "./expression.js";
|
|
4
4
|
import { WorkflowPreFlightError, WorkflowRunner } from "./runner.js";
|
|
5
5
|
import { InMemoryStateStore } from "./adapters/memory-store.js";
|
|
6
6
|
import { PrismaStateStore } from "./adapters/db-adapter.js";
|
|
7
7
|
import { createFileStateStore } from "./adapters/file-adapter.js";
|
|
8
|
+
import { WorkflowContextError, calculateWorkflowProgress, createWorkflowContext, getAverageStepDuration, getWorkflowDuration } from "./context.js";
|
|
9
|
+
import { SLAMonitor } from "./sla-monitor.js";
|
|
8
10
|
|
|
9
11
|
//#region src/workflow/index.ts
|
|
10
12
|
/**
|
|
@@ -13,4 +15,4 @@ import { createFileStateStore } from "./adapters/file-adapter.js";
|
|
|
13
15
|
const defineWorkflow = (spec) => spec;
|
|
14
16
|
|
|
15
17
|
//#endregion
|
|
16
|
-
export { InMemoryStateStore, PrismaStateStore, WorkflowPreFlightError, WorkflowRegistry, WorkflowRunner, WorkflowValidationError, assertWorkflowSpecValid, createFileStateStore, defineWorkflow, evaluateExpression, validateWorkflowSpec };
|
|
18
|
+
export { InMemoryStateStore, PrismaStateStore, SLAMonitor, WorkflowContextError, WorkflowPreFlightError, WorkflowRegistry, WorkflowRunner, WorkflowValidationError, assertWorkflowConsistency, assertWorkflowSpecValid, calculateWorkflowProgress, createFileStateStore, createWorkflowContext, defineWorkflow, evaluateExpression, getAverageStepDuration, getWorkflowDuration, validateCompensation, validateRetryConfig, validateSlaConfig, validateWorkflowComprehensive, validateWorkflowConsistency, validateWorkflowSpec };
|
package/dist/workflow/spec.d.ts
CHANGED
|
@@ -1,19 +1,12 @@
|
|
|
1
|
-
import { OwnerShipMeta } from "../ownership.js";
|
|
2
1
|
import { CapabilityRef } from "../capabilities/capabilities.js";
|
|
2
|
+
import { SpecContractRegistry } from "../registry.js";
|
|
3
3
|
import "../capabilities/index.js";
|
|
4
4
|
import { ExperimentRef } from "../experiments/spec.js";
|
|
5
|
-
import { OpRef } from "../features/types.js";
|
|
5
|
+
import { FormRef, OpRef } from "../features/types.js";
|
|
6
6
|
import "../features/index.js";
|
|
7
|
-
import {
|
|
7
|
+
import { OwnerShipMeta } from "../ownership.js";
|
|
8
8
|
|
|
9
9
|
//#region src/workflow/spec.d.ts
|
|
10
|
-
/**
|
|
11
|
-
* Reference to a form spec declared in {@link FormRegistry}.
|
|
12
|
-
*/
|
|
13
|
-
interface FormRef {
|
|
14
|
-
key: string;
|
|
15
|
-
version: string;
|
|
16
|
-
}
|
|
17
10
|
type StepType = 'human' | 'automation' | 'decision';
|
|
18
11
|
type WorkflowStatus = 'running' | 'paused' | 'completed' | 'failed' | 'cancelled';
|
|
19
12
|
type GuardConditionKind = 'policy' | 'expression';
|
|
@@ -90,4 +83,4 @@ declare class WorkflowRegistry extends SpecContractRegistry<'workflow', Workflow
|
|
|
90
83
|
constructor(items?: WorkflowSpec[]);
|
|
91
84
|
}
|
|
92
85
|
//#endregion
|
|
93
|
-
export { CompensationStep, CompensationStrategy, FormRef, GuardCondition, GuardConditionKind, RetryPolicy, SLA, Step, StepAction, StepType, Transition, WorkflowDefinition, WorkflowMeta, WorkflowRegistry, WorkflowSpec, WorkflowStatus };
|
|
86
|
+
export { CompensationStep, CompensationStrategy, type FormRef, GuardCondition, GuardConditionKind, RetryPolicy, SLA, Step, StepAction, StepType, Transition, WorkflowDefinition, WorkflowMeta, WorkflowRegistry, WorkflowSpec, WorkflowStatus };
|
|
@@ -1,7 +1,9 @@
|
|
|
1
|
+
import { CapabilityRegistry } from "../capabilities/capabilities.js";
|
|
2
|
+
import { WorkflowRegistry, WorkflowSpec } from "./spec.js";
|
|
1
3
|
import { OperationSpecRegistry } from "../operations/registry.js";
|
|
2
|
-
import { WorkflowSpec } from "./spec.js";
|
|
3
4
|
import { FormRegistry } from "../forms/forms.js";
|
|
4
5
|
import "../forms/index.js";
|
|
6
|
+
import { EventRegistry } from "../events.js";
|
|
5
7
|
|
|
6
8
|
//#region src/workflow/validation.d.ts
|
|
7
9
|
type WorkflowValidationLevel = 'error' | 'warning';
|
|
@@ -25,5 +27,65 @@ declare class WorkflowValidationError extends Error {
|
|
|
25
27
|
*/
|
|
26
28
|
declare function validateWorkflowSpec(spec: WorkflowSpec, options?: ValidateWorkflowSpecOptions): WorkflowValidationIssue[];
|
|
27
29
|
declare function assertWorkflowSpecValid(spec: WorkflowSpec, options?: ValidateWorkflowSpecOptions): void;
|
|
30
|
+
interface WorkflowConsistencyDeps {
|
|
31
|
+
workflows: WorkflowRegistry;
|
|
32
|
+
operations?: OperationSpecRegistry;
|
|
33
|
+
forms?: FormRegistry;
|
|
34
|
+
events?: EventRegistry;
|
|
35
|
+
capabilities?: CapabilityRegistry;
|
|
36
|
+
}
|
|
37
|
+
interface WorkflowValidationResult {
|
|
38
|
+
valid: boolean;
|
|
39
|
+
issues: WorkflowValidationIssue[];
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Validate workflow consistency across registries.
|
|
43
|
+
*
|
|
44
|
+
* Checks that:
|
|
45
|
+
* - All workflow specs are internally valid
|
|
46
|
+
* - Operations referenced by steps exist
|
|
47
|
+
* - Forms referenced by steps exist
|
|
48
|
+
* - Capabilities required by steps exist
|
|
49
|
+
*
|
|
50
|
+
* @param deps - Registry dependencies
|
|
51
|
+
* @returns Validation result
|
|
52
|
+
*/
|
|
53
|
+
declare function validateWorkflowConsistency(deps: WorkflowConsistencyDeps): WorkflowValidationResult;
|
|
54
|
+
/**
|
|
55
|
+
* Assert workflow consistency across registries.
|
|
56
|
+
*
|
|
57
|
+
* @param deps - Registry dependencies
|
|
58
|
+
* @throws {WorkflowValidationError} If validation fails
|
|
59
|
+
*/
|
|
60
|
+
declare function assertWorkflowConsistency(deps: WorkflowConsistencyDeps): void;
|
|
61
|
+
/**
|
|
62
|
+
* Validate SLA configuration is reasonable.
|
|
63
|
+
*
|
|
64
|
+
* @param spec - Workflow spec to validate
|
|
65
|
+
* @returns Array of validation issues
|
|
66
|
+
*/
|
|
67
|
+
declare function validateSlaConfig(spec: WorkflowSpec): WorkflowValidationIssue[];
|
|
68
|
+
/**
|
|
69
|
+
* Validate compensation configuration.
|
|
70
|
+
*
|
|
71
|
+
* @param spec - Workflow spec to validate
|
|
72
|
+
* @returns Array of validation issues
|
|
73
|
+
*/
|
|
74
|
+
declare function validateCompensation(spec: WorkflowSpec): WorkflowValidationIssue[];
|
|
75
|
+
/**
|
|
76
|
+
* Validate retry configuration.
|
|
77
|
+
*
|
|
78
|
+
* @param spec - Workflow spec to validate
|
|
79
|
+
* @returns Array of validation issues
|
|
80
|
+
*/
|
|
81
|
+
declare function validateRetryConfig(spec: WorkflowSpec): WorkflowValidationIssue[];
|
|
82
|
+
/**
|
|
83
|
+
* Perform comprehensive workflow validation.
|
|
84
|
+
*
|
|
85
|
+
* @param spec - Workflow spec to validate
|
|
86
|
+
* @param options - Validation options
|
|
87
|
+
* @returns Validation result
|
|
88
|
+
*/
|
|
89
|
+
declare function validateWorkflowComprehensive(spec: WorkflowSpec, options?: ValidateWorkflowSpecOptions): WorkflowValidationResult;
|
|
28
90
|
//#endregion
|
|
29
|
-
export { ValidateWorkflowSpecOptions, WorkflowValidationError, WorkflowValidationIssue, WorkflowValidationLevel, assertWorkflowSpecValid, validateWorkflowSpec };
|
|
91
|
+
export { ValidateWorkflowSpecOptions, WorkflowConsistencyDeps, WorkflowValidationError, WorkflowValidationIssue, WorkflowValidationLevel, WorkflowValidationResult, assertWorkflowConsistency, assertWorkflowSpecValid, validateCompensation, validateRetryConfig, validateSlaConfig, validateWorkflowComprehensive, validateWorkflowConsistency, validateWorkflowSpec };
|