branch: elpa/aidermacs
commit c65fc855a9cabde289a11f1e9c502735cc745432
Author: Mingde (Matthew) Zeng <matthew...@posteo.net>
Commit: Mingde (Matthew) Zeng <matthew...@posteo.net>

    Introduce overwrite configurations with config file
---
 README.md           | 65 ++++++++++++++++++++++++++++++++++++++++-------------
 aidermacs-models.el |  5 +++--
 aidermacs.el        | 57 +++++++++++++++++++++++++++++++---------------
 3 files changed, 92 insertions(+), 35 deletions(-)

diff --git a/README.md b/README.md
index a0cc1bef40..596ea9d78d 100644
--- a/README.md
+++ b/README.md
@@ -43,7 +43,10 @@ Your contributions are essential to making Aidermacs the 
best AI pair programmin
   :config
   (setq aidermacs-default-model "anthropic/claude-3-5-sonnet-20241022")
   (setenv "ANTHROPIC_API_KEY" anthropic-api-key)
-  (global-set-key (kbd "C-c a") 'aidermacs-transient-menu))
+  (global-set-key (kbd "C-c a") 'aidermacs-transient-menu)
+  ; See the Configuration section below
+  (setq aidermacs-auto-commits t)
+  (setq aidermacs-use-architect-mode t))
 ```
 
 ### Sample Config With Doom Emacs
@@ -63,6 +66,8 @@ You can customize the default AI model used by Aidermacs by 
setting the `aiderma
 
 This allows you to easily switch between different AI models without modifying 
the `aidermacs-extra-args` variable.
 
+*Note: This will be overwritten by the existence of an `.aider.conf.yml` file 
(see [details](#Overwrite-Configuration-with-Configuration-File))*
+
 ### Dynamic Model Selection
 
 Aidermacs provides intelligent model selection for the solo (non-Architect) 
mode that automatically detects and integrates with multiple AI providers:
@@ -90,16 +95,19 @@ The system will automatically filter models to only show 
ones that are:
 
 Aidermacs supports an experimental mode that leverages two models for each 
coding task: an Architect model for reasoning and an Editor model for 
generating code edits. This approach has **achieved state-of-the-art (SOTA) 
results on aider's code editing benchmark**, as detailed in [this blog 
post](https://aider.chat/2024/09/26/architect.html).
 
-To enable this mode, set `aidermacs-use-architect-mode` to `t`. You may also 
configure the `aidermacs-architect-model` and `aidermacs-editor-model` 
variables to specify the models to use for the Architect and Editor roles, 
respectively.
+To enable this mode, set `aidermacs-use-architect-mode` to `t`. You must also 
configure the `aidermacs-architect-model` variable to specify the model to use 
for the Architect role.
+
+By default, the `aidermacs-editor-model` is the same as 
`aidermacs-default-model`. You only need to set `aidermacs-editor-model` if you 
want to use a different model for the Editor role.
 
-When Architect mode is enabled, the `aidermacs-default-model` setting is 
ignored.
+When Architect mode is enabled, the `aidermacs-default-model` setting is 
ignored, and `aidermacs-architect-model` and `aidermacs-editor-model` are used 
instead.
 
 ```emacs-lisp
 (setq aidermacs-use-architect-mode t)
-(setq aidermacs-architect-model "o1-mini") ; default
-(setq aidermacs-editor-model "anthropic/claude-3-5-sonnet-20241022") ; default
+(setq aidermacs-architect-model "o1-mini") ; required
+;; (setq aidermacs-editor-model "anthropic/claude-3-5-sonnet-20241022") ; 
optional, defaults to aidermacs-default-model
 ```
 
+*Note: This will be overwritten by the existence of an `.aider.conf.yml` file 
(see [details](#Overwrite-Configuration-with-Configuration-File))*
 
 ### Terminal Backend Selection
 
@@ -116,16 +124,6 @@ Available backends:
 - `comint` (default): Uses Emacs' built-in terminal emulation
 - `vterm`: Leverages vterm for better terminal compatibility
 
-### Re-Enable Auto-Commits
-
-Aider by default automatically commits changes made by the AI. We find this 
behavior /very/ intrusive, so we disabled it for you. You can re-enable 
auto-commits by setting `aidermacs-auto-commits` to `t`:
-
-```emacs-lisp
-;; Enable auto-commits
-(setq aidermacs-auto-commits t)
-```
-
-With auto-commits disabled, you'll need to manually commit changes using your 
preferred Git workflow.
 
 ### Multiline Input Configuration
 
@@ -138,6 +136,18 @@ When using the comint backend, you can customize the key 
binding for multiline i
 
 This key allows you to enter multiple lines without sending the command to 
Aider. Press `RET` normally to send the command.
 
+### Re-Enable Auto-Commits
+
+Aider by default automatically commits changes made by the AI. We find this 
behavior /very/ intrusive, so we disabled it for you. You can re-enable 
auto-commits by setting `aidermacs-auto-commits` to `t`:
+
+```emacs-lisp
+;; Enable auto-commits
+(setq aidermacs-auto-commits t)
+```
+
+With auto-commits disabled, you'll need to manually commit changes using your 
preferred Git workflow.
+
+*Note: This will be overwritten by the existence of an `.aider.conf.yml` file 
(see [details](#Overwrite-Configuration-with-Configuration-File))*
 
 ### Customizing Aider Options with `aidermacs-extra-args`
 
@@ -152,6 +162,31 @@ See the [Aider configuration 
documentation](https://aider.chat/docs/config/optio
 
 These arguments will be appended to the Aider command when it is run. Note 
that the `--model` argument is automatically handled by 
`aidermacs-default-model` and should not be included in `aidermacs-extra-args`.
 
+
+### Overwrite Configuration with Configuration File
+
+Aidermacs supports project-specific configurations via `.aider.conf.yml` 
files. To enable this:
+
+1.  Create a `.aider.conf.yml` in your home dir, project's root, or the 
current directory, defining your desired settings. See the [Aider 
documentation](https://aider.chat/docs/config/aider_conf.html) for available 
options.
+
+2.  Tell Aidermacs to use the config file in one of two ways:
+
+    ```emacs-lisp
+    ;; Set the `aidermacs-config-file` variable in your Emacs config:
+    (setq aidermacs-config-file "/path/to/your/project/.aider.conf.yml")
+    ;; *Or*, include the `--config` or `-c` flag in `aidermacs-extra-args`:
+    (setq aidermacs-extra-args '("--config" 
"/path/to/your/project/.aider.conf.yml"))
+    ```
+
+*Note: You can also rely on Aider's default behavior of automatically 
searching for `.aider.conf.yml` in the home directory, project root, or current 
directory, in that order. In this case, you do not need to set 
`aidermacs-config-file` or include `--config` in `aidermacs-extra-args`.*
+
+**Important: When using a config file, all other Aidermacs configuration 
variables related to model selection (e.g., `aidermacs-default-model`, 
`aidermacs-architect-model`, `aidermacs-use-architect-mode`) are IGNORED.** 
Aider will *only* use the settings specified in your `.aider.conf.yml` file.  
Do not attempt to combine these Emacs settings with a config file, as the 
results will be unpredictable.
+
+#### Notes
+
+*   **Precedence:** Settings in `.aider.conf.yml` *always* take precedence 
when a config file is explicitly specified.
+*   **Avoid Conflicts:** When using a config file, *do not* include 
model-related arguments (like `--model`, `--architect`, etc.) in 
`aidermacs-extra-args`.  Configure *all* settings within your `.aider.conf.yml` 
file.
+
 ## Usage
 
 ### Getting Started
diff --git a/aidermacs-models.el b/aidermacs-models.el
index c682061fa0..fe48ec6f3a 100644
--- a/aidermacs-models.el
+++ b/aidermacs-models.el
@@ -28,8 +28,9 @@
   :type 'string
   :group 'aidermacs-models)
 
-(defcustom aidermacs-editor-model "anthropic/claude-3-5-sonnet-20241022"
-  "Default AI model to use for code editing in aidermacs sessions."
+(defcustom aidermacs-editor-model aidermacs-default-model
+  "Default AI model to use for code editing in aidermacs sessions.
+Defaults to `aidermacs-default-model` if not explicitly set."
   :type 'string
   :group 'aidermacs-models)
 
diff --git a/aidermacs.el b/aidermacs.el
index 3add7c2637..0826ed8654 100644
--- a/aidermacs.el
+++ b/aidermacs.el
@@ -17,7 +17,7 @@
 (require 'vc-git)
 (require 'which-func)
 (require 'ansi-color)
-
+(require 'cl-lib)
 
 (require 'aidermacs-backends)
 (require 'aidermacs-models)
@@ -37,7 +37,15 @@
 (define-obsolete-variable-alias 'aidermacs-args 'aidermacs-extra-args "0.5.0"
   "Old name for `aidermacs-extra-args', please update your config to use the 
new name.")
 
-(defcustom aidermacs-extra-args nil
+(defcustom aidermacs-config-file nil
+  "Path to aider configuration file.
+When set, Aidermacs will pass this to aider via --config flag,
+ignoring other configuration settings except aidermacs-extra-args."
+  :type '(choice (const :tag "None" nil)
+                 (file :tag "Config file"))
+  :group 'aidermacs)
+
+(defcustom aidermacs-extra-args '()
   "Additional arguments to pass to the aidermacs command.
 The model argument will be added automatically based on 
`aidermacs-default-model'."
   :type '(repeat string)
@@ -224,24 +232,37 @@ Prefers existing sessions closer to current directory."
   "Run aidermacs process using the selected backend."
   (interactive)
   (let* ((buffer-name (aidermacs-buffer-name))
-         (has-model-arg (seq-position aidermacs-extra-args "--model"))
-         (final-args (append
-                      (when aidermacs-use-architect-mode
-                        (list "--architect"
-                              "--model" aidermacs-architect-model
-                              "--editor-model" aidermacs-editor-model))
-                      (unless aidermacs-use-architect-mode
-                        (unless has-model-arg
-                          (list "--model" aidermacs-default-model)))
-                      (unless aidermacs-auto-commits
-                        '("--no-auto-commits"))
-                      (when aidermacs-subtree-only
-                        '("--subtree-only"))
-                      aidermacs-extra-args)))
-    ;; Check if a matching buffer exists (handled by aidermacs-buffer-name)
+         ;; Process extra args: split each string on whitespace.
+         (flat-extra-args
+          (cl-mapcan (lambda (s)
+                       (if (stringp s)
+                           (split-string s "[[:space:]]+" t)
+                         (list s)))
+                     aidermacs-extra-args))
+         (has-model-arg (cl-some (lambda (x) (member x flat-extra-args))
+                                 '("--model" "--opus" "--sonnet" "--haiku"
+                                   "--4" "--4o" "--mini" "--4-turbo" 
"--35turbo"
+                                   "--deepseek" "--o1-mini" "--o1-preview")))
+         (has-config-arg (cl-some (lambda (x) (member x flat-extra-args))
+                                  '("--config" "-c")))
+         (backend-args
+          (if has-config-arg
+              (when aidermacs-config-file
+                (list "--config" aidermacs-config-file))
+            (append
+             (if aidermacs-use-architect-mode
+                 (list "--architect"
+                       "--model" aidermacs-architect-model
+                       "--editor-model" aidermacs-editor-model)
+               (unless has-model-arg
+                 (list "--model" aidermacs-default-model)))
+             (when (not aidermacs-auto-commits)
+               '("--no-auto-commits"))
+             (when aidermacs-subtree-only
+               '("--subtree-only")))))
+         (final-args (append backend-args flat-extra-args)))
     (if (get-buffer buffer-name)
         (aidermacs-switch-to-buffer)
-      ;; Start new session with proper directory and args
       (aidermacs-run-backend aidermacs-program final-args buffer-name)
       (aidermacs-switch-to-buffer))))
 

Reply via email to