The best way to control Claude Code, Codex and more from any computer or phone

main
ts 111 lines 4.08 KB
Raw
1 import * as Config from "effect/Config";
2 import * as ConfigProvider from "effect/ConfigProvider";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 import * as Option from "effect/Option";
6 import { AlchemyContext } from "../AlchemyContext.ts";
7 import { CredentialsStoreLive } from "../Auth/Credentials.ts";
8 import { ProfileStoreLive } from "../Auth/Profile.ts";
9 import { AuthProvidersLive } from "../Auth/Registry.ts";
10 import { withProfileOverride } from "../Auth/Resolve.ts";
11 import { Stack } from "../Stack.ts";
12 import { Stage } from "../Stage.ts";
13 import { loadConfigProvider } from "../Util/ConfigProvider.ts";
14
15 /**
16 * The stack-specific half of the sidecar environment. One sidecar process
17 * serves many stacks (the test harness shares a single child across all
18 * test files), so this part travels per RPC session — as a query parameter
19 * on the session websocket — rather than being baked into the child's env
20 * at spawn time.
21 */
22 export interface SessionEnvironment {
23 alchemyContext: AlchemyContext["Service"];
24 stack: {
25 name: string;
26 stage: string;
27 };
28 }
29
30 export interface RpcServerEnvironment {
31 profile: string | undefined;
32 envFile: string | undefined;
33 /**
34 * Default session environment for sessions that do not carry their own
35 * (legacy single-stack boots). Sessions created by {@link RpcProviderProxy}
36 * always send an explicit {@link SessionEnvironment}.
37 */
38 alchemyContext?: AlchemyContext["Service"];
39 stack?: {
40 name: string;
41 stage: string;
42 };
43 }
44
45 /** Query parameter carrying the JSON {@link SessionEnvironment} on session websockets. */
46 export const SESSION_ENV_PARAM = "alchemy-session-env";
47
48 export const encodeSessionEnvironment = (env: SessionEnvironment): string => JSON.stringify(env);
49
50 export const decodeSessionEnvironment = (raw: string): SessionEnvironment =>
51 JSON.parse(raw) as SessionEnvironment;
52
53 export type RpcEnvironmentServices = Layer.Success<ReturnType<typeof layer>>;
54
55 export const layer = (
56 environment: Pick<RpcServerEnvironment, "profile" | "envFile"> & SessionEnvironment,
57 ) =>
58 Layer.mergeAll(
59 ProfileStoreLive,
60 CredentialsStoreLive,
61 AuthProvidersLive,
62 loadConfigProvider(Option.fromNullishOr(environment.envFile)).pipe(
63 Effect.map((base) => withProfileOverride(base, environment.profile)),
64 ConfigProvider.layer,
65 ),
66 Layer.succeed(AlchemyContext, environment.alchemyContext),
67 Layer.succeed(Stack, {
68 name: environment.stack.name,
69 stage: environment.stack.stage,
70 resources: {},
71 bindings: {},
72 actions: {},
73 }),
74 Layer.succeed(Stage, environment.stack.stage),
75 );
76
77 export const RPC_SERVER_ENVIRONMENT_KEY = "ALCHEMY_RPC_SERVER_ENVIRONMENT" as const;
78
79 /** The spawn-time environment the parent baked into the child's process env. */
80 export const fromProcessEnv: Effect.Effect<RpcServerEnvironment, unknown> = Config.String(
81 RPC_SERVER_ENVIRONMENT_KEY,
82 ).pipe(Config.map((raw) => JSON.parse(raw) as RpcServerEnvironment));
83
84 /**
85 * Legacy single-stack boot: the full environment (including the stack) baked
86 * into the process env at spawn time. Used by children that serve exactly one
87 * stack for their whole lifetime (e.g. the Vite dev-server child, which gets
88 * an explicit environment from `startViteChild`). RPC sidecars do NOT use
89 * this — their sessions each carry a {@link SessionEnvironment}.
90 */
91 export const fromEnv = () =>
92 Layer.unwrap(
93 fromProcessEnv.pipe(
94 Effect.flatMap((environment): Effect.Effect<ReturnType<typeof layer>, unknown, never> =>
95 environment.stack === undefined || environment.alchemyContext === undefined
96 ? Effect.die(
97 new Error(
98 `${RPC_SERVER_ENVIRONMENT_KEY} carries no stack/alchemyContext — this child requires the legacy single-stack environment`,
99 ),
100 )
101 : Effect.succeed(
102 layer({
103 profile: environment.profile,
104 envFile: environment.envFile,
105 stack: environment.stack,
106 alchemyContext: environment.alchemyContext,
107 }),
108 ),
109 ),
110 ),
111 );