2023-06-02 05:10:33 +00:00
|
|
|
|
;;; khoj.el --- AI personal assistant for your digital brain -*- lexical-binding: t -*-
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
|
|
|
|
;; Copyright (C) 2021-2022 Debanjum Singh Solanky
|
|
|
|
|
|
|
|
|
|
;; Author: Debanjum Singh Solanky <debanjum@gmail.com>
|
2023-06-02 05:10:33 +00:00
|
|
|
|
;; Description: An AI personal assistant for your digital brain
|
2023-07-02 23:49:51 +00:00
|
|
|
|
;; Keywords: search, chat, org-mode, outlines, markdown, pdf, image
|
2023-08-28 21:46:03 +00:00
|
|
|
|
;; Version: 0.11.3
|
2023-03-22 22:25:34 +00:00
|
|
|
|
;; Package-Requires: ((emacs "27.1") (transient "0.3.0") (dash "2.19.1"))
|
2023-06-21 07:13:21 +00:00
|
|
|
|
;; URL: https://github.com/khoj-ai/khoj/tree/master/src/interface/emacs
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
|
|
|
|
;; This file is NOT part of GNU Emacs.
|
|
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or
|
|
|
|
|
;; modify it under the terms of the GNU General Public License
|
|
|
|
|
;; as published by the Free Software Foundation; either version 3
|
|
|
|
|
;; of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
|
|
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
|
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
2023-06-02 05:10:33 +00:00
|
|
|
|
;; Create an AI personal assistant for your `org-mode', `markdown' notes,
|
2023-07-02 23:49:51 +00:00
|
|
|
|
;; PDFs and images. The assistant exposes 2 modes, search and chat:
|
2023-03-27 13:47:08 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Chat provides faster answers, iterative discovery and assisted
|
|
|
|
|
;; creativity. It requires your OpenAI API key to access GPT models
|
|
|
|
|
;;
|
|
|
|
|
;; Search allows natural language, incremental and local search.
|
|
|
|
|
;; It relies on AI models that run locally on your machine.
|
2022-08-13 17:29:06 +00:00
|
|
|
|
;;
|
|
|
|
|
;; Quickstart
|
|
|
|
|
;; -------------
|
2023-03-27 13:47:08 +00:00
|
|
|
|
;; 1. Install khoj.el from MELPA Stable
|
2022-12-23 22:08:38 +00:00
|
|
|
|
;; (use-package khoj :pin melpa-stable :bind ("C-c s" . 'khoj))
|
2023-03-27 13:47:08 +00:00
|
|
|
|
;; 2. Start khoj from Emacs
|
|
|
|
|
;; C-c s or M-x khoj
|
|
|
|
|
;;
|
|
|
|
|
;; See the repository docs for detailed setup and configuration steps.
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
|
|
|
|
|
|
(require 'url)
|
|
|
|
|
(require 'json)
|
2023-01-18 23:59:19 +00:00
|
|
|
|
(require 'transient)
|
2023-01-20 23:40:15 +00:00
|
|
|
|
(require 'outline)
|
2023-03-22 07:13:17 +00:00
|
|
|
|
(require 'dash)
|
2023-03-22 08:13:00 +00:00
|
|
|
|
(require 'org)
|
|
|
|
|
|
2023-01-23 21:41:58 +00:00
|
|
|
|
(eval-when-compile (require 'subr-x)) ;; for string-trim before Emacs 28.2
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
2023-01-20 02:36:54 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
;; -------------------------
|
|
|
|
|
;; Khoj Static Configuration
|
|
|
|
|
;; -------------------------
|
|
|
|
|
|
2023-07-11 03:16:25 +00:00
|
|
|
|
(defcustom khoj-server-url "http://localhost:42110"
|
2022-07-19 14:26:16 +00:00
|
|
|
|
"Location of Khoj API server."
|
|
|
|
|
:group 'khoj
|
2021-08-16 08:27:46 +00:00
|
|
|
|
:type 'string)
|
|
|
|
|
|
2023-03-27 09:42:38 +00:00
|
|
|
|
(defcustom khoj-server-is-local t
|
2023-03-26 03:12:06 +00:00
|
|
|
|
"Is Khoj server on local machine?."
|
|
|
|
|
:group 'khoj
|
|
|
|
|
:type 'boolean)
|
|
|
|
|
|
2022-08-09 17:49:34 +00:00
|
|
|
|
(defcustom khoj-image-width 156
|
2022-07-27 14:55:18 +00:00
|
|
|
|
"Width of rendered images returned by Khoj."
|
2022-07-19 14:26:16 +00:00
|
|
|
|
:group 'khoj
|
2021-09-10 08:01:23 +00:00
|
|
|
|
:type 'integer)
|
|
|
|
|
|
2022-08-09 17:55:10 +00:00
|
|
|
|
(defcustom khoj-image-height 156
|
|
|
|
|
"Height of rendered images returned by Khoj."
|
|
|
|
|
:group 'khoj
|
|
|
|
|
:type 'integer)
|
|
|
|
|
|
2022-08-09 17:49:34 +00:00
|
|
|
|
(defcustom khoj-results-count 5
|
2023-07-07 19:34:50 +00:00
|
|
|
|
"Number of results to show in search and use for chat responses."
|
2022-07-27 14:55:18 +00:00
|
|
|
|
:group 'khoj
|
|
|
|
|
:type 'integer)
|
|
|
|
|
|
2023-01-19 01:13:49 +00:00
|
|
|
|
(defcustom khoj-default-content-type "org"
|
2022-08-07 15:04:26 +00:00
|
|
|
|
"The default content type to perform search on."
|
|
|
|
|
:group 'khoj
|
|
|
|
|
:type '(choice (const "org")
|
|
|
|
|
(const "markdown")
|
2023-01-19 01:01:17 +00:00
|
|
|
|
(const "image")
|
2023-07-02 23:21:21 +00:00
|
|
|
|
(const "pdf")))
|
2022-08-07 15:04:26 +00:00
|
|
|
|
|
2023-01-20 02:36:54 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
;; --------------------------
|
|
|
|
|
;; Khoj Dynamic Configuration
|
|
|
|
|
;; --------------------------
|
|
|
|
|
|
2022-07-27 16:08:37 +00:00
|
|
|
|
(defvar khoj--minibuffer-window nil
|
2023-01-19 01:01:17 +00:00
|
|
|
|
"Minibuffer window used to enter query.")
|
2022-07-27 16:08:37 +00:00
|
|
|
|
|
2023-06-21 08:13:50 +00:00
|
|
|
|
(defconst khoj--query-prompt "🏮 Khoj: "
|
2023-01-19 01:01:17 +00:00
|
|
|
|
"Query prompt shown in the minibuffer.")
|
2022-07-27 00:14:14 +00:00
|
|
|
|
|
2023-06-21 08:13:50 +00:00
|
|
|
|
(defconst khoj--search-buffer-name "*🏮 Khoj Search*"
|
2023-03-22 03:01:14 +00:00
|
|
|
|
"Name of buffer to show search results from Khoj.")
|
2022-08-05 14:31:46 +00:00
|
|
|
|
|
2023-06-21 08:13:50 +00:00
|
|
|
|
(defconst khoj--chat-buffer-name "*🏮 Khoj Chat*"
|
2023-03-22 07:13:17 +00:00
|
|
|
|
"Name of chat buffer for Khoj.")
|
|
|
|
|
|
2023-01-19 01:13:49 +00:00
|
|
|
|
(defvar khoj--content-type "org"
|
2022-07-27 02:13:04 +00:00
|
|
|
|
"The type of content to perform search on.")
|
|
|
|
|
|
2023-03-24 22:28:13 +00:00
|
|
|
|
(declare-function org-element-property "org-mode" (PROPERTY ELEMENT))
|
|
|
|
|
(declare-function org-element-type "org-mode" (ELEMENT))
|
2022-12-15 01:31:52 +00:00
|
|
|
|
(declare-function markdown-mode "markdown-mode" ())
|
|
|
|
|
(declare-function which-key--show-keymap "which-key" (KEYMAP-NAME KEYMAP &optional PRIOR-ARGS ALL
|
|
|
|
|
NO-PAGING FILTER))
|
|
|
|
|
|
2022-08-07 15:40:35 +00:00
|
|
|
|
(defun khoj--keybindings-info-message ()
|
2023-01-19 01:01:17 +00:00
|
|
|
|
"Show available khoj keybindings in-context, when khoj invoked."
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(let ((enabled-content-types (khoj--get-enabled-content-types)))
|
2022-08-07 15:40:35 +00:00
|
|
|
|
(concat
|
|
|
|
|
"
|
2023-01-19 01:13:49 +00:00
|
|
|
|
Set Content Type
|
2022-08-07 15:40:35 +00:00
|
|
|
|
-------------------------\n"
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(when (member 'markdown enabled-content-types)
|
2023-01-19 04:04:05 +00:00
|
|
|
|
"C-x m | markdown\n")
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(when (member 'org enabled-content-types)
|
2022-08-07 15:40:35 +00:00
|
|
|
|
"C-x o | org-mode\n")
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(when (member 'image enabled-content-types)
|
2023-01-19 01:01:17 +00:00
|
|
|
|
"C-x i | image\n")
|
2023-06-01 12:34:24 +00:00
|
|
|
|
(when (member 'pdf enabled-content-types)
|
2023-07-02 23:21:21 +00:00
|
|
|
|
"C-x p | pdf\n"))))
|
2022-08-07 15:40:35 +00:00
|
|
|
|
|
2022-12-15 01:31:52 +00:00
|
|
|
|
(defvar khoj--rerank nil "Track when re-rank of results triggered.")
|
2023-03-24 18:43:11 +00:00
|
|
|
|
(defvar khoj--reference-count 0 "Track number of references currently in chat bufffer.")
|
2023-01-19 01:13:49 +00:00
|
|
|
|
(defun khoj--search-markdown () "Set content-type to `markdown'." (interactive) (setq khoj--content-type "markdown"))
|
|
|
|
|
(defun khoj--search-org () "Set content-type to `org-mode'." (interactive) (setq khoj--content-type "org"))
|
|
|
|
|
(defun khoj--search-images () "Set content-type to image." (interactive) (setq khoj--content-type "image"))
|
2023-06-01 12:34:24 +00:00
|
|
|
|
(defun khoj--search-pdf () "Set content-type to pdf." (interactive) (setq khoj--content-type "pdf"))
|
2022-08-15 03:14:28 +00:00
|
|
|
|
(defun khoj--improve-rank () "Use cross-encoder to rerank search results." (interactive) (khoj--incremental-search t))
|
2022-08-05 17:15:51 +00:00
|
|
|
|
(defun khoj--make-search-keymap (&optional existing-keymap)
|
2022-08-13 17:29:06 +00:00
|
|
|
|
"Setup keymap to configure Khoj search. Build of EXISTING-KEYMAP when passed."
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(let ((enabled-content-types (khoj--get-enabled-content-types))
|
2022-08-07 15:01:21 +00:00
|
|
|
|
(kmap (or existing-keymap (make-sparse-keymap))))
|
2022-08-15 03:14:28 +00:00
|
|
|
|
(define-key kmap (kbd "C-c RET") #'khoj--improve-rank)
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(when (member 'markdown enabled-content-types)
|
2022-08-07 15:01:21 +00:00
|
|
|
|
(define-key kmap (kbd "C-x m") #'khoj--search-markdown))
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(when (member 'org enabled-content-types)
|
2022-08-07 15:01:21 +00:00
|
|
|
|
(define-key kmap (kbd "C-x o") #'khoj--search-org))
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(when (member 'image enabled-content-types)
|
2022-08-07 15:01:21 +00:00
|
|
|
|
(define-key kmap (kbd "C-x i") #'khoj--search-images))
|
2023-06-01 12:34:24 +00:00
|
|
|
|
(when (member 'pdf enabled-content-types)
|
|
|
|
|
(define-key kmap (kbd "C-x p") #'khoj--search-pdf))
|
2022-08-05 17:15:51 +00:00
|
|
|
|
kmap))
|
2022-08-13 17:29:06 +00:00
|
|
|
|
|
|
|
|
|
(defvar khoj--keymap nil "Track Khoj keymap in this variable.")
|
2022-08-05 17:15:51 +00:00
|
|
|
|
(defun khoj--display-keybinding-info ()
|
|
|
|
|
"Display information on keybindings to customize khoj search.
|
|
|
|
|
Use `which-key` if available, else display simple message in echo area"
|
2022-08-06 13:27:23 +00:00
|
|
|
|
(if (fboundp 'which-key-show-full-keymap)
|
|
|
|
|
(let ((khoj--keymap (khoj--make-search-keymap)))
|
2022-08-07 12:57:08 +00:00
|
|
|
|
(which-key--show-keymap (symbol-name 'khoj--keymap)
|
|
|
|
|
(symbol-value 'khoj--keymap)
|
|
|
|
|
nil t t))
|
2022-08-07 15:40:35 +00:00
|
|
|
|
(message "%s" (khoj--keybindings-info-message))))
|
2022-08-05 16:32:58 +00:00
|
|
|
|
|
2023-03-26 01:41:51 +00:00
|
|
|
|
|
|
|
|
|
;; ----------------
|
|
|
|
|
;; Khoj Setup
|
|
|
|
|
;; ----------------
|
|
|
|
|
(defcustom khoj-server-command
|
|
|
|
|
(or (executable-find "khoj")
|
|
|
|
|
(executable-find "khoj.exe")
|
|
|
|
|
"khoj")
|
|
|
|
|
"Command to interact with Khoj server."
|
2023-03-27 09:42:38 +00:00
|
|
|
|
:type 'string
|
|
|
|
|
:group 'khoj)
|
2023-03-26 01:41:51 +00:00
|
|
|
|
|
2023-07-02 02:07:59 +00:00
|
|
|
|
(defcustom khoj-server-args '()
|
2023-03-26 02:38:46 +00:00
|
|
|
|
"Arguments to pass to Khoj server on startup."
|
2023-03-27 09:42:38 +00:00
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:group 'khoj)
|
2023-03-26 02:38:46 +00:00
|
|
|
|
|
2023-03-26 01:41:51 +00:00
|
|
|
|
(defcustom khoj-server-python-command
|
|
|
|
|
(if (equal system-type 'windows-nt)
|
|
|
|
|
(or (executable-find "py")
|
|
|
|
|
(executable-find "pythonw")
|
|
|
|
|
"python")
|
|
|
|
|
(if (executable-find "python")
|
|
|
|
|
"python"
|
|
|
|
|
;; Fallback on systems where python is not
|
|
|
|
|
;; symlinked to python3.
|
|
|
|
|
"python3"))
|
|
|
|
|
"The Python interpreter used for the Khoj server.
|
|
|
|
|
|
|
|
|
|
Khoj will try to use the system interpreter if it exists. If you wish
|
|
|
|
|
to use a specific python interpreter (from a virtual environment
|
|
|
|
|
for example), set this to the full interpreter path."
|
|
|
|
|
:type '(choice (const :tag "python" "python")
|
|
|
|
|
(const :tag "python3" "python3")
|
|
|
|
|
(const :tag "pythonw (Python on Windows)" "pythonw")
|
|
|
|
|
(const :tag "py (other Python on Windows)" "py")
|
|
|
|
|
(string :tag "Other"))
|
|
|
|
|
:safe (lambda (val)
|
|
|
|
|
(member val '("python" "python3" "pythonw" "py")))
|
|
|
|
|
:group 'khoj)
|
|
|
|
|
|
2023-03-27 11:30:53 +00:00
|
|
|
|
(defcustom khoj-org-files (org-agenda-files t t)
|
2023-03-26 21:38:17 +00:00
|
|
|
|
"List of org-files to index on khoj server."
|
2023-03-27 09:42:38 +00:00
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:group 'khoj)
|
2023-03-26 11:44:10 +00:00
|
|
|
|
|
2023-03-27 11:30:53 +00:00
|
|
|
|
(defcustom khoj-org-directories nil
|
2023-05-02 13:39:47 +00:00
|
|
|
|
"List of directories with `org-mode' files to index on khoj server."
|
2023-03-27 11:30:53 +00:00
|
|
|
|
:type '(repeat string)
|
|
|
|
|
:group 'khoj)
|
|
|
|
|
|
2023-07-22 07:26:52 +00:00
|
|
|
|
(defcustom khoj-chat-model "gpt-3.5-turbo"
|
2023-07-19 00:54:43 +00:00
|
|
|
|
"Specify chat model to use for chat with khoj."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'khoj)
|
|
|
|
|
|
2023-03-26 21:38:17 +00:00
|
|
|
|
(defcustom khoj-openai-api-key nil
|
|
|
|
|
"OpenAI API key used to configure chat on khoj server."
|
2023-03-27 09:42:38 +00:00
|
|
|
|
:type 'string
|
|
|
|
|
:group 'khoj)
|
2023-03-26 21:38:17 +00:00
|
|
|
|
|
2023-07-28 23:04:34 +00:00
|
|
|
|
(defcustom khoj-chat-offline nil
|
|
|
|
|
"Use offline model to chat with khoj."
|
|
|
|
|
:type 'boolean
|
|
|
|
|
:group 'khoj)
|
|
|
|
|
|
2023-03-27 10:53:08 +00:00
|
|
|
|
(defcustom khoj-auto-setup t
|
2023-03-27 11:33:09 +00:00
|
|
|
|
"Automate install, configure and start of khoj server.
|
2023-03-27 10:53:08 +00:00
|
|
|
|
Auto invokes setup steps on calling main entrypoint."
|
|
|
|
|
:type 'string
|
|
|
|
|
:group 'khoj)
|
|
|
|
|
|
2023-03-27 11:33:09 +00:00
|
|
|
|
(defvar khoj--server-process nil "Track khoj server process.")
|
|
|
|
|
(defvar khoj--server-name "*khoj-server*" "Track khoj server buffer.")
|
2023-03-26 04:56:04 +00:00
|
|
|
|
(defvar khoj--server-ready? nil "Track if khoj server is ready to receive API calls.")
|
2023-03-26 22:52:32 +00:00
|
|
|
|
(defvar khoj--server-configured? t "Track if khoj server is configured to receive API calls.")
|
|
|
|
|
(defvar khoj--progressbar '(🌑 🌘 🌗 🌖 🌕 🌔 🌓 🌒) "Track progress via moon phase animations.")
|
2023-03-26 02:38:46 +00:00
|
|
|
|
|
2023-03-26 01:41:51 +00:00
|
|
|
|
(defun khoj--server-get-version ()
|
|
|
|
|
"Return the khoj server version."
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(call-process khoj-server-command nil t nil "--version")
|
|
|
|
|
(goto-char (point-min))
|
|
|
|
|
(re-search-forward "\\([a-z0-9.]+\\)")
|
|
|
|
|
(match-string 1)))
|
|
|
|
|
|
|
|
|
|
(defun khoj--server-install-upgrade ()
|
|
|
|
|
"Install or upgrade the khoj server."
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(message "khoj.el: Installing server...")
|
2023-05-02 13:39:47 +00:00
|
|
|
|
(if (/= (apply #'call-process khoj-server-python-command
|
|
|
|
|
nil t nil
|
|
|
|
|
"-m" "pip" "install" "--upgrade"
|
|
|
|
|
'("khoj-assistant"))
|
2023-03-26 01:41:51 +00:00
|
|
|
|
0)
|
|
|
|
|
(message "khoj.el: Failed to install Khoj server. Please install it manually using pip install `khoj-assistant'.\n%s" (buffer-string))
|
|
|
|
|
(message "khoj.el: Installed and upgraded Khoj server version: %s" (khoj--server-get-version)))))
|
|
|
|
|
|
2023-03-26 02:38:46 +00:00
|
|
|
|
(defun khoj--server-start ()
|
|
|
|
|
"Start the khoj server."
|
2023-03-26 18:11:43 +00:00
|
|
|
|
(interactive)
|
2023-03-26 02:38:46 +00:00
|
|
|
|
(let* ((url-parts (split-string (cadr (split-string khoj-server-url "://")) ":"))
|
|
|
|
|
(server-host (nth 0 url-parts))
|
|
|
|
|
(server-port (or (nth 1 url-parts) "80"))
|
|
|
|
|
(server-args (append khoj-server-args
|
|
|
|
|
(list (format "--host=%s" server-host)
|
|
|
|
|
(format "--port=%s" server-port)))))
|
|
|
|
|
(message "khoj.el: Starting server at %s %s..." server-host server-port)
|
|
|
|
|
(setq khoj--server-process
|
2023-03-26 04:56:04 +00:00
|
|
|
|
(make-process
|
|
|
|
|
:name khoj--server-name
|
|
|
|
|
:buffer khoj--server-name
|
|
|
|
|
:command (append (list khoj-server-command) server-args)
|
2023-04-27 12:43:43 +00:00
|
|
|
|
:sentinel (lambda (_ event)
|
2023-03-26 04:56:04 +00:00
|
|
|
|
(message "khoj.el: khoj server stopped with: %s" event)
|
|
|
|
|
(setq khoj--server-ready? nil))
|
|
|
|
|
:filter (lambda (process msg)
|
|
|
|
|
(cond ((string-match (format "Uvicorn running on %s" khoj-server-url) msg)
|
2023-03-26 18:16:05 +00:00
|
|
|
|
(progn
|
|
|
|
|
(setq khoj--server-ready? t)
|
|
|
|
|
(khoj--server-configure)))
|
2023-03-26 22:52:32 +00:00
|
|
|
|
((string-match "Batches: " msg)
|
|
|
|
|
(when (string-match "\\([0-9]+\\.[0-9]+\\|\\([0-9]+\\)\\)%?" msg)
|
|
|
|
|
(message "khoj.el: %s updating index %s"
|
|
|
|
|
(nth (% (string-to-number (match-string 1 msg)) (length khoj--progressbar)) khoj--progressbar)
|
|
|
|
|
(match-string 0 msg)))
|
|
|
|
|
(setq khoj--server-configured? nil))
|
|
|
|
|
((and (not khoj--server-configured?)
|
|
|
|
|
(string-match "Processor reconfigured via API" msg))
|
|
|
|
|
(setq khoj--server-configured? t))
|
2023-03-26 21:46:31 +00:00
|
|
|
|
((and (not khoj--server-ready?)
|
|
|
|
|
(or (string-match "configure.py" msg)
|
|
|
|
|
(string-match "main.py" msg)
|
|
|
|
|
(string-match "api.py" msg)))
|
2023-03-26 04:56:04 +00:00
|
|
|
|
(dolist (line (split-string msg "\n"))
|
2023-04-28 03:15:13 +00:00
|
|
|
|
(when (string-match " " line)
|
|
|
|
|
(message "khoj.el: %s" (nth 1 (split-string line " " t " *")))))))
|
2023-03-26 04:56:04 +00:00
|
|
|
|
;; call default process filter to write output to process buffer
|
2023-03-26 21:46:31 +00:00
|
|
|
|
(internal-default-process-filter process msg))))
|
2023-03-26 04:56:04 +00:00
|
|
|
|
(set-process-query-on-exit-flag khoj--server-process nil)
|
|
|
|
|
(when (not khoj--server-process)
|
|
|
|
|
(message "khoj.el: Failed to start Khoj server. Please start it manually by running `khoj' on terminal.\n%s" (buffer-string)))))
|
2023-03-26 02:38:46 +00:00
|
|
|
|
|
2023-03-27 10:53:08 +00:00
|
|
|
|
(defun khoj--server-started? ()
|
|
|
|
|
"Check if the khoj server has been started."
|
|
|
|
|
;; check for when server process handled from within emacs
|
|
|
|
|
(if (and khoj--server-process
|
2023-05-02 13:39:47 +00:00
|
|
|
|
(process-live-p khoj--server-process))
|
2023-03-27 10:53:08 +00:00
|
|
|
|
t
|
|
|
|
|
;; else general check via ping to khoj-server-url
|
|
|
|
|
(if (ignore-errors
|
2023-05-02 13:39:47 +00:00
|
|
|
|
(url-retrieve-synchronously (format "%s/api/config/data/default" khoj-server-url)))
|
2023-03-27 10:53:08 +00:00
|
|
|
|
;; Successful ping to non-emacs khoj server indicates it is started and ready.
|
|
|
|
|
;; So update ready state tracker variable (and implicitly return true for started)
|
|
|
|
|
(setq khoj--server-ready? t)
|
|
|
|
|
nil)))
|
2023-03-26 03:12:06 +00:00
|
|
|
|
|
2023-03-27 11:33:09 +00:00
|
|
|
|
(defun khoj--server-restart ()
|
|
|
|
|
"Restart the khoj server."
|
|
|
|
|
(interactive)
|
|
|
|
|
(khoj--server-stop)
|
|
|
|
|
(khoj--server-start))
|
|
|
|
|
|
2023-03-26 03:12:06 +00:00
|
|
|
|
(defun khoj--server-stop ()
|
|
|
|
|
"Stop the khoj server."
|
2023-03-26 18:11:43 +00:00
|
|
|
|
(interactive)
|
2023-03-27 10:53:08 +00:00
|
|
|
|
(when (khoj--server-started?)
|
2023-03-26 03:12:06 +00:00
|
|
|
|
(message "khoj.el: Stopping server...")
|
|
|
|
|
(kill-process khoj--server-process)
|
|
|
|
|
(message "khoj.el: Stopped server.")))
|
|
|
|
|
|
|
|
|
|
(defun khoj--server-setup ()
|
|
|
|
|
"Install and start the khoj server, if required."
|
2023-03-26 18:11:43 +00:00
|
|
|
|
(interactive)
|
2023-03-26 03:12:06 +00:00
|
|
|
|
;; Install khoj server, if not available but expected on local machine
|
2023-03-27 09:42:38 +00:00
|
|
|
|
(when (and khoj-server-is-local
|
2023-03-26 03:12:06 +00:00
|
|
|
|
(or (not (executable-find khoj-server-command))
|
|
|
|
|
(not (khoj--server-get-version))))
|
|
|
|
|
(khoj--server-install-upgrade))
|
2023-03-27 10:53:08 +00:00
|
|
|
|
;; Start khoj server if not already started
|
|
|
|
|
(when (not (khoj--server-started?))
|
2023-03-26 03:12:06 +00:00
|
|
|
|
(khoj--server-start)))
|
|
|
|
|
|
2023-03-26 20:44:03 +00:00
|
|
|
|
(defun khoj--get-directory-from-config (config keys &optional level)
|
|
|
|
|
"Extract directory under specified KEYS in CONFIG and trim it to LEVEL.
|
2023-03-26 11:44:10 +00:00
|
|
|
|
CONFIG is json obtained from Khoj config API."
|
2023-03-26 20:44:03 +00:00
|
|
|
|
(let ((item config))
|
|
|
|
|
(dolist (key keys)
|
|
|
|
|
(setq item (cdr (assoc key item))))
|
|
|
|
|
(-> item
|
|
|
|
|
(split-string "/")
|
|
|
|
|
(butlast (or level nil))
|
|
|
|
|
(string-join "/"))))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
|
|
|
|
|
(defun khoj--server-configure ()
|
2023-03-26 21:38:17 +00:00
|
|
|
|
"Configure the the Khoj server for search and chat."
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(interactive)
|
2023-03-27 11:30:53 +00:00
|
|
|
|
(let* ((org-directory-regexes (or (mapcar (lambda (dir) (format "%s/**/*.org" dir)) khoj-org-directories) json-null))
|
|
|
|
|
(current-config
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(url-insert-file-contents (format "%s/api/config/data" khoj-server-url))
|
|
|
|
|
(ignore-error json-end-of-file (json-parse-buffer :object-type 'alist :array-type 'list :null-object json-null :false-object json-false))))
|
|
|
|
|
(default-config
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(url-insert-file-contents (format "%s/api/config/data/default" khoj-server-url))
|
|
|
|
|
(ignore-error json-end-of-file (json-parse-buffer :object-type 'alist :array-type 'list :null-object json-null :false-object json-false))))
|
2023-03-26 20:44:03 +00:00
|
|
|
|
(default-index-dir (khoj--get-directory-from-config default-config '(content-type org embeddings-file)))
|
2023-03-26 21:38:17 +00:00
|
|
|
|
(default-chat-dir (khoj--get-directory-from-config default-config '(processor conversation conversation-logfile)))
|
2023-07-26 23:27:08 +00:00
|
|
|
|
(chat-model (or khoj-chat-model (alist-get 'chat-model (alist-get 'openai (alist-get 'conversation (alist-get 'processor default-config))))))
|
2023-07-19 00:54:43 +00:00
|
|
|
|
(default-model (alist-get 'model (alist-get 'conversation (alist-get 'processor default-config))))
|
2023-07-28 23:04:34 +00:00
|
|
|
|
(enable-offline-chat (or khoj-chat-offline (alist-get 'enable-offline-chat (alist-get 'conversation (alist-get 'processor default-config)))))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(config (or current-config default-config)))
|
2023-03-26 21:38:17 +00:00
|
|
|
|
|
|
|
|
|
;; Configure content types
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(cond
|
|
|
|
|
;; If khoj backend is not configured yet
|
|
|
|
|
((not current-config)
|
2023-04-27 12:28:55 +00:00
|
|
|
|
(message "khoj.el: Server not configured yet.")
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(setq config (delq (assoc 'content-type config) config))
|
2023-05-02 13:26:30 +00:00
|
|
|
|
(cl-pushnew `(content-type . ((org . ((input-files . ,khoj-org-files)
|
2023-05-03 07:01:58 +00:00
|
|
|
|
(input-filter . ,org-directory-regexes)
|
|
|
|
|
(compressed-jsonl . ,(format "%s/org.jsonl.gz" default-index-dir))
|
|
|
|
|
(embeddings-file . ,(format "%s/org.pt" default-index-dir))
|
|
|
|
|
(index-heading-entries . ,json-false)))))
|
|
|
|
|
config))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
|
|
|
|
|
;; Else if khoj config has no org content config
|
|
|
|
|
((not (alist-get 'org (alist-get 'content-type config)))
|
2023-04-27 12:28:55 +00:00
|
|
|
|
(message "khoj.el: Org-mode content on server not configured yet.")
|
|
|
|
|
(let ((new-content-type (alist-get 'content-type config)))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(setq new-content-type (delq (assoc 'org new-content-type) new-content-type))
|
2023-05-02 13:26:30 +00:00
|
|
|
|
(cl-pushnew `(org . ((input-files . ,khoj-org-files)
|
2023-05-03 07:01:58 +00:00
|
|
|
|
(input-filter . ,org-directory-regexes)
|
|
|
|
|
(compressed-jsonl . ,(format "%s/org.jsonl.gz" default-index-dir))
|
|
|
|
|
(embeddings-file . ,(format "%s/org.pt" default-index-dir))
|
|
|
|
|
(index-heading-entries . ,json-false)))
|
|
|
|
|
new-content-type)
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(setq config (delq (assoc 'content-type config) config))
|
2023-05-03 07:01:58 +00:00
|
|
|
|
(cl-pushnew `(content-type . ,new-content-type) config)))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
|
2023-03-26 21:38:17 +00:00
|
|
|
|
;; Else if khoj is not configured to index specified org files
|
2023-03-27 11:30:53 +00:00
|
|
|
|
((not (and (equal (alist-get 'input-files (alist-get 'org (alist-get 'content-type config))) khoj-org-files)
|
|
|
|
|
(equal (alist-get 'input-filter (alist-get 'org (alist-get 'content-type config))) org-directory-regexes)))
|
2023-04-27 12:28:55 +00:00
|
|
|
|
(message "khoj.el: Org-mode content on server is stale.")
|
2023-03-26 20:44:03 +00:00
|
|
|
|
(let* ((index-directory (khoj--get-directory-from-config config '(content-type org embeddings-file)))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(new-content-type (alist-get 'content-type config)))
|
|
|
|
|
(setq new-content-type (delq (assoc 'org new-content-type) new-content-type))
|
2023-05-02 13:26:30 +00:00
|
|
|
|
(cl-pushnew `(org . ((input-files . ,khoj-org-files)
|
2023-05-03 07:01:58 +00:00
|
|
|
|
(input-filter . ,org-directory-regexes)
|
|
|
|
|
(compressed-jsonl . ,(format "%s/org.jsonl.gz" index-directory))
|
|
|
|
|
(embeddings-file . ,(format "%s/org.pt" index-directory))
|
|
|
|
|
(index-heading-entries . ,json-false)))
|
|
|
|
|
new-content-type)
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(setq config (delq (assoc 'content-type config) config))
|
2023-05-03 07:01:58 +00:00
|
|
|
|
(cl-pushnew `(content-type . ,new-content-type) config))))
|
2023-03-26 21:38:17 +00:00
|
|
|
|
|
|
|
|
|
;; Configure processors
|
|
|
|
|
(cond
|
|
|
|
|
((not khoj-openai-api-key)
|
2023-07-26 23:27:08 +00:00
|
|
|
|
(let* ((processor (assoc 'processor config))
|
|
|
|
|
(conversation (assoc 'conversation processor))
|
|
|
|
|
(openai (assoc 'openai conversation)))
|
|
|
|
|
(when openai
|
|
|
|
|
;; Unset the `openai' field in the khoj conversation processor config
|
2023-07-28 23:04:34 +00:00
|
|
|
|
(message "khoj.el: Disable Chat using OpenAI as your OpenAI API key got removed from config")
|
2023-07-26 23:27:08 +00:00
|
|
|
|
(setcdr conversation (delq openai (cdr conversation)))
|
|
|
|
|
(push conversation (cdr processor))
|
|
|
|
|
(push processor config))))
|
2023-03-26 21:38:17 +00:00
|
|
|
|
|
2023-07-28 23:04:34 +00:00
|
|
|
|
;; If khoj backend isn't configured yet
|
2023-03-26 21:38:17 +00:00
|
|
|
|
((not current-config)
|
2023-04-27 12:28:55 +00:00
|
|
|
|
(message "khoj.el: Chat not configured yet.")
|
2023-03-26 21:38:17 +00:00
|
|
|
|
(setq config (delq (assoc 'processor config) config))
|
2023-05-02 13:26:30 +00:00
|
|
|
|
(cl-pushnew `(processor . ((conversation . ((conversation-logfile . ,(format "%s/conversation.json" default-chat-dir))
|
2023-07-28 23:04:34 +00:00
|
|
|
|
(enable-offline-chat . ,enable-offline-chat)
|
2023-07-27 00:34:16 +00:00
|
|
|
|
(openai . ((chat-model . ,chat-model)
|
|
|
|
|
(api-key . ,khoj-openai-api-key)))))))
|
2023-05-03 07:01:58 +00:00
|
|
|
|
config))
|
2023-03-26 21:38:17 +00:00
|
|
|
|
|
2023-07-28 23:04:34 +00:00
|
|
|
|
;; Else if chat isn't configured in khoj backend
|
2023-03-26 21:38:17 +00:00
|
|
|
|
((not (alist-get 'conversation (alist-get 'processor config)))
|
2023-04-27 12:28:55 +00:00
|
|
|
|
(message "khoj.el: Chat not configured yet.")
|
2023-03-26 21:38:17 +00:00
|
|
|
|
(let ((new-processor-type (alist-get 'processor config)))
|
|
|
|
|
(setq new-processor-type (delq (assoc 'conversation new-processor-type) new-processor-type))
|
2023-05-02 13:26:30 +00:00
|
|
|
|
(cl-pushnew `(conversation . ((conversation-logfile . ,(format "%s/conversation.json" default-chat-dir))
|
2023-07-28 23:04:34 +00:00
|
|
|
|
(enable-offline-chat . ,enable-offline-chat)
|
2023-07-27 00:34:16 +00:00
|
|
|
|
(openai . ((chat-model . ,chat-model)
|
|
|
|
|
(api-key . ,khoj-openai-api-key)))))
|
2023-05-03 07:01:58 +00:00
|
|
|
|
new-processor-type)
|
2023-03-26 21:38:17 +00:00
|
|
|
|
(setq config (delq (assoc 'processor config) config))
|
2023-05-03 07:01:58 +00:00
|
|
|
|
(cl-pushnew `(processor . ,new-processor-type) config)))
|
2023-03-26 21:38:17 +00:00
|
|
|
|
|
2023-07-28 23:04:34 +00:00
|
|
|
|
;; Else if chat configuration in khoj backend has gone stale
|
2023-07-26 23:27:08 +00:00
|
|
|
|
((not (and (equal (alist-get 'api-key (alist-get 'openai (alist-get 'conversation (alist-get 'processor config)))) khoj-openai-api-key)
|
2023-07-28 23:04:34 +00:00
|
|
|
|
(equal (alist-get 'chat-model (alist-get 'openai (alist-get 'conversation (alist-get 'processor config)))) khoj-chat-model)
|
|
|
|
|
(equal (alist-get 'enable-offline-chat (alist-get 'conversation (alist-get 'processor config))) enable-offline-chat)))
|
2023-04-27 12:28:55 +00:00
|
|
|
|
(message "khoj.el: Chat configuration has gone stale.")
|
2023-03-26 21:38:17 +00:00
|
|
|
|
(let* ((chat-directory (khoj--get-directory-from-config config '(processor conversation conversation-logfile)))
|
|
|
|
|
(new-processor-type (alist-get 'processor config)))
|
|
|
|
|
(setq new-processor-type (delq (assoc 'conversation new-processor-type) new-processor-type))
|
2023-05-02 13:26:30 +00:00
|
|
|
|
(cl-pushnew `(conversation . ((conversation-logfile . ,(format "%s/conversation.json" chat-directory))
|
2023-07-28 23:04:34 +00:00
|
|
|
|
(enable-offline-chat . ,enable-offline-chat)
|
2023-07-27 00:34:16 +00:00
|
|
|
|
(openai . ((chat-model . ,khoj-chat-model)
|
|
|
|
|
(api-key . ,khoj-openai-api-key)))))
|
2023-05-03 07:01:58 +00:00
|
|
|
|
new-processor-type)
|
2023-03-26 21:38:17 +00:00
|
|
|
|
(setq config (delq (assoc 'processor config) config))
|
2023-05-03 07:01:58 +00:00
|
|
|
|
(cl-pushnew `(processor . ,new-processor-type) config))))
|
2023-03-26 21:38:17 +00:00
|
|
|
|
|
2023-04-27 12:28:55 +00:00
|
|
|
|
;; Update server with latest configuration, if required
|
|
|
|
|
(cond ((not current-config)
|
|
|
|
|
(khoj--post-new-config config)
|
2023-03-26 21:38:17 +00:00
|
|
|
|
(message "khoj.el: ⚙️ Generated new khoj server configuration."))
|
|
|
|
|
((not (equal config current-config))
|
2023-04-27 12:28:55 +00:00
|
|
|
|
(khoj--post-new-config config)
|
|
|
|
|
(message "khoj.el: ⚙️ Updated khoj server configuration.")))))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
|
2023-03-27 10:53:08 +00:00
|
|
|
|
(defun khoj-setup (&optional interact)
|
2023-05-02 13:39:47 +00:00
|
|
|
|
"Install, start and configure Khoj server. Get permission if INTERACT is non-nil."
|
2023-03-27 10:53:08 +00:00
|
|
|
|
(interactive "p")
|
|
|
|
|
;; Setup khoj server if not running
|
|
|
|
|
(let* ((not-started (not (khoj--server-started?)))
|
|
|
|
|
(permitted (if (and not-started interact)
|
|
|
|
|
(y-or-n-p "Could not connect to Khoj server. Should I install, start and configure it for you?")
|
|
|
|
|
t)))
|
2023-04-27 10:55:48 +00:00
|
|
|
|
;; If user permits setup of khoj server from khoj.el
|
|
|
|
|
(when permitted
|
|
|
|
|
; Install, start server if server not running
|
|
|
|
|
(when not-started
|
|
|
|
|
(khoj--server-setup))
|
2023-03-27 10:53:08 +00:00
|
|
|
|
|
2023-04-27 10:55:48 +00:00
|
|
|
|
;; Wait until server is ready
|
|
|
|
|
;; As server can be started but not ready to use/configure
|
|
|
|
|
(while (not khoj--server-ready?)
|
|
|
|
|
(sit-for 0.5))
|
2023-03-27 10:53:08 +00:00
|
|
|
|
|
2023-04-27 10:55:48 +00:00
|
|
|
|
;; Configure server once it's ready
|
2023-03-27 10:53:08 +00:00
|
|
|
|
(khoj--server-configure))))
|
|
|
|
|
|
2023-01-20 02:36:54 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
;; -----------------------------------------------
|
|
|
|
|
;; Extract and Render Entries of each Content Type
|
|
|
|
|
;; -----------------------------------------------
|
|
|
|
|
|
2022-07-21 16:22:24 +00:00
|
|
|
|
(defun khoj--extract-entries-as-markdown (json-response query)
|
2022-08-13 17:29:06 +00:00
|
|
|
|
"Convert JSON-RESPONSE, QUERY from API to markdown entries."
|
2023-01-26 21:59:44 +00:00
|
|
|
|
(thread-last
|
|
|
|
|
json-response
|
|
|
|
|
;; Extract and render each markdown entry from response
|
|
|
|
|
(mapcar (lambda (json-response-item)
|
|
|
|
|
(thread-last
|
|
|
|
|
;; Extract markdown entry from each item in json response
|
|
|
|
|
(cdr (assoc 'entry json-response-item))
|
|
|
|
|
;; Format markdown entry as a string
|
2023-01-26 22:04:46 +00:00
|
|
|
|
(format "%s\n\n")
|
2023-01-26 21:59:44 +00:00
|
|
|
|
;; Standardize results to 2nd level heading for consistent rendering
|
|
|
|
|
(replace-regexp-in-string "^\#+" "##"))))
|
|
|
|
|
;; Render entries into markdown formatted string with query set as as top level heading
|
|
|
|
|
(format "# %s\n%s" query)
|
|
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
|
(replace-regexp-in-string "^[\(\) ]" "")))
|
2022-07-21 16:22:24 +00:00
|
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
|
(defun khoj--extract-entries-as-org (json-response query)
|
2023-01-05 08:47:38 +00:00
|
|
|
|
"Convert JSON-RESPONSE, QUERY from API to `org-mode' entries."
|
2023-03-25 23:34:18 +00:00
|
|
|
|
(thread-last
|
|
|
|
|
json-response
|
|
|
|
|
;; Extract and render each org-mode entry from response
|
|
|
|
|
(mapcar (lambda (json-response-item)
|
|
|
|
|
(thread-last
|
|
|
|
|
;; Extract org entry from each item in json response
|
|
|
|
|
(cdr (assoc 'entry json-response-item))
|
|
|
|
|
;; Format org entry as a string
|
|
|
|
|
(format "%s")
|
|
|
|
|
;; Standardize results to 2nd level heading for consistent rendering
|
|
|
|
|
(replace-regexp-in-string "^\*+" "**"))))
|
|
|
|
|
;; Render entries into org formatted string with query set as as top level heading
|
|
|
|
|
(format "* %s\n%s\n" query)
|
|
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
|
(replace-regexp-in-string "^[\(\) ]" "")))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
2023-06-01 12:34:24 +00:00
|
|
|
|
(defun khoj--extract-entries-as-pdf (json-response query)
|
|
|
|
|
"Convert QUERY, JSON-RESPONSE from API with PDF results to `org-mode' entries."
|
|
|
|
|
(thread-last
|
|
|
|
|
json-response
|
|
|
|
|
;; Extract and render each pdf entry from response
|
|
|
|
|
(mapcar (lambda (json-response-item)
|
|
|
|
|
(thread-last
|
|
|
|
|
;; Extract pdf entry from each item in json response
|
|
|
|
|
(cdr (assoc 'compiled (assoc 'additional json-response-item)))
|
|
|
|
|
;; Format pdf entry as a org entry string
|
|
|
|
|
(format "** %s\n\n"))))
|
|
|
|
|
;; Render entries into org formatted string with query set as as top level heading
|
|
|
|
|
(format "* %s\n%s\n" query)
|
|
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
|
(replace-regexp-in-string "^[\(\) ]" "")))
|
|
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
|
(defun khoj--extract-entries-as-images (json-response query)
|
2022-08-13 17:29:06 +00:00
|
|
|
|
"Convert JSON-RESPONSE, QUERY from API to html with images."
|
2023-01-26 21:59:44 +00:00
|
|
|
|
(let ((image-results-buffer-html-format-str "<html>\n<body>\n<h1>%s</h1>%s\n\n</body>\n</html>")
|
|
|
|
|
;; Format string to wrap images into html img, href tags with metadata in headings
|
|
|
|
|
(image-result-html-format-str "\n\n<h2>Score: %s Meta: %s Image: %s</h2>\n\n<a href=\"%s\">\n<img src=\"%s?%s\" width=%s height=%s>\n</a>"))
|
|
|
|
|
(thread-last
|
|
|
|
|
json-response
|
|
|
|
|
;; Extract each image entry from response and render as html
|
|
|
|
|
(mapcar (lambda (json-response-item)
|
|
|
|
|
(let ((score (cdr (assoc 'score json-response-item)))
|
|
|
|
|
(metadata_score (cdr (assoc 'metadata_score (assoc 'additional json-response-item))))
|
|
|
|
|
(image_score (cdr (assoc 'image_score (assoc 'additional json-response-item))))
|
|
|
|
|
(image_url (concat khoj-server-url (cdr (assoc 'entry json-response-item)))))
|
|
|
|
|
;; Wrap images into html img, href tags with metadata in headings
|
|
|
|
|
(format image-result-html-format-str
|
|
|
|
|
;; image scores metadata
|
|
|
|
|
score metadata_score image_score
|
|
|
|
|
;; image url
|
|
|
|
|
image_url image_url (random 10000)
|
|
|
|
|
;; image dimensions
|
|
|
|
|
khoj-image-width khoj-image-height))))
|
|
|
|
|
;; Collate entries into single html page string
|
|
|
|
|
(format image-results-buffer-html-format-str query)
|
|
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
|
(replace-regexp-in-string "^[\(\) ]" "")
|
|
|
|
|
;; remove trailing (, ) or SPC from extracted entries string
|
|
|
|
|
(replace-regexp-in-string "[\(\) ]$" ""))))
|
2021-09-10 08:01:23 +00:00
|
|
|
|
|
2023-02-24 22:16:13 +00:00
|
|
|
|
(defun khoj--extract-entries (json-response query)
|
|
|
|
|
"Convert JSON-RESPONSE, QUERY from API to text entries."
|
|
|
|
|
(thread-last json-response
|
|
|
|
|
;; extract and render entries from API response
|
2023-06-28 19:52:28 +00:00
|
|
|
|
(mapcar (lambda (json-response-item)
|
|
|
|
|
(thread-last
|
|
|
|
|
;; Extract pdf entry from each item in json response
|
|
|
|
|
(cdr (assoc 'entry json-response-item))
|
|
|
|
|
(format "%s\n\n")
|
|
|
|
|
;; Standardize results to 2nd level heading for consistent rendering
|
|
|
|
|
(replace-regexp-in-string "^\*+" "")
|
|
|
|
|
;; Standardize results to 2nd level heading for consistent rendering
|
|
|
|
|
(replace-regexp-in-string "^\#+" "")
|
|
|
|
|
;; Format entries as org entry string
|
|
|
|
|
(format "** %s"))))
|
2023-02-24 22:16:13 +00:00
|
|
|
|
;; Set query as heading in rendered results buffer
|
2023-06-28 19:52:28 +00:00
|
|
|
|
(format "* %s\n%s\n" query)
|
2023-02-24 22:16:13 +00:00
|
|
|
|
;; remove leading (, ) or SPC from extracted entries string
|
|
|
|
|
(replace-regexp-in-string "^[\(\) ]" "")
|
|
|
|
|
;; remove trailing (, ) or SPC from extracted entries string
|
|
|
|
|
(replace-regexp-in-string "[\(\) ]$" "")))
|
|
|
|
|
|
2023-01-19 01:13:49 +00:00
|
|
|
|
(defun khoj--buffer-name-to-content-type (buffer-name)
|
|
|
|
|
"Infer content type based on BUFFER-NAME."
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(let ((enabled-content-types (khoj--get-enabled-content-types))
|
2022-08-07 15:04:26 +00:00
|
|
|
|
(file-extension (file-name-extension buffer-name)))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
(cond
|
2022-08-07 15:53:14 +00:00
|
|
|
|
((and (member 'org enabled-content-types) (equal file-extension "org")) "org")
|
2023-07-17 22:49:20 +00:00
|
|
|
|
((and (member 'pdf enabled-content-types) (equal file-extension "pdf")) "pdf")
|
2022-08-07 15:53:14 +00:00
|
|
|
|
((and (member 'markdown enabled-content-types) (or (equal file-extension "markdown") (equal file-extension "md"))) "markdown")
|
2023-01-19 01:13:49 +00:00
|
|
|
|
(t khoj-default-content-type))))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
2023-01-20 02:36:54 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
;; --------------
|
|
|
|
|
;; Query Khoj API
|
|
|
|
|
;; --------------
|
|
|
|
|
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(defun khoj--post-new-config (config)
|
|
|
|
|
"Configure khoj server with provided CONFIG."
|
|
|
|
|
;; POST provided config to khoj server
|
|
|
|
|
(let ((url-request-method "POST")
|
|
|
|
|
(url-request-extra-headers '(("Content-Type" . "application/json")))
|
2023-07-11 08:06:05 +00:00
|
|
|
|
(url-request-data (encode-coding-string (json-encode-alist config) 'utf-8))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
(config-url (format "%s/api/config/data" khoj-server-url)))
|
|
|
|
|
(with-current-buffer (url-retrieve-synchronously config-url)
|
|
|
|
|
(buffer-string)))
|
|
|
|
|
;; Update index on khoj server after configuration update
|
|
|
|
|
(let ((khoj--server-ready? nil))
|
2023-07-19 02:32:06 +00:00
|
|
|
|
(url-retrieve (format "%s/api/update?client=emacs" khoj-server-url) #'identity)))
|
2023-03-26 11:44:10 +00:00
|
|
|
|
|
2022-08-07 15:53:14 +00:00
|
|
|
|
(defun khoj--get-enabled-content-types ()
|
2022-08-13 17:29:06 +00:00
|
|
|
|
"Get content types enabled for search from API."
|
2023-02-24 10:01:51 +00:00
|
|
|
|
(let ((config-url (format "%s/api/config/types" khoj-server-url))
|
2022-09-19 19:46:46 +00:00
|
|
|
|
(url-request-method "GET"))
|
2022-08-07 14:28:43 +00:00
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(url-insert-file-contents config-url)
|
2023-02-24 10:01:51 +00:00
|
|
|
|
(thread-last
|
|
|
|
|
(json-parse-buffer :object-type 'alist)
|
2023-03-22 22:25:34 +00:00
|
|
|
|
(mapcar #'intern)))))
|
2022-08-07 14:28:43 +00:00
|
|
|
|
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(defun khoj--construct-search-api-query (query content-type &optional rerank)
|
2023-03-22 07:13:17 +00:00
|
|
|
|
"Construct Search API Query.
|
|
|
|
|
Use QUERY, CONTENT-TYPE and (optional) RERANK as query params"
|
2022-07-27 02:58:36 +00:00
|
|
|
|
(let ((rerank (or rerank "false"))
|
|
|
|
|
(encoded-query (url-hexify-string query)))
|
2023-06-28 19:52:28 +00:00
|
|
|
|
(format "%s/api/search?q=%s&t=%s&r=%s&n=%s&client=emacs" khoj-server-url encoded-query content-type rerank khoj-results-count)))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(defun khoj--query-search-api-and-render-results (query-url content-type query buffer-name)
|
2023-03-22 07:13:17 +00:00
|
|
|
|
"Query Khoj Search with QUERY-URL.
|
|
|
|
|
Render results in BUFFER-NAME using QUERY, CONTENT-TYPE."
|
2022-07-27 00:14:14 +00:00
|
|
|
|
;; get json response from api
|
|
|
|
|
(with-current-buffer buffer-name
|
2022-09-19 19:46:46 +00:00
|
|
|
|
(let ((inhibit-read-only t)
|
|
|
|
|
(url-request-method "GET"))
|
2022-07-27 00:14:14 +00:00
|
|
|
|
(erase-buffer)
|
|
|
|
|
(url-insert-file-contents query-url)))
|
|
|
|
|
;; render json response into formatted entries
|
|
|
|
|
(with-current-buffer buffer-name
|
|
|
|
|
(let ((inhibit-read-only t)
|
|
|
|
|
(json-response (json-parse-buffer :object-type 'alist)))
|
|
|
|
|
(erase-buffer)
|
|
|
|
|
(insert
|
2023-07-02 23:21:21 +00:00
|
|
|
|
(cond ((equal content-type "org") (khoj--extract-entries-as-org json-response query))
|
2023-01-19 01:13:49 +00:00
|
|
|
|
((equal content-type "markdown") (khoj--extract-entries-as-markdown json-response query))
|
2023-06-01 12:34:24 +00:00
|
|
|
|
((equal content-type "pdf") (khoj--extract-entries-as-pdf json-response query))
|
2023-01-19 01:13:49 +00:00
|
|
|
|
((equal content-type "image") (khoj--extract-entries-as-images json-response query))
|
2023-02-24 22:16:13 +00:00
|
|
|
|
(t (khoj--extract-entries json-response query))))
|
2023-06-28 19:52:28 +00:00
|
|
|
|
(cond ((or (equal content-type "all")
|
|
|
|
|
(equal content-type "pdf")
|
2023-06-01 12:34:24 +00:00
|
|
|
|
(equal content-type "org"))
|
|
|
|
|
(progn (visual-line-mode)
|
|
|
|
|
(org-mode)
|
|
|
|
|
(setq-local
|
|
|
|
|
org-startup-folded "showall"
|
|
|
|
|
org-hide-leading-stars t
|
|
|
|
|
org-startup-with-inline-images t)
|
|
|
|
|
(org-set-startup-visibility)))
|
2023-02-12 13:33:50 +00:00
|
|
|
|
((equal content-type "markdown") (progn (markdown-mode)
|
|
|
|
|
(visual-line-mode)))
|
2023-01-19 01:13:49 +00:00
|
|
|
|
((equal content-type "image") (progn (shr-render-region (point-min) (point-max))
|
2022-07-26 23:05:00 +00:00
|
|
|
|
(goto-char (point-min))))
|
|
|
|
|
(t (fundamental-mode))))
|
2022-07-27 00:14:14 +00:00
|
|
|
|
(read-only-mode t)))
|
|
|
|
|
|
2023-03-22 07:13:17 +00:00
|
|
|
|
|
|
|
|
|
;; ----------------
|
|
|
|
|
;; Khoj Chat
|
|
|
|
|
;; ----------------
|
|
|
|
|
|
|
|
|
|
(defun khoj--chat ()
|
|
|
|
|
"Chat with Khoj."
|
2023-03-24 18:43:11 +00:00
|
|
|
|
(interactive)
|
2023-03-24 06:43:46 +00:00
|
|
|
|
(when (not (get-buffer khoj--chat-buffer-name))
|
|
|
|
|
(khoj--load-chat-history khoj--chat-buffer-name))
|
|
|
|
|
(switch-to-buffer khoj--chat-buffer-name)
|
2023-03-22 07:13:17 +00:00
|
|
|
|
(let ((query (read-string "Query: ")))
|
2023-03-24 06:43:46 +00:00
|
|
|
|
(when (not (string-empty-p query))
|
|
|
|
|
(khoj--query-chat-api-and-render-messages query khoj--chat-buffer-name))))
|
2023-03-22 07:13:17 +00:00
|
|
|
|
|
2023-03-22 17:08:17 +00:00
|
|
|
|
(defun khoj--load-chat-history (buffer-name)
|
2023-03-24 22:28:13 +00:00
|
|
|
|
"Load Khoj Chat conversation history into BUFFER-NAME."
|
2023-07-09 17:12:09 +00:00
|
|
|
|
(let ((json-response (cdr (assoc 'response (khoj--get-chat-history-api)))))
|
2023-03-22 17:08:17 +00:00
|
|
|
|
(with-current-buffer (get-buffer-create buffer-name)
|
|
|
|
|
(erase-buffer)
|
2023-03-25 23:03:30 +00:00
|
|
|
|
(insert "* Khoj Chat\n")
|
2023-03-22 17:08:17 +00:00
|
|
|
|
(thread-last
|
|
|
|
|
json-response
|
|
|
|
|
;; generate chat messages from Khoj Chat API response
|
|
|
|
|
(mapcar #'khoj--render-chat-response)
|
|
|
|
|
;; insert chat messages into Khoj Chat Buffer
|
2023-03-22 22:25:34 +00:00
|
|
|
|
(mapc #'insert))
|
2023-03-25 22:28:44 +00:00
|
|
|
|
(progn
|
2023-03-25 23:03:30 +00:00
|
|
|
|
(org-mode)
|
2023-03-25 22:28:44 +00:00
|
|
|
|
(khoj--add-hover-text-to-footnote-refs (point-min))
|
|
|
|
|
|
|
|
|
|
;; render reference footnotes as superscript
|
|
|
|
|
(setq-local
|
2023-03-25 23:03:30 +00:00
|
|
|
|
org-startup-folded "showall"
|
|
|
|
|
org-hide-leading-stars t
|
2023-03-25 22:28:44 +00:00
|
|
|
|
org-use-sub-superscripts '{}
|
|
|
|
|
org-pretty-entities-include-sub-superscripts t
|
|
|
|
|
org-pretty-entities t)
|
2023-03-25 23:03:30 +00:00
|
|
|
|
(org-set-startup-visibility)
|
2023-03-25 22:28:44 +00:00
|
|
|
|
|
|
|
|
|
;; create khoj chat shortcut keybindings
|
|
|
|
|
(use-local-map (copy-keymap org-mode-map))
|
|
|
|
|
(local-set-key (kbd "m") #'khoj--chat)
|
|
|
|
|
(local-set-key (kbd "C-x m") #'khoj--chat)
|
|
|
|
|
|
2023-03-25 23:03:30 +00:00
|
|
|
|
;; enable minor modes for khoj chat
|
2023-03-25 22:28:44 +00:00
|
|
|
|
(visual-line-mode)
|
|
|
|
|
(read-only-mode t)))))
|
2023-03-22 17:08:17 +00:00
|
|
|
|
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(defun khoj--add-hover-text-to-footnote-refs (start-pos)
|
2023-03-24 22:28:13 +00:00
|
|
|
|
"Show footnote defs on mouse hover on footnote refs from START-POS."
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(org-with-wide-buffer
|
|
|
|
|
(goto-char start-pos)
|
|
|
|
|
(while (re-search-forward org-footnote-re nil t)
|
|
|
|
|
(backward-char)
|
|
|
|
|
(let* ((context (org-element-context))
|
|
|
|
|
(label (org-element-property :label context))
|
|
|
|
|
(footnote-def (nth 3 (org-footnote-get-definition label)))
|
|
|
|
|
(footnote-width (if (< (length footnote-def) 70) nil 70))
|
|
|
|
|
(begin-pos (org-element-property :begin context))
|
|
|
|
|
(end-pos (org-element-property :end context))
|
|
|
|
|
(overlay (make-overlay begin-pos end-pos)))
|
|
|
|
|
(when (memq (org-element-type context)
|
|
|
|
|
'(footnote-reference))
|
|
|
|
|
(-->
|
|
|
|
|
footnote-def
|
2023-03-24 22:28:13 +00:00
|
|
|
|
;; truncate footnote definition if required
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(substring it 0 footnote-width)
|
2023-03-24 22:28:13 +00:00
|
|
|
|
;; append continuation suffix if truncated
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(concat it (if footnote-width "..." ""))
|
2023-03-24 22:28:13 +00:00
|
|
|
|
;; show definition on hover on footnote reference
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(overlay-put overlay 'help-echo it)))))))
|
|
|
|
|
|
2023-03-22 07:13:17 +00:00
|
|
|
|
(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."
|
|
|
|
|
;; render json response into formatted chat messages
|
2023-03-24 06:43:46 +00:00
|
|
|
|
(with-current-buffer (get-buffer buffer-name)
|
|
|
|
|
(let ((inhibit-read-only t)
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(new-content-start-pos (point-max))
|
2023-03-24 08:27:30 +00:00
|
|
|
|
(query-time (format-time-string "%F %T"))
|
2023-03-24 06:43:46 +00:00
|
|
|
|
(json-response (khoj--query-chat-api query)))
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(goto-char new-content-start-pos)
|
2023-03-24 06:43:46 +00:00
|
|
|
|
(insert
|
2023-03-24 08:27:30 +00:00
|
|
|
|
(khoj--render-chat-message query "you" query-time)
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(khoj--render-chat-response json-response))
|
|
|
|
|
(khoj--add-hover-text-to-footnote-refs new-content-start-pos))
|
2023-03-24 22:28:13 +00:00
|
|
|
|
(progn
|
|
|
|
|
(org-set-startup-visibility)
|
|
|
|
|
(visual-line-mode)
|
2023-06-21 08:13:50 +00:00
|
|
|
|
(re-search-backward "^\*+ 🏮" nil t))))
|
2023-03-22 07:13:17 +00:00
|
|
|
|
|
|
|
|
|
(defun khoj--query-chat-api (query)
|
|
|
|
|
"Send QUERY to Khoj Chat API."
|
|
|
|
|
(let* ((url-request-method "GET")
|
|
|
|
|
(encoded-query (url-hexify-string query))
|
2023-07-09 17:12:09 +00:00
|
|
|
|
(query-url (format "%s/api/chat?q=%s&n=%s&client=emacs" khoj-server-url encoded-query khoj-results-count)))
|
|
|
|
|
(with-temp-buffer
|
|
|
|
|
(condition-case ex
|
|
|
|
|
(progn
|
|
|
|
|
(url-insert-file-contents query-url)
|
|
|
|
|
(json-parse-buffer :object-type 'alist))
|
|
|
|
|
('file-error (cond ((string-match "Internal server error" (nth 2 ex))
|
|
|
|
|
(message "Chat processor not configured. Configure OpenAI API key and restart it. Exception: [%s]" ex))
|
|
|
|
|
(t (message "Chat exception: [%s]" ex))))))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(defun khoj--get-chat-history-api ()
|
|
|
|
|
"Send QUERY to Khoj Chat History API."
|
|
|
|
|
(let* ((url-request-method "GET")
|
|
|
|
|
(query-url (format "%s/api/chat/history?client=emacs" khoj-server-url)))
|
2023-03-22 07:13:17 +00:00
|
|
|
|
(with-temp-buffer
|
2023-03-26 19:08:21 +00:00
|
|
|
|
(condition-case ex
|
|
|
|
|
(progn
|
|
|
|
|
(url-insert-file-contents query-url)
|
|
|
|
|
(json-parse-buffer :object-type 'alist))
|
|
|
|
|
('file-error (cond ((string-match "Internal server error" (nth 2 ex))
|
|
|
|
|
(message "Chat processor not configured. Configure OpenAI API key and restart it. Exception: [%s]" ex))
|
|
|
|
|
(t (message "Chat exception: [%s]" ex))))))))
|
|
|
|
|
|
2023-03-22 07:13:17 +00:00
|
|
|
|
|
|
|
|
|
(defun khoj--render-chat-message (message sender &optional receive-date)
|
|
|
|
|
"Render chat messages as `org-mode' list item.
|
|
|
|
|
MESSAGE is the text of the chat message.
|
|
|
|
|
SENDER is the message sender.
|
|
|
|
|
RECEIVE-DATE is the message receive date."
|
2023-03-22 18:00:43 +00:00
|
|
|
|
(let ((first-message-line (car (split-string message "\n" t)))
|
2023-03-24 07:25:00 +00:00
|
|
|
|
(rest-message-lines (string-join (cdr (split-string message "\n" t)) "\n"))
|
2023-03-22 18:00:43 +00:00
|
|
|
|
(heading-level (if (equal sender "you") "**" "***"))
|
2023-06-21 08:13:50 +00:00
|
|
|
|
(emojified-sender (if (equal sender "you") "🤔 *You*" "🏮 *Khoj*"))
|
2023-03-24 14:24:42 +00:00
|
|
|
|
(suffix-newlines (if (equal sender "khoj") "\n\n" ""))
|
2023-03-22 22:25:34 +00:00
|
|
|
|
(received (or receive-date (format-time-string "%F %T"))))
|
2023-03-24 14:24:42 +00:00
|
|
|
|
(format "%s %s: %s\n :PROPERTIES:\n :RECEIVED: [%s]\n :END:\n%s\n%s"
|
2023-03-22 18:00:43 +00:00
|
|
|
|
heading-level
|
2023-03-24 14:24:42 +00:00
|
|
|
|
emojified-sender
|
2023-03-22 18:00:43 +00:00
|
|
|
|
first-message-line
|
|
|
|
|
received
|
2023-03-24 14:24:42 +00:00
|
|
|
|
rest-message-lines
|
|
|
|
|
suffix-newlines)))
|
2023-03-22 07:13:17 +00:00
|
|
|
|
|
2023-03-24 07:25:00 +00:00
|
|
|
|
(defun khoj--generate-reference (reference)
|
|
|
|
|
"Create `org-mode' footnotes with REFERENCE."
|
|
|
|
|
(setq khoj--reference-count (1+ khoj--reference-count))
|
|
|
|
|
(cons
|
2023-03-24 10:51:39 +00:00
|
|
|
|
(propertize (format "^{ [fn:%x]}" khoj--reference-count) 'help-echo reference)
|
2023-03-24 07:25:00 +00:00
|
|
|
|
(thread-last
|
|
|
|
|
reference
|
2023-06-02 04:59:12 +00:00
|
|
|
|
;; remove filename top heading line from reference
|
|
|
|
|
;; prevents actual reference heading in next line jumping out of references footnote section
|
|
|
|
|
(replace-regexp-in-string "^\* .*\n" "")
|
|
|
|
|
;; remove multiple, consecutive empty lines from reference
|
2023-03-24 07:25:00 +00:00
|
|
|
|
(replace-regexp-in-string "\n\n" "\n")
|
|
|
|
|
(format "\n[fn:%x] %s" khoj--reference-count))))
|
2023-03-22 07:13:17 +00:00
|
|
|
|
|
|
|
|
|
(defun khoj--render-chat-response (json-response)
|
|
|
|
|
"Render chat message using JSON-RESPONSE from Khoj Chat API."
|
2023-03-22 17:08:17 +00:00
|
|
|
|
(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)))
|
2023-03-24 14:55:22 +00:00
|
|
|
|
(references (or (cdr (assoc 'context json-response)) '()))
|
|
|
|
|
(footnotes (mapcar #'khoj--generate-reference references))
|
2023-03-24 07:25:00 +00:00
|
|
|
|
(footnote-links (mapcar #'car footnotes))
|
|
|
|
|
(footnote-defs (mapcar #'cdr footnotes)))
|
2023-03-22 07:13:17 +00:00
|
|
|
|
(thread-first
|
2023-03-24 14:24:42 +00:00
|
|
|
|
;; concatenate khoj message and references from API
|
|
|
|
|
(concat
|
|
|
|
|
message
|
|
|
|
|
;; append reference links to khoj message
|
|
|
|
|
(string-join footnote-links "")
|
|
|
|
|
;; append reference sub-section to khoj message and fold it
|
|
|
|
|
(if footnote-defs "\n**** References\n:PROPERTIES:\n:VISIBILITY: folded\n:END:" "")
|
2023-03-24 22:28:13 +00:00
|
|
|
|
;; append reference definitions to references subsection
|
2023-03-24 14:24:42 +00:00
|
|
|
|
(string-join footnote-defs " "))
|
2023-03-22 17:08:17 +00:00
|
|
|
|
;; Render chat message using data obtained from API
|
|
|
|
|
(khoj--render-chat-message sender receive-date))))
|
2023-03-22 07:13:17 +00:00
|
|
|
|
|
2023-01-20 02:36:54 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
;; ------------------
|
|
|
|
|
;; Incremental Search
|
|
|
|
|
;; ------------------
|
|
|
|
|
|
2022-07-27 16:08:37 +00:00
|
|
|
|
(defun khoj--incremental-search (&optional rerank)
|
2022-08-13 17:29:06 +00:00
|
|
|
|
"Perform Incremental Search on Khoj. Allow optional RERANK of results."
|
2022-07-27 16:08:37 +00:00
|
|
|
|
(let* ((rerank-str (cond (rerank "true") (t "false")))
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(khoj-buffer-name (get-buffer-create khoj--search-buffer-name))
|
2022-07-27 00:14:14 +00:00
|
|
|
|
(query (minibuffer-contents-no-properties))
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(query-url (khoj--construct-search-api-query query khoj--content-type rerank-str)))
|
2022-08-05 16:34:12 +00:00
|
|
|
|
;; Query khoj API only when user in khoj minibuffer and non-empty query
|
|
|
|
|
;; Prevents querying if
|
|
|
|
|
;; 1. user hasn't started typing query
|
|
|
|
|
;; 2. during recursive edits
|
|
|
|
|
;; 3. with contents of other buffers user may jump to
|
2022-08-15 15:41:12 +00:00
|
|
|
|
;; 4. search not triggered right after rerank
|
|
|
|
|
;; ignore to not overwrite reranked results before the user even sees them
|
|
|
|
|
(if khoj--rerank
|
|
|
|
|
(setq khoj--rerank nil)
|
|
|
|
|
(when
|
|
|
|
|
(and
|
|
|
|
|
(not (equal query ""))
|
|
|
|
|
(active-minibuffer-window)
|
|
|
|
|
(equal (current-buffer) khoj--minibuffer-window))
|
2022-07-27 23:37:16 +00:00
|
|
|
|
(progn
|
|
|
|
|
(when rerank
|
2022-08-15 15:41:12 +00:00
|
|
|
|
(setq khoj--rerank t)
|
2023-04-28 03:15:13 +00:00
|
|
|
|
(message "khoj.el: Rerank Results"))
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(khoj--query-search-api-and-render-results
|
2022-07-27 23:37:16 +00:00
|
|
|
|
query-url
|
2023-01-21 01:11:00 +00:00
|
|
|
|
khoj--content-type
|
|
|
|
|
query
|
2022-08-15 15:41:12 +00:00
|
|
|
|
khoj-buffer-name))))))
|
2022-07-27 16:08:37 +00:00
|
|
|
|
|
2022-08-13 17:29:06 +00:00
|
|
|
|
(defun khoj--delete-open-network-connections-to-server ()
|
|
|
|
|
"Delete all network connections to khoj server."
|
2022-08-03 12:14:34 +00:00
|
|
|
|
(dolist (proc (process-list))
|
|
|
|
|
(let ((proc-buf (buffer-name (process-buffer proc)))
|
2022-08-09 17:49:34 +00:00
|
|
|
|
(khoj-network-proc-buf (string-join (split-string khoj-server-url "://") " ")))
|
2022-08-03 12:14:34 +00:00
|
|
|
|
(when (string-match (format "%s" khoj-network-proc-buf) proc-buf)
|
|
|
|
|
(delete-process proc)))))
|
|
|
|
|
|
2022-07-27 16:08:37 +00:00
|
|
|
|
(defun khoj--teardown-incremental-search ()
|
2022-08-15 03:14:28 +00:00
|
|
|
|
"Teardown hooks used for incremental search."
|
2023-04-28 03:15:13 +00:00
|
|
|
|
(message "khoj.el: Teardown Incremental Search")
|
2022-07-27 16:08:37 +00:00
|
|
|
|
;; unset khoj minibuffer window
|
|
|
|
|
(setq khoj--minibuffer-window nil)
|
2022-08-13 17:29:06 +00:00
|
|
|
|
;; delete open connections to khoj server
|
|
|
|
|
(khoj--delete-open-network-connections-to-server)
|
2022-07-27 16:08:37 +00:00
|
|
|
|
;; remove hooks for khoj incremental query and self
|
|
|
|
|
(remove-hook 'post-command-hook #'khoj--incremental-search)
|
|
|
|
|
(remove-hook 'minibuffer-exit-hook #'khoj--teardown-incremental-search))
|
2022-07-27 00:14:14 +00:00
|
|
|
|
|
2023-01-18 23:59:19 +00:00
|
|
|
|
(defun khoj-incremental ()
|
2023-07-02 23:21:21 +00:00
|
|
|
|
"Natural, Incremental Search for your personal notes and documents."
|
2022-07-27 00:14:14 +00:00
|
|
|
|
(interactive)
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(let* ((khoj-buffer-name (get-buffer-create khoj--search-buffer-name)))
|
2022-07-27 16:08:37 +00:00
|
|
|
|
;; switch to khoj results buffer
|
2022-08-05 14:23:14 +00:00
|
|
|
|
(switch-to-buffer khoj-buffer-name)
|
2022-07-27 16:08:37 +00:00
|
|
|
|
;; open and setup minibuffer for incremental search
|
2022-07-27 00:14:14 +00:00
|
|
|
|
(minibuffer-with-setup-hook
|
|
|
|
|
(lambda ()
|
2022-08-05 15:47:20 +00:00
|
|
|
|
;; Add khoj keybindings for configuring search to minibuffer keybindings
|
|
|
|
|
(khoj--make-search-keymap minibuffer-local-map)
|
2022-08-05 17:15:51 +00:00
|
|
|
|
;; Display information on keybindings to customize khoj search
|
|
|
|
|
(khoj--display-keybinding-info)
|
2022-07-27 16:08:37 +00:00
|
|
|
|
;; set current (mini-)buffer entered as khoj minibuffer
|
|
|
|
|
;; used to query khoj API only when user in khoj minibuffer
|
|
|
|
|
(setq khoj--minibuffer-window (current-buffer))
|
|
|
|
|
(add-hook 'post-command-hook #'khoj--incremental-search) ; do khoj incremental search after every user action
|
|
|
|
|
(add-hook 'minibuffer-exit-hook #'khoj--teardown-incremental-search)) ; teardown khoj incremental search on minibuffer exit
|
2022-07-27 00:14:14 +00:00
|
|
|
|
(read-string khoj--query-prompt))))
|
2022-07-26 22:48:27 +00:00
|
|
|
|
|
2023-01-20 08:14:16 +00:00
|
|
|
|
|
|
|
|
|
;; --------------
|
|
|
|
|
;; Similar Search
|
|
|
|
|
;; --------------
|
|
|
|
|
|
2023-01-20 23:40:15 +00:00
|
|
|
|
(defun khoj--get-current-outline-entry-text ()
|
|
|
|
|
"Get text under current outline section."
|
2023-01-23 21:41:58 +00:00
|
|
|
|
(string-trim
|
|
|
|
|
;; get text of current outline entry
|
|
|
|
|
(cond
|
|
|
|
|
;; when at heading of entry
|
|
|
|
|
((looking-at outline-regexp)
|
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
|
(point)
|
|
|
|
|
(save-excursion (outline-next-heading) (point))))
|
|
|
|
|
;; when within entry
|
|
|
|
|
(t (buffer-substring-no-properties
|
|
|
|
|
(save-excursion (outline-previous-heading) (point))
|
|
|
|
|
(save-excursion (outline-next-heading) (point)))))))
|
2023-01-20 23:40:15 +00:00
|
|
|
|
|
|
|
|
|
(defun khoj--get-current-paragraph-text ()
|
2023-01-23 21:41:58 +00:00
|
|
|
|
"Get trimmed text in current paragraph at point.
|
|
|
|
|
Paragraph only starts at first text after blank line."
|
|
|
|
|
(string-trim
|
|
|
|
|
(cond
|
|
|
|
|
;; when at end of a middle paragraph
|
|
|
|
|
((and (looking-at paragraph-start) (not (equal (point) (point-min))))
|
|
|
|
|
(buffer-substring-no-properties
|
|
|
|
|
(save-excursion (backward-paragraph) (point))
|
|
|
|
|
(point)))
|
|
|
|
|
;; else
|
|
|
|
|
(t (thing-at-point 'paragraph t)))))
|
|
|
|
|
|
2023-01-20 23:40:15 +00:00
|
|
|
|
|
|
|
|
|
(defun khoj--find-similar (&optional content-type)
|
|
|
|
|
"Find items of CONTENT-TYPE in khoj index similar to text surrounding point."
|
|
|
|
|
(interactive)
|
2023-01-20 08:14:16 +00:00
|
|
|
|
(let* ((rerank "true")
|
2023-01-20 23:40:15 +00:00
|
|
|
|
;; set content type to: specified > based on current buffer > default type
|
|
|
|
|
(content-type (or content-type (khoj--buffer-name-to-content-type (buffer-name))))
|
|
|
|
|
;; get text surrounding current point based on the major mode context
|
|
|
|
|
(query (cond
|
|
|
|
|
;; get section outline derived mode like org or markdown
|
|
|
|
|
((or (derived-mode-p 'outline-mode) (equal major-mode 'markdown-mode))
|
|
|
|
|
(khoj--get-current-outline-entry-text))
|
|
|
|
|
;; get paragraph, if in text mode
|
|
|
|
|
(t
|
|
|
|
|
(khoj--get-current-paragraph-text))))
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(query-url (khoj--construct-search-api-query query content-type rerank))
|
2023-01-20 23:40:15 +00:00
|
|
|
|
;; extract heading to show in result buffer from query
|
|
|
|
|
(query-title
|
|
|
|
|
(format "Similar to: %s"
|
|
|
|
|
(replace-regexp-in-string "^[#\\*]* " "" (car (split-string query "\n")))))
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(buffer-name (get-buffer-create khoj--search-buffer-name)))
|
2023-01-20 23:40:15 +00:00
|
|
|
|
(progn
|
2023-03-22 03:01:14 +00:00
|
|
|
|
(khoj--query-search-api-and-render-results
|
2023-01-20 23:40:15 +00:00
|
|
|
|
query-url
|
2023-01-21 01:11:00 +00:00
|
|
|
|
content-type
|
|
|
|
|
query-title
|
2023-01-20 23:40:15 +00:00
|
|
|
|
buffer-name)
|
|
|
|
|
(switch-to-buffer buffer-name)
|
|
|
|
|
(goto-char (point-min)))))
|
2023-01-19 04:04:05 +00:00
|
|
|
|
|
2023-01-20 02:36:54 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
;; ---------
|
|
|
|
|
;; Khoj Menu
|
|
|
|
|
;; ---------
|
|
|
|
|
|
2023-01-18 23:59:19 +00:00
|
|
|
|
(transient-define-argument khoj--content-type-switch ()
|
|
|
|
|
:class 'transient-switches
|
|
|
|
|
:argument-format "--content-type=%s"
|
|
|
|
|
:argument-regexp ".+"
|
2023-01-20 23:40:15 +00:00
|
|
|
|
;; set content type to: last used > based on current buffer > default type
|
2023-01-19 01:13:49 +00:00
|
|
|
|
:init-value (lambda (obj) (oset obj value (format "--content-type=%s" (or khoj--content-type (khoj--buffer-name-to-content-type (buffer-name))))))
|
2023-01-19 01:00:12 +00:00
|
|
|
|
;; dynamically set choices to content types enabled on khoj backend
|
2023-07-02 23:49:51 +00:00
|
|
|
|
:choices (or (ignore-errors (mapcar #'symbol-name (khoj--get-enabled-content-types))) '("all" "org" "markdown" "pdf" "image")))
|
2023-01-18 23:59:19 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
(transient-define-suffix khoj--search-command (&optional args)
|
2023-01-18 23:59:19 +00:00
|
|
|
|
(interactive (list (transient-args transient-current-command)))
|
|
|
|
|
(progn
|
2023-01-20 23:40:15 +00:00
|
|
|
|
;; set content type to: specified > last used > based on current buffer > default type
|
2023-01-19 01:13:49 +00:00
|
|
|
|
(setq khoj--content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
|
2023-01-20 23:40:15 +00:00
|
|
|
|
;; set results count to: specified > last used > to default
|
2023-01-18 23:59:19 +00:00
|
|
|
|
(setq khoj-results-count (or (transient-arg-value "--results-count=" args) khoj-results-count))
|
|
|
|
|
;; trigger incremental search
|
|
|
|
|
(call-interactively #'khoj-incremental)))
|
|
|
|
|
|
2023-01-20 23:40:15 +00:00
|
|
|
|
(transient-define-suffix khoj--find-similar-command (&optional args)
|
|
|
|
|
"Find items similar to current item at point."
|
|
|
|
|
(interactive (list (transient-args transient-current-command)))
|
|
|
|
|
(progn
|
|
|
|
|
;; set content type to: specified > last used > based on current buffer > default type
|
|
|
|
|
(setq khoj--content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
|
|
|
|
|
;; set results count to: specified > last used > to default
|
|
|
|
|
(setq khoj-results-count (or (transient-arg-value "--results-count=" args) khoj-results-count))
|
|
|
|
|
(khoj--find-similar khoj--content-type)))
|
2023-01-20 08:14:16 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
(transient-define-suffix khoj--update-command (&optional args)
|
|
|
|
|
"Call khoj API to update index of specified content type."
|
2023-01-19 02:07:59 +00:00
|
|
|
|
(interactive (list (transient-args transient-current-command)))
|
|
|
|
|
(let* ((force-update (if (member "--force-update" args) "true" "false"))
|
2023-01-20 23:40:15 +00:00
|
|
|
|
;; set content type to: specified > last used > based on current buffer > default type
|
2023-01-19 02:07:59 +00:00
|
|
|
|
(content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
|
2023-07-19 02:32:06 +00:00
|
|
|
|
(type-query (if (equal content-type "all") "" (format "t=%s" content-type)))
|
|
|
|
|
(update-url (format "%s/api/update?%s&force=%s&client=emacs" khoj-server-url type-query force-update))
|
2023-01-19 02:07:59 +00:00
|
|
|
|
(url-request-method "GET"))
|
2023-01-20 23:40:15 +00:00
|
|
|
|
(progn
|
|
|
|
|
(setq khoj--content-type content-type)
|
2023-04-28 03:15:13 +00:00
|
|
|
|
(url-retrieve update-url (lambda (_) (message "khoj.el: %s index %supdated!" content-type (if (member "--force-update" args) "force " "")))))))
|
2023-01-18 23:59:19 +00:00
|
|
|
|
|
2023-03-22 22:25:34 +00:00
|
|
|
|
(transient-define-suffix khoj--chat-command (&optional _)
|
2023-03-22 07:13:17 +00:00
|
|
|
|
"Command to Chat with Khoj."
|
|
|
|
|
(interactive (list (transient-args transient-current-command)))
|
|
|
|
|
(khoj--chat))
|
|
|
|
|
|
2023-03-26 18:11:43 +00:00
|
|
|
|
(transient-define-prefix khoj--menu ()
|
2023-01-19 04:04:05 +00:00
|
|
|
|
"Create Khoj Menu to Configure and Execute Commands."
|
2023-03-22 07:13:17 +00:00
|
|
|
|
[["Configure Search"
|
|
|
|
|
("n" "Results Count" "--results-count=" :init-value (lambda (obj) (oset obj value (format "%s" khoj-results-count))))
|
2023-01-19 02:07:59 +00:00
|
|
|
|
("t" "Content Type" khoj--content-type-switch)]
|
2023-01-19 06:02:59 +00:00
|
|
|
|
["Configure Update"
|
2023-01-19 02:07:59 +00:00
|
|
|
|
("-f" "Force Update" "--force-update")]]
|
|
|
|
|
[["Act"
|
2023-03-22 07:13:17 +00:00
|
|
|
|
("c" "Chat" khoj--chat-command)
|
2023-01-19 04:04:05 +00:00
|
|
|
|
("s" "Search" khoj--search-command)
|
2023-01-20 23:40:15 +00:00
|
|
|
|
("f" "Find Similar" khoj--find-similar-command)
|
2023-01-19 05:47:07 +00:00
|
|
|
|
("u" "Update" khoj--update-command)
|
|
|
|
|
("q" "Quit" transient-quit-one)]])
|
2023-01-19 04:04:05 +00:00
|
|
|
|
|
2023-01-20 02:36:54 +00:00
|
|
|
|
|
2023-01-19 04:04:05 +00:00
|
|
|
|
;; ----------
|
|
|
|
|
;; Entrypoint
|
|
|
|
|
;; ----------
|
2023-01-18 23:59:19 +00:00
|
|
|
|
|
2021-09-10 05:10:37 +00:00
|
|
|
|
;;;###autoload
|
2023-01-19 04:04:05 +00:00
|
|
|
|
(defun khoj ()
|
2023-07-02 23:49:51 +00:00
|
|
|
|
"Provide natural, search assistance for your notes, documents and images."
|
2023-01-19 04:04:05 +00:00
|
|
|
|
(interactive)
|
2023-03-27 10:53:08 +00:00
|
|
|
|
(when khoj-auto-setup
|
|
|
|
|
(khoj-setup t))
|
2023-03-26 18:11:43 +00:00
|
|
|
|
(khoj--menu))
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
|
(provide 'khoj)
|
2021-08-16 08:27:46 +00:00
|
|
|
|
|
2022-07-19 14:26:16 +00:00
|
|
|
|
;;; khoj.el ends here
|