@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 223 lines 8.66 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as RePostSpace from "@alchemy.run/aws/RePostSpace";
3 import * as Effect from "effect/Effect";
4 import { HttpServerRequest } from "effect/http/HttpServerRequest";
5 import * as HttpServerResponse from "effect/http/HttpServerResponse";
6 import * as Layer from "effect/Layer";
7 import * as Redacted from "effect/Redacted";
8 import path from "pathe";
9
10 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
11
12 // A syntactically-valid but nonexistent IAM Identity Center accessor id.
13 const BOGUS_ACCESSOR_ID = "00000000-0000-0000-0000-000000000000";
14
15 /** Unwrap a distilled sensitive value into its plain string. */
16 const unwrapSensitive = (
17 value: string | Redacted.Redacted<string> | undefined,
18 ): string | undefined =>
19 value === undefined ? undefined : Redacted.isRedacted(value) ? Redacted.value(value) : value;
20
21 export class RePostSpaceBindingsFunction extends Lambda.Function<Lambda.Function>()(
22 "RePostSpaceBindingsFunction",
23 ) {}
24
25 export default RePostSpaceBindingsFunction.make(
26 {
27 main,
28 functionUrl: true,
29 },
30 Effect.gen(function* () {
31 const space = yield* RePostSpace.Space("BindingsSpace", {
32 // subdomain must be globally unique across re:Post Private —
33 // deterministic, alchemy-branded constant.
34 subdomain: "alchemy-e2e-repost-bindings",
35 tier: "BASIC",
36 description: "alchemy repostspace bindings fixture",
37 tags: { fixture: "repostspace-bindings" },
38 });
39
40 const sendInvites = yield* RePostSpace.SendInvites(space);
41 const registerAdmin = yield* RePostSpace.RegisterAdmin(space);
42 const deregisterAdmin = yield* RePostSpace.DeregisterAdmin(space);
43 const batchAddRole = yield* RePostSpace.BatchAddRole(space);
44 const batchRemoveRole = yield* RePostSpace.BatchRemoveRole(space);
45 const createChannel = yield* RePostSpace.CreateChannel(space);
46 const getChannel = yield* RePostSpace.GetChannel(space);
47 const listChannels = yield* RePostSpace.ListChannels(space);
48 const updateChannel = yield* RePostSpace.UpdateChannel(space);
49 const addChannelRole = yield* RePostSpace.BatchAddChannelRoleToAccessors(space);
50 const removeChannelRole = yield* RePostSpace.BatchRemoveChannelRoleFromAccessors(space);
51
52 const bound = {
53 sendInvites,
54 registerAdmin,
55 deregisterAdmin,
56 batchAddRole,
57 batchRemoveRole,
58 createChannel,
59 getChannel,
60 listChannels,
61 updateChannel,
62 addChannelRole,
63 removeChannelRole,
64 };
65
66 return {
67 fetch: Effect.gen(function* () {
68 const request = yield* HttpServerRequest;
69 const url = new URL(request.originalUrl);
70 const pathname = url.pathname;
71
72 if (request.method === "GET" && pathname === "/bindings") {
73 return yield* HttpServerResponse.json({
74 bound: Object.keys(bound),
75 });
76 }
77
78 if (request.method === "GET" && pathname === "/channels") {
79 const response = yield* listChannels();
80 return yield* HttpServerResponse.json({
81 count: (response.channels ?? []).length,
82 });
83 }
84
85 if (request.method === "POST" && pathname === "/channels") {
86 // Create → read → rename round-trip; channels cannot be deleted,
87 // but the whole space is destroyed at teardown so nothing leaks.
88 const { channelId } = yield* createChannel({
89 channelName: "alchemy-bindings-channel",
90 channelDescription: "created by the alchemy bindings fixture",
91 });
92 const read = yield* getChannel({ channelId });
93 yield* updateChannel({
94 channelId,
95 channelName: "alchemy-bindings-channel-renamed",
96 });
97 const reread = yield* getChannel({ channelId });
98 return yield* HttpServerResponse.json({
99 channelId,
100 name: unwrapSensitive(read.channelName),
101 renamed: unwrapSensitive(reread.channelName),
102 });
103 }
104
105 if (request.method === "POST" && pathname === "/roles/bogus") {
106 // Batch role APIs report per-accessor failures in the output's
107 // `errors` list (or fail with a typed 400 for malformed input) —
108 // either shape proves the grant + spaceId injection end-to-end.
109 const added = yield* batchAddRole({
110 accessorIds: [BOGUS_ACCESSOR_ID],
111 role: "EXPERT",
112 }).pipe(
113 Effect.map((r) => ({
114 ok: true,
115 errors: r.errors.length,
116 added: r.addedAccessorIds.length,
117 })),
118 Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) =>
119 Effect.succeed({ ok: false, tag: e._tag }),
120 ),
121 );
122 const removed = yield* batchRemoveRole({
123 accessorIds: [BOGUS_ACCESSOR_ID],
124 role: "EXPERT",
125 }).pipe(
126 Effect.map((r) => ({
127 ok: true,
128 errors: r.errors.length,
129 removed: r.removedAccessorIds.length,
130 })),
131 Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) =>
132 Effect.succeed({ ok: false, tag: e._tag }),
133 ),
134 );
135 return yield* HttpServerResponse.json({ added, removed });
136 }
137
138 if (request.method === "POST" && pathname === "/channel-roles/bogus") {
139 const channelId = url.searchParams.get("channelId") ?? "";
140 const added = yield* addChannelRole({
141 channelId,
142 accessorIds: [BOGUS_ACCESSOR_ID],
143 channelRole: "EXPERT",
144 }).pipe(
145 Effect.map((r) => ({ ok: true, errors: r.errors.length })),
146 Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) =>
147 Effect.succeed({ ok: false, tag: e._tag }),
148 ),
149 );
150 const removed = yield* removeChannelRole({
151 channelId,
152 accessorIds: [BOGUS_ACCESSOR_ID],
153 channelRole: "EXPERT",
154 }).pipe(
155 Effect.map((r) => ({ ok: true, errors: r.errors.length })),
156 Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) =>
157 Effect.succeed({ ok: false, tag: e._tag }),
158 ),
159 );
160 return yield* HttpServerResponse.json({ added, removed });
161 }
162
163 if (request.method === "POST" && pathname === "/admins/bogus") {
164 // A nonexistent accessor id surfaces the typed 400/404 union —
165 // an IAM gap would surface AccessDeniedException instead, so the
166 // typed tag proves the grant reached the space-scoped API.
167 const register = yield* registerAdmin({
168 adminId: BOGUS_ACCESSOR_ID,
169 }).pipe(
170 Effect.map(() => ({ ok: true as const })),
171 Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) =>
172 Effect.succeed({ ok: false as const, tag: e._tag }),
173 ),
174 );
175 const deregister = yield* deregisterAdmin({
176 adminId: BOGUS_ACCESSOR_ID,
177 }).pipe(
178 Effect.map(() => ({ ok: true as const })),
179 Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) =>
180 Effect.succeed({ ok: false as const, tag: e._tag }),
181 ),
182 );
183 return yield* HttpServerResponse.json({ register, deregister });
184 }
185
186 if (request.method === "POST" && pathname === "/invites/bogus") {
187 const result = yield* sendInvites({
188 accessorIds: [BOGUS_ACCESSOR_ID],
189 title: "alchemy bindings probe",
190 body: "This invite targets a nonexistent accessor on purpose.",
191 }).pipe(
192 Effect.map(() => ({ ok: true as const })),
193 Effect.catchTag(["ValidationException", "ResourceNotFoundException"], (e) =>
194 Effect.succeed({ ok: false as const, tag: e._tag }),
195 ),
196 );
197 return yield* HttpServerResponse.json(result);
198 }
199
200 return yield* HttpServerResponse.json(
201 { error: "Not found", method: request.method, pathname },
202 { status: 404 },
203 );
204 }).pipe(Effect.orDie),
205 };
206 }).pipe(
207 Effect.provide(
208 Layer.mergeAll(
209 RePostSpace.SendInvitesHttp,
210 RePostSpace.RegisterAdminHttp,
211 RePostSpace.DeregisterAdminHttp,
212 RePostSpace.BatchAddRoleHttp,
213 RePostSpace.BatchRemoveRoleHttp,
214 RePostSpace.CreateChannelHttp,
215 RePostSpace.GetChannelHttp,
216 RePostSpace.ListChannelsHttp,
217 RePostSpace.UpdateChannelHttp,
218 RePostSpace.BatchAddChannelRoleToAccessorsHttp,
219 RePostSpace.BatchRemoveChannelRoleFromAccessorsHttp,
220 ),
221 ),
222 ),
223 );