feat: add memory management tools and utility commands

This commit is contained in:
asepharyana
2026-07-11 20:44:15 +07:00
parent 08490532d2
commit fe82840c03
13 changed files with 535 additions and 0 deletions
+47
View File
@@ -0,0 +1,47 @@
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use super::super::Tool;
use super::super::ToolCtx;
pub struct Cd;
impl Tool for Cd {
fn name(&self) -> &'static str {
"cd"
}
fn description(&self) -> &'static str {
"Check if a directory exists within the workspace and print its resolved path. Use this to verify a directory path before running other commands there."
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory path (relative to workspace root)"
}
},
"required": ["path"]
})
}
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
let rel = args.get("path")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: path"))?;
let path = super::super::resolve_path(&ctx.workspaces, rel)?;
if !path.exists() {
return Ok(format!("path '{}' does not exist (resolved to {})", rel, path.display()));
}
if !path.is_dir() {
return Ok(format!("path '{}' is not a directory (resolved to {})", rel, path.display()));
}
let canon = path.canonicalize().unwrap_or(path);
Ok(format!("{}", canon.display()))
}
}
+64
View File
@@ -0,0 +1,64 @@
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use super::super::Tool;
use super::super::ToolCtx;
pub struct DirCacheUpdate;
impl Tool for DirCacheUpdate {
fn name(&self) -> &'static str {
"dir_cache_update"
}
fn description(&self) -> &'static str {
"Update the cached directory listing for a path. The directory cache is used by other tools for faster path resolution."
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory path to cache (relative to workspace root)"
}
},
"required": ["path"]
})
}
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
let rel = args.get("path")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: path"))?;
let path = super::super::resolve_path(&ctx.workspaces, rel)?;
if !path.exists() {
return Ok(format!("path '{}' does not exist (resolved to {})", rel, path.display()));
}
let entries = walk_directory(&path);
let count = entries.len();
let dc = ctx.dir_cache.clone();
let rt = tokio::runtime::Handle::try_current()
.map_err(|e| anyhow!("no tokio runtime available: {}", e))?;
rt.block_on(async move {
let cache = dc.write().await;
cache.set(entries).await;
});
Ok(format!("cached {} entries for {}", count, rel))
}
}
fn walk_directory(path: &std::path::Path) -> Vec<std::path::PathBuf> {
let mut result = Vec::new();
if let Ok(entries) = std::fs::read_dir(path) {
for entry in entries.flatten() {
result.push(entry.path());
}
}
result
}
+67
View File
@@ -0,0 +1,67 @@
use std::fs;
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use super::super::Tool;
use super::super::ToolCtx;
pub struct DirList;
impl Tool for DirList {
fn name(&self) -> &'static str {
"dir_list"
}
fn description(&self) -> &'static str {
"List files and directories in a directory. Use this to explore the workspace structure."
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"path": {
"type": "string",
"description": "Directory path to list (relative to workspace root)"
}
},
"required": ["path"]
})
}
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
let rel = args.get("path")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: path"))?;
let path = super::super::resolve_path(&ctx.workspaces, rel)?;
if !path.exists() {
return Ok(format!("path '{}' does not exist (resolved to {})", rel, path.display()));
}
if !path.is_dir() {
return Ok(format!("path '{}' is not a directory (resolved to {})", rel, path.display()));
}
let entries: Vec<String> = fs::read_dir(&path)
.map_err(|e| anyhow!("failed to read directory '{}': {}", rel, e))?
.filter_map(|e| e.ok())
.map(|e| {
let name = e.file_name().to_string_lossy().to_string();
let is_dir = e.file_type().map(|t| t.is_dir()).unwrap_or(false);
if is_dir {
format!("{}/", name)
} else {
name
}
})
.collect();
let canon = path.canonicalize().unwrap_or(path);
let header = format!("{} entries in {}:\n", entries.len(), canon.display());
if entries.is_empty() {
Ok(format!("{} (empty directory)", header.trim()))
} else {
Ok(header + &entries.join("\n"))
}
}
}
+5
View File
@@ -0,0 +1,5 @@
pub mod cd;
pub mod dir_cache_update;
pub mod dir_list;
pub mod pong;
pub mod todowrite;
+35
View File
@@ -0,0 +1,35 @@
use serde_json::{json, Value};
use anyhow::Result;
use super::super::Tool;
use super::super::ToolCtx;
pub struct Pong;
impl Tool for Pong {
fn name(&self) -> &'static str {
"pong"
}
fn description(&self) -> &'static str {
"Simple connectivity check. Echoes back any input for health checks and latency testing."
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"message": {
"type": "string",
"description": "Message to echo back"
}
}
})
}
fn run(&self, _ctx: &ToolCtx, args: &Value) -> Result<String> {
let msg = args.get("message")
.and_then(|v| v.as_str())
.unwrap_or("pong");
Ok(format!("pong: {}", msg))
}
}
+54
View File
@@ -0,0 +1,54 @@
use std::fs;
use std::path::PathBuf;
use serde_json::{json, Value};
use anyhow::{Result, anyhow};
use super::super::Tool;
use super::super::ToolCtx;
pub struct Todowrite;
impl Tool for Todowrite {
fn name(&self) -> &'static str {
"todowrite"
}
fn description(&self) -> &'static str {
"Append a task to the session todo list. The todo persists in the session directory and is visible in the Todo panel."
}
fn parameters(&self) -> Value {
json!({
"type": "object",
"properties": {
"task": {
"type": "string",
"description": "Task description to add"
}
},
"required": ["task"]
})
}
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
let task = args.get("task")
.and_then(|v| v.as_str())
.ok_or_else(|| anyhow!("missing required argument: task"))?;
let path: PathBuf = ctx.session_dir.join("todo.md");
let now = chrono::Utc::now();
let timestamp = now.format("%Y-%m-%d %H:%M:%S");
let line = format!("- [ ] {} ({})\n", task, timestamp);
fs::OpenOptions::new()
.create(true)
.append(true)
.open(&path)
.map_err(|e| anyhow!("failed to open todo.md: {}", e))?
.write_all(line.as_bytes())
.map_err(|e| anyhow!("failed to write to todo.md: {}", e))?;
Ok(format!("added task to todo.md: {}", task))
}
}
use std::io::Write;