| 1 |
|
defmodule Tools.Sys.Spawn do |
| 2 |
|
import Elita, only: [spawn: 2] |
| 3 |
|
import Enum, only: [join: 2] |
| 4 |
|
import Log, only: [agent: 5] |
| 5 |
|
import Map, only: [get: 2, get: 3] |
| 6 |
|
import Utils.World, only: [agents: 0] |
| 7 |
|
|
| 8 |
|
@icon "🚀" |
| 9 |
|
|
| 10 |
|
def spec(name, state) do |
| 11 |
45 |
%{name: name, description: description(state), parameters: parameters()} |
| 12 |
|
end |
| 13 |
|
|
| 14 |
:-( |
def icon, do: @icon |
| 15 |
|
|
| 16 |
|
defp description(state) do |
| 17 |
45 |
"Spawn an agent with the person's name and one or more configs. Configs must be existing agent files (boss, worker, actor, etc).#{help(state)}" |
| 18 |
|
end |
| 19 |
|
|
| 20 |
|
def exec(_, %{"name" => %{"name" => name} = inner}, state) do |
| 21 |
:-( |
run(name, configs(get(inner, "configs"), name), state) |
| 22 |
|
end |
| 23 |
|
|
| 24 |
|
def exec(_, %{"name" => name} = args, state) do |
| 25 |
21 |
run(name, get(args, "configs", [name]), state) |
| 26 |
|
end |
| 27 |
|
|
| 28 |
|
def exec(_, %{"configs" => [name | _] = configs}, state) do |
| 29 |
:-( |
run(name, configs, state) |
| 30 |
|
end |
| 31 |
|
|
| 32 |
|
defp help(_state) do |
| 33 |
45 |
available = agents() |> join(", ") |
| 34 |
|
|
| 35 |
45 |
"\nAvailable: #{available}\nExamples:\n- spawn agent: spawn(name: \"my_agent\")\n- spawn named agent: spawn(name: \"agent_name\", configs: [\"config\"])\n- spawn multi role: spawn(name: \"hybrid\", configs: [\"config1\", \"config2\"])" |
| 36 |
|
end |
| 37 |
|
|
| 38 |
|
defp parameters do |
| 39 |
45 |
%{type: "object", properties: props(), required: ["name"]} |
| 40 |
|
end |
| 41 |
|
|
| 42 |
|
defp props do |
| 43 |
45 |
%{ |
| 44 |
|
name: %{type: "string", description: "Person's name, e.g. michael, dwight, pam, jim"}, |
| 45 |
|
configs: configs() |
| 46 |
|
} |
| 47 |
|
end |
| 48 |
|
|
| 49 |
|
defp configs do |
| 50 |
45 |
%{type: "array", items: items(), description: blurb()} |
| 51 |
|
end |
| 52 |
|
|
| 53 |
|
defp items do |
| 54 |
45 |
%{type: "string", enum: agents()} |
| 55 |
|
end |
| 56 |
|
|
| 57 |
45 |
defp blurb do |
| 58 |
|
"Configs for the agent, defaults to [name]" |
| 59 |
|
end |
| 60 |
|
|
| 61 |
|
defp configs([_ | _] = list, _name) do |
| 62 |
:-( |
list |
| 63 |
|
end |
| 64 |
|
|
| 65 |
:-( |
defp configs(_other, name) do |
| 66 |
|
[name] |
| 67 |
|
end |
| 68 |
|
|
| 69 |
21 |
defp run(name, configs, %{name: spawner, skip_logs: silent} = state) do |
| 70 |
21 |
log(name, configs, spawner, silent) |
| 71 |
21 |
started(spawn(name, configs), name) |
| 72 |
|
{"spawned", state} |
| 73 |
|
end |
| 74 |
|
|
| 75 |
21 |
defp started({:ok, _pid}, _name), do: :ok |
| 76 |
|
|
| 77 |
:-( |
defp log(_name, _config, _spawner, true) do |
| 78 |
|
:ok |
| 79 |
|
end |
| 80 |
|
|
| 81 |
:-( |
defp log(_name, _config, "el", _) do |
| 82 |
|
:ok |
| 83 |
|
end |
| 84 |
|
|
| 85 |
|
defp log(name, [name], spawner, false) do |
| 86 |
2 |
agent(@icon, name, " | ", "spawn", %{name: spawner}) |
| 87 |
|
end |
| 88 |
|
|
| 89 |
|
defp log(name, config, spawner, false) do |
| 90 |
19 |
agent(@icon, name, " as ", join(config, ", "), %{name: spawner}) |
| 91 |
|
end |
| 92 |
|
end |