@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 421 lines 14.7 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Personalize from "@alchemy.run/aws/Personalize";
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 path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "handler.ts");
11
12 export class PersonalizeTestFunction extends Lambda.Function<Lambda.Function>()(
13 "PersonalizeTestFunction",
14 ) {}
15
16 const INTERACTIONS_SCHEMA = JSON.stringify({
17 type: "record",
18 name: "Interactions",
19 namespace: "com.amazonaws.personalize.schema",
20 fields: [
21 { name: "USER_ID", type: "string" },
22 { name: "ITEM_ID", type: "string" },
23 { name: "TIMESTAMP", type: "long" },
24 ],
25 version: "1.0",
26 });
27
28 const ITEMS_SCHEMA = JSON.stringify({
29 type: "record",
30 name: "Items",
31 namespace: "com.amazonaws.personalize.schema",
32 fields: [
33 { name: "ITEM_ID", type: "string" },
34 { name: "CATEGORY", type: "string", categorical: true },
35 ],
36 version: "1.0",
37 });
38
39 const USERS_SCHEMA = JSON.stringify({
40 type: "record",
41 name: "Users",
42 namespace: "com.amazonaws.personalize.schema",
43 fields: [
44 { name: "USER_ID", type: "string" },
45 { name: "MEMBERSHIP", type: "string", categorical: true },
46 ],
47 version: "1.0",
48 });
49
50 /**
51 * Personalize bindings fixture: deploys the cheap definition resources (three
52 * schemas, a dataset group, Interactions/Items/Users datasets, and an event
53 * tracker) plus a Lambda bound to all nineteen Personalize bindings.
54 *
55 * The event-ingestion routes (`/put-events`, `/put-items`, `/put-users`)
56 * exercise the data plane for real against the deployed datasets/tracker.
57 * The campaign/solution routes are probes: training a solution takes ~an hour
58 * of paid compute, so they drive well-formed-but-nonexistent ARNs (passed by
59 * the test as a query parameter) and return the typed error tag — proving the
60 * deploy-time IAM bind, the runtime call plumbing, and the typed error decode.
61 */
62 export default PersonalizeTestFunction.make(
63 {
64 main,
65 functionUrl: true,
66 timeout: Duration.seconds(30),
67 },
68 Effect.gen(function* () {
69 const interactionsSchema = yield* Personalize.Schema("InteractionsSchema", {
70 schema: INTERACTIONS_SCHEMA,
71 });
72 const itemsSchema = yield* Personalize.Schema("ItemsSchema", {
73 schema: ITEMS_SCHEMA,
74 });
75 const usersSchema = yield* Personalize.Schema("UsersSchema", {
76 schema: USERS_SCHEMA,
77 });
78 const group = yield* Personalize.DatasetGroup("BindingsGroup", {});
79 const interactions = yield* Personalize.Dataset("Interactions", {
80 schemaArn: interactionsSchema.schemaArn,
81 datasetGroupArn: group.datasetGroupArn,
82 datasetType: "Interactions",
83 });
84 const items = yield* Personalize.Dataset("Items", {
85 schemaArn: itemsSchema.schemaArn,
86 datasetGroupArn: group.datasetGroupArn,
87 datasetType: "Items",
88 });
89 const users = yield* Personalize.Dataset("Users", {
90 schemaArn: usersSchema.schemaArn,
91 datasetGroupArn: group.datasetGroupArn,
92 datasetType: "Users",
93 });
94 // Depend on the Interactions dataset (events are recorded into it) so the
95 // tracker is created after it and deleted before it.
96 const tracker = yield* Personalize.EventTracker("Tracker", {
97 datasetGroupArn: interactions.datasetGroupArn,
98 });
99
100 const putEvents = yield* Personalize.PutEvents(tracker);
101 const putActionInteractions = yield* Personalize.PutActionInteractions(tracker);
102 const putItems = yield* Personalize.PutItems(items);
103 const putUsers = yield* Personalize.PutUsers(users);
104 // Bound to the Items dataset on purpose: without an Actions dataset the
105 // route is a typed-error probe for the PutActions plumbing.
106 const putActions = yield* Personalize.PutActions(items);
107 const getRecommendations = yield* Personalize.GetRecommendations();
108 const getPersonalizedRanking = yield* Personalize.GetPersonalizedRanking();
109 const getActionRecommendations = yield* Personalize.GetActionRecommendations();
110 const createDatasetImportJob = yield* Personalize.CreateDatasetImportJob();
111 const describeDatasetImportJob = yield* Personalize.DescribeDatasetImportJob();
112 const createSolutionVersion = yield* Personalize.CreateSolutionVersion();
113 const describeSolutionVersion = yield* Personalize.DescribeSolutionVersion();
114 const updateCampaign = yield* Personalize.UpdateCampaign();
115 const describeCampaign = yield* Personalize.DescribeCampaign();
116 const createSolution = yield* Personalize.CreateSolution();
117 const createCampaign = yield* Personalize.CreateCampaign();
118 const getSolutionMetrics = yield* Personalize.GetSolutionMetrics();
119 const createBatchInferenceJob = yield* Personalize.CreateBatchInferenceJob();
120 const describeBatchInferenceJob = yield* Personalize.DescribeBatchInferenceJob();
121
122 const bound = {
123 putEvents,
124 putActionInteractions,
125 putItems,
126 putUsers,
127 putActions,
128 getRecommendations,
129 getPersonalizedRanking,
130 getActionRecommendations,
131 createDatasetImportJob,
132 describeDatasetImportJob,
133 createSolutionVersion,
134 describeSolutionVersion,
135 updateCampaign,
136 describeCampaign,
137 createSolution,
138 createCampaign,
139 getSolutionMetrics,
140 createBatchInferenceJob,
141 describeBatchInferenceJob,
142 };
143
144 return {
145 fetch: Effect.gen(function* () {
146 const request = yield* HttpServerRequest;
147 const url = new URL(request.originalUrl);
148 const pathname = url.pathname;
149 const arn = url.searchParams.get("arn") ?? "";
150 const roleArn =
151 url.searchParams.get("role") ?? "arn:aws:iam::000000000000:role/alchemy_probe";
152
153 if (pathname === "/bindings") {
154 return yield* HttpServerResponse.json({ bound: Object.keys(bound) });
155 }
156
157 if (pathname === "/put-events") {
158 const tag = yield* putEvents({
159 sessionId: "alchemy-session-1",
160 userId: "alchemy-user-1",
161 eventList: [
162 {
163 eventType: "click",
164 itemId: "alchemy-item-1",
165 sentAt: new Date(),
166 },
167 ],
168 }).pipe(
169 Effect.map(() => "Recorded"),
170 Effect.catch((e) => Effect.succeed(e._tag)),
171 );
172 return yield* HttpServerResponse.json({ tag });
173 }
174
175 if (pathname === "/put-items") {
176 const tag = yield* putItems({
177 items: [
178 {
179 itemId: "alchemy-item-1",
180 properties: JSON.stringify({ category: "books" }),
181 },
182 ],
183 }).pipe(
184 Effect.map(() => "Recorded"),
185 Effect.catch((e) => Effect.succeed(e._tag)),
186 );
187 return yield* HttpServerResponse.json({ tag });
188 }
189
190 if (pathname === "/put-users") {
191 const tag = yield* putUsers({
192 users: [
193 {
194 userId: "alchemy-user-1",
195 properties: JSON.stringify({ membership: "gold" }),
196 },
197 ],
198 }).pipe(
199 Effect.map(() => "Recorded"),
200 Effect.catch((e) => Effect.succeed(e._tag)),
201 );
202 return yield* HttpServerResponse.json({ tag });
203 }
204
205 if (pathname === "/put-actions-probe") {
206 const tag = yield* putActions({
207 actions: [{ actionId: "alchemy-action-1" }],
208 }).pipe(
209 Effect.map(() => "Recorded"),
210 Effect.catch((e) => Effect.succeed(e._tag)),
211 );
212 return yield* HttpServerResponse.json({ tag });
213 }
214
215 if (pathname === "/put-action-interactions-probe") {
216 const tag = yield* putActionInteractions({
217 actionInteractions: [
218 {
219 actionId: "alchemy-action-1",
220 userId: "alchemy-user-1",
221 sessionId: "alchemy-session-1",
222 eventType: "Taken",
223 timestamp: new Date(),
224 },
225 ],
226 }).pipe(
227 Effect.map(() => "Recorded"),
228 Effect.catch((e) => Effect.succeed(e._tag)),
229 );
230 return yield* HttpServerResponse.json({ tag });
231 }
232
233 if (pathname === "/recommendations-probe") {
234 const tag = yield* getRecommendations({
235 campaignArn: arn,
236 userId: "alchemy-user-1",
237 }).pipe(
238 Effect.map(() => "Recommended"),
239 Effect.catch((e) => Effect.succeed(e._tag)),
240 );
241 return yield* HttpServerResponse.json({ tag });
242 }
243
244 if (pathname === "/ranking-probe") {
245 const tag = yield* getPersonalizedRanking({
246 campaignArn: arn,
247 userId: "alchemy-user-1",
248 inputList: ["alchemy-item-1", "alchemy-item-2"],
249 }).pipe(
250 Effect.map(() => "Ranked"),
251 Effect.catch((e) => Effect.succeed(e._tag)),
252 );
253 return yield* HttpServerResponse.json({ tag });
254 }
255
256 if (pathname === "/action-recommendations-probe") {
257 const tag = yield* getActionRecommendations({
258 campaignArn: arn,
259 userId: "alchemy-user-1",
260 }).pipe(
261 Effect.map(() => "Recommended"),
262 Effect.catch((e) => Effect.succeed(e._tag)),
263 );
264 return yield* HttpServerResponse.json({ tag });
265 }
266
267 if (pathname === "/import-create-probe") {
268 const tag = yield* createDatasetImportJob({
269 jobName: "alchemy-import-probe",
270 datasetArn: arn,
271 dataSource: {
272 dataLocation: "s3://alchemy-nonexistent-probe-bucket/data.csv",
273 },
274 roleArn,
275 }).pipe(
276 Effect.map(() => "Created"),
277 Effect.catch((e) => Effect.succeed(e._tag)),
278 );
279 return yield* HttpServerResponse.json({ tag });
280 }
281
282 if (pathname === "/import-probe") {
283 const tag = yield* describeDatasetImportJob({
284 datasetImportJobArn: arn,
285 }).pipe(
286 Effect.map(() => "Found"),
287 Effect.catch((e) => Effect.succeed(e._tag)),
288 );
289 return yield* HttpServerResponse.json({ tag });
290 }
291
292 if (pathname === "/solution-version-create-probe") {
293 const tag = yield* createSolutionVersion({
294 solutionArn: arn,
295 }).pipe(
296 Effect.map(() => "Created"),
297 Effect.catch((e) => Effect.succeed(e._tag)),
298 );
299 return yield* HttpServerResponse.json({ tag });
300 }
301
302 if (pathname === "/solution-version-probe") {
303 const tag = yield* describeSolutionVersion({
304 solutionVersionArn: arn,
305 }).pipe(
306 Effect.map(() => "Found"),
307 Effect.catch((e) => Effect.succeed(e._tag)),
308 );
309 return yield* HttpServerResponse.json({ tag });
310 }
311
312 if (pathname === "/campaign-update-probe") {
313 const tag = yield* updateCampaign({ campaignArn: arn }).pipe(
314 Effect.map(() => "Updated"),
315 Effect.catch((e) => Effect.succeed(e._tag)),
316 );
317 return yield* HttpServerResponse.json({ tag });
318 }
319
320 if (pathname === "/campaign-probe") {
321 const tag = yield* describeCampaign({ campaignArn: arn }).pipe(
322 Effect.map(() => "Found"),
323 Effect.catch((e) => Effect.succeed(e._tag)),
324 );
325 return yield* HttpServerResponse.json({ tag });
326 }
327
328 if (pathname === "/solution-create-probe") {
329 const tag = yield* createSolution({
330 name: "alchemy-solution-probe",
331 recipeArn: "arn:aws:personalize:::recipe/aws-user-personalization",
332 datasetGroupArn: arn,
333 }).pipe(
334 Effect.map(() => "Created"),
335 Effect.catch((e) => Effect.succeed(e._tag)),
336 );
337 return yield* HttpServerResponse.json({ tag });
338 }
339
340 if (pathname === "/campaign-create-probe") {
341 const tag = yield* createCampaign({
342 name: "alchemy-campaign-probe",
343 solutionVersionArn: arn,
344 }).pipe(
345 Effect.map(() => "Created"),
346 Effect.catch((e) => Effect.succeed(e._tag)),
347 );
348 return yield* HttpServerResponse.json({ tag });
349 }
350
351 if (pathname === "/solution-metrics-probe") {
352 const tag = yield* getSolutionMetrics({
353 solutionVersionArn: arn,
354 }).pipe(
355 Effect.map(() => "Found"),
356 Effect.catch((e) => Effect.succeed(e._tag)),
357 );
358 return yield* HttpServerResponse.json({ tag });
359 }
360
361 if (pathname === "/batch-create-probe") {
362 const tag = yield* createBatchInferenceJob({
363 jobName: "alchemy-batch-probe",
364 solutionVersionArn: arn,
365 jobInput: {
366 s3DataSource: {
367 path: "s3://alchemy-nonexistent-probe-bucket/users.json",
368 },
369 },
370 jobOutput: {
371 s3DataDestination: {
372 path: "s3://alchemy-nonexistent-probe-bucket/scores/",
373 },
374 },
375 roleArn,
376 }).pipe(
377 Effect.map(() => "Created"),
378 Effect.catch((e) => Effect.succeed(e._tag)),
379 );
380 return yield* HttpServerResponse.json({ tag });
381 }
382
383 if (pathname === "/batch-probe") {
384 const tag = yield* describeBatchInferenceJob({
385 batchInferenceJobArn: arn,
386 }).pipe(
387 Effect.map(() => "Found"),
388 Effect.catch((e) => Effect.succeed(e._tag)),
389 );
390 return yield* HttpServerResponse.json({ tag });
391 }
392
393 return yield* HttpServerResponse.json({ error: "Not found", pathname }, { status: 404 });
394 }).pipe(Effect.orDie),
395 };
396 }).pipe(
397 Effect.provide(
398 Layer.mergeAll(
399 Personalize.PutEventsHttp,
400 Personalize.PutActionInteractionsHttp,
401 Personalize.PutItemsHttp,
402 Personalize.PutUsersHttp,
403 Personalize.PutActionsHttp,
404 Personalize.GetRecommendationsHttp,
405 Personalize.GetPersonalizedRankingHttp,
406 Personalize.GetActionRecommendationsHttp,
407 Personalize.CreateDatasetImportJobHttp,
408 Personalize.DescribeDatasetImportJobHttp,
409 Personalize.CreateSolutionVersionHttp,
410 Personalize.DescribeSolutionVersionHttp,
411 Personalize.UpdateCampaignHttp,
412 Personalize.DescribeCampaignHttp,
413 Personalize.CreateSolutionHttp,
414 Personalize.CreateCampaignHttp,
415 Personalize.GetSolutionMetricsHttp,
416 Personalize.CreateBatchInferenceJobHttp,
417 Personalize.DescribeBatchInferenceJobHttp,
418 ),
419 ),
420 ),
421 );