From e2a425c2b04fd5ea95667ec3cb5adea712b19bf0 Mon Sep 17 00:00:00 2001
From: Oleksandr Yakushev <alex@bytopia.org>
Date: Thu, 13 Mar 2025 23:04:11 +0200
Subject: [PATCH] [util] Add util/respond-to

---
 src/cider/nrepl/middleware/debug.clj       |  23 ++---
 src/cider/nrepl/middleware/info.clj        |  11 +--
 src/cider/nrepl/middleware/log.clj         |  16 ++-
 src/cider/nrepl/middleware/macroexpand.clj |   7 +-
 src/cider/nrepl/middleware/profile.clj     | 110 +++++++++------------
 src/cider/nrepl/middleware/refresh.clj     |  25 ++---
 src/cider/nrepl/middleware/reload.clj      |  17 ++--
 src/cider/nrepl/middleware/slurp.clj       |  11 +--
 src/cider/nrepl/middleware/stacktrace.clj  |  24 ++---
 src/cider/nrepl/middleware/test.clj        |  26 +++--
 src/cider/nrepl/middleware/util.clj        |   9 +-
 src/cider/nrepl/middleware/util/reload.clj |  50 ++++------
 src/cider/nrepl/middleware/version.clj     |   8 +-
 13 files changed, 137 insertions(+), 200 deletions(-)

diff --git a/src/cider/nrepl/middleware/debug.clj b/src/cider/nrepl/middleware/debug.clj
index aa0e3c6be..4aaa0ff1e 100644
--- a/src/cider/nrepl/middleware/debug.clj
+++ b/src/cider/nrepl/middleware/debug.clj
@@ -3,22 +3,20 @@
   {:author "Artur Malabarba"}
   (:require
    [cider.nrepl.middleware.inspect :refer [swap-inspector!]]
-   [cider.nrepl.middleware.util :as util]
+   [cider.nrepl.middleware.util :as util :refer [respond-to]]
    [cider.nrepl.middleware.util.cljs :as cljs]
    [cider.nrepl.middleware.util.instrument :as ins]
    [cider.nrepl.middleware.util.nrepl :refer [notify-client]]
    [nrepl.middleware.interruptible-eval :refer [*msg*]]
    [nrepl.middleware.print :as print]
-   [nrepl.misc :refer [response-for]]
-   [nrepl.transport :as transport]
    [orchard.info :as info]
    [orchard.inspect :as inspect]
    [orchard.meta :as m]
    [orchard.print]
    [orchard.stacktrace :as stacktrace])
   (:import
-   [clojure.lang Compiler$LocalBinding]
-   [java.util UUID]))
+   (clojure.lang Compiler$LocalBinding)
+   (java.util UUID)))
 
 ;;;; # The Debugger
 ;;;
