11import { DatabaseStore } from '$lib/stores/database' ;
22import { chatService , slotsService } from '$lib/services' ;
33import { config } from '$lib/stores/settings.svelte' ;
4+ import { serverStore } from '$lib/stores/server.svelte' ;
45import { normalizeModelName } from '$lib/utils/model-names' ;
56import { filterByLeafNodeId , findLeafNode , findDescendantMessages } from '$lib/utils/branching' ;
67import { browser } from '$app/environment' ;
@@ -364,8 +365,75 @@ class ChatStore {
364365
365366 let resolvedModel : string | null = null ;
366367 let modelPersisted = false ;
368+ const PROPS_REFRESH_RETRY_DELAY_MS = 1_000 ;
369+ let serverPropsRefreshRequested = false ;
370+ let lastPropsRefreshAttempt = 0 ;
371+
372+ const resetPropsRefreshGate = ( options ?: { immediate ?: boolean } ) => {
373+ serverPropsRefreshRequested = false ;
374+ if ( options ?. immediate ) {
375+ lastPropsRefreshAttempt = Date . now ( ) - PROPS_REFRESH_RETRY_DELAY_MS ;
376+ } else {
377+ lastPropsRefreshAttempt = Date . now ( ) ;
378+ }
379+ } ;
380+
381+ const ensureServerPropsRefresh = ( ) => {
382+ const now = Date . now ( ) ;
383+
384+ if ( serverPropsRefreshRequested ) {
385+ if ( resolvedModel ) {
386+ const currentModel = serverStore . modelName ;
387+ const normalizedStoreModel = currentModel ? normalizeModelName ( currentModel ) : null ;
388+
389+ if ( ! normalizedStoreModel || normalizedStoreModel !== resolvedModel ) {
390+ resetPropsRefreshGate ( { immediate : true } ) ;
391+ } else {
392+ return ;
393+ }
394+ } else {
395+ return ;
396+ }
397+ }
398+
399+ if ( now - lastPropsRefreshAttempt < PROPS_REFRESH_RETRY_DELAY_MS ) {
400+ return ;
401+ }
402+
403+ serverPropsRefreshRequested = true ;
404+ lastPropsRefreshAttempt = now ;
405+
406+ const hasExistingProps = serverStore . serverProps !== null ;
407+
408+ serverStore
409+ . fetchServerProps ( { silent : hasExistingProps } )
410+ . then ( ( ) => {
411+ if ( ! resolvedModel ) {
412+ return ;
413+ }
414+
415+ const currentModel = serverStore . modelName ;
416+
417+ if ( ! currentModel ) {
418+ resetPropsRefreshGate ( { immediate : true } ) ;
419+ return ;
420+ }
421+
422+ const normalizedStoreModel = normalizeModelName ( currentModel ) ;
423+
424+ if ( ! normalizedStoreModel || normalizedStoreModel !== resolvedModel ) {
425+ resetPropsRefreshGate ( { immediate : true } ) ;
426+ }
427+ } )
428+ . catch ( ( error ) => {
429+ console . error ( 'Failed to refresh server props during streaming:' , error ) ;
430+ resetPropsRefreshGate ( ) ;
431+ } ) ;
432+ } ;
367433
368434 const recordModel = ( modelName : string , persistImmediately = true ) : void => {
435+ ensureServerPropsRefresh ( ) ;
436+
369437 const normalizedModel = normalizeModelName ( modelName ) ;
370438
371439 if ( ! normalizedModel || normalizedModel === resolvedModel ) {
@@ -399,6 +467,8 @@ class ChatStore {
399467 ...this . getApiOptions ( ) ,
400468
401469 onChunk : ( chunk : string ) => {
470+ ensureServerPropsRefresh ( ) ;
471+
402472 streamedContent += chunk ;
403473 this . setConversationStreaming (
404474 assistantMessage . convId ,
@@ -413,6 +483,8 @@ class ChatStore {
413483 } ,
414484
415485 onReasoningChunk : ( reasoningChunk : string ) => {
486+ ensureServerPropsRefresh ( ) ;
487+
416488 streamedReasoningContent += reasoningChunk ;
417489
418490 const messageIndex = this . findMessageIndex ( assistantMessage . id ) ;
@@ -425,6 +497,8 @@ class ChatStore {
425497 } ,
426498
427499 onToolCallChunk : ( toolCallChunk : string ) => {
500+ ensureServerPropsRefresh ( ) ;
501+
428502 const chunk = toolCallChunk . trim ( ) ;
429503
430504 if ( ! chunk ) {
@@ -444,6 +518,8 @@ class ChatStore {
444518 timings ?: ChatMessageTimings ,
445519 toolCallContent ?: string
446520 ) => {
521+ ensureServerPropsRefresh ( ) ;
522+
447523 slotsService . stopStreaming ( ) ;
448524
449525 const updateData : {
0 commit comments