| | import { THREAD_RENAME_EVENT } from "@/components/Sidebar/ActiveWorkspaces/ThreadContainer"; |
| | import { emitAssistantMessageCompleteEvent } from "@/components/contexts/TTSProvider"; |
| | export const ABORT_STREAM_EVENT = "abort-chat-stream"; |
| |
|
| | |
| | export default function handleChat( |
| | chatResult, |
| | setLoadingResponse, |
| | setChatHistory, |
| | remHistory, |
| | _chatHistory, |
| | setWebsocket |
| | ) { |
| | const { |
| | uuid, |
| | textResponse, |
| | type, |
| | sources = [], |
| | error, |
| | close, |
| | animate = false, |
| | chatId = null, |
| | action = null, |
| | metrics = {}, |
| | } = chatResult; |
| |
|
| | if (type === "abort" || type === "statusResponse") { |
| | setLoadingResponse(false); |
| | setChatHistory([ |
| | ...remHistory, |
| | { |
| | type, |
| | uuid, |
| | content: textResponse, |
| | role: "assistant", |
| | sources, |
| | closed: true, |
| | error, |
| | animate, |
| | pending: false, |
| | metrics, |
| | }, |
| | ]); |
| | _chatHistory.push({ |
| | type, |
| | uuid, |
| | content: textResponse, |
| | role: "assistant", |
| | sources, |
| | closed: true, |
| | error, |
| | animate, |
| | pending: false, |
| | metrics, |
| | }); |
| | } else if (type === "textResponse") { |
| | setLoadingResponse(false); |
| | setChatHistory([ |
| | ...remHistory, |
| | { |
| | uuid, |
| | content: textResponse, |
| | role: "assistant", |
| | sources, |
| | closed: close, |
| | error, |
| | animate: !close, |
| | pending: false, |
| | chatId, |
| | metrics, |
| | }, |
| | ]); |
| | _chatHistory.push({ |
| | uuid, |
| | content: textResponse, |
| | role: "assistant", |
| | sources, |
| | closed: close, |
| | error, |
| | animate: !close, |
| | pending: false, |
| | chatId, |
| | metrics, |
| | }); |
| | emitAssistantMessageCompleteEvent(chatId); |
| | } else if ( |
| | type === "textResponseChunk" || |
| | type === "finalizeResponseStream" |
| | ) { |
| | const chatIdx = _chatHistory.findIndex((chat) => chat.uuid === uuid); |
| | if (chatIdx !== -1) { |
| | const existingHistory = { ..._chatHistory[chatIdx] }; |
| | let updatedHistory; |
| |
|
| | |
| | |
| | if (type === "finalizeResponseStream") { |
| | updatedHistory = { |
| | ...existingHistory, |
| | closed: close, |
| | animate: !close, |
| | pending: false, |
| | chatId, |
| | metrics, |
| | }; |
| |
|
| | _chatHistory[chatIdx - 1] = { ..._chatHistory[chatIdx - 1], chatId }; |
| |
|
| | emitAssistantMessageCompleteEvent(chatId); |
| | setLoadingResponse(false); |
| | } else { |
| | updatedHistory = { |
| | ...existingHistory, |
| | content: existingHistory.content + textResponse, |
| | sources, |
| | error, |
| | closed: close, |
| | animate: !close, |
| | pending: false, |
| | chatId, |
| | metrics, |
| | }; |
| | } |
| | _chatHistory[chatIdx] = updatedHistory; |
| | } else { |
| | _chatHistory.push({ |
| | uuid, |
| | sources, |
| | error, |
| | content: textResponse, |
| | role: "assistant", |
| | closed: close, |
| | animate: !close, |
| | pending: false, |
| | chatId, |
| | metrics, |
| | }); |
| | } |
| | setChatHistory([..._chatHistory]); |
| | } else if (type === "agentInitWebsocketConnection") { |
| | setWebsocket(chatResult.websocketUUID); |
| | } else if (type === "stopGeneration") { |
| | const chatIdx = _chatHistory.length - 1; |
| | const existingHistory = { ..._chatHistory[chatIdx] }; |
| | const updatedHistory = { |
| | ...existingHistory, |
| | sources: [], |
| | closed: true, |
| | error: null, |
| | animate: false, |
| | pending: false, |
| | metrics, |
| | }; |
| | _chatHistory[chatIdx] = updatedHistory; |
| |
|
| | setChatHistory([..._chatHistory]); |
| | setLoadingResponse(false); |
| | } |
| |
|
| | |
| | if (action === "reset_chat") { |
| | |
| | setChatHistory([_chatHistory.pop()]); |
| | } |
| |
|
| | |
| | |
| | if (action === "rename_thread") { |
| | if (!!chatResult?.thread?.slug && chatResult.thread.name) { |
| | window.dispatchEvent( |
| | new CustomEvent(THREAD_RENAME_EVENT, { |
| | detail: { |
| | threadSlug: chatResult.thread.slug, |
| | newName: chatResult.thread.name, |
| | }, |
| | }) |
| | ); |
| | } |
| | } |
| | } |
| |
|
| | export function chatPrompt(workspace) { |
| | return ( |
| | workspace?.openAiPrompt ?? |
| | "Given the following conversation, relevant context, and a follow up question, reply with an answer to the current question the user is asking. Return only your response to the question given the above information following the users instructions as needed." |
| | ); |
| | } |
| |
|
| | export function chatQueryRefusalResponse(workspace) { |
| | return ( |
| | workspace?.queryRefusalResponse ?? |
| | "There is no relevant information in this workspace to answer your query." |
| | ); |
| | } |
| |
|