feat(config): Use toml instead of env

This commit is contained in:
ItzCrazyKns
2024-04-20 09:32:19 +05:30
parent dd1ce4e324
commit c6a5790d33
26 changed files with 799 additions and 596 deletions

View File

@ -1,10 +1,23 @@
import { WebSocket } from 'ws';
import { handleMessage } from './messageHandler';
import { ChatOpenAI, OpenAIEmbeddings } from '@langchain/openai';
import { getOpenaiApiKey } from '../config';
export const handleConnection = (ws: WebSocket) => {
const llm = new ChatOpenAI({
temperature: 0.7,
openAIApiKey: getOpenaiApiKey(),
});
const embeddings = new OpenAIEmbeddings({
openAIApiKey: getOpenaiApiKey(),
modelName: 'text-embedding-3-large',
});
ws.on(
'message',
async (message) => await handleMessage(message.toString(), ws),
async (message) =>
await handleMessage(message.toString(), ws, llm, embeddings),
);
ws.on('close', () => console.log('Connection closed'));

View File

@ -6,6 +6,8 @@ import handleWritingAssistant from '../agents/writingAssistant';
import handleWolframAlphaSearch from '../agents/wolframAlphaSearchAgent';
import handleYoutubeSearch from '../agents/youtubeSearchAgent';
import handleRedditSearch from '../agents/redditSearchAgent';
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
import type { Embeddings } from '@langchain/core/embeddings';
type Message = {
type: string;
@ -58,7 +60,12 @@ const handleEmitterEvents = (
});
};
export const handleMessage = async (message: string, ws: WebSocket) => {
export const handleMessage = async (
message: string,
ws: WebSocket,
llm: BaseChatModel,
embeddings: Embeddings,
) => {
try {
const parsedMessage = JSON.parse(message) as Message;
const id = Math.random().toString(36).substring(7);
@ -83,7 +90,12 @@ export const handleMessage = async (message: string, ws: WebSocket) => {
if (parsedMessage.type === 'message') {
const handler = searchHandlers[parsedMessage.focusMode];
if (handler) {
const emitter = handler(parsedMessage.content, history);
const emitter = handler(
parsedMessage.content,
history,
llm,
embeddings,
);
handleEmitterEvents(emitter, ws, id);
} else {
ws.send(JSON.stringify({ type: 'error', data: 'Invalid focus mode' }));

View File

@ -1,15 +1,17 @@
import { WebSocketServer } from 'ws';
import { handleConnection } from './connectionManager';
import http from 'http';
import { getPort } from '../config';
export const initServer = (
server: http.Server<typeof http.IncomingMessage, typeof http.ServerResponse>,
) => {
const port = getPort();
const wss = new WebSocketServer({ server });
wss.on('connection', (ws) => {
handleConnection(ws);
});
console.log(`WebSocket server started on port ${process.env.PORT}`);
console.log(`WebSocket server started on port ${port}`);
};