@devries-cameron20 / t3code Mirror · read-only
Fork 0

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, 03:37 UTC

main
ts 289 lines 8.63 KB
Raw
1 import * as Schema from "effect/Schema";
2 import { NonNegativeInt, TrimmedNonEmptyString } from "./baseSchemas.ts";
3
4 export const VcsDriverKind = Schema.Literals(["git", "jj", "unknown"]);
5 export type VcsDriverKind = typeof VcsDriverKind.Type;
6
7 export const VcsFreshnessSource = Schema.Literals([
8 "live-local",
9 "cached-local",
10 "cached-remote",
11 "explicit-remote",
12 ]);
13 export type VcsFreshnessSource = typeof VcsFreshnessSource.Type;
14
15 export const VcsFreshness = Schema.Struct({
16 source: VcsFreshnessSource,
17 observedAt: Schema.DateTimeUtc,
18 expiresAt: Schema.Option(Schema.DateTimeUtc),
19 });
20 export type VcsFreshness = typeof VcsFreshness.Type;
21
22 export const VcsDriverCapabilities = Schema.Struct({
23 kind: VcsDriverKind,
24 supportsWorktrees: Schema.Boolean,
25 supportsBookmarks: Schema.Boolean,
26 supportsAtomicSnapshot: Schema.Boolean,
27 supportsPushDefaultRemote: Schema.Boolean,
28 ignoreClassifier: Schema.Literals(["native", "git-compatible-fallback"]),
29 });
30 export type VcsDriverCapabilities = typeof VcsDriverCapabilities.Type;
31
32 export const VcsRepositoryIdentity = Schema.Struct({
33 kind: VcsDriverKind,
34 rootPath: TrimmedNonEmptyString,
35 metadataPath: Schema.NullOr(TrimmedNonEmptyString),
36 freshness: VcsFreshness,
37 });
38 export type VcsRepositoryIdentity = typeof VcsRepositoryIdentity.Type;
39
40 export const VcsListWorkspaceFilesResult = Schema.Struct({
41 paths: Schema.Array(TrimmedNonEmptyString),
42 truncated: Schema.Boolean,
43 freshness: VcsFreshness,
44 });
45 export type VcsListWorkspaceFilesResult = typeof VcsListWorkspaceFilesResult.Type;
46
47 export const VcsRemote = Schema.Struct({
48 name: TrimmedNonEmptyString,
49 url: TrimmedNonEmptyString,
50 pushUrl: Schema.Option(TrimmedNonEmptyString),
51 isPrimary: Schema.Boolean,
52 });
53 export type VcsRemote = typeof VcsRemote.Type;
54
55 export const VcsListRemotesResult = Schema.Struct({
56 remotes: Schema.Array(VcsRemote),
57 freshness: VcsFreshness,
58 });
59 export type VcsListRemotesResult = typeof VcsListRemotesResult.Type;
60
61 export interface VcsProcessErrorContext {
62 readonly operation: string;
63 readonly command: string;
64 readonly cwd: string;
65 readonly argumentCount?: number;
66 }
67
68 export interface VcsProcessSpawnFailure {
69 readonly cause: unknown;
70 }
71
72 export interface VcsProcessTimeoutFailure {
73 readonly timeoutMs: number;
74 }
75
76 export const VcsProcessExitFailureKind = Schema.Literals([
77 "authentication",
78 "not-found",
79 "rate-limited",
80 "command-failed",
81 ]);
82 export type VcsProcessExitFailureKind = typeof VcsProcessExitFailureKind.Type;
83
84 export interface VcsProcessExitFailure {
85 readonly exitCode: number;
86 readonly stderr: string;
87 readonly stderrTruncated: boolean;
88 }
89
90 export class VcsProcessSpawnError extends Schema.TaggedError<VcsProcessSpawnError>()(
91 "VcsProcessSpawnError",
92 {
93 operation: Schema.String,
94 command: Schema.String,
95 cwd: Schema.String,
96 argumentCount: Schema.optional(NonNegativeInt),
97 cause: Schema.Defect(),
98 },
99 ) {
100 override get message(): string {
101 return `VCS process failed to spawn in ${this.operation}: ${this.command} (${this.cwd})`;
102 }
103
104 static fromProcessSpawnError(context: VcsProcessErrorContext, error: VcsProcessSpawnFailure) {
105 return new VcsProcessSpawnError({
106 ...context,
107 cause: error.cause,
108 });
109 }
110 }
111
112 export class VcsProcessExitError extends Schema.TaggedError<VcsProcessExitError>()(
113 "VcsProcessExitError",
114 {
115 operation: Schema.String,
116 command: Schema.String,
117 cwd: Schema.String,
118 argumentCount: Schema.optional(NonNegativeInt),
119 exitCode: Schema.Number,
120 detail: Schema.String,
121 failureKind: Schema.optional(VcsProcessExitFailureKind),
122 retryable: Schema.optional(Schema.Boolean),
123 stderrLength: Schema.optional(NonNegativeInt),
124 stderrTruncated: Schema.optional(Schema.Boolean),
125 },
126 ) {
127 override get message(): string {
128 return `VCS process failed in ${this.operation}: ${this.command} (${this.cwd}) exited with ${this.exitCode} - ${this.detail}`;
129 }
130
131 static fromProcessExit(
132 context: VcsProcessErrorContext,
133 error: VcsProcessExitFailure,
134 failureKind: VcsProcessExitFailureKind,
135 options?: { readonly retryable?: boolean; readonly notFoundDetail?: string },
136 ) {
137 const detail =
138 failureKind === "authentication"
139 ? "Authentication failed."
140 : failureKind === "rate-limited"
141 ? "API rate limit exceeded."
142 : failureKind === "not-found"
143 ? (options?.notFoundDetail ?? "VCS resource not found.")
144 : "Process exited with a non-zero status.";
145
146 return new VcsProcessExitError({
147 ...context,
148 exitCode: error.exitCode,
149 detail,
150 failureKind,
151 ...(options?.retryable === true ? { retryable: true } : {}),
152 stderrLength: error.stderr.length,
153 stderrTruncated: error.stderrTruncated,
154 });
155 }
156 }
157
158 export class VcsProcessTimeoutError extends Schema.TaggedError<VcsProcessTimeoutError>()(
159 "VcsProcessTimeoutError",
160 {
161 operation: Schema.String,
162 command: Schema.String,
163 cwd: Schema.String,
164 argumentCount: Schema.optional(NonNegativeInt),
165 timeoutMs: Schema.Number,
166 },
167 ) {
168 override get message(): string {
169 return `VCS process timed out in ${this.operation}: ${this.command} (${this.cwd}) after ${this.timeoutMs}ms`;
170 }
171
172 static fromProcessTimeoutError(context: VcsProcessErrorContext, error: VcsProcessTimeoutFailure) {
173 return new VcsProcessTimeoutError({
174 ...context,
175 timeoutMs: error.timeoutMs,
176 });
177 }
178 }
179
180 const VcsProcessBoundaryErrorFields = {
181 operation: Schema.String,
182 command: Schema.String,
183 cwd: Schema.String,
184 argumentCount: Schema.optional(NonNegativeInt),
185 };
186
187 export class VcsProcessStdinWriteError extends Schema.TaggedError<VcsProcessStdinWriteError>()(
188 "VcsProcessStdinWriteError",
189 {
190 ...VcsProcessBoundaryErrorFields,
191 stdinBytes: NonNegativeInt,
192 cause: Schema.Defect(),
193 },
194 ) {
195 override get message(): string {
196 return `VCS process failed to write ${this.stdinBytes} bytes to stdin in ${this.operation}: ${this.command} (${this.cwd})`;
197 }
198 }
199
200 export class VcsProcessOutputReadError extends Schema.TaggedError<VcsProcessOutputReadError>()(
201 "VcsProcessOutputReadError",
202 {
203 ...VcsProcessBoundaryErrorFields,
204 stream: Schema.Literals(["stdout", "stderr", "exitCode"]),
205 cause: Schema.Defect(),
206 },
207 ) {
208 override get message(): string {
209 return `VCS process failed to read ${this.stream} in ${this.operation}: ${this.command} (${this.cwd})`;
210 }
211 }
212
213 export class VcsProcessOutputLimitError extends Schema.TaggedError<VcsProcessOutputLimitError>()(
214 "VcsProcessOutputLimitError",
215 {
216 ...VcsProcessBoundaryErrorFields,
217 stream: Schema.Literals(["stdout", "stderr"]),
218 maxBytes: NonNegativeInt,
219 observedBytes: NonNegativeInt,
220 },
221 ) {
222 override get message(): string {
223 return `VCS process ${this.stream} produced ${this.observedBytes} bytes in ${this.operation}: ${this.command} (${this.cwd}), exceeding the ${this.maxBytes} byte limit`;
224 }
225 }
226
227 export class VcsProcessMissingExitCodeError extends Schema.TaggedError<VcsProcessMissingExitCodeError>()(
228 "VcsProcessMissingExitCodeError",
229 VcsProcessBoundaryErrorFields,
230 ) {
231 override get message(): string {
232 return `VCS process completed without an exit code in ${this.operation}: ${this.command} (${this.cwd})`;
233 }
234 }
235
236 export class VcsRepositoryDetectionError extends Schema.TaggedError<VcsRepositoryDetectionError>()(
237 "VcsRepositoryDetectionError",
238 {
239 operation: Schema.String,
240 cwd: Schema.String,
241 detail: Schema.String,
242 cause: Schema.optional(Schema.Defect()),
243 },
244 ) {
245 override get message(): string {
246 return `VCS repository detection failed in ${this.operation}: ${this.cwd} - ${this.detail}`;
247 }
248 }
249
250 export class VcsUnsupportedOperationError extends Schema.TaggedError<VcsUnsupportedOperationError>()(
251 "VcsUnsupportedOperationError",
252 {
253 operation: Schema.String,
254 kind: VcsDriverKind,
255 detail: Schema.String,
256 },
257 ) {
258 override get message(): string {
259 return `VCS operation is unsupported for ${this.kind} in ${this.operation}: ${this.detail}`;
260 }
261 }
262
263 export class VcsCheckpointStorageError extends Schema.TaggedError<VcsCheckpointStorageError>()(
264 "VcsCheckpointStorageError",
265 {
266 operation: Schema.String,
267 cwd: Schema.String,
268 stage: Schema.Literals(["isolate", "publish"]),
269 cause: Schema.Defect(),
270 },
271 ) {
272 override get message(): string {
273 return `Could not ${this.stage} checkpoint objects.`;
274 }
275 }
276
277 export const VcsError = Schema.Union([
278 VcsCheckpointStorageError,
279 VcsProcessSpawnError,
280 VcsProcessExitError,
281 VcsProcessTimeoutError,
282 VcsProcessStdinWriteError,
283 VcsProcessOutputReadError,
284 VcsProcessOutputLimitError,
285 VcsProcessMissingExitCodeError,
286 VcsRepositoryDetectionError,
287 VcsUnsupportedOperationError,
288 ]);
289 export type VcsError = typeof VcsError.Type;