Sha256: 4b6012bd3330f2194f8ea7e6a6961898bdc182973d2504fd62ae4b135a89fdac

Contents?: true

Size: 1.71 KB

Versions: 3

Compression:

Stored size: 1.71 KB

Contents

# frozen_string_literal: true

module Luo
  class AgentRunnerBase

    include Configurable

    setting :language, default: "en"

    def initialize
      on_init
    end

    def on_init
    end

    def context
      @context ||= Luo::AgentRunnerContext.new
    end

    def reset_context
      histories = context.histories
      @context = Luo::AgentRunnerContext.new
      @context.histories = histories
      @context
    end

    def call(user_input)
      context.user_input = user_input
      on_request
      on_result
      on_run
      after_run
      rt = Marshal.load(Marshal.dump(context))
      reset_context
      rt
    end

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

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

    def after_run
    end

    def on_run
      context.have_running_agents.each do |agent|
        context.agent_results << { agent_name: agent.class.agent_name, agent_response: agent.call }
      end
      context.have_running_agents.clear
    end

    def add_agent(agent)
      context.have_running_agents << agent
    end

    class << self

      def agents
        @agents ||= {}
      end

      [:on_result, :on_request, :on_init, :after_run].each do |method_name|
        define_method(method_name) do |&block|
          define_method(method_name, &block)
        end
      end

      def register(agent)
        agents[agent.agent_name] = agent
      end

      def language_info
        if self.config.language == "en"
          ""
        elsif self.config.language == "zh"
          "(must be a Chinese string)"
        else
          ""
        end
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

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