From 4492017b96db26a5c6432d2a9c232237f055d581 Mon Sep 17 00:00:00 2001 From: sabaimran Date: Fri, 2 Aug 2024 12:31:43 +0530 Subject: [PATCH] Move processmessagechunk file into a common chat function --- src/interface/web/app/chat/page.tsx | 68 +++---------------- src/interface/web/app/common/chatFunctions.ts | 55 ++++++++++++++- src/interface/web/app/share/chat/page.tsx | 67 +++--------------- 3 files changed, 76 insertions(+), 114 deletions(-) diff --git a/src/interface/web/app/chat/page.tsx b/src/interface/web/app/chat/page.tsx index 9bbd4c6c..fe2f9a23 100644 --- a/src/interface/web/app/chat/page.tsx +++ b/src/interface/web/app/chat/page.tsx @@ -9,7 +9,7 @@ import NavMenu from '../components/navMenu/navMenu'; import { useSearchParams } from 'next/navigation' import Loading from '../components/loading/loading'; -import { convertMessageChunkToJson, handleImageResponse, RawReferenceData } from '../common/chatFunctions'; +import { convertMessageChunkToJson, handleImageResponse, processMessageChunk, RawReferenceData } from '../common/chatFunctions'; import 'katex/dist/katex.min.css'; @@ -186,10 +186,18 @@ export default function Chat() { const event = buffer.slice(0, newEventIndex); buffer = buffer.slice(newEventIndex + eventDelimiter.length); if (event) { - processMessageChunk(event); + const currentMessage = messages.find(message => !message.completed); + + if (!currentMessage) { + console.error("No current message found"); + return; + } + + processMessageChunk(event, currentMessage); + + setMessages([...messages]); } } - } } @@ -209,60 +217,6 @@ export default function Chat() { } } - function processMessageChunk(rawChunk: string) { - const chunk = convertMessageChunkToJson(rawChunk); - const currentMessage = messages.find(message => !message.completed); - - if (!currentMessage) { - return; - } - - if (!chunk || !chunk.type) { - return; - } - - if (chunk.type === "status") { - const statusMessage = chunk.data as string; - currentMessage.trainOfThought.push(statusMessage); - } else if (chunk.type === "references") { - const references = chunk.data as RawReferenceData; - - if (references.context) { - currentMessage.context = references.context; - } - - if (references.onlineContext) { - currentMessage.onlineContext = references.onlineContext; - } - } else if (chunk.type === "message") { - const chunkData = chunk.data; - - if (chunkData !== null && typeof chunkData === 'object') { - try { - const jsonData = chunkData as any; - if (jsonData.image || jsonData.detail) { - let responseWithReference = handleImageResponse(chunk.data, true); - if (responseWithReference.response) currentMessage.rawResponse = responseWithReference.response; - if (responseWithReference.online) currentMessage.onlineContext = responseWithReference.online; - if (responseWithReference.context) currentMessage.context = responseWithReference.context; - } else if (jsonData.response) { - currentMessage.rawResponse = jsonData.response; - } - else { - console.log("any message", chunk); - } - } catch (e) { - currentMessage.rawResponse += chunkData; - } - } else { - currentMessage.rawResponse += chunkData; - } - } else if (chunk.type === "end_llm_response") { - currentMessage.completed = true; - } - setMessages([...messages]); - } - const handleConversationIdChange = (newConversationId: string) => { setConversationID(newConversationId); }; diff --git a/src/interface/web/app/common/chatFunctions.ts b/src/interface/web/app/common/chatFunctions.ts index a1657ad5..51a21115 100644 --- a/src/interface/web/app/common/chatFunctions.ts +++ b/src/interface/web/app/common/chatFunctions.ts @@ -1,4 +1,4 @@ -import { Context, OnlineContextData } from "../components/chatMessage/chatMessage"; +import { Context, OnlineContextData, StreamMessage } from "../components/chatMessage/chatMessage"; export interface RawReferenceData { context?: Context[]; @@ -68,6 +68,59 @@ export function convertMessageChunkToJson(chunk: string): MessageChunk { } } + +export function processMessageChunk(rawChunk: string, currentMessage: StreamMessage) { + const chunk = convertMessageChunkToJson(rawChunk); + + if (!currentMessage) { + return; + } + + if (!chunk || !chunk.type) { + return; + } + + if (chunk.type === "status") { + const statusMessage = chunk.data as string; + currentMessage.trainOfThought.push(statusMessage); + } else if (chunk.type === "references") { + const references = chunk.data as RawReferenceData; + + if (references.context) { + currentMessage.context = references.context; + } + + if (references.onlineContext) { + currentMessage.onlineContext = references.onlineContext; + } + } else if (chunk.type === "message") { + const chunkData = chunk.data; + + if (chunkData !== null && typeof chunkData === 'object') { + try { + const jsonData = chunkData as any; + if (jsonData.image || jsonData.detail) { + let responseWithReference = handleImageResponse(chunk.data, true); + if (responseWithReference.response) currentMessage.rawResponse = responseWithReference.response; + if (responseWithReference.online) currentMessage.onlineContext = responseWithReference.online; + if (responseWithReference.context) currentMessage.context = responseWithReference.context; + } else if (jsonData.response) { + currentMessage.rawResponse = jsonData.response; + } + else { + console.debug("any message", chunk); + } + } catch (e) { + currentMessage.rawResponse += chunkData; + } + } else { + currentMessage.rawResponse += chunkData; + } + } else if (chunk.type === "end_llm_response") { + currentMessage.completed = true; + } +} + export function handleImageResponse(imageJson: any, liveStream: boolean): ResponseWithReferences { let rawResponse = ""; diff --git a/src/interface/web/app/share/chat/page.tsx b/src/interface/web/app/share/chat/page.tsx index 56d27bbc..4a08f0a2 100644 --- a/src/interface/web/app/share/chat/page.tsx +++ b/src/interface/web/app/share/chat/page.tsx @@ -15,7 +15,7 @@ import { useAuthenticatedData } from '@/app/common/auth'; import ChatInputArea, { ChatOptions } from '@/app/components/chatInputArea/chatInputArea'; import { StreamMessage } from '@/app/components/chatMessage/chatMessage'; -import { convertMessageChunkToJson, handleCompiledReferences, handleImageResponse, RawReferenceData } from '@/app/common/chatFunctions'; +import { convertMessageChunkToJson, handleCompiledReferences, handleImageResponse, processMessageChunk, RawReferenceData } from '@/app/common/chatFunctions'; import { AgentData } from '@/app/agents/page'; @@ -204,7 +204,16 @@ export default function SharedChat() { const event = buffer.slice(0, newEventIndex); buffer = buffer.slice(newEventIndex + eventDelimiter.length); if (event) { - processMessageChunk(event); + const currentMessage = messages.find(message => !message.completed); + + if (!currentMessage) { + console.error("No current message found"); + return; + } + + processMessageChunk(event, currentMessage); + + setMessages([...messages]); } } @@ -245,60 +254,6 @@ export default function SharedChat() { })(); }, [conversationId]); - function processMessageChunk(rawChunk: string) { - const chunk = convertMessageChunkToJson(rawChunk); - const currentMessage = messages.find(message => !message.completed); - - if (!currentMessage) { - return; - } - - if (!chunk || !chunk.type) { - return; - } - - if (chunk.type === "status") { - const statusMessage = chunk.data as string; - currentMessage.trainOfThought.push(statusMessage); - } else if (chunk.type === "references") { - const references = chunk.data as RawReferenceData; - - if (references.context) { - currentMessage.context = references.context; - } - - if (references.onlineContext) { - currentMessage.onlineContext = references.onlineContext; - } - } else if (chunk.type === "message") { - const chunkData = chunk.data; - - if (chunkData !== null && typeof chunkData === 'object') { - try { - const jsonData = chunkData as any; - if (jsonData.image || jsonData.detail) { - let responseWithReference = handleImageResponse(chunk.data, true); - if (responseWithReference.response) currentMessage.rawResponse = responseWithReference.response; - if (responseWithReference.online) currentMessage.onlineContext = responseWithReference.online; - if (responseWithReference.context) currentMessage.context = responseWithReference.context; - } else if (jsonData.response) { - currentMessage.rawResponse = jsonData.response; - } - else { - console.log("any message", chunk); - } - } catch (e) { - currentMessage.rawResponse += chunkData; - } - } else { - currentMessage.rawResponse += chunkData; - } - } else if (chunk.type === "end_llm_response") { - currentMessage.completed = true; - } - setMessages([...messages]); - } - if (isLoading) { return ; }