diff options
author | icebaker <icebaker@proton.me> | 2023-11-18 19:07:10 -0300 |
---|---|---|
committer | icebaker <icebaker@proton.me> | 2023-11-18 19:07:10 -0300 |
commit | 8ae78b954350755a47a13133668dba93bac15f37 (patch) | |
tree | 9cdc3bb770d778bd8d00675fdbc1f27a6e27e37c /controllers/interfaces | |
parent | ab22d1bbe37093912cb7418b3c945153a15f4255 (diff) |
adding support for tools
Diffstat (limited to 'controllers/interfaces')
-rw-r--r-- | controllers/interfaces/repl.rb | 2 | ||||
-rw-r--r-- | controllers/interfaces/tools.rb | 90 |
2 files changed, 92 insertions, 0 deletions
diff --git a/controllers/interfaces/repl.rb b/controllers/interfaces/repl.rb index fd16ea6..d4191b1 100644 --- a/controllers/interfaces/repl.rb +++ b/controllers/interfaces/repl.rb @@ -37,6 +37,8 @@ module NanoBot [proc { prompt }, proc { 'MISSING INPUT' }] ) + Logic::Cartridge::Streaming.enabled?(cartridge, :repl) + Pry.commands.block_command(/(.*)/, 'handler') do |line| session.print(prefix) unless prefix.nil? session.evaluate_and_print(line, mode: 'repl') diff --git a/controllers/interfaces/tools.rb b/controllers/interfaces/tools.rb new file mode 100644 index 0000000..5105da1 --- /dev/null +++ b/controllers/interfaces/tools.rb @@ -0,0 +1,90 @@ +# frozen_string_literal: true + +require 'rainbow' + +require_relative '../../logic/cartridge/tools' +require_relative '../../components/embedding' + +module NanoBot + module Controllers + module Interfaces + module Tool + def self.adapt(feedback, adapter) + call = { + parameters: %w[id name parameters parameters-as-json output], + values: [ + feedback[:id], feedback[:name], feedback[:parameters], + feedback[:parameters].to_json, + feedback[:output] + ], + safety: false + } + + raise StandardError, 'conflicting adapters' if %i[fennel lua clojure].count { |key| !adapter[key].nil? } > 1 + + if adapter[:fennel] + call[:source] = adapter[:fennel] + Components::Embedding.fennel(**call) + elsif adapter[:clojure] + call[:source] = adapter[:clojure] + Components::Embedding.clojure(**call) + elsif adapter[:lua] + call[:parameters] = %w[id name parameters parameters_as_json output] + call[:source] = adapter[:lua] + Components::Embedding.lua(**call) + else + raise 'missing handler for adapter' + end + end + + def self.dispatch_feedback(session, cartridge, mode, feedback) + enabled = Logic::Cartridge::Tools.feedback?(cartridge, mode.to_sym, feedback[:action].to_sym) + + return unless enabled + + color = Logic::Cartridge::Tools.fetch_from_interface( + cartridge, mode.to_sym, feedback[:action].to_sym, [:color] + ) + + adapter = Tool.adapter(cartridge, mode, feedback) + + if %i[fennel lua clojure].any? { |key| !adapter[key].nil? } + message = adapt(feedback, adapter) + else + message = "(#{feedback[:name]} #{feedback[:parameters].to_json})" + + message += " =>\n#{feedback[:output]}" if feedback[:action].to_sym == :response + end + + message = "#{adapter[:prefix]}#{message}#{adapter[:suffix]}" + + session.print(color.nil? ? message : Rainbow(message).send(color)) + end + + def self.adapter(cartridge, mode, feedback) + prefix = Logic::Cartridge::Tools.fetch_from_interface( + cartridge, mode.to_sym, feedback[:action].to_sym, [:prefix] + ) + + suffix = Logic::Cartridge::Tools.fetch_from_interface( + cartridge, mode.to_sym, feedback[:action].to_sym, [:suffix] + ) + + fennel = Logic::Cartridge::Tools.fetch_from_interface( + cartridge, mode.to_sym, feedback[:action].to_sym, %i[adapter fennel] + ) + + lua = Logic::Cartridge::Tools.fetch_from_interface( + cartridge, mode.to_sym, feedback[:action].to_sym, %i[adapter lua] + ) + + clojure = Logic::Cartridge::Tools.fetch_from_interface( + cartridge, mode.to_sym, feedback[:action].to_sym, %i[adapter clojure] + ) + + { prefix:, suffix:, fennel:, lua:, clojure: } + end + end + end + end +end |