-
Notifications
You must be signed in to change notification settings - Fork 256
Expand file tree
/
Copy pathruns.ts
More file actions
131 lines (122 loc) · 3.96 KB
/
runs.ts
File metadata and controls
131 lines (122 loc) · 3.96 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
import { z } from 'zod';
import { type SerializedData, SerializedDataSchema } from './serialization.js';
import {
type PaginationOptions,
type ResolveData,
StructuredErrorSchema,
} from './shared.js';
// Workflow run schemas
export const WorkflowRunStatusSchema = z.enum([
'pending',
'running',
'completed',
'failed',
'cancelled',
]);
/**
* Base schema for the Workflow runs. Prefer using WorkflowRunSchema
* which implements a discriminatedUnion for various states.
*
* Note: input/output use SerializedDataSchema to support both:
* - specVersion >= 2: Uint8Array (binary devalue format)
* - specVersion 1: any (legacy JSON format)
*/
export const WorkflowRunBaseSchema = z.object({
runId: z.string(),
status: WorkflowRunStatusSchema,
deploymentId: z.string(),
/**
* The machine-readable name of the workflow function.
*
* This field contains a structured identifier like `workflow//./src/workflows/order//processOrder`
* that encodes the workflow's module specifier and function name.
*
* Use `parseWorkflowName()` from `@workflow/utils/parse-name` to extract:
* - `shortName`: User-friendly display name (e.g., `"processOrder"`)
* - `moduleSpecifier`: The module path or package (e.g., `"./src/workflows/order"`)
* - `functionName`: The full function path (e.g., `"processOrder"`)
*
* @example
* ```ts
* import { parseWorkflowName } from "@workflow/utils/parse-name";
*
* const parsed = parseWorkflowName(run.workflowName);
* // parsed.shortName → "processOrder"
* // parsed.moduleSpecifier → "./src/workflows/order"
* ```
*/
workflowName: z.string(),
// Optional in database for backwards compatibility, defaults to 1 (legacy) when reading
specVersion: z.number().optional(),
executionContext: z.record(z.string(), z.any()).optional(),
input: SerializedDataSchema,
output: SerializedDataSchema.optional(),
error: StructuredErrorSchema.optional(),
expiredAt: z.coerce.date().optional(),
startedAt: z.coerce.date().optional(),
completedAt: z.coerce.date().optional(),
createdAt: z.coerce.date(),
updatedAt: z.coerce.date(),
});
// Discriminated union based on status
export const WorkflowRunSchema = z.discriminatedUnion('status', [
// Non-final states
WorkflowRunBaseSchema.extend({
status: z.enum(['pending', 'running']),
output: z.undefined().optional(),
error: z.undefined().optional(),
completedAt: z.undefined().optional(),
}),
// Cancelled state
WorkflowRunBaseSchema.extend({
status: z.literal('cancelled'),
output: z.undefined().optional(),
error: z.undefined().optional(),
completedAt: z.coerce.date(),
}),
// Completed state - output can be v1 or v2 format
WorkflowRunBaseSchema.extend({
status: z.literal('completed'),
output: SerializedDataSchema,
error: z.undefined().optional(),
completedAt: z.coerce.date(),
}),
// Failed state
WorkflowRunBaseSchema.extend({
status: z.literal('failed'),
output: z.undefined().optional(),
error: StructuredErrorSchema,
completedAt: z.coerce.date(),
}),
]);
// Inferred types
export type WorkflowRunStatus = z.infer<typeof WorkflowRunStatusSchema>;
export type WorkflowRun = z.infer<typeof WorkflowRunSchema>;
/**
* WorkflowRun with input/output fields excluded (when resolveData='none').
* Used for listing runs without fetching the full serialized data.
*/
export type WorkflowRunWithoutData = Omit<WorkflowRun, 'input' | 'output'> & {
input: undefined;
output: undefined;
};
// Request types
export interface CreateWorkflowRunRequest {
deploymentId: string;
workflowName: string;
input: SerializedData;
executionContext?: SerializedData;
specVersion?: number;
}
export interface GetWorkflowRunParams {
resolveData?: ResolveData;
}
export interface ListWorkflowRunsParams {
workflowName?: string;
status?: WorkflowRunStatus;
pagination?: PaginationOptions;
resolveData?: ResolveData;
}
export interface CancelWorkflowRunParams {
resolveData?: ResolveData;
}