55 lines
1.5 KiB
Rust
55 lines
1.5 KiB
Rust
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;
|