Open History to see the last commit
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 /**
280 * False when the checkout could not be brought to the pull request head — a reused worktree
281 * holding local commits or uncommitted changes keeps its own state, so the code being handed
282 * over is older than the pull request.
283 */
284 isOnPullRequestHead: Schema.Boolean,
285});
286export type GitPreparePullRequestThreadResult = typeof GitPreparePullRequestThreadResult.Type;
287
288export const VcsSwitchRefResult = Schema.Struct({
289 refName: Schema.NullOr(TrimmedNonEmptyStringSchema),
290});
291export type VcsSwitchRefResult = typeof VcsSwitchRefResult.Type;
292
293export const GitRunStackedActionResult = Schema.Struct({
294 action: GitStackedAction,
295 branch: Schema.Struct({
296 status: GitBranchStepStatus,
297 name: Schema.optional(TrimmedNonEmptyStringSchema),
298 }),
299 commit: Schema.Struct({
300 status: GitCommitStepStatus,
301 commitSha: Schema.optional(TrimmedNonEmptyStringSchema),
302 subject: Schema.optional(TrimmedNonEmptyStringSchema),
303 }),
304 push: Schema.Struct({
305 status: GitPushStepStatus,
306 branch: Schema.optional(TrimmedNonEmptyStringSchema),
307 upstreamBranch: Schema.optional(TrimmedNonEmptyStringSchema),
308 setUpstream: Schema.optional(Schema.Boolean),
309 }),
310 pr: Schema.Struct({
311 status: GitPrStepStatus,
312 url: Schema.optional(Schema.String),
313 number: Schema.optional(PositiveInt),
314 baseBranch: Schema.optional(TrimmedNonEmptyStringSchema),
315 headBranch: Schema.optional(TrimmedNonEmptyStringSchema),
316 title: Schema.optional(TrimmedNonEmptyStringSchema),
317 }),
318 toast: GitRunStackedActionToast,
319});
320export type GitRunStackedActionResult = typeof GitRunStackedActionResult.Type;
321
322export const VcsPullResult = Schema.Struct({
323 status: Schema.Literals(["pulled", "skipped_up_to_date"]),
324 refName: TrimmedNonEmptyStringSchema,
325 upstreamRef: TrimmedNonEmptyStringSchema.pipe(Schema.NullOr),
326});
327export type VcsPullResult = typeof VcsPullResult.Type;
328
329// RPC / domain errors
330export class GitCommandError extends Schema.TaggedErrorClass<GitCommandError>()("GitCommandError", {
331 operation: Schema.String,
332 command: Schema.String,
333 cwd: Schema.String,
334 argumentCount: Schema.optional(Schema.Number),
335 exitCode: Schema.optional(Schema.Number),
336 stdoutLength: Schema.optional(Schema.Number),
337 stderrLength: Schema.optional(Schema.Number),
338 outputLength: Schema.optional(Schema.Number),
339 detail: Schema.String,
340 cause: Schema.optional(Schema.Defect()),
341}) {
342 override get message(): string {
343 return `Git command failed in ${this.operation} (${this.cwd}): ${this.detail}`;
344 }
345}
346
347export class TextGenerationError extends Schema.TaggedErrorClass<TextGenerationError>()(
348 "TextGenerationError",
349 {
350 operation: Schema.String,
351 detail: Schema.String,
352 cause: Schema.optional(Schema.Defect()),
353 },
354) {
355 override get message(): string {
356 return `Text generation failed in ${this.operation}: ${this.detail}`;
357 }
358}
359
360export class GitManagerError extends Schema.TaggedErrorClass<GitManagerError>()("GitManagerError", {
361 operation: Schema.String,
362 cwd: Schema.String,
363 detail: Schema.String,
364 cause: Schema.optional(Schema.Defect()),
365}) {
366 override get message(): string {
367 return `Git manager failed in ${this.operation}: ${this.detail}`;
368 }
369}
370
371export class GitPullRequestMaterializationError extends Schema.TaggedErrorClass<GitPullRequestMaterializationError>()(
372 "GitPullRequestMaterializationError",
373 {
374 cwd: TrimmedNonEmptyStringSchema,
375 pullRequestNumber: PositiveInt,
376 headRepository: Schema.NullOr(TrimmedNonEmptyStringSchema),
377 headBranch: TrimmedNonEmptyStringSchema,
378 localBranch: TrimmedNonEmptyStringSchema,
379 cause: Schema.Defect(),
380 },
381) {
382 override get message(): string {
383 return `Failed to materialize pull request #${this.pullRequestNumber} branch ${this.headBranch} as ${this.localBranch}.`;
384 }
385}
386
387export const GitManagerServiceError = Schema.Union([
388 GitManagerError,
389 GitPullRequestMaterializationError,
390 GitCommandError,
391 SourceControlProviderError,
392 TextGenerationError,
393]);
394export type GitManagerServiceError = typeof GitManagerServiceError.Type;
395
396const GitActionProgressBase = Schema.Struct({
397 actionId: TrimmedNonEmptyStringSchema,
398 cwd: TrimmedNonEmptyStringSchema,
399 action: GitStackedAction,
400});
401
402const GitActionStartedEvent = Schema.Struct({
403 ...GitActionProgressBase.fields,
404 kind: Schema.Literal("action_started"),
405 phases: Schema.Array(GitActionProgressPhase),
406});
407const GitActionPhaseStartedEvent = Schema.Struct({
408 ...GitActionProgressBase.fields,
409 kind: Schema.Literal("phase_started"),
410 phase: GitActionProgressPhase,
411 label: TrimmedNonEmptyStringSchema,
412});
413const GitActionHookStartedEvent = Schema.Struct({
414 ...GitActionProgressBase.fields,
415 kind: Schema.Literal("hook_started"),
416 hookName: TrimmedNonEmptyStringSchema,
417});
418const GitActionHookOutputEvent = Schema.Struct({
419 ...GitActionProgressBase.fields,
420 kind: Schema.Literal("hook_output"),
421 hookName: Schema.NullOr(TrimmedNonEmptyStringSchema),
422 stream: GitActionProgressStream,
423 text: TrimmedNonEmptyStringSchema,
424});
425const GitActionHookFinishedEvent = Schema.Struct({
426 ...GitActionProgressBase.fields,
427 kind: Schema.Literal("hook_finished"),
428 hookName: TrimmedNonEmptyStringSchema,
429 exitCode: Schema.NullOr(Schema.Int),
430 durationMs: Schema.NullOr(NonNegativeInt),
431});
432const GitActionFinishedEvent = Schema.Struct({
433 ...GitActionProgressBase.fields,
434 kind: Schema.Literal("action_finished"),
435 result: GitRunStackedActionResult,
436});
437const GitActionFailedEvent = Schema.Struct({
438 ...GitActionProgressBase.fields,
439 kind: Schema.Literal("action_failed"),
440 phase: Schema.NullOr(GitActionProgressPhase),
441 message: TrimmedNonEmptyStringSchema,
442});
443
444export const GitActionProgressEvent = Schema.Union([
445 GitActionStartedEvent,
446 GitActionPhaseStartedEvent,
447 GitActionHookStartedEvent,
448 GitActionHookOutputEvent,
449 GitActionHookFinishedEvent,
450 GitActionFinishedEvent,
451 GitActionFailedEvent,
452]);
453export type GitActionProgressEvent = typeof GitActionProgressEvent.Type;