# Refactoring Plan: Decouple Transcription from Text Insertion
**Goal:** Refactor `process_audio_chunks` to remove direct dependency on `TextInserter` using a trait-based approach (Hexagonal Architecture).
**Steps:**
1. [X] **Define `TranscriptionResultHandler` Trait:**
* Create `src/transcription/result_handler.rs`.
* Define `pub trait TranscriptionResultHandler { fn handle_result(&mut self, text: &str, options: platform::InsertOptions) -> anyhow::Result<()>; }`.
* Ensure the trait is `Send + Sync` compatible if needed across threads.
2. [X] **Modify `process_audio_chunks` (`src/transcription/mod.rs`):**
* Change the function signature:
* Replace `text_inserter: Option<Arc<Mutex<Box<dyn TextInserter + Send + Sync>>>>`
* With `result_handler: Option<Arc<Mutex<Box<dyn TranscriptionResultHandler + Send + Sync>>>>`.
* Update the text handling logic:
* Replace `inserter.lock().await.insert_text_with_options(...)`
* With `handler.lock().await.handle_result(&text, options)...`.
* Update imports to use the new trait.
3. [X] **Create `PlatformTextInserterHandler` (`src/platform/handler.rs`):**
* Create a new file `src/platform/handler.rs`.
* Define `pub struct PlatformTextInserterHandler { inserter: Box<dyn TextInserter + Send + Sync> }`.
* Implement `TranscriptionResultHandler` for `PlatformTextInserterHandler`:
* The `handle_result` method should call `self.inserter.insert_text_with_options(text, options)`.
* Add necessary constructor (`new`) method.
* Export the struct and trait implementation from `src/platform/mod.rs`.
4. [ ] **Update Call Site (e.g., `main.rs`):**
* Find where `process_audio_chunks` is called.
* Instead of creating and passing the `TextInserter` directly, do:
* Create the `TextInserter` instance.
* Create `PlatformTextInserterHandler::new(text_inserter)`.
* Wrap it in `Arc<Mutex<Box<dyn TranscriptionResultHandler...>>>`.
* Pass this wrapped handler to `process_audio_chunks`.
5. [ ] **Testing:**
* Ensure existing tests pass or update them as necessary.
* Consider adding tests specifically for the new handler and trait interaction (might require mocking).**
## 4. Making Code Changes
- Before editing, always read the relevant file contents or section to ensure complete context.
- If a patch is not applied correctly, attempt to reapply it.
- Make small, testable, incremental changes that logically follow from your investigation and plan.
**