wiseguru_ReWrite-Voice-Notes/src/assistant-prompt.ts
WiseGuru 3ec83943eb Add four UX features and bundle pending pre-release work
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>
2026-05-26 10:13:42 -07:00

79 lines
2.7 KiB
TypeScript

import { App, normalizePath, TFile } from 'obsidian';
export const DEFAULT_ASSISTANT_PROMPT = 'The user spoke these instructions mid-dictation. Apply each one as you produce the final text:';
const DEFAULT_FILE_BODY = `${DEFAULT_ASSISTANT_PROMPT}\n`;
export async function loadAssistantPromptFromFile(app: App, path: string): Promise<string | null> {
const normalized = normalizeFilePath(path);
if (!normalized) return null;
const file = app.vault.getAbstractFileByPath(normalized);
if (!(file instanceof TFile)) return null;
try {
const content = await app.vault.read(file);
const { body } = splitFrontmatter(content);
const trimmed = body.trim();
return trimmed.length > 0 ? trimmed : null;
} catch {
return null;
}
}
export async function populateDefaultAssistantPrompt(app: App, path: string): Promise<boolean> {
const normalized = normalizeFilePath(path);
if (!normalized) throw new Error('Assistant prompt path is empty.');
if (app.vault.getAbstractFileByPath(normalized)) return false;
const parent = parentFolder(normalized);
if (parent) await ensureFolder(app, parent);
await app.vault.create(normalized, DEFAULT_FILE_BODY);
return true;
}
export function isPathAssistantPrompt(path: string, configuredPath: string): boolean {
const normalizedConfigured = normalizeFilePath(configuredPath);
if (!normalizedConfigured) return false;
return normalizePath(path) === normalizedConfigured;
}
function splitFrontmatter(content: string): { frontmatter: string | null; body: string } {
if (!content.startsWith('---')) return { frontmatter: null, body: content };
const lines = content.split(/\r?\n/);
if (lines[0]?.trim() !== '---') return { frontmatter: null, body: content };
for (let i = 1; i < lines.length; i++) {
if (lines[i]?.trim() === '---') {
return {
frontmatter: lines.slice(1, i).join('\n'),
body: lines.slice(i + 1).join('\n'),
};
}
}
return { frontmatter: null, body: content };
}
function normalizeFilePath(path: string): string {
const trimmed = path.trim();
if (!trimmed) return '';
const normalized = normalizePath(trimmed);
if (!normalized || normalized === '/' || normalized === '.') return '';
return normalized;
}
function parentFolder(path: string): string {
const idx = path.lastIndexOf('/');
if (idx <= 0) return '';
return path.slice(0, idx);
}
async function ensureFolder(app: App, folder: string): Promise<void> {
const normalized = normalizePath(folder);
if (app.vault.getAbstractFileByPath(normalized)) return;
const parts = normalized.split('/');
let current = '';
for (const part of parts) {
if (!part) continue;
current = current ? `${current}/${part}` : part;
if (!app.vault.getAbstractFileByPath(current)) {
await app.vault.createFolder(current);
}
}
}