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 <tukon479@gmail.com>
This commit is contained in:
@@ -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
|
||||
|
||||
|
||||
@@ -227,6 +227,7 @@ const ChatInterfaceContent = ({ centerInputOnEmpty }: ChatInterfaceProps) => {
|
||||
},
|
||||
})
|
||||
} else {
|
||||
staticEditor.setEditorState(message)
|
||||
parts.push({
|
||||
type: "data-rich-text",
|
||||
data: {
|
||||
|
||||
+2
-5
@@ -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<UserChatMessageProps> = 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,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -63,6 +63,6 @@ export type BizUIMessage = UIMessage<BizUIMetadata, BizUIDataTypes, BizUITools>
|
||||
createdAt: Date
|
||||
}
|
||||
|
||||
export type BizUIMessagePart = UIMessagePart<BizUIMetadata, BizUITools>
|
||||
export type BizUIMessagePart = UIMessagePart<BizUIDataTypes, BizUITools>
|
||||
|
||||
export type SendingUIMessage = Omit<BizUIMessage, "createdAt">
|
||||
|
||||
@@ -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[])
|
||||
|
||||
Reference in New Issue
Block a user