Files
Lumotia/docs/architecture-map/04-llm-formatting-mcp/llm-cleanup-text.md
Jake 26c7307607
Some checks failed
check / cargo check (macos-latest) (push) Has been cancelled
check / cargo check (ubuntu-22.04) (push) Has been cancelled
check / cargo check (windows-latest) (push) Has been cancelled
check / svelte build + lint (push) Has been cancelled
agent: lumotia-rebrand — docs, scripts, root config, residuals
Phase 9 of the rebrand cascade. Sweep covers everything the Phase 8
frontend pass deliberately skipped: docs/, root markdown, scripts,
Cargo.toml descriptions, code comments that survived earlier
word-boundary sed, plus a handful of identifiers caught on the final
verify pass.

transcription-app changes:
- README.md, HANDOVER.md, KNOWN-ISSUES.md, run.sh — magnotia/Magnotia
  -> lumotia/Lumotia.
- docs/ — sweep across all subdirs except docs/handovers/ (preserved
  as immutable audit trail). Includes architecture-map references
  to magnotia_core::*, magnotia_storage::*, etc. now pointing at
  lumotia_*; dev-setup.md tracing output examples (lumotia_startup
  target); brief/ + superpowers/ + issues/ + whisper-ecosystem/ +
  audit/.
- Cargo.toml descriptions on 9 crates (core, audio, cloud-providers,
  hotkey, llm, mcp, plus referenced others).
- crates/core/src/{error,hardware,recommendation,paths}.rs +
  crates/audio/src/wav.rs + crates/llm/src/model_manager.rs +
  crates/cloud-providers/src/keystore.rs + crates/mcp/src/lib.rs —
  doc comments and a model-manager user-agent string.
- Caught on final pass: BroadcastChannel("magnotia_task_sync") -> ...
  ("lumotia_task_sync"); magnotia_locale i18n localStorage key
  renamed + migration shim added; CSS keyframe names
  magnotiaPulse / magnotiaBar / magnotiaFade renamed in the design-
  system kit; magnotia_viewer_item / magnotia_viewer_mode handoff
  keys renamed in HistoryPage + viewer/+page.svelte; src/assets/
  wordmark.svg text.
- src-tauri/src/lib.rs comment cleanup ("magnotia era" was sed'd
  to "lumotia era" earlier — restored).

Preserved (intentional):
- crates/core/src/paths.rs — keeps "magnotia" / "Magnotia" / ".magnotia"
  legacy detection strings in legacy_and_target_paths() so the
  migration shim can still find user data from the magnotia era.
- src/lib/stores/{page,focusTimer}.svelte.ts + src/lib/i18n/index.ts
  — migration call sites reference the legacy magnotia keys
  deliberately.
- docs/handovers/ — historical audit trail.

cargo build --workspace passes. npm run check: 0 errors / 0 warnings
(3958 files). cargo test --workspace: 339 pass / 0 fail.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-13 12:38:03 +01:00

79 lines
4.5 KiB
Markdown

---
name: LLM cleanup_text surface
type: architecture-map-page
slice: 04-llm-formatting-mcp
last_verified: 2026/05/09
---
# LLM `cleanup_text` surface
> **Where you are:** [Architecture map](../README.md) → [LLM, Formatting, MCP](README.md) → cleanup_text
**Plain English summary.** `cleanup_text` is the freeform LLM call. Given any system prompt and a transcript, it returns the LLM's cleaned version as plain text. No grammar constraint, no JSON parsing. The formatting crate's `llm_client::cleanup_text` is the only canonical caller and supplies the prompt-injection-hardened system prompt.
## At a glance
- Crate: `lumotia-llm`
- Path: `crates/llm/src/lib.rs:232`
- LOC: 21 lines for this method
- Public surface: `pub fn cleanup_text(&self, system_prompt: &str, transcript: &str) -> Result<String, EngineError>`
- External deps that matter: none beyond what `LlmEngine::generate` already pulls in
- Tauri command that calls this (slice 2, best guess): not called directly by Tauri. The chain is `commands::llm::cleanup_transcript_text_cmd` (`src-tauri/src/commands/llm.rs:363`) → `lumotia_ai_formatting::llm_cleanup_text` (`src-tauri/src/commands/llm.rs:395`) → this method. Also reached from `commands::transcription::*` and `commands::live::*` via the formatting pipeline at `crates/ai-formatting/src/pipeline.rs:84`.
## What's in here
```text
pub fn cleanup_text(&self, system_prompt: &str, transcript: &str) -> Result<String, EngineError>
```
Behaviour:
1. If `transcript.trim().is_empty()`, return `Ok(String::new())` immediately. No model touch.
2. Borrow the loaded model via `loaded_model_arc()`. Returns `EngineError::NotLoaded` if no model is loaded.
3. Render a chat prompt with two messages — `("system", system_prompt)` and `("user", transcript)` — through `render_chat_prompt`, which applies the model's tokenizer-bundled chat template and falls back to ChatML if missing.
4. Call `generate` with:
- `max_tokens: 1024`
- `temperature: 0.0`
- `stop_sequences: ["<|im_end|>", "<|im_end_of_text|>"]`
- `grammar: None`
Returns the trimmed, post-stop-sequence-truncated output verbatim.
## Data flow
```
(system_prompt: &str, transcript: &str)
→ empty-transcript short-circuit (returns "")
→ loaded_model_arc() (NotLoaded error if absent)
→ render_chat_prompt([(system, system_prompt), (user, transcript)])
→ generate(prompt, GenerationConfig { max_tokens: 1024, temp: 0.0, stops: [<|im_end|>, <|im_end_of_text|>], grammar: None })
→ trimmed String
```
No JSON parse, no GBNF, no closed set. The contract with the caller is "do whatever the system prompt tells you to do".
## Prompts and grammars
`cleanup_text` itself does not own a prompt — the system prompt is a parameter. The canonical caller is `lumotia_ai_formatting::llm_client::cleanup_text` which composes:
```text
CLEANUP_PROMPT + format_dictionary_suffix(dictionary_terms) + preset.suffix()
```
See [`formatting-llm-cleanup-bridge.md`](formatting-llm-cleanup-bridge.md) for the full composition logic and prompt-injection-hardening rationale, and [`llm-prompts-and-grammars.md`](llm-prompts-and-grammars.md) for the prompt text in full.
## Watch-outs
- **No grammar means the model can output anything.** This is by design — cleanup is freeform — but it means the prompt is the only line of defence against prompt injection. The `llm_client` bridge handles this; do not call `LlmEngine::cleanup_text` from anywhere else without porting that hardening.
- **`max_tokens: 1024` is a hard ceiling on output length.** A long dictation that compresses well is fine; one that compresses poorly will be cut off mid-sentence. The pipeline does not detect or retry truncated output. If we ever get reports of mid-sentence drops on long transcripts, raise this constant in tandem with the preflight cap.
- **`temperature: 0.0` plus the fixed seed makes output deterministic for a given prompt and loaded model.** Switching tier (e.g. 4B → 9B) will change the output even with the same input.
- **Stop sequences are Qwen-specific.** Both `<|im_end|>` and `<|im_end_of_text|>` are emitted by Qwen3.5 / 3.6 chat templates. A future model from a different family would need its own stop set.
- **Empty transcript returns empty string, not an error.** Callers that want to distinguish "nothing to clean" from "model not loaded" should check `is_loaded()` first.
## See also
- [LLM engine](llm-engine.md)
- [LLM cleanup bridge in formatting crate](formatting-llm-cleanup-bridge.md)
- [Prompts and grammars catalogue](llm-prompts-and-grammars.md)
- [Slice README](README.md)