2026-07-20 12:42:53 +07:00
|
|
|
//! Update the shared directory cache by resolving each path against
|
|
|
|
|
//! workspaces and storing the resolved paths in `ctx.dir_cache`.
|
2026-07-20 09:04:57 +07:00
|
|
|
|
2026-07-20 12:42:53 +07:00
|
|
|
use crate::tools::{resolve_path, ToolCtx};
|
2026-07-20 09:04:57 +07:00
|
|
|
use anyhow::Result;
|
|
|
|
|
use serde_json::{json, Value};
|
2026-07-20 12:42:53 +07:00
|
|
|
use std::path::PathBuf;
|
2026-07-20 09:04:57 +07:00
|
|
|
|
|
|
|
|
pub struct DirCacheUpdate;
|
|
|
|
|
|
|
|
|
|
impl crate::tools::Tool for DirCacheUpdate {
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
|
"dir_cache_update"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
|
"Update the cached directory listing"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parameters(&self) -> Value {
|
|
|
|
|
json!({
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"paths": {
|
|
|
|
|
"type": "array",
|
|
|
|
|
"items": {"type": "string"},
|
|
|
|
|
"description": "New list of paths for the cache"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 12:42:53 +07:00
|
|
|
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
|
2026-07-20 09:04:57 +07:00
|
|
|
let paths: Vec<String> = args
|
|
|
|
|
.get("paths")
|
|
|
|
|
.and_then(|v| v.as_array())
|
|
|
|
|
.map(|arr| {
|
|
|
|
|
arr.iter()
|
|
|
|
|
.filter_map(|v| v.as_str().map(String::from))
|
|
|
|
|
.collect()
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
2026-07-20 12:42:53 +07:00
|
|
|
let resolved: Vec<PathBuf> = paths
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|p| resolve_path(&ctx.workspaces, p))
|
|
|
|
|
.collect::<Result<Vec<_>>>()?;
|
|
|
|
|
|
|
|
|
|
let count = resolved.len();
|
|
|
|
|
|
|
|
|
|
// Persist the resolved paths into the shared DirCache so the TUI
|
|
|
|
|
// and other tools can read the cached listing without re-scanning.
|
|
|
|
|
let dc = ctx.dir_cache.clone();
|
|
|
|
|
let rt = tokio::runtime::Runtime::new()?;
|
|
|
|
|
rt.block_on(async { dc.write().await.set(resolved).await });
|
|
|
|
|
|
2026-07-20 09:04:57 +07:00
|
|
|
Ok(format!("Directory cache updated with {} entries", count))
|
|
|
|
|
}
|
|
|
|
|
}
|