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
| 1 | import { Unowned } from "alchemy/AdoptPolicy"; |
| 2 | import { isResolved } from "alchemy/Diff"; |
| 3 | import * as Provider from "alchemy/Provider"; |
| 4 | import { Resource } from "alchemy/Resource"; |
| 5 | import { createInternalTags, hasAlchemyTags, stripInternalTags } from "alchemy/Tags"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Equal from "effect/Equal"; |
| 8 | import { Docker, dockerEngineContextName, dockerPhysicalName } from "./Docker.ts"; |
| 9 | import type { Providers } from "./Providers.ts"; |
| 10 | |
| 11 | export interface NetworkProps { |
| 12 | /** |
| 13 | * Docker network name. |
| 14 | * |
| 15 | * @default Generated from stack, stage, logical id, and instance id. |
| 16 | */ |
| 17 | name?: string; |
| 18 | /** Network driver. @default "bridge" */ |
| 19 | driver?: "bridge" | "host" | "none" | "overlay" | "macvlan" | (string & {}); |
| 20 | /** Enable IPv6 on the network. @default false */ |
| 21 | enableIPv6?: boolean; |
| 22 | /** Network labels. */ |
| 23 | labels?: Record<string, string>; |
| 24 | /** |
| 25 | * The engine the network is created on: a Docker context name, a |
| 26 | * `Docker.Context` resource, or a `Docker.Swarm` — overlay networks |
| 27 | * require a swarm manager, and passing the swarm orders the network after |
| 28 | * the swarm is initialized. |
| 29 | */ |
| 30 | context?: Docker.EngineRef; |
| 31 | } |
| 32 | |
| 33 | export interface Network extends Resource< |
| 34 | "Docker.Network", |
| 35 | NetworkProps, |
| 36 | { |
| 37 | /** Docker network ID. */ |
| 38 | id: string; |
| 39 | /** Docker network name. */ |
| 40 | name: string; |
| 41 | /** Network driver. */ |
| 42 | driver: string; |
| 43 | /** Whether IPv6 is enabled. */ |
| 44 | enableIPv6: boolean; |
| 45 | /** Labels reported by Docker. */ |
| 46 | labels: Record<string, string>; |
| 47 | /** Creation timestamp in milliseconds since epoch. */ |
| 48 | createdAt: number; |
| 49 | }, |
| 50 | never, |
| 51 | Providers |
| 52 | > {} |
| 53 | |
| 54 | /** |
| 55 | * A Docker network managed through the active Docker context. |
| 56 | * |
| 57 | * Existing same-name networks are treated as foreign unless the engine is |
| 58 | * explicitly allowed to adopt them with `--adopt` or `adopt(true)`. |
| 59 | * |
| 60 | * |
| 61 | * ### Creating Networks |
| 62 | * **Example:** Basic bridge network |
| 63 | * ```typescript |
| 64 | * const network = yield* Docker.Network("app-network", { |
| 65 | * name: "app-network", |
| 66 | * }); |
| 67 | * ``` |
| 68 | * |
| 69 | * ### Adoption |
| 70 | * **Example:** Adopt a pre-existing network |
| 71 | * ```typescript |
| 72 | * const network = yield* Docker.Network("app-network", { |
| 73 | * name: "shared-app-network", |
| 74 | * }).pipe(adopt(true)); |
| 75 | * ``` |
| 76 | * |
| 77 | * @resource |
| 78 | * @product Network |
| 79 | */ |
| 80 | export const Network = Resource<Network>("Docker.Network"); |
| 81 | |
| 82 | export const NetworkProvider = () => |
| 83 | Provider.effect( |
| 84 | Network, |
| 85 | Effect.gen(function* () { |
| 86 | const docker = yield* Docker; |
| 87 | |
| 88 | return Network.Provider.of({ |
| 89 | list: () => Effect.succeed([]), |
| 90 | read: Effect.fn(function* ({ id, instanceId, olds, output }) { |
| 91 | const context = dockerEngineContextName(olds?.context); |
| 92 | const name = yield* dockerPhysicalName(id, olds, instanceId); |
| 93 | const info = yield* docker.network |
| 94 | .inspect(name, context) |
| 95 | .pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined)); |
| 96 | if (!info) return undefined; |
| 97 | const attrs = toNetworkAttributes(info); |
| 98 | if (output) return attrs; |
| 99 | // Without prior state, only adopt a network that carries our branding; |
| 100 | // anything else is foreign and gated behind `--adopt`. |
| 101 | const owned = yield* hasAlchemyTags(id, info.Labels ?? undefined); |
| 102 | return owned ? attrs : Unowned(attrs); |
| 103 | }), |
| 104 | diff: Effect.fn(function* ({ id, output, instanceId, news, olds }) { |
| 105 | if (!isResolved(news) || !output) return undefined; |
| 106 | if (dockerEngineContextName(olds?.context) !== dockerEngineContextName(news?.context)) { |
| 107 | return { action: "replace", deleteFirst: true }; |
| 108 | } |
| 109 | const args = yield* makeNetworkArgs(id, news, instanceId); |
| 110 | // Auto-generated names are engine-owned: the deployed name stays |
| 111 | // authoritative even if the generator would name this id differently |
| 112 | // today. Only an explicit user-provided name can force a replace. |
| 113 | const desiredName = news?.name ?? output.name; |
| 114 | if ( |
| 115 | output.name !== desiredName || |
| 116 | output.driver !== args.driver || |
| 117 | output.enableIPv6 !== args.ipv6 || |
| 118 | // Compare only user labels; internal `alchemy::*` branding lives on |
| 119 | // the observed network but must not drive replacement. |
| 120 | !Equal.equals(stripInternalTags(output.labels), args.label ?? {}) |
| 121 | ) { |
| 122 | return { action: "replace", deleteFirst: true }; |
| 123 | } |
| 124 | }), |
| 125 | reconcile: Effect.fn(function* ({ output, id, instanceId, news }) { |
| 126 | const context = dockerEngineContextName(news?.context); |
| 127 | if (output) { |
| 128 | const refreshed = yield* docker.network.inspect(output.id, context).pipe( |
| 129 | Effect.map(toNetworkAttributes), |
| 130 | Effect.catchReason("PlatformError", "NotFound", () => Effect.undefined), |
| 131 | ); |
| 132 | if (refreshed) return refreshed; |
| 133 | } |
| 134 | const args = yield* makeNetworkArgs(id, news, instanceId); |
| 135 | const internalTags = yield* createInternalTags(id); |
| 136 | const { stdout: createdId } = yield* docker.network.create({ |
| 137 | ...args, |
| 138 | label: { ...internalTags, ...args.label }, |
| 139 | context, |
| 140 | }); |
| 141 | return toNetworkAttributes(yield* docker.network.inspect(createdId, context)); |
| 142 | }), |
| 143 | delete: Effect.fn(({ olds, output }) => |
| 144 | docker.network |
| 145 | .remove(output.id, dockerEngineContextName(olds?.context)) |
| 146 | .pipe(Effect.catchReason("PlatformError", "NotFound", () => Effect.void)), |
| 147 | ), |
| 148 | }); |
| 149 | }), |
| 150 | ); |
| 151 | |
| 152 | const makeNetworkArgs = (id: string, props: NetworkProps | undefined, instanceId: string) => |
| 153 | dockerPhysicalName(id, props, instanceId).pipe( |
| 154 | Effect.map((name): Parameters<Docker["Service"]["network"]["create"]>[0] => ({ |
| 155 | name, |
| 156 | driver: props?.driver ?? "bridge", |
| 157 | ipv6: props?.enableIPv6 ?? false, |
| 158 | label: props?.labels ?? {}, |
| 159 | })), |
| 160 | ); |
| 161 | |
| 162 | export const toNetworkAttributes = (info: Docker.Network): Network["Attributes"] => ({ |
| 163 | id: info.Id, |
| 164 | name: info.Name, |
| 165 | driver: info.Driver, |
| 166 | enableIPv6: info.EnableIPv6, |
| 167 | labels: info.Labels ?? {}, |
| 168 | createdAt: Date.parse(info.Created) || Date.now(), |
| 169 | }); |