2026-07-20 09:04:57 +07:00
|
|
|
//! Write a TODO item.
|
2026-07-20 15:53:20 +07:00
|
|
|
//!
|
|
|
|
|
//! Appends a new unchecked TODO entry to the session's `TODO.md`
|
|
|
|
|
//! file with an optional priority marker and emits a
|
|
|
|
|
//! `TurnEvent::TodoUpdate` for the TUI.
|
2026-07-20 09:04:57 +07:00
|
|
|
|
|
|
|
|
use crate::tools::{Tool, ToolCtx};
|
|
|
|
|
use anyhow::Result;
|
|
|
|
|
use serde_json::{json, Value};
|
2026-07-20 15:53:20 +07:00
|
|
|
use tracing::{info, instrument};
|
2026-07-20 09:04:57 +07:00
|
|
|
|
2026-07-20 15:53:20 +07:00
|
|
|
/// Tool that adds an item to the session TODO list.
|
|
|
|
|
///
|
|
|
|
|
/// Flow: parse item + optional priority → append `- [priority] item\n`
|
|
|
|
|
/// to `TODO.md` → write file → push `TurnEvent::TodoUpdate` if events
|
|
|
|
|
/// channel exists → confirm addition.
|
2026-07-20 09:04:57 +07:00
|
|
|
pub struct Todowrite;
|
|
|
|
|
|
|
|
|
|
impl Tool for Todowrite {
|
|
|
|
|
fn name(&self) -> &'static str {
|
|
|
|
|
"todowrite"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn description(&self) -> &'static str {
|
|
|
|
|
"Add an item to the TODO list"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn parameters(&self) -> Value {
|
|
|
|
|
json!({
|
|
|
|
|
"type": "object",
|
|
|
|
|
"properties": {
|
|
|
|
|
"item": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"description": "TODO item text"
|
|
|
|
|
},
|
|
|
|
|
"priority": {
|
|
|
|
|
"type": "string",
|
|
|
|
|
"enum": ["high", "medium", "low"],
|
|
|
|
|
"description": "Priority level"
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
"required": ["item"]
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2026-07-20 15:53:20 +07:00
|
|
|
#[instrument(skip(self, ctx, args))]
|
2026-07-20 14:47:13 +07:00
|
|
|
fn run(&self, ctx: &ToolCtx, args: &Value) -> Result<String> {
|
2026-07-20 09:04:57 +07:00
|
|
|
let item = crate::tools::arg_str(args, "item")?;
|
|
|
|
|
let priority = args
|
|
|
|
|
.get("priority")
|
|
|
|
|
.and_then(|v| v.as_str())
|
|
|
|
|
.unwrap_or("medium");
|
|
|
|
|
|
2026-07-20 15:53:20 +07:00
|
|
|
info!(item, priority, "todowrite invoked");
|
|
|
|
|
|
2026-07-20 14:47:13 +07:00
|
|
|
let todo_line = format!("- [{}] {}\n", priority, item);
|
|
|
|
|
|
|
|
|
|
let todo_path = ctx.session_dir.join("TODO.md");
|
|
|
|
|
let mut content = std::fs::read_to_string(&todo_path).unwrap_or_default();
|
|
|
|
|
content.push_str(&todo_line);
|
|
|
|
|
|
|
|
|
|
let _ = std::fs::write(&todo_path, &content);
|
|
|
|
|
|
|
|
|
|
if let Some(events) = &ctx.turn_events {
|
2026-07-21 07:00:15 +07:00
|
|
|
if let Ok(mut q) = events.lock() {
|
|
|
|
|
q.push_back(crate::TurnEvent::TodoUpdate(content));
|
|
|
|
|
}
|
2026-07-20 14:47:13 +07:00
|
|
|
}
|
|
|
|
|
|
2026-07-20 15:53:20 +07:00
|
|
|
info!(item, priority, "TODO item added");
|
2026-07-20 09:04:57 +07:00
|
|
|
Ok(format!("[{}] TODO added: {}", priority, item))
|
|
|
|
|
}
|
|
|
|
|
}
|