@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 55 lines 1.76 KB
Raw
1 import * as appengine from "@distilled.cloud/gcp/appengine_v1";
2 import * as Effect from "effect/Effect";
3 import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
4
5 const lastSegment = (value: string) => {
6 const trimmed = value.replace(/\/+$/, "");
7 const parts = trimmed.split("/");
8 return parts[parts.length - 1] || trimmed;
9 };
10
11 export const parseOperationName = (name: string) => {
12 const parts = name.split("/").filter((part) => part.length > 0);
13 const appsAt = parts.indexOf("apps");
14 const operationsAt = parts.lastIndexOf("operations");
15 return {
16 appsId: appsAt >= 0 ? parts[appsAt + 1] : undefined,
17 operationsId: operationsAt >= 0 ? parts[operationsAt + 1] : lastSegment(name),
18 };
19 };
20
21 /**
22 * Wait for an App Engine operation. Version deploys take several minutes.
23 * `notFoundOk` accepts an operation that finished with `NOT_FOUND` (5) or
24 * has itself been garbage-collected.
25 */
26 export const waitForOperation = (
27 operation: appengine.Operation,
28 options: { appsId: string; notFoundOk?: boolean },
29 ) =>
30 waitForGcpOperation(
31 operation,
32 (name) => {
33 const parsed = parseOperationName(name);
34 return appengine
35 .getAppsOperations({
36 appsId: parsed.appsId ?? options.appsId,
37 operationsId: parsed.operationsId ?? lastSegment(name),
38 })
39 .pipe(
40 Effect.catchTag("NotFound", (error) =>
41 options.notFoundOk === true
42 ? Effect.succeed<appengine.Operation>({ name, done: true })
43 : Effect.fail(error),
44 ),
45 );
46 },
47 { budget: "20 minutes" },
48 ).pipe(
49 Effect.catchIf(
50 (error) =>
51 error._tag === "GCP.OperationFailed" && options.notFoundOk === true && error.code === 5,
52 () => Effect.void,
53 ),
54 Effect.asVoid,
55 );