summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoricebaker <icebaker@proton.me>2023-12-16 21:09:29 -0300
committericebaker <icebaker@proton.me>2023-12-16 21:09:29 -0300
commit55a717a548375e7fed0b02fbd9d69aae167d98ec (patch)
tree1f9b121ca28db627d079ad488adca41bc2ddaaa3
parent3cfd5ee507ead55322cebe514b383ffc50c41728 (diff)
improving repl experience
-rw-r--r--Gemfile.lock4
-rw-r--r--components/providers/google.rb58
-rw-r--r--controllers/interfaces/repl.rb61
-rw-r--r--nano-bots.gemspec2
4 files changed, 79 insertions, 46 deletions
diff --git a/Gemfile.lock b/Gemfile.lock
index 8a26530..7c70609 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -5,7 +5,7 @@ PATH
babosa (~> 2.0)
concurrent-ruby (~> 1.2, >= 1.2.2)
dotenv (~> 2.8, >= 2.8.1)
- gemini-ai (~> 2.1)
+ gemini-ai (~> 2.2)
pry (~> 0.14.2)
rainbow (~> 3.1, >= 3.1.1)
rbnacl (~> 7.1, >= 7.1.1)
@@ -34,7 +34,7 @@ GEM
multipart-post (~> 2)
faraday-net_http (3.0.2)
ffi (1.16.3)
- gemini-ai (2.1.0)
+ gemini-ai (2.2.0)
event_stream_parser (~> 1.0)
faraday (~> 2.7, >= 2.7.12)
googleauth (~> 1.9, >= 1.9.1)
diff --git a/components/providers/google.rb b/components/providers/google.rb
index a522d06..25ffbde 100644
--- a/components/providers/google.rb
+++ b/components/providers/google.rb
@@ -130,46 +130,34 @@ module NanoBot
end
end
- begin
- @client.stream_generate_content(
- Logic::Google::Tokens.apply_policies!(cartridge, payload),
- stream: true, &stream_call_back
- )
-
- if tools&.size&.positive?
- feedback.call(
- { should_be_stored: true,
- needs_another_round: true,
- interaction: { who: 'AI', message: nil, meta: { tool_calls: tools } } }
- )
- Tools.apply(
- cartridge, input[:tools], tools, feedback, Logic::Google::Tools
- ).each do |interaction|
- feedback.call({ should_be_stored: true, needs_another_round: true, interaction: })
- end
- end
+ @client.stream_generate_content(
+ Logic::Google::Tokens.apply_policies!(cartridge, payload),
+ stream: true, &stream_call_back
+ )
+ if tools&.size&.positive?
feedback.call(
- { should_be_stored: !(content.nil? || content == ''),
- interaction: content.nil? || content == '' ? nil : { who: 'AI', message: content },
- finished: true }
+ { should_be_stored: true,
+ needs_another_round: true,
+ interaction: { who: 'AI', message: nil, meta: { tool_calls: tools } } }
)
- rescue StandardError => e
- raise e.class, e.response[:body] if e.response && e.response[:body]
-
- raise e
+ Tools.apply(
+ cartridge, input[:tools], tools, feedback, Logic::Google::Tools
+ ).each do |interaction|
+ feedback.call({ should_be_stored: true, needs_another_round: true, interaction: })
+ end
end
- else
- begin
- result = @client.stream_generate_content(
- Logic::Google::Tokens.apply_policies!(cartridge, payload),
- stream: false
- )
- rescue StandardError => e
- raise e.class, e.response[:body] if e.response && e.response[:body]
- raise e
- end
+ feedback.call(
+ { should_be_stored: !(content.nil? || content == ''),
+ interaction: content.nil? || content == '' ? nil : { who: 'AI', message: content },
+ finished: true }
+ )
+ else
+ result = @client.stream_generate_content(
+ Logic::Google::Tokens.apply_policies!(cartridge, payload),
+ stream: false
+ )
tools = result.dig(0, 'candidates', 0, 'content', 'parts').filter do |part|
part.key?('functionCall')
diff --git a/controllers/interfaces/repl.rb b/controllers/interfaces/repl.rb
index fd16ea6..bf581a7 100644
--- a/controllers/interfaces/repl.rb
+++ b/controllers/interfaces/repl.rb
@@ -10,6 +10,21 @@ module NanoBot
module Controllers
module Interfaces
module REPL
+ COMMANDS_TO_BE_REMOVED = [
+ 'help', 'cd', 'find-method', 'ls', 'pry-backtrace', 'raise-up', 'reset', 'watch',
+ 'whereami', 'wtf?', '!', 'amend-line', 'edit', 'hist', 'show-input', 'ri', 'show-doc',
+ 'show-source', 'stat', 'import-set', 'play', '!!!', '!!@', '$', '?', '@', 'file-mode',
+ 'history', 'quit', 'quit-program', 'reload-method', 'show-method', 'cat',
+ 'change-inspector', 'change-prompt', 'clear-screen', 'fix-indent', 'list-inspectors',
+ 'save-file', 'shell-mode', 'pry-version', 'reload-code', 'toggle-color', '!pry',
+ 'disable-pry', 'jump-to', 'nesting', 'switch-to',
+ 'pry-theme'
+ ].freeze
+
+ COMMANDS_TO_KEEP = [
+ '/whereami[!?]+/', '.<shell command>', 'exit', 'exit-all', 'exit-program'
+ ].freeze
+
def self.boot(cartridge, session, prefix = nil, suffix = nil, as: 'repl')
return unless Logic::Helpers::Hash.fetch(cartridge, %i[behaviors boot instruction])
@@ -31,13 +46,7 @@ module NanoBot
prompt = self.prompt(cartridge)
- Pry.config.prompt = Pry::Prompt.new(
- 'REPL',
- 'REPL Prompt',
- [proc { prompt }, proc { 'MISSING INPUT' }]
- )
-
- Pry.commands.block_command(/(.*)/, 'handler') do |line|
+ handler = proc do |line|
session.print(prefix) unless prefix.nil?
session.evaluate_and_print(line, mode: 'repl')
session.print(suffix) unless suffix.nil?
@@ -45,7 +54,43 @@ module NanoBot
session.flush
end
- Pry.start
+ pry_prompt = Pry::Prompt.new(
+ 'REPL',
+ 'REPL Prompt',
+ [proc { prompt }, proc { 'MISSING INPUT' }]
+ )
+
+ pry_instance = Pry.new({ prompt: pry_prompt })
+
+ pry_instance.config.correct_indent = false
+
+ pry_instance.config.completer = Struct.new(:initialize, :call) do
+ def initialize(...); end
+ def call(...); end
+ end
+
+ first_whereami = true
+
+ pry_instance.config.commands.block_command(/whereami --quiet(.*)/, '/whereami[!?]+/') do |line|
+ unless first_whereami
+ handler.call(line.nil? ? 'whereami --quiet' : "whereami --quiet#{line}")
+ end
+ first_whereami = false
+ end
+
+ pry_instance.config.commands.block_command(/\.(.*)/, '.<shell command>') do |line|
+ handler.call(line.nil? ? '.' : ".#{line}")
+ end
+
+ COMMANDS_TO_BE_REMOVED.each do |command|
+ pry_instance.config.commands.block_command(command, 'handler') do |line|
+ handler.call(line.nil? ? command : "#{command} #{line}")
+ end
+ end
+
+ pry_instance.commands.block_command(/(.*)/, 'handler', &handler)
+
+ Pry::REPL.new(pry_instance).start
end
def self.prompt(cartridge)
diff --git a/nano-bots.gemspec b/nano-bots.gemspec
index 074cf50..15fc8cf 100644
--- a/nano-bots.gemspec
+++ b/nano-bots.gemspec
@@ -34,7 +34,7 @@ Gem::Specification.new do |spec|
spec.add_dependency 'babosa', '~> 2.0'
spec.add_dependency 'concurrent-ruby', '~> 1.2', '>= 1.2.2'
spec.add_dependency 'dotenv', '~> 2.8', '>= 2.8.1'
- spec.add_dependency 'gemini-ai', '~> 2.1'
+ spec.add_dependency 'gemini-ai', '~> 2.2'
spec.add_dependency 'pry', '~> 0.14.2'
spec.add_dependency 'rainbow', '~> 3.1', '>= 3.1.1'
spec.add_dependency 'rbnacl', '~> 7.1', '>= 7.1.1'