@@ -135,7 +133,7 @@
   []
   (if (map? *msg*)
     (do
-      (transport/send (:transport *msg*) (response-for *msg* :value 'QUIT))
+      (respond-to *msg* :value 'QUIT)
       (.stop ^Thread (:thread (meta (:session *msg*)))))
     ;; We can't really abort if there's no *msg*, so we do our best
     ;; impression of that. This is only used in some panic situations,
@@ -200,8 +198,7 @@ this map (identified by a key), and will `dissoc` it afterwards."}
   (when (not @debugger-message)
     (throw (Exception. "Debugger not initialized!")))
   (try
-    (transport/send (:transport @debugger-message)
-                    (apply response-for @debugger-message r))
+    (apply respond-to @debugger-message r)
     (catch java.net.SocketException _
       (reset! debugger-message nil))))
 
@@ -710,12 +707,10 @@ this map (identified by a key), and will `dissoc` it afterwards."}
 (defn- instrumented-defs-reply
   "Reply to `msg` with an alist of instrumented defs on the \"list\" entry."
   [msg]
-  (->> (all-ns)
-       (map #(cons (ns-name %) (ins/list-instrumented-defs %)))
-       (filter second)
-       (util/transform-value)
-       (response-for msg :status :done :list)
-       (transport/send (:transport msg))))
+  (let [defs (->> (all-ns)
+                  (map #(cons (ns-name %) (ins/list-instrumented-defs %)))
+                  (filter second))]
+    (respond-to msg :status :done, :list (util/transform-value defs))))
 
 (defn handle-debug
   [handler {:keys [op input session] :as msg}]
diff --git a/src/cider/nrepl/middleware/info.clj b/src/cider/nrepl/middleware/info.clj
index 8c2832871..ff19600cf 100644
--- a/src/cider/nrepl/middleware/info.clj
+++ b/src/cider/nrepl/middleware/info.clj
@@ -2,12 +2,10 @@
   (:require
    [compliment.context]
    [compliment.sources.class-members]
-   [cider.nrepl.middleware.util :as util]
+   [cider.nrepl.middleware.util :as util :refer [respond-to]]
    [cider.nrepl.middleware.util.cljs :as cljs]
    [cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
    [clojure.string :as str]
-   [nrepl.transport :as transport]
-   [nrepl.misc :refer [response-for]]
    [orchard.eldoc :as eldoc]
    [orchard.info :as info]
    [orchard.java.source-files :as src-files]
@@ -89,14 +87,13 @@
   download an artifact that doesn't exist or can't be found."
   (ConcurrentHashMap.))
 
-(defn- download-sources-jar-for-class
-  [klass {:keys [transport] :as msg}]
+(defn- download-sources-jar-for-class [klass msg]
   (when-let [coords (src-files/infer-maven-coordinates-for-class klass)]
     (when (nil? (.putIfAbsent attempted-to-download-coords coords true))
       ;; Tell the client we are going to download an artifict so it can notify
       ;; the user. It may take a few seconds.
-      (transport/send transport (response-for msg {:status :download-sources-jar
-                                                   :coords coords}))
+      (respond-to msg {:status :download-sources-jar
+                       :coords coords})
       (src-files/download-sources-jar-for-coordinates coords))))
 
 (defn info
diff --git a/src/cider/nrepl/middleware/log.clj b/src/cider/nrepl/middleware/log.clj
index 8415021ce..1758a8605 100644
--- a/src/cider/nrepl/middleware/log.clj
+++ b/src/cider/nrepl/middleware/log.clj
@@ -3,12 +3,11 @@
   {:author "r0man"
    :added "0.32.0"}
   (:require [cider.nrepl.middleware.inspect :as middleware.inspect]
+            [cider.nrepl.middleware.util :refer [respond-to]]
             [cider.nrepl.middleware.util.error-handling :refer [with-safe-transport]]
             [logjam.event :as event]
             [logjam.framework :as framework]
-            [nrepl.middleware.print :as print]
-            [nrepl.misc :refer [response-for]]
-            [nrepl.transport :as transport])
+            [nrepl.middleware.print :as print])
   (:import (java.io StringWriter)
            (java.util UUID)))
 
@@ -139,16 +138,15 @@
 
 (defn add-consumer-reply
   "Add a consumer to an appender of a log framework."
-  [{:keys [consumer filters transport] :as msg}]
+  [{:keys [consumer filters] :as msg}]
   (let [appender (appender msg)
         consumer {:id (or consumer (str (UUID/randomUUID)))
                   :filters (or filters {})
                   :callback (fn [consumer event]
-                              (->> (response-for msg
-                                                 :cider/log-consumer (str (:id consumer))
-                                                 :cider/log-event (select-event event)
-                                                 :status :cider/log-event)
-                                   (transport/send transport)))}]
+                              (respond-to msg
+                                          :cider/log-consumer (str (:id consumer))
+                                          :cider/log-event (select-event event)
+                                          :status :cider/log-event))}]
     {:cider/log-add-consumer
      (-> (swap-framework! msg framework/add-consumer appender consumer)
          (framework/consumer appender consumer)
diff --git a/src/cider/nrepl/middleware/macroexpand.clj b/src/cider/nrepl/middleware/macroexpand.clj
index dbcaf9f98..4d536fcc9 100644
--- a/src/cider/nrepl/middleware/macroexpand.clj
+++ b/src/cider/nrepl/middleware/macroexpand.clj
@@ -2,11 +2,11 @@
   "Macroexpansion middleware."
   {:author "Bozhidar Batsov"}
   (:require
+   [cider.nrepl.middleware.util :refer [respond-to]]
    [cider.nrepl.middleware.util.cljs :as cljs]
    [cider.nrepl.middleware.util.error-handling
     :refer [base-error-response eval-interceptor-transport with-safe-transport]]
    [orchard.cljs.analysis :as cljs-ana]
-   [nrepl.misc :refer [response-for]]
    [nrepl.transport :as transport]
    [clojure.pprint :as pp]
    [clojure.tools.reader :as reader]
@@ -124,12 +124,11 @@
   (transport/send (:transport msg)
                   (base-error-response msg ex :done :macroexpand-error)))
 
-(defn macroexpansion-reply-clj [{:keys [transport] :as msg}
-                                {:keys [value] :as resp}]
+(defn macroexpansion-reply-clj [msg {:keys [value] :as resp}]
   (try (let [msg (update msg :ns #(or (misc/as-sym %) 'user))
              expansion (walk/prewalk (post-expansion-walker-clj msg) value)
              response-map (macroexpansion-response-map msg expansion)]
-         (transport/send transport (response-for msg response-map)))
+         (respond-to msg response-map))
        (catch Exception ex
          (send-middleware-error msg ex))))
 
diff --git a/src/cider/nrepl/middleware/profile.clj b/src/cider/nrepl/middleware/profile.clj
index 486c71047..ed66cf43d 100644
--- a/src/cider/nrepl/middleware/profile.clj
+++ b/src/cider/nrepl/middleware/profile.clj
@@ -15,30 +15,25 @@
   maintained anymore."
   {:author "Edwin Watkeys"}
   (:require
-   [nrepl.misc :refer [response-for]]
-   [nrepl.transport :as t]
+   [cider.nrepl.middleware.util :refer [respond-to]]
    [profile.core :as p]))
 
-(defn send-exception
-  [_e msg transport]
-  (t/send transport (response-for msg :status :done :value "exception")))
+(defn- send-exception
+  [_e msg]
+  (respond-to msg :status :done :value "exception"))
 
 (defn toggle-profile
   [{:keys [ns sym transport] :as msg}]
   (try
     (if-let [v (ns-resolve (symbol ns) (symbol sym))]
       (let [profiled? (p/toggle-profile-var* v)]
-        (t/send transport
-                (response-for
-                 msg
-                 :status :done
-                 :value (if profiled? "profiled" "unprofiled"))))
-      (t/send transport
-              (response-for
-               msg
-               :status #{:toggle-profile-not-such-var :done}
-               :value "unbound")))
-    (catch Exception e (send-exception e msg transport))))
+        (respond-to msg
+                    :status :done
+                    :value (if profiled? "profiled" "unprofiled")))
+      (respond-to msg
+                  :status #{:toggle-profile-not-such-var :done}
+                  :value "unbound"))
+    (catch Exception e (send-exception e msg))))
 
 (defn profile-var-summary
   [{:keys [ns sym transport] :as msg}]
@@ -46,69 +41,58 @@
     (if-let [v (ns-resolve (symbol ns) (symbol sym))]
       (if-let [table (with-out-str (binding [*err* *out*]
                                      (p/print-entry-summary v)))]
-        (t/send transport
-                (response-for msg
-                              :status :done
-                              :err table))
-        (t/send transport
-                (response-for msg
-                              :status :done
-                              :err (format "No profile data for %s." v))))
-      (t/send transport
-              (response-for msg
-                            :status :done
-                            :value (format "Var %s/%s is not bound." ns sym))))
-    (catch Exception e (prn :e e) (send-exception e msg transport))))
+        (respond-to msg
+                    :status :done
+                    :err table)
+        (respond-to msg
+                    :status :done
+                    :err (format "No profile data for %s." v)))
+      (respond-to msg
+                  :status :done
+                  :value (format "Var %s/%s is not bound." ns sym)))
+    (catch Exception e (prn :e e) (send-exception e msg))))
 
 (defn profile-summary
   [{:keys [transport] :as msg}]
   (try
-    (t/send transport
-            (response-for msg
-                          :status :done
-                          :err (with-out-str
-                                 (binding [*err* *out*] (p/print-summary)))))
-    (catch Exception e (send-exception e msg transport))))
+    (respond-to msg
+                :status :done
+                :err (with-out-str
+                       (binding [*err* *out*] (p/print-summary))))
+    (catch Exception e (send-exception e msg))))
 
 (defn clear-profile
   [{:keys [transport] :as msg}]
   (try
     (p/clear-profile-data)
-    (t/send transport
-            (response-for msg
-                          :status :done
-                          :value "cleared"))
-    (catch Exception e (send-exception e msg transport))))
+    (respond-to msg
+                :status :done
+                :value "cleared")
+    (catch Exception e (send-exception e msg))))
 
 (defn toggle-profile-ns
   [{:keys [ns transport] :as msg}]
   (try (let [profiled? (p/toggle-profile-ns (symbol ns))]
-         (t/send transport
-                 (response-for
-                  msg
-                  :status :done
-                  :value (if profiled? "profiled" "unprofiled"))))
-       (catch Exception e (send-exception e msg transport))))
+         (respond-to msg
+                     :status :done
+                     :value (if profiled? "profiled" "unprofiled")))
+       (catch Exception e (send-exception e msg))))
 
 (defn is-var-profiled
   [{:keys [ns sym transport] :as msg}]
   (try (let [var (ns-resolve (symbol ns) (symbol sym))
              profiled? (p/profiled? @var)]
-         (t/send transport
-                 (response-for
-                  msg
-                  :status :done
-                  :value (if profiled? "profiled" "unprofiled"))))
-       (catch Exception e (send-exception e msg transport))))
+         (respond-to msg
+                     :status :done
+                     :value (if profiled? "profiled" "unprofiled")))
+       (catch Exception e (send-exception e msg))))
 
 (defn get-max-samples
   [{:keys [transport] :as msg}]
-  (try (t/send transport
-               (response-for
-                msg
-                :status :done
-                :value (str (p/max-sample-count))))
-       (catch Exception e (send-exception e msg transport))))
+  (try (respond-to msg
+                   :status :done
+                   :value (str (p/max-sample-count)))
+       (catch Exception e (send-exception e msg))))
 
 (defn normalize-max-samples [n]
   (cond (and (sequential? n) (empty? n)) nil
@@ -119,12 +103,10 @@
   [{:keys [max-samples transport] :as msg}]
   (try (let [max-samples (normalize-max-samples max-samples)]
          (p/set-max-sample-count max-samples)
-         (t/send transport
-                 (response-for
-                  msg
-                  :status :done
-                  :value (str (p/max-sample-count)))))
-       (catch Exception e  (send-exception e msg transport))))
+         (respond-to msg
+                     :status :done
+                     :value (str (p/max-sample-count))))
+       (catch Exception e  (send-exception e msg))))
 
 (defn handle-profile
   [handler msg]
diff --git a/src/cider/nrepl/middleware/refresh.clj b/src/cider/nrepl/middleware/refresh.clj
index 58287438c..17de9fcec 100644
--- a/src/cider/nrepl/middleware/refresh.clj
+++ b/src/cider/nrepl/middleware/refresh.clj
@@ -5,14 +5,13 @@
   ;; when developing cider-nrepl itself, or when cider-nrepl is used as a
   ;; checkout dependency - tools.namespace doesn't reload source in JARs.
   (:require
+   [cider.nrepl.middleware.util :refer [respond-to]]
    [cider.nrepl.middleware.util.reload :as reload-utils]
    [clojure.main :refer [repl-caught]]
    [clojure.tools.namespace.dir :as dir]
    [clojure.tools.namespace.find :as find]
    [clojure.tools.namespace.reload :as reload]
-   [clojure.tools.namespace.track :as track]
-   [nrepl.misc :refer [response-for]]
-   [nrepl.transport :as transport]))
+   [clojure.tools.namespace.track :as track]))
 
 (defonce ^:private refresh-tracker (volatile! (track/tracker)))
 
