import { ChatChunk as ChatChunkType, InputSubmitContent } from '@/types' const chatHistory = { messages: [] as { role: 'bot' | 'user'; content: string }[], } export const updateChatHistory = ( chatChunks: ChatChunkType[], answer?: InputSubmitContent ): string => { const lastChunk = chatChunks[chatChunks.length - 1] // Store bot messages if (lastChunk?.messages?.length) { lastChunk.messages.forEach((msg) => { if ( msg.type === 'text' && msg.content?.type === 'richText' && msg.content.richText ) { let plainText = '' // NEW CONDITION: Check for specific rich text structure with inline-variables const hasInlineVariables = msg.content.richText.some( (block: { children?: { type?: string }[] }) => block.children?.some((child) => child.type === 'inline-variable') ) // NEW CONDITION: Check for variable type blocks const hasVariableBlocks = msg.content.richText.some( (block: { type?: string }) => block.type === 'variable' ) if (hasInlineVariables) { // Process messages with inline-variables plainText = msg.content.richText .map( (block: { children?: { text?: string type?: string children?: { text?: string }[] }[] }) => { return ( block.children ?.map((child) => { // Handle regular text if (child.text) return child.text // Handle inline-variable nodes if (child.type === 'inline-variable') { return ( child.children ?.map((variableChild) => { if ( 'type' in variableChild && variableChild.type === 'p' ) { return 'children' in variableChild && Array.isArray(variableChild.children) ? variableChild.children .map( (grandChild) => grandChild.text || '' ) .join('') : '' } return '' }) .join('') || '' ) } return '' }) .join('') || '' ) } ) .join('\n') } else if (hasVariableBlocks) { // Process variable type blocks plainText = msg.content.richText .map( (block: { type?: string children?: Array<{ type?: string children?: Array<{ text?: string type?: string bold?: boolean children?: Array<{ children: Array<{ text?: string type?: string bold?: boolean children?: Array<{ text?: string }> }> text?: string }> }> }> }) => { if (block.type === 'variable' && block.children) { return block.children .map((child) => { if (child.type === 'p' || child.type === 'h2') { return child.children?.map((c) => c.text).join('') || '' } else if (child.type === 'ol' || child.type === 'ul') { return ( child.children ?.map((li) => li.children ?.map((lic) => lic.children ?.map( (item) => item.text || (item.bold ? `**${item.text}**` : '') ) .join('') ) .join('') ) .join('\n• ') || '' ) } return '' }) .join('\n') } else if (block.type === 'p') { return ( block.children ?.map((c) => ('text' in c && c.text ? c.text : '')) .join('') || '' ) } return '' } ) .join('\n') } else { // Original simple text extraction (fallback) plainText = msg.content.richText .map((block: { children?: { text: string }[] }) => block?.children?.map((c) => c.text).join('') ) .join('\n') } if (plainText.trim()) { chatHistory.messages.push({ role: 'bot', content: plainText }) } } }) } // Store user input (unchanged) if (answer?.type === 'text' && answer.value?.trim()) { chatHistory.messages.push({ role: 'user', content: answer.value }) } return JSON.stringify(chatHistory.messages) }