-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
167 lines (134 loc) · 6.06 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import { Plugin, TFile, Notice, MarkdownView } from 'obsidian';
import { YoutubeVideoProcessor } from './services/YoutubeVideoProcessor';
import { VectorStoreService } from './services/VectorStoreService';
import { YouTubeInputModal } from './ui/YouTubeInputModal';
import { ProgressNotice } from './ui/ProgressNotice';
import { SettingTab } from './settings/SettingTab';
import { TranscriptService } from './services/TranscriptService';
import { NoteUpdateService } from './services/NoteUpdateService';
import { YouTubeLinkExtractor } from './utils/YouTubeLinkExtractor';
import { DEFAULT_SETTINGS } from './settings/settings';
import { PluginSettings } from './models/interfaces';
export default class YoutubeKnowledgeGraphPlugin extends Plugin {
settings: PluginSettings;
vectorStore: VectorStoreService; // Changed to public
private youtubeProcessor: YoutubeVideoProcessor;
private transcriptService: TranscriptService;
private noteUpdateService: NoteUpdateService;
private linkExtractor: YouTubeLinkExtractor;
private currentFile: TFile | null = null;
async onload() {
// Load settings first
await this.loadSettings();
// Initialize services
this.vectorStore = new VectorStoreService(this);
this.youtubeProcessor = new YoutubeVideoProcessor(this);
this.transcriptService = new TranscriptService(this);
this.noteUpdateService = new NoteUpdateService(this);
this.linkExtractor = new YouTubeLinkExtractor();
// Add ribbon icon
this.addRibbonIcon('youtube', 'Add YouTube Video', this.handleYouTubeIconClick.bind(this));
// Add settings tab
this.addSettingTab(new SettingTab(this.app, this));
// Add commands
this.registerCommands();
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
private async handleYouTubeIconClick() {
new YouTubeInputModal(this.app, async (url) => {
const progress = new ProgressNotice('Processing YouTube video');
try {
const videoId = this.linkExtractor.extractVideoId(url);
if (!videoId) {
throw new Error('Invalid YouTube URL');
}
await this.processVideoWithProgress(videoId, progress);
} catch (error) {
progress.setMessage(`Error: ${error.message}`);
setTimeout(() => progress.hide(), 3000);
console.error('Error creating note:', error);
}
}).open();
}
private async processVideoWithProgress(videoId: string, progress: ProgressNotice) {
progress.setProgress(10);
progress.setMessage('Fetching video information');
const note = await this.youtubeProcessor.createInitialNote(videoId);
progress.setProgress(40);
progress.setMessage('Transcribing video');
await this.processNewNoteWithProgress(note, progress);
progress.setProgress(100);
progress.setMessage('Complete!');
this.app.workspace.getLeaf().openFile(note);
setTimeout(() => progress.hide(), 2000);
}
private registerCommands() {
this.addCommand({
id: 'process-youtube-links',
name: 'Process YouTube Links in Current Note',
callback: () => this.processCurrentNote()
});
this.addCommand({
id: 'inspect-vectors',
name: 'Debug: Inspect Vector Store',
callback: () => this.vectorStore.debugInfo()
});
// Add other commands...
}
private async processCurrentNote() {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeView) {
new Notice('No active markdown file');
return;
}
await this.processNewNote(activeView.file);
}
private async processNewNote(file: TFile) {
try {
const content = await this.app.vault.read(file);
const youtubeLinks = this.linkExtractor.extractYoutubeLinks(content);
this.currentFile = file;
if (youtubeLinks.length === 0) return;
for (const link of youtubeLinks) {
const videoId = this.linkExtractor.extractVideoId(link);
if (!videoId) continue;
const metadata = await this.youtubeProcessor.processVideo(videoId);
await this.transcriptService.createTranscriptNote(file, metadata);
await this.noteUpdateService.updateOriginalNote(file, metadata);
}
} catch (error) {
console.error('Error processing note:', error);
new Notice(`Error processing note: ${error.message}`);
} finally {
this.currentFile = null;
}
}
private async processNewNoteWithProgress(file: TFile, progress: ProgressNotice) {
try {
const content = await this.app.vault.read(file);
const youtubeLinks = this.linkExtractor.extractYoutubeLinks(content);
if (youtubeLinks.length === 0) return;
for (const link of youtubeLinks) {
const videoId = this.linkExtractor.extractVideoId(link);
if (!videoId) continue;
progress.setProgress(50);
progress.setMessage('Getting transcript');
const metadata = await this.youtubeProcessor.processVideo(videoId);
progress.setProgress(70);
progress.setMessage('Creating transcript note');
await this.transcriptService.createTranscriptNote(file, metadata);
progress.setProgress(90);
progress.setMessage('Updating notes with analysis');
await this.noteUpdateService.updateOriginalNote(file, metadata);
}
} catch (error) {
console.error('Error processing note:', error);
throw error;
}
}
}