From 20ef5bfc9357c55fcd38223630a269446889ea35 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Sun, 26 Nov 2023 20:33:26 -0800 Subject: [PATCH] Properly stop mediaRecorder stream to clear microphone in-use state --- src/interface/desktop/chat.html | 2 ++ src/interface/obsidian/src/chat_modal.ts | 4 +++- src/khoj/interface/web/chat.html | 9 +++++++-- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/src/interface/desktop/chat.html b/src/interface/desktop/chat.html index 120f6647..efb505f4 100644 --- a/src/interface/desktop/chat.html +++ b/src/interface/desktop/chat.html @@ -618,6 +618,8 @@ }); } else if (mediaRecorder.state === 'recording') { mediaRecorder.stop(); + mediaRecorder.stream.getTracks().forEach(track => track.stop()); + mediaRecorder = null; speakButtonImg.src = './assets/icons/microphone-solid.svg'; speakButtonImg.alt = 'Transcribe'; } diff --git a/src/interface/obsidian/src/chat_modal.ts b/src/interface/obsidian/src/chat_modal.ts index 16c5614f..7ad9d069 100644 --- a/src/interface/obsidian/src/chat_modal.ts +++ b/src/interface/obsidian/src/chat_modal.ts @@ -1,4 +1,4 @@ -import { App, Modal, RequestUrlParam, request, requestUrl, setIcon } from 'obsidian'; +import { App, Modal, request, requestUrl, setIcon } from 'obsidian'; import { KhojSetting } from 'src/settings'; import fetch from "node-fetch"; @@ -321,6 +321,8 @@ export class KhojChatModal extends Modal { }); } else if (this.mediaRecorder.state === 'recording') { this.mediaRecorder.stop(); + this.mediaRecorder.stream.getTracks().forEach(track => track.stop()); + this.mediaRecorder = undefined; setIcon(transcribeButton, "mic"); } } diff --git a/src/khoj/interface/web/chat.html b/src/khoj/interface/web/chat.html index de6b899c..14d587f1 100644 --- a/src/khoj/interface/web/chat.html +++ b/src/khoj/interface/web/chat.html @@ -407,11 +407,14 @@ To get started, just start typing below. You can also type / to see a list of co try { const responseAsJson = JSON.parse(chunk); if (responseAsJson.detail) { - newResponseText.innerHTML += responseAsJson.detail; + rawResponse += responseAsJson.detail; } } catch (error) { // If the chunk is not a JSON object, just display it as is - newResponseText.innerHTML += chunk; + rawResponse += chunk; + } finally { + newResponseText.innerHTML = ""; + newResponseText.appendChild(formatHTMLMessage(rawResponse)); } } else { // If the chunk is not a JSON object, just display it as is @@ -635,6 +638,8 @@ To get started, just start typing below. You can also type / to see a list of co }); } else if (mediaRecorder.state === 'recording') { mediaRecorder.stop(); + mediaRecorder.stream.getTracks().forEach(track => track.stop()); + mediaRecorder = null; speakButtonImg.src = '/static/assets/icons/microphone-solid.svg'; speakButtonImg.alt = 'Transcribe'; }