@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 225 lines 5.18 KB
Raw
1 import type { WorkflowEvent } from "cloudflare:workers";
2 // Alchemy modifications are licensed under Apache-2.0.
3 // This file includes third-party code; see /THIRD_PARTY_LICENSES.md.
4 import type { ResolvedStepConfig } from "./context.ts";
5 import type { DatabaseInstance, DatabaseVersion, DatabaseWorkflow } from "./engine.ts";
6
7 export type Instance = {
8 id: string;
9 created_on: string;
10 modified_on: string;
11 workflow_id: string;
12 version_id: string;
13 status: InstanceStatus;
14 started_on: string | null;
15 ended_on: string | null;
16 };
17
18 export const INSTANCE_METADATA = `INSTANCE_METADATA`;
19
20 export type InstanceMetadata = {
21 accountId: number;
22 workflow: DatabaseWorkflow;
23 version: DatabaseVersion;
24 instance: DatabaseInstance;
25 event: WorkflowEvent<unknown>;
26 };
27
28 export enum InstanceStatus {
29 Queued = 0, // Queued and waiting to start
30 Running = 1,
31 Paused = 2, // TODO (WOR-73): Implement pause
32 Errored = 3, // Stopped due to a user or system Error
33 Terminated = 4, // Stopped explicitly by user
34 Complete = 5, // Successful completion
35 WaitingForPause = 6,
36 Waiting = 7,
37 }
38
39 export function instanceStatusName(status: InstanceStatus) {
40 switch (status) {
41 case InstanceStatus.Queued:
42 return "queued";
43 case InstanceStatus.Running:
44 return "running";
45 case InstanceStatus.Paused:
46 return "paused";
47 case InstanceStatus.Errored:
48 return "errored";
49 case InstanceStatus.Terminated:
50 return "terminated";
51 case InstanceStatus.Complete:
52 return "complete";
53 case InstanceStatus.WaitingForPause:
54 return "waitingForPause";
55 case InstanceStatus.Waiting:
56 return "waiting";
57 default:
58 return "unknown";
59 }
60 }
61
62 export const instanceStatusNames = [
63 "queued",
64 "running",
65 "paused",
66 "errored",
67 "terminated",
68 "complete",
69 "waitingForPause",
70 "waiting",
71 "unknown",
72 ] as const;
73
74 export function toInstanceStatus(status: string): InstanceStatus {
75 switch (status) {
76 case "queued":
77 return InstanceStatus.Queued;
78 case "running":
79 return InstanceStatus.Running;
80 case "paused":
81 return InstanceStatus.Paused;
82 case "errored":
83 return InstanceStatus.Errored;
84 case "terminated":
85 return InstanceStatus.Terminated;
86 case "complete":
87 return InstanceStatus.Complete;
88 case "waitingForPause":
89 return InstanceStatus.WaitingForPause;
90 case "waiting":
91 return InstanceStatus.Waiting;
92 case "unknown":
93 throw new Error("unknown cannot be parsed into a InstanceStatus");
94 default:
95 throw new Error(`${status} was not handled because it's not a valid InstanceStatus`);
96 }
97 }
98
99 export const enum InstanceEvent {
100 WORKFLOW_QUEUED = 0,
101 WORKFLOW_START = 1,
102 WORKFLOW_SUCCESS = 2,
103 WORKFLOW_FAILURE = 3,
104 WORKFLOW_TERMINATED = 4,
105
106 STEP_START = 5,
107 STEP_SUCCESS = 6,
108 STEP_FAILURE = 7,
109
110 SLEEP_START = 8,
111 SLEEP_COMPLETE = 9,
112
113 ATTEMPT_START = 10,
114 ATTEMPT_SUCCESS = 11,
115 ATTEMPT_FAILURE = 12,
116
117 // It's here just to make it sequential and to not have gaps in the event types.
118 __INTERNAL_PROD = 13,
119
120 WAIT_START = 14,
121 WAIT_COMPLETE = 15,
122 WAIT_TIMED_OUT = 16,
123
124 ROLLBACK_START = 17,
125 ROLLBACK_STEP_START = 18,
126 ROLLBACK_ATTEMPT_START = 19,
127 ROLLBACK_ATTEMPT_SUCCESS = 20,
128 ROLLBACK_ATTEMPT_FAILURE = 21,
129 ROLLBACK_STEP_SUCCESS = 22,
130 ROLLBACK_STEP_FAILURE = 23,
131 ROLLBACK_COMPLETE = 24,
132 ROLLBACK_FAILED = 25,
133
134 WORKFLOW_RUNNING = 26,
135 WORKFLOW_PAUSED = 27,
136 WORKFLOW_WAITING_FOR_PAUSE = 28,
137 WORKFLOW_WAITING = 29,
138 }
139
140 export const enum InstanceTrigger {
141 API = 0,
142 BINDING = 1,
143 EVENT = 2,
144 CRON = 3,
145 }
146
147 export function instanceTriggerName(trigger: InstanceTrigger) {
148 switch (trigger) {
149 case InstanceTrigger.API:
150 return "api";
151 case InstanceTrigger.BINDING:
152 return "binding";
153 case InstanceTrigger.EVENT:
154 return "event";
155 case InstanceTrigger.CRON:
156 return "cron";
157 default:
158 return "unknown";
159 }
160 }
161
162 export type RawInstanceLog = {
163 id: number;
164 timestamp: string;
165 event: InstanceEvent;
166 groupKey: string | null;
167 target: string | null;
168 metadata: string;
169 };
170
171 export type InstanceAttempt = {
172 start: string;
173 end: string | null;
174 success: boolean | null;
175 error: { name: string; message: string } | null;
176 };
177
178 export type InstanceStepLog = {
179 name: string;
180 start: string;
181 end: string | null;
182 attempts: Array<InstanceAttempt>;
183 config: ResolvedStepConfig;
184 output: unknown;
185 success: boolean | null;
186 type: "step";
187 };
188
189 export type InstanceSleepLog = {
190 name: string;
191 start: string;
192 end: string;
193 finished: boolean;
194 type: "sleep";
195 };
196
197 export type InstanceTerminateLog = {
198 type: "termination";
199 trigger: {
200 source: string;
201 };
202 };
203
204 export type InstanceLogsResponse = {
205 params: Record<string, unknown>;
206 trigger: {
207 source: ReturnType<typeof instanceTriggerName>;
208 };
209 versionId: string;
210 queued: string;
211 start: string | null;
212 end: string | null;
213 steps: Array<InstanceStepLog | InstanceSleepLog | InstanceTerminateLog>;
214 success: boolean | null;
215 error: { name: string; message: string } | null;
216 output: Rpc.Serializable<unknown>;
217 };
218
219 export type WakerPriorityEntry = {
220 hash: string;
221 type: WakerPriorityType;
222 targetTimestamp: number;
223 };
224
225 export type WakerPriorityType = "sleep" | "retry" | "timeout";