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

git.ts

464 lines15.9 KB

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