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

main
ts 119 lines 4.04 KB
Raw
1 import { Query } from "@distilled.cloud/core/query";
2 import type { Config } from "@distilled.cloud/railway";
3 import { Credentials, GraphQLLive, Railway } from "@distilled.cloud/railway";
4 import * as Context from "effect/Context";
5 import * as Data from "effect/Data";
6 import * as Effect from "effect/Effect";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as Layer from "effect/Layer";
9
10 export class RailwayWorkspaceNotFound extends Data.TaggedError("Railway.WorkspaceNotFound")<{
11 message: string;
12 }> {}
13
14 /**
15 * Default Railway workspace for the current token
16 * (`me.workspace ?? me.workspaces[0]`). Not a resource — the Environment
17 * *resource* is a project-scoped deploy environment.
18 */
19 export type RailwayWorkspace = { readonly id: string; readonly name: string };
20
21 /**
22 * Fully-resolved Railway environment for a stack.
23 *
24 * `{ token, tokenKind, apiBaseUrl }` comes from distilled `Credentials`.
25 * `workspaceId` is the current token's default workspace
26 * (`me.workspace ?? me.workspaces[0]`), cached for the process. Resolve
27 * it inside lifecycle operations with `RailwayEnvironment.current`.
28 */
29 export type RailwayEnvironmentShape = Config & { readonly workspaceId: string };
30
31 export class RailwayEnvironment extends Context.Service<
32 RailwayEnvironment,
33 Effect.Effect<RailwayEnvironmentShape>
34 >()("Railway::Environment") {
35 static current = RailwayEnvironment.use((env) => env);
36 readonly kind = "Environment" as const;
37 }
38
39 /**
40 * Discover the current token's default workspace.
41 *
42 * Personal account tokens answer `me.workspace ?? me.workspaces[0]`.
43 * Workspace/team tokens reject `me` with {@link RailwayForbidden}
44 * (`Not Authorized`); fall back to `apiToken.workspaces[0]`.
45 */
46 const meWorkspaces = Query.fn(() => {
47 const me = Railway.me();
48 return {
49 workspace: { id: me.workspace.id, name: me.workspace.name },
50 workspaces: me.workspaces.pipe(
51 Query.map((workspace) => ({ id: workspace.id, name: workspace.name })),
52 ),
53 };
54 });
55
56 const tokenWorkspaces = Query.fn(() =>
57 Railway.apiToken().workspaces.pipe(
58 Query.map((workspace) => ({ id: workspace.id, name: workspace.name })),
59 ),
60 );
61
62 export const resolveWorkspace = Effect.fn(function* () {
63 const fromMe = yield* meWorkspaces().pipe(
64 Effect.map((me) => (me.workspace.id !== null ? me.workspace : me.workspaces[0])),
65 Effect.catchTag(["RailwayForbidden", "RailwayUnauthenticated"], () => Effect.undefined),
66 );
67 if (fromMe?.id != null && fromMe.id.length > 0) {
68 return { id: fromMe.id, name: fromMe.name ?? "" } satisfies RailwayWorkspace;
69 }
70
71 const workspace = (yield* tokenWorkspaces())[0];
72 if (workspace === undefined || workspace.id.length === 0) {
73 return yield* new RailwayWorkspaceNotFound({
74 message:
75 "Railway current token did not include a workspace. Check RAILWAY_API_TOKEN is an account or workspace token.",
76 });
77 }
78 return { id: workspace.id, name: workspace.name } satisfies RailwayWorkspace;
79 });
80
81 /**
82 * Current token workspace id (`me.workspace ?? me.workspaces[0]`).
83 */
84 export const resolveWorkspaceId = Effect.fn(function* () {
85 const workspace = yield* resolveWorkspace();
86 return workspace.id;
87 });
88
89 /**
90 * Build a `RailwayEnvironment` layer from the distilled `Credentials`
91 * service. Provide this after `Credentials.fromAuthProvider()`.
92 *
93 * `workspaceId` is resolved from `me` and cached so Project.list /
94 * Catalog do not re-hit `me` on every call.
95 */
96 export const fromCredentials = () =>
97 Layer.effect(
98 RailwayEnvironment,
99 Effect.gen(function* () {
100 const creds = yield* Credentials;
101 const http = yield* HttpClient.HttpClient;
102 return yield* creds.pipe(
103 Effect.flatMap((resolved) =>
104 resolveWorkspaceId().pipe(
105 Effect.provide(
106 Layer.mergeAll(
107 GraphQLLive,
108 Layer.succeed(Credentials, creds),
109 Layer.succeed(HttpClient.HttpClient, http),
110 ),
111 ),
112 Effect.map((workspaceId) => ({ ...resolved, workspaceId })),
113 ),
114 ),
115 Effect.orDie,
116 Effect.cached,
117 );
118 }),
119 );