1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// <auto-generated by typra-emitter>
// Code generated by Typra emitter; DO NOT EDIT.
// Prompty Context
#![allow(
unused_imports,
dead_code,
non_camel_case_types,
unused_variables,
unexpected_cfgs,
clippy::all
)]
/// Callback type for pre-processing input data before parsing.
pub type PreProcessFn = Box<dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync>;
/// Callback type for post-processing the result after instantiation.
pub type PostProcessFn = Box<dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync>;
/// Callback type for pre-processing an object before serialization.
pub type PreSaveFn = Box<dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync>;
/// Callback type for post-processing a dictionary after serialization.
pub type PostSaveFn = Box<dyn Fn(serde_json::Value) -> serde_json::Value + Send + Sync>;
/// Context for customizing the loading process of agent definitions.
///
/// Provides hooks for pre-processing input data before parsing and
/// post-processing output data after instantiation.
pub struct LoadContext {
/// Optional callback to transform input data before parsing.
pub pre_process: Option<PreProcessFn>,
/// Optional callback to transform the result after instantiation.
pub post_process: Option<PostProcessFn>,
}
impl std::fmt::Debug for LoadContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LoadContext")
.field("pre_process", &self.pre_process.as_ref().map(|_| "..."))
.field("post_process", &self.post_process.as_ref().map(|_| "..."))
.finish()
}
}
impl Default for LoadContext {
fn default() -> Self {
Self {
pre_process: None,
post_process: None,
}
}
}
impl LoadContext {
/// Create a new empty LoadContext.
pub fn new() -> Self {
Self::default()
}
/// Apply pre-processing to input data if a pre_process callback is set.
///
/// # Arguments
/// * `data` - The raw input value to process.
///
/// # Returns
/// The processed value, or the original if no callback is set.
pub fn process_input(&self, data: serde_json::Value) -> serde_json::Value {
if let Some(ref f) = self.pre_process {
f(data)
} else {
data
}
}
/// Apply post-processing to the result if a post_process callback is set.
///
/// # Arguments
/// * `result` - The instantiated value to process.
///
/// # Returns
/// The processed result, or the original if no callback is set.
pub fn process_output(&self, result: serde_json::Value) -> serde_json::Value {
if let Some(ref f) = self.post_process {
f(result)
} else {
result
}
}
}
/// Context for customizing the serialization process of agent definitions.
///
/// Provides hooks for pre-processing the object before serialization and
/// post-processing the dictionary after serialization.
pub struct SaveContext {
/// Optional callback to transform the object before serialization.
pub pre_save: Option<PreSaveFn>,
/// Optional callback to transform the dictionary after serialization.
pub post_save: Option<PostSaveFn>,
/// Output format for collections: "object" (name as key) or "array" (list of dicts).
/// Defaults to "object".
pub collection_format: String,
/// Use shorthand scalar representation when possible.
/// Defaults to true.
pub use_shorthand: bool,
}
impl std::fmt::Debug for SaveContext {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SaveContext")
.field("pre_save", &self.pre_save.as_ref().map(|_| "..."))
.field("post_save", &self.post_save.as_ref().map(|_| "..."))
.field("collection_format", &self.collection_format)
.field("use_shorthand", &self.use_shorthand)
.finish()
}
}
impl Default for SaveContext {
fn default() -> Self {
Self {
pre_save: None,
post_save: None,
collection_format: "object".to_string(),
use_shorthand: true,
}
}
}
impl SaveContext {
/// Create a new SaveContext with defaults.
pub fn new() -> Self {
Self::default()
}
/// Apply pre-processing to the object if a pre_save callback is set.
///
/// # Arguments
/// * `obj` - The value to process before serialization.
///
/// # Returns
/// The processed value, or the original if no callback is set.
pub fn process_object(&self, obj: serde_json::Value) -> serde_json::Value {
if let Some(ref f) = self.pre_save {
f(obj)
} else {
obj
}
}
/// Apply post-processing to the dictionary if a post_save callback is set.
///
/// # Arguments
/// * `data` - The serialized value to process.
///
/// # Returns
/// The processed value, or the original if no callback is set.
pub fn process_dict(&self, data: serde_json::Value) -> serde_json::Value {
if let Some(ref f) = self.post_save {
f(data)
} else {
data
}
}
/// Convert a value to a YAML string.
pub fn to_yaml(&self, data: &serde_json::Value) -> Result<String, serde_yaml::Error> {
serde_yaml::to_string(data)
}
/// Convert a value to a JSON string.
pub fn to_json(
&self,
data: &serde_json::Value,
indent: bool,
) -> Result<String, serde_json::Error> {
if indent {
serde_json::to_string_pretty(data)
} else {
serde_json::to_string(data)
}
}
}