adk_ui/tools/
render_toast.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 RenderToastParams {
13 pub message: String,
15 #[serde(default = "default_variant")]
17 pub variant: String,
18 #[serde(default = "default_duration")]
20 pub duration: u32,
21 #[serde(default = "default_true")]
23 pub dismissible: bool,
24 #[serde(flatten)]
26 pub protocol: LegacyProtocolOptions,
27}
28
29fn default_variant() -> String {
30 "info".to_string()
31}
32
33fn default_duration() -> u32 {
34 5000
35}
36
37fn default_true() -> bool {
38 true
39}
40
41pub struct RenderToastTool;
43
44impl RenderToastTool {
45 pub fn new() -> Self {
46 Self
47 }
48}
49
50impl Default for RenderToastTool {
51 fn default() -> Self {
52 Self::new()
53 }
54}
55
56#[async_trait]
57impl Tool for RenderToastTool {
58 fn name(&self) -> &str {
59 "render_toast"
60 }
61
62 fn description(&self) -> &str {
63 "Render a temporary toast notification. Use for brief status updates, success messages, or non-blocking alerts that auto-dismiss."
64 }
65
66 fn parameters_schema(&self) -> Option<Value> {
67 Some(super::generate_gemini_schema::<RenderToastParams>())
68 }
69
70 async fn execute(&self, _ctx: Arc<dyn ToolContext>, args: Value) -> Result<Value> {
71 let params: RenderToastParams = serde_json::from_value(args)
72 .map_err(|e| adk_core::AdkError::Tool(format!("Invalid parameters: {}", e)))?;
73 let protocol_options = params.protocol.clone();
74
75 let variant = match params.variant.as_str() {
76 "success" => AlertVariant::Success,
77 "warning" => AlertVariant::Warning,
78 "error" => AlertVariant::Error,
79 _ => AlertVariant::Info,
80 };
81
82 let ui = UiResponse::new(vec![Component::Toast(Toast {
83 id: None,
84 message: params.message,
85 variant,
86 duration: params.duration,
87 dismissible: params.dismissible,
88 })]);
89
90 render_ui_response_with_protocol(ui, &protocol_options, "toast")
91 }
92}