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
| 1 | import * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import * as Provider from "alchemy/Provider"; |
| 5 | import { Resource } from "alchemy/Resource"; |
| 6 | import * as Data from "effect/Data"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schedule from "effect/Schedule"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { |
| 12 | collectionParent, |
| 13 | dataStoreName, |
| 14 | normalizeCollection, |
| 15 | normalizeLocation, |
| 16 | parseResourceName, |
| 17 | toResourceId, |
| 18 | } from "./internal.ts"; |
| 19 | import { waitForOperation } from "./operations.ts"; |
| 20 | |
| 21 | export type CollectionsDataStoreProps = { |
| 22 | /** |
| 23 | * Data store id (the `{data_store}` segment). If omitted, a unique |
| 24 | * RFC-1034 id is generated. Immutable — changing it replaces the |
| 25 | * data store. |
| 26 | */ |
| 27 | dataStoreId?: string; |
| 28 | /** |
| 29 | * Location (`global`, `us`, `eu`). Immutable — changing it replaces |
| 30 | * the data store. |
| 31 | * @default "global" |
| 32 | */ |
| 33 | location?: string; |
| 34 | /** |
| 35 | * Collection id. Immutable — changing it replaces the data store. |
| 36 | * @default "default_collection" |
| 37 | */ |
| 38 | collection?: string; |
| 39 | /** |
| 40 | * User-facing display name (max 128 characters). |
| 41 | * @default the data store id |
| 42 | */ |
| 43 | displayName?: string; |
| 44 | /** |
| 45 | * Industry vertical. Immutable. |
| 46 | * @default "GENERIC" |
| 47 | */ |
| 48 | industryVertical?: |
| 49 | | "INDUSTRY_VERTICAL_UNSPECIFIED" |
| 50 | | "GENERIC" |
| 51 | | "MEDIA" |
| 52 | | "HEALTHCARE_FHIR" |
| 53 | | (string & {}); |
| 54 | /** |
| 55 | * Content config. Immutable. |
| 56 | * @default "NO_CONTENT" |
| 57 | */ |
| 58 | contentConfig?: |
| 59 | | "CONTENT_CONFIG_UNSPECIFIED" |
| 60 | | "NO_CONTENT" |
| 61 | | "CONTENT_REQUIRED" |
| 62 | | "PUBLIC_WEBSITE" |
| 63 | | "GOOGLE_WORKSPACE" |
| 64 | | (string & {}); |
| 65 | /** |
| 66 | * Solutions this data store enrolls. |
| 67 | * @default ["SOLUTION_TYPE_SEARCH"] |
| 68 | */ |
| 69 | solutionTypes?: Array< |
| 70 | | "SOLUTION_TYPE_UNSPECIFIED" |
| 71 | | "SOLUTION_TYPE_RECOMMENDATION" |
| 72 | | "SOLUTION_TYPE_SEARCH" |
| 73 | | "SOLUTION_TYPE_CHAT" |
| 74 | | "SOLUTION_TYPE_GENERATIVE_CHAT" |
| 75 | | "SOLUTION_TYPE_AI_MODE" |
| 76 | | (string & {}) |
| 77 | >; |
| 78 | /** |
| 79 | * Whether ingested documents carry ACL information. Immutable. |
| 80 | * @default false |
| 81 | */ |
| 82 | aclEnabled?: boolean; |
| 83 | /** |
| 84 | * Skip creating the default schema. Cannot be combined with |
| 85 | * `startingSchema`. |
| 86 | * @default false |
| 87 | */ |
| 88 | skipDefaultSchemaCreation?: boolean; |
| 89 | /** |
| 90 | * Create an advanced site-search data store. Ignored unless |
| 91 | * `contentConfig` is `PUBLIC_WEBSITE`. |
| 92 | * @default false |
| 93 | */ |
| 94 | createAdvancedSiteSearch?: boolean; |
| 95 | /** |
| 96 | * Disable CMEK even if the project has a default CmekConfig. |
| 97 | * @default true |
| 98 | */ |
| 99 | disableCmek?: boolean; |
| 100 | /** |
| 101 | * Input-only CMEK key resource name used at create time. |
| 102 | */ |
| 103 | kmsKeyName?: string; |
| 104 | /** |
| 105 | * Starting schema applied only on create. |
| 106 | */ |
| 107 | startingSchema?: { |
| 108 | jsonSchema?: string; |
| 109 | structSchema?: Record<string, unknown>; |
| 110 | }; |
| 111 | /** |
| 112 | * When true, the data store is not available for serving search |
| 113 | * requests. |
| 114 | */ |
| 115 | disabledForServing?: boolean; |
| 116 | }; |
| 117 | |
| 118 | export type CollectionsDataStore = Resource< |
| 119 | "GCP.DiscoveryEngine.CollectionsDataStore", |
| 120 | CollectionsDataStoreProps, |
| 121 | { |
| 122 | /** Full resource name. */ |
| 123 | name: string; |
| 124 | /** Data store id (last path segment). */ |
| 125 | dataStoreId: string; |
| 126 | /** Project id. */ |
| 127 | project: string; |
| 128 | /** Location id. */ |
| 129 | location: string; |
| 130 | /** Collection id. */ |
| 131 | collection: string; |
| 132 | /** Display name. */ |
| 133 | displayName: string | undefined; |
| 134 | /** Industry vertical. */ |
| 135 | industryVertical: string | undefined; |
| 136 | /** Content config. */ |
| 137 | contentConfig: string | undefined; |
| 138 | /** Enrolled solution types. */ |
| 139 | solutionTypes: string[]; |
| 140 | /** Whether ACL is enabled. */ |
| 141 | aclEnabled: boolean; |
| 142 | /** Default schema id, if any. */ |
| 143 | defaultSchemaId: string | undefined; |
| 144 | /** Whether serving is disabled. */ |
| 145 | disabledForServing: boolean; |
| 146 | /** RFC3339 creation timestamp. */ |
| 147 | createTime: string | undefined; |
| 148 | }, |
| 149 | never, |
| 150 | Providers |
| 151 | >; |
| 152 | |
| 153 | /** |
| 154 | * A Discovery Engine data store under a collection. |
| 155 | * |
| 156 | * Without labels, ownership rests on the deterministic id: `read` reports a |
| 157 | * resource it finds without prior state as unowned (adopt it with `--adopt`). |
| 158 | * Location, collection, data store id, industry vertical, content config, and |
| 159 | * ACL are immutable. Display name and serving-disabled flag update in place. |
| 160 | * |
| 161 | * ### Creating a Data Store |
| 162 | * **Example:** Generated id, generic search store |
| 163 | * ```typescript |
| 164 | * const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Docs", { |
| 165 | * displayName: "product docs", |
| 166 | * }); |
| 167 | * ``` |
| 168 | * |
| 169 | * **Example:** Site-search store |
| 170 | * ```typescript |
| 171 | * const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Web", { |
| 172 | * contentConfig: "PUBLIC_WEBSITE", |
| 173 | * displayName: "public site", |
| 174 | * }); |
| 175 | * ``` |
| 176 | * |
| 177 | * ### Updating a Data Store |
| 178 | * **Example:** Rename |
| 179 | * ```typescript |
| 180 | * // Same logical id as before; only the changed props differ. |
| 181 | * const store = yield* GCP.DiscoveryEngine.CollectionsDataStore("Docs", { |
| 182 | * displayName: "product docs v2", |
| 183 | * }); |
| 184 | * ``` |
| 185 | * |
| 186 | * @resource |
| 187 | * @category DiscoveryEngine |
| 188 | */ |
| 189 | export const CollectionsDataStore = Resource<CollectionsDataStore>( |
| 190 | "GCP.DiscoveryEngine.CollectionsDataStore", |
| 191 | ); |
| 192 | |
| 193 | export class CollectionsDataStoreNotResolved extends Data.TaggedError( |
| 194 | "GCP.DiscoveryEngine.CollectionsDataStoreNotResolved", |
| 195 | )<{ |
| 196 | resourceName: string; |
| 197 | }> {} |
| 198 | |
| 199 | export class CollectionsDataStoreStillExists extends Data.TaggedError( |
| 200 | "GCP.DiscoveryEngine.CollectionsDataStoreStillExists", |
| 201 | )<{ |
| 202 | resourceName: string; |
| 203 | }> {} |
| 204 | |
| 205 | const verticalOf = (value: string | undefined) => value ?? "GENERIC"; |
| 206 | const contentOf = (value: string | undefined) => value ?? "NO_CONTENT"; |
| 207 | const solutionsOf = (value: readonly string[] | undefined) => |
| 208 | value && value.length > 0 ? [...value] : ["SOLUTION_TYPE_SEARCH"]; |
| 209 | |
| 210 | const toAttrs = (store: discoveryengine.GoogleCloudDiscoveryengineV1DataStore, project: string) => { |
| 211 | const name = store.name ?? ""; |
| 212 | const parsed = parseResourceName(name, "dataStores"); |
| 213 | return { |
| 214 | name, |
| 215 | dataStoreId: parsed.id, |
| 216 | project: parsed.project || project, |
| 217 | location: parsed.location, |
| 218 | collection: parsed.collection, |
| 219 | displayName: store.displayName, |
| 220 | industryVertical: store.industryVertical, |
| 221 | contentConfig: store.contentConfig, |
| 222 | solutionTypes: [...(store.solutionTypes ?? [])], |
| 223 | aclEnabled: store.aclEnabled === true, |
| 224 | defaultSchemaId: store.defaultSchemaId, |
| 225 | disabledForServing: store.servingConfigDataStore?.disabledForServing === true, |
| 226 | createTime: store.createTime, |
| 227 | }; |
| 228 | }; |
| 229 | |
| 230 | const getByName = (name: string) => |
| 231 | name.length === 0 |
| 232 | ? Effect.undefined |
| 233 | : discoveryengine |
| 234 | .getProjectsLocationsCollectionsDataStores({ name }) |
| 235 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 236 | |
| 237 | const waitUntilExists = (name: string) => |
| 238 | getByName(name).pipe( |
| 239 | Effect.flatMap((store) => |
| 240 | store |
| 241 | ? Effect.succeed(store) |
| 242 | : Effect.fail(new CollectionsDataStoreNotResolved({ resourceName: name })), |
| 243 | ), |
| 244 | Effect.retry({ |
| 245 | while: (error) => error._tag === "GCP.DiscoveryEngine.CollectionsDataStoreNotResolved", |
| 246 | times: 8, |
| 247 | schedule: Schedule.spaced("2 seconds"), |
| 248 | }), |
| 249 | ); |
| 250 | |
| 251 | const waitUntilGone = (name: string) => |
| 252 | getByName(name).pipe( |
| 253 | Effect.flatMap((store) => |
| 254 | store === undefined |
| 255 | ? Effect.void |
| 256 | : Effect.fail(new CollectionsDataStoreStillExists({ resourceName: name })), |
| 257 | ), |
| 258 | Effect.retry({ |
| 259 | while: (error) => error._tag === "GCP.DiscoveryEngine.CollectionsDataStoreStillExists", |
| 260 | times: 10, |
| 261 | schedule: Schedule.spaced("3 seconds"), |
| 262 | }), |
| 263 | ); |
| 264 | |
| 265 | export const CollectionsDataStoreProvider = () => |
| 266 | Provider.succeed(CollectionsDataStore, { |
| 267 | stables: [ |
| 268 | "name", |
| 269 | "dataStoreId", |
| 270 | "project", |
| 271 | "location", |
| 272 | "collection", |
| 273 | "industryVertical", |
| 274 | "contentConfig", |
| 275 | "createTime", |
| 276 | ], |
| 277 | |
| 278 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 279 | if (!isResolved(news)) return undefined; |
| 280 | const previousId = olds?.dataStoreId ?? output?.dataStoreId; |
| 281 | const nextId = news.dataStoreId ?? previousId; |
| 282 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 283 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 284 | const previousCollection = normalizeCollection(olds?.collection ?? output?.collection); |
| 285 | const nextCollection = normalizeCollection( |
| 286 | news.collection ?? olds?.collection ?? output?.collection, |
| 287 | ); |
| 288 | const previousVertical = verticalOf(olds?.industryVertical ?? output?.industryVertical); |
| 289 | const nextVertical = verticalOf( |
| 290 | news.industryVertical ?? olds?.industryVertical ?? output?.industryVertical, |
| 291 | ); |
| 292 | const previousContent = contentOf(olds?.contentConfig ?? output?.contentConfig); |
| 293 | const nextContent = contentOf( |
| 294 | news.contentConfig ?? olds?.contentConfig ?? output?.contentConfig, |
| 295 | ); |
| 296 | const previousAcl = olds?.aclEnabled ?? output?.aclEnabled ?? false; |
| 297 | const nextAcl = news.aclEnabled ?? previousAcl; |
| 298 | const replace = |
| 299 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 300 | previousLocation !== nextLocation || |
| 301 | previousCollection !== nextCollection || |
| 302 | previousVertical !== nextVertical || |
| 303 | previousContent !== nextContent || |
| 304 | previousAcl !== nextAcl; |
| 305 | if (!replace) return undefined; |
| 306 | return { |
| 307 | action: "replace" as const, |
| 308 | deleteFirst: |
| 309 | previousLocation === nextLocation && |
| 310 | previousCollection === nextCollection && |
| 311 | previousId !== undefined && |
| 312 | nextId === previousId, |
| 313 | }; |
| 314 | }), |
| 315 | |
| 316 | read: Effect.fn(function* ({ id, olds, output }) { |
| 317 | const env = yield* GcpEnvironment.current; |
| 318 | const dataStoreId = yield* toResourceId(id, olds?.dataStoreId, output?.dataStoreId); |
| 319 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 320 | const collection = normalizeCollection(olds?.collection ?? output?.collection); |
| 321 | const existing = yield* getByName( |
| 322 | output?.name ?? dataStoreName(env.project, location, collection, dataStoreId), |
| 323 | ); |
| 324 | if (existing === undefined) return undefined; |
| 325 | const attrs = toAttrs(existing, env.project); |
| 326 | // No labels field: without prior state it may not be ours. |
| 327 | return output !== undefined ? attrs : Unowned(attrs); |
| 328 | }), |
| 329 | |
| 330 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 331 | const env = yield* GcpEnvironment.current; |
| 332 | const location = normalizeLocation(news.location ?? output?.location); |
| 333 | const collection = normalizeCollection(news.collection ?? output?.collection); |
| 334 | const dataStoreId = yield* toResourceId(id, news.dataStoreId, output?.dataStoreId); |
| 335 | const name = dataStoreName(env.project, location, collection, dataStoreId); |
| 336 | const displayName = news.displayName ?? dataStoreId; |
| 337 | const industryVertical = verticalOf(news.industryVertical); |
| 338 | const contentConfig = contentOf(news.contentConfig); |
| 339 | const solutionTypes = solutionsOf(news.solutionTypes); |
| 340 | const desiredDisabled = news.disabledForServing === true; |
| 341 | |
| 342 | let current = yield* getByName(output?.name ?? name); |
| 343 | |
| 344 | if (current === undefined) { |
| 345 | const created = yield* discoveryengine |
| 346 | .createProjectsLocationsCollectionsDataStores({ |
| 347 | parent: collectionParent(env.project, location, collection), |
| 348 | dataStoreId, |
| 349 | skipDefaultSchemaCreation: news.skipDefaultSchemaCreation, |
| 350 | createAdvancedSiteSearch: news.createAdvancedSiteSearch, |
| 351 | disableCmek: news.disableCmek !== false, |
| 352 | body: { |
| 353 | displayName, |
| 354 | industryVertical, |
| 355 | contentConfig, |
| 356 | solutionTypes, |
| 357 | aclEnabled: news.aclEnabled === true ? true : undefined, |
| 358 | kmsKeyName: news.kmsKeyName, |
| 359 | startingSchema: news.startingSchema, |
| 360 | servingConfigDataStore: |
| 361 | news.disabledForServing !== undefined |
| 362 | ? { disabledForServing: desiredDisabled } |
| 363 | : undefined, |
| 364 | }, |
| 365 | }) |
| 366 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 367 | if (created !== undefined) { |
| 368 | yield* waitForOperation(created); |
| 369 | } |
| 370 | current = yield* waitUntilExists(name); |
| 371 | } |
| 372 | |
| 373 | if (current === undefined) { |
| 374 | return yield* new CollectionsDataStoreNotResolved({ resourceName: name }); |
| 375 | } |
| 376 | |
| 377 | const resource = current.name ?? name; |
| 378 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 379 | const observedDisabled = current.servingConfigDataStore?.disabledForServing === true; |
| 380 | const servingChanged = |
| 381 | news.disabledForServing !== undefined && observedDisabled !== desiredDisabled; |
| 382 | |
| 383 | if (displayNameChanged || servingChanged) { |
| 384 | current = yield* discoveryengine.patchProjectsLocationsCollectionsDataStores({ |
| 385 | name: resource, |
| 386 | updateMask: [ |
| 387 | displayNameChanged ? "display_name" : undefined, |
| 388 | servingChanged ? "serving_config_data_store.disabled_for_serving" : undefined, |
| 389 | ] |
| 390 | .filter((field): field is string => field !== undefined) |
| 391 | .join(","), |
| 392 | body: { |
| 393 | name: resource, |
| 394 | displayName, |
| 395 | servingConfigDataStore: servingChanged |
| 396 | ? { disabledForServing: desiredDisabled } |
| 397 | : current.servingConfigDataStore, |
| 398 | }, |
| 399 | }); |
| 400 | } |
| 401 | |
| 402 | return toAttrs(current, env.project); |
| 403 | }), |
| 404 | |
| 405 | delete: Effect.fn(function* ({ output }) { |
| 406 | const existing = yield* getByName(output.name); |
| 407 | if (existing === undefined) return; |
| 408 | const operation = yield* discoveryengine |
| 409 | .deleteProjectsLocationsCollectionsDataStores({ name: output.name }) |
| 410 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 411 | if (operation !== undefined) { |
| 412 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 413 | } |
| 414 | yield* waitUntilGone(output.name); |
| 415 | }), |
| 416 | }); |