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

use-project-actions.ts

95 lines3.9 KB

1import { useCallback } from "react";
2​
3import { scopeThreadRef } from "@t3tools/client-runtime/environment";
4import { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
5import { mapAtomCommandResult } from "@t3tools/client-runtime/state/runtime";
6import {
7 ThreadId,
8 type ModelSelection,
9 type ProviderInteractionMode,
10 type RuntimeMode,
11} from "@t3tools/contracts";
12import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git";
13import * as Cause from "effect/Cause";
14import { AsyncResult } from "effect/unstable/reactivity";
15​
16import { threadEnvironment } from "../../state/threads";
17import type { DraftComposerImageAttachment } from "../../lib/composerImages";
18import { makeTurnCommandMetadata, type TurnCommandMetadata } from "../../lib/commandMetadata";
19import { buildProjectThreadStartTurnInput } from "../../lib/projectThreadStartTurn";
20import { randomHex } from "../../lib/uuid";
21import { useAtomCommand } from "../../state/use-atom-command";
22import { setPendingConnectionError } from "../../state/use-remote-environment-registry";
23import { validateProjectThreadCreation } from "./projectThreadCreationValidation";
24​
25export function useCreateProjectThread() {
26 const startTurn = useAtomCommand(threadEnvironment.startTurn, { reportFailure: false });
27​
28 return useCallback(
29 async (input: {
30 readonly project: EnvironmentProject;
31 readonly modelSelection: ModelSelection;
32 readonly envMode: "local" | "worktree";
33 readonly branch: string | null;
34 readonly worktreePath: string | null;
35 readonly startFromOrigin?: boolean;
36 readonly runtimeMode: RuntimeMode;
37 readonly interactionMode: ProviderInteractionMode;
38 readonly initialMessageText: string;
39 readonly initialAttachments: ReadonlyArray<DraftComposerImageAttachment>;
40 /** Reuse identifiers from a queued pending task instead of minting new ones. */
41 readonly turnMetadata?: TurnCommandMetadata;
42 }) => {
43 const metadata = input.turnMetadata ?? makeTurnCommandMetadata();
44 const threadId = ThreadId.make(metadata.threadId);
45 const initialMessageText = input.initialMessageText.trim();
46​
47 const validationError = validateProjectThreadCreation({
48 environmentId: input.project.environmentId,
49 projectId: input.project.id,
50 environmentMode: input.envMode,
51 branch: input.branch,
52 initialMessageText,
53 });
54 if (validationError !== null) {
55 setPendingConnectionError(validationError.message);
56 return AsyncResult.failure(Cause.fail(validationError));
57 }
58​
59 const result = await startTurn({
60 environmentId: input.project.environmentId,
61 input: buildProjectThreadStartTurnInput({
62 projectId: input.project.id,
63 projectCwd: input.project.workspaceRoot,
64 threadId: metadata.threadId,
65 commandId: metadata.commandId,
66 messageId: metadata.messageId,
67 createdAt: metadata.createdAt,
68 text: initialMessageText,
69 attachments: input.initialAttachments,
70 modelSelection: input.modelSelection,
71 runtimeMode: input.runtimeMode,
72 interactionMode: input.interactionMode,
73 workspaceMode: input.envMode,
74 branch: input.branch,
75 worktreePath: input.worktreePath,
76 startFromOrigin: input.startFromOrigin ?? false,
77 worktreeBranchName: buildTemporaryWorktreeBranchName(randomHex),
78 }),
79 });
80 if (AsyncResult.isFailure(result)) {
81 const error = Cause.squash(result.cause);
82 setPendingConnectionError(
83 error instanceof Error ? error.message : "The task could not be started.",
84 );
85 return AsyncResult.failure(result.cause);
86 }
87 setPendingConnectionError(null);
88​
89 return mapAtomCommandResult(result, () =>
90 scopeThreadRef(input.project.environmentId, threadId),
91 );
92 },
93 [startTurn],
94 );