@@ -55,21 +54,17 @@
 
 (defn- reloading-reply
   [{reloading ::track/load}
-   {:keys [transport] :as msg}]
-  (transport/send
-   transport
-   (response-for msg {:reloading reloading})))
+   msg]
+  (respond-to msg :reloading reloading))
 
 (defn- result-reply
   [{error ::reload/error
     error-ns ::reload/error-ns}
-   {:keys [transport] :as msg}]
+   msg]
 
   (if error
     (reload-utils/error-reply {:error error :error-ns error-ns} msg)
-    (transport/send
-     transport
-     (response-for msg {:status :ok}))))
+    (respond-to msg :status :ok)))
 
 (defn after-reply
   [{error ::reload/error}
@@ -81,7 +76,7 @@
   (atom false))
 
 (defn- refresh-reply
-  [{:keys [dirs transport session id] :as msg}]
+  [{:keys [dirs session id] :as msg}]
   (let [{:keys [exec]} (meta session)]
     (exec id
           (fn []
@@ -109,10 +104,10 @@
                           (finally
                             (reset! client-requested-clear? false)))))))
           (fn []
-            (transport/send transport (response-for msg {:status :done}))))))
+            (respond-to msg :status :done)))))
 
 (defn- clear-reply
-  [{:keys [transport session id] :as msg}]
+  [{:keys [session id] :as msg}]
   (let [{:keys [exec]} (meta session)]
     (exec id
           (fn []
@@ -121,7 +116,7 @@
             ;; because that `locking` could cause unnecessary nREPL timeouts (https://github.com/clojure-emacs/cider/issues/3652 ).
             (reset! client-requested-clear? true))
           (fn []
-            (transport/send transport (response-for msg {:status :done}))))))
+            (respond-to msg :status :done)))))
 
 (defn handle-refresh [handler msg]
   (case (:op msg)
diff --git a/src/cider/nrepl/middleware/reload.clj b/src/cider/nrepl/middleware/reload.clj
index dfdb05fe8..ce60a27c2 100644
--- a/src/cider/nrepl/middleware/reload.clj
+++ b/src/cider/nrepl/middleware/reload.clj
@@ -4,13 +4,12 @@
   of tools.namespace."
   (:require
    [cider.nrepl.middleware.util.reload :as reload-utils]
+   [cider.nrepl.middleware.util :refer [respond-to]]
    [clj-reload.core :as reload]
    [clojure.main :refer [repl-caught]]
    [clojure.string :as str]
    [nrepl.middleware.interruptible-eval :refer [*msg*]]
    [nrepl.middleware.print :as print]
-   [nrepl.misc :refer [response-for]]
-   [nrepl.transport :as transport]
    [orchard.stacktrace :as stacktrace]))
 
 (defn- user-reload
@@ -26,14 +25,10 @@
   [dirs]
   (reload/init {:dirs dirs}))
 
-(defn respond
-  [{:keys [transport] :as msg} response]
-  (transport/send transport (response-for msg response)))
-
 (defn operation
   [msg]
   (let [opts   {:log-fn (fn [& args]
-                          (respond msg {:progress (str/join " " args)}))
+                          (respond-to msg {:progress (str/join " " args)}))
                 :throw false} ;; mimic the tools.namespace behavior so that we can use `reload-utils/after-reply` uniformly
         reload (user-reload 'reload reload/reload)
         unload (user-reload 'unload reload/unload)]
@@ -53,15 +48,15 @@
                 (reload-utils/after-reply exception msg)
                 (when exception
                   (throw exception))
-                (respond msg {:status :ok}))
+                (respond-to msg {:status :ok}))
               (catch Throwable error
-                (respond msg {:status :error
-                              :error  (stacktrace/analyze error print-fn)})
+                (respond-to msg {:status :error
+                                 :error  (stacktrace/analyze error print-fn)})
                 (binding [*msg* msg
                           *err* (print/replying-PrintWriter :err msg {})]
                   (repl-caught error)))))
 
-          (fn [] (respond msg {:status :done})))))
+          (fn [] (respond-to msg {:status :done})))))
 
 (defn handle-reload [handler msg]
   (case (:op msg)
diff --git a/src/cider/nrepl/middleware/slurp.clj b/src/cider/nrepl/middleware/slurp.clj
index 077e36244..c12a92449 100644
--- a/src/cider/nrepl/middleware/slurp.clj
+++ b/src/cider/nrepl/middleware/slurp.clj
@@ -5,11 +5,10 @@
   convert URLs to values which can be handled nicely."
   {:authors ["Reid 'arrdem' McKenzie <me@arrdem.com>"]}
   (:require
+   [cider.nrepl.middleware.util :refer [respond-to]]
    [clojure.edn :as edn]
    [clojure.java.io :as io]
-   [clojure.string :as str]
-   [nrepl.misc :refer [response-for]]
-   [nrepl.transport :as transport])
+   [clojure.string :as str])
   (:import
    (java.io ByteArrayOutputStream FileNotFoundException InputStream)
    (java.net MalformedURLException URI URL URLConnection)
@@ -120,8 +119,6 @@
   [handler msg]
   (let [{:keys [op url transport]} msg]
     (if (and (= "slurp" op) url)
-      (do (transport/send transport
-                          (response-for msg (slurp-url-to-content+body url)))
-          (transport/send transport
-                          (response-for msg {:status ["done"]})))
+      (do (respond-to msg (slurp-url-to-content+body url))
+          (respond-to msg :status :done))
       (handler msg))))
diff --git a/src/cider/nrepl/middleware/stacktrace.clj b/src/cider/nrepl/middleware/stacktrace.clj
index 5821e4fc8..5178a1e11 100644
--- a/src/cider/nrepl/middleware/stacktrace.clj
+++ b/src/cider/nrepl/middleware/stacktrace.clj
@@ -3,27 +3,17 @@
   {:author "Jeff Valk"}
   (:require
    [cider.nrepl.middleware.inspect :as middleware.inspect]
+   [cider.nrepl.middleware.util :refer [respond-to]]
    [cider.nrepl.middleware.util.nrepl :refer [notify-client]]
    [nrepl.middleware.print :as print]
-   [nrepl.misc :refer [response-for]]
    [nrepl.transport :as t]
    [orchard.stacktrace :as stacktrace]))
 
