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

git.ts

374 lines12.2 KB

1import { Schema } from "effect";
2import { NonNegativeInt, PositiveInt, TrimmedNonEmptyString } from "./baseSchemas";
3​
4const TrimmedNonEmptyStringSchema = TrimmedNonEmptyString;
5​
6// Domain Types
7​
8export const GitStackedAction = Schema.Literals([
9 "commit",
10 "push",
11 "create_pr",
12 "commit_push",
13 "commit_push_pr",
14]);
15export type GitStackedAction = typeof GitStackedAction.Type;
16export const GitActionProgressPhase = Schema.Literals(["branch", "commit", "push", "pr"]);
17export type GitActionProgressPhase = typeof GitActionProgressPhase.Type;
18export const GitActionProgressKind = Schema.Literals([
19 "action_started",
20 "phase_started",
21 "hook_started",
22 "hook_output",
23 "hook_finished",
24 "action_finished",
25 "action_failed",
26]);
27export type GitActionProgressKind = typeof GitActionProgressKind.Type;
28export const GitActionProgressStream = Schema.Literals(["stdout", "stderr"]);
29export type GitActionProgressStream = typeof GitActionProgressStream.Type;
30const GitCommitStepStatus = Schema.Literals([
31 "created",
32 "skipped_no_changes",
33 "skipped_not_requested",
34]);
35const GitPushStepStatus = Schema.Literals([
36 "pushed",
37 "skipped_not_requested",
38 "skipped_up_to_date",
39]);
40const GitBranchStepStatus = Schema.Literals(["created", "skipped_not_requested"]);
41const GitPrStepStatus = Schema.Literals(["created", "opened_existing", "skipped_not_requested"]);
42const GitStatusPrState = Schema.Literals(["open", "closed", "merged"]);
43const GitPullRequestReference = TrimmedNonEmptyStringSchema;
44const GitPullRequestState = Schema.Literals(["open", "closed", "merged"]);
45const GitPreparePullRequestThreadMode = Schema.Literals(["local", "worktree"]);
46export const GitRunStackedActionToastRunAction = Schema.Struct({
47 kind: GitStackedAction,
48});
49export type GitRunStackedActionToastRunAction = typeof GitRunStackedActionToastRunAction.Type;
50const GitRunStackedActionToastCta = Schema.Union([
51 Schema.Struct({
52 kind: Schema.Literal("none"),
53 }),
54 Schema.Struct({
55 kind: Schema.Literal("open_pr"),
56 label: TrimmedNonEmptyStringSchema,
57 url: Schema.String,
58 }),
59 Schema.Struct({
60 kind: Schema.Literal("run_action"),
61 label: TrimmedNonEmptyStringSchema,
62 action: GitRunStackedActionToastRunAction,
63 }),
64]);
65export type GitRunStackedActionToastCta = typeof GitRunStackedActionToastCta.Type;
66const GitRunStackedActionToast = Schema.Struct({
67 title: TrimmedNonEmptyStringSchema,
68 description: Schema.optional(TrimmedNonEmptyStringSchema),
69 cta: GitRunStackedActionToastCta,
70});
71export type GitRunStackedActionToast = typeof GitRunStackedActionToast.Type;
72​
73export const GitBranch = Schema.Struct({
74 name: TrimmedNonEmptyStringSchema,
75 isRemote: Schema.optional(Schema.Boolean),
76 remoteName: Schema.optional(TrimmedNonEmptyStringSchema),
77 current: Schema.Boolean,
78 isDefault: Schema.Boolean,
79 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
80});
81export type GitBranch = typeof GitBranch.Type;
82​
83const GitWorktree = Schema.Struct({
84 path: TrimmedNonEmptyStringSchema,
85 branch: TrimmedNonEmptyStringSchema,
86});
87const GitResolvedPullRequest = Schema.Struct({
88 number: PositiveInt,
89 title: TrimmedNonEmptyStringSchema,
90 url: Schema.String,
91 baseBranch: TrimmedNonEmptyStringSchema,
92 headBranch: TrimmedNonEmptyStringSchema,
93 state: GitPullRequestState,
94});
95export type GitResolvedPullRequest = typeof GitResolvedPullRequest.Type;
96​
97// RPC Inputs
98​
99export const GitStatusInput = Schema.Struct({
100 cwd: TrimmedNonEmptyStringSchema,
101});
102export type GitStatusInput = typeof GitStatusInput.Type;
103​
104export const GitPullInput = Schema.Struct({
105 cwd: TrimmedNonEmptyStringSchema,
106});
107export type GitPullInput = typeof GitPullInput.Type;
108​
109export const GitRunStackedActionInput = Schema.Struct({
110 actionId: TrimmedNonEmptyStringSchema,
111 cwd: TrimmedNonEmptyStringSchema,
112 action: GitStackedAction,
113 commitMessage: Schema.optional(TrimmedNonEmptyStringSchema.check(Schema.isMaxLength(10_000))),
114 featureBranch: Schema.optional(Schema.Boolean),
115 filePaths: Schema.optional(
116 Schema.Array(TrimmedNonEmptyStringSchema).check(Schema.isMinLength(1)),
117 ),
118});
119export type GitRunStackedActionInput = typeof GitRunStackedActionInput.Type;
120​
121export const GitListBranchesInput = Schema.Struct({
122 cwd: TrimmedNonEmptyStringSchema,
123});
124export type GitListBranchesInput = typeof GitListBranchesInput.Type;
125​
126export const GitCreateWorktreeInput = Schema.Struct({
127 cwd: TrimmedNonEmptyStringSchema,
128 branch: TrimmedNonEmptyStringSchema,
129 newBranch: Schema.optional(TrimmedNonEmptyStringSchema),
130 path: Schema.NullOr(TrimmedNonEmptyStringSchema),
131});
132export type GitCreateWorktreeInput = typeof GitCreateWorktreeInput.Type;
133​
134export const GitPullRequestRefInput = Schema.Struct({
135 cwd: TrimmedNonEmptyStringSchema,
136 reference: GitPullRequestReference,
137});
138export type GitPullRequestRefInput = typeof GitPullRequestRefInput.Type;
139​
140export const GitPreparePullRequestThreadInput = Schema.Struct({
141 cwd: TrimmedNonEmptyStringSchema,
142 reference: GitPullRequestReference,
143 mode: GitPreparePullRequestThreadMode,
144});
145export type GitPreparePullRequestThreadInput = typeof GitPreparePullRequestThreadInput.Type;
146​
147export const GitRemoveWorktreeInput = Schema.Struct({
148 cwd: TrimmedNonEmptyStringSchema,
149 path: TrimmedNonEmptyStringSchema,
150 force: Schema.optional(Schema.Boolean),
151});
152export type GitRemoveWorktreeInput = typeof GitRemoveWorktreeInput.Type;
153​
154export const GitCreateBranchInput = Schema.Struct({
155 cwd: TrimmedNonEmptyStringSchema,
156 branch: TrimmedNonEmptyStringSchema,
157});
158export type GitCreateBranchInput = typeof GitCreateBranchInput.Type;
159​
160export const GitCheckoutInput = Schema.Struct({
161 cwd: TrimmedNonEmptyStringSchema,
162 branch: TrimmedNonEmptyStringSchema,
163});
164export type GitCheckoutInput = typeof GitCheckoutInput.Type;
165​
166export const GitInitInput = Schema.Struct({
167 cwd: TrimmedNonEmptyStringSchema,
168});
169export type GitInitInput = typeof GitInitInput.Type;
170​
171// RPC Results
172​
173const GitStatusPr = Schema.Struct({
174 number: PositiveInt,
175 title: TrimmedNonEmptyStringSchema,
176 url: Schema.String,
177 baseBranch: TrimmedNonEmptyStringSchema,
178 headBranch: TrimmedNonEmptyStringSchema,
179 state: GitStatusPrState,
180});
181​
182export const GitStatusResult = Schema.Struct({
183 branch: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
184 hasWorkingTreeChanges: Schema.Boolean,
185 workingTree: Schema.Struct({
186 files: Schema.Array(
187 Schema.Struct({
188 path: TrimmedNonEmptyStringSchema,
189 insertions: NonNegativeInt,
190 deletions: NonNegativeInt,
191 }),
192 ),
193 insertions: NonNegativeInt,
194 deletions: NonNegativeInt,
195 }),
196 hasUpstream: Schema.Boolean,
197 aheadCount: NonNegativeInt,
198 behindCount: NonNegativeInt,
199 pr: Schema.NullOr(GitStatusPr),
200});
201export type GitStatusResult = typeof GitStatusResult.Type;
202​
203export const GitListBranchesResult = Schema.Struct({
204 branches: Schema.Array(GitBranch),
205 isRepo: Schema.Boolean,
206 hasOriginRemote: Schema.Boolean,
207});
208export type GitListBranchesResult = typeof GitListBranchesResult.Type;
209​
210export const GitCreateWorktreeResult = Schema.Struct({
211 worktree: GitWorktree,
212});
213export type GitCreateWorktreeResult = typeof GitCreateWorktreeResult.Type;
214​
215export const GitResolvePullRequestResult = Schema.Struct({
216 pullRequest: GitResolvedPullRequest,
217});
218export type GitResolvePullRequestResult = typeof GitResolvePullRequestResult.Type;
219​
220export const GitPreparePullRequestThreadResult = Schema.Struct({
221 pullRequest: GitResolvedPullRequest,
222 branch: TrimmedNonEmptyStringSchema,
223 worktreePath: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
224});
225export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
226​
227export const GitRunStackedActionResult = Schema.Struct({
228 action: GitStackedAction,
229 branch: Schema.Struct({
230 status: GitBranchStepStatus,
231 name: Schema.optional(TrimmedNonEmptyStringSchema),
232 }),
233 commit: Schema.Struct({
234 status: GitCommitStepStatus,
235 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
236 subject: Schema.optional(TrimmedNonEmptyStringSchema),
237 }),
238 push: Schema.Struct({
239 status: GitPushStepStatus,
240 branch: Schema.optional(TrimmedNonEmptyStringSchema),
241 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
242 setUpstream: Schema.optional(Schema.Boolean),
243 }),
244 pr: Schema.Struct({
245 status: GitPrStepStatus,
246 url: Schema.optional(Schema.String),
247 number: Schema.optional(PositiveInt),
248 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
249 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
250 title: Schema.optional(TrimmedNonEmptyStringSchema),
251 }),
252 toast: GitRunStackedActionToast,
253});
254export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
255​
256export const GitPullResult = Schema.Struct({
257 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
258 branch: TrimmedNonEmptyStringSchema,
259 upstreamBranch: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
260});
261export type GitPullResult = typeof GitPullResult.Type;
262​
263// RPC / domain errors
264export class GitCommandError extends Schema.TaggedErrorClass<GitCommandError>()("GitCommandError", {
265 operation: Schema.String,
266 command: Schema.String,
267 cwd: Schema.String,
268 detail: Schema.String,
269 cause: Schema.optional(Schema.Defect),
270}) {
271 override get message(): string {
272 return `Git command failed in ${this.operation}: ${this.command} (${this.cwd}) - ${this.detail}`;
273 }
275​
276export class GitHubCliError extends Schema.TaggedErrorClass<GitHubCliError>()("GitHubCliError", {
277 operation: Schema.String,
278 detail: Schema.String,
279 cause: Schema.optional(Schema.Defect),
280}) {
281 override get message(): string {
282 return `GitHub CLI failed in ${this.operation}: ${this.detail}`;
283 }
285​
286export class TextGenerationError extends Schema.TaggedErrorClass<TextGenerationError>()(
287 "TextGenerationError",
288 {
289 operation: Schema.String,
290 detail: Schema.String,
291 cause: Schema.optional(Schema.Defect),
292 },
293) {
294 override get message(): string {
295 return `Text generation failed in ${this.operation}: ${this.detail}`;
296 }
298​
299export class GitManagerError extends Schema.TaggedErrorClass<GitManagerError>()("GitManagerError", {
300 operation: Schema.String,
301 detail: Schema.String,
302 cause: Schema.optional(Schema.Defect),
303}) {
304 override get message(): string {
305 return `Git manager failed in ${this.operation}: ${this.detail}`;
306 }
308​
309export const GitManagerServiceError = Schema.Union([
310 GitManagerError,
311 GitCommandError,
312 GitHubCliError,
313 TextGenerationError,
314]);
315export type GitManagerServiceError = typeof GitManagerServiceError.Type;
316​
317const GitActionProgressBase = Schema.Struct({
318 actionId: TrimmedNonEmptyStringSchema,
319 cwd: TrimmedNonEmptyStringSchema,
320 action: GitStackedAction,
321});
322​
323const GitActionStartedEvent = Schema.Struct({
324 ...GitActionProgressBase.fields,
325 kind: Schema.Literal("action_started"),
326 phases: Schema.Array(GitActionProgressPhase),
327});
328const GitActionPhaseStartedEvent = Schema.Struct({
329 ...GitActionProgressBase.fields,
330 kind: Schema.Literal("phase_started"),
331 phase: GitActionProgressPhase,
332 label: TrimmedNonEmptyStringSchema,
333});
334const GitActionHookStartedEvent = Schema.Struct({
335 ...GitActionProgressBase.fields,
336 kind: Schema.Literal("hook_started"),
337 hookName: TrimmedNonEmptyStringSchema,
338});
339const GitActionHookOutputEvent = Schema.Struct({
340 ...GitActionProgressBase.fields,
341 kind: Schema.Literal("hook_output"),
342 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
343 stream: GitActionProgressStream,
344 text: TrimmedNonEmptyStringSchema,
345});
346const GitActionHookFinishedEvent = Schema.Struct({
347 ...GitActionProgressBase.fields,
348 kind: Schema.Literal("hook_finished"),
349 hookName: TrimmedNonEmptyStringSchema,
350 exitCode: Schema.NullOr(Schema.Int),
351 durationMs: Schema.NullOr(NonNegativeInt),
352});
353const GitActionFinishedEvent = Schema.Struct({
354 ...GitActionProgressBase.fields,
355 kind: Schema.Literal("action_finished"),
356 result: GitRunStackedActionResult,
357});
358const GitActionFailedEvent = Schema.Struct({
359 ...GitActionProgressBase.fields,
360 kind: Schema.Literal("action_failed"),
361 phase: Schema.NullOr(GitActionProgressPhase),
362 message: TrimmedNonEmptyStringSchema,
363});
364​
365export const GitActionProgressEvent = Schema.Union([
366 GitActionStartedEvent,
367 GitActionPhaseStartedEvent,
368 GitActionHookStartedEvent,
369 GitActionHookOutputEvent,
370 GitActionHookFinishedEvent,
371 GitActionFinishedEvent,
372 GitActionFailedEvent,
373]);
374export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;