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 deadline from "@distilled.cloud/aws/deadline"; |
| 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 { createInternalTags, hasAlchemyTags, type Tags } from "alchemy/Tags"; |
| 8 | import * as Data from "effect/Data"; |
| 9 | import * as Effect from "effect/Effect"; |
| 10 | import * as EffectStream from "effect/Stream"; |
| 11 | import type { Providers } from "../Providers.ts"; |
| 12 | import { |
| 13 | deadlineArnOf, |
| 14 | fetchDeadlineTags, |
| 15 | retryThroughIamPropagation, |
| 16 | syncDeadlineTags, |
| 17 | } from "./internal.ts"; |
| 18 | |
| 19 | export class DeadlineMonitorInvalidProps extends Data.TaggedError("DeadlineMonitorInvalidProps")<{ |
| 20 | readonly message: string; |
| 21 | readonly cause?: unknown; |
| 22 | }> {} |
| 23 | |
| 24 | export class DeadlineMonitorUnexpectedResponse extends Data.TaggedError( |
| 25 | "DeadlineMonitorUnexpectedResponse", |
| 26 | )<{ |
| 27 | readonly message: string; |
| 28 | readonly cause?: unknown; |
| 29 | }> {} |
| 30 | |
| 31 | export interface MonitorProps { |
| 32 | /** |
| 33 | * Display name of the monitor, shown on its web page. |
| 34 | * @default ${app}-${stage}-${id} |
| 35 | */ |
| 36 | displayName?: string; |
| 37 | /** |
| 38 | * Subdomain the monitor URL is served under |
| 39 | * (`https://{subdomain}.{region}.deadlinecloud.amazonaws.com`). |
| 40 | */ |
| 41 | subdomain: string; |
| 42 | /** |
| 43 | * ARN of the IAM Identity Center instance responsible for authenticating |
| 44 | * monitor users. Changing it replaces the monitor. |
| 45 | */ |
| 46 | identityCenterInstanceArn: string; |
| 47 | /** |
| 48 | * Region of the IAM Identity Center instance, when it differs from the |
| 49 | * monitor's region. |
| 50 | */ |
| 51 | identityCenterRegion?: string; |
| 52 | /** |
| 53 | * ARN of the IAM role the monitor assumes on behalf of signed-in users. |
| 54 | */ |
| 55 | roleArn: string; |
| 56 | /** |
| 57 | * Tags to associate with the monitor. |
| 58 | */ |
| 59 | tags?: Record<string, string>; |
| 60 | } |
| 61 | |
| 62 | export interface Monitor extends Resource< |
| 63 | "AWS.Deadline.Monitor", |
| 64 | MonitorProps, |
| 65 | { |
| 66 | /** |
| 67 | * Service-assigned unique identifier of the monitor (`monitor-...`). |
| 68 | */ |
| 69 | monitorId: string; |
| 70 | /** |
| 71 | * ARN of the monitor. |
| 72 | */ |
| 73 | monitorArn: string; |
| 74 | /** |
| 75 | * The monitor's display name. |
| 76 | */ |
| 77 | displayName: string; |
| 78 | /** |
| 79 | * The configured subdomain. |
| 80 | */ |
| 81 | subdomain: string; |
| 82 | /** |
| 83 | * Full URL of the monitor's web page. |
| 84 | */ |
| 85 | url: string; |
| 86 | /** |
| 87 | * ARN of the monitor's user role. |
| 88 | */ |
| 89 | roleArn: string; |
| 90 | /** |
| 91 | * ARN of the IAM Identity Center instance backing sign-in. |
| 92 | */ |
| 93 | identityCenterInstanceArn: string; |
| 94 | /** |
| 95 | * ARN of the Identity Center application the monitor registered. |
| 96 | */ |
| 97 | identityCenterApplicationArn: string; |
| 98 | /** |
| 99 | * Current tags reported for the monitor. |
| 100 | */ |
| 101 | tags: Record<string, string>; |
| 102 | }, |
| 103 | never, |
| 104 | Providers |
| 105 | > {} |
| 106 | |
| 107 | /** |
| 108 | * An AWS Deadline Cloud monitor — the hosted web console where artists and |
| 109 | * administrators view farms, queues, and jobs, authenticated through IAM |
| 110 | * Identity Center. |
| 111 | * |
| 112 | * ### Creating Monitors |
| 113 | * **Example:** Basic Monitor |
| 114 | * ```typescript |
| 115 | * import * as AWS from "@alchemy.run/aws"; |
| 116 | * |
| 117 | * const monitor = yield* AWS.Deadline.Monitor("StudioMonitor", { |
| 118 | * subdomain: "studio-renders", |
| 119 | * identityCenterInstanceArn: "arn:aws:sso:::instance/ssoins-1234567890abcdef", |
| 120 | * roleArn: monitorRole.roleArn, |
| 121 | * }); |
| 122 | * ``` |
| 123 | * |
| 124 | * **Example:** Export the Monitor URL |
| 125 | * ```typescript |
| 126 | * // The monitor's web console URL is available as an output attribute — |
| 127 | * // return it from the stack so users know where to sign in. |
| 128 | * const monitor = yield* AWS.Deadline.Monitor("StudioMonitor", { |
| 129 | * subdomain: "studio-renders", |
| 130 | * identityCenterInstanceArn: identityCenterArn, |
| 131 | * roleArn: monitorRole.roleArn, |
| 132 | * }); |
| 133 | * return { monitorUrl: monitor.url }; |
| 134 | * ``` |
| 135 | * |
| 136 | * @resource |
| 137 | */ |
| 138 | export const Monitor = Resource<Monitor>("AWS.Deadline.Monitor"); |
| 139 | |
| 140 | const createMonitorName = (id: string, props: { displayName?: string | undefined }) => |
| 141 | props.displayName |
| 142 | ? Effect.succeed(props.displayName) |
| 143 | : createPhysicalName({ id, maxLength: 100 }); |
| 144 | |
| 145 | interface MonitorState { |
| 146 | attrs: Monitor["Attributes"]; |
| 147 | described: deadline.GetMonitorResponse; |
| 148 | } |
| 149 | |
| 150 | const readMonitorById = Effect.fn(function* (monitorId: string, arnOf: (path: string) => string) { |
| 151 | const described = yield* deadline |
| 152 | .getMonitor({ monitorId }) |
| 153 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.undefined)); |
| 154 | if (!described) return undefined; |
| 155 | const monitorArn = arnOf(`monitor/${described.monitorId}`); |
| 156 | const state: MonitorState = { |
| 157 | described, |
| 158 | attrs: { |
| 159 | monitorId: described.monitorId, |
| 160 | monitorArn, |
| 161 | displayName: described.displayName, |
| 162 | subdomain: described.subdomain, |
| 163 | url: described.url, |
| 164 | roleArn: described.roleArn, |
| 165 | identityCenterInstanceArn: described.identityCenterInstanceArn, |
| 166 | identityCenterApplicationArn: described.identityCenterApplicationArn, |
| 167 | tags: yield* fetchDeadlineTags(monitorArn), |
| 168 | }, |
| 169 | }; |
| 170 | return state; |
| 171 | }); |
| 172 | |
| 173 | const findMonitorBySubdomain = Effect.fn(function* ( |
| 174 | subdomain: string, |
| 175 | arnOf: (path: string) => string, |
| 176 | ) { |
| 177 | const summaries = yield* deadline.listMonitors.items({}).pipe( |
| 178 | EffectStream.runCollect, |
| 179 | Effect.map((chunk) => Array.from(chunk)), |
| 180 | ); |
| 181 | const match = summaries.find((summary) => summary.subdomain === subdomain); |
| 182 | if (!match) return undefined; |
| 183 | return yield* readMonitorById(match.monitorId, arnOf); |
| 184 | }); |
| 185 | |
| 186 | export const MonitorProvider = () => |
| 187 | Provider.effect( |
| 188 | Monitor, |
| 189 | Effect.gen(function* () { |
| 190 | return { |
| 191 | stables: [ |
| 192 | "monitorId", |
| 193 | "monitorArn", |
| 194 | "identityCenterInstanceArn", |
| 195 | "identityCenterApplicationArn", |
| 196 | ], |
| 197 | list: () => |
| 198 | Effect.gen(function* () { |
| 199 | const arnOf = yield* deadlineArnOf; |
| 200 | const summaries = yield* deadline.listMonitors.items({}).pipe( |
| 201 | EffectStream.runCollect, |
| 202 | Effect.map((chunk) => Array.from(chunk)), |
| 203 | ); |
| 204 | const states = yield* Effect.forEach( |
| 205 | summaries, |
| 206 | (summary) => readMonitorById(summary.monitorId, arnOf), |
| 207 | { concurrency: 4 }, |
| 208 | ); |
| 209 | return states |
| 210 | .filter((state): state is MonitorState => state !== undefined) |
| 211 | .map((state) => state.attrs); |
| 212 | }), |
| 213 | read: Effect.fn(function* ({ id, olds, output }) { |
| 214 | const arnOf = yield* deadlineArnOf; |
| 215 | const subdomain = output?.subdomain ?? olds?.subdomain; |
| 216 | const state = output?.monitorId |
| 217 | ? yield* readMonitorById(output.monitorId, arnOf) |
| 218 | : subdomain !== undefined |
| 219 | ? yield* findMonitorBySubdomain(subdomain, arnOf) |
| 220 | : undefined; |
| 221 | if (!state) return undefined; |
| 222 | return (yield* hasAlchemyTags(id, state.attrs.tags as Tags)) |
| 223 | ? state.attrs |
| 224 | : Unowned(state.attrs); |
| 225 | }), |
| 226 | diff: Effect.fn(function* ({ news, olds }) { |
| 227 | if (!isResolved(news)) return; |
| 228 | if (olds === undefined) return; |
| 229 | // The Identity Center instance is fixed at creation. |
| 230 | if (olds.identityCenterInstanceArn !== news.identityCenterInstanceArn) { |
| 231 | return { action: "replace" } as const; |
| 232 | } |
| 233 | }), |
| 234 | reconcile: Effect.fn(function* ({ id, news, output, session }) { |
| 235 | if (news === undefined) { |
| 236 | return yield* new DeadlineMonitorInvalidProps({ |
| 237 | message: "AWS.Deadline.Monitor requires props", |
| 238 | }); |
| 239 | } |
| 240 | const arnOf = yield* deadlineArnOf; |
| 241 | const displayName = yield* createMonitorName(id, news); |
| 242 | const internalTags = yield* createInternalTags(id); |
| 243 | const desiredTags = { ...internalTags, ...news.tags }; |
| 244 | |
| 245 | // Observe. |
| 246 | let state = output?.monitorId |
| 247 | ? yield* readMonitorById(output.monitorId, arnOf) |
| 248 | : yield* findMonitorBySubdomain(news.subdomain, arnOf); |
| 249 | |
| 250 | // Ensure. |
| 251 | if (state === undefined) { |
| 252 | const created = yield* retryThroughIamPropagation( |
| 253 | deadline.createMonitor({ |
| 254 | displayName, |
| 255 | subdomain: news.subdomain, |
| 256 | identityCenterInstanceArn: news.identityCenterInstanceArn, |
| 257 | identityCenterRegion: news.identityCenterRegion, |
| 258 | roleArn: news.roleArn, |
| 259 | tags: desiredTags, |
| 260 | }), |
| 261 | ); |
| 262 | yield* session.note(`Created monitor ${displayName} (${created.monitorId})`); |
| 263 | state = yield* readMonitorById(created.monitorId, arnOf); |
| 264 | if (state === undefined) { |
| 265 | return yield* new DeadlineMonitorUnexpectedResponse({ |
| 266 | message: `failed to read created monitor ${displayName}`, |
| 267 | }); |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | // Sync mutable settings — only when drifted from OBSERVED state. |
| 272 | const described = state.described; |
| 273 | const needsUpdate = |
| 274 | displayName !== described.displayName || |
| 275 | news.subdomain !== described.subdomain || |
| 276 | news.roleArn !== described.roleArn; |
| 277 | if (needsUpdate) { |
| 278 | yield* deadline.updateMonitor({ |
| 279 | monitorId: state.attrs.monitorId, |
| 280 | displayName, |
| 281 | subdomain: news.subdomain, |
| 282 | roleArn: news.roleArn, |
| 283 | }); |
| 284 | yield* session.note(`Updated monitor ${displayName}`); |
| 285 | } |
| 286 | |
| 287 | // Sync tags — diff against observed cloud tags. |
| 288 | yield* syncDeadlineTags(state.attrs.monitorArn, desiredTags); |
| 289 | |
| 290 | yield* session.note(state.attrs.monitorArn); |
| 291 | const final = yield* readMonitorById(state.attrs.monitorId, arnOf); |
| 292 | if (!final) { |
| 293 | return yield* new DeadlineMonitorUnexpectedResponse({ |
| 294 | message: `failed to read reconciled monitor ${displayName}`, |
| 295 | }); |
| 296 | } |
| 297 | return final.attrs; |
| 298 | }), |
| 299 | delete: Effect.fn(function* ({ output }) { |
| 300 | yield* deadline |
| 301 | .deleteMonitor({ monitorId: output.monitorId }) |
| 302 | .pipe(Effect.catchTag("ResourceNotFoundException", () => Effect.void)); |
| 303 | }), |
| 304 | }; |
| 305 | }), |
| 306 | ); |