Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions apps/chat-with-pdf/app/(default)/chat/[documentId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@ import { getChat } from "lib/supabase/queries/get-chat";
import { getDocumentByChatId } from "@/app/actions/get-document-by-chat-id";

type Props = {
params: {
params: Promise<{
documentId: string;
};
}>;
};

export async function generateMetadata({ params }: Props): Promise<Metadata> {
export async function generateMetadata(props: Props): Promise<Metadata> {
const params = await props.params;
const document = await getDocumentByChatId(params.documentId);

return {
title: `Chat - ${document?.name || "Untitled"}`,
};
}

export default async function Page({ params }: Props) {
export default async function Page(props: Props) {
const params = await props.params;
const chatData = await getChat(params.documentId);

if (!chatData) redirect("/chat");
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/(default)/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default async function DefaultLayout({
}: {
children: React.ReactNode;
}) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const {
data: { user },
} = await supabase.auth.getUser();
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/actions/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const verifyOtp = async (data: {
otp: string;
type: string;
}) => {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

const res = await supabase.auth.verifyOtp({
email: data.email,
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/actions/delete-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function deleteChat(
chatId: Tables<"Chat">["id"],
shouldRedirect = true,
) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

const { data, error } = await supabase
.from("Chat")
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/actions/edit-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { Tables } from "@makefy/supabase/types";
import { revalidatePath, revalidateTag } from "next/cache";

export async function editChat(document: Tables<"Document">, title: string) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

const { error } = await supabase
.from("Document")
Expand Down
4 changes: 2 additions & 2 deletions apps/chat-with-pdf/app/actions/generate-document-title.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use server";

import { getContext } from "@/lib/context";
import { getContext } from "@/utils/context";
import { createSupabaseServer } from "@makefy/supabase/server";
import { google } from "@ai-sdk/google";
import { generateObject } from "ai";
Expand All @@ -22,7 +22,7 @@ export async function generateDocumentTitle(documentId: string) {
});

if (object.title) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const { data } = await supabase.auth.getSession();
revalidatePath(`/chat/${documentId}`);
revalidateTag(documentId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"use server";

import { getContext } from "@/lib/context";
import { getContext } from "@/utils/context";
import { google } from "@ai-sdk/google";
import { generateObject } from "ai";
import { revalidatePath } from "next/cache";
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/actions/get-document-by-chat-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async function retrieveDocumentByChatId(
}

export async function getDocumentByChatId(chatId: string) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const { data, error: errorOnFetchingSession } = await supabase.auth.getUser();

if (errorOnFetchingSession) {
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/actions/login.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type LoginProps = {
};

export async function login(loginData: LoginProps) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

const { error } = await supabase.auth.signInWithPassword(loginData);

Expand Down
4 changes: 2 additions & 2 deletions apps/chat-with-pdf/app/actions/sign-in-with-oauth.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use server";

import { getOAuthRedirectUrl } from "@/lib/oauth-redirect-url";
import { getOAuthRedirectUrl } from "@/utils/oauth-redirect-url";
import { createSupabaseServer } from "@makefy/supabase/server";
import { ReadonlyURLSearchParams, redirect } from "next/navigation";
import type { SignInWithOAuthCredentials } from "@makefy/supabase/types";
export async function signInWithOAuth(
provider: SignInWithOAuthCredentials["provider"],
searchParams: ReadonlyURLSearchParams,
) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const redirectTo = getOAuthRedirectUrl(searchParams);

const { data, error } = await supabase.auth.signInWithOAuth({
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/actions/signup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ type SignUpProps = {
};

export async function signup(signUpData: SignUpProps) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

const baseUrl = process.env.VERCEL_URL || "https://localhost:3000";
const emailRedirectTo = `${baseUrl}/api/auth/callback`;
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/actions/submit-feedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function submitFeedback({
throw new Error("Type and message are required");
}

const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

const { data, error } = await supabase.from("Feedback").insert({
type,
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/actions/update-chat-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export async function updateChatMessages({
messages,
documentMetadata,
}: UpdateChatMessagesParams) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

await supabase
.from("Chat")
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/api/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function GET(request: Request) {
const next = searchParams.get("next") ?? "/";

if (code) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const { error } = await supabase.auth.exchangeCodeForSession(code);

if (!error) {
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/api/auth/confirm/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export async function GET(request: NextRequest) {
const next = searchParams.get("next") ?? "/";

if (token_hash && type) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

const { error } = await supabase.auth.verifyOtp({
type,
Expand Down
10 changes: 5 additions & 5 deletions apps/chat-with-pdf/app/api/chat/new-chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { deleteChat } from "@/app/actions/delete-chat";
import { INPUT_NAME } from "@/components/header/document-title/constants/input-names";
import { embedDocument, prepareDocument } from "@/lib/embed-document";
import { getLoadingMessages } from "@/lib/get-loading-messages";
import { getPdfData } from "@/lib/get-pdf-metadata";
import { rateLimitRequests } from "@/lib/rate-limit-requests";
import { embedDocument, prepareDocument } from "@/utils/embed-document";
import { getLoadingMessages } from "@/utils/get-loading-messages";
import { getPdfData } from "@/utils/get-pdf-metadata";
import { rateLimitRequests } from "@/utils/rate-limit-requests";
import { createSupabaseServer } from "@makefy/supabase/server";
import { WebPDFLoader } from "@langchain/community/document_loaders/web/pdf";
import { PineconeRecord } from "@pinecone-database/pinecone";
Expand Down Expand Up @@ -85,7 +85,7 @@ async function* createNewChat({
documentUrl: string;
documentFile?: File;
}) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
// Fetching PDF data and creating a new chat in the database
yield getLoadingMessages({
isViaLink: !!documentUrl,
Expand Down
7 changes: 3 additions & 4 deletions apps/chat-with-pdf/app/api/chat/route.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { rateLimitRequests } from "@/lib/rate-limit-requests";
import { rateLimitRequests } from "@/utils/rate-limit-requests";
import { google } from "@ai-sdk/google";
import { Message, StreamData, convertToCoreMessages, streamText } from "ai";
import { Tables } from "@makefy/supabase/types";
Expand Down Expand Up @@ -77,20 +77,19 @@ export async function POST(req: Request) {
const data = new StreamData();
data.append(messageData);

const result = streamText({
const result = await streamText({
model: google("gemini-1.5-flash-latest"),
messages: convertToCoreMessages(messages),
system: systemInstructions,
maxTokens: 3000,
onFinish({ text, toolCalls, toolResults, usage, finishReason, ...rest }) {
onFinish({ text, toolCalls, toolResults, usage, finishReason }) {
console.log({
onFinish: {
text,
toolCalls,
toolResults,
usage,
finishReason,
...rest,
},
});
data.close();
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/auth/callback/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export async function GET(request: Request) {
console.log({ code, next });

if (code) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const { error } = await supabase.auth.exchangeCodeForSession(code);
if (!error) {
return NextResponse.redirect(`${origin}${next}`);
Expand Down
10 changes: 9 additions & 1 deletion apps/chat-with-pdf/app/components/chat/assistant-message.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ export function AssistantMessage({
rehypePlugins={[rehypeRaw]}
className="px-4 py-3"
components={{
p: ({ children }) => <p className="text-sm">{children}</p>,
p: ({ children }) => {
// Check if the children contains a <div> element
const hasDiv = (children as string).includes("<div");
return hasDiv ? (
<div className="text-sm">{children}</div>
) : (
<p className="text-sm">{children}</p>
);
},
a: ({ children, href }) => (
<a href={href} className="text-blue-500 hover:underline">
{children}
Expand Down
4 changes: 2 additions & 2 deletions apps/chat-with-pdf/app/components/chat/chat-footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ export function ChatFooter() {

return (
<div className="border-border bg-background relative z-10 flex flex-col gap-2 border-t p-3">
<AnimatePresence>
<AnimatePresence key="suggested-questions">
{chatData.suggestedQuestions && (
<AnimatedSuggestedQuestions questions={chatData.suggestedQuestions} />
)}
</AnimatePresence>
<AnimatePresence>
<AnimatePresence key="quoted-text">
{(extraData?.quotedText as string) && (
<motion.div
initial={{ opacity: 0, height: 0 }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export const SuggestedQuestions = forwardRef<
})}
>
<motion.div
key="suggested-questions-container"
className={cn("flex", {
"space-x-2 overflow-x-auto pb-3": !isSuggestedQuestionsOpen,
"max-h-full w-full overflow-auto": isSuggestedQuestionsOpen,
Expand All @@ -109,6 +110,7 @@ export const SuggestedQuestions = forwardRef<
>
{questions.map((question) => (
<AnimatedButton
key={question}
variant="outline"
size="sm"
transition={{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@ import dynamic from "next/dynamic";
import { Message } from "ai";
import { useState } from "react";
import { SadFaceIcon } from "icons/sad-face";

// Dynamically import BlobProvider from react-pdf/renderer to avoid SSR issues
const BlobProvider = dynamic(
() => import("@react-pdf/renderer").then((mod) => mod.BlobProvider),
{
ssr: false,
},
);
import { BlobProvider } from "@react-pdf/renderer";

export function ChatHeader() {
const params = useParams();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@makefy/ui";
import { cn } from "@makefy/ui/lib/utils";
import { Message } from "ai";
import { AnimatePresence, CustomDomComponent, motion } from "framer-motion";
import { AnimatePresence, motion } from "framer-motion";
import { useGlobalChat } from "hooks/use-global-chat";
import {
BookmarkIcon,
Expand Down Expand Up @@ -178,9 +178,7 @@ export function MessageQuickActions({
quickActionIndex,
) => {
const AnimatedIcon = motion(Icon);
const AnimatedSucessIcon = (
SucessIcon ? motion(SucessIcon) : SucessIcon
) as CustomDomComponent<LucideProps & RefAttributes<SVGSVGElement>>;
const AnimatedSucessIcon = motion(SucessIcon ? SucessIcon : "svg");
const ActiveIcon = active?.Icon;
if (onlyLastMessage && index !== messages.length - 1) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
import { cn } from "@makefy/ui/lib/utils";
import { TrashIcon } from "lucide-react";
import { PDFDocument } from "pdf-lib";
import { RefObject, useEffect, useState } from "react";
import { RefObject, useEffect, useState, type JSX } from "react";
import { DropzoneInputProps, useDropzone } from "react-dropzone";
import {
Controller,
Expand Down Expand Up @@ -146,7 +146,7 @@ export function NewDocumentDialogContent() {
INPUT_NAME.FILE
>,
reactDropzoneInputProps: DropzoneInputProps & {
ref: RefObject<HTMLInputElement>;
ref: RefObject<HTMLInputElement | null>;
},
) {
const {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { UserNavMenuItems } from "./user-nav-menu-items";
import { createSupabaseServer } from "@makefy/supabase/server";

export async function UserNav() {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const {
data: { user },
} = await supabase.auth.getUser();
Expand Down
6 changes: 4 additions & 2 deletions apps/chat-with-pdf/app/components/pdf/pdf-viewer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { cn } from "@makefy/ui/lib/utils";
import { useGlobalChat } from "hooks/use-global-chat";
import { MessageSquareQuoteIcon } from "lucide-react";
import { PDFDocument } from "pdf-lib";
import { useRef, useState } from "react";
import { RefObject, useRef, useState } from "react";
import { Document, Page, pdfjs } from "react-pdf";
import "react-pdf/dist/Page/AnnotationLayer.css";
import "react-pdf/dist/Page/TextLayer.css";
Expand Down Expand Up @@ -53,7 +53,9 @@ export function PdfViewer({ className }: { className?: string }) {
const {
globalContext: { chatData, setExtraData, documentState, setDocumentState },
} = useGlobalChat();
useOnClickOutside(popoverRef, () => setSelectedTextOptions(null));
useOnClickOutside(popoverRef as RefObject<HTMLDivElement>, () =>
setSelectedTextOptions(null),
);
/* Tools */
const [currentZoom, setCurrentZoom] = useState<number>(1);
const [enableChangePageOnScroll, setEnableChangePageOnScroll] =
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/components/ui/user-avatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ interface UserAvatarProps {
}

export async function UserAvatar({ className }: UserAvatarProps) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const {
data: { user },
error,
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/app/private/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { createSupabaseServer } from "@makefy/supabase/server";
import { redirect } from "next/navigation";

export default async function PrivatePage() {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();

const { data, error } = await supabase.auth.getUser();
if (error || !data?.user) {
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/lib/supabase/queries/get-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ async function generateAndUpdateSuggestedQuestions(
}

export async function getChat(id: string) {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const { data, error: errorOnFetchingSession } = await supabase.auth.getUser();

if (errorOnFetchingSession) {
Expand Down
2 changes: 1 addition & 1 deletion apps/chat-with-pdf/lib/supabase/queries/get-chats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function retrieveChats(supabase: SupabaseClient) {
}

export async function getChats() {
const supabase = createSupabaseServer();
const supabase = await createSupabaseServer();
const { data, error: errorOnFetchingSession } = await supabase.auth.getUser();

if (errorOnFetchingSession) {
Expand Down
Loading