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

git.ts

471 lines15.9 KB

1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import {
4 NonNegativeInt,
5 PositiveInt,
6 ProjectId,
7 ThreadId,
8 TrimmedNonEmptyString,
9} from "./baseSchemas.ts";
10import { SourceControlProviderError, SourceControlProviderInfo } from "./sourceControl.ts";
11import { VcsDriverKind } from "./vcs.ts";
12​
13const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
14const GIT_LIST_BRANCHES_MAX_LIMIT = 200;
15​
16// Domain Types
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 current: Schema.Boolean,
88 isDefault: Schema.Boolean,
89 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
90});
91export type VcsRef = typeof VcsRef.Type;
92​
93const VcsWorktree = Schema.Struct({
94 path: TrimmedNonEmptyStringSchema,
95 refName: TrimmedNonEmptyStringSchema,
96});
97const GitResolvedPullRequest = Schema.Struct({
98 number: PositiveInt,
99 title: TrimmedNonEmptyStringSchema,
100 url: Schema.String,
101 baseBranch: TrimmedNonEmptyStringSchema,
102 headBranch: TrimmedNonEmptyStringSchema,
103 state: GitPullRequestState,
104});
105export type GitResolvedPullRequest = typeof GitResolvedPullRequest.Type;
106​
107// RPC Inputs
108​
109export const VcsStatusInput = Schema.Struct({
110 cwd: TrimmedNonEmptyStringSchema,
111});
112export type VcsStatusInput = typeof VcsStatusInput.Type;
113​
114export const VcsPullInput = Schema.Struct({
115 cwd: TrimmedNonEmptyStringSchema,
116});
117export type VcsPullInput = typeof VcsPullInput.Type;
118​
119export const GitRunStackedActionInput = Schema.Struct({
120 actionId: TrimmedNonEmptyStringSchema,
121 cwd: TrimmedNonEmptyStringSchema,
122 action: GitStackedAction,
123 commitMessage: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(10_000))),
124 featureBranch: Schema.optional(Schema.Boolean),
125 filePaths: Schema.optional(
126 Schema.Array(TrimmedNonEmptyStringSchema).check(Schema.isMinLength(1)),
127 ),
128 /** The thread the action runs beside; a pull request it creates is linked to it. */
129 threadId: Schema.optional(ThreadId),
130 projectId: Schema.optional(ProjectId),
131});
132export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
133​
134export const VcsListRefsInput = Schema.Struct({
135 cwd: TrimmedNonEmptyStringSchema,
136 query: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(256))),
137 cursor: Schema.optional(NonNegativeInt),
138 includeMatchingRemoteRefs: Schema.optional(Schema.Boolean),
139 refKind: Schema.optional(Schema.Literals(["all", "local", "remote"])),
140 refresh: Schema.optional(Schema.Boolean),
141 limit: Schema.optional(
142 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
143 ),
144});
145export type VcsListRefsInput = typeof VcsListRefsInput.Type;
146​
147export const VcsCreateWorktreeInput = Schema.Struct({
148 cwd: TrimmedNonEmptyStringSchema,
149 refName: TrimmedNonEmptyStringSchema,
150 newRefName: Schema.optional(TrimmedNonEmptyStringSchema),
151 baseRefName: Schema.optional(TrimmedNonEmptyStringSchema),
152 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
153});
154export type VcsCreateWorktreeInput = typeof VcsCreateWorktreeInput.Type;
155​
156export const GitPullRequestRefInput = Schema.Struct({
157 cwd: TrimmedNonEmptyStringSchema,
158 reference: GitPullRequestReference,
159});
160export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
161​
162export const GitPreparePullRequestThreadInput = Schema.Struct({
163 cwd: TrimmedNonEmptyStringSchema,
164 reference: GitPullRequestReference,
165 mode: GitPreparePullRequestThreadMode,
166 threadId: Schema.optional(ThreadId),
167});
168export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
169​
170export const VcsRemoveWorktreeInput = Schema.Struct({
171 cwd: TrimmedNonEmptyStringSchema,
172 path: TrimmedNonEmptyStringSchema,
173 force: Schema.optional(Schema.Boolean),
174});
175export type VcsRemoveWorktreeInput = typeof VcsRemoveWorktreeInput.Type;
176​
177export const VcsCreateRefInput = Schema.Struct({
178 cwd: TrimmedNonEmptyStringSchema,
179 refName: TrimmedNonEmptyStringSchema,
180 switchRef: Schema.optional(Schema.Boolean),
181});
182export type VcsCreateRefInput = typeof VcsCreateRefInput.Type;
183​
184export const VcsCreateRefResult = Schema.Struct({
185 refName: TrimmedNonEmptyStringSchema,
186});
187export type VcsCreateRefResult = typeof VcsCreateRefResult.Type;
188​
189export const VcsSwitchRefInput = Schema.Struct({
190 cwd: TrimmedNonEmptyStringSchema,
191 refName: TrimmedNonEmptyStringSchema,
192});
193export type VcsSwitchRefInput = typeof VcsSwitchRefInput.Type;
194​
195export const VcsInitInput = Schema.Struct({
196 cwd: TrimmedNonEmptyStringSchema,
197 kind: Schema.optional(VcsDriverKind),
198});
199export type VcsInitInput = typeof VcsInitInput.Type;
200​
201// RPC Results
202​
203const VcsStatusChangeRequest = Schema.Struct({
204 number: PositiveInt,
205 title: TrimmedNonEmptyStringSchema,
206 url: Schema.String,
207 baseRef: TrimmedNonEmptyStringSchema,
208 headRef: TrimmedNonEmptyStringSchema,
209 state: VcsStatusChangeRequestState,
210 /** Optional for compatibility with older servers and providers. */
211 isDraft: Schema.optional(Schema.Boolean),
212 /**
213 * Last provider-side activity (ISO), including comments and metadata edits.
214 * This is not the time a change request closed or merged. Optional for old
215 * servers and providers whose lookups do not report it.
216 */
217 updatedAt: Schema.optional(Schema.NullOr(Schema.String)),
218});
219​
220const VcsStatusLocalShape = {
221 isRepo: Schema.Boolean,
222 sourceControlProvider: Schema.optional(SourceControlProviderInfo),
223 hasPrimaryRemote: Schema.Boolean,
224 isDefaultRef: Schema.Boolean,
225 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
226 hasWorkingTreeChanges: Schema.Boolean,
227 workingTree: Schema.Struct({
228 files: Schema.Array(
229 Schema.Struct({
230 path: TrimmedNonEmptyStringSchema,
231 insertions: NonNegativeInt,
232 deletions: NonNegativeInt,
233 }),
234 ),
235 insertions: NonNegativeInt,
236 deletions: NonNegativeInt,
237 }),
238};
239​
240const VcsStatusRemoteShape = {
241 hasUpstream: Schema.Boolean,
242 aheadCount: NonNegativeInt,
243 behindCount: NonNegativeInt,
244 aheadOfDefaultCount: Schema.optional(NonNegativeInt),
245 pr: Schema.NullOr(VcsStatusChangeRequest),
246};
247​
248export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
249export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
250​
251export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
252export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
253​
254export const VcsStatusResult = Schema.Struct({
255 ...VcsStatusLocalShape,
256 ...VcsStatusRemoteShape,
257});
258export type VcsStatusResult = typeof VcsStatusResult.Type;
259​
260export const VcsStatusStreamEvent = Schema.Union([
261 Schema.TaggedStruct("snapshot", {
262 local: VcsStatusLocalResult,
263 remote: Schema.NullOr(VcsStatusRemoteResult),
264 }),
265 Schema.TaggedStruct("localUpdated", {
266 local: VcsStatusLocalResult,
267 }),
268 Schema.TaggedStruct("remoteUpdated", {
269 remote: Schema.NullOr(VcsStatusRemoteResult),
270 }),
271]);
272export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
273​
274export const VcsListRefsResult = Schema.Struct({
275 refs: Schema.Array(VcsRef),
276 isRepo: Schema.Boolean,
277 hasPrimaryRemote: Schema.Boolean,
278 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
279 totalCount: NonNegativeInt,
280});
281export type VcsListRefsResult = typeof VcsListRefsResult.Type;
282​
283export const VcsCreateWorktreeResult = Schema.Struct({
284 worktree: VcsWorktree,
285});
286export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
287​
288export const GitResolvePullRequestResult = Schema.Struct({
289 pullRequest: GitResolvedPullRequest,
290});
291export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
292​
293export const GitPreparePullRequestThreadResult = Schema.Struct({
294 pullRequest: GitResolvedPullRequest,
295 branch: TrimmedNonEmptyStringSchema,
296 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
297 /**
298 * False when the checkout could not be brought to the pull request head — a reused worktree
299 * holding local commits or uncommitted changes keeps its own state, so the code being handed
300 * over is older than the pull request.
301 */
302 isOnPullRequestHead: Schema.Boolean.pipe(Schema.withDecodingDefaultKey(Effect.succeed(true))),
303});
304export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
305​
306export const VcsSwitchRefResult = Schema.Struct({
307 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
308});
309export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
310​
311export const GitRunStackedActionResult = Schema.Struct({
312 action: GitStackedAction,
313 branch: Schema.Struct({
314 status: GitBranchStepStatus,
315 name: Schema.optional(TrimmedNonEmptyStringSchema),
316 }),
317 commit: Schema.Struct({
318 status: GitCommitStepStatus,
319 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
320 subject: Schema.optional(TrimmedNonEmptyStringSchema),
321 }),
322 push: Schema.Struct({
323 status: GitPushStepStatus,
324 branch: Schema.optional(TrimmedNonEmptyStringSchema),
325 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
326 setUpstream: Schema.optional(Schema.Boolean),
327 }),
328 pr: Schema.Struct({
329 status: GitPrStepStatus,
330 url: Schema.optional(Schema.String),
331 number: Schema.optional(PositiveInt),
332 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
333 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
334 title: Schema.optional(TrimmedNonEmptyStringSchema),
335 }),
336 toast: GitRunStackedActionToast,
337});
338export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
339​
340export const VcsPullResult = Schema.Struct({
341 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
342 refName: TrimmedNonEmptyStringSchema,
343 upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
344});
345export type VcsPullResult = typeof VcsPullResult.Type;
346​
347// RPC / domain errors
348export class GitCommandError extends Schema.TaggedError<GitCommandError>()("GitCommandError", {
349 operation: Schema.String,
350 command: Schema.String,
351 cwd: Schema.String,
352 argumentCount: Schema.optional(Schema.Number),
353 exitCode: Schema.optional(Schema.Number),
354 stdoutLength: Schema.optional(Schema.Number),
355 stderrLength: Schema.optional(Schema.Number),
356 outputLength: Schema.optional(Schema.Number),
357 detail: Schema.String,
358 cause: Schema.optional(Schema.Defect()),
359}) {
360 override get message(): string {
361 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}`;
362 }
364​
365export class TextGenerationError extends Schema.TaggedError<TextGenerationError>()(
366 "TextGenerationError",
367 {
368 operation: Schema.String,
369 detail: Schema.String,
370 cause: Schema.optional(Schema.Defect()),
371 },
372) {
373 override get message(): string {
374 return `Text generation failed in ${this.operation}: ${this.detail}`;
375 }
377​
378export class GitManagerError extends Schema.TaggedError<GitManagerError>()("GitManagerError", {
379 operation: Schema.String,
380 cwd: Schema.String,
381 detail: Schema.String,
382 cause: Schema.optional(Schema.Defect()),
383}) {
384 override get message(): string {
385 return `Git manager failed in ${this.operation}: ${this.detail}`;
386 }
388​
389export class GitPullRequestMaterializationError extends Schema.TaggedError<GitPullRequestMaterializationError>()(
390 "GitPullRequestMaterializationError",
391 {
392 cwd: TrimmedNonEmptyStringSchema,
393 pullRequestNumber: PositiveInt,
394 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
395 headBranch: TrimmedNonEmptyStringSchema,
396 localBranch: TrimmedNonEmptyStringSchema,
397 cause: Schema.Defect(),
398 },
399) {
400 override get message(): string {
401 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
402 }
404​
405export const GitManagerServiceError = Schema.Union([
406 GitManagerError,
407 GitPullRequestMaterializationError,
408 GitCommandError,
409 SourceControlProviderError,
410 TextGenerationError,
411]);
412export type GitManagerServiceError = typeof GitManagerServiceError.Type;
413​
414const GitActionProgressBase = Schema.Struct({
415 actionId: TrimmedNonEmptyStringSchema,
416 cwd: TrimmedNonEmptyStringSchema,
417 action: GitStackedAction,
418});
419​
420const GitActionStartedEvent = Schema.Struct({
421 ...GitActionProgressBase.fields,
422 kind: Schema.Literal("action_started"),
423 phases: Schema.Array(GitActionProgressPhase),
424});
425const GitActionPhaseStartedEvent = Schema.Struct({
426 ...GitActionProgressBase.fields,
427 kind: Schema.Literal("phase_started"),
428 phase: GitActionProgressPhase,
429 label: TrimmedNonEmptyStringSchema,
430});
431const GitActionHookStartedEvent = Schema.Struct({
432 ...GitActionProgressBase.fields,
433 kind: Schema.Literal("hook_started"),
434 hookName: TrimmedNonEmptyStringSchema,
435});
436const GitActionHookOutputEvent = Schema.Struct({
437 ...GitActionProgressBase.fields,
438 kind: Schema.Literal("hook_output"),
439 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
440 stream: GitActionProgressStream,
441 text: TrimmedNonEmptyStringSchema,
442});
443const GitActionHookFinishedEvent = Schema.Struct({
444 ...GitActionProgressBase.fields,
445 kind: Schema.Literal("hook_finished"),
446 hookName: TrimmedNonEmptyStringSchema,
447 exitCode: Schema.NullOr(Schema.Int),
448 durationMs: Schema.NullOr(NonNegativeInt),
449});
450const GitActionFinishedEvent = Schema.Struct({
451 ...GitActionProgressBase.fields,
452 kind: Schema.Literal("action_finished"),
453 result: GitRunStackedActionResult,
454});
455const GitActionFailedEvent = Schema.Struct({
456 ...GitActionProgressBase.fields,
457 kind: Schema.Literal("action_failed"),
458 phase: Schema.NullOr(GitActionProgressPhase),
459 message: TrimmedNonEmptyStringSchema,
460});
461​
462export const GitActionProgressEvent = Schema.Union([
463 GitActionStartedEvent,
464 GitActionPhaseStartedEvent,
465 GitActionHookStartedEvent,
466 GitActionHookOutputEvent,
467 GitActionHookFinishedEvent,
468 GitActionFinishedEvent,
469 GitActionFailedEvent,
470]);
471export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;