mirror of
https://github.com/wiseguru/ReWrite-Voice-Notes.git
synced 2026-07-22 07:49:19 +00:00
This session's features: - Quick Record mode selector: third button on the floater opens a popover of templates, switchable mid-recording. Updates the controller's template in place; does not touch lastUsedTemplateId until completion. - Per-invocation destination override on the main modal: a Destination row under the template picker overrides insertMode and the new-file fields for one run, plumbed via PipelineParams.destinationOverride and shallow-merged onto a template copy before insertOutput. The template file on disk is never mutated. - Assistant prompt as a vault file: replaces the settings textarea with ReWrite/AssistantPrompt.md, loaded via src/assistant-prompt.ts and cached on plugin.assistantPrompt. AI/Agent labels standardized to "Assistant"; aiName -> assistantName; adHocInstructionsPrompt field removed. - Known Nouns list: ReWrite/KnownNouns.md with YAML guidance frontmatter and a Markdown body of nouns (optional misheard alternates after a colon). buildKnownNounsSystemPromptSection injects a "Known nouns" block into the LLM system prompt only when the list is non-empty. Frontmatter is for humans and is not sent to the LLM in v1. Cross-cutting: PipelineParams gains host: PipelineHost (narrow interface in src/types.ts) so the pipeline can read assistantPrompt and knownNouns without importing ReWritePlugin and forming a cycle. Pre-existing uncommitted work also rolled in: - Passphrase encryption for secrets.json.nosync via AES-GCM + PBKDF2 (src/secrets.ts), with PassphraseModal and a settings-tab banner/dropdown for mode selection. - WhisperHost adopt/external states + PID sidecar so an orphaned whisper-server from a previous session is adopted, and external ones are never killed by ReWrite (src/whisper-host.ts). - Whisper status bar (src/ui/whisper-status-bar.ts) and start/stop commands. - Audio persistence before transcription (src/audio-persist.ts) plus the reprocess-audio flow (src/ui/audio-source.ts, src/ui/audio-file-picker.ts). - Wake-name extraction (src/wake-name.ts). - Templates as vault Markdown files in a folder (src/templates-folder.ts). - Linux build script for whisper-server (scripts/build-whisper-linux.sh). - README, CLAUDE.md, docs/FEATURES.md, eslint.config.mts updates covering all of the above. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
132 lines
4.7 KiB
TypeScript
132 lines
4.7 KiB
TypeScript
import { App, Notice } from 'obsidian';
|
|
import { DestinationOverride, EnvironmentProfile, GlobalSettings, NoteTemplate, PipelineHost } from './types';
|
|
import { createTranscriptionProvider } from './transcription';
|
|
import { createLLMProvider } from './llm';
|
|
import { insertOutput, InsertResult } from './insert';
|
|
import { persistAudio } from './audio-persist';
|
|
import { extractAdHocInstructions } from './wake-name';
|
|
import { DEFAULT_ASSISTANT_PROMPT } from './assistant-prompt';
|
|
import { buildKnownNounsSystemPromptSection } from './known-nouns';
|
|
|
|
export type PipelineStage = 'persist-audio' | 'transcribe' | 'cleanup' | 'insert';
|
|
|
|
export type PipelineSource =
|
|
| { kind: 'audio'; audio: Blob; sourcePath?: string }
|
|
| { kind: 'paste'; text: string }
|
|
| { kind: 'webspeech'; transcript: string }
|
|
| { kind: 'text'; text: string };
|
|
|
|
export interface PipelineParams {
|
|
app: App;
|
|
settings: GlobalSettings;
|
|
host: PipelineHost;
|
|
profile: EnvironmentProfile;
|
|
template: NoteTemplate;
|
|
source: PipelineSource;
|
|
destinationOverride?: DestinationOverride;
|
|
onStage?: (stage: PipelineStage) => void;
|
|
signal?: AbortSignal;
|
|
}
|
|
|
|
export interface PipelineResult {
|
|
transcript: string;
|
|
cleaned: string;
|
|
insert: InsertResult;
|
|
}
|
|
|
|
export async function runPipeline(params: PipelineParams): Promise<PipelineResult> {
|
|
let audioPath: string | undefined;
|
|
if (params.source.kind === 'audio') {
|
|
if (params.source.sourcePath) {
|
|
audioPath = params.source.sourcePath;
|
|
} else {
|
|
params.onStage?.('persist-audio');
|
|
try {
|
|
audioPath = await persistAudio(params.app, params.source.audio, params.settings);
|
|
} catch (e) {
|
|
console.error('ReWrite: persist audio failed', e);
|
|
new Notice('Could not save audio file; continuing with transcription.');
|
|
}
|
|
}
|
|
}
|
|
|
|
const transcript = (await collectTranscript(params)).trim();
|
|
if (!transcript) {
|
|
throw new Error('Transcript is empty; nothing to clean up.');
|
|
}
|
|
|
|
params.onStage?.('cleanup');
|
|
const cleaned = await cleanupTranscript(params, transcript);
|
|
const finalContent = audioPath ? `![[${audioPath}]]\n\n${cleaned}` : cleaned;
|
|
|
|
params.onStage?.('insert');
|
|
const insert = await insertOutput({
|
|
app: params.app,
|
|
template: applyDestinationOverride(params.template, params.destinationOverride),
|
|
content: finalContent,
|
|
});
|
|
|
|
return { transcript, cleaned: finalContent, insert };
|
|
}
|
|
|
|
function applyDestinationOverride(template: NoteTemplate, override: DestinationOverride | undefined): NoteTemplate {
|
|
if (!override) return template;
|
|
return {
|
|
...template,
|
|
insertMode: override.insertMode ?? template.insertMode,
|
|
newFileFolder: override.newFileFolder ?? template.newFileFolder,
|
|
newFileNameTemplate: override.newFileNameTemplate ?? template.newFileNameTemplate,
|
|
};
|
|
}
|
|
|
|
async function collectTranscript(params: PipelineParams): Promise<string> {
|
|
const source = params.source;
|
|
switch (source.kind) {
|
|
case 'paste':
|
|
case 'text':
|
|
return source.text;
|
|
case 'webspeech':
|
|
return source.transcript;
|
|
case 'audio': {
|
|
params.onStage?.('transcribe');
|
|
const provider = createTranscriptionProvider(params.profile.transcriptionProvider);
|
|
return provider.transcribe(source.audio, params.profile.transcriptionConfig, params.signal);
|
|
}
|
|
}
|
|
}
|
|
|
|
async function cleanupTranscript(params: PipelineParams, transcript: string): Promise<string> {
|
|
let systemPrompt = params.template.prompt;
|
|
let workingTranscript = transcript;
|
|
if (params.settings.adHocInstructionsEnabled && params.settings.assistantName.trim().length > 0) {
|
|
const { transcript: stripped, instructions } = extractAdHocInstructions(transcript, params.settings.assistantName);
|
|
if (instructions.length > 0) {
|
|
workingTranscript = stripped;
|
|
const list = instructions.map((i, n) => `${n + 1}. ${i}`).join('\n');
|
|
const assistantPrompt = params.host.assistantPrompt ?? DEFAULT_ASSISTANT_PROMPT;
|
|
systemPrompt = `${systemPrompt}\n\n## Ad-hoc instructions\n${assistantPrompt}\n${list}`;
|
|
new Notice(`Heard ${instructions.length} ad-hoc instruction${instructions.length === 1 ? '' : 's'}.`);
|
|
}
|
|
}
|
|
|
|
const knownNounsBlock = buildKnownNounsSystemPromptSection(params.host.knownNouns);
|
|
if (knownNounsBlock) {
|
|
systemPrompt = `${systemPrompt}\n\n${knownNounsBlock}`;
|
|
}
|
|
|
|
const llm = createLLMProvider(params.profile.llmProvider);
|
|
try {
|
|
return await llm.complete(systemPrompt, workingTranscript, params.profile.llmConfig, params.signal);
|
|
} catch (e) {
|
|
const original = e instanceof Error ? e : new Error(String(e));
|
|
try {
|
|
await navigator.clipboard.writeText(workingTranscript);
|
|
throw new Error(`${original.message} (Raw transcript copied to clipboard as fallback.)`);
|
|
} catch (clipErr) {
|
|
if (clipErr instanceof Error && clipErr.message.includes('copied to clipboard')) {
|
|
throw clipErr;
|
|
}
|
|
throw new Error(`${original.message} (Clipboard fallback also failed.)`);
|
|
}
|
|
}
|
|
}
|