use super::raw::check_status;
use super::FilesClient;
#[cfg(not(target_family = "wasm"))]
use super::{AudioClient, ImagesClient};
#[cfg(not(target_family = "wasm"))]
use crate::builders::audio::SpeechBuilder;
use crate::builders::files::FilePurpose;
#[cfg(not(target_family = "wasm"))]
use crate::builders::Builder;
#[cfg(not(target_family = "wasm"))]
use crate::interceptor::{AfterResponseContext, BeforeRequestContext, ErrorContext};
#[cfg(not(target_family = "wasm"))]
use crate::semantic_conventions::operation_names;
#[cfg(not(target_family = "wasm"))]
use crate::time::Instant;
#[cfg(not(target_family = "wasm"))]
use crate::Error;
use crate::Result;
#[cfg(not(target_family = "wasm"))]
use base64::engine::general_purpose::STANDARD as BASE64;
#[cfg(not(target_family = "wasm"))]
use base64::Engine;
use http::Method;
use openai_client_base::models::OpenAiFile;
#[cfg(not(target_family = "wasm"))]
use openai_client_base::models::{Image, ImagesResponse};
#[cfg(not(target_family = "wasm"))]
use std::path::{Path, PathBuf};
#[cfg(not(target_family = "wasm"))]
use std::sync::atomic::{AtomicU64, Ordering};
use timvw_rust_runtime::multipart::{Form, Part};
use timvw_rust_runtime::Upload;
#[cfg(not(target_family = "wasm"))]
impl<T: Default + crate::MaybeSend + crate::MaybeSync> ImagesClient<'_, T> {
pub async fn save(&self, image: &Image, path: impl AsRef<Path>) -> Result<u64> {
let path = path.as_ref();
if let Some(encoded) = image.b64_json.as_deref() {
let bytes = BASE64
.decode(encoded.trim())
.map_err(|e| Error::InvalidRequest(format!("the image is not base64: {e}")))?;
write_atomically(path, &bytes).await?;
return Ok(bytes.len() as u64);
}
let Some(url) = image.url.as_deref() else {
return Err(Error::InvalidRequest(
"the image has neither `b64_json` nor `url`".to_string(),
));
};
let response = self.client.http.request(Method::GET, url).send().await?;
Ok(check_status(response).await?.save_to(path).await?)
}
pub async fn save_all(
&self,
response: &ImagesResponse,
directory: impl AsRef<Path>,
stem: &str,
) -> Result<Vec<PathBuf>> {
let extension = response
.output_format
.and_then(|format| serde_json::to_value(format).ok())
.and_then(|value| value.as_str().map(str::to_string))
.filter(|format| format != "unknown")
.unwrap_or_else(|| "png".to_string());
let mut paths = Vec::new();
for (index, image) in response.data.iter().flatten().enumerate() {
let path = directory
.as_ref()
.join(format!("{stem}-{index}.{extension}"));
self.save(image, &path).await?;
paths.push(path);
}
Ok(paths)
}
}
#[cfg(not(target_family = "wasm"))]
impl<T: Default + crate::MaybeSend + crate::MaybeSync> AudioClient<'_, T> {
pub async fn speech_to_file(
&self,
builder: SpeechBuilder,
path: impl AsRef<Path>,
) -> Result<u64> {
let request = builder.build()?;
let mut state = T::default();
let operation = operation_names::AUDIO_SPEECH;
let model = request.model.clone();
let request_json = serde_json::to_string(&request).unwrap_or_default();
let interceptors = &self.client.interceptors;
if !interceptors.is_empty() {
let mut ctx = BeforeRequestContext {
operation,
model: &model,
request_json: &request_json,
state: &mut state,
};
interceptors.before_request(&mut ctx).await?;
}
let start_time = Instant::now();
let written = async {
let response = self
.client
.raw_request(Method::POST, "/audio/speech")
.json(&request)
.send()
.await?;
Ok::<_, Error>(check_status(response).await?.save_to(path).await?)
}
.await;
let written = match written {
Ok(written) => written,
Err(error) => {
if !interceptors.is_empty() {
let ctx = ErrorContext {
operation,
model: Some(&model),
request_json: Some(&request_json),
error: &error,
state: Some(&state),
};
interceptors.on_error(&ctx).await;
}
return Err(error);
}
};
if !interceptors.is_empty() {
let response_json = format!("{{\"size\": {written}}}");
let ctx = AfterResponseContext {
operation,
model: &model,
request_json: &request_json,
response_json: &response_json,
duration: start_time.elapsed(),
input_tokens: None,
output_tokens: None,
state: &state,
};
if let Err(error) = interceptors.after_response(&ctx).await {
tracing::warn!("Interceptor after_response failed: {}", error);
}
}
Ok(written)
}
}
impl<T: Default + crate::MaybeSend + crate::MaybeSync> FilesClient<'_, T> {
pub async fn upload_file(
&self,
upload: impl Into<Upload>,
purpose: FilePurpose,
) -> Result<OpenAiFile> {
let upload = upload.into().resolve().await?;
let form = Form::new()
.text("purpose", purpose.to_string())
.part("file", Part::upload(upload));
let response = self
.client
.raw_request(Method::POST, "/files")
.multipart(form)
.send()
.await?;
Ok(check_status(response).await?.json().await?)
}
#[cfg(not(target_family = "wasm"))]
pub async fn download_to(&self, file_id: &str, path: impl AsRef<Path>) -> Result<u64> {
let response = self
.client
.raw_request(Method::GET, &format!("/files/{file_id}/content"))
.send()
.await?;
Ok(check_status(response).await?.save_to(path).await?)
}
}
#[cfg(not(target_family = "wasm"))]
async fn write_atomically(path: &Path, bytes: &[u8]) -> Result<()> {
static COUNTER: AtomicU64 = AtomicU64::new(0);
let name = path
.file_name()
.map_or_else(|| "image".into(), |name| name.to_string_lossy());
let unique = COUNTER.fetch_add(1, Ordering::Relaxed);
let temporary = path.with_file_name(format!(".{name}.{}-{unique}.part", std::process::id()));
if let Err(error) = tokio::fs::write(&temporary, bytes).await {
let _ = tokio::fs::remove_file(&temporary).await;
return Err(Error::File(error));
}
if let Err(error) = tokio::fs::rename(&temporary, path).await {
let _ = tokio::fs::remove_file(&temporary).await;
return Err(Error::File(error));
}
Ok(())
}