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 pub(crate) fn replace_output(&mut self, output: ToolOutput) {
109 self.content = ToolResultContent::from(output);
110 }
111
112 #[must_use]
116 pub fn content(&self) -> &ToolOutput {
117 self.content.full()
118 }
119
120 #[must_use]
125 pub fn model_content(&self) -> Option<&ToolOutput> {
126 self.content.model()
127 }
128
129 #[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 #[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 #[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 #[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 #[must_use]
168 pub fn images(&self) -> &[UserImageInput] {
169 self.content.images()
170 }
171
172 #[must_use]
174 pub fn diagnostic(&self) -> Option<&ErrorInfo> {
175 self.diagnostic.as_ref()
176 }
177
178 #[must_use]
184 pub fn execution_evidence(&self) -> Option<&ActionExecutionEvidence> {
185 self.execution_evidence.as_ref()
186 }
187
188 #[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}