-(defn- done
-  "Send the done response to the client."
-  [{:keys [transport] :as msg}]
-  (t/send transport (response-for msg :status :done)))
-
-(defn- no-error
-  "Send the no error response to the client."
-  [{:keys [transport] :as msg}]
-  (t/send transport (response-for msg :status :no-error)))
-
 (defn- send-analysis
   "Send the stacktrace analysis response to the client."
-  [{:keys [transport] :as msg} analysis]
+  [msg analysis]
   (doseq [cause analysis]
-    (t/send transport (response-for msg cause))))
+    (respond-to msg cause)))
 
 ;; Analyze the last stacktrace
 
@@ -41,8 +31,8 @@
   [{:keys [session] :as msg}]
   (if (@session #'*e)
     (analyze-last-stacktrace msg)
-    (no-error msg))
-  (done msg))
+    (respond-to msg :status :no-error))
+  (respond-to msg :status :done))
 
 ;; Stacktrace
 
@@ -60,8 +50,8 @@
                 (nth causes index nil))]
     (if cause
       (t/send transport (middleware.inspect/inspect-reply* msg cause))
-      (no-error msg))
-    (done msg)))
+      (respond-to msg :status :no-error))
+    (respond-to msg :status :done)))
 
 (defn handle-stacktrace
   "Handle stacktrace ops."
diff --git a/src/cider/nrepl/middleware/test.clj b/src/cider/nrepl/middleware/test.clj
index 498e6254d..5d2ce1627 100644
--- a/src/cider/nrepl/middleware/test.clj
+++ b/src/cider/nrepl/middleware/test.clj
@@ -3,7 +3,7 @@
   {:author "Jeff Valk"}
   (:require
    [cider.nrepl.middleware.test.extensions :as extensions]
-   [cider.nrepl.middleware.util :as util]
+   [cider.nrepl.middleware.util :as util :refer [respond-to]]
    [cider.nrepl.middleware.util.coerce :as util.coerce]
    [clojure.pprint :as pp]
    [clojure.string :as str]
@@ -11,8 +11,6 @@
    [clojure.walk :as walk]
    [nrepl.middleware.interruptible-eval :as ie]
    [nrepl.middleware.print :as print]
-   [nrepl.misc :refer [response-for]]
-   [nrepl.transport :as t]
    [orchard.misc :as misc]
    [orchard.query :as query]
    [orchard.stacktrace :as stacktrace]))
