diff --git a/ee/tabby-ui/app/playground/components/chats.tsx b/ee/tabby-ui/app/playground/components/chats.tsx index 747bc244d258..3dc5d16eab0a 100644 --- a/ee/tabby-ui/app/playground/components/chats.tsx +++ b/ee/tabby-ui/app/playground/components/chats.tsx @@ -3,32 +3,74 @@ import React from 'react' import type { Message } from 'ai' +import useRouterStuff from '@/lib/hooks/use-router-stuff' import { useStore } from '@/lib/hooks/use-store' +import { addChat } from '@/lib/stores/chat-actions' import { useChatStore } from '@/lib/stores/chat-store' import { getChatById } from '@/lib/stores/utils' -import { Chat } from '@/components/chat' +import { truncateText } from '@/lib/utils' +import { Chat, ChatRef } from '@/components/chat' +import LoadingWrapper from '@/components/loading-wrapper' +import { ListSkeleton } from '@/components/skeleton' import { ChatSessions } from './chat-sessions' const emptyMessages: Message[] = [] export default function Chats() { + const { searchParams, updateSearchParams } = useRouterStuff() + const initialMessage = searchParams.get('initialMessage')?.toString() + const shouldConsumeInitialMessage = React.useRef(!!initialMessage) + const chatRef = React.useRef(null) + const _hasHydrated = useStore(useChatStore, state => state._hasHydrated) const chats = useStore(useChatStore, state => state.chats) const activeChatId = useStore(useChatStore, state => state.activeChatId) + const chat = getChatById(chats, activeChatId) + + React.useEffect(() => { + if (!shouldConsumeInitialMessage.current) return + if (!chatRef.current?.append) return + + if (activeChatId && initialMessage) { + // request initialMessage + chatRef.current + .append({ + role: 'user', + content: initialMessage + }) + .then(() => { + // Remove the initialMessage params after the request is completed. + updateSearchParams({ + del: 'initialMessage' + }) + }) + // store as a new chat + addChat(activeChatId, truncateText(initialMessage)) - const chatId = activeChatId - const chat = getChatById(chats, chatId) + shouldConsumeInitialMessage.current = false + } + }, [chatRef.current?.append]) return (
- + + +
+ } + > + + ) } diff --git a/ee/tabby-ui/components/chat.tsx b/ee/tabby-ui/components/chat.tsx index 6419cfb771ef..dbfd2ee1a785 100644 --- a/ee/tabby-ui/components/chat.tsx +++ b/ee/tabby-ui/components/chat.tsx @@ -2,7 +2,7 @@ import React from 'react' import { useChat } from 'ai/react' -import type { Message } from 'ai/react' +import type { Message, UseChatHelpers } from 'ai/react' import { find, findIndex } from 'lodash-es' import { toast } from 'sonner' @@ -16,27 +16,22 @@ import { ChatList } from '@/components/chat-list' import { ChatPanel } from '@/components/chat-panel' import { ChatScrollAnchor } from '@/components/chat-scroll-anchor' import { EmptyScreen } from '@/components/empty-screen' -import { ListSkeleton } from '@/components/skeleton' export interface ChatProps extends React.ComponentProps<'div'> { initialMessages?: Message[] id?: string - loading?: boolean } -export function Chat({ id, initialMessages, loading, className }: ChatProps) { +export interface ChatRef extends UseChatHelpers {} + +function ChatRenderer( + { id, initialMessages, className }: ChatProps, + ref: React.ForwardedRef +) { usePatchFetch() const chats = useStore(useChatStore, state => state.chats) - const { - messages, - append, - reload, - stop, - isLoading, - input, - setInput, - setMessages - } = useChat({ + + const useChatHelpers = useChat({ initialMessages, id, body: { @@ -49,6 +44,17 @@ export function Chat({ id, initialMessages, loading, className }: ChatProps) { } }) + const { + messages, + append, + reload, + stop, + isLoading, + input, + setInput, + setMessages + } = useChatHelpers + const [selectedMessageId, setSelectedMessageId] = React.useState() const onRegenerateResponse = (messageId: string) => { @@ -122,20 +128,19 @@ export function Chat({ id, initialMessages, loading, className }: ChatProps) { return () => stop() }, []) + React.useImperativeHandle( + ref, + () => { + return useChatHelpers + }, + [useChatHelpers] + ) + return (
- {loading ? ( -
-
- -
-
- -
-
- ) : messages.length ? ( + {messages.length ? ( <> ) } + +export const Chat = React.forwardRef(ChatRenderer)