mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-05-02 09:12:37 +00:00
Compare commits
3 Commits
8b2f1b9c49
...
feat/azure
Author | SHA1 | Date | |
---|---|---|---|
|
da1123d84b | ||
|
627775c430 | ||
|
245573efca |
@ -29,7 +29,6 @@ type Message = {
|
|||||||
messageId: string;
|
messageId: string;
|
||||||
chatId: string;
|
chatId: string;
|
||||||
content: string;
|
content: string;
|
||||||
userSessionId: string;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
type ChatModel = {
|
type ChatModel = {
|
||||||
@ -139,7 +138,6 @@ const handleHistorySave = async (
|
|||||||
where: eq(chats.id, message.chatId),
|
where: eq(chats.id, message.chatId),
|
||||||
});
|
});
|
||||||
|
|
||||||
let currentDate = new Date();
|
|
||||||
if (!chat) {
|
if (!chat) {
|
||||||
await db
|
await db
|
||||||
.insert(chats)
|
.insert(chats)
|
||||||
@ -149,8 +147,6 @@ const handleHistorySave = async (
|
|||||||
createdAt: new Date().toString(),
|
createdAt: new Date().toString(),
|
||||||
focusMode: focusMode,
|
focusMode: focusMode,
|
||||||
files: files.map(getFileDetails),
|
files: files.map(getFileDetails),
|
||||||
userSessionId: message.userSessionId,
|
|
||||||
timestamp: currentDate.toISOString(),
|
|
||||||
})
|
})
|
||||||
.execute();
|
.execute();
|
||||||
}
|
}
|
||||||
|
@ -1,47 +1,10 @@
|
|||||||
import db from '@/lib/db';
|
import db from '@/lib/db';
|
||||||
import { chats } from '@/lib/db/schema';
|
|
||||||
import { eq, sql} from 'drizzle-orm';
|
|
||||||
|
|
||||||
export const GET = async (req: Request) => {
|
export const GET = async (req: Request) => {
|
||||||
try {
|
try {
|
||||||
// get header from request
|
let chats = await db.query.chats.findMany();
|
||||||
const headers = await req.headers;
|
chats = chats.reverse();
|
||||||
const userSessionId = headers.get('user-session-id')?.toString() ?? '';
|
return Response.json({ chats: chats }, { status: 200 });
|
||||||
const maxRecordLimit = parseInt(headers.get('max-record-limit') || '20', 10);
|
|
||||||
|
|
||||||
if (userSessionId == '') {
|
|
||||||
return Response.json({ chats: {} }, { status: 200 });
|
|
||||||
}
|
|
||||||
|
|
||||||
let chatsRes = await db.query.chats.findMany({
|
|
||||||
where: eq(chats.userSessionId, userSessionId),
|
|
||||||
});
|
|
||||||
|
|
||||||
chatsRes = chatsRes.reverse();
|
|
||||||
// Keep only the latest records in the database. Delete older records.
|
|
||||||
if (chatsRes.length > maxRecordLimit) {
|
|
||||||
const deleteChatsQuery = sql`DELETE FROM chats
|
|
||||||
WHERE userSessionId = ${userSessionId} AND (
|
|
||||||
timestamp IS NULL OR
|
|
||||||
timestamp NOT in (
|
|
||||||
SELECT timestamp FROM chats
|
|
||||||
WHERE userSessionId = ${userSessionId}
|
|
||||||
ORDER BY timestamp DESC
|
|
||||||
LIMIT ${maxRecordLimit}
|
|
||||||
)
|
|
||||||
)
|
|
||||||
`;
|
|
||||||
await db.run(deleteChatsQuery);
|
|
||||||
// Delete messages that no longer link with the chat from the database.
|
|
||||||
const deleteMessagesQuery = sql`DELETE FROM messages
|
|
||||||
WHERE chatId NOT IN (
|
|
||||||
SELECT id FROM chats
|
|
||||||
)
|
|
||||||
`;
|
|
||||||
await db.run(deleteMessagesQuery);
|
|
||||||
}
|
|
||||||
|
|
||||||
return Response.json({ chats: chatsRes }, { status: 200 });
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error in getting chats: ', err);
|
console.error('Error in getting chats: ', err);
|
||||||
return Response.json(
|
return Response.json(
|
||||||
|
@ -1,6 +1,5 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import crypto from 'crypto';
|
|
||||||
import DeleteChat from '@/components/DeleteChat';
|
import DeleteChat from '@/components/DeleteChat';
|
||||||
import { cn, formatTimeDifference } from '@/lib/utils';
|
import { cn, formatTimeDifference } from '@/lib/utils';
|
||||||
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
|
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
|
||||||
@ -22,34 +21,10 @@ const Page = () => {
|
|||||||
const fetchChats = async () => {
|
const fetchChats = async () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
|
|
||||||
let userSessionId = localStorage.getItem('userSessionId');
|
|
||||||
if (!userSessionId) {
|
|
||||||
userSessionId = crypto.randomBytes(20).toString('hex');
|
|
||||||
localStorage.setItem('userSessionId', userSessionId)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get maxRecordLimit from localStorage or set default
|
|
||||||
let maxRecordLimit = localStorage.getItem('maxRecordLimit');
|
|
||||||
if (!maxRecordLimit) {
|
|
||||||
maxRecordLimit = '20';
|
|
||||||
localStorage.setItem('maxRecordLimit', maxRecordLimit);
|
|
||||||
} else {
|
|
||||||
let valueInt = parseInt(maxRecordLimit, 10) || 20;
|
|
||||||
if (valueInt < 1) {
|
|
||||||
valueInt = 1;
|
|
||||||
} else if (valueInt > 100) {
|
|
||||||
valueInt = 100;
|
|
||||||
}
|
|
||||||
maxRecordLimit = valueInt.toString();
|
|
||||||
localStorage.setItem('maxRecordLimit', maxRecordLimit);
|
|
||||||
}
|
|
||||||
|
|
||||||
const res = await fetch(`/api/chats`, {
|
const res = await fetch(`/api/chats`, {
|
||||||
method: 'GET',
|
method: 'GET',
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
'user-session-id': userSessionId!,
|
|
||||||
'max-record-limit': maxRecordLimit,
|
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -24,7 +24,6 @@ interface SettingsType {
|
|||||||
customOpenaiApiKey: string;
|
customOpenaiApiKey: string;
|
||||||
customOpenaiApiUrl: string;
|
customOpenaiApiUrl: string;
|
||||||
customOpenaiModelName: string;
|
customOpenaiModelName: string;
|
||||||
maxRecordLimit: string;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
||||||
@ -147,7 +146,6 @@ const Page = () => {
|
|||||||
const [automaticVideoSearch, setAutomaticVideoSearch] = useState(false);
|
const [automaticVideoSearch, setAutomaticVideoSearch] = useState(false);
|
||||||
const [systemInstructions, setSystemInstructions] = useState<string>('');
|
const [systemInstructions, setSystemInstructions] = useState<string>('');
|
||||||
const [savingStates, setSavingStates] = useState<Record<string, boolean>>({});
|
const [savingStates, setSavingStates] = useState<Record<string, boolean>>({});
|
||||||
const [maxRecordLimit, setMaxRecordLimit] = useState<string>('20');
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchConfig = async () => {
|
const fetchConfig = async () => {
|
||||||
@ -210,8 +208,6 @@ const Page = () => {
|
|||||||
|
|
||||||
setSystemInstructions(localStorage.getItem('systemInstructions')!);
|
setSystemInstructions(localStorage.getItem('systemInstructions')!);
|
||||||
|
|
||||||
setMaxRecordLimit(localStorage.getItem('maxRecordLimit') || data.maxRecordLimit || '20');
|
|
||||||
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -370,15 +366,6 @@ const Page = () => {
|
|||||||
localStorage.setItem('embeddingModel', value);
|
localStorage.setItem('embeddingModel', value);
|
||||||
} else if (key === 'systemInstructions') {
|
} else if (key === 'systemInstructions') {
|
||||||
localStorage.setItem('systemInstructions', value);
|
localStorage.setItem('systemInstructions', value);
|
||||||
} else if (key === 'maxRecordLimit') {
|
|
||||||
let valueInt = parseInt(value, 10) || 20;
|
|
||||||
if (valueInt < 1) {
|
|
||||||
valueInt = 1;
|
|
||||||
} else if (valueInt > 100) {
|
|
||||||
valueInt = 100;
|
|
||||||
}
|
|
||||||
setMaxRecordLimit(valueInt.toString());
|
|
||||||
localStorage.setItem('maxRecordLimit', valueInt.toString());
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Failed to save:', err);
|
console.error('Failed to save:', err);
|
||||||
@ -873,37 +860,6 @@ const Page = () => {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</SettingsSection>
|
</SettingsSection>
|
||||||
|
|
||||||
<SettingsSection title="Chat History">
|
|
||||||
<div className="flex flex-col space-y-4">
|
|
||||||
<div className="flex flex-col space-y-1">
|
|
||||||
<p className="text-black/70 dark:text-white/70 text-sm">
|
|
||||||
Maximum Chat History Records
|
|
||||||
</p>
|
|
||||||
<div className="flex items-center space-x-2">
|
|
||||||
<Input
|
|
||||||
type="number"
|
|
||||||
min="1"
|
|
||||||
max="100"
|
|
||||||
pattern="[0-9]*"
|
|
||||||
inputMode="numeric"
|
|
||||||
value={maxRecordLimit}
|
|
||||||
isSaving={savingStates['maxRecordLimit']}
|
|
||||||
onChange={(e) => {
|
|
||||||
setMaxRecordLimit(e.target.value);
|
|
||||||
}}
|
|
||||||
onSave={(value) => saveConfig('maxRecordLimit', value)}
|
|
||||||
/>
|
|
||||||
<span className="text-black/60 dark:text-white/60 text-sm">
|
|
||||||
records
|
|
||||||
</span>
|
|
||||||
</div>
|
|
||||||
<p className="text-xs text-black/60 dark:text-white/60 mt-1">
|
|
||||||
Maximum number of chat records to keep in history. Older records will be automatically deleted.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</SettingsSection>
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
)}
|
)}
|
||||||
|
@ -95,18 +95,6 @@ const checkConfig = async (
|
|||||||
if (!embeddingModel || !embeddingModelProvider) {
|
if (!embeddingModel || !embeddingModelProvider) {
|
||||||
const embeddingModelProviders = providers.embeddingModelProviders;
|
const embeddingModelProviders = providers.embeddingModelProviders;
|
||||||
|
|
||||||
let userSessionId = localStorage.getItem('userSessionId');
|
|
||||||
if (!userSessionId) {
|
|
||||||
userSessionId = crypto.randomBytes(20).toString('hex');
|
|
||||||
localStorage.setItem('userSessionId', userSessionId!)
|
|
||||||
}
|
|
||||||
|
|
||||||
let maxRecordLimit = localStorage.getItem('maxRecordLimit');
|
|
||||||
if (!maxRecordLimit) {
|
|
||||||
maxRecordLimit = '20';
|
|
||||||
localStorage.setItem('maxRecordLimit', maxRecordLimit);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!embeddingModelProviders ||
|
!embeddingModelProviders ||
|
||||||
Object.keys(embeddingModelProviders).length === 0
|
Object.keys(embeddingModelProviders).length === 0
|
||||||
@ -354,7 +342,6 @@ const ChatWindow = ({ id }: { id?: string }) => {
|
|||||||
let added = false;
|
let added = false;
|
||||||
|
|
||||||
messageId = messageId ?? crypto.randomBytes(7).toString('hex');
|
messageId = messageId ?? crypto.randomBytes(7).toString('hex');
|
||||||
let userSessionId = localStorage.getItem('userSessionId');
|
|
||||||
|
|
||||||
setMessages((prevMessages) => [
|
setMessages((prevMessages) => [
|
||||||
...prevMessages,
|
...prevMessages,
|
||||||
@ -479,7 +466,6 @@ const ChatWindow = ({ id }: { id?: string }) => {
|
|||||||
messageId: messageId,
|
messageId: messageId,
|
||||||
chatId: chatId!,
|
chatId: chatId!,
|
||||||
content: message,
|
content: message,
|
||||||
userSessionId: userSessionId,
|
|
||||||
},
|
},
|
||||||
chatId: chatId!,
|
chatId: chatId!,
|
||||||
files: fileIds,
|
files: fileIds,
|
||||||
|
@ -25,6 +25,4 @@ export const chats = sqliteTable('chats', {
|
|||||||
files: text('files', { mode: 'json' })
|
files: text('files', { mode: 'json' })
|
||||||
.$type<File[]>()
|
.$type<File[]>()
|
||||||
.default(sql`'[]'`),
|
.default(sql`'[]'`),
|
||||||
userSessionId: text('userSessionId'),
|
|
||||||
timestamp: text('timestamp'),
|
|
||||||
});
|
});
|
||||||
|
@ -72,6 +72,14 @@ const groqChatModels: Record<string, string>[] = [
|
|||||||
displayName: 'Llama 3.2 90B Vision Preview (Preview)',
|
displayName: 'Llama 3.2 90B Vision Preview (Preview)',
|
||||||
key: 'llama-3.2-90b-vision-preview',
|
key: 'llama-3.2-90b-vision-preview',
|
||||||
},
|
},
|
||||||
|
/* {
|
||||||
|
displayName: 'Llama 4 Maverick 17B 128E Instruct (Preview)',
|
||||||
|
key: 'meta-llama/llama-4-maverick-17b-128e-instruct',
|
||||||
|
}, */
|
||||||
|
{
|
||||||
|
displayName: 'Llama 4 Scout 17B 16E Instruct (Preview)',
|
||||||
|
key: 'meta-llama/llama-4-scout-17b-16e-instruct',
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
export const loadGroqChatModels = async () => {
|
export const loadGroqChatModels = async () => {
|
||||||
|
Reference in New Issue
Block a user