From c38cff44383dd4cd2d885f363511bbc26cf7f9ab Mon Sep 17 00:00:00 2001 From: Valentino Stoll Date: Thu, 19 Dec 2024 14:43:55 -0500 Subject: [PATCH] Refactor input switching (#8) * Refactor CoHost so that it uses a much simpler looping mechanism for toggling input between listening modes * Highlight ShowAssistant thoughts in blue --- lib/podcast_buddy/co_host.rb | 64 +++++++++--------------- lib/podcast_buddy/show_assistant.rb | 2 +- spec/fixtures/tmp/session/transcript.log | 3 +- 3 files changed, 26 insertions(+), 43 deletions(-) diff --git a/lib/podcast_buddy/co_host.rb b/lib/podcast_buddy/co_host.rb index d15e006..eba83b5 100644 --- a/lib/podcast_buddy/co_host.rb +++ b/lib/podcast_buddy/co_host.rb @@ -24,14 +24,15 @@ def start loop do PodcastBuddy.logger.debug("Shutdown: wait_for_question...") and break if @shutdown - PodcastBuddy.logger.info Rainbow("Press ").blue + Rainbow("Enter").black.bg(:yellow) + Rainbow(" to signal a question start...").blue - wait_for_question_start - next unless @listening_for_question_at + if @listening_for_question_at + PodcastBuddy.logger.info PodcastBuddy.to_human("🎙️ Listening for question. Press ", :wait) + + PodcastBuddy.to_human("Enter", :input) + + PodcastBuddy.to_human(" to signal the end of the question...", :wait) + else + PodcastBuddy.logger.info Rainbow("Press ").blue + Rainbow("Enter").black.bg(:yellow) + Rainbow(" to signal a question start...").blue + end - PodcastBuddy.logger.info PodcastBuddy.to_human("🎙️ Listening for question. Press ", :wait) + - PodcastBuddy.to_human("Enter", :input) + - PodcastBuddy.to_human(" to signal the end of the question...", :wait) - wait_for_question_end + wait_for_input end end end @@ -43,56 +44,39 @@ def stop private def handle_transcription(data) - if question_listening_started_before?(data[:started_at]) + if @listening_for_question_at PodcastBuddy.logger.info PodcastBuddy.to_human("Heard Question: #{data[:text]}", :wait) @question_buffer << data[:text] end end - def question_listening_started_before?(start) - @listening_for_question_at && start <= @listening_for_question_at + def start_question + @listening_for_question_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) + @listener.suppress_what_you_hear! + @question_buffer = "" end - def wait_for_question_start - input = "" - Timeout.timeout(5) do - input = gets - PodcastBuddy.logger.debug("Input received...") if input.include?("\n") - if input.include?("\n") - @listening_for_question_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) if @listening_for_question_at.nil? - @listener.suppress_what_you_hear! - @question_buffer = "" - end - rescue Timeout::Error - return - end + def end_question + PodcastBuddy.logger.info "End of question signal. Generating answer..." + @listening_for_question_at = nil + @listener.announce_what_you_hear! + answer_question(@question_buffer).wait end - def wait_for_question_end + def wait_for_input + input = "" loop do - PodcastBuddy.logger.debug("Shutdown: wait_for_question_end...") and break if @shutdown - - sleep 0.1 and next if @listening_for_question_at.nil? - - input = "" - Timeout.timeout(5) do + PodcastBuddy.logger.debug("Shutdown: wait_for_input...") and break if @shutdown + Timeout.timeout(2) do input = gets PodcastBuddy.logger.debug("Input received...") if input.include?("\n") next unless input.to_s.include?("\n") + @listening_for_question_at.nil? ? start_question : end_question + break rescue Timeout::Error PodcastBuddy.logger.debug("Input timeout...") next end - - if input.empty? - next - else - PodcastBuddy.logger.info "End of question signal. Generating answer..." - @listening_for_question_at = nil - @listener.announce_what_you_hear! - answer_question(@question_buffer).wait - break - end end end diff --git a/lib/podcast_buddy/show_assistant.rb b/lib/podcast_buddy/show_assistant.rb index 32ec054..21ea9d9 100644 --- a/lib/podcast_buddy/show_assistant.rb +++ b/lib/podcast_buddy/show_assistant.rb @@ -113,7 +113,7 @@ def think_about(text) max_tokens: 250 }) new_summary = response.dig("choices", 0, "message", "content").strip - PodcastBuddy.logger.info "Thoughts: #{new_summary}" + PodcastBuddy.logger.info PodcastBuddy.to_human("Thoughts: #{new_summary}", :info) @session.update_summary(new_summary) rescue => e PodcastBuddy.logger.error "Failed to summarize discussion: #{e.message}" diff --git a/spec/fixtures/tmp/session/transcript.log b/spec/fixtures/tmp/session/transcript.log index cd09d44..dfc5b4c 100644 --- a/spec/fixtures/tmp/session/transcript.log +++ b/spec/fixtures/tmp/session/transcript.log @@ -1,2 +1 @@ -Existing transcripttest transcription -test transcription +Existing transcript \ No newline at end of file