@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 129 lines 4.65 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as Effect from "effect/Effect";
3 import { HttpServerRequest } from "effect/http/HttpServerRequest";
4 import * as HttpServerResponse from "effect/http/HttpServerResponse";
5 import * as Layer from "effect/Layer";
6 import * as Redacted from "effect/Redacted";
7 import * as Stream from "effect/Stream";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
11 const targetMain = path.resolve(import.meta.dirname, "timeout-handler.ts");
12
13 export class LambdaBindingsTestFunction extends Lambda.Function<Lambda.Function>()(
14 "LambdaBindingsTestFunction",
15 ) {}
16
17 export default LambdaBindingsTestFunction.make(
18 {
19 main,
20 functionUrl: true,
21 // Resolve distilled from `src/*.ts` (the `bun` export condition — same
22 // as vitest) so distilled source changes are test-visible in the
23 // deployed bundle without a `lib/` rebuild.
24 build: {
25 resolve: { conditionNames: ["bun", "import", "module", "default"] },
26 },
27 },
28 Effect.gen(function* () {
29 const target = yield* Lambda.Function("BindingsTarget", {
30 main: targetMain,
31 handler: "handler",
32 isExternal: true,
33 functionUrl: false,
34 // Invoke bindings follow the alias instead of `$LATEST`.
35 alias: "live",
36 });
37
38 const invokeFunction = yield* Lambda.InvokeFunction(target);
39 const invokeWithResponseStream = yield* Lambda.InvokeWithResponseStream(target);
40 const getFunction = yield* Lambda.GetFunction(target);
41 const getAccountSettings = yield* Lambda.GetAccountSettings();
42 const listFunctions = yield* Lambda.ListFunctions();
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 (pathname === "/invoke") {
51 const response = yield* invokeFunction({});
52 return yield* HttpServerResponse.json({
53 statusCode: response.StatusCode,
54 executedVersion: response.ExecutedVersion,
55 });
56 }
57
58 if (pathname === "/invoke-stream") {
59 const response = yield* invokeWithResponseStream({});
60 const events = response.EventStream
61 ? Array.from(yield* Stream.runCollect(response.EventStream))
62 : [];
63 const decoder = new TextDecoder();
64 const decodeChunk = (chunk: unknown): string => {
65 const value = Redacted.isRedacted(chunk) ? Redacted.value(chunk) : chunk;
66 if (value instanceof Uint8Array) return decoder.decode(value);
67 if (typeof value === "string") return value;
68 return "";
69 };
70 const payload = events
71 .map((event) =>
72 "PayloadChunk" in event && event.PayloadChunk
73 ? decodeChunk(event.PayloadChunk.Payload)
74 : "",
75 )
76 .join("");
77 const complete = events.some((event) => "InvokeComplete" in event);
78 return yield* HttpServerResponse.json({
79 statusCode: response.StatusCode,
80 payload,
81 complete,
82 // debug: surface the raw event shapes so assertion failures are
83 // diagnosable from the test output
84 eventShapes: events.map((event) =>
85 Object.entries(event).map(
86 ([key, value]) =>
87 `${key}:${value === undefined ? "undefined" : `[${Object.keys(value ?? {}).join(",")}]`}`,
88 ),
89 ),
90 });
91 }
92
93 if (pathname === "/get-function") {
94 const response = yield* getFunction();
95 return yield* HttpServerResponse.json({
96 functionName: response.Configuration?.FunctionName,
97 state: response.Configuration?.State,
98 });
99 }
100
101 if (pathname === "/account-settings") {
102 const response = yield* getAccountSettings();
103 return yield* HttpServerResponse.json({
104 concurrentExecutions: response.AccountLimit?.ConcurrentExecutions,
105 });
106 }
107
108 if (pathname === "/list-functions") {
109 const response = yield* listFunctions({ MaxItems: 5 });
110 return yield* HttpServerResponse.json({
111 count: response.Functions?.length ?? 0,
112 });
113 }
114
115 return yield* HttpServerResponse.json({ error: "Not found", pathname }, { status: 404 });
116 }).pipe(Effect.orDie),
117 };
118 }).pipe(
119 Effect.provide(
120 Layer.mergeAll(
121 Lambda.InvokeFunctionHttp,
122 Lambda.InvokeWithResponseStreamHttp,
123 Lambda.GetFunctionHttp,
124 Lambda.GetAccountSettingsHttp,
125 Lambda.ListFunctionsHttp,
126 ),
127 ),
128 ),
129 );