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

main
ts 180 lines 6.81 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as MediaConvert from "@alchemy.run/aws/MediaConvert";
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 MediaConvertTestFunction extends Lambda.Function<Lambda.Function>()(
14 "MediaConvertTestFunction",
15 ) {}
16
17 export default MediaConvertTestFunction.make(
18 {
19 main,
20 functionUrl: true,
21 // MediaConvert control-plane calls routinely take a few seconds; AWS's
22 // default 3s Lambda timeout is too tight under cold starts.
23 timeout: Duration.seconds(30),
24 },
25 Effect.gen(function* () {
26 const createJob = yield* MediaConvert.CreateJob();
27 const getJob = yield* MediaConvert.GetJob();
28 const cancelJob = yield* MediaConvert.CancelJob();
29 const listJobs = yield* MediaConvert.ListJobs();
30 const searchJobs = yield* MediaConvert.SearchJobs();
31 const probe = yield* MediaConvert.Probe();
32 const startJobsQuery = yield* MediaConvert.StartJobsQuery();
33 const getJobsQueryResults = yield* MediaConvert.GetJobsQueryResults();
34
35 // Deploy-time: creates the EventBridge rule (default bus, source
36 // aws.mediaconvert) targeting this Function. The test verifies the rule
37 // deploys; runtime firing would require a billable transcode.
38 yield* MediaConvert.consumeJobEvents({ statuses: ["COMPLETE", "ERROR"] }, (events) =>
39 Stream.runForEach(events, (event) =>
40 Effect.log(`mediaconvert job event: ${event.detail.jobId} -> ${event.detail.status}`),
41 ),
42 );
43
44 return {
45 fetch: Effect.gen(function* () {
46 const request = yield* HttpServerRequest;
47 const url = new URL(request.originalUrl);
48 const pathname = url.pathname;
49
50 if (request.method === "GET" && pathname === "/jobs") {
51 const result = yield* listJobs({ MaxResults: 5 });
52 return yield* HttpServerResponse.json({
53 count: result.Jobs?.length ?? 0,
54 });
55 }
56
57 if (request.method === "GET" && pathname === "/search") {
58 const result = yield* searchJobs({
59 Status: "COMPLETE",
60 MaxResults: 5,
61 });
62 return yield* HttpServerResponse.json({
63 count: result.Jobs?.length ?? 0,
64 });
65 }
66
67 if (request.method === "GET" && pathname === "/job") {
68 const id = url.searchParams.get("id")!;
69 const result = yield* getJob({ Id: id }).pipe(
70 Effect.map((r) => ({ status: r.Job?.Status, error: undefined })),
71 Effect.catch((e) => Effect.succeed({ status: undefined, error: e._tag })),
72 );
73 return yield* HttpServerResponse.json(result);
74 }
75
76 if (request.method === "POST" && pathname === "/cancel") {
77 const body = (yield* request.json) as unknown as { id: string };
78 const result = yield* cancelJob({ Id: body.id }).pipe(
79 Effect.map(() => ({ cancelled: true, error: undefined })),
80 Effect.catch((e) => Effect.succeed({ cancelled: false, error: e._tag })),
81 );
82 return yield* HttpServerResponse.json(result);
83 }
84
85 if (request.method === "POST" && pathname === "/probe") {
86 const body = (yield* request.json) as unknown as { fileUrl: string };
87 const result = yield* probe({
88 InputFiles: [{ FileUrl: body.fileUrl }],
89 }).pipe(
90 Effect.map((r) => ({
91 probed: (r.ProbeResults?.length ?? 0) > 0,
92 error: undefined,
93 })),
94 Effect.catch((e) => Effect.succeed({ probed: false, error: e._tag })),
95 );
96 return yield* HttpServerResponse.json(result);
97 }
98
99 if (request.method === "POST" && pathname === "/submit") {
100 const body = (yield* request.json) as unknown as { role: string };
101 const result = yield* createJob({
102 Role: body.role,
103 Settings: {
104 Inputs: [{ FileInput: "s3://alchemy-nonexistent/in.mp4" }],
105 OutputGroups: [
106 {
107 Name: "File Group",
108 OutputGroupSettings: {
109 Type: "FILE_GROUP_SETTINGS",
110 FileGroupSettings: {
111 Destination: "s3://alchemy-nonexistent/out/",
112 },
113 },
114 Outputs: [
115 {
116 ContainerSettings: { Container: "MP4", Mp4Settings: {} },
117 VideoDescription: {
118 CodecSettings: {
119 Codec: "H_264",
120 H264Settings: {
121 RateControlMode: "QVBR",
122 MaxBitrate: 3000000,
123 },
124 },
125 },
126 },
127 ],
128 },
129 ],
130 },
131 }).pipe(
132 Effect.map((r) => ({ jobId: r.Job?.Id, error: undefined })),
133 Effect.catch((e) => Effect.succeed({ jobId: undefined, error: e._tag })),
134 );
135 return yield* HttpServerResponse.json(result);
136 }
137
138 if (request.method === "POST" && pathname === "/jobsQuery") {
139 const result = yield* startJobsQuery({ MaxResults: 5 }).pipe(
140 Effect.map((r) => ({ queryId: r.Id, error: undefined })),
141 Effect.catch((e) => Effect.succeed({ queryId: undefined, error: e._tag })),
142 );
143 return yield* HttpServerResponse.json(result);
144 }
145
146 if (request.method === "GET" && pathname === "/jobsQueryResults") {
147 const id = url.searchParams.get("id")!;
148 const result = yield* getJobsQueryResults({ Id: id }).pipe(
149 Effect.map((r) => ({
150 status: r.Status,
151 count: r.Jobs?.length ?? 0,
152 error: undefined,
153 })),
154 Effect.catch((e) => Effect.succeed({ status: undefined, count: 0, error: e._tag })),
155 );
156 return yield* HttpServerResponse.json(result);
157 }
158
159 return yield* HttpServerResponse.json(
160 { error: "Not found", method: request.method, pathname },
161 { status: 404 },
162 );
163 }).pipe(Effect.orDie),
164 };
165 }).pipe(
166 Effect.provide(
167 Layer.mergeAll(
168 Lambda.EventSource,
169 MediaConvert.CreateJobHttp,
170 MediaConvert.GetJobHttp,
171 MediaConvert.CancelJobHttp,
172 MediaConvert.ListJobsHttp,
173 MediaConvert.SearchJobsHttp,
174 MediaConvert.ProbeHttp,
175 MediaConvert.StartJobsQueryHttp,
176 MediaConvert.GetJobsQueryResultsHttp,
177 ),
178 ),
179 ),
180 );