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

use-project-actions.ts

125 lines4.6 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 creationSource: "mobile",
74 threadId,
75 message: {
76 messageId: MessageId.make(metadata.messageId),
77 role: "user",
78 text: initialMessageText,
79 attachments: input.initialAttachments,
80 },
81 modelSelection: input.modelSelection,
82 titleSeed: nextTitle,
83 runtimeMode: input.runtimeMode,
84 interactionMode: input.interactionMode,
85 bootstrap: {
86 createThread: {
87 projectId: input.project.id,
88 title: nextTitle,
89 modelSelection: input.modelSelection,
90 runtimeMode: input.runtimeMode,
91 interactionMode: input.interactionMode,
92 branch: input.branch,
93 worktreePath: isWorktree ? null : input.worktreePath,
94 createdAt: metadata.createdAt,
95 },
96 ...(isWorktree
97 ? {
98 prepareWorktree: {
99 projectCwd: input.project.workspaceRoot,
100 baseBranch: input.branch!,
101 branch: buildTemporaryWorktreeBranchName(uuidv4),
102 },
103 runSetupScript: true,
104 }
105 : {}),
106 },
107 createdAt: metadata.createdAt,
108 },
109 });
110 if (AsyncResult.isFailure(result)) {
111 const error = Cause.squash(result.cause);
112 setPendingConnectionError(
113 error instanceof Error ? error.message : "The task could not be started.",
114 );
115 return AsyncResult.failure(result.cause);
116 }
117 setPendingConnectionError(null);
118​
119 return mapAtomCommandResult(result, () =>
120 scopeThreadRef(input.project.environmentId, threadId),
121 );
122 },
123 [startTurn],
124 );