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 * as discoveryengine from "@distilled.cloud/gcp/discoveryengine_v1"; |
| 2 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 3 | import { isResolved } from "alchemy/Diff"; |
| 4 | import { createPhysicalName } from "alchemy/PhysicalName"; |
| 5 | import * as Provider from "alchemy/Provider"; |
| 6 | import { Resource } from "alchemy/Resource"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import type { Providers } from "../Providers.ts"; |
| 11 | import { parentBefore, parseResourceName, sameStringList, servingConfigId } from "./internal.ts"; |
| 12 | |
| 13 | export type CollectionsEnginesServingConfigProps = { |
| 14 | /** |
| 15 | * Parent Engine resource name |
| 16 | * `projects/{project}/locations/{location}/collections/{collection}/engines/{engine}`. |
| 17 | * Immutable — changing it replaces the serving config. |
| 18 | */ |
| 19 | engine: string; |
| 20 | /** |
| 21 | * Serving config id (4-63 alphanumeric characters). If omitted, a |
| 22 | * unique id is generated. Immutable — changing it replaces the |
| 23 | * serving config. |
| 24 | */ |
| 25 | servingConfigId?: string; |
| 26 | /** |
| 27 | * Human-readable name (max 128 characters). |
| 28 | */ |
| 29 | displayName?: string; |
| 30 | /** |
| 31 | * Solution type. Immutable. |
| 32 | * @default "SOLUTION_TYPE_SEARCH" |
| 33 | */ |
| 34 | solutionType?: discoveryengine.GoogleCloudDiscoveryengineV1ServingConfigSolutionTypeEnum; |
| 35 | /** |
| 36 | * Custom ranking expression. |
| 37 | */ |
| 38 | rankingExpression?: string; |
| 39 | /** |
| 40 | * Recommendation model id. Required for `SOLUTION_TYPE_RECOMMENDATION`. |
| 41 | */ |
| 42 | modelId?: string; |
| 43 | /** |
| 44 | * Diversity level for recommendation results. |
| 45 | */ |
| 46 | diversityLevel?: string; |
| 47 | /** |
| 48 | * Boost control ids applied at serving time. |
| 49 | */ |
| 50 | boostControlIds?: string[]; |
| 51 | /** |
| 52 | * Filter control ids applied at serving time. |
| 53 | */ |
| 54 | filterControlIds?: string[]; |
| 55 | /** |
| 56 | * Synonyms control ids. |
| 57 | */ |
| 58 | synonymsControlIds?: string[]; |
| 59 | /** |
| 60 | * Redirect control ids. |
| 61 | */ |
| 62 | redirectControlIds?: string[]; |
| 63 | }; |
| 64 | |
| 65 | export type CollectionsEnginesServingConfig = Resource< |
| 66 | "GCP.DiscoveryEngine.CollectionsEnginesServingConfig", |
| 67 | CollectionsEnginesServingConfigProps, |
| 68 | { |
| 69 | /** Full resource name. */ |
| 70 | name: string; |
| 71 | /** Serving config id (last path segment). */ |
| 72 | servingConfigId: string; |
| 73 | /** Parent engine resource name. */ |
| 74 | engine: string; |
| 75 | /** Project id. */ |
| 76 | project: string; |
| 77 | /** Location id. */ |
| 78 | location: string; |
| 79 | /** Collection id. */ |
| 80 | collectionId: string; |
| 81 | /** Display name. */ |
| 82 | displayName: string | undefined; |
| 83 | /** Solution type. */ |
| 84 | solutionType: string | undefined; |
| 85 | /** Ranking expression. */ |
| 86 | rankingExpression: string | undefined; |
| 87 | /** Model id. */ |
| 88 | modelId: string | undefined; |
| 89 | /** RFC3339 creation timestamp. */ |
| 90 | createTime: string | undefined; |
| 91 | /** RFC3339 last-update timestamp. */ |
| 92 | updateTime: string | undefined; |
| 93 | }, |
| 94 | never, |
| 95 | Providers |
| 96 | >; |
| 97 | |
| 98 | /** |
| 99 | * A Discovery Engine ServingConfig on a collection Engine. |
| 100 | * |
| 101 | * Without labels, ownership rests on the deterministic id: `read` reports a |
| 102 | * resource it finds without prior state as unowned (adopt it with `--adopt`). |
| 103 | * Additional serving configs are API-only (the console uses the default). |
| 104 | * Parent engine, id, and solution type are immutable; display name, ranking, |
| 105 | * and control lists update in place. |
| 106 | * |
| 107 | * ### Creating a Serving Config |
| 108 | * **Example:** Extra search serving config |
| 109 | * ```typescript |
| 110 | * const serving = |
| 111 | * yield* GCP.DiscoveryEngine.CollectionsEnginesServingConfig("Primary", { |
| 112 | * engine: engine.name, |
| 113 | * displayName: "primary", |
| 114 | * }); |
| 115 | * ``` |
| 116 | * |
| 117 | * ### Updating a Serving Config |
| 118 | * **Example:** Rename |
| 119 | * ```typescript |
| 120 | * // Same logical id as before; only the changed props differ. |
| 121 | * const serving = |
| 122 | * yield* GCP.DiscoveryEngine.CollectionsEnginesServingConfig("Primary", { |
| 123 | * engine: engine.name, |
| 124 | * displayName: "primary-prod", |
| 125 | * }); |
| 126 | * ``` |
| 127 | * |
| 128 | * @resource |
| 129 | * @category DiscoveryEngine |
| 130 | */ |
| 131 | export const CollectionsEnginesServingConfig = Resource<CollectionsEnginesServingConfig>( |
| 132 | "GCP.DiscoveryEngine.CollectionsEnginesServingConfig", |
| 133 | ); |
| 134 | |
| 135 | export class CollectionsEnginesServingConfigNotResolved extends Data.TaggedError( |
| 136 | "GCP.DiscoveryEngine.CollectionsEnginesServingConfigNotResolved", |
| 137 | )<{ |
| 138 | resourceName: string; |
| 139 | }> {} |
| 140 | |
| 141 | const resourceName = (engine: string, servingConfigId: string) => |
| 142 | `${engine}/servingConfigs/${servingConfigId}`; |
| 143 | |
| 144 | const solutionOf = ( |
| 145 | value: discoveryengine.GoogleCloudDiscoveryengineV1ServingConfigSolutionTypeEnum | undefined, |
| 146 | ) => value ?? "SOLUTION_TYPE_SEARCH"; |
| 147 | |
| 148 | const toId = (id: string, explicit: string | undefined, existing: string | undefined) => |
| 149 | Effect.gen(function* () { |
| 150 | if (explicit !== undefined) return servingConfigId(explicit); |
| 151 | if (existing !== undefined) return existing; |
| 152 | return servingConfigId( |
| 153 | yield* createPhysicalName({ |
| 154 | id, |
| 155 | maxLength: 63, |
| 156 | lowercase: true, |
| 157 | }), |
| 158 | ); |
| 159 | }); |
| 160 | |
| 161 | const getByName = (name: string) => |
| 162 | discoveryengine |
| 163 | .getProjectsLocationsCollectionsEnginesServingConfigs({ name }) |
| 164 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 165 | |
| 166 | const toAttrs = ( |
| 167 | config: discoveryengine.GoogleCloudDiscoveryengineV1ServingConfig, |
| 168 | project: string, |
| 169 | ) => { |
| 170 | const name = config.name ?? ""; |
| 171 | const parsed = parseResourceName(name, "servingConfigs"); |
| 172 | return { |
| 173 | name, |
| 174 | servingConfigId: parsed.id, |
| 175 | engine: parentBefore(name, "servingConfigs"), |
| 176 | project: parsed.project || project, |
| 177 | location: parsed.location, |
| 178 | collectionId: parsed.collectionId, |
| 179 | displayName: config.displayName, |
| 180 | solutionType: config.solutionType, |
| 181 | rankingExpression: config.rankingExpression, |
| 182 | modelId: config.modelId, |
| 183 | createTime: config.createTime, |
| 184 | updateTime: config.updateTime, |
| 185 | }; |
| 186 | }; |
| 187 | |
| 188 | export const CollectionsEnginesServingConfigProvider = () => |
| 189 | Provider.succeed(CollectionsEnginesServingConfig, { |
| 190 | stables: [ |
| 191 | "name", |
| 192 | "servingConfigId", |
| 193 | "engine", |
| 194 | "project", |
| 195 | "location", |
| 196 | "collectionId", |
| 197 | "solutionType", |
| 198 | "createTime", |
| 199 | ], |
| 200 | |
| 201 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 202 | if (!isResolved(news)) return undefined; |
| 203 | const previousEngine = olds?.engine ?? output?.engine; |
| 204 | const previousId = olds?.servingConfigId ?? output?.servingConfigId; |
| 205 | const nextId = news.servingConfigId ?? previousId; |
| 206 | const previousSolution = solutionOf( |
| 207 | olds?.solutionType ?? |
| 208 | (output?.solutionType as CollectionsEnginesServingConfigProps["solutionType"]), |
| 209 | ); |
| 210 | const nextSolution = solutionOf( |
| 211 | news.solutionType ?? |
| 212 | (output?.solutionType as CollectionsEnginesServingConfigProps["solutionType"]), |
| 213 | ); |
| 214 | if ( |
| 215 | (previousEngine !== undefined && news.engine !== previousEngine) || |
| 216 | (previousId !== undefined && nextId !== undefined && nextId !== previousId) || |
| 217 | previousSolution !== nextSolution |
| 218 | ) { |
| 219 | return { action: "replace" as const, deleteFirst: false }; |
| 220 | } |
| 221 | return undefined; |
| 222 | }), |
| 223 | |
| 224 | read: Effect.fn(function* ({ id, olds, output }) { |
| 225 | const env = yield* GcpEnvironment.current; |
| 226 | const engine = olds?.engine ?? output?.engine; |
| 227 | const idValue = yield* toId(id, olds?.servingConfigId, output?.servingConfigId); |
| 228 | const name = |
| 229 | output?.name ?? (engine !== undefined ? resourceName(engine, idValue) : undefined); |
| 230 | if (name === undefined) return undefined; |
| 231 | const existing = yield* getByName(name); |
| 232 | if (existing === undefined) return undefined; |
| 233 | const attrs = toAttrs(existing, env.project); |
| 234 | // No labels field: without prior state it may not be ours. |
| 235 | return output !== undefined ? attrs : Unowned(attrs); |
| 236 | }), |
| 237 | |
| 238 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 239 | const env = yield* GcpEnvironment.current; |
| 240 | const idValue = yield* toId(id, news.servingConfigId, output?.servingConfigId); |
| 241 | const displayName = news.displayName ?? idValue; |
| 242 | const solutionType = solutionOf(news.solutionType); |
| 243 | const fallbackName = output?.name ?? resourceName(news.engine, idValue); |
| 244 | |
| 245 | let current = yield* getByName(fallbackName); |
| 246 | |
| 247 | if (current === undefined) { |
| 248 | const created = yield* discoveryengine |
| 249 | .createProjectsLocationsCollectionsEnginesServingConfigs({ |
| 250 | parent: news.engine, |
| 251 | servingConfigId: idValue, |
| 252 | body: { |
| 253 | displayName, |
| 254 | solutionType, |
| 255 | rankingExpression: news.rankingExpression, |
| 256 | modelId: news.modelId, |
| 257 | diversityLevel: news.diversityLevel, |
| 258 | boostControlIds: news.boostControlIds, |
| 259 | filterControlIds: news.filterControlIds, |
| 260 | synonymsControlIds: news.synonymsControlIds, |
| 261 | redirectControlIds: news.redirectControlIds, |
| 262 | }, |
| 263 | }) |
| 264 | .pipe(Effect.catchTag("Conflict", () => getByName(fallbackName))); |
| 265 | current = created ?? undefined; |
| 266 | } |
| 267 | |
| 268 | if (current === undefined) { |
| 269 | return yield* new CollectionsEnginesServingConfigNotResolved({ |
| 270 | resourceName: fallbackName, |
| 271 | }); |
| 272 | } |
| 273 | |
| 274 | const name = current.name ?? fallbackName; |
| 275 | const displayNameChanged = (current.displayName ?? "") !== displayName; |
| 276 | const rankingChanged = (current.rankingExpression ?? "") !== (news.rankingExpression ?? ""); |
| 277 | const modelChanged = (current.modelId ?? "") !== (news.modelId ?? ""); |
| 278 | const diversityChanged = (current.diversityLevel ?? "") !== (news.diversityLevel ?? ""); |
| 279 | const boostChanged = !sameStringList(current.boostControlIds, news.boostControlIds); |
| 280 | const filterChanged = !sameStringList(current.filterControlIds, news.filterControlIds); |
| 281 | const synonymsChanged = !sameStringList(current.synonymsControlIds, news.synonymsControlIds); |
| 282 | const redirectChanged = !sameStringList(current.redirectControlIds, news.redirectControlIds); |
| 283 | |
| 284 | if ( |
| 285 | displayNameChanged || |
| 286 | rankingChanged || |
| 287 | modelChanged || |
| 288 | diversityChanged || |
| 289 | boostChanged || |
| 290 | filterChanged || |
| 291 | synonymsChanged || |
| 292 | redirectChanged |
| 293 | ) { |
| 294 | current = yield* discoveryengine.patchProjectsLocationsCollectionsEnginesServingConfigs({ |
| 295 | name, |
| 296 | updateMask: [ |
| 297 | displayNameChanged ? "display_name" : undefined, |
| 298 | rankingChanged ? "ranking_expression" : undefined, |
| 299 | modelChanged ? "model_id" : undefined, |
| 300 | diversityChanged ? "diversity_level" : undefined, |
| 301 | boostChanged ? "boost_control_ids" : undefined, |
| 302 | filterChanged ? "filter_control_ids" : undefined, |
| 303 | synonymsChanged ? "synonyms_control_ids" : undefined, |
| 304 | redirectChanged ? "redirect_control_ids" : undefined, |
| 305 | ] |
| 306 | .filter((field): field is string => field !== undefined) |
| 307 | .join(","), |
| 308 | body: { |
| 309 | name, |
| 310 | displayName, |
| 311 | rankingExpression: news.rankingExpression, |
| 312 | modelId: news.modelId, |
| 313 | diversityLevel: news.diversityLevel, |
| 314 | boostControlIds: news.boostControlIds, |
| 315 | filterControlIds: news.filterControlIds, |
| 316 | synonymsControlIds: news.synonymsControlIds, |
| 317 | redirectControlIds: news.redirectControlIds, |
| 318 | }, |
| 319 | }); |
| 320 | } |
| 321 | |
| 322 | return toAttrs(current, env.project); |
| 323 | }), |
| 324 | |
| 325 | delete: Effect.fn(function* ({ output }) { |
| 326 | const existing = yield* getByName(output.name); |
| 327 | if (existing === undefined) return; |
| 328 | yield* discoveryengine |
| 329 | .deleteProjectsLocationsCollectionsEnginesServingConfigs({ |
| 330 | name: output.name, |
| 331 | }) |
| 332 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 333 | }), |
| 334 | }); |