adk_ui/tools/
render_alert.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_alert tool
10#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
11pub struct RenderAlertParams {
12    /// Alert title
13    pub title: String,
14    /// Optional detailed message
15    #[serde(default)]
16    pub description: Option<String>,
17    /// Alert variant: info, success, warning, error
18    #[serde(default = "default_variant")]
19    pub variant: String,
20}
21
22fn default_variant() -> String {
23    "info".to_string()
24}
25
26/// Tool for rendering alerts/notifications
27pub struct RenderAlertTool;
28
29impl RenderAlertTool {
30    pub fn new() -> Self {
31        Self
32    }
33}
34
35impl Default for RenderAlertTool {
36    fn default() -> Self {
37        Self::new()
38    }
39}
40
41#[async_trait]
42impl Tool for RenderAlertTool {
43    fn name(&self) -> &str {
44        "render_alert"
45    }
46
47    fn description(&self) -> &str {
48        "Render an alert notification to inform the user about something. Use for success messages, warnings, errors, or important information."
49    }
50
51    fn parameters_schema(&self) -> Option<Value> {
52        Some(super::generate_gemini_schema::<RenderAlertParams>())
53    }
54
55    async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
56        let params: RenderAlertParams = serde_json::from_value(args)
57            .map_err(|e| adk_core::AdkError::Tool(format!("Invalid parameters: {}", e)))?;
58
59        let variant = match params.variant.as_str() {
60            "success" => AlertVariant::Success,
61            "warning" => AlertVariant::Warning,
62            "error" => AlertVariant::Error,
63            _ => AlertVariant::Info,
64        };
65
66        let ui = UiResponse::new(vec![Component::Alert(Alert {
67            id: None,
68            title: params.title,
69            description: params.description,
70            variant,
71        })]);
72
73        serde_json::to_value(ui)
74            .map_err(|e| adk_core::AdkError::Tool(format!("Failed to serialize UI: {}", e)))
75    }
76}