Open History to see the last commit
1import { DEFAULT_RUNTIME_MODE, type ProjectId, ThreadId } from "@t3tools/contracts";
2import { useNavigate, useParams } from "@tanstack/react-router";
3import { useCallback } from "react";
4import { inferProviderForModel } from "@t3tools/shared/model";
5import {
6 type DraftThreadEnvMode,
7 type DraftThreadState,
8 useComposerDraftStore,
9} from "../composerDraftStore";
10import { newThreadId } from "../lib/utils";
11import { useStore } from "../store";
12
13export function useHandleNewThread() {
14 const projects = useStore((store) => store.projects);
15 const threads = useStore((store) => store.threads);
16 const stickyModel = useComposerDraftStore((store) => store.stickyModel);
17 const stickyModelOptions = useComposerDraftStore((store) => store.stickyModelOptions);
18 const navigate = useNavigate();
19 const routeThreadId = useParams({
20 strict: false,
21 select: (params) => (params.threadId ? ThreadId.makeUnsafe(params.threadId) : null),
22 });
23 const activeDraftThread = useComposerDraftStore((store) =>
24 routeThreadId ? (store.draftThreadsByThreadId[routeThreadId] ?? null) : null,
25 );
26
27 const activeThread = routeThreadId
28 ? threads.find((thread) => thread.id === routeThreadId)
29 : undefined;
30
31 const handleNewThread = useCallback(
32 (
33 projectId: ProjectId,
34 options?: {
35 branch?: string | null;
36 worktreePath?: string | null;
37 envMode?: DraftThreadEnvMode;
38 },
39 ): Promise<void> => {
40 const {
41 clearProjectDraftThreadId,
42 getDraftThread,
43 getDraftThreadByProjectId,
44 setModel,
45 setModelOptions,
46 setProvider,
47 setDraftThreadContext,
48 setProjectDraftThreadId,
49 } = useComposerDraftStore.getState();
50 const hasBranchOption = options?.branch !== undefined;
51 const hasWorktreePathOption = options?.worktreePath !== undefined;
52 const hasEnvModeOption = options?.envMode !== undefined;
53 const storedDraftThread = getDraftThreadByProjectId(projectId);
54 const latestActiveDraftThread: DraftThreadState | null = routeThreadId
55 ? getDraftThread(routeThreadId)
56 : null;
57 if (storedDraftThread) {
58 return (async () => {
59 if (hasBranchOption || hasWorktreePathOption || hasEnvModeOption) {
60 setDraftThreadContext(storedDraftThread.threadId, {
61 ...(hasBranchOption ? { branch: options?.branch ?? null } : {}),
62 ...(hasWorktreePathOption ? { worktreePath: options?.worktreePath ?? null } : {}),
63 ...(hasEnvModeOption ? { envMode: options?.envMode } : {}),
64 });
65 }
66 setProjectDraftThreadId(projectId, storedDraftThread.threadId);
67 if (routeThreadId === storedDraftThread.threadId) {
68 return;
69 }
70 await navigate({
71 to: "/$threadId",
72 params: { threadId: storedDraftThread.threadId },
73 });
74 })();
75 }
76
77 clearProjectDraftThreadId(projectId);
78
79 if (
80 latestActiveDraftThread &&
81 routeThreadId &&
82 latestActiveDraftThread.projectId === projectId
83 ) {
84 if (hasBranchOption || hasWorktreePathOption || hasEnvModeOption) {
85 setDraftThreadContext(routeThreadId, {
86 ...(hasBranchOption ? { branch: options?.branch ?? null } : {}),
87 ...(hasWorktreePathOption ? { worktreePath: options?.worktreePath ?? null } : {}),
88 ...(hasEnvModeOption ? { envMode: options?.envMode } : {}),
89 });
90 }
91 setProjectDraftThreadId(projectId, routeThreadId);
92 return Promise.resolve();
93 }
94
95 const threadId = newThreadId();
96 const createdAt = new Date().toISOString();
97 return (async () => {
98 setProjectDraftThreadId(projectId, threadId, {
99 createdAt,
100 branch: options?.branch ?? null,
101 worktreePath: options?.worktreePath ?? null,
102 envMode: options?.envMode ?? "local",
103 runtimeMode: DEFAULT_RUNTIME_MODE,
104 });
105 if (stickyModel) {
106 setProvider(threadId, inferProviderForModel(stickyModel));
107 setModel(threadId, stickyModel);
108 }
109 if (Object.keys(stickyModelOptions).length > 0) {
110 setModelOptions(threadId, stickyModelOptions);
111 }
112
113 await navigate({
114 to: "/$threadId",
115 params: { threadId },
116 });
117 })();
118 },
119 [navigate, routeThreadId, stickyModel, stickyModelOptions],
120 );
121
122 return {
123 activeDraftThread,
124 activeThread,
125 handleNewThread,
126 projects,
127 routeThreadId,
128 };
129}