feat(app): add password auth for settings
This commit is contained in:
parent
c4932c659a
commit
fdfe8d1f41
7 changed files with 152 additions and 41 deletions
|
@ -1,6 +1,10 @@
|
|||
[GENERAL]
|
||||
PORT = 3001 # Port to run the server on
|
||||
SIMILARITY_MEASURE = "cosine" # "cosine" or "dot"
|
||||
CONFIG_PASSWORD = "lorem_ipsum" # Password to access config
|
||||
DISCOVER_ENABLED = true
|
||||
LIBRARY_ENABLED = true
|
||||
COPILOT_ENABLED = true
|
||||
|
||||
[API_KEYS]
|
||||
OPENAI = "" # OpenAI API key - sk-1234567890abcdef1234567890abcdef
|
||||
|
|
|
@ -8,6 +8,10 @@ interface Config {
|
|||
GENERAL: {
|
||||
PORT: number;
|
||||
SIMILARITY_MEASURE: string;
|
||||
CONFIG_PASSWORD: string;
|
||||
DISCOVER_ENABLED: boolean;
|
||||
LIBRARY_ENABLED: boolean;
|
||||
COPILOT_ENABLED: boolean;
|
||||
};
|
||||
API_KEYS: {
|
||||
OPENAI: string;
|
||||
|
@ -34,6 +38,14 @@ export const getPort = () => loadConfig().GENERAL.PORT;
|
|||
export const getSimilarityMeasure = () =>
|
||||
loadConfig().GENERAL.SIMILARITY_MEASURE;
|
||||
|
||||
export const getConfigPassword = () => loadConfig().GENERAL.CONFIG_PASSWORD;
|
||||
|
||||
export const isDiscoverEnabled = () => loadConfig().GENERAL.DISCOVER_ENABLED;
|
||||
|
||||
export const isLibraryEnabled = () => loadConfig().GENERAL.LIBRARY_ENABLED;
|
||||
|
||||
export const isCopilotEnabled = () => loadConfig().GENERAL.COPILOT_ENABLED;
|
||||
|
||||
export const getOpenaiApiKey = () => loadConfig().API_KEYS.OPENAI;
|
||||
|
||||
export const getGroqApiKey = () => loadConfig().API_KEYS.GROQ;
|
||||
|
@ -53,6 +65,7 @@ export const updateConfig = (config: RecursivePartial<Config>) => {
|
|||
if (typeof currentConfig[key] === 'object' && currentConfig[key] !== null) {
|
||||
for (const nestedKey in currentConfig[key]) {
|
||||
if (
|
||||
typeof config[key][nestedKey] !== 'boolean' &&
|
||||
!config[key][nestedKey] &&
|
||||
currentConfig[key][nestedKey] &&
|
||||
config[key][nestedKey] !== ''
|
||||
|
|
|
@ -9,11 +9,23 @@ import {
|
|||
getAnthropicApiKey,
|
||||
getOpenaiApiKey,
|
||||
updateConfig,
|
||||
getConfigPassword,
|
||||
isLibraryEnabled,
|
||||
isCopilotEnabled,
|
||||
isDiscoverEnabled,
|
||||
} from '../config';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.get('/', async (_, res) => {
|
||||
router.get('/', async (req, res) => {
|
||||
const authHeader = req.headers['authorization']?.split(' ')[1];
|
||||
const password = getConfigPassword();
|
||||
|
||||
if (authHeader !== password) {
|
||||
res.status(401).json({ message: 'Unauthorized' });
|
||||
return;
|
||||
}
|
||||
|
||||
const config = {};
|
||||
|
||||
const [chatModelProviders, embeddingModelProviders] = await Promise.all([
|
||||
|
@ -40,14 +52,30 @@ router.get('/', async (_, res) => {
|
|||
config['ollamaApiUrl'] = getOllamaApiEndpoint();
|
||||
config['anthropicApiKey'] = getAnthropicApiKey();
|
||||
config['groqApiKey'] = getGroqApiKey();
|
||||
config['isLibraryEnabled'] = isLibraryEnabled();
|
||||
config['isCopilotEnabled'] = isCopilotEnabled();
|
||||
config['isDiscoverEnabled'] = isDiscoverEnabled();
|
||||
|
||||
res.status(200).json(config);
|
||||
});
|
||||
|
||||
router.post('/', async (req, res) => {
|
||||
const authHeader = req.headers['authorization']?.split(' ')[1];
|
||||
const password = getConfigPassword();
|
||||
|
||||
if (authHeader !== password) {
|
||||
res.status(401).json({ message: 'Unauthorized' });
|
||||
return;
|
||||
}
|
||||
|
||||
const config = req.body;
|
||||
|
||||
const updatedConfig = {
|
||||
GENERAL: {
|
||||
DISCOVER_ENABLED: config.isDiscoverEnabled,
|
||||
LIBRARY_ENABLED: config.isLibraryEnabled,
|
||||
COPILOT_ENABLED: config.isCopilotEnabled,
|
||||
},
|
||||
API_KEYS: {
|
||||
OPENAI: config.openaiApiKey,
|
||||
GROQ: config.groqApiKey,
|
||||
|
@ -63,4 +91,14 @@ router.post('/', async (req, res) => {
|
|||
res.status(200).json({ message: 'Config updated' });
|
||||
});
|
||||
|
||||
router.get('/preferences', (_, res) => {
|
||||
const preferences = {
|
||||
isLibraryEnabled: isLibraryEnabled(),
|
||||
isCopilotEnabled: isCopilotEnabled(),
|
||||
isDiscoverEnabled: isDiscoverEnabled(),
|
||||
};
|
||||
|
||||
res.status(200).json(preferences);
|
||||
});
|
||||
|
||||
export default router;
|
||||
|
|
|
@ -9,10 +9,33 @@ const router = express.Router();
|
|||
|
||||
router.get('/', async (req, res) => {
|
||||
try {
|
||||
const [chatModelProviders, embeddingModelProviders] = await Promise.all([
|
||||
getAvailableChatModelProviders(),
|
||||
getAvailableEmbeddingModelProviders(),
|
||||
]);
|
||||
const [chatModelProvidersRaw, embeddingModelProvidersRaw] =
|
||||
await Promise.all([
|
||||
getAvailableChatModelProviders(),
|
||||
getAvailableEmbeddingModelProviders(),
|
||||
]);
|
||||
|
||||
const chatModelProviders = {};
|
||||
|
||||
const chatModelProvidersKeys = Object.keys(chatModelProvidersRaw);
|
||||
chatModelProvidersKeys.forEach((provider) => {
|
||||
chatModelProviders[provider] = {};
|
||||
const models = Object.keys(chatModelProvidersRaw[provider]);
|
||||
models.forEach((model) => {
|
||||
chatModelProviders[provider][model] = {};
|
||||
});
|
||||
});
|
||||
|
||||
const embeddingModelProviders = {};
|
||||
|
||||
const embeddingModelProvidersKeys = Object.keys(embeddingModelProvidersRaw);
|
||||
embeddingModelProvidersKeys.forEach((provider) => {
|
||||
embeddingModelProviders[provider] = {};
|
||||
const models = Object.keys(embeddingModelProvidersRaw[provider]);
|
||||
models.forEach((model) => {
|
||||
embeddingModelProviders[provider][model] = {};
|
||||
});
|
||||
});
|
||||
|
||||
res.status(200).json({ chatModelProviders, embeddingModelProviders });
|
||||
} catch (err) {
|
||||
|
|
|
@ -13,6 +13,7 @@ import db from '../db';
|
|||
import { chats, messages } from '../db/schema';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import crypto from 'crypto';
|
||||
import { isLibraryEnabled } from '../config';
|
||||
|
||||
type Message = {
|
||||
messageId: string;
|
||||
|
@ -46,6 +47,8 @@ const handleEmitterEvents = (
|
|||
let recievedMessage = '';
|
||||
let sources = [];
|
||||
|
||||
const libraryEnabled = isLibraryEnabled();
|
||||
|
||||
emitter.on('data', (data) => {
|
||||
const parsedData = JSON.parse(data);
|
||||
if (parsedData.type === 'response') {
|
||||
|
@ -71,18 +74,20 @@ const handleEmitterEvents = (
|
|||
emitter.on('end', () => {
|
||||
ws.send(JSON.stringify({ type: 'messageEnd', messageId: messageId }));
|
||||
|
||||
db.insert(messages)
|
||||
.values({
|
||||
content: recievedMessage,
|
||||
chatId: chatId,
|
||||
messageId: messageId,
|
||||
role: 'assistant',
|
||||
metadata: JSON.stringify({
|
||||
createdAt: new Date(),
|
||||
...(sources && sources.length > 0 && { sources }),
|
||||
}),
|
||||
})
|
||||
.execute();
|
||||
if (libraryEnabled) {
|
||||
db.insert(messages)
|
||||
.values({
|
||||
content: recievedMessage,
|
||||
chatId: chatId,
|
||||
messageId: messageId,
|
||||
role: 'assistant',
|
||||
metadata: JSON.stringify({
|
||||
createdAt: new Date(),
|
||||
...(sources && sources.length > 0 && { sources }),
|
||||
}),
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
});
|
||||
emitter.on('error', (data) => {
|
||||
const parsedData = JSON.parse(data);
|
||||
|
@ -132,6 +137,8 @@ export const handleMessage = async (
|
|||
if (parsedWSMessage.type === 'message') {
|
||||
const handler = searchHandlers[parsedWSMessage.focusMode];
|
||||
|
||||
const libraryEnabled = isLibraryEnabled();
|
||||
|
||||
if (handler) {
|
||||
const emitter = handler(
|
||||
parsedMessage.content,
|
||||
|
@ -142,34 +149,36 @@ export const handleMessage = async (
|
|||
|
||||
handleEmitterEvents(emitter, ws, id, parsedMessage.chatId);
|
||||
|
||||
const chat = await db.query.chats.findFirst({
|
||||
where: eq(chats.id, parsedMessage.chatId),
|
||||
});
|
||||
if (libraryEnabled) {
|
||||
const chat = await db.query.chats.findFirst({
|
||||
where: eq(chats.id, parsedMessage.chatId),
|
||||
});
|
||||
|
||||
if (!chat) {
|
||||
await db
|
||||
.insert(chats)
|
||||
.values({
|
||||
id: parsedMessage.chatId,
|
||||
title: parsedMessage.content,
|
||||
createdAt: new Date().toString(),
|
||||
focusMode: parsedWSMessage.focusMode,
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
if (!chat) {
|
||||
await db
|
||||
.insert(chats)
|
||||
.insert(messages)
|
||||
.values({
|
||||
id: parsedMessage.chatId,
|
||||
title: parsedMessage.content,
|
||||
createdAt: new Date().toString(),
|
||||
focusMode: parsedWSMessage.focusMode,
|
||||
content: parsedMessage.content,
|
||||
chatId: parsedMessage.chatId,
|
||||
messageId: id,
|
||||
role: 'user',
|
||||
metadata: JSON.stringify({
|
||||
createdAt: new Date(),
|
||||
}),
|
||||
})
|
||||
.execute();
|
||||
}
|
||||
|
||||
await db
|
||||
.insert(messages)
|
||||
.values({
|
||||
content: parsedMessage.content,
|
||||
chatId: parsedMessage.chatId,
|
||||
messageId: id,
|
||||
role: 'user',
|
||||
metadata: JSON.stringify({
|
||||
createdAt: new Date(),
|
||||
}),
|
||||
})
|
||||
.execute();
|
||||
} else {
|
||||
ws.send(
|
||||
JSON.stringify({
|
||||
|
|
|
@ -5,7 +5,31 @@ export const metadata: Metadata = {
|
|||
title: 'Library - Perplexica',
|
||||
};
|
||||
|
||||
const Layout = ({ children }: { children: React.ReactNode }) => {
|
||||
const Layout = async ({ children }: { children: React.ReactNode }) => {
|
||||
const res = await fetch(
|
||||
`${process.env.NEXT_PUBLIC_API_URL}/config/preferences`,
|
||||
{
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
},
|
||||
);
|
||||
|
||||
const data = await res.json();
|
||||
|
||||
const { isLibraryEnabled } = data;
|
||||
|
||||
if (!isLibraryEnabled) {
|
||||
return (
|
||||
<div className="flex flex-row items-center justify-center min-h-screen w-full">
|
||||
<p className="text-lg dark:text-white/70 text-black/70">
|
||||
Library is disabled
|
||||
</p>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
return <div>{children}</div>;
|
||||
};
|
||||
|
||||
|
|
|
@ -2,7 +2,7 @@
|
|||
|
||||
import DeleteChat from '@/components/DeleteChat';
|
||||
import { formatTimeDifference } from '@/lib/utils';
|
||||
import { BookOpenText, ClockIcon, Delete, ScanEye } from 'lucide-react';
|
||||
import { BookOpenText, ClockIcon } from 'lucide-react';
|
||||
import Link from 'next/link';
|
||||
import { useEffect, useState } from 'react';
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue