Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit4585d15

Browse files
iamfaranraheeliftikhar5
authored andcommitted
[feat] replace mock data with query
1 parent6d5fbfd commit4585d15

File tree

6 files changed

+125
-125
lines changed

6 files changed

+125
-125
lines changed

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

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,39 +8,22 @@ import { useEffect, useState } from "react";
88
import{changeChildAction}from"lowcoder-core";
99

1010
// Build the component
11-
letChatTmpComp=newUICompBuilder(
12-
chatChildrenMap,
13-
(props,dispatch)=>{
14-
useEffect(()=>{
15-
if(Boolean(props.tableName))return;
16-
17-
// Generate a unique database name for this ChatApp instance
18-
constgenerateUniqueTableName=()=>{
19-
consttimestamp=Date.now();
20-
constrandomId=Math.random().toString(36).substring(2,15);
21-
return`TABLE_${timestamp}`;
22-
};
23-
24-
consttableName=generateUniqueTableName();
25-
dispatch(changeChildAction('tableName',tableName,true));
26-
},[props.tableName]);
27-
28-
if(!props.tableName){
29-
returnnull;// Don't render until we have a unique DB name
30-
}
31-
return<ChatView{...props}chatQuery={props.chatQuery.value}/>;
32-
}
11+
constChatTmpComp=newUICompBuilder(
12+
chatChildrenMap,
13+
(props,dispatch)=>(
14+
<ChatView
15+
{...props}
16+
chatQuery={props.chatQuery.value}
17+
currentMessage={props.currentMessage.value}
18+
dispatch={dispatch}
19+
/>
20+
)
3321
)
3422
.setPropertyViewFn((children)=><ChatPropertyViewchildren={children}/>)
3523
.build();
3624

37-
ChatTmpComp=classextendsChatTmpComp{
38-
overrideautoHeight():boolean{
39-
returnthis.children.autoHeight.getView();
40-
}
41-
};
42-
43-
// Export the component
25+
// Export the component with exposed variables
4426
exportconstChatComp=withExposingConfigs(ChatTmpComp,[
4527
newNameConfig("text","Chat component text"),
28+
newNameConfig("currentMessage","Current user message"),
4629
]);
Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// client/packages/lowcoder/src/comps/comps/chatComp/chatCompTypes.ts
22
import{StringControl,NumberControl}from"comps/controls/codeControl";
3+
import{stringExposingStateControl}from"comps/controls/codeStateControl";
34
import{withDefault}from"comps/generators";
45
import{BoolControl}from"comps/controls/boolControl";
56
import{dropdownControl}from"comps/controls/dropdownControl";
@@ -14,26 +15,25 @@ const ModelTypeOptions = [
1415

1516
exportconstchatChildrenMap={
1617
text:withDefault(StringControl,"Chat Component Placeholder"),
18+
chatQuery:QuerySelectControl,
19+
currentMessage:stringExposingStateControl("currentMessage",""),
1720
modelType:dropdownControl(ModelTypeOptions,"direct-llm"),
1821
modelHost:withDefault(StringControl,""),
1922
streaming:BoolControl.DEFAULT_TRUE,
2023
systemPrompt:withDefault(StringControl,"You are a helpful assistant."),
2124
agent:BoolControl,
2225
maxInteractions:withDefault(NumberControl,10),
23-
chatQuery:QuerySelectControl,
2426
autoHeight:AutoHeightControl,
2527
tableName:withDefault(StringControl,""),
2628
};
2729

2830
exporttypeChatCompProps={
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;
31+
text:string;
32+
chatQuery:string;
33+
currentMessage:string;
34+
modelType:string;
35+
streaming:boolean;
36+
systemPrompt:string;
37+
agent:boolean;
38+
maxInteractions:number;
3939
};

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

Lines changed: 21 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,27 @@ export const ChatPropertyView = React.memo((props: any) => {
77
const{ children}=props;
88

99
return(
10-
<>
11-
<Sectionname={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-
<Sectionname={sectionNames.layout}>
29-
{children.autoHeight.propertyView({label:trans("prop.height")})}
30-
</Section>
31-
</>
10+
<Sectionname={sectionNames.basic}>
11+
{children.text.propertyView({label:"Text"})}
12+
{children.chatQuery.propertyView({label:"Chat Query"})}
13+
{children.currentMessage.propertyView({
14+
label:"Current Message (Dynamic)",
15+
placeholder:"Shows the current user message",
16+
disabled:true
17+
})}
18+
{children.modelType.propertyView({label:"Model Type"})}
19+
{children.streaming.propertyView({label:"Enable Streaming"})}
20+
{children.systemPrompt.propertyView({
21+
label:"System Prompt",
22+
placeholder:"Enter system prompt...",
23+
enableSpellCheck:false,
24+
})}
25+
{children.agent.propertyView({label:"Enable Agent Mode"})}
26+
{children.maxInteractions.propertyView({
27+
label:"Max Interactions",
28+
placeholder:"10",
29+
})}
30+
</Section>
3231
);
3332
});
3433

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
// client/packages/lowcoder/src/comps/comps/chatComp/chatView.tsx
22
importReactfrom"react";
33
import{ChatCompProps}from"./chatCompTypes";
4+
import{CompAction}from"lowcoder-core";
45
import{ChatApp}from"./components/ChatApp";
56

67
import"@assistant-ui/styles/index.css";
78
import"@assistant-ui/styles/markdown.css";
89

9-
exportconstChatView=React.memo((props:ChatCompProps)=>{
10-
return<ChatApp{...props}/>;
10+
// Extend the props we receive so we can forward the redux dispatch
11+
interfaceChatViewPropsextendsChatCompProps{
12+
dispatch?:(action:CompAction<any>)=>void;
13+
}
14+
15+
exportconstChatView=React.memo((props:ChatViewProps)=>{
16+
const{ chatQuery, currentMessage, dispatch}=props;
17+
return<ChatAppchatQuery={chatQuery}currentMessage={currentMessage}dispatch={dispatch}/>;
1118
});
1219

1320
ChatView.displayName='ChatView';
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
import{ChatProvider}from"./context/ChatContext";
22
import{ChatMain}from"./ChatMain";
3-
import{ChatCompProps}from"../chatCompTypes";
4-
import{useEffect,useState}from"react";
3+
import{CompAction}from"lowcoder-core";
54

6-
exportfunctionChatApp(props:ChatCompProps){
7-
if(!Boolean(props.tableName)){
8-
returnnull;// Don't render until we have a unique DB name
9-
}
10-
5+
interfaceChatAppProps{
6+
chatQuery:string;
7+
currentMessage:string;
8+
dispatch?:(action:CompAction<any>)=>void;
9+
}
10+
11+
exportfunctionChatApp({ chatQuery, currentMessage, dispatch}:ChatAppProps){
1112
return(
1213
<ChatProvider>
13-
<ChatMain{...props}/>
14+
<ChatMainchatQuery={chatQuery}currentMessage={currentMessage}dispatch={dispatch}/>
1415
</ChatProvider>
1516
);
1617
}

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

Lines changed: 63 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@ import {
1616
ArchivedThreadData
1717
}from"./context/ChatContext";
1818
importstyledfrom"styled-components";
19-
import{ChatCompProps}from"../chatCompTypes";
20-
import{message}from"antd";
21-
import{EditorContext}from"@lowcoder-ee/comps/editorState";
22-
import{addComponentAction,nestComponentAction}from"../../preLoadComp/actions/componentManagement";
23-
import{configureComponentAction}from"../../preLoadComp/actions/componentConfiguration";
19+
import{routeByNameAction,executeQueryAction,CompAction,changeChildAction}from"lowcoder-core";
20+
import{getPromiseAfterDispatch}from"util/promiseUtils";
2421

2522
constChatContainer=styled.div<{$autoHeight?:boolean}>`
2623
display: flex;
@@ -54,38 +51,57 @@ const ChatContainer = styled.div<{ $autoHeight?: boolean }>`
5451

5552
constgenerateId=()=>Math.random().toString(36).substr(2,9);
5653

57-
constcallYourAPI=async(params:{
58-
text:string,
59-
modelHost:string,
60-
modelType:string,
61-
sessionId:string,
62-
})=>{
63-
const{ text, modelHost, modelType, sessionId}=params;
64-
65-
leturl=modelHost;
66-
if(modelType==="direct-llm"){
67-
url=`${modelHost}/api/chat/completions`;
54+
// Helper to call the Lowcoder query system
55+
constcallQuery=async(
56+
queryName:string,
57+
prompt:string,
58+
dispatch?:(action:CompAction<any>)=>void
59+
)=>{
60+
// If no query selected or dispatch unavailable, fallback with mock response
61+
if(!queryName||!dispatch){
62+
awaitnewPromise((res)=>setTimeout(res,500));
63+
return{content:"(mock) You typed: "+prompt};
6864
}
6965

70-
constresponse=awaitfetch(`${url}`,{
71-
method:"POST",
72-
body:JSON.stringify({
73-
text,
74-
sessionId,
75-
}),
76-
});
66+
try{
67+
constresult:any=awaitgetPromiseAfterDispatch(
68+
dispatch,
69+
routeByNameAction(
70+
queryName,
71+
executeQueryAction({
72+
// Send the user prompt as variable named 'prompt' by default
73+
args:{prompt:{value:prompt}},
74+
})
75+
)
76+
);
77+
78+
// Extract reply text from the query result
79+
letreply:string;
80+
if(typeofresult==="string"){
81+
reply=result;
82+
}elseif(result&&typeofresult==="object"){
83+
reply=
84+
(resultasany).response??
85+
(resultasany).message??
86+
(resultasany).content??
87+
JSON.stringify(result);
88+
}else{
89+
reply=String(result);
90+
}
7791

78-
returnresponse.json();
79-
// Simulate API delay
80-
// await new Promise(resolve => setTimeout(resolve, 1500));
81-
82-
// Simple responses
83-
// return {
84-
// content: "This is a mock response from your backend. You typed: " + text
85-
// };
92+
return{content:reply};
93+
}catch(e:any){
94+
thrownewError(e?.message||"Query execution failed");
95+
}
8696
};
8797

88-
exportfunctionChatMain(props:ChatCompProps){
98+
interfaceChatMainProps{
99+
chatQuery:string;
100+
currentMessage:string;
101+
dispatch?:(action:CompAction<any>)=>void;
102+
}
103+
104+
exportfunctionChatMain({ chatQuery, currentMessage, dispatch}:ChatMainProps){
89105
const{ state, actions}=useChatContext();
90106
const[isRunning,setIsRunning]=useState(false);
91107
consteditorState=useContext(EditorContext);
@@ -178,21 +194,19 @@ export function ChatMain(props: ChatCompProps) {
178194
timestamp:Date.now(),
179195
};
180196

197+
// Update currentMessage state to expose to queries
198+
if(dispatch){
199+
dispatch(changeChildAction("currentMessage",userMessage.text,false));
200+
}
201+
181202
// Update current thread with new user message
182203
awaitactions.addMessage(state.currentThreadId,userMessage);
183204
setIsRunning(true);
184205

185206
try{
186-
// Call mock API
187-
constresponse=awaitcallYourAPI({
188-
text:userMessage.text,
189-
modelHost:props.modelHost!,
190-
modelType:props.modelType!,
191-
sessionId:state.currentThreadId,
192-
});
193-
const{explanation:reply,actions:editorActions}=JSON.parse(response?.output);
194-
performAction(editorActions);
195-
207+
// Call selected query / fallback to mock
208+
constresponse=awaitcallQuery(chatQuery,userMessage.text,dispatch);
209+
196210
constassistantMessage:MyMessage={
197211
id:generateId(),
198212
role:"assistant",
@@ -239,22 +253,18 @@ export function ChatMain(props: ChatCompProps) {
239253
};
240254
newMessages.push(editedMessage);
241255

256+
// Update currentMessage state to expose to queries
257+
if(dispatch){
258+
dispatch(changeChildAction("currentMessage",editedMessage.text,false));
259+
}
260+
242261
// Update messages using the new context action
243262
awaitactions.updateMessages(state.currentThreadId,newMessages);
244263
setIsRunning(true);
245264

246265
try{
247-
// Generate new response
248-
constresponse=awaitcallYourAPI({
249-
text:editedMessage.text,
250-
modelHost:props.modelHost!,
251-
modelType:props.modelType!,
252-
sessionId:state.currentThreadId,
253-
});
254-
255-
const{explanation:reply,actions:editorActions}=JSON.parse(response?.output);
256-
performAction(editorActions);
257-
266+
constresponse=awaitcallQuery(chatQuery,editedMessage.text,dispatch);
267+
258268
constassistantMessage:MyMessage={
259269
id:generateId(),
260270
role:"assistant",

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp