diaryx_core 1.4.4

Core library for Diaryx - a tool to manage markdown files with YAML frontmatter
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
//! Plugin registry for collecting and dispatching to plugins.
//!
//! The registry is the central hub that holds all registered plugins and
//! provides methods to emit events and route commands.

use std::collections::HashMap;
use std::sync::{Arc, Mutex};

use serde_json::Value as JsonValue;

use super::events::*;
use super::manifest::{PluginManifest, UiContribution};
use super::{
    FilePlugin, Plugin, PluginContext, PluginError, PluginHealth, PluginId, WorkspacePlugin,
};

/// Per-plugin health tracking state.
struct PluginHealthTracker {
    health: HashMap<PluginId, PluginHealth>,
}

/// Central registry that holds all registered plugins.
///
/// Plugins are registered by namespace (workspace, file) and the registry
/// dispatches events and commands to the appropriate plugins.
///
/// The registry tracks plugin health — plugins that fail to initialize are
/// marked as [`PluginHealth::Failed`] and skipped for subsequent dispatches.
pub struct PluginRegistry {
    plugins: Vec<Arc<dyn Plugin>>,
    workspace_plugins: Vec<Arc<dyn WorkspacePlugin>>,
    file_plugins: Vec<Arc<dyn FilePlugin>>,
    health: Mutex<PluginHealthTracker>,
}

impl PluginRegistry {
    /// Create an empty registry.
    pub fn new() -> Self {
        Self {
            plugins: Vec::new(),
            workspace_plugins: Vec::new(),
            file_plugins: Vec::new(),
            health: Mutex::new(PluginHealthTracker {
                health: HashMap::new(),
            }),
        }
    }

    /// Register a workspace plugin.
    ///
    /// The plugin is added to the base `plugins` list only if no plugin with
    /// the same ID is already registered, preventing double `init()`/`shutdown()`
    /// calls and duplicate manifests when a plugin implements both
    /// `WorkspacePlugin` and `FilePlugin`.
    pub fn register_workspace_plugin(&mut self, plugin: Arc<dyn WorkspacePlugin>) {
        if !self.plugins.iter().any(|p| p.id() == plugin.id()) {
            self.plugins.push(plugin.clone());
        }
        self.workspace_plugins.push(plugin);
    }

    /// Register a file plugin.
    ///
    /// The plugin is added to the base `plugins` list only if no plugin with
    /// the same ID is already registered (see [`register_workspace_plugin`](Self::register_workspace_plugin)).
    pub fn register_file_plugin(&mut self, plugin: Arc<dyn FilePlugin>) {
        if !self.plugins.iter().any(|p| p.id() == plugin.id()) {
            self.plugins.push(plugin.clone());
        }
        self.file_plugins.push(plugin);
    }

    /// Get all registered plugin IDs.
    pub fn plugin_ids(&self) -> Vec<PluginId> {
        self.plugins.iter().map(|p| p.id()).collect()
    }

    /// Get a reference to all registered workspace plugins.
    pub fn workspace_plugins(&self) -> &[Arc<dyn WorkspacePlugin>] {
        &self.workspace_plugins
    }

    // ========================================================================
    // Health Tracking
    // ========================================================================

    fn set_health(&self, id: PluginId, health: PluginHealth) {
        if let Ok(mut tracker) = self.health.lock() {
            tracker.health.insert(id, health);
        }
    }

    fn is_plugin_healthy(&self, id: &PluginId) -> bool {
        if let Ok(tracker) = self.health.lock() {
            !matches!(tracker.health.get(id), Some(PluginHealth::Failed(_)))
        } else {
            // If lock is poisoned, assume healthy to avoid blocking everything.
            true
        }
    }

    /// Get the health status of a specific plugin.
    pub fn get_plugin_health(&self, plugin_id: &PluginId) -> PluginHealth {
        if let Ok(tracker) = self.health.lock() {
            tracker
                .health
                .get(plugin_id)
                .cloned()
                .unwrap_or(PluginHealth::Healthy)
        } else {
            PluginHealth::Healthy
        }
    }

    /// Get health status of all registered plugins.
    pub fn get_all_plugin_health(&self) -> Vec<(PluginId, PluginHealth)> {
        self.plugins
            .iter()
            .map(|p| {
                let id = p.id();
                let health = self.get_plugin_health(&id);
                (id, health)
            })
            .collect()
    }

    // ========================================================================
    // Manifests
    // ========================================================================

    /// Get manifests from all registered plugins.
    pub fn get_all_manifests(&self) -> Vec<PluginManifest> {
        self.plugins.iter().map(|p| p.manifest()).collect()
    }

    /// Get UI contributions from all registered plugins, tagged with plugin ID.
    pub fn get_all_ui_contributions(&self) -> Vec<(PluginId, Vec<UiContribution>)> {
        self.plugins
            .iter()
            .map(|p| {
                let m = p.manifest();
                (m.id, m.ui)
            })
            .collect()
    }

    // ========================================================================
    // Lifecycle
    // ========================================================================

    /// Initialize all registered plugins.
    ///
    /// Plugins that fail to init are marked as [`PluginHealth::Failed`] and
    /// skipped for subsequent event dispatch. Returns a list of all failures
    /// (empty means all plugins initialized successfully).
    pub async fn init_all(&self, ctx: &PluginContext) -> Vec<(PluginId, PluginError)> {
        let mut errors = Vec::new();
        for plugin in &self.plugins {
            match plugin.init(ctx).await {
                Ok(()) => {
                    self.set_health(plugin.id(), PluginHealth::Healthy);
                }
                Err(e) => {
                    let id = plugin.id();
                    log::error!("Plugin {} failed to init: {}", id, e);
                    self.set_health(id.clone(), PluginHealth::Failed(e.to_string()));
                    errors.push((id, e));
                }
            }
        }
        errors
    }

    /// Shut down all registered plugins (in reverse registration order).
    pub async fn shutdown_all(&self) -> Result<(), PluginError> {
        for plugin in self.plugins.iter().rev() {
            plugin.shutdown().await?;
        }
        Ok(())
    }

    // ========================================================================
    // Workspace Events
    // ========================================================================

    /// Emit a workspace-opened event to all healthy workspace plugins.
    pub async fn emit_workspace_opened(&self, event: &WorkspaceOpenedEvent) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_workspace_opened(event).await;
        }
    }

    /// Emit a workspace-closed event to all healthy workspace plugins.
    pub async fn emit_workspace_closed(&self, event: &WorkspaceClosedEvent) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_workspace_closed(event).await;
        }
    }

    /// Emit a workspace-changed event to all healthy workspace plugins.
    pub async fn emit_workspace_changed(&self, event: &WorkspaceChangedEvent) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_workspace_changed(event).await;
        }
    }

    /// Emit a workspace-committed event to all healthy workspace plugins.
    pub async fn emit_workspace_committed(&self, event: &WorkspaceCommittedEvent) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_workspace_committed(event).await;
        }
    }

    // ========================================================================
    // File Events
    // ========================================================================

    /// Emit a file-saved event to all healthy file plugins.
    pub async fn emit_file_saved(&self, event: &FileSavedEvent) {
        for plugin in &self.file_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_file_saved(event).await;
        }
    }

    /// Emit a file-created event to all healthy file plugins.
    pub async fn emit_file_created(&self, event: &FileCreatedEvent) {
        for plugin in &self.file_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_file_created(event).await;
        }
    }

    /// Emit a file-deleted event to all healthy file plugins.
    pub async fn emit_file_deleted(&self, event: &FileDeletedEvent) {
        for plugin in &self.file_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_file_deleted(event).await;
        }
    }

    /// Emit a file-moved event to all healthy file plugins.
    pub async fn emit_file_moved(&self, event: &FileMovedEvent) {
        for plugin in &self.file_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_file_moved(event).await;
        }
    }

    // ========================================================================
    // CRDT Side-Effect Dispatch
    // ========================================================================

    /// Notify all healthy workspace plugins that a workspace-modifying operation completed.
    ///
    /// Plugins managing sync state should broadcast CRDT workspace updates.
    pub async fn notify_workspace_modified(&self) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.notify_workspace_modified().await;
        }
    }

    /// Notify all healthy workspace plugins that a body document was renamed.
    pub async fn emit_body_doc_renamed(&self, old_path: &str, new_path: &str) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_body_doc_renamed(old_path, new_path).await;
        }
    }

    /// Notify all healthy workspace plugins that a body document was deleted.
    pub async fn emit_body_doc_deleted(&self, path: &str) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.on_body_doc_deleted(path).await;
        }
    }

    /// Ask healthy workspace plugins to track CRDT metadata for echo detection.
    pub async fn track_file_for_sync(&self, canonical_path: &str) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.track_file_for_sync(canonical_path).await;
        }
    }

    /// Ask healthy workspace plugins to track body content for echo detection.
    pub fn track_content_for_sync(&self, canonical_path: &str, content: &str) {
        for plugin in &self.workspace_plugins {
            if !self.is_plugin_healthy(&plugin.id()) {
                continue;
            }
            plugin.track_content_for_sync(canonical_path, content);
        }
    }

    /// Resolve a canonical path from a storage path via workspace plugins.
    ///
    /// Returns the first `Some` result from any plugin, or `None` to use the default.
    pub fn get_canonical_path(&self, storage_path: &str) -> Option<String> {
        for plugin in &self.workspace_plugins {
            if let Some(canonical) = plugin.get_canonical_path(storage_path) {
                return Some(canonical);
            }
        }
        None
    }

    /// Get the title for a file from CRDT metadata via workspace plugins.
    pub fn get_file_title(&self, canonical_path: &str) -> Option<String> {
        for plugin in &self.workspace_plugins {
            if let Some(title) = plugin.get_file_title(canonical_path) {
                return Some(title);
            }
        }
        None
    }

    // ========================================================================
    // Command Dispatch
    // ========================================================================

    /// Route a command to the matching workspace plugin.
    ///
    /// Finds the first workspace plugin whose [`PluginId`] matches `plugin_id`
    /// and calls [`WorkspacePlugin::handle_command`]. Returns `None` if no
    /// plugin matches or the matched plugin doesn't handle the command.
    ///
    /// Returns an error if the matched plugin is in a [`PluginHealth::Failed`] state.
    pub async fn handle_plugin_command(
        &self,
        plugin_id: &str,
        cmd: &str,
        params: JsonValue,
    ) -> Option<Result<JsonValue, PluginError>> {
        for plugin in &self.workspace_plugins {
            if plugin.id().0 == plugin_id {
                if !self.is_plugin_healthy(&plugin.id()) {
                    return Some(Err(PluginError::Other(format!(
                        "Plugin '{}' is in failed state",
                        plugin_id
                    ))));
                }
                return plugin.handle_command(cmd, params).await;
            }
        }
        None
    }
}

