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 dataproc from "@distilled.cloud/gcp/dataproc_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 { tagRecord } from "alchemy/Tags"; |
| 7 | import * as Data from "effect/Data"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import { GcpEnvironment } from "../Environment.ts"; |
| 10 | import { createInternalLabels, diffLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | collectPages, |
| 14 | LIST_LOCATIONS, |
| 15 | MAX_SESSION_TEMPLATE_ID_LENGTH, |
| 16 | emptyOnMissing, |
| 17 | fingerprint, |
| 18 | hasAlchemyLabelMap, |
| 19 | locationParent, |
| 20 | normalizeLocation, |
| 21 | parseResourceName, |
| 22 | toPhysicalId, |
| 23 | userLabels, |
| 24 | waitUntilGone, |
| 25 | } from "./internal.ts"; |
| 26 | |
| 27 | export type SessionTemplateProps = { |
| 28 | /** |
| 29 | * Template id (the `{template}` segment of |
| 30 | * `projects/{project}/locations/{location}/sessionTemplates/{template}`). |
| 31 | * If omitted, a unique RFC1035 name is generated. Immutable — changing |
| 32 | * it replaces the template. |
| 33 | */ |
| 34 | templateId?: string; |
| 35 | /** |
| 36 | * Location (`us-central1`, …). Immutable — changing it replaces the |
| 37 | * template. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 38 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 39 | */ |
| 40 | location?: string; |
| 41 | /** |
| 42 | * User labels. Alchemy ownership labels are merged in automatically. |
| 43 | */ |
| 44 | labels?: Record<string, string>; |
| 45 | /** |
| 46 | * Brief description. |
| 47 | */ |
| 48 | description?: string; |
| 49 | /** |
| 50 | * Jupyter kernel config. Defaults to a PYTHON kernel when neither |
| 51 | * `jupyterSession` nor `sparkConnectSession` is set. |
| 52 | */ |
| 53 | jupyterSession?: dataproc.JupyterConfig; |
| 54 | /** |
| 55 | * Spark Connect session config. Mutually exclusive with Jupyter. |
| 56 | */ |
| 57 | sparkConnectSession?: dataproc.SparkConnectConfig; |
| 58 | /** |
| 59 | * Runtime configuration (version, properties, container image). |
| 60 | */ |
| 61 | runtimeConfig?: dataproc.RuntimeConfig; |
| 62 | /** |
| 63 | * Environment configuration (execution and peripherals). |
| 64 | */ |
| 65 | environmentConfig?: dataproc.EnvironmentConfig; |
| 66 | }; |
| 67 | |
| 68 | export type SessionTemplate = Resource< |
| 69 | "GCP.Dataproc.SessionTemplate", |
| 70 | SessionTemplateProps, |
| 71 | { |
| 72 | /** Full resource name `projects/{project}/locations/{location}/sessionTemplates/{template}`. */ |
| 73 | name: string; |
| 74 | /** Template id (last path segment). */ |
| 75 | templateId: string; |
| 76 | /** Project id. */ |
| 77 | project: string; |
| 78 | /** Location id. */ |
| 79 | location: string; |
| 80 | /** User labels (Alchemy ownership labels stripped). */ |
| 81 | labels: Record<string, string>; |
| 82 | /** Description. */ |
| 83 | description: string | undefined; |
| 84 | /** Jupyter kernel, if configured. */ |
| 85 | jupyterKernel: string | undefined; |
| 86 | /** Server-assigned uuid. */ |
| 87 | uuid: string | undefined; |
| 88 | /** RFC3339 creation timestamp. */ |
| 89 | createTime: string | undefined; |
| 90 | /** RFC3339 last-update timestamp. */ |
| 91 | updateTime: string | undefined; |
| 92 | }, |
| 93 | never, |
| 94 | Providers |
| 95 | >; |
| 96 | |
| 97 | /** |
| 98 | * A Dataproc serverless interactive session template. |
| 99 | * |
| 100 | * Changing `templateId` or `location` replaces the template. Description, |
| 101 | * labels, Jupyter config, runtime, and environment patch in place. |
| 102 | * |
| 103 | * ### Creating a Template |
| 104 | * **Example:** Generated name |
| 105 | * ```typescript |
| 106 | * const template = yield* GCP.Dataproc.SessionTemplate("Notebook", {}); |
| 107 | * ``` |
| 108 | * |
| 109 | * **Example:** Explicit Jupyter kernel |
| 110 | * ```typescript |
| 111 | * const template = yield* GCP.Dataproc.SessionTemplate("Notebook", { |
| 112 | * templateId: "analytics-nb", |
| 113 | * location: "us-central1", |
| 114 | * description: "python notebooks", |
| 115 | * jupyterSession: { kernel: "PYTHON" }, |
| 116 | * labels: { env: "prod" }, |
| 117 | * }); |
| 118 | * ``` |
| 119 | * |
| 120 | * @resource |
| 121 | * @category Dataproc |
| 122 | */ |
| 123 | export const SessionTemplate = Resource<SessionTemplate>("GCP.Dataproc.SessionTemplate"); |
| 124 | |
| 125 | export class SessionTemplateNotResolved extends Data.TaggedError( |
| 126 | "GCP.Dataproc.SessionTemplateNotResolved", |
| 127 | )<{ |
| 128 | resourceName: string; |
| 129 | }> {} |
| 130 | |
| 131 | const resourceName = (project: string, location: string, templateId: string) => |
| 132 | `${locationParent(project, location)}/sessionTemplates/${templateId}`; |
| 133 | |
| 134 | // Dataproc rejects a Jupyter session template without a display name. |
| 135 | const defaultJupyter = ( |
| 136 | news: SessionTemplateProps, |
| 137 | templateId: string, |
| 138 | ): dataproc.JupyterConfig | undefined => { |
| 139 | if (news.sparkConnectSession !== undefined) return undefined; |
| 140 | const jupyter = news.jupyterSession ?? { kernel: "PYTHON" }; |
| 141 | return { ...jupyter, displayName: jupyter.displayName ?? templateId }; |
| 142 | }; |
| 143 | |
| 144 | const toAttrs = (template: dataproc.SessionTemplate, project: string, location: string) => { |
| 145 | const name = template.name ?? ""; |
| 146 | const parsed = parseResourceName(name, "sessionTemplates", location); |
| 147 | return { |
| 148 | name, |
| 149 | templateId: parsed.id, |
| 150 | project: parsed.project || project, |
| 151 | location: parsed.location || location, |
| 152 | labels: userLabels(template.labels), |
| 153 | description: template.description, |
| 154 | jupyterKernel: template.jupyterSession?.kernel, |
| 155 | uuid: template.uuid, |
| 156 | createTime: template.createTime, |
| 157 | updateTime: template.updateTime, |
| 158 | }; |
| 159 | }; |
| 160 | |
| 161 | const desiredBody = ( |
| 162 | news: SessionTemplateProps, |
| 163 | name: string, |
| 164 | templateId: string, |
| 165 | desiredLabels: Record<string, string>, |
| 166 | ): dataproc.SessionTemplate => ({ |
| 167 | name, |
| 168 | description: news.description, |
| 169 | labels: desiredLabels, |
| 170 | jupyterSession: defaultJupyter(news, templateId), |
| 171 | sparkConnectSession: news.sparkConnectSession, |
| 172 | runtimeConfig: news.runtimeConfig, |
| 173 | environmentConfig: news.environmentConfig, |
| 174 | }); |
| 175 | |
| 176 | const getByName = (name: string) => |
| 177 | dataproc |
| 178 | .getProjectsLocationsSessionTemplates({ name }) |
| 179 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 180 | |
| 181 | const listLocation = (project: string, location: string) => |
| 182 | emptyOnMissing( |
| 183 | collectPages( |
| 184 | dataproc.listProjectsLocationsSessionTemplates.pages({ |
| 185 | parent: locationParent(project, location), |
| 186 | pageSize: 1000, |
| 187 | }), |
| 188 | (page) => page.sessionTemplates, |
| 189 | ).pipe( |
| 190 | Effect.map((items) => |
| 191 | items |
| 192 | .filter((template) => hasAlchemyLabelMap(template.labels)) |
| 193 | .map((template) => toAttrs(template, project, location)), |
| 194 | ), |
| 195 | ), |
| 196 | ); |
| 197 | |
| 198 | const templateChanged = (current: dataproc.SessionTemplate, desired: dataproc.SessionTemplate) => |
| 199 | (current.description ?? "") !== (desired.description ?? "") || |
| 200 | fingerprint(current.jupyterSession) !== fingerprint(desired.jupyterSession) || |
| 201 | fingerprint(current.sparkConnectSession) !== fingerprint(desired.sparkConnectSession) || |
| 202 | fingerprint(current.runtimeConfig) !== fingerprint(desired.runtimeConfig) || |
| 203 | fingerprint(current.environmentConfig) !== fingerprint(desired.environmentConfig); |
| 204 | |
| 205 | export const SessionTemplateProvider = () => |
| 206 | Provider.succeed(SessionTemplate, { |
| 207 | stables: ["name", "templateId", "project", "location", "uuid", "createTime"], |
| 208 | |
| 209 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 210 | if (!isResolved(news)) return undefined; |
| 211 | const env = yield* GcpEnvironment.current; |
| 212 | const previousId = olds?.templateId ?? output?.templateId; |
| 213 | const nextId = news.templateId ?? previousId; |
| 214 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 215 | const nextLocation = normalizeLocation( |
| 216 | news.location ?? olds?.location ?? output?.location, |
| 217 | env.region, |
| 218 | ); |
| 219 | if ( |
| 220 | (previousId !== undefined && nextId !== undefined && previousId !== nextId) || |
| 221 | (output !== undefined && previousLocation !== nextLocation) |
| 222 | ) { |
| 223 | return { |
| 224 | action: "replace" as const, |
| 225 | deleteFirst: |
| 226 | previousLocation === nextLocation && previousId !== undefined && nextId === previousId, |
| 227 | }; |
| 228 | } |
| 229 | return undefined; |
| 230 | }), |
| 231 | |
| 232 | read: Effect.fn(function* ({ id, olds, output }) { |
| 233 | const env = yield* GcpEnvironment.current; |
| 234 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 235 | const templateId = yield* toPhysicalId( |
| 236 | id, |
| 237 | olds?.templateId, |
| 238 | output?.templateId, |
| 239 | MAX_SESSION_TEMPLATE_ID_LENGTH, |
| 240 | "session", |
| 241 | ); |
| 242 | const name = output?.name ?? resourceName(env.project, location, templateId); |
| 243 | const existing = yield* getByName(name); |
| 244 | if (existing === undefined) return undefined; |
| 245 | const attrs = toAttrs(existing, env.project, location); |
| 246 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 247 | }), |
| 248 | |
| 249 | list: () => |
| 250 | Effect.gen(function* () { |
| 251 | const env = yield* GcpEnvironment.current; |
| 252 | const pages = yield* Effect.forEach( |
| 253 | [...new Set([env.region, ...LIST_LOCATIONS])], |
| 254 | (location) => listLocation(env.project, location), |
| 255 | { concurrency: 4 }, |
| 256 | ); |
| 257 | return pages.flat(); |
| 258 | }), |
| 259 | |
| 260 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 261 | const env = yield* GcpEnvironment.current; |
| 262 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 263 | const templateId = yield* toPhysicalId( |
| 264 | id, |
| 265 | news.templateId, |
| 266 | output?.templateId, |
| 267 | MAX_SESSION_TEMPLATE_ID_LENGTH, |
| 268 | "session", |
| 269 | ); |
| 270 | const name = resourceName(env.project, location, templateId); |
| 271 | const desiredLabels = { |
| 272 | ...toLabels(news.labels), |
| 273 | ...(yield* createInternalLabels(id)), |
| 274 | }; |
| 275 | const desired = desiredBody(news, name, templateId, desiredLabels); |
| 276 | |
| 277 | let current = yield* getByName(name); |
| 278 | |
| 279 | if (current === undefined) { |
| 280 | const created = yield* dataproc |
| 281 | .createProjectsLocationsSessionTemplates({ |
| 282 | parent: locationParent(env.project, location), |
| 283 | body: desired, |
| 284 | }) |
| 285 | .pipe(Effect.catchTag("Conflict", () => getByName(name))); |
| 286 | current = created ?? undefined; |
| 287 | } |
| 288 | |
| 289 | if (current === undefined) { |
| 290 | return yield* new SessionTemplateNotResolved({ resourceName: name }); |
| 291 | } |
| 292 | |
| 293 | const observedLabels = tagRecord(current.labels); |
| 294 | const { upsert, removed } = diffLabels(observedLabels, desiredLabels); |
| 295 | const labelsChanged = upsert.length > 0 || removed.length > 0; |
| 296 | |
| 297 | if (labelsChanged || templateChanged(current, desired)) { |
| 298 | current = yield* dataproc.patchProjectsLocationsSessionTemplates({ |
| 299 | name: current.name ?? name, |
| 300 | body: { |
| 301 | ...desired, |
| 302 | name: current.name ?? name, |
| 303 | }, |
| 304 | }); |
| 305 | } |
| 306 | |
| 307 | return toAttrs(current, env.project, location); |
| 308 | }), |
| 309 | |
| 310 | delete: Effect.fn(function* ({ output }) { |
| 311 | if (!output.name) return; |
| 312 | yield* dataproc |
| 313 | .deleteProjectsLocationsSessionTemplates({ name: output.name }) |
| 314 | .pipe(Effect.catchTag("NotFound", () => Effect.void)); |
| 315 | yield* waitUntilGone(getByName(output.name), output.name); |
| 316 | }), |
| 317 | }); |