Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { EnvironmentId, ProjectId } from "@t3tools/contracts";
import * as Schema from "effect/Schema";

export class ProjectThreadTaskRequiredError extends Schema.TaggedErrorClass<ProjectThreadTaskRequiredError>()(
"ProjectThreadTaskRequiredError",
{
environmentId: EnvironmentId,
projectId: ProjectId,
environmentMode: Schema.Literals(["local", "worktree"]),
},
) {
override get message(): string {
return "Enter a task before starting the thread.";
}
}

export class ProjectThreadBaseBranchRequiredError extends Schema.TaggedErrorClass<ProjectThreadBaseBranchRequiredError>()(
"ProjectThreadBaseBranchRequiredError",
{
environmentId: EnvironmentId,
projectId: ProjectId,
},
) {
override get message(): string {
return "Select a base branch before creating a worktree.";
}
}

export const ProjectThreadCreationValidationError = Schema.Union([
ProjectThreadTaskRequiredError,
ProjectThreadBaseBranchRequiredError,
]);
export type ProjectThreadCreationValidationError = typeof ProjectThreadCreationValidationError.Type;

export function validateProjectThreadCreation(input: {
readonly environmentId: EnvironmentId;
readonly projectId: ProjectId;
readonly environmentMode: "local" | "worktree";
readonly branch: string | null;
readonly initialMessageText: string;
}): ProjectThreadCreationValidationError | null {
if (input.initialMessageText.trim().length === 0) {
return new ProjectThreadTaskRequiredError({
environmentId: input.environmentId,
projectId: input.projectId,
environmentMode: input.environmentMode,
});
}
if (input.environmentMode === "worktree" && !input.branch) {
return new ProjectThreadBaseBranchRequiredError({
environmentId: input.environmentId,
projectId: input.projectId,
});
}
return null;
}
20 changes: 11 additions & 9 deletions apps/mobile/src/features/threads/use-project-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { makeTurnCommandMetadata } from "../../lib/commandMetadata";
import { uuidv4 } from "../../lib/uuid";
import { useAtomCommand } from "../../state/use-atom-command";
import { setPendingConnectionError } from "../../state/use-remote-environment-registry";
import { validateProjectThreadCreation } from "./projectThreadCreationValidation";

function deriveThreadTitleFromPrompt(value: string): string {
const trimmed = value.trim();
Expand Down Expand Up @@ -52,15 +53,16 @@ export function useCreateProjectThread() {
const initialMessageText = input.initialMessageText.trim();
const nextTitle = deriveThreadTitleFromPrompt(input.initialMessageText);

if (initialMessageText.length === 0) {
const error = new Error("Enter a task before starting the thread.");
setPendingConnectionError(error.message);
return AsyncResult.failure(Cause.fail(error));
}
if (input.envMode === "worktree" && !input.branch) {
const error = new Error("Select a base branch before creating a worktree.");
setPendingConnectionError(error.message);
return AsyncResult.failure(Cause.fail(error));
const validationError = validateProjectThreadCreation({
environmentId: input.project.environmentId,
projectId: input.project.id,
environmentMode: input.envMode,
branch: input.branch,
initialMessageText,
});
if (validationError !== null) {
setPendingConnectionError(validationError.message);
return AsyncResult.failure(Cause.fail(validationError));
}

const isWorktree = input.envMode === "worktree";
Expand Down
Loading