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 { |
| 2 | EnvironmentId, |
| 3 | EnvironmentMachineKind, |
| 4 | VcsRef, |
| 5 | ProjectId, |
| 6 | WorktreeSubmodules, |
| 7 | } from "@t3tools/contracts"; |
| 8 | import type { ThreadStructureRuntime } from "@t3tools/client-runtime/state/shell"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import { msg } from "gt-react"; |
| 11 | import { sanitizeNewRefName } from "@t3tools/shared/git"; |
| 12 | import { workspaceRelativeFilePath } from "@t3tools/shared/path"; |
| 13 | import { toSortableTimestamp } from "@t3tools/client-runtime/state/thread-sort"; |
| 14 | import { m, type MessageResolver } from "../i18n/messages"; |
| 15 | import { deriveCanInterruptRunningThread } from "../session-logic"; |
| 16 | export { deriveLocalBranchNameFromRemoteRef, sanitizeNewRefName } from "@t3tools/shared/git"; |
| 17 | |
| 18 | export interface EnvironmentOption { |
| 19 | environmentId: EnvironmentId; |
| 20 | projectId: ProjectId | null; |
| 21 | label: string; |
| 22 | isPrimary: boolean; |
| 23 | machine: EnvironmentMachineKind; |
| 24 | } |
| 25 | |
| 26 | /** "Add to another machine" stays muted until hovered: easy to find, quieter than the machines. */ |
| 27 | export const ADD_TO_MACHINE_LABEL_CLASS = |
| 28 | "flex min-w-0 items-center gap-1.5 opacity-55 in-data-highlighted:opacity-100 in-data-popup-open:opacity-100"; |
| 29 | |
| 30 | export const EnvMode = Schema.Literals(["local", "worktree"]); |
| 31 | export type EnvMode = typeof EnvMode.Type; |
| 32 | |
| 33 | const GENERIC_LOCAL_ENVIRONMENT_LABELS = new Set(["local", "local environment"]); |
| 34 | |
| 35 | function normalizeDisplayLabel(value: string | null | undefined): string | null { |
| 36 | const trimmed = value?.trim(); |
| 37 | return trimmed && trimmed.length > 0 ? trimmed : null; |
| 38 | } |
| 39 | |
| 40 | export function resolveEnvironmentOptionLabel( |
| 41 | input: { |
| 42 | isPrimary: boolean; |
| 43 | environmentId: EnvironmentId; |
| 44 | runtimeLabel?: string | null; |
| 45 | savedLabel?: string | null; |
| 46 | }, |
| 47 | resolve: MessageResolver = m, |
| 48 | ): string { |
| 49 | const runtimeLabel = normalizeDisplayLabel(input.runtimeLabel); |
| 50 | const savedLabel = normalizeDisplayLabel(input.savedLabel); |
| 51 | |
| 52 | if (input.isPrimary) { |
| 53 | const preferredLocalLabel = [runtimeLabel, savedLabel].find((label) => { |
| 54 | if (!label) return false; |
| 55 | return !GENERIC_LOCAL_ENVIRONMENT_LABELS.has(label.toLowerCase()); |
| 56 | }); |
| 57 | return preferredLocalLabel ?? resolve(msg("This device")); |
| 58 | } |
| 59 | |
| 60 | return runtimeLabel ?? savedLabel ?? input.environmentId; |
| 61 | } |
| 62 | |
| 63 | export function shouldShowEnvironmentIndicator(input: { |
| 64 | activeEnvironment: Pick<EnvironmentOption, "isPrimary"> | null; |
| 65 | canPickEnvironment: boolean; |
| 66 | }): boolean { |
| 67 | if (input.canPickEnvironment) return true; |
| 68 | return input.activeEnvironment !== null && !input.activeEnvironment.isPrimary; |
| 69 | } |
| 70 | |
| 71 | export function shouldShowComposerContextStrip(input: { |
| 72 | isDraftHeroState: boolean; |
| 73 | persistInActiveThreads: boolean; |
| 74 | hasActiveProject: boolean; |
| 75 | isGitRepo: boolean; |
| 76 | showEnvironmentIndicator: boolean; |
| 77 | }): boolean { |
| 78 | // A new thread always shows the strip, since it names the permissions the first send uses. |
| 79 | return ( |
| 80 | input.hasActiveProject && |
| 81 | (input.isDraftHeroState || |
| 82 | (input.persistInActiveThreads && (input.isGitRepo || input.showEnvironmentIndicator))) |
| 83 | ); |
| 84 | } |
| 85 | |
| 86 | // Hysteresis prevents labels and icons from flapping at the width boundary. |
| 87 | const CONTEXT_STRIP_COMPACT_EXPAND_HYSTERESIS_PX = 16; |
| 88 | |
| 89 | export function resolveContextStripLabelsCompact(input: { |
| 90 | compact: boolean; |
| 91 | neededWidth: number; |
| 92 | availableWidth: number; |
| 93 | }): boolean { |
| 94 | return input.compact |
| 95 | ? input.neededWidth > input.availableWidth - CONTEXT_STRIP_COMPACT_EXPAND_HYSTERESIS_PX |
| 96 | : input.neededWidth > input.availableWidth; |
| 97 | } |
| 98 | |
| 99 | export function resolveEnvModeLabel(mode: EnvMode, resolve: MessageResolver = m): string { |
| 100 | return mode === "worktree" ? resolve(msg("New worktree")) : resolve(msg("Current checkout")); |
| 101 | } |
| 102 | |
| 103 | export const WORKTREE_SUBMODULES_LABELS: Record<WorktreeSubmodules, string> = { |
| 104 | recursive: msg("Recursive"), |
| 105 | "top-level": msg("Top level only"), |
| 106 | none: msg("Skip"), |
| 107 | }; |
| 108 | |
| 109 | export function resolveCurrentWorkspaceLabel( |
| 110 | activeWorktreePath: string | null, |
| 111 | resolve: MessageResolver = m, |
| 112 | ): string { |
| 113 | return activeWorktreePath |
| 114 | ? resolve(msg("Current worktree")) |
| 115 | : resolveEnvModeLabel("local", resolve); |
| 116 | } |
| 117 | |
| 118 | export function resolveLockedWorkspaceLabel( |
| 119 | activeWorktreePath: string | null, |
| 120 | effectiveEnvMode: EnvMode, |
| 121 | resolve: MessageResolver = m, |
| 122 | ): string { |
| 123 | if (activeWorktreePath) |
| 124 | return activeWorktreePath.split(/[\\/]/).findLast(Boolean) ?? resolve(msg("Worktree")); |
| 125 | return effectiveEnvMode === "worktree" |
| 126 | ? resolveEnvModeLabel("worktree", resolve) |
| 127 | : resolve(msg("Local checkout")); |
| 128 | } |
| 129 | |
| 130 | export interface PreviousWorktreeSeed { |
| 131 | branch: string | null; |
| 132 | worktreePath: string; |
| 133 | } |
| 134 | |
| 135 | export function resolvePreviousWorktreeSeed(input: { |
| 136 | threads: ReadonlyArray<{ |
| 137 | branch: string | null; |
| 138 | worktreePath: string | null; |
| 139 | updatedAt: string; |
| 140 | archivedAt?: string | null; |
| 141 | }>; |
| 142 | currentWorktreePath: string | null; |
| 143 | }): PreviousWorktreeSeed | null { |
| 144 | let latest: { branch: string | null; worktreePath: string; updatedAt: number } | null = null; |
| 145 | for (const thread of input.threads) { |
| 146 | if ( |
| 147 | !thread.worktreePath || |
| 148 | thread.worktreePath === input.currentWorktreePath || |
| 149 | (thread.archivedAt ?? null) !== null |
| 150 | ) { |
| 151 | continue; |
| 152 | } |
| 153 | const updatedAt = toSortableTimestamp(thread.updatedAt); |
| 154 | if (updatedAt === null) { |
| 155 | continue; |
| 156 | } |
| 157 | if (latest === null || updatedAt > latest.updatedAt) { |
| 158 | latest = { |
| 159 | branch: thread.branch, |
| 160 | worktreePath: thread.worktreePath, |
| 161 | updatedAt, |
| 162 | }; |
| 163 | } |
| 164 | } |
| 165 | return latest === null ? null : { branch: latest.branch, worktreePath: latest.worktreePath }; |
| 166 | } |
| 167 | |
| 168 | export function resolvePreviousWorktreeLabel( |
| 169 | seed: PreviousWorktreeSeed, |
| 170 | resolve: MessageResolver = m, |
| 171 | ): string { |
| 172 | return seed.branch |
| 173 | ? resolve(msg("Previous worktree ({branch})", { branch: seed.branch })) |
| 174 | : resolve(msg("Previous worktree")); |
| 175 | } |
| 176 | |
| 177 | export function resolveEffectiveEnvMode(input: { |
| 178 | activeWorktreePath: string | null; |
| 179 | hasServerThread: boolean; |
| 180 | draftThreadEnvMode: EnvMode | undefined; |
| 181 | preparingWorktree?: boolean; |
| 182 | }): EnvMode { |
| 183 | const { activeWorktreePath, hasServerThread, draftThreadEnvMode, preparingWorktree } = input; |
| 184 | if (!hasServerThread) { |
| 185 | if (activeWorktreePath) { |
| 186 | return "local"; |
| 187 | } |
| 188 | return draftThreadEnvMode === "worktree" ? "worktree" : "local"; |
| 189 | } |
| 190 | return activeWorktreePath || preparingWorktree ? "worktree" : "local"; |
| 191 | } |
| 192 | |
| 193 | export function resolveDraftEnvModeAfterBranchChange(input: { |
| 194 | nextWorktreePath: string | null; |
| 195 | currentWorktreePath: string | null; |
| 196 | effectiveEnvMode: EnvMode; |
| 197 | }): EnvMode { |
| 198 | const { nextWorktreePath, currentWorktreePath, effectiveEnvMode } = input; |
| 199 | if (nextWorktreePath) { |
| 200 | return "worktree"; |
| 201 | } |
| 202 | if (effectiveEnvMode === "worktree" && !currentWorktreePath) { |
| 203 | return "worktree"; |
| 204 | } |
| 205 | return "local"; |
| 206 | } |
| 207 | |
| 208 | export function resolveBranchToolbarValue(input: { |
| 209 | envMode: EnvMode; |
| 210 | activeWorktreePath: string | null; |
| 211 | activeThreadBranch: string | null; |
| 212 | currentGitBranch: string | null; |
| 213 | }): string | null { |
| 214 | const { envMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input; |
| 215 | if (envMode === "worktree" && !activeWorktreePath) { |
| 216 | return activeThreadBranch ?? currentGitBranch; |
| 217 | } |
| 218 | return currentGitBranch ?? activeThreadBranch; |
| 219 | } |
| 220 | |
| 221 | export interface BranchHold { |
| 222 | readonly branch: string | null; |
| 223 | readonly environmentId: EnvironmentId; |
| 224 | /** |
| 225 | * Each checkout the thread can show before it catches up, with the branch its status reported |
| 226 | * before the switch. A move from a worktree to the project root lists both. |
| 227 | */ |
| 228 | readonly checkouts: ReadonlyArray<{ readonly cwd: string; readonly replaces: string | null }>; |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * A finished checkout's branch stays on screen until status reports any change, because the status |
| 233 | * stream and the RPC reply are not ordered and showing status first would flip back to the old branch. |
| 234 | */ |
| 235 | export function isBranchHoldCurrent( |
| 236 | hold: BranchHold, |
| 237 | current: { |
| 238 | readonly canonicalBranch: string | null; |
| 239 | readonly environmentId: EnvironmentId; |
| 240 | readonly cwd: string | null; |
| 241 | }, |
| 242 | ): boolean { |
| 243 | return ( |
| 244 | hold.environmentId === current.environmentId && |
| 245 | hold.checkouts.some( |
| 246 | (checkout) => checkout.cwd === current.cwd && checkout.replaces === current.canonicalBranch, |
| 247 | ) |
| 248 | ); |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * The base a New worktree thread gets without the user picking one. Only a listed ref qualifies: |
| 253 | * during a clone the status names an unborn HEAD that no ref backs. |
| 254 | */ |
| 255 | export function resolveDefaultWorktreeBaseBranch( |
| 256 | refs: ReadonlyArray<Pick<VcsRef, "name" | "isDefault" | "current">>, |
| 257 | ): string | null { |
| 258 | return (refs.find((ref) => ref.isDefault) ?? refs.find((ref) => ref.current))?.name ?? null; |
| 259 | } |
| 260 | |
| 261 | export function resolveBranchTriggerLabel( |
| 262 | input: { |
| 263 | activeWorktreePath: string | null; |
| 264 | effectiveEnvMode: EnvMode; |
| 265 | resolvedActiveBranch: string | null; |
| 266 | resolvedActiveBranchIsRemote: boolean | null; |
| 267 | startFromOrigin: boolean; |
| 268 | }, |
| 269 | resolve: MessageResolver = m, |
| 270 | ): string { |
| 271 | const { |
| 272 | activeWorktreePath, |
| 273 | effectiveEnvMode, |
| 274 | resolvedActiveBranch, |
| 275 | resolvedActiveBranchIsRemote, |
| 276 | startFromOrigin, |
| 277 | } = input; |
| 278 | if (!resolvedActiveBranch) { |
| 279 | return resolve(msg("Select ref")); |
| 280 | } |
| 281 | if (effectiveEnvMode === "worktree" && !activeWorktreePath) { |
| 282 | // Only the server can determine a local branch's configured remote. |
| 283 | return startFromOrigin && resolvedActiveBranchIsRemote === false |
| 284 | ? resolve(msg("From {branch} (remote)", { branch: resolvedActiveBranch })) |
| 285 | : resolve(msg("From {branch}", { branch: resolvedActiveBranch })); |
| 286 | } |
| 287 | return resolvedActiveBranch; |
| 288 | } |
| 289 | |
| 290 | export function resolveBranchToolbarPrBranch(input: { |
| 291 | activeThreadBranch: string | null; |
| 292 | resolvedActiveBranch: string | null; |
| 293 | }): string | null { |
| 294 | return input.activeThreadBranch === input.resolvedActiveBranch ? input.activeThreadBranch : null; |
| 295 | } |
| 296 | |
| 297 | export function resolveLocalCheckoutBranchMismatch(input: { |
| 298 | effectiveEnvMode: EnvMode; |
| 299 | activeWorktreePath: string | null; |
| 300 | activeThreadBranch: string | null; |
| 301 | currentGitBranch: string | null; |
| 302 | }): { threadBranch: string; currentBranch: string } | null { |
| 303 | const { effectiveEnvMode, activeWorktreePath, activeThreadBranch, currentGitBranch } = input; |
| 304 | if (effectiveEnvMode !== "local" || activeWorktreePath !== null) { |
| 305 | return null; |
| 306 | } |
| 307 | if (!activeThreadBranch || !currentGitBranch || activeThreadBranch === currentGitBranch) { |
| 308 | return null; |
| 309 | } |
| 310 | return { threadBranch: activeThreadBranch, currentBranch: currentGitBranch }; |
| 311 | } |
| 312 | |
| 313 | /** |
| 314 | * Keeps a picked ref's worktree claim only when a read in that worktree finds it checked out. |
| 315 | * `freshRead` is `null` when the read failed. |
| 316 | */ |
| 317 | export function resolveConfirmedSelectedRef( |
| 318 | listedRef: VcsRef, |
| 319 | freshRead: { readonly refs: ReadonlyArray<VcsRef> } | null, |
| 320 | ): VcsRef { |
| 321 | const checkedOut = |
| 322 | freshRead?.refs.some((ref) => ref.name === listedRef.name && !ref.isRemote && ref.current) ?? |
| 323 | false; |
| 324 | return checkedOut ? listedRef : { ...listedRef, worktreePath: null }; |
| 325 | } |
| 326 | |
| 327 | export function resolveBranchSelectionTarget(input: { |
| 328 | activeProjectCwd: string; |
| 329 | activeProjectCheckoutRoot?: string | undefined; |
| 330 | activeWorktreePath: string | null; |
| 331 | refName: Pick<VcsRef, "isDefault" | "worktreePath">; |
| 332 | }): { |
| 333 | checkoutCwd: string; |
| 334 | nextWorktreePath: string | null; |
| 335 | reuseExistingWorktree: boolean; |
| 336 | staysInActiveCheckout: boolean; |
| 337 | } { |
| 338 | const { activeProjectCwd, activeWorktreePath, refName } = input; |
| 339 | |
| 340 | if (refName.worktreePath) { |
| 341 | // Until the project's checkout root loads, a checkout containing the project counts as its own. |
| 342 | const isProjectCheckout = |
| 343 | input.activeProjectCheckoutRoot === undefined |
| 344 | ? workspaceRelativeFilePath(activeProjectCwd, refName.worktreePath) !== null |
| 345 | : workspaceRelativeFilePath(input.activeProjectCheckoutRoot, refName.worktreePath) === "."; |
| 346 | const nextWorktreePath = isProjectCheckout ? null : refName.worktreePath; |
| 347 | return { |
| 348 | checkoutCwd: refName.worktreePath, |
| 349 | nextWorktreePath, |
| 350 | reuseExistingWorktree: true, |
| 351 | // Listed refs can predate a checkout made outside the picker, so only Git can confirm this one. |
| 352 | staysInActiveCheckout: nextWorktreePath === activeWorktreePath, |
| 353 | }; |
| 354 | } |
| 355 | |
| 356 | const nextWorktreePath = |
| 357 | activeWorktreePath !== null && refName.isDefault ? null : activeWorktreePath; |
| 358 | |
| 359 | return { |
| 360 | checkoutCwd: nextWorktreePath ?? activeProjectCwd, |
| 361 | nextWorktreePath, |
| 362 | reuseExistingWorktree: false, |
| 363 | staysInActiveCheckout: nextWorktreePath === activeWorktreePath, |
| 364 | }; |
| 365 | } |
| 366 | |
| 367 | export function isWorktreeChangeBlocked(input: { |
| 368 | runtime: ThreadStructureRuntime | null; |
| 369 | currentWorktreePath: string | null; |
| 370 | nextWorktreePath: string | null; |
| 371 | }): boolean { |
| 372 | return ( |
| 373 | input.nextWorktreePath !== input.currentWorktreePath && |
| 374 | deriveCanInterruptRunningThread(true, input.runtime) |
| 375 | ); |
| 376 | } |
| 377 | |
| 378 | export function shouldIncludeBranchPickerItem(input: { |
| 379 | itemValue: string; |
| 380 | normalizedQuery: string; |
| 381 | createBranchItemValue: string | null; |
| 382 | checkoutPullRequestItemValue: string | null; |
| 383 | }): boolean { |
| 384 | const { itemValue, normalizedQuery, createBranchItemValue, checkoutPullRequestItemValue } = input; |
| 385 | |
| 386 | if (normalizedQuery.length === 0) { |
| 387 | return true; |
| 388 | } |
| 389 | |
| 390 | if (createBranchItemValue && itemValue === createBranchItemValue) { |
| 391 | return true; |
| 392 | } |
| 393 | |
| 394 | if (checkoutPullRequestItemValue && itemValue === checkoutPullRequestItemValue) { |
| 395 | return true; |
| 396 | } |
| 397 | |
| 398 | const lowerItemValue = itemValue.toLowerCase(); |
| 399 | if (lowerItemValue.includes(normalizedQuery)) { |
| 400 | return true; |
| 401 | } |
| 402 | |
| 403 | const sanitizedQuery = sanitizeNewRefName(normalizedQuery); |
| 404 | return ( |
| 405 | sanitizedQuery.length > 0 && |
| 406 | sanitizedQuery !== normalizedQuery && |
| 407 | lowerItemValue.includes(sanitizedQuery) |
| 408 | ); |
| 409 | } |