Skip to content
Open
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
11 changes: 6 additions & 5 deletions app/api/rag/summary/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { NextRequest, NextResponse } from 'next/server';
import { createClient } from '@supabase/supabase-js';
import OpenAI from 'openai';
import { SUMMARY_MODEL, SUMMARY_MAX_TOKENS, MAX_SUMMARY_CONTEXT_LENGTH, TOP_K_CHUNKS } from '@/lib/constants';

const supabase = createClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
Expand Down Expand Up @@ -72,18 +73,18 @@ export async function POST(request: NextRequest) {
.select('content')
.eq('paper_id', paper_id)
.order('page_no', { ascending: true })
.limit(5);
.limit(TOP_K_CHUNKS);

// If we have chunks, use them. Otherwise, try to use raw PDF content
let contextForSummary = '';

if (chunks && chunks.length > 0) {
contextForSummary = chunks.map(c => c.content).join('\n\n').slice(0, 4000);
contextForSummary = chunks.map(c => c.content).join('\n\n').slice(0, MAX_SUMMARY_CONTEXT_LENGTH);
} else if (pdfContent) {
// Extract readable text from the PDF content (it might be garbled but often contains some text)
const textMatch = pdfContent.match(/[\x20-\x7E\n\r]+/g);
if (textMatch) {
contextForSummary = textMatch.join(' ').slice(0, 4000);
contextForSummary = textMatch.join(' ').slice(0, MAX_SUMMARY_CONTEXT_LENGTH);
}
}

Expand All @@ -104,7 +105,7 @@ You can inquire about complex concepts, request explanations of technical detail

// Generate summary using OpenAI with whatever content we have
const completion = await openai.chat.completions.create({
model: 'gpt-3.5-turbo',
model: SUMMARY_MODEL,
messages: [
{
role: 'system',
Expand All @@ -115,7 +116,7 @@ You can inquire about complex concepts, request explanations of technical detail
content: `Extract the title and summarize this research paper content:\n\n${contextForSummary}`
}
],
max_tokens: 400,
max_tokens: SUMMARY_MAX_TOKENS,
temperature: 0.3,
});

Expand Down
47 changes: 19 additions & 28 deletions app/chat-new/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,27 +22,18 @@ import { useConfirm } from "@/lib/contexts/ConfirmContext";
import { useBreakpoint } from "@/lib/hooks/useMediaQuery";
import { MobileChatLayout } from "@/frontend/components/chat/MobileChatLayout";
import { PdfViewer } from "@/frontend/components/PdfViewer";
import { ChatMessage, ChatSession } from "@/lib/types/chat";
import {
CHAT_PANEL_DEFAULT_WIDTH,
CHAT_PANEL_MIN_WIDTH,
CHAT_PANEL_MAX_WIDTH,
SIDEBAR_EXPANDED_WIDTH,
SIDEBAR_COLLAPSED_WIDTH,
MESSAGE_TITLE_TRUNCATE_LENGTH,
SIGNED_URL_EXPIRY_SECONDS,
} from "@/lib/constants";

interface ChatSession {
id: string;
paper_id: string | null;
title: string;
created_at: string;
updated_at: string;
user_id: string;
paper?: {
title: string;
};
}

interface Message {
id: string;
content: string;
role: 'user' | 'assistant';
created_at: string;
session_id: string;
metadata?: any;
}
type Message = ChatMessage;

function ChatNewPageContent() {
const supabase = useSupabase();
Expand All @@ -64,7 +55,7 @@ function ChatNewPageContent() {
const [previewLoading, setPreviewLoading] = useState(false);
const [chatOpen, setChatOpen] = useState(false);
const [chatEverOpened, setChatEverOpened] = useState(false);
const [chatWidth, setChatWidth] = useState(550);
const [chatWidth, setChatWidth] = useState(CHAT_PANEL_DEFAULT_WIDTH);
const [isResizing, setIsResizing] = useState(false);
const [pdfBaseUrl, setPdfBaseUrl] = useState<string | null>(null);
const [processingStatus, setProcessingStatus] = useState<'pending' | 'processing' | 'completed' | 'failed' | null>(null);
Expand Down Expand Up @@ -258,13 +249,13 @@ function ChatNewPageContent() {

if (messagesError) throw messagesError;

const transformedMessages = (messagesData || []).map((msg, index) => ({
const transformedMessages: ChatMessage[] = (messagesData || []).map((msg, index) => ({
id: `${msg.id}-${index}`,
content: msg.content,
role: msg.role as 'user' | 'assistant',
created_at: msg.created_at,
session_id: msg.session_id,
metadata: msg.metadata
metadata: msg.metadata as ChatMessage['metadata']
}));

// Cache the messages
Expand Down Expand Up @@ -318,7 +309,7 @@ function ChatNewPageContent() {

try {
if (messages.length === 0) {
const newTitle = messageContent.slice(0, 50) + (messageContent.length > 50 ? '...' : '');
const newTitle = messageContent.slice(0, MESSAGE_TITLE_TRUNCATE_LENGTH) + (messageContent.length > MESSAGE_TITLE_TRUNCATE_LENGTH ? '...' : '');
await supabase
.from('chat_sessions')
.update({
Expand Down Expand Up @@ -516,7 +507,7 @@ function ChatNewPageContent() {
if (updatedPaper.storage_path) {
const { data: signed } = await supabase.storage
.from('papers')
.createSignedUrl(updatedPaper.storage_path, 60 * 60);
.createSignedUrl(updatedPaper.storage_path, SIGNED_URL_EXPIRY_SECONDS);
if (signed?.signedUrl) {
setPdfBaseUrl(signed.signedUrl);
}
Expand Down Expand Up @@ -556,7 +547,7 @@ function ChatNewPageContent() {

const { data: signed, error: signedError } = await supabase.storage
.from('papers')
.createSignedUrl(paper.storage_path, 60 * 60);
.createSignedUrl(paper.storage_path, SIGNED_URL_EXPIRY_SECONDS);

if (signedError || !signed?.signedUrl) {
console.error('Signed URL error:', signedError);
Expand Down Expand Up @@ -661,10 +652,10 @@ function ChatNewPageContent() {
const handleMouseMove = (e: MouseEvent) => {
if (!isResizing) return;
e.preventDefault();
const sidebarWidth = sidebarOpen ? 256 : 56;
const sidebarWidth = sidebarOpen ? SIDEBAR_EXPANDED_WIDTH : SIDEBAR_COLLAPSED_WIDTH;
const newWidth = e.clientX - sidebarWidth;
requestAnimationFrame(() => {
setChatWidth(Math.min(Math.max(280, newWidth), 800));
setChatWidth(Math.min(Math.max(CHAT_PANEL_MIN_WIDTH, newWidth), CHAT_PANEL_MAX_WIDTH));
});
};

Expand Down
9 changes: 0 additions & 9 deletions app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ body {
-webkit-overflow-scrolling: touch;
}

/* Hide scrollbar on mobile for cleaner UI */
.scrollbar-hide::-webkit-scrollbar {
display: none;
}
.scrollbar-hide {
-ms-overflow-style: none;
scrollbar-width: none;
}

/* Hide scrollbar utility */
.scrollbar-hide {
-ms-overflow-style: none;
Expand Down
3 changes: 2 additions & 1 deletion app/papers/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
import { CollectionsSidebar, AddToCollectionModal, CollectionBadges } from "@/frontend/components/collections";
import { useCollections, useCollectionPapers, invalidateCollectionCaches } from "@/lib/hooks/useCollections";
import { CollectionWithCount } from "@/lib/types/database";
import { SIGNED_URL_EXPIRY_SECONDS } from "@/lib/constants";
import { formatDate } from "@/lib/utils/dateFormat";
import { renderPdfFirstPage } from "@/lib/utils/pdfPreview";

Expand Down Expand Up @@ -155,7 +156,7 @@ function PapersPageContent() {
if (paper.storage_path) {
const { data, error } = await supabase.storage
.from('papers')
.createSignedUrl(paper.storage_path, 60 * 60);
.createSignedUrl(paper.storage_path, SIGNED_URL_EXPIRY_SECONDS);
if (!error && data?.signedUrl) {
const img = await renderPdfFirstPage(data.signedUrl, 560);
if (img) {
Expand Down
14 changes: 2 additions & 12 deletions frontend/components/ExportChatButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,14 @@ import { useState } from "react";
import { Download, Loader2, FileText, File } from "lucide-react";
import { exportChatAsPDF, exportChatAsTXT } from "@/lib/utils/export-chat";
import { useAlert } from "@/lib/contexts/AlertContext";
import { ChatMessage } from "@/lib/types/chat";

interface ExportChatButtonProps {
sessionId: string;
sessionTitle: string;
paperTitle?: string;
sessionDate: string;
messages: Array<{
id: string;
content: string;
role: 'user' | 'assistant';
created_at: string;
metadata?: {
citations?: Array<{
page_no: number;
score: number;
}>;
};
}>;
messages: ChatMessage[];
}

export function ExportChatButton({
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/NextReadList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
hasPreview
} from "@/lib/utils/previewCache";
import { useCollections, useCollectionPapers, invalidateCollectionCaches } from "@/lib/hooks/useCollections";
import { NEXT_READ_COLLECTION_NAME } from "@/lib/constants";
import { NEXT_READ_COLLECTION_NAME, SIGNED_URL_EXPIRY_SECONDS } from "@/lib/constants";

export function NextReadList() {
const supabase = useSupabase();
Expand Down Expand Up @@ -91,7 +91,7 @@ export function NextReadList() {
if (paper.storage_path) {
const signed = await supabase.storage
.from('papers')
.createSignedUrl(paper.storage_path, 60 * 60);
.createSignedUrl(paper.storage_path, SIGNED_URL_EXPIRY_SECONDS);

const pdfUrl = signed.data?.signedUrl;
if (pdfUrl) {
Expand Down
4 changes: 2 additions & 2 deletions frontend/components/RecentPapers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import {
clearPreview
} from "@/lib/utils/previewCache";
import { useCollections, useCollectionPapers } from "@/lib/hooks/useCollections";
import { SYSTEM_COLLECTION_NAME } from "@/lib/constants";
import { SYSTEM_COLLECTION_NAME, SIGNED_URL_EXPIRY_SECONDS } from "@/lib/constants";
import { formatDate } from "@/lib/utils/dateFormat";
import { renderPdfFirstPage } from "@/lib/utils/pdfPreview";

Expand Down Expand Up @@ -110,7 +110,7 @@ export function RecentPapers() {
if (paper.storage_path) {
const signed = await supabase.storage
.from('papers')
.createSignedUrl(paper.storage_path, 60 * 60);
.createSignedUrl(paper.storage_path, SIGNED_URL_EXPIRY_SECONDS);

const pdfUrl = signed.data?.signedUrl;

Expand Down
27 changes: 2 additions & 25 deletions frontend/components/chat/MobileChatLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,13 @@ import { useState, useCallback, useRef, useEffect } from "react";
import { MobileChatView } from "./MobileChatView";
import { MobilePdfView } from "./MobilePdfView";
import { MessageSquare, X, ChevronDown, Trash2, Plus, Search } from "lucide-react";

interface Message {
id: string;
content: string;
role: "user" | "assistant";
created_at: string;
session_id: string;
metadata?: {
is_loading?: boolean;
is_system_summary?: boolean;
};
}

interface ChatSession {
id: string;
paper_id: string | null;
title: string;
created_at: string;
updated_at: string;
user_id: string;
paper?: {
title: string;
};
}
import { ChatMessage, ChatSession } from "@/lib/types/chat";

interface MobileChatLayoutProps {
sessions: ChatSession[];
currentSession: ChatSession | null;
filterPaperId: string | null;
messages: Message[];
messages: ChatMessage[];
input: string;
onInputChange: (value: string) => void;
onSendMessage: () => void;
Expand Down
15 changes: 2 additions & 13 deletions frontend/components/chat/MobileChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,10 @@

import { useRef, useEffect } from "react";
import { Send, Loader2 } from "lucide-react";

interface Message {
id: string;
content: string;
role: "user" | "assistant";
created_at: string;
session_id: string;
metadata?: {
is_loading?: boolean;
is_system_summary?: boolean;
};
}
import { ChatMessage } from "@/lib/types/chat";

interface MobileChatViewProps {
messages: Message[];
messages: ChatMessage[];
input: string;
onInputChange: (value: string) => void;
onSendMessage: () => void;
Expand Down
Loading