Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.
Merge branch 'feat/public-summary' into launch-week-2
Marc Seitz9682acb2d756
diff --git a/components/chat/chat-input.tsx b/components/chat/chat-input.tsxindex e0a3b8ca..82e328aa 100644--- a/components/chat/chat-input.tsx+++ b/components/chat/chat-input.tsx@@ -33,9 +33,9 @@ export function ChatInput({}, [status]);return (- <div className="fixed inset-x-0 bottom-0">+ <div className="relative inset-x-0 bottom-0"><div className="mx-auto sm:max-w-3xl sm:px-4">- <div className="space-y-4 bg-background px-4 py-4 shadow-lg md:py-4">+ <div className="space-y-4 bg-background px-4 py-4 md:py-4"><form onSubmit={submitMessage} ref={formRef}><div className="relative flex max-h-60 w-full flex-col overflow-hidden bg-background pr-8 rounded-xl ring-1 ring-muted-foreground/50 sm:pr-12 focus-within:ring-1 focus-within:ring-foreground"><Textareadiff --git a/components/chat/chat.tsx b/components/chat/chat.tsxindex a7a9d342..7ac7c0bb 100644--- a/components/chat/chat.tsx+++ b/components/chat/chat.tsx@@ -14,8 +14,9 @@ import { useEffect, useState } from "react";export interface ChatProps extends React.ComponentProps<"div"> {initialMessages: Message[];- threadId: string;- firstPage: string;+ threadId?: string;+ firstPage?: string;+ isPublic?: boolean;}export function Chat({@@ -23,6 +24,7 @@ export function Chat({threadId,firstPage,className,+ isPublic,}: ChatProps) {const {status,@@ -34,6 +36,9 @@ export function Chat({} = useAssistant({api: "/api/assistants/chat",threadId: threadId,+ body: {+ isPublic: isPublic,+ },});const [combinedMessages, setCombinedMessages] = useState<Message[]>([]);@@ -56,11 +61,18 @@ export function Chat({return (<>- <div className={cn("pb-[100px] pt-24", className)}>+ <div+ className={cn(+ "pb-[20px] pt-24 h-[calc(100vh-96px)] relative overflow-y-auto",+ className,+ )}+ >{combinedMessages.length ? (<><ChatList messages={combinedMessages} status={status} />- <ChatScrollAnchor trackVisibility={isLoading} />+ {!isPublic ? (+ <ChatScrollAnchor trackVisibility={isLoading} />+ ) : null}</>) : (<EmptyScreendiff --git a/components/chat/empty-screen.tsx b/components/chat/empty-screen.tsxindex 56d46f95..0b6e6430 100644--- a/components/chat/empty-screen.tsx+++ b/components/chat/empty-screen.tsx@@ -1,5 +1,6 @@import { Button } from "@/components/ui/button";import Image from "next/image";+import Sparkle from "../shared/icons/sparke";const exampleMessages = [{@@ -26,7 +27,7 @@ export function EmptyScreen({setInput,handleInputChange,}: {- firstPage: string;+ firstPage?: string;setInput: (input: string) => void;handleInputChange: (e: any) => void;}) {@@ -40,19 +41,36 @@ export function EmptyScreen({<><div className="mx-auto w-1/2 md:w-1/3"><div className="flex items-center justify-center">- <Image- src={firstPage}- width={768}- height={100}- className="object-contain rounded-md ring-1 ring-gray-700"- alt="First page of the document"- />+ {firstPage ? (+ <Image+ src={firstPage}+ width={768}+ height={100}+ className="object-contain rounded-md ring-1 ring-gray-700"+ alt="First page of the document"+ />+ ) : (+ <div className="flex items-center justify-center rounded-md w-full">+ <p className="inline-flex text-xl">+ Chat with{" "}+ <span className="inline-flex items-center mx-1 space-x-1">+ <span className="text-xl font-bold tracking-tighter ">+ Papermark+ </span>+ 's+ </span>+ pitchdeck+ </p>+ </div>+ )}</div>+<div className="flex justify-center text-lg mt-4">What would you like to know?</div></div>- <div className="mx-auto max-w-2xl px-4 fixed bottom-28 inset-x-0">++ <div className="mx-auto max-w-2xl px-4 absolute bottom-0 inset-x-0"><div className="grid grid-cols-1 sm:grid-cols-2 gap-2">{exampleMessages.map((message, index) => (<Buttondiff --git a/components/documents/document-card.tsx b/components/documents/document-card.tsxindex 437f118f..fadf8d48 100644--- a/components/documents/document-card.tsx+++ b/components/documents/document-card.tsx@@ -103,7 +103,7 @@ export default function DocumentsCard({</div><div className="flex flex-row space-x-2">- {document.type === "notion" ? (+ {document.type !== "notion" ? (<buttononClick={(e) => {e.stopPropagation();@@ -111,8 +111,8 @@ export default function DocumentsCard({}}className="group/button flex items-center z-10 space-x-1 rounded-md ring-1 ring-gray-200 dark:ring-gray-700 px-2 py-0.5 transition-all duration-75 hover:scale-105 active:scale-95">- <PapermarkSparkle className="h-4 w-4 text-gray-400 group-hover/row:text-foreground group-hover/row:animate-pulse group-hover/button:text-white group-hover/button:animate-none" />- <span className="whitespace-nowrap text-sm ml-1 hidden sm:inline-block text-gray-400 group-hover/button:text-white">+ <PapermarkSparkle className="h-4 w-4 text-gray-400 group-hover/row:text-foreground group-hover/row:animate-pulse group-hover/button:text-foreground group-hover/button:animate-none" />+ <span className="whitespace-nowrap text-sm ml-1 hidden sm:inline-block text-gray-400 group-hover/button:text-foreground">AI Assistant</span></button>diff --git a/components/web/assistant/public-chat.tsx b/components/web/assistant/public-chat.tsxnew file mode 100644index 00000000..0d925d95--- /dev/null+++ b/components/web/assistant/public-chat.tsx@@ -0,0 +1,15 @@+import { type Message } from "ai/react";+import { Chat } from "@/components/chat/chat";++export default function ChatPage() {+ const initialMessages: Message[] = [];+ return (+ <>+ <Chat+ initialMessages={initialMessages}+ isPublic={true}+ className="h-[calc(100vh-180px)]"+ />+ </>+ );+}diff --git a/lib/redis.ts b/lib/redis.tsnew file mode 100644index 00000000..3a1dd024--- /dev/null+++ b/lib/redis.ts@@ -0,0 +1,6 @@+import { Redis } from "@upstash/redis";++export const redis = new Redis({+ url: process.env.UPSTASH_REDIS_REST_TOKEN as string,+ token: process.env.UPSTASH_REDIS_REST_TOKEN as string,+});diff --git a/package-lock.json b/package-lock.jsonindex a2ecd5ac..48d9f0a8 100644--- a/package-lock.json+++ b/package-lock.json@@ -38,6 +38,8 @@"@types/react": "^18.2.37","@types/react-dom": "^18.2.15","@upstash/qstash": "^2.2.0",+ "@upstash/ratelimit": "^1.0.0",+ "@upstash/redis": "^1.25.1","@vercel/analytics": "^1.1.1","@vercel/blob": "^0.15.1","ai": "^2.2.25",@@ -3082,6 +3084,17 @@"resolved": "https://registry.npmjs.org/@ungap/structured-clone/-/structured-clone-1.2.0.tgz","integrity": "sha512-zuVdFrMJiuCDQUMCzQaD6KL28MjnqqN8XnAqiEq9PNm/hCPTSGfrXCOfwj1ow4LFb/tNymJPwsNbVePc1xFqrQ=="},+ "node_modules/@upstash/core-analytics": {+ "version": "0.0.6",+ "resolved": "https://registry.npmjs.org/@upstash/core-analytics/-/core-analytics-0.0.6.tgz",+ "integrity": "sha512-cpPSR0XJAJs4Ddz9nq3tINlPS5aLfWVCqhhtHnXt4p7qr5+/Znlt1Es736poB/9rnl1hAHrOsOvVj46NEXcVqA==",+ "dependencies": {+ "@upstash/redis": "^1.19.3"+ },+ "engines": {+ "node": ">=16.0.0"+ }+ },"node_modules/@upstash/qstash": {"version": "2.2.0","resolved": "https://registry.npmjs.org/@upstash/qstash/-/qstash-2.2.0.tgz",@@ -3091,6 +3104,22 @@"jose": "^4.14.4"}},+ "node_modules/@upstash/ratelimit": {+ "version": "1.0.0",+ "resolved": "https://registry.npmjs.org/@upstash/ratelimit/-/ratelimit-1.0.0.tgz",+ "integrity": "sha512-IUlc+UKuHJQqXGQi7E3UKV0CKfRZ0g6qeNnbSve940k3xLoM+X3H45MrSwDaH7meMWemztLs0Q4yjkd3ItddCw==",+ "dependencies": {+ "@upstash/core-analytics": "^0.0.6"+ }+ },+ "node_modules/@upstash/redis": {+ "version": "1.25.1",+ "resolved": "https://registry.npmjs.org/@upstash/redis/-/redis-1.25.1.tgz",+ "integrity": "sha512-ACj0GhJ4qrQyBshwFgPod6XufVEfKX2wcaihsEvSdLYnY+m+pa13kGt1RXm/yTHKf4TQi/Dy2A8z/y6WUEOmlg==",+ "dependencies": {+ "crypto-js": "^4.2.0"+ }+ },"node_modules/@vercel/analytics": {"version": "1.1.1","resolved": "https://registry.npmjs.org/@vercel/analytics/-/analytics-1.1.1.tgz",diff --git a/package.json b/package.jsonindex f831d32e..b560333b 100644--- a/package.json+++ b/package.json@@ -45,6 +45,8 @@"@types/react": "^18.2.37","@types/react-dom": "^18.2.15","@upstash/qstash": "^2.2.0",+ "@upstash/ratelimit": "^1.0.0",+ "@upstash/redis": "^1.25.1","@vercel/analytics": "^1.1.1","@vercel/blob": "^0.15.1","ai": "^2.2.25",@@ -69,9 +71,9 @@"next-auth": "^4.24.5","next-plausible": "^3.11.3","next-themes": "^0.2.1",- "openai": "^4.20.0","notion-client": "^6.16.0","notion-utils": "^6.16.0",+ "openai": "^4.20.0","postcss": "^8.4.31","react": "^18.2.0","react-dom": "^18.2.0",diff --git a/pages/api/assistants/chat.ts b/pages/api/assistants/chat.tsindex 7b2a736d..a9bbdf4f 100644--- a/pages/api/assistants/chat.ts+++ b/pages/api/assistants/chat.ts@@ -1,12 +1,35 @@import { experimental_AssistantResponse } from "ai";import OpenAI from "openai";import { type MessageContentText } from "openai/resources/beta/threads/messages/messages";+import { Ratelimit } from "@upstash/ratelimit";+import { redis } from "@/lib/redis";// Create an OpenAI API client (that's edge friendly!)const openai = new OpenAI({apiKey: process.env.OPENAI_API_KEY || "",});+const ratelimit = {+ public: new Ratelimit({+ redis,+ analytics: true,+ prefix: "ratelimit:public",+ limiter: Ratelimit.slidingWindow(1, "3600s"),+ }),+ free: new Ratelimit({+ redis,+ analytics: true,+ prefix: "ratelimit:free",+ limiter: Ratelimit.slidingWindow(60, "10s"),+ }),+ paid: new Ratelimit({+ redis,+ analytics: true,+ prefix: "ratelimit:paid",+ limiter: Ratelimit.slidingWindow(60, "10s"),+ }),+};+// IMPORTANT! Set the runtime to edgeexport const config = {runtime: "edge",@@ -15,28 +38,60 @@ export const config = {export default async function POST(req: Request) {// Parse the request bodyconst input: {- threadId: string;+ threadId: string | null;message: string;+ isPublic: boolean | null;} = await req.json();+ if (input.isPublic) {+ const ip = req.headers.get("x-forwarded-for");+ const ratelimit = new Ratelimit({+ redis: redis,+ // rate limit to 5 requests per hour+ limiter: Ratelimit.slidingWindow(5, "3600s"),+ });++ const { success, limit, reset, remaining } = await ratelimit.limit(+ `ratelimit_${ip}`,+ );++ console.log("ratelimit", { success, limit, reset, remaining });++ if (!success) {+ return new Response("You have reached your request limit for the day.", {+ status: 429,+ headers: {+ "X-RateLimit-Limit": limit.toString(),+ "X-RateLimit-Remaining": remaining.toString(),+ "X-RateLimit-Reset": reset.toString(),+ },+ });+ }+ }++ // create a threadId if one wasn't provided+ const threadId = input.threadId ?? (await openai.beta.threads.create()).id;+// Add a message to the thread- const createdMessage = await openai.beta.threads.messages.create(- input.threadId,- {- role: "user",- content: input.message,- },- );+ const createdMessage = await openai.beta.threads.messages.create(threadId, {+ role: "user",+ content: input.message,+ });++ // select the assistantId based on the isPublic flag+ const assistantId = input.isPublic+ ? (process.env.OAI_PUBLIC_ASSISTANT_ID as string)+ : (process.env.OAI_ASSISTANT_ID as string);return experimental_AssistantResponse({- threadId: input.threadId,+ threadId,messageId: createdMessage.id,},async ({ threadId, sendMessage }) => {// Run the assistant on the threadconst run = await openai.beta.threads.runs.create(threadId, {- assistant_id: process.env.OAI_ASSISTANT_ID!,+ assistant_id: assistantId!,});async function waitForRun(run: OpenAI.Beta.Threads.Runs.Run) {diff --git a/pages/launch-week.tsx b/pages/launch-week.tsxindex 0f828319..d1ed9164 100644--- a/pages/launch-week.tsx+++ b/pages/launch-week.tsx@@ -4,6 +4,7 @@ import Hero from "@/components/web/launch/hero";import Grid from "@/components/web/launch/daysgrid";import CTA from "@/components/web/launch/cta";import Head from "next/head";+import ChatPage from "@/components/web/assistant/public-chat";export default function LaunchWeek() {return (@@ -32,6 +33,9 @@ export default function LaunchWeek() {<Hero /><Grid /><CTA />+ <div className="relative bg-background">+ <ChatPage />+ </div></div></div><Footer />