@@ -574,7 +574,29 @@ export class ChatActions extends Actions<ChatState> {
574574 return true ;
575575 } ;
576576
577- markThreadRead = ( threadKey : string , count : number ) : boolean => {
577+ setThreadPin = ( threadKey : string , pinned : boolean ) : boolean => {
578+ if ( this . syncdb == null ) {
579+ return false ;
580+ }
581+ const entry = this . getThreadRootDoc ( threadKey ) ;
582+ if ( entry == null ) {
583+ return false ;
584+ }
585+ if ( pinned ) {
586+ entry . doc . pin = true ;
587+ } else {
588+ entry . doc . pin = false ;
589+ }
590+ this . syncdb . set ( entry . doc ) ;
591+ this . syncdb . commit ( ) ;
592+ return true ;
593+ } ;
594+
595+ markThreadRead = (
596+ threadKey : string ,
597+ count : number ,
598+ commit = true ,
599+ ) : boolean => {
578600 if ( this . syncdb == null ) {
579601 return false ;
580602 }
@@ -588,7 +610,9 @@ export class ChatActions extends Actions<ChatState> {
588610 }
589611 entry . doc [ `read-${ account_id } ` ] = count ;
590612 this . syncdb . set ( entry . doc ) ;
591- this . syncdb . commit ( ) ;
613+ if ( commit ) {
614+ this . syncdb . commit ( ) ;
615+ }
592616 return true ;
593617 } ;
594618
@@ -752,30 +776,44 @@ export class ChatActions extends Actions<ChatState> {
752776 * This checks a thread of messages to see if it is a language model thread and if so, returns it.
753777 */
754778 isLanguageModelThread = ( date ?: Date ) : false | LanguageModel => {
755- if ( date == null ) {
779+ if ( date == null || this . store == null ) {
756780 return false ;
757781 }
758- const thread = this . getMessagesInThread ( date . toISOString ( ) ) ;
759- if ( thread == null ) {
782+ const messages = this . store . get ( "messages" ) ;
783+ if ( messages == null ) {
784+ return false ;
785+ }
786+ const rootMs =
787+ getThreadRootDate ( { date : date . valueOf ( ) , messages } ) || date . valueOf ( ) ;
788+ const entry = this . getThreadRootDoc ( `${ rootMs } ` ) ;
789+ const rootMessage = entry ?. message ;
790+ if ( rootMessage == null ) {
760791 return false ;
761792 }
762793
763- // We deliberately start at the last most recent message.
764- // Why? If we use the LLM regenerate dropdown button to change the LLM, we want to keep it.
765- for ( const message of thread . reverse ( ) ) {
766- const lastHistory = message . get ( "history" ) ?. first ( ) ;
767- // this must be an invalid message, because there is no history
768- if ( lastHistory == null ) continue ;
769- const sender_id = lastHistory . get ( "author_id" ) ;
770- if ( isLanguageModelService ( sender_id ) ) {
771- return service2model ( sender_id ) ;
772- }
773- const input = lastHistory . get ( "content" ) ?. toLowerCase ( ) ;
774- if ( mentionsLanguageModel ( input ) ) {
775- return getLanguageModel ( input ) ;
776- }
794+ const thread = this . getMessagesInThread (
795+ rootMessage . get ( "date" ) ?. toISOString ?.( ) ?? `${ rootMs } ` ,
796+ ) ;
797+ if ( thread == null ) {
798+ return false ;
777799 }
778800
801+ const firstMessage = thread . first ( ) ;
802+ if ( firstMessage == null ) {
803+ return false ;
804+ }
805+ const firstHistory = firstMessage . get ( "history" ) ?. first ( ) ;
806+ if ( firstHistory == null ) {
807+ return false ;
808+ }
809+ const sender_id = firstHistory . get ( "author_id" ) ;
810+ if ( isLanguageModelService ( sender_id ) ) {
811+ return service2model ( sender_id ) ;
812+ }
813+ const input = firstHistory . get ( "content" ) ?. toLowerCase ( ) ;
814+ if ( mentionsLanguageModel ( input ) ) {
815+ return getLanguageModel ( input ) ;
816+ }
779817 return false ;
780818 } ;
781819
@@ -1270,7 +1308,6 @@ export class ChatActions extends Actions<ChatState> {
12701308 } ;
12711309
12721310 setSelectedThread = ( threadKey : string | null ) => {
1273- console . log ( "setSelectedThread" , { threadKey } ) ;
12741311 this . frameTreeActions ?. set_frame_data ( {
12751312 id : this . frameId ,
12761313 selectedThreadKey : threadKey ,
0 commit comments