llm_tool/types/
sanitized.rs1use alloc::string::String;
45use core::ops::{Deref, DerefMut};
46
47use serde::ser::{Serialize, Serializer};
48
49use super::{Json, TOOL_OUTPUT_QUARANTINE_TAG, ToolOutput};
50
51#[derive(
60 Debug,
61 Clone,
62 Copy,
63 PartialEq,
64 Eq,
65 PartialOrd,
66 Ord,
67 Hash,
68 Default,
69 serde::Deserialize,
70 schemars::JsonSchema,
71)]
72#[serde(transparent)]
73pub struct Sanitized<T>(pub T);
74
75impl<T> Sanitized<T> {
76 #[must_use]
78 pub const fn new(value: T) -> Self {
79 Self(value)
80 }
81
82 #[must_use]
84 pub fn into_inner(self) -> T {
85 self.0
86 }
87
88 #[must_use]
90 pub const fn as_inner(&self) -> &T {
91 &self.0
92 }
93}
94
95impl<T> Deref for Sanitized<T> {
96 type Target = T;
97
98 fn deref(&self) -> &Self::Target {
99 &self.0
100 }
101}
102
103impl<T> DerefMut for Sanitized<T> {
104 fn deref_mut(&mut self) -> &mut Self::Target {
105 &mut self.0
106 }
107}
108
109impl<T> From<T> for Sanitized<T> {
110 fn from(value: T) -> Self {
111 Self(value)
112 }
113}
114
115impl From<Sanitized<Self>> for ToolOutput {
116 fn from(value: Sanitized<Self>) -> Self {
117 value.0.sanitized()
118 }
119}
120
121impl From<Sanitized<String>> for ToolOutput {
122 fn from(value: Sanitized<String>) -> Self {
123 Self::new(value.0).sanitized()
124 }
125}
126
127impl From<Sanitized<&str>> for ToolOutput {
128 fn from(value: Sanitized<&str>) -> Self {
129 Self::new(value.0).sanitized()
130 }
131}
132
133impl<T: Serialize> From<Sanitized<Json<T>>> for ToolOutput {
134 fn from(value: Sanitized<Json<T>>) -> Self {
135 Self::from(Json(Sanitized(value.0.0))).sanitized()
136 }
137}
138
139impl<T: Serialize> Serialize for Sanitized<T> {
140 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
141 md_tmpl_core::serialize_with_sanitization(
142 &self.0,
143 serializer,
144 md_tmpl_core::StringSanitizeMode::SanitizeUntrusted {
145 tag_spec: TOOL_OUTPUT_QUARANTINE_TAG,
146 },
147 )
148 }
149}
150
151#[derive(
161 Debug,
162 Clone,
163 Copy,
164 PartialEq,
165 Eq,
166 PartialOrd,
167 Ord,
168 Hash,
169 Default,
170 serde::Deserialize,
171 schemars::JsonSchema,
172)]
173#[serde(transparent)]
174pub struct Untrusted<T>(pub T);
175
176pub type Quarantined<T> = Untrusted<T>;
178
179impl<T> Untrusted<T> {
180 #[must_use]
182 pub const fn new(value: T) -> Self {
183 Self(value)
184 }
185
186 #[must_use]
188 pub fn into_inner(self) -> T {
189 self.0
190 }
191
192 #[must_use]
194 pub const fn as_inner(&self) -> &T {
195 &self.0
196 }
197}
198
199impl<T> Deref for Untrusted<T> {
200 type Target = T;
201
202 fn deref(&self) -> &Self::Target {
203 &self.0
204 }
205}
206
207impl<T> DerefMut for Untrusted<T> {
208 fn deref_mut(&mut self) -> &mut Self::Target {
209 &mut self.0
210 }
211}
212
213impl<T> From<T> for Untrusted<T> {
214 fn from(value: T) -> Self {
215 Self(value)
216 }
217}
218
219impl From<Untrusted<Self>> for ToolOutput {
220 fn from(value: Untrusted<Self>) -> Self {
221 value.0.quarantined()
222 }
223}
224
225impl From<Untrusted<String>> for ToolOutput {
226 fn from(value: Untrusted<String>) -> Self {
227 Self::new(value.0).quarantined()
228 }
229}
230
231impl From<Untrusted<&str>> for ToolOutput {
232 fn from(value: Untrusted<&str>) -> Self {
233 Self::new(value.0).quarantined()
234 }
235}
236
237impl<T: Serialize> From<Untrusted<Json<T>>> for ToolOutput {
238 fn from(value: Untrusted<Json<T>>) -> Self {
239 Self::from(value.0).quarantined()
240 }
241}
242
243impl<T: Serialize> Serialize for Untrusted<T> {
244 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
245 md_tmpl_core::serialize_with_sanitization(
246 &self.0,
247 serializer,
248 md_tmpl_core::StringSanitizeMode::Quarantine {
249 tag_spec: TOOL_OUTPUT_QUARANTINE_TAG,
250 },
251 )
252 }
253}