@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 255 lines 9.13 KB
Raw
1 import * as DataBrew from "@alchemy.run/aws/DataBrew";
2 import * as IAM from "@alchemy.run/aws/IAM";
3 import * as Lambda from "@alchemy.run/aws/Lambda";
4 import * as S3 from "@alchemy.run/aws/S3";
5 import type * as databrew from "@distilled.cloud/aws/databrew";
6 import * as Duration from "effect/Duration";
7 import * as Effect from "effect/Effect";
8 import { HttpServerRequest } from "effect/http/HttpServerRequest";
9 import * as HttpServerResponse from "effect/http/HttpServerResponse";
10 import * as Layer from "effect/Layer";
11 import * as Schedule from "effect/Schedule";
12 import * as Stream from "effect/Stream";
13 import path from "pathe";
14
15 const main = path.resolve(import.meta.dirname, "handler.ts");
16
17 export const SOURCE_KEY = "raw/data.csv";
18
19 // DataBrew can observe a fresh role's grants at different times per operation.
20 const authorizationPolicy = {
21 while: (
22 error:
23 | databrew.StartJobRunError
24 | databrew.PublishRecipeError
25 | databrew.StartProjectSessionError
26 | databrew.SendProjectSessionActionError,
27 ) => error._tag === "AccessDeniedException",
28 schedule: Schedule.spaced("3 seconds"),
29 times: 8,
30 };
31
32 /**
33 * Shared foundation for the bindings fixture: bucket + DataBrew service role
34 * + dataset + published recipe. The Bindings test deploys this FIRST, seeds
35 * the CSV source object, and only then deploys the full fixture — DataBrew
36 * validates the dataset's source object with the job role when the job and
37 * project are created.
38 */
39 export const foundation = Effect.gen(function* () {
40 const bucket = yield* S3.Bucket("DataBrewBindingsBucket", {
41 forceDestroy: true,
42 });
43 const role = yield* IAM.Role("DataBrewBindingsRole", {
44 assumeRolePolicyDocument: {
45 Version: "2012-10-17",
46 Statement: [
47 {
48 Effect: "Allow",
49 Principal: { Service: "databrew.amazonaws.com" },
50 Action: ["sts:AssumeRole"],
51 },
52 ],
53 },
54 managedPolicyArns: [
55 "arn:aws:iam::aws:policy/service-role/AwsGlueDataBrewServiceRole",
56 "arn:aws:iam::aws:policy/AmazonS3FullAccess",
57 ],
58 });
59 const dataset = yield* DataBrew.Dataset("DataBrewBindingsSource", {
60 format: "CSV",
61 formatOptions: { csv: { delimiter: ",", headerRow: true } },
62 input: {
63 s3InputDefinition: { bucket: bucket.bucketName, key: SOURCE_KEY },
64 },
65 });
66 const recipe = yield* DataBrew.Recipe("DataBrewBindingsRecipe", {
67 publish: true,
68 steps: [
69 {
70 action: {
71 operation: "UPPER_CASE",
72 parameters: { sourceColumn: "name" },
73 },
74 },
75 ],
76 });
77 return { bucket, role, dataset, recipe };
78 });
79
80 export class DataBrewTestFunction extends Lambda.Function<Lambda.Function>()(
81 "DataBrewTestFunction",
82 ) {}
83
84 /**
85 * Every route answers `{ …fields }` on success or `{ errorTag }` when the
86 * operation fails with a TYPED error — the test asserts the tag is a typed,
87 * non-authorization tag, which proves both the binding wiring and the IAM
88 * grant. An untyped error crashes into a 500 instead.
89 */
90 const errorTagged = <A, E extends { _tag: string }, R>(
91 effect: Effect.Effect<A, E, R>,
92 ): Effect.Effect<A | { errorTag: string }, never, R> =>
93 effect.pipe(
94 Effect.map((a): A | { errorTag: string } => a),
95 Effect.catch((e) => Effect.succeed({ errorTag: e._tag })),
96 );
97
98 export default DataBrewTestFunction.make(
99 {
100 main,
101 functionUrl: true,
102 // Session start + action retries fan out SDK calls — AWS's 3s default
103 // intermittently times out under cold starts.
104 timeout: Duration.seconds(90),
105 },
106 Effect.gen(function* () {
107 const base = yield* foundation;
108
109 const profileJob = yield* DataBrew.Job("DataBrewBindingsProfileJob", {
110 type: "PROFILE",
111 datasetName: base.dataset.datasetName,
112 role: base.role.roleArn,
113 outputLocation: { bucket: base.bucket.bucketName, key: "profiles/" },
114 jobSample: { mode: "CUSTOM_ROWS", size: 100 },
115 maxCapacity: 2,
116 timeout: "30 minutes",
117 });
118
119 const project = yield* DataBrew.Project("DataBrewBindingsProject", {
120 datasetName: base.dataset.datasetName,
121 recipeName: base.recipe.recipeName,
122 sample: { type: "FIRST_N", size: 100 },
123 role: base.role.roleArn,
124 });
125
126 // Job-run plane
127 const startJobRun = yield* DataBrew.StartJobRun(profileJob);
128 const stopJobRun = yield* DataBrew.StopJobRun(profileJob);
129 const describeJobRun = yield* DataBrew.DescribeJobRun(profileJob);
130 const listJobRuns = yield* DataBrew.ListJobRuns(profileJob);
131 // Recipe plane
132 const publishRecipe = yield* DataBrew.PublishRecipe(base.recipe);
133 // Interactive-session plane
134 const startProjectSession = yield* DataBrew.StartProjectSession(project);
135 const sendProjectSessionAction = yield* DataBrew.SendProjectSessionAction(project);
136
137 // Deploy-time: creates the EventBridge rule (default bus, source
138 // aws.databrew) targeting this Function. Runtime firing rides on the
139 // real job runs the suite starts; the test verifies the rule deploys.
140 yield* DataBrew.consumeJobEvents({}, (events) =>
141 Stream.runForEach(events, (event) =>
142 Effect.log(
143 `databrew event: ${event.detail.jobRunId} of ${event.detail.jobName} -> ${event.detail.state}`,
144 ),
145 ),
146 );
147
148 return {
149 fetch: Effect.gen(function* () {
150 const request = yield* HttpServerRequest;
151 const url = new URL(request.originalUrl);
152 const pathname = url.pathname;
153 const route = `${request.method} ${pathname}`;
154 const param = (name: string) => url.searchParams.get(name)!;
155
156 switch (route) {
157 // ---- job-run plane ----
158 case "POST /run/start": {
159 const result = yield* errorTagged(
160 startJobRun().pipe(Effect.retry(authorizationPolicy)),
161 );
162 return yield* HttpServerResponse.json(
163 "errorTag" in result ? result : { runId: result.RunId },
164 );
165 }
166 case "GET /run/list": {
167 const result = yield* errorTagged(listJobRuns());
168 return yield* HttpServerResponse.json(
169 "errorTag" in result
170 ? result
171 : { runIds: (result.JobRuns ?? []).map((r) => r.RunId) },
172 );
173 }
174 case "GET /run/get": {
175 const result = yield* errorTagged(describeJobRun({ RunId: param("id") }));
176 return yield* HttpServerResponse.json(
177 "errorTag" in result ? result : { state: result.State },
178 );
179 }
180 case "POST /run/stop": {
181 const body = (yield* request.json) as unknown as { id: string };
182 const result = yield* errorTagged(stopJobRun({ RunId: body.id }));
183 return yield* HttpServerResponse.json(
184 "errorTag" in result ? result : { runId: result.RunId },
185 );
186 }
187
188 // ---- recipe plane ----
189 case "POST /recipe/publish": {
190 const result = yield* errorTagged(
191 publishRecipe({
192 Description: "published by bindings fixture",
193 }).pipe(Effect.retry(authorizationPolicy)),
194 );
195 return yield* HttpServerResponse.json(
196 "errorTag" in result ? result : { name: result.Name },
197 );
198 }
199
200 // ---- interactive-session plane ----
201 case "POST /session/run": {
202 const started = yield* errorTagged(
203 startProjectSession({ AssumeControl: true }).pipe(Effect.retry(authorizationPolicy)),
204 );
205 if ("errorTag" in started) {
206 return yield* HttpServerResponse.json({ started });
207 }
208 // Session readiness and IAM propagation share one bounded retry.
209 const action = yield* errorTagged(
210 sendProjectSessionAction({
211 Preview: true,
212 // The Redacted session token flows straight back in.
213 ClientSessionId: started.ClientSessionId,
214 RecipeStep: {
215 Action: {
216 Operation: "UPPER_CASE",
217 Parameters: { sourceColumn: "name" },
218 },
219 },
220 }).pipe(
221 Effect.retry({
222 while: (e): boolean => e._tag === "ConflictException",
223 schedule: Schedule.max([Schedule.fixed("5 seconds"), Schedule.recurs(6)]),
224 }),
225 ),
226 );
227 return yield* HttpServerResponse.json({
228 started: {
229 name: started.Name,
230 hasSessionId: started.ClientSessionId !== undefined,
231 },
232 action: "errorTag" in action ? action : { actionId: action.ActionId ?? null },
233 });
234 }
235
236 default:
237 return yield* HttpServerResponse.json({ error: "Not found", route }, { status: 404 });
238 }
239 }).pipe(Effect.orDie),
240 };
241 }).pipe(
242 Effect.provide(
243 Layer.mergeAll(
244 Lambda.EventSource,
245 DataBrew.StartJobRunHttp,
246 DataBrew.StopJobRunHttp,
247 DataBrew.DescribeJobRunHttp,
248 DataBrew.ListJobRunsHttp,
249 DataBrew.PublishRecipeHttp,
250 DataBrew.StartProjectSessionHttp,
251 DataBrew.SendProjectSessionActionHttp,
252 ),
253 ),
254 ),
255 );