mirror of
https://github.com/khoj-ai/khoj.git
synced 2024-11-27 17:35:07 +01:00
Render chat history from API in chat buffer on first run
- Generalize the render-chat-response method to handle rendering history or chat response from chat API reponse - Trigger rendering of khoj chat history if Khoj chat buffer not created for this session yet
This commit is contained in:
parent
36b52fdd0a
commit
364e6c11af
1 changed files with 33 additions and 14 deletions
|
@ -339,10 +339,26 @@ Render results in BUFFER-NAME using QUERY, CONTENT-TYPE."
|
||||||
(khoj--query-chat-api-and-render-messages query khoj--chat-buffer-name)
|
(khoj--query-chat-api-and-render-messages query khoj--chat-buffer-name)
|
||||||
(switch-to-buffer khoj--chat-buffer-name)))
|
(switch-to-buffer khoj--chat-buffer-name)))
|
||||||
|
|
||||||
|
(defun khoj--load-chat-history (buffer-name)
|
||||||
|
(let ((json-response (cdr (assoc 'response (khoj--query-chat-api "")))))
|
||||||
|
(with-current-buffer (get-buffer-create buffer-name)
|
||||||
|
(erase-buffer)
|
||||||
|
(thread-last
|
||||||
|
json-response
|
||||||
|
;; generate chat messages from Khoj Chat API response
|
||||||
|
(mapcar #'khoj--render-chat-response)
|
||||||
|
;; insert chat messages into Khoj Chat Buffer
|
||||||
|
(mapcar #'insert))
|
||||||
|
(progn (org-mode)
|
||||||
|
(visual-line-mode)
|
||||||
|
(read-only-mode t)))))
|
||||||
|
|
||||||
(defun khoj--query-chat-api-and-render-messages (query buffer-name)
|
(defun khoj--query-chat-api-and-render-messages (query buffer-name)
|
||||||
"Send QUERY to Khoj Chat. Render the chat messages from exchange in BUFFER-NAME."
|
"Send QUERY to Khoj Chat. Render the chat messages from exchange in BUFFER-NAME."
|
||||||
;; render json response into formatted chat messages
|
;; render json response into formatted chat messages
|
||||||
(with-current-buffer (get-buffer-create buffer-name)
|
(if (not (get-buffer buffer-name))
|
||||||
|
(khoj--load-chat-history buffer-name)
|
||||||
|
(with-current-buffer (get-buffer buffer-name)
|
||||||
(let ((inhibit-read-only t)
|
(let ((inhibit-read-only t)
|
||||||
(json-response (khoj--query-chat-api query)))
|
(json-response (khoj--query-chat-api query)))
|
||||||
(goto-char (point-max))
|
(goto-char (point-max))
|
||||||
|
@ -351,7 +367,7 @@ Render results in BUFFER-NAME using QUERY, CONTENT-TYPE."
|
||||||
(khoj--render-chat-response json-response)))
|
(khoj--render-chat-response json-response)))
|
||||||
(progn (org-mode)
|
(progn (org-mode)
|
||||||
(visual-line-mode))
|
(visual-line-mode))
|
||||||
(read-only-mode t)))
|
(read-only-mode t))))
|
||||||
|
|
||||||
(defun khoj--query-chat-api (query)
|
(defun khoj--query-chat-api (query)
|
||||||
"Send QUERY to Khoj Chat API."
|
"Send QUERY to Khoj Chat API."
|
||||||
|
@ -383,16 +399,19 @@ RECEIVE-DATE is the message receive date."
|
||||||
|
|
||||||
(defun khoj--render-chat-response (json-response)
|
(defun khoj--render-chat-response (json-response)
|
||||||
"Render chat message using JSON-RESPONSE from Khoj Chat API."
|
"Render chat message using JSON-RESPONSE from Khoj Chat API."
|
||||||
(let* ((context (or (cdr (assoc 'context json-response)) ""))
|
(let* ((message (cdr (or (assoc 'response json-response) (assoc 'message json-response))))
|
||||||
|
(sender (cdr (assoc 'by json-response)))
|
||||||
|
(receive-date (cdr (assoc 'created json-response)))
|
||||||
|
(context (or (cdr (assoc 'context json-response)) ""))
|
||||||
(reference-texts (split-string context "\n\n# " t))
|
(reference-texts (split-string context "\n\n# " t))
|
||||||
(reference-links (-map-indexed #'khoj--generate-reference reference-texts)))
|
(reference-links (-map-indexed #'khoj--generate-reference reference-texts)))
|
||||||
(thread-first
|
(thread-first
|
||||||
;; extract khoj message from API response and make it bold
|
;; extract khoj message from API response and make it bold
|
||||||
(format "*%s*" (cdr (assoc 'response json-response)))
|
(format "*%s*" message)
|
||||||
;; append references to khoj message
|
;; append references to khoj message
|
||||||
(concat " " (string-join reference-links " "))
|
(concat " " (string-join reference-links " "))
|
||||||
;; Set query as heading in rendered results buffer
|
;; Render chat message using data obtained from API
|
||||||
(khoj--render-chat-message "khoj"))))
|
(khoj--render-chat-message sender receive-date))))
|
||||||
|
|
||||||
|
|
||||||
;; ------------------
|
;; ------------------
|
||||||
|
|
Loading…
Reference in a new issue