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

git.ts

434 lines14.3 KB

1import { Schema } from "effect";
2import { NonNegativeInt, PositiveInt, ThreadId, TrimmedNonEmptyString } from "./baseSchemas.ts";
3​
4const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
5const GIT_LIST_BRANCHES_MAX_LIMIT = 200;
6​
7// Domain Types
8​
9export const GitStackedAction = Schema.Literals([
10 "commit",
11 "push",
12 "create_pr",
13 "commit_push",
14 "commit_push_pr",
15]);
16export type GitStackedAction = typeof GitStackedAction.Type;
17export const GitActionProgressPhase = Schema.Literals(["branch", "commit", "push", "pr"]);
18export type GitActionProgressPhase = typeof GitActionProgressPhase.Type;
19export const GitActionProgressKind = Schema.Literals([
20 "action_started",
21 "phase_started",
22 "hook_started",
23 "hook_output",
24 "hook_finished",
25 "action_finished",
26 "action_failed",
27]);
28export type GitActionProgressKind = typeof GitActionProgressKind.Type;
29export const GitActionProgressStream = Schema.Literals(["stdout", "stderr"]);
30export type GitActionProgressStream = typeof GitActionProgressStream.Type;
31const GitCommitStepStatus = Schema.Literals([
32 "created",
33 "skipped_no_changes",
34 "skipped_not_requested",
35]);
36const GitPushStepStatus = Schema.Literals([
37 "pushed",
38 "skipped_not_requested",
39 "skipped_up_to_date",
40]);
41const GitBranchStepStatus = Schema.Literals(["created", "skipped_not_requested"]);
42const GitPrStepStatus = Schema.Literals(["created", "opened_existing", "skipped_not_requested"]);
43const GitStatusPrState = Schema.Literals(["open", "closed", "merged"]);
44const GitPullRequestReference = TrimmedNonEmptyStringSchema;
45const GitPullRequestState = Schema.Literals(["open", "closed", "merged"]);
46const GitPreparePullRequestThreadMode = Schema.Literals(["local", "worktree"]);
47export const GitHostingProviderKind = Schema.Literals(["github", "gitlab", "unknown"]);
48export type GitHostingProviderKind = typeof GitHostingProviderKind.Type;
49export const GitHostingProvider = Schema.Struct({
50 kind: GitHostingProviderKind,
51 name: TrimmedNonEmptyStringSchema,
52 baseUrl: Schema.String,
53});
54export type GitHostingProvider = typeof GitHostingProvider.Type;
55export const GitRunStackedActionToastRunAction = Schema.Struct({
56 kind: GitStackedAction,
57});
58export type GitRunStackedActionToastRunAction = typeof GitRunStackedActionToastRunAction.Type;
59const GitRunStackedActionToastCta = Schema.Union([
60 Schema.Struct({
61 kind: Schema.Literal("none"),
62 }),
63 Schema.Struct({
64 kind: Schema.Literal("open_pr"),
65 label: TrimmedNonEmptyStringSchema,
66 url: Schema.String,
67 }),
68 Schema.Struct({
69 kind: Schema.Literal("run_action"),
70 label: TrimmedNonEmptyStringSchema,
71 action: GitRunStackedActionToastRunAction,
72 }),
73]);
74export type GitRunStackedActionToastCta = typeof GitRunStackedActionToastCta.Type;
75const GitRunStackedActionToast = Schema.Struct({
76 title: TrimmedNonEmptyStringSchema,
77 description: Schema.optional(TrimmedNonEmptyStringSchema),
78 cta: GitRunStackedActionToastCta,
79});
80export type GitRunStackedActionToast = typeof GitRunStackedActionToast.Type;
81​
82export const GitBranch = Schema.Struct({
83 name: TrimmedNonEmptyStringSchema,
84 isRemote: Schema.optional(Schema.Boolean),
85 remoteName: Schema.optional(TrimmedNonEmptyStringSchema),
86 current: Schema.Boolean,
87 isDefault: Schema.Boolean,
88 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
89});
90export type GitBranch = typeof GitBranch.Type;
91​
92const GitWorktree = Schema.Struct({
93 path: TrimmedNonEmptyStringSchema,
94 branch: TrimmedNonEmptyStringSchema,
95});
96const GitResolvedPullRequest = Schema.Struct({
97 number: PositiveInt,
98 title: TrimmedNonEmptyStringSchema,
99 url: Schema.String,
100 baseBranch: TrimmedNonEmptyStringSchema,
101 headBranch: TrimmedNonEmptyStringSchema,
102 state: GitPullRequestState,
103});
104export type GitResolvedPullRequest = typeof GitResolvedPullRequest.Type;
105​
106// RPC Inputs
107​
108export const GitStatusInput = Schema.Struct({
109 cwd: TrimmedNonEmptyStringSchema,
110});
111export type GitStatusInput = typeof GitStatusInput.Type;
112​
113export const GitPullInput = Schema.Struct({
114 cwd: TrimmedNonEmptyStringSchema,
115});
116export type GitPullInput = typeof GitPullInput.Type;
117​
118export const GitRunStackedActionInput = Schema.Struct({
119 actionId: TrimmedNonEmptyStringSchema,
120 cwd: TrimmedNonEmptyStringSchema,
121 action: GitStackedAction,
122 commitMessage: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(10_000))),
123 featureBranch: Schema.optional(Schema.Boolean),
124 filePaths: Schema.optional(
125 Schema.Array(TrimmedNonEmptyStringSchema).check(Schema.isMinLength(1)),
126 ),
127});
128export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
129​
130export const GitListBranchesInput = Schema.Struct({
131 cwd: TrimmedNonEmptyStringSchema,
132 query: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(256))),
133 cursor: Schema.optional(NonNegativeInt),
134 limit: Schema.optional(
135 PositiveInt.check(Schema.isLessThanOrEqualTo(GIT_LIST_BRANCHES_MAX_LIMIT)),
136 ),
137});
138export type GitListBranchesInput = typeof GitListBranchesInput.Type;
139​
140export const GitCreateWorktreeInput = Schema.Struct({
141 cwd: TrimmedNonEmptyStringSchema,
142 branch: TrimmedNonEmptyStringSchema,
143 newBranch: Schema.optional(TrimmedNonEmptyStringSchema),
144 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
145});
146export type GitCreateWorktreeInput = typeof GitCreateWorktreeInput.Type;
147​
148export const GitPullRequestRefInput = Schema.Struct({
149 cwd: TrimmedNonEmptyStringSchema,
150 reference: GitPullRequestReference,
151});
152export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
153​
154export const GitPreparePullRequestThreadInput = Schema.Struct({
155 cwd: TrimmedNonEmptyStringSchema,
156 reference: GitPullRequestReference,
157 mode: GitPreparePullRequestThreadMode,
158 threadId: Schema.optional(ThreadId),
159});
160export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
161​
162export const GitRemoveWorktreeInput = Schema.Struct({
163 cwd: TrimmedNonEmptyStringSchema,
164 path: TrimmedNonEmptyStringSchema,
165 force: Schema.optional(Schema.Boolean),
166});
167export type GitRemoveWorktreeInput = typeof GitRemoveWorktreeInput.Type;
168​
169export const GitCreateBranchInput = Schema.Struct({
170 cwd: TrimmedNonEmptyStringSchema,
171 branch: TrimmedNonEmptyStringSchema,
172 checkout: Schema.optional(Schema.Boolean),
173});
174export type GitCreateBranchInput = typeof GitCreateBranchInput.Type;
175​
176export const GitCreateBranchResult = Schema.Struct({
177 branch: TrimmedNonEmptyStringSchema,
178});
179export type GitCreateBranchResult = typeof GitCreateBranchResult.Type;
180​
181export const GitCheckoutInput = Schema.Struct({
182 cwd: TrimmedNonEmptyStringSchema,
183 branch: TrimmedNonEmptyStringSchema,
184});
185export type GitCheckoutInput = typeof GitCheckoutInput.Type;
186​
187export const GitInitInput = Schema.Struct({
188 cwd: TrimmedNonEmptyStringSchema,
189});
190export type GitInitInput = typeof GitInitInput.Type;
191​
192// RPC Results
193​
194const GitStatusPr = Schema.Struct({
195 number: PositiveInt,
196 title: TrimmedNonEmptyStringSchema,
197 url: Schema.String,
198 baseBranch: TrimmedNonEmptyStringSchema,
199 headBranch: TrimmedNonEmptyStringSchema,
200 state: GitStatusPrState,
201});
202​
203const GitStatusLocalShape = {
204 isRepo: Schema.Boolean,
205 hostingProvider: Schema.optional(GitHostingProvider),
206 hasOriginRemote: Schema.Boolean,
207 isDefaultBranch: Schema.Boolean,
208 branch: Schema.NullOr(TrimmedNonEmptyStringSchema),
209 hasWorkingTreeChanges: Schema.Boolean,
210 workingTree: Schema.Struct({
211 files: Schema.Array(
212 Schema.Struct({
213 path: TrimmedNonEmptyStringSchema,
214 insertions: NonNegativeInt,
215 deletions: NonNegativeInt,
216 }),
217 ),
218 insertions: NonNegativeInt,
219 deletions: NonNegativeInt,
220 }),
221};
222​
223const GitStatusRemoteShape = {
224 hasUpstream: Schema.Boolean,
225 aheadCount: NonNegativeInt,
226 behindCount: NonNegativeInt,
227 pr: Schema.NullOr(GitStatusPr),
228};
229​
230export const GitStatusLocalResult = Schema.Struct(GitStatusLocalShape);
231export type GitStatusLocalResult = typeof GitStatusLocalResult.Type;
232​
233export const GitStatusRemoteResult = Schema.Struct(GitStatusRemoteShape);
234export type GitStatusRemoteResult = typeof GitStatusRemoteResult.Type;
235​
236export const GitStatusResult = Schema.Struct({
237 ...GitStatusLocalShape,
238 ...GitStatusRemoteShape,
239});
240export type GitStatusResult = typeof GitStatusResult.Type;
241​
242export const GitStatusStreamEvent = Schema.Union([
243 Schema.TaggedStruct("snapshot", {
244 local: GitStatusLocalResult,
245 remote: Schema.NullOr(GitStatusRemoteResult),
246 }),
247 Schema.TaggedStruct("localUpdated", {
248 local: GitStatusLocalResult,
249 }),
250 Schema.TaggedStruct("remoteUpdated", {
251 remote: Schema.NullOr(GitStatusRemoteResult),
252 }),
253]);
254export type GitStatusStreamEvent = typeof GitStatusStreamEvent.Type;
255​
256export const GitListBranchesResult = Schema.Struct({
257 branches: Schema.Array(GitBranch),
258 isRepo: Schema.Boolean,
259 hasOriginRemote: Schema.Boolean,
260 nextCursor: NonNegativeInt.pipe(Schema.NullOr),
261 totalCount: NonNegativeInt,
262});
263export type GitListBranchesResult = typeof GitListBranchesResult.Type;
264​
265export const GitCreateWorktreeResult = Schema.Struct({
266 worktree: GitWorktree,
267});
268export type GitCreateWorktreeResult = typeof GitCreateWorktreeResult.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 GitCheckoutResult = Schema.Struct({
283 branch: Schema.NullOr(TrimmedNonEmptyStringSchema),
284});
285export type GitCheckoutResult = typeof GitCheckoutResult.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 GitPullResult = Schema.Struct({
317 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
318 branch: TrimmedNonEmptyStringSchema,
319 upstreamBranch: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
320});
321export type GitPullResult = typeof GitPullResult.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 detail: Schema.String,
329 cause: Schema.optional(Schema.Defect),
330}) {
331 override get message(): string {
332 return `Git command failed in ${this.operation}: ${this.command} (${this.cwd}) - ${this.detail}`;
333 }
335​
336export class GitHubCliError extends Schema.TaggedErrorClass<GitHubCliError>()("GitHubCliError", {
337 operation: Schema.String,
338 detail: Schema.String,
339 cause: Schema.optional(Schema.Defect),
340}) {
341 override get message(): string {
342 return `GitHub CLI failed in ${this.operation}: ${this.detail}`;
343 }
345​
346export class TextGenerationError extends Schema.TaggedErrorClass<TextGenerationError>()(
347 "TextGenerationError",
348 {
349 operation: Schema.String,
350 detail: Schema.String,
351 cause: Schema.optional(Schema.Defect),
352 },
353) {
354 override get message(): string {
355 return `Text generation failed in ${this.operation}: ${this.detail}`;
356 }
358​
359export class GitManagerError extends Schema.TaggedErrorClass<GitManagerError>()("GitManagerError", {
360 operation: Schema.String,
361 detail: Schema.String,
362 cause: Schema.optional(Schema.Defect),
363}) {
364 override get message(): string {
365 return `Git manager failed in ${this.operation}: ${this.detail}`;
366 }
368​
369export const GitManagerServiceError = Schema.Union([
370 GitManagerError,
371 GitCommandError,
372 GitHubCliError,
373 TextGenerationError,
374]);
375export type GitManagerServiceError = typeof GitManagerServiceError.Type;
376​
377const GitActionProgressBase = Schema.Struct({
378 actionId: TrimmedNonEmptyStringSchema,
379 cwd: TrimmedNonEmptyStringSchema,
380 action: GitStackedAction,
381});
382​
383const GitActionStartedEvent = Schema.Struct({
384 ...GitActionProgressBase.fields,
385 kind: Schema.Literal("action_started"),
386 phases: Schema.Array(GitActionProgressPhase),
387});
388const GitActionPhaseStartedEvent = Schema.Struct({
389 ...GitActionProgressBase.fields,
390 kind: Schema.Literal("phase_started"),
391 phase: GitActionProgressPhase,
392 label: TrimmedNonEmptyStringSchema,
393});
394const GitActionHookStartedEvent = Schema.Struct({
395 ...GitActionProgressBase.fields,
396 kind: Schema.Literal("hook_started"),
397 hookName: TrimmedNonEmptyStringSchema,
398});
399const GitActionHookOutputEvent = Schema.Struct({
400 ...GitActionProgressBase.fields,
401 kind: Schema.Literal("hook_output"),
402 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
403 stream: GitActionProgressStream,
404 text: TrimmedNonEmptyStringSchema,
405});
406const GitActionHookFinishedEvent = Schema.Struct({
407 ...GitActionProgressBase.fields,
408 kind: Schema.Literal("hook_finished"),
409 hookName: TrimmedNonEmptyStringSchema,
410 exitCode: Schema.NullOr(Schema.Int),
411 durationMs: Schema.NullOr(NonNegativeInt),
412});
413const GitActionFinishedEvent = Schema.Struct({
414 ...GitActionProgressBase.fields,
415 kind: Schema.Literal("action_finished"),
416 result: GitRunStackedActionResult,
417});
418const GitActionFailedEvent = Schema.Struct({
419 ...GitActionProgressBase.fields,
420 kind: Schema.Literal("action_failed"),
421 phase: Schema.NullOr(GitActionProgressPhase),
422 message: TrimmedNonEmptyStringSchema,
423});
424​
425export const GitActionProgressEvent = Schema.Union([
426 GitActionStartedEvent,
427 GitActionPhaseStartedEvent,
428 GitActionHookStartedEvent,
429 GitActionHookOutputEvent,
430 GitActionHookFinishedEvent,
431 GitActionFinishedEvent,
432 GitActionFailedEvent,
433]);
434export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;