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

projectThreadCreationValidation.ts

67 lines2.1 KB

1import { EnvironmentId, ProjectId } from "@t3tools/contracts";
2import * as Schema from "effect/Schema";
3​
4export class ProjectThreadTaskRequiredError extends Schema.TaggedError<ProjectThreadTaskRequiredError>()(
5 "ProjectThreadTaskRequiredError",
6 {
7 environmentId: EnvironmentId,
8 projectId: ProjectId,
9 environmentMode: Schema.Literals(["local", "worktree"]),
10 },
11) {
12 override get message(): string {
13 return "Enter a task before starting the thread.";
14 }
16​
17export class ProjectThreadBaseBranchRequiredError extends Schema.TaggedError<ProjectThreadBaseBranchRequiredError>()(
18 "ProjectThreadBaseBranchRequiredError",
19 {
20 environmentId: EnvironmentId,
21 projectId: ProjectId,
22 },
23) {
24 override get message(): string {
25 return "Select a base branch before creating a worktree.";
26 }
28​
29export const ProjectThreadCreationValidationError = Schema.Union([
30 ProjectThreadTaskRequiredError,
31 ProjectThreadBaseBranchRequiredError,
32]);
33export type ProjectThreadCreationValidationError = typeof ProjectThreadCreationValidationError.Type;
34​
35export function resolveProjectThreadCreationBranch(input: {
36 readonly workspaceMode: "local" | "worktree";
37 readonly selectedBranch: string | null;
38 readonly currentCheckoutBranch: string | null;
39}): string | null {
40 if (input.selectedBranch !== null) {
41 return input.selectedBranch;
42 }
43 return input.workspaceMode === "local" ? input.currentCheckoutBranch : null;
45​
46export function validateProjectThreadCreation(input: {
47 readonly environmentId: EnvironmentId;
48 readonly projectId: ProjectId;
49 readonly environmentMode: "local" | "worktree";
50 readonly branch: string | null;
51 readonly initialMessageText: string;
52}): ProjectThreadCreationValidationError | null {
53 if (input.initialMessageText.trim().length === 0) {
54 return new ProjectThreadTaskRequiredError({
55 environmentId: input.environmentId,
56 projectId: input.projectId,
57 environmentMode: input.environmentMode,
58 });
59 }
60 if (input.environmentMode === "worktree" && !input.branch) {
61 return new ProjectThreadBaseBranchRequiredError({
62 environmentId: input.environmentId,
63 projectId: input.projectId,
64 });
65 }
66 return null;