forked from enowdev/enowX-Coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppShell.tsx
More file actions
617 lines (552 loc) · 20.8 KB
/
Copy pathAppShell.tsx
File metadata and controls
617 lines (552 loc) · 20.8 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
import React, { useEffect } from 'react';
import { invoke, Channel } from '@tauri-apps/api/core';
import { listen, UnlistenFn } from '@tauri-apps/api/event';
import { LeftSidebar } from '@/components/layout/LeftSidebar';
import { RightSidebar } from '@/components/layout/RightSidebar';
import { ChatHeader } from '@/components/layout/ChatHeader';
import { ChatPanel } from '@/components/chat/ChatPanel';
import { ChatInputBar, ChatInputBarHandle } from '@/components/chat/ChatInputBar';
import { PermissionDialog } from '@/components/chat/PermissionDialog';
import { useChatStore } from '@/stores/useChatStore';
import { useProjectStore } from '@/stores/useProjectStore';
import { useSessionStore } from '@/stores/useSessionStore';
import { useSettingsStore } from '@/stores/useSettingsStore';
import { useUIStore } from '@/stores/useUIStore';
import { useAgentStore } from '@/stores/useAgentStore';
import { SettingsModal } from '@/components/settings/SettingsModal';
import { ExcalidrawCanvas } from '@/components/canvas/ExcalidrawCanvas';
import { AgentConfig, AgentRunWithTools, AgentType, ChatUsageEvent, Message, PermissionRequest, Project, Provider, ProviderModelConfig, Session, ToolCall } from '@/types';
import { cn } from '@/lib/utils';
export const AppShell: React.FC = () => {
const { addMessage, appendStreamToken, setStreaming, clearStreaming, setMessages, addTokenUsage } = useChatStore();
const setProjects = useProjectStore((s) => s.setProjects);
const addProject = useProjectStore((s) => s.addProject);
const setActiveProjectId = useProjectStore((s) => s.setActiveProjectId);
const activeSessionId = useSessionStore((s) => s.activeSessionId);
const setSessions = useSessionStore((s) => s.setSessions);
const addSession = useSessionStore((s) => s.addSession);
const setActiveSessionId = useSessionStore((s) => s.setActiveSessionId);
const { setProviders, setDefaultProviderId, defaultProviderId, selectedModelId, setSelectedModelId } = useSettingsStore();
const leftSidebarOpen = useUIStore((s) => s.leftSidebarOpen);
const toggleLeftSidebar = useUIStore((s) => s.toggleLeftSidebar);
const rightSidebarOpen = useUIStore((s) => s.rightSidebarOpen);
const mainView = useUIStore((s) => s.mainView);
const {
addAgentRun,
setAgentRuns,
updateAgentRun,
appendAgentToken,
flushThinkingBlock,
setAgentConfigs,
setPendingPermission,
pendingPermission,
selectedAgentType,
agentConfigs,
} = useAgentStore();
const chatInputRef = React.useRef<ChatInputBarHandle>(null);
// Track which sessions have already been auto-renamed to avoid duplicates
const renamedSessionsRef = React.useRef<Set<string>>(new Set());
const autoRenameSession = React.useCallback((sessionId: string) => {
if (renamedSessionsRef.current.has(sessionId)) return;
const session = useSessionStore.getState().sessions.find(s => s.id === sessionId);
if (!session || session.title !== 'New Chat') return;
renamedSessionsRef.current.add(sessionId);
// Ask the LLM to generate a short title based on the conversation
const { defaultProviderId: pid, selectedModelId: mid } = useSettingsStore.getState();
invoke<string>('generate_title', {
sessionId,
providerId: pid ?? null,
modelId: mid ?? null,
})
.then((title) => {
if (title && title !== 'New Chat') {
useSessionStore.getState().updateSessionTitle(sessionId, title);
invoke('update_session_title', { id: sessionId, title }).catch(console.error);
}
})
.catch((err) => {
console.error('Auto-rename failed:', err);
// Remove from set so it can retry next time
renamedSessionsRef.current.delete(sessionId);
});
}, []);
useEffect(() => {
const loadPersistedData = async () => {
try {
const loadedProjects = await invoke<Project[]>('list_projects');
setProjects(loadedProjects);
if (loadedProjects.length === 0) {
setActiveProjectId(null);
setSessions([]);
setActiveSessionId(null);
return;
}
const allSessions = (
await Promise.all(
loadedProjects.map((p) => invoke<Session[]>('list_sessions', { projectId: p.id }))
)
).flat();
setSessions(allSessions);
const activeProject = [...loadedProjects].sort(
(a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
)[0];
setActiveProjectId(activeProject.id);
const projectSessions = allSessions.filter((s) => s.projectId === activeProject.id);
if (projectSessions.length === 0) {
setActiveSessionId(null);
return;
}
const activeSession = [...projectSessions].sort(
(a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime()
)[0];
setActiveSessionId(activeSession.id);
} catch (error) {
console.error('Failed to load projects and sessions:', error);
}
};
void loadPersistedData();
}, [setProjects, setActiveProjectId, setSessions, setActiveSessionId]);
useEffect(() => {
invoke<Provider[]>('list_providers')
.then(async (ps) => {
setProviders(ps);
// Auto-select provider: default > first enabled > first any
const def = ps.find((p) => p.isDefault && p.isEnabled);
const firstEnabled = ps.find((p) => p.isEnabled);
const picked = def ?? firstEnabled ?? ps[0];
if (picked) {
setDefaultProviderId(picked.id);
// Auto-select first model if none selected
if (!selectedModelId) {
try {
const models = await invoke<ProviderModelConfig[]>('list_provider_models', { providerId: picked.id });
const enabled = models.filter((m) => m.enabled);
if (enabled.length > 0) {
setSelectedModelId(enabled[0].modelId);
} else {
// Fallback: fetch all available models
const allModels = await invoke<string[]>('list_models', { providerId: picked.id });
if (allModels.length > 0) setSelectedModelId(allModels[0]);
}
} catch {}
}
}
})
.catch(console.error);
}, [setProviders, setDefaultProviderId]);
useEffect(() => {
invoke<AgentConfig[]>('list_agent_configs')
.then(setAgentConfigs)
.catch(console.error);
}, [setAgentConfigs]);
useEffect(() => {
let cancelled = false;
const localUnlisten: UnlistenFn[] = [];
const setup = async () => {
const unlistenChatDone = await listen<string>('chat-done', () => {
clearStreaming();
const sessionId = useSessionStore.getState().activeSessionId;
if (sessionId) {
invoke<Message[]>('get_messages', { sessionId })
.then((msgs) => {
useChatStore.getState().setMessages(msgs);
autoRenameSession(sessionId);
})
.catch(console.error);
}
});
const unlistenChatError = await listen<string>('chat-error', (event) => {
console.error('Chat error:', event.payload);
clearStreaming();
});
const unlistenChatUsage = await listen<ChatUsageEvent>('chat-usage', (event) => {
addTokenUsage(event.payload.sessionId, event.payload.usage);
});
const unlistenAgentStarted = await listen<{
agentRunId: string;
agentType: string;
parentAgentRunId: string | null;
}>('agent-started', (event) => {
const { agentRunId, agentType, parentAgentRunId } = event.payload;
if (useAgentStore.getState().agentRuns.some((r) => r.id === agentRunId)) {
return;
}
const now = new Date().toISOString();
const newRun: AgentRunWithTools = {
id: agentRunId,
sessionId: useSessionStore.getState().activeSessionId ?? '',
agentType: agentType as AgentType,
status: 'running',
input: undefined,
output: undefined,
error: undefined,
startedAt: now,
completedAt: undefined,
createdAt: now,
toolCalls: [],
streamingText: '',
thinkingBlocks: [],
parentAgentRunId: parentAgentRunId,
projectPath: null,
};
addAgentRun(newRun);
});
const unlistenAgentToken = await listen<{ agentRunId: string; token: string }>(
'agent-token',
(event) => {
appendAgentToken(event.payload.agentRunId, event.payload.token);
}
);
const unlistenAgentToolCall = await listen<{
toolCallId: string;
agentRunId: string;
toolName: string;
input: unknown;
}>('agent-tool-call', (event) => {
const { toolCallId, agentRunId, toolName, input } = event.payload;
flushThinkingBlock(agentRunId);
const now = new Date().toISOString();
const newToolCall: ToolCall = {
id: toolCallId,
agentRunId,
toolName: toolName as ToolCall['toolName'],
input: typeof input === 'string' ? input : JSON.stringify(input),
output: null,
status: 'running',
error: null,
startedAt: now,
completedAt: null,
createdAt: now,
};
updateAgentRun(agentRunId, {
toolCalls: (() => {
const existing = useAgentStore
.getState()
.agentRuns.find((r) => r.id === agentRunId)?.toolCalls ?? [];
if (existing.some((tc) => tc.id === newToolCall.id)) {
return existing;
}
return [...existing, newToolCall];
})(),
});
});
const unlistenAgentToolResult = await listen<{
toolCallId: string;
output: string;
isError: boolean;
}>('agent-tool-result', (event) => {
const { toolCallId, output, isError } = event.payload;
const runs = useAgentStore.getState().agentRuns;
const run = runs.find((r) => r.toolCalls.some((tc) => tc.id === toolCallId));
if (!run) return;
const updatedToolCalls = run.toolCalls.map((tc) =>
tc.id === toolCallId
? {
...tc,
output,
status: (isError ? 'failed' : 'completed') as ToolCall['status'],
completedAt: new Date().toISOString(),
}
: tc
);
updateAgentRun(run.id, { toolCalls: updatedToolCalls });
});
const unlistenAgentDone = await listen<{ agentRunId: string; output: string }>(
'agent-done',
(event) => {
const { agentRunId, output } = event.payload;
flushThinkingBlock(agentRunId);
updateAgentRun(agentRunId, {
status: 'completed',
output,
completedAt: new Date().toISOString(),
});
// Auto-rename after agent completes
const sessionId = useSessionStore.getState().activeSessionId;
if (sessionId) {
autoRenameSession(sessionId);
}
}
);
const unlistenAgentError = await listen<{ agentRunId: string; error: string }>(
'agent-error',
(event) => {
const { agentRunId, error } = event.payload;
flushThinkingBlock(agentRunId);
updateAgentRun(agentRunId, {
status: 'failed',
error,
completedAt: new Date().toISOString(),
});
}
);
const unlistenPermission = await listen<{
agentRunId: string;
type: 'sensitive_file' | 'outside_sandbox';
path: string;
agentType: string;
}>('agent-permission-request', (event) => {
const req: PermissionRequest = {
agentRunId: event.payload.agentRunId,
type: event.payload.type,
path: event.payload.path,
agentType: event.payload.agentType as AgentType,
};
setPendingPermission(req);
});
localUnlisten.push(
unlistenChatDone,
unlistenChatError,
unlistenChatUsage,
unlistenAgentStarted,
unlistenAgentToken,
unlistenAgentToolCall,
unlistenAgentToolResult,
unlistenAgentDone,
unlistenAgentError,
unlistenPermission,
);
if (cancelled) {
localUnlisten.forEach((fn) => fn());
}
};
void setup();
return () => {
cancelled = true;
localUnlisten.forEach((fn) => fn());
};
}, [
clearStreaming,
addAgentRun,
appendAgentToken,
flushThinkingBlock,
updateAgentRun,
setPendingPermission,
]);
useEffect(() => {
if (!activeSessionId) return;
// Skip DB reload for sessions we just created — they're empty and we already
// have the optimistic user message in the store. The reload would wipe it.
if (justCreatedSessionRef.current.has(activeSessionId)) {
justCreatedSessionRef.current.delete(activeSessionId);
return;
}
invoke<Message[]>('get_messages', { sessionId: activeSessionId })
.then(setMessages)
.catch(console.error);
invoke<AgentRunWithTools[]>('list_agent_runs', { sessionId: activeSessionId })
.then(async (runs) => {
const hydratedRuns = await Promise.all(
runs.map(async (run) => {
const toolCalls = await invoke<ToolCall[]>('list_tool_calls', { agentRunId: run.id }).catch(
() => [] as ToolCall[]
);
return {
...run,
toolCalls,
streamingText: '',
thinkingBlocks: [],
parentAgentRunId: run.parentAgentRunId ?? null,
projectPath: run.projectPath ?? null,
} as AgentRunWithTools;
})
);
setAgentRuns(hydratedRuns);
})
.catch(console.error);
}, [activeSessionId, setMessages, setAgentRuns]);
// Track sessions we just created so the activeSessionId effect doesn't wipe messages
const justCreatedSessionRef = React.useRef<Set<string>>(new Set());
const ensureSession = async (): Promise<{ sessionId: string; projectPath: string } | null> => {
let currentSessionId = useSessionStore.getState().activeSessionId;
let currentProjectId = useProjectStore.getState().activeProjectId;
const currentProjects = useProjectStore.getState().projects;
// If we already have an active session, just return it
if (currentSessionId) {
const proj = currentProjects.find(p => p.id === currentProjectId);
return { sessionId: currentSessionId, projectPath: proj?.path ?? '' };
}
try {
// If no project exists, create a default one
if (!currentProjectId || currentProjects.length === 0) {
const project = await invoke<Project>('create_project', { name: 'Default', path: null });
addProject(project);
setActiveProjectId(project.id);
currentProjectId = project.id;
}
// Create a new session
const session = await invoke<Session>('create_session', { projectId: currentProjectId, title: 'New Chat' });
addSession(session);
// Mark as just-created so the effect doesn't wipe our optimistic messages
justCreatedSessionRef.current.add(session.id);
setActiveSessionId(session.id);
return { sessionId: session.id, projectPath: currentProjects.find(p => p.id === currentProjectId)?.path ?? '' };
} catch (err) {
console.error('Failed to auto-create session:', err);
return null;
}
};
const handleSend = async (content: string) => {
const ctx = await ensureSession();
if (!ctx) return;
const { sessionId: currentSessionId, projectPath } = ctx;
if (selectedAgentType === 'orchestrator' || selectedAgentType === 'planner') {
const userMsg: Message = {
id: crypto.randomUUID(),
sessionId: currentSessionId,
role: 'user',
content,
createdAt: new Date().toISOString(),
};
addMessage(userMsg);
const agentConfig = agentConfigs.find((c) => c.agentType === selectedAgentType);
const agentProviderId = agentConfig?.providerId ?? defaultProviderId ?? null;
const agentModelId = agentConfig?.modelId ?? selectedModelId ?? null;
const onToken = new Channel<string>();
onToken.onmessage = () => {};
try {
await invoke('run_agent', {
request: {
sessionId: currentSessionId,
agentType: selectedAgentType,
task: content,
projectPath,
providerId: agentProviderId,
modelId: agentModelId,
fluxEnabled: useUIStore.getState().fluxEnabled,
},
onToken,
});
} catch (err) {
console.error('run_agent error:', err);
}
return;
}
const userMsg: Message = {
id: crypto.randomUUID(),
sessionId: currentSessionId,
role: 'user',
content,
createdAt: new Date().toISOString(),
};
addMessage(userMsg);
setStreaming(true);
const onToken = new Channel<string>();
onToken.onmessage = (token) => {
appendStreamToken(token);
};
try {
await invoke('send_message', {
sessionId: currentSessionId,
content,
providerId: defaultProviderId ?? null,
modelId: selectedModelId ?? null,
onToken,
});
} catch (err) {
console.error('send_message error:', err);
clearStreaming();
}
};
const handleStop = async () => {
const currentSessionId = useSessionStore.getState().activeSessionId;
// 1. Stop chat streaming via backend cancellation
const { isStreaming, streamingText, clearStreaming } = useChatStore.getState();
if (isStreaming && currentSessionId) {
try {
await invoke('cancel_chat', { sessionId: currentSessionId });
} catch (e) {
console.error('Failed to cancel chat:', e);
}
// Save partial output as assistant message
if (streamingText.trim()) {
const partialMsg: Message = {
id: crypto.randomUUID(),
sessionId: currentSessionId,
role: 'assistant',
content: streamingText.trim(),
createdAt: new Date().toISOString(),
};
useChatStore.getState().addMessage(partialMsg);
}
clearStreaming();
}
// 2. Cancel any running agent runs via backend cancellation
if (currentSessionId) {
const { agentRuns } = useAgentStore.getState();
const runningAgents = agentRuns.filter((r) => r.status === 'running');
if (runningAgents.length > 0) {
try {
// Cancel by session ID — this cancels the token that the agent runner uses
await invoke('cancel_agent', { id: currentSessionId });
} catch (e) {
console.error('Failed to cancel agent:', e);
}
for (const run of runningAgents) {
updateAgentRun(run.id, {
status: 'failed',
error: 'Stopped by user',
completedAt: new Date().toISOString(),
});
}
}
}
};
const handlePermissionAllow = () => {
if (!pendingPermission) return;
invoke('agent_permission_response', {
agentRunId: pendingPermission.agentRunId,
allowed: true,
}).catch(console.error);
setPendingPermission(null);
};
const handlePermissionDeny = () => {
if (!pendingPermission) return;
invoke('agent_permission_response', {
agentRunId: pendingPermission.agentRunId,
allowed: false,
}).catch(console.error);
setPendingPermission(null);
};
return (
<div
className="bg-[var(--bg)] text-[var(--text)] h-screen w-screen overflow-hidden"
style={{
display: 'grid',
gridTemplateColumns: `${leftSidebarOpen ? 'var(--sidebar-width-left)' : '0px'} 1fr ${rightSidebarOpen ? 'var(--sidebar-width-right)' : '0px'}`,
gridTemplateRows: '1fr',
transition: 'grid-template-columns 0.25s cubic-bezier(0.4, 0, 0.2, 1)',
}}
>
<div className={cn(
'h-full overflow-hidden transition-opacity duration-200',
leftSidebarOpen ? 'opacity-100' : 'opacity-0 pointer-events-none'
)}>
<LeftSidebar />
</div>
<main className="flex flex-col overflow-hidden min-h-0">
<ChatHeader onToggleLeftSidebar={!leftSidebarOpen ? toggleLeftSidebar : undefined} />
{mainView === 'chat' ? (
<>
<ChatPanel onChipClick={(text) => chatInputRef.current?.prefill(text)} />
<ChatInputBar ref={chatInputRef} onSend={handleSend} onStop={handleStop} />
</>
) : (
<ExcalidrawCanvas />
)}
</main>
<div className={cn(
'h-full overflow-hidden transition-opacity duration-200',
rightSidebarOpen ? 'opacity-100' : 'opacity-0 pointer-events-none'
)}>
<RightSidebar />
</div>
<SettingsModal />
<PermissionDialog
request={pendingPermission}
onAllow={handlePermissionAllow}
onDeny={handlePermissionDeny}
/>
</div>
);
};