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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//! Typed decisions: questions declared as a struct, answers read back into it.
//!
//! [`DecisionAnswers`] turns a struct into the questions of a
//! [`decision`](crate::context::WorkflowContext::decision) step, one question
//! per field, and reads the provider's answers back into it.
//! [`DecisionChoice`] lists the options of a choice question from the unit
//! variants of an enum. Both are derived:
//!
//! ```no_run
//! use ironflow_engine::config::DecisionConfig;
//! use ironflow_engine::context::WorkflowContext;
//! use ironflow_engine::decision::{DecisionAnswers, DecisionChoice};
//! use ironflow_engine::error::EngineError;
//!
//! #[derive(Debug, DecisionChoice)]
//! enum Team {
//! #[choice(description = "Payments and invoices")]
//! Billing,
//! Technical,
//! Sales,
//! }
//!
//! #[derive(Debug, DecisionAnswers)]
//! struct Triage {
//! #[noul("Does this convey urgency?")]
//! is_urgent: f64,
//! #[choice("Which team should handle this?")]
//! team: Team,
//! #[score("How frustrated is the customer?", levels = ["Calm", "Frustrated", "Very angry"])]
//! mood: f64,
//! }
//!
//! # async fn example(ctx: &mut WorkflowContext, ticket: &str) -> Result<(), EngineError> {
//! let triage = ctx
//! .decision("triage", DecisionConfig::new(ticket).answers::<Triage>().escalate_below(0.7))
//! .await?;
//! match triage.team {
//! Team::Billing => { /* .. */ }
//! Team::Technical | Team::Sales => { /* .. */ }
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Field attributes of `#[derive(DecisionAnswers)]`
//!
//! Every field is one question, named after the field, with exactly one of:
//!
//! | Attribute | Field type | Answer |
//! |-----------|------------|--------|
//! | `#[noul("..")]`, optionally `if_true = ".."`, `if_false = ".."` | `f64` | Probability of "yes", in `[0, 1]` |
//! | `#[choice("..")]` | an enum deriving [`DecisionChoice`] | The option picked |
//! | `#[score("..", levels = ["..", ".."])]` | `f64` | Probability-weighted level index |
//!
//! The first argument is the instruction sent to the model. A field without a
//! question does not compile:
//!
//! ```compile_fail
//! use ironflow_engine::decision::DecisionAnswers;
//!
//! #[derive(DecisionAnswers)]
//! struct Triage {
//! is_urgent: f64,
//! }
//! ```
//!
//! nor does a score without levels:
//!
//! ```compile_fail
//! use ironflow_engine::decision::DecisionAnswers;
//!
//! #[derive(DecisionAnswers)]
//! struct Triage {
//! #[score("How frustrated?")]
//! mood: f64,
//! }
//! ```
//!
//! nor a field whose type does not fit its question:
//!
//! ```compile_fail,E0308
//! use ironflow_engine::decision::DecisionAnswers;
//!
//! #[derive(DecisionAnswers)]
//! struct Triage {
//! #[noul("Does this convey urgency?")]
//! is_urgent: String,
//! }
//! ```
//!
//! # Variant attributes of `#[derive(DecisionChoice)]`
//!
//! An option is labelled with its variant name in `snake_case` (`OnCall` is
//! `on_call`). `#[choice(rename = "..")]` overrides the label,
//! `#[choice(description = "..")]` tells the model what the option means. Doc
//! comments are never sent to the model. Options are unit variants:
//!
//! ```compile_fail
//! use ironflow_engine::decision::DecisionChoice;
//!
//! #[derive(DecisionChoice)]
//! enum Team {
//! Billing,
//! Other(String),
//! }
//! ```
use BTreeMap;
use ;
use DecisionError;
pub use ;
/// A struct whose fields are the questions of a decision step and whose
/// values are the answers.
///
/// Derive it, see the [module documentation](self).
///
/// # Examples
///
/// ```
/// use ironflow_engine::decision::DecisionAnswers;
///
/// #[derive(DecisionAnswers)]
/// struct Review {
/// #[noul("Is the change safe to ship?")]
/// safe: f64,
/// }
///
/// assert!(Review::questions().contains_key("safe"));
/// ```
/// An enum whose unit variants are the options of a choice question.
///
/// Derive it, see the [module documentation](self).
///
/// # Examples
///
/// ```
/// use ironflow_engine::decision::DecisionChoice;
///
/// #[derive(Debug, PartialEq, DecisionChoice)]
/// enum Severity {
/// Low,
/// #[choice(description = "Customers are affected")]
/// High,
/// }
///
/// assert_eq!(Severity::options(), vec![("low", None), ("high", Some("Customers are affected"))]);
/// assert_eq!(Severity::from_label("high"), Some(Severity::High));
/// assert_eq!(Severity::Low.label(), "low");
/// ```
/// Support code for the derives. Not a public API.