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:
Debanjum Singh Solanky 2023-03-22 11:08:17 -06:00
parent 36b52fdd0a
commit 364e6c11af

View file

@ -339,19 +339,35 @@ 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))
(let ((inhibit-read-only t) (khoj--load-chat-history buffer-name)
(json-response (khoj--query-chat-api query))) (with-current-buffer (get-buffer buffer-name)
(goto-char (point-max)) (let ((inhibit-read-only t)
(insert (json-response (khoj--query-chat-api query)))
(khoj--render-chat-message query "you") (goto-char (point-max))
(khoj--render-chat-response json-response))) (insert
(progn (org-mode) (khoj--render-chat-message query "you")
(visual-line-mode)) (khoj--render-chat-response json-response)))
(read-only-mode t))) (progn (org-mode)
(visual-line-mode))
(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))))
;; ------------------ ;; ------------------