Skip to content

Commit 7428fdf

Browse files
committed
add change suggested on code review
1 parent 15665d9 commit 7428fdf

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

deps.edn

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@
9595
:exec-args {:port 7888 :shadow-build "app" :shadow-port 7889}}
9696

9797
:nrepl {:extra-paths ["test" "dev"]
98-
:extra-deps {nrepl/nrepl {:mvn/version "1.3.1"}}
9998
:main-opts ["-m" "nrepl.cmdline" "--port" "7888"]}
10099

101100
:dkr-nrepl {:extra-paths ["test" "dev"]

src/clojure_mcp/agent/general_agent.clj

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns clojure-mcp.agent.general-agent
22
"A generalized agent library with system prompts, context, tools, memory, and models."
33
(:require [clojure.string :as string]
4+
[clojure.set :as set]
45
[taoensso.timbre :as log]
56
[clojure.java.io :as io]
67
[clojure-mcp.agent.langchain :as chain]
@@ -12,6 +13,10 @@
1213
(def DEFAULT-STATELESS-BUFFER 100)
1314
(def MIN-PERSISTENT-WINDOW 10)
1415

16+
(def MEMORY-RESET-BUFFER
17+
"Buffer size before triggering memory reset to prevent overflow."
18+
15)
19+
1520
(defn build-read-only-tools
1621
"Builds the read-only tools for agents.
1722
These tools are safe for exploration and analysis without modifying files.
@@ -138,7 +143,7 @@ Please use it to inform you as to which files should be investigated.\n=========
138143
139144
Returns: The memory object (possibly reset)"
140145
[memory context memory-size]
141-
(if (> (chain/memory-count memory) (- memory-size 15))
146+
(if (> (chain/memory-count memory) (- memory-size MEMORY-RESET-BUFFER))
142147
(do
143148
(chain/memory-clear! memory)
144149
(initialize-memory-with-context! memory context))
@@ -258,5 +263,6 @@ Please use it to inform you as to which files should be investigated.\n=========
258263
[agent new-tools]
259264
(create-general-agent
260265
(-> agent
261-
(select-keys [:system-prompt :context :model :memory-size])
266+
(select-keys [:system-message :context :model :memory-size])
267+
(set/rename-keys {:system-message :system-prompt})
262268
(assoc :tools (vec (concat (:tools agent) new-tools))))))

src/clojure_mcp/agent/langchain.clj

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@
6969
;; Tool conversion
7070

7171
(defn- adapt-tool-fn
72-
"Adapts clojure-mcp callback-style tool to langchain4clj return style."
72+
"Adapts clojure-mcp callback-style tool to langchain4clj return style.
73+
Includes a 5-minute timeout to prevent indefinite blocking."
7374
[callback-style-fn]
7475
(fn [args]
7576
(let [result-promise (promise)]
@@ -83,7 +84,10 @@
8384
(if (sequential? result)
8485
(string/join "\n\n" result)
8586
(str result))))))
86-
@result-promise)))
87+
(let [result (deref result-promise 300000 ::timeout)]
88+
(if (= result ::timeout)
89+
"Tool Error: Execution timed out after 5 minutes"
90+
result)))))
8791

8892
(defn- registration-map->tool-map
8993
"Converts clojure-mcp tool registration map to langchain4clj tool map format."
@@ -127,13 +131,23 @@
127131

128132
;; Assistant creation
129133

130-
(defn create-assistant-fn [{:keys [model memory tools system-message]}]
134+
(defn create-assistant-fn
135+
"Creates an assistant function with the given configuration.
136+
137+
Options:
138+
- :model - The LLM model to use
139+
- :memory - Chat memory instance
140+
- :tools - Sequence of tool registration maps
141+
- :system-message - System message for the assistant
142+
- :max-iterations - Maximum tool iterations (default: 10)"
143+
[{:keys [model memory tools system-message max-iterations]
144+
:or {max-iterations 10}}]
131145
(assistant/create-assistant
132146
{:model model
133147
:memory memory
134148
:tools (when (seq tools) (convert-tools tools))
135149
:system-message system-message
136-
:max-iterations 10}))
150+
:max-iterations max-iterations}))
137151

138152
;; Message helpers
139153

@@ -143,6 +157,4 @@
143157
(defn system-message [text]
144158
(messages/edn->message {:type :system :text text}))
145159

146-
(defn user-message-with-contents [content-items]
147-
(let [text (string/join "\n" (map #(.text %) content-items))]
148-
(messages/edn->message {:type :user :text text})))
160+

src/clojure_mcp/agent/langchain/chat_listener.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
(on-response (assoc ctx :ctx (:raw-context ctx)))))
2626
:on-error (when on-error
2727
(fn [ctx]
28-
(on-error ctx)))}))
28+
(on-error (assoc ctx :ctx (:raw-context ctx)))))}))
2929

3030
(defn logging-listener
3131
"Create a listener that logs all events."

0 commit comments

Comments
 (0)