claude_agent/client/adapter/
traits.rs

1//! Provider adapter trait definition.
2
3use std::fmt::Debug;
4
5use async_trait::async_trait;
6
7use super::config::{ModelType, ProviderConfig};
8use crate::client::messages::{CountTokensRequest, CountTokensResponse, CreateMessageRequest};
9use crate::types::ApiResponse;
10use crate::{Error, Result};
11
12#[async_trait]
13pub trait ProviderAdapter: Send + Sync + Debug {
14    fn config(&self) -> &ProviderConfig;
15
16    fn name(&self) -> &'static str;
17
18    fn model(&self, model_type: ModelType) -> &str {
19        self.config().models.get(model_type)
20    }
21
22    async fn build_url(&self, model: &str, stream: bool) -> String;
23
24    async fn prepare_request(&self, request: CreateMessageRequest) -> CreateMessageRequest {
25        request
26    }
27
28    async fn transform_request(&self, request: CreateMessageRequest) -> Result<serde_json::Value>;
29
30    fn transform_response(&self, response: serde_json::Value) -> Result<ApiResponse>;
31
32    async fn apply_auth_headers(&self, req: reqwest::RequestBuilder) -> reqwest::RequestBuilder {
33        req
34    }
35
36    async fn send(
37        &self,
38        http: &reqwest::Client,
39        request: CreateMessageRequest,
40    ) -> Result<ApiResponse>;
41
42    async fn send_stream(
43        &self,
44        http: &reqwest::Client,
45        request: CreateMessageRequest,
46    ) -> Result<reqwest::Response>;
47
48    fn supports_credential_refresh(&self) -> bool {
49        false
50    }
51
52    async fn ensure_fresh_credentials(&self) -> Result<()> {
53        Ok(())
54    }
55
56    async fn refresh_credentials(&self) -> Result<()> {
57        Ok(())
58    }
59
60    async fn count_tokens(
61        &self,
62        _http: &reqwest::Client,
63        _request: CountTokensRequest,
64    ) -> Result<CountTokensResponse> {
65        Err(Error::NotSupported {
66            provider: self.name(),
67            operation: "count_tokens",
68        })
69    }
70}