2024-04-17 11:27:05 +05:30
|
|
|
import { EventEmitter, WebSocket } from 'ws';
|
2024-04-09 16:21:05 +05:30
|
|
|
import { BaseMessage, AIMessage, HumanMessage } from '@langchain/core/messages';
|
2024-04-13 12:20:36 +05:30
|
|
|
import handleWebSearch from '../agents/webSearchAgent';
|
2024-04-13 12:11:47 +05:30
|
|
|
import handleAcademicSearch from '../agents/academicSearchAgent';
|
|
|
|
import handleWritingAssistant from '../agents/writingAssistant';
|
|
|
|
import handleWolframAlphaSearch from '../agents/wolframAlphaSearchAgent';
|
|
|
|
import handleYoutubeSearch from '../agents/youtubeSearchAgent';
|
|
|
|
import handleRedditSearch from '../agents/redditSearchAgent';
|
2024-04-20 09:32:19 +05:30
|
|
|
import type { BaseChatModel } from '@langchain/core/language_models/chat_models';
|
|
|
|
import type { Embeddings } from '@langchain/core/embeddings';
|
2024-04-30 12:18:18 +05:30
|
|
|
import logger from '../utils/logger';
|
2024-06-29 11:09:31 +05:30
|
|
|
import db from '../db';
|
|
|
|
import { chats, messages } from '../db/schema';
|
|
|
|
import { eq } from 'drizzle-orm';
|
|
|
|
import crypto from 'crypto';
|
2024-04-09 16:21:05 +05:30
|
|
|
|
|
|
|
type Message = {
|
2024-06-29 11:09:31 +05:30
|
|
|
messageId: string;
|
|
|
|
chatId: string;
|
2024-04-09 16:21:05 +05:30
|
|
|
content: string;
|
2024-06-29 11:09:31 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
type WSMessage = {
|
|
|
|
message: Message;
|
2024-04-09 16:21:05 +05:30
|
|
|
copilot: boolean;
|
2024-06-29 11:09:31 +05:30
|
|
|
type: string;
|
2024-04-13 12:11:47 +05:30
|
|
|
focusMode: string;
|
2024-04-09 16:21:05 +05:30
|
|
|
history: Array<[string, string]>;
|
|
|
|
};
|
|
|
|
|
2024-04-17 11:27:05 +05:30
|
|
|
const searchHandlers = {
|
|
|
|
webSearch: handleWebSearch,
|
|
|
|
academicSearch: handleAcademicSearch,
|
|
|
|
writingAssistant: handleWritingAssistant,
|
|
|
|
wolframAlphaSearch: handleWolframAlphaSearch,
|
|
|
|
youtubeSearch: handleYoutubeSearch,
|
|
|
|
redditSearch: handleRedditSearch,
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleEmitterEvents = (
|
|
|
|
emitter: EventEmitter,
|
|
|
|
ws: WebSocket,
|
2024-06-29 11:09:31 +05:30
|
|
|
messageId: string,
|
|
|
|
chatId: string,
|
2024-04-17 11:27:05 +05:30
|
|
|
) => {
|
2024-06-29 11:09:31 +05:30
|
|
|
let recievedMessage = '';
|
|
|
|
let sources = [];
|
|
|
|
|
2024-04-17 11:27:05 +05:30
|
|
|
emitter.on('data', (data) => {
|
|
|
|
const parsedData = JSON.parse(data);
|
|
|
|
if (parsedData.type === 'response') {
|
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'message',
|
|
|
|
data: parsedData.data,
|
2024-06-29 11:09:31 +05:30
|
|
|
messageId: messageId,
|
2024-04-17 11:27:05 +05:30
|
|
|
}),
|
|
|
|
);
|
2024-06-29 11:09:31 +05:30
|
|
|
recievedMessage += parsedData.data;
|
2024-04-17 11:27:05 +05:30
|
|
|
} else if (parsedData.type === 'sources') {
|
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'sources',
|
|
|
|
data: parsedData.data,
|
2024-06-29 11:09:31 +05:30
|
|
|
messageId: messageId,
|
2024-04-17 11:27:05 +05:30
|
|
|
}),
|
|
|
|
);
|
2024-06-29 11:09:31 +05:30
|
|
|
sources = parsedData.data;
|
2024-04-17 11:27:05 +05:30
|
|
|
}
|
|
|
|
});
|
|
|
|
emitter.on('end', () => {
|
2024-06-29 11:09:31 +05:30
|
|
|
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();
|
2024-04-17 11:27:05 +05:30
|
|
|
});
|
|
|
|
emitter.on('error', (data) => {
|
|
|
|
const parsedData = JSON.parse(data);
|
2024-05-05 16:28:46 +05:30
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'error',
|
|
|
|
data: parsedData.data,
|
|
|
|
key: 'CHAIN_ERROR',
|
|
|
|
}),
|
|
|
|
);
|
2024-04-17 11:27:05 +05:30
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-04-20 09:32:19 +05:30
|
|
|
export const handleMessage = async (
|
|
|
|
message: string,
|
|
|
|
ws: WebSocket,
|
|
|
|
llm: BaseChatModel,
|
|
|
|
embeddings: Embeddings,
|
|
|
|
) => {
|
2024-04-09 16:21:05 +05:30
|
|
|
try {
|
2024-06-29 11:09:31 +05:30
|
|
|
const parsedWSMessage = JSON.parse(message) as WSMessage;
|
|
|
|
const parsedMessage = parsedWSMessage.message;
|
|
|
|
|
|
|
|
const id = crypto.randomBytes(7).toString('hex');
|
2024-04-09 16:21:05 +05:30
|
|
|
|
|
|
|
if (!parsedMessage.content)
|
|
|
|
return ws.send(
|
2024-05-05 16:28:46 +05:30
|
|
|
JSON.stringify({
|
|
|
|
type: 'error',
|
|
|
|
data: 'Invalid message format',
|
|
|
|
key: 'INVALID_FORMAT',
|
|
|
|
}),
|
2024-04-09 16:21:05 +05:30
|
|
|
);
|
|
|
|
|
2024-06-29 11:09:31 +05:30
|
|
|
const history: BaseMessage[] = parsedWSMessage.history.map((msg) => {
|
2024-04-09 16:21:05 +05:30
|
|
|
if (msg[0] === 'human') {
|
|
|
|
return new HumanMessage({
|
|
|
|
content: msg[1],
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
return new AIMessage({
|
|
|
|
content: msg[1],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-06-29 11:09:31 +05:30
|
|
|
if (parsedWSMessage.type === 'message') {
|
|
|
|
const handler = searchHandlers[parsedWSMessage.focusMode];
|
|
|
|
|
2024-04-17 11:27:05 +05:30
|
|
|
if (handler) {
|
2024-04-20 09:32:19 +05:30
|
|
|
const emitter = handler(
|
|
|
|
parsedMessage.content,
|
|
|
|
history,
|
|
|
|
llm,
|
|
|
|
embeddings,
|
|
|
|
);
|
2024-06-29 11:09:31 +05:30
|
|
|
|
|
|
|
handleEmitterEvents(emitter, ws, id, parsedMessage.chatId);
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
await db
|
|
|
|
.insert(messages)
|
|
|
|
.values({
|
|
|
|
content: parsedMessage.content,
|
|
|
|
chatId: parsedMessage.chatId,
|
|
|
|
messageId: id,
|
|
|
|
role: 'user',
|
|
|
|
metadata: JSON.stringify({
|
|
|
|
createdAt: new Date(),
|
|
|
|
}),
|
|
|
|
})
|
|
|
|
.execute();
|
2024-04-17 11:27:05 +05:30
|
|
|
} else {
|
2024-05-05 16:28:46 +05:30
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'error',
|
|
|
|
data: 'Invalid focus mode',
|
|
|
|
key: 'INVALID_FOCUS_MODE',
|
|
|
|
}),
|
|
|
|
);
|
2024-04-09 16:21:05 +05:30
|
|
|
}
|
|
|
|
}
|
2024-04-30 12:18:18 +05:30
|
|
|
} catch (err) {
|
2024-05-05 16:28:46 +05:30
|
|
|
ws.send(
|
|
|
|
JSON.stringify({
|
|
|
|
type: 'error',
|
|
|
|
data: 'Invalid message format',
|
|
|
|
key: 'INVALID_FORMAT',
|
|
|
|
}),
|
|
|
|
);
|
2024-04-30 12:18:18 +05:30
|
|
|
logger.error(`Failed to handle message: ${err}`);
|
2024-04-09 16:21:05 +05:30
|
|
|
}
|
|
|
|
};
|