The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { Query } from "@distilled.cloud/core/query"; |
| 2 | import { |
| 3 | CredentialsFromToken, |
| 4 | GraphQLLive, |
| 5 | Railway, |
| 6 | type GqlTransport, |
| 7 | } from "@distilled.cloud/railway"; |
| 8 | import * as Effect from "effect/Effect"; |
| 9 | import * as FetchHttpClient from "effect/http/FetchHttpClient"; |
| 10 | import * as Layer from "effect/Layer"; |
| 11 | import * as Redacted from "effect/Redacted"; |
| 12 | import * as Schedule from "effect/Schedule"; |
| 13 | |
| 14 | /** Dashboard host used by `railway login --browserless` pairing URLs. */ |
| 15 | export const RAILWAY_CLI_LOGIN_HOST = "https://railway.com"; |
| 16 | |
| 17 | /** |
| 18 | * Build the Railway CLI pairing URL for a `loginSessionCreate` |
| 19 | * code. |
| 20 | * |
| 21 | * Mirrors `railway login --browserless`: the payload is |
| 22 | * `wordCode={code}&hostname={hostname}` (URL-safe base64) on |
| 23 | * `https://railway.com/cli-login?d=…`. The user confirms the pairing code |
| 24 | * in the browser; `loginSessionAuth` is the dashboard-side |
| 25 | * mutation that marks the session authorized. `loginSessionVerify` |
| 26 | * is a liveness check (true while the pairing session exists). The CLI then |
| 27 | * polls `loginSessionConsume` until a token is returned. |
| 28 | * |
| 29 | * Alchemy's AuthProvider `method: "oauth"` runs this flow: create → print |
| 30 | * the pairing URL → poll consume → store the token. |
| 31 | * |
| 32 | * ### Pairing URL |
| 33 | * **Example:** From a session code |
| 34 | * ```typescript |
| 35 | * const code = yield* createLoginSession(); |
| 36 | * const url = loginSessionUrl(code, { hostname: "dev-box" }); |
| 37 | * ``` |
| 38 | */ |
| 39 | export const loginSessionUrl = ( |
| 40 | code: string, |
| 41 | options?: { hostname?: string; host?: string }, |
| 42 | ): string => { |
| 43 | const hostname = options?.hostname ?? "alchemy"; |
| 44 | const host = (options?.host ?? RAILWAY_CLI_LOGIN_HOST).replace(/\/+$/, ""); |
| 45 | const payload = `wordCode=${code}&hostname=${hostname}`; |
| 46 | const encoded = Buffer.from(payload).toString("base64").replace(/\+/g, "-").replace(/\//g, "_"); |
| 47 | return `${host}/cli-login?d=${encoded}`; |
| 48 | }; |
| 49 | |
| 50 | /** |
| 51 | * Credentials layer that sends no `Authorization` header. Login-session |
| 52 | * mutations are public: they run before the user has a token. |
| 53 | */ |
| 54 | const anonymousRailwayCredentials = (apiBaseUrl?: string) => |
| 55 | CredentialsFromToken({ token: Redacted.make(""), tokenKind: "account", apiBaseUrl }); |
| 56 | |
| 57 | const anonymousRailway = (apiBaseUrl?: string) => |
| 58 | Layer.mergeAll(GraphQLLive, anonymousRailwayCredentials(apiBaseUrl), FetchHttpClient.layer); |
| 59 | |
| 60 | export const provideAnonymousRailway = <A, E>( |
| 61 | effect: Effect.Effect<A, E, GqlTransport>, |
| 62 | apiBaseUrl?: string, |
| 63 | ): Effect.Effect<A, E> => effect.pipe(Effect.provide(anonymousRailway(apiBaseUrl))); |
| 64 | |
| 65 | const LOGIN_POLL_TIMES = 300; |
| 66 | |
| 67 | export const createLoginSession = Query.fn(() => Railway.loginSessionCreate()); |
| 68 | |
| 69 | export const cancelLoginSession = Query.fn((code: string) => Railway.loginSessionCancel({ code })); |
| 70 | |
| 71 | const verifyLoginSession = Query.fn((code: string) => Railway.loginSessionVerify({ code })); |
| 72 | |
| 73 | const consumeLoginSession = Query.fn((code: string) => Railway.loginSessionConsume({ code })); |
| 74 | |
| 75 | /** |
| 76 | * Poll `loginSessionVerify` then |
| 77 | * `loginSessionConsume` until a token is returned, or 5 minutes |
| 78 | * elapse. Mirrors `railway login --browserless`: consume is the token source; |
| 79 | * verify is a liveness check. Does not cancel the session — the caller should |
| 80 | * cancel on timeout or interrupt. |
| 81 | * |
| 82 | * Exhaustion returns `undefined` (not a failure) so the AuthProvider can |
| 83 | * surface a timeout rather than a poll error. |
| 84 | */ |
| 85 | export const pollLoginSessionToken = (code: string) => |
| 86 | verifyLoginSession(code).pipe( |
| 87 | Effect.catchTag("RailwayNotFound", () => Effect.succeed(false)), |
| 88 | Effect.flatMap(() => |
| 89 | consumeLoginSession(code).pipe( |
| 90 | Effect.catchTag("RailwayNotFound", () => Effect.succeed(null)), |
| 91 | ), |
| 92 | ), |
| 93 | Effect.map((token) => (token != null && Redacted.value(token).length > 0 ? token : undefined)), |
| 94 | Effect.repeat({ |
| 95 | schedule: Schedule.spaced("1 second"), |
| 96 | while: (token) => token == null, |
| 97 | times: LOGIN_POLL_TIMES, |
| 98 | }), |
| 99 | ); |