Skip to main content

rs_adk/planners/
mod.rs

1//! Planner system — enables agents to generate plans before acting.
2//!
3//! Mirrors ADK-Python's `planners` module. Provides traits for building
4//! planning instructions and processing planning responses, plus a
5//! Plan-ReAct planner implementation.
6
7mod built_in;
8mod plan_re_act;
9
10pub use built_in::BuiltInPlanner;
11pub use plan_re_act::PlanReActPlanner;
12
13use async_trait::async_trait;
14
15use crate::llm::LlmRequest;
16
17/// Errors from planner operations.
18#[derive(Debug, thiserror::Error)]
19pub enum PlannerError {
20    /// Planning instruction generation failed.
21    #[error("Planning instruction error: {0}")]
22    Instruction(String),
23    /// Planning response processing failed.
24    #[error("Planning response error: {0}")]
25    Response(String),
26}
27
28/// Trait for agent planners that guide reasoning and action.
29///
30/// A planner modifies the agent's LLM request to inject planning instructions
31/// and post-processes the LLM response to extract/filter planning steps.
32#[async_trait]
33pub trait Planner: Send + Sync {
34    /// Build planning instructions to inject into the LLM request.
35    ///
36    /// Returns `Some(instruction)` to append to the system instruction,
37    /// or `None` to skip planning for this request.
38    fn build_planning_instruction(
39        &self,
40        request: &LlmRequest,
41    ) -> Result<Option<String>, PlannerError>;
42
43    /// Process the LLM response from a planning-augmented request.
44    ///
45    /// Can filter, reorder, or annotate response parts.
46    /// Returns `None` to use the response as-is.
47    fn process_planning_response(
48        &self,
49        response_text: &str,
50    ) -> Result<Option<String>, PlannerError>;
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    fn _assert_object_safe(_: &dyn Planner) {}
58
59    #[test]
60    fn planner_error_display() {
61        let err = PlannerError::Instruction("test".into());
62        assert!(err.to_string().contains("test"));
63    }
64}