File size: 5,045 Bytes
f8b5d42 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | import { THREAD_RENAME_EVENT } from "@/components/Sidebar/ActiveWorkspaces/ThreadContainer";
import { emitAssistantMessageCompleteEvent } from "@/components/contexts/TTSProvider";
export const ABORT_STREAM_EVENT = "abort-chat-stream";
// For handling of chat responses in the frontend by their various types.
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 the response is finalized, we can set the loading state to false.
// and append the metrics to the history.
if (type === "finalizeResponseStream") {
updatedHistory = {
...existingHistory,
closed: close,
animate: !close,
pending: false,
chatId,
metrics,
};
_chatHistory[chatIdx - 1] = { ..._chatHistory[chatIdx - 1], chatId }; // update prompt with 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);
}
// Action Handling via special 'action' attribute on response.
if (action === "reset_chat") {
// Chat was reset, keep reset message and clear everything else.
setChatHistory([_chatHistory.pop()]);
}
// If thread was updated automatically based on chat prompt
// then we can handle the updating of the thread here.
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."
);
}
|