From 8a68bde8544799290de433f89d0eff9d507aa24b Mon Sep 17 00:00:00 2001 From: Innei Date: Tue, 30 Sep 2025 23:41:51 +0800 Subject: [PATCH] fix(ai): rich editor state transformer - Updated ChatInterface to set editor state directly for improved message handling. - Adjusted UserChatMessage to stringify new state for rich text parts. - Included createdAt in message loading for better message tracking. - Refined message conversion in AIPersistServiceStatic to ensure proper type handling. - Modified types to align with updated message structure. Signed-off-by: Innei --- .../layer/renderer/src/modules/ai-chat-session/service.ts | 4 ++-- .../modules/ai-chat/components/layouts/ChatInterface.tsx | 1 + .../modules/ai-chat/components/message/UserChatMessage.tsx | 7 ++----- .../renderer/src/modules/ai-chat/hooks/useLoadMessages.ts | 1 + .../layer/renderer/src/modules/ai-chat/services/index.ts | 7 ++----- .../layer/renderer/src/modules/ai-chat/store/types.ts | 2 +- .../layer/renderer/src/modules/ai-chat/utils/export.ts | 5 ++--- 7 files changed, 11 insertions(+), 16 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat-session/service.ts b/apps/desktop/layer/renderer/src/modules/ai-chat-session/service.ts index 3524c8240..b669d14e5 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat-session/service.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat-session/service.ts @@ -3,7 +3,7 @@ import type { AIChatMessage, AIChatSession } from "@follow-app/client-sdk" import { followApi } from "../../lib/api-client" import { queryClient } from "../../lib/query-client" import { AIPersistService } from "../ai-chat/services" -import type { BizUIMessage, BizUIMetadata } from "../ai-chat/store/types" +import type { BizUIMessage, BizUIMetadata, SendingUIMessage } from "../ai-chat/store/types" import { aiChatSessionKeys } from "./query" // Hard cap on pagination to prevent excessive API calls while keeping initial sync fast. @@ -77,7 +77,7 @@ class AIChatSessionServiceStatic { /** * Normalize a remote message object into BizUIMessage shape expected by the local chat system. */ - private normalizeRemoteMessage = (msg: AIChatMessage): BizUIMessage => { + private normalizeRemoteMessage = (msg: AIChatMessage): SendingUIMessage => { const metadata = msg.metadata && typeof msg.metadata === "object" ? (msg.metadata as BizUIMetadata) : undefined diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx index 10bc17588..99aaa37fa 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/ChatInterface.tsx @@ -227,6 +227,7 @@ const ChatInterfaceContent = ({ centerInputOnEmpty }: ChatInterfaceProps) => { }, }) } else { + staticEditor.setEditorState(message) parts.push({ type: "data-rich-text", data: { diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/UserChatMessage.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/UserChatMessage.tsx index 2f65e0468..c8d6488bc 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/UserChatMessage.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/message/UserChatMessage.tsx @@ -8,7 +8,6 @@ import { useEditingMessageId, useSetEditingMessageId } from "~/modules/ai-chat/a import { useChatActions, useChatStatus } from "~/modules/ai-chat/store/hooks" import type { AIChatContextBlock, BizUIMessage } from "~/modules/ai-chat/store/types" -import type { RichTextPart } from "../../types/ChatSession" import { convertLexicalToMarkdown } from "../../utils/lexical-markdown" import { AIDataBlockPart } from "./AIDataBlockPart" import { AIMessageIdContext } from "./AIMessageIdContext" @@ -68,12 +67,10 @@ export const UserChatMessage: React.FC = React.memo(({ mes const nextMessage = messages[messageIndex]! chatActions.setMessages(messagesToKeep) - const richTextPart = nextMessage.parts.find( - (part) => part.type === "data-rich-text", - ) as RichTextPart + const richTextPart = nextMessage.parts.find((part) => part.type === "data-rich-text") if (richTextPart) { richTextPart.data = { - state: newState, + state: JSON.stringify(newState), text: messageContent, } } diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useLoadMessages.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useLoadMessages.ts index 6f55885f9..bcbcef8e6 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useLoadMessages.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useLoadMessages.ts @@ -28,6 +28,7 @@ export const useLoadMessages = ( parts: message.messageParts as any[], role: message.role, metadata: message.metadata as BizUIMetadata, + createdAt: message.createdAt, })) chatActions.setMessages(messagesToSet) onLoadEventCallback(messagesToSet) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts index 920d1ac5e..3b6ecf90d 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/services/index.ts @@ -39,18 +39,15 @@ class AIPersistServiceStatic { * Convert enhanced database message to BizUIMessage format for compatibility */ private convertToUIMessage(dbMessage: AiChatMessagesModel): BizUIMessage { - // Reconstruct UIMessage from database fields const uiMessage: BizUIMessage = { id: dbMessage.id, role: dbMessage.role, createdAt: dbMessage.createdAt, - parts: [], // AI SDK v5 uses parts array + parts: [], } - // Add parts based on content format and data if (dbMessage.messageParts && dbMessage.messageParts.length > 0) { - // For assistant messages with complex parts (tools, reasoning, etc) - uiMessage.parts = dbMessage.messageParts as BizUIMessagePart[] + uiMessage.parts = dbMessage.messageParts as any[] as BizUIMessagePart[] } return uiMessage 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 b7ac27879..4a6f1cc77 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 @@ -63,6 +63,6 @@ export type BizUIMessage = UIMessage createdAt: Date } -export type BizUIMessagePart = UIMessagePart +export type BizUIMessagePart = UIMessagePart export type SendingUIMessage = Omit diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/utils/export.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/utils/export.ts index b3925e59b..06585cd75 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/utils/export.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/utils/export.ts @@ -1,5 +1,4 @@ import type { BizUIMessage } from "../store/types" -import type { RichTextPart } from "../types/ChatSession" export const exportChatToMarkdown = (messages: BizUIMessage[], title?: string) => { const date = new Date().toLocaleString() @@ -22,7 +21,7 @@ export const exportChatToMarkdown = (messages: BizUIMessage[], title?: string) = if (part.type === "text") { acc.push(part.text) } else if (part.type === "data-rich-text") { - acc.push((part as RichTextPart).data.text) + acc.push(part.data.text) } return acc }, [] as string[]) @@ -40,7 +39,7 @@ export const exportChatToMarkdown = (messages: BizUIMessage[], title?: string) = if (part.type === "text") { acc.push(part.text) } else if (part.type === "data-rich-text") { - acc.push((part as RichTextPart).data.text) + acc.push(part.data.text) } return acc }, [] as string[])