Initial release v1.0.0
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import { Editor, Notice } from "obsidian";
|
||||
import { AIService } from "../AIService";
|
||||
import { AIProvider, PROVIDER_URLS } from "../SettingsManager";
|
||||
import NibbleAI from "main";
|
||||
|
||||
export class Proofreader {
|
||||
private plugin: NibbleAI;
|
||||
|
||||
constructor(plugin: NibbleAI) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
private resolveProvider(): { apiKey: string; endpoint: string; provider: AIProvider } | null {
|
||||
const provider = this.plugin.settings.proofreadingProvider || "openrouter";
|
||||
let apiKey = "";
|
||||
let endpoint = PROVIDER_URLS[provider] || "";
|
||||
|
||||
switch (provider) {
|
||||
case "openai":
|
||||
apiKey = this.plugin.settings.openAiApiKey;
|
||||
break;
|
||||
case "anthropic":
|
||||
apiKey = this.plugin.settings.anthropicApiKey;
|
||||
break;
|
||||
case "openrouter":
|
||||
apiKey = this.plugin.settings.openRouterApiKey;
|
||||
break;
|
||||
case "custom":
|
||||
apiKey = this.plugin.settings.customApiKey;
|
||||
endpoint = this.plugin.settings.postProcessingUrl || "";
|
||||
break;
|
||||
}
|
||||
|
||||
if (!apiKey) return null;
|
||||
if (!endpoint) return null;
|
||||
|
||||
return { apiKey, endpoint, provider };
|
||||
}
|
||||
|
||||
async fixSelection(editor: Editor): Promise<void> {
|
||||
const selection = editor.getSelection();
|
||||
if (!selection || selection.trim().length === 0) {
|
||||
new Notice("No text selected.");
|
||||
return;
|
||||
}
|
||||
|
||||
const config = this.resolveProvider();
|
||||
if (!config) {
|
||||
new Notice(
|
||||
"⚠️ No API key for selected provider. Check Settings \u2192 NibbleAI \u2192 API Keys."
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
new Notice("🤖 Fixing punctuation and grammar...");
|
||||
|
||||
try {
|
||||
const systemPrompt =
|
||||
this.plugin.settings.proofreadingSystemPrompt ||
|
||||
"You are a text proofreading tool. Fix punctuation and grammar in the text below. Return only the corrected text, nothing else.";
|
||||
|
||||
const corrected = await AIService.callChatCompletion(
|
||||
selection,
|
||||
systemPrompt,
|
||||
{
|
||||
apiKey: config.apiKey,
|
||||
model: this.plugin.settings.proofreadingModel,
|
||||
endpoint: config.endpoint,
|
||||
provider: config.provider,
|
||||
temperature: this.plugin.settings.proofreadingTemperature,
|
||||
maxTokens: this.plugin.settings.proofreadingMaxTokens,
|
||||
}
|
||||
);
|
||||
|
||||
if (!corrected || corrected.trim().length === 0) {
|
||||
new Notice("⚠️ AI returned empty result. No changes made.");
|
||||
return;
|
||||
}
|
||||
|
||||
editor.replaceSelection(corrected);
|
||||
new Notice("✅ Punctuation and grammar fixed.");
|
||||
} catch (error) {
|
||||
console.error("Proofreading error:", error);
|
||||
new Notice(
|
||||
`❌ Fix failed: ${error instanceof Error ? error.message : String(error)}`
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user