@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 50 lines 1.86 KB
Raw
1 import { Credentials, CredentialsFromEnv } from "@distilled.cloud/fly-io";
2 import type { RuntimeContext } from "alchemy/RuntimeContext";
3 import type * as Context from "effect/Context";
4 import * as Effect from "effect/Effect";
5 import * as FetchHttpClient from "effect/http/FetchHttpClient";
6 import type * as HttpClient from "effect/http/HttpClient";
7 import * as Layer from "effect/Layer";
8 import { bindFlyApiToken } from "./Credentials.ts";
9 import type { Sprite } from "./Sprite.ts";
10
11 /**
12 * Shared scaffolding for HTTP-backed Fly Sprite bindings.
13 *
14 * Captures ambient credentials during stack-eval (so Actions work
15 * in-process) and `yield*`s the org token plus sprite `name` so
16 * RuntimeContext.set runs. Runtime calls inside a deployed host read
17 * `FLY_API_TOKEN` via {@link CredentialsFromEnv} after Platform copies
18 * `runtimeContext.env` onto the host.
19 *
20 * NOT exported from `index.ts`.
21 */
22 export const makeHttpSpriteBinding = <Client>(options: {
23 makeClient: (auth: SpriteAuth, spriteName: Effect.Effect<string>) => Client;
24 }) =>
25 Effect.gen(function* () {
26 const context = yield* Effect.context<Credentials | HttpClient.HttpClient>();
27
28 return Effect.fn(function* (sprite: Sprite) {
29 yield* bindFlyApiToken().pipe(Effect.provideContext(context));
30 const name = yield* sprite.name;
31 return options.makeClient(makeSpriteAuth(context), name);
32 });
33 });
34
35 export interface SpriteAuth {
36 authorize: <A, E>(
37 eff: Effect.Effect<A, E, Credentials | HttpClient.HttpClient>,
38 ) => Effect.Effect<A, E, RuntimeContext>;
39 }
40
41 export const makeSpriteAuth = (
42 ambient: Context.Context<Credentials | HttpClient.HttpClient>,
43 ): SpriteAuth => ({
44 authorize: (eff) => {
45 if (globalThis.__ALCHEMY_RUNTIME__) {
46 return eff.pipe(Effect.provide(Layer.mergeAll(CredentialsFromEnv, FetchHttpClient.layer)));
47 }
48 return eff.pipe(Effect.provideContext(ambient));
49 },
50 });