Skip to content

Commit a349af4

Browse files
iamfaranraheeliftikhar5
authored andcommitted
add unique storage / expose convo history
1 parent cf0b99c commit a349af4

File tree

7 files changed

+336
-277
lines changed

7 files changed

+336
-277
lines changed

client/packages/lowcoder/src/comps/comps/chatComp/chatComp.tsx

Lines changed: 58 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { UICompBuilder } from "comps/generators";
44
import { NameConfig, withExposingConfigs } from "comps/generators/withExposing";
55
import { StringControl } from "comps/controls/codeControl";
6-
import { stringExposingStateControl } from "comps/controls/codeStateControl";
6+
import { arrayObjectExposingStateControl, stringExposingStateControl } from "comps/controls/codeStateControl";
77
import { withDefault } from "comps/generators";
88
import { BoolControl } from "comps/controls/boolControl";
99
import { dropdownControl } from "comps/controls/dropdownControl";
@@ -12,7 +12,7 @@ import { ChatCore } from "./components/ChatCore";
1212
import { ChatPropertyView } from "./chatPropertyView";
1313
import { createChatStorage } from "./utils/storageFactory";
1414
import { QueryHandler, createMessageHandler } from "./handlers/messageHandlers";
15-
import { useMemo } from "react";
15+
import { useMemo, useRef, useEffect } from "react";
1616
import { changeChildAction } from "lowcoder-core";
1717

1818
import "@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+
2529
const ModelTypeOptions = [
2630
{ label: "Query", value: "query" },
2731
{ label: "N8N Workflow", value: "n8n" },
2832
] as const;
2933

3034
export 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 = {
5260
const 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

115164
export const ChatComp = withExposingConfigs(ChatTmpComp, [
116165
new NameConfig("currentMessage", "Current user message"),
166+
new NameConfig("conversationHistory", "Full conversation history as JSON array"),
117167
]);

client/packages/lowcoder/src/comps/comps/chatComp/chatPropertyView.tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ export const ChatPropertyView = React.memo((props: any) => {
1919
placeholder: "Enter placeholder text..."
2020
})}
2121

22-
{children.tableName.propertyView({
23-
label: "Storage Table",
24-
placeholder: "default",
25-
tooltip: "Storage identifier - use same value to share conversations between components"
22+
{children.databaseName.propertyView({
23+
label: "Database Name",
24+
placeholder: "Database will be auto-generated...",
25+
tooltip: "Read-only: Auto-generated database name for data persistence. You can reference this in queries if needed.",
26+
disabled: true
2627
})}
28+
2729
</Section>
2830

2931
{/* Message Handler Configuration */}

client/packages/lowcoder/src/comps/comps/chatComp/components/ChatCore.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@ import { ChatCoreProps } from "../types/chatTypes";
99
// CHAT CORE - THE SHARED FOUNDATION
1010
// ============================================================================
1111

12-
export function ChatCore({ storage, messageHandler, onMessageUpdate }: ChatCoreProps) {
12+
export function ChatCore({ storage, messageHandler, onMessageUpdate, onConversationUpdate }: ChatCoreProps) {
1313
return (
1414
<ChatProvider storage={storage}>
1515
<ChatCoreMain
1616
messageHandler={messageHandler}
1717
onMessageUpdate={onMessageUpdate}
18+
onConversationUpdate={onConversationUpdate}
1819
/>
1920
</ChatProvider>
2021
);

0 commit comments

Comments
 (0)