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 type { MarkedText } from "../errors/translatable.ts"; |
| 2 | import { |
| 3 | resolveEnvironmentMachineKind, |
| 4 | type EnvironmentMachineKind, |
| 5 | type EnvironmentId, |
| 6 | type ServerConfig, |
| 7 | type ServerProvider, |
| 8 | } from "@t3tools/contracts"; |
| 9 | import * as Option from "effect/Option"; |
| 10 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 11 | |
| 12 | import { AVAILABLE_CONNECTION_STATE, type SupervisorConnectionState } from "../connection/model.ts"; |
| 13 | import { |
| 14 | connectionCatalogDisplayUrl, |
| 15 | presentConnectionState, |
| 16 | type EnvironmentConnectionPhase, |
| 17 | type EnvironmentConnectionPresentation, |
| 18 | type EnvironmentPresentation, |
| 19 | type EnvironmentServerConfig, |
| 20 | } from "../connection/presentation.ts"; |
| 21 | import { type EnvironmentCatalogState, enabledEnvironmentIds } from "./connections.ts"; |
| 22 | import { hasRelayRoute } from "../connection/routes.ts"; |
| 23 | |
| 24 | function mapsEqual<K, V>(left: ReadonlyMap<K, V>, right: ReadonlyMap<K, V>): boolean { |
| 25 | if (left.size !== right.size) { |
| 26 | return false; |
| 27 | } |
| 28 | for (const [key, value] of left) { |
| 29 | if (right.get(key) !== value) { |
| 30 | return false; |
| 31 | } |
| 32 | } |
| 33 | return true; |
| 34 | } |
| 35 | |
| 36 | function sameConnection( |
| 37 | left: EnvironmentConnectionPresentation, |
| 38 | right: EnvironmentConnectionPresentation, |
| 39 | ): boolean { |
| 40 | return left.phase === right.phase && left.error === right.error && left.traceId === right.traceId; |
| 41 | } |
| 42 | |
| 43 | function sameRouteIds(left: ReadonlyArray<string>, right: ReadonlyArray<string>): boolean { |
| 44 | return left.length === right.length && left.every((routeId, index) => right[index] === routeId); |
| 45 | } |
| 46 | |
| 47 | function withoutProviders( |
| 48 | config: ServerConfig | null, |
| 49 | previous: EnvironmentServerConfig | null, |
| 50 | ): EnvironmentServerConfig | null { |
| 51 | if (config === null) { |
| 52 | return null; |
| 53 | } |
| 54 | const { providers: _providers, ...next } = config; |
| 55 | if (previous === null) { |
| 56 | return next; |
| 57 | } |
| 58 | const keys = Object.keys(next) as Array<keyof EnvironmentServerConfig>; |
| 59 | return keys.length === Object.keys(previous).length && |
| 60 | keys.every((key) => key in previous && previous[key] === next[key]) |
| 61 | ? previous |
| 62 | : next; |
| 63 | } |
| 64 | |
| 65 | export interface EnvironmentPresentationWithProviders extends Omit< |
| 66 | EnvironmentPresentation, |
| 67 | "serverConfig" |
| 68 | > { |
| 69 | readonly serverConfig: ServerConfig | null; |
| 70 | } |
| 71 | |
| 72 | export function createEnvironmentPresentationAtoms<E>(input: { |
| 73 | readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>; |
| 74 | readonly stateAtom: ( |
| 75 | environmentId: EnvironmentId, |
| 76 | ) => Atom.Atom<AsyncResult.AsyncResult<SupervisorConnectionState, E>>; |
| 77 | readonly serverConfigValueAtom: (environmentId: EnvironmentId) => Atom.Atom<ServerConfig | null>; |
| 78 | }) { |
| 79 | const presentationAtom = Atom.family((environmentId: EnvironmentId) => |
| 80 | Atom.make((get) => { |
| 81 | const entry = get(input.catalogValueAtom).entries.get(environmentId); |
| 82 | if (entry === undefined) { |
| 83 | return null; |
| 84 | } |
| 85 | const state = AsyncResult.getOrElse( |
| 86 | get(input.stateAtom(environmentId)), |
| 87 | () => AVAILABLE_CONNECTION_STATE, |
| 88 | ); |
| 89 | const previous = Option.getOrNull(get.self<EnvironmentPresentation | null>()); |
| 90 | // The supervisor never publishes this placeholder; an enabled one starts out connecting. |
| 91 | const connection: EnvironmentConnectionPresentation = |
| 92 | entry.unsupportedReason !== undefined |
| 93 | ? { phase: "unsupported", error: entry.unsupportedReason, traceId: null } |
| 94 | : state === AVAILABLE_CONNECTION_STATE && entry.enabled |
| 95 | ? { phase: "connecting", error: null, traceId: null } |
| 96 | : presentConnectionState(state); |
| 97 | const next = { |
| 98 | entry, |
| 99 | connection: |
| 100 | previous !== null && sameConnection(previous.connection, connection) |
| 101 | ? previous.connection |
| 102 | : connection, |
| 103 | serverConfig: withoutProviders( |
| 104 | get(input.serverConfigValueAtom(environmentId)), |
| 105 | previous?.serverConfig ?? null, |
| 106 | ), |
| 107 | refusedRouteIds: |
| 108 | previous !== null && sameRouteIds(previous.refusedRouteIds, state.refusedRouteIds) |
| 109 | ? previous.refusedRouteIds |
| 110 | : state.refusedRouteIds, |
| 111 | } satisfies EnvironmentPresentation; |
| 112 | return previous !== null && |
| 113 | previous.entry === next.entry && |
| 114 | previous.connection === next.connection && |
| 115 | previous.serverConfig === next.serverConfig && |
| 116 | previous.refusedRouteIds === next.refusedRouteIds |
| 117 | ? previous |
| 118 | : next; |
| 119 | }).pipe(Atom.withLabel(`environment-presentation:${environmentId}`)), |
| 120 | ); |
| 121 | |
| 122 | let previous: ReadonlyMap<EnvironmentId, EnvironmentPresentation> = new Map(); |
| 123 | const presentationsAtom = Atom.make((get) => { |
| 124 | const next = new Map<EnvironmentId, EnvironmentPresentation>(); |
| 125 | for (const environmentId of get(input.catalogValueAtom).entries.keys()) { |
| 126 | const presentation = get(presentationAtom(environmentId)); |
| 127 | if (presentation !== null) { |
| 128 | next.set(environmentId, presentation); |
| 129 | } |
| 130 | } |
| 131 | if (mapsEqual(previous, next)) { |
| 132 | return previous; |
| 133 | } |
| 134 | previous = next; |
| 135 | return previous; |
| 136 | }).pipe(Atom.withLabel("environment-presentations")); |
| 137 | |
| 138 | // Enabled environments only, like the full server-configs map, but without providers, so |
| 139 | // capability checks on every sidebar row and markdown link skip provider status pushes. |
| 140 | const serverConfigsAtom = Atom.make((get) => { |
| 141 | const next = new Map<EnvironmentId, EnvironmentServerConfig>(); |
| 142 | for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) { |
| 143 | const serverConfig = get(presentationAtom(environmentId))?.serverConfig; |
| 144 | if (serverConfig != null) { |
| 145 | next.set(environmentId, serverConfig); |
| 146 | } |
| 147 | } |
| 148 | const previous = Option.getOrNull( |
| 149 | get.self<ReadonlyMap<EnvironmentId, EnvironmentServerConfig>>(), |
| 150 | ); |
| 151 | return previous !== null && mapsEqual(previous, next) ? previous : next; |
| 152 | }).pipe(Atom.withLabel("environment-server-configs-without-providers")); |
| 153 | |
| 154 | const providersByEnvironmentAtom = Atom.make((get) => { |
| 155 | const next = new Map<EnvironmentId, ReadonlyArray<ServerProvider>>(); |
| 156 | for (const environmentId of get(input.catalogValueAtom).entries.keys()) { |
| 157 | const providers = get(input.serverConfigValueAtom(environmentId))?.providers; |
| 158 | if (providers !== undefined) { |
| 159 | next.set(environmentId, providers); |
| 160 | } |
| 161 | } |
| 162 | const previous = Option.getOrNull( |
| 163 | get.self<ReadonlyMap<EnvironmentId, ReadonlyArray<ServerProvider>>>(), |
| 164 | ); |
| 165 | return previous !== null && mapsEqual(previous, next) ? previous : next; |
| 166 | }).pipe(Atom.withLabel("environment-providers")); |
| 167 | |
| 168 | // Enabled environments only, like serverConfigsAtom: a disabled connection keeps its cached |
| 169 | // config, and its accounts would otherwise count in usage limits. Usage limits ride on provider |
| 170 | // statuses, so these surfaces rebuild on every push by design. |
| 171 | const presentationsWithProvidersAtom = Atom.make((get) => { |
| 172 | const previous = Option.getOrNull( |
| 173 | get.self<ReadonlyMap<EnvironmentId, EnvironmentPresentationWithProviders>>(), |
| 174 | ); |
| 175 | const next = new Map<EnvironmentId, EnvironmentPresentationWithProviders>(); |
| 176 | for (const environmentId of enabledEnvironmentIds(get(input.catalogValueAtom))) { |
| 177 | const presentation = get(presentationAtom(environmentId)); |
| 178 | if (presentation === null) continue; |
| 179 | const serverConfig = get(input.serverConfigValueAtom(environmentId)); |
| 180 | const prior = previous?.get(environmentId); |
| 181 | next.set( |
| 182 | environmentId, |
| 183 | prior !== undefined && |
| 184 | prior.entry === presentation.entry && |
| 185 | prior.connection === presentation.connection && |
| 186 | prior.refusedRouteIds === presentation.refusedRouteIds && |
| 187 | prior.serverConfig === serverConfig |
| 188 | ? prior |
| 189 | : { ...presentation, serverConfig }, |
| 190 | ); |
| 191 | } |
| 192 | return previous !== null && mapsEqual(previous, next) ? previous : next; |
| 193 | }).pipe(Atom.withLabel("environment-presentations-with-providers")); |
| 194 | |
| 195 | return { |
| 196 | presentationAtom, |
| 197 | presentationsAtom, |
| 198 | serverConfigsAtom, |
| 199 | providersByEnvironmentAtom, |
| 200 | presentationsWithProvidersAtom, |
| 201 | }; |
| 202 | } |
| 203 | |
| 204 | export interface EnvironmentConnectionSummary { |
| 205 | readonly environmentId: EnvironmentId; |
| 206 | readonly environmentLabel: string; |
| 207 | readonly displayUrl: string; |
| 208 | readonly isRelayManaged: boolean; |
| 209 | readonly isEnabled: boolean; |
| 210 | readonly connectionState: EnvironmentConnectionPhase; |
| 211 | readonly connectionError: string | null; |
| 212 | /** `connectionError` as marked text; display sites resolve it with `resolveMarkedText`. */ |
| 213 | readonly i18nConnectionError?: MarkedText; |
| 214 | readonly connectionErrorTraceId: string | null; |
| 215 | } |
| 216 | |
| 217 | export function projectEnvironmentConnectionSummary( |
| 218 | environmentId: EnvironmentId, |
| 219 | environment: EnvironmentPresentation, |
| 220 | ): EnvironmentConnectionSummary { |
| 221 | return { |
| 222 | environmentId, |
| 223 | environmentLabel: environment.entry.target.label, |
| 224 | displayUrl: connectionCatalogDisplayUrl(environment.entry) ?? "", |
| 225 | isRelayManaged: hasRelayRoute(environment.entry), |
| 226 | isEnabled: environment.entry.enabled, |
| 227 | connectionState: environment.connection.phase, |
| 228 | connectionError: environment.connection.error, |
| 229 | ...(environment.connection.i18nError === undefined |
| 230 | ? {} |
| 231 | : { i18nConnectionError: environment.connection.i18nError }), |
| 232 | connectionErrorTraceId: environment.connection.traceId, |
| 233 | }; |
| 234 | } |
| 235 | |
| 236 | export function createEnvironmentSummaryAtoms(input: { |
| 237 | readonly catalogValueAtom: Atom.Atom<EnvironmentCatalogState>; |
| 238 | readonly presentationAtom: ( |
| 239 | environmentId: EnvironmentId, |
| 240 | ) => Atom.Atom<EnvironmentPresentation | null>; |
| 241 | }) { |
| 242 | const environmentIdsAtom = Atom.make((get) => { |
| 243 | const next = [...get(input.catalogValueAtom).entries.keys()]; |
| 244 | const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentId>>()); |
| 245 | return previous !== null && |
| 246 | previous.length === next.length && |
| 247 | next.every((id, index) => id === previous[index]) |
| 248 | ? previous |
| 249 | : next; |
| 250 | }); |
| 251 | const identitiesAtom = Atom.make((get) => { |
| 252 | const next = [...get(input.catalogValueAtom).entries].map(([environmentId, entry]) => ({ |
| 253 | environmentId, |
| 254 | label: entry.target.label, |
| 255 | })); |
| 256 | const previous = Option.getOrNull( |
| 257 | get.self<ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly label: string }>>(), |
| 258 | ); |
| 259 | return previous !== null && |
| 260 | previous.length === next.length && |
| 261 | next.every( |
| 262 | (value, index) => |
| 263 | value.environmentId === previous[index]?.environmentId && |
| 264 | value.label === previous[index]?.label, |
| 265 | ) |
| 266 | ? previous |
| 267 | : next; |
| 268 | }); |
| 269 | const environmentAtom = Atom.family((environmentId: EnvironmentId) => |
| 270 | Atom.make((get) => { |
| 271 | const presentation = get(input.presentationAtom(environmentId)); |
| 272 | if (presentation === null) return null; |
| 273 | const next = projectEnvironmentConnectionSummary(environmentId, presentation); |
| 274 | const previous = Option.getOrNull(get.self<EnvironmentConnectionSummary | null>()); |
| 275 | if ( |
| 276 | previous !== null && |
| 277 | previous.environmentId === next.environmentId && |
| 278 | previous.environmentLabel === next.environmentLabel && |
| 279 | previous.displayUrl === next.displayUrl && |
| 280 | previous.isRelayManaged === next.isRelayManaged && |
| 281 | previous.isEnabled === next.isEnabled && |
| 282 | previous.connectionState === next.connectionState && |
| 283 | previous.connectionError === next.connectionError && |
| 284 | previous.connectionErrorTraceId === next.connectionErrorTraceId |
| 285 | ) |
| 286 | return previous; |
| 287 | return next; |
| 288 | }), |
| 289 | ); |
| 290 | const environmentsAtom = Atom.make((get) => { |
| 291 | const next: Array<EnvironmentConnectionSummary> = []; |
| 292 | for (const environmentId of get(input.catalogValueAtom).entries.keys()) { |
| 293 | const environment = get(environmentAtom(environmentId)); |
| 294 | if (environment !== null) next.push(environment); |
| 295 | } |
| 296 | const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentConnectionSummary>>()); |
| 297 | return previous !== null && |
| 298 | previous.length === next.length && |
| 299 | next.every((value, index) => value === previous[index]) |
| 300 | ? previous |
| 301 | : next; |
| 302 | }).pipe(Atom.withLabel("environment-connection-summaries")); |
| 303 | const connectedEnvironmentIdsAtom = Atom.make((get) => { |
| 304 | const next = get(environmentIdsAtom).filter( |
| 305 | (id) => get(environmentAtom(id))?.connectionState === "connected", |
| 306 | ); |
| 307 | const previous = Option.getOrNull(get.self<ReadonlyArray<EnvironmentId>>()); |
| 308 | return previous !== null && |
| 309 | previous.length === next.length && |
| 310 | next.every((id, index) => id === previous[index]) |
| 311 | ? previous |
| 312 | : next; |
| 313 | }); |
| 314 | const machineByIdAtom = Atom.make((get) => { |
| 315 | const next = new Map( |
| 316 | get(environmentIdsAtom).map( |
| 317 | (environmentId) => |
| 318 | [ |
| 319 | environmentId, |
| 320 | resolveEnvironmentMachineKind( |
| 321 | get(input.presentationAtom(environmentId))?.serverConfig ?? null, |
| 322 | ), |
| 323 | ] as const, |
| 324 | ), |
| 325 | ); |
| 326 | const previous = Option.getOrNull( |
| 327 | get.self<ReadonlyMap<EnvironmentId, EnvironmentMachineKind>>(), |
| 328 | ); |
| 329 | return previous !== null && mapsEqual(previous, next) ? previous : next; |
| 330 | }); |
| 331 | const pullRequestsSupportedAtom = Atom.make((get) => |
| 332 | get(environmentIdsAtom).some( |
| 333 | (environmentId) => |
| 334 | (get(input.presentationAtom(environmentId))?.serverConfig ?? null) !== null, |
| 335 | ), |
| 336 | ); |
| 337 | const issuesSupportedAtom = Atom.make((get) => |
| 338 | get(environmentIdsAtom).some( |
| 339 | (environmentId) => |
| 340 | get(input.presentationAtom(environmentId))?.serverConfig?.environment.capabilities |
| 341 | .issues === true, |
| 342 | ), |
| 343 | ); |
| 344 | return { |
| 345 | environmentIdsAtom, |
| 346 | connectedEnvironmentIdsAtom, |
| 347 | identitiesAtom, |
| 348 | environmentsAtom, |
| 349 | machineByIdAtom, |
| 350 | pullRequestsSupportedAtom, |
| 351 | issuesSupportedAtom, |
| 352 | }; |
| 353 | } |