google-ai-rs 0.1.1

Type-safe Rust client for Google's Generative AI APIs
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
use std::collections::VecDeque;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::RwLock;
use tonic::transport::{Channel, ClientTlsConfig, Endpoint};
use tonic::IntoRequest;

use crate::auth::{add_auth, Auth};
use crate::content::UpdateFieldMask as _;
use crate::error::{status_into_error, Error, NetError, SetupError, TonicTransportError};
use crate::full_model_name;
use crate::proto::model_service_client::ModelServiceClient;
use crate::proto::{
    cache_service_client::CacheServiceClient, generative_service_client::GenerativeServiceClient,
    CachedContent, CreateCachedContentRequest, DeleteCachedContentRequest, GetCachedContentRequest,
    ListCachedContentsRequest, UpdateCachedContentRequest,
};
use crate::proto::{
    DeleteTunedModelRequest, GetModelRequest, GetTunedModelRequest, ListModelsRequest,
    ListTunedModelsRequest, Model, TunedModel, UpdateTunedModelRequest,
};

/// Default timeout for client requests (2 minutes)
const DEFAULT_TIMEOUT: Duration = Duration::from_secs(120);
/// Base URL for Google's Generative Language API
const BASE_API_URL: &str = "https://generativelanguage.googleapis.com";
/// Default page size for paginated requests (server determines actual size when 0)
const DEFAULT_PAGE_SIZE: i32 = 0;

/// A thread-safe client for interacting with Google's Generative Language API.
///
/// # Features
/// - Manages authentication tokens and TLS configuration
/// - Provides access to generative AI operations
/// - Implements content caching functionality
/// - Supports automatic pagination of list operations
///
/// # Example
/// ```
/// use google_ai_rs::{Client, Auth};
///
/// # async fn f() -> Result<(), Box<dyn std::error::Error>> {
/// let auth = Auth::new("your-api-key");
/// let client = Client::new(auth).await?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Debug)]
pub struct Client {
    /// Generative service gRPC client
    pub(super) gc: GenerativeServiceClient<Channel>,
    /// Cache service gRPC client
    pub(super) cc: CacheServiceClient<Channel>,
    pub(super) mc: ModelServiceClient<Channel>,
    /// Authentication credentials with concurrent access support
    auth: Arc<RwLock<Auth>>,
}

impl Client {
    /// Constructs a new client with authentication and optional configuration.
    ///
    /// # Arguments
    /// * `auth` - API authentication credentials
    ///
    /// # Errors
    /// Returns [`Error::Setup`] for configuration issues or [`Error::Net`] for connection failures.
    pub async fn new(auth: Auth) -> Result<Self, Error> {
        ClientBuilder::new()
            .timeout(DEFAULT_TIMEOUT)
            .build(auth)
            .await
    }

    /// Create a builder for configuring client options
    pub fn builder() -> ClientBuilder {
        ClientBuilder::new()
    }

    /// Updates authentication credentials atomically
    ///
    /// Subsequent requests will use the new credentials immediately. This operation
    /// is thread-safe.
    pub async fn update_auth(&self, new_auth: Auth) {
        *self.auth.write().await = new_auth;
    }

    /// Creates a new cached content entry
    ///
    /// # Arguments
    /// * `content` - Content to cache without name (server-generated)
    ///
    /// # Errors
    /// Returns [`Error::InvalidArgument`] if content contains a name
    pub async fn create_cached_content(
        &self,
        content: CachedContent,
    ) -> Result<CachedContent, Error> {
        if content.name.is_some() {
            return Err(Error::InvalidArgument(
                "CachedContent name must be empty for creation".into(),
            ));
        }

        let mut request = CreateCachedContentRequest {
            cached_content: Some(content),
        }
        .into_request();

        self.add_auth(&mut request).await?;

        self.cc
            .clone()
            .create_cached_content(request)
            .await
            .map_err(status_into_error)
            .map(|r| r.into_inner())
    }

    /// Retrieves the `CachedContent` with the given name.
    pub async fn get_cached_content(&self, name: &str) -> Result<CachedContent, Error> {
        let mut request = GetCachedContentRequest {
            name: name.to_owned(),
        }
        .into_request();

        self.add_auth(&mut request).await?;

        self.cc
            .clone()
            .get_cached_content(request)
            .await
            .map_err(status_into_error)
            .map(|r| r.into_inner())
    }

    /// Deletes the `CachedContent` with the given name.
    pub async fn delete_cached_content(&self, name: &str) -> Result<(), Error> {
        let mut request = DeleteCachedContentRequest {
            name: name.to_owned(),
        }
        .into_request();

        self.add_auth(&mut request).await?;

        self.cc
            .clone()
            .delete_cached_content(request)
            .await
            .map_err(status_into_error)
            .map(|r| r.into_inner())
    }

