merry_runtime/tool/
outcome.rs1use crate::{UserImageInput, tool::proposal::ActionExecutionEvidence};
2use merry_core::{ErrorInfo, ToolCallResultStatus, ToolOutput};
3
4#[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#[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 #[must_use]
74 pub fn succeeded_text(content: impl Into<String>) -> Self {
75 Self::succeeded(ToolOutput::text(content))
76 }
77
78 #[must_use]
80 pub fn succeeded_json(content: impl Into<String>) -> Self {
81 Self::succeeded(ToolOutput::json(content))
82 }
83
84 #[must_use]
89 pub fn failed_text(content: impl Into<String>, diagnostic: ErrorInfo) -> Self {
90 Self::failed(ToolOutput::text(content), diagnostic)
91 }
92
93 #[must_use]
98 pub fn failed_json(content: impl Into<String>, diagnostic: ErrorInfo) -> Self {
99 Self::failed(ToolOutput::json(content), diagnostic)
100 }
101
102 #[must_use]
104 pub fn status(&self) -> ToolCallResultStatus {
105 self.status
106 }
107
108 #[must_use]
112 pub fn content(&self) -> &ToolOutput {
113 self.content.full()
114 }
115
116 #[must_use]
121 pub fn model_content(&self) -> Option<&ToolOutput> {
122 self.content.model()
123 }
124
125 #[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 #[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 #[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 #[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 #[must_use]
164 pub fn images(&self) -> &[UserImageInput] {
165 self.content.images()
166 }
167
168 #[must_use]
170 pub fn diagnostic(&self) -> Option<&ErrorInfo> {
171 self.diagnostic.as_ref()
172 }
173
174 #[must_use]
180 pub fn execution_evidence(&self) -> Option<&ActionExecutionEvidence> {
181 self.execution_evidence.as_ref()
182 }
183
184 #[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}