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

git.ts

446 lines15.0 KB

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