@@ -14,12 +14,12 @@ interface UseChatMessagingProps {
1414 activeChat : ChatSession | null ;
1515 personas : Persona [ ] ;
1616 setChats : React . Dispatch < React . SetStateAction < ChatSession [ ] > > ;
17-
1817 setActiveChatId : React . Dispatch < React . SetStateAction < string | null > > ;
1918 addToast : ( message : string , type : 'success' | 'error' | 'info' ) => void ;
19+ availableModels : string [ ] ;
2020}
2121
22- export const useChatMessaging = ( { settings, activeChat, personas, setChats, setActiveChatId, addToast } : UseChatMessagingProps ) => {
22+ export const useChatMessaging = ( { settings, activeChat, personas, setChats, setActiveChatId, addToast, availableModels } : UseChatMessagingProps ) => {
2323 const [ isLoading , setIsLoading ] = useState ( false ) ;
2424 const isCancelledRef = useRef ( false ) ;
2525 let inactivityTimer : NodeJS . Timeout ; // For stream watchdog
@@ -29,7 +29,7 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
2929 setIsLoading ( false ) ;
3030 } , [ ] ) ;
3131
32- const _initiateStream = useCallback ( async ( chatId : string , historyForAPI : Message [ ] , personaId : string | null | undefined , titleGenerationMode : 'INITIAL' | 'RECURRING' | null = null ) => {
32+ const _initiateStream = useCallback ( async ( chatId : string , historyForAPI : Message [ ] , personaId : string | null | undefined , titleGenerationMode : 'INITIAL' | 'RECURRING' | null = null , availableModels : string [ ] = [ ] ) => {
3333 // 获取 API Key:如果用户启用了自定义,使用用户的配置;否则使用环境变量
3434 let apiKeys : string [ ] = [ ] ;
3535 if ( settings . useCustomApi ) {
@@ -53,9 +53,12 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
5353 isCancelledRef . current = false ;
5454 setIsLoading ( true ) ;
5555
56- const chatSession = activeChat && activeChat . id === chatId
57- ? activeChat
58- : { id : chatId , messages : historyForAPI , model : settings . defaultModel , personaId, title : "New Chat" , createdAt : Date . now ( ) , folderId : null } ;
56+ // 确定使用的模型:优先使用 lastSelectedModel,否则使用模型列表第一个
57+ const defaultModel = settings . lastSelectedModel ?? availableModels [ 0 ] ?? '' ;
58+
59+ const chatSession = activeChat && activeChat . id === chatId
60+ ? activeChat
61+ : { id : chatId , messages : historyForAPI , model : defaultModel , personaId, title : "New Chat" , createdAt : Date . now ( ) , folderId : null } ;
5962
6063 const activePersona = chatSession . personaId ? personas . find ( p => p && p . id === chatSession . personaId ) : null ;
6164
@@ -348,8 +351,8 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
348351 if ( ! currentChatId ) {
349352 currentPersonaId = settings . defaultPersona ;
350353 const persona = personas . find ( p => p . id === currentPersonaId ) ;
351- // 优先级:用户最后选择的模型 > 角色默认模型 > 系统默认模型
352- const modelToUse = settings . lastSelectedModel ?? persona ?. model ?? settings . defaultModel ;
354+ // 优先级:角色默认模型 > 用户最后选择的模型 > 模型列表第一个
355+ const modelToUse = persona ?. model ?? settings . lastSelectedModel ?? availableModels [ 0 ] ?? '' ;
353356 const newChat : ChatSession = { id : crypto . randomUUID ( ) , title : persona ?. name || content . substring ( 0 , 40 ) || "New Chat" , icon : ( persona ?. avatar ?. type === 'emoji' ? persona . avatar . value : '👤' ) || "💬" , messages : [ userMessage ] , createdAt : Date . now ( ) , model : modelToUse , folderId : null , personaId : currentPersonaId } ;
354357 currentChatId = newChat . id ;
355358 history = newChat . messages ;
@@ -372,8 +375,8 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
372375 historyForAPI = [ ...history . slice ( 0 , - 1 ) , messageWithPDF ] ;
373376 }
374377
375- await _initiateStream ( currentChatId , historyForAPI , currentPersonaId , titleGenerationMode ) ;
376- } , [ activeChat , settings , setChats , setActiveChatId , _initiateStream , personas ] ) ;
378+ await _initiateStream ( currentChatId , historyForAPI , currentPersonaId , titleGenerationMode , availableModels ) ;
379+ } , [ activeChat , settings , setChats , setActiveChatId , _initiateStream , personas , availableModels ] ) ;
377380
378381 const handleDeleteMessage = useCallback ( ( messageId : string ) => {
379382 if ( ! activeChat ?. id ) return ;
@@ -421,9 +424,9 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
421424
422425 if ( historyForResubmit . length > 0 ) {
423426 setChats ( prev => prev . map ( c => c . id === chatId ? { ...c , messages : historyForResubmit } : c ) ) ;
424- _initiateStream ( chatId , historyForResubmit , activeChat . personaId ) ;
427+ _initiateStream ( chatId , historyForResubmit , activeChat . personaId , null , availableModels ) ;
425428 }
426- } , [ activeChat , isLoading , setChats , _initiateStream ] ) ;
429+ } , [ activeChat , isLoading , setChats , _initiateStream , availableModels ] ) ;
427430
428431 const handleEditAndResubmit = useCallback ( ( messageId : string , newContent : string ) => {
429432 if ( ! activeChat ?. id || isLoading ) return ;
@@ -440,9 +443,9 @@ export const useChatMessaging = ({ settings, activeChat, personas, setChats, set
440443
441444 if ( historyForResubmit . length > 0 ) {
442445 setChats ( prev => prev . map ( c => c . id === chatId ? { ...c , messages : historyForResubmit } : c ) ) ;
443- _initiateStream ( chatId , historyForResubmit , activeChat . personaId ) ;
446+ _initiateStream ( chatId , historyForResubmit , activeChat . personaId , null , availableModels ) ;
444447 }
445- } , [ activeChat , isLoading , setChats , _initiateStream ] ) ;
448+ } , [ activeChat , isLoading , setChats , _initiateStream , availableModels ] ) ;
446449
447450 return {
448451 isLoading,
0 commit comments