    /// Modifies the `CachedContent`.
    ///
    /// It returns the modified CachedContent.
    ///
    /// The argument CachedContent must have its name field and fields to update populated.
    pub async fn update_cached_content(&self, cc: &CachedContent) -> Result<CachedContent, Error> {
        let mut request = UpdateCachedContentRequest {
            cached_content: Some(cc.to_owned()),
            update_mask: Some(cc.field_mask()),
        }
        .into_request();

        self.add_auth(&mut request).await?;

        self.cc
            .clone()
            .update_cached_content(request)
            .await
            .map_err(status_into_error)
            .map(|r| r.into_inner())
    }

    /// Returns an async iterator over cached content entries
    ///
    /// Automatically handles pagination through server-side results.
    pub fn list_cached_contents(&self) -> CachedContentIterator {
        PageIterator::<CachedContentPager>::new(self)
    }

    /// Gets information about a specific `Model` such as its version number, token
    /// limits, etc
    pub async fn get_model(&self, name: &str) -> Result<Model, Error> {
        let mut request = GetModelRequest {
            name: full_model_name(name),
        }
        .into_request();

        self.add_auth(&mut request).await?;
        self.mc
            .clone()
            .get_model(request)
            .await
            .map_err(status_into_error)
            .map(|r| r.into_inner())
    }

    /// Gets information about a specific `TunedModel`.
    pub async fn get_tuned_model(&self, resource_name: &str) -> Result<TunedModel, Error> {
        let mut request = GetTunedModelRequest {
            name: resource_name.to_owned(),
        }
        .into_request();

        self.add_auth(&mut request).await?;
        self.mc
            .clone()
            .get_tuned_model(request)
            .await
            .map_err(status_into_error)
            .map(|r| r.into_inner())
    }

    /// Returns an async iterator over models list results
    ///
    /// Automatically handles pagination through server-side results.
    pub async fn list_models(&self) -> ModelsListIterator {
        PageIterator::<ModelsListPager>::new(self)
    }

    /// Returns an async iterator over tuned models list results
    ///
    /// Automatically handles pagination through server-side results.
    pub async fn list_tuned_models(&self) -> TunedModelsListIterator {
        PageIterator::<TunedModelsListPager>::new(self)
    }

    /// Updates a tuned model.
    pub async fn update_tuned_model(&self, m: &TunedModel) -> Result<TunedModel, Error> {
        let mut request = UpdateTunedModelRequest {
            tuned_model: Some(m.to_owned()),
            update_mask: Some(m.field_mask()),
        }
        .into_request();

        self.add_auth(&mut request).await?;

        self.mc
            .clone()
            .update_tuned_model(request)
            .await
            .map_err(status_into_error)
            .map(|r| r.into_inner())
    }

    /// Deletes the `TunedModel` with the given name.
    pub async fn delete_tuned_model(&self, name: &str) -> Result<(), Error> {
        let mut request = DeleteTunedModelRequest {
            name: name.to_owned(),
        }
        .into_request();

        self.add_auth(&mut request).await?;

        self.mc
            .clone()
            .delete_tuned_model(request)
            .await
            .map_err(status_into_error)
            .map(|r| r.into_inner())
    }

    /// Adds authentication metadata to a request
    pub(super) async fn add_auth<T>(&self, request: &mut tonic::Request<T>) -> Result<(), Error> {
        Ok(add_auth(request, &*self.auth.read().await).await?)
    }
}

#[derive(Debug, Clone)]
pub struct ClientBuilder {
    endpoint: Endpoint,
}

impl Default for ClientBuilder {
    fn default() -> Self {
        Self::new()
    }
}

impl ClientBuilder {
    /// Creates new builder with required authentication
    pub fn new() -> Self {
        Self {
            endpoint: Endpoint::from_static(BASE_API_URL),
        }
    }

    /// Sets overall request timeout (default: 120s)
    pub fn timeout(mut self, duration: Duration) -> Self {
        self.endpoint = self.endpoint.timeout(duration);
        self
    }

    /// Set connection establishment timeout
    pub fn connect_timeout(mut self, duration: Duration) -> Self {
        self.endpoint = self.endpoint.connect_timeout(duration);
        self
    }

    /// Set custom user agent string
    pub fn user_agent(mut self, ua: impl Into<String>) -> Result<Self, Error> {
        self.endpoint = self
            .endpoint
            .user_agent(ua.into())
            .map_err(|e| SetupError::new("User-Agent configuration", e))?;
        Ok(self)
    }

    /// Set maximum concurrent requests per connection
    pub fn concurrency_limit(mut self, limit: usize) -> Self {
        self.endpoint = self.endpoint.concurrency_limit(limit);
        self
    }

