fix: timeline chat id unique, and message id generator

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei
2025-09-30 17:13:30 +08:00
parent 4cc5a1cf5e
commit 9f2769b34c
8 changed files with 26 additions and 9 deletions
@@ -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<AIChatRootProps> = ({ children, chatId: externalChatId }) => {
@@ -55,9 +57,13 @@ export const AIChatRoot: FC<AIChatRootProps> = ({
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)
@@ -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) => {
@@ -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
>
<div className="mx-auto flex w-full flex-1 flex-col justify-center space-y-8 pb-52">
{showTimelineSummary ? (
<AIChatRoot chatId={todayTimelineSummaryId}>
<AIChatRoot chatId={todayTimelineSummaryId} generateId={() => todayTimelineSummaryId}>
<TimelineSummarySection />
</AIChatRoot>
) : (
@@ -1 +1,5 @@
export const SCROLLED_BEYOND_THRESHOLD = 100
export const AI_CHAT_SPECIAL_ID_PREFIX = {
TIMELINE_SUMMARY: "timeline-summary:",
}
@@ -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
@@ -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<ChatSlice, [], [], ChatSlice> =
(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
@@ -13,9 +13,10 @@ export type AiChatStore = BlockSlice &
}
export const createAIChatStore = (initialState?: Partial<AIChatStoreInitial>) => {
const { blocks, chatId, generateId } = initialState || {}
return createWithEqualityFn<AiChatStore>((...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,
@@ -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 {