Skip to content

Commit 0dafafc

Browse files
author
Yetibot
committed
fix(agent): restrict channel context to thread channels only
1 parent f0bbb61 commit 0dafafc

2 files changed

Lines changed: 26 additions & 8 deletions

File tree

src/yetibot/core/commands/agent.clj

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,17 @@
573573
whole thread lets a follow-up like \"retry\" resolve to the original ask."
574574
[channel-id]
575575
(try
576-
(let [topic (try (:name @(discord/get-channel! (rest-conn) channel-id))
577-
(catch Exception _ nil))
578-
lines (->> (all-channel-messages channel-id)
579-
(sort-by :timestamp)
580-
(map (fn [m] (str (get-in m [:author :username]) ": " (:content m))))
581-
(remove string/blank?))]
582-
(string/join "\n" (cond->> lines
583-
(not (string/blank? topic)) (cons (str "[thread topic] " topic)))))
576+
(let [channel @(discord/get-channel! (rest-conn) channel-id)
577+
type (:type channel)]
578+
(if (not (#{10 11 12} type))
579+
""
580+
(let [topic (:name channel)
581+
lines (->> (all-channel-messages channel-id)
582+
(sort-by :timestamp)
583+
(map (fn [m] (str (get-in m [:author :username]) ": " (:content m))))
584+
(remove string/blank?))]
585+
(string/join "\n" (cond->> lines
586+
(not (string/blank? topic)) (cons (str "[thread topic] " topic)))))))
584587
(catch Exception e (debug "thread-context failed:" (.getMessage e)) "")))
585588

586589
;; ---------------------------------------------------------------------------

test/yetibot/core/test/commands/agent.clj

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,21 @@
184184
(fact "keeps the original request text"
185185
(agent/resume-request "add a bagif command") => (contains "add a bagif command")))
186186

187+
(facts "about thread-context"
188+
(fact "returns empty string if the channel is not a thread (type is not 10, 11, or 12)"
189+
(#'agent/thread-context "channel-id-123") => ""
190+
(provided
191+
(#'agent/rest-conn) => "mock-conn"
192+
(discljord.messaging/get-channel! "mock-conn" "channel-id-123") => (atom {:type 0 :name "general"})))
193+
194+
(fact "returns thread context if the channel is a thread (type 11)"
195+
(#'agent/thread-context "thread-id-456") => "[thread topic] cool-thread\nalice: hello\nbob: world"
196+
(provided
197+
(#'agent/rest-conn) => "mock-conn"
198+
(discljord.messaging/get-channel! "mock-conn" "thread-id-456") => (atom {:type 11 :name "cool-thread"})
199+
(#'agent/all-channel-messages "thread-id-456") => [{:author {:username "alice"} :content "hello" :timestamp 1}
200+
{:author {:username "bob"} :content "world" :timestamp 2}])))
201+
187202
(facts "about agent subcommands"
188203
(fact "agent-list-commands-cmd returns available commands in JSON"
189204
(agent/agent-list-commands-cmd {}) => {:result/value "{\"commands\":[\"cmd1\",\"cmd2\"]}"}

0 commit comments

Comments
 (0)