mirror of
https://github.com/khoaliber/khoj.git
synced 2026-03-09 05:39:12 +00:00
Make plugin object accessible to chat, find similar panes in obsidian
Allows ability to access, save settings in a cleaner way
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import { ItemView, MarkdownRenderer, Scope, WorkspaceLeaf, request, requestUrl, setIcon, Platform, TFile } from 'obsidian';
|
import { ItemView, MarkdownRenderer, Scope, WorkspaceLeaf, request, requestUrl, setIcon, Platform, TFile } from 'obsidian';
|
||||||
import * as DOMPurify from 'isomorphic-dompurify';
|
import * as DOMPurify from 'isomorphic-dompurify';
|
||||||
import { KhojSetting } from 'src/settings';
|
|
||||||
import { KhojPaneView } from 'src/pane_view';
|
import { KhojPaneView } from 'src/pane_view';
|
||||||
import { KhojView, createCopyParentText, getLinkToEntry, pasteTextAtCursor } from 'src/utils';
|
import { KhojView, createCopyParentText, getLinkToEntry, pasteTextAtCursor } from 'src/utils';
|
||||||
import { KhojSearchModal } from 'src/search_modal';
|
import { KhojSearchModal } from 'src/search_modal';
|
||||||
|
import Khoj from 'src/main';
|
||||||
import { FileInteractions, EditBlock } from 'src/interact_with_files';
|
import { FileInteractions, EditBlock } from 'src/interact_with_files';
|
||||||
|
|
||||||
export interface ChatJsonResult {
|
export interface ChatJsonResult {
|
||||||
@@ -67,7 +67,6 @@ interface Agent {
|
|||||||
|
|
||||||
export class KhojChatView extends KhojPaneView {
|
export class KhojChatView extends KhojPaneView {
|
||||||
result: string;
|
result: string;
|
||||||
setting: KhojSetting;
|
|
||||||
waitingForLocation: boolean;
|
waitingForLocation: boolean;
|
||||||
location: Location = { timezone: Intl.DateTimeFormat().resolvedOptions().timeZone };
|
location: Location = { timezone: Intl.DateTimeFormat().resolvedOptions().timeZone };
|
||||||
keyPressTimeout: NodeJS.Timeout | null = null;
|
keyPressTimeout: NodeJS.Timeout | null = null;
|
||||||
@@ -101,8 +100,8 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
// 2. Higher invalid edit blocks than tolerable
|
// 2. Higher invalid edit blocks than tolerable
|
||||||
private maxEditRetries: number = 1; // Maximum retries for edit blocks
|
private maxEditRetries: number = 1; // Maximum retries for edit blocks
|
||||||
|
|
||||||
constructor(leaf: WorkspaceLeaf, setting: KhojSetting) {
|
constructor(leaf: WorkspaceLeaf, plugin: Khoj) {
|
||||||
super(leaf, setting);
|
super(leaf, plugin);
|
||||||
this.fileInteractions = new FileInteractions(this.app);
|
this.fileInteractions = new FileInteractions(this.app);
|
||||||
|
|
||||||
this.waitingForLocation = true;
|
this.waitingForLocation = true;
|
||||||
@@ -1190,7 +1189,7 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
chatUrl += `&conversation_id=${chatBodyEl.dataset.conversationId}`;
|
chatUrl += `&conversation_id=${chatBodyEl.dataset.conversationId}`;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log("Fetching chat history from:", chatUrl);
|
console.debug("Fetching chat history from:", chatUrl);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let response = await fetch(chatUrl, {
|
let response = await fetch(chatUrl, {
|
||||||
@@ -1199,7 +1198,7 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
});
|
});
|
||||||
|
|
||||||
let responseJson: any = await response.json();
|
let responseJson: any = await response.json();
|
||||||
console.log("Chat history response:", responseJson);
|
console.debug("Chat history response:", responseJson);
|
||||||
|
|
||||||
chatBodyEl.dataset.conversationId = responseJson.conversation_id;
|
chatBodyEl.dataset.conversationId = responseJson.conversation_id;
|
||||||
|
|
||||||
@@ -1221,7 +1220,7 @@ export class KhojChatView extends KhojPaneView {
|
|||||||
|
|
||||||
// Update current agent from conversation history
|
// Update current agent from conversation history
|
||||||
if (responseJson.response.agent?.slug) {
|
if (responseJson.response.agent?.slug) {
|
||||||
console.log("Found agent in conversation history:", responseJson.response.agent);
|
console.debug("Found agent in conversation history:", responseJson.response.agent);
|
||||||
this.currentAgent = responseJson.response.agent.slug;
|
this.currentAgent = responseJson.response.agent.slug;
|
||||||
// Update the agent selector if it exists
|
// Update the agent selector if it exists
|
||||||
const agentSelect = this.contentEl.querySelector('.khoj-header-agent-select') as HTMLSelectElement;
|
const agentSelect = this.contentEl.querySelector('.khoj-header-agent-select') as HTMLSelectElement;
|
||||||
|
|||||||
@@ -3,8 +3,8 @@ import { KhojSetting, KhojSettingTab, DEFAULT_SETTINGS } from 'src/settings'
|
|||||||
import { KhojSearchModal } from 'src/search_modal'
|
import { KhojSearchModal } from 'src/search_modal'
|
||||||
import { KhojChatView } from 'src/chat_view'
|
import { KhojChatView } from 'src/chat_view'
|
||||||
import { KhojSimilarView } from 'src/similar_view'
|
import { KhojSimilarView } from 'src/similar_view'
|
||||||
import { updateContentIndex, canConnectToBackend, KhojView, jumpToPreviousView } from './utils';
|
import { updateContentIndex, canConnectToBackend, KhojView } from 'src/utils';
|
||||||
import { KhojPaneView } from './pane_view';
|
import { KhojPaneView } from 'src/pane_view';
|
||||||
|
|
||||||
|
|
||||||
export default class Khoj extends Plugin {
|
export default class Khoj extends Plugin {
|
||||||
@@ -136,8 +136,8 @@ export default class Khoj extends Plugin {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Register views
|
// Register views
|
||||||
this.registerView(KhojView.CHAT, (leaf) => new KhojChatView(leaf, this.settings));
|
this.registerView(KhojView.CHAT, (leaf) => new KhojChatView(leaf, this));
|
||||||
this.registerView(KhojView.SIMILAR, (leaf) => new KhojSimilarView(leaf, this.settings));
|
this.registerView(KhojView.SIMILAR, (leaf) => new KhojSimilarView(leaf, this));
|
||||||
|
|
||||||
// Create an icon in the left ribbon.
|
// Create an icon in the left ribbon.
|
||||||
this.addRibbonIcon('message-circle', 'Khoj', (_: MouseEvent) => {
|
this.addRibbonIcon('message-circle', 'Khoj', (_: MouseEvent) => {
|
||||||
|
|||||||
@@ -1,14 +1,17 @@
|
|||||||
import { ItemView, WorkspaceLeaf } from 'obsidian';
|
import { ItemView, WorkspaceLeaf } from 'obsidian';
|
||||||
import { KhojSetting } from 'src/settings';
|
import { KhojSetting } from 'src/settings';
|
||||||
import { KhojView, populateHeaderPane } from './utils';
|
import { KhojView, populateHeaderPane } from './utils';
|
||||||
|
import Khoj from 'src/main';
|
||||||
|
|
||||||
export abstract class KhojPaneView extends ItemView {
|
export abstract class KhojPaneView extends ItemView {
|
||||||
setting: KhojSetting;
|
setting: KhojSetting;
|
||||||
|
plugin: Khoj;
|
||||||
|
|
||||||
constructor(leaf: WorkspaceLeaf, setting: KhojSetting) {
|
constructor(leaf: WorkspaceLeaf, plugin: Khoj) {
|
||||||
super(leaf);
|
super(leaf);
|
||||||
|
|
||||||
this.setting = setting;
|
this.setting = plugin.settings;
|
||||||
|
this.plugin = plugin;
|
||||||
|
|
||||||
// Register Modal Keybindings to send user message
|
// Register Modal Keybindings to send user message
|
||||||
// this.scope.register([], 'Enter', async () => { await this.chat() });
|
// this.scope.register([], 'Enter', async () => { await this.chat() });
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import { WorkspaceLeaf, TFile, MarkdownRenderer, Notice, setIcon } from 'obsidian';
|
import { WorkspaceLeaf, TFile, MarkdownRenderer, Notice, setIcon } from 'obsidian';
|
||||||
import { KhojSetting } from 'src/settings';
|
|
||||||
import { KhojPaneView } from 'src/pane_view';
|
import { KhojPaneView } from 'src/pane_view';
|
||||||
import { KhojView, getLinkToEntry, supportedBinaryFileTypes } from 'src/utils';
|
import { KhojView, getLinkToEntry, supportedBinaryFileTypes } from 'src/utils';
|
||||||
|
import Khoj from 'src/main';
|
||||||
|
|
||||||
export interface SimilarResult {
|
export interface SimilarResult {
|
||||||
entry: string;
|
entry: string;
|
||||||
@@ -11,7 +11,6 @@ export interface SimilarResult {
|
|||||||
|
|
||||||
export class KhojSimilarView extends KhojPaneView {
|
export class KhojSimilarView extends KhojPaneView {
|
||||||
static iconName: string = "search";
|
static iconName: string = "search";
|
||||||
setting: KhojSetting;
|
|
||||||
currentController: AbortController | null = null;
|
currentController: AbortController | null = null;
|
||||||
isLoading: boolean = false;
|
isLoading: boolean = false;
|
||||||
loadingEl: HTMLElement;
|
loadingEl: HTMLElement;
|
||||||
@@ -21,9 +20,8 @@ export class KhojSimilarView extends KhojPaneView {
|
|||||||
fileWatcher: any;
|
fileWatcher: any;
|
||||||
component: any;
|
component: any;
|
||||||
|
|
||||||
constructor(leaf: WorkspaceLeaf, setting: KhojSetting) {
|
constructor(leaf: WorkspaceLeaf, plugin: Khoj) {
|
||||||
super(leaf, setting);
|
super(leaf, plugin);
|
||||||
this.setting = setting;
|
|
||||||
this.component = this;
|
this.component = this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user