forked from dyad-sh/dyad
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatAtoms.ts
More file actions
235 lines (219 loc) · 7.96 KB
/
Copy pathchatAtoms.ts
File metadata and controls
235 lines (219 loc) · 7.96 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import type {
FileAttachment,
Message,
AgentTodo,
ComponentSelection,
} from "@/ipc/types";
import type { ListedApp } from "@/ipc/types/app";
import type { Getter, Setter } from "jotai";
import { atom } from "jotai";
// Per-chat atoms implemented with maps keyed by chatId
export const chatMessagesByIdAtom = atom<Map<number, Message[]>>(new Map());
export const chatErrorByIdAtom = atom<Map<number, string | null>>(new Map());
// Atom to hold the currently selected chat ID
export const selectedChatIdAtom = atom<number | null>(null);
export const isStreamingByIdAtom = atom<Map<number, boolean>>(new Map());
export const chatInputValueAtom = atom<string>("");
export const homeChatInputValueAtom = atom<string>("");
export const homeSelectedAppAtom = atom<ListedApp | null>(null);
// Used for scrolling to the bottom of the chat messages (per chat)
export const chatStreamCountByIdAtom = atom<Map<number, number>>(new Map());
export const recentStreamChatIdsAtom = atom<Set<number>>(new Set<number>());
export const recentViewedChatIdsAtom = atom<number[]>([]);
// Track explicitly closed tabs - these should not reappear in the tab bar
export const closedChatIdsAtom = atom<Set<number>>(new Set<number>());
// Track chats opened in the current session - tabs are only shown for these
export const sessionOpenedChatIdsAtom = atom<Set<number>>(new Set<number>());
const MAX_RECENT_VIEWED_CHAT_IDS = 100;
// Helper to remove a chat ID from the closed set (used when a closed tab is re-opened)
function removeFromClosedSet(get: Getter, set: Setter, chatId: number): void {
const closedIds = get(closedChatIdsAtom);
if (closedIds.has(chatId)) {
const newClosedIds = new Set(closedIds);
newClosedIds.delete(chatId);
set(closedChatIdsAtom, newClosedIds);
}
}
export const setRecentViewedChatIdsAtom = atom(
null,
(_get, set, chatIds: number[]) => {
if (chatIds.length > MAX_RECENT_VIEWED_CHAT_IDS) {
set(
recentViewedChatIdsAtom,
chatIds.slice(0, MAX_RECENT_VIEWED_CHAT_IDS),
);
} else {
set(recentViewedChatIdsAtom, chatIds);
}
},
);
// Helper to add a chat ID to the session-opened set
function addToSessionSet(get: Getter, set: Setter, chatId: number): void {
const sessionIds = get(sessionOpenedChatIdsAtom);
if (!sessionIds.has(chatId)) {
const newSessionIds = new Set(sessionIds);
newSessionIds.add(chatId);
set(sessionOpenedChatIdsAtom, newSessionIds);
}
}
// Add a chat ID to the recent list only if it's not already present.
// Unlike pushRecentViewedChatIdAtom, this does NOT move existing IDs to the front,
// preserving the current tab order for chats already tracked.
// Also adds to session tracking so the tab appears in the tab bar.
export const ensureRecentViewedChatIdAtom = atom(
null,
(get, set, chatId: number) => {
const currentIds = get(recentViewedChatIdsAtom);
if (!currentIds.includes(chatId)) {
const nextIds = [chatId, ...currentIds];
if (nextIds.length > MAX_RECENT_VIEWED_CHAT_IDS) {
nextIds.length = MAX_RECENT_VIEWED_CHAT_IDS;
}
set(recentViewedChatIdsAtom, nextIds);
}
// Remove from closed set when explicitly selected
removeFromClosedSet(get, set, chatId);
// Track in session so the tab appears
addToSessionSet(get, set, chatId);
},
);
export const pushRecentViewedChatIdAtom = atom(
null,
(get, set, chatId: number) => {
const nextIds = get(recentViewedChatIdsAtom).filter((id) => id !== chatId);
nextIds.unshift(chatId);
if (nextIds.length > MAX_RECENT_VIEWED_CHAT_IDS) {
nextIds.length = MAX_RECENT_VIEWED_CHAT_IDS;
}
set(recentViewedChatIdsAtom, nextIds);
// Remove from closed set when explicitly selected
removeFromClosedSet(get, set, chatId);
// Track in session so the tab appears (fixes re-open after bulk close)
addToSessionSet(get, set, chatId);
},
);
export const removeRecentViewedChatIdAtom = atom(
null,
(get, set, chatId: number) => {
set(
recentViewedChatIdsAtom,
get(recentViewedChatIdsAtom).filter((id) => id !== chatId),
);
// Add to closed set so it doesn't reappear
const closedIds = get(closedChatIdsAtom);
const newClosedIds = new Set(closedIds);
newClosedIds.add(chatId);
set(closedChatIdsAtom, newClosedIds);
// Also remove from session tracking (consistent with closeMultipleTabsAtom)
removeFromSessionSet(get, set, [chatId]);
},
);
// Prune closed chat IDs that no longer exist in the chats list
export const pruneClosedChatIdsAtom = atom(
null,
(get, set, validChatIds: Set<number>) => {
const closedIds = get(closedChatIdsAtom);
let changed = false;
const pruned = new Set<number>();
for (const id of closedIds) {
if (validChatIds.has(id)) {
pruned.add(id);
} else {
changed = true;
}
}
if (changed) {
set(closedChatIdsAtom, pruned);
}
},
);
// Add a chat ID to the session-opened set (delegates to helper)
export const addSessionOpenedChatIdAtom = atom(
null,
(get, set, chatId: number) => addToSessionSet(get, set, chatId),
);
// Helper to remove chat IDs from the session-opened set
function removeFromSessionSet(
get: Getter,
set: Setter,
chatIds: number[],
): void {
const sessionIds = get(sessionOpenedChatIdsAtom);
let changed = false;
const newSessionIds = new Set(sessionIds);
for (const id of chatIds) {
if (newSessionIds.has(id)) {
newSessionIds.delete(id);
changed = true;
}
}
if (changed) {
set(sessionOpenedChatIdsAtom, newSessionIds);
}
}
// Close multiple tabs at once (for "Close other tabs" / "Close tabs to the right")
export const closeMultipleTabsAtom = atom(
null,
(get, set, chatIdsToClose: number[]) => {
if (chatIdsToClose.length === 0) return;
// Remove from recent viewed
const currentIds = get(recentViewedChatIdsAtom);
const closeSet = new Set(chatIdsToClose);
set(
recentViewedChatIdsAtom,
currentIds.filter((id) => !closeSet.has(id)),
);
// Add to closed set
const closedIds = get(closedChatIdsAtom);
const newClosedIds = new Set(closedIds);
for (const id of chatIdsToClose) {
newClosedIds.add(id);
}
set(closedChatIdsAtom, newClosedIds);
// Remove from session tracking to prevent unbounded growth
removeFromSessionSet(get, set, chatIdsToClose);
},
);
// Remove a chat ID from all tracking (used when chat is deleted)
export const removeChatIdFromAllTrackingAtom = atom(
null,
(get, set, chatId: number) => {
set(
recentViewedChatIdsAtom,
get(recentViewedChatIdsAtom).filter((id) => id !== chatId),
);
removeFromClosedSet(get, set, chatId);
// Also remove from session tracking
removeFromSessionSet(get, set, [chatId]);
},
);
export const attachmentsAtom = atom<FileAttachment[]>([]);
// Agent tool consent request queue
export interface PendingAgentConsent {
requestId: string;
chatId: number;
toolName: string;
toolDescription?: string | null;
inputPreview?: string | null;
}
export const pendingAgentConsentsAtom = atom<PendingAgentConsent[]>([]);
// Agent todos per chat
export const agentTodosByChatIdAtom = atom<Map<number, AgentTodo[]>>(new Map());
// Flag: set when user switches to plan mode from another mode in a chat with messages
export const needsFreshPlanChatAtom = atom<boolean>(false);
// Queued messages (multiple messages per chat, sent in sequence after streams complete)
export interface QueuedMessageItem {
id: string; // UUID for stable identification during reordering/editing
prompt: string;
attachments?: FileAttachment[];
selectedComponents?: ComponentSelection[];
}
// Map<chatId, QueuedMessageItem[]>
export const queuedMessagesByIdAtom = atom<Map<number, QueuedMessageItem[]>>(
new Map(),
);
// Tracks whether the last stream for a chat completed successfully (via onEnd, not cancelled or errored)
// This is used to safely process the queue only when we're certain the stream finished normally
export const streamCompletedSuccessfullyByIdAtom = atom<Map<number, boolean>>(
new Map(),
);