@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, 04:45 UTC

main
ts 115 lines 4.3 KB
Raw
1 import * as Lambda from "@alchemy.run/aws/Lambda";
2 import * as AOSS from "@alchemy.run/aws/OpenSearchServerless";
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 path from "pathe";
8
9 const main = path.resolve(import.meta.dirname, "bindings-handler.ts");
10
11 export class AossBindingsFunction extends Lambda.Function<Lambda.Function>()(
12 "AossBindingsFunction",
13 ) {}
14
15 export default AossBindingsFunction.make(
16 {
17 main,
18 functionUrl: true,
19 },
20 Effect.gen(function* () {
21 // Account-level bindings only — free and instant (no collection needed).
22 // The collection-scoped index bindings are exercised by the gated
23 // IndexBindings.test.ts fixture.
24 const getAccountSettings = yield* AOSS.GetAccountSettings();
25 const updateAccountSettings = yield* AOSS.UpdateAccountSettings();
26 const getPoliciesStats = yield* AOSS.GetPoliciesStats();
27 const batchGetEffectiveLifecyclePolicy = yield* AOSS.BatchGetEffectiveLifecyclePolicy();
28
29 const bound = {
30 getAccountSettings,
31 updateAccountSettings,
32 getPoliciesStats,
33 batchGetEffectiveLifecyclePolicy,
34 };
35
36 return {
37 fetch: Effect.gen(function* () {
38 const request = yield* HttpServerRequest;
39 const url = new URL(request.originalUrl);
40 const pathname = url.pathname;
41
42 if (request.method === "GET" && pathname === "/bindings") {
43 return yield* HttpServerResponse.json({
44 bound: Object.keys(bound),
45 });
46 }
47
48 if (request.method === "GET" && pathname === "/account-settings") {
49 const response = yield* getAccountSettings();
50 return yield* HttpServerResponse.json({
51 capacityLimits: response.accountSettingsDetail?.capacityLimits ?? null,
52 });
53 }
54
55 if (request.method === "POST" && pathname === "/account-settings/noop") {
56 // Read-modify-write with the observed values (falling back to the
57 // service defaults) — proves the UpdateAccountSettings grant
58 // without changing the account's effective configuration.
59 const current = yield* getAccountSettings().pipe(
60 Effect.map((r) => r.accountSettingsDetail?.capacityLimits),
61 );
62 const response = yield* updateAccountSettings({
63 capacityLimits: {
64 maxIndexingCapacityInOCU: current?.maxIndexingCapacityInOCU ?? 10,
65 maxSearchCapacityInOCU: current?.maxSearchCapacityInOCU ?? 10,
66 },
67 });
68 return yield* HttpServerResponse.json({
69 capacityLimits: response.accountSettingsDetail?.capacityLimits ?? null,
70 });
71 }
72
73 if (request.method === "GET" && pathname === "/policies-stats") {
74 const response = yield* getPoliciesStats();
75 return yield* HttpServerResponse.json({
76 total: response.TotalPolicyCount ?? 0,
77 securityPolicies: response.SecurityPolicyStats?.EncryptionPolicyCount ?? 0,
78 });
79 }
80
81 if (request.method === "POST" && pathname === "/effective-lifecycle") {
82 // A nonexistent index resolves through the error-detail channel —
83 // the call round-tripping at all proves the grant end-to-end (an
84 // IAM gap would surface AccessDeniedException, a 500).
85 const response = yield* batchGetEffectiveLifecyclePolicy({
86 resourceIdentifiers: [
87 {
88 type: "retention",
89 resource: "index/alchemy-nonexistent-probe/probe",
90 },
91 ],
92 });
93 return yield* HttpServerResponse.json({
94 details: (response.effectiveLifecyclePolicyDetails ?? []).length,
95 errors: (response.effectiveLifecyclePolicyErrorDetails ?? []).length,
96 });
97 }
98
99 return yield* HttpServerResponse.json(
100 { error: "Not found", method: request.method, pathname },
101 { status: 404 },
102 );
103 }).pipe(Effect.orDie),
104 };
105 }).pipe(
106 Effect.provide(
107 Layer.mergeAll(
108 AOSS.GetAccountSettingsHttp,
109 AOSS.UpdateAccountSettingsHttp,
110 AOSS.GetPoliciesStatsHttp,
111 AOSS.BatchGetEffectiveLifecyclePolicyHttp,
112 ),
113 ),
114 ),
115 );