From baaa696797c6d40677979514c4278472a49d126b Mon Sep 17 00:00:00 2001 From: vivoidos Date: Sat, 23 Nov 2024 07:14:54 +0300 Subject: [PATCH] feat: add agent selection, router and sidebar layout in React client --- client/package.json | 11 +- client/src/Agent.tsx | 10 + client/src/Agents.tsx | 47 + client/src/App.css | 1 - client/src/App.tsx | 65 +- client/src/Character.tsx | 7 + client/src/Chat.tsx | 104 ++ client/src/Layout.tsx | 12 + client/src/components/app-sidebar.tsx | 56 + client/src/components/ui/separator.tsx | 33 + client/src/components/ui/sheet.tsx | 136 ++ client/src/components/ui/sidebar.tsx | 786 ++++++++++++ client/src/components/ui/skeleton.tsx | 15 + client/src/components/ui/tooltip.tsx | 32 + client/src/hooks/use-mobile.tsx | 23 + client/src/index.css | 40 + client/src/main.tsx | 11 +- client/src/router.tsx | 32 + client/tailwind.config.js | 2 +- packages/client-direct/package.json | 1 + packages/client-direct/src/api.ts | 70 ++ packages/client-direct/src/index.ts | 4 + pnpm-lock.yaml | 1596 +++++++++++++++++------- 23 files changed, 2541 insertions(+), 553 deletions(-) create mode 100644 client/src/Agent.tsx create mode 100644 client/src/Agents.tsx create mode 100644 client/src/Character.tsx create mode 100644 client/src/Chat.tsx create mode 100644 client/src/Layout.tsx create mode 100644 client/src/components/app-sidebar.tsx create mode 100644 client/src/components/ui/separator.tsx create mode 100644 client/src/components/ui/sheet.tsx create mode 100644 client/src/components/ui/sidebar.tsx create mode 100644 client/src/components/ui/skeleton.tsx create mode 100644 client/src/components/ui/tooltip.tsx create mode 100644 client/src/hooks/use-mobile.tsx create mode 100644 client/src/router.tsx create mode 100644 packages/client-direct/src/api.ts diff --git a/client/package.json b/client/package.json index aad8c3c91f..aef79273ff 100644 --- a/client/package.json +++ b/client/package.json @@ -11,12 +11,17 @@ }, "dependencies": { "@ai16z/eliza": "workspace:*", + "@radix-ui/react-dialog": "^1.1.2", + "@radix-ui/react-separator": "^1.1.0", "@radix-ui/react-slot": "^1.1.0", + "@radix-ui/react-tooltip": "^1.1.4", + "@tanstack/react-query": "^5.61.0", "class-variance-authority": "^0.7.0", "clsx": "2.1.0", "lucide-react": "^0.460.0", "react": "^18.3.1", "react-dom": "^18.3.1", + "react-router-dom": "6.22.1", "tailwind-merge": "^2.5.4", "tailwindcss-animate": "^1.0.7", "vite-plugin-top-level-await": "^1.4.4", @@ -25,8 +30,8 @@ "devDependencies": { "@eslint/js": "^9.13.0", "@types/node": "22.8.4", - "@types/react": "18.3.12", - "@types/react-dom": "18.3.1", + "@types/react": "^18.3.12", + "@types/react-dom": "^18.3.1", "@vitejs/plugin-react": "^4.3.3", "autoprefixer": "^10.4.20", "eslint": "^9.13.0", @@ -37,6 +42,6 @@ "tailwindcss": "^3.4.15", "typescript": "~5.6.2", "typescript-eslint": "^8.11.0", - "vite": "^5.4.10" + "vite": "link:@tanstack/router-plugin/vite" } } diff --git a/client/src/Agent.tsx b/client/src/Agent.tsx new file mode 100644 index 0000000000..f3094f14eb --- /dev/null +++ b/client/src/Agent.tsx @@ -0,0 +1,10 @@ +export default function Agent() { + return ( +
+

+ Select an option from the sidebar to configure, view, or chat + with your ELIZA agent +

+
+ ); +} diff --git a/client/src/Agents.tsx b/client/src/Agents.tsx new file mode 100644 index 0000000000..06e2c56b49 --- /dev/null +++ b/client/src/Agents.tsx @@ -0,0 +1,47 @@ +import { useQuery } from "@tanstack/react-query"; +import { Button } from "@/components/ui/button"; +import { useNavigate } from "react-router-dom"; +import "./App.css"; + +type Agent = { + id: string; + name: string; +}; + +function Agents() { + const navigate = useNavigate(); + const { data: agents, isLoading } = useQuery({ + queryKey: ["agents"], + queryFn: async () => { + const res = await fetch("/api/agents"); + const data = await res.json(); + return data.agents as Agent[]; + }, + }); + + return ( +
+

Select your agent:

+ + {isLoading ? ( +
Loading agents...
+ ) : ( +
+ {agents?.map((agent) => ( + + ))} +
+ )} +
+ ); +} + +export default Agents; diff --git a/client/src/App.css b/client/src/App.css index f44fb79ad3..d6055f0d02 100644 --- a/client/src/App.css +++ b/client/src/App.css @@ -1,7 +1,6 @@ #root { max-width: 1280px; margin: 0 auto; - padding: 2rem; text-align: center; } diff --git a/client/src/App.tsx b/client/src/App.tsx index f48537f0cb..c5b0826f12 100644 --- a/client/src/App.tsx +++ b/client/src/App.tsx @@ -1,71 +1,10 @@ -import { useState } from "react"; -import { Input } from "@/components/ui/input"; -import { Button } from "@/components/ui/button"; import "./App.css"; -import { stringToUuid } from "@ai16z/eliza"; - -type TextResponse = { - text: string; - user: string; -}; +import Agents from "./Agents"; function App() { - const [input, setInput] = useState(""); - const [response, setResponse] = useState([]); - const [loading, setLoading] = useState(false); - - const handleSubmit = async (e: React.FormEvent) => { - e.preventDefault(); - setLoading(true); - - try { - const res = await fetch(`/api/${stringToUuid("Eliza")}/message`, { - method: "POST", - headers: { - "Content-Type": "application/json", - }, - body: JSON.stringify({ - text: input, - userId: "user", - roomId: `default-room-${stringToUuid("Eliza")}`, - }), - }); - - const data: TextResponse[] = await res.json(); - - console.log(data); - setResponse(data); - setInput(""); - } catch (error) { - console.error("Error:", error); - setResponse([{ text: "An error occurred", user: "system" }]); - } finally { - setLoading(false); - } - }; - return (
-

