use std::collections::HashMap;
use std::path::PathBuf;
use serde::{Deserialize, Serialize};
use serde_json::Value;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DocumentChunk {
pub path: Option<PathBuf>,
pub content: String,
pub byte_range: Option<(usize, usize)>,
#[serde(flatten)]
pub metadata: HashMap<String, Value>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum ImageFormat {
PNG,
JPEG,
GIF,
WebP,
BMP,
TIFF,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImageChunk {
pub data: Vec<u8>,
pub format: ImageFormat,
pub dimensions: Option<(u32, u32)>,
#[serde(flatten)]
pub metadata: HashMap<String, Value>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum AudioFormat {
MP3,
WAV,
FLAC,
OGG,
M4A,
OPUS,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VoiceChunk {
pub audio_data: Vec<u8>,
pub format: AudioFormat,
pub duration_ms: Option<u64>,
pub sample_rate: Option<u32>,
#[serde(flatten)]
pub metadata: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatMessageChunk {
pub content: String,
pub role: crate::domain::message::MessageRole,
pub is_final: bool,
#[serde(flatten)]
pub metadata: HashMap<String, Value>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum FinishReason {
Stop,
Length,
ContentFilter,
ToolCalls,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Usage {
pub prompt_tokens: u32,
pub completion_tokens: u32,
pub total_tokens: u32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CompletionChunk {
pub text: String,
pub finish_reason: Option<FinishReason>,
pub usage: Option<Usage>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EmbeddingChunk {
pub embeddings: Vec<f32>,
pub index: usize,
#[serde(flatten)]
pub metadata: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TranscriptionChunk {
pub text: String,
pub confidence: Option<f32>,
pub start_time_ms: Option<u64>,
pub end_time_ms: Option<u64>,
pub is_final: bool,
#[serde(flatten)]
pub metadata: HashMap<String, Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpeechChunk {
pub audio_data: Vec<u8>,
pub format: AudioFormat,
pub duration_ms: Option<u64>,
pub sample_rate: Option<u32>,
pub is_final: bool,
#[serde(flatten)]
pub metadata: HashMap<String, Value>,
}
impl DocumentChunk {
pub fn new(content: impl Into<String>) -> Self {
Self {
path: None,
content: content.into(),
byte_range: None,
metadata: HashMap::new(),
}
}
pub fn with_path(mut self, path: PathBuf) -> Self {
self.path = Some(path);
self
}
pub fn with_range(mut self, start: usize, end: usize) -> Self {
self.byte_range = Some((start, end));
self
}
pub fn with_metadata(mut self, key: impl Into<String>, value: Value) -> Self {
self.metadata.insert(key.into(), value);
self
}
}
impl ChatMessageChunk {
pub fn new(content: impl Into<String>, role: crate::domain::message::MessageRole) -> Self {
Self {
content: content.into(),
role,
is_final: false,
metadata: HashMap::new(),
}
}
pub fn final_chunk(mut self) -> Self {
self.is_final = true;
self
}
}
impl CompletionChunk {
pub fn new(text: impl Into<String>) -> Self {
Self {
text: text.into(),
finish_reason: None,
usage: None,
}
}
pub fn finished(mut self, reason: FinishReason) -> Self {
self.finish_reason = Some(reason);
self
}
pub fn with_usage(mut self, usage: Usage) -> Self {
self.usage = Some(usage);
self
}
}