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

main
ts 91 lines 3.28 KB
Raw
1 import { Query } from "@distilled.cloud/core/query";
2 import {
3 Railway,
4 type Bucket,
5 type Group,
6 type Service,
7 type ServiceInstance,
8 type VolumeInstance,
9 } from "@distilled.cloud/railway";
10 import * as Data from "effect/Data";
11 import * as Effect from "effect/Effect";
12 import * as Schedule from "effect/Schedule";
13 import * as Stream from "effect/Stream";
14
15 /**
16 * Collect every node of a connection. `select` projects one node, e.g.
17 * `(service) => ({ id: service.id, name: service.name })`.
18 */
19 const collect = <Item, Error>(connection: Query<ReadonlyArray<Item> | null, Error>) =>
20 Stream.runCollect(Query.items(connection)).pipe(Effect.map((items) => Array.from(items)));
21
22 /** Every service of a project, projected by `select`. */
23 export const projectServices = <Mapped>(
24 projectId: string,
25 select: (service: Query<Service>) => Mapped,
26 ) => collect(Railway.project({ id: projectId }).services({ first: 50 }).pipe(Query.map(select)));
27
28 /** Every bucket of a project, projected by `select`. */
29 export const projectBuckets = <Mapped>(
30 projectId: string,
31 select: (bucket: Query<Bucket>) => Mapped,
32 ) => collect(Railway.project({ id: projectId }).buckets({ first: 50 }).pipe(Query.map(select)));
33
34 /** Every group of a project, projected by `select`. */
35 export const projectGroups = <Mapped>(projectId: string, select: (group: Query<Group>) => Mapped) =>
36 collect(Railway.project({ id: projectId }).groups({ first: 50 }).pipe(Query.map(select)));
37
38 const environmentDeleted = Query.fn((id: string, projectId: string) => ({
39 deletedAt: Railway.environment({ id, projectId }).deletedAt,
40 }));
41
42 /** Volumes are read through their environment so access checks remain scoped. */
43 export const environmentVolumes = <Mapped>(
44 environmentId: string,
45 projectId: string,
46 select: (volume: Query<VolumeInstance>) => Mapped,
47 ) =>
48 Effect.gen(function* () {
49 const { deletedAt } = yield* environmentDeleted(environmentId, projectId);
50 if (deletedAt !== null) return [];
51 return yield* collect(
52 Railway.environment({ id: environmentId, projectId })
53 .volumeInstances({ first: 50 })
54 .pipe(Query.map(select)),
55 );
56 });
57
58 /** Enumerate existing service instances without probing absent service/environment pairs. */
59 export const environmentServiceInstances = <Mapped>(
60 environmentId: string,
61 projectId: string,
62 select: (instance: Query<ServiceInstance>) => Mapped,
63 ) =>
64 Effect.gen(function* () {
65 const { deletedAt } = yield* environmentDeleted(environmentId, projectId);
66 if (deletedAt !== null) return [];
67 return yield* collect(
68 Railway.environment({ id: environmentId, projectId })
69 .serviceInstances({ first: 50 })
70 .pipe(Query.map(select)),
71 );
72 });
73
74 /** A delete remains pending until its read path confirms absence. */
75 export class ResourceDeletionPending extends Data.TaggedError("Railway.ResourceDeletionPending")<{
76 resourceType: string;
77 resourceId: string;
78 }> {}
79
80 export const waitUntilDeleted = <E, R>(
81 resourceType: string,
82 resourceId: string,
83 absent: Effect.Effect<boolean, E, R>,
84 times: 4 | 8 | 10 = 8,
85 ) =>
86 absent.pipe(
87 Effect.repeat({ schedule: Schedule.spaced("1 second"), until: (gone) => gone, times }),
88 Effect.flatMap((gone) =>
89 gone ? Effect.void : Effect.fail(new ResourceDeletionPending({ resourceType, resourceId })),
90 ),
91 );