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    pub(crate) fn replace_output(&mut self, output: ToolOutput) {
109        self.content = ToolResultContent::from(output);
110    }
111
112    /// Borrows the exact full execution output.
113    ///
114    /// Runtime records this output before emitting the resolution event.
115    #[must_use]
116    pub fn content(&self) -> &ToolOutput {
117        self.content.full()
118    }
119
120    /// Borrows the optional model-facing body instead of the full output.
121    ///
122    /// Runtime persists this body for request replay and compaction. Full tool
123    /// output reads and presentation retain [`Self::content`].
124    #[must_use]
125    pub fn model_content(&self) -> Option<&ToolOutput> {
126        self.content.model()
127    }
128
129    /// Supplies model-facing text without changing the full result output.
130    ///
131    /// Keep execution status, useful output, and completeness warnings in this
132    /// body. Runtime rejects blank bodies before resolving the tool call.
133    #[must_use]
134    pub fn with_model_text(mut self, content: impl Into<String>) -> Self {
135        self.content = self.content.with_model(ToolOutput::text(content));
136        self
137    }
138
139    /// Supplies model-facing JSON without changing the full result output.
140    ///
141    /// The caller owns the JSON schema and must preserve actionable results.
142    /// Runtime rejects blank bodies before resolving the tool call.
143    #[must_use]
144    pub fn with_model_json(mut self, content: impl Into<String>) -> Self {
145        self.content = self.content.with_model(ToolOutput::json(content));
146        self
147    }
148
149    /// Attaches one normalized PNG image observed alongside this tool outcome.
150    #[must_use]
151    pub fn with_image(mut self, image: UserImageInput) -> Self {
152        self.content = self.content.with_image(image);
153        self
154    }
155
156    /// Attaches normalized PNG images observed alongside this tool outcome.
157    #[must_use]
158    pub fn with_images<I>(mut self, images: I) -> Self
159    where
160        I: IntoIterator<Item = UserImageInput>,
161    {
162        self.content = self.content.with_images(images);
163        self
164    }
165
166    /// Borrows normalized PNG images attached to this tool outcome.
167    #[must_use]
168    pub fn images(&self) -> &[UserImageInput] {
169        self.content.images()
170    }
171
172    /// Borrows the optional failure diagnostic.
173    #[must_use]
174    pub fn diagnostic(&self) -> Option<&ErrorInfo> {
175        self.diagnostic.as_ref()
176    }
177
178    /// Borrows provider-invisible evidence produced by the actual execution.
179    ///
180    /// Runtime validates this internally against the approved proposal. It must
181    /// not be rendered into tool results, provider continuations, or provider
182    /// request payloads.
183    #[must_use]
184    pub fn execution_evidence(&self) -> Option<&ActionExecutionEvidence> {
185        self.execution_evidence.as_ref()
186    }
187
188    /// Attaches provider-invisible evidence from the actual execution.
189    #[must_use]
190    pub fn with_execution_evidence(mut self, evidence: ActionExecutionEvidence) -> Self {
191        self.execution_evidence = Some(evidence);
192        self
193    }
194
195    pub(crate) fn into_parts(
196        self,
197    ) -> (
198        ToolCallResultStatus,
199        ToolResultContent,
200        Option<ErrorInfo>,
201        Option<ActionExecutionEvidence>,
202    ) {
203        (
204            self.status,
205            self.content,
206            self.diagnostic,
207            self.execution_evidence,
208        )
209    }
210
211    pub(super) fn succeeded(content: ToolOutput) -> Self {
212        Self {
213            status: ToolCallResultStatus::Succeeded,
214            content: content.into(),
215            diagnostic: None,
216            execution_evidence: None,
217        }
218    }
219
220    pub(super) fn failed(content: ToolOutput, diagnostic: ErrorInfo) -> Self {
221        Self {
222            status: ToolCallResultStatus::Failed,
223            content: content.into(),
224            diagnostic: Some(diagnostic),
225            execution_evidence: None,
226        }
227    }
228}