Sha256: 7e99b965983c114899930ba64873dacf98f31677fc2e7580a368d6067c0e33c1

Contents?: true

Size: 1.22 KB

Versions: 3

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

module Luo
  class Agent
    attr_reader :context, :action_input, :client
    def initialize(context: nil, action_input: nil, client: nil)
      @context = context
      @action_input = action_input
      @client = client
    end

    def call
      raise NotImplementedError, "call method must be implemented in subclass"
    end

    class << self
      def on_call(&block)
        define_method(:call, &block)
      end

      def on_call_with_final_result(&block)
        define_method(:call) do
          context.final_result = instance_eval(&block)
          context.final_result
        end
      end

      def self.create_parameter_method(method_name, not_provided = Object.new, &block)
        define_method(method_name.to_sym) do |content = not_provided|
          if content === not_provided
            class_variable_get("@@#{method_name}")
          else
            content = block.call(content) if block_given?
            class_variable_set("@@#{method_name}", content)
          end
        end
      end

      create_parameter_method(:agent_desc) { |content| content.gsub(/\n/, "") }
      create_parameter_method(:agent_name) { |context| context.gsub(/[[:punct:]]/, '') }

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
luo-0.1.3 lib/luo/agent.rb
luo-0.1.2 lib/luo/agent.rb
luo-0.1.1 lib/luo/agent.rb