Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add `:config-file` cli option to pass in config.

## 0.72.2

- Run `preToolCall` hook before user approval if any. #170
Expand Down
25 changes: 22 additions & 3 deletions src/eca/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
1. base: fixed config var `eca.config/initial-config`.
2. env var: searching for a `ECA_CONFIG` env var which should contains a valid json config.
3. local config-file: searching from a local `.eca/config.json` file.
4. `initializatonOptions` sent in `initialize` request."
4. `initializatonOptions` sent in `initialize` request.

When `:config-file` from cli option is passed, it uses that instead of searching default locations."
(:require
[camel-snake-kebab.core :as csk]
[cheshire.core :as json]
Expand All @@ -23,11 +25,14 @@
(def ^:private logger-tag "[CONFIG]")

(def ^:dynamic *env-var-config-error* false)
(def ^:dynamic *custom-config-error* false)
(def ^:dynamic *global-config-error* false)
(def ^:dynamic *local-config-error* false)

(def ^:private listen-idle-ms 3000)

(def custom-config-file-path* (atom nil))

(def initial-config
{:providers {"openai" {:api "openai-responses"
:url "https://api.openai.com"
Expand Down Expand Up @@ -148,6 +153,14 @@

(def ^:private config-from-envvar (memoize config-from-envvar*))

(defn ^:private config-from-custom* []
(when-some [path @custom-config-file-path*]
(let [config-file (io/file path)]
(when (.exists config-file)
(safe-read-json-string (slurp config-file) (var *custom-config-error*))))))

(def ^:private config-from-custom (memoize config-from-custom*))

(defn global-config-dir ^File []
(let [xdg-config-home (or (get-env "XDG_CONFIG_HOME")
(io/file (get-property "user.home") ".config"))]
Expand Down Expand Up @@ -262,6 +275,13 @@
[:behavior :ANY :toolCall :approval :deny :ANY :argsMatchers]
[:otlp]]})

(defn ^:private config-from-custom-or-default-location [pure-config? db]
(if-some [config-from-custom (config-from-custom)]
(when-not pure-config? config-from-custom)
(deep-merge
(when-not pure-config? (config-from-global-file))
(when-not pure-config? (config-from-local-file (:workspace-folders db))))))

(defn all [db]
(let [initialization-config @initialization-config*
pure-config? (:pureConfig initialization-config)]
Expand All @@ -270,8 +290,7 @@
eca-config-normalization-rules
(deep-merge initialization-config
(when-not pure-config? (config-from-envvar))
(when-not pure-config? (config-from-global-file))
(when-not pure-config? (config-from-local-file (:workspace-folders db))))))))
(config-from-custom-or-default-location pure-config? db))))))

(defn validation-error []
(cond
Expand Down
8 changes: 7 additions & 1 deletion src/eca/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
(def log-levels #{"error" "warn" "info" "debug"})

(def cli-spec
{:order [:help :version :verbose]
{:order [:help :version :verbose :config-file]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ericdallo I noticed you left out --log-level in cli print, is that by design?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I missed that!

:spec {:help {:alias :h
:desc "Print the available commands and its options"}
:version {:desc "Print eca version"}
Expand All @@ -52,6 +52,10 @@
:validate {:pred log-levels
:ex-msg (fn [{:keys [_option _value]}]
(format "Must be in %s" log-levels))}}
:config-file {:ref "<FILE>"
:desc "Path to a JSON config <FILE> to use instead of searching default locations"
:coerce :string
:default nil}
:verbose {:desc "Use stdout for eca logs instead of default log settings"}}})

(defn ^:private parse-opts
Expand Down Expand Up @@ -92,6 +96,8 @@
[action options]
(proxy/load!)
(when (= "server" action)
(when-some [cfg-file (:config-file options)]
(reset! config/custom-config-file-path* cfg-file))
(alter-var-root #'logger/*level* (constantly (keyword (:log-level options))))
(let [finished @(server/run-io-server! (:verbose options))]
{:result-code (if (= :done finished) 0 1)})))
Expand Down
4 changes: 4 additions & 0 deletions test/eca/main_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
[] {:options {:verbose m/absent}}
["--verbose"] {:options {:verbose true}}
["-v"] {:options {:verbose m/absent}}
;; config-file
[] {:options {:config-file m/absent}}
["--config-file"] {:options {:config-file m/absent}}
["--config-file" "/dev/config.json"] {:options {:config-file "/dev/config.json"}}
#_()))

(deftest parse
Expand Down
Loading