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

scheduledTask.ts

182 lines6.4 KB

1import * as Schema from "effect/Schema";
2​
3import {
4 CommandId,
5 IsoDateTime,
6 ProjectId,
7 ScheduledTaskId,
8 ThreadId,
9 TrimmedNonEmptyString,
10} from "./baseSchemas.ts";
11import { ModelSelection } from "./modelSelection.ts";
12import {
13 OrchestrationV2Actor,
14 OrchestrationV2CreationSource,
15 OrchestrationV2ThreadLaunchWorkspaceStrategy,
16} from "./orchestrationV2.ts";
17import { ProviderInteractionMode, RuntimeMode } from "./providerPolicy.ts";
18​
19/** 24-hour "HH:MM" wall-clock time. Mirrors `parseTimeOfDay` on the server. */
20const TimeOfDay = TrimmedNonEmptyString.check(
21 Schema.isPattern(/^([01]?\d|2[0-3]):([0-5]\d)$/),
22).annotate({ description: "Local wall-clock time in 24-hour HH:MM form, such as 09:30." });
23​
24export const MIN_SCHEDULED_TASK_INTERVAL_MS = 60_000;
25​
26const ScheduledTaskIntervalMs = Schema.Int.check(Schema.isGreaterThan(0)).annotate({
27 description: "Positive interval in milliseconds.",
28});
29​
30const ScheduledTaskIntervalSchedule = Schema.Struct({
31 type: Schema.Literal("interval").annotate({
32 description: "Select interval scheduling.",
33 }),
34 everyMs: ScheduledTaskIntervalMs,
35}).annotate({
36 description: "Run repeatedly after a fixed number of milliseconds.",
37});
38​
39const ScheduledTaskFixedTimeSchedule = Schema.Struct({
40 type: Schema.Literal("fixed_time").annotate({
41 description: "Select a fixed local wall-clock time.",
42 }),
43 timeOfDay: TimeOfDay,
44 weekdays: Schema.optional(
45 Schema.Array(
46 Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: 6 })).annotate({
47 description: "Weekday number where 0 is Sunday and 6 is Saturday.",
48 }),
49 ).annotate({
50 description: "Optional weekdays; omit to run every day.",
51 }),
52 ),
53}).annotate({
54 description: "Run at a fixed local wall-clock time on selected weekdays.",
55});
56​
57/**
58 * Read model for persisted schedules. Keep accepting legacy sub-minute rows so
59 * users can list, disable, edit, or delete them after the write minimum changes.
60 */
61export const ScheduledTaskSchedule = Schema.Union([
62 ScheduledTaskIntervalSchedule,
63 ScheduledTaskFixedTimeSchedule,
64]).annotate({
65 description:
66 "Structured recurring schedule. Pass an object with type 'interval' or 'fixed_time'.",
67});
68export type ScheduledTaskSchedule = typeof ScheduledTaskSchedule.Type;
69​
70/** Mutation model: newly created or updated interval schedules run at most once per minute. */
71export const ScheduledTaskUpsertSchedule = Schema.Union([
72 Schema.Struct({
73 type: Schema.Literal("interval").annotate({
74 description: "Select interval scheduling.",
75 }),
76 everyMs: ScheduledTaskIntervalMs.check(
77 Schema.isGreaterThanOrEqualTo(MIN_SCHEDULED_TASK_INTERVAL_MS),
78 ).annotate({
79 description: "Interval in milliseconds, with a minimum of 60000 (one minute).",
80 }),
81 }).annotate({
82 description: "Run repeatedly after a fixed number of milliseconds.",
83 }),
84 ScheduledTaskFixedTimeSchedule,
85]).annotate({
86 description: "Writable recurring schedule. Pass an object with type 'interval' or 'fixed_time'.",
87});
88export type ScheduledTaskUpsertSchedule = typeof ScheduledTaskUpsertSchedule.Type;
89​
90export const ScheduledTaskRunStatus = Schema.Literals(["never", "running", "succeeded", "failed"]);
91export type ScheduledTaskRunStatus = typeof ScheduledTaskRunStatus.Type;
92​
93export const ScheduledTask = Schema.Struct({
94 id: ScheduledTaskId,
95 title: TrimmedNonEmptyString,
96 prompt: TrimmedNonEmptyString,
97 enabled: Schema.Boolean,
98 schedule: ScheduledTaskSchedule,
99 projectId: ProjectId,
100 threadId: Schema.NullOr(ThreadId),
101 workspaceStrategy: OrchestrationV2ThreadLaunchWorkspaceStrategy,
102 modelSelection: ModelSelection,
103 runtimeMode: RuntimeMode,
104 interactionMode: ProviderInteractionMode,
105 createdBy: OrchestrationV2Actor,
106 creationSource: OrchestrationV2CreationSource,
107 createdAt: IsoDateTime,
108 updatedAt: IsoDateTime,
109 nextRunAt: Schema.NullOr(IsoDateTime),
110 lastRunAt: Schema.NullOr(IsoDateTime),
111 lastRunStatus: ScheduledTaskRunStatus,
112 lastRunError: Schema.NullOr(Schema.String),
113 runCount: Schema.Int.check(Schema.isGreaterThanOrEqualTo(0)),
114});
115export type ScheduledTask = typeof ScheduledTask.Type;
116​
117export const ScheduledTaskListInput = Schema.Struct({});
118export type ScheduledTaskListInput = typeof ScheduledTaskListInput.Type;
119​
120export const ScheduledTaskListResult = Schema.Struct({
121 tasks: Schema.Array(ScheduledTask),
122});
123export type ScheduledTaskListResult = typeof ScheduledTaskListResult.Type;
124​
125export const ScheduledTaskUpsertInput = Schema.Struct({
126 id: Schema.optional(ScheduledTaskId),
127 commandId: Schema.optional(CommandId),
128 title: TrimmedNonEmptyString,
129 prompt: TrimmedNonEmptyString,
130 enabled: Schema.Boolean,
131 schedule: ScheduledTaskUpsertSchedule,
132 projectId: ProjectId,
133 threadId: Schema.optional(Schema.NullOr(ThreadId)),
134 workspaceStrategy: OrchestrationV2ThreadLaunchWorkspaceStrategy,
135 modelSelection: ModelSelection,
136 runtimeMode: RuntimeMode,
137 interactionMode: ProviderInteractionMode,
138 createdBy: Schema.optional(OrchestrationV2Actor),
139 creationSource: Schema.optional(OrchestrationV2CreationSource),
140});
141export type ScheduledTaskUpsertInput = typeof ScheduledTaskUpsertInput.Type;
142​
143/** Partial update that flips only the enabled flag — never overwrites other fields. */
144export const ScheduledTaskSetEnabledInput = Schema.Struct({
145 id: ScheduledTaskId,
146 enabled: Schema.Boolean,
147});
148export type ScheduledTaskSetEnabledInput = typeof ScheduledTaskSetEnabledInput.Type;
149​
150export const ScheduledTaskDeleteInput = Schema.Struct({
151 id: ScheduledTaskId,
152});
153export type ScheduledTaskDeleteInput = typeof ScheduledTaskDeleteInput.Type;
154​
155export const ScheduledTaskRunNowInput = Schema.Struct({
156 id: ScheduledTaskId,
157});
158export type ScheduledTaskRunNowInput = typeof ScheduledTaskRunNowInput.Type;
159​
160export const ScheduledTaskMutationResult = Schema.Struct({
161 task: ScheduledTask,
162});
163export type ScheduledTaskMutationResult = typeof ScheduledTaskMutationResult.Type;
164​
165export const ScheduledTaskDeleteResult = Schema.Struct({
166 id: ScheduledTaskId,
167});
168export type ScheduledTaskDeleteResult = typeof ScheduledTaskDeleteResult.Type;
169​
170export const ScheduledTaskRunNowResult = Schema.Struct({
171 task: ScheduledTask,
172});
173export type ScheduledTaskRunNowResult = typeof ScheduledTaskRunNowResult.Type;
174​
175export class ScheduledTaskError extends Schema.TaggedErrorClass<ScheduledTaskError>()(
176 "ScheduledTaskError",
177 {
178 message: Schema.String,
179 taskId: Schema.optional(ScheduledTaskId),
180 cause: Schema.optional(Schema.Defect()),
181 },
182) {}