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 * as Schedule from "effect/Schedule"; |
| 10 | import { GcpEnvironment } from "../Environment.ts"; |
| 11 | import { createInternalLabels, hasAlchemyLabels, toLabels } from "../Labels.ts"; |
| 12 | import type { Providers } from "../Providers.ts"; |
| 13 | import { |
| 14 | collectPages, |
| 15 | LIST_LOCATIONS, |
| 16 | MAX_POLLS, |
| 17 | MAX_WORKLOAD_ID_LENGTH, |
| 18 | emptyOnMissing, |
| 19 | hasAlchemyLabelMap, |
| 20 | locationParent, |
| 21 | normalizeLocation, |
| 22 | parseResourceName, |
| 23 | toPhysicalId, |
| 24 | userLabels, |
| 25 | waitForOperation, |
| 26 | waitUntilExists, |
| 27 | waitUntilGone, |
| 28 | } from "./internal.ts"; |
| 29 | |
| 30 | const READY = new Set(["ACTIVE", "TERMINATING", "TERMINATED", "FAILED"]); |
| 31 | |
| 32 | export type SessionProps = { |
| 33 | /** |
| 34 | * Session id (the `{session}` segment of |
| 35 | * `projects/{project}/locations/{location}/sessions/{session}`). If |
| 36 | * omitted, a unique RFC1035 name is generated. Immutable — changing it |
| 37 | * replaces the session. |
| 38 | */ |
| 39 | sessionId?: string; |
| 40 | /** |
| 41 | * Location (`us-central1`, …). Immutable — changing it replaces the |
| 42 | * session. `US-CENTRAL1` is accepted and normalized to `us-central1`. |
| 43 | * @default the stack's GCP region (`GCP.Region`, profile region, `us-central1`) |
| 44 | */ |
| 45 | location?: string; |
| 46 | /** |
| 47 | * User labels. Alchemy ownership labels are merged in automatically. |
| 48 | */ |
| 49 | labels?: Record<string, string>; |
| 50 | /** |
| 51 | * Existing session template resource name. If omitted, `jupyterSession` |
| 52 | * (default PYTHON) is used. |
| 53 | */ |
| 54 | sessionTemplate?: string; |
| 55 | /** |
| 56 | * Jupyter kernel config. |
| 57 | */ |
| 58 | jupyterSession?: dataproc.JupyterConfig; |
| 59 | /** |
| 60 | * Spark Connect session config. |
| 61 | */ |
| 62 | sparkConnectSession?: dataproc.SparkConnectConfig; |
| 63 | /** |
| 64 | * Runtime configuration (version, properties, container image). |
| 65 | */ |
| 66 | runtimeConfig?: dataproc.RuntimeConfig; |
| 67 | /** |
| 68 | * Environment configuration (execution and peripherals). |
| 69 | */ |
| 70 | environmentConfig?: dataproc.EnvironmentConfig; |
| 71 | /** |
| 72 | * Email of the user who owns the session. |
| 73 | */ |
| 74 | user?: string; |
| 75 | }; |
| 76 | |
| 77 | export type Session = Resource< |
| 78 | "GCP.Dataproc.Session", |
| 79 | SessionProps, |
| 80 | { |
| 81 | /** Full resource name `projects/{project}/locations/{location}/sessions/{session}`. */ |
| 82 | name: string; |
| 83 | /** Session id (last path segment). */ |
| 84 | sessionId: string; |
| 85 | /** Project id. */ |
| 86 | project: string; |
| 87 | /** Location id. */ |
| 88 | location: string; |
| 89 | /** User labels (Alchemy ownership labels stripped). */ |
| 90 | labels: Record<string, string>; |
| 91 | /** Server-reported state (`ACTIVE`, `CREATING`, …). */ |
| 92 | state: string | undefined; |
| 93 | /** Extra status text, if any. */ |
| 94 | stateMessage: string | undefined; |
| 95 | /** Session template used, if any. */ |
| 96 | sessionTemplate: string | undefined; |
| 97 | /** Server-assigned uuid. */ |
| 98 | uuid: string | undefined; |
| 99 | /** Creator email. */ |
| 100 | creator: string | undefined; |
| 101 | /** RFC3339 creation timestamp. */ |
| 102 | createTime: string | undefined; |
| 103 | }, |
| 104 | never, |
| 105 | Providers |
| 106 | >; |
| 107 | |
| 108 | /** |
| 109 | * A Dataproc serverless interactive session. |
| 110 | * |
| 111 | * Sessions are immutable after create. Changing identity or config |
| 112 | * replaces the session. Provisioning typically takes several minutes. |
| 113 | * |
| 114 | * ### Creating a Session |
| 115 | * **Example:** Jupyter session |
| 116 | * ```typescript |
| 117 | * const session = yield* GCP.Dataproc.Session("Notebook", { |
| 118 | * jupyterSession: { kernel: "PYTHON" }, |
| 119 | * environmentConfig: { executionConfig: { idleTtl: "600s", ttl: "3600s" } }, |
| 120 | * }); |
| 121 | * ``` |
| 122 | * |
| 123 | * **Example:** From a session template |
| 124 | * ```typescript |
| 125 | * const session = yield* GCP.Dataproc.Session("Notebook", { |
| 126 | * sessionTemplate: template.name, |
| 127 | * }); |
| 128 | * ``` |
| 129 | * |
| 130 | * @resource |
| 131 | * @category Dataproc |
| 132 | */ |
| 133 | export const Session = Resource<Session>("GCP.Dataproc.Session"); |
| 134 | |
| 135 | export class SessionNotResolved extends Data.TaggedError("GCP.Dataproc.SessionNotResolved")<{ |
| 136 | resourceName: string; |
| 137 | }> {} |
| 138 | |
| 139 | export class SessionFailed extends Data.TaggedError("GCP.Dataproc.SessionFailed")<{ |
| 140 | resourceName: string; |
| 141 | state: string | undefined; |
| 142 | detail: string | undefined; |
| 143 | }> {} |
| 144 | |
| 145 | export class SessionNotReady extends Data.TaggedError("GCP.Dataproc.SessionNotReady")<{ |
| 146 | resourceName: string; |
| 147 | state: string | undefined; |
| 148 | }> {} |
| 149 | |
| 150 | const resourceName = (project: string, location: string, sessionId: string) => |
| 151 | `${locationParent(project, location)}/sessions/${sessionId}`; |
| 152 | |
| 153 | const defaultJupyter = (news: SessionProps): dataproc.JupyterConfig | undefined => { |
| 154 | if (news.sessionTemplate !== undefined) return news.jupyterSession; |
| 155 | if (news.sparkConnectSession !== undefined) return undefined; |
| 156 | return news.jupyterSession ?? { kernel: "PYTHON" }; |
| 157 | }; |
| 158 | |
| 159 | const toAttrs = (session: dataproc.Session, project: string, location: string) => { |
| 160 | const name = session.name ?? ""; |
| 161 | const parsed = parseResourceName(name, "sessions", location); |
| 162 | return { |
| 163 | name, |
| 164 | sessionId: parsed.id, |
| 165 | project: parsed.project || project, |
| 166 | location: parsed.location || location, |
| 167 | labels: userLabels(session.labels), |
| 168 | state: session.state, |
| 169 | stateMessage: session.stateMessage, |
| 170 | sessionTemplate: session.sessionTemplate, |
| 171 | uuid: session.uuid, |
| 172 | creator: session.creator, |
| 173 | createTime: session.createTime, |
| 174 | }; |
| 175 | }; |
| 176 | |
| 177 | // Dataproc rejects a session create whose body omits `name` with a bare |
| 178 | // `INVALID_ARGUMENT`. |
| 179 | const desiredBody = ( |
| 180 | news: SessionProps, |
| 181 | name: string, |
| 182 | desiredLabels: Record<string, string>, |
| 183 | ): dataproc.Session => ({ |
| 184 | name, |
| 185 | labels: desiredLabels, |
| 186 | sessionTemplate: news.sessionTemplate, |
| 187 | jupyterSession: defaultJupyter(news), |
| 188 | sparkConnectSession: news.sparkConnectSession, |
| 189 | runtimeConfig: news.runtimeConfig, |
| 190 | environmentConfig: news.environmentConfig ?? { |
| 191 | executionConfig: { idleTtl: "600s", ttl: "3600s" }, |
| 192 | }, |
| 193 | user: news.user, |
| 194 | }); |
| 195 | |
| 196 | const getByName = (name: string) => |
| 197 | dataproc |
| 198 | .getProjectsLocationsSessions({ name }) |
| 199 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 200 | |
| 201 | const waitUntilReady = (name: string) => |
| 202 | getByName(name).pipe( |
| 203 | Effect.flatMap((session) => |
| 204 | session |
| 205 | ? Effect.succeed(session) |
| 206 | : Effect.fail(new SessionNotResolved({ resourceName: name })), |
| 207 | ), |
| 208 | Effect.filterOrFail( |
| 209 | (session) => session.state !== "FAILED", |
| 210 | (session) => |
| 211 | new SessionFailed({ |
| 212 | resourceName: name, |
| 213 | state: session.state, |
| 214 | detail: session.stateMessage, |
| 215 | }), |
| 216 | ), |
| 217 | Effect.filterOrFail( |
| 218 | (session) => READY.has(session.state ?? ""), |
| 219 | (session) => |
| 220 | new SessionNotReady({ |
| 221 | resourceName: name, |
| 222 | state: session.state, |
| 223 | }), |
| 224 | ), |
| 225 | Effect.retry({ |
| 226 | while: (error) => |
| 227 | error._tag === "GCP.Dataproc.SessionNotReady" || |
| 228 | error._tag === "GCP.Dataproc.SessionNotResolved", |
| 229 | times: MAX_POLLS, |
| 230 | schedule: Schedule.spaced("5 seconds"), |
| 231 | }), |
| 232 | ); |
| 233 | |
| 234 | const listLocation = (project: string, location: string) => |
| 235 | emptyOnMissing( |
| 236 | collectPages( |
| 237 | dataproc.listProjectsLocationsSessions.pages({ |
| 238 | parent: locationParent(project, location), |
| 239 | pageSize: 1000, |
| 240 | }), |
| 241 | (page) => page.sessions, |
| 242 | ).pipe( |
| 243 | Effect.map((items) => |
| 244 | items |
| 245 | .filter((session) => hasAlchemyLabelMap(session.labels)) |
| 246 | .map((session) => toAttrs(session, project, location)), |
| 247 | ), |
| 248 | ), |
| 249 | ); |
| 250 | |
| 251 | export const SessionProvider = () => |
| 252 | Provider.succeed(Session, { |
| 253 | stables: ["name", "sessionId", "project", "location", "uuid", "createTime"], |
| 254 | |
| 255 | diff: Effect.fn(function* ({ news, olds, output }) { |
| 256 | if (!isResolved(news)) return undefined; |
| 257 | const env = yield* GcpEnvironment.current; |
| 258 | const previousId = olds?.sessionId ?? output?.sessionId; |
| 259 | const nextId = news.sessionId ?? previousId; |
| 260 | const previousLocation = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 261 | const nextLocation = normalizeLocation( |
| 262 | news.location ?? olds?.location ?? output?.location, |
| 263 | env.region, |
| 264 | ); |
| 265 | if ( |
| 266 | (previousId !== undefined && nextId !== undefined && previousId !== nextId) || |
| 267 | (output !== undefined && previousLocation !== nextLocation) |
| 268 | ) { |
| 269 | return { |
| 270 | action: "replace" as const, |
| 271 | deleteFirst: |
| 272 | previousLocation === nextLocation && previousId !== undefined && nextId === previousId, |
| 273 | }; |
| 274 | } |
| 275 | return undefined; |
| 276 | }), |
| 277 | |
| 278 | read: Effect.fn(function* ({ id, olds, output }) { |
| 279 | const env = yield* GcpEnvironment.current; |
| 280 | const location = normalizeLocation(olds?.location ?? output?.location, env.region); |
| 281 | const sessionId = yield* toPhysicalId( |
| 282 | id, |
| 283 | olds?.sessionId, |
| 284 | output?.sessionId, |
| 285 | MAX_WORKLOAD_ID_LENGTH, |
| 286 | "session", |
| 287 | ); |
| 288 | const name = output?.name ?? resourceName(env.project, location, sessionId); |
| 289 | const existing = yield* getByName(name); |
| 290 | if (existing === undefined) return undefined; |
| 291 | const attrs = toAttrs(existing, env.project, location); |
| 292 | return (yield* hasAlchemyLabels(id, tagRecord(existing.labels))) ? attrs : Unowned(attrs); |
| 293 | }), |
| 294 | |
| 295 | list: () => |
| 296 | Effect.gen(function* () { |
| 297 | const env = yield* GcpEnvironment.current; |
| 298 | const pages = yield* Effect.forEach( |
| 299 | [...new Set([env.region, ...LIST_LOCATIONS])], |
| 300 | (location) => listLocation(env.project, location), |
| 301 | { concurrency: 4 }, |
| 302 | ); |
| 303 | return pages.flat(); |
| 304 | }), |
| 305 | |
| 306 | reconcile: Effect.fn(function* ({ id, news, output }) { |
| 307 | const env = yield* GcpEnvironment.current; |
| 308 | const location = normalizeLocation(news.location ?? output?.location, env.region); |
| 309 | const sessionId = yield* toPhysicalId( |
| 310 | id, |
| 311 | news.sessionId, |
| 312 | output?.sessionId, |
| 313 | MAX_WORKLOAD_ID_LENGTH, |
| 314 | "session", |
| 315 | ); |
| 316 | const name = resourceName(env.project, location, sessionId); |
| 317 | const desiredLabels = { |
| 318 | ...toLabels(news.labels), |
| 319 | ...(yield* createInternalLabels(id)), |
| 320 | }; |
| 321 | const desired = desiredBody(news, name, desiredLabels); |
| 322 | |
| 323 | let current = yield* getByName(name); |
| 324 | |
| 325 | if (current === undefined) { |
| 326 | const created = yield* dataproc |
| 327 | .createProjectsLocationsSessions({ |
| 328 | parent: locationParent(env.project, location), |
| 329 | sessionId, |
| 330 | body: desired, |
| 331 | }) |
| 332 | .pipe(Effect.catchTag("Conflict", () => Effect.undefined)); |
| 333 | if (created) { |
| 334 | yield* waitForOperation(created); |
| 335 | } |
| 336 | current = yield* waitUntilExists(getByName(name), name); |
| 337 | current = yield* waitUntilReady(name); |
| 338 | } |
| 339 | |
| 340 | if (current === undefined) { |
| 341 | return yield* new SessionNotResolved({ resourceName: name }); |
| 342 | } |
| 343 | |
| 344 | return toAttrs(current, env.project, location); |
| 345 | }), |
| 346 | |
| 347 | delete: Effect.fn(function* ({ output }) { |
| 348 | if (!output.name) return; |
| 349 | const operation = yield* dataproc |
| 350 | .deleteProjectsLocationsSessions({ name: output.name }) |
| 351 | .pipe(Effect.catchTag("NotFound", () => Effect.undefined)); |
| 352 | if (operation !== undefined) { |
| 353 | yield* waitForOperation(operation, { notFoundOk: true }); |
| 354 | } |
| 355 | yield* waitUntilGone(getByName(output.name), output.name); |
| 356 | }), |
| 357 | }); |