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