| 1 |
|
defmodule Agent.Portal do |
| 2 |
|
import Agent.Session, only: [ask: 2] |
| 3 |
|
import Agent.Watch, only: [start: 2] |
| 4 |
|
import String, only: [trim: 1] |
| 5 |
|
import Process, only: [whereis: 1] |
| 6 |
|
|
| 7 |
|
def response(agent, question) do |
| 8 |
:-( |
query("user", "el.#{agent}", question) |
| 9 |
:-( |
start(agent, question) |
| 10 |
:-( |
handle(agent, question) |
| 11 |
|
end |
| 12 |
|
|
| 13 |
|
defp handle(agent, question) do |
| 14 |
:-( |
reply = process(locate(), agent, question) |
| 15 |
:-( |
respond(agent, reply) |
| 16 |
:-( |
reply |
| 17 |
|
end |
| 18 |
|
|
| 19 |
|
defp process(nil, agent, _) do |
| 20 |
:-( |
"unknown: #{agent}" |
| 21 |
|
end |
| 22 |
|
|
| 23 |
|
defp process(pid, _, question) do |
| 24 |
:-( |
{:ok, resp} = ask(pid, question) |
| 25 |
:-( |
resp |
| 26 |
|
end |
| 27 |
|
|
| 28 |
:-( |
defp respond(_, reply) when not is_binary(reply), do: :ok |
| 29 |
:-( |
defp respond(_, reply) when byte_size(reply) == 0, do: :ok |
| 30 |
:-( |
defp respond(agent, reply), do: answer(agent, trim(reply)) |
| 31 |
|
|
| 32 |
|
defp locate do |
| 33 |
:-( |
whereis(:puppet) |
| 34 |
|
end |
| 35 |
|
|
| 36 |
|
defp answer(agent, text) do |
| 37 |
:-( |
:erlang.apply(:"Elixir.Tools.Sys.Ask", :answer, [agent, text]) |
| 38 |
|
end |
| 39 |
|
|
| 40 |
|
defp query(role, context, question) do |
| 41 |
:-( |
:erlang.apply(:"Elixir.Tools.Sys.Ask", :query, [role, context, question]) |
| 42 |
|
end |
| 43 |
|
end |