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
| 1 | import { waitForOperation } from "@alchemy.run/gcp/DiscoveryEngine"; |
| 2 | import * as Category from "@distilled.cloud/core/category"; |
| 3 | import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1"; |
| 4 | import * as GcpRetry from "@distilled.cloud/gcp/Retry"; |
| 5 | import * as Effect from "effect/Effect"; |
| 6 | import * as Schedule from "effect/Schedule"; |
| 7 | |
| 8 | /** |
| 9 | * The suite runs ~25 files at once against one project, which overruns the |
| 10 | * per-minute "Data store read requests" quota. Distilled's default backoff |
| 11 | * gives up before the minute rolls over, so the suite rides it out. |
| 12 | */ |
| 13 | export const quotaTolerant = GcpRetry.policy({ |
| 14 | while: (error) => Category.isThrottling(error) || Category.isTransientError(error), |
| 15 | schedule: Schedule.max([Schedule.spaced("10 seconds"), Schedule.recurs(12)]), |
| 16 | }); |
| 17 | |
| 18 | export const DEFAULT_LOCATION = "global"; |
| 19 | export const DEFAULT_COLLECTION = "default_collection"; |
| 20 | |
| 21 | export const collectionParent = (project: string, location = DEFAULT_LOCATION) => |
| 22 | `projects/${project}/locations/${location}/collections/${DEFAULT_COLLECTION}`; |
| 23 | |
| 24 | export const dataStoreName = (project: string, dataStoreId: string, location = DEFAULT_LOCATION) => |
| 25 | `${collectionParent(project, location)}/dataStores/${dataStoreId}`; |
| 26 | |
| 27 | export const getDataStore = (name: string) => |
| 28 | discoveryengine |
| 29 | .getProjectsLocationsCollectionsDataStores({ name }) |
| 30 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 31 | |
| 32 | export const ensureDataStore = ( |
| 33 | project: string, |
| 34 | dataStoreId: string, |
| 35 | options?: { |
| 36 | contentConfig?: string; |
| 37 | industryVertical?: string; |
| 38 | location?: string; |
| 39 | withEngine?: boolean; |
| 40 | skipDefaultSchemaCreation?: boolean; |
| 41 | }, |
| 42 | ) => |
| 43 | Effect.gen(function* () { |
| 44 | const location = options?.location ?? DEFAULT_LOCATION; |
| 45 | const parent = collectionParent(project, location); |
| 46 | const name = dataStoreName(project, dataStoreId, location); |
| 47 | let store = yield* getDataStore(name); |
| 48 | if (store === undefined) { |
| 49 | const created = yield* discoveryengine |
| 50 | .createProjectsLocationsCollectionsDataStores({ |
| 51 | parent, |
| 52 | dataStoreId, |
| 53 | disableCmek: true, |
| 54 | skipDefaultSchemaCreation: options?.skipDefaultSchemaCreation, |
| 55 | body: { |
| 56 | displayName: dataStoreId, |
| 57 | industryVertical: options?.industryVertical ?? "GENERIC", |
| 58 | contentConfig: options?.contentConfig ?? "CONTENT_REQUIRED", |
| 59 | solutionTypes: ["SOLUTION_TYPE_SEARCH"], |
| 60 | }, |
| 61 | }) |
| 62 | .pipe(Effect.catchTag("Conflict", () => Effect.succeed(undefined))); |
| 63 | if (created !== undefined) { |
| 64 | yield* waitForOperation(created); |
| 65 | } |
| 66 | store = yield* discoveryengine.getProjectsLocationsCollectionsDataStores({ name }).pipe( |
| 67 | Effect.retry({ |
| 68 | while: (error) => error._tag === "NotFound", |
| 69 | times: 8, |
| 70 | schedule: Schedule.spaced("2 seconds"), |
| 71 | }), |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | if (options?.withEngine === true) { |
| 76 | const engineId = `${dataStoreId}eng`.replace(/[^a-z0-9]/g, "").slice(0, 63); |
| 77 | const engineName = `${parent}/engines/${engineId}`; |
| 78 | const existingEngine = yield* discoveryengine |
| 79 | .getProjectsLocationsCollectionsEngines({ name: engineName }) |
| 80 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 81 | if (existingEngine === undefined) { |
| 82 | const createdEngine = yield* discoveryengine |
| 83 | .createProjectsLocationsCollectionsEngines({ |
| 84 | parent, |
| 85 | engineId, |
| 86 | body: { |
| 87 | displayName: engineId, |
| 88 | solutionType: "SOLUTION_TYPE_SEARCH", |
| 89 | dataStoreIds: [dataStoreId], |
| 90 | searchEngineConfig: { searchTier: "SEARCH_TIER_STANDARD" }, |
| 91 | }, |
| 92 | }) |
| 93 | .pipe(Effect.catchTag("Conflict", () => Effect.succeed(undefined))); |
| 94 | if (createdEngine !== undefined) { |
| 95 | yield* waitForOperation(createdEngine); |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | return store; |
| 101 | }); |
| 102 | |
| 103 | export const engineNameOf = (project: string, dataStoreId: string, location = DEFAULT_LOCATION) => |
| 104 | `${collectionParent(project, location)}/engines/${dataStoreId.replace(/[^a-z0-9]/g, "").slice(0, 60)}eng`; |
| 105 | |
| 106 | export const deleteEngine = (name: string) => |
| 107 | Effect.gen(function* () { |
| 108 | const existing = yield* discoveryengine |
| 109 | .getProjectsLocationsCollectionsEngines({ name }) |
| 110 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 111 | if (existing === undefined) return; |
| 112 | const operation = yield* discoveryengine |
| 113 | .deleteProjectsLocationsCollectionsEngines({ name }) |
| 114 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 115 | if (operation !== undefined) { |
| 116 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 117 | } |
| 118 | }); |
| 119 | |
| 120 | export const deleteDataStore = (name: string) => |
| 121 | Effect.gen(function* () { |
| 122 | const existing = yield* getDataStore(name); |
| 123 | if (existing === undefined) return; |
| 124 | const operation = yield* discoveryengine |
| 125 | .deleteProjectsLocationsCollectionsDataStores({ name }) |
| 126 | .pipe(Effect.catchTag("NotFound", () => Effect.succeed(undefined))); |
| 127 | if (operation !== undefined) { |
| 128 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 129 | } |
| 130 | }); |
| 131 | |
| 132 | export const deleteSearchParent = ( |
| 133 | project: string, |
| 134 | dataStoreId: string, |
| 135 | location = DEFAULT_LOCATION, |
| 136 | ) => |
| 137 | Effect.gen(function* () { |
| 138 | yield* deleteEngine(engineNameOf(project, dataStoreId, location)); |
| 139 | yield* deleteDataStore(dataStoreName(project, dataStoreId, location)); |
| 140 | }); |