@@ -409,7 +407,7 @@
   (atom {}))
 
 (defn handle-test-var-query-op
-  [{:keys [fail-fast var-query transport session id] :as msg}]
+  [{:keys [fail-fast var-query session id] :as msg}]
   (let [fail-fast? (= "true" fail-fast)
         {:keys [exec]} (meta session)]
     (exec id
@@ -429,15 +427,15 @@
                                  (test-var-query fail-fast?)
                                  stringify-msg)]
                   (reset! results (:results report))
-                  (t/send transport (response-for msg (util/transform-value report))))
+                  (respond-to msg (util/transform-value report)))
                 (catch clojure.lang.ExceptionInfo e
                   (let [d (ex-data e)]
                     (if (::util.coerce/id d)
                       (case (::util.coerce/id d)
-                        :namespace-not-found (t/send transport (response-for msg :status :namespace-not-found)))
+                        :namespace-not-found (respond-to msg :status :namespace-not-found))
                       (throw e)))))))
           (fn []
-            (t/send transport (response-for msg {:status :done}))))))
+            (respond-to msg :status :done)))))
 
 (defn handle-test-op
   [{:keys [ns tests include exclude] :as msg}]
@@ -456,7 +454,7 @@
                            :exclude-meta-key exclude}})))
 
 (defn handle-retest-op
-  [{:keys [transport session id fail-fast] :as msg}]
+  [{:keys [session id fail-fast] :as msg}]
   (let [{:keys [exec]} (meta session)]
     (exec id
           (fn []
@@ -471,12 +469,12 @@
                                 {} @results)
                     report (test-nss nss (= "true" fail-fast))]
                 (reset! results (:results report))
-                (t/send transport (response-for msg (util/transform-value report))))))
+                (respond-to msg (util/transform-value report)))))
           (fn []
-            (t/send transport (response-for msg :status :done))))))
+            (respond-to msg :status :done)))))
 
 (defn handle-stacktrace-op
-  [{:keys [ns var index transport session id ::print/print-fn] :as msg}]
+  [{:keys [ns var index session id ::print/print-fn] :as msg}]
   (let [{:keys [exec]} (meta session)]
     (exec id
           (fn []
@@ -484,10 +482,10 @@
               (let [[ns var] (map misc/as-sym [ns var])]
                 (if-let [e (get-in @results [ns var index :error])]
                   (doseq [cause (stacktrace/analyze e print-fn)]
-                    (t/send transport (response-for msg cause)))
-                  (t/send transport (response-for msg :status :no-error))))))
+                    (respond-to msg cause))
+                  (respond-to msg :status :no-error)))))
           (fn []
-            (t/send transport (response-for msg :status :done))))))
+            (respond-to msg :status :done)))))
 
 (defn handle-test [handler msg & _configuration]
   (case (:op msg)
diff --git a/src/cider/nrepl/middleware/util.clj b/src/cider/nrepl/middleware/util.clj
index 071c64a79..f423ee9db 100644
--- a/src/cider/nrepl/middleware/util.clj
+++ b/src/cider/nrepl/middleware/util.clj
@@ -1,5 +1,7 @@
 (ns cider.nrepl.middleware.util
-  "Utility functions that might be useful in middleware.")
+  "Utility functions that might be useful in middleware."
+  (:require [nrepl.transport :as transport]
+            [nrepl.misc :refer [response-for]]))
 
 (defmulti transform-value "Transform a value for output" type)
 
@@ -42,3 +44,8 @@
 
 ;; handles vectors
 (prefer-method transform-value clojure.lang.Sequential clojure.lang.Associative)
+
+(defn respond-to
+  "Send a response for `msg` with `response-data` using message's transport."
+  [msg & response-data]
+  (transport/send (:transport msg) (apply response-for msg response-data)))
diff --git a/src/cider/nrepl/middleware/util/reload.clj b/src/cider/nrepl/middleware/util/reload.clj
index 16e9eddf9..319911cb9 100644
--- a/src/cider/nrepl/middleware/util/reload.clj
+++ b/src/cider/nrepl/middleware/util/reload.clj
@@ -2,23 +2,20 @@
   "Common parts for the code-reloading middleware namespaces."
   {:added "0.47.0"}
   (:require
+   [cider.nrepl.middleware.util :refer [respond-to]]
    [clojure.main :refer [repl-caught]]
    [nrepl.middleware.interruptible-eval :refer [*msg*]]
    [nrepl.middleware.print :as print]
-   [nrepl.misc :refer [response-for]]
-   [nrepl.transport :as transport]
    [orchard.misc :as misc]
    [orchard.stacktrace :as stacktrace]))
 
 (defn error-reply
   [{:keys [error error-ns]}
-   {:keys [::print/print-fn transport] :as msg}]
+   {:keys [::print/print-fn] :as msg}]
 
