Skip to main content

starweaver_model/providers/client/
factory.rs

1use std::sync::Arc;
2
3use crate::{
4    ModelError,
5    presets::{
6        anthropic_http_config, gemini_http_config, openai_chat_http_config,
7        openai_responses_http_config,
8    },
9    profile::{ModelProfile, ProtocolFamily},
10    transport::{DynHttpClient, HttpModelConfig, ReqwestHttpClient},
11};
12
13use super::ProtocolModelClient;
14
15impl ProtocolModelClient {
16    /// Create an `OpenAI` Chat Completions client.
17    ///
18    /// # Errors
19    ///
20    /// Returns an error when the default HTTP client cannot be created.
21    pub fn openai_chat(
22        model_name: impl Into<String>,
23        token: impl Into<String>,
24    ) -> Result<Self, ModelError> {
25        let config = openai_chat_http_config(token);
26        Ok(Self::new(
27            "openai",
28            model_name,
29            ModelProfile::for_protocol(ProtocolFamily::OpenAiChatCompletions),
30            config,
31            Arc::new(ReqwestHttpClient::new()?),
32        ))
33    }
34
35    /// Create an `OpenAI` Responses client.
36    ///
37    /// # Errors
38    ///
39    /// Returns an error when the default HTTP client cannot be created.
40    pub fn openai_responses(
41        model_name: impl Into<String>,
42        token: impl Into<String>,
43    ) -> Result<Self, ModelError> {
44        let config = openai_responses_http_config(token);
45        Ok(Self::new(
46            "openai",
47            model_name,
48            ModelProfile::for_protocol(ProtocolFamily::OpenAiResponses),
49            config,
50            Arc::new(ReqwestHttpClient::new()?),
51        ))
52    }
53
54    /// Create an Anthropic Messages client.
55    ///
56    /// # Errors
57    ///
58    /// Returns an error when the default HTTP client cannot be created.
59    pub fn anthropic_messages(
60        model_name: impl Into<String>,
61        api_key: impl Into<String>,
62    ) -> Result<Self, ModelError> {
63        let config = anthropic_http_config(api_key);
64        Ok(Self::new(
65            "anthropic",
66            model_name,
67            ModelProfile::for_protocol(ProtocolFamily::AnthropicMessages),
68            config,
69            Arc::new(ReqwestHttpClient::new()?),
70        ))
71    }
72
73    /// Create a Gemini generateContent client.
74    ///
75    /// # Errors
76    ///
77    /// Returns an error when the default HTTP client cannot be created.
78    pub fn gemini_generate_content(
79        model_name: impl Into<String>,
80        api_key: impl Into<String>,
81    ) -> Result<Self, ModelError> {
82        let model_name = model_name.into();
83        let config = gemini_http_config(api_key, model_name.clone());
84        Ok(Self::new(
85            "gemini",
86            model_name,
87            ModelProfile::for_protocol(ProtocolFamily::GeminiGenerateContent),
88            config,
89            Arc::new(ReqwestHttpClient::new()?),
90        ))
91    }
92
93    /// Create a Bedrock Converse client for a gateway endpoint.
94    ///
95    /// Bedrock production calls usually require AWS `SigV4`. Gateways and signed clients can inject
96    /// the final endpoint and headers through `HttpModelConfig` and `ModelHttpClient`.
97    #[must_use]
98    pub fn bedrock_converse_gateway(
99        model_name: impl Into<String>,
100        http_config: HttpModelConfig,
101        http_client: DynHttpClient,
102    ) -> Self {
103        Self::new(
104            "bedrock",
105            model_name,
106            ModelProfile::for_protocol(ProtocolFamily::BedrockConverse),
107            http_config,
108            http_client,
109        )
110    }
111}