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 { |
| 2 | EMPTY_SHELL_STATE, |
| 3 | type EnvironmentShellState, |
| 4 | type EnvironmentShellSummary, |
| 5 | } from "@t3tools/client-runtime/state/shell"; |
| 6 | import type { MarkedText } from "@t3tools/client-runtime/errors"; |
| 7 | import type { EnvironmentId, ServerConfig } from "@t3tools/contracts"; |
| 8 | |
| 9 | import { |
| 10 | type EnvironmentConnectionPhase, |
| 11 | type NetworkStatus, |
| 12 | } from "@t3tools/client-runtime/connection"; |
| 13 | |
| 14 | import type { EnvironmentConnectionSummary as WorkspaceEnvironment } from "@t3tools/client-runtime/state/presentation"; |
| 15 | |
| 16 | export type { EnvironmentConnectionSummary as WorkspaceEnvironment } from "@t3tools/client-runtime/state/presentation"; |
| 17 | |
| 18 | export interface WorkspaceConnectionState { |
| 19 | readonly isLoadingConnections: boolean; |
| 20 | readonly hasConnections: boolean; |
| 21 | readonly hasReadyEnvironment: boolean; |
| 22 | readonly hasConnectingEnvironment: boolean; |
| 23 | readonly connectingEnvironments: ReadonlyArray<WorkspaceEnvironment>; |
| 24 | readonly connectionState: EnvironmentConnectionPhase; |
| 25 | readonly connectionError: string | null; |
| 26 | readonly i18nConnectionError?: MarkedText; |
| 27 | readonly networkStatus: NetworkStatus; |
| 28 | } |
| 29 | |
| 30 | export interface WorkspaceState extends WorkspaceConnectionState { |
| 31 | readonly hasLoadedShellSnapshot: boolean; |
| 32 | readonly hasPendingShellSnapshot: boolean; |
| 33 | readonly hasSyncingConnectedEnvironment: boolean; |
| 34 | readonly shellSnapshotError: string | null; |
| 35 | } |
| 36 | |
| 37 | function overallConnectionState( |
| 38 | environments: ReadonlyArray<WorkspaceEnvironment>, |
| 39 | networkStatus: NetworkStatus, |
| 40 | ): EnvironmentConnectionPhase { |
| 41 | if (environments.length === 0) { |
| 42 | return "available"; |
| 43 | } |
| 44 | if (networkStatus === "offline") { |
| 45 | return "offline"; |
| 46 | } |
| 47 | if (environments.some((environment) => environment.connectionState === "connected")) { |
| 48 | return "connected"; |
| 49 | } |
| 50 | if (environments.some((environment) => environment.connectionState === "reconnecting")) { |
| 51 | return "reconnecting"; |
| 52 | } |
| 53 | if (environments.some((environment) => environment.connectionState === "connecting")) { |
| 54 | return "connecting"; |
| 55 | } |
| 56 | if (environments.some((environment) => environment.connectionState === "unsupported")) { |
| 57 | return "unsupported"; |
| 58 | } |
| 59 | if (environments.some((environment) => environment.connectionState === "error")) { |
| 60 | return "error"; |
| 61 | } |
| 62 | if (environments.some((environment) => environment.connectionState === "offline")) { |
| 63 | return "offline"; |
| 64 | } |
| 65 | return "available"; |
| 66 | } |
| 67 | |
| 68 | export function projectWorkspaceConnectionState(input: { |
| 69 | readonly isReady: boolean; |
| 70 | readonly networkStatus: NetworkStatus; |
| 71 | readonly environments: ReadonlyArray<WorkspaceEnvironment>; |
| 72 | }): WorkspaceConnectionState { |
| 73 | const activeEnvironments = input.environments.filter((environment) => environment.isEnabled); |
| 74 | const failedEnvironment = activeEnvironments.find( |
| 75 | (environment) => environment.connectionError !== null, |
| 76 | ); |
| 77 | const connectingEnvironments = activeEnvironments.filter( |
| 78 | (environment) => |
| 79 | environment.connectionState === "connecting" || |
| 80 | environment.connectionState === "reconnecting", |
| 81 | ); |
| 82 | |
| 83 | return { |
| 84 | isLoadingConnections: !input.isReady, |
| 85 | hasConnections: input.environments.length > 0, |
| 86 | hasReadyEnvironment: |
| 87 | input.networkStatus !== "offline" && |
| 88 | activeEnvironments.some((environment) => environment.connectionState === "connected"), |
| 89 | hasConnectingEnvironment: connectingEnvironments.length > 0, |
| 90 | connectingEnvironments, |
| 91 | connectionState: overallConnectionState(activeEnvironments, input.networkStatus), |
| 92 | connectionError: failedEnvironment?.connectionError ?? null, |
| 93 | ...(failedEnvironment?.i18nConnectionError !== undefined |
| 94 | ? { i18nConnectionError: failedEnvironment.i18nConnectionError } |
| 95 | : {}), |
| 96 | networkStatus: input.networkStatus, |
| 97 | }; |
| 98 | } |
| 99 | |
| 100 | export function projectWorkspaceState(input: { |
| 101 | readonly isReady: boolean; |
| 102 | readonly networkStatus: NetworkStatus; |
| 103 | readonly environments: ReadonlyArray<WorkspaceEnvironment>; |
| 104 | readonly shellSummary: EnvironmentShellSummary; |
| 105 | readonly hasSyncingConnectedEnvironment: boolean; |
| 106 | }): WorkspaceState { |
| 107 | return { |
| 108 | ...projectWorkspaceConnectionState(input), |
| 109 | hasLoadedShellSnapshot: input.shellSummary.hasSnapshot, |
| 110 | hasPendingShellSnapshot: input.shellSummary.hasSynchronizingShell, |
| 111 | hasSyncingConnectedEnvironment: input.hasSyncingConnectedEnvironment, |
| 112 | shellSnapshotError: input.shellSummary.firstError, |
| 113 | }; |
| 114 | } |
| 115 | |
| 116 | // The atom starts at EMPTY_SHELL_STATE itself; any state after the cache read is a new object. |
| 117 | export function isEnvironmentShellPending(state: EnvironmentShellState): boolean { |
| 118 | return state === EMPTY_SHELL_STATE || state.status === "synchronizing"; |
| 119 | } |
| 120 | |
| 121 | // Reconnecting environments keep their last failure, so an error alone does not end the wait. |
| 122 | export function isWorkspaceCatalogPending(state: WorkspaceState): boolean { |
| 123 | return ( |
| 124 | state.networkStatus !== "offline" && |
| 125 | (state.hasConnectingEnvironment || state.hasSyncingConnectedEnvironment) |
| 126 | ); |
| 127 | } |
| 128 | |
| 129 | export type ServerConfigByEnvironmentId = ReadonlyMap<EnvironmentId, ServerConfig>; |