How am I supposed to manage these ServerChats from the UI (so the user can add/remove/sync different instances in parallel), given the fact that AI context should stay on the server since it acts as a portal between ? Code:
export default async function Playground({ params }: PlaygroundProps) {
const session = (await auth()) as Session
if (!session?.user) redirect(`/login?next=/chat`)
const userId = session.user.id as string
const ChatServer = async ({ id }: { id: string }) => {
const chat = await getChat(id, userId)
if (!chat) redirect('/')
return (
<AI
key={chat.id}
initialAIState={{
id: chat.id,
model: chat.model,
messages: chat.messages
}}
>
<div
id={`chat-${chat.id}`}
className="flex-shrink-0 md:flex-shrink min-h-[250px] border border-border rounded-md"
>
<Chat id={chat.id} session={session} />
</div>
</AI>
)
}
return (
<ChatClient>
<ChatServer id="cuT9MKk" />
</ChatClient>
)
}
"use client"
export default function ChatClient({ children }: ChatClientProps) {
const [chats, setChats] = useState<string[]>(['cuT9MKk', 'EufJ5I8'])
const addChat = (id: string) => {
setChats([...chats, id])
}
const removeChat = (id: string) => {
setChats(chats.filter(chatId => chatId !== id))
}
return (
<div className="group flex w-full h-full space-x-5 overflow-auto m-2 border-2 border-red-500/20 rounded-md peer-[[data-state=open]]:lg:ml-[250px] peer-[[data-state=open]]:xl:ml-[300px]">
{chats.map((id, index) => cloneElement(children as ReactElement, { id }))}
</div>
)
}