Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

git.ts

498 lines17.2 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import {
4 ForwardCompatibleOptional,
5 NonNegativeInt,
6 PositiveInt,
7 ProjectId,
8 ThreadId,
9 TrimmedNonEmptyString,
10} from "./baseSchemas.ts";
11import { SourceControlProviderError, SourceControlProviderInfo } from "./sourceControl.ts";
12import { VcsDriverKind } from "./vcs.ts";
13​
14const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
15const GitFilePath = Schema.String.check(Schema.isNonEmpty());
16const GIT_LIST_BRANCHES_MAX_LIMIT = 200;
17​
18export const GitStackedAction = Schema.Literals([
19 "commit",
20 "push",
21 "create_pr",
22 "commit_push",
23 "commit_push_pr",
24]);
25export type GitStackedAction = typeof GitStackedAction.Type;
26export const GitActionProgressPhase = Schema.Literals(["branch", "commit", "push", "pr"]);
27export type GitActionProgressPhase = typeof GitActionProgressPhase.Type;
28export const GitActionProgressKind = Schema.Literals([
29 "action_started",
30 "phase_started",
31 "hook_started",
32 "hook_output",
33 "hook_finished",
34 "action_finished",
35 "action_failed",
36]);
37export type GitActionProgressKind = typeof GitActionProgressKind.Type;
38export const GitActionProgressStream = Schema.Literals(["stdout", "stderr"]);
39export type GitActionProgressStream = typeof GitActionProgressStream.Type;
40const GitCommitStepStatus = Schema.Literals([
41 "created",
42 "skipped_no_changes",
43 "skipped_not_requested",
44]);
45const GitPushStepStatus = Schema.Literals([
46 "pushed",
47 "skipped_not_requested",
48 "skipped_up_to_date",
49]);
50const GitBranchStepStatus = Schema.Literals(["created", "skipped_not_requested"]);
51const GitPrStepStatus = Schema.Literals(["created", "opened_existing", "skipped_not_requested"]);
52const VcsStatusChangeRequestState = Schema.Literals(["open", "closed", "merged"]);
53const GitPullRequestReference = TrimmedNonEmptyStringSchema;
54const GitPullRequestState = Schema.Literals(["open", "closed", "merged"]);
55const GitPreparePullRequestThreadMode = Schema.Literals(["local", "worktree"]);
56export const GitRunStackedActionToastRunAction = Schema.Struct({
57 kind: GitStackedAction,
58});
59export type GitRunStackedActionToastRunAction = typeof GitRunStackedActionToastRunAction.Type;
60const GitRunStackedActionToastCta = Schema.Union([
61 Schema.Struct({
62 kind: Schema.Literal("none"),
63 }),
64 Schema.Struct({
65 kind: Schema.Literal("open_pr"),
66 label: TrimmedNonEmptyStringSchema,
67 url: Schema.String,
68 }),
69 Schema.Struct({
70 kind: Schema.Literal("run_action"),
71 label: TrimmedNonEmptyStringSchema,
72 action: GitRunStackedActionToastRunAction,
73 }),
74]);
75export type GitRunStackedActionToastCta = typeof GitRunStackedActionToastCta.Type;
76const GitRunStackedActionToast = Schema.Struct({
77 title: TrimmedNonEmptyStringSchema,
78 description: Schema.optional(TrimmedNonEmptyStringSchema),
79 cta: GitRunStackedActionToastCta,
80});
81export type GitRunStackedActionToast = typeof GitRunStackedActionToast.Type;
82​
83export const VcsRef = Schema.Struct({
84 name: TrimmedNonEmptyStringSchema,
85 isRemote: Schema.optional(Schema.Boolean),
86 remoteName: Schema.optional(TrimmedNonEmptyStringSchema),
87 // Full Git ref, for example refs/remotes/origin/feature.
88 upstreamRef: Schema.optional(TrimmedNonEmptyStringSchema),
89 current: Schema.Boolean,
90 isDefault: Schema.Boolean,
91 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
92});
93export type VcsRef = typeof VcsRef.Type;
94​
95const VcsWorktree = Schema.Struct({
96 path: TrimmedNonEmptyStringSchema,
97 refName: TrimmedNonEmptyStringSchema,
98});
99const GitResolvedPullRequest = Schema.Struct({
100 number: PositiveInt,
101 title: TrimmedNonEmptyStringSchema,
102 url: Schema.String,
103 baseBranch: TrimmedNonEmptyStringSchema,
104 headBranch: TrimmedNonEmptyStringSchema,
105 state: GitPullRequestState,
106});
107export type GitResolvedPullRequest = typeof GitResolvedPullRequest.Type;
108​
109export const VcsStatusInput = Schema.Struct({
110 cwd: TrimmedNonEmptyStringSchema,
111});
112export type VcsStatusInput = typeof VcsStatusInput.Type;
113​
114export const VcsBranchStatus = Schema.Struct({ refName: Schema.NullOr(Schema.String) });
115export type VcsBranchStatus = typeof VcsBranchStatus.Type;
116​
117export const VcsStatusSubscriptionInput = Schema.Struct({
118 ...VcsStatusInput.fields,
119 /** Passive observers receive cached remote status without retaining its refresh loop. */
120 includeRemote: Schema.optional(Schema.Boolean),
121});
122export type VcsStatusSubscriptionInput = typeof VcsStatusSubscriptionInput.Type;
123​
124export const VcsPullInput = Schema.Struct({
125 cwd: TrimmedNonEmptyStringSchema,
126});
127export type VcsPullInput = typeof VcsPullInput.Type;
128​
129export const GitRunStackedActionInput = Schema.Struct({
130 actionId: TrimmedNonEmptyStringSchema,
131 cwd: TrimmedNonEmptyStringSchema,
132 action: GitStackedAction,
133 commitMessage: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(10_000))),
134 featureBranch: Schema.optional(Schema.Boolean),
135 filePaths: Schema.optional(Schema.Array(GitFilePath).check(Schema.isMinLength(1))),
136 threadId: Schema.optional(ThreadId),
137 projectId: Schema.optional(ProjectId),
138});
139export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
140​
141export const VcsListRefsInput = Schema.Struct({
142 cwd: TrimmedNonEmptyStringSchema,
143 query: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(256))),
144 cursor: Schema.optional(NonNegativeInt),
145 includeMatchingRemoteRefs: Schema.optional(Schema.Boolean),
146 refKind: Schema.optional(Schema.Literals(["all", "local", "remote"])),
147 refresh: Schema.optional(Schema.Boolean),
148 limit: Schema.optional(
149 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
150 ),
151});
152export type VcsListRefsInput = typeof VcsListRefsInput.Type;
153​
154export const VcsCreateWorktreeInput = Schema.Struct({
155 cwd: TrimmedNonEmptyStringSchema,
156 refName: TrimmedNonEmptyStringSchema,
157 newRefName: Schema.optional(TrimmedNonEmptyStringSchema),
158 baseRefName: Schema.optional(TrimmedNonEmptyStringSchema),
159 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
160});
161export type VcsCreateWorktreeInput = typeof VcsCreateWorktreeInput.Type;
162​
163export const GitPullRequestRefInput = Schema.Struct({
164 cwd: TrimmedNonEmptyStringSchema,
165 reference: GitPullRequestReference,
166});
167export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
168​
169export const GitPreparePullRequestThreadInput = Schema.Struct({
170 cwd: TrimmedNonEmptyStringSchema,
171 reference: GitPullRequestReference,
172 mode: GitPreparePullRequestThreadMode,
173 threadId: Schema.optional(ThreadId),
174});
175export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
176​
177export const VcsRemoveWorktreeInput = Schema.Struct({
178 cwd: TrimmedNonEmptyStringSchema,
179 path: TrimmedNonEmptyStringSchema,
180 force: Schema.optional(Schema.Boolean),
181});
182export type VcsRemoveWorktreeInput = typeof VcsRemoveWorktreeInput.Type;
183​
184export const VcsCreateRefInput = Schema.Struct({
185 cwd: TrimmedNonEmptyStringSchema,
186 refName: TrimmedNonEmptyStringSchema,
187 switchRef: Schema.optional(Schema.Boolean),
188});
189export type VcsCreateRefInput = typeof VcsCreateRefInput.Type;
190​
191export const VcsCreateRefResult = Schema.Struct({
192 refName: TrimmedNonEmptyStringSchema,
193});
194export type VcsCreateRefResult = typeof VcsCreateRefResult.Type;
195​
196export const VcsSwitchRefInput = Schema.Struct({
197 cwd: TrimmedNonEmptyStringSchema,
198 refName: TrimmedNonEmptyStringSchema,
199});
200export type VcsSwitchRefInput = typeof VcsSwitchRefInput.Type;
201​
202export const VcsInitInput = Schema.Struct({
203 cwd: TrimmedNonEmptyStringSchema,
204 kind: Schema.optional(VcsDriverKind),
205});
206export type VcsInitInput = typeof VcsInitInput.Type;
207​
208const VcsStatusChangeRequest = Schema.Struct({
209 number: PositiveInt,
210 title: TrimmedNonEmptyStringSchema,
211 url: Schema.String,
212 baseRef: TrimmedNonEmptyStringSchema,
213 headRef: TrimmedNonEmptyStringSchema,
214 state: VcsStatusChangeRequestState,
215 isDraft: Schema.optional(Schema.Boolean),
216 updatedAt: Schema.optional(Schema.NullOr(Schema.String)),
217});
218​
219const VcsStatusLocalShape = {
220 isRepo: Schema.Boolean,
221 sourceControlProvider: Schema.optional(SourceControlProviderInfo),
222 hasPrimaryRemote: Schema.Boolean,
223 isDefaultRef: Schema.Boolean,
224 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
225 hasWorkingTreeChanges: Schema.Boolean,
226 workingTree: Schema.Struct({
227 // Absolute worktree root on the environment that every files[].path is
228 // relative to. Absent when the server could not resolve it.
229 rootPath: Schema.optional(GitFilePath),
230 files: Schema.Array(
231 Schema.Struct({
232 path: GitFilePath,
233 insertions: NonNegativeInt,
234 deletions: NonNegativeInt,
235 }),
236 ),
237 insertions: NonNegativeInt,
238 deletions: NonNegativeInt,
239 }),
240 branchChanges: Schema.optional(
241 Schema.Struct({
242 baseRef: Schema.NullOr(TrimmedNonEmptyStringSchema),
243 insertions: NonNegativeInt,
244 deletions: NonNegativeInt,
245 }),
246 ),
247};
248​
249const VcsStatusRemoteShape = {
250 hasUpstream: Schema.Boolean,
251 aheadCount: NonNegativeInt,
252 behindCount: NonNegativeInt,
253 aheadOfDefaultCount: Schema.optional(NonNegativeInt),
254 pr: Schema.NullOr(VcsStatusChangeRequest),
255};
256​
257export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
258export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
259​
260export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
261export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
262​
263export const VcsStatusResult = Schema.Struct({
264 ...VcsStatusLocalShape,
265 ...VcsStatusRemoteShape,
266});
267export type VcsStatusResult = typeof VcsStatusResult.Type;
268​
269export const VcsStatusStreamEvent = Schema.Union([
270 Schema.TaggedStruct("snapshot", {
271 local: VcsStatusLocalResult,
272 remote: Schema.NullOr(VcsStatusRemoteResult),
273 }),
274 Schema.TaggedStruct("localUpdated", {
275 local: VcsStatusLocalResult,
276 }),
277 Schema.TaggedStruct("remoteUpdated", {
278 remote: Schema.NullOr(VcsStatusRemoteResult),
279 }),
280]);
281export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
282​
283export const VcsListRefsResult = Schema.Struct({
284 isAtRepositoryRoot: Schema.optional(Schema.Boolean),
285 refs: Schema.Array(VcsRef),
286 isRepo: Schema.Boolean,
287 hasPrimaryRemote: Schema.Boolean,
288 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
289 totalCount: NonNegativeInt,
290});
291export type VcsListRefsResult = typeof VcsListRefsResult.Type;
292​
293export const VcsCreateWorktreeResult = Schema.Struct({
294 worktree: VcsWorktree,
295});
296export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
297​
298export const GitResolvePullRequestResult = Schema.Struct({
299 pullRequest: GitResolvedPullRequest,
300});
301export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
302​
303export const GitPreparePullRequestThreadResult = Schema.Struct({
304 pullRequest: GitResolvedPullRequest,
305 branch: TrimmedNonEmptyStringSchema,
306 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
307 /** False when local commits or edits prevented a reused worktree from reaching the PR head. */
308 isOnPullRequestHead: Schema.Boolean.pipe(Schema.withDecodingDefaultKey(Effect.succeed(true))),
309});
310export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
311​
312export const VcsSwitchRefResult = Schema.Struct({
313 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
314});
315export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
316​
317export const GitRunStackedActionResult = Schema.Struct({
318 action: GitStackedAction,
319 branch: Schema.Struct({
320 status: GitBranchStepStatus,
321 name: Schema.optional(TrimmedNonEmptyStringSchema),
322 }),
323 commit: Schema.Struct({
324 status: GitCommitStepStatus,
325 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
326 subject: Schema.optional(TrimmedNonEmptyStringSchema),
327 }),
328 push: Schema.Struct({
329 status: GitPushStepStatus,
330 branch: Schema.optional(TrimmedNonEmptyStringSchema),
331 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
332 setUpstream: Schema.optional(Schema.Boolean),
333 }),
334 pr: Schema.Struct({
335 status: GitPrStepStatus,
336 url: Schema.optional(Schema.String),
337 number: Schema.optional(PositiveInt),
338 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
339 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
340 title: Schema.optional(TrimmedNonEmptyStringSchema),
341 }),
342 toast: GitRunStackedActionToast,
343});
344export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
345​
346export const VcsPullResult = Schema.Struct({
347 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
348 refName: TrimmedNonEmptyStringSchema,
349 upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
350});
351export type VcsPullResult = typeof VcsPullResult.Type;
352​
353// Git stderr can echo credentials from argv and remote URLs. Only a fixed
354// reason reaches the error and its diagnostic suffix; newer, unknown reasons
355// decode as absent through GitCommandError.
356export const GitCommandFailureReason = Schema.Literals([
357 "authentication_failed",
358 "branch_already_exists",
359 "branch_checked_out_in_worktree",
360 "config_locked",
361 "host_key_unverified",
362 "not_a_repository",
363 "path_already_exists",
364 "push_rejected_by_remote",
365 "push_rejected_non_fast_forward",
366 "ref_name_conflict",
367 "ref_not_found",
368 "remote_unreachable",
369 "tag_would_be_clobbered",
370]);
371export type GitCommandFailureReason = typeof GitCommandFailureReason.Type;
372​
373export class GitCommandError extends Schema.TaggedError<GitCommandError>()("GitCommandError", {
374 operation: Schema.String,
375 command: Schema.String,
376 cwd: Schema.String,
377 argumentCount: Schema.optional(Schema.Number),
378 exitCode: Schema.optional(Schema.Number),
379 stdoutLength: Schema.optional(Schema.Number),
380 stderrLength: Schema.optional(Schema.Number),
381 outputLength: Schema.optional(Schema.Number),
382 reason: ForwardCompatibleOptional(GitCommandFailureReason),
383 detail: Schema.String,
384 cause: Schema.optional(Schema.Defect()),
385}) {
386 override get message(): string {
387 const reason = this.reason === undefined ? "" : ` (${this.reason})`;
388 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}${reason}`;
389 }
391​
392export class TextGenerationError extends Schema.TaggedError<TextGenerationError>()(
393 "TextGenerationError",
394 {
395 operation: Schema.String,
396 detail: Schema.String,
397 cause: Schema.optional(Schema.Defect()),
398 },
399) {
400 override get message(): string {
401 return `Text generation failed in ${this.operation}: ${this.detail}`;
402 }
404​
405export class GitManagerError extends Schema.TaggedError<GitManagerError>()("GitManagerError", {
406 operation: Schema.String,
407 cwd: Schema.String,
408 detail: Schema.String,
409 cause: Schema.optional(Schema.Defect()),
410}) {
411 override get message(): string {
412 return `Git manager failed in ${this.operation}: ${this.detail}`;
413 }
415​
416export class GitPullRequestMaterializationError extends Schema.TaggedError<GitPullRequestMaterializationError>()(
417 "GitPullRequestMaterializationError",
418 {
419 cwd: TrimmedNonEmptyStringSchema,
420 pullRequestNumber: PositiveInt,
421 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
422 headBranch: TrimmedNonEmptyStringSchema,
423 localBranch: TrimmedNonEmptyStringSchema,
424 cause: Schema.Defect(),
425 },
426) {
427 override get message(): string {
428 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
429 }
431​
432export const GitManagerServiceError = Schema.Union([
433 GitManagerError,
434 GitPullRequestMaterializationError,
435 GitCommandError,
436 SourceControlProviderError,
437 TextGenerationError,
438]);
439export type GitManagerServiceError = typeof GitManagerServiceError.Type;
440​
441const GitActionProgressBase = Schema.Struct({
442 actionId: TrimmedNonEmptyStringSchema,
443 cwd: TrimmedNonEmptyStringSchema,
444 action: GitStackedAction,
445});
446​
447const GitActionStartedEvent = Schema.Struct({
448 ...GitActionProgressBase.fields,
449 kind: Schema.Literal("action_started"),
450 phases: Schema.Array(GitActionProgressPhase),
451});
452const GitActionPhaseStartedEvent = Schema.Struct({
453 ...GitActionProgressBase.fields,
454 kind: Schema.Literal("phase_started"),
455 phase: GitActionProgressPhase,
456 label: TrimmedNonEmptyStringSchema,
457});
458const GitActionHookStartedEvent = Schema.Struct({
459 ...GitActionProgressBase.fields,
460 kind: Schema.Literal("hook_started"),
461 hookName: TrimmedNonEmptyStringSchema,
462});
463const GitActionHookOutputEvent = Schema.Struct({
464 ...GitActionProgressBase.fields,
465 kind: Schema.Literal("hook_output"),
466 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
467 stream: GitActionProgressStream,
468 text: TrimmedNonEmptyStringSchema,
469});
470const GitActionHookFinishedEvent = Schema.Struct({
471 ...GitActionProgressBase.fields,
472 kind: Schema.Literal("hook_finished"),
473 hookName: TrimmedNonEmptyStringSchema,
474 exitCode: Schema.NullOr(Schema.Int),
475 durationMs: Schema.NullOr(NonNegativeInt),
476});
477const GitActionFinishedEvent = Schema.Struct({
478 ...GitActionProgressBase.fields,
479 kind: Schema.Literal("action_finished"),
480 result: GitRunStackedActionResult,
481});
482const GitActionFailedEvent = Schema.Struct({
483 ...GitActionProgressBase.fields,
484 kind: Schema.Literal("action_failed"),
485 phase: Schema.NullOr(GitActionProgressPhase),
486 message: TrimmedNonEmptyStringSchema,
487});
488​
489export const GitActionProgressEvent = Schema.Union([
490 GitActionStartedEvent,
491 GitActionPhaseStartedEvent,
492 GitActionHookStartedEvent,
493 GitActionHookOutputEvent,
494 GitActionHookFinishedEvent,
495 GitActionFinishedEvent,
496 GitActionFailedEvent,
497]);
498export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;