-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathchatThreadPanel.tsx
More file actions
104 lines (95 loc) · 4.1 KB
/
chatThreadPanel.tsx
File metadata and controls
104 lines (95 loc) · 4.1 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
'use client';
import { ResizablePanel } from '@/components/ui/resizable';
import { ChatThread } from '@/features/chat/components/chatThread';
import { LanguageModelInfo, SBChatMessage, SET_CHAT_STATE_QUERY_PARAM, SetChatStatePayload } from '@/features/chat/types';
import { RepositoryQuery } from '@/lib/types';
import { CreateUIMessage } from 'ai';
import { useRouter, useSearchParams } from 'next/navigation';
import { useEffect, useState } from 'react';
import { useChatId } from '../../useChatId';
import { ContextItem } from '@/features/chat/components/chatBox/contextSelector';
interface ChatThreadPanelProps {
languageModels: LanguageModelInfo[];
repos: RepositoryQuery[];
order: number;
messages: SBChatMessage[];
isChatReadonly: boolean;
}
export const ChatThreadPanel = ({
languageModels,
repos,
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 defaultSelectedRepos = lastUserMessage?.metadata?.selectedRepos ?? [];
const defaultSelectedContexts = lastUserMessage?.metadata?.selectedContexts ?? [];
const [selectedItems, setSelectedItems] = useState<ContextItem[]>([
...defaultSelectedRepos.map(repoName => {
const repoInfo = repos.find(r => r.repoName === repoName);
return {
type: 'repo' as const,
value: repoName,
name: repoInfo?.repoDisplayName || repoName.split('/').pop() || repoName,
codeHostType: repoInfo?.codeHostType
};
}),
...defaultSelectedContexts.map(context => ({ type: 'context' as const, value: context, name: context }))
]);
useEffect(() => {
const setChatState = searchParams.get(SET_CHAT_STATE_QUERY_PARAM);
if (!setChatState) {
return;
}
try {
const { inputMessage, selectedRepos, selectedContexts } = JSON.parse(setChatState) as SetChatStatePayload;
setInputMessage(inputMessage);
setSelectedItems([
...selectedRepos.map(repoName => {
const repoInfo = repos.find(r => r.repoName === repoName);
return {
type: 'repo' as const,
value: repoName,
name: repoInfo?.repoDisplayName || repoName.split('/').pop() || repoName,
codeHostType: repoInfo?.codeHostType
};
}),
...(selectedContexts || []).map(context => ({ type: 'context' as const, value: context, name: context }))
]);
} 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, repos]);
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}
selectedItems={selectedItems}
onSelectedItemsChange={setSelectedItems}
isChatReadonly={isChatReadonly}
/>
</div>
</ResizablePanel>
)
}