adk_tool/bigquery/toolset.rs
1//! BigQuery toolset structure and `Toolset` trait implementation.
2
3use crate::bigquery::tools::{
4 BigQueryExecuteSql, BigQueryGetTableSchema, BigQueryListDatasets, BigQueryListTables,
5};
6use adk_core::{ReadonlyContext, Result, Tool, Toolset};
7use async_trait::async_trait;
8use std::sync::Arc;
9
10/// How BigQuery credentials are resolved at runtime.
11#[derive(Debug, Clone)]
12pub enum CredentialSource {
13 /// Use Google Cloud Application Default Credentials.
14 ApplicationDefault,
15 /// A secret name resolved via `ToolContext::get_secret()` at execution time.
16 SecretRef(String),
17}
18
19/// Native BigQuery toolset providing SQL execution, schema inspection, and
20/// dataset/table listing tools.
21///
22/// Authenticates using Google Cloud Application Default Credentials or a
23/// service account key provided via the configured
24/// [`SecretProvider`](adk_core::ToolContext::get_secret).
25///
26/// # Example
27///
28/// ```rust,ignore
29/// use adk_tool::bigquery::BigQueryToolset;
30///
31/// // Application Default Credentials
32/// let toolset = BigQueryToolset::new();
33///
34/// // With explicit project ID
35/// let toolset = BigQueryToolset::with_project("my-gcp-project");
36///
37/// // Via secret provider (resolved at tool execution time)
38/// let toolset = BigQueryToolset::from_secret("bigquery-credentials");
39///
40/// // Allow DML, DDL, and scripts through `bigquery_execute_sql`
41/// let toolset = BigQueryToolset::with_project("my-gcp-project").with_read_only(false);
42/// ```
43///
44/// # Read-only mode
45///
46/// `bigquery_execute_sql` is read-only by default: it accepts a single `SELECT` or
47/// `WITH … SELECT` statement, which a BigQuery dry run must classify as `SELECT`
48/// before the query runs. See [`with_read_only`](Self::with_read_only).
49pub struct BigQueryToolset {
50 pub(crate) project_id: Option<String>,
51 pub(crate) credentials: CredentialSource,
52 pub(crate) read_only: bool,
53}
54
55impl BigQueryToolset {
56 /// Create a new `BigQueryToolset` using Application Default Credentials.
57 ///
58 /// The project ID will be inferred from the environment or must be
59 /// specified in each query.
60 pub fn new() -> Self {
61 Self {
62 project_id: None,
63 credentials: CredentialSource::ApplicationDefault,
64 read_only: true,
65 }
66 }
67
68 /// Create a new `BigQueryToolset` with an explicit Google Cloud project ID.
69 ///
70 /// Uses Application Default Credentials for authentication.
71 pub fn with_project(project_id: impl Into<String>) -> Self {
72 Self {
73 project_id: Some(project_id.into()),
74 credentials: CredentialSource::ApplicationDefault,
75 read_only: true,
76 }
77 }
78
79 /// Create a new `BigQueryToolset` that resolves credentials from the
80 /// secret provider at execution time via `ctx.get_secret(secret_name)`.
81 pub fn from_secret(secret_name: impl Into<String>) -> Self {
82 Self {
83 project_id: None,
84 credentials: CredentialSource::SecretRef(secret_name.into()),
85 read_only: true,
86 }
87 }
88
89 /// Sets whether `bigquery_execute_sql` is restricted to read-only queries.
90 ///
91 /// On by default. In read-only mode the tool rejects anything but a single
92 /// `SELECT` or `WITH … SELECT` statement, confirms the statement type with a
93 /// BigQuery dry run before running the query, and reports itself as read-only.
94 /// Passing `false` allows DML, DDL, and scripts, and the tool then reports itself
95 /// as mutating, so parallel dispatch no longer runs it alongside other calls.
96 ///
97 /// # Example
98 ///
99 /// ```rust,ignore
100 /// use adk_tool::bigquery::BigQueryToolset;
101 ///
102 /// let toolset = BigQueryToolset::with_project("my-gcp-project").with_read_only(false);
103 /// ```
104 #[must_use]
105 pub fn with_read_only(mut self, read_only: bool) -> Self {
106 self.read_only = read_only;
107 self
108 }
109}
110
111impl Default for BigQueryToolset {
112 fn default() -> Self {
113 Self::new()
114 }
115}
116
117#[async_trait]
118impl Toolset for BigQueryToolset {
119 fn name(&self) -> &str {
120 "bigquery"
121 }
122
123 async fn tools(&self, _ctx: Arc<dyn ReadonlyContext>) -> Result<Vec<Arc<dyn Tool>>> {
124 let project_id = self.project_id.clone();
125 let credentials = self.credentials.clone();
126
127 Ok(vec![
128 Arc::new(BigQueryExecuteSql::new(
129 project_id.clone(),
130 credentials.clone(),
131 self.read_only,
132 )),
133 Arc::new(BigQueryGetTableSchema::new(project_id.clone(), credentials.clone())),
134 Arc::new(BigQueryListDatasets::new(project_id.clone(), credentials.clone())),
135 Arc::new(BigQueryListTables::new(project_id, credentials)),
136 ])
137 }
138}