Skip to content

Commit c2daad6

Browse files
committed
add shared state and fix date format
1 parent ab37868 commit c2daad6

8 files changed

Lines changed: 890 additions & 120 deletions

File tree

client/packages/lowcoder/src/comps/comps/chatBoxComponentv2/READMEv2.md

Lines changed: 622 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// UPDATED MessageSent Query Code
2+
// Replaces broadcastNewMessage with setRoomData
3+
4+
const currentRoomId = chatControllerSignal1.currentRoomId;
5+
const rooms = chatControllerSignal1.sharedState?.rooms || [];
6+
const currentRoom = rooms.find(r => r.id === currentRoomId);
7+
8+
console.log("CURRENT ROOM", currentRoom);
9+
10+
saveMessage.run()
11+
.then(() => {
12+
// Check if current room is an LLM room
13+
if (currentRoom && currentRoom.type === 'llm') {
14+
console.log("STARTING AI THINKING...");
15+
// Broadcast to all users: AI is thinking
16+
chatControllerSignal1.setAiThinking(currentRoomId, true);
17+
return getAIResponse.run();
18+
}
19+
})
20+
.then(() => {
21+
// AI finished - stop thinking animation
22+
if (currentRoom && currentRoom.type === 'llm') {
23+
console.log("AI THINKING STOPPED");
24+
chatControllerSignal1.setAiThinking(currentRoomId, false);
25+
}
26+
27+
// NEW: Signal other users that a message was saved
28+
// This triggers their "Room Data Changed" event which reloads messages
29+
chatControllerSignal1.setRoomData(currentRoomId, "lastMessage", {
30+
ts: Date.now(),
31+
authorId: chatControllerSignal1.userId
32+
});
33+
34+
// Reload your own messages
35+
return loadMessages.run();
36+
})
37+
.catch(err => {
38+
// Stop thinking on error so it doesn't get stuck
39+
if (currentRoom && currentRoom.type === 'llm') {
40+
chatControllerSignal1.setAiThinking(currentRoomId, false);
41+
}
42+
console.error("Error:", err);
43+
});

client/packages/lowcoder/src/comps/comps/chatBoxComponentv2/components/MessageList.tsx

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import ReactMarkdown from "react-markdown";
33
import remarkGfm from "remark-gfm";
44
import { Tooltip } from "antd";
55
import { CopyOutlined, CheckOutlined, RobotOutlined } from "@ant-design/icons";
6+
import dayjs from "dayjs";
7+
import { parseMessageTimestamp, formatChatTime } from "util/dateTimeUtils";
68
import { LLM_BOT_AUTHOR_ID } from "../store";
79
import {
810
MessagesArea,
@@ -21,10 +23,17 @@ import {
2123
LlmLoadingBubble,
2224
} from "../styles";
2325

26+
function readField(msg: any, ...keys: string[]): string {
27+
for (const k of keys) {
28+
if (msg[k] != null && msg[k] !== "") return String(msg[k]);
29+
}
30+
return "";
31+
}
32+
2433
// ── AI message bubble with copy button ───────────────────────────────────────
2534

2635
const AiMessageBubble = React.memo(
27-
({ text, authorName, timestamp }: { text: string; authorName: string; timestamp: number }) => {
36+
({ text, authorName, ts }: { text: string; authorName: string; ts: dayjs.Dayjs | null }) => {
2837
const [copied, setCopied] = useState(false);
2938

3039
const handleCopy = useCallback(() => {
@@ -58,12 +67,9 @@ const AiMessageBubble = React.memo(
5867
</AiCopyButton>
5968
</Tooltip>
6069
</div>
61-
{timestamp > 0 && (
70+
{ts && (
6271
<BubbleTime $own={false}>
63-
{new Date(timestamp).toLocaleTimeString([], {
64-
hour: "2-digit",
65-
minute: "2-digit",
66-
})}
72+
{formatChatTime(ts)}
6773
</BubbleTime>
6874
)}
6975
</AiBubbleWrapper>
@@ -73,23 +79,6 @@ const AiMessageBubble = React.memo(
7379

7480
AiMessageBubble.displayName = "AiMessageBubble";
7581

76-
// ── Helpers to read message fields flexibly ───────────────────────────────────
77-
78-
function readField(msg: any, ...keys: string[]): string {
79-
for (const k of keys) {
80-
if (msg[k] != null && msg[k] !== "") return String(msg[k]);
81-
}
82-
return "";
83-
}
84-
85-
function readTimestamp(msg: any): number {
86-
const raw =
87-
msg.timestamp ?? msg.createdAt ?? msg.created_at ?? msg.time ?? 0;
88-
if (typeof raw === "number") return raw;
89-
const parsed = Date.parse(raw);
90-
return Number.isNaN(parsed) ? 0 : parsed;
91-
}
92-
9382
// ── Main component ───────────────────────────────────────────────────────────
9483

9584
export interface MessageListProps {
@@ -139,7 +128,7 @@ export const MessageList = React.memo((props: MessageListProps) => {
139128
"author_name",
140129
"senderName",
141130
) || authorId;
142-
const timestamp = readTimestamp(msg);
131+
const ts = parseMessageTimestamp(msg);
143132
const authorType = msg.authorType || msg.role || "";
144133

145134
const isAssistant =
@@ -153,7 +142,7 @@ export const MessageList = React.memo((props: MessageListProps) => {
153142
key={id}
154143
text={text}
155144
authorName={authorName}
156-
timestamp={timestamp}
145+
ts={ts}
157146
/>
158147
);
159148
}
@@ -162,12 +151,9 @@ export const MessageList = React.memo((props: MessageListProps) => {
162151
<MessageWrapper key={id} $own={isOwn}>
163152
<BubbleMeta $own={isOwn}>{authorName}</BubbleMeta>
164153
<Bubble $own={isOwn}>{text}</Bubble>
165-
{timestamp > 0 && (
154+
{ts && (
166155
<BubbleTime $own={isOwn}>
167-
{new Date(timestamp).toLocaleTimeString([], {
168-
hour: "2-digit",
169-
minute: "2-digit",
170-
})}
156+
{formatChatTime(ts)}
171157
</BubbleTime>
172158
)}
173159
</MessageWrapper>

client/packages/lowcoder/src/comps/comps/chatBoxComponentv2/store/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export type {
55
PendingRoomInvite,
66
TypingUser,
77
OnlineUser,
8-
MessageBroadcast,
98
} from "./types";
109

1110
export { uid, LLM_BOT_AUTHOR_ID } from "./types";

client/packages/lowcoder/src/comps/comps/chatBoxComponentv2/store/pluvClient.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,9 @@ const client = createClient({
3636
return `${PLUV_AUTH_URL}?${params}`;
3737
},
3838
initialStorage: yjs.doc((t: any) => ({
39-
messageActivity: t.map("messageActivity", []),
4039
aiActivity: t.map("aiActivity", []),
40+
sharedState: t.map("sharedState", []),
41+
roomData: t.map("roomData", []),
4142
})),
4243
presence: z.object({
4344
userId: z.string(),

client/packages/lowcoder/src/comps/comps/chatBoxComponentv2/store/types.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,6 @@ export interface OnlineUser {
4141
currentRoomId: string | null;
4242
}
4343

44-
export interface MessageBroadcast {
45-
roomId: string;
46-
messageId: string;
47-
authorId: string;
48-
authorName: string;
49-
timestamp: number;
50-
counter: number;
51-
}
52-
5344
export interface AiThinkingState {
5445
roomId: string;
5546
isThinking: boolean;

0 commit comments

Comments
 (0)