use alloc::borrow::ToOwned;
use alloc::collections::BTreeMap;
use alloc::string::String;
use alloc::vec::Vec;
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct FunctionCall {
pub name: String,
pub args: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Candidate {
pub text: String,
pub function_calls: Vec<FunctionCall>,
pub finish_reason: Option<String>,
}
#[derive(Debug, Default)]
pub struct GeminiAccumulator {
candidates: BTreeMap<usize, Candidate>,
}
impl GeminiAccumulator {
#[must_use]
pub fn new() -> Self {
Self::default()
}
pub fn push_text(&mut self, index: usize, fragment: &str) {
self.candidate_mut(index).text.push_str(fragment);
}
pub fn push_function_call(&mut self, index: usize, name: &str, args: &str) {
self.candidate_mut(index).function_calls.push(FunctionCall {
name: name.to_owned(),
args: args.to_owned(),
});
}
pub fn set_finish_reason(&mut self, index: usize, reason: &str) {
self.candidate_mut(index).finish_reason = Some(reason.to_owned());
}
#[must_use]
pub fn candidate(&self, index: usize) -> Option<&Candidate> {
self.candidates.get(&index)
}
pub fn candidates(&self) -> impl Iterator<Item = (usize, &Candidate)> {
self.candidates.iter().map(|(&i, c)| (i, c))
}
fn candidate_mut(&mut self, index: usize) -> &mut Candidate {
self.candidates.entry(index).or_default()
}
}