3
3
import { UICompBuilder } from "comps/generators" ;
4
4
import { NameConfig , withExposingConfigs } from "comps/generators/withExposing" ;
5
5
import { StringControl } from "comps/controls/codeControl" ;
6
- import { stringExposingStateControl } from "comps/controls/codeStateControl" ;
6
+ import { arrayObjectExposingStateControl , stringExposingStateControl } from "comps/controls/codeStateControl" ;
7
7
import { withDefault } from "comps/generators" ;
8
8
import { BoolControl } from "comps/controls/boolControl" ;
9
9
import { dropdownControl } from "comps/controls/dropdownControl" ;
@@ -12,7 +12,7 @@ import { ChatCore } from "./components/ChatCore";
12
12
import { ChatPropertyView } from "./chatPropertyView" ;
13
13
import { createChatStorage } from "./utils/storageFactory" ;
14
14
import { QueryHandler , createMessageHandler } from "./handlers/messageHandlers" ;
15
- import { useMemo } from "react" ;
15
+ import { useMemo , useRef , useEffect } from "react" ;
16
16
import { changeChildAction } from "lowcoder-core" ;
17
17
18
18
import "@assistant-ui/styles/index.css" ;
@@ -22,15 +22,19 @@ import "@assistant-ui/styles/markdown.css";
22
22
// SIMPLIFIED CHILDREN MAP - ONLY ESSENTIAL PROPS
23
23
// ============================================================================
24
24
25
+ function generateUniqueTableName ( ) : string {
26
+ return `chat${ Math . floor ( 1000 + Math . random ( ) * 9000 ) } ` ;
27
+ }
28
+
25
29
const ModelTypeOptions = [
26
30
{ label : "Query" , value : "query" } ,
27
31
{ label : "N8N Workflow" , value : "n8n" } ,
28
32
] as const ;
29
33
30
34
export const chatChildrenMap = {
31
35
// Storage
32
- tableName : withDefault ( StringControl , "default" ) ,
33
-
36
+ // Storage (add the hidden property here)
37
+ _internalDbName : withDefault ( StringControl , "" ) ,
34
38
// Message Handler Configuration
35
39
handlerType : dropdownControl ( ModelTypeOptions , "query" ) ,
36
40
chatQuery : QuerySelectControl , // Only used for "query" type
@@ -41,8 +45,12 @@ export const chatChildrenMap = {
41
45
// UI Configuration
42
46
placeholder : withDefault ( StringControl , "Chat Component" ) ,
43
47
48
+ // Database Information (read-only)
49
+ databaseName : withDefault ( StringControl , "" ) ,
50
+
44
51
// Exposed Variables (not shown in Property View)
45
52
currentMessage : stringExposingStateControl ( "currentMessage" , "" ) ,
53
+ conversationHistory : stringExposingStateControl ( "conversationHistory" , "[]" ) ,
46
54
} ;
47
55
48
56
// ============================================================================
@@ -52,10 +60,27 @@ export const chatChildrenMap = {
52
60
const ChatTmpComp = new UICompBuilder (
53
61
chatChildrenMap ,
54
62
( 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
+ [ ]
59
84
) ;
60
85
61
86
// Create message handler based on type
@@ -96,11 +121,35 @@ const ChatTmpComp = new UICompBuilder(
96
121
dispatch ( changeChildAction ( "currentMessage" , message , false ) ) ;
97
122
} ;
98
123
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
+
99
147
return (
100
148
< ChatCore
101
149
storage = { storage }
102
150
messageHandler = { messageHandler }
103
151
onMessageUpdate = { handleMessageUpdate }
152
+ onConversationUpdate = { handleConversationUpdate }
104
153
/>
105
154
) ;
106
155
}
@@ -114,4 +163,5 @@ const ChatTmpComp = new UICompBuilder(
114
163
115
164
export const ChatComp = withExposingConfigs ( ChatTmpComp , [
116
165
new NameConfig ( "currentMessage" , "Current user message" ) ,
166
+ new NameConfig ( "conversationHistory" , "Full conversation history as JSON array" ) ,
117
167
] ) ;
0 commit comments