-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.ts
More file actions
267 lines (224 loc) · 10.1 KB
/
Copy pathmain.ts
File metadata and controls
267 lines (224 loc) · 10.1 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import { App, Plugin, PluginSettingTab, Setting, Notice, MarkdownView, TFile } from 'obsidian';
import { JournalingAssistantSettings, DEFAULT_SETTINGS } from './src/types';
export default class JournalingAssistantPlugin extends Plugin {
settings: JournalingAssistantSettings;
async onload() {
await this.loadSettings();
// Add settings tab
this.addSettingTab(new JournalingAssistantSettingTab(this.app, this));
// Add ribbon icon
this.addRibbonIcon('bot', 'Journal with AI', async () => {
await this.openTodaysJournal();
});
// Add command for opening today's journal
this.addCommand({
id: 'open-todays-journal',
name: 'Open Today\'s Journal',
callback: async () => {
await this.openTodaysJournal();
},
});
}
async loadSettings() {
this.settings = Object.assign({}, DEFAULT_SETTINGS, await this.loadData());
}
async saveSettings() {
await this.saveData(this.settings);
}
onunload() {
console.log("Unloading Journaling Assistant Plugin...");
}
private insertHelloWorld() {
const activeView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (activeView) {
const editor = activeView.editor;
editor.replaceSelection("Hello World");
} else {
// If there's no active markdown editor (e.g., no note open), do nothing or show a notice.
new Notice("No active note is open.");
}
}
private getTodayFileName(): string {
const date = new Date();
return date.toISOString().split('T')[0] + '.md'; // Format: YYYY-MM-DD.md
}
private async ensureJournalFolder(): Promise<void> {
const folderPath = this.settings.journalFolder;
if (!(await this.app.vault.adapter.exists(folderPath))) {
await this.app.vault.createFolder(folderPath);
}
}
private async getPastJournalEntries(count: number): Promise<string[]> {
const folder = this.app.vault.getAbstractFileByPath(this.settings.journalFolder);
if (!folder) return [];
const files = this.app.vault.getMarkdownFiles()
.filter(file => file.path.startsWith(this.settings.journalFolder + '/'))
.sort((a, b) => b.stat.mtime - a.stat.mtime)
.slice(0, count);
const entries = await Promise.all(
files.map(async file => {
const content = await this.app.vault.read(file);
return {
date: file.basename,
content: content
};
})
);
return entries.map(entry => `Date: ${entry.date}\n${entry.content}`);
}
private async generatePromptWithAI(pastEntries: string[]): Promise<string> {
if (!this.settings.openAIApiKey) {
throw new Error('OpenAI API key not configured');
}
const prompt = `Based on these past journal entries:\n\n${pastEntries.join('\n\n---\n\n')}\n\n${this.settings.defaultJournalingPrompt}`;
// Add debug logging
console.log('=== GPT API Call Debug ===');
console.log('Past Entries Count:', pastEntries.length);
console.log('Prompt:', prompt);
console.log('API Key (first 4 chars):', this.settings.openAIApiKey.slice(0, 4));
try {
const requestBody = {
model: 'gpt-4o',
messages: [{
role: 'user',
content: prompt
}],
temperature: 0.7,
};
// Log the request
console.log('Request Body:', JSON.stringify(requestBody, null, 2));
const response = await fetch('https://api.openai.com/v1/chat/completions', {
method: 'POST',
headers: {
'Authorization': `Bearer ${this.settings.openAIApiKey}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(requestBody),
});
const data = await response.json();
// Log the response
console.log('Response:', JSON.stringify(data, null, 2));
if (!response.ok) {
throw new Error(data.error?.message || 'API request failed');
}
return data.choices[0].message.content;
} catch (error) {
console.error('OpenAI API error:', error);
throw new Error('Failed to generate prompt: ' + error.message);
}
}
private async openTodaysJournal(): Promise<void> {
try {
await this.ensureJournalFolder();
const fileName = this.getTodayFileName();
const filePath = `${this.settings.journalFolder}/${fileName}`;
let file = this.app.vault.getAbstractFileByPath(filePath);
// Get past entries and generate prompt
let initialContent = `# Journal Entry - ${new Date().toLocaleDateString()}\n\n`;
try {
const pastEntries = await this.getPastJournalEntries(this.settings.numberOfPastEntries);
if (pastEntries.length > 0) {
const aiPrompt = await this.generatePromptWithAI(pastEntries);
initialContent += `## Today's Prompt\n\n${aiPrompt}\n\n## Your Journal Response\n\n`;
} else {
initialContent += `## Your Journal Response\n\n`;
}
} catch (error) {
new Notice('Error generating prompt: ' + error.message);
initialContent += `## Your Journal Response\n\n`;
}
if (!file) {
file = await this.app.vault.create(filePath, initialContent);
new Notice('Created new journal entry for today');
}
const leaf = this.app.workspace.getUnpinnedLeaf();
await leaf.openFile(file as TFile);
} catch (error) {
new Notice('Error opening today\'s journal: ' + error.message);
console.error('Error opening today\'s journal:', error);
}
}
}
class JournalingAssistantSettingTab extends PluginSettingTab {
plugin: JournalingAssistantPlugin;
constructor(app: App, plugin: JournalingAssistantPlugin) {
super(app, plugin);
this.plugin = plugin;
}
display(): void {
const { containerEl } = this;
containerEl.empty();
containerEl.createEl('h2', { text: 'Journaling Assistant Settings' });
new Setting(containerEl)
.setName('Journal Folder')
.setDesc('The folder where your journal entries will be stored')
.addText(text => text
.setPlaceholder('Journal')
.setValue(this.plugin.settings.journalFolder)
.onChange(async (value) => {
this.plugin.settings.journalFolder = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Summaries Folder')
.setDesc('The folder where your journal summaries will be stored')
.addText(text => text
.setPlaceholder('Summaries')
.setValue(this.plugin.settings.summariesFolder)
.onChange(async (value) => {
this.plugin.settings.summariesFolder = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('OpenAI API Key')
.setDesc('Your OpenAI API key for generating prompts and summaries')
.addText(text => text
.setPlaceholder('sk-...')
.setValue(this.plugin.settings.openAIApiKey)
.onChange(async (value) => {
this.plugin.settings.openAIApiKey = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Whisper API Key')
.setDesc('Your Whisper API key for voice-to-text functionality')
.addText(text => text
.setPlaceholder('sk-...')
.setValue(this.plugin.settings.whisperApiKey)
.onChange(async (value) => {
this.plugin.settings.whisperApiKey = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Default Journaling Prompt')
.setDesc('The default prompt used to generate journaling questions')
.addTextArea(text => text
.setPlaceholder('Generate a thought-provoking journaling prompt for today.')
.setValue(this.plugin.settings.defaultJournalingPrompt)
.onChange(async (value) => {
this.plugin.settings.defaultJournalingPrompt = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Default Summarization Prompt')
.setDesc('The default prompt used to generate journal entry summaries')
.addTextArea(text => text
.setPlaceholder('Summarize the key points and insights from this journal entry.')
.setValue(this.plugin.settings.defaultSummarizationPrompt)
.onChange(async (value) => {
this.plugin.settings.defaultSummarizationPrompt = value;
await this.plugin.saveSettings();
}));
new Setting(containerEl)
.setName('Number of Past Entries')
.setDesc('Number of past journal entries to consider when generating prompts')
.addSlider(slider => slider
.setLimits(1, 10, 1)
.setValue(this.plugin.settings.numberOfPastEntries)
.setDynamicTooltip()
.onChange(async (value) => {
this.plugin.settings.numberOfPastEntries = value;
await this.plugin.saveSettings();
}));
}
}