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