use std::sync::{Arc, Mutex};
use async_trait::async_trait;
use serde::Deserialize;
use serde_json::{Value, json};
use crate::agent_loop::{GoalSlots, GoalSource};
use crate::tools::traits::{ToolExecutor, ToolResult};
pub const SET_GOAL_TOOL_NAME: &str = "set_goal";
pub const CLEAR_GOAL_TOOL_NAME: &str = "clear_goal";
#[derive(Debug, Deserialize)]
struct SetGoalArgs {
slot: usize,
text: String,
}
#[derive(Debug, Deserialize)]
struct ClearGoalArgs {
slot: usize,
}
fn lock_or_recoverable_error(
goals: &Mutex<GoalSlots>,
) -> Result<std::sync::MutexGuard<'_, GoalSlots>, ToolResult> {
goals
.lock()
.map_err(|_| ToolResult::err("goal slots lock was poisoned by a prior panic"))
}
pub struct SetGoalTool {
goals: Arc<Mutex<GoalSlots>>,
}
impl SetGoalTool {
pub fn new(goals: Arc<Mutex<GoalSlots>>) -> Self {
Self { goals }
}
}
#[async_trait]
impl ToolExecutor for SetGoalTool {
fn name(&self) -> &str {
SET_GOAL_TOOL_NAME
}
fn schema(&self) -> Value {
json!({
"type": "function",
"function": {
"name": SET_GOAL_TOOL_NAME,
"description": "Record or update one of your 5 fixed, privileged standing goals — objectives that persist across turns and survive context compaction. These are NOT a todo list: use this only when your actual, durable objective changes (e.g. the user gives you a new top-level task, or a sub-objective is added). Do not use this for step-by-step task tracking.",
"parameters": {
"type": "object",
"properties": {
"slot": {
"type": "integer",
"description": "Which of the 5 fixed goal slots to write (1-5). Writing an occupied slot replaces its previous content."
},
"text": {
"type": "string",
"description": "The standing goal's description."
}
},
"required": ["slot", "text"],
"additionalProperties": false
}
}
})
}
async fn execute(&self, args: Value) -> ToolResult {
let parsed: SetGoalArgs = match serde_json::from_value(args) {
Ok(p) => p,
Err(e) => {
return ToolResult::err(format!(
"set_goal arguments did not match the expected shape ({e}). \
'slot' must be an integer 1-5 and 'text' must be a string."
));
}
};
let mut guard = match lock_or_recoverable_error(&self.goals) {
Ok(g) => g,
Err(err) => return err,
};
match guard.set(parsed.slot, parsed.text.clone(), GoalSource::Model) {
Ok(()) => ToolResult::ok(format!("Goal slot {} set: {}", parsed.slot, parsed.text)),
Err(e) => ToolResult::err(e.to_string()),
}
}
}
pub struct ClearGoalTool {
goals: Arc<Mutex<GoalSlots>>,
}
impl ClearGoalTool {
pub fn new(goals: Arc<Mutex<GoalSlots>>) -> Self {
Self { goals }
}
}
#[async_trait]
impl ToolExecutor for ClearGoalTool {
fn name(&self) -> &str {
CLEAR_GOAL_TOOL_NAME
}
fn schema(&self) -> Value {
json!({
"type": "function",
"function": {
"name": CLEAR_GOAL_TOOL_NAME,
"description": "Retire one of your 5 fixed, privileged standing goals once it is fully accomplished or no longer relevant, leaving that slot empty.",
"parameters": {
"type": "object",
"properties": {
"slot": {
"type": "integer",
"description": "Which of the 5 fixed goal slots to clear (1-5)."
}
},
"required": ["slot"],
"additionalProperties": false
}
}
})
}
async fn execute(&self, args: Value) -> ToolResult {
let parsed: ClearGoalArgs = match serde_json::from_value(args) {
Ok(p) => p,
Err(e) => {
return ToolResult::err(format!(
"clear_goal arguments did not match the expected shape ({e}). \
'slot' must be an integer 1-5."
));
}
};
let mut guard = match lock_or_recoverable_error(&self.goals) {
Ok(g) => g,
Err(err) => return err,
};
match guard.clear(parsed.slot) {
Ok(()) => ToolResult::ok(format!("Goal slot {} cleared.", parsed.slot)),
Err(e) => ToolResult::err(e.to_string()),
}
}
}
#[cfg(test)]
#[path = "goals_tests.rs"]
mod tests;