import { BotContext, ChatChunk as ChatChunkType, InputSubmitContent, } from '@/types' import { isMobile } from '@/utils/isMobileSignal' import { ContinueChatResponse, Settings, Theme } from '@indite.io/schemas' import { defaultSettings } from '@indite.io/schemas/features/bot/settings/constants' import { defaultGuestAvatarIsEnabled, defaultHostAvatarIsEnabled, } from '@indite.io/schemas/features/bot/theme/constants' import { createSignal, For, onMount, Show } from 'solid-js' import { HostBubble } from '../bubbles/HostBubble' import { StreamingBubble } from '../bubbles/StreamingBubble' import { InputChatBlock } from '../InputChatBlock' import { AvatarSideContainer } from './AvatarSideContainer' type Props = Pick & { theme: Theme settings: Settings index: number context: BotContext hasError: boolean hideAvatar: boolean streamingMessageId: ChatChunkType['streamingMessageId'] isTransitionDisabled?: boolean onNewBubbleDisplayed: (blockId: string) => Promise onScrollToBottom: (ref?: HTMLDivElement, offset?: number) => void onSubmit: (answer?: InputSubmitContent) => void onSkip: () => void onAllBubblesDisplayed: () => void botContainerWidth?: string } export const ChatChunk = (props: Props) => { console.log('ChatChunk', props); let inputRef: HTMLDivElement | undefined const [displayedMessageIndex, setDisplayedMessageIndex] = createSignal( props.isTransitionDisabled ? props.messages.length : 0 ) const [lastBubble, setLastBubble] = createSignal() onMount(() => { if (props.streamingMessageId) return if (props.messages.length === 0) { props.onAllBubblesDisplayed() } props.onScrollToBottom(inputRef, 50) }) const displayNextMessage = async (bubbleRef?: HTMLDivElement) => { if ( (props.settings.typingEmulation?.delayBetweenBubbles ?? defaultSettings.typingEmulation.delayBetweenBubbles) > 0 && displayedMessageIndex() < props.messages.length - 1 ) { await new Promise((resolve) => setTimeout( resolve, (props.settings.typingEmulation?.delayBetweenBubbles ?? defaultSettings.typingEmulation.delayBetweenBubbles) * 1000 ) ) } const lastBubbleBlockId = props.messages[displayedMessageIndex()].id await props.onNewBubbleDisplayed(lastBubbleBlockId) setDisplayedMessageIndex( displayedMessageIndex() === props.messages.length ? displayedMessageIndex() : displayedMessageIndex() + 1 ) props.onScrollToBottom(bubbleRef) if (displayedMessageIndex() === props.messages.length) { setLastBubble(bubbleRef) props.onAllBubblesDisplayed() } } return (
0}>
0 } >
{(message, idx) => ( )}
{props.input && displayedMessageIndex() === props.messages.length && ( props.onScrollToBottom(lastBubble())} onSubmit={props.onSubmit} onSkip={props.onSkip} /> )} {(streamingMessageId) => (
)}
) }