// ========================================================================
// Filesystem Event Forwarding
// ========================================================================

impl PluginRegistry {
    /// Forward a filesystem event to all registered plugins.
    ///
    /// Converts a `FileSystemEvent` into the appropriate plugin events
    /// (`FileSavedEvent`, `FileCreatedEvent`, etc.) and dispatches them.
    /// This replaces CrdtFs interception when sync runs as an Extism plugin.
    pub async fn forward_fs_event(&self, event: &crate::fs::FileSystemEvent) {
        use crate::fs::FileSystemEvent;

        match event {
            FileSystemEvent::FileCreated { path, .. } => {
                let path_str = path.to_string_lossy().to_string();
                self.emit_file_created(&FileCreatedEvent { path: path_str })
                    .await;
            }
            FileSystemEvent::FileDeleted { path, .. } => {
                let path_str = path.to_string_lossy().to_string();
                self.emit_file_deleted(&FileDeletedEvent { path: path_str })
                    .await;
            }
            FileSystemEvent::FileRenamed {
                old_path, new_path, ..
            } => {
                let old = old_path.to_string_lossy().to_string();
                let new = new_path.to_string_lossy().to_string();
                self.emit_file_moved(&FileMovedEvent {
                    old_path: old,
                    new_path: new,
                })
                .await;
            }
            FileSystemEvent::FileMoved { path, .. } => {
                // FileMoved in FS events only has the new path
                let path_str = path.to_string_lossy().to_string();
                self.emit_file_saved(&FileSavedEvent { path: path_str })
                    .await;
            }
            FileSystemEvent::MetadataChanged { path, .. }
            | FileSystemEvent::ContentsChanged { path, .. } => {
                let path_str = path.to_string_lossy().to_string();
                self.emit_file_saved(&FileSavedEvent { path: path_str })
                    .await;
            }
            // Sync events and other variants are not forwarded to plugins
            _ => {}
        }
    }
}

impl Default for PluginRegistry {
    fn default() -> Self {
        Self::new()
    }
}