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

main
ts 118 lines 3.9 KB
Raw
1 import * as Data from "effect/Data";
2 import * as Duration from "effect/Duration";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5
6 /**
7 * Shared waiter for Google long-running operations (`google.longrunning.Operation`
8 * and the Compute `Operation` shape). Every provider waits through this one
9 * helper so each service only states its time budget.
10 *
11 * NOT exported from `index.ts`.
12 */
13
14 /** The subset of a long-running operation the waiter reads. */
15 export interface LongRunningOperation {
16 name?: string;
17 done?: boolean;
18 /** Compute operations report `status: "DONE"` instead of `done`. */
19 status?: string;
20 error?: {
21 code?: number;
22 message?: string;
23 /** Compute operations list their errors here. */
24 errors?: ReadonlyArray<{ code?: string; message?: string }>;
25 };
26 }
27
28 /** The operation finished with an error. */
29 export class OperationFailed extends Data.TaggedError("GCP.OperationFailed")<{
30 operation: string;
31 /** Numeric RPC code (google.rpc.Code), when reported. */
32 code: number | undefined;
33 /** Compute error code (e.g. `RESOURCE_NOT_FOUND`), when reported. */
34 reason: string | undefined;
35 message: string;
36 }> {}
37
38 /** The operation did not finish within the service's time budget. */
39 export class OperationTimedOut extends Data.TaggedError("GCP.OperationTimedOut")<{
40 operation: string;
41 budget: string;
42 }> {}
43
44 class OperationPending extends Data.TaggedError("GCP.OperationPending")<{
45 operation: string;
46 }> {}
47
48 const isDone = (operation: LongRunningOperation) =>
49 operation.done === true || operation.status === "DONE";
50
51 const failureOf = (operation: LongRunningOperation) => {
52 const error = operation.error;
53 if (error === undefined) return undefined;
54 const first = error.errors?.[0];
55 if (error.code === undefined && first === undefined && !error.message) {
56 return undefined;
57 }
58 return new OperationFailed({
59 operation: operation.name ?? "",
60 code: error.code,
61 reason: first?.code,
62 message: first?.message ?? error.message ?? "operation failed",
63 });
64 };
65
66 export interface WaitOptions {
67 /**
68 * How long the service may take. Use the real provisioning time for the
69 * resource (Composer ~45m, AlloyDB/Cloud SQL ~20m, GKE ~20m), not a test
70 * timeout.
71 * @default "10 minutes"
72 */
73 budget?: Duration.Input;
74 /** Poll interval. @default "5 seconds" */
75 interval?: Duration.Input;
76 }
77
78 /**
79 * Poll `get` until the operation is done, then fail with
80 * {@link OperationFailed} if it reports an error. `get` re-reads the
81 * operation by name with the service's own operations API.
82 */
83 export const waitForOperation = <E extends { readonly _tag: string }, R>(
84 operation: LongRunningOperation,
85 get: (name: string) => Effect.Effect<LongRunningOperation, E, R>,
86 options: WaitOptions = {},
87 ): Effect.Effect<LongRunningOperation, E | OperationFailed | OperationTimedOut, R> => {
88 const budget = Duration.fromInputUnsafe(options.budget ?? "10 minutes");
89 const interval = Duration.fromInputUnsafe(options.interval ?? "5 seconds");
90 const name = operation.name ?? "";
91 const settle = (current: LongRunningOperation) => {
92 const failed = failureOf(current);
93 return failed ? Effect.fail(failed) : Effect.succeed(current);
94 };
95 if (isDone(operation) || name.length === 0) return settle(operation);
96 const times = Math.max(1, Math.ceil(Duration.toMillis(budget) / Duration.toMillis(interval)));
97 return get(name).pipe(
98 Effect.flatMap((current) =>
99 isDone(current)
100 ? Effect.succeed(current)
101 : Effect.fail(new OperationPending({ operation: name })),
102 ),
103 Effect.retry({
104 while: (error) => error._tag === "GCP.OperationPending",
105 schedule: Schedule.spaced(interval),
106 times,
107 }),
108 Effect.catchTag("GCP.OperationPending", () =>
109 Effect.fail(
110 new OperationTimedOut({
111 operation: name,
112 budget: Duration.format(budget),
113 }),
114 ),
115 ),
116 Effect.flatMap(settle),
117 );
118 };