Skip to main content

objectiveai_api/ensemble/
client.rs

1//! Ensemble client for listing, retrieving, and fetching ensembles.
2
3use crate::ctx;
4use std::sync::Arc;
5
6/// Client for ensemble operations.
7///
8/// Combines a caching fetcher for ensemble definitions with a retrieval
9/// client for listing and usage statistics.
10pub struct Client<CTXEXT, FENS, RTRVL> {
11    /// Caching fetcher for ensemble definitions.
12    pub ensemble_fetcher: Arc<super::fetcher::CachingFetcher<CTXEXT, FENS>>,
13    /// Client for listing ensembles and getting usage.
14    pub retrieval_client: Arc<RTRVL>,
15    /// Phantom data for the context extension type.
16    pub _ctx_ext: std::marker::PhantomData<CTXEXT>,
17}
18
19impl<CTXEXT, FENS, RTRVL> Client<CTXEXT, FENS, RTRVL> {
20    /// Creates a new ensemble client.
21    pub fn new(
22        ensemble_fetcher: Arc<
23            super::fetcher::CachingFetcher<CTXEXT, FENS>,
24        >,
25        retrieval_client: Arc<RTRVL>,
26    ) -> Self {
27        Self {
28            ensemble_fetcher,
29            retrieval_client,
30            _ctx_ext: std::marker::PhantomData,
31        }
32    }
33}
34
35impl<CTXEXT, FENS, RTRVL> Client<CTXEXT, FENS, RTRVL>
36where
37    CTXEXT: Send + Sync + 'static,
38    FENS: super::fetcher::Fetcher<CTXEXT>
39        + Send
40        + Sync
41        + 'static,
42    RTRVL: super::retrieval_client::Client<CTXEXT> + Send + Sync + 'static,
43{
44    /// Lists all ensembles.
45    pub async fn list(
46        &self,
47        ctx: ctx::Context<CTXEXT>,
48    ) -> Result<
49        objectiveai::ensemble::response::ListEnsemble,
50        objectiveai::error::ResponseError,
51    > {
52        self.retrieval_client.list(ctx).await
53    }
54
55    /// Retrieves an ensemble by its ID.
56    ///
57    /// Returns a 404 error if the ensemble is not found.
58    pub async fn get(
59        &self,
60        ctx: ctx::Context<CTXEXT>,
61        id: &str,
62    ) -> Result<
63        objectiveai::ensemble::response::GetEnsemble,
64        objectiveai::error::ResponseError,
65    > {
66        self.ensemble_fetcher
67            .fetch(ctx, id)
68            .await?
69            .ok_or_else(|| objectiveai::error::ResponseError {
70                code: 404,
71                message: serde_json::json!({
72                    "kind": "ensemble",
73                    "error": "Ensemble not found"
74                }),
75            })
76            .map(|(inner, created)| {
77                objectiveai::ensemble::response::GetEnsemble { created, inner }
78            })
79    }
80
81    /// Retrieves usage statistics for an ensemble.
82    pub async fn get_usage(
83        &self,
84        ctx: ctx::Context<CTXEXT>,
85        id: &str,
86    ) -> Result<
87        objectiveai::ensemble::response::UsageEnsemble,
88        objectiveai::error::ResponseError,
89    > {
90        self.retrieval_client.get_usage(ctx, id).await
91    }
92}