@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 48 lines 1.75 KB
Raw
1 import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1";
2 import * as Effect from "effect/Effect";
3 import { waitForOperation as waitForGcpOperation } from "../Operation.ts";
4
5 export const resourceNameFromOperation = (
6 operation: discoveryengine.GoogleLongrunningOperation,
7 ): string | undefined => {
8 const response = operation.response;
9 if (response && typeof response === "object" && "name" in response) {
10 const name = (response as { name?: unknown }).name;
11 if (typeof name === "string" && name.length > 0) return name;
12 }
13 const metadata = operation.metadata;
14 if (metadata && typeof metadata === "object") {
15 const record = metadata as { target?: unknown; name?: unknown };
16 if (typeof record.target === "string" && record.target.length > 0) {
17 return record.target;
18 }
19 if (typeof record.name === "string" && record.name.length > 0) {
20 return record.name;
21 }
22 }
23 return undefined;
24 };
25
26 /**
27 * Wait for a Discovery Engine operation; data store and engine creation
28 * take a few minutes. ALREADY_EXISTS (code 6) counts as success (create
29 * race); with `notFoundOk`, so does NOT_FOUND (code 5, delete race) and an
30 * operation that is already gone.
31 */
32 export const waitForOperation = (
33 operation: discoveryengine.GoogleLongrunningOperation,
34 options?: { notFoundOk?: boolean },
35 ) =>
36 waitForGcpOperation(
37 operation,
38 (name) => discoveryengine.getProjectsLocationsOperations({ name }),
39 { budget: "10 minutes" },
40 ).pipe(
41 Effect.catchIf(
42 (error) =>
43 (error._tag === "GCP.OperationFailed" &&
44 (error.code === 6 || (options?.notFoundOk === true && error.code === 5))) ||
45 (options?.notFoundOk === true && error._tag === "NotFound"),
46 () => Effect.succeed(operation),
47 ),
48 );