commander_agent.rb 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. module Agents
  2. class CommanderAgent < Agent
  3. include AgentControllerConcern
  4. cannot_create_events!
  5. description <<-MD
  6. This agent is triggered by schedule or an incoming event and commands other agents ("targets") to run, disable, configure, or enable themselves.
  7. # Action types
  8. Set `action` to one of the action types below:
  9. * `run`: Target Agents are run when this agent is triggered.
  10. * `disable`: Target Agents are disabled (if not) when this agent is triggered.
  11. * `enable`: Target Agents are enabled (if not) when this agent is triggered.
  12. * `configure`: Target Agents have their options updated with the contents of `configure_options`.
  13. Here's a tip: you can use Liquid templating to dynamically determine the action type. For example:
  14. - To create a CommanderAgent that receives an event from a WeatherAgent every morning to kick an agent flow that is only useful in a nice weather, try this: `{% if conditions contains 'Sunny' or conditions contains 'Cloudy' %}` `run{% endif %}`
  15. - Likewise, if you have a scheduled agent flow specially crafted for rainy days, try this: `{% if conditions contains 'Rain' %}enable{% else %}disabled{% endif %}`
  16. - If you want to update a WeatherAgent based on a UserLocationAgent, you could use `'action': 'configure'` and set 'configure_options' to `{ 'location': '{{_location_.latlng}}' }`.
  17. # Targets
  18. Select Agents that you want to control from this CommanderAgent.
  19. MD
  20. def working?
  21. true
  22. end
  23. def check!
  24. control!
  25. end
  26. def receive(incoming_events)
  27. incoming_events.each do |event|
  28. interpolate_with(event) do
  29. control!
  30. end
  31. end
  32. end
  33. end
  34. end