use crate::{UserImageInput, tool::proposal::ActionExecutionEvidence};
use merry_core::{ErrorInfo, ToolCallResultStatus, ToolOutput};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ToolExecutionOutcome {
status: ToolCallResultStatus,
content: ToolResultContent,
diagnostic: Option<ErrorInfo>,
execution_evidence: Option<ActionExecutionEvidence>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ToolResultContent {
full: ToolOutput,
model: Option<Box<ToolOutput>>,
images: Vec<UserImageInput>,
}
impl From<ToolOutput> for ToolResultContent {
fn from(full: ToolOutput) -> Self {
Self {
full,
model: None,
images: Vec::new(),
}
}
}
impl ToolResultContent {
pub(crate) fn full(&self) -> &ToolOutput {
&self.full
}
pub(crate) fn model(&self) -> Option<&ToolOutput> {
self.model.as_deref()
}
pub(crate) fn images(&self) -> &[UserImageInput] {
&self.images
}
pub(crate) fn with_model(mut self, model: ToolOutput) -> Self {
self.model = Some(Box::new(model));
self
}
pub(crate) fn with_image(mut self, image: UserImageInput) -> Self {
self.images.push(image);
self
}
pub(crate) fn with_images<I>(mut self, images: I) -> Self
where
I: IntoIterator<Item = UserImageInput>,
{
self.images.extend(images);
self
}
pub(crate) fn into_parts(self) -> (ToolOutput, Option<ToolOutput>, Vec<UserImageInput>) {
(self.full, self.model.map(|content| *content), self.images)
}
}
impl ToolExecutionOutcome {
#[must_use]
pub fn succeeded_text(content: impl Into<String>) -> Self {
Self::succeeded(ToolOutput::text(content))
}
#[must_use]
pub fn succeeded_json(content: impl Into<String>) -> Self {
Self::succeeded(ToolOutput::json(content))
}
#[must_use]
pub fn failed_text(content: impl Into<String>, diagnostic: ErrorInfo) -> Self {
Self::failed(ToolOutput::text(content), diagnostic)
}
#[must_use]
pub fn failed_json(content: impl Into<String>, diagnostic: ErrorInfo) -> Self {
Self::failed(ToolOutput::json(content), diagnostic)
}
#[must_use]
pub fn status(&self) -> ToolCallResultStatus {
self.status
}
#[must_use]
pub fn content(&self) -> &ToolOutput {
self.content.full()
}
#[must_use]
pub fn model_content(&self) -> Option<&ToolOutput> {
self.content.model()
}
#[must_use]
pub fn with_model_text(mut self, content: impl Into<String>) -> Self {
self.content = self.content.with_model(ToolOutput::text(content));
self
}
#[must_use]
pub fn with_model_json(mut self, content: impl Into<String>) -> Self {
self.content = self.content.with_model(ToolOutput::json(content));
self
}
#[must_use]
pub fn with_image(mut self, image: UserImageInput) -> Self {
self.content = self.content.with_image(image);
self
}
#[must_use]
pub fn with_images<I>(mut self, images: I) -> Self
where
I: IntoIterator<Item = UserImageInput>,
{
self.content = self.content.with_images(images);
self
}
#[must_use]
pub fn images(&self) -> &[UserImageInput] {
self.content.images()
}
#[must_use]
pub fn diagnostic(&self) -> Option<&ErrorInfo> {
self.diagnostic.as_ref()
}
#[must_use]
pub fn execution_evidence(&self) -> Option<&ActionExecutionEvidence> {
self.execution_evidence.as_ref()
}
#[must_use]
pub fn with_execution_evidence(mut self, evidence: ActionExecutionEvidence) -> Self {
self.execution_evidence = Some(evidence);
self
}
pub(crate) fn into_parts(
self,
) -> (
ToolCallResultStatus,
ToolResultContent,
Option<ErrorInfo>,
Option<ActionExecutionEvidence>,
) {
(
self.status,
self.content,
self.diagnostic,
self.execution_evidence,
)
}
pub(super) fn succeeded(content: ToolOutput) -> Self {
Self {
status: ToolCallResultStatus::Succeeded,
content: content.into(),
diagnostic: None,
execution_evidence: None,
}
}
pub(super) fn failed(content: ToolOutput, diagnostic: ErrorInfo) -> Self {
Self {
status: ToolCallResultStatus::Failed,
content: content.into(),
diagnostic: Some(diagnostic),
execution_evidence: None,
}
}
}