Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

scheduledTask.ts

331 lines12.2 KB

1import * as Schema from "effect/Schema";
2​
3import {
4 CommandId,
5 ForwardCompatibleArray,
6 IsoDateTime,
7 ProjectId,
8 ScheduledTaskId,
9 SecretRef,
10 ThreadId,
11 TrimmedNonEmptyString,
12} from "./baseSchemas.ts";
13import { ModelSelection } from "./modelSelection.ts";
14import {
15 OrchestrationV2Actor,
16 OrchestrationV2CreationSource,
17 OrchestrationV2ThreadLaunchWorkspaceStrategy,
18} from "./orchestrationV2.ts";
19import { ProviderInteractionMode, RuntimeMode } from "./providerPolicy.ts";
20​
21/** 24-hour "HH:MM" wall-clock time. Mirrors `parseTimeOfDay` on the server. */
22const TimeOfDay = TrimmedNonEmptyString.check(
23 Schema.isPattern(/^([01]?\d|2[0-3]):([0-5]\d)$/),
24).annotate({ description: "Local wall-clock time in 24-hour HH:MM form, such as 09:30." });
25​
26export const MIN_SCHEDULED_TASK_INTERVAL_MS = 60_000;
27​
28const ScheduledTaskIntervalMs = Schema.Int.check(Schema.isGreaterThan(0)).annotate({
29 description: "Positive interval in milliseconds.",
30});
31​
32const ScheduledTaskIntervalSchedule = Schema.Struct({
33 type: Schema.Literal("interval").annotate({
34 description: "Select interval scheduling.",
35 }),
36 everyMs: ScheduledTaskIntervalMs,
37}).annotate({
38 description: "Run repeatedly after a fixed number of milliseconds.",
39});
40​
41const ScheduledTaskFixedTimeSchedule = Schema.Struct({
42 type: Schema.Literal("fixed_time").annotate({
43 description: "Select a fixed local wall-clock time.",
44 }),
45 timeOfDay: TimeOfDay,
46 weekdays: Schema.optional(
47 Schema.Array(
48 Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 6 })).annotate({
49 description: "Weekday number where 0 is Sunday and 6 is Saturday.",
50 }),
51 ).annotate({
52 description: "Optional weekdays; omit to run every day.",
53 }),
54 ),
55}).annotate({
56 description: "Run at a fixed local wall-clock time on selected weekdays.",
57});
58​
59const ScheduledTaskWebhookSignatureFields = {
60 header: TrimmedNonEmptyString.annotate({
61 description: "Request header carrying the signature, such as x-hub-signature-256.",
62 }),
63 encoding: Schema.Literals(["hex", "base64"]).annotate({
64 description: "How the HMAC-SHA256 digest is encoded in the header.",
65 }),
66 prefix: Schema.String.annotate({
67 description: "Text before the digest in the header value, such as 'sha256='. Empty for none.",
68 }),
69};
70​
71export const ScheduledTaskWebhookSignature = Schema.Struct(
72 ScheduledTaskWebhookSignatureFields,
73).annotate({ description: "Optional HMAC-SHA256 signature check over the raw request body." });
74export type ScheduledTaskWebhookSignature = typeof ScheduledTaskWebhookSignature.Type;
75​
76/** Matches how long the relay holds a request for an offline environment. */
77export const MAX_WEBHOOK_DELIVERY_AGE_MINUTES = 24 * 60;
78​
79const WebhookMaxDeliveryAgeMinutes = Schema.Int.check(
80 Schema.isBetween({ minimum: 1, maximum: MAX_WEBHOOK_DELIVERY_AGE_MINUTES }),
81).annotate({
82 description:
83 "Skip requests the relay held longer than this many minutes while the environment was offline. Null runs every request.",
84});
85​
86const ScheduledTaskWebhookSchedule = Schema.Struct({
87 type: Schema.Literal("webhook").annotate({
88 description: "Run when the task's webhook URL receives a request.",
89 }),
90 signature: Schema.NullOr(ScheduledTaskWebhookSignature),
91 maxDeliveryAgeMinutes: Schema.optional(Schema.NullOr(WebhookMaxDeliveryAgeMinutes)),
92}).annotate({
93 description:
94 "Run on each request to the task's webhook URL. The prompt may use {{body.path}}, {{headers.name}}, {{query.name}}, {{body}} and {{request}} placeholders.",
95});
96​
97const ScheduledTaskUpsertWebhookSchedule = Schema.Struct({
98 type: Schema.Literal("webhook").annotate({
99 description: "Run when the task's webhook URL receives a request.",
100 }),
101 signature: Schema.optional(
102 Schema.NullOr(
103 Schema.Struct({
104 ...ScheduledTaskWebhookSignatureFields,
105 secret: Schema.optional(TrimmedNonEmptyString).annotate({
106 description: "Shared signing secret. Omit to keep the stored secret.",
107 }),
108 secretRef: Schema.optional(SecretRef).annotate({
109 description:
110 "A secret the user entered through request_secret, used instead of secret. It is consumed by this save.",
111 }),
112 }),
113 ),
114 ).annotate({
115 description: "Signature check; omit or null to accept requests by URL token only.",
116 }),
117 maxDeliveryAgeMinutes: Schema.optional(Schema.NullOr(WebhookMaxDeliveryAgeMinutes)),
118}).annotate({
119 description:
120 "Run on each request to the task's webhook URL. The prompt may use {{body.path}}, {{headers.name}}, {{query.name}}, {{body}} and {{request}} placeholders.",
121});
122​
123/**
124 * Read model for persisted schedules. Keep accepting legacy sub-minute rows so
125 * users can list, disable, edit, or delete them after the write minimum changes.
126 */
127export const ScheduledTaskSchedule = Schema.Union([
128 ScheduledTaskIntervalSchedule,
129 ScheduledTaskFixedTimeSchedule,
130 ScheduledTaskWebhookSchedule,
131]).annotate({
132 description:
133 "Structured trigger. Pass an object with type 'interval', 'fixed_time' or 'webhook'.",
134});
135export type ScheduledTaskSchedule = typeof ScheduledTaskSchedule.Type;
136​
137export const ScheduledTaskUpsertSchedule = Schema.Union([
138 Schema.Struct({
139 type: Schema.Literal("interval").annotate({
140 description: "Select interval scheduling.",
141 }),
142 everyMs: ScheduledTaskIntervalMs.check(
143 Schema.isGreaterThanOrEqualTo(MIN_SCHEDULED_TASK_INTERVAL_MS),
144 ).annotate({
145 description: "Interval in milliseconds, with a minimum of 60000 (one minute).",
146 }),
147 }).annotate({
148 description: "Run repeatedly after a fixed number of milliseconds.",
149 }),
150 ScheduledTaskFixedTimeSchedule,
151 ScheduledTaskUpsertWebhookSchedule,
152]).annotate({
153 description: "Writable trigger. Pass an object with type 'interval', 'fixed_time' or 'webhook'.",
154});
155export type ScheduledTaskUpsertSchedule = typeof ScheduledTaskUpsertSchedule.Type;
156​
157export const ScheduledTaskRunStatus = Schema.Literals(["never", "running", "succeeded", "failed"]);
158export type ScheduledTaskRunStatus = typeof ScheduledTaskRunStatus.Type;
159​
160export const ScheduledTaskWebhookEndpoint = Schema.Struct({
161 path: TrimmedNonEmptyString,
162 url: Schema.NullOr(TrimmedNonEmptyString),
163 hasSecret: Schema.Boolean,
164});
165export type ScheduledTaskWebhookEndpoint = typeof ScheduledTaskWebhookEndpoint.Type;
166​
167export const ScheduledTask = Schema.Struct({
168 id: ScheduledTaskId,
169 title: TrimmedNonEmptyString,
170 prompt: TrimmedNonEmptyString,
171 enabled: Schema.Boolean,
172 schedule: ScheduledTaskSchedule,
173 projectId: ProjectId,
174 threadId: Schema.NullOr(ThreadId),
175 workspaceStrategy: OrchestrationV2ThreadLaunchWorkspaceStrategy,
176 modelSelection: ModelSelection,
177 runtimeMode: RuntimeMode,
178 interactionMode: ProviderInteractionMode,
179 createdBy: OrchestrationV2Actor,
180 creationSource: OrchestrationV2CreationSource,
181 createdAt: IsoDateTime,
182 updatedAt: IsoDateTime,
183 nextRunAt: Schema.NullOr(IsoDateTime),
184 lastRunAt: Schema.NullOr(IsoDateTime),
185 lastRunStatus: ScheduledTaskRunStatus,
186 lastRunError: Schema.NullOr(Schema.String),
187 runCount: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
188 webhook: Schema.optional(ScheduledTaskWebhookEndpoint),
189});
190export type ScheduledTask = typeof ScheduledTask.Type;
191​
192export const ScheduledTaskListInput = Schema.Struct({});
193export type ScheduledTaskListInput = typeof ScheduledTaskListInput.Type;
194​
195export const ScheduledTaskListResult = Schema.Struct({
196 // Trigger types grow over time; a client must not lose the whole list over
197 // one task it cannot decode.
198 tasks: ForwardCompatibleArray(ScheduledTask),
199 /** The server's IANA zone for fixed-time schedules. Absent from older servers. */
200 timeZone: Schema.optional(TrimmedNonEmptyString),
201});
202export type ScheduledTaskListResult = typeof ScheduledTaskListResult.Type;
203​
204export const ScheduledTaskUpsertInput = Schema.Struct({
205 id: Schema.optional(ScheduledTaskId),
206 requireExisting: Schema.optional(Schema.Boolean).annotate({
207 description: "Reject the save if the task no longer exists, for edits from a client form.",
208 }),
209 commandId: Schema.optional(CommandId),
210 title: TrimmedNonEmptyString,
211 prompt: TrimmedNonEmptyString,
212 enabled: Schema.Boolean,
213 schedule: ScheduledTaskUpsertSchedule,
214 projectId: ProjectId,
215 threadId: Schema.optional(Schema.NullOr(ThreadId)),
216 workspaceStrategy: OrchestrationV2ThreadLaunchWorkspaceStrategy,
217 modelSelection: ModelSelection,
218 runtimeMode: RuntimeMode,
219 interactionMode: ProviderInteractionMode,
220 createdBy: Schema.optional(OrchestrationV2Actor),
221 creationSource: Schema.optional(OrchestrationV2CreationSource),
222});
223export type ScheduledTaskUpsertInput = typeof ScheduledTaskUpsertInput.Type;
224​
225export const ScheduledTaskSetEnabledInput = Schema.Struct({
226 id: ScheduledTaskId,
227 enabled: Schema.Boolean,
228});
229export type ScheduledTaskSetEnabledInput = typeof ScheduledTaskSetEnabledInput.Type;
230​
231export const ScheduledTaskDeleteInput = Schema.Struct({
232 id: ScheduledTaskId,
233});
234export type ScheduledTaskDeleteInput = typeof ScheduledTaskDeleteInput.Type;
235​
236export const ScheduledTaskRunNowInput = Schema.Struct({
237 id: ScheduledTaskId,
238});
239export type ScheduledTaskRunNowInput = typeof ScheduledTaskRunNowInput.Type;
240​
241export const ScheduledTaskRotateWebhookTokenInput = Schema.Struct({
242 id: ScheduledTaskId,
243});
244export type ScheduledTaskRotateWebhookTokenInput = typeof ScheduledTaskRotateWebhookTokenInput.Type;
245​
246export const ScheduledTaskWebhookDeliveryId = TrimmedNonEmptyString.pipe(
247 Schema.brand("ScheduledTaskWebhookDeliveryId"),
248);
249export type ScheduledTaskWebhookDeliveryId = typeof ScheduledTaskWebhookDeliveryId.Type;
250​
251export const ScheduledTaskWebhookDeliveryOutcome = Schema.Literals([
252 "accepted",
253 "dispatch_failed",
254 "rejected_signature",
255 "disabled",
256 "rate_limited",
257 "expired",
258]);
259export type ScheduledTaskWebhookDeliveryOutcome = typeof ScheduledTaskWebhookDeliveryOutcome.Type;
260​
261export const ScheduledTaskWebhookDeliverySummary = Schema.Struct({
262 id: ScheduledTaskWebhookDeliveryId,
263 taskId: ScheduledTaskId,
264 receivedAt: IsoDateTime,
265 method: TrimmedNonEmptyString,
266 contentType: Schema.NullOr(Schema.String),
267 bodyBytes: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
268 outcome: ScheduledTaskWebhookDeliveryOutcome,
269 signatureVerified: Schema.Boolean,
270 missingFields: Schema.Array(Schema.String),
271 error: Schema.NullOr(Schema.String),
272});
273export type ScheduledTaskWebhookDeliverySummary = typeof ScheduledTaskWebhookDeliverySummary.Type;
274​
275export const ScheduledTaskWebhookDelivery = Schema.Struct({
276 ...ScheduledTaskWebhookDeliverySummary.fields,
277 query: Schema.String,
278 headers: Schema.Record(Schema.String, Schema.String),
279 body: Schema.String,
280 bodyTruncated: Schema.Boolean,
281 renderedPrompt: Schema.NullOr(Schema.String),
282});
283export type ScheduledTaskWebhookDelivery = typeof ScheduledTaskWebhookDelivery.Type;
284​
285export const ScheduledTaskListWebhookDeliveriesInput = Schema.Struct({
286 id: ScheduledTaskId,
287});
288export type ScheduledTaskListWebhookDeliveriesInput =
289 typeof ScheduledTaskListWebhookDeliveriesInput.Type;
290​
291export const ScheduledTaskListWebhookDeliveriesResult = Schema.Struct({
292 deliveries: Schema.Array(ScheduledTaskWebhookDeliverySummary),
293});
294export type ScheduledTaskListWebhookDeliveriesResult =
295 typeof ScheduledTaskListWebhookDeliveriesResult.Type;
296​
297export const ScheduledTaskGetWebhookDeliveryInput = Schema.Struct({
298 id: ScheduledTaskId,
299 deliveryId: ScheduledTaskWebhookDeliveryId,
300});
301export type ScheduledTaskGetWebhookDeliveryInput = typeof ScheduledTaskGetWebhookDeliveryInput.Type;
302​
303export const ScheduledTaskGetWebhookDeliveryResult = Schema.Struct({
304 delivery: ScheduledTaskWebhookDelivery,
305});
306export type ScheduledTaskGetWebhookDeliveryResult =
307 typeof ScheduledTaskGetWebhookDeliveryResult.Type;
308​
309export const ScheduledTaskMutationResult = Schema.Struct({
310 task: ScheduledTask,
311});
312export type ScheduledTaskMutationResult = typeof ScheduledTaskMutationResult.Type;
313​
314export const ScheduledTaskDeleteResult = Schema.Struct({
315 id: ScheduledTaskId,
316});
317export type ScheduledTaskDeleteResult = typeof ScheduledTaskDeleteResult.Type;
318​
319export const ScheduledTaskRunNowResult = Schema.Struct({
320 task: ScheduledTask,
321});
322export type ScheduledTaskRunNowResult = typeof ScheduledTaskRunNowResult.Type;
323​
324export class ScheduledTaskError extends Schema.TaggedError<ScheduledTaskError>()(
325 "ScheduledTaskError",
326 {
327 message: Schema.String,
328 taskId: Schema.optional(ScheduledTaskId),
329 cause: Schema.optional(Schema.Defect()),
330 },
331) {}