#![allow(dead_code)]
use std::ffi::c_char;
use blazen_uniffi::batch::{BatchItem as InnerBatchItem, BatchResult as InnerBatchResult};
use crate::llm_records::{BlazenCompletionResponse, BlazenTokenUsage};
use crate::string::alloc_cstring;
pub const BLAZEN_BATCH_ITEM_SUCCESS: u32 = 0;
pub const BLAZEN_BATCH_ITEM_FAILURE: u32 = 1;
pub struct BlazenBatchItem(pub(crate) InnerBatchItem);
impl BlazenBatchItem {
pub(crate) fn into_ptr(self) -> *mut BlazenBatchItem {
Box::into_raw(Box::new(self))
}
}
impl From<InnerBatchItem> for BlazenBatchItem {
fn from(inner: InnerBatchItem) -> Self {
Self(inner)
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_item_kind(handle: *const BlazenBatchItem) -> u32 {
if handle.is_null() {
return BLAZEN_BATCH_ITEM_FAILURE;
}
let item = unsafe { &*handle };
match &item.0 {
InnerBatchItem::Success { .. } => BLAZEN_BATCH_ITEM_SUCCESS,
InnerBatchItem::Failure { .. } => BLAZEN_BATCH_ITEM_FAILURE,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_item_success_response(
handle: *const BlazenBatchItem,
) -> *mut BlazenCompletionResponse {
if handle.is_null() {
return std::ptr::null_mut();
}
let item = unsafe { &*handle };
match &item.0 {
InnerBatchItem::Success { response } => {
BlazenCompletionResponse(response.clone()).into_ptr()
}
InnerBatchItem::Failure { .. } => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_item_failure_message(
handle: *const BlazenBatchItem,
) -> *mut c_char {
if handle.is_null() {
return std::ptr::null_mut();
}
let item = unsafe { &*handle };
match &item.0 {
InnerBatchItem::Failure { error_message } => alloc_cstring(error_message),
InnerBatchItem::Success { .. } => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_item_free(handle: *mut BlazenBatchItem) {
if handle.is_null() {
return;
}
drop(unsafe { Box::from_raw(handle) });
}
pub struct BlazenBatchResult(pub(crate) InnerBatchResult);
impl BlazenBatchResult {
pub(crate) fn into_ptr(self) -> *mut BlazenBatchResult {
Box::into_raw(Box::new(self))
}
}
impl From<InnerBatchResult> for BlazenBatchResult {
fn from(inner: InnerBatchResult) -> Self {
Self(inner)
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_result_responses_count(
handle: *const BlazenBatchResult,
) -> usize {
if handle.is_null() {
return 0;
}
let r = unsafe { &*handle };
r.0.responses.len()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_result_responses_get(
handle: *const BlazenBatchResult,
idx: usize,
) -> *mut BlazenBatchItem {
if handle.is_null() {
return std::ptr::null_mut();
}
let r = unsafe { &*handle };
match r.0.responses.get(idx) {
Some(item) => BlazenBatchItem(item.clone()).into_ptr(),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_result_total_usage(
handle: *const BlazenBatchResult,
) -> *mut BlazenTokenUsage {
if handle.is_null() {
return std::ptr::null_mut();
}
let r = unsafe { &*handle };
BlazenTokenUsage(r.0.total_usage.clone()).into_ptr()
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_result_total_cost_usd(
handle: *const BlazenBatchResult,
) -> f64 {
if handle.is_null() {
return 0.0;
}
let r = unsafe { &*handle };
r.0.total_cost_usd
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn blazen_batch_result_free(handle: *mut BlazenBatchResult) {
if handle.is_null() {
return;
}
drop(unsafe { Box::from_raw(handle) });
}