adk_ui/tools/
render_toast.rs

1use crate::schema::*;
2use adk_core::{Result, Tool, ToolContext};
3use async_trait::async_trait;
4use schemars::JsonSchema;
5use serde::{Deserialize, Serialize};
6use serde_json::Value;
7use std::sync::Arc;
8
9/// Parameters for the render_toast tool
10#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
11pub struct RenderToastParams {
12    /// Toast message to display
13    pub message: String,
14    /// Toast variant: info, success, warning, error
15    #[serde(default = "default_variant")]
16    pub variant: String,
17    /// Duration in milliseconds before auto-dismiss (default 5000)
18    #[serde(default = "default_duration")]
19    pub duration: u32,
20    /// Whether the toast can be manually dismissed
21    #[serde(default = "default_true")]
22    pub dismissible: bool,
23}
24
25fn default_variant() -> String {
26    "info".to_string()
27}
28
29fn default_duration() -> u32 {
30    5000
31}
32
33fn default_true() -> bool {
34    true
35}
36
37/// Tool for rendering toast notifications
38pub struct RenderToastTool;
39
40impl RenderToastTool {
41    pub fn new() -> Self {
42        Self
43    }
44}
45
46impl Default for RenderToastTool {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52#[async_trait]
53impl Tool for RenderToastTool {
54    fn name(&self) -> &str {
55        "render_toast"
56    }
57
58    fn description(&self) -> &str {
59        "Render a temporary toast notification. Use for brief status updates, success messages, or non-blocking alerts that auto-dismiss."
60    }
61
62    fn parameters_schema(&self) -> Option<Value> {
63        Some(super::generate_gemini_schema::<RenderToastParams>())
64    }
65
66    async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
67        let params: RenderToastParams = serde_json::from_value(args)
68            .map_err(|e| adk_core::AdkError::Tool(format!("Invalid parameters: {}", e)))?;
69
70        let variant = match params.variant.as_str() {
71            "success" => AlertVariant::Success,
72            "warning" => AlertVariant::Warning,
73            "error" => AlertVariant::Error,
74            _ => AlertVariant::Info,
75        };
76
77        let ui = UiResponse::new(vec![Component::Toast(Toast {
78            id: None,
79            message: params.message,
80            variant,
81            duration: params.duration,
82            dismissible: params.dismissible,
83        })]);
84
85        serde_json::to_value(ui)
86            .map_err(|e| adk_core::AdkError::Tool(format!("Failed to serialize UI: {}", e)))
87    }
88}