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 { EnvironmentConnectionPhase } from "@t3tools/client-runtime/connection"; |
| 2 | import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects"; |
| 3 | import { isPickableProjectMember } from "@t3tools/client-runtime/state/project-grouping"; |
| 4 | import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
| 5 | import { repositoryGroupingKeyOf, type EnvironmentId, type ServerConfig } from "@t3tools/contracts"; |
| 6 | |
| 7 | import { scopedProjectKey } from "../../lib/scopedEntities"; |
| 8 | import type { HomeProjectScope } from "../home/homeThreadList"; |
| 9 | |
| 10 | type ProjectRef = { readonly environmentId: string; readonly projectId: string }; |
| 11 | |
| 12 | type DraftProjectSelectionResolution = |
| 13 | | { readonly kind: "preserve" } |
| 14 | | { readonly kind: "select"; readonly project: EnvironmentProject } |
| 15 | | { readonly kind: "pick" } |
| 16 | | { readonly kind: "dismiss" }; |
| 17 | |
| 18 | function selectOpenProjectScopeMember( |
| 19 | scope: HomeProjectScope, |
| 20 | preferredEnvironmentId: EnvironmentId | null, |
| 21 | connectedEnvironmentIds: ReadonlySet<EnvironmentId>, |
| 22 | lastProject?: ProjectRef, |
| 23 | ): EnvironmentProject | undefined { |
| 24 | const open = scope.projects.filter(isPickableProjectMember); |
| 25 | const remembered = |
| 26 | lastProject && |
| 27 | open.find( |
| 28 | (project) => |
| 29 | project.environmentId === lastProject.environmentId && project.id === lastProject.projectId, |
| 30 | ); |
| 31 | if (remembered) return remembered; |
| 32 | |
| 33 | const rank = (project: EnvironmentProject) => |
| 34 | project.environmentId === preferredEnvironmentId ? 0 : project === scope.representative ? 1 : 2; |
| 35 | open.sort((left, right) => rank(left) - rank(right)); |
| 36 | return open.find((project) => connectedEnvironmentIds.has(project.environmentId)) ?? open[0]; |
| 37 | } |
| 38 | |
| 39 | /** Picker scopes have an open copy; prefer a connected one, then the selected environment. */ |
| 40 | export function getProjectScopeSelectionTarget( |
| 41 | scope: HomeProjectScope, |
| 42 | preferredEnvironmentId: EnvironmentId | null, |
| 43 | connectedEnvironmentIds: ReadonlySet<EnvironmentId>, |
| 44 | ): EnvironmentProject { |
| 45 | const project = selectOpenProjectScopeMember( |
| 46 | scope, |
| 47 | preferredEnvironmentId, |
| 48 | connectedEnvironmentIds, |
| 49 | ); |
| 50 | if (!project) throw new Error("A pickable project group must have an open workspace"); |
| 51 | return project; |
| 52 | } |
| 53 | |
| 54 | export function filterProjectScopes( |
| 55 | scopes: ReadonlyArray<HomeProjectScope>, |
| 56 | searchText: string, |
| 57 | ): ReadonlyArray<HomeProjectScope> { |
| 58 | const query = searchText.trim().toLowerCase(); |
| 59 | if (!query) return scopes; |
| 60 | return scopes.filter( |
| 61 | (scope) => |
| 62 | scope.title.toLowerCase().includes(query) || |
| 63 | scope.projects.some( |
| 64 | (project) => |
| 65 | project.title.toLowerCase().includes(query) || |
| 66 | project.workspaceRoot.toLowerCase().includes(query), |
| 67 | ), |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | // Cached configuration reserves the row; connection readiness only gates activation. |
| 72 | export function resolveNewTaskScratchOption(input: { |
| 73 | readonly environments: ReadonlyArray<{ |
| 74 | readonly environmentId: EnvironmentId; |
| 75 | readonly connectionState: EnvironmentConnectionPhase; |
| 76 | }>; |
| 77 | readonly serverConfigs: ReadonlyMap<EnvironmentId, Pick<ServerConfig, "scratchWorkspaceRoot">>; |
| 78 | readonly selectedEnvironmentId: EnvironmentId | null; |
| 79 | }) { |
| 80 | const offering = input.environments.filter( |
| 81 | (environment) => |
| 82 | (input.serverConfigs.get(environment.environmentId)?.scratchWorkspaceRoot ?? null) !== null, |
| 83 | ); |
| 84 | const ready = offering.filter( |
| 85 | (environment) => |
| 86 | availableScratchWorkspaceRoot( |
| 87 | environment.connectionState, |
| 88 | input.serverConfigs.get(environment.environmentId), |
| 89 | ) !== null, |
| 90 | ); |
| 91 | const candidates = ready.length > 0 ? ready : offering; |
| 92 | const environment = |
| 93 | candidates.find((candidate) => candidate.environmentId === input.selectedEnvironmentId) ?? |
| 94 | candidates[0]; |
| 95 | return environment ? { environmentId: environment.environmentId, ready: ready.length > 0 } : null; |
| 96 | } |
| 97 | |
| 98 | export function resolveEnvironmentProjectMatch( |
| 99 | projectsOnTarget: ReadonlyArray<EnvironmentProject>, |
| 100 | selectedProject: EnvironmentProject | null, |
| 101 | ): EnvironmentProject | null { |
| 102 | const repositoryKey = selectedProject?.repositoryIdentity |
| 103 | ? repositoryGroupingKeyOf(selectedProject.repositoryIdentity) |
| 104 | : null; |
| 105 | // Use || null: placeholder workspaceRoot can be empty, which supplies no useful basename. |
| 106 | const workspaceBasename = selectedProject?.workspaceRoot.split("/").at(-1) || null; |
| 107 | // Use weaker signals only when identity is unknown; different known repositories never match by basename or title. |
| 108 | const isKnownMismatch = (project: EnvironmentProject) => { |
| 109 | const projectKey = project.repositoryIdentity |
| 110 | ? repositoryGroupingKeyOf(project.repositoryIdentity) |
| 111 | : null; |
| 112 | return repositoryKey !== null && projectKey !== null && projectKey !== repositoryKey; |
| 113 | }; |
| 114 | return ( |
| 115 | (repositoryKey !== null |
| 116 | ? projectsOnTarget.find( |
| 117 | (project) => |
| 118 | (project.repositoryIdentity |
| 119 | ? repositoryGroupingKeyOf(project.repositoryIdentity) |
| 120 | : null) === repositoryKey, |
| 121 | ) |
| 122 | : undefined) ?? |
| 123 | (workspaceBasename !== null |
| 124 | ? projectsOnTarget.find( |
| 125 | (project) => |
| 126 | !isKnownMismatch(project) && |
| 127 | project.workspaceRoot.split("/").at(-1) === workspaceBasename, |
| 128 | ) |
| 129 | : undefined) ?? |
| 130 | (selectedProject !== null |
| 131 | ? projectsOnTarget.find( |
| 132 | (project) => !isKnownMismatch(project) && project.title === selectedProject.title, |
| 133 | ) |
| 134 | : undefined) ?? |
| 135 | projectsOnTarget[0] ?? |
| 136 | null |
| 137 | ); |
| 138 | } |
| 139 | |
| 140 | /** Both projects have a known repository identity, and it is the same one. */ |
| 141 | export function isSameRepository( |
| 142 | left: EnvironmentProject | null, |
| 143 | right: EnvironmentProject | null, |
| 144 | ): boolean { |
| 145 | return ( |
| 146 | left?.repositoryIdentity != null && |
| 147 | right?.repositoryIdentity != null && |
| 148 | repositoryGroupingKeyOf(left.repositoryIdentity) === |
| 149 | repositoryGroupingKeyOf(right.repositoryIdentity) |
| 150 | ); |
| 151 | } |
| 152 | |
| 153 | export function resolveDraftProjectSelection( |
| 154 | selectedProjectKey: string | null, |
| 155 | projects: ReadonlyArray<EnvironmentProject>, |
| 156 | projectScopes: ReadonlyArray<HomeProjectScope>, |
| 157 | draft: { |
| 158 | readonly incomingShareId: string | undefined; |
| 159 | /** The draft's first project picker closed and the draft is showing again. */ |
| 160 | readonly pickerClosed: boolean; |
| 161 | readonly useLastProject?: boolean | undefined; |
| 162 | readonly lastProject?: ProjectRef | undefined; |
| 163 | }, |
| 164 | ): DraftProjectSelectionResolution { |
| 165 | const hasExplicitProjectSelection = |
| 166 | selectedProjectKey !== null && |
| 167 | projects.some( |
| 168 | (project) => scopedProjectKey(project.environmentId, project.id) === selectedProjectKey, |
| 169 | ); |
| 170 | if (hasExplicitProjectSelection) { |
| 171 | return { kind: "preserve" }; |
| 172 | } |
| 173 | // A share draft holds content the user sent here, so cancelling the picker must not drop it. |
| 174 | if (draft.pickerClosed && selectedProjectKey === null && draft.incomingShareId === undefined) { |
| 175 | return { kind: "dismiss" }; |
| 176 | } |
| 177 | |
| 178 | if (draft.incomingShareId === undefined) { |
| 179 | return { kind: "pick" }; |
| 180 | } |
| 181 | const last = |
| 182 | draft.useLastProject === false |
| 183 | ? undefined |
| 184 | : projects.find( |
| 185 | (project) => |
| 186 | project.environmentId === draft.lastProject?.environmentId && |
| 187 | project.id === draft.lastProject.projectId && |
| 188 | project.archivedAt == null, |
| 189 | ); |
| 190 | if (last) { |
| 191 | return { kind: "select", project: last }; |
| 192 | } |
| 193 | const onlyScope = projectScopes.length === 1 ? projectScopes[0] : null; |
| 194 | return onlyScope ? { kind: "select", project: onlyScope.representative } : { kind: "pick" }; |
| 195 | } |
| 196 | |
| 197 | /** The live copy of the thread list's filtered project a new thread starts in: the remembered one if it is a copy. */ |
| 198 | export function newTaskFilteredProject( |
| 199 | scope: HomeProjectScope, |
| 200 | preferredEnvironmentId: EnvironmentId | null, |
| 201 | connectedEnvironmentIds: ReadonlySet<EnvironmentId>, |
| 202 | lastProject: ProjectRef | undefined, |
| 203 | ): ProjectRef | undefined { |
| 204 | const project = selectOpenProjectScopeMember( |
| 205 | scope, |
| 206 | preferredEnvironmentId, |
| 207 | connectedEnvironmentIds, |
| 208 | lastProject, |
| 209 | ); |
| 210 | return project && { environmentId: project.environmentId, projectId: project.id }; |
| 211 | } |
| 212 | |
| 213 | export function newTaskDestination(options: { |
| 214 | readonly projects: ReadonlyArray<EnvironmentProject>; |
| 215 | readonly requestId: string; |
| 216 | readonly useLastProject: boolean | undefined; |
| 217 | readonly currentProject: ProjectRef | undefined; |
| 218 | readonly filteredProject: ProjectRef | undefined; |
| 219 | readonly lastProject: ProjectRef | undefined; |
| 220 | }) { |
| 221 | const find = (ref: ProjectRef | undefined) => |
| 222 | ref && |
| 223 | options.projects.find( |
| 224 | (project) => project.environmentId === ref.environmentId && project.id === ref.projectId, |
| 225 | ); |
| 226 | // A remembered project that was archived since opens the picker; the current one still wins. |
| 227 | const last = find(options.lastProject); |
| 228 | const project = |
| 229 | options.useLastProject === false |
| 230 | ? undefined |
| 231 | : (find(options.currentProject) ?? |
| 232 | find(options.filteredProject) ?? |
| 233 | (last?.archivedAt == null ? last : undefined)); |
| 234 | return project |
| 235 | ? { |
| 236 | draft: { |
| 237 | requestId: options.requestId, |
| 238 | environmentId: String(project.environmentId), |
| 239 | projectId: String(project.id), |
| 240 | }, |
| 241 | } |
| 242 | : { draft: { requestId: options.requestId } }; |
| 243 | } |