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

git.ts

447 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 refresh: Schema.optional(Schema.Boolean),
131 limit: Schema.optional(
132 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
133 ),
134});
135export type VcsListRefsInput = typeof VcsListRefsInput.Type;
136​
137export const VcsCreateWorktreeInput = Schema.Struct({
138 cwd: TrimmedNonEmptyStringSchema,
139 refName: TrimmedNonEmptyStringSchema,
140 newRefName: Schema.optional(TrimmedNonEmptyStringSchema),
141 baseRefName: Schema.optional(TrimmedNonEmptyStringSchema),
142 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
143});
144export type VcsCreateWorktreeInput = typeof VcsCreateWorktreeInput.Type;
145​
146export const GitPullRequestRefInput = Schema.Struct({
147 cwd: TrimmedNonEmptyStringSchema,
148 reference: GitPullRequestReference,
149});
150export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
151​
152export const GitPreparePullRequestThreadInput = Schema.Struct({
153 cwd: TrimmedNonEmptyStringSchema,
154 reference: GitPullRequestReference,
155 mode: GitPreparePullRequestThreadMode,
156 threadId: Schema.optional(ThreadId),
157});
158export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
159​
160export const VcsRemoveWorktreeInput = Schema.Struct({
161 cwd: TrimmedNonEmptyStringSchema,
162 path: TrimmedNonEmptyStringSchema,
163 force: Schema.optional(Schema.Boolean),
164});
165export type VcsRemoveWorktreeInput = typeof VcsRemoveWorktreeInput.Type;
166​
167export const VcsCreateRefInput = Schema.Struct({
168 cwd: TrimmedNonEmptyStringSchema,
169 refName: TrimmedNonEmptyStringSchema,
170 switchRef: Schema.optional(Schema.Boolean),
171});
172export type VcsCreateRefInput = typeof VcsCreateRefInput.Type;
173​
174export const VcsCreateRefResult = Schema.Struct({
175 refName: TrimmedNonEmptyStringSchema,
176});
177export type VcsCreateRefResult = typeof VcsCreateRefResult.Type;
178​
179export const VcsSwitchRefInput = Schema.Struct({
180 cwd: TrimmedNonEmptyStringSchema,
181 refName: TrimmedNonEmptyStringSchema,
182});
183export type VcsSwitchRefInput = typeof VcsSwitchRefInput.Type;
184​
185export const VcsInitInput = Schema.Struct({
186 cwd: TrimmedNonEmptyStringSchema,
187 kind: Schema.optional(VcsDriverKind),
188});
189export type VcsInitInput = typeof VcsInitInput.Type;
190​
191// RPC Results
192​
193const VcsStatusChangeRequest = Schema.Struct({
194 number: PositiveInt,
195 title: TrimmedNonEmptyStringSchema,
196 url: Schema.String,
197 baseRef: TrimmedNonEmptyStringSchema,
198 headRef: TrimmedNonEmptyStringSchema,
199 state: VcsStatusChangeRequestState,
200});
201​
202const VcsStatusLocalShape = {
203 isRepo: Schema.Boolean,
204 sourceControlProvider: Schema.optional(SourceControlProviderInfo),
205 hasPrimaryRemote: Schema.Boolean,
206 isDefaultRef: Schema.Boolean,
207 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
208 hasWorkingTreeChanges: Schema.Boolean,
209 workingTree: Schema.Struct({
210 files: Schema.Array(
211 Schema.Struct({
212 path: TrimmedNonEmptyStringSchema,
213 insertions: NonNegativeInt,
214 deletions: NonNegativeInt,
215 }),
216 ),
217 insertions: NonNegativeInt,
218 deletions: NonNegativeInt,
219 }),
220};
221​
222const VcsStatusRemoteShape = {
223 hasUpstream: Schema.Boolean,
224 aheadCount: NonNegativeInt,
225 behindCount: NonNegativeInt,
226 aheadOfDefaultCount: Schema.optional(NonNegativeInt),
227 pr: Schema.NullOr(VcsStatusChangeRequest),
228};
229​
230export const VcsStatusLocalResult = Schema.Struct(VcsStatusLocalShape);
231export type VcsStatusLocalResult = typeof VcsStatusLocalResult.Type;
232​
233export const VcsStatusRemoteResult = Schema.Struct(VcsStatusRemoteShape);
234export type VcsStatusRemoteResult = typeof VcsStatusRemoteResult.Type;
235​
236export const VcsStatusResult = Schema.Struct({
237 ...VcsStatusLocalShape,
238 ...VcsStatusRemoteShape,
239});
240export type VcsStatusResult = typeof VcsStatusResult.Type;
241​
242export const VcsStatusStreamEvent = Schema.Union([
243 Schema.TaggedStruct("snapshot", {
244 local: VcsStatusLocalResult,
245 remote: Schema.NullOr(VcsStatusRemoteResult),
246 }),
247 Schema.TaggedStruct("localUpdated", {
248 local: VcsStatusLocalResult,
249 }),
250 Schema.TaggedStruct("remoteUpdated", {
251 remote: Schema.NullOr(VcsStatusRemoteResult),
252 }),
253]);
254export type VcsStatusStreamEvent = typeof VcsStatusStreamEvent.Type;
255​
256export const VcsListRefsResult = Schema.Struct({
257 refs: Schema.Array(VcsRef),
258 isRepo: Schema.Boolean,
259 hasPrimaryRemote: Schema.Boolean,
260 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
261 totalCount: NonNegativeInt,
262});
263export type VcsListRefsResult = typeof VcsListRefsResult.Type;
264​
265export const VcsCreateWorktreeResult = Schema.Struct({
266 worktree: VcsWorktree,
267});
268export type VcsCreateWorktreeResult = typeof VcsCreateWorktreeResult.Type;
269​
270export const GitResolvePullRequestResult = Schema.Struct({
271 pullRequest: GitResolvedPullRequest,
272});
273export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
274​
275export const GitPreparePullRequestThreadResult = Schema.Struct({
276 pullRequest: GitResolvedPullRequest,
277 branch: TrimmedNonEmptyStringSchema,
278 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
279});
280export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
281​
282export const VcsSwitchRefResult = Schema.Struct({
283 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
284});
285export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
286​
287export const GitRunStackedActionResult = Schema.Struct({
288 action: GitStackedAction,
289 branch: Schema.Struct({
290 status: GitBranchStepStatus,
291 name: Schema.optional(TrimmedNonEmptyStringSchema),
292 }),
293 commit: Schema.Struct({
294 status: GitCommitStepStatus,
295 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
296 subject: Schema.optional(TrimmedNonEmptyStringSchema),
297 }),
298 push: Schema.Struct({
299 status: GitPushStepStatus,
300 branch: Schema.optional(TrimmedNonEmptyStringSchema),
301 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
302 setUpstream: Schema.optional(Schema.Boolean),
303 }),
304 pr: Schema.Struct({
305 status: GitPrStepStatus,
306 url: Schema.optional(Schema.String),
307 number: Schema.optional(PositiveInt),
308 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
309 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
310 title: Schema.optional(TrimmedNonEmptyStringSchema),
311 }),
312 toast: GitRunStackedActionToast,
313});
314export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
315​
316export const VcsPullResult = Schema.Struct({
317 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
318 refName: TrimmedNonEmptyStringSchema,
319 upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
320});
321export type VcsPullResult = typeof VcsPullResult.Type;
322​
323// RPC / domain errors
324export class GitCommandError extends Schema.TaggedErrorClass<GitCommandError>()("GitCommandError", {
325 operation: Schema.String,
326 command: Schema.String,
327 cwd: Schema.String,
328 argumentCount: Schema.optional(Schema.Number),
329 exitCode: Schema.optional(Schema.Number),
330 stdoutLength: Schema.optional(Schema.Number),
331 stderrLength: Schema.optional(Schema.Number),
332 outputLength: Schema.optional(Schema.Number),
333 detail: Schema.String,
334 cause: Schema.optional(Schema.Defect()),
335}) {
336 override get message(): string {
337 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}`;
338 }
340​
341export class TextGenerationError extends Schema.TaggedErrorClass<TextGenerationError>()(
342 "TextGenerationError",
343 {
344 operation: Schema.String,
345 detail: Schema.String,
346 cause: Schema.optional(Schema.Defect()),
347 },
348) {
349 override get message(): string {
350 return `Text generation failed in ${this.operation}: ${this.detail}`;
351 }
353​
354export class GitManagerError extends Schema.TaggedErrorClass<GitManagerError>()("GitManagerError", {
355 operation: Schema.String,
356 cwd: Schema.String,
357 detail: Schema.String,
358 cause: Schema.optional(Schema.Defect()),
359}) {
360 override get message(): string {
361 return `Git manager failed in ${this.operation}: ${this.detail}`;
362 }
364​
365export class GitPullRequestMaterializationError extends Schema.TaggedErrorClass<GitPullRequestMaterializationError>()(
366 "GitPullRequestMaterializationError",
367 {
368 cwd: TrimmedNonEmptyStringSchema,
369 pullRequestNumber: PositiveInt,
370 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
371 headBranch: TrimmedNonEmptyStringSchema,
372 localBranch: TrimmedNonEmptyStringSchema,
373 cause: Schema.Defect(),
374 },
375) {
376 override get message(): string {
377 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
378 }
380​
381export const GitManagerServiceError = Schema.Union([
382 GitManagerError,
383 GitPullRequestMaterializationError,
384 GitCommandError,
385 SourceControlProviderError,
386 TextGenerationError,
387]);
388export type GitManagerServiceError = typeof GitManagerServiceError.Type;
389​
390const GitActionProgressBase = Schema.Struct({
391 actionId: TrimmedNonEmptyStringSchema,
392 cwd: TrimmedNonEmptyStringSchema,
393 action: GitStackedAction,
394});
395​
396const GitActionStartedEvent = Schema.Struct({
397 ...GitActionProgressBase.fields,
398 kind: Schema.Literal("action_started"),
399 phases: Schema.Array(GitActionProgressPhase),
400});
401const GitActionPhaseStartedEvent = Schema.Struct({
402 ...GitActionProgressBase.fields,
403 kind: Schema.Literal("phase_started"),
404 phase: GitActionProgressPhase,
405 label: TrimmedNonEmptyStringSchema,
406});
407const GitActionHookStartedEvent = Schema.Struct({
408 ...GitActionProgressBase.fields,
409 kind: Schema.Literal("hook_started"),
410 hookName: TrimmedNonEmptyStringSchema,
411});
412const GitActionHookOutputEvent = Schema.Struct({
413 ...GitActionProgressBase.fields,
414 kind: Schema.Literal("hook_output"),
415 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
416 stream: GitActionProgressStream,
417 text: TrimmedNonEmptyStringSchema,
418});
419const GitActionHookFinishedEvent = Schema.Struct({
420 ...GitActionProgressBase.fields,
421 kind: Schema.Literal("hook_finished"),
422 hookName: TrimmedNonEmptyStringSchema,
423 exitCode: Schema.NullOr(Schema.Int),
424 durationMs: Schema.NullOr(NonNegativeInt),
425});
426const GitActionFinishedEvent = Schema.Struct({
427 ...GitActionProgressBase.fields,
428 kind: Schema.Literal("action_finished"),
429 result: GitRunStackedActionResult,
430});
431const GitActionFailedEvent = Schema.Struct({
432 ...GitActionProgressBase.fields,
433 kind: Schema.Literal("action_failed"),
434 phase: Schema.NullOr(GitActionProgressPhase),
435 message: TrimmedNonEmptyStringSchema,
436});
437​
438export const GitActionProgressEvent = Schema.Union([
439 GitActionStartedEvent,
440 GitActionPhaseStartedEvent,
441 GitActionHookStartedEvent,
442 GitActionHookOutputEvent,
443 GitActionHookFinishedEvent,
444 GitActionFinishedEvent,
445 GitActionFailedEvent,
446]);
447export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;