33import  {  UICompBuilder  }  from  "comps/generators" ; 
44import  {  NameConfig ,  withExposingConfigs  }  from  "comps/generators/withExposing" ; 
55import  {  StringControl  }  from  "comps/controls/codeControl" ; 
6- import  {  stringExposingStateControl  }  from  "comps/controls/codeStateControl" ; 
6+ import  {  arrayObjectExposingStateControl ,   stringExposingStateControl  }  from  "comps/controls/codeStateControl" ; 
77import  {  withDefault  }  from  "comps/generators" ; 
88import  {  BoolControl  }  from  "comps/controls/boolControl" ; 
99import  {  dropdownControl  }  from  "comps/controls/dropdownControl" ; 
@@ -12,7 +12,7 @@ import { ChatCore } from "./components/ChatCore";
1212import  {  ChatPropertyView  }  from  "./chatPropertyView" ; 
1313import  {  createChatStorage  }  from  "./utils/storageFactory" ; 
1414import  {  QueryHandler ,  createMessageHandler  }  from  "./handlers/messageHandlers" ; 
15- import  {  useMemo   }  from  "react" ; 
15+ import  {  useMemo ,   useRef ,   useEffect   }  from  "react" ;    
1616import  {  changeChildAction  }  from  "lowcoder-core" ; 
1717
1818import  "@assistant-ui/styles/index.css" ; 
@@ -22,15 +22,19 @@ import "@assistant-ui/styles/markdown.css";
2222// SIMPLIFIED CHILDREN MAP - ONLY ESSENTIAL PROPS 
2323// ============================================================================ 
2424
25+ function  generateUniqueTableName ( ) : string  { 
26+   return  `chat${ Math . floor ( 1000  +  Math . random ( )  *  9000 ) }  ; 
27+  } 
28+ 
2529const  ModelTypeOptions  =  [ 
2630  {  label : "Query" ,  value : "query"  } , 
2731  {  label : "N8N Workflow" ,  value : "n8n"  } , 
2832]  as  const ; 
2933
3034export  const  chatChildrenMap  =  { 
3135  // Storage 
32-   tableName :  withDefault ( StringControl ,   "default" ) , 
33-   
36+   // Storage (add the hidden property here) 
37+   _internalDbName :  withDefault ( StringControl ,   "" ) , 
3438  // Message Handler Configuration 
3539  handlerType : dropdownControl ( ModelTypeOptions ,  "query" ) , 
3640  chatQuery : QuerySelectControl ,                     // Only used for "query" type 
@@ -41,8 +45,12 @@ export const chatChildrenMap = {
4145  // UI Configuration   
4246  placeholder : withDefault ( StringControl ,  "Chat Component" ) , 
4347
48+   // Database Information (read-only) 
49+   databaseName : withDefault ( StringControl ,  "" ) , 
50+   
4451  // Exposed Variables (not shown in Property View) 
4552  currentMessage : stringExposingStateControl ( "currentMessage" ,  "" ) , 
53+   conversationHistory : stringExposingStateControl ( "conversationHistory" ,  "[]" ) , 
4654} ; 
4755
4856// ============================================================================ 
@@ -52,10 +60,27 @@ export const chatChildrenMap = {
5260const  ChatTmpComp  =  new  UICompBuilder ( 
5361  chatChildrenMap , 
5462  ( props ,  dispatch )  =>  { 
55-     // Create storage from tableName 
56-     const  storage  =  useMemo ( ( )  =>  
57-       createChatStorage ( props . tableName ) ,  
58-       [ props . tableName ] 
63+ 
64+     const  uniqueTableName  =  useRef < string > ( ) ; 
65+ 
66+       // Generate unique table name once (with persistence) 
67+     if  ( ! uniqueTableName . current )  { 
68+       // Use persisted name if exists, otherwise generate new one 
69+       uniqueTableName . current  =  props . _internalDbName  ||  generateUniqueTableName ( ) ; 
70+       
71+       // Save the name for future refreshes 
72+       if  ( ! props . _internalDbName )  { 
73+         dispatch ( changeChildAction ( "_internalDbName" ,  uniqueTableName . current ,  false ) ) ; 
74+       } 
75+       
76+       // Update the database name in the props for display 
77+       const  dbName  =  `ChatDB_${ uniqueTableName . current }  ; 
78+       dispatch ( changeChildAction ( "databaseName" ,  dbName ,  false ) ) ; 
79+     } 
80+      // Create storage with unique table name 
81+      const  storage  =  useMemo ( ( )  =>  
82+       createChatStorage ( uniqueTableName . current ! ) ,  
83+       [ ] 
5984    ) ; 
6085
6186    // Create message handler based on type 
@@ -96,11 +121,35 @@ const ChatTmpComp = new UICompBuilder(
96121      dispatch ( changeChildAction ( "currentMessage" ,  message ,  false ) ) ; 
97122    } ; 
98123
124+     // Handle conversation history updates for exposed variable 
125+     const  handleConversationUpdate  =  ( conversationHistory : any [ ] )  =>  { 
126+       // Format conversation history for use in queries 
127+       const  formattedHistory  =  conversationHistory . map ( msg  =>  ( { 
128+         role : msg . role , 
129+         content : msg . text , 
130+         timestamp : msg . timestamp 
131+       } ) ) ; 
132+       dispatch ( changeChildAction ( "conversationHistory" ,  JSON . stringify ( formattedHistory ) ,  false ) ) ; 
133+     } ; 
134+ 
135+        // Cleanup on unmount 
136+        useEffect ( ( )  =>  { 
137+         console . log ( "cleanup on unmount" ) ; 
138+         return  ( )  =>  { 
139+           console . log ( "cleanup on unmount" ) ; 
140+           const  tableName  =  uniqueTableName . current ; 
141+           if  ( tableName )  { 
142+             storage . cleanup ( ) ; 
143+           } 
144+         } ; 
145+       } ,  [ ] ) ; 
146+ 
99147    return  ( 
100148      < ChatCore 
101149        storage = { storage } 
102150        messageHandler = { messageHandler } 
103151        onMessageUpdate = { handleMessageUpdate } 
152+         onConversationUpdate = { handleConversationUpdate } 
104153      /> 
105154    ) ; 
106155  } 
@@ -114,4 +163,5 @@ const ChatTmpComp = new UICompBuilder(
114163
115164export  const  ChatComp  =  withExposingConfigs ( ChatTmpComp ,  [ 
116165  new  NameConfig ( "currentMessage" ,  "Current user message" ) , 
166+   new  NameConfig ( "conversationHistory" ,  "Full conversation history as JSON array" ) , 
117167] ) ; 
0 commit comments