diff options
author | icebaker <icebaker@proton.me> | 2023-05-27 12:40:53 -0300 |
---|---|---|
committer | icebaker <icebaker@proton.me> | 2023-05-27 12:40:53 -0300 |
commit | 3723b08d1ff2b87c51e9595398e9c214f498f748 (patch) | |
tree | 4e71e396831a64fceb4b90e28fd9fb3a44548abb | |
parent | 084923fe46390b3f38350650574503d71d42cc89 (diff) |
improving repl apis
-rw-r--r-- | Gemfile.lock | 4 | ||||
-rw-r--r-- | controllers/instance.rb | 18 | ||||
-rw-r--r-- | controllers/interfaces/eval.rb | 8 | ||||
-rw-r--r-- | controllers/interfaces/repl.rb | 25 | ||||
-rw-r--r-- | controllers/session.rb | 12 | ||||
-rw-r--r-- | nano-bots.gemspec | 2 |
6 files changed, 51 insertions, 18 deletions
diff --git a/Gemfile.lock b/Gemfile.lock index 3c9346a..95f3767 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -4,7 +4,7 @@ PATH nano-bots (0.0.7) babosa (~> 2.0) dotenv (~> 2.8, >= 2.8.1) - faraday (~> 2.7, >= 2.7.4) + faraday (~> 2.7, >= 2.7.5) pry (~> 0.14.2) rainbow (~> 3.1, >= 3.1.1) ruby-openai (~> 4.0) @@ -18,7 +18,7 @@ GEM coderay (1.1.3) diff-lcs (1.5.0) dotenv (2.8.1) - faraday (2.7.4) + faraday (2.7.5) faraday-net_http (>= 2.0, < 3.1) ruby2_keywords (>= 0.0.4) faraday-multipart (1.0.4) diff --git a/controllers/instance.rb b/controllers/instance.rb index fbe2348..a982261 100644 --- a/controllers/instance.rb +++ b/controllers/instance.rb @@ -31,10 +31,24 @@ module NanoBot @session.state end - def eval(input, &block) + def boot(as: 'eval', &block) @stream.callback = block if block && @stream.is_a?(Components::Stream) - Interfaces::Eval.evaluate(input, @cartridge, @session) + Interfaces::REPL.boot(@cartridge, @session, as:) + + return unless @stream.is_a?(Components::Stream) + + @stream.finish + end + + def prompt + Interfaces::REPL.prompt(@cartridge) + end + + def eval(input, as: 'eval', &block) + @stream.callback = block if block && @stream.is_a?(Components::Stream) + + Interfaces::Eval.evaluate(input, @cartridge, @session, as) return unless @stream.is_a?(Components::Stream) diff --git a/controllers/interfaces/eval.rb b/controllers/interfaces/eval.rb index 851770b..5f472ad 100644 --- a/controllers/interfaces/eval.rb +++ b/controllers/interfaces/eval.rb @@ -10,13 +10,13 @@ module NanoBot module Controllers module Interfaces module Eval - def self.evaluate(input, cartridge, session) - prefix = Logic::Cartridge::Affixes.get(cartridge, :eval, :output, :prefix) - suffix = Logic::Cartridge::Affixes.get(cartridge, :eval, :output, :suffix) + def self.evaluate(input, cartridge, session, mode) + prefix = Logic::Cartridge::Affixes.get(cartridge, mode.to_sym, :output, :prefix) + suffix = Logic::Cartridge::Affixes.get(cartridge, mode.to_sym, :output, :suffix) session.print(prefix) unless prefix.nil? - session.evaluate_and_print(input, mode: 'eval') + session.evaluate_and_print(input, mode:) session.print(suffix) unless suffix.nil? end diff --git a/controllers/interfaces/repl.rb b/controllers/interfaces/repl.rb index ee6234b..fd16ea6 100644 --- a/controllers/interfaces/repl.rb +++ b/controllers/interfaces/repl.rb @@ -10,18 +10,26 @@ module NanoBot module Controllers module Interfaces module REPL + def self.boot(cartridge, session, prefix = nil, suffix = nil, as: 'repl') + return unless Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction]) + + prefix ||= Logic::Cartridge::Affixes.get(cartridge, as.to_sym, :output, :prefix) + suffix ||= Logic::Cartridge::Affixes.get(cartridge, as.to_sym, :output, :suffix) + + session.print(prefix) unless prefix.nil? + session.boot(mode: as) + session.print(suffix) unless suffix.nil? + end + def self.start(cartridge, session) prefix = Logic::Cartridge::Affixes.get(cartridge, :repl, :output, :prefix) suffix = Logic::Cartridge::Affixes.get(cartridge, :repl, :output, :suffix) - if Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction]) - session.print(prefix) unless prefix.nil? - session.boot(mode: 'repl') - session.print(suffix) unless suffix.nil? - session.print("\n") - end + boot(cartridge, session, prefix, suffix) + + session.print("\n") if Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction]) - prompt = build_prompt(Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl prompt])) + prompt = self.prompt(cartridge) Pry.config.prompt = Pry::Prompt.new( 'REPL', @@ -40,7 +48,8 @@ module NanoBot Pry.start end - def self.build_prompt(prompt) + def self.prompt(cartridge) + prompt = Logic::Helpers::Hash.fetch(cartridge, %i[interfaces repl prompt]) result = '' if prompt.is_a?(Array) diff --git a/controllers/session.rb b/controllers/session.rb index 3d5d707..270b623 100644 --- a/controllers/session.rb +++ b/controllers/session.rb @@ -61,6 +61,8 @@ module NanoBot @state[:history] << { who: 'user', + mode: mode.to_s, + input: message, message: Components::Adapter.apply( :input, Logic::Cartridge::Interaction.input(@cartridge, mode.to_sym, message) ) @@ -72,6 +74,9 @@ module NanoBot end def process(input, mode:) + prefix = Logic::Cartridge::Affixes.get(@cartridge, mode.to_sym, :output, :prefix) + suffix = Logic::Cartridge::Affixes.get(@cartridge, mode.to_sym, :output, :suffix) + interface = Logic::Helpers::Hash.fetch(@cartridge, [:interfaces, mode.to_sym]) || {} streaming = Logic::Cartridge::Streaming.enabled?(@cartridge, mode.to_sym) @@ -85,7 +90,7 @@ module NanoBot updated_at = Time.now if finished - @state[:history] << Marshal.load(Marshal.dump(output)) + event = Marshal.load(Marshal.dump(output)) output = Logic::Cartridge::Interaction.output( @cartridge, mode.to_sym, output, streaming, finished @@ -93,6 +98,11 @@ module NanoBot output[:message] = Components::Adapter.apply(:output, output[:message]) + event[:mode] = mode.to_s + event[:output] = "#{prefix}#{output[:message]}#{suffix}" + + @state[:history] << event + self.print(output[:message]) unless streaming ready = true diff --git a/nano-bots.gemspec b/nano-bots.gemspec index b717371..eb6f748 100644 --- a/nano-bots.gemspec +++ b/nano-bots.gemspec @@ -33,7 +33,7 @@ Gem::Specification.new do |spec| spec.add_dependency 'babosa', '~> 2.0' spec.add_dependency 'dotenv', '~> 2.8', '>= 2.8.1' - spec.add_dependency 'faraday', '~> 2.7', '>= 2.7.4' + spec.add_dependency 'faraday', '~> 2.7', '>= 2.7.5' spec.add_dependency 'pry', '~> 0.14.2' spec.add_dependency 'rainbow', '~> 3.1', '>= 3.1.1' spec.add_dependency 'ruby-openai', '~> 4.0' |