From b27f59b12b75ec86fd5fd6c32c0ec83b18e83cf5 Mon Sep 17 00:00:00 2001 From: sabaimran Date: Thu, 30 May 2024 11:39:04 +0530 Subject: [PATCH] Remove all unused code related to websockets --- src/interface/obsidian/src/chat_view.ts | 186 ------------------------ 1 file changed, 186 deletions(-) diff --git a/src/interface/obsidian/src/chat_view.ts b/src/interface/obsidian/src/chat_view.ts index 3aa5a947..3bca574e 100644 --- a/src/interface/obsidian/src/chat_view.ts +++ b/src/interface/obsidian/src/chat_view.ts @@ -10,13 +10,6 @@ export interface ChatJsonResult { inferredQueries?: string[]; } -interface WebSocketState { - newResponseTextEl: HTMLElement | null, - newResponseEl: HTMLElement | null, - loadingEllipsis: HTMLElement | null, - references: object, - rawResponse: string, -} interface Location { region: string; @@ -29,21 +22,12 @@ export class KhojChatView extends KhojPaneView { result: string; setting: KhojSetting; waitingForLocation: boolean; - websocket: WebSocket; - websocketState: WebSocketState; location: Location; constructor(leaf: WorkspaceLeaf, setting: KhojSetting) { super(leaf, setting); this.waitingForLocation = true; - this.websocketState = { - newResponseTextEl: null, - newResponseEl: null, - loadingEllipsis: null, - references: {}, - rawResponse: "", - }; fetch("https://ipapi.co/json") .then(response => response.json()) @@ -60,7 +44,6 @@ export class KhojChatView extends KhojPaneView { }) .finally(() => { this.waitingForLocation = false; - this.setupWebSocket(); }); } @@ -78,10 +61,6 @@ export class KhojChatView extends KhojPaneView { } async chat() { - if (this.websocket?.readyState === WebSocket.OPEN){ - this.sendMessageViaWebSocket(); - return; - } // Get text in chat input element let input_el = this.contentEl.getElementsByClassName("khoj-chat-input")[0]; @@ -650,7 +629,6 @@ export class KhojChatView extends KhojPaneView { let chatUrl = `${this.setting.khojUrl}/api/chat/history?client=obsidian`; if (chatBodyEl.dataset.conversationId) { chatUrl += `&conversation_id=${chatBodyEl.dataset.conversationId}`; - this.setupWebSocket(); } try { @@ -671,7 +649,6 @@ export class KhojChatView extends KhojPaneView { } else if (responseJson.response) { // Render conversation history, if any chatBodyEl.dataset.conversationId = responseJson.response.conversation_id; - this.setupWebSocket(); chatBodyEl.dataset.conversationTitle = responseJson.response.slug || `New conversation 🌱`; @@ -1140,167 +1117,4 @@ export class KhojChatView extends KhojPaneView { return referencesDiv; } - - setupWebSocket() { - let chatBody = this.contentEl.getElementsByClassName("khoj-chat-body")[0] as HTMLElement; - let wsProtocol = this.setting.khojUrl.startsWith('https:') ? 'wss:' : 'ws:'; - let baseUrl = this.setting.khojUrl.replace(/^https?:\/\//, ''); - let webSocketUrl = `${wsProtocol}//${baseUrl}/api/chat/ws`; - - if (this.waitingForLocation) { - console.debug("Waiting for location data to be fetched. Will setup WebSocket once location data is available."); - return; - } - if (!chatBody) return; - - this.websocketState = { - newResponseTextEl: null, - newResponseEl: null, - loadingEllipsis: null, - references: {}, - rawResponse: "", - } - - if (chatBody.dataset.conversationId) { - webSocketUrl += `?conversation_id=${chatBody.dataset.conversationId}`; - webSocketUrl += !!this.location ? `®ion=${this.location.region}&city=${this.location.city}&country=${this.location.countryName}&timezone=${this.location.timezone}` : ''; - - this.websocket = new WebSocket(webSocketUrl); - this.websocket.onmessage = (event) => { - // Get the last element in the chat-body - let chunk = event.data; - if (chunk == "start_llm_response") { - console.log("Started streaming", new Date()); - } else if(chunk == "end_llm_response") { - console.log("Stopped streaming", new Date()); - // Append any references after all the data has been streamed - this.finalizeChatBodyResponse(this.websocketState.references, this.websocketState.newResponseTextEl); - - // Reset variables - this.websocketState = { - newResponseTextEl: null, - newResponseEl: null, - loadingEllipsis: null, - references: {}, - rawResponse: "", - } - } else { - try { - if (chunk.includes("application/json")) { - chunk = JSON.parse(chunk); - } - } catch (error) { - // If the chunk is not a JSON object, continue. - } - - const contentType = chunk["content-type"] - if (contentType === "application/json") { - // Handle JSON response - try { - if (chunk.image || chunk.detail) { - const { rawResponse, references } = this.handleImageResponse(chunk, this.websocketState.rawResponse); - this.websocketState.rawResponse = rawResponse; - this.websocketState.references = references; - } else if (chunk.type == "status") { - this.handleStreamResponse(this.websocketState.newResponseTextEl, chunk.message, null, false); - } else if (chunk.type == "rate_limit") { - this.handleStreamResponse(this.websocketState.newResponseTextEl, chunk.message, this.websocketState.loadingEllipsis, true); - } else { - this.websocketState.rawResponse = chunk.response; - } - } catch (error) { - // If the chunk is not a JSON object, just display it as is - this.websocketState.rawResponse += chunk; - } finally { - if (chunk.type != "status" && chunk.type != "rate_limit") { - this.addMessageToChatBody(this.websocketState.rawResponse, this.websocketState.newResponseTextEl, this.websocketState.references); - } - } - } else { - // Handle streamed response of type text/event-stream or text/plain - if (chunk && chunk.includes("### compiled references:")) { - const { rawResponse, references } = this.handleCompiledReferences(this.websocketState.newResponseTextEl, chunk, this.websocketState.references, this.websocketState.rawResponse); - this.websocketState.rawResponse = rawResponse; - this.websocketState.references = references; - } else { - // If the chunk is not a JSON object, just display it as is - this.websocketState.rawResponse += chunk; - if (this.websocketState.newResponseTextEl) { - this.handleStreamResponse(this.websocketState.newResponseTextEl, this.websocketState.rawResponse, this.websocketState.loadingEllipsis); - } - } - - // Scroll to bottom of chat window as chat response is streamed - chatBody.scrollTop = chatBody.scrollHeight; - }; - } - } - }; - if (!this.websocket) return; - this.websocket.onclose = (event: Event) => { - console.log("WebSocket is closed now."); - let statusDotIcon = document.getElementById("connection-status-icon"); - let statusDotText = document.getElementById("connection-status-text"); - if (!statusDotIcon || !statusDotText) return; - statusDotIcon.style.backgroundColor = "red"; - statusDotText.style.marginTop = "5px"; - statusDotText.innerHTML = ''; - } - this.websocket.onerror = (event: Event) => { - console.log("WebSocket error observed:", event); - } - this.websocket.onopen = (event: Event) => { - console.log("WebSocket is open now.") - let statusDotIcon = document.getElementById("connection-status-icon"); - let statusDotText = document.getElementById("connection-status-text"); - if (!statusDotIcon || !statusDotText) return; - statusDotIcon.style.backgroundColor = "green"; - statusDotText.style.marginTop = "10px"; - statusDotText.textContent = "Connected to Server"; - } - } - - sendMessageViaWebSocket() { - let chatBody = this.contentEl.getElementsByClassName("khoj-chat-body")[0] as HTMLElement; - let chatInput = this.contentEl.getElementsByClassName("khoj-chat-input")[0] as HTMLTextAreaElement; - let query = chatInput?.value.trim(); - if (!chatInput || !chatBody || !query) return; - console.log(`Query: ${query}`); - - // Add message by user to chat body - this.renderMessage(chatBody, query, "you"); - chatInput.value = ""; - this.autoResize(); - chatInput.setAttribute("disabled", "disabled"); - - let newResponseEl = this.contentEl.createDiv(); - newResponseEl.classList.add("khoj-chat-message", "khoj"); - newResponseEl.setAttribute("data-meta", "🏮 Khoj at " + this.formatDate(new Date())); - chatBody.appendChild(newResponseEl); - - let newResponseTextEl = this.contentEl.createDiv(); - newResponseTextEl.classList.add("khoj-chat-message-text", "khoj"); - newResponseEl.appendChild(newResponseTextEl); - - // Temporary status message to indicate that Khoj is thinking - let loadingEllipsis = this.createLoadingEllipse(); - newResponseTextEl.appendChild(loadingEllipsis); - chatBody.scrollTop = chatBody.scrollHeight; - - // let chatTooltip = document.getElementById("chat-tooltip"); - // if (chatTooltip) chatTooltip.style.display = "none"; - - chatInput.classList.remove("option-enabled"); - - // Call specified Khoj API - this.websocket.send(query); - - this.websocketState = { - newResponseTextEl, - newResponseEl, - loadingEllipsis, - references: [], - rawResponse: "", - } - } }