@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

main
ts 283 lines 10.2 KB
Raw
1 import * as sagemaker from "@distilled.cloud/aws/sagemaker";
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, diffTags, hasAlchemyTags, type Tags } from "alchemy/Tags";
8 import * as Data from "effect/Data";
9 import * as Effect from "effect/Effect";
10 import * as Schedule from "effect/Schedule";
11 import * as EffectStream from "effect/Stream";
12 import type { Providers } from "../Providers.ts";
13
14 export class ModelInvalidProps extends Data.TaggedError("ModelInvalidProps")<{
15 readonly message: string;
16 readonly cause?: unknown;
17 }> {}
18
19 export class ModelUnexpectedResponse extends Data.TaggedError("ModelUnexpectedResponse")<{
20 readonly message: string;
21 readonly cause?: unknown;
22 }> {}
23
24 export interface ModelProps {
25 /**
26 * Name of the model. Must be unique within the account/region.
27 * Maximum 63 characters, alphanumeric and hyphens.
28 * @default ${app}-${stage}-${id}
29 */
30 modelName?: string;
31 /**
32 * The primary inference container: the ECR image (and optionally model
33 * artifacts in S3) SageMaker runs when the model is deployed to an
34 * endpoint. Exactly one of `primaryContainer` or `containers` is required.
35 */
36 primaryContainer?: sagemaker.ContainerDefinition;
37 /**
38 * Containers for an inference pipeline (executed as a sequence) or a
39 * multi-container model. Mutually exclusive with `primaryContainer`.
40 */
41 containers?: sagemaker.ContainerDefinition[];
42 /**
43 * How containers in a multi-container model are run (`Serial` pipeline or
44 * `Direct` invocation of a specific container).
45 */
46 inferenceExecutionConfig?: sagemaker.InferenceExecutionConfig;
47 /**
48 * ARN of the IAM role SageMaker assumes to pull the container image and
49 * model artifacts. The role must trust `sagemaker.amazonaws.com`.
50 */
51 executionRoleArn: string;
52 /**
53 * VPC configuration for the model's containers.
54 */
55 vpcConfig?: sagemaker.VpcConfig;
56 /**
57 * Isolate the model containers from the network (no outbound calls).
58 * @default false
59 */
60 enableNetworkIsolation?: boolean;
61 /**
62 * Tags to associate with the model. Merged with internal Alchemy tags.
63 */
64 tags?: Record<string, string>;
65 }
66
67 export interface Model extends Resource<
68 "AWS.SageMaker.Model",
69 ModelProps,
70 {
71 /**
72 * The model's name.
73 */
74 modelName: string;
75 /**
76 * ARN of the model.
77 */
78 modelArn: string;
79 },
80 never,
81 Providers
82 > {}
83
84 /**
85 * An Amazon SageMaker Model — the immutable pairing of an inference
86 * container image (and optional S3 model artifacts) with an execution role.
87 * A model is pure configuration: it costs nothing until it is deployed to an
88 * endpoint via an `EndpointConfig` + `Endpoint`.
89 *
90 * SageMaker models are immutable — any change other than tags replaces the
91 * model.
92 * ### Creating Models
93 * **Example:** Model from an ECR image
94 * ```typescript
95 * import * as AWS from "@alchemy.run/aws";
96 *
97 * const role = yield* AWS.IAM.Role("SageMakerRole", {
98 * assumeRolePolicyDocument: {
99 * Version: "2012-10-17",
100 * Statement: [{
101 * Effect: "Allow",
102 * Principal: { Service: "sagemaker.amazonaws.com" },
103 * Action: ["sts:AssumeRole"],
104 * }],
105 * },
106 * managedPolicyArns: ["arn:aws:iam::aws:policy/AmazonSageMakerFullAccess"],
107 * });
108 *
109 * const model = yield* AWS.SageMaker.Model("MyModel", {
110 * executionRoleArn: role.roleArn,
111 * primaryContainer: {
112 * Image: "123456789012.dkr.ecr.us-west-2.amazonaws.com/my-inference:latest",
113 * ModelDataUrl: "s3://my-bucket/model.tar.gz",
114 * },
115 * });
116 * ```
117 *
118 * **Example:** Serverless deployment (Model → EndpointConfig → Endpoint)
119 * ```typescript
120 * const config = yield* AWS.SageMaker.EndpointConfig("MyConfig", {
121 * productionVariants: [{
122 * VariantName: "AllTraffic",
123 * ModelName: model.modelName,
124 * ServerlessConfig: { MemorySizeInMB: 2048, MaxConcurrency: 5 },
125 * }],
126 * });
127 * const endpoint = yield* AWS.SageMaker.Endpoint("MyEndpoint", {
128 * endpointConfigName: config.endpointConfigName,
129 * });
130 * ```
131 *
132 * @resource
133 */
134 export const Model = Resource<Model>("AWS.SageMaker.Model");
135
136 const createModelName = (id: string, props: { modelName?: string | undefined }) =>
137 props.modelName ? Effect.succeed(props.modelName) : createPhysicalName({ id, maxLength: 63 });
138
139 const fetchModelTags = Effect.fn(function* (arn: string) {
140 const tags = yield* sagemaker.listTags.items({ ResourceArn: arn }).pipe(
141 EffectStream.runCollect,
142 Effect.map((chunk) => Array.from(chunk)),
143 // A just-deleted (or foreign) resource ARN surfaces as AccessDenied.
144 Effect.catchTag("AccessDeniedException", () => Effect.succeed<sagemaker.Tag[]>([])),
145 );
146 return Object.fromEntries(
147 tags.flatMap((tag) => (tag.Key !== undefined ? [[tag.Key, tag.Value ?? ""]] : [])),
148 );
149 });
150
151 const readModel = Effect.fn(function* (modelName: string) {
152 const described = yield* sagemaker
153 .describeModel({ ModelName: modelName })
154 .pipe(Effect.catchTag("ModelNotFound", () => Effect.undefined));
155 if (!described) return undefined;
156 return {
157 modelName: described.ModelName,
158 modelArn: described.ModelArn,
159 };
160 });
161
162 // CreateModel validates the execution role (existence + sagemaker trust); a
163 // freshly-created IAM role takes a few seconds to propagate and surfaces as
164 // ValidationException. Bounded retry through the propagation window.
165 // Explicitly typed so Retry.Return's conditional type never reaches
166 // declaration emit (it would widen AWS.providers() to unknown downstream).
167 const retryThroughIamPropagation = <A, E extends { readonly _tag: string }, R>(
168 self: Effect.Effect<A, E, R>,
169 ): Effect.Effect<A, E, R> =>
170 Effect.retry(self, {
171 while: (e) => e._tag === "ValidationException",
172 schedule: Schedule.max([Schedule.spaced("5 seconds"), Schedule.recurs(10)]),
173 });
174
175 export const ModelProvider = () =>
176 Provider.effect(
177 Model,
178 Effect.gen(function* () {
179 return {
180 stables: ["modelName", "modelArn"],
181 list: () =>
182 Effect.gen(function* () {
183 const summaries = yield* sagemaker.listModels.pages({}).pipe(
184 EffectStream.runCollect,
185 Effect.map((chunk) => Array.from(chunk).flatMap((page) => page.Models ?? [])),
186 );
187 return summaries.flatMap((s) =>
188 s.ModelName !== undefined && s.ModelArn !== undefined
189 ? [{ modelName: s.ModelName, modelArn: s.ModelArn }]
190 : [],
191 );
192 }),
193 read: Effect.fn(function* ({ id, olds, output }) {
194 const modelName = output?.modelName ?? (yield* createModelName(id, olds ?? {}));
195 const attrs = yield* readModel(modelName);
196 if (!attrs) return undefined;
197 const tags = yield* fetchModelTags(attrs.modelArn);
198 return (yield* hasAlchemyTags(id, tags as Tags)) ? attrs : Unowned(attrs);
199 }),
200 diff: Effect.fn(function* ({ id, news, olds }) {
201 if (!isResolved(news)) return;
202 if (olds === undefined) return;
203 // SageMaker models are immutable — everything except tags replaces.
204 const identity = (props: ModelProps) => [
205 props.primaryContainer,
206 props.containers,
207 props.inferenceExecutionConfig,
208 props.executionRoleArn,
209 props.vpcConfig,
210 props.enableNetworkIsolation ?? false,
211 ];
212 const oldName = yield* createModelName(id, olds);
213 const newName = yield* createModelName(id, news);
214 if (
215 oldName !== newName ||
216 JSON.stringify(identity(olds)) !== JSON.stringify(identity(news))
217 ) {
218 return { action: "replace" } as const;
219 }
220 }),
221 reconcile: Effect.fn(function* ({ id, news, output, session }) {
222 if (!news) {
223 return yield* new ModelInvalidProps({ message: "SageMaker Model requires props" });
224 }
225 const modelName = output?.modelName ?? (yield* createModelName(id, news));
226 const internalTags = yield* createInternalTags(id);
227 const desiredTags = { ...internalTags, ...news.tags };
228
229 // Observe — cloud state is authoritative.
230 let attrs = yield* readModel(modelName);
231
232 // Ensure — create if missing; tolerate the already-exists race.
233 if (attrs === undefined) {
234 yield* retryThroughIamPropagation(
235 sagemaker.createModel({
236 ModelName: modelName,
237 PrimaryContainer: news.primaryContainer,
238 Containers: news.containers,
239 InferenceExecutionConfig: news.inferenceExecutionConfig,
240 ExecutionRoleArn: news.executionRoleArn,
241 VpcConfig: news.vpcConfig,
242 EnableNetworkIsolation: news.enableNetworkIsolation,
243 Tags: Object.entries(desiredTags).map(([Key, Value]) => ({
244 Key,
245 Value,
246 })),
247 }),
248 ).pipe(Effect.catchTag("ModelAlreadyExists", () => Effect.void));
249 attrs = yield* readModel(modelName);
250 if (attrs === undefined) {
251 return yield* new ModelUnexpectedResponse({
252 message: `failed to read created model ${modelName}`,
253 });
254 }
255 }
256
257 // Sync tags — diff against OBSERVED cloud tags.
258 const currentTags = yield* fetchModelTags(attrs.modelArn);
259 const { removed, upsert } = diffTags(currentTags, desiredTags);
260 if (removed.length > 0) {
261 yield* sagemaker.deleteTags({
262 ResourceArn: attrs.modelArn,
263 TagKeys: removed,
264 });
265 }
266 if (upsert.length > 0) {
267 yield* sagemaker.addTags({
268 ResourceArn: attrs.modelArn,
269 Tags: upsert.map(({ Key, Value }) => ({ Key, Value })),
270 });
271 }
272
273 yield* session.note(attrs.modelArn);
274 return attrs;
275 }),
276 delete: Effect.fn(function* ({ output }) {
277 yield* sagemaker
278 .deleteModel({ ModelName: output.modelName })
279 .pipe(Effect.catchTag("ModelNotFound", () => Effect.void));
280 }),
281 };
282 }),
283 );