@@ -14,7 +14,6 @@ import { SchemaContextBuilder } from "@/lib/schema-context-builder";
1414import { ConversationAnalyzer } from "@/lib/conversation-analyzer" ;
1515import { StorageUtils } from "@/lib/storage-utils" ;
1616import { schemaCacheAtom , loadAndCacheTableSchemaAtom } from "./database-atoms" ;
17- import { apiUrlAtom } from "./connection-atoms" ;
1817import { getCurrentTabData } from "./tab-atoms" ;
1918import type {
2019 ChatSession ,
@@ -244,7 +243,6 @@ export const sessionListAtom = atom((get) => {
244243 ) ;
245244} ) ;
246245
247-
248246// ============================================================================
249247// Helper Functions
250248// ============================================================================
@@ -332,54 +330,51 @@ export const createSessionAtom = atom(
332330) ;
333331
334332// Cancel streaming message
335- export const cancelMessageAtom = atom (
336- null ,
337- ( get , set , sessionId : string ) => {
338- const abortControllers = get ( activeAbortControllersAtom ) ;
339- const controller = abortControllers . get ( sessionId ) ;
340-
341- if ( controller ) {
342- // Abort the fetch request
343- controller . abort ( ) ;
344-
345- // Remove from active controllers
346- const newControllers = new Map ( abortControllers ) ;
347- newControllers . delete ( sessionId ) ;
348- set ( activeAbortControllersAtom , newControllers ) ;
349-
350- // Update the session to remove streaming flag from the last message
351- const sessions = get ( chatSessionsAtom ) ;
352- const session = sessions [ sessionId ] ;
353- if ( session && session . messages . length > 0 ) {
354- const lastMessage = session . messages [ session . messages . length - 1 ] ;
355- if ( lastMessage . metadata ?. isStreaming ) {
356- const updatedMessages = [ ...session . messages ] ;
357- updatedMessages [ updatedMessages . length - 1 ] = {
358- ...lastMessage ,
359- metadata : {
360- ...lastMessage . metadata ,
361- isStreaming : false ,
362- wasCancelled : true ,
363- } ,
364- } ;
365-
366- set ( chatSessionsAtom , {
367- ...sessions ,
368- [ sessionId ] : {
369- ...session ,
370- messages : updatedMessages ,
371- updatedAt : new Date ( ) . toISOString ( ) ,
372- } ,
373- } ) ;
374- }
333+ export const cancelMessageAtom = atom ( null , ( get , set , sessionId : string ) => {
334+ const abortControllers = get ( activeAbortControllersAtom ) ;
335+ const controller = abortControllers . get ( sessionId ) ;
336+
337+ if ( controller ) {
338+ // Abort the fetch request
339+ controller . abort ( ) ;
340+
341+ // Remove from active controllers
342+ const newControllers = new Map ( abortControllers ) ;
343+ newControllers . delete ( sessionId ) ;
344+ set ( activeAbortControllersAtom , newControllers ) ;
345+
346+ // Update the session to remove streaming flag from the last message
347+ const sessions = get ( chatSessionsAtom ) ;
348+ const session = sessions [ sessionId ] ;
349+ if ( session && session . messages . length > 0 ) {
350+ const lastMessage = session . messages [ session . messages . length - 1 ] ;
351+ if ( lastMessage . metadata ?. isStreaming ) {
352+ const updatedMessages = [ ...session . messages ] ;
353+ updatedMessages [ updatedMessages . length - 1 ] = {
354+ ...lastMessage ,
355+ metadata : {
356+ ...lastMessage . metadata ,
357+ isStreaming : false ,
358+ wasCancelled : true ,
359+ } ,
360+ } ;
361+
362+ set ( chatSessionsAtom , {
363+ ...sessions ,
364+ [ sessionId ] : {
365+ ...session ,
366+ messages : updatedMessages ,
367+ updatedAt : new Date ( ) . toISOString ( ) ,
368+ } ,
369+ } ) ;
375370 }
376-
377- return true ;
378371 }
379-
380- return false ;
372+
373+ return true ;
381374 }
382- ) ;
375+
376+ return false ;
377+ } ) ;
383378
384379// Send message with streaming support
385380export const sendMessageAtom = atom (
@@ -515,7 +510,9 @@ export const sendMessageAtom = atom(
515510
516511 // Get time context from current tab
517512 const currentTab = getCurrentTabData ( ) ;
518- const timeRange = currentTab ?. timeRange ? JSON . stringify ( currentTab . timeRange ) : null ;
513+ const timeRange = currentTab ?. timeRange
514+ ? JSON . stringify ( currentTab . timeRange )
515+ : null ;
519516 const selectedDb = currentTab ?. database || "" ;
520517 const selectedTable = currentTab ?. table || "" ;
521518
@@ -686,7 +683,7 @@ export const sendMessageAtom = atom(
686683 } ,
687684 abortController . signal
688685 ) ;
689-
686+
690687 // Clean up abort controller after successful completion
691688 const currentControllers = get ( activeAbortControllersAtom ) ;
692689 const updatedControllers = new Map ( currentControllers ) ;
@@ -700,9 +697,9 @@ export const sendMessageAtom = atom(
700697 set ( activeAbortControllersAtom , updatedControllers ) ;
701698
702699 // Check if the error is due to abort
703- if ( error instanceof Error && error . name === ' AbortError' ) {
700+ if ( error instanceof Error && error . name === " AbortError" ) {
704701 // Don't show error message for cancelled requests
705- console . log ( ' Message streaming was cancelled' ) ;
702+ console . log ( " Message streaming was cancelled" ) ;
706703 return ;
707704 }
708705
@@ -789,7 +786,11 @@ export const deleteSessionAtom = atom(null, (get, set, sessionId: string) => {
789786// Edit message
790787export const editMessageAtom = atom (
791788 null ,
792- async ( get , set , options : { sessionId : string ; messageId : string ; newContent : string } ) => {
789+ async (
790+ get ,
791+ set ,
792+ options : { sessionId : string ; messageId : string ; newContent : string }
793+ ) => {
793794 const { sessionId, messageId, newContent } = options ;
794795 const sessions = get ( chatSessionsAtom ) ;
795796 const session = sessions [ sessionId ] ;
@@ -844,7 +845,11 @@ export const editMessageAtom = atom(
844845// Regenerate from message
845846export const regenerateFromMessageAtom = atom (
846847 null ,
847- async ( get , set , options : { sessionId : string ; messageId : string ; isAgentic ?: boolean } ) => {
848+ async (
849+ get ,
850+ set ,
851+ options : { sessionId : string ; messageId : string ; isAgentic ?: boolean }
852+ ) => {
848853 const { sessionId, messageId, isAgentic } = options ;
849854 const sessions = get ( chatSessionsAtom ) ;
850855 const session = sessions [ sessionId ] ;
@@ -911,7 +916,9 @@ export const regenerateFromMessageAtom = atom(
911916
912917 // Get time context from current tab
913918 const currentTab = getCurrentTabData ( ) ;
914- const timeRange = currentTab ?. timeRange ? JSON . stringify ( currentTab . timeRange ) : null ;
919+ const timeRange = currentTab ?. timeRange
920+ ? JSON . stringify ( currentTab . timeRange )
921+ : null ;
915922 const selectedDb = currentTab ?. database || "" ;
916923 const selectedTable = currentTab ?. table || "" ;
917924
@@ -963,7 +970,10 @@ export const regenerateFromMessageAtom = atom(
963970 function processChunkForThinking ( text : string ) {
964971 const thinkingStartMatch = text . match ( / < t h i n k > / ) ;
965972 if ( thinkingStartMatch && thinkingStartMatch . index !== undefined ) {
966- const beforeThinking = text . substring ( 0 , thinkingStartMatch . index ) ;
973+ const beforeThinking = text . substring (
974+ 0 ,
975+ thinkingStartMatch . index
976+ ) ;
967977 const afterThinking = text . substring (
968978 thinkingStartMatch . index + thinkingStartMatch [ 0 ] . length
969979 ) ;
@@ -1049,7 +1059,7 @@ export const regenerateFromMessageAtom = atom(
10491059 } ,
10501060 abortController . signal
10511061 ) ;
1052-
1062+
10531063 // Clean up abort controller after successful completion
10541064 const currentControllers = get ( activeAbortControllersAtom ) ;
10551065 const updatedControllers = new Map ( currentControllers ) ;
@@ -1063,8 +1073,8 @@ export const regenerateFromMessageAtom = atom(
10631073 set ( activeAbortControllersAtom , updatedControllers ) ;
10641074
10651075 // Check if the error is due to abort
1066- if ( error instanceof Error && error . name === ' AbortError' ) {
1067- console . log ( ' Message streaming was cancelled' ) ;
1076+ if ( error instanceof Error && error . name === " AbortError" ) {
1077+ console . log ( " Message streaming was cancelled" ) ;
10681078 return ;
10691079 }
10701080
@@ -1122,7 +1132,6 @@ export const renameSessionAtom = atom(
11221132 }
11231133) ;
11241134
1125-
11261135// Delete connection
11271136export const deleteConnectionAtom = atom (
11281137 null ,
@@ -1325,11 +1334,9 @@ Example: When user asks "show me sales by month", respond with:
13251334 includeIndices : true ,
13261335 summaryOnly : false ,
13271336 includeRecentContext : true , // Always include recent context for better continuity
1328- isAgentic, // Pass agentic mode flag
1329- // Enhanced with sample data
1337+ isAgentic,
13301338 includeSampleData : true ,
13311339 sampleDataLimit : 5 ,
1332- apiUrl : queryContext . apiUrl || "http://localhost:7971/query" ,
13331340 }
13341341 ) ;
13351342
0 commit comments