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

main
ts 83 lines 2.87 KB
Raw
1 import type { Url } from "@alchemy.run/redis";
2 import { UrlMissing as RedisUrlMissing } from "@alchemy.run/redis";
3 import * as Binding from "alchemy/Binding";
4 import * as Output from "alchemy/Output";
5 import * as Config from "effect/Config";
6 import * as Effect from "effect/Effect";
7 import * as Redacted from "effect/Redacted";
8 import { isRailwayHost } from "./MountVolume.ts";
9 import type { Redis } from "./Redis.ts";
10
11 export const REDIS_URL_ENV = "REDIS_URL";
12
13 /**
14 * Shared scaffolding for Railway Redis bindings.
15 *
16 * Each `{Op}Http.ts` is a thin `Layer.effect` over {@link makeRedisBinding}.
17 * Deploy-time writes `REDIS_URL` onto the host Service as a Railway
18 * reference (`${{RedisName.REDIS_URL}}`). Runtime commands use that URL
19 * internally — callers never read `Config.Redacted`.
20 *
21 * The RESP client lives in `@alchemy.run/redis`. This file only wires the
22 * Railway host binding.
23 *
24 * NOT exported from `index.ts`.
25 */
26
27 const asPlain = (value: unknown): string | undefined => {
28 if (typeof value === "string" && value.length > 0) return value;
29 if (Redacted.isRedacted(value)) return value.pipe(Redacted.value, asPlain);
30 return undefined;
31 };
32
33 const _resolveName = (redis: Redis) =>
34 Effect.gen(function* () {
35 const value = redis.name as unknown;
36 const direct = asPlain(value);
37 if (direct !== undefined) return direct;
38 if (Output.isOutput(value)) {
39 const accessor = yield* value as Output.Output<string>;
40 return asPlain(yield* accessor) ?? "";
41 }
42 if (Effect.isEffect(value)) {
43 // oxlint-disable-next-line effecttsgo/unsafe-effect-type-assertion -- resource attribute Effects resolve without requirements
44 return asPlain(yield* value as Effect.Effect<unknown>) ?? "";
45 }
46 return redis.LogicalId;
47 });
48
49 const redisUrlFromEnv = Config.Redacted(REDIS_URL_ENV).pipe(
50 Effect.map((value) => Redacted.value(value)),
51 );
52
53 export const makeRedisBinding = <Client>(options: { makeClient: (url: Url) => Client }) =>
54 Effect.succeed(
55 Effect.fn(function* (redis: Redis) {
56 if (!globalThis.__ALCHEMY_RUNTIME__) {
57 const host = yield* Binding.Host;
58 if (isRailwayHost(host)) {
59 // Resolve the physical service name at apply, not at Service
60 // init — LogicalId (`Cache`) is not a Railway variable
61 // reference. Same Output-in-env pattern as ConnectPostgres.
62 yield* host.bind`${redis}`({
63 env: {
64 [REDIS_URL_ENV]: Output.map(
65 Output.asOutput(redis.name as Output.Output<string>),
66 (name) => `\${{${name}.${REDIS_URL_ENV}}}`,
67 ),
68 },
69 });
70 }
71 }
72
73 const url = redisUrlFromEnv.pipe(
74 Effect.mapError(
75 () =>
76 new RedisUrlMissing({
77 resourceName: asPlain(redis.name) ?? redis.LogicalId,
78 }),
79 ),
80 );
81 return options.makeClient(url);
82 }),
83 );