-
Notifications
You must be signed in to change notification settings - Fork 6
/
main.rb
85 lines (61 loc) · 2.19 KB
/
main.rb
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
# frozen_string_literal: true
require 'dotenv/load'
require './src/ruby/components/persona'
require './src/ruby/components/event'
persona_path, state_key, audio_file_path = ARGV
persona = Persona.load(persona_path)
@event = Event.new(persona)
@event.dispatch('interaction-engine-started')
require './src/ruby/components/speech_engine'
require './src/ruby/components/terminator'
require './src/ruby/components/speech_manager'
Terminator.instance.event = @event
SpeechManager.instance.event = @event
Terminator.instance.register_trap!
SpeechManager.instance.start!
begin
SpeechEngine.instance.persona = persona
SpeechManager.instance.persona = persona
unless audio_file_path
if (message = persona.dig(:events, :'welcome-message-booted', :message))
@event.dispatch('welcome-message-booted')
SpeechManager.instance.add_to_queue(message)
end
SpeechManager.instance.wait!
Terminator.instance.wait!
return
end
@event.dispatch('speech-to-text-started')
user_input = SpeechEngine.instance.hear(audio_file_path)
@event.dispatch('speech-to-text-completed', user_input)
require 'nano-bots'
bot = NanoBot.new(cartridge: persona[:'cartridge-file-path'], state: state_key)
buffer = ''
first = true
bot.eval(user_input) do |_content, fragment, _finished, meta|
next if fragment.nil?
if !meta.nil?
@event.dispatch("nano-bot-tool-#{meta[:tool][:action]}", fragment)
else
buffer += fragment
next unless persona[:'text-to-speech'][:settings][:'fragment-speech'] && buffer =~ /\.\s/
parts = buffer.split(/\.\s/)
dispatch_message = "#{parts[0].strip}."
@event.dispatch('nano-bot-answer-received', dispatch_message.strip, !first)
SpeechManager.instance.add_to_queue(dispatch_message)
buffer = parts[1] || ''
first = false
end
end
if buffer.strip != ''
@event.dispatch('nano-bot-answer-received', buffer.strip, !first)
SpeechManager.instance.add_to_queue(buffer.strip)
end
SpeechManager.instance.wait!
Terminator.instance.wait!
@event.dispatch('interaction-completed')
rescue Interrupt
Terminator.instance.terminate!
rescue StandardError => e
@event.dispatch('error-raised', e.message)
end