-
Notifications
You must be signed in to change notification settings - Fork 271
Expand file tree
/
Copy pathchatThreadPanel.tsx
More file actions
82 lines (74 loc) · 3.11 KB
/
chatThreadPanel.tsx
File metadata and controls
82 lines (74 loc) · 3.11 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
'use client';
import { ResizablePanel } from '@/components/ui/resizable';
import { ChatThread } from '@/features/chat/components/chatThread';
import { LanguageModelInfo, SBChatMessage, SearchScope, SET_CHAT_STATE_QUERY_PARAM, SetChatStatePayload } from '@/features/chat/types';
import { RepositoryQuery, SearchContextQuery } from '@/lib/types';
import { CreateUIMessage } from 'ai';
import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect, useState } from 'react';
import { useChatId } from '../../useChatId';
interface ChatThreadPanelProps {
languageModels: LanguageModelInfo[];
repos: RepositoryQuery[];
searchContexts: SearchContextQuery[];
order: number;
messages: SBChatMessage[];
isChatReadonly: boolean;
}
export const ChatThreadPanel = ({
languageModels,
repos,
searchContexts,
order,
messages,
isChatReadonly,
}: ChatThreadPanelProps) => {
// @note: we are guaranteed to have a chatId because this component will only be
// mounted when on a /chat/[id] route.
const chatId = useChatId()!;
const router = useRouter();
const searchParams = useSearchParams();
const [inputMessage, setInputMessage] = useState<CreateUIMessage<SBChatMessage> | undefined>(undefined);
// Use the last user's last message to determine what repos and contexts we should select by default.
const lastUserMessage = messages.findLast((message) => message.role === "user");
const defaultSelectedSearchScopes = lastUserMessage?.metadata?.selectedSearchScopes ?? [];
const [selectedSearchScopes, setSelectedSearchScopes] = useState<SearchScope[]>(defaultSelectedSearchScopes);
useEffect(() => {
const setChatState = searchParams.get(SET_CHAT_STATE_QUERY_PARAM);
if (!setChatState) {
return;
}
try {
const { inputMessage, selectedSearchScopes } = JSON.parse(setChatState) as SetChatStatePayload;
setInputMessage(inputMessage);
setSelectedSearchScopes(selectedSearchScopes);
} catch {
console.error('Invalid message in URL');
}
// Remove the message from the URL
const newSearchParams = new URLSearchParams(searchParams.toString());
newSearchParams.delete(SET_CHAT_STATE_QUERY_PARAM);
router.replace(`?${newSearchParams.toString()}`, { scroll: false });
}, [searchParams, router]);
return (
<ResizablePanel
order={order}
id="chat-thread-panel"
defaultSize={85}
>
<div className="flex flex-col h-full w-full">
<ChatThread
id={chatId}
initialMessages={messages}
inputMessage={inputMessage}
languageModels={languageModels}
repos={repos}
searchContexts={searchContexts}
selectedSearchScopes={selectedSearchScopes}
onSelectedSearchScopesChange={setSelectedSearchScopes}
isChatReadonly={isChatReadonly}
/>
</div>
</ResizablePanel>
)
}