33import { ResizablePanel } from '@/components/ui/resizable' ;
44import { ChatThread } from '@/features/chat/components/chatThread' ;
55import { LanguageModelInfo , SBChatMessage , SET_CHAT_STATE_QUERY_PARAM , SetChatStatePayload } from '@/features/chat/types' ;
6- import { RepositoryQuery } from '@/lib/types' ;
6+ import { RepositoryQuery , SearchContextQuery } from '@/lib/types' ;
77import { CreateUIMessage } from 'ai' ;
88import { useRouter , useSearchParams } from 'next/navigation' ;
99import { useEffect , useState } from 'react' ;
1010import { useChatId } from '../../useChatId' ;
11+ import { ContextItem } from '@/features/chat/components/chatBox/contextSelector' ;
1112
1213interface ChatThreadPanelProps {
1314 languageModels : LanguageModelInfo [ ] ;
1415 repos : RepositoryQuery [ ] ;
16+ searchContexts : SearchContextQuery [ ] ;
1517 order : number ;
1618 messages : SBChatMessage [ ] ;
1719 isChatReadonly : boolean ;
@@ -20,6 +22,7 @@ interface ChatThreadPanelProps {
2022export const ChatThreadPanel = ( {
2123 languageModels,
2224 repos,
25+ searchContexts,
2326 order,
2427 messages,
2528 isChatReadonly,
@@ -31,8 +34,31 @@ export const ChatThreadPanel = ({
3134 const searchParams = useSearchParams ( ) ;
3235 const [ inputMessage , setInputMessage ] = useState < CreateUIMessage < SBChatMessage > | undefined > ( undefined ) ;
3336
34- // Use the last user's last message to determine what repos we should select by default.
35- const [ selectedRepos , setSelectedRepos ] = useState < string [ ] > ( messages . findLast ( ( message ) => message . role === "user" ) ?. metadata ?. selectedRepos ?? [ ] ) ;
37+ // Use the last user's last message to determine what repos and contexts we should select by default.
38+ const lastUserMessage = messages . findLast ( ( message ) => message . role === "user" ) ;
39+ const defaultSelectedRepos = lastUserMessage ?. metadata ?. selectedRepos ?? [ ] ;
40+ const defaultSelectedContexts = lastUserMessage ?. metadata ?. selectedContexts ?? [ ] ;
41+
42+ const [ selectedItems , setSelectedItems ] = useState < ContextItem [ ] > ( [
43+ ...defaultSelectedRepos . map ( repoName => {
44+ const repoInfo = repos . find ( r => r . repoName === repoName ) ;
45+ return {
46+ type : 'repo' as const ,
47+ value : repoName ,
48+ name : repoInfo ?. repoDisplayName || repoName . split ( '/' ) . pop ( ) || repoName ,
49+ codeHostType : repoInfo ?. codeHostType || ''
50+ } ;
51+ } ) ,
52+ ...defaultSelectedContexts . map ( contextName => {
53+ const context = searchContexts . find ( c => c . name === contextName ) ;
54+ return {
55+ type : 'context' as const ,
56+ value : contextName ,
57+ name : contextName ,
58+ repoCount : context ?. repoNames . length || 0
59+ } ;
60+ } )
61+ ] ) ;
3662
3763 useEffect ( ( ) => {
3864 const setChatState = searchParams . get ( SET_CHAT_STATE_QUERY_PARAM ) ;
@@ -41,9 +67,28 @@ export const ChatThreadPanel = ({
4167 }
4268
4369 try {
44- const { inputMessage, selectedRepos } = JSON . parse ( setChatState ) as SetChatStatePayload ;
70+ const { inputMessage, selectedRepos, selectedContexts } = JSON . parse ( setChatState ) as SetChatStatePayload ;
4571 setInputMessage ( inputMessage ) ;
46- setSelectedRepos ( selectedRepos ) ;
72+ setSelectedItems ( [
73+ ...selectedRepos . map ( repoName => {
74+ const repoInfo = repos . find ( r => r . repoName === repoName ) ;
75+ return {
76+ type : 'repo' as const ,
77+ value : repoName ,
78+ name : repoInfo ?. repoDisplayName || repoName . split ( '/' ) . pop ( ) || repoName ,
79+ codeHostType : repoInfo ?. codeHostType || ''
80+ } ;
81+ } ) ,
82+ ...selectedContexts . map ( contextName => {
83+ const context = searchContexts . find ( c => c . name === contextName ) ;
84+ return {
85+ type : 'context' as const ,
86+ value : contextName ,
87+ name : contextName ,
88+ repoCount : context ?. repoNames . length || 0
89+ } ;
90+ } )
91+ ] ) ;
4792 } catch {
4893 console . error ( 'Invalid message in URL' ) ;
4994 }
@@ -52,7 +97,7 @@ export const ChatThreadPanel = ({
5297 const newSearchParams = new URLSearchParams ( searchParams . toString ( ) ) ;
5398 newSearchParams . delete ( SET_CHAT_STATE_QUERY_PARAM ) ;
5499 router . replace ( `?${ newSearchParams . toString ( ) } ` , { scroll : false } ) ;
55- } , [ searchParams , router ] ) ;
100+ } , [ searchParams , router , repos , searchContexts ] ) ;
56101
57102 return (
58103 < ResizablePanel
@@ -67,8 +112,9 @@ export const ChatThreadPanel = ({
67112 inputMessage = { inputMessage }
68113 languageModels = { languageModels }
69114 repos = { repos }
70- selectedRepos = { selectedRepos }
71- onSelectedReposChange = { setSelectedRepos }
115+ searchContexts = { searchContexts }
116+ selectedItems = { selectedItems }
117+ onSelectedItemsChange = { setSelectedItems }
72118 isChatReadonly = { isChatReadonly }
73119 />
74120 </ div >
0 commit comments