    /// Finalizes configuration and constructs client
    ///
    /// # Arguments
    /// * `auth` - Authentication credentials (API key or service account)
    ///
    /// # Errors
    /// - Returns [`Error::Setup`] for invalid configurations
    /// - Returns [`Error::Net`] for connection failures  
    pub async fn build(self, auth: Auth) -> Result<Client, Error> {
        let channel = self
            .endpoint
            .tls_config(ClientTlsConfig::new().with_enabled_roots())
            .map_err(|e| SetupError::new("TLS configuration", e))?
            .connect()
            .await
            .map_err(|e| {
                Error::Net(NetError::TransportFailure(TonicTransportError(Box::new(e))))
            })?;

        Ok(Client {
            gc: GenerativeServiceClient::new(channel.clone()),
            cc: CacheServiceClient::new(channel.clone()),
            mc: ModelServiceClient::new(channel),
            auth: Arc::new(RwLock::new(auth)),
        })
    }
}

/// Async iterator for paginated cached content results
pub type CachedContentIterator<'a> = PageIterator<'a, CachedContentPager>;

/// Async iterator for paginated models results
pub type ModelsListIterator<'a> = PageIterator<'a, ModelsListPager>;

/// Async iterator for paginated tuned models results
pub type TunedModelsListIterator<'a> = PageIterator<'a, TunedModelsListPager>;

/// Async iterator for paginated contents
///
/// Buffers results from multiple pages and provides linear access.
/// Implements automatic page fetching when buffer is exhausted.
pub struct PageIterator<'a, P>
where
    P: Page + Send,
{
    client: &'a Client,
    next_page_token: Option<String>,
    buffer: VecDeque<P::Content>,
}

impl<'a, P> PageIterator<'a, P>
where
    P: Page + Send,
{
    fn new(client: &'a Client) -> Self {
        Self {
            client,
            next_page_token: Some(String::new()),
            buffer: VecDeque::new(),
        }
    }

    /// Returns the next content item
    ///
    /// Returns `Ok(None)` when all items have been exhausted.
    pub async fn next(&mut self) -> Result<Option<P::Content>, Error> {
        if self.buffer.is_empty() {
            if self.next_page_token.is_none() {
                // We've already fetched all pages
                return Ok(None);
            }

            let (items, next_token) =
                P::next(self.client, self.next_page_token.as_ref().unwrap()).await?;

            self.next_page_token = if next_token.is_empty() {
                None
            } else {
                Some(next_token)
            };
            self.buffer.extend(items);
        }

        Ok(self.buffer.pop_front())
    }
}

#[tonic::async_trait]
pub trait Page: sealed::Sealed {
    type Content;
    /// Fetches the next page of results
    async fn next(client: &Client, page_token: &str)
        -> Result<(Vec<Self::Content>, String), Error>;
}

impl<T> sealed::Sealed for T {}

mod sealed {
    pub trait Sealed {}
}

pub struct CachedContentPager;

#[tonic::async_trait]
impl Page for CachedContentPager {
    type Content = CachedContent;

    async fn next(
        client: &Client,
        page_token: &str,
    ) -> Result<(Vec<Self::Content>, String), Error> {
        let mut request = ListCachedContentsRequest {
            page_size: DEFAULT_PAGE_SIZE,
            page_token: page_token.to_owned(),
        }
        .into_request();

        client.add_auth(&mut request).await?;

        let response = client
            .cc
            .clone()
            .list_cached_contents(request)
            .await
            .map_err(status_into_error)?
            .into_inner();
        Ok((response.cached_contents, response.next_page_token))
    }
}

pub struct ModelsListPager;

#[tonic::async_trait]
impl Page for ModelsListPager {
    type Content = Model;

    async fn next(
        client: &Client,
        page_token: &str,
    ) -> Result<(Vec<Self::Content>, String), Error> {
        let mut request = ListModelsRequest {
            page_size: DEFAULT_PAGE_SIZE,
            page_token: page_token.to_owned(),
        }
        .into_request();

        client.add_auth(&mut request).await?;

        let response = client
            .mc
            .clone()
            .list_models(request)
            .await
            .map_err(status_into_error)?
            .into_inner();
        Ok((response.models, response.next_page_token))
    }
}

pub struct TunedModelsListPager;

#[tonic::async_trait]
impl Page for TunedModelsListPager {
    type Content = TunedModel;

    async fn next(
        client: &Client,
        page_token: &str,
    ) -> Result<(Vec<Self::Content>, String), Error> {
        let mut request = ListTunedModelsRequest {
            page_size: DEFAULT_PAGE_SIZE,
            page_token: page_token.to_owned(),
            filter: String::new(),
        }
        .into_request();

        client.add_auth(&mut request).await?;

        let response = client
            .mc
            .clone()
            .list_tuned_models(request)
            .await
            .map_err(status_into_error)?
            .into_inner();
        Ok((response.tuned_models, response.next_page_token))
    }
}