moduforge-search 0.6.2

moduforge 搜索服务
Documentation
use std::sync::Arc;

use anyhow::Result;
use async_trait::async_trait;
use mf_model::node_pool::NodePool;
use mf_state::plugin::{
    Plugin, PluginMetadata, PluginSpec, PluginTrait, StateField,
};
use mf_state::state::State;
use mf_state::transaction::Transaction;

// TantivyBackend imported indirectly via create_tantivy_service
use crate::service::{IndexEvent, IndexService};
use crate::step_registry::ensure_default_step_indexers;
use crate::create_tantivy_service;

// Resource wrappers
pub struct TantivySearchIndexResource {
    pub service: Arc<IndexService>,
}
impl mf_state::resource::Resource for TantivySearchIndexResource {}

struct TantivyIndexStateField {
    service: Arc<IndexService>,
}

impl std::fmt::Debug for TantivyIndexStateField {
    fn fmt(
        &self,
        f: &mut std::fmt::Formatter<'_>,
    ) -> std::fmt::Result {
        f.debug_struct("TantivyIndexStateField").finish()
    }
}

// in-memory backend removed

#[async_trait]
impl StateField for TantivyIndexStateField {
    type Value = TantivySearchIndexResource;

    async fn init(
        &self,
        _config: &mf_state::state::StateConfig,
        _instance: &State,
    ) -> Arc<Self::Value> {
        Arc::new(TantivySearchIndexResource { service: self.service.clone() })
    }

    async fn apply(
        &self,
        tr: &Transaction,
        value: Arc<Self::Value>,
        old_state: &State,
        new_state: &State,
    ) -> Arc<Self::Value> {
        // ✅ 不再需要 downcast,直接使用类型安全的 value
        let svc = value.service.clone();
        let steps: Vec<Arc<dyn mf_transform::step::Step>> =
            tr.steps.iter().cloned().collect();
        let pool_before: Arc<NodePool> = old_state.doc();
        let pool_after: Arc<NodePool> = new_state.doc();
        drop(svc.handle(IndexEvent::TransactionCommitted {
            pool_before: Some(pool_before),
            pool_after,
            steps,
        }));
        value
    }
}
#[derive(Debug)]
struct TantivyIndexPluginTrait {}

impl PluginTrait for TantivyIndexPluginTrait {
    fn metadata(&self) -> PluginMetadata {
        PluginMetadata {
            name: "tantivy_index".to_string(),
            version: "1.0.0".to_string(),
            description: "Tantivy 索引插件".to_string(),
            author: "Tantivy".to_string(),
            dependencies: vec![],
            conflicts: vec![],
            state_fields: vec![],
            tags: vec![],
        }
    }
}
// Create a plugin that maintains a Tantivy index and updates it on each transaction.
pub fn create_tantivy_index_plugin(
    index_dir: &std::path::Path
) -> Result<Arc<Plugin>> {
    ensure_default_step_indexers();
    let service = Arc::new(create_tantivy_service(index_dir)?);
    // StateField 自动实现 ErasedStateField(通过 blanket impl)
    let field = Arc::new(TantivyIndexStateField { service });
    let spec = PluginSpec {
        state_field: Some(field),
        tr: Arc::new(TantivyIndexPluginTrait {}),
    };
    Ok(Arc::new(Plugin::new(spec)))
}