forked from jcollingj/caret
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvertTextToNoteModal.ts
112 lines (101 loc) · 4.32 KB
/
convertTextToNoteModal.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
import { App, Modal, Notice, Setting } from "obsidian";
export class ConvertTextToNoteModal extends Modal {
plugin: any;
messages: string[];
formatting_prompt: string = "Format the below content into a nice markdown document.";
fileName: string = "";
apply_formatting: boolean = true;
constructor(app: App, plugin: any, messages: string[]) {
super(app);
this.plugin = plugin;
this.messages = messages;
}
onOpen() {
const { contentEl } = this;
contentEl.empty();
contentEl.createEl("h2", { text: "Convert text to note" });
contentEl.createEl("div", { text: `Converting ${this.messages.length} messages`, cls: "callout" });
new Setting(contentEl)
.setName("File name")
.setDesc("Enter the name for the note.")
.addText((text) => {
text.setValue(this.fileName).onChange((value) => {
this.fileName = value;
});
});
new Setting(contentEl)
.setName("Auto format")
.setDesc("Apply prompt formatting to the note.")
.addToggle((toggle) => {
toggle.setValue(this.apply_formatting).onChange((value) => {
this.apply_formatting = value;
});
});
const textArea = contentEl.createEl("textarea", {
text: this.formatting_prompt,
cls: "content-l caret-w-full",
placeholder: "Enter the formatting song.",
});
textArea.onchange = (event) => {
this.formatting_prompt = (event.target as HTMLTextAreaElement).value;
};
new Setting(contentEl).addButton((button) => {
button.setButtonText("Submit").onClick(async () => {
if (!this.fileName || this.fileName.trim() === "") {
new Notice("File name must be set before saving");
console.error("Validation Error: File name must exist");
return;
}
let final_content = this.messages.join("\n");
if (this.apply_formatting) {
if (this.formatting_prompt.length < 1) {
new Notice("Must have formatting prompt");
return;
}
let final_prompt = `${this.formatting_prompt}\n\n${this.messages.join("\n")}`;
const conversation = [{ role: "user", content: final_prompt }];
const response = await this.plugin.llm_call(
this.plugin.settings.llm_provider,
this.plugin.settings.model,
conversation
);
final_content = response;
}
const file_path = `${this.fileName}.md`;
// Check if the file path contains parentheses
if (file_path.includes("/")) {
const pathParts = file_path.split("/");
let currentPath = "";
for (const part of pathParts.slice(0, -1)) {
currentPath += part;
const folder = await this.app.vault.getAbstractFileByPath(currentPath);
if (!folder) {
try {
await this.app.vault.createFolder(currentPath);
} catch (error) {
console.error("Failed to create folder:", error);
}
}
currentPath += "/";
}
}
const file = await this.app.vault.getFileByPath(file_path);
try {
if (file) {
new Notice("File exists already, please choose another name");
} else {
await this.app.vault.create(file_path, final_content);
new Notice("Chat saved to note");
this.close();
}
} catch (error) {
console.error("Failed to save note:", error);
}
});
});
}
onClose() {
const { contentEl } = this;
contentEl.empty();
}
}