Add writing status check

This commit is contained in:
Simon Yang 2023-03-17 23:49:59 +11:00
parent 0e6911c8ff
commit 0e64454235

View file

@ -32,6 +32,7 @@ const DEFAULT_SETTINGS: AICommanderPluginSettings = {
export default class AICommanderPlugin extends Plugin { export default class AICommanderPlugin extends Plugin {
settings: AICommanderPluginSettings; settings: AICommanderPluginSettings;
writing: boolean;
async improvePrompt(prompt: string, targetModel: string) { async improvePrompt(prompt: string, targetModel: string) {
@ -105,7 +106,6 @@ export default class AICommanderPlugin extends Plugin {
}, },
}); });
// Read chunks from the response stream
const reader = response.body?.getReader(); const reader = response.body?.getReader();
if (!reader) { if (!reader) {
throw new Error('No response body reader available'); throw new Error('No response body reader available');
@ -153,7 +153,7 @@ export default class AICommanderPlugin extends Plugin {
getNextNewLine(editor: Editor, Ln: number) { getNextNewLine(editor: Editor, Ln: number) {
let newLine = Ln; let newLine = Ln;
while (editor.getLine(newLine).trim().length > 0) { while (editor.getLine(newLine).trim().length > 0) {
if (newLine == editor.lastLine()) editor.setLine(newLine, '\n'); if (newLine == editor.lastLine()) editor.setLine(newLine, editor.getLine(newLine) + '\n');
newLine++; newLine++;
} }
return newLine; return newLine;
@ -360,16 +360,21 @@ export default class AICommanderPlugin extends Plugin {
return this.generateText(prompt, editor, currentLn, context); return this.generateText(prompt, editor, currentLn, context);
} }
commandGenerateText(editor: Editor, prompt: string) { commandGenerateText(editor: Editor, prompt: string) {
const currentLn = editor.getCursor('to').line; const currentLn = editor.getCursor('to').line;
if (this.writing) {
new Notice('Generator is already in progress.');
return;
}
this.writing = true;
new Notice("Generating text..."); new Notice("Generating text...");
this.generateText(prompt, editor, currentLn).then((text) => { this.generateText(prompt, editor, currentLn).then((text) => {
new Notice("Text completed."); new Notice("Text completed.");
this.writing = false;
}).catch(error => { }).catch(error => {
console.log(error.message); console.log(error.message);
new Notice(error.message); new Notice(error.message);
this.writing = false;
}); });
} }
@ -380,9 +385,16 @@ export default class AICommanderPlugin extends Plugin {
const regex = [/(?<=\[(.*)]\()(([^[\]])+)\.pdf(?=\))/g, const regex = [/(?<=\[(.*)]\()(([^[\]])+)\.pdf(?=\))/g,
/(?<=\[\[)(([^[\]])+)\.pdf(?=]])/g]; /(?<=\[\[)(([^[\]])+)\.pdf(?=]])/g];
this.findFilePath(text, regex).then((path) => { this.findFilePath(text, regex).then((path) => {
if (this.writing) throw new Error('Generator is already in progress.');
this.writing = true;
new Notice(`Generating text in context of ${path}...`); new Notice(`Generating text in context of ${path}...`);
this.generateTextWithPdf(prompt, editor, currentLn, path).then((text) => { this.generateTextWithPdf(prompt, editor, currentLn, path).then((text) => {
new Notice("Text completed."); new Notice("Text completed.");
this.writing = false;
}).catch(error => {
console.log(error.message);
new Notice(error.message);
this.writing = false;
}); });
}).catch(error => { }).catch(error => {
console.log(error.message); console.log(error.message);
@ -392,13 +404,20 @@ export default class AICommanderPlugin extends Plugin {
commandGenerateImage(editor: Editor, prompt: string) { commandGenerateImage(editor: Editor, prompt: string) {
const currentLn = editor.getCursor('to').line; const currentLn = editor.getCursor('to').line;
if (this.writing) {
new Notice('Generator is already in progress.');
return;
}
this.writing = true;
new Notice("Generating image..."); new Notice("Generating image...");
this.generateImage(prompt).then((text) => { this.generateImage(prompt).then((text) => {
this.writeText(editor, currentLn, text); this.writeText(editor, currentLn, text);
new Notice('Image Generated.'); new Notice('Image Generated.');
this.writing = false;
}).catch(error => { }).catch(error => {
console.log(error.message); console.log(error.message);
new Notice(error.message); new Notice(error.message);
this.writing = false;
}); });
} }
@ -415,10 +434,20 @@ export default class AICommanderPlugin extends Plugin {
this.app.vault.adapter.exists(path).then((exists) => { this.app.vault.adapter.exists(path).then((exists) => {
if (!exists) throw new Error(path + ' does not exist'); if (!exists) throw new Error(path + ' does not exist');
this.app.vault.adapter.readBinary(path).then((audioBuffer) => { this.app.vault.adapter.readBinary(path).then((audioBuffer) => {
if (this.writing) {
new Notice('Generator is already in progress.');
return;
}
this.writing = true;
new Notice("Generating transcript..."); new Notice("Generating transcript...");
this.generateTranscript(audioBuffer, fileType).then((result) => { this.generateTranscript(audioBuffer, fileType).then((result) => {
this.writeText(editor, position.line, result); this.writeText(editor, position.line, result);
new Notice('Transcript Generated.'); new Notice('Transcript Generated.');
this.writing = false;
}).catch(error => {
console.log(error.message);
new Notice(error.message);
this.writing = false;
}); });
}); });
}); });
@ -431,6 +460,7 @@ export default class AICommanderPlugin extends Plugin {
async onload() { async onload() {
await this.loadSettings(); await this.loadSettings();
this.writing = false;
this.addCommand({ this.addCommand({
id: 'text-prompt', id: 'text-prompt',