@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 143 lines 5.49 KB
Raw
1 import * as appconfigdata from "@distilled.cloud/aws/appconfigdata";
2 import * as Output from "alchemy/Output";
3 import { toWireSeconds } from "alchemy/Util/Duration";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Ref from "effect/Ref";
7 import * as Stream from "effect/Stream";
8 import * as AWSBindings from "../AWSBindings.ts";
9 import { AWSEnvironment } from "../Environment.ts";
10 import type { Application } from "./Application.ts";
11 import type { ConfigurationProfile } from "./ConfigurationProfile.ts";
12 import type { Environment } from "./Environment.ts";
13 import { GetConfiguration, type GetConfigurationOptions } from "./GetConfiguration.ts";
14
15 interface SessionCache {
16 token: string | undefined;
17 lastContent: string | undefined;
18 lastContentType: string | undefined;
19 lastVersionLabel: string | undefined;
20 }
21
22 /**
23 * HTTP implementation of the {@link GetConfiguration} binding. Calls the
24 * AppConfig data plane (`StartConfigurationSession` +
25 * `GetLatestConfiguration`) with the Lambda's IAM role, caching the poll
26 * token and last-seen content across calls.
27 *
28 * Provide it on the hosting Lambda function's Effect so the binding is
29 * available at runtime:
30 *
31 * @example
32 * ```typescript
33 * export default MyFunction.make(
34 * { main: import.meta.url },
35 * Effect.gen(function* () {
36 * const getConfig = yield* AppConfig.GetConfiguration(app, env, profile);
37 * return {
38 * fetch: Effect.gen(function* () {
39 * const { content } = yield* getConfig().pipe(Effect.orDie);
40 * return HttpServerResponse.text(content ?? "");
41 * }),
42 * };
43 * }).pipe(Effect.provide(AppConfig.GetConfigurationHttp)),
44 * );
45 * ```
46 */
47 export const GetConfigurationHttp = Layer.effect(
48 GetConfiguration,
49 Effect.gen(function* () {
50 const bind = yield* AWSBindings.Bind;
51 const startSession = yield* AWSBindings.withCredentials(
52 appconfigdata.startConfigurationSession,
53 );
54 const getLatest = yield* AWSBindings.withCredentials(appconfigdata.getLatestConfiguration);
55
56 return Effect.fn(function* (
57 application: Application,
58 environment: Environment,
59 configurationProfile: ConfigurationProfile,
60 options?: GetConfigurationOptions,
61 ) {
62 // Outputs yield DEFERRED effects — resolve again per invocation below.
63 const ApplicationId = yield* application.applicationId;
64 const EnvironmentId = yield* environment.environmentId;
65 const ConfigurationProfileId = yield* configurationProfile.configurationProfileId;
66
67 if (!globalThis.__ALCHEMY_RUNTIME__) {
68 const { accountId, region } = yield* AWSEnvironment.current as unknown as Effect.Effect<{
69 accountId: string;
70 region: string;
71 }>;
72 yield* bind`AWS.AppConfig.GetConfiguration(${application}, ${environment}, ${configurationProfile})`(
73 {
74 policyStatements: [
75 {
76 Effect: "Allow",
77 Action: ["appconfig:StartConfigurationSession", "appconfig:GetLatestConfiguration"],
78 Resource: [
79 Output.interpolate`arn:aws:appconfig:${region}:${accountId}:application/${application.applicationId}/environment/${environment.environmentId}/configuration/${configurationProfile.configurationProfileId}`,
80 ],
81 },
82 ],
83 },
84 );
85 }
86
87 const cache = yield* Ref.make<SessionCache>({
88 token: undefined,
89 lastContent: undefined,
90 lastContentType: undefined,
91 lastVersionLabel: undefined,
92 });
93
94 return Effect.fn(
95 `AWS.AppConfig.GetConfiguration(${application.LogicalId}, ${environment.LogicalId}, ${configurationProfile.LogicalId})`,
96 )(function* () {
97 const applicationId = yield* ApplicationId;
98 const environmentId = yield* EnvironmentId;
99 const configurationProfileId = yield* ConfigurationProfileId;
100
101 let state = yield* Ref.get(cache);
102 if (state.token === undefined) {
103 const session = yield* startSession({
104 ApplicationIdentifier: applicationId,
105 EnvironmentIdentifier: environmentId,
106 ConfigurationProfileIdentifier: configurationProfileId,
107 RequiredMinimumPollIntervalInSeconds: toWireSeconds(
108 options?.requiredMinimumPollInterval,
109 ),
110 });
111 state = { ...state, token: session.InitialConfigurationToken };
112 }
113
114 const response = yield* getLatest({
115 ConfigurationToken: state.token!,
116 });
117
118 // Each token is single-use; the response hands back the next one.
119 const nextToken = response.NextPollConfigurationToken ?? state.token;
120
121 // Decoding the (already-buffered) config body only fails on a
122 // corrupt payload — a defect, not part of the operation's typed union.
123 const fetched = response.Configuration
124 ? yield* response.Configuration.pipe(Stream.decodeText, Stream.mkString, Effect.orDie)
125 : "";
126
127 // An empty body means "unchanged" — keep the last-seen content.
128 const content = fetched.length > 0 ? fetched : state.lastContent;
129 const contentType = response.ContentType ?? state.lastContentType;
130 const versionLabel = response.VersionLabel ?? state.lastVersionLabel;
131
132 yield* Ref.set(cache, {
133 token: nextToken,
134 lastContent: content,
135 lastContentType: contentType,
136 lastVersionLabel: versionLabel,
137 });
138
139 return { content, contentType, versionLabel };
140 });
141 });
142 }),
143 );