mirror of
https://github.com/ItzCrazyKns/Perplexica.git
synced 2025-09-14 13:31:32 +00:00
Adding max record limt in settings
This commit is contained in:
@@ -6,7 +6,8 @@ export const GET = async (req: Request) => {
|
|||||||
try {
|
try {
|
||||||
// get header from request
|
// get header from request
|
||||||
const headers = await req.headers;
|
const headers = await req.headers;
|
||||||
let userSessionId = headers.get('user-session-id')?.toString() ?? '';
|
const userSessionId = headers.get('user-session-id')?.toString() ?? '';
|
||||||
|
const maxRecordLimit = parseInt(headers.get('max-record-limit') || '20', 10);
|
||||||
|
|
||||||
if (userSessionId == '') {
|
if (userSessionId == '') {
|
||||||
return Response.json({ chats: {} }, { status: 200 });
|
return Response.json({ chats: {} }, { status: 200 });
|
||||||
@@ -17,8 +18,7 @@ export const GET = async (req: Request) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
chatsRes = chatsRes.reverse();
|
chatsRes = chatsRes.reverse();
|
||||||
// Keep only the latest 20 records in the database. Delete older records.
|
// Keep only the latest records in the database. Delete older records.
|
||||||
let maxRecordLimit = 20;
|
|
||||||
if (chatsRes.length > maxRecordLimit) {
|
if (chatsRes.length > maxRecordLimit) {
|
||||||
const deleteChatsQuery = sql`DELETE FROM chats
|
const deleteChatsQuery = sql`DELETE FROM chats
|
||||||
WHERE userSessionId = ${userSessionId} AND (
|
WHERE userSessionId = ${userSessionId} AND (
|
||||||
|
@@ -28,11 +28,28 @@ const Page = () => {
|
|||||||
localStorage.setItem('userSessionId', userSessionId)
|
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!,
|
'user-session-id': userSessionId!,
|
||||||
|
'max-record-limit': maxRecordLimit,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@@ -23,6 +23,7 @@ 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> {
|
||||||
@@ -145,6 +146,7 @@ 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 () => {
|
||||||
@@ -207,6 +209,8 @@ const Page = () => {
|
|||||||
|
|
||||||
setSystemInstructions(localStorage.getItem('systemInstructions')!);
|
setSystemInstructions(localStorage.getItem('systemInstructions')!);
|
||||||
|
|
||||||
|
setMaxRecordLimit(localStorage.getItem('maxRecordLimit') || data.maxRecordLimit || '20');
|
||||||
|
|
||||||
setIsLoading(false);
|
setIsLoading(false);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -365,6 +369,15 @@ 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);
|
||||||
@@ -840,6 +853,37 @@ 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>
|
||||||
)
|
)
|
||||||
)}
|
)}
|
||||||
|
@@ -101,6 +101,12 @@ const checkConfig = async (
|
|||||||
localStorage.setItem('userSessionId', userSessionId!)
|
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
|
||||||
|
Reference in New Issue
Block a user