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

main
ts 94 lines 2.83 KB
Raw
1 import * as cloudidentity from "@distilled.cloud/gcp/cloudidentity_v1";
2 import * as Data from "effect/Data";
3 import * as Effect from "effect/Effect";
4 import * as Schedule from "effect/Schedule";
5
6 export class CloudidentityOperationFailed extends Data.TaggedError(
7 "GCP.CloudIdentity.OperationFailed",
8 )<{
9 operation: string;
10 message: string;
11 }> {}
12
13 export class CloudidentityOperationPending extends Data.TaggedError(
14 "GCP.CloudIdentity.OperationPending",
15 )<{
16 operation: string;
17 }> {}
18
19 const alreadyExists = (error: cloudidentity.Status | undefined) => error?.code === 6;
20
21 const isNotFoundStatus = (error: cloudidentity.Status | undefined) => error?.code === 5;
22
23 export const resourceNameFromOperation = (
24 operation: cloudidentity.Operation,
25 ): string | undefined => {
26 const response = operation.response;
27 const responseName = response?.name;
28 if (typeof responseName === "string" && responseName.length > 0) {
29 return responseName;
30 }
31 const metadata = operation.metadata;
32 const target = metadata?.target;
33 if (typeof target === "string" && target.length > 0) {
34 return target;
35 }
36 const metadataName = metadata?.name;
37 if (typeof metadataName === "string" && metadataName.length > 0) {
38 return metadataName;
39 }
40 return undefined;
41 };
42
43 /**
44 * Cloud Identity create/delete/patch return long-running Operations.
45 * Distilled does not expose operations.get, so this helper settles
46 * immediately when `done` is set and otherwise treats a resource name
47 * in `response`/`metadata` as success. Callers poll get/lookup when
48 * the operation is still pending.
49 */
50 export const waitForOperation = (
51 operation: cloudidentity.Operation,
52 options?: { notFoundOk?: boolean },
53 ) =>
54 Effect.gen(function* () {
55 const name = operation.name;
56 if (operation.error) {
57 if (alreadyExists(operation.error)) return operation;
58 if (options?.notFoundOk === true && isNotFoundStatus(operation.error)) {
59 return operation;
60 }
61 return yield* new CloudidentityOperationFailed({
62 operation: name ?? "",
63 message: operation.error.message ?? "operation failed",
64 });
65 }
66 if (operation.done === true) {
67 return operation;
68 }
69 if (resourceNameFromOperation(operation) !== undefined) {
70 return operation;
71 }
72 if (options?.notFoundOk === true) {
73 return operation;
74 }
75 return yield* new CloudidentityOperationPending({
76 operation: name ?? "",
77 });
78 });
79
80 export const waitUntilPresent = <A, E, R>(
81 fetch: Effect.Effect<A | undefined, E, R>,
82 operation = "",
83 ) =>
84 fetch.pipe(
85 Effect.filterOrFail(
86 (value): value is A => value !== undefined,
87 () => new CloudidentityOperationPending({ operation }),
88 ),
89 Effect.retry({
90 while: (error) => error instanceof CloudidentityOperationPending,
91 times: 8,
92 schedule: Schedule.spaced("1 second"),
93 }),
94 );