x-ai 0.1.0

✨ A cli, tui, and sdk for interacting with the 𝕏-AI API
Documentation
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Reference: [docs/api-reference#get-model](https://docs.x.ai/docs/api-reference#get-model)

use crate::error::XaiError;
use crate::error::check_for_model_error;
use crate::traits::{ClientConfig, ModelInfoFetcher};
use reqwest::Method;
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModelInfoResponse {
    pub created: u64,
    pub id: String,
    pub object: String,
    pub owned_by: String,
}

#[derive(Debug, Clone)]
pub struct ModelRequestBuilder<T: ClientConfig + Clone + Send + Sync> {
    client: T,
    model_id: String,
}

impl<T> ModelRequestBuilder<T>
where
    T: ClientConfig + Clone + Send + Sync,
{
    pub fn new(client: T, model_id: String) -> Self {
        Self { client, model_id }
    }
}

impl<T> ModelInfoFetcher for ModelRequestBuilder<T>
where
    T: ClientConfig + Clone + Send + Sync,
{
    async fn fetch_model_info(&self) -> Result<ModelInfoResponse, XaiError> {
        let url = format!("models/{}", self.model_id);

        let response = self.client.request(Method::GET, &url)?.send().await?;

        if response.status().is_success() {
            let body = response.text().await?;
            let chat_completion = serde_json::from_str::<ModelInfoResponse>(&body)?;
            Ok(chat_completion)
        } else {
            let error_body = response.text().await.unwrap_or_else(|_| "".to_string());

            if let Some(model_error) = check_for_model_error(&error_body) {
                return Err(model_error);
            }

            Err(XaiError::Http(error_body))
        }
    }
}
// Copyright 2026 Mahmoud Harmouch.
//
// Licensed under the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.