@@ -7,6 +7,7 @@ import type { aiChat } from "@/trigger/chat";
77import { useCallback , useEffect , useState } from "react" ;
88import { Chat } from "@/components/chat" ;
99import { ChatSidebar } from "@/components/chat-sidebar" ;
10+ import { DEFAULT_MODEL } from "@/lib/models" ;
1011import {
1112 getChatToken ,
1213 getChatList ,
@@ -19,18 +20,22 @@ import {
1920type ChatMeta = {
2021 id : string ;
2122 title : string ;
23+ model : string ;
2224 createdAt : number ;
2325 updatedAt : number ;
2426} ;
2527
28+ type SessionInfo = {
29+ runId : string ;
30+ publicAccessToken : string ;
31+ lastEventId ?: string ;
32+ } ;
33+
2634type ChatAppProps = {
2735 initialChatList : ChatMeta [ ] ;
2836 initialActiveChatId : string | null ;
2937 initialMessages : UIMessage [ ] ;
30- initialSessions : Record <
31- string ,
32- { runId : string ; publicAccessToken : string ; lastEventId ?: string }
33- > ;
38+ initialSessions : Record < string , SessionInfo > ;
3439} ;
3540
3641export function ChatApp ( {
@@ -42,15 +47,21 @@ export function ChatApp({
4247 const [ chatList , setChatList ] = useState < ChatMeta [ ] > ( initialChatList ) ;
4348 const [ activeChatId , setActiveChatId ] = useState < string | null > ( initialActiveChatId ) ;
4449 const [ messages , setMessages ] = useState < UIMessage [ ] > ( initialMessages ) ;
50+ const [ sessions , setSessions ] = useState < Record < string , SessionInfo > > ( initialSessions ) ;
51+
52+ // Model for new chats (before first message is sent)
53+ const [ newChatModel , setNewChatModel ] = useState ( DEFAULT_MODEL ) ;
4554
4655 const handleSessionChange = useCallback (
47- (
48- chatId : string ,
49- session : { runId : string ; publicAccessToken : string ; lastEventId ?: string } | null
50- ) => {
51- // Session creation and token updates are handled server-side via onChatStart/onTurnComplete.
52- // We only need to clean up when the run ends (session = null).
53- if ( ! session ) {
56+ ( chatId : string , session : SessionInfo | null ) => {
57+ if ( session ) {
58+ setSessions ( ( prev ) => ( { ...prev , [ chatId ] : session } ) ) ;
59+ } else {
60+ setSessions ( ( prev ) => {
61+ const next = { ...prev } ;
62+ delete next [ chatId ] ;
63+ return next ;
64+ } ) ;
5465 deleteSessionAction ( chatId ) ;
5566 }
5667 } ,
@@ -86,6 +97,7 @@ export function ChatApp({
8697 const id = generateId ( ) ;
8798 setActiveChatId ( id ) ;
8899 setMessages ( [ ] ) ;
100+ setNewChatModel ( DEFAULT_MODEL ) ;
89101 }
90102
91103 function handleSelectChat ( id : string ) {
@@ -119,6 +131,14 @@ export function ChatApp({
119131 setChatList ( list ) ;
120132 } , [ ] ) ;
121133
134+ // Determine the model for the active chat
135+ const activeChatMeta = chatList . find ( ( c ) => c . id === activeChatId ) ;
136+ const isNewChat = activeChatId != null && ! activeChatMeta ;
137+ const activeModel = isNewChat ? newChatModel : ( activeChatMeta ?. model ?? DEFAULT_MODEL ) ;
138+
139+ // Get session for the active chat
140+ const activeSession = activeChatId ? sessions [ activeChatId ] : undefined ;
141+
122142 return (
123143 < main className = "flex h-screen" >
124144 < ChatSidebar
@@ -136,6 +156,11 @@ export function ChatApp({
136156 initialMessages = { messages }
137157 transport = { transport }
138158 resume = { messages . length > 0 }
159+ model = { activeModel }
160+ isNewChat = { isNewChat }
161+ onModelChange = { isNewChat ? setNewChatModel : undefined }
162+ session = { activeSession }
163+ dashboardUrl = { process . env . NEXT_PUBLIC_TRIGGER_DASHBOARD_URL }
139164 onFirstMessage = { handleFirstMessage }
140165 onMessagesChange = { handleMessagesChange }
141166 />
0 commit comments