ActiveWorkflow Agent Ruby
Helps you to develop your own ActiveWorkflow agents in Ruby.
The active_workflow_agent
library helps you to write your own ActiveWorkflow
agents in Ruby using ActiveWorkflow's remote agent API.
“Remote” in this context means that agents run in separate processes
from ActiveWorkflow itself. Communication between agents and ActiveWorkflow
takes place via HTTP. Each agent is effectively an HTTP server which ActiveWorkflow
connects to and interacts with via the remote agent API protocol.
#
InstallationAdd this line to your agent's Gemfile:
And then execute:
Or install it yourself as:
#
The ActiveWorkflow Agent API ProtocolActiveWorkflow talks to an agent over HTTP and all the requests and responses are in JSON. All communication happens using a single endpoint (the agent's registered URL) and an agent has to implement and handle the following 3 “methods”:
- register: called to retrieve an agent's metadata (name, description, options).
- receive: called when an agent has to process a message.
- check: called on schedule (as set by ActiveWorkflow users) and has no message to process.
For more details please see the remote agent API protocol.
#
Library FunctionalityTo deal with the protocol in your agent implementation this library offers four helper classes that are described below.
#
ParsedRequestParsedRequest
is a helper class to parse the content of a request
from the ActiveWorkflow Agent API.
#
Public Class Methods::new(request)
- create a new ParsedRequest object.
#
AttributesA ParsedRequest object always has the following attributes:
- method: a symbol representing the method called on the agent.
- options: a hash with all the agent options.
- memory: a hash with the state of the agent.
- credentials: an array of hashes containing credentials that the agent might need.
- message: a hash containing the message the agent received.
#
Example#
RegisterResponseRegisterResponse
is a helper class to construct a response to the 'register' method API call. It has the following methods:
#
Public Class Methods::new(name:, display_name:, description:, default_options:)
- create a new RegisterResponse object. Arguments:
- name: a unique identifier string in upper CamelCase without any whitespace.
- display_name: a string containing the name of the agent for display purposes.
- description: a string containing the description of the agent in markdown format.
- default_options: (optional) a hash containing the agent's options.
#
Public Instance Methods#to_h()
: returns the response as a hash.#to_json()
: returns the response as a JSON string.
#
Example#
CheckResponseCheckResponse
is helper class to construct a response to the 'check' method API call.
#add_logs(*str)
: add one or more log messages to the response object.#add_errors(*str)
: add one or more error messages to the response object.#add_messages(*hash)
: add the messages that the agent will emit.#add_memory(hash)
: add the agent's memory (state) to the response object.#to_h()
: returns the response as a hash.#to_json()
: returns the response as a JSON string.
#
ReceiveResponseReceiveResponse
is a helper class to construct a response to the 'receive' method API call.
#add_logs(*str)
: add one or more log messages to the response object.#add_errors(*str)
: add one or more error messages to the response object.#add_messages(*hash)
: add the messages that the agent will emit.#add_memory(hash)
: add the agent's memory (state) to the response object.#to_h()
: returns the response as a hash.#to_json()
: returns the response as a JSON string.
#
Example AgentBelow you can find an example of an ActiveWorkflow agent using the library helper classes.
#
Running an Agent Locally for DevelopmentSuppose that for development purposes you have created a simple agent that can run locally on http://0.0.0.0:4567
and you also run ActiveWorkflow using Docker. You would typically proceed in the following manner:
- You would first start your agent process at
http://0.0.0.0:4567
. - You would then start ActiveWorkflow locally with Docker, registering your agent by passing an environment variable. For example:
docker run -e REMOTE_AGENT_URL="http://host.docker.internal:4567/" -p 3000:3000 --rm automaticmode/active_workflow
. - When starting, ActiveWorkflow would make a request to your agent at
http://0.0.0.0:4567
with the 'register' method, in order to retrieve your agent's information. (see the register method for the details). - You would then log in to ActiveWorkflow, go to 'create a new agent' panel, select the agent with the name and description provided as a response to the 'register' method, and you would create an instance of this agent. You or other users could create multiple instances of this agent, possibly with different options (if your agent is configurable).
- If you schedule your agent instance to be invoked periodically, then ActiveWorkflow would make a request to
http://0.0.0.0:4567
at the scheduled time with the method 'check'. - Every time your agent receives a message from another agent, ActiveWorkflow would call it with the method 'receive' passing the message along.
#
Source CodeYou can find the activeworkflow_agent source code on GitHub.