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 | normalizeLocation, |
| 14 | parseResourceName, |
| 15 | sameStringList, |
| 16 | toResourceId, |
| 17 | } from "./internal.ts"; |
| 18 | import { waitForOperation } from "./operations.ts"; |
| 19 | |
| 20 | export type DataStoreProps = { |
| 21 | /** |
| 22 | * Data store id (the `{dataStore}` segment of the resource name). If |
| 23 | * omitted, a unique RFC-1034 id is generated. Immutable — changing it |
| 24 | * replaces the data store. |
| 25 | */ |
| 26 | dataStoreId?: string; |
| 27 | /** |
| 28 | * Location (`global`, `us`, `eu`, …). Immutable — changing it replaces |
| 29 | * the data store. |
| 30 | * @default "global" |
| 31 | */ |
| 32 | location?: string; |
| 33 | /** |
| 34 | * User-facing display name (max 128 characters). |
| 35 | * @default the data store id |
| 36 | */ |
| 37 | displayName?: string; |
| 38 | /** |
| 39 | * Industry vertical. Immutable — changing it replaces the data store. |
| 40 | * @default "GENERIC" |
| 41 | */ |
| 42 | industryVertical?: discoveryengine.GoogleCloudDiscoveryengineV1DataStoreIndustryVerticalEnum; |
| 43 | /** |
| 44 | * Content config. Immutable — changing it replaces the data store. |
| 45 | * @default "NO_CONTENT" |
| 46 | */ |
| 47 | contentConfig?: discoveryengine.GoogleCloudDiscoveryengineV1DataStoreContentConfigEnum; |
| 48 | /** |
| 49 | * Solutions to enroll. Available values depend on `industryVertical`. |
| 50 | * @default ["SOLUTION_TYPE_SEARCH"] |
| 51 | */ |
| 52 | solutionTypes?: discoveryengine.GoogleCloudDiscoveryengineV1DataStoreSolutionTypesItemEnumList; |
| 53 | /** |
| 54 | * Whether ingested documents carry ACL information. Immutable. |
| 55 | * @default false |
| 56 | */ |
| 57 | aclEnabled?: boolean; |
| 58 | /** |
| 59 | * Skip creating the default schema. Cannot be combined with a starting |
| 60 | * schema. |
| 61 | * @default false |
| 62 | */ |
| 63 | skipDefaultSchemaCreation?: boolean; |
| 64 | /** |
| 65 | * Create an advanced site-search data store. Ignored unless the store |
| 66 | * is GENERIC + PUBLIC_WEBSITE. |
| 67 | * @default false |
| 68 | */ |
| 69 | createAdvancedSiteSearch?: boolean; |
| 70 | /** |
| 71 | * Disable CMEK even if the project has a default CmekConfig. |
| 72 | * @default true |
| 73 | */ |
| 74 | disableCmek?: boolean; |
| 75 | /** |
| 76 | * Resource name of the CmekConfig used to protect this data store. |
| 77 | */ |
| 78 | cmekConfigName?: string; |
| 79 | /** |
| 80 | * Customer-managed KMS key used at creation time. |
| 81 | */ |
| 82 | kmsKeyName?: string; |
| 83 | }; |
| 84 | |
| 85 | export type DataStore = Resource< |
| 86 | "GCP.DiscoveryEngine.DataStore", |
| 87 | DataStoreProps, |
| 88 | { |
| 89 | /** Full resource name. */ |
| 90 | name: string; |
| 91 | /** Data store id (last path segment). */ |
| 92 | dataStoreId: string; |
| 93 | /** Project id. */ |
| 94 | project: string; |
| 95 | /** Location id. */ |
| 96 | location: string; |
| 97 | /** Collection id parsed from the resource name, if present. */ |
| 98 | collectionId: string | undefined; |
| 99 | /** Display name. */ |
| 100 | displayName: string | undefined; |
| 101 | /** Industry vertical. */ |
| 102 | industryVertical: string | undefined; |
| 103 | /** Content config. */ |
| 104 | contentConfig: string | undefined; |
| 105 | /** Enrolled solutions. */ |
| 106 | solutionTypes: string[]; |
| 107 | /** Whether ACL is enabled. */ |
| 108 | aclEnabled: boolean; |
| 109 | /** Default schema id. */ |
| 110 | defaultSchemaId: string | undefined; |
| 111 | /** RFC3339 creation timestamp. */ |
| 112 | createTime: string | undefined; |
| 113 | }, |
| 114 | never, |
| 115 | Providers |
| 116 | >; |
| 117 | |
| 118 | /** |
| 119 | * A Discovery Engine DataStore — a container for Documents. |
| 120 | * |
| 121 | * Without labels, ownership rests on the deterministic id: `read` reports a |
| 122 | * resource it finds without prior state as unowned (adopt it with `--adopt`). |
| 123 | * Location-scoped (`projects/{project}/locations/{location}/dataStores`). Id, |
| 124 | * location, vertical, content config, and ACL are immutable; display name |
| 125 | * updates in place. |
| 126 | * |
| 127 | * ### Creating a Data Store |
| 128 | * **Example:** Generated id, generic search store |
| 129 | * ```typescript |
| 130 | * const store = yield* GCP.DiscoveryEngine.DataStore("Docs", { |
| 131 | * displayName: "docs", |
| 132 | * }); |
| 133 | * ``` |
| 134 | * |
| 135 | * **Example:** Explicit id |
| 136 | * ```typescript |
| 137 | * const store = yield* GCP.DiscoveryEngine.DataStore("Docs", { |
| 138 | * dataStoreId: "app-docs", |
| 139 | * location: "global", |
| 140 | * industryVertical: "GENERIC", |
| 141 | * contentConfig: "NO_CONTENT", |
| 142 | * solutionTypes: ["SOLUTION_TYPE_SEARCH"], |
| 143 | * }); |
| 144 | * ``` |
| 145 | * |
| 146 | * ### Updating a Data Store |
| 147 | * **Example:** Rename |
| 148 | * ```typescript |
| 149 | * // Same logical id as before; only the display name changes. |
| 150 | * const store = yield* GCP.DiscoveryEngine.DataStore("Docs", { |
| 151 | * displayName: "docs-prod", |
| 152 | * }); |
| 153 | * ``` |
| 154 | * |
| 155 | * @resource |
| 156 | * @category DiscoveryEngine |
| 157 | */ |
| 158 | export const DataStore = Resource<DataStore>("GCP.DiscoveryEngine.DataStore"); |
| 159 | |
| 160 | export class DataStoreNotResolved extends Data.TaggedError( |
| 161 | "GCP.DiscoveryEngine.DataStoreNotResolved", |
| 162 | )<{ |
| 163 | resourceName: string; |
| 164 | }> {} |
| 165 | |
| 166 | export class DataStoreStillExists extends Data.TaggedError( |
| 167 | "GCP.DiscoveryEngine.DataStoreStillExists", |
| 168 | )<{ |
| 169 | resourceName: string; |
| 170 | }> {} |
| 171 | |
| 172 | const resourceName = (project: string, location: string, dataStoreId: string) => |
| 173 | `projects/${project}/locations/${location}/dataStores/${dataStoreId}`; |
| 174 | |
| 175 | const verticalOf = ( |
| 176 | value: discoveryengine.GoogleCloudDiscoveryengineV1DataStoreIndustryVerticalEnum | undefined, |
| 177 | ) => value ?? "GENERIC"; |
| 178 | |
| 179 | const contentOf = ( |
| 180 | value: discoveryengine.GoogleCloudDiscoveryengineV1DataStoreContentConfigEnum | undefined, |
| 181 | ) => value ?? "NO_CONTENT"; |
| 182 | |
| 183 | const solutionsOf = ( |
| 184 | value: discoveryengine.GoogleCloudDiscoveryengineV1DataStoreSolutionTypesItemEnumList | undefined, |
| 185 | ): discoveryengine.GoogleCloudDiscoveryengineV1DataStoreSolutionTypesItemEnumList => |
| 186 | value && value.length > 0 ? value : ["SOLUTION_TYPE_SEARCH"]; |
| 187 | |
| 188 | const getByName = (name: string) => |
| 189 | discoveryengine |
| 190 | .getProjectsLocationsDataStores({ name }) |
| 191 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 192 | |
| 193 | const toAttrs = (store: discoveryengine.GoogleCloudDiscoveryengineV1DataStore, project: string) => { |
| 194 | const name = store.name ?? ""; |
| 195 | const parsed = parseResourceName(name, "dataStores"); |
| 196 | return { |
| 197 | name, |
| 198 | dataStoreId: parsed.id, |
| 199 | project: parsed.project || project, |
| 200 | location: parsed.location, |
| 201 | collectionId: name.includes("/collections/") ? parsed.collectionId : undefined, |
| 202 | displayName: store.displayName, |
| 203 | industryVertical: store.industryVertical, |
| 204 | contentConfig: store.contentConfig, |
| 205 | solutionTypes: [...(store.solutionTypes ?? [])], |
| 206 | aclEnabled: store.aclEnabled === true, |
| 207 | defaultSchemaId: store.defaultSchemaId, |
| 208 | createTime: store.createTime, |
| 209 | }; |
| 210 | }; |
| 211 | |
| 212 | const waitUntilExists = (name: string) => |
| 213 | getByName(name).pipe( |
| 214 | Effect.flatMap((store) => |
| 215 | store ? Effect.succeed(store) : Effect.fail(new DataStoreNotResolved({ resourceName: name })), |
| 216 | ), |
| 217 | Effect.retry({ |
| 218 | while: (error) => error._tag === "GCP.DiscoveryEngine.DataStoreNotResolved", |
| 219 | times: 8, |
| 220 | schedule: Schedule.spaced("1 second"), |
| 221 | }), |
| 222 | ); |
| 223 | |
| 224 | const waitUntilGone = (name: string) => |
| 225 | getByName(name).pipe( |
| 226 | Effect.flatMap((store) => |
| 227 | store === undefined |
| 228 | ? Effect.void |
| 229 | : Effect.fail(new DataStoreStillExists({ resourceName: name })), |
| 230 | ), |
| 231 | Effect.retry({ |
| 232 | while: (error) => error._tag === "GCP.DiscoveryEngine.DataStoreStillExists", |
| 233 | times: 10, |
| 234 | schedule: Schedule.spaced("2 seconds"), |
| 235 | }), |
| 236 | ); |
| 237 | |
| 238 | export const DataStoreProvider = () => |
| 239 | Provider.succeed(DataStore, { |
| 240 | stables: [ |
| 241 | "name", |
| 242 | "dataStoreId", |
| 243 | "project", |
| 244 | "location", |
| 245 | "collectionId", |
| 246 | "industryVertical", |
| 247 | "contentConfig", |
| 248 | "createTime", |
| 249 | ], |
| 250 | |
| 251 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 252 | if (!isResolved(news)) return undefined; |
| 253 | const previousId = olds?.dataStoreId ?? output?.dataStoreId; |
| 254 | const nextId = news.dataStoreId ?? previousId; |
| 255 | const previousLocation = normalizeLocation(olds?.location ?? output?.location); |
| 256 | const nextLocation = normalizeLocation(news.location ?? olds?.location ?? output?.location); |
| 257 | const previousVertical = verticalOf( |
| 258 | olds?.industryVertical ?? (output?.industryVertical as DataStoreProps["industryVertical"]), |
| 259 | ); |
| 260 | const nextVertical = verticalOf( |
| 261 | news.industryVertical ?? (output?.industryVertical as DataStoreProps["industryVertical"]), |
| 262 | ); |
| 263 | const previousContent = contentOf( |
| 264 | olds?.contentConfig ?? (output?.contentConfig as DataStoreProps["contentConfig"]), |
| 265 | ); |
| 266 | const nextContent = contentOf( |
| 267 | news.contentConfig ?? (output?.contentConfig as DataStoreProps["contentConfig"]), |
| 268 | ); |
| 269 | const previousAcl = olds?.aclEnabled ?? output?.aclEnabled ?? false; |
| 270 | const nextAcl = news.aclEnabled ?? previousAcl; |
| 271 | const replace = |
| 272 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 273 | previousLocation !== nextLocation || |
| 274 | previousVertical !== nextVertical || |
| 275 | previousContent !== nextContent || |
| 276 | previousAcl !== nextAcl; |
| 277 | if (!replace) return undefined; |
| 278 | return { |
| 279 | action: "replace" as const, |
| 280 | deleteFirst: |
| 281 | previousLocation === nextLocation && previousId !== undefined && nextId === previousId, |
| 282 | }; |
| 283 | }), |
| 284 | |
| 285 | read: Effect.fn(function* ({ id, olds, output }) { |
| 286 | const env = yield* GcpEnvironment.current; |
| 287 | const dataStoreId = yield* toResourceId(id, olds?.dataStoreId, output?.dataStoreId); |
| 288 | const location = normalizeLocation(olds?.location ?? output?.location); |
| 289 | const existing = yield* getByName( |
| 290 | output?.name ?? resourceName(env.project, location, dataStoreId), |
| 291 | ); |
| 292 | if (existing === undefined) return undefined; |
| 293 | const attrs = toAttrs(existing, env.project); |
| 294 | // No labels field: without prior state it may not be ours. |
| 295 | return output !== undefined ? attrs : Unowned(attrs); |
| 296 | }), |
| 297 | |
| 298 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 299 | const env = yield* GcpEnvironment.current; |
| 300 | const location = normalizeLocation(news.location ?? output?.location); |
| 301 | const dataStoreId = yield* toResourceId(id, news.dataStoreId, output?.dataStoreId); |
| 302 | const displayName = news.displayName ?? dataStoreId; |
| 303 | const industryVertical = verticalOf(news.industryVertical); |
| 304 | const contentConfig = contentOf(news.contentConfig); |
| 305 | const solutionTypes = solutionsOf(news.solutionTypes); |
| 306 | const fallbackName = output?.name ?? resourceName(env.project, location, dataStoreId); |
| 307 | |
| 308 | let current = yield* getByName(fallbackName); |
| 309 | |
| 310 | if (current === undefined) { |
| 311 | // The collection-less create endpoint answers 500 "Internal error |
| 312 | // encountered." for every request; the same data store is created |
| 313 | // through the default collection and stays addressable as |
| 314 | // `projects/{p}/locations/{l}/dataStores/{id}`. |
| 315 | const created = yield* discoveryengine |
| 316 | .createProjectsLocationsCollectionsDataStores({ |
| 317 | parent: collectionParent(env.project, location, "default_collection"), |
| 318 | dataStoreId, |
| 319 | skipDefaultSchemaCreation: news.skipDefaultSchemaCreation, |
| 320 | createAdvancedSiteSearch: news.createAdvancedSiteSearch, |
| 321 | disableCmek: news.disableCmek ?? true, |
| 322 | cmekConfigName: news.cmekConfigName, |
| 323 | body: { |
| 324 | displayName, |
| 325 | industryVertical, |
| 326 | contentConfig, |
| 327 | solutionTypes, |
| 328 | aclEnabled: news.aclEnabled === true ? true : undefined, |
| 329 | kmsKeyName: news.kmsKeyName, |
| 330 | }, |
| 331 | }) |
| 332 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 333 | if (created !== undefined) { |
| 334 | yield* waitForOperation(created); |
| 335 | } |
| 336 | current = yield* waitUntilExists(fallbackName); |
| 337 | } |
| 338 | |
| 339 | if (current === undefined) { |
| 340 | return yield* new DataStoreNotResolved({ resourceName: fallbackName }); |
| 341 | } |
| 342 | |
| 343 | const name = current.name ?? fallbackName; |
| 344 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 345 | const solutionsChanged = !sameStringList(current.solutionTypes, solutionTypes); |
| 346 | |
| 347 | if (displayNameChanged || solutionsChanged) { |
| 348 | current = yield* discoveryengine.patchProjectsLocationsDataStores({ |
| 349 | name, |
| 350 | updateMask: [ |
| 351 | displayNameChanged ? "display_name" : undefined, |
| 352 | solutionsChanged ? "solution_types" : undefined, |
| 353 | ] |
| 354 | .filter((field): field is string => field !== undefined) |
| 355 | .join(","), |
| 356 | body: { |
| 357 | name, |
| 358 | displayName, |
| 359 | solutionTypes, |
| 360 | }, |
| 361 | }); |
| 362 | } |
| 363 | |
| 364 | return toAttrs(current, env.project); |
| 365 | }), |
| 366 | |
| 367 | delete: Effect.fn(function* ({ output }) { |
| 368 | const existing = yield* getByName(output.name); |
| 369 | if (existing === undefined) return; |
| 370 | const operation = yield* discoveryengine |
| 371 | .deleteProjectsLocationsDataStores({ name: output.name }) |
| 372 | .pipe( |
| 373 | Effect.retry({ |
| 374 | while: (error) => error._tag === "Conflict", |
| 375 | times: 8, |
| 376 | schedule: Schedule.spaced("2 seconds"), |
| 377 | }), |
| 378 | Effect.catchTag("NotFound", () => Effect.undefined), |
| 379 | ); |
| 380 | if (operation !== undefined) { |
| 381 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 382 | } |
| 383 | yield* waitUntilGone(output.name); |
| 384 | }), |
| 385 | }); |