branch: externals/ellama
commit 1bebb51698d572f6da9a65e3eb7ce9b2039b1f2a
Merge: a791ec0a70 03a65ca738
Author: Sergey Kostyaev <s-kosty...@users.noreply.github.com>
Commit: GitHub <nore...@github.com>

    Merge pull request #153 from s-kostyaev/make-sessions-awailable
    
    Add ability to call ellama functions with selected session
---
 ellama.el | 82 +++++++++++++++++++++++++++++++++++++++++++++++----------------
 1 file changed, 61 insertions(+), 21 deletions(-)

diff --git a/ellama.el b/ellama.el
index 705d26bab2..d7c675d657 100644
--- a/ellama.el
+++ b/ellama.el
@@ -780,6 +780,10 @@ If EPHEMERAL non nil new session will not be associated 
with any file."
     (when (file-exists-p translation-file)
       (delete-file translation-file t))))
 
+(defun ellama-activate-session (id)
+  "Change current active session to session with ID."
+  (setq ellama--current-session-id id))
+
 ;;;###autoload
 (defun ellama-session-switch ()
   "Change current active session."
@@ -788,7 +792,7 @@ If EPHEMERAL non nil new session will not be associated 
with any file."
              "Select session to activate: "
              (hash-table-keys ellama--active-sessions)))
         (buffer (ellama-get-session-buffer id)))
-    (setq ellama--current-session-id id)
+    (ellama-activate-session id)
     (display-buffer buffer)))
 
 ;;;###autoload
@@ -1248,6 +1252,24 @@ If EPHEMERAL non nil new session will not be associated 
with any file."
   (with-current-buffer buffer
     (not (not ellama--current-session))))
 
+(defun ellama-get-current-session-id ()
+  "Return current session id.
+If buffer contains ellama session return its id.
+Otherwire return id of current active session."
+  (if ellama--current-session
+      (ellama-session-id ellama--current-session)
+    ellama--current-session-id))
+
+(defun ellama-get-current-session ()
+  "Return current session.
+If buffer contains ellama session return it.
+Otherwire return current active session."
+  (if ellama--current-session
+      ellama--current-session
+    (when ellama--current-session-id
+      (with-current-buffer (ellama-get-session-buffer 
ellama--current-session-id)
+       ellama--current-session))))
+
 (defun ellama-stream (prompt &rest args)
   "Query ellama for PROMPT.
 ARGS contains keys for fine control.
@@ -1264,6 +1286,8 @@ strings before they're inserted into the BUFFER.
 
 :session SESSION -- SESSION is a ellama conversation session.
 
+:session-id ID -- ID is a ellama session unique identifier.
+
 :ephemeral-session BOOL -- if BOOL is set session will not be saved to named
 file by default.
 
@@ -1272,7 +1296,11 @@ failure (with BUFFER current).
 
 :on-done ON-DONE -- ON-DONE a function or list of functions that's called with
  the full response text when the request completes (with BUFFER current)."
-  (let* ((session (plist-get args :session))
+  (let* ((session-id (plist-get args :session-id))
+        (session (or (plist-get args :session)
+                     (when session-id
+                       (with-current-buffer (ellama-get-session-buffer 
session-id)
+                         ellama--current-session))))
         (provider (if session
                       (ellama-session-provider session)
                     (or (plist-get args :provider) ellama-provider)))
@@ -1372,6 +1400,8 @@ prompt.  FUCTION will be called with two arguments 
INITIAL-PROMPT and ACC.
 
 :session SESSION - use SESSION in current step.
 
+:session-id ID -- ID is a ellama session unique identifier.
+
 :chat BOOL - if BOOL use chat buffer, otherwise use temp buffer.  Make sense 
for
 last step only.
 
@@ -1383,7 +1413,11 @@ last step only.
         (prompt (if transform
                     (apply transform (list initial-prompt acc))
                   initial-prompt))
-        (session (plist-get hd :session))
+        (session-id (plist-get hd :session-id))
+        (session (or (plist-get hd :session)
+                     (when session-id
+                       (with-current-buffer (ellama-get-session-buffer 
session-id)
+                         ellama--current-session))))
         (chat (plist-get hd :chat))
         (show (or (plist-get hd :show) ellama-always-show-chain-steps))
         (buf (if (or (and (not chat)) (not session))
@@ -1549,6 +1583,10 @@ ARGS contains keys for fine control.
 
 :provider PROVIDER -- PROVIDER is an llm provider for generation.
 
+:session SESSION -- SESSION is a ellama conversation session.
+
+:session-id ID -- ID is a ellama session unique identifier.
+
 :on-done ON-DONE -- ON-DONE a function that's called with
 the full response text when the request completes (with BUFFER current)."
   (interactive "sAsk ellama: ")
@@ -1569,24 +1607,26 @@ the full response text when the request completes (with 
BUFFER current)."
                                providers nil nil #'string=)))
                     (or (plist-get args :provider)
                         ellama-provider)))
-        (session (if (or create-session
-                         current-prefix-arg
-                         (and provider
-                              (or (plist-get args :provider)
-                                  (not (equal provider ellama-provider)))
-                              ellama--current-session-id
-                              (with-current-buffer (ellama-get-session-buffer
-                                                    ellama--current-session-id)
-                                (not (equal
-                                      provider
-                                      (ellama-session-provider 
ellama--current-session)))))
-                         (and (not ellama--current-session)
-                              (not ellama--current-session-id)))
-                     (ellama-new-session provider prompt)
-                   (or ellama--current-session
-                       (with-current-buffer (ellama-get-session-buffer
-                                             ellama--current-session-id)
-                         ellama--current-session))))
+        (session (or (plist-get args :session)
+                     (if (or create-session
+                             current-prefix-arg
+                             (and provider
+                                  (or (plist-get args :provider)
+                                      (not (equal provider ellama-provider)))
+                                  ellama--current-session-id
+                                  (with-current-buffer 
(ellama-get-session-buffer
+                                                        
ellama--current-session-id)
+                                    (not (equal
+                                          provider
+                                          (ellama-session-provider 
ellama--current-session)))))
+                             (and (not ellama--current-session)
+                                  (not ellama--current-session-id)))
+                         (ellama-new-session provider prompt)
+                       (or ellama--current-session
+                           (with-current-buffer (ellama-get-session-buffer
+                                                 (or (plist-get args 
:session-id)
+                                                     
ellama--current-session-id))
+                             ellama--current-session)))))
         (buffer (ellama-get-session-buffer
                  (ellama-session-id session)))
         (file-name (ellama-session-file session))

Reply via email to