Skip to content

Commit 88cef45

Browse files
committed
Return output and error
1 parent 58ca2f5 commit 88cef45

File tree

2 files changed

+52
-42
lines changed

2 files changed

+52
-42
lines changed

src/eca/features/chat.clj

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,27 +34,30 @@
3434
:role role
3535
:content content}))
3636

37-
(defn ^:private notify-before-hook! [chat-ctx {:keys [id name]}]
37+
(defn ^:private notify-before-hook-action! [chat-ctx {:keys [id name type]}]
3838
(send-content! chat-ctx :system
39-
{:type :hookStarted
39+
{:type :hookActionStarted
40+
:action-type type
4041
:name name
4142
:id id}))
4243

43-
(defn ^:private notify-after-hook! [chat-ctx {:keys [id name outputs status]}]
44+
(defn ^:private notify-after-hook-action! [chat-ctx {:keys [id name output error status type]}]
4445
(send-content! chat-ctx :system
45-
{:type :hookFinished
46+
{:type :hookActionFinished
47+
:action-type type
4648
:id id
4749
:name name
4850
:status status
49-
:outputs outputs}))
51+
:output output
52+
:error error}))
5053

5154
(defn finish-chat-prompt! [status {:keys [message chat-id db* metrics config on-finished-side-effect] :as chat-ctx}]
5255
(swap! db* assoc-in [:chats chat-id :status] status)
5356
(f.hooks/trigger-if-matches! :postPrompt
5457
{:chat-id chat-id
5558
:prompt message}
56-
{:on-before-execute (partial notify-before-hook! chat-ctx)
57-
:on-after-execute (partial notify-after-hook! chat-ctx)}
59+
{:on-before-action (partial notify-before-hook-action! chat-ctx)
60+
:on-after-action (partial notify-after-hook-action! chat-ctx)}
5861
@db*
5962
config)
6063
(send-content! chat-ctx :system
@@ -338,8 +341,8 @@
338341
:name (:name tool-call-state)
339342
:server (:server tool-call-state)
340343
:arguments (:arguments tool-call-state)}
341-
{:on-before-execute (partial notify-before-hook! chat-ctx)
342-
:on-after-execute (partial notify-after-hook! chat-ctx)}
344+
{:on-before-action (partial notify-before-hook-action! chat-ctx)
345+
:on-after-action (partial notify-after-hook-action! chat-ctx)}
343346
@db*
344347
(:config chat-ctx)))
345348

@@ -663,14 +666,14 @@
663666
:name name
664667
:server server
665668
:arguments arguments}
666-
{:on-before-execute (partial notify-before-hook! chat-ctx)
667-
:on-after-execute (fn [result]
668-
(when (= 2 (:status result))
669-
(transition-tool-call! db* chat-ctx id :hook-rejected
670-
{:reason {:code :hook-rejected
671-
:text (str "Tool call rejected by hook: " (string/join "\n" (:outputs result)))}})
672-
(reset! hook-approved?* false))
673-
(notify-after-hook! chat-ctx result))}
669+
{:on-before-action (partial notify-before-hook-action! chat-ctx)
670+
:on-after-action (fn [result]
671+
(when (= 2 (:status result))
672+
(transition-tool-call! db* chat-ctx id :hook-rejected
673+
{:reason {:code :hook-rejected
674+
:text (str "Tool call rejected by hook, output: " (:output result))}})
675+
(reset! hook-approved?* false))
676+
(notify-after-hook-action! chat-ctx result))}
674677
db
675678
config)
676679
(if (and @approved?* @hook-approved?*)
@@ -962,11 +965,11 @@
962965
_ (f.hooks/trigger-if-matches! :prePrompt
963966
{:chat-id chat-id
964967
:prompt message}
965-
{:on-before-execute (partial notify-before-hook! chat-ctx)
966-
:on-after-execute (fn [result]
967-
(when (= 0 (:status result))
968-
(reset! hook-outputs* (:outputs result)))
969-
(notify-after-hook! chat-ctx result))}
968+
{:on-before-action (partial notify-before-hook-action! chat-ctx)
969+
:on-after-action (fn [result]
970+
(when (= 0 (:status result))
971+
(reset! hook-outputs* (:outputs result)))
972+
(notify-after-hook-action! chat-ctx result))}
970973
db
971974
config)
972975
user-messages (if (seq @hook-outputs*)

src/eca/features/hooks.clj

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,35 @@
2424
shell (:shell action)
2525
input (json/generate-string (merge {:hook-name name} data))]
2626
(logger/info logger-tag (format "Running hook '%s' shell '%s' with input '%s'" name shell input))
27-
(let [{:keys [out err exit]} @(p/sh {:dir cwd
28-
:continue true}
29-
"bash" "-c" shell "--" input)]
27+
(let [{:keys [exit out err]} (p/sh {:dir cwd}
28+
"bash" "-c" shell "--" input)]
3029
[exit out err]))
3130
(logger/warn logger-tag (format "Unknown hook action %s for %s" (:type action) name))))
3231

3332
(defn trigger-if-matches!
3433
"Run hook of specified type if matches any config for that type"
35-
[type data {:keys [on-before-execute on-after-execute]} db config]
36-
(let [outputs* (atom [])
37-
status* (atom 0)]
38-
(doseq [[name hook] (:hooks config)]
39-
(when (= type (keyword (:type hook)))
40-
(when (hook-matches? type data hook)
41-
(let [id (str (random-uuid))]
42-
(on-before-execute {:id id
43-
:name name})
44-
(doseq [action (:actions hook)]
45-
(when-let [[status output] (run-hook-action! action name data db)]
46-
(reset! status* (max @status* status))
47-
(swap! outputs* conj output)))
48-
(on-after-execute {:id id
49-
:name name
50-
:status @status*
51-
:outputs @outputs*})))))))
34+
[type data {:keys [on-before-action on-after-action]} db config]
35+
(doseq [[name hook] (:hooks config)]
36+
(when (= type (keyword (:type hook)))
37+
(when (hook-matches? type data hook)
38+
(vec
39+
(map-indexed (fn [i action]
40+
(let [id (str (random-uuid))
41+
type (:type action)
42+
name (if (> 1 (count (:actions hook)))
43+
(str name "-" (inc i))
44+
name)]
45+
(on-before-action {:id id
46+
:name name})
47+
(if-let [[status output error] (run-hook-action! action name data db)]
48+
(on-after-action {:id id
49+
:name name
50+
:type type
51+
:status status
52+
:output output
53+
:error error})
54+
(on-after-action {:id id
55+
:name name
56+
:type type
57+
:status -1}))))
58+
(:actions hook)))))))

0 commit comments

Comments
 (0)