adk_ui/tools/
render_alert.rs1use crate::schema::*;
2use crate::tools::{LegacyProtocolOptions, render_ui_response_with_protocol};
3use adk_core::{Result, Tool, ToolContext};
4use async_trait::async_trait;
5use schemars::JsonSchema;
6use serde::{Deserialize, Serialize};
7use serde_json::Value;
8use std::sync::Arc;
9
10#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
12pub struct RenderAlertParams {
13 pub title: String,
15 #[serde(default)]
17 pub description: Option<String>,
18 #[serde(default = "default_variant")]
20 pub variant: String,
21 #[serde(flatten)]
23 pub protocol: LegacyProtocolOptions,
24}
25
26fn default_variant() -> String {
27 "info".to_string()
28}
29
30pub struct RenderAlertTool;
32
33impl RenderAlertTool {
34 pub fn new() -> Self {
35 Self
36 }
37}
38
39impl Default for RenderAlertTool {
40 fn default() -> Self {
41 Self::new()
42 }
43}
44
45#[async_trait]
46impl Tool for RenderAlertTool {
47 fn name(&self) -> &str {
48 "render_alert"
49 }
50
51 fn description(&self) -> &str {
52 "Render an alert notification to inform the user about something. Use for success messages, warnings, errors, or important information."
53 }
54
55 fn parameters_schema(&self) -> Option<Value> {
56 Some(super::generate_gemini_schema::<RenderAlertParams>())
57 }
58
59 async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
60 let params: RenderAlertParams = serde_json::from_value(args)
61 .map_err(|e| adk_core::AdkError::Tool(format!("Invalid parameters: {}", e)))?;
62 let protocol_options = params.protocol.clone();
63
64 let variant = match params.variant.as_str() {
65 "success" => AlertVariant::Success,
66 "warning" => AlertVariant::Warning,
67 "error" => AlertVariant::Error,
68 _ => AlertVariant::Info,
69 };
70
71 let ui = UiResponse::new(vec![Component::Alert(Alert {
72 id: None,
73 title: params.title,
74 description: params.description,
75 variant,
76 })]);
77
78 render_ui_response_with_protocol(ui, &protocol_options, "alert")
79 }
80}