Skip to content

Commit 975462c

Browse files
committed
Deprecate prePrompt and postPrompt in favor of preRequest and prePrompt.
1 parent e06470a commit 975462c

File tree

5 files changed

+72
-36
lines changed

5 files changed

+72
-36
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- Deprecate prePrompt and postPrompt in favor of preRequest and prePrompt.
6+
57
## 0.70.2
68

79
- Fix model capabilities for models with custom names.

docs/configuration.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,8 +359,8 @@ Hooks are actions that can run before or after an specific event, useful to noti
359359

360360
Allowed hook types:
361361

362-
- `prePrompt`: Run before prompt is sent to LLM, if a hook output is provided, append to user prompt.
363-
- `postPrompt`: Run after prompt is finished, when chat come back to idle state.
362+
- `preRequest`: Run before prompt is sent to LLM, if a hook output is provided, append to user prompt.
363+
- `postRequest`: Run after prompt is finished, when chat come back to idle state.
364364
- `preToolCall`: Run before a tool is called, if a hook exit with status `2`, reject the tool call.
365365
- `postToolCall`: Run after a tool was called.
366366

@@ -378,7 +378,7 @@ Examples:
378378
{
379379
"hooks": {
380380
"notify-me": {
381-
"type": "postPrompt",
381+
"type": "postRequest",
382382
"actions": [
383383
{
384384
"type": "shell",
@@ -444,7 +444,7 @@ To configure, add your OTLP collector config via `:otlp` map following [otlp aut
444444
}};
445445
defaultModel?: string;
446446
hooks?: {[key: string]: {
447-
type: 'preToolCall' | 'postToolCall' | 'preToolCallApproval' | 'prePrompt' | 'postPrompt';
447+
type: 'preToolCall' | 'postToolCall' | 'preRequest' | 'postRequest';
448448
matcher: string;
449449
actions: {
450450
type: 'shell';

src/eca/features/chat.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353

5454
(defn finish-chat-prompt! [status {:keys [message chat-id db* metrics config on-finished-side-effect] :as chat-ctx}]
5555
(swap! db* assoc-in [:chats chat-id :status] status)
56-
(f.hooks/trigger-if-matches! :postPrompt
56+
(f.hooks/trigger-if-matches! :postRequest
5757
{:chat-id chat-id
5858
:prompt message}
5959
{:on-before-action (partial notify-before-hook-action! chat-ctx)
@@ -962,7 +962,7 @@
962962
:messenger messenger}
963963
decision (message->decision message)
964964
hook-outputs* (atom [])
965-
_ (f.hooks/trigger-if-matches! :prePrompt
965+
_ (f.hooks/trigger-if-matches! :preRequest
966966
{:chat-id chat-id
967967
:prompt message}
968968
{:on-before-action (partial notify-before-hook-action! chat-ctx)

src/eca/features/hooks.clj

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,21 @@
88
(def ^:private logger-tag "[HOOK]")
99

1010
(defn ^:private hook-matches? [type data hook]
11-
(case type
12-
(:preToolCall :postToolCall)
13-
(re-matches (re-pattern (or (:matcher hook) ".*"))
14-
(str (:server data) "__" (:tool-name data)))
11+
(let [hook-config-type (keyword (:type hook))
12+
hook-config-type (cond ;; legacy values
13+
(= :prePrompt hook-config-type) :preRequest
14+
(= :postPrompt hook-config-type) :postRequest
15+
:else hook-config-type)]
16+
(cond
17+
(not= type hook-config-type)
18+
false
1519

16-
true))
20+
(contains? #{:preToolCall :postToolCall} type)
21+
(re-matches (re-pattern (or (:matcher hook) ".*"))
22+
(str (:server data) "__" (:tool-name data)))
23+
24+
:else
25+
true)))
1726

1827
(defn ^:private run-hook-action! [action name data db]
1928
(case (:type action)
@@ -39,26 +48,25 @@
3948
db
4049
config]
4150
(doseq [[name hook] (:hooks config)]
42-
(when (= type (keyword (:type hook)))
43-
(when (hook-matches? type data hook)
44-
(vec
45-
(map-indexed (fn [i action]
46-
(let [id (str (random-uuid))
47-
type (:type action)
48-
name (if (> 1 (count (:actions hook)))
49-
(str name "-" (inc i))
50-
name)]
51-
(on-before-action {:id id
52-
:name name})
53-
(if-let [[status output error] (run-hook-action! action name data db)]
54-
(on-after-action {:id id
55-
:name name
56-
:type type
57-
:status status
58-
:output output
59-
:error error})
60-
(on-after-action {:id id
61-
:name name
62-
:type type
63-
:status -1}))))
64-
(:actions hook)))))))
51+
(when (hook-matches? type data hook)
52+
(vec
53+
(map-indexed (fn [i action]
54+
(let [id (str (random-uuid))
55+
type (:type action)
56+
name (if (> 1 (count (:actions hook)))
57+
(str name "-" (inc i))
58+
name)]
59+
(on-before-action {:id id
60+
:name name})
61+
(if-let [[status output error] (run-hook-action! action name data db)]
62+
(on-after-action {:id id
63+
:name name
64+
:type type
65+
:status status
66+
:output output
67+
:error error})
68+
(on-after-action {:id id
69+
:name name
70+
:type type
71+
:status -1}))))
72+
(:actions hook))))))

test/eca/features/hooks_test.clj

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
(reset! a* p)))
1414

1515
(deftest trigger-if-matches!-test
16-
(testing "prePrompt"
16+
(testing "legacy prePrompt"
1717
(h/reset-components!)
1818
(h/config! {:hooks {"my-hook" {:type "prePrompt"
1919
:actions [{:type "shell"
@@ -22,7 +22,33 @@
2222
on-after-action* (atom nil)]
2323
(with-redefs [p/sh (constantly {:exit 0 :out "hey" :err nil})]
2424
(f.hooks/trigger-if-matches!
25-
:prePrompt
25+
:preRequest
26+
{:foo "1"}
27+
{:on-before-action (set-action-payload on-before-action*)
28+
:on-after-action (set-action-payload on-after-action*)}
29+
(h/db)
30+
(h/config)))
31+
(is (match?
32+
{:id string?
33+
:name "my-hook"}
34+
@on-before-action*))
35+
(is (match?
36+
{:id string?
37+
:name "my-hook"
38+
:status 0
39+
:output "hey"
40+
:error nil}
41+
@on-after-action*))))
42+
(testing "preRequest"
43+
(h/reset-components!)
44+
(h/config! {:hooks {"my-hook" {:type "preRequest"
45+
:actions [{:type "shell"
46+
:shell "echo hey"}]}}})
47+
(let [on-before-action* (atom nil)
48+
on-after-action* (atom nil)]
49+
(with-redefs [p/sh (constantly {:exit 0 :out "hey" :err nil})]
50+
(f.hooks/trigger-if-matches!
51+
:preRequest
2652
{:foo "1"}
2753
{:on-before-action (set-action-payload on-before-action*)
2854
:on-after-action (set-action-payload on-after-action*)}

0 commit comments

Comments
 (0)