branch: externals/ellama
commit 87368d22b56d35992aab344b2c2fc530faded8e1
Author: Sergey Kostyaev <[email protected]>
Commit: Sergey Kostyaev <[email protected]>

    Improve new session handling in ellama-chat
    
    Refactored session resolution logic to conditionally create new session when
    create-session flag is set, ensuring proper chat session creation. Added 
test to
    verify that using the create-session flag forces creation of a new session
    independent of existing sessions.
---
 ellama.el            | 10 +++++++---
 tests/test-ellama.el | 29 +++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/ellama.el b/ellama.el
index 5024b5a40c..aba9fd2ac7 100644
--- a/ellama.el
+++ b/ellama.el
@@ -2233,9 +2233,13 @@ the full response text when the request completes (with 
BUFFER current)."
                          ellama-provider
                          (ellama-get-first-ollama-chat-model))))
          (ephemeral (plist-get args :ephemeral))
-         (explicit-session (ellama--resolve-session
-                            (plist-get args :session)
-                            (plist-get args :session-id)))
+         (explicit-session-arg (plist-get args :session))
+         (explicit-session-id (plist-get args :session-id))
+         (explicit-session (when (or explicit-session-arg
+                                     explicit-session-id)
+                             (ellama--resolve-session
+                              explicit-session-arg
+                              explicit-session-id)))
          (current-session (or explicit-session
                               (ellama--resolve-session nil 
ellama--current-session-id)))
          (need-new-session (and (not explicit-session)
diff --git a/tests/test-ellama.el b/tests/test-ellama.el
index e8dca852b8..af099ef2ef 100644
--- a/tests/test-ellama.el
+++ b/tests/test-ellama.el
@@ -1282,6 +1282,35 @@ region, season, or type)! 🍎🍊"))))
       (should (equal global-callback-text "final"))
       (should (equal local-callback-text "final")))))
 
+(ert-deftest test-ellama-chat-create-session-flag-forces-new-session ()
+  (let* ((provider (make-llm-fake :chat-action-func (lambda () "ok")))
+         (ellama-provider provider)
+         (ellama-coding-provider provider)
+         (ellama-major-mode 'text-mode)
+         (ellama-session-auto-save nil)
+         (ellama-chat-translation-enabled nil)
+         (session-1 (ellama-new-session provider "initial prompt" t))
+         (uid-1 (ellama--session-uid session-1))
+         (buffer-1 (ellama-get-session-buffer uid-1))
+         uid-2
+         buffer-2)
+    (unwind-protect
+        (progn
+          (cl-letf (((symbol-function 'ellama-stream)
+                     (lambda (&rest _args) :stubbed)))
+            (ellama-chat "next prompt" t))
+          (setq uid-2 ellama--current-session-uid
+                buffer-2 (ellama-get-session-buffer uid-2))
+          (should uid-2)
+          (should (not (equal uid-1 uid-2)))
+          (should (buffer-live-p buffer-2)))
+      (when (buffer-live-p buffer-1)
+        (kill-buffer buffer-1))
+      (when (and buffer-2
+                 (buffer-live-p buffer-2)
+                 (not (eq buffer-2 buffer-1)))
+        (kill-buffer buffer-2)))))
+
 
 (ert-deftest test-ellama-remove-reasoning ()
   (should (equal

Reply via email to