@@ -3,8 +3,6 @@ import { DatabaseService } from '$lib/services';
33import { goto } from '$app/navigation' ;
44import { browser } from '$app/environment' ;
55import { extractPartialThinking } from '$lib/utils/thinking' ;
6- import { wouldExceedContextLength } from '$lib/utils/token-estimation' ;
7- import { serverStore } from '$lib/stores/server.svelte' ;
86import { config } from '$lib/stores/settings.svelte' ;
97
108class ChatStore {
@@ -14,7 +12,7 @@ class ChatStore {
1412 currentResponse = $state ( '' ) ;
1513 isInitialized = $state ( false ) ;
1614 isLoading = $state ( false ) ;
17- contextError = $state < { message : string ; estimatedTokens : number ; maxAllowed : number ; maxContext : number } | null > ( null ) ;
15+ maxContextError = $state < { message : string ; estimatedTokens : number ; maxAllowed : number ; maxContext : number } | null > ( null ) ;
1816 private chatService = new ChatService ( ) ;
1917
2018 constructor ( ) {
@@ -242,32 +240,6 @@ class ChatStore {
242240 async sendMessage ( content : string , extras ?: DatabaseMessageExtra [ ] ) : Promise < void > {
243241 if ( ! content . trim ( ) || this . isLoading ) return ;
244242
245- // Check context length BEFORE creating conversation or processing anything
246- const maxContextLength = serverStore . serverProps ?. default_generation_settings . n_ctx ;
247- if ( maxContextLength ) {
248- const contextCheck = wouldExceedContextLength (
249- this . activeMessages ,
250- content ,
251- extras ,
252- maxContextLength
253- ) ;
254-
255- if ( contextCheck . wouldExceed ) {
256- const errorMessage = `Message too long for context window. Estimated tokens: ${ contextCheck . estimatedTokens . toLocaleString ( ) } , Maximum allowed: ${ contextCheck . maxAllowed . toLocaleString ( ) } (Context: ${ maxContextLength . toLocaleString ( ) } )` ;
257- console . error ( 'Context length exceeded:' , errorMessage ) ;
258-
259- // Set context error state for UI to display alert dialog
260- this . contextError = {
261- message : errorMessage ,
262- estimatedTokens : contextCheck . estimatedTokens ,
263- maxAllowed : contextCheck . maxAllowed ,
264- maxContext : maxContextLength
265- } ;
266- // Early return - prevent any conversation creation or message processing
267- return ;
268- }
269- }
270-
271243 let isNewConversation = false ;
272244
273245 if ( ! this . activeConversation ) {
@@ -340,8 +312,13 @@ class ChatStore {
340312 /**
341313 * Clear context error state
342314 */
343- clearContextError ( ) : void {
344- this . contextError = null ;
315+ clearMaxContextError ( ) : void {
316+ this . maxContextError = null ;
317+ }
318+
319+ // Allow external modules to set context error without importing heavy utils here
320+ setMaxContextError ( error : { message : string ; estimatedTokens : number ; maxAllowed : number ; maxContext : number } | null ) : void {
321+ this . maxContextError = error ;
345322 }
346323
347324 private async savePartialResponseIfNeeded ( ) {
@@ -573,7 +550,7 @@ export const activeMessages = () => chatStore.activeMessages;
573550export const isLoading = ( ) => chatStore . isLoading ;
574551export const currentResponse = ( ) => chatStore . currentResponse ;
575552export const isInitialized = ( ) => chatStore . isInitialized ;
576- export const contextError = ( ) => chatStore . contextError ;
553+ export const maxContextError = ( ) => chatStore . maxContextError ;
577554
578555export const createConversation = chatStore . createConversation . bind ( chatStore ) ;
579556export const loadConversation = chatStore . loadConversation . bind ( chatStore ) ;
@@ -584,7 +561,8 @@ export const updateConversationName = chatStore.updateConversationName.bind(chat
584561export const deleteConversation = chatStore . deleteConversation . bind ( chatStore ) ;
585562export const clearActiveConversation = chatStore . clearActiveConversation . bind ( chatStore ) ;
586563export const gracefulStop = chatStore . gracefulStop . bind ( chatStore ) ;
587- export const clearContextError = chatStore . clearContextError . bind ( chatStore ) ;
564+ export const clearMaxContextError = chatStore . clearMaxContextError . bind ( chatStore ) ;
565+ export const setMaxContextError = chatStore . setMaxContextError . bind ( chatStore ) ;
588566
589567export function stopGeneration ( ) {
590568 chatStore . stopGeneration ( ) ;
0 commit comments