@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 152 lines 5.37 KB
Raw
1 import type { FlyContainerConfig, FlyMachineConfig } from "@distilled.cloud/fly-io/machines";
2 import { deepEqual } from "alchemy/Diff";
3 import * as Data from "effect/Data";
4 import * as Effect from "effect/Effect";
5 import type { MachineContainer, MachineProps } from "./Machine.ts";
6
7 export class InvalidMachineContainers extends Data.TaggedError("Fly.InvalidMachineContainers")<{
8 message: string;
9 }> {}
10
11 /** Validate resolved inputs before any Machine or Volume is changed. */
12 export const validateMachineContainers = (
13 props: Pick<MachineProps, "image" | "containers" | "init">,
14 ) => {
15 const image = "image" in props ? props.image : undefined;
16 const containers = "containers" in props ? props.containers : undefined;
17 if ((image !== undefined) === (containers !== undefined))
18 return Effect.fail(
19 new InvalidMachineContainers({
20 message: "Fly.Machine requires exactly one of image or containers.",
21 }),
22 );
23 if (image !== undefined)
24 return typeof image === "string" && image.trim().length > 0
25 ? Effect.void
26 : Effect.fail(new InvalidMachineContainers({ message: "image must be nonempty." }));
27 if (!Array.isArray(containers) || containers.length === 0)
28 return Effect.fail(
29 new InvalidMachineContainers({
30 message: "containers must be a nonempty array.",
31 }),
32 );
33 if (props.init !== undefined)
34 return Effect.fail(
35 new InvalidMachineContainers({
36 message: "init is only supported with image.",
37 }),
38 );
39 const names = new Set<string>();
40 for (const container of containers) {
41 if (
42 container == null ||
43 typeof container.name !== "string" ||
44 !container.name.trim() ||
45 typeof container.image !== "string" ||
46 !container.image.trim()
47 )
48 return Effect.fail(
49 new InvalidMachineContainers({
50 message: "Every container needs a nonempty name and image.",
51 }),
52 );
53 if ("exec" in container && container.exec !== undefined)
54 return Effect.fail(
55 new InvalidMachineContainers({
56 message: `Container ${container.name}: use cmd and entrypoint; native container exec overrides are not supported.`,
57 }),
58 );
59 if (names.has(container.name))
60 return Effect.fail(
61 new InvalidMachineContainers({
62 message: `Duplicate container name: ${container.name}.`,
63 }),
64 );
65 names.add(container.name);
66 }
67 for (const container of containers)
68 for (const dependency of container.dependsOn ?? [])
69 if (dependency == null || typeof dependency.name !== "string" || !names.has(dependency.name))
70 return Effect.fail(
71 new InvalidMachineContainers({
72 message: `Container ${container.name} depends on an undeclared container.`,
73 }),
74 );
75 return Effect.void;
76 };
77
78 export const toFlyContainers = (containers: MachineContainer[]): FlyContainerConfig[] =>
79 containers.map((container) => ({
80 name: container.name,
81 image: container.image,
82 cmd: container.cmd,
83 entrypoint: container.entrypoint,
84 env: container.env,
85 depends_on: container.dependsOn?.map(({ name, condition }) => ({
86 name,
87 condition,
88 })),
89 healthchecks: container.healthChecks?.map((check) => ({
90 name: check.name,
91 kind: check.kind,
92 interval: check.interval,
93 timeout: check.timeout,
94 grace_period: check.gracePeriod,
95 success_threshold: check.successThreshold,
96 failure_threshold: check.failureThreshold,
97 http: check.http && {
98 port: check.http.port,
99 path: check.http.path,
100 method: check.http.method,
101 scheme: check.http.scheme,
102 headers: check.http.headers,
103 tls_server_name: check.http.tlsServerName,
104 tls_skip_verify: check.http.tlsSkipVerify,
105 },
106 tcp: check.tcp,
107 exec: check.exec,
108 })),
109 }));
110
111 /** Canonical semantic config shared by drift detection and generation identity. */
112 export const canonicalContainers = (containers: FlyContainerConfig[] | undefined) =>
113 (containers ?? [])
114 .map((container) => ({
115 name: container.name,
116 image: container.image,
117 cmd: container.cmd,
118 entrypoint: container.entrypoint,
119 env: Object.fromEntries(
120 Object.entries(container.env ?? {}).filter(([, value]) => value !== undefined),
121 ),
122 depends_on: [...(container.depends_on ?? [])]
123 .map(({ name, condition }) => ({ name, condition }))
124 .sort((a, b) => (a.name ?? "").localeCompare(b.name ?? "")),
125 healthchecks: (container.healthchecks ?? []).map((check) => ({
126 name: check.name,
127 kind: check.kind,
128 interval: check.interval,
129 timeout: check.timeout,
130 grace_period: check.grace_period,
131 success_threshold: check.success_threshold,
132 failure_threshold: check.failure_threshold,
133 http: check.http,
134 tcp: check.tcp,
135 exec: check.exec,
136 })),
137 }))
138 .sort((a, b) => (a.name ?? "").localeCompare(b.name ?? ""));
139
140 export const sameContainers = (
141 observed: FlyContainerConfig[] | undefined,
142 desired: FlyContainerConfig[] | undefined,
143 ) =>
144 deepEqual(canonicalContainers(observed), canonicalContainers(desired), {
145 stripNullish: true,
146 });
147
148 /** Fly may synthesize a top-level image from the first container on readback. */
149 export const sameContainerWorkload = (
150 observed: FlyMachineConfig | undefined,
151 desired: FlyContainerConfig[],
152 ) => sameContainers(observed?.containers, desired);