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

main
ts 164 lines 5.8 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as SageMaker from "@alchemy.run/aws/SageMaker";
3 import * as Duration from "effect/Duration";
4 import * as Effect from "effect/Effect";
5 import { HttpServerRequest } from "effect/http/HttpServerRequest";
6 import * as HttpServerResponse from "effect/http/HttpServerResponse";
7 import * as Layer from "effect/Layer";
8 import * as Stream from "effect/Stream";
9 import path from "pathe";
10
11 const main = path.resolve(import.meta.dirname, "handler.ts");
12
13 export class SageMakerTestFunction extends Lambda.Function<Lambda.Function>()(
14 "SageMakerTestFunction",
15 ) {}
16
17 export default SageMakerTestFunction.make(
18 {
19 main,
20 functionUrl: true,
21 timeout: Duration.seconds(30),
22 },
23 Effect.gen(function* () {
24 const featureGroup = yield* SageMaker.FeatureGroup("BindingsFeatures", {
25 recordIdentifierFeatureName: "user_id",
26 eventTimeFeatureName: "event_time",
27 featureDefinitions: [
28 { FeatureName: "user_id", FeatureType: "String" },
29 { FeatureName: "event_time", FeatureType: "String" },
30 { FeatureName: "clicks", FeatureType: "Integral" },
31 ],
32 onlineStoreConfig: { EnableOnlineStore: true },
33 });
34
35 // Event source: subscribe the host to SageMaker feature-group state
36 // changes. The deploy proves the EventBridge rule + invoke permission
37 // wiring.
38 yield* SageMaker.consumeSageMakerEvents({ kinds: ["feature-group"] }, (events) =>
39 Stream.runForEach(events, (event) =>
40 Effect.log(
41 `feature group ${event.detail.FeatureGroupName} -> ${event.detail.FeatureGroupStatus}`,
42 ),
43 ),
44 );
45
46 const putRecord = yield* SageMaker.PutRecord(featureGroup);
47 const getRecord = yield* SageMaker.GetRecord(featureGroup);
48 const deleteRecord = yield* SageMaker.DeleteRecord(featureGroup);
49 const batchGetRecord = yield* SageMaker.BatchGetRecord(featureGroup);
50 const batchWriteRecord = yield* SageMaker.BatchWriteRecord(featureGroup);
51 const listRecords = yield* SageMaker.ListRecords(featureGroup);
52
53 const bound = {
54 putRecord,
55 getRecord,
56 deleteRecord,
57 batchGetRecord,
58 batchWriteRecord,
59 listRecords,
60 };
61
62 const record = (userId: string, clicks: number) => [
63 { FeatureName: "user_id", ValueAsString: userId },
64 { FeatureName: "event_time", ValueAsString: new Date().toISOString() },
65 { FeatureName: "clicks", ValueAsString: String(clicks) },
66 ];
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 === "POST" && pathname === "/put-record") {
79 const body = (yield* request.json) as unknown as {
80 userId: string;
81 clicks: number;
82 };
83 yield* putRecord({ Record: record(body.userId, body.clicks) });
84 return yield* HttpServerResponse.json({ success: true });
85 }
86
87 if (request.method === "GET" && pathname === "/get-record") {
88 const userId = url.searchParams.get("userId") ?? "";
89 const result = yield* getRecord({
90 RecordIdentifierValueAsString: userId,
91 });
92 return yield* HttpServerResponse.json({
93 record: result.Record ?? [],
94 });
95 }
96
97 if (request.method === "POST" && pathname === "/delete-record") {
98 const body = (yield* request.json) as unknown as { userId: string };
99 yield* deleteRecord({
100 RecordIdentifierValueAsString: body.userId,
101 EventTime: new Date().toISOString(),
102 });
103 return yield* HttpServerResponse.json({ success: true });
104 }
105
106 if (request.method === "POST" && pathname === "/batch-write-record") {
107 const body = (yield* request.json) as unknown as {
108 records: { userId: string; clicks: number }[];
109 };
110 const result = yield* batchWriteRecord({
111 Entries: body.records.map((r) => ({
112 Record: record(r.userId, r.clicks),
113 })),
114 });
115 return yield* HttpServerResponse.json({
116 errors: result.Errors ?? [],
117 unprocessed: result.UnprocessedEntries ?? [],
118 });
119 }
120
121 if (request.method === "POST" && pathname === "/batch-get-record") {
122 const body = (yield* request.json) as unknown as {
123 userIds: string[];
124 };
125 const result = yield* batchGetRecord({
126 RecordIdentifiersValueAsString: body.userIds,
127 });
128 return yield* HttpServerResponse.json({
129 records: (result.Records ?? []).map((r) => ({
130 userId: r.RecordIdentifierValueAsString,
131 record: r.Record ?? [],
132 })),
133 errors: result.Errors ?? [],
134 });
135 }
136
137 if (request.method === "GET" && pathname === "/list-records") {
138 const result = yield* listRecords({ MaxResults: 100 });
139 return yield* HttpServerResponse.json({
140 identifiers: result.RecordIdentifiers ?? [],
141 });
142 }
143
144 if (request.method === "GET" && pathname === "/health") {
145 return yield* HttpServerResponse.json({ ok: true });
146 }
147
148 return yield* HttpServerResponse.json({ error: "Not found", pathname }, { status: 404 });
149 }).pipe(Effect.orDie),
150 };
151 }).pipe(
152 Effect.provide(
153 Layer.mergeAll(
154 Lambda.EventSource,
155 SageMaker.PutRecordHttp,
156 SageMaker.GetRecordHttp,
157 SageMaker.DeleteRecordHttp,
158 SageMaker.BatchGetRecordHttp,
159 SageMaker.BatchWriteRecordHttp,
160 SageMaker.ListRecordsHttp,
161 ),
162 ),
163 ),
164 );