From 9f2769b34c9eff169111a2d8a4b96b51d73da701 Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 30 Sep 2025 17:13:30 +0800 Subject: [PATCH] fix: timeline chat id unique, and message id generator Signed-off-by: Innei --- .../src/modules/ai-chat/components/layouts/AIChatRoot.tsx | 8 +++++++- .../src/modules/ai-chat/components/layouts/ChatHeader.tsx | 5 +++-- .../modules/ai-chat/components/layouts/WelcomeScreen.tsx | 5 +++-- .../layer/renderer/src/modules/ai-chat/constants/index.ts | 4 ++++ .../renderer/src/modules/ai-chat/store/chat-core/types.ts | 1 - .../src/modules/ai-chat/store/slices/chat.slice.ts | 5 ++++- .../layer/renderer/src/modules/ai-chat/store/store.ts | 5 +++-- .../layer/renderer/src/modules/ai-chat/store/types.ts | 2 ++ 8 files changed, 26 insertions(+), 9 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/AIChatRoot.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/AIChatRoot.tsx index a07ba4a02..20bb449e0 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/AIChatRoot.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/AIChatRoot.tsx @@ -1,4 +1,5 @@ import type { LexicalRichEditorRef } from "@follow/components/ui/lexical-rich-editor/types.js" +import type { IdGenerator } from "ai" import { atom } from "jotai" import type { FC, PropsWithChildren } from "react" import { useEffect, useMemo, useRef } from "react" @@ -20,6 +21,7 @@ import { createAIChatStore } from "../../store/store" interface AIChatRootProps extends PropsWithChildren { wrapFocusable?: boolean chatId?: string + generateId?: IdGenerator } const AIChatRootInner: FC = ({ children, chatId: externalChatId }) => { @@ -55,9 +57,13 @@ export const AIChatRoot: FC = ({ children, wrapFocusable = true, chatId: externalChatId, + generateId, }) => { + const stableGenerateIdFn = useRef(generateId) + stableGenerateIdFn.current = generateId + const useAiContextStore = useMemo( - () => createAIChatStore({ chatId: externalChatId }), + () => createAIChatStore({ chatId: externalChatId, generateId: stableGenerateIdFn.current }), [externalChatId], ) const chatActions = useAiContextStore((state) => state.chatActions) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatHeader.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatHeader.tsx index bc471a556..31b77992f 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatHeader.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatHeader.tsx @@ -29,13 +29,14 @@ const ChatHeaderLayout = ({ const handleNewChatClick = useCallback(() => { const messages = chatActions.getMessages() - if (messages.length === 0 && !currentTitle) { + + if (messages.length === 0) { return } chatActions.newChat() blockActions.clearBlocks({ keepSpecialTypes: true }) - }, [chatActions, currentTitle, blockActions]) + }, [chatActions, blockActions]) const handleTitleSave = useCallback( async (newTitle: string) => { diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx index 2c77f4ad2..df60a28d4 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/WelcomeScreen.tsx @@ -12,6 +12,7 @@ import { ROUTE_ENTRY_PENDING } from "~/constants" import { useRouteParamsSelector } from "~/hooks/biz/useRouteParams" import { AISpline } from "~/modules/ai-chat/components/3d-models/AISpline" +import { AI_CHAT_SPECIAL_ID_PREFIX } from "../../constants" import { useAttachScrollBeyond } from "../../hooks/useAttachScrollBeyond" import { useMainEntryId } from "../../hooks/useMainEntryId" import { AIPersistService } from "../../services" @@ -52,7 +53,7 @@ export const WelcomeScreen = ({ onSend, centerInputOnEmpty }: WelcomeScreenProps const month = now.getMonth() + 1 const year = now.getFullYear() - return `today-timeline-summary-${year}-${month}-${day}` + return `${AI_CHAT_SPECIAL_ID_PREFIX}${year}-${month}-${day}` }, []) const status = useChatStatus() @@ -81,7 +82,7 @@ export const WelcomeScreen = ({ onSend, centerInputOnEmpty }: WelcomeScreenProps >
{showTimelineSummary ? ( - + todayTimelineSummaryId}> ) : ( diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/constants/index.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/constants/index.ts index e3b6e31f7..74be46ffc 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/constants/index.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/constants/index.ts @@ -1 +1,5 @@ export const SCROLLED_BEYOND_THRESHOLD = 100 + +export const AI_CHAT_SPECIAL_ID_PREFIX = { + TIMELINE_SUMMARY: "timeline-summary:", +} diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/store/chat-core/types.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/store/chat-core/types.ts index 15a4101fe..3be582f45 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/store/chat-core/types.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/store/chat-core/types.ts @@ -6,7 +6,6 @@ import type { ZustandChat } from "./chat-instance" // Zustand slice interface export interface ChatSlice { - // Chat state (mirrored from ChatState) chatId: string messages: BizUIMessage[] status: ChatStatus diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/chat.slice.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/chat.slice.ts index 0cc713d85..54d0ca56a 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/chat.slice.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/store/slices/chat.slice.ts @@ -1,3 +1,4 @@ +import type { IdGenerator } from "ai" import type { StateCreator } from "zustand" import { AIPersistService } from "../../services" @@ -9,11 +10,12 @@ import { createChatTransport } from "../transport" export const createChatSlice: (options: { chatId: string + generateId?: IdGenerator }) => StateCreator = (options) => (...params) => { const [set, get] = params - const { chatId } = options + const { chatId, generateId } = options // Create chat instance with Zustand integration const chatInstance = new ZustandChat( @@ -21,6 +23,7 @@ export const createChatSlice: (options: { id: chatId, messages: [], transport: createChatTransport(), + generateId, onFinish: async (options) => { const { message } = options diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/store/store.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/store/store.ts index 3aa48e213..142f9195e 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/store/store.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/store/store.ts @@ -13,9 +13,10 @@ export type AiChatStore = BlockSlice & } export const createAIChatStore = (initialState?: Partial) => { + const { blocks, chatId, generateId } = initialState || {} return createWithEqualityFn((...a) => { - const blockSlice = createBlockSlice(initialState?.blocks)(...a) - const chatSlice = createChatSlice({ chatId: initialState?.chatId || nanoid() })(...a) + const blockSlice = createBlockSlice(blocks)(...a) + const chatSlice = createChatSlice({ chatId: chatId || nanoid(), generateId })(...a) return { ...blockSlice, diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/store/types.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/store/types.ts index 8e31b57ee..3f4768ea1 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/store/types.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/store/types.ts @@ -1,4 +1,5 @@ import type { BizUITools, ToolWithState } from "@folo-services/ai-tools" +import type { IdGenerator } from "ai" export interface FileAttachment { id: string @@ -38,6 +39,7 @@ export type AIChatContextBlockInput = export interface AIChatStoreInitial { blocks: AIChatContextBlock[] chatId?: string + generateId?: IdGenerator } export interface AIChatContextBlocks {