use std::borrow::Cow;
use std::fmt::Write as _;
use std::sync::Arc;
#[cfg(test)]
mod tests {
use super::*;
use aither_core::llm::tool::ToolDefinition;
#[test]
fn todo_schema_has_status_enum() {
let tool = TodoTool::new();
let def = ToolDefinition::new(&tool);
let schema = def.arguments_openai_schema();
let schema_obj = schema.as_object().expect("schema should be object");
let properties = schema_obj
.get("properties")
.expect("should have properties")
.as_object()
.unwrap();
let todos = properties
.get("todos")
.expect("should have todos")
.as_object()
.unwrap();
let items = todos
.get("items")
.expect("todos should have items")
.as_object()
.unwrap();
let item_props = items
.get("properties")
.expect("item should have properties")
.as_object()
.unwrap();
let status = item_props
.get("status")
.expect("should have status")
.as_object()
.unwrap();
assert!(
status.contains_key("enum") || status.contains_key("type"),
"Status should have enum or type field. Full schema: {}",
serde_json::to_string_pretty(&schema).unwrap()
);
}
}
use aither_core::llm::{Tool, ToolResult};
use arc_swap::ArcSwap;
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "snake_case")]
pub enum TodoStatus {
Pending,
InProgress,
Completed,
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TodoItem {
pub content: String,
pub status: TodoStatus,
#[serde(rename = "activeForm")]
pub active_form: String,
}
#[derive(Debug, Clone, Default)]
pub struct TodoList {
items: Arc<ArcSwap<Vec<TodoItem>>>,
}
impl TodoList {
#[must_use]
pub fn new() -> Self {
Self::default()
}
#[must_use]
pub fn items(&self) -> Vec<TodoItem> {
self.items.load_full().as_ref().clone()
}
pub fn write(&self, items: Vec<TodoItem>) {
self.items.store(Arc::new(items));
}
pub fn clear(&self) {
self.items.store(Arc::new(Vec::new()));
}
#[must_use]
pub fn current_task(&self) -> Option<TodoItem> {
self.items
.load()
.iter()
.find(|i| i.status == TodoStatus::InProgress)
.cloned()
}
#[must_use]
pub fn progress_summary(&self) -> String {
let items = self.items.load();
if items.is_empty() {
return String::new();
}
let completed = items
.iter()
.filter(|i| i.status == TodoStatus::Completed)
.count();
let in_progress = items
.iter()
.filter(|i| i.status == TodoStatus::InProgress)
.count();
let pending = items
.iter()
.filter(|i| i.status == TodoStatus::Pending)
.count();
let total = items.len();
let mut summary = format!("Progress: {completed}/{total} completed");
if in_progress > 0
&& let Some(current) = items.iter().find(|i| i.status == TodoStatus::InProgress)
{
let _ = write!(summary, " | Current: {}", current.active_form);
}
if pending > 0 {
let _ = write!(summary, " | {pending} pending");
}
summary
}
}
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct TodoWriteArgs {
pub todos: Vec<TodoItem>,
}
#[derive(Debug, Clone)]
pub struct TodoTool {
list: TodoList,
}
impl TodoTool {
#[must_use]
pub fn new() -> Self {
Self {
list: TodoList::new(),
}
}
#[must_use]
pub const fn with_list(list: TodoList) -> Self {
Self { list }
}
#[must_use]
pub const fn list(&self) -> &TodoList {
&self.list
}
}
impl Default for TodoTool {
fn default() -> Self {
Self::new()
}
}
impl Tool for TodoTool {
fn name(&self) -> Cow<'static, str> {
Cow::Borrowed("todo")
}
type Arguments = TodoWriteArgs;
type Res = ToolResult;
fn call(
&self,
arguments: Self::Arguments,
_cx: aither_core::llm::ToolContext,
) -> impl std::future::Future<Output = aither_core::Result<Self::Res>> + Send {
std::future::ready((|| {
let in_progress_count = arguments
.todos
.iter()
.filter(|t| t.status == TodoStatus::InProgress)
.count();
if in_progress_count > 1 {
return Err(anyhow::anyhow!(
"Only one task should be in_progress at a time, found {in_progress_count}"
));
}
self.list.write(arguments.todos);
Ok(ToolResult::Done)
})())
}
}