@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, 06:00 UTC

main
ts 290 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 EffectStream from "effect/Stream";
11 import type { Providers } from "../Providers.ts";
12
13 export class EndpointConfigInvalidProps extends Data.TaggedError("EndpointConfigInvalidProps")<{
14 readonly message: string;
15 readonly cause?: unknown;
16 }> {}
17
18 export class EndpointConfigUnexpectedResponse extends Data.TaggedError(
19 "EndpointConfigUnexpectedResponse",
20 )<{
21 readonly message: string;
22 readonly cause?: unknown;
23 }> {}
24
25 export interface EndpointConfigProps {
26 /**
27 * Name of the endpoint configuration. Maximum 63 characters.
28 * @default ${app}-${stage}-${id}
29 */
30 endpointConfigName?: string;
31 /**
32 * The models to host and the resources (instances or serverless capacity)
33 * to deploy each on. At least one variant is required. Use
34 * `ServerlessConfig` for pay-per-request serverless inference or
35 * `InstanceType` + `InitialInstanceCount` for provisioned instances.
36 */
37 productionVariants: sagemaker.ProductionVariant[];
38 /**
39 * Shadow variants that receive a copy of the traffic for testing a new
40 * model behind the production variant.
41 */
42 shadowProductionVariants?: sagemaker.ProductionVariant[];
43 /**
44 * Capture inference request/response payloads to S3 (for monitoring or
45 * retraining).
46 */
47 dataCaptureConfig?: sagemaker.DataCaptureConfig;
48 /**
49 * KMS key that SageMaker uses to encrypt data on the storage volume of the
50 * hosting instances. Not supported for serverless variants.
51 */
52 kmsKeyId?: string;
53 /**
54 * Configure the endpoint for asynchronous inference.
55 */
56 asyncInferenceConfig?: sagemaker.AsyncInferenceConfig;
57 /**
58 * SageMaker Clarify explainer configuration.
59 */
60 explainerConfig?: sagemaker.ExplainerConfig;
61 /**
62 * IAM role used by the endpoint (when variants require one).
63 */
64 executionRoleArn?: string;
65 /**
66 * VPC configuration for the hosted models.
67 */
68 vpcConfig?: sagemaker.VpcConfig;
69 /**
70 * Isolate the hosted containers from the network.
71 * @default false
72 */
73 enableNetworkIsolation?: boolean;
74 /**
75 * Tags to associate with the endpoint configuration. Merged with internal
76 * Alchemy tags.
77 */
78 tags?: Record<string, string>;
79 }
80
81 export interface EndpointConfig extends Resource<
82 "AWS.SageMaker.EndpointConfig",
83 EndpointConfigProps,
84 {
85 /**
86 * The endpoint configuration's name.
87 */
88 endpointConfigName: string;
89 /**
90 * ARN of the endpoint configuration.
91 */
92 endpointConfigArn: string;
93 },
94 never,
95 Providers
96 > {}
97
98 /**
99 * An Amazon SageMaker EndpointConfig — the deployment recipe that maps one
100 * or more `Model`s to hosting resources (provisioned instances or serverless
101 * capacity). Pure configuration: it costs nothing until an `Endpoint`
102 * references it.
103 *
104 * Endpoint configurations are immutable — any change other than tags
105 * replaces the configuration. To roll a live endpoint onto new settings,
106 * point the `Endpoint` at the replacement config (alchemy creates the new
107 * config first, updates the endpoint, then deletes the old config).
108 * ### Creating Endpoint Configurations
109 * **Example:** Serverless Variant
110 * ```typescript
111 * import * as AWS from "@alchemy.run/aws";
112 *
113 * const config = yield* AWS.SageMaker.EndpointConfig("MyConfig", {
114 * productionVariants: [{
115 * VariantName: "AllTraffic",
116 * ModelName: model.modelName,
117 * ServerlessConfig: { MemorySizeInMB: 2048, MaxConcurrency: 5 },
118 * }],
119 * });
120 * ```
121 *
122 * **Example:** Provisioned Instances
123 * ```typescript
124 * const config = yield* AWS.SageMaker.EndpointConfig("MyConfig", {
125 * productionVariants: [{
126 * VariantName: "AllTraffic",
127 * ModelName: model.modelName,
128 * InstanceType: "ml.m5.large",
129 * InitialInstanceCount: 1,
130 * }],
131 * });
132 * ```
133 *
134 * @resource
135 */
136 export const EndpointConfig = Resource<EndpointConfig>("AWS.SageMaker.EndpointConfig");
137
138 const createConfigName = (id: string, props: { endpointConfigName?: string | undefined }) =>
139 props.endpointConfigName
140 ? Effect.succeed(props.endpointConfigName)
141 : createPhysicalName({ id, maxLength: 63 });
142
143 const fetchConfigTags = Effect.fn(function* (arn: string) {
144 const response = yield* sagemaker
145 .listTags({ ResourceArn: arn })
146 .pipe(Effect.catchTag("AccessDeniedException", () => Effect.undefined));
147 return Object.fromEntries(
148 (response?.Tags ?? []).flatMap((tag) =>
149 tag.Key !== undefined ? [[tag.Key, tag.Value ?? ""]] : [],
150 ),
151 );
152 });
153
154 const readEndpointConfig = Effect.fn(function* (name: string) {
155 const described = yield* sagemaker
156 .describeEndpointConfig({ EndpointConfigName: name })
157 .pipe(Effect.catchTag("EndpointConfigNotFound", () => Effect.undefined));
158 if (!described) return undefined;
159 return {
160 endpointConfigName: described.EndpointConfigName,
161 endpointConfigArn: described.EndpointConfigArn,
162 };
163 });
164
165 export const EndpointConfigProvider = () =>
166 Provider.effect(
167 EndpointConfig,
168 Effect.gen(function* () {
169 return {
170 stables: ["endpointConfigName", "endpointConfigArn"],
171 list: () =>
172 Effect.gen(function* () {
173 const summaries = yield* sagemaker.listEndpointConfigs.pages({}).pipe(
174 EffectStream.runCollect,
175 Effect.map((chunk) =>
176 Array.from(chunk).flatMap((page) => page.EndpointConfigs ?? []),
177 ),
178 );
179 return summaries.flatMap((s) =>
180 s.EndpointConfigName !== undefined && s.EndpointConfigArn !== undefined
181 ? [
182 {
183 endpointConfigName: s.EndpointConfigName,
184 endpointConfigArn: s.EndpointConfigArn,
185 },
186 ]
187 : [],
188 );
189 }),
190 read: Effect.fn(function* ({ id, olds, output }) {
191 const name = output?.endpointConfigName ?? (yield* createConfigName(id, olds ?? {}));
192 const attrs = yield* readEndpointConfig(name);
193 if (!attrs) return undefined;
194 const tags = yield* fetchConfigTags(attrs.endpointConfigArn);
195 return (yield* hasAlchemyTags(id, tags as Tags)) ? attrs : Unowned(attrs);
196 }),
197 diff: Effect.fn(function* ({ id, news, olds }) {
198 if (!isResolved(news)) return;
199 if (olds === undefined) return;
200 // Endpoint configs are immutable — everything except tags replaces.
201 const identity = (props: EndpointConfigProps) => [
202 props.productionVariants,
203 props.shadowProductionVariants,
204 props.dataCaptureConfig,
205 props.kmsKeyId,
206 props.asyncInferenceConfig,
207 props.explainerConfig,
208 props.executionRoleArn,
209 props.vpcConfig,
210 props.enableNetworkIsolation ?? false,
211 ];
212 const oldName = yield* createConfigName(id, olds);
213 const newName = yield* createConfigName(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 EndpointConfigInvalidProps({
224 message: "SageMaker EndpointConfig requires props",
225 });
226 }
227 const name = output?.endpointConfigName ?? (yield* createConfigName(id, news));
228 const internalTags = yield* createInternalTags(id);
229 const desiredTags = { ...internalTags, ...news.tags };
230
231 // Observe.
232 let attrs = yield* readEndpointConfig(name);
233
234 // Ensure — create if missing; tolerate the already-exists race.
235 if (attrs === undefined) {
236 yield* sagemaker
237 .createEndpointConfig({
238 EndpointConfigName: name,
239 ProductionVariants: news.productionVariants,
240 ShadowProductionVariants: news.shadowProductionVariants,
241 DataCaptureConfig: news.dataCaptureConfig,
242 KmsKeyId: news.kmsKeyId,
243 AsyncInferenceConfig: news.asyncInferenceConfig,
244 ExplainerConfig: news.explainerConfig,
245 ExecutionRoleArn: news.executionRoleArn,
246 VpcConfig: news.vpcConfig,
247 EnableNetworkIsolation: news.enableNetworkIsolation,
248 Tags: Object.entries(desiredTags).map(([Key, Value]) => ({
249 Key,
250 Value,
251 })),
252 })
253 .pipe(Effect.catchTag("EndpointConfigAlreadyExists", () => Effect.void));
254 attrs = yield* readEndpointConfig(name);
255 if (attrs === undefined) {
256 return yield* new EndpointConfigUnexpectedResponse({
257 message: `failed to read created endpoint config ${name}`,
258 });
259 }
260 }
261
262 // Sync tags — diff against OBSERVED cloud tags.
263 const currentTags = yield* fetchConfigTags(attrs.endpointConfigArn);
264 const { removed, upsert } = diffTags(currentTags, desiredTags);
265 if (removed.length > 0) {
266 yield* sagemaker.deleteTags({
267 ResourceArn: attrs.endpointConfigArn,
268 TagKeys: removed,
269 });
270 }
271 if (upsert.length > 0) {
272 yield* sagemaker.addTags({
273 ResourceArn: attrs.endpointConfigArn,
274 Tags: upsert.map(({ Key, Value }) => ({ Key, Value })),
275 });
276 }
277
278 yield* session.note(attrs.endpointConfigArn);
279 return attrs;
280 }),
281 delete: Effect.fn(function* ({ output }) {
282 yield* sagemaker
283 .deleteEndpointConfig({
284 EndpointConfigName: output.endpointConfigName,
285 })
286 .pipe(Effect.catchTag("EndpointConfigNotFound", () => Effect.void));
287 }),
288 };
289 }),
290 );