|
84 | 84 | (async/<! |
85 | 85 | (->> (tools/make-tool-calls |
86 | 86 | (or (-> state :opts :level) 0) |
87 | | - (partial |
88 | | - tools/function-handler |
| 87 | + (partial |
| 88 | + tools/function-handler |
89 | 89 | ;; defaults for tool handling are opts, current state functions, and a host-dir override |
90 | | - (merge |
91 | | - (:opts state) |
92 | | - (select-keys state [:functions]) |
| 90 | + (merge |
| 91 | + (:opts state) |
| 92 | + (select-keys state [:functions]) |
93 | 93 | ;; note that host-dir, if it exists, is an override here |
94 | | - (select-keys (:metadata state) [:host-dir :timeout]))) |
| 94 | + (select-keys (:metadata state) [:host-dir :timeout]))) |
95 | 95 | calls) |
96 | 96 | (async/reduce conj []))))}))) |
97 | 97 |
|
|
104 | 104 | [_] |
105 | 105 | (async/go {})) |
106 | 106 |
|
| 107 | +(defn require-graph [s] |
| 108 | + (let [graphs-ns-symbol (symbol (format "graphs.%s" s))] |
| 109 | + (require graphs-ns-symbol) |
| 110 | + (ns-resolve graphs-ns-symbol 'graph))) |
| 111 | + |
107 | 112 | (declare stream chat-with-tools) |
108 | 113 |
|
109 | 114 | (defn apply-functions [coll] |
|
117 | 122 | [{:keys [init-state construct-graph next-state]}] |
118 | 123 | (fn [state] |
119 | 124 | (async/go |
120 | | - (let [sub-graph-state |
121 | | - (async/<! |
122 | | - (stream |
123 | | - ((or construct-graph chat-with-tools) state) |
| 125 | + (try |
| 126 | + (let [new-conversation-state |
124 | 127 | (-> |
125 | | - ((or |
126 | | - ;; the sub-graph might have a function or a vector of state overlays to apply |
127 | | - (and |
128 | | - init-state |
129 | | - (if (coll? init-state) |
130 | | - (apply-functions init-state) |
131 | | - init-state)) |
132 | | - ;; default is to assume there's a tool call with a function that contains a prompt |
133 | | - (comp state/construct-initial-state-from-prompts state/add-prompt-ref)) state) |
134 | | - (update-in [:opts :level] (fnil inc 0)))))] |
135 | | - ((or next-state state/add-last-message-as-tool-call) state sub-graph-state))))) |
| 128 | + ((or |
| 129 | + ;; the sub-graph might have a function or a vector of state overlays to apply |
| 130 | + (and |
| 131 | + init-state |
| 132 | + (if (coll? init-state) |
| 133 | + (apply-functions init-state) |
| 134 | + init-state)) |
| 135 | + ;; default is to assume there's a tool call with a function that contains a prompt |
| 136 | + (comp state/construct-initial-state-from-prompts state/add-prompt-ref)) state) |
| 137 | + (update-in [:opts :level] (fnil inc 0))) |
| 138 | + |
| 139 | + sub-graph-state |
| 140 | + (async/<! |
| 141 | + (stream |
| 142 | + ((or construct-graph |
| 143 | + (if-let [agent (-> new-conversation-state :metadata :agent)] |
| 144 | + (require-graph agent) |
| 145 | + chat-with-tools)) state) |
| 146 | + (-> new-conversation-state |
| 147 | + (update-in [:metadata] dissoc :agent))))] |
| 148 | + ((or next-state state/add-last-message-as-tool-call) state sub-graph-state)) |
| 149 | + (catch Throwable t |
| 150 | + (jsonrpc/notify :error {:content (str t)}) |
| 151 | + {:error (format "unable to enter sub-graph: %s" t)}))))) |
136 | 152 |
|
137 | 153 | ; ===================================================== |
138 | 154 | ; edge functions takes state and returns next node |
|
163 | 179 | (defn state-reducer |
164 | 180 | "reduce the state with the change from running a node" |
165 | 181 | [state change] |
| 182 | + (jsonrpc/notify :message {:debug (format "---\n%s\n---\n%s\n---\n" |
| 183 | + (with-out-str (pprint/pprint (state/summarize state))) |
| 184 | + (with-out-str (pprint/pprint change)))}) |
166 | 185 | (-> state |
167 | 186 | (merge (dissoc change :messages :tools)) |
168 | 187 | (update :messages concat (:messages change)) |
|
176 | 195 | [state m |
177 | 196 | node "start"] |
178 | 197 | (jsonrpc/notify :message {:debug (format "\n-> entering %s\n\n" node)}) |
179 | | - #_(jsonrpc/notify :message {:debug (with-out-str (pprint/pprint (state/summarize (dissoc state :opts))))}) |
180 | 198 | ;; TODO handling bad graphs with missing nodes |
181 | 199 | (let [enter-node (get-in graph [:nodes node]) |
182 | 200 | new-state (state-reducer state (async/<! (enter-node state)))] |
|
224 | 242 | (defn chat-with-tools [_] |
225 | 243 | (construct-graph |
226 | 244 | [[["start" start] |
227 | | - ["tools-query" tools-query] |
228 | 245 | ["completion" completion] |
229 | 246 | [:edge tool-or-end]] |
230 | 247 | [["sub-graph" (sub-graph-node {})] |
231 | | - ["tools-query"]] |
| 248 | + ["completion"]] |
232 | 249 | [["tool" (tool-node {})] |
233 | | - ["tools-query"]] |
| 250 | + ["completion"]] |
234 | 251 | [["end" end]]])) |
235 | 252 |
|
236 | 253 | (defn generate-one-tool-call [_] |
|
0 commit comments