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 | GitCommandError, |
| 3 | ServerSettings, |
| 4 | ServerSettingsError, |
| 5 | ServerSettingsPatch, |
| 6 | VcsError, |
| 7 | VcsListRemotesResult, |
| 8 | VcsProcessExitFailureKind, |
| 9 | VcsSwitchRefInput, |
| 10 | VcsSwitchRefResult, |
| 11 | } from "@t3tools/contracts"; |
| 12 | import * as Context from "effect/Context"; |
| 13 | import type * as Effect from "effect/Effect"; |
| 14 | import type * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; |
| 15 | |
| 16 | export interface SourceControlProcessInput { |
| 17 | readonly operation: string; |
| 18 | readonly command: string; |
| 19 | readonly args: ReadonlyArray<string>; |
| 20 | readonly cwd: string; |
| 21 | readonly spawnCwd?: string; |
| 22 | readonly stdin?: string; |
| 23 | readonly onStdoutChunk?: (chunk: Uint8Array) => void; |
| 24 | readonly env?: NodeJS.ProcessEnv; |
| 25 | readonly allowNonZeroExit?: boolean; |
| 26 | readonly timeoutMs?: number; |
| 27 | readonly maxOutputBytes?: number; |
| 28 | readonly outputMode?: "error" | "truncate" | undefined; |
| 29 | readonly appendTruncationMarker?: boolean; |
| 30 | /** |
| 31 | * Reads a failure the generic rules cannot, such as a host CLI's sign-in hint, from its stderr. |
| 32 | * Null leaves it to them. |
| 33 | */ |
| 34 | readonly classifyFailure?: (stderr: string) => VcsProcessExitFailureKind | null; |
| 35 | /** The error's detail when the command reports something it was asked for as missing. */ |
| 36 | readonly notFoundDetail?: string; |
| 37 | /** |
| 38 | * Runs sharing a key run at most `limit` at a time, for a host that throttles bursts of its CLI. |
| 39 | * The first run of a key fixes its limit. |
| 40 | */ |
| 41 | readonly concurrency?: { readonly key: string; readonly limit: number }; |
| 42 | } |
| 43 | |
| 44 | export interface SourceControlProcessOutput { |
| 45 | readonly exitCode: ChildProcessSpawner.ExitCode; |
| 46 | readonly stdout: string; |
| 47 | readonly stderr: string; |
| 48 | readonly stdoutTruncated: boolean; |
| 49 | readonly stderrTruncated: boolean; |
| 50 | readonly stdoutInvalidUtf8?: boolean; |
| 51 | readonly stderrInvalidUtf8?: boolean; |
| 52 | } |
| 53 | |
| 54 | export type SourceControlGitOutput = Omit< |
| 55 | SourceControlProcessOutput, |
| 56 | "stdoutInvalidUtf8" | "stderrInvalidUtf8" |
| 57 | >; |
| 58 | |
| 59 | export interface SourceControlGitRemoteBranch { |
| 60 | readonly cwd: string; |
| 61 | readonly remoteName: string; |
| 62 | readonly remoteBranch: string; |
| 63 | } |
| 64 | |
| 65 | export class SourceControlHost extends Context.Service< |
| 66 | SourceControlHost, |
| 67 | { |
| 68 | readonly settings: { |
| 69 | /** Read fresh on each call to pick up credential changes. */ |
| 70 | readonly get: Effect.Effect<ServerSettings, ServerSettingsError>; |
| 71 | /** |
| 72 | * Like `get`, with each secret provider reference in one host kind's credentials replaced |
| 73 | * by the secret it names. An unreadable reference stays in place; read the field with |
| 74 | * `requireSettingsSecretLiteral` so it fails rather than passing for an unset credential. |
| 75 | * Only a reader of that kind should call it: resolving can wait on a secret provider. |
| 76 | */ |
| 77 | readonly getWithSecrets: (kind: string) => Effect.Effect<ServerSettings, ServerSettingsError>; |
| 78 | /** For credentials the host obtains itself, such as a sign-in it refreshes. */ |
| 79 | readonly update: ( |
| 80 | patch: ServerSettingsPatch, |
| 81 | ) => Effect.Effect<ServerSettings, ServerSettingsError>; |
| 82 | }; |
| 83 | readonly process: { |
| 84 | readonly run: ( |
| 85 | input: SourceControlProcessInput, |
| 86 | ) => Effect.Effect<SourceControlProcessOutput, VcsError>; |
| 87 | }; |
| 88 | readonly git: { |
| 89 | readonly execute: (input: { |
| 90 | readonly operation: string; |
| 91 | readonly cwd: string; |
| 92 | readonly args: ReadonlyArray<string>; |
| 93 | readonly maxOutputBytes?: number; |
| 94 | readonly appendTruncationMarker?: boolean; |
| 95 | }) => Effect.Effect<SourceControlGitOutput, GitCommandError>; |
| 96 | readonly resolveCommit: (input: { |
| 97 | readonly cwd: string; |
| 98 | readonly revision: string; |
| 99 | }) => Effect.Effect<{ readonly commitSha: string }, GitCommandError>; |
| 100 | /** The outer effect resolves the repository; the inner effect lists its remotes. */ |
| 101 | readonly remotes: ( |
| 102 | cwd: string, |
| 103 | ) => Effect.Effect<Effect.Effect<VcsListRemotesResult, VcsError>, VcsError>; |
| 104 | readonly readConfigValue: ( |
| 105 | cwd: string, |
| 106 | key: string, |
| 107 | ) => Effect.Effect<string | null, GitCommandError>; |
| 108 | readonly resolvePrimaryRemoteName: (cwd: string) => Effect.Effect<string, GitCommandError>; |
| 109 | readonly ensureRemote: (input: { |
| 110 | readonly cwd: string; |
| 111 | readonly preferredName: string; |
| 112 | readonly url: string; |
| 113 | }) => Effect.Effect<string, GitCommandError>; |
| 114 | readonly listLocalBranchNames: (cwd: string) => Effect.Effect<string[], GitCommandError>; |
| 115 | readonly fetchRemoteBranch: ( |
| 116 | input: SourceControlGitRemoteBranch & { readonly localBranch: string }, |
| 117 | ) => Effect.Effect<void, GitCommandError>; |
| 118 | readonly fetchRemoteTrackingBranch: ( |
| 119 | input: SourceControlGitRemoteBranch, |
| 120 | ) => Effect.Effect<void, GitCommandError>; |
| 121 | readonly setBranchUpstream: ( |
| 122 | input: SourceControlGitRemoteBranch & { readonly branch: string }, |
| 123 | ) => Effect.Effect<void, GitCommandError>; |
| 124 | /** |
| 125 | * Writes to .git/config, which every worktree shares, go through these rather than |
| 126 | * `execute`: the host serializes them per repository. |
| 127 | */ |
| 128 | readonly createBranch: (input: { |
| 129 | readonly cwd: string; |
| 130 | readonly branch: string; |
| 131 | readonly startPoint: string; |
| 132 | }) => Effect.Effect<void, GitCommandError>; |
| 133 | readonly writeConfigValue: (input: { |
| 134 | readonly cwd: string; |
| 135 | readonly key: string; |
| 136 | readonly value: string; |
| 137 | }) => Effect.Effect<void, GitCommandError>; |
| 138 | readonly switchRef: ( |
| 139 | input: VcsSwitchRefInput, |
| 140 | ) => Effect.Effect<VcsSwitchRefResult, GitCommandError>; |
| 141 | }; |
| 142 | } |
| 143 | >()("@t3tools/source-control-core/server/SourceControlHost") {} |