-
Notifications
You must be signed in to change notification settings - Fork 422
/
runs.ts
207 lines (185 loc) · 6.51 KB
/
runs.ts
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
import { z } from "zod";
import { Prettify } from "../types";
import { RuntimeEnvironmentType } from "./api";
import { ErrorWithStack } from "./errors";
import { JobRunStatusRecord, JobRunStatusRecordSchema } from "./statuses";
import { TaskStatusSchema } from "./tasks";
export const RunStatusSchema = z.union([
z.literal("PENDING"),
z.literal("QUEUED"),
z.literal("WAITING_ON_CONNECTIONS"),
z.literal("PREPROCESSING"),
z.literal("STARTED"),
z.literal("SUCCESS"),
z.literal("FAILURE"),
z.literal("TIMED_OUT"),
z.literal("ABORTED"),
z.literal("CANCELED"),
z.literal("UNRESOLVED_AUTH"),
z.literal("INVALID_PAYLOAD"),
z.literal("EXECUTING"),
z.literal("WAITING_TO_CONTINUE"),
z.literal("WAITING_TO_EXECUTE"),
]);
export const RunTaskSchema = z.object({
/** The Task id */
id: z.string(),
/** The key that you defined when creating the Task, the first param in any task. */
displayKey: z.string().nullable(),
/** The Task status */
status: TaskStatusSchema,
/** The name of the Task */
name: z.string(),
/** The icon of the Task, a string.
* For integrations, this will be a lowercase name of the company.
* Can be used with the [@trigger.dev/companyicons](https://www.npmjs.com/package/@trigger.dev/companyicons) package to display an svg. */
icon: z.string().nullable(),
/** When the task started */
startedAt: z.coerce.date().nullable(),
/** When the task completed */
completedAt: z.coerce.date().nullable(),
});
export type RunTaskWithSubtasks = z.infer<typeof RunTaskSchema> & {
/** The subtasks of the task */
subtasks?: RunTaskWithSubtasks[];
};
const RunTaskWithSubtasksSchema: z.ZodType<RunTaskWithSubtasks> = RunTaskSchema.extend({
subtasks: z.lazy(() => RunTaskWithSubtasksSchema.array()).optional(),
});
const GetRunOptionsSchema = z.object({
/** Return subtasks, which appear in a `subtasks` array on a task. @default false */
subtasks: z.boolean().optional(),
/** You can use this to get more tasks, if there are more than are returned in a single batch @default undefined */
cursor: z.string().optional(),
/** How many tasks you want to return in one go, max 50. @default 20 */
take: z.number().optional(),
});
export type GetRunOptions = z.infer<typeof GetRunOptionsSchema>;
const GetRunOptionsWithTaskDetailsSchema = GetRunOptionsSchema.extend({
/** If `true`, it returns the `params` and `output` of all tasks. @default false */
taskdetails: z.boolean().optional(),
});
export type GetRunOptionsWithTaskDetails = z.infer<typeof GetRunOptionsWithTaskDetailsSchema>;
const RunSchema = z.object({
/** The Run id */
id: z.string(),
/** The Run status */
status: RunStatusSchema,
/** When the run started */
startedAt: z.coerce.date().nullable(),
/** When the run was last updated */
updatedAt: z.coerce.date().nullable(),
/** When the run was completed */
completedAt: z.coerce.date().nullable(),
});
export const GetRunSchema = RunSchema.extend({
/** The output of the run */
output: z.any().optional(),
/** The tasks from the run */
tasks: z.array(RunTaskWithSubtasksSchema),
/** Any status updates that were published from the run */
statuses: z.array(JobRunStatusRecordSchema).default([]),
/** If there are more tasks, you can use this to get them */
nextCursor: z.string().optional(),
});
export type GetRun = Prettify<z.infer<typeof GetRunSchema>>;
const GetRunsOptionsSchema = z.object({
/** You can use this to get more tasks, if there are more than are returned in a single batch @default undefined */
cursor: z.string().optional(),
/** How many runs you want to return in one go, max 50. @default 20 */
take: z.number().optional(),
});
export type GetRunsOptions = z.infer<typeof GetRunsOptionsSchema>;
export const GetRunsSchema = z.object({
/** The runs from the query */
runs: RunSchema.array(),
/** If there are more runs, you can use this to get them */
nextCursor: z.string().optional(),
});
export type RunNotificationJobMetadata = { id: string; version: string };
export type RunNotificationEnvMetadata = {
slug: string;
id: string;
type: RuntimeEnvironmentType;
};
export type RunNotificationOrgMetadata = { slug: string; id: string; title: string };
export type RunNotificationProjectMetadata = { slug: string; id: string; name: string };
export type RunNotificationAccountMetadata = { id: string; metadata?: any };
export type RunNotificationInvocationMetadata<T = any> = {
id: string;
context: any;
timestamp: Date;
payload: T;
};
export type RunNotificationRunMetadata = {
/** The Run id */
id: string;
/** The Run status */
statuses: JobRunStatusRecord[];
/** When the run started */
startedAt: Date;
/** When the run was last updated */
updatedAt: Date;
/** When the run was completed */
completedAt: Date;
/** If the run was a test or not */
isTest: boolean;
executionDurationInMs: number;
executionCount: number;
};
type RunNotificationCommon<TPayload = any> = {
/** The Run id */
id: string;
/** The Run status */
statuses: JobRunStatusRecord[];
/** When the run started */
startedAt: Date;
/** When the run was last updated */
updatedAt: Date;
/** When the run was completed */
completedAt: Date;
/** If the run was a test or not */
isTest: boolean;
executionDurationInMs: number;
executionCount: number;
/** Job metadata */
job: RunNotificationJobMetadata;
/** Environment metadata */
environment: RunNotificationEnvMetadata;
/** Organization metadata */
organization: RunNotificationOrgMetadata;
/** Project metadata */
project: RunNotificationProjectMetadata;
/** Account metadata */
account?: RunNotificationAccountMetadata;
/** Invocation metadata */
invocation: RunNotificationInvocationMetadata<TPayload>;
};
export type SuccessfulRunNotification<TOutput, TPayload = any> = RunNotificationCommon<TPayload> & {
ok: true;
/** The Run status */
status: "SUCCESS";
/** The output of the run */
output: TOutput;
};
export type FailedRunNotification<TPayload = any> = RunNotificationCommon<TPayload> & {
ok: false;
/** The Run status */
status: "FAILURE" | "TIMED_OUT" | "ABORTED" | "CANCELED" | "UNRESOLVED_AUTH" | "INVALID_PAYLOAD";
/** The error of the run */
error: any;
/** The task that failed */
task?: {
id: string;
cacheKey: string | null;
status: string;
name: string;
icon: string | null;
startedAt: string;
error: ErrorWithStack;
params: any | null;
};
};
export type RunNotification<TOutput, TPayload = any> =
| SuccessfulRunNotification<TOutput, TPayload>
| FailedRunNotification<TPayload>;