@@ -779,6 +779,8 @@ export default class ThothPlugin extends Plugin {
779779 ` ;
780780
781781 // Handle quick message sending
782+ // NOTE: This uses the same Letta API as the full chat (MultiChatModal.sendMessage)
783+ // but without streaming for faster, simpler responses in the minimized pill view
782784 const sendQuickMessage = async ( ) => {
783785 const message = inputEl . value . trim ( ) ;
784786 if ( ! message || sendBtn . disabled ) return ;
@@ -793,42 +795,75 @@ export default class ThothPlugin extends Plugin {
793795 sendBtn . textContent = 'Asking...' ;
794796
795797 try {
796- // Use the chat modal instance to send message
797- if ( this . chatModalInstance && this . chatModalInstance . activeSessionId ) {
798- // Send to server using existing chat functionality
799- const endpoint = this . getEndpointUrl ( ) ;
800- const response = await fetch ( `${ endpoint } /research/chat` , {
801- method : 'POST' ,
802- headers : { 'Content-Type' : 'application/json' } ,
803- body : JSON . stringify ( {
804- message : message ,
805- conversation_id : this . chatModalInstance . activeSessionId ,
806- timestamp : Date . now ( ) ,
807- id : crypto . randomUUID ( )
808- } )
809- } ) ;
810-
811- if ( response . ok ) {
812- const result = await response . json ( ) ;
813- const assistantResponse = result . response ;
814-
815- // Show response
816- responseArea . textContent = assistantResponse . length > 150 ?
817- assistantResponse . substring ( 0 , 150 ) + '...' : assistantResponse ;
818-
819- // Update full chat in background
820- if ( this . chatModalInstance ) {
821- await this . chatModalInstance . loadChatSessions ( ) ;
822- this . chatModalInstance . renderSessionList ( ) ;
798+ // Ensure we have a chat modal instance
799+ if ( ! this . chatModalInstance ) {
800+ throw new Error ( 'Chat system not initialized' ) ;
801+ }
802+
803+ // Ensure we have an active session, create one if needed
804+ let conversationId = this . chatModalInstance . activeSessionId ;
805+ if ( ! conversationId ) {
806+ responseArea . textContent = 'Initializing chat session...' ;
807+ try {
808+ conversationId = await this . chatModalInstance . getOrCreateDefaultConversation ( ) ;
809+ this . chatModalInstance . activeSessionId = conversationId ;
810+ await this . chatModalInstance . loadChatSessions ( ) ;
811+ } catch ( initError ) {
812+ throw new Error ( `Failed to initialize chat: ${ initError . message } ` ) ;
813+ }
814+ }
815+
816+ // Send to server using Letta chat endpoint (same as full chat but without streaming)
817+ const endpoint = this . getLettaEndpointUrl ( ) ;
818+
819+ const response = await fetch ( `${ endpoint } /v1/conversations/${ conversationId } /messages` , {
820+ method : 'POST' ,
821+ headers : { 'Content-Type' : 'application/json' } ,
822+ body : JSON . stringify ( {
823+ input : message ,
824+ streaming : false // No streaming for quick responses
825+ } )
826+ } ) ;
827+
828+ if ( response . ok ) {
829+ const result = await response . json ( ) ;
830+
831+ // Extract assistant response from Letta response format
832+ // Letta returns an array of messages - find the last assistant message
833+ let assistantResponse = 'No response received' ;
834+
835+ if ( Array . isArray ( result . messages ) ) {
836+ // Find last assistant_message
837+ const assistantMsg = [ ...result . messages ]
838+ . reverse ( )
839+ . find ( ( m : any ) => m . message_type === 'assistant_message' ) ;
840+
841+ if ( assistantMsg ) {
842+ // Extract text from assistant message
843+ assistantResponse = assistantMsg . assistant_message ?. text ||
844+ assistantMsg . text ||
845+ assistantMsg . content ||
846+ assistantResponse ;
823847 }
824- } else {
825- throw new Error ( 'Failed to send message' ) ;
848+ } else if ( result . response ) {
849+ assistantResponse = result . response ;
826850 }
851+
852+ // Show response
853+ responseArea . style . color = 'var(--text-muted)' ;
854+ responseArea . textContent = assistantResponse . length > 150 ?
855+ assistantResponse . substring ( 0 , 150 ) + '...' : assistantResponse ;
856+
857+ // Update full chat in background
858+ await this . chatModalInstance . loadChatSessions ( ) ;
859+ this . chatModalInstance . renderSessionList ( ) ;
827860 } else {
828- throw new Error ( 'No active chat session' ) ;
861+ const errorData = await response . json ( ) . catch ( ( ) => ( { } ) ) ;
862+ throw new Error ( errorData . message || errorData . detail || `Server error: ${ response . status } ` ) ;
829863 }
830864 } catch ( error ) {
831865 console . error ( 'Quick chat error:' , error ) ;
866+ responseArea . style . color = 'var(--text-error)' ;
832867 responseArea . textContent = `Error: ${ error . message } ` ;
833868 } finally {
834869 sendBtn . disabled = false ;
0 commit comments