mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2026-01-10 13:25:43 +00:00
Compare commits
5 Commits
f1c9fa0e33
...
0987ee4370
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
0987ee4370 | ||
|
|
d1bd22786d | ||
|
|
bb7b7170ca | ||
|
|
be7bd62a74 | ||
|
|
a691f3bab0 |
@@ -11,6 +11,13 @@ const nextConfig = {
|
|||||||
],
|
],
|
||||||
},
|
},
|
||||||
serverExternalPackages: ['pdf-parse'],
|
serverExternalPackages: ['pdf-parse'],
|
||||||
|
outputFileTracingIncludes: {
|
||||||
|
'/api/**': [
|
||||||
|
'./node_modules/@napi-rs/canvas/**',
|
||||||
|
'./node_modules/@napi-rs/canvas-linux-x64-gnu/**',
|
||||||
|
'./node_modules/@napi-rs/canvas-linux-x64-musl/**',
|
||||||
|
],
|
||||||
|
},
|
||||||
env: {
|
env: {
|
||||||
NEXT_PUBLIC_VERSION: pkg.version,
|
NEXT_PUBLIC_VERSION: pkg.version,
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -67,7 +67,9 @@
|
|||||||
"postcss": "^8",
|
"postcss": "^8",
|
||||||
"prettier": "^3.2.5",
|
"prettier": "^3.2.5",
|
||||||
"tailwindcss": "^3.3.0",
|
"tailwindcss": "^3.3.0",
|
||||||
"typescript": "^5.9.3",
|
"typescript": "^5.9.3"
|
||||||
|
},
|
||||||
|
"optionalDependencies": {
|
||||||
"@napi-rs/canvas": "^0.1.87"
|
"@napi-rs/canvas": "^0.1.87"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ export const POST = async (req: Request) => {
|
|||||||
|
|
||||||
const images = await searchImages(
|
const images = await searchImages(
|
||||||
{
|
{
|
||||||
chatHistory: body.chatHistory,
|
chatHistory: body.chatHistory.map(([role, content]) => ({
|
||||||
|
role: role === 'human' ? 'user' : 'assistant',
|
||||||
|
content,
|
||||||
|
})),
|
||||||
query: body.query,
|
query: body.query,
|
||||||
},
|
},
|
||||||
llm,
|
llm,
|
||||||
|
|||||||
@@ -20,7 +20,10 @@ export const POST = async (req: Request) => {
|
|||||||
|
|
||||||
const suggestions = await generateSuggestions(
|
const suggestions = await generateSuggestions(
|
||||||
{
|
{
|
||||||
chatHistory: body.chatHistory,
|
chatHistory: body.chatHistory.map(([role, content]) => ({
|
||||||
|
role: role === 'human' ? 'user' : 'assistant',
|
||||||
|
content,
|
||||||
|
})),
|
||||||
},
|
},
|
||||||
llm,
|
llm,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -21,7 +21,10 @@ export const POST = async (req: Request) => {
|
|||||||
|
|
||||||
const videos = await handleVideoSearch(
|
const videos = await handleVideoSearch(
|
||||||
{
|
{
|
||||||
chatHistory: body.chatHistory,
|
chatHistory: body.chatHistory.map(([role, content]) => ({
|
||||||
|
role: role === 'human' ? 'user' : 'assistant',
|
||||||
|
content,
|
||||||
|
})),
|
||||||
query: body.query,
|
query: body.query,
|
||||||
},
|
},
|
||||||
llm,
|
llm,
|
||||||
|
|||||||
@@ -50,7 +50,14 @@ const MessageBox = ({
|
|||||||
dividerRef?: MutableRefObject<HTMLDivElement | null>;
|
dividerRef?: MutableRefObject<HTMLDivElement | null>;
|
||||||
isLast: boolean;
|
isLast: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const { loading, sendMessage, rewrite, messages, researchEnded } = useChat();
|
const {
|
||||||
|
loading,
|
||||||
|
sendMessage,
|
||||||
|
rewrite,
|
||||||
|
messages,
|
||||||
|
researchEnded,
|
||||||
|
chatHistory,
|
||||||
|
} = useChat();
|
||||||
|
|
||||||
const parsedMessage = section.parsedTextBlocks.join('\n\n');
|
const parsedMessage = section.parsedTextBlocks.join('\n\n');
|
||||||
const speechMessage = section.speechMessage || '';
|
const speechMessage = section.speechMessage || '';
|
||||||
@@ -265,11 +272,11 @@ const MessageBox = ({
|
|||||||
<div className="lg:sticky lg:top-20 flex flex-col items-center space-y-3 w-full lg:w-3/12 z-30 h-full pb-4">
|
<div className="lg:sticky lg:top-20 flex flex-col items-center space-y-3 w-full lg:w-3/12 z-30 h-full pb-4">
|
||||||
<SearchImages
|
<SearchImages
|
||||||
query={section.message.query}
|
query={section.message.query}
|
||||||
chatHistory={messages}
|
chatHistory={chatHistory}
|
||||||
messageId={section.message.messageId}
|
messageId={section.message.messageId}
|
||||||
/>
|
/>
|
||||||
<SearchVideos
|
<SearchVideos
|
||||||
chatHistory={messages}
|
chatHistory={chatHistory}
|
||||||
query={section.message.query}
|
query={section.message.query}
|
||||||
messageId={section.message.messageId}
|
messageId={section.message.messageId}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@@ -17,7 +17,7 @@ const SearchImages = ({
|
|||||||
messageId,
|
messageId,
|
||||||
}: {
|
}: {
|
||||||
query: string;
|
query: string;
|
||||||
chatHistory: Message[];
|
chatHistory: [string, string][];
|
||||||
messageId: string;
|
messageId: string;
|
||||||
}) => {
|
}) => {
|
||||||
const [images, setImages] = useState<Image[] | null>(null);
|
const [images, setImages] = useState<Image[] | null>(null);
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ const Searchvideos = ({
|
|||||||
messageId,
|
messageId,
|
||||||
}: {
|
}: {
|
||||||
query: string;
|
query: string;
|
||||||
chatHistory: Message[];
|
chatHistory: [string, string][];
|
||||||
messageId: string;
|
messageId: string;
|
||||||
}) => {
|
}) => {
|
||||||
const [videos, setVideos] = useState<Video[] | null>(null);
|
const [videos, setVideos] = useState<Video[] | null>(null);
|
||||||
|
|||||||
@@ -175,7 +175,7 @@ const loadMessages = async (
|
|||||||
chatId: string,
|
chatId: string,
|
||||||
setMessages: (messages: Message[]) => void,
|
setMessages: (messages: Message[]) => void,
|
||||||
setIsMessagesLoaded: (loaded: boolean) => void,
|
setIsMessagesLoaded: (loaded: boolean) => void,
|
||||||
setChatHistory: (history: [string, string][]) => void,
|
chatHistory: React.MutableRefObject<[string, string][]>,
|
||||||
setSources: (sources: string[]) => void,
|
setSources: (sources: string[]) => void,
|
||||||
setNotFound: (notFound: boolean) => void,
|
setNotFound: (notFound: boolean) => void,
|
||||||
setFiles: (files: File[]) => void,
|
setFiles: (files: File[]) => void,
|
||||||
@@ -233,7 +233,7 @@ const loadMessages = async (
|
|||||||
setFiles(files);
|
setFiles(files);
|
||||||
setFileIds(files.map((file: File) => file.fileId));
|
setFileIds(files.map((file: File) => file.fileId));
|
||||||
|
|
||||||
setChatHistory(history);
|
chatHistory.current = history;
|
||||||
setSources(data.chat.sources);
|
setSources(data.chat.sources);
|
||||||
setIsMessagesLoaded(true);
|
setIsMessagesLoaded(true);
|
||||||
};
|
};
|
||||||
@@ -281,7 +281,7 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
|
|
||||||
const [researchEnded, setResearchEnded] = useState(false);
|
const [researchEnded, setResearchEnded] = useState(false);
|
||||||
|
|
||||||
const [chatHistory, setChatHistory] = useState<[string, string][]>([]);
|
const chatHistory = useRef<[string, string][]>([]);
|
||||||
const [messages, setMessages] = useState<Message[]>([]);
|
const [messages, setMessages] = useState<Message[]>([]);
|
||||||
|
|
||||||
const [files, setFiles] = useState<File[]>([]);
|
const [files, setFiles] = useState<File[]>([]);
|
||||||
@@ -402,7 +402,12 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
});
|
});
|
||||||
}, [messages]);
|
}, [messages]);
|
||||||
|
|
||||||
|
const isReconnectingRef = useRef(false);
|
||||||
|
const handledMessageEndRef = useRef<Set<string>>(new Set());
|
||||||
|
|
||||||
const checkReconnect = async () => {
|
const checkReconnect = async () => {
|
||||||
|
if (isReconnectingRef.current) return;
|
||||||
|
|
||||||
setIsReady(true);
|
setIsReady(true);
|
||||||
console.debug(new Date(), 'app:ready');
|
console.debug(new Date(), 'app:ready');
|
||||||
|
|
||||||
@@ -414,6 +419,8 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
setResearchEnded(false);
|
setResearchEnded(false);
|
||||||
setMessageAppeared(false);
|
setMessageAppeared(false);
|
||||||
|
|
||||||
|
isReconnectingRef.current = true;
|
||||||
|
|
||||||
const res = await fetch(`/api/reconnect/${lastMsg.backendId}`, {
|
const res = await fetch(`/api/reconnect/${lastMsg.backendId}`, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
});
|
});
|
||||||
@@ -427,23 +434,27 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
|
|
||||||
const messageHandler = getMessageHandler(lastMsg);
|
const messageHandler = getMessageHandler(lastMsg);
|
||||||
|
|
||||||
while (true) {
|
try {
|
||||||
const { value, done } = await reader.read();
|
while (true) {
|
||||||
if (done) break;
|
const { value, done } = await reader.read();
|
||||||
|
if (done) break;
|
||||||
|
|
||||||
partialChunk += decoder.decode(value, { stream: true });
|
partialChunk += decoder.decode(value, { stream: true });
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const messages = partialChunk.split('\n');
|
const messages = partialChunk.split('\n');
|
||||||
for (const msg of messages) {
|
for (const msg of messages) {
|
||||||
if (!msg.trim()) continue;
|
if (!msg.trim()) continue;
|
||||||
const json = JSON.parse(msg);
|
const json = JSON.parse(msg);
|
||||||
messageHandler(json);
|
messageHandler(json);
|
||||||
|
}
|
||||||
|
partialChunk = '';
|
||||||
|
} catch (error) {
|
||||||
|
console.warn('Incomplete JSON, waiting for next chunk...');
|
||||||
}
|
}
|
||||||
partialChunk = '';
|
|
||||||
} catch (error) {
|
|
||||||
console.warn('Incomplete JSON, waiting for next chunk...');
|
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
isReconnectingRef.current = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -463,7 +474,7 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
if (params.chatId && params.chatId !== chatId) {
|
if (params.chatId && params.chatId !== chatId) {
|
||||||
setChatId(params.chatId);
|
setChatId(params.chatId);
|
||||||
setMessages([]);
|
setMessages([]);
|
||||||
setChatHistory([]);
|
chatHistory.current = [];
|
||||||
setFiles([]);
|
setFiles([]);
|
||||||
setFileIds([]);
|
setFileIds([]);
|
||||||
setIsMessagesLoaded(false);
|
setIsMessagesLoaded(false);
|
||||||
@@ -483,7 +494,7 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
chatId,
|
chatId,
|
||||||
setMessages,
|
setMessages,
|
||||||
setIsMessagesLoaded,
|
setIsMessagesLoaded,
|
||||||
setChatHistory,
|
chatHistory,
|
||||||
setSources,
|
setSources,
|
||||||
setNotFound,
|
setNotFound,
|
||||||
setFiles,
|
setFiles,
|
||||||
@@ -519,9 +530,7 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
|
|
||||||
setMessages((prev) => prev.slice(0, index));
|
setMessages((prev) => prev.slice(0, index));
|
||||||
|
|
||||||
setChatHistory((prev) => {
|
chatHistory.current = chatHistory.current.slice(0, index * 2);
|
||||||
return prev.slice(0, index * 2);
|
|
||||||
});
|
|
||||||
|
|
||||||
const messageToRewrite = messages[index];
|
const messageToRewrite = messages[index];
|
||||||
sendMessage(messageToRewrite.query, messageToRewrite.messageId, true);
|
sendMessage(messageToRewrite.query, messageToRewrite.messageId, true);
|
||||||
@@ -621,12 +630,18 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (data.type === 'messageEnd') {
|
if (data.type === 'messageEnd') {
|
||||||
|
if (handledMessageEndRef.current.has(messageId)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
handledMessageEndRef.current.add(messageId);
|
||||||
|
|
||||||
const currentMsg = messagesRef.current.find(
|
const currentMsg = messagesRef.current.find(
|
||||||
(msg) => msg.messageId === messageId,
|
(msg) => msg.messageId === messageId,
|
||||||
);
|
);
|
||||||
|
|
||||||
const newHistory: [string, string][] = [
|
const newHistory: [string, string][] = [
|
||||||
...chatHistory,
|
...chatHistory.current,
|
||||||
['human', message.query],
|
['human', message.query],
|
||||||
[
|
[
|
||||||
'assistant',
|
'assistant',
|
||||||
@@ -635,7 +650,7 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
setChatHistory(newHistory);
|
chatHistory.current = newHistory;
|
||||||
|
|
||||||
setMessages((prev) =>
|
setMessages((prev) =>
|
||||||
prev.map((msg) =>
|
prev.map((msg) =>
|
||||||
@@ -652,13 +667,15 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
const autoMediaSearch = getAutoMediaSearch();
|
const autoMediaSearch = getAutoMediaSearch();
|
||||||
|
|
||||||
if (autoMediaSearch) {
|
if (autoMediaSearch) {
|
||||||
document
|
setTimeout(() => {
|
||||||
.getElementById(`search-images-${lastMsg.messageId}`)
|
document
|
||||||
?.click();
|
.getElementById(`search-images-${lastMsg.messageId}`)
|
||||||
|
?.click();
|
||||||
|
|
||||||
document
|
document
|
||||||
.getElementById(`search-videos-${lastMsg.messageId}`)
|
.getElementById(`search-videos-${lastMsg.messageId}`)
|
||||||
?.click();
|
?.click();
|
||||||
|
}, 200);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if there are sources and no suggestions
|
// Check if there are sources and no suggestions
|
||||||
@@ -742,8 +759,11 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
sources: sources,
|
sources: sources,
|
||||||
optimizationMode: optimizationMode,
|
optimizationMode: optimizationMode,
|
||||||
history: rewrite
|
history: rewrite
|
||||||
? chatHistory.slice(0, messageIndex === -1 ? undefined : messageIndex)
|
? chatHistory.current.slice(
|
||||||
: chatHistory,
|
0,
|
||||||
|
messageIndex === -1 ? undefined : messageIndex,
|
||||||
|
)
|
||||||
|
: chatHistory.current,
|
||||||
chatModel: {
|
chatModel: {
|
||||||
key: chatModelProvider.key,
|
key: chatModelProvider.key,
|
||||||
providerId: chatModelProvider.providerId,
|
providerId: chatModelProvider.providerId,
|
||||||
@@ -790,7 +810,7 @@ export const ChatProvider = ({ children }: { children: React.ReactNode }) => {
|
|||||||
value={{
|
value={{
|
||||||
messages,
|
messages,
|
||||||
sections,
|
sections,
|
||||||
chatHistory,
|
chatHistory: chatHistory.current,
|
||||||
files,
|
files,
|
||||||
fileIds,
|
fileIds,
|
||||||
sources,
|
sources,
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { ChatTurnMessage } from '@/lib/types';
|
|||||||
export const imageSearchPrompt = `
|
export const imageSearchPrompt = `
|
||||||
You will be given a conversation below and a follow up question. You need to rephrase the follow-up question so it is a standalone question that can be used by the LLM to search the web for images.
|
You will be given a conversation below and a follow up question. You need to rephrase the follow-up question so it is a standalone question that can be used by the LLM to search the web for images.
|
||||||
You need to make sure the rephrased question agrees with the conversation and is relevant to the conversation.
|
You need to make sure the rephrased question agrees with the conversation and is relevant to the conversation.
|
||||||
|
Make sure to make the querey standalone and not something very broad, use context from the answers in the conversation to make it specific so user can get best image search results.
|
||||||
Output only the rephrased query in query key JSON format. Do not include any explanation or additional text.
|
Output only the rephrased query in query key JSON format. Do not include any explanation or additional text.
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ import { ChatTurnMessage } from '@/lib/types';
|
|||||||
export const videoSearchPrompt = `
|
export const videoSearchPrompt = `
|
||||||
You will be given a conversation below and a follow up question. You need to rephrase the follow-up question so it is a standalone question that can be used by the LLM to search Youtube for videos.
|
You will be given a conversation below and a follow up question. You need to rephrase the follow-up question so it is a standalone question that can be used by the LLM to search Youtube for videos.
|
||||||
You need to make sure the rephrased question agrees with the conversation and is relevant to the conversation.
|
You need to make sure the rephrased question agrees with the conversation and is relevant to the conversation.
|
||||||
|
Make sure to make the querey standalone and not something very broad, use context from the answers in the conversation to make it specific so user can get best video search results.
|
||||||
Output only the rephrased query in query key JSON format. Do not include any explanation or additional text.
|
Output only the rephrased query in query key JSON format. Do not include any explanation or additional text.
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user