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

use-project-actions.ts

124 lines4.5 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 CommandId,
8 MessageId,
9 ThreadId,
10 type ModelSelection,
11 type ProviderInteractionMode,
12 type RuntimeMode,
13} from "@t3tools/contracts";
14import { buildTemporaryWorktreeBranchName } from "@t3tools/shared/git";
15import * as Cause from "effect/Cause";
16import { AsyncResult } from "effect/unstable/reactivity";
17​
18import { threadEnvironment } from "../../state/threads";
19import type { DraftComposerImageAttachment } from "../../lib/composerImages";
20import { makeTurnCommandMetadata } from "../../lib/commandMetadata";
21import { uuidv4 } from "../../lib/uuid";
22import { useAtomCommand } from "../../state/use-atom-command";
23import { setPendingConnectionError } from "../../state/use-remote-environment-registry";
24import { validateProjectThreadCreation } from "./projectThreadCreationValidation";
25​
26function deriveThreadTitleFromPrompt(value: string): string {
27 const trimmed = value.trim();
28 if (trimmed.length === 0) {
29 return "New thread";
30 }
31​
32 const compact = trimmed.replace(/\s+/g, " ");
33 return compact.length <= 72 ? compact : `${compact.slice(0, 69).trimEnd()}...`;
35​
36export function useCreateProjectThread() {
37 const startTurn = useAtomCommand(threadEnvironment.startTurn, { reportFailure: false });
38​
39 return useCallback(
40 async (input: {
41 readonly project: EnvironmentProject;
42 readonly modelSelection: ModelSelection;
43 readonly envMode: "local" | "worktree";
44 readonly branch: string | null;
45 readonly worktreePath: string | null;
46 readonly runtimeMode: RuntimeMode;
47 readonly interactionMode: ProviderInteractionMode;
48 readonly initialMessageText: string;
49 readonly initialAttachments: ReadonlyArray<DraftComposerImageAttachment>;
50 }) => {
51 const metadata = makeTurnCommandMetadata();
52 const threadId = ThreadId.make(metadata.threadId);
53 const initialMessageText = input.initialMessageText.trim();
54 const nextTitle = deriveThreadTitleFromPrompt(input.initialMessageText);
55​
56 const validationError = validateProjectThreadCreation({
57 environmentId: input.project.environmentId,
58 projectId: input.project.id,
59 environmentMode: input.envMode,
60 branch: input.branch,
61 initialMessageText,
62 });
63 if (validationError !== null) {
64 setPendingConnectionError(validationError.message);
65 return AsyncResult.failure(Cause.fail(validationError));
66 }
67​
68 const isWorktree = input.envMode === "worktree";
69 const result = await startTurn({
70 environmentId: input.project.environmentId,
71 input: {
72 commandId: CommandId.make(metadata.commandId),
73 threadId,
74 message: {
75 messageId: MessageId.make(metadata.messageId),
76 role: "user",
77 text: initialMessageText,
78 attachments: input.initialAttachments,
79 },
80 modelSelection: input.modelSelection,
81 titleSeed: nextTitle,
82 runtimeMode: input.runtimeMode,
83 interactionMode: input.interactionMode,
84 bootstrap: {
85 createThread: {
86 projectId: input.project.id,
87 title: nextTitle,
88 modelSelection: input.modelSelection,
89 runtimeMode: input.runtimeMode,
90 interactionMode: input.interactionMode,
91 branch: input.branch,
92 worktreePath: isWorktree ? null : input.worktreePath,
93 createdAt: metadata.createdAt,
94 },
95 ...(isWorktree
96 ? {
97 prepareWorktree: {
98 projectCwd: input.project.workspaceRoot,
99 baseBranch: input.branch!,
100 branch: buildTemporaryWorktreeBranchName(uuidv4),
101 },
102 runSetupScript: true,
103 }
104 : {}),
105 },
106 createdAt: metadata.createdAt,
107 },
108 });
109 if (AsyncResult.isFailure(result)) {
110 const error = Cause.squash(result.cause);
111 setPendingConnectionError(
112 error instanceof Error ? error.message : "The task could not be started.",
113 );
114 return AsyncResult.failure(result.cause);
115 }
116 setPendingConnectionError(null);
117​
118 return mapAtomCommandResult(result, () =>
119 scopeThreadRef(input.project.environmentId, threadId),
120 );
121 },
122 [startTurn],
123 );