-  (transport/send
-   transport
-   (response-for msg (cond-> {:status :error}
-                       error (assoc :error (stacktrace/analyze error print-fn))
-                       error-ns (assoc :error-ns error-ns))))
+  (respond-to msg (cond-> {:status :error}
+                    error (assoc :error (stacktrace/analyze error print-fn))
+                    error-ns (assoc :error-ns error-ns)))
 
   (binding [*msg* msg
             *err* (print/replying-PrintWriter :err msg {})]
@@ -49,38 +46,29 @@
         (@the-var))
       (var? the-var))))
 
-(defn before-reply [{:keys [before transport] :as msg}]
+(defn before-reply [{:keys [before] :as msg}]
   (when before
-    (transport/send
-     transport
-     (response-for msg {:status :invoking-before
-                        :before before}))
+    (respond-to msg {:status :invoking-before
+                     :before before})
 
     (let [resolved? (resolve-and-invoke before msg)]
-      (transport/send
-       transport
-       (response-for msg
-                     {:status (if resolved?
-                                :invoked-before
-                                :invoked-not-resolved)
-                      :before before})))))
+      (respond-to msg {:status (if resolved?
+                                 :invoked-before
+                                 :invoked-not-resolved)
+                       :before before}))))
 
 (defn after-reply [error
-                   {:keys [after transport] :as msg}]
+                   {:keys [after] :as msg}]
   (when (and (not error) after)
     (try
-      (transport/send
-       transport
-       (response-for msg {:status :invoking-after
-                          :after after}))
+      (respond-to msg {:status :invoking-after
+                       :after after})
 
       (let [resolved? (resolve-and-invoke after msg)]
-        (transport/send
-         transport
-         (response-for msg {:status (if resolved?
-                                      :invoked-after
-                                      :invoked-not-resolved)
-                            :after after})))
+        (respond-to msg {:status (if resolved?
+                                   :invoked-after
+                                   :invoked-not-resolved)
+                         :after after}))
 
       (catch Exception e
         (error-reply {:error e} msg)))))
diff --git a/src/cider/nrepl/middleware/version.clj b/src/cider/nrepl/middleware/version.clj
index 29b558536..7d916130e 100644
--- a/src/cider/nrepl/middleware/version.clj
+++ b/src/cider/nrepl/middleware/version.clj
@@ -2,13 +2,9 @@
   "Return version info of the CIDER-nREPL middleware itself."
   (:require
    [cider.nrepl.version :as version]
-   [nrepl.misc :refer [response-for]]
-   [nrepl.transport :as transport]))
+   [cider.nrepl.middleware.util :refer [respond-to]]))
 
 (defn handle-version [handler msg]
   (if (= (:op msg) "cider-version")
-    (->> {:cider-version version/version}
-         (merge {:status #{"done"}})
-         (response-for msg)
-         (transport/send (:transport msg)))
+    (respond-to msg {:status :done, :cider-version version/version})
     (handler msg)))