Skip to content

Commit 2bb9ae8

Browse files
update chat component to be used in bottom panel
1 parent 003d38e commit 2bb9ae8

File tree

7 files changed

+115
-43
lines changed

7 files changed

+115
-43
lines changed

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,42 @@ import { NameConfig, withExposingConfigs } from "comps/generators/withExposing";
44
import { chatChildrenMap } from "./chatCompTypes";
55
import { ChatView } from "./chatView";
66
import { ChatPropertyView } from "./chatPropertyView";
7+
import { useEffect, useState } from "react";
8+
import { changeChildAction } from "lowcoder-core";
79

810
// Build the component
9-
const ChatTmpComp = new UICompBuilder(
11+
let ChatTmpComp = new UICompBuilder(
1012
chatChildrenMap,
11-
(props) => <ChatView {...props} chatQuery={props.chatQuery.value} />
13+
(props, dispatch) => {
14+
useEffect(() => {
15+
if (Boolean(props.tableName)) return;
16+
17+
// Generate a unique database name for this ChatApp instance
18+
const generateUniqueTableName = () => {
19+
const timestamp = Date.now();
20+
const randomId = Math.random().toString(36).substring(2, 15);
21+
return `TABLE_${timestamp}`;
22+
};
23+
24+
const tableName = generateUniqueTableName();
25+
dispatch(changeChildAction('tableName', tableName, true));
26+
}, [props.tableName]);
27+
28+
if (!props.tableName) {
29+
return null; // Don't render until we have a unique DB name
30+
}
31+
return <ChatView {...props} chatQuery={props.chatQuery.value} />;
32+
}
1233
)
1334
.setPropertyViewFn((children) => <ChatPropertyView children={children} />)
1435
.build();
1536

37+
ChatTmpComp = class extends ChatTmpComp {
38+
override autoHeight(): boolean {
39+
return this.children.autoHeight.getView();
40+
}
41+
};
42+
1643
// Export the component
1744
export const ChatComp = withExposingConfigs(ChatTmpComp, [
1845
new NameConfig("text", "Chat component text"),

client/packages/lowcoder/src/comps/comps/chatComp/chatCompTypes.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { withDefault } from "comps/generators";
44
import { BoolControl } from "comps/controls/boolControl";
55
import { dropdownControl } from "comps/controls/dropdownControl";
66
import QuerySelectControl from "comps/controls/querySelectControl";
7+
import { AutoHeightControl } from "@lowcoder-ee/comps/controls/autoHeightControl";
78

89
// Model type dropdown options
910
const ModelTypeOptions = [
@@ -13,20 +14,26 @@ const ModelTypeOptions = [
1314

1415
export const chatChildrenMap = {
1516
text: withDefault(StringControl, "Chat Component Placeholder"),
16-
chatQuery: QuerySelectControl,
1717
modelType: dropdownControl(ModelTypeOptions, "direct-llm"),
18+
modelHost: withDefault(StringControl, ""),
1819
streaming: BoolControl.DEFAULT_TRUE,
1920
systemPrompt: withDefault(StringControl, "You are a helpful assistant."),
2021
agent: BoolControl,
2122
maxInteractions: withDefault(NumberControl, 10),
23+
chatQuery: QuerySelectControl,
24+
autoHeight: AutoHeightControl,
25+
tableName: withDefault(StringControl, ""),
2226
};
2327

2428
export type ChatCompProps = {
25-
text: string;
26-
chatQuery: string;
27-
modelType: string;
28-
streaming: boolean;
29-
systemPrompt: string;
30-
agent: boolean;
31-
maxInteractions: number;
29+
text?: string;
30+
chatQuery?: string;
31+
modelType?: string;
32+
streaming?: boolean;
33+
systemPrompt?: string;
34+
agent?: boolean;
35+
maxInteractions?: number;
36+
modelHost?: string;
37+
autoHeight?: boolean;
38+
tableName?: string;
3239
};

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

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,34 @@
11
// client/packages/lowcoder/src/comps/comps/chatComp/chatPropertyView.tsx
22
import React from "react";
33
import { Section, sectionNames } from "lowcoder-design";
4+
import { trans } from "i18n";
45

56
export const ChatPropertyView = React.memo((props: any) => {
67
const { children } = props;
78

89
return (
9-
<Section name={sectionNames.basic}>
10-
{children.text.propertyView({ label: "Text" })}
11-
{children.chatQuery.propertyView({ label: "Chat Query" })}
12-
{children.modelType.propertyView({ label: "Model Type" })}
13-
{children.streaming.propertyView({ label: "Enable Streaming" })}
14-
{children.systemPrompt.propertyView({
15-
label: "System Prompt",
16-
placeholder: "Enter system prompt...",
17-
enableSpellCheck: false,
18-
})}
19-
{children.agent.propertyView({ label: "Enable Agent Mode" })}
20-
{children.maxInteractions.propertyView({
21-
label: "Max Interactions",
22-
placeholder: "10",
23-
})}
24-
</Section>
10+
<>
11+
<Section name={sectionNames.basic}>
12+
{children.modelType.propertyView({ label: "Model Type" })}
13+
{children.modelHost.propertyView({ label: "Model Host" })}
14+
{/* {children.text.propertyView({ label: "Text" })}
15+
{children.chatQuery.propertyView({ label: "Chat Query" })} */}
16+
{children.streaming.propertyView({ label: "Enable Streaming" })}
17+
{children.systemPrompt.propertyView({
18+
label: "System Prompt",
19+
placeholder: "Enter system prompt...",
20+
enableSpellCheck: false,
21+
})}
22+
{children.agent.propertyView({ label: "Enable Agent Mode" })}
23+
{children.maxInteractions.propertyView({
24+
label: "Max Interactions",
25+
placeholder: "10",
26+
})}
27+
</Section>
28+
<Section name={sectionNames.layout}>
29+
{children.autoHeight.propertyView({ label: trans("prop.height") })}
30+
</Section>
31+
</>
2532
);
2633
});
2734

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import "@assistant-ui/styles/index.css";
77
import "@assistant-ui/styles/markdown.css";
88

99
export const ChatView = React.memo((props: ChatCompProps) => {
10-
return <ChatApp />;
10+
return <ChatApp {...props} />;
1111
});
1212

1313
ChatView.displayName = 'ChatView';
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { ChatProvider } from "./context/ChatContext";
22
import { ChatMain } from "./ChatMain";
3+
import { ChatCompProps } from "../chatCompTypes";
4+
import { useEffect, useState } from "react";
35

4-
export function ChatApp() {
6+
export function ChatApp(props: ChatCompProps) {
7+
if (!Boolean(props.tableName)) {
8+
return null; // Don't render until we have a unique DB name
9+
}
10+
511
return (
612
<ChatProvider>
7-
<ChatMain />
13+
<ChatMain {...props} />
814
</ChatProvider>
915
);
1016
}

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

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ import {
1616
ArchivedThreadData
1717
} from "./context/ChatContext";
1818
import styled from "styled-components";
19+
import { ChatCompProps } from "../chatCompTypes";
1920

20-
const ChatContainer = styled.div`
21+
const ChatContainer = styled.div<{ $autoHeight?: boolean }>`
2122
display: flex;
22-
height: 500px;
23+
height: ${props => props.$autoHeight ? '500px' : '100%'};
24+
25+
p {
26+
margin: 0;
27+
}
2328
2429
.aui-thread-list-root {
2530
width: 250px;
@@ -45,7 +50,18 @@ const ChatContainer = styled.div`
4550

4651
const generateId = () => Math.random().toString(36).substr(2, 9);
4752

48-
const callYourAPI = async (text: string) => {
53+
const callYourAPI = async (params: {
54+
text: string,
55+
modelHost: string,
56+
modelType: string,
57+
}) => {
58+
const { text, modelHost, modelType } = params;
59+
60+
let url = modelHost;
61+
if (modelType === "direct-llm") {
62+
url = `${modelHost}/api/chat/completions`;
63+
}
64+
4965
// Simulate API delay
5066
await new Promise(resolve => setTimeout(resolve, 1500));
5167

@@ -55,7 +71,7 @@ const callYourAPI = async (text: string) => {
5571
};
5672
};
5773

58-
export function ChatMain() {
74+
export function ChatMain(props: ChatCompProps) {
5975
const { state, actions } = useChatContext();
6076
const [isRunning, setIsRunning] = useState(false);
6177

@@ -92,7 +108,11 @@ export function ChatMain() {
92108

93109
try {
94110
// Call mock API
95-
const response = await callYourAPI(userMessage.text);
111+
const response = await callYourAPI({
112+
text: userMessage.text,
113+
modelHost: props.modelHost!,
114+
modelType: props.modelType!,
115+
});
96116

97117
const assistantMessage: MyMessage = {
98118
id: generateId(),
@@ -146,7 +166,11 @@ export function ChatMain() {
146166

147167
try {
148168
// Generate new response
149-
const response = await callYourAPI(editedMessage.text);
169+
const response = await callYourAPI({
170+
text: editedMessage.text,
171+
modelHost: props.modelHost!,
172+
modelType: props.modelType!,
173+
});
150174

151175
const assistantMessage: MyMessage = {
152176
id: generateId(),
@@ -221,7 +245,7 @@ export function ChatMain() {
221245

222246
return (
223247
<AssistantRuntimeProvider runtime={runtime}>
224-
<ChatContainer>
248+
<ChatContainer $autoHeight={props.autoHeight}>
225249
<ThreadList />
226250
<Thread />
227251
</ChatContainer>

client/packages/lowcoder/src/comps/comps/chatComp/components/assistant-ui/thread-list.tsx

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,30 +7,32 @@ import { PencilIcon, PlusIcon, Trash2Icon } from "lucide-react";
77

88
import { TooltipIconButton } from "./tooltip-icon-button";
99
import { useThreadListItemRuntime } from "@assistant-ui/react";
10-
import { Button } from "antd";
10+
import { Button, Flex } from "antd";
1111

1212
import styled from "styled-components";
1313
import { useChatContext } from "../context/ChatContext";
1414

1515
const StyledPrimaryButton = styled(Button)`
16-
padding: 20px;
17-
margin-bottom: 20px;
16+
// padding: 20px;
17+
// margin-bottom: 20px;
1818
`;
1919

2020

2121
export const ThreadList: FC = () => {
2222
return (
2323
<ThreadListPrimitive.Root className="aui-root aui-thread-list-root">
2424
<ThreadListNew />
25-
<ThreadListItems />
25+
<Flex vertical style={{flex: 1, overflow: 'auto', gap: 4}}>
26+
<ThreadListItems />
27+
</Flex>
2628
</ThreadListPrimitive.Root>
2729
);
2830
};
2931

3032
const ThreadListNew: FC = () => {
3133
return (
3234
<ThreadListPrimitive.New asChild>
33-
<StyledPrimaryButton size="large" type="primary" icon={<PlusIcon />}>
35+
<StyledPrimaryButton size="middle" type="primary" icon={<PlusIcon size={16}/>}>
3436
New Thread
3537
</StyledPrimaryButton>
3638
</ThreadListPrimitive.New>
@@ -54,9 +56,8 @@ const ThreadListItem: FC = () => {
5456
};
5557

5658
const ThreadListItemTitle: FC = () => {
57-
5859
return (
59-
<p className="aui-thread-list-item-title">
60+
<p className="aui-thread-list-item-title" style={{margin: 0}}>
6061
<ThreadListItemPrimitive.Title fallback="New Chat" />
6162
</p>
6263
);

0 commit comments

Comments
 (0)