use super::helpers::{extract_bool, extract_string};
use super::protocol::JsonRpcError;
use crate::cli::registry::ProjectRegistry;
use serde_json::Value;
use std::sync::Arc;
#[derive(Clone)]
pub struct IndexHandler;
impl IndexHandler {
pub fn name(&self) -> &str {
"leindex.index"
}
pub fn title(&self) -> &str {
"LeIndex [Index]"
}
pub fn description(&self) -> &str {
"Start or poll a registry-owned project index job. Returns immediately by default; \
use wait=true only when an interactive caller explicitly wants to wait. Core PDG and TF-IDF \
results publish first, then the configured neural worker is actively evaluated for hybrid rows."
}
pub fn argument_schema(&self) -> Value {
serde_json::json!({
"type": "object",
"properties": {
"project_path": {
"type": "string",
"description": "Absolute path to the project directory to index"
},
"force_reindex": {
"type": "boolean",
"description": "If true, re-index even if already indexed (default: false). \
Also accepts compatibility strings: 'true'/'false', '1'/'0', 'yes'/'no'.",
"default": false
},
"wait": {
"type": "boolean",
"description": "Wait for completion instead of returning a pollable job snapshot (default: false)",
"default": false
}
},
"required": ["project_path"]
})
}
pub async fn execute(
&self,
registry: &Arc<ProjectRegistry>,
args: Value,
) -> Result<Value, JsonRpcError> {
let project_path = extract_string(&args, "project_path")?;
let force_reindex = extract_bool(&args, "force_reindex", false);
let wait = extract_bool(&args, "wait", false);
let snapshot = registry
.start_index_job(Some(&project_path), force_reindex, wait)
.await?;
serde_json::to_value(snapshot)
.map_err(|e| JsonRpcError::internal_error(format!("Serialization error: {}", e)))
}
}