@@ -9,6 +9,7 @@ class ChatMKSearch {
99 this . isLoaded = false ;
1010 this . model = null ;
1111 this . transformersLoaded = false ;
12+ this . embeddingController = null ; // For cancelling embedding downloads
1213 }
1314
1415 /**
@@ -72,7 +73,7 @@ class ChatMKSearch {
7273 /**
7374 * Show embedding model loading progress
7475 */
75- showEmbeddingProgress ( percent , mbLoaded , mbTotal ) {
76+ showEmbeddingProgress ( percent , mbLoaded , mbTotal , customMessage = null ) {
7677 const messagesContainer = document . getElementById ( 'chatmk-messages' ) ;
7778 if ( messagesContainer ) {
7879 // Check if there's already a progress message and update it
@@ -82,19 +83,23 @@ class ChatMKSearch {
8283 // Update existing progress message
8384 const content = existingProgress . querySelector ( '.message-content' ) ;
8485 if ( content ) {
85- if ( percent === 100 ) {
86+ if ( customMessage ) {
87+ content . innerHTML = `<p>${ customMessage } </p>` ;
88+ } else if ( percent === 100 ) {
8689 content . innerHTML = `<p>Embedding model loaded: MiniLM-L6-v2</p>` ;
8790 } else {
88- content . innerHTML = `<p>Embedding model loading: ${ percent } % (${ mbLoaded } /${ mbTotal } MB)</p>` ;
91+ content . innerHTML = `<p>Embedding model loading: ${ percent } % (${ mbLoaded } /${ mbTotal } MB) <span onclick="window.chatMKSearch.cancelEmbeddingDownload()" style="margin-left: 10px; font-size: 11px; color: var(--text-sub); cursor: pointer; text-decoration: underline;">skip magic</span> </p>` ;
8992 }
9093 }
9194 } else {
9295 // Create new progress message
9396 const messageDiv = document . createElement ( 'div' ) ;
9497 messageDiv . className = 'chatmk-message system embedding-loading' ;
95- const messageContent = percent === 100
98+ const messageContent = customMessage
99+ ? `<p>${ customMessage } </p>`
100+ : percent === 100
96101 ? `<p>Embedding model loaded: MiniLM-L6-v2</p>`
97- : `<p>Embedding model loading: ${ percent } % (${ mbLoaded } /${ mbTotal } MB)</p>` ;
102+ : `<p>Embedding model loading: ${ percent } % (${ mbLoaded } /${ mbTotal } MB) <span onclick="window.chatMKSearch.cancelEmbeddingDownload()" style="margin-left: 10px; font-size: 11px; color: var(--text-sub); cursor: pointer; text-decoration: underline;">skip magic</span> </p>` ;
98103
99104 messageDiv . innerHTML = `
100105 <div class="message-content">
@@ -108,6 +113,16 @@ class ChatMKSearch {
108113 }
109114 }
110115
116+ /**
117+ * Cancel the embedding download
118+ */
119+ cancelEmbeddingDownload ( ) {
120+ if ( this . embeddingController ) {
121+ this . embeddingController . abort ( ) ;
122+ console . log ( "Embedding download cancelled" ) ;
123+ }
124+ }
125+
111126 /**
112127 * Simulate embedding model loading progress
113128 */
@@ -116,6 +131,11 @@ class ChatMKSearch {
116131
117132 // Simulate progress from 0% to 100%
118133 for ( let percent = 0 ; percent <= 100 ; percent += 20 ) {
134+ // Check if cancelled
135+ if ( this . embeddingController ?. signal . aborted ) {
136+ throw new Error ( 'Embedding download cancelled by user' ) ;
137+ }
138+
119139 const mbLoaded = Math . round ( ( percent / 100 ) * totalMB ) ;
120140 this . showEmbeddingProgress ( percent , mbLoaded , totalMB ) ;
121141
@@ -134,6 +154,7 @@ class ChatMKSearch {
134154 return ;
135155 }
136156
157+ this . embeddingController = new AbortController ( ) ; // Create abort controller
137158 console . log ( 'ChatMK: Pre-loading embedding model...' ) ;
138159
139160 try {
@@ -160,9 +181,19 @@ class ChatMKSearch {
160181 // Keep showing 100% when done (like AI model)
161182 this . showEmbeddingProgress ( 100 , 25 , 25 ) ;
162183
184+ this . embeddingController = null ;
185+
163186 } catch ( error ) {
187+ this . embeddingController = null ;
188+
189+ if ( error . message . includes ( 'cancelled' ) ) {
190+ console . log ( 'ChatMK: Embedding download cancelled by user' ) ;
191+ this . showEmbeddingProgress ( 0 , 0 , 25 , 'Embedding model skipped → this won\'t work' ) ;
192+ return false ;
193+ }
194+
164195 console . error ( 'ChatMK: Failed to pre-load embedding model:' , error ) ;
165- this . hideModelLoadingMessage ( ) ;
196+ this . showEmbeddingProgress ( 0 , 0 , 25 , 'Embedding model failed → this won\'t work' ) ;
166197 // Don't throw - allow fallback to keyword search
167198 }
168199 }
@@ -378,3 +409,6 @@ class ChatMKSearch {
378409
379410// Export for use in other modules
380411window . ChatMKSearch = ChatMKSearch ;
412+
413+ // Global instance
414+ window . chatMKSearch = new ChatMKSearch ( ) ;
0 commit comments