From d5a7cc5b0f04d496dce0de9eccdc86810fae3a51 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 17 Jan 2023 17:44:35 -0300 Subject: [PATCH 1/6] Compact code to map results from search API into SearchResult objects Make code compact for readability Remove unneeded temporary variables and return statements --- src/interface/obsidian/src/modal.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/src/interface/obsidian/src/modal.ts b/src/interface/obsidian/src/modal.ts index c1005042..6d8aa723 100644 --- a/src/interface/obsidian/src/modal.ts +++ b/src/interface/obsidian/src/modal.ts @@ -54,15 +54,8 @@ export class KhojModal extends SuggestModal { let searchUrl = `${this.setting.khojUrl}/api/search?q=${query}&n=${this.setting.resultsCount}&r=${this.rerank}&t=markdown` let results = await request(searchUrl) .then(response => JSON.parse(response)) - .then(data => { - return data.map((result: any) => { - let processedResult: SearchResult = { - entry: result.entry, - file: result.additional.file - }; - return processedResult; - }) - }); + .then(data => data + .map((result: any) => { return { entry: result.entry, file: result.additional.file } as SearchResult; })); return results; } From ffaef9247611a7522d32432aaea0e4b291182ac1 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 17 Jan 2023 17:47:21 -0300 Subject: [PATCH 2/6] Encode query string before passing as query param to search API --- src/interface/obsidian/src/modal.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/interface/obsidian/src/modal.ts b/src/interface/obsidian/src/modal.ts index 6d8aa723..cb3e4a20 100644 --- a/src/interface/obsidian/src/modal.ts +++ b/src/interface/obsidian/src/modal.ts @@ -51,7 +51,8 @@ export class KhojModal extends SuggestModal { async getSuggestions(query: string): Promise { // Query Khoj backend for search results - let searchUrl = `${this.setting.khojUrl}/api/search?q=${query}&n=${this.setting.resultsCount}&r=${this.rerank}&t=markdown` + let encodedQuery = encodeURIComponent(query); + let searchUrl = `${this.setting.khojUrl}/api/search?q=${encodedQuery}&n=${this.setting.resultsCount}&r=${this.rerank}&t=markdown` let results = await request(searchUrl) .then(response => JSON.parse(response)) .then(data => data From 39a18e2080bc14f7809be8f74f8725e40d6cf5e2 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 17 Jan 2023 18:08:44 -0300 Subject: [PATCH 3/6] Add ability to search for similar notes in Khoj Obsidian - Hide input field on init, Trigger search on opening modal in similar notes mode - Set input to current markdown file and get similar notes to it - Enable rerank when searching for similar notes - Filter out current note from similar note search results --- src/interface/obsidian/src/main.ts | 11 +++++++++++ src/interface/obsidian/src/modal.ts | 27 ++++++++++++++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index 1725b5ae..98f1cbb6 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -21,6 +21,17 @@ export default class Khoj extends Plugin { } }); + // Add a similar notes command + this.addCommand({ + id: 'similar', + name: 'Find Similar Notes', + checkCallback: (checking) => { + if (!checking && this.settings.connectedToBackend) + new KhojModal(this.app, this.settings, true).open(); + return this.settings.connectedToBackend; + } + }); + // Create an icon in the left ribbon. this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => { // Called when the user clicks the icon. diff --git a/src/interface/obsidian/src/modal.ts b/src/interface/obsidian/src/modal.ts index cb3e4a20..d44da44a 100644 --- a/src/interface/obsidian/src/modal.ts +++ b/src/interface/obsidian/src/modal.ts @@ -9,10 +9,15 @@ export interface SearchResult { export class KhojModal extends SuggestModal { setting: KhojSetting; rerank: boolean = false; + find_similar_notes: boolean; - constructor(app: App, setting: KhojSetting) { + constructor(app: App, setting: KhojSetting, find_similar_notes: boolean = false) { super(app); this.setting = setting; + this.find_similar_notes = find_similar_notes; + + // Hide input element in Similar Notes mode + this.inputEl.hidden = this.find_similar_notes; // Register Modal Keybindings to Rerank Results this.scope.register(['Mod'], 'Enter', async () => { @@ -49,6 +54,25 @@ export class KhojModal extends SuggestModal { this.setPlaceholder('Search with Khoj 🦅...'); } + async onOpen() { + if (this.find_similar_notes) { + // If markdown file is currently active + let file = this.app.workspace.getActiveFile(); + if (file && file.extension === 'md') { + // Enable rerank of search results + this.rerank = true + // Set contents of active markdown file to input element + this.inputEl.value = await this.app.vault.read(file); + // Trigger search to get and render similar notes from khoj backend + this.inputEl.dispatchEvent(new Event('input')); + this.rerank = false + } + else { + this.resultContainerEl.setText('Cannot find similar notes for non-markdown files'); + } + } + } + async getSuggestions(query: string): Promise { // Query Khoj backend for search results let encodedQuery = encodeURIComponent(query); @@ -56,6 +80,7 @@ export class KhojModal extends SuggestModal { let results = await request(searchUrl) .then(response => JSON.parse(response)) .then(data => data + .filter((result: any) => !this.find_similar_notes || !result.additional.file.endsWith(this.app.workspace.getActiveFile()?.path)) .map((result: any) => { return { entry: result.entry, file: result.additional.file } as SearchResult; })); return results; From 0bed410712f2537500fbbe4fad055ad46c613c8c Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 17 Jan 2023 19:34:48 -0300 Subject: [PATCH 4/6] Limit Find Similar Note command to be triggered from Editor Fixup indentation and comments --- src/interface/obsidian/src/main.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index 98f1cbb6..7d464dd6 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -10,7 +10,7 @@ export default class Khoj extends Plugin { async onload() { await this.loadSettings(); - // Add a search command. It can be triggered from anywhere + // Add search command. It can be triggered from anywhere this.addCommand({ id: 'search', name: 'Search', @@ -21,11 +21,11 @@ export default class Khoj extends Plugin { } }); - // Add a similar notes command + // Add similar notes command. It can only be triggered from the editor this.addCommand({ id: 'similar', name: 'Find Similar Notes', - checkCallback: (checking) => { + editorCheckCallback: (checking) => { if (!checking && this.settings.connectedToBackend) new KhojModal(this.app, this.settings, true).open(); return this.settings.connectedToBackend; @@ -36,8 +36,8 @@ export default class Khoj extends Plugin { this.addRibbonIcon('search', 'Khoj', (_: MouseEvent) => { // Called when the user clicks the icon. this.settings.connectedToBackend - ? new KhojModal(this.app, this.settings).open() - : new Notice(`❗️Ensure Khoj backend is running and Khoj URL is pointing to it in the plugin settings`); + ? new KhojModal(this.app, this.settings).open() + : new Notice(`❗️Ensure Khoj backend is running and Khoj URL is pointing to it in the plugin settings`); }); // Add a settings tab so the user can configure khoj From 657e4557851655d916ce92e42d5e38e3caa47fc6 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Tue, 17 Jan 2023 23:46:38 -0300 Subject: [PATCH 5/6] Remove unused `onunload' method in main.ts of khoj obsidian plugin --- src/interface/obsidian/src/main.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/interface/obsidian/src/main.ts b/src/interface/obsidian/src/main.ts index 7d464dd6..1adc1eff 100644 --- a/src/interface/obsidian/src/main.ts +++ b/src/interface/obsidian/src/main.ts @@ -44,9 +44,6 @@ export default class Khoj extends Plugin { this.addSettingTab(new KhojSettingTab(this.app, this)); } - onunload() { - } - async loadSettings() { // Load khoj obsidian plugin settings this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData()); From 6119d0a69ef4c95154a28371631253b2064beaf8 Mon Sep 17 00:00:00 2001 From: Debanjum Singh Solanky Date: Wed, 18 Jan 2023 00:03:13 -0300 Subject: [PATCH 6/6] Add usage of "Find Similar Notes" command to the Khoj Obsidian Readme --- src/interface/obsidian/README.md | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/interface/obsidian/README.md b/src/interface/obsidian/README.md index b4691cd6..ef8fe2d7 100644 --- a/src/interface/obsidian/README.md +++ b/src/interface/obsidian/README.md @@ -11,7 +11,8 @@ - [Setup Backend](#1-Setup-Backend) - [Setup Plugin](#2-Setup-Plugin) - [Use](#Use) - - [Query Filters](#Query-filters) + - [Search](#search) + - [Find Similar Notes](#find-similar-notes) - [Upgrade](#Upgrade) - [Upgrade Backend](#1-Upgrade-Backend) - [Upgrade Plugin](#2-Upgrade-Plugin) @@ -56,7 +57,8 @@ pip install khoj-assistant && khoj --no-gui See [official docs](https://help.obsidian.md/Advanced+topics/Community+plugins#Discover+and+install+community+plugins) for details ## Use -Click the *Khoj search* icon 🔎 on the [Ribbon](https://help.obsidian.md/User+interface/Workspace/Ribbon) or Search for *Khoj: Search* in the [Command Palette](https://help.obsidian.md/Plugins/Command+palette) +### Search +Click the *Khoj search* icon 🔎 on the [Ribbon](https://help.obsidian.md/User+interface/Workspace/Ribbon) or run *Khoj: Search* from the [Command Palette](https://help.obsidian.md/Plugins/Command+palette)
Query Filters @@ -80,6 +82,9 @@ Use structured query syntax to filter the natural language search results
+### Find Similar Notes +To see other notes similar to the current one, run *Khoj: Find Similar Notes* from the [Command Palette](https://help.obsidian.md/Plugins/Command+palette) + ## Upgrade ### 1. Upgrade Backend ```shell