Move processmessagechunk file into a common chat function

This commit is contained in:
sabaimran
2024-08-02 12:31:43 +05:30
parent 13dee7d89e
commit 4492017b96
3 changed files with 76 additions and 114 deletions

View File

@@ -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 = "";