Chat with Eliza

-
- setInput(e.target.value)} - placeholder="Enter your message..." - className="w-full" - /> - -
- - {(loading || response) && ( -
- {response.map((r) => ( -

{r.text}

- ))} -
- )} +
); } diff --git a/client/src/Character.tsx b/client/src/Character.tsx new file mode 100644 index 0000000000..bdb53882ad --- /dev/null +++ b/client/src/Character.tsx @@ -0,0 +1,7 @@ +export default function Character() { + return ( +
+

WIP

+
+ ); +} diff --git a/client/src/Chat.tsx b/client/src/Chat.tsx new file mode 100644 index 0000000000..b32cc0b83e --- /dev/null +++ b/client/src/Chat.tsx @@ -0,0 +1,104 @@ +import { useState } from "react"; +import { useParams } from "react-router-dom"; +import { useMutation } from "@tanstack/react-query"; +import { Input } from "@/components/ui/input"; +import { Button } from "@/components/ui/button"; +import "./App.css"; + +type TextResponse = { + text: string; + user: string; +}; + +export default function Chat() { + const { agentId } = useParams(); + const [input, setInput] = useState(""); + const [messages, setMessages] = useState([]); + + const mutation = useMutation({ + mutationFn: async (text: string) => { + const res = await fetch(`/api/${agentId}/message`, { + method: "POST", + headers: { + "Content-Type": "application/json", + }, + body: JSON.stringify({ + text, + userId: "user", + roomId: `default-room-${agentId}`, + }), + }); + return res.json() as Promise; + }, + onSuccess: (data) => { + setMessages((prev) => [...prev, ...data]); + }, + }); + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + if (!input.trim()) return; + + // Add user message immediately to state + const userMessage: TextResponse = { + text: input, + user: "user", + }; + setMessages((prev) => [...prev, userMessage]); + + mutation.mutate(input); + setInput(""); + }; + + return ( +
+
+
+ {messages.length > 0 ? ( + messages.map((message, index) => ( +
+
+ {message.text} +
+
+ )) + ) : ( +
+ No messages yet. Start a conversation! +
+ )} +
+
+ +
+
+
+ setInput(e.target.value)} + placeholder="Type a message..." + className="flex-1" + disabled={mutation.isPending} + /> + +
+
+
+
+ ); +} diff --git a/client/src/Layout.tsx b/client/src/Layout.tsx new file mode 100644 index 0000000000..70c79f7403 --- /dev/null +++ b/client/src/Layout.tsx @@ -0,0 +1,12 @@ +import { SidebarProvider } from "@/components/ui/sidebar"; +import { AppSidebar } from "@/components/app-sidebar"; +import { Outlet } from "react-router-dom"; + +export default function Layout() { + return ( + + + + + ); +} diff --git a/client/src/components/app-sidebar.tsx b/client/src/components/app-sidebar.tsx new file mode 100644 index 0000000000..5245ad8feb --- /dev/null +++ b/client/src/components/app-sidebar.tsx @@ -0,0 +1,56 @@ +import { Calendar, Home, Inbox, Search, Settings } from "lucide-react"; +import { useParams } from "react-router-dom"; + +import { + Sidebar, + SidebarContent, + SidebarGroup, + SidebarGroupContent, + SidebarGroupLabel, + SidebarMenu, + SidebarMenuButton, + SidebarMenuItem, + SidebarTrigger, +} from "@/components/ui/sidebar"; + +// Menu items. +const items = [ + { + title: "Chat", + url: "chat", + icon: Inbox, + }, + { + title: "Character Overview", + url: "character", + icon: Calendar, + }, +]; + +export function AppSidebar() { + const { agentId } = useParams(); + + return ( + + + + Application + + + {items.map((item) => ( + + + + + {item.title} + + + + ))} + + + + + + ); +} diff --git a/client/src/components/ui/separator.tsx b/client/src/components/ui/separator.tsx new file mode 100644 index 0000000000..2af4ec891e --- /dev/null +++ b/client/src/components/ui/separator.tsx @@ -0,0 +1,33 @@ +"use client"; + +import * as React from "react"; +import * as SeparatorPrimitive from "@radix-ui/react-separator"; + +import { cn } from "@/lib/utils"; + +const Separator = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>( + ( + { className, orientation = "horizontal", decorative = true, ...props }, + ref + ) => ( + + ) +); +Separator.displayName = SeparatorPrimitive.Root.displayName; + +export { Separator }; diff --git a/client/src/components/ui/sheet.tsx b/client/src/components/ui/sheet.tsx new file mode 100644 index 0000000000..e18e295c73 --- /dev/null +++ b/client/src/components/ui/sheet.tsx @@ -0,0 +1,136 @@ +import * as React from "react"; +import * as SheetPrimitive from "@radix-ui/react-dialog"; +import { cva, type VariantProps } from "class-variance-authority"; +import { X } from "lucide-react"; + +import { cn } from "@/lib/utils"; + +const Sheet = SheetPrimitive.Root; + +const SheetTrigger = SheetPrimitive.Trigger; + +const SheetClose = SheetPrimitive.Close; + +const SheetPortal = SheetPrimitive.Portal; + +const SheetOverlay = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)); +SheetOverlay.displayName = SheetPrimitive.Overlay.displayName; + +const sheetVariants = cva( + "fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=closed]:duration-300 data-[state=open]:duration-500 data-[state=open]:animate-in data-[state=closed]:animate-out", + { + variants: { + side: { + top: "inset-x-0 top-0 border-b data-[state=closed]:slide-out-to-top data-[state=open]:slide-in-from-top", + bottom: "inset-x-0 bottom-0 border-t data-[state=closed]:slide-out-to-bottom data-[state=open]:slide-in-from-bottom", + left: "inset-y-0 left-0 h-full w-3/4 border-r data-[state=closed]:slide-out-to-left data-[state=open]:slide-in-from-left sm:max-w-sm", + right: "inset-y-0 right-0 h-full w-3/4 border-l data-[state=closed]:slide-out-to-right data-[state=open]:slide-in-from-right sm:max-w-sm", + }, + }, + defaultVariants: { + side: "right", + }, + } +); + +interface SheetContentProps + extends React.ComponentPropsWithoutRef, + VariantProps {} + +const SheetContent = React.forwardRef< + React.ElementRef, + SheetContentProps +>(({ side = "right", className, children, ...props }, ref) => ( + + + + + + Close + + {children} + + +)); +SheetContent.displayName = SheetPrimitive.Content.displayName; + +const SheetHeader = ({ + className, + ...props +}: React.HTMLAttributes) => ( +
+); +SheetHeader.displayName = "SheetHeader"; + +const SheetFooter = ({ + className, + ...props +}: React.HTMLAttributes) => ( +
+); +SheetFooter.displayName = "SheetFooter"; + +const SheetTitle = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)); +SheetTitle.displayName = SheetPrimitive.Title.displayName; + +const SheetDescription = React.forwardRef< + React.ElementRef, + React.ComponentPropsWithoutRef +>(({ className, ...props }, ref) => ( + +)); +SheetDescription.displayName = SheetPrimitive.Description.displayName; + +export { + Sheet, + SheetPortal, + SheetOverlay, + SheetTrigger, + SheetClose, + SheetContent, + SheetHeader, + SheetFooter, + SheetTitle, + SheetDescription, +}; diff --git a/client/src/components/ui/sidebar.tsx b/client/src/components/ui/sidebar.tsx new file mode 100644 index 0000000000..ab5862ab35 --- /dev/null +++ b/client/src/components/ui/sidebar.tsx @@ -0,0 +1,786 @@ +import * as React from "react"; +import { Slot } from "@radix-ui/react-slot"; +import { VariantProps, cva } from "class-variance-authority"; +import { PanelLeft } from "lucide-react"; + +import { useIsMobile } from "@/hooks/use-mobile"; +import { cn } from "@/lib/utils"; +import { Button } from "@/components/ui/button"; +import { Input } from "@/components/ui/input"; +import { Separator } from "@/components/ui/separator"; +import { Sheet, SheetContent } from "@/components/ui/sheet"; +import { Skeleton } from "@/components/ui/skeleton"; +import { + Tooltip, + TooltipContent, + TooltipProvider, + TooltipTrigger, +} from "@/components/ui/tooltip"; + +const SIDEBAR_COOKIE_NAME = "sidebar:state"; +const SIDEBAR_COOKIE_MAX_AGE = 60 * 60 * 24 * 7; +const SIDEBAR_WIDTH = "16rem"; +const SIDEBAR_WIDTH_MOBILE = "18rem"; +const SIDEBAR_WIDTH_ICON = "3rem"; +const SIDEBAR_KEYBOARD_SHORTCUT = "b"; + +type SidebarContext = { + state: "expanded" | "collapsed"; + open: boolean; + setOpen: (open: boolean) => void; + openMobile: boolean; + setOpenMobile: (open: boolean) => void; + isMobile: boolean; + toggleSidebar: () => void; +}; + +const SidebarContext = React.createContext(null); + +function useSidebar() { + const context = React.useContext(SidebarContext); + if (!context) { + throw new Error("useSidebar must be used within a SidebarProvider."); + } + + return context; +} + +const SidebarProvider = React.forwardRef< + HTMLDivElement, + React.ComponentProps<"div"> & { + defaultOpen?: boolean; + open?: boolean; + onOpenChange?: (open: boolean) => void; + } +>( + ( + { + defaultOpen = true, + open: openProp, + onOpenChange: setOpenProp, + className, + style, + children, + ...props + }, + ref + ) => { + const isMobile = useIsMobile(); + const [openMobile, setOpenMobile] = React.useState(false); + + // This is the internal state of the sidebar. + // We use openProp and setOpenProp for control from outside the component. + const [_open, _setOpen] = React.useState(defaultOpen); + const open = openProp ?? _open; + const setOpen = React.useCallback( + (value: boolean | ((value: boolean) => boolean)) => { + const openState = + typeof value === "function" ? value(open) : value; + if (setOpenProp) { + setOpenProp(openState); + } else { + _setOpen(openState); + } + + // This sets the cookie to keep the sidebar state. + document.cookie = `${SIDEBAR_COOKIE_NAME}=${openState}; path=/; max-age=${SIDEBAR_COOKIE_MAX_AGE}`; + }, + [setOpenProp, open] + ); + + // Helper to toggle the sidebar. + const toggleSidebar = React.useCallback(() => { + return isMobile + ? setOpenMobile((open) => !open) + : setOpen((open) => !open); + }, [isMobile, setOpen, setOpenMobile]); + + // Adds a keyboard shortcut to toggle the sidebar. + React.useEffect(() => { + const handleKeyDown = (event: KeyboardEvent) => { + if ( + event.key === SIDEBAR_KEYBOARD_SHORTCUT && + (event.metaKey || event.ctrlKey) + ) { + event.preventDefault(); + toggleSidebar(); + } + }; + + window.addEventListener("keydown", handleKeyDown); + return () => window.removeEventListener("keydown", handleKeyDown); + }, [toggleSidebar]); + + // We add a state so that we can do data-state="expanded" or "collapsed". + // This makes it easier to style the sidebar with Tailwind classes. + const state = open ? "expanded" : "collapsed"; + + const contextValue = React.useMemo( + () => ({ + state, + open, + setOpen, + isMobile, + openMobile, + setOpenMobile, + toggleSidebar, + }), + [ + state, + open, + setOpen, + isMobile, + openMobile, + setOpenMobile, + toggleSidebar, + ] + ); + + return ( + + +
+ {children} +
+
+
+ ); + } +); +SidebarProvider.displayName = "SidebarProvider"; + +const Sidebar = React.forwardRef< + HTMLDivElement, + React.ComponentProps<"div"> & { + side?: "left" | "right"; + variant?: "sidebar" | "floating" | "inset"; + collapsible?: "offcanvas" | "icon" | "none"; + } +>( + ( + { + side = "left", + variant = "sidebar", + collapsible = "offcanvas", + className, + children, + ...props + }, + ref + ) => { + const { isMobile, state, openMobile, setOpenMobile } = useSidebar(); + + if (collapsible === "none") { + return ( +
+ {children} +
+ ); + } + + if (isMobile) { + return ( + + +
+ {children} +
+
+
+ ); + } + + return ( +
+ {/* This is what handles the sidebar gap on desktop */} +
+ +
+ ); + } +); +Sidebar.displayName = "Sidebar"; + +const SidebarTrigger = React.forwardRef< + React.ElementRef, + React.ComponentProps +>(({ className, onClick, ...props }, ref) => { + const { toggleSidebar } = useSidebar(); + + return ( + + ); +}); +SidebarTrigger.displayName = "SidebarTrigger"; + +const SidebarRail = React.forwardRef< + HTMLButtonElement, + React.ComponentProps<"button"> +>(({ className, ...props }, ref) => { + const { toggleSidebar } = useSidebar(); + + return ( +