pub mod builder;
pub mod handle;
pub mod model;
pub mod stream;
pub use builder::InteractionBuilder;
pub use handle::InteractionHandle;
pub use model::*;
pub use stream::{InteractionEvent, InteractionStream, StepDeltaData};
impl Interaction {
pub fn output_text(&self) -> String {
self.steps
.iter()
.rev()
.find(|s| matches!(s, Step::ModelOutput { .. }))
.and_then(|s| {
if let Step::ModelOutput { content, .. } = s {
Some(
content
.iter()
.filter_map(|c| {
if let InteractionContent::Text { text, .. } = c {
Some(text.clone())
} else {
None
}
})
.collect::<Vec<_>>()
.join(""),
)
} else {
None
}
})
.unwrap_or_default()
}
pub fn function_calls(&self) -> Vec<&Step> {
self.steps
.iter()
.filter(|s| matches!(s, Step::FunctionCall { .. }))
.collect()
}
pub fn model_outputs(&self) -> Vec<&Step> {
self.steps
.iter()
.filter(|s| matches!(s, Step::ModelOutput { .. }))
.collect()
}
pub fn thoughts(&self) -> Vec<&Step> {
self.steps
.iter()
.filter(|s| matches!(s, Step::Thought { .. }))
.collect()
}
pub fn requires_action(&self) -> bool {
self.status == InteractionStatus::RequiresAction
}
pub fn is_completed(&self) -> bool {
self.status == InteractionStatus::Completed
}
pub fn output_image(&self) -> Option<&InteractionContent> {
self.steps.iter().rev().find_map(|s| {
if let Step::ModelOutput { content, .. } = s {
content
.iter()
.find(|c| matches!(c, InteractionContent::Image { .. }))
} else {
None
}
})
}
pub fn output_audio(&self) -> Option<&InteractionContent> {
self.steps.iter().rev().find_map(|s| {
if let Step::ModelOutput { content, .. } = s {
content
.iter()
.find(|c| matches!(c, InteractionContent::Audio { .. }))
} else {
None
}
})
}
pub fn output_video(&self) -> Option<&InteractionContent> {
self.steps.iter().rev().find_map(|s| {
if let Step::ModelOutput { content, .. } = s {
content
.iter()
.find(|c| matches!(c, InteractionContent::Video { .. }))
} else {
None
}
})
}
pub fn output_document(&self) -> Option<&InteractionContent> {
self.steps.iter().rev().find_map(|s| {
if let Step::ModelOutput { content, .. } = s {
content
.iter()
.find(|c| matches!(c, InteractionContent::Document { .. }))
} else {
None
}
})
}
pub fn citations(&self) -> Vec<&Annotation> {
self.steps
.iter()
.filter_map(|s| {
if let Step::ModelOutput { content, .. } = s {
Some(content.iter().flat_map(|c| {
if let InteractionContent::Text { annotations, .. } = c {
annotations.iter().collect::<Vec<_>>()
} else {
vec![]
}
}))
} else {
None
}
})
.flatten()
.collect()
}
pub fn total_tokens(&self) -> Option<i64> {
self.usage.as_ref()?.total_tokens
}
pub fn id(&self) -> Option<&str> {
self.id.as_deref()
}
}