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 analyticshub from "@distilled.cloud/gcp/analyticshub_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 Effect from "effect/Effect"; |
| 7 | import { GcpEnvironment } from "../Environment.ts"; |
| 8 | import type { Providers } from "../Providers.ts"; |
| 9 | import { |
| 10 | AnalyticshubNotResolved, |
| 11 | displayNameOf, |
| 12 | encodeDescription, |
| 13 | expandParent, |
| 14 | deleteRetry, |
| 15 | hasOwnershipMarker, |
| 16 | listChildResources, |
| 17 | listExchangesInProject, |
| 18 | listQueryTemplates, |
| 19 | namedOf, |
| 20 | normalizeLocation, |
| 21 | ownedById, |
| 22 | ownershipLabels, |
| 23 | parseDescription, |
| 24 | parseResourceName, |
| 25 | replaceOnIdentity, |
| 26 | retryTransient, |
| 27 | sameText, |
| 28 | toPhysicalId, |
| 29 | updateMaskOf, |
| 30 | } from "./internal.ts"; |
| 31 | |
| 32 | export type QueryTemplateRoutine = analyticshub.Routine; |
| 33 | export type QueryTemplateState = analyticshub.QueryTemplateStateEnum | (string & {}); |
| 34 | |
| 35 | export type DataExchangesQueryTemplateProps = { |
| 36 | /** |
| 37 | * Parent data exchange. Full name |
| 38 | * `projects/{project}/locations/{location}/dataExchanges/{dataExchange}` |
| 39 | * or the exchange id (combined with `location`). Query templates live |
| 40 | * in data clean room exchanges. Immutable — changing the parent |
| 41 | * replaces the template. |
| 42 | */ |
| 43 | dataExchange: string; |
| 44 | /** |
| 45 | * Query template id (the `{queryTemplate}` segment of |
| 46 | * `.../dataExchanges/{dataExchange}/queryTemplates/{queryTemplate}`). |
| 47 | * If omitted, a unique id is generated. Letters, numbers, and |
| 48 | * underscores; max 100 bytes. Immutable — changing it replaces the |
| 49 | * template. |
| 50 | */ |
| 51 | queryTemplateId?: string; |
| 52 | /** |
| 53 | * Location used when `dataExchange` is a bare id. |
| 54 | * @default the stack's GCP region (`GCP.Region`, else the profile region, else `us-central1`) |
| 55 | */ |
| 56 | location?: string; |
| 57 | /** |
| 58 | * Human-readable display name. Max 63 bytes. Defaults to the template |
| 59 | * id. Immutable — changing it replaces the template. |
| 60 | */ |
| 61 | displayName?: string; |
| 62 | /** |
| 63 | * Short description (max 2000 bytes). Query templates have no labels |
| 64 | * field, so Alchemy ownership is stored in a `[alchemy …]` prefix and |
| 65 | * stripped from attributes. |
| 66 | */ |
| 67 | description?: string; |
| 68 | /** |
| 69 | * Documentation describing the query template. |
| 70 | */ |
| 71 | documentation?: string; |
| 72 | /** |
| 73 | * Email or URL of the primary point of contact. Max 1000 bytes. |
| 74 | */ |
| 75 | primaryContact?: string; |
| 76 | /** |
| 77 | * Email or URL of the proposer. Max 1000 bytes. Will be deprecated in |
| 78 | * favor of `primaryContact`. |
| 79 | */ |
| 80 | proposer?: string; |
| 81 | /** |
| 82 | * Table-valued function associated with the template. |
| 83 | */ |
| 84 | routine?: QueryTemplateRoutine; |
| 85 | }; |
| 86 | |
| 87 | export type DataExchangesQueryTemplate = Resource< |
| 88 | "GCP.AnalyticsHub.DataExchangesQueryTemplate", |
| 89 | DataExchangesQueryTemplateProps, |
| 90 | { |
| 91 | /** Full resource name. */ |
| 92 | name: string; |
| 93 | /** Query template id (last path segment). */ |
| 94 | queryTemplateId: string; |
| 95 | /** Parent data exchange resource name. */ |
| 96 | dataExchange: string; |
| 97 | /** Project id. */ |
| 98 | project: string; |
| 99 | /** Location id. */ |
| 100 | location: string; |
| 101 | /** Display name. */ |
| 102 | displayName: string | undefined; |
| 103 | /** User description (Alchemy ownership marker stripped). */ |
| 104 | description: string | undefined; |
| 105 | /** Documentation. */ |
| 106 | documentation: string | undefined; |
| 107 | /** Primary contact. */ |
| 108 | primaryContact: string | undefined; |
| 109 | /** Proposer contact. */ |
| 110 | proposer: string | undefined; |
| 111 | /** Associated routine. */ |
| 112 | routine: QueryTemplateRoutine | undefined; |
| 113 | /** Lifecycle state (`DRAFTED`, `PENDING`, `APPROVED`, …). */ |
| 114 | state: string | undefined; |
| 115 | /** RFC3339 creation timestamp. */ |
| 116 | createTime: string | undefined; |
| 117 | /** RFC3339 last-update timestamp. */ |
| 118 | updateTime: string | undefined; |
| 119 | }, |
| 120 | never, |
| 121 | Providers |
| 122 | >; |
| 123 | |
| 124 | /** |
| 125 | * An Analytics Hub query template — a shared table-valued function |
| 126 | * defined by contributors in a data clean room. |
| 127 | * |
| 128 | * Parent exchange, location, template id, and display name are |
| 129 | * immutable. Description, documentation, contacts, and the routine |
| 130 | * body update in place. Create the parent exchange with |
| 131 | * `sharingEnvironmentConfig: { dcrExchangeConfig: {} }`. |
| 132 | * |
| 133 | * Query templates have no labels. Alchemy stamps ownership into the |
| 134 | * description so `list` / `pnpm nuke:gcp` can find them. |
| 135 | * |
| 136 | * ### Creating a Query Template |
| 137 | * **Example:** Draft TVF in a data clean room |
| 138 | * ```typescript |
| 139 | * const dcr = yield* GCP.AnalyticsHub.DataExchange("CleanRoom", { |
| 140 | * displayName: "Clean Room", |
| 141 | * sharingEnvironmentConfig: { dcrExchangeConfig: {} }, |
| 142 | * }); |
| 143 | * const template = yield* GCP.AnalyticsHub.DataExchangesQueryTemplate( |
| 144 | * "Counts", |
| 145 | * { |
| 146 | * dataExchange: dcr.name, |
| 147 | * displayName: "Counts", |
| 148 | * routine: { |
| 149 | * routineType: "TABLE_VALUED_FUNCTION", |
| 150 | * definitionBody: "SELECT 1 AS n", |
| 151 | * }, |
| 152 | * }, |
| 153 | * ); |
| 154 | * ``` |
| 155 | * |
| 156 | * @resource |
| 157 | * @category AnalyticsHub |
| 158 | */ |
| 159 | export const DataExchangesQueryTemplate = Resource<DataExchangesQueryTemplate>( |
| 160 | "GCP.AnalyticsHub.DataExchangesQueryTemplate", |
| 161 | ); |
| 162 | |
| 163 | const parentExchange = (dataExchange: string, project: string, location: string) => |
| 164 | expandParent(dataExchange, project, location, "dataExchanges"); |
| 165 | |
| 166 | const resourceName = (dataExchange: string, queryTemplateId: string) => |
| 167 | `${dataExchange}/queryTemplates/${queryTemplateId}`; |
| 168 | |
| 169 | const desiredRoutine = ( |
| 170 | queryTemplateId: string, |
| 171 | routine: QueryTemplateRoutine | undefined, |
| 172 | ): QueryTemplateRoutine | undefined => { |
| 173 | if (routine === undefined) return undefined; |
| 174 | const body = routine.definitionBody ?? ""; |
| 175 | const definitionBody = |
| 176 | body.startsWith(`${queryTemplateId}(`) || body.startsWith(`${queryTemplateId} `) |
| 177 | ? body |
| 178 | : `${queryTemplateId}() AS (${body})`; |
| 179 | return { ...routine, definitionBody }; |
| 180 | }; |
| 181 | |
| 182 | const toAttrs = (template: analyticshub.QueryTemplate, project: string) => { |
| 183 | const name = template.name ?? ""; |
| 184 | const parsed = parseResourceName(name, "queryTemplates"); |
| 185 | const { description } = parseDescription(template.description); |
| 186 | return { |
| 187 | name, |
| 188 | queryTemplateId: parsed.id, |
| 189 | dataExchange: parsed.parent, |
| 190 | project: parsed.project || project, |
| 191 | location: parsed.location, |
| 192 | displayName: template.displayName, |
| 193 | description, |
| 194 | documentation: template.documentation, |
| 195 | primaryContact: template.primaryContact, |
| 196 | proposer: template.proposer, |
| 197 | routine: template.routine, |
| 198 | state: template.state, |
| 199 | createTime: template.createTime, |
| 200 | updateTime: template.updateTime, |
| 201 | }; |
| 202 | }; |
| 203 | |
| 204 | const getByName = (name: string) => |
| 205 | Effect.gen(function* () { |
| 206 | if (name.length === 0) return undefined; |
| 207 | const parsed = parseResourceName(name, "queryTemplates"); |
| 208 | const items = yield* listQueryTemplates(parsed.parent); |
| 209 | return items.find((item) => (item.name ?? "") === name); |
| 210 | }); |
| 211 | |
| 212 | export const DataExchangesQueryTemplateProvider = () => |
| 213 | Provider.succeed(DataExchangesQueryTemplate, { |
| 214 | stables: [ |
| 215 | "name", |
| 216 | "queryTemplateId", |
| 217 | "dataExchange", |
| 218 | "project", |
| 219 | "location", |
| 220 | "createTime", |
| 221 | "displayName", |
| 222 | ], |
| 223 | |
| 224 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 225 | if (!isResolved(news)) return undefined; |
| 226 | const env = yield* GcpEnvironment.current; |
| 227 | const location = normalizeLocation( |
| 228 | news.location ?? olds?.location ?? output?.location, |
| 229 | env.region, |
| 230 | ); |
| 231 | const previousDisplay = olds?.displayName ?? output?.displayName; |
| 232 | const nextDisplay = news.displayName; |
| 233 | const previousDocs = olds?.documentation ?? output?.documentation; |
| 234 | const nextDocs = news.documentation; |
| 235 | return replaceOnIdentity({ |
| 236 | previousId: olds?.queryTemplateId ?? output?.queryTemplateId, |
| 237 | nextId: news.queryTemplateId, |
| 238 | previousLocation: normalizeLocation(olds?.location ?? output?.location, env.region), |
| 239 | nextLocation: location, |
| 240 | previousParent: olds?.dataExchange ?? output?.dataExchange, |
| 241 | nextParent: parentExchange(news.dataExchange, env.project, location), |
| 242 | extra: |
| 243 | (previousDisplay !== undefined && |
| 244 | nextDisplay !== undefined && |
| 245 | previousDisplay !== nextDisplay) || |
| 246 | (previousDocs !== undefined && nextDocs !== undefined && previousDocs !== nextDocs), |
| 247 | }); |
| 248 | }), |
| 249 | |
| 250 | read: Effect.fn(function* ({ id, olds, output }) { |
| 251 | const env = yield* GcpEnvironment.current; |
| 252 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 253 | const queryTemplateId = yield* toPhysicalId( |
| 254 | id, |
| 255 | olds?.queryTemplateId, |
| 256 | output?.queryTemplateId, |
| 257 | ); |
| 258 | const dataExchange = |
| 259 | olds?.dataExchange !== undefined |
| 260 | ? parentExchange(olds.dataExchange, env.project, location) |
| 261 | : (output?.dataExchange ?? ""); |
| 262 | const name = |
| 263 | output?.name ?? (dataExchange ? resourceName(dataExchange, queryTemplateId) : ""); |
| 264 | const existing = yield* getByName(name); |
| 265 | if (existing === undefined) return undefined; |
| 266 | const attrs = toAttrs(existing, env.project); |
| 267 | return (yield* ownedById(id, existing.description)) ? attrs : Unowned(attrs); |
| 268 | }), |
| 269 | |
| 270 | list: () => |
| 271 | Effect.gen(function* () { |
| 272 | const env = yield* GcpEnvironment.current; |
| 273 | const exchanges = yield* listExchangesInProject(env.project, env.region); |
| 274 | const templates = yield* listChildResources(namedOf(exchanges), listQueryTemplates); |
| 275 | return templates |
| 276 | .filter((item) => hasOwnershipMarker(item.description)) |
| 277 | .map((item) => toAttrs(item, env.project)); |
| 278 | }), |
| 279 | |
| 280 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 281 | const env = yield* GcpEnvironment.current; |
| 282 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 283 | const dataExchange = parentExchange(news.dataExchange, env.project, location); |
| 284 | const queryTemplateId = yield* toPhysicalId( |
| 285 | id, |
| 286 | news.queryTemplateId, |
| 287 | output?.queryTemplateId, |
| 288 | ); |
| 289 | const name = output?.name ?? resourceName(dataExchange, queryTemplateId); |
| 290 | const ownership = yield* ownershipLabels(id); |
| 291 | const desiredDescription = encodeDescription(ownership, news.description); |
| 292 | const displayName = displayNameOf(news.displayName, queryTemplateId); |
| 293 | const body: analyticshub.QueryTemplate = { |
| 294 | displayName, |
| 295 | description: desiredDescription, |
| 296 | }; |
| 297 | if (news.documentation !== undefined) { |
| 298 | body.documentation = news.documentation; |
| 299 | } |
| 300 | if (news.primaryContact !== undefined) { |
| 301 | body.primaryContact = news.primaryContact; |
| 302 | } |
| 303 | if (news.proposer !== undefined) { |
| 304 | body.proposer = news.proposer; |
| 305 | } |
| 306 | const routine = desiredRoutine(queryTemplateId, news.routine); |
| 307 | if (routine !== undefined) { |
| 308 | body.routine = routine; |
| 309 | } |
| 310 | |
| 311 | let current = yield* getByName(name); |
| 312 | |
| 313 | if (current === undefined) { |
| 314 | const created = yield* retryTransient( |
| 315 | analyticshub.createProjectsLocationsDataExchangesQueryTemplates({ |
| 316 | parent: dataExchange, |
| 317 | queryTemplateId, |
| 318 | body, |
| 319 | }), |
| 320 | ).pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 321 | current = created ?? undefined; |
| 322 | } |
| 323 | |
| 324 | if (current === undefined) { |
| 325 | return yield* new AnalyticshubNotResolved({ resourceName: name }); |
| 326 | } |
| 327 | |
| 328 | const currentName = current.name ?? name; |
| 329 | const descriptionChanged = !sameText(current.description, desiredDescription); |
| 330 | const contactChanged = |
| 331 | news.primaryContact !== undefined && !sameText(current.primaryContact, news.primaryContact); |
| 332 | const proposerChanged = |
| 333 | news.proposer !== undefined && !sameText(current.proposer, news.proposer); |
| 334 | const routineChanged = |
| 335 | routine !== undefined && |
| 336 | (!sameText(current.routine?.routineType, routine.routineType) || |
| 337 | !sameText(current.routine?.definitionBody, routine.definitionBody)); |
| 338 | |
| 339 | if (descriptionChanged || contactChanged || proposerChanged || routineChanged) { |
| 340 | current = yield* retryTransient( |
| 341 | analyticshub.patchProjectsLocationsDataExchangesQueryTemplates({ |
| 342 | name: currentName, |
| 343 | updateMask: updateMaskOf( |
| 344 | descriptionChanged ? "description" : undefined, |
| 345 | contactChanged ? "primaryContact" : undefined, |
| 346 | proposerChanged ? "proposer" : undefined, |
| 347 | routineChanged ? "routine" : undefined, |
| 348 | ), |
| 349 | body: { name: currentName, ...body }, |
| 350 | }), |
| 351 | ); |
| 352 | } |
| 353 | |
| 354 | return toAttrs(current, env.project); |
| 355 | }), |
| 356 | |
| 357 | delete: Effect.fn(function* ({ output }) { |
| 358 | yield* deleteRetry( |
| 359 | analyticshub.deleteProjectsLocationsDataExchangesQueryTemplates({ |
| 360 | name: output.name, |
| 361 | }), |
| 362 | ); |
| 363 | }), |
| 364 | }); |