@@ -307,8 +307,30 @@ class ChatStore {
307
307
onError ?: ( error : Error ) => void
308
308
) : Promise < void > {
309
309
let streamedContent = '' ;
310
-
311
310
let streamedReasoningContent = '' ;
311
+ let modelCaptured = false ;
312
+
313
+ const captureModelIfNeeded = ( updateDbImmediately = true ) : string | undefined => {
314
+ if ( ! modelCaptured ) {
315
+ const currentModelName = serverStore . modelName ;
316
+
317
+ if ( currentModelName ) {
318
+ if ( updateDbImmediately ) {
319
+ DatabaseStore . updateMessage ( assistantMessage . id , { model : currentModelName } ) . catch (
320
+ console . error
321
+ ) ;
322
+ }
323
+
324
+ const messageIndex = this . findMessageIndex ( assistantMessage . id ) ;
325
+
326
+ this . updateMessageAtIndex ( messageIndex , { model : currentModelName } ) ;
327
+ modelCaptured = true ;
328
+
329
+ return currentModelName ;
330
+ }
331
+ }
332
+ return undefined ;
333
+ } ;
312
334
313
335
slotsService . startStreaming ( ) ;
314
336
@@ -319,6 +341,8 @@ class ChatStore {
319
341
streamedContent += chunk ;
320
342
this . currentResponse = streamedContent ;
321
343
344
+ captureModelIfNeeded ( ) ;
345
+
322
346
const partialThinking = extractPartialThinking ( streamedContent ) ;
323
347
const messageIndex = this . findMessageIndex ( assistantMessage . id ) ;
324
348
this . updateMessageAtIndex ( messageIndex , {
@@ -328,7 +352,11 @@ class ChatStore {
328
352
329
353
onReasoningChunk : ( reasoningChunk : string ) => {
330
354
streamedReasoningContent += reasoningChunk ;
355
+
356
+ captureModelIfNeeded ( ) ;
357
+
331
358
const messageIndex = this . findMessageIndex ( assistantMessage . id ) ;
359
+
332
360
this . updateMessageAtIndex ( messageIndex , { thinking : streamedReasoningContent } ) ;
333
361
} ,
334
362
@@ -339,17 +367,36 @@ class ChatStore {
339
367
) => {
340
368
slotsService . stopStreaming ( ) ;
341
369
342
- await DatabaseStore . updateMessage ( assistantMessage . id , {
370
+ const updateData : {
371
+ content : string ;
372
+ thinking : string ;
373
+ timings ?: ChatMessageTimings ;
374
+ model ?: string ;
375
+ } = {
343
376
content : finalContent || streamedContent ,
344
377
thinking : reasoningContent || streamedReasoningContent ,
345
378
timings : timings
346
- } ) ;
379
+ } ;
380
+
381
+ const capturedModel = captureModelIfNeeded ( false ) ;
382
+
383
+ if ( capturedModel ) {
384
+ updateData . model = capturedModel ;
385
+ }
386
+
387
+ await DatabaseStore . updateMessage ( assistantMessage . id , updateData ) ;
347
388
348
389
const messageIndex = this . findMessageIndex ( assistantMessage . id ) ;
349
390
350
- this . updateMessageAtIndex ( messageIndex , {
391
+ const localUpdateData : { timings ?: ChatMessageTimings ; model ?: string } = {
351
392
timings : timings
352
- } ) ;
393
+ } ;
394
+
395
+ if ( updateData . model ) {
396
+ localUpdateData . model = updateData . model ;
397
+ }
398
+
399
+ this . updateMessageAtIndex ( messageIndex , localUpdateData ) ;
353
400
354
401
await DatabaseStore . updateCurrentNode ( this . activeConversation ! . id , assistantMessage . id ) ;
355
402
this . activeConversation ! . currNode = assistantMessage . id ;
@@ -478,9 +525,6 @@ class ChatStore {
478
525
private async createAssistantMessage ( parentId ?: string ) : Promise < DatabaseMessage | null > {
479
526
if ( ! this . activeConversation ) return null ;
480
527
481
- // Capture the current model name when creating the assistant message
482
- const currentModelName = serverStore . modelName ;
483
-
484
528
return await DatabaseStore . createMessageBranch (
485
529
{
486
530
convId : this . activeConversation . id ,
@@ -489,8 +533,7 @@ class ChatStore {
489
533
content : '' ,
490
534
timestamp : Date . now ( ) ,
491
535
thinking : '' ,
492
- children : [ ] ,
493
- model : currentModelName || undefined
536
+ children : [ ]
494
537
} ,
495
538
parentId || null
496
539
) ;
@@ -1287,9 +1330,6 @@ class ChatStore {
1287
1330
this . isLoading = true ;
1288
1331
this . currentResponse = '' ;
1289
1332
1290
- // Capture the current model name when creating the assistant message
1291
- const currentModelName = serverStore . modelName ;
1292
-
1293
1333
const newAssistantMessage = await DatabaseStore . createMessageBranch (
1294
1334
{
1295
1335
convId : this . activeConversation . id ,
@@ -1298,8 +1338,7 @@ class ChatStore {
1298
1338
role : 'assistant' ,
1299
1339
content : '' ,
1300
1340
thinking : '' ,
1301
- children : [ ] ,
1302
- model : currentModelName || undefined
1341
+ children : [ ]
1303
1342
} ,
1304
1343
parentMessage . id
1305
1344
) ;
@@ -1346,9 +1385,6 @@ class ChatStore {
1346
1385
false
1347
1386
) as DatabaseMessage [ ] ;
1348
1387
1349
- // Capture the current model name when creating the assistant message
1350
- const currentModelName = serverStore . modelName ;
1351
-
1352
1388
// Create new assistant message branch
1353
1389
const assistantMessage = await DatabaseStore . createMessageBranch (
1354
1390
{
@@ -1358,8 +1394,7 @@ class ChatStore {
1358
1394
role : 'assistant' ,
1359
1395
content : '' ,
1360
1396
thinking : '' ,
1361
- children : [ ] ,
1362
- model : currentModelName || undefined
1397
+ children : [ ]
1363
1398
} ,
1364
1399
userMessageId
1365
1400
) ;
0 commit comments