RockteMQ-AI commented on code in PR #2111:
URL: 
https://github.com/apache/rocketmq-dashboard/pull/2111#discussion_r3775012020


##########
web/src/pages/ai/index.tsx:
##########
@@ -511,6 +541,7 @@ const AiPage = () => {
 

Review Comment:
   Using `Date.now()` for conversation IDs (`conversation-${Date.now()}`) is 
fine for single-user browser sessions, but if this store is ever shared or 
persisted across tabs, there's a small collision risk. For now this is 
acceptable since it's browser-local state.



##########
web/src/stores/aiChatHistoryStore.ts:
##########
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+import { create } from 'zustand';
+import { createJSONStorage, persist } from 'zustand/middleware';
+
+export interface AiChatMessage {
+  id: string;
+  role: 'user' | 'ai';
+  text?: string;
+  summary?: string;
+  thinking?: string;
+  pending?: boolean;
+}
+
+export type AiChatDataMode = 'mock' | 'real';
+
+export interface AiChatConversation {
+  id: string;
+  messages: AiChatMessage[];
+  updatedAt: number;
+}
+
+export interface AiChatHistory {
+  conversations: AiChatConversation[];
+  activeConversationId: string | null;
+}
+
+interface AiChatHistoryState {
+  histories: Record<AiChatDataMode, AiChatHistory>;
+  startConversation: (mode: AiChatDataMode, conversationId: string) => void;
+  selectConversation: (mode: AiChatDataMode, conversationId: string) => void;
+  setMessages: (
+    mode: AiChatDataMode,
+    conversationId: string,
+    messages: AiChatMessage[] | ((messages: AiChatMessage[]) => 
AiChatMessage[]),
+  ) => void;
+}
+
+const emptyHistory = (): AiChatHistory => ({ conversations: [], 
activeConversationId: null });
+
+const restoreMessages = (messages?: AiChatMessage[]): AiChatMessage[] =>
+  (messages ?? []).map((message) => (message.pending ? { ...message, pending: 
false } : message));
+
+const restoreHistory = (history?: Partial<AiChatHistory> & { messages?: 
AiChatMessage[]; conversationId?: string | null }): AiChatHistory => {
+  if (history?.conversations) {
+    const conversations = history.conversations.map((conversation) => ({
+      ...conversation,
+      messages: restoreMessages(conversation.messages),
+    }));
+    return {
+      conversations,
+      activeConversationId: conversations.some((item) => item.id === 
history.activeConversationId)
+        ? history.activeConversationId ?? null
+        : conversations[0]?.id ?? null,

Review Comment:
   The `restoreHistory` function handles legacy migration from a flat 
`messages`/`conversationId` shape to the new multi-conversation structure. This 
is good forward-thinking design. Consider adding a brief comment noting this is 
for backward compatibility with pre-multi-conversation state, so future 
maintainers don't remove it.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to