@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 149 lines 5.62 KB
Raw
1 import {
2 Credentials,
3 CredentialsFromEnv,
4 credentials,
5 MachineIdentity,
6 } from "@distilled.cloud/fly-io";
7 import * as NodeHttpClient from "@effect/platform-node/NodeHttpClient";
8 import type { RuntimeContext } from "alchemy/RuntimeContext";
9 import type * as Context from "effect/Context";
10 import * as Effect from "effect/Effect";
11 import * as FetchHttpClient from "effect/http/FetchHttpClient";
12 import * as HttpClient from "effect/http/HttpClient";
13 import * as Layer from "effect/Layer";
14 import * as Redacted from "effect/Redacted";
15 import type { App } from "./App.ts";
16 import { bindFlyApiToken } from "./Credentials.ts";
17 import type { Secret } from "./Secret.ts";
18 import type { SecretKey } from "./SecretKey.ts";
19
20 /**
21 * Shared scaffolding for the HTTP-backed Fly Secret bindings.
22 *
23 * Fly has no native Worker-style binding. This layer captures the ambient
24 * org token during stack-eval (so Actions work in-process) and `yield*`s
25 * it plus `appName` / secret `name` so RuntimeContext.set runs. Platform
26 * copies those Outputs into host env; runtime `yield*` gets them back.
27 * {@link CredentialsFromEnv} still reads `FLY_API_TOKEN` after that copy.
28 *
29 * NOT exported from `index.ts`.
30 */
31 export type AppNamed = Secret | SecretKey;
32
33 export const makeHttpSecretBinding = <Target extends AppNamed, Client>(options: {
34 makeClient: (
35 auth: SecretAuth,
36 appName: Effect.Effect<string>,
37 secretName: Effect.Effect<string>,
38 ) => Client;
39 /**
40 * PetSem encrypt/sign/decrypt/verify only work from a Machine over
41 * `/.fly/api` (implicit machine identity). Org tokens return Forbidden.
42 */
43 kms?: boolean;
44 }) =>
45 Effect.gen(function* () {
46 const context = yield* Effect.context<Credentials | HttpClient.HttpClient>();
47 const auth = options.kms === true ? makeKmsAuth(context) : makeSecretAuth(context);
48
49 return Effect.fn(function* (resource: Target) {
50 yield* bindFlyApiToken().pipe(Effect.provideContext(context));
51 const appName = yield* resource.appName;
52 const secretName = yield* resource.name;
53 return options.makeClient(auth, appName, secretName);
54 });
55 });
56
57 /**
58 * Same scaffolding as {@link makeHttpSecretBinding}, but the target is
59 * an {@link App} (no secret name). Used by ListSecrets.
60 */
61 export const makeHttpAppBinding = <Client>(options: {
62 makeClient: (auth: SecretAuth, appName: Effect.Effect<string>) => Client;
63 }) =>
64 Effect.gen(function* () {
65 const context = yield* Effect.context<Credentials | HttpClient.HttpClient>();
66
67 return Effect.fn(function* (app: App) {
68 yield* bindFlyApiToken().pipe(Effect.provideContext(context));
69 const appName = yield* app.appName;
70 return options.makeClient(makeSecretAuth(context), appName);
71 });
72 });
73
74 /**
75 * Injectable auth for the Secret HTTP client builders. Supplies an
76 * `authorize` that provides `Credentials` + `HttpClient` to a raw SDK op.
77 */
78 export interface SecretAuth {
79 authorize: <A, E>(
80 eff: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>,
81 ) => Effect.Effect<A, E, RuntimeContext>;
82 }
83
84 /** Build auth that uses ambient stack creds, or env creds inside a host. */
85 export const makeSecretAuth = (
86 ambient: Context.Context<Credentials | HttpClient.HttpClient>,
87 ): SecretAuth => ({
88 authorize: <A, E>(
89 eff: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>,
90 ): Effect.Effect<A, E, RuntimeContext> => {
91 if (globalThis.__ALCHEMY_RUNTIME__) {
92 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- TimeoutError/env-credential errors surface untyped; widening SecretAuth would ripple through every Fly secret client
93 return eff.pipe(
94 Effect.provide(Layer.mergeAll(CredentialsFromEnv, FetchHttpClient.layer)),
95 Effect.timeout("8 seconds"),
96 ) as Effect.Effect<A, E, RuntimeContext>;
97 }
98 return eff.pipe(Effect.provideContext(ambient)) as Effect.Effect<A, E, RuntimeContext>;
99 },
100 });
101
102 const FLY_MACHINE_API_SOCKET = "/.fly/api";
103
104 /** Distilled machines client over `/.fly/api`. URL host is unused. */
105 const flyMachineApiHttp: Layer.Layer<HttpClient.HttpClient> =
106 NodeHttpClient.layerNodeHttpNoAgent.pipe(
107 Layer.provide(
108 NodeHttpClient.layerAgentOptions({
109 socketPath: FLY_MACHINE_API_SOCKET,
110 keepAlive: false,
111 }),
112 ),
113 );
114
115 /**
116 * PetSem encrypt/sign/decrypt/verify from a Machine. Org API tokens are
117 * Forbidden; the machine identity is the unix socket at `/.fly/api`.
118 * {@link MachineIdentity} is the protocol signal (no Authorization,
119 * Connection: close). GetSecret in the same process keeps Bearer.
120 */
121 export const makeKmsAuth = (
122 ambient: Context.Context<Credentials | HttpClient.HttpClient>,
123 ): SecretAuth => ({
124 authorize: <A, E>(
125 eff: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>,
126 ): Effect.Effect<A, E, RuntimeContext> => {
127 if (globalThis.__ALCHEMY_RUNTIME__) {
128 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- TimeoutError surfaces untyped; widening SecretAuth would ripple through every Fly secret client
129 return Effect.scoped(
130 eff.pipe(
131 Effect.provide(
132 Layer.mergeAll(
133 Layer.succeed(MachineIdentity, true),
134 flyMachineApiHttp,
135 credentials({
136 apiKey: Redacted.make("unused"),
137 apiBaseUrl: "http://localhost/v1",
138 }),
139 ),
140 ),
141 ),
142 ).pipe(Effect.timeout("8 seconds")) as Effect.Effect<A, E, RuntimeContext>;
143 }
144 return eff.pipe(Effect.provideContext(ambient)) as Effect.Effect<A, E, RuntimeContext>;
145 },
146 });
147
148 export const unwrapSecretValue = (value: Redacted.Redacted<string> | string): string =>
149 Redacted.isRedacted(value) ? Redacted.value(value) : value;