The best way to control Claude Code, Codex and more from any computer or phone

main
ts 102 lines 3.53 KB
Raw
1 import { Role } from "@alchemy.run/aws/IAM/Role";
2 import * as Lambda from "@alchemy.run/aws/Lambda";
3 import * as SageMaker from "@alchemy.run/aws/SageMaker";
4 import * as Duration from "effect/Duration";
5 import * as Effect from "effect/Effect";
6 import { HttpServerRequest } from "effect/http/HttpServerRequest";
7 import * as HttpServerResponse from "effect/http/HttpServerResponse";
8 import * as Layer from "effect/Layer";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "endpoint-handler.ts");
12
13 export class SageMakerEndpointTestFunction extends Lambda.Function<Lambda.Function>()(
14 "SageMakerEndpointTestFunction",
15 ) {}
16
17 // Gated fixture — deploying a live endpoint takes minutes and requires a
18 // working serving container (AWS_TEST_SAGEMAKER_IMAGE). Only deployed by
19 // EndpointBindings.test.ts when AWS_TEST_SAGEMAKER_ENDPOINT=1.
20 export default SageMakerEndpointTestFunction.make(
21 {
22 main,
23 functionUrl: true,
24 timeout: Duration.seconds(30),
25 },
26 Effect.gen(function* () {
27 const image = process.env.AWS_TEST_SAGEMAKER_IMAGE!;
28 const modelData = process.env.AWS_TEST_SAGEMAKER_MODEL_DATA;
29
30 const role = yield* Role("SageMakerBindingsEndpointRole", {
31 assumeRolePolicyDocument: {
32 Version: "2012-10-17",
33 Statement: [
34 {
35 Effect: "Allow",
36 Principal: { Service: "sagemaker.amazonaws.com" },
37 Action: ["sts:AssumeRole"],
38 },
39 ],
40 },
41 managedPolicyArns: ["arn:aws:iam::aws:policy/AmazonSageMakerFullAccess"],
42 });
43 const model = yield* SageMaker.Model("BindingsEndpointModel", {
44 executionRoleArn: role.roleArn,
45 primaryContainer: {
46 Image: image,
47 ...(modelData ? { ModelDataUrl: modelData } : {}),
48 },
49 });
50 const config = yield* SageMaker.EndpointConfig("BindingsEndpointConfig", {
51 productionVariants: [
52 {
53 VariantName: "AllTraffic",
54 ModelName: model.modelName,
55 ServerlessConfig: { MemorySizeInMB: 2048, MaxConcurrency: 1 },
56 },
57 ],
58 });
59 const endpoint = yield* SageMaker.Endpoint("BindingsEndpoint", {
60 endpointConfigName: config.endpointConfigName,
61 });
62
63 const describeEndpoint = yield* SageMaker.DescribeEndpoint(endpoint);
64 const updateWeights = yield* SageMaker.UpdateEndpointWeightsAndCapacities(endpoint);
65
66 const bound = { describeEndpoint, updateWeights };
67
68 return {
69 fetch: Effect.gen(function* () {
70 const request = yield* HttpServerRequest;
71 const url = new URL(request.originalUrl);
72 const pathname = url.pathname;
73
74 if (request.method === "GET" && pathname === "/bindings") {
75 return yield* HttpServerResponse.json({ bound: Object.keys(bound) });
76 }
77
78 if (request.method === "GET" && pathname === "/describe-endpoint") {
79 const described = yield* describeEndpoint();
80 return yield* HttpServerResponse.json({
81 endpointName: described.EndpointName,
82 status: described.EndpointStatus,
83 variants: (described.ProductionVariants ?? []).map((v) => v.VariantName),
84 });
85 }
86
87 if (request.method === "GET" && pathname === "/health") {
88 return yield* HttpServerResponse.json({ ok: true });
89 }
90
91 return yield* HttpServerResponse.json({ error: "Not found", pathname }, { status: 404 });
92 }).pipe(Effect.orDie),
93 };
94 }).pipe(
95 Effect.provide(
96 Layer.mergeAll(
97 SageMaker.DescribeEndpointHttp,
98 SageMaker.UpdateEndpointWeightsAndCapacitiesHttp,
99 ),
100 ),
101 ),
102 );