cover/Elixir.Tools.Sys.Ask.Schema.html

1 defmodule Tools.Sys.Ask.Schema do
2 def get(name, _state) do
3 47 %{name: name, description: description(), parameters: params()}
4 end
5
6 47 defp description do
7 "Ask question to another agent and get response"
8 end
9
10 defp params do
11 47 %{type: "object", properties: properties(), required: required()}
12 end
13
14 47 defp required do
15 ["recipient", "question"]
16 end
17
18 defp properties do
19 47 %{
20 recipient: %{type: "string", description: "Agent name to ask question to"},
21 question: %{type: "string", description: "Question to ask"}
22 }
23 end
24 end
25
26 defmodule Tools.Sys.Ask do
27 import Agent.Harness, only: [dispatch: 3]
28 import Log, only: [write: 1, agent: 5, trace: 1]
29 import String, only: [trim: 1]
30
31 @icon "🤔"
32 @reply "✨"
33
34 defdelegate spec(name, state), to: Tools.Sys.Ask.Schema, as: :get
35
36 def icon, do: @icon
37
38 def exec(_, %{"recipient" => r, "question" => q}, %{name: s, skip_logs: l} = state) do
39 query(s, r, q, l)
40 result = dispatch(r, q, :ask)
41 reply(r, s, result, l)
42 {result, state}
43 end
44
45 def exec(_, _, state) do
46 {"ask needs recipient and question", state}
47 end
48
49 def query(sender, recipient, question) do
50 query(sender, recipient, question, false)
51 end
52
53 def query(sender, recipient, question, silent) do
54 record(sender, recipient, question, silent)
55 end
56
57 defp record(_sender, _recipient, _question, true) do
58 :ok
59 end
60
61 defp record("el", _recipient, _question, _) do
62 :ok
63 end
64
65 defp record(sender, recipient, question, _) do
66 msg = "#{@icon} #{sender} → #{recipient} | #{question}\n"
67 write(msg)
68 trace(msg)
69 agent(@icon, "#{sender} → #{recipient}", " | ", question, %{name: sender})
70 end
71
72 def answer(agent, text) when is_binary(text) do
73 emit(agent, text)
74 log(agent, text)
75 end
76
77 def answer(_agent, _text), do: :ok
78
79 defp emit(agent, text) do
80 msg = "#{@reply} #{agent} | #{trim(text)}\n"
81 write(msg)
82 trace(msg)
83 end
84
85 defp log(agent, text) do
86 agent(@reply, agent, " | ", text, %{name: agent})
87 end
88
89 defp reply(replier, asker, text, silent) when is_binary(text) do
90 log(replier, asker, text, silent)
91 end
92
93 defp reply(_replier, _asker, _text, _silent), do: :ok
94
95 defp log(_replier, _asker, _text, true) do
96 :ok
97 end
98
99 defp log(replier, asker, text, _) do
100 msg = "#{@reply} #{replier} → #{asker} | #{trim(text)}\n"
101 write(msg)
102 trace(msg)
103 agent(@reply, "#{replier} → #{asker}", " | ", text, %{name: asker})
104 end
105 end
Line Hits Source