Skip to content

Commit

Permalink
Zeta AI improvements (#415)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-janon authored Jul 31, 2024
1 parent 3e378a9 commit eda62df
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 18 deletions.
6 changes: 3 additions & 3 deletions src/app/api/chat/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Ratelimit } from "@upstash/ratelimit";
import { kv } from "@vercel/kv";
import { CoreMessage, embed, streamText } from "ai";

import { inputSchema } from "~/lib/ai.constants";
import { supabaseClient } from "~/lib/supabase/client";

const getPrompt = (
Expand Down Expand Up @@ -62,8 +63,7 @@ export async function POST(req: Request) {
});
}

const { messages: _messages } = await req.json();
const messages = _messages as CoreMessage[];
const { messages } = inputSchema.parse(await req.json());

const userPrompt = messages[messages.length - 1].content;

Expand Down Expand Up @@ -100,6 +100,6 @@ export async function POST(req: Request) {
} catch (error) {
console.error(error);

return new Response("Internal Server Error", { status: 500 });
return new Response("Zeta AI had an error processing your request, please try asking again.", { status: 500 });
}
}
8 changes: 7 additions & 1 deletion src/components/Cmdk/components/Cmdk.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,13 @@ export const Cmdk: React.FC<CmdkProps> = ({ isOpen, setIsCmdkOpen }) => {
setIsCmdkOpen={setIsCmdkOpen}
/>
)}
{activePage === "chat" && <CmdkChat initialValue={inputValue} setCmdkInputValue={setInputValue} />}
{activePage === "chat" && (
<CmdkChat
closeCmdk={() => setIsCmdkOpen(false)}
initialValue={inputValue}
setCmdkInputValue={setInputValue}
/>
)}
</Command.List>
</Command>
</DialogContent>
Expand Down
7 changes: 4 additions & 3 deletions src/components/Cmdk/components/CmdkChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const AssistantMessage: React.FC<{ children: React.ReactNode; className?: string
);
};

