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 { unownedUnlessPrevious } 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 { GcpEnvironment } from "../Environment.ts"; |
| 9 | import type { Providers } from "../Providers.ts"; |
| 10 | import { |
| 11 | controlIdOf, |
| 12 | expandDataStore, |
| 13 | fingerprint, |
| 14 | parseResourceName, |
| 15 | toPhysical, |
| 16 | } from "./internal.ts"; |
| 17 | |
| 18 | export type DataStoresControlQueryTerm = { |
| 19 | /** Query value to match. Must be lowercase UTF-8. */ |
| 20 | value: string; |
| 21 | /** |
| 22 | * Whether the search query must exactly match the term. |
| 23 | * @default false |
| 24 | */ |
| 25 | fullMatch?: boolean; |
| 26 | }; |
| 27 | |
| 28 | export type DataStoresControlTimeRange = { |
| 29 | /** Inclusive start of the active window (RFC3339). */ |
| 30 | startTime?: string; |
| 31 | /** Inclusive end of the active window (RFC3339). */ |
| 32 | endTime?: string; |
| 33 | }; |
| 34 | |
| 35 | export type DataStoresControlCondition = { |
| 36 | /** Query regex matching the whole search query. */ |
| 37 | queryRegex?: string; |
| 38 | /** Query terms to match. Maximum 10. */ |
| 39 | queryTerms?: DataStoresControlQueryTerm[]; |
| 40 | /** Active time windows. Maximum 10. */ |
| 41 | activeTimeRange?: DataStoresControlTimeRange[]; |
| 42 | }; |
| 43 | |
| 44 | export type DataStoresControlRedirectAction = { |
| 45 | /** URI to redirect the shopper to (max 2000 characters). */ |
| 46 | redirectUri: string; |
| 47 | }; |
| 48 | |
| 49 | export type DataStoresControlFilterAction = { |
| 50 | /** Filter expression applied to matching results. */ |
| 51 | filter: string; |
| 52 | /** Data store whose documents can be filtered. Defaults to the parent. */ |
| 53 | dataStore?: string; |
| 54 | }; |
| 55 | |
| 56 | export type DataStoresControlSynonymsAction = { |
| 57 | /** Synonym set. At least 2, at most 100. */ |
| 58 | synonyms: string[]; |
| 59 | }; |
| 60 | |
| 61 | export type DataStoresControlBoostAction = { |
| 62 | /** Filter selecting documents to boost. */ |
| 63 | filter?: string; |
| 64 | /** Data store whose documents can be boosted. Defaults to the parent. */ |
| 65 | dataStore?: string; |
| 66 | /** Boost strength in `[-1, 1]`. Negative values demote. */ |
| 67 | boost?: number; |
| 68 | /** Fixed boost strength in `[-1, 1]`. */ |
| 69 | fixedBoost?: number; |
| 70 | }; |
| 71 | |
| 72 | export type DataStoresControlPromoteAction = { |
| 73 | /** Data store this promotion is attached to. Defaults to the parent. */ |
| 74 | dataStore?: string; |
| 75 | /** Promotion title (max 160 characters). */ |
| 76 | title: string; |
| 77 | /** Promotion URI. */ |
| 78 | uri?: string; |
| 79 | /** Promotion description (max 200 characters). */ |
| 80 | description?: string; |
| 81 | /** Thumbnail image URI. */ |
| 82 | imageUri?: string; |
| 83 | /** Document resource name to promote. */ |
| 84 | document?: string; |
| 85 | /** |
| 86 | * Whether the promotion is enabled. |
| 87 | * @default true |
| 88 | */ |
| 89 | enabled?: boolean; |
| 90 | }; |
| 91 | |
| 92 | export type DataStoresControlProps = { |
| 93 | /** |
| 94 | * Parent Data Store resource name |
| 95 | * `projects/{project}/locations/{location}/dataStores/{dataStore}`. |
| 96 | * Immutable — changing it replaces the control. |
| 97 | */ |
| 98 | dataStore: string; |
| 99 | /** |
| 100 | * Control id (1-63 characters, `a-z`, `_`, `-`). If omitted, a unique |
| 101 | * id is generated. Immutable — changing it replaces the control. |
| 102 | */ |
| 103 | controlId?: string; |
| 104 | /** |
| 105 | * Human-readable name (max 128 characters). |
| 106 | */ |
| 107 | displayName?: string; |
| 108 | /** |
| 109 | * Solution this control belongs to. Immutable — changing it replaces |
| 110 | * the control. |
| 111 | * @default "SOLUTION_TYPE_SEARCH" |
| 112 | */ |
| 113 | solutionType?: string; |
| 114 | /** |
| 115 | * Search use cases. Required when `solutionType` is |
| 116 | * `SOLUTION_TYPE_SEARCH`. |
| 117 | * @default ["SEARCH_USE_CASE_SEARCH"] |
| 118 | */ |
| 119 | useCases?: string[]; |
| 120 | /** |
| 121 | * Conditions that trigger the action. Omit to always apply. |
| 122 | */ |
| 123 | conditions?: DataStoresControlCondition[]; |
| 124 | /** Redirect-type action. Mutually exclusive with the other actions. */ |
| 125 | redirectAction?: DataStoresControlRedirectAction; |
| 126 | /** Filter-type action. Mutually exclusive with the other actions. */ |
| 127 | filterAction?: DataStoresControlFilterAction; |
| 128 | /** Synonyms-type action. Mutually exclusive with the other actions. */ |
| 129 | synonymsAction?: DataStoresControlSynonymsAction; |
| 130 | /** Boost-type action. Mutually exclusive with the other actions. */ |
| 131 | boostAction?: DataStoresControlBoostAction; |
| 132 | /** Promote-type action. Mutually exclusive with the other actions. */ |
| 133 | promoteAction?: DataStoresControlPromoteAction; |
| 134 | }; |
| 135 | |
| 136 | export type DataStoresControl = Resource< |
| 137 | "GCP.DiscoveryEngine.DataStoresControl", |
| 138 | DataStoresControlProps, |
| 139 | { |
| 140 | /** Full resource name `.../dataStores/{dataStore}/controls/{control}`. */ |
| 141 | name: string; |
| 142 | /** Control id (last path segment). */ |
| 143 | controlId: string; |
| 144 | /** Parent data store resource name. */ |
| 145 | dataStore: string; |
| 146 | /** Project id. */ |
| 147 | project: string; |
| 148 | /** Location id. */ |
| 149 | location: string; |
| 150 | /** Display name. */ |
| 151 | displayName: string | undefined; |
| 152 | /** Solution type. */ |
| 153 | solutionType: string | undefined; |
| 154 | /** Search use cases. */ |
| 155 | useCases: string[]; |
| 156 | /** Serving configs this control is attached to. */ |
| 157 | associatedServingConfigIds: string[]; |
| 158 | }, |
| 159 | never, |
| 160 | Providers |
| 161 | >; |
| 162 | |
| 163 | /** |
| 164 | * A Vertex AI Search Control attached to a Data Store. |
| 165 | * |
| 166 | * Without labels, ownership rests on the deterministic id: `read` reports a |
| 167 | * resource it finds without prior state as unowned (adopt it with `--adopt`). |
| 168 | * Parent, control id, solution type, and action kind are immutable; display |
| 169 | * name, conditions, and action payload update in place. |
| 170 | * |
| 171 | * ### Creating a Control |
| 172 | * **Example:** Synonym control |
| 173 | * ```typescript |
| 174 | * const control = yield* GCP.DiscoveryEngine.DataStoresControl("Synonyms", { |
| 175 | * dataStore: dataStore.name, |
| 176 | * displayName: "happy-glad", |
| 177 | * synonymsAction: { synonyms: ["happy", "glad"] }, |
| 178 | * }); |
| 179 | * ``` |
| 180 | * |
| 181 | * ### Updating a Control |
| 182 | * **Example:** Rename and expand the synonym set |
| 183 | * ```typescript |
| 184 | * // Same logical id as before; only the changed props differ. |
| 185 | * const control = yield* GCP.DiscoveryEngine.DataStoresControl("Synonyms", { |
| 186 | * dataStore: dataStore.name, |
| 187 | * displayName: "mood", |
| 188 | * synonymsAction: { synonyms: ["happy", "glad", "cheerful"] }, |
| 189 | * }); |
| 190 | * ``` |
| 191 | * |
| 192 | * @resource |
| 193 | * @category DiscoveryEngine |
| 194 | */ |
| 195 | export const DataStoresControl = Resource<DataStoresControl>( |
| 196 | "GCP.DiscoveryEngine.DataStoresControl", |
| 197 | ); |
| 198 | |
| 199 | export class DataStoresControlNotResolved extends Data.TaggedError( |
| 200 | "GCP.DiscoveryEngine.DataStoresControlNotResolved", |
| 201 | )<{ |
| 202 | resourceName: string; |
| 203 | }> {} |
| 204 | |
| 205 | const DEFAULT_SOLUTION = "SOLUTION_TYPE_SEARCH"; |
| 206 | const DEFAULT_USE_CASES = ["SEARCH_USE_CASE_SEARCH"]; |
| 207 | |
| 208 | const resourceName = (dataStore: string, controlId: string) => `${dataStore}/controls/${controlId}`; |
| 209 | |
| 210 | const actionKind = (props: { |
| 211 | redirectAction?: unknown; |
| 212 | filterAction?: unknown; |
| 213 | synonymsAction?: unknown; |
| 214 | boostAction?: unknown; |
| 215 | promoteAction?: unknown; |
| 216 | }) => { |
| 217 | if (props.redirectAction) return "redirect"; |
| 218 | if (props.filterAction) return "filter"; |
| 219 | if (props.synonymsAction) return "synonyms"; |
| 220 | if (props.boostAction) return "boost"; |
| 221 | if (props.promoteAction) return "promote"; |
| 222 | return "none"; |
| 223 | }; |
| 224 | |
| 225 | const toBody = ( |
| 226 | news: DataStoresControlProps, |
| 227 | displayName: string, |
| 228 | ): discoveryengine.GoogleCloudDiscoveryengineV1Control => ({ |
| 229 | displayName, |
| 230 | solutionType: news.solutionType ?? DEFAULT_SOLUTION, |
| 231 | useCases: news.useCases ?? DEFAULT_USE_CASES, |
| 232 | conditions: news.conditions, |
| 233 | redirectAction: news.redirectAction, |
| 234 | filterAction: news.filterAction |
| 235 | ? { |
| 236 | filter: news.filterAction.filter, |
| 237 | dataStore: news.filterAction.dataStore ?? news.dataStore, |
| 238 | } |
| 239 | : undefined, |
| 240 | synonymsAction: news.synonymsAction, |
| 241 | boostAction: news.boostAction |
| 242 | ? { |
| 243 | filter: news.boostAction.filter, |
| 244 | dataStore: news.boostAction.dataStore ?? news.dataStore, |
| 245 | boost: news.boostAction.boost, |
| 246 | fixedBoost: news.boostAction.fixedBoost, |
| 247 | } |
| 248 | : undefined, |
| 249 | promoteAction: news.promoteAction |
| 250 | ? { |
| 251 | dataStore: news.promoteAction.dataStore ?? news.dataStore, |
| 252 | searchLinkPromotion: { |
| 253 | title: news.promoteAction.title, |
| 254 | uri: news.promoteAction.uri, |
| 255 | description: news.promoteAction.description, |
| 256 | imageUri: news.promoteAction.imageUri, |
| 257 | document: news.promoteAction.document, |
| 258 | enabled: news.promoteAction.enabled, |
| 259 | }, |
| 260 | } |
| 261 | : undefined, |
| 262 | }); |
| 263 | |
| 264 | const toAttrs = ( |
| 265 | control: discoveryengine.GoogleCloudDiscoveryengineV1Control, |
| 266 | project: string, |
| 267 | dataStoreHint?: string, |
| 268 | ) => { |
| 269 | const name = control.name ?? ""; |
| 270 | const parsed = parseResourceName(name, "controls"); |
| 271 | const dataStore = parsed.dataStore.includes("/dataStores/") |
| 272 | ? parsed.dataStore |
| 273 | : (dataStoreHint ?? parsed.dataStore); |
| 274 | return { |
| 275 | name, |
| 276 | controlId: parsed.id, |
| 277 | dataStore, |
| 278 | project: parsed.project || project, |
| 279 | location: parsed.location, |
| 280 | displayName: control.displayName, |
| 281 | solutionType: control.solutionType, |
| 282 | useCases: [...(control.useCases ?? [])], |
| 283 | associatedServingConfigIds: [...(control.associatedServingConfigIds ?? [])], |
| 284 | }; |
| 285 | }; |
| 286 | |
| 287 | const getByName = (name: string) => |
| 288 | name.length === 0 |
| 289 | ? Effect.undefined |
| 290 | : discoveryengine |
| 291 | .getProjectsLocationsDataStoresControls({ name }) |
| 292 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 293 | |
| 294 | export const DataStoresControlProvider = () => |
| 295 | Provider.succeed(DataStoresControl, { |
| 296 | stables: ["name", "controlId", "dataStore", "project", "location"], |
| 297 | |
| 298 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 299 | if (!isResolved(news)) return undefined; |
| 300 | const previousParent = olds?.dataStore ?? output?.dataStore; |
| 301 | if (previousParent !== undefined && news.dataStore !== previousParent) { |
| 302 | return { action: "replace" as const, deleteFirst: false }; |
| 303 | } |
| 304 | const previousId = olds?.controlId ?? output?.controlId; |
| 305 | if ( |
| 306 | previousId !== undefined && |
| 307 | news.controlId !== undefined && |
| 308 | news.controlId !== previousId |
| 309 | ) { |
| 310 | return { action: "replace" as const, deleteFirst: true }; |
| 311 | } |
| 312 | const previousSolution = olds?.solutionType ?? output?.solutionType ?? DEFAULT_SOLUTION; |
| 313 | const nextSolution = news.solutionType ?? previousSolution; |
| 314 | if (previousSolution !== nextSolution) { |
| 315 | return { action: "replace" as const, deleteFirst: true }; |
| 316 | } |
| 317 | const previousKind = actionKind(olds ?? {}); |
| 318 | const nextKind = actionKind(news); |
| 319 | if (previousKind !== "none" && nextKind !== previousKind) { |
| 320 | return { action: "replace" as const, deleteFirst: true }; |
| 321 | } |
| 322 | const previousAction = fingerprint({ |
| 323 | redirectAction: olds?.redirectAction, |
| 324 | filterAction: olds?.filterAction, |
| 325 | synonymsAction: olds?.synonymsAction, |
| 326 | boostAction: olds?.boostAction, |
| 327 | promoteAction: olds?.promoteAction, |
| 328 | }); |
| 329 | const nextAction = fingerprint({ |
| 330 | redirectAction: news.redirectAction, |
| 331 | filterAction: news.filterAction, |
| 332 | synonymsAction: news.synonymsAction, |
| 333 | boostAction: news.boostAction, |
| 334 | promoteAction: news.promoteAction, |
| 335 | }); |
| 336 | if (previousKind !== "none" && previousAction !== nextAction) { |
| 337 | return { action: "replace" as const, deleteFirst: true }; |
| 338 | } |
| 339 | return undefined; |
| 340 | }), |
| 341 | |
| 342 | read: Effect.fn(function* ({ id, olds, output, previous }) { |
| 343 | const env = yield* GcpEnvironment.current; |
| 344 | const controlId = yield* toPhysical(id, olds?.controlId, output?.controlId, controlIdOf); |
| 345 | const parent = olds?.dataStore |
| 346 | ? expandDataStore(olds.dataStore, env.project, output?.location ?? "global") |
| 347 | : undefined; |
| 348 | const name = output?.name ?? (parent ? resourceName(parent, controlId) : ""); |
| 349 | const existing = yield* getByName(name); |
| 350 | if (existing === undefined) return undefined; |
| 351 | const attrs = toAttrs(existing, env.project); |
| 352 | // No labels field: without prior state it may not be ours. |
| 353 | return output !== undefined ? attrs : unownedUnlessPrevious(attrs, previous, "name"); |
| 354 | }), |
| 355 | |
| 356 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 357 | const env = yield* GcpEnvironment.current; |
| 358 | const parent = expandDataStore(news.dataStore, env.project, output?.location ?? "global"); |
| 359 | const controlId = yield* toPhysical(id, news.controlId, output?.controlId, controlIdOf); |
| 360 | const name = resourceName(parent, controlId); |
| 361 | const displayName = news.displayName ?? controlId; |
| 362 | |
| 363 | let current = yield* getByName(output?.name ?? name); |
| 364 | |
| 365 | if (current === undefined) { |
| 366 | const created = yield* discoveryengine |
| 367 | .createProjectsLocationsDataStoresControls({ |
| 368 | parent, |
| 369 | controlId, |
| 370 | body: toBody(news, displayName), |
| 371 | }) |
| 372 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 373 | current = created ?? undefined; |
| 374 | } |
| 375 | |
| 376 | if (current === undefined) { |
| 377 | return yield* new DataStoresControlNotResolved({ resourceName: name }); |
| 378 | } |
| 379 | |
| 380 | const desired = toBody(news, displayName); |
| 381 | const displayChanged = (current.displayName ?? "") !== displayName; |
| 382 | const useCasesChanged = |
| 383 | fingerprint([...(current.useCases ?? [])].sort()) !== |
| 384 | fingerprint([...(desired.useCases ?? [])].sort()); |
| 385 | const conditionsChanged = fingerprint(current.conditions) !== fingerprint(desired.conditions); |
| 386 | if (displayChanged || useCasesChanged || conditionsChanged) { |
| 387 | current = yield* discoveryengine.patchProjectsLocationsDataStoresControls({ |
| 388 | name: current.name ?? name, |
| 389 | updateMask: [ |
| 390 | displayChanged ? "display_name" : undefined, |
| 391 | useCasesChanged ? "use_cases" : undefined, |
| 392 | conditionsChanged ? "conditions" : undefined, |
| 393 | ] |
| 394 | .filter((field): field is string => field !== undefined) |
| 395 | .join(","), |
| 396 | body: { ...desired, name: current.name ?? name }, |
| 397 | }); |
| 398 | } |
| 399 | |
| 400 | return toAttrs(current, env.project, parent); |
| 401 | }), |
| 402 | |
| 403 | delete: Effect.fn(function* ({ output }) { |
| 404 | const existing = yield* getByName(output.name); |
| 405 | if (existing === undefined) return; |
| 406 | yield* discoveryengine |
| 407 | .deleteProjectsLocationsDataStoresControls({ name: output.name }) |
| 408 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 409 | }), |
| 410 | }); |