Update indexer API endpoint query params for force, content type

New URL query params, `force' and `t' match name of query parameter in
existing Khoj API endpoints

Update Desktop, Obsidian and Emacs client to call using these new API
query params. Set `client' query param from each client for telemetry
visibility
This commit is contained in:
Debanjum Singh Solanky 2023-10-17 04:42:04 -07:00
parent 84654ffc5d
commit 5efae1ad55
5 changed files with 20 additions and 16 deletions

View file

@ -163,7 +163,7 @@ function pushDataToKhoj (regenerate = false) {
const headers = { const headers = {
'x-api-key': 'secret' 'x-api-key': 'secret'
}; };
axios.post(`${hostURL}/api/v1/index/update?regenerate=${regenerate}`, formData, { headers }) axios.post(`${hostURL}/api/v1/index/update?force=${regenerate}&client=desktop`, formData, { headers })
.then(response => { .then(response => {
console.log(response.data); console.log(response.data);
const win = BrowserWindow.getAllWindows()[0]; const win = BrowserWindow.getAllWindows()[0];

View file

@ -537,12 +537,14 @@ CONFIG is json obtained from Khoj config API."
;; Khoj Index Content ;; Khoj Index Content
;; ------------------- ;; -------------------
(defun khoj--server-index-files (&optional file-paths) (defun khoj--server-index-files (&optional force content-type file-paths)
"Send files at `FILE-PATHS' to the Khoj server to index for search and chat." "Send files at `FILE-PATHS' to the Khoj server to index for search and chat.
`FORCE' re-indexes all files of `CONTENT-TYPE' even if they are already indexed."
(interactive) (interactive)
(let ((boundary (format "-------------------------%d" (random (expt 10 10)))) (let ((boundary (format "-------------------------%d" (random (expt 10 10))))
(files-to-index (or file-paths (files-to-index (or file-paths
(append (mapcan (lambda (dir) (directory-files-recursively dir "\\.org$")) khoj-org-directories) khoj-org-files))) (append (mapcan (lambda (dir) (directory-files-recursively dir "\\.org$")) khoj-org-directories) khoj-org-files)))
(type-query (if (or (equal content-type "all") (not content-type)) "" (format "t=%s" content-type)))
(inhibit-message t) (inhibit-message t)
(message-log-max nil)) (message-log-max nil))
(let ((url-request-method "POST") (let ((url-request-method "POST")
@ -550,14 +552,18 @@ CONFIG is json obtained from Khoj config API."
(url-request-extra-headers `(("content-type" . ,(format "multipart/form-data; boundary=%s" boundary)) (url-request-extra-headers `(("content-type" . ,(format "multipart/form-data; boundary=%s" boundary))
("x-api-key" . ,khoj-server-api-key)))) ("x-api-key" . ,khoj-server-api-key))))
(with-current-buffer (with-current-buffer
(url-retrieve (format "%s/api/v1/index/update" khoj-server-url) (url-retrieve (format "%s/api/v1/index/update?%s&force=%s&client=emacs" khoj-server-url type-query (or force "false"))
;; render response from indexing API endpoint on server ;; render response from indexing API endpoint on server
(lambda (status) (lambda (status)
(if (not status) (if (not status)
(message "khoj.el: Updated Content Index") (message "khoj.el: %scontent index %supdated" (if content-type (format "%s " content-type) "") (if force "force " ""))
(with-current-buffer (current-buffer) (with-current-buffer (current-buffer)
(goto-char "\n\n") (goto-char "\n\n")
(message "khoj.el: Failed to update Content Index. Status: %s. Response: %s" status (string-trim (buffer-substring-no-properties (point) (point-max))))))) (message "khoj.el: Failed to %supdate %s content index. Status: %s. Response: %s"
(if force "force " "")
content-type
status
(string-trim (buffer-substring-no-properties (point) (point-max)))))))
nil t t))) nil t t)))
(setq khoj--indexed-files files-to-index))) (setq khoj--indexed-files files-to-index)))
@ -1141,12 +1147,10 @@ Paragraph only starts at first text after blank line."
(let* ((force-update (if (member "--force-update" args) "true" "false")) (let* ((force-update (if (member "--force-update" args) "true" "false"))
;; set content type to: specified > last used > based on current buffer > default type ;; set content type to: specified > last used > based on current buffer > default type
(content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name)))) (content-type (or (transient-arg-value "--content-type=" args) (khoj--buffer-name-to-content-type (buffer-name))))
(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))
(url-request-method "GET")) (url-request-method "GET"))
(progn (progn
(setq khoj--content-type content-type) (setq khoj--content-type content-type)
(url-retrieve update-url (lambda (_) (message "khoj.el: %s index %supdated!" content-type (if (member "--force-update" args) "force " ""))))))) (khoj--server-index-files force-update content-type))))
(transient-define-suffix khoj--chat-command (&optional _) (transient-define-suffix khoj--chat-command (&optional _)
"Command to Chat with Khoj." "Command to Chat with Khoj."

View file

@ -68,7 +68,7 @@ export async function updateContentIndex(vault: Vault, setting: KhojSetting, las
} }
// Call Khoj backend to update index with all markdown, pdf files // Call Khoj backend to update index with all markdown, pdf files
const response = await fetch(`${setting.khojUrl}/api/v1/index/update?regenerate=${regenerate}`, { const response = await fetch(`${setting.khojUrl}/api/v1/index/update?force=${regenerate}&client=obsidian`, {
method: 'POST', method: 'POST',
headers: { headers: {
'x-api-key': 'secret', 'x-api-key': 'secret',

View file

@ -61,8 +61,8 @@ async def update(
request: Request, request: Request,
files: list[UploadFile], files: list[UploadFile],
x_api_key: str = Header(None), x_api_key: str = Header(None),
regenerate: bool = False, force: bool = False,
search_type: Optional[Union[state.SearchType, str]] = None, t: Optional[Union[state.SearchType, str]] = None,
client: Optional[str] = None, client: Optional[str] = None,
user_agent: Optional[str] = Header(None), user_agent: Optional[str] = Header(None),
referer: Optional[str] = Header(None), referer: Optional[str] = Header(None),
@ -132,8 +132,8 @@ async def update(
state.config.content_type, state.config.content_type,
indexer_input.dict(), indexer_input.dict(),
state.search_models, state.search_models,
regenerate=regenerate, regenerate=force,
t=search_type, t=t,
full_corpus=False, full_corpus=False,
) )

View file

@ -80,7 +80,7 @@ def test_regenerate_with_valid_content_type(client):
headers = {"x-api-key": "secret"} headers = {"x-api-key": "secret"}
# Act # Act
response = client.post(f"/api/v1/index/update?search_type={content_type}", files=files, headers=headers) response = client.post(f"/api/v1/index/update?t={content_type}", files=files, headers=headers)
# Assert # Assert
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}" assert response.status_code == 200, f"Returned status: {response.status_code} for content type: {content_type}"
@ -95,7 +95,7 @@ def test_regenerate_with_github_fails_without_pat(client):
headers = {"x-api-key": "secret"} headers = {"x-api-key": "secret"}
# Act # Act
response = client.post(f"/api/v1/index/update?search_type=github", files=files, headers=headers) response = client.post(f"/api/v1/index/update?t=github", files=files, headers=headers)
# Assert # Assert
assert response.status_code == 200, f"Returned status: {response.status_code} for content type: github" assert response.status_code == 200, f"Returned status: {response.status_code} for content type: github"