Skip to content

Commit

Permalink
Refactor input switching (#8)
Browse files Browse the repository at this point in the history
* Refactor CoHost so that it uses a much simpler looping mechanism for toggling input between listening modes
* Highlight ShowAssistant thoughts in blue
  • Loading branch information
codenamev authored Dec 19, 2024
1 parent 5539ca2 commit c38cff4
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 43 deletions.
64 changes: 24 additions & 40 deletions lib/podcast_buddy/co_host.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion lib/podcast_buddy/show_assistant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
3 changes: 1 addition & 2 deletions spec/fixtures/tmp/session/transcript.log
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
Existing transcripttest transcription
test transcription
Existing transcript

0 comments on commit c38cff4

Please sign in to comment.