add create thread command

This commit is contained in:
John Mavrick 2023-11-27 22:12:10 -08:00
parent a6e0d18f41
commit 2e978d264b
3 changed files with 33 additions and 11 deletions

13
main.ts
View File

@ -1,5 +1,5 @@
import { App, Plugin, PluginSettingTab, Setting } from 'obsidian'; import { App, Plugin, PluginSettingTab, Setting } from 'obsidian';
import { AppView, VIEW_TYPE } from './src/ui/AppView'; import { AppView, OBSIDIAN_INTELLIGENCE_VIEW_TYPE } from './src/ui/AppView';
import OpenAI from 'openai'; import OpenAI from 'openai';
import { IThread } from './src/ui/types'; import { IThread } from './src/ui/types';
@ -25,7 +25,7 @@ export default class ObsidianIntelligence extends Plugin {
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
this.registerView(VIEW_TYPE, (leaf) => new AppView(leaf, this)); this.registerView(OBSIDIAN_INTELLIGENCE_VIEW_TYPE, (leaf) => new AppView(leaf, this));
const ribbonIconEl = this.addRibbonIcon( const ribbonIconEl = this.addRibbonIcon(
'bot', 'bot',
@ -68,15 +68,18 @@ export default class ObsidianIntelligence extends Plugin {
} }
async activateView() { async activateView() {
this.app.workspace.detachLeavesOfType(VIEW_TYPE); this.app.workspace.detachLeavesOfType(OBSIDIAN_INTELLIGENCE_VIEW_TYPE);
await this.app.workspace.getRightLeaf(false).setViewState({ await this.app.workspace.getRightLeaf(false).setViewState({
type: VIEW_TYPE, type: OBSIDIAN_INTELLIGENCE_VIEW_TYPE,
active: true, active: true,
}); });
this.revealView();
}
async revealView() {
this.app.workspace.revealLeaf( this.app.workspace.revealLeaf(
this.app.workspace.getLeavesOfType(VIEW_TYPE)[0], this.app.workspace.getLeavesOfType(OBSIDIAN_INTELLIGENCE_VIEW_TYPE)[0],
); );
} }
} }

View File

@ -6,7 +6,9 @@ import { App } from 'obsidian';
import ObsidianIntelligence from '../../main'; import ObsidianIntelligence from '../../main';
import OpenAI from 'openai'; import OpenAI from 'openai';
export const VIEW_TYPE = 'example-view'; export const OBSIDIAN_INTELLIGENCE_VIEW_TYPE = 'obsidian-intelligence-view';
export const AppContext = React.createContext<App | undefined>(undefined); export const AppContext = React.createContext<App | undefined>(undefined);
@ -53,7 +55,7 @@ export class AppView extends ItemView {
} }
getViewType() { getViewType() {
return VIEW_TYPE; return OBSIDIAN_INTELLIGENCE_VIEW_TYPE;
} }
getDisplayText() { getDisplayText() {

View File

@ -1,6 +1,6 @@
import React, { useEffect, useMemo } from 'react'; import React, { useEffect, useMemo } from 'react';
import OpenAI from 'openai'; import OpenAI from 'openai';
import { useApp, useOpenAI, usePlugin } from '../AppView'; import { OBSIDIAN_INTELLIGENCE_VIEW_TYPE, useApp, useOpenAI, usePlugin } from '../AppView';
import { IThread } from '../types'; import { IThread } from '../types';
import DropdownSelect from './DropdownSelect'; import DropdownSelect from './DropdownSelect';
import { MarkdownView } from 'obsidian'; import { MarkdownView } from 'obsidian';
@ -96,9 +96,27 @@ const AssistantManager = ({
} }
}, },
}); });
plugin.addCommand({
id: 'create-assistant-from-active-note',
name: 'Create Thread',
callback: async () => {
const isViewOpen = app.workspace.getLeavesOfType(OBSIDIAN_INTELLIGENCE_VIEW_TYPE).some((leaf) => {
return leaf.view;
});
if (!isViewOpen) {
plugin.activateView();
}
plugin.revealView();
createThread();
}
});
}, [plugin]); }, [plugin]);
useEffect(() => {}, [plugin]); useEffect(() => {
console.log('threads update', threads);
}, [threads]);
const createThread = async () => { const createThread = async () => {
if (!openaiInstance || !plugin) { if (!openaiInstance || !plugin) {
@ -120,9 +138,8 @@ const AssistantManager = ({
name: newThreadName, name: newThreadName,
}, },
}; };
updateThreads([...threads, newThread]); updateThreads([...plugin.settings.threads, newThread]);
updateActiveThread(newThread); updateActiveThread(newThread);
plugin.saveSettings();
}); });
}; };