The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { msg, useMessages } from "gt-react"; |
| 2 | import { useAtomValue } from "@effect/atom-react"; |
| 3 | import type { |
| 4 | AtomCommandFailure, |
| 5 | AtomCommandResult, |
| 6 | AtomCommandSuccess, |
| 7 | } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import { |
| 9 | VcsActionUnavailableError, |
| 10 | type VcsActionOperation, |
| 11 | type RunVcsStackedActionInput, |
| 12 | } from "@t3tools/client-runtime/state/vcs"; |
| 13 | import { |
| 14 | AuthOrchestrationOperateScope, |
| 15 | AuthSourceControlWriteScope, |
| 16 | EnvironmentAuthorizationError, |
| 17 | type EnvironmentId, |
| 18 | type GitResolvePullRequestResult, |
| 19 | type SourceControlCloneProtocol, |
| 20 | type SourceControlProviderKind, |
| 21 | type SourceControlRepositoryVisibility, |
| 22 | type ThreadId, |
| 23 | } from "@t3tools/contracts"; |
| 24 | import * as Cause from "effect/Cause"; |
| 25 | import { AsyncResult } from "effect/reactivity"; |
| 26 | |
| 27 | import { appAtomRegistry } from "../rpc/atomRegistry"; |
| 28 | import { m } from "../i18n/messages"; |
| 29 | import { gitEnvironment } from "./git"; |
| 30 | import { useEnvironmentQuery } from "./query"; |
| 31 | import { readEnvironmentScope, useEnvironmentScope } from "./session"; |
| 32 | import { sourceControlEnvironment } from "./sourceControl"; |
| 33 | import { useAtomCommand } from "./use-atom-command"; |
| 34 | import { vcsActionManager, vcsEnvironment } from "./vcs"; |
| 35 | import { suggestAppAccessForRemote } from "../components/sourceControlAppAccessToast"; |
| 36 | |
| 37 | export type SourceControlActionKind = |
| 38 | | "init" |
| 39 | | "pull" |
| 40 | | "publishRepository" |
| 41 | | "runStackedAction" |
| 42 | | "preparePullRequestThread"; |
| 43 | |
| 44 | export interface SourceControlActionScope { |
| 45 | readonly environmentId: EnvironmentId | null; |
| 46 | readonly cwd: string | null; |
| 47 | } |
| 48 | |
| 49 | interface SourceControlActionState< |
| 50 | TArgs extends ReadonlyArray<unknown>, |
| 51 | R extends AtomCommandResult<unknown, unknown>, |
| 52 | > { |
| 53 | readonly isPending: boolean; |
| 54 | readonly isAllowed: boolean; |
| 55 | readonly error: unknown; |
| 56 | readonly run: ( |
| 57 | ...args: TArgs |
| 58 | ) => Promise< |
| 59 | AtomCommandResult< |
| 60 | AtomCommandSuccess<R>, |
| 61 | AtomCommandFailure<R> | VcsActionUnavailableError | EnvironmentAuthorizationError |
| 62 | > |
| 63 | >; |
| 64 | readonly resetError: () => void; |
| 65 | } |
| 66 | |
| 67 | const ACTION_OPERATION = { |
| 68 | init: "init", |
| 69 | pull: "pull", |
| 70 | publishRepository: "publish_repository", |
| 71 | runStackedAction: "run_change_request", |
| 72 | preparePullRequestThread: "prepare_pull_request_thread", |
| 73 | } as const satisfies Record<SourceControlActionKind, VcsActionOperation>; |
| 74 | |
| 75 | function useAction< |
| 76 | TArgs extends ReadonlyArray<unknown>, |
| 77 | R extends AtomCommandResult<unknown, unknown>, |
| 78 | >(input: { |
| 79 | readonly kind: SourceControlActionKind; |
| 80 | readonly label: string; |
| 81 | readonly scope: SourceControlActionScope; |
| 82 | readonly action: (...args: TArgs) => Promise<R>; |
| 83 | readonly onSuccess?: () => void; |
| 84 | readonly managedExternally?: boolean; |
| 85 | }): SourceControlActionState<TArgs, R> { |
| 86 | const isAllowed = useEnvironmentScope(input.scope.environmentId, AuthSourceControlWriteScope); |
| 87 | const operation = ACTION_OPERATION[input.kind]; |
| 88 | const state = useAtomValue(vcsActionManager.stateAtom(input.scope)); |
| 89 | const ownsState = state.operation === operation; |
| 90 | |
| 91 | const resetError = () => { |
| 92 | vcsActionManager.resetError(appAtomRegistry, input.scope, operation); |
| 93 | }; |
| 94 | |
| 95 | const run = async (...args: TArgs) => { |
| 96 | if ( |
| 97 | input.scope.environmentId === null || |
| 98 | !readEnvironmentScope(input.scope.environmentId, AuthSourceControlWriteScope) |
| 99 | ) { |
| 100 | return AsyncResult.failure<never, EnvironmentAuthorizationError>( |
| 101 | Cause.fail( |
| 102 | new EnvironmentAuthorizationError({ |
| 103 | requiredScope: AuthSourceControlWriteScope, |
| 104 | message: m(msg("This connection cannot change source control.")), |
| 105 | }), |
| 106 | ), |
| 107 | ); |
| 108 | } |
| 109 | const execute = async (): Promise< |
| 110 | AtomCommandResult<AtomCommandSuccess<R>, AtomCommandFailure<R>> |
| 111 | > => { |
| 112 | const result = await input.action(...args); |
| 113 | if (AsyncResult.isSuccess(result)) { |
| 114 | input.onSuccess?.(); |
| 115 | } |
| 116 | return result as AtomCommandResult<AtomCommandSuccess<R>, AtomCommandFailure<R>>; |
| 117 | }; |
| 118 | return input.managedExternally === true |
| 119 | ? execute() |
| 120 | : vcsActionManager.track( |
| 121 | appAtomRegistry, |
| 122 | input.scope, |
| 123 | { |
| 124 | operation, |
| 125 | label: m(input.label), |
| 126 | }, |
| 127 | execute, |
| 128 | ); |
| 129 | }; |
| 130 | |
| 131 | return { |
| 132 | isAllowed, |
| 133 | error: ownsState ? state.error : null, |
| 134 | isPending: ownsState && state.isRunning, |
| 135 | resetError, |
| 136 | run, |
| 137 | }; |
| 138 | } |
| 139 | |
| 140 | function resolveScope(scope: SourceControlActionScope) { |
| 141 | if (scope.environmentId === null || scope.cwd === null) { |
| 142 | return null; |
| 143 | } |
| 144 | return { |
| 145 | environmentId: scope.environmentId, |
| 146 | cwd: scope.cwd, |
| 147 | }; |
| 148 | } |
| 149 | |
| 150 | export function useSourceControlActionRunning( |
| 151 | scope: SourceControlActionScope, |
| 152 | kinds: ReadonlyArray<SourceControlActionKind>, |
| 153 | ): boolean { |
| 154 | const state = useAtomValue(vcsActionManager.stateAtom(scope)); |
| 155 | return ( |
| 156 | state.isRunning && |
| 157 | state.operation !== null && |
| 158 | kinds.some((kind) => ACTION_OPERATION[kind] === state.operation) |
| 159 | ); |
| 160 | } |
| 161 | |
| 162 | export function useVcsInitAction(scope: SourceControlActionScope) { |
| 163 | const init = useAtomCommand(vcsEnvironment.init, { reportFailure: false }); |
| 164 | const action = async () => { |
| 165 | const target = resolveScope(scope); |
| 166 | if (target === null) { |
| 167 | return AsyncResult.failure<never, VcsActionUnavailableError>( |
| 168 | Cause.fail( |
| 169 | new VcsActionUnavailableError({ |
| 170 | operation: "init", |
| 171 | environmentId: scope.environmentId, |
| 172 | cwd: scope.cwd, |
| 173 | }), |
| 174 | ), |
| 175 | ); |
| 176 | } |
| 177 | return init({ |
| 178 | environmentId: target.environmentId, |
| 179 | input: { cwd: target.cwd }, |
| 180 | }); |
| 181 | }; |
| 182 | return useAction({ kind: "init", label: msg("Initializing repository"), scope, action }); |
| 183 | } |
| 184 | |
| 185 | export function useVcsPullAction(scope: SourceControlActionScope) { |
| 186 | const pull = useAtomCommand(vcsEnvironment.pull, { reportFailure: false }); |
| 187 | const status = useEnvironmentQuery( |
| 188 | scope.environmentId !== null && scope.cwd !== null |
| 189 | ? vcsEnvironment.status({ |
| 190 | environmentId: scope.environmentId, |
| 191 | input: { cwd: scope.cwd }, |
| 192 | }) |
| 193 | : null, |
| 194 | ); |
| 195 | const action = async () => { |
| 196 | const target = resolveScope(scope); |
| 197 | if (target === null) { |
| 198 | return AsyncResult.failure<never, VcsActionUnavailableError>( |
| 199 | Cause.fail( |
| 200 | new VcsActionUnavailableError({ |
| 201 | operation: "pull", |
| 202 | environmentId: scope.environmentId, |
| 203 | cwd: scope.cwd, |
| 204 | }), |
| 205 | ), |
| 206 | ); |
| 207 | } |
| 208 | return pull({ |
| 209 | environmentId: target.environmentId, |
| 210 | input: { cwd: target.cwd }, |
| 211 | }); |
| 212 | }; |
| 213 | return useAction({ |
| 214 | kind: "pull", |
| 215 | label: msg("Pulling latest changes..."), |
| 216 | scope, |
| 217 | action, |
| 218 | onSuccess: status.refresh, |
| 219 | }); |
| 220 | } |
| 221 | |
| 222 | export function useGitStackedAction(scope: SourceControlActionScope) { |
| 223 | const runStackedAction = useAtomCommand(vcsActionManager.runStackedAction(scope), { |
| 224 | reportFailure: false, |
| 225 | }); |
| 226 | const status = useEnvironmentQuery( |
| 227 | scope.environmentId !== null && scope.cwd !== null |
| 228 | ? vcsEnvironment.status({ |
| 229 | environmentId: scope.environmentId, |
| 230 | input: { cwd: scope.cwd }, |
| 231 | }) |
| 232 | : null, |
| 233 | ); |
| 234 | |
| 235 | const action = async (input: RunVcsStackedActionInput) => { |
| 236 | if (resolveScope(scope) === null) { |
| 237 | return AsyncResult.failure<never, VcsActionUnavailableError>( |
| 238 | Cause.fail( |
| 239 | new VcsActionUnavailableError({ |
| 240 | operation: "run_change_request", |
| 241 | environmentId: scope.environmentId, |
| 242 | cwd: scope.cwd, |
| 243 | }), |
| 244 | ), |
| 245 | ); |
| 246 | } |
| 247 | return runStackedAction(input); |
| 248 | }; |
| 249 | |
| 250 | return useAction({ |
| 251 | kind: "runStackedAction", |
| 252 | label: msg("Running source control action"), |
| 253 | scope, |
| 254 | action, |
| 255 | onSuccess: status.refresh, |
| 256 | managedExternally: true, |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | export function useSourceControlPublishRepositoryAction(scope: SourceControlActionScope) { |
| 261 | const publishRepository = useAtomCommand(sourceControlEnvironment.publishRepository, { |
| 262 | reportFailure: false, |
| 263 | }); |
| 264 | const status = useEnvironmentQuery( |
| 265 | scope.environmentId !== null && scope.cwd !== null |
| 266 | ? vcsEnvironment.status({ |
| 267 | environmentId: scope.environmentId, |
| 268 | input: { cwd: scope.cwd }, |
| 269 | }) |
| 270 | : null, |
| 271 | ); |
| 272 | const action = async (input: { |
| 273 | provider: SourceControlProviderKind; |
| 274 | repository: string; |
| 275 | visibility: SourceControlRepositoryVisibility; |
| 276 | remoteName: string; |
| 277 | protocol: SourceControlCloneProtocol; |
| 278 | }) => { |
| 279 | const target = resolveScope(scope); |
| 280 | if (target === null) { |
| 281 | return AsyncResult.failure<never, VcsActionUnavailableError>( |
| 282 | Cause.fail( |
| 283 | new VcsActionUnavailableError({ |
| 284 | operation: "publish_repository", |
| 285 | environmentId: scope.environmentId, |
| 286 | cwd: scope.cwd, |
| 287 | }), |
| 288 | ), |
| 289 | ); |
| 290 | } |
| 291 | const result = await publishRepository({ |
| 292 | environmentId: target.environmentId, |
| 293 | input: { |
| 294 | cwd: target.cwd, |
| 295 | ...input, |
| 296 | }, |
| 297 | }); |
| 298 | if (result._tag === "Success") { |
| 299 | void suggestAppAccessForRemote(target.environmentId, result.value.remoteUrl); |
| 300 | } |
| 301 | return result; |
| 302 | }; |
| 303 | return useAction({ |
| 304 | kind: "publishRepository", |
| 305 | label: msg("Publishing repository"), |
| 306 | scope, |
| 307 | action, |
| 308 | onSuccess: status.refresh, |
| 309 | }); |
| 310 | } |
| 311 | |
| 312 | export function usePreparePullRequestThreadAction(scope: SourceControlActionScope) { |
| 313 | const preparePullRequestThread = useAtomCommand(gitEnvironment.preparePullRequestThread, { |
| 314 | reportFailure: false, |
| 315 | }); |
| 316 | const action = async (input: { |
| 317 | reference: string; |
| 318 | mode: "local" | "worktree"; |
| 319 | threadId?: ThreadId; |
| 320 | }) => { |
| 321 | const target = resolveScope(scope); |
| 322 | if (target === null) { |
| 323 | return AsyncResult.failure<never, VcsActionUnavailableError>( |
| 324 | Cause.fail( |
| 325 | new VcsActionUnavailableError({ |
| 326 | operation: "prepare_pull_request_thread", |
| 327 | environmentId: scope.environmentId, |
| 328 | cwd: scope.cwd, |
| 329 | }), |
| 330 | ), |
| 331 | ); |
| 332 | } |
| 333 | if ( |
| 334 | input.mode === "worktree" && |
| 335 | input.threadId !== undefined && |
| 336 | !readEnvironmentScope(target.environmentId, AuthOrchestrationOperateScope) |
| 337 | ) { |
| 338 | return AsyncResult.failure<never, EnvironmentAuthorizationError>( |
| 339 | Cause.fail( |
| 340 | new EnvironmentAuthorizationError({ |
| 341 | requiredScope: AuthOrchestrationOperateScope, |
| 342 | message: m(msg("This connection cannot change threads.")), |
| 343 | }), |
| 344 | ), |
| 345 | ); |
| 346 | } |
| 347 | return preparePullRequestThread({ |
| 348 | environmentId: target.environmentId, |
| 349 | input: { |
| 350 | cwd: target.cwd, |
| 351 | reference: input.reference, |
| 352 | mode: input.mode, |
| 353 | ...(input.threadId ? { threadId: input.threadId } : {}), |
| 354 | }, |
| 355 | }); |
| 356 | }; |
| 357 | return useAction({ |
| 358 | kind: "preparePullRequestThread", |
| 359 | label: msg("Preparing pull request thread"), |
| 360 | scope, |
| 361 | action, |
| 362 | }); |
| 363 | } |
| 364 | |
| 365 | export interface PullRequestResolutionTarget { |
| 366 | readonly environmentId: EnvironmentId | null; |
| 367 | readonly cwd: string | null; |
| 368 | readonly reference: string | null; |
| 369 | } |
| 370 | |
| 371 | export function readCachedPullRequestResolution( |
| 372 | target: PullRequestResolutionTarget, |
| 373 | ): GitResolvePullRequestResult | null { |
| 374 | if (target.environmentId === null || target.cwd === null || target.reference === null) { |
| 375 | return null; |
| 376 | } |
| 377 | return AsyncResult.getOrElse( |
| 378 | appAtomRegistry.get( |
| 379 | gitEnvironment.pullRequestResolution({ |
| 380 | environmentId: target.environmentId, |
| 381 | input: { cwd: target.cwd, reference: target.reference }, |
| 382 | }), |
| 383 | ), |
| 384 | () => null, |
| 385 | ); |
| 386 | } |
| 387 | |
| 388 | export function usePullRequestResolutionState(target: PullRequestResolutionTarget) { |
| 389 | const resolve = useMessages(); |
| 390 | const query = useEnvironmentQuery( |
| 391 | target.environmentId !== null && target.cwd !== null && target.reference !== null |
| 392 | ? gitEnvironment.pullRequestResolution({ |
| 393 | environmentId: target.environmentId, |
| 394 | input: { |
| 395 | cwd: target.cwd, |
| 396 | reference: target.reference, |
| 397 | }, |
| 398 | }) |
| 399 | : null, |
| 400 | resolve, |
| 401 | ); |
| 402 | const cached = readCachedPullRequestResolution(target); |
| 403 | |
| 404 | return { |
| 405 | data: query.data ?? cached, |
| 406 | error: query.error, |
| 407 | isPending: query.isPending && cached === null, |
| 408 | isFetching: query.isPending, |
| 409 | refresh: query.refresh, |
| 410 | }; |
| 411 | } |