|
2 | 2 | "This ns centralizes all available tools for LLMs including |
3 | 3 | eca native tools and MCP servers." |
4 | 4 | (:require |
| 5 | + [cheshire.core :as json] |
5 | 6 | [clojure.string :as string] |
6 | 7 | [eca.features.tools.chat :as f.tools.chat] |
7 | 8 | [eca.features.tools.custom :as f.tools.custom] |
|
74 | 75 | (mapv #(assoc % :origin :native) (native-tools db config)) |
75 | 76 | (mapv #(assoc % :origin :mcp) (f.mcp/all-tools db)))))) |
76 | 77 |
|
| 78 | +(defn ^:private pretty-format-any-json-output |
| 79 | + [result] |
| 80 | + (update result |
| 81 | + :contents |
| 82 | + (fn [contents] |
| 83 | + (mapv (fn [content] |
| 84 | + (if (= :text (:type content)) |
| 85 | + (let [text (string/trim (:text content))] |
| 86 | + (if (or (and (string/starts-with? text "{") |
| 87 | + (string/ends-with? text "}")) |
| 88 | + (and (string/starts-with? text "[") |
| 89 | + (string/ends-with? text "]"))) |
| 90 | + (try |
| 91 | + (update content :text #(json/generate-string (json/parse-string %) {:pretty true})) |
| 92 | + (catch Exception e |
| 93 | + (println e) |
| 94 | + content)) |
| 95 | + content)) |
| 96 | + content)) |
| 97 | + contents)))) |
| 98 | + |
77 | 99 | (defn call-tool! [^String name ^Map arguments chat-id tool-call-id behavior db* config messenger metrics |
78 | 100 | call-state-fn ; thunk |
79 | 101 | state-transition-fn ; params: event & event-data |
|
82 | 104 | (let [arguments (update-keys arguments clojure.core/name) |
83 | 105 | db @db*] |
84 | 106 | (try |
85 | | - (let [result (if-let [native-tool-handler (get-in (native-definitions db config) [name :handler])] |
86 | | - (native-tool-handler arguments {:db db |
87 | | - :db* db* |
88 | | - :config config |
89 | | - :messenger messenger |
90 | | - :behavior behavior |
91 | | - :chat-id chat-id |
92 | | - :tool-call-id tool-call-id |
93 | | - :call-state-fn call-state-fn |
94 | | - :state-transition-fn state-transition-fn}) |
95 | | - (f.mcp/call-tool! name arguments {:db db |
96 | | - :db* db* |
97 | | - :config config |
98 | | - :messenger messenger |
99 | | - :behavior behavior |
100 | | - :chat-id chat-id |
101 | | - :tool-call-id tool-call-id |
102 | | - :call-state-fn call-state-fn |
103 | | - :state-transition-fn state-transition-fn}))] |
| 107 | + (let [result (-> (if-let [native-tool-handler (get-in (native-definitions db config) [name :handler])] |
| 108 | + (native-tool-handler arguments {:db db |
| 109 | + :db* db* |
| 110 | + :config config |
| 111 | + :messenger messenger |
| 112 | + :behavior behavior |
| 113 | + :chat-id chat-id |
| 114 | + :tool-call-id tool-call-id |
| 115 | + :call-state-fn call-state-fn |
| 116 | + :state-transition-fn state-transition-fn}) |
| 117 | + (f.mcp/call-tool! name arguments {:db db |
| 118 | + :db* db* |
| 119 | + :config config |
| 120 | + :messenger messenger |
| 121 | + :behavior behavior |
| 122 | + :chat-id chat-id |
| 123 | + :tool-call-id tool-call-id |
| 124 | + :call-state-fn call-state-fn |
| 125 | + :state-transition-fn state-transition-fn})) |
| 126 | + (pretty-format-any-json-output))] |
104 | 127 | (logger/debug logger-tag "Tool call result: " result) |
105 | 128 | (metrics/count-up! "tool-called" {:name name :error (:error result)} metrics) |
106 | 129 | result) |
|
0 commit comments