Skip to main content

merry_runtime/tool/
outcome.rs

1use crate::{UserImageInput, tool::proposal::ActionExecutionEvidence};
2use merry_core::{ErrorInfo, ToolCallResultStatus, ToolOutput};
3
4/// Domain-level result from a tool execution.
5///
6/// Executors supply full output and may omit its presentation-only envelope
7/// from a separate model body. Runtime owns persistence, events, and ledger
8/// updates; an absent model body preserves the full result.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub struct ToolExecutionOutcome {
11    status: ToolCallResultStatus,
12    content: ToolResultContent,
13    diagnostic: Option<ErrorInfo>,
14    execution_evidence: Option<ActionExecutionEvidence>,
15}
16
17/// Full output, optional model body, and optional normalized PNG images, kept together through admission.
18#[derive(Debug, Clone, PartialEq, Eq)]
19pub(crate) struct ToolResultContent {
20    full: ToolOutput,
21    model: Option<Box<ToolOutput>>,
22    images: Vec<UserImageInput>,
23}
24
25impl From<ToolOutput> for ToolResultContent {
26    fn from(full: ToolOutput) -> Self {
27        Self {
28            full,
29            model: None,
30            images: Vec::new(),
31        }
32    }
33}
34
35impl ToolResultContent {
36    pub(crate) fn full(&self) -> &ToolOutput {
37        &self.full
38    }
39
40    pub(crate) fn model(&self) -> Option<&ToolOutput> {
41        self.model.as_deref()
42    }
43
44    pub(crate) fn images(&self) -> &[UserImageInput] {
45        &self.images
46    }
47
48    pub(crate) fn with_model(mut self, model: ToolOutput) -> Self {
49        self.model = Some(Box::new(model));
50        self
51    }
52
53    pub(crate) fn with_image(mut self, image: UserImageInput) -> Self {
54        self.images.push(image);
55        self
56    }
57
58    pub(crate) fn with_images<I>(mut self, images: I) -> Self
59    where
60        I: IntoIterator<Item = UserImageInput>,
61    {
62        self.images.extend(images);
63        self
64    }
65
66    pub(crate) fn into_parts(self) -> (ToolOutput, Option<ToolOutput>, Vec<UserImageInput>) {
67        (self.full, self.model.map(|content| *content), self.images)
68    }
69}
70
71impl ToolExecutionOutcome {
72    /// Creates a successful text result.
73    #[must_use]
74    pub fn succeeded_text(content: impl Into<String>) -> Self {
75        Self::succeeded(ToolOutput::text(content))
76    }
77
78    /// Creates a successful JSON result.
79    #[must_use]
80    pub fn succeeded_json(content: impl Into<String>) -> Self {
81        Self::succeeded(ToolOutput::json(content))
82    }
83
84    /// Creates a failed text result with a small diagnostic.
85    ///
86    /// Use this when the tool ran and produced a domain-level failure that
87    /// should resolve the pending call durably.
88    #[must_use]
89    pub fn failed_text(content: impl Into<String>, diagnostic: ErrorInfo) -> Self {
90        Self::failed(ToolOutput::text(content), diagnostic)
91    }
92
93    /// Creates a failed JSON result with a small diagnostic.
94    ///
95    /// Use this when the tool ran and produced a domain-level failure that
96    /// should resolve the pending call durably.
97    #[must_use]
98    pub fn failed_json(content: impl Into<String>, diagnostic: ErrorInfo) -> Self {
99        Self::failed(ToolOutput::json(content), diagnostic)
100    }
101
102    /// Returns the tool execution status.
103    #[must_use]
104    pub fn status(&self) -> ToolCallResultStatus {
105        self.status
106    }
107
108    /// Borrows the exact full execution output.
109    ///
110    /// Runtime records this output before emitting the resolution event.
111    #[must_use]
112    pub fn content(&self) -> &ToolOutput {
113        self.content.full()
114    }
115
116    /// Borrows the optional model-facing body instead of the full output.
117    ///
118    /// Runtime persists this body for request replay and compaction. Full tool
119    /// output reads and presentation retain [`Self::content`].
120    #[must_use]
121    pub fn model_content(&self) -> Option<&ToolOutput> {
122        self.content.model()
123    }
124
125    /// Supplies model-facing text without changing the full result output.
126    ///
127    /// Keep execution status, useful output, and completeness warnings in this
128    /// body. Runtime rejects blank bodies before resolving the tool call.
129    #[must_use]
130    pub fn with_model_text(mut self, content: impl Into<String>) -> Self {
131        self.content = self.content.with_model(ToolOutput::text(content));
132        self
133    }
134
135    /// Supplies model-facing JSON without changing the full result output.
136    ///
137    /// The caller owns the JSON schema and must preserve actionable results.
138    /// Runtime rejects blank bodies before resolving the tool call.
139    #[must_use]
140    pub fn with_model_json(mut self, content: impl Into<String>) -> Self {
141        self.content = self.content.with_model(ToolOutput::json(content));
142        self
143    }
144
145    /// Attaches one normalized PNG image observed alongside this tool outcome.
146    #[must_use]
147    pub fn with_image(mut self, image: UserImageInput) -> Self {
148        self.content = self.content.with_image(image);
149        self
150    }
151
152    /// Attaches normalized PNG images observed alongside this tool outcome.
153    #[must_use]
154    pub fn with_images<I>(mut self, images: I) -> Self
155    where
156        I: IntoIterator<Item = UserImageInput>,
157    {
158        self.content = self.content.with_images(images);
159        self
160    }
161
162    /// Borrows normalized PNG images attached to this tool outcome.
163    #[must_use]
164    pub fn images(&self) -> &[UserImageInput] {
165        self.content.images()
166    }
167
168    /// Borrows the optional failure diagnostic.
169    #[must_use]
170    pub fn diagnostic(&self) -> Option<&ErrorInfo> {
171        self.diagnostic.as_ref()
172    }
173
174    /// Borrows provider-invisible evidence produced by the actual execution.
175    ///
176    /// Runtime validates this internally against the approved proposal. It must
177    /// not be rendered into tool results, provider continuations, or provider
178    /// request payloads.
179    #[must_use]
180    pub fn execution_evidence(&self) -> Option<&ActionExecutionEvidence> {
181        self.execution_evidence.as_ref()
182    }
183
184    /// Attaches provider-invisible evidence from the actual execution.
185    #[must_use]
186    pub fn with_execution_evidence(mut self, evidence: ActionExecutionEvidence) -> Self {
187        self.execution_evidence = Some(evidence);
188        self
189    }
190
191    pub(crate) fn into_parts(
192        self,
193    ) -> (
194        ToolCallResultStatus,
195        ToolResultContent,
196        Option<ErrorInfo>,
197        Option<ActionExecutionEvidence>,
198    ) {
199        (
200            self.status,
201            self.content,
202            self.diagnostic,
203            self.execution_evidence,
204        )
205    }
206
207    pub(super) fn succeeded(content: ToolOutput) -> Self {
208        Self {
209            status: ToolCallResultStatus::Succeeded,
210            content: content.into(),
211            diagnostic: None,
212            execution_evidence: None,
213        }
214    }
215
216    pub(super) fn failed(content: ToolOutput, diagnostic: ErrorInfo) -> Self {
217        Self {
218            status: ToolCallResultStatus::Failed,
219            content: content.into(),
220            diagnostic: Some(diagnostic),
221            execution_evidence: None,
222        }
223    }
224}