@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, 06:00 UTC

main
ts 173 lines 4.99 KB
Raw
1 import * as Hetzner from "@distilled.cloud/hetzner";
2 import type { GetActionResponseAction } from "@distilled.cloud/hetzner/actions";
3 import * as Data from "effect/Data";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import * as Schedule from "effect/Schedule";
7
8 export class ActionPending extends Data.TaggedError("ActionPending")<{
9 actionId: number;
10 status: string;
11 }> {}
12
13 export class ActionFailed extends Data.TaggedError("ActionFailed")<{
14 actionId: number;
15 command: string;
16 code?: string;
17 message: string;
18 }> {}
19
20 export class ActionTimeout extends Data.TaggedError("ActionTimeout")<{
21 actionId: number;
22 status: string;
23 }> {}
24
25 export type ActionRef = number | Pick<GetActionResponseAction, "id"> | GetActionResponseAction;
26
27 const actionIdOf = (ref: ActionRef): number => (typeof ref === "number" ? ref : ref.id);
28
29 const isErrorStatus = (status: string): boolean => status === "error";
30 const isSuccessStatus = (status: string): boolean => status === "success";
31
32 const failIfError = (action: GetActionResponseAction) => {
33 if (isErrorStatus(action.status)) {
34 return Effect.fail(
35 new ActionFailed({
36 actionId: action.id,
37 command: action.command,
38 code: action.error?.code,
39 message: action.error?.message ?? "Hetzner Action failed",
40 }),
41 );
42 }
43 return Effect.void;
44 };
45
46 const retryable = (e: { readonly _tag: string }): boolean =>
47 e._tag === "ActionPending" ||
48 e._tag === "TooManyRequests" ||
49 e._tag === "ServiceUnavailable" ||
50 e._tag === "InternalServerError" ||
51 e._tag === "BadGateway" ||
52 e._tag === "GatewayTimeout";
53
54 const backoff = Schedule.min([
55 Schedule.exponential(Duration.millis(500), 1.5),
56 Schedule.spaced(Duration.seconds(5)),
57 ]);
58
59 /**
60 * Poll a Hetzner Action until it reaches `success` or `error`.
61 *
62 * Bounded: at most 24 polls, exponential backoff starting at 500ms and
63 * capped at 5s. Already-finished actions return immediately.
64 */
65 export const waitForAction = (
66 ref: ActionRef,
67 ): Effect.Effect<
68 GetActionResponseAction,
69 ActionFailed | ActionTimeout | Hetzner.actions.GetActionError,
70 Hetzner.actions.HetznerOpContext
71 > =>
72 Effect.gen(function* () {
73 if (typeof ref !== "number" && "status" in ref) {
74 yield* failIfError(ref);
75 if (isSuccessStatus(ref.status)) {
76 return ref;
77 }
78 }
79
80 const id = actionIdOf(ref);
81 return yield* Hetzner.actions.getAction({ id }).pipe(
82 Effect.flatMap(({ action }) =>
83 Effect.gen(function* () {
84 yield* failIfError(action);
85 if (!isSuccessStatus(action.status)) {
86 return yield* new ActionPending({
87 actionId: action.id,
88 status: action.status,
89 });
90 }
91 return action;
92 }),
93 ),
94 Effect.retry({
95 while: retryable,
96 times: 24,
97 schedule: backoff,
98 }),
99 Effect.catchTag(
100 "ActionPending",
101 (e) =>
102 new ActionTimeout({
103 actionId: e.actionId,
104 status: e.status,
105 }),
106 ),
107 );
108 });
109
110 /**
111 * Wait for every Action in `refs` (in order). Empty input is a no-op.
112 */
113 export const waitForActions = (
114 refs: ReadonlyArray<ActionRef>,
115 ): Effect.Effect<
116 GetActionResponseAction[],
117 ActionFailed | ActionTimeout | Hetzner.actions.GetActionError,
118 Hetzner.actions.HetznerOpContext
119 > => Effect.forEach(refs, waitForAction, { concurrency: 1 });
120
121 /**
122 * Poll a Hetzner Cloud DNS Zone Action (`GET /zones/actions/{id}`) until it
123 * reaches `success` or `error`. Zone actions live in a separate namespace
124 * from compute `/actions/{id}` — do not use {@link waitForAction} for them.
125 *
126 * Bounded: at most 10 polls, exponential backoff starting at 500ms and
127 * capped at 5s (under 60s total). Already-finished actions return
128 * immediately.
129 */
130 export const waitForZoneAction = (
131 ref: ActionRef,
132 ): Effect.Effect<
133 GetActionResponseAction,
134 ActionFailed | ActionTimeout | Hetzner.zoneActions.GetZonesActionError,
135 Hetzner.zoneActions.HetznerOpContext
136 > =>
137 Effect.gen(function* () {
138 if (typeof ref !== "number" && "status" in ref) {
139 yield* failIfError(ref as GetActionResponseAction);
140 if (isSuccessStatus(ref.status)) {
141 return ref as GetActionResponseAction;
142 }
143 }
144
145 const id = actionIdOf(ref);
146 return yield* Hetzner.zoneActions.getZonesAction({ id }).pipe(
147 Effect.flatMap(({ action }) =>
148 Effect.gen(function* () {
149 yield* failIfError(action as GetActionResponseAction);
150 if (!isSuccessStatus(action.status)) {
151 return yield* new ActionPending({
152 actionId: action.id,
153 status: action.status,
154 });
155 }
156 return action as GetActionResponseAction;
157 }),
158 ),
159 Effect.retry({
160 while: retryable,
161 times: 24,
162 schedule: backoff,
163 }),
164 Effect.catchTag(
165 "ActionPending",
166 (e) =>
167 new ActionTimeout({
168 actionId: e.actionId,
169 status: e.status,
170 }),
171 ),
172 );
173 });