export const CmdkChat: React.FC<CmdkChatProps> = ({ initialValue, setCmdkInputValue }) => {
export const CmdkChat: React.FC<CmdkChatProps> = ({ closeCmdk, initialValue, setCmdkInputValue }) => {
const inputRef = useRef<HTMLInputElement>(null);
const basePath =
process.env.NEXT_PUBLIC_BASE_PATH && typeof window !== "undefined"
Expand Down Expand Up @@ -87,14 +87,14 @@ export const CmdkChat: React.FC<CmdkChatProps> = ({ initialValue, setCmdkInputVa
className={clsx(index === messages.length - 1 && "mb-8")}
messageClasses={message.content.length < 90 ? "flex items-center" : undefined}
>
<MarkdownMessage message={message} />
<MarkdownMessage closeCmdk={closeCmdk} message={message} />
</AssistantMessage>
);
default:
return (
<div key={index} className="px-4 [overflow-anchor:none] mb-[25px]">
<div className="flex gap-6 [overflow-anchor:none] mb-6">
<MarkdownMessage message={message} />
<MarkdownMessage closeCmdk={closeCmdk} message={message} />
</div>
</div>
);
Expand Down Expand Up @@ -183,6 +183,7 @@ export const CmdkChat: React.FC<CmdkChatProps> = ({ initialValue, setCmdkInputVa
};

interface CmdkChatProps {
closeCmdk: () => void;
initialValue: string;
setCmdkInputValue: React.Dispatch<React.SetStateAction<string>>;
}
20 changes: 16 additions & 4 deletions src/components/Cmdk/components/MarkdownMesage.tsx
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { Message } from "ai";
import clsx from "clsx";
import Link from "next/link";
import React from "react";
import Markdown from "react-markdown";
import rehypeRaw from "rehype-raw";
import remarkGfm from "remark-gfm";

import { basePath } from "~/lib/app.constants";

import { MarkdownCodeBlock } from "./MarkdownCodeBlock";

const remarkPlugins = [[remarkGfm, { singleTilde: false }]];
const rehypePlugins = [rehypeRaw];

export const MarkdownMessage: React.FC<MarkdownMessageProps> = ({ message }) => {
export const MarkdownMessage: React.FC<MarkdownMessageProps> = ({ closeCmdk, message }) => {
return (
<Markdown
remarkPlugins={remarkPlugins as any}
Expand All @@ -26,11 +29,19 @@ export const MarkdownMessage: React.FC<MarkdownMessageProps> = ({ message }) =>
{children}
</MarkdownCodeBlock>
),
a: ({ children, ...args }) => {
a: ({ children, ref, ...args }) => {
if (typeof args.href !== "string") return null;
const isAbsoluteHref = args.href?.startsWith("http");

return (
<a {...args} className={clsx("text-green-300 hover:text-green-400", args.className)}>
<Link
{...args}
href={isAbsoluteHref ? args.href : `${basePath}${args.href}`}
onClick={closeCmdk}
className={clsx("text-green-300 hover:text-green-400", args.className)}
>
{children}
</a>
</Link>
);
},
// img: (props: any) => NextImageHandler(props),
Expand Down Expand Up @@ -86,5 +97,6 @@ export const MarkdownMessage: React.FC<MarkdownMessageProps> = ({ message }) =>
};

interface MarkdownMessageProps {
closeCmdk: () => void;
message: Message;
}
8 changes: 5 additions & 3 deletions src/components/Cmdk/components/ZetaAiIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import React from "react";

export const ZetaAiIcon: React.FC<ZetaAiIconProps> = ({ className }) => {
export const ZetaAiIcon: React.FC<ZetaAiIconProps> = ({ className, width, height }) => {
return (
<svg
width="91"
height="24"
width={width || "91"}
height={height || "24"}
viewBox="0 0 91 24"
fill="none"
xmlns="http://www.w3.org/2000/svg"
Expand All @@ -28,4 +28,6 @@ export const ZetaAiIcon: React.FC<ZetaAiIconProps> = ({ className }) => {

interface ZetaAiIconProps {
className?: string;
height?: string;
width?: string;
}
7 changes: 5 additions & 2 deletions src/components/shared/components/Layout/components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ const LayoutContainer = styled.div<{ isMainPage: boolean }>`

type LayoutProps = {
className?: string;
setIsCmdkOpen: React.Dispatch<React.SetStateAction<boolean>>;
};

export const Layout: React.FC<PropsWithChildren<LayoutProps>> = ({ className, children }) => {
export const Layout: React.FC<PropsWithChildren<LayoutProps>> = ({ className, children, setIsCmdkOpen }) => {
const { route } = useRouter();
const isMainPage = useMemo(() => mainNavRoutes.includes(route), [route]);

Expand All @@ -201,7 +202,9 @@ export const Layout: React.FC<PropsWithChildren<LayoutProps>> = ({ className, ch
return (
<LayoutContainer className={className} isMainPage={isMainPage}>
{!isMounted && <div className="absolute inset-0 z-[999999999] w-screen h-screen initial-overlay" />}
<NavigationLayout isMainPage={isMainPage}>{children}</NavigationLayout>
<NavigationLayout isMainPage={isMainPage} setIsCmdkOpen={setIsCmdkOpen}>
{children}
</NavigationLayout>
</LayoutContainer>
);
};
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { ListItem } from "@mui/material";
import Divider from "@mui/material/Divider";
import List from "@mui/material/List";
import clsx from "clsx";
import { motion } from "framer-motion";
import Link from "next/link";
import { PropsWithChildren, useEffect, useMemo, useState } from "react";

import { ZetaAiIcon } from "~/components/Cmdk/components/ZetaAiIcon";
import { useCurrentBreakpoint } from "~/hooks/useCurrentBreakpoint";
import { getRevealProps } from "~/lib/helpers/animations";

Expand All @@ -17,6 +19,7 @@ import { NavigationItem } from "./NavigationItem";

type NavigationLayoutProps = PropsWithChildren<{
isMainPage: boolean;
setIsCmdkOpen: React.Dispatch<React.SetStateAction<boolean>>;
}>;

const mainLayoutAnimationVariants = {
Expand All @@ -43,7 +46,7 @@ const leftDrawerAnimationVariants = {
},
};

export const NavigationLayout: React.FC<NavigationLayoutProps> = ({ isMainPage, children }) => {
export const NavigationLayout: React.FC<NavigationLayoutProps> = ({ isMainPage, children, setIsCmdkOpen }) => {
const { upSm } = useCurrentBreakpoint();

const [isLeftDrawerOpen, setIsLeftDrawerOpen] = useState(true);
Expand Down Expand Up @@ -132,6 +135,16 @@ export const NavigationLayout: React.FC<NavigationLayoutProps> = ({ isMainPage,

<div className="pb-6">
<List className="w-full font-medium">
<ListItem className={clsx("px-5 sm:px-[30px] py-3 hover:!bg-[transparent] whitespace-nowrap")}>
<button
onClick={() => {
setIsCmdkOpen(true);
if (!upSm) setIsLeftDrawerOpen(false);
}}
>
<ZetaAiIcon width={"60"} />
</button>
</ListItem>
{navBottomItems.map((item) => (
<NavigationItem
key={item.label}
Expand Down
10 changes: 10 additions & 0 deletions src/lib/ai.constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { z } from "zod";

export const inputSchema = z.object({
messages: z.array(
z.object({
role: z.enum(["assistant", "user", "system"]),
content: z.string().min(1).max(1500),
})
),
});
2 changes: 1 addition & 1 deletion src/pages/_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const App = ({ Component, pageProps, ...rest }: AppProps & { emotionCache: Emoti
<GlobalStyles />
<HeadProgressBar />

<Layout>
<Layout setIsCmdkOpen={setIsCmdkOpen}>
<Cmdk isOpen={isCmdkOpen} setIsCmdkOpen={setIsCmdkOpen} />
<Component {...pageProps} />
</Layout>
Expand Down

0 comments on commit eda62df

Please sign in to comment.