calmlens 0.22.0

Rust SDK for CalmLens moderation, storage and CDN delivery
Documentation
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
//! Moderation decisions for [`MockOptions::moderate`](super::MockOptions::moderate) and
//! [`MockServerClient::complete_processing`](super::MockServerClient::complete_processing).
use super::classification::{CATEGORIES, QUALIFIERS};
use crate::error::ErrorCode;
use crate::models::Asset;
use crate::{Error, Result};
use serde_json::{json, Map, Value};
use std::{fmt, str::FromStr};

/// Where the mock's moderation leaves an upload: still processing, failed (retryable), or
/// decided with that moderation status.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MockOutcome {
    /// Processing continues with moderation `pending`.
    Pending,
    /// Processing fails (retryable) with moderation `pending` and failure reason
    /// `processing-error`.
    Failed,
    /// Processing completes with moderation `allowed`.
    Allowed,
    /// Processing completes with moderation `limited`; the CDN serves the asset like an
    /// allowed one.
    Limited,
    /// Processing completes with moderation `age-restricted`; the CDN then serves the asset
    /// only with `ViewerClaims::age_confirmed` set to `true`, and a private one only with the
    /// download permission too.
    AgeRestricted,
    /// Processing completes with moderation `blocked`; the CDN refuses it.
    Blocked,
    /// Processing completes with moderation `purged` and the content is removed (reason `policy`).
    Purged,
}
const OUTCOMES: [MockOutcome; 7] = [
    MockOutcome::Pending,
    MockOutcome::Failed,
    MockOutcome::Allowed,
    MockOutcome::Limited,
    MockOutcome::AgeRestricted,
    MockOutcome::Blocked,
    MockOutcome::Purged,
];
impl MockOutcome {
    /// The status as the API writes it: `pending`, `failed`, `allowed`, `limited`,
    /// `age-restricted`, `blocked` or `purged`.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Pending => "pending",
            Self::Failed => "failed",
            Self::Allowed => "allowed",
            Self::Limited => "limited",
            Self::AgeRestricted => "age-restricted",
            Self::Blocked => "blocked",
            Self::Purged => "purged",
        }
    }
    /// Whether the outcome is a moderation decision (not `pending` or `failed`).
    pub fn is_decided(self) -> bool {
        !matches!(self, Self::Pending | Self::Failed)
    }
    /// The policy action that gives this status.
    fn action(self) -> &'static str {
        match self {
            Self::Limited => "limit",
            Self::AgeRestricted => "age-restrict",
            Self::Blocked => "block",
            Self::Purged => "purge",
            _ => "allow",
        }
    }
}
impl fmt::Display for MockOutcome {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}
impl FromStr for MockOutcome {
    type Err = Error;
    /// Parses a status; anything else is `sdk/invalid-argument`.
    fn from_str(value: &str) -> Result<Self> {
        OUTCOMES
            .into_iter()
            .find(|outcome| outcome.as_str() == value)
            .ok_or_else(|| {
                let known: Vec<&str> = OUTCOMES.iter().map(|outcome| outcome.as_str()).collect();
                invalid_argument(format!("status must be one of {}", known.join(", ")))
            })
    }
}

fn invalid_argument(message: impl Into<String>) -> Error {
    Error::new(ErrorCode::InvalidArgument, message)
}

/// The likelihood the API reports for a score.
fn likelihood(score: f64) -> &'static str {
    if score >= 0.8 {
        "very-likely"
    } else if score >= 0.6 {
        "likely"
    } else if score >= 0.4 {
        "possible"
    } else if score >= 0.2 {
        "unlikely"
    } else {
        "very-unlikely"
    }
}

fn is_score(score: f64) -> bool {
    score.is_finite() && (0.0..=1.0).contains(&score)
}

#[derive(Clone, Debug, PartialEq)]
struct Flag {
    category: &'static str,
    score: f64,
    qualifiers: Vec<(&'static str, f64)>,
}

/// A moderation decision: a status, the categories it flagged with their scores and qualifiers,
/// and the categories it could not assess. Each call checks its names and scores and fails with
/// `sdk/invalid-argument` for one CalmLens doesn't have.
///
/// A decided status fills the asset's report as the API would: every category (those not
/// flagged evaluated with score 0), the policy's actions and
/// `flagged_categories`. The policy acts on a category at 0.5 and above, so a flagged category
/// at 0.5 or more is listed there unless the status is `allowed`.
///
/// ```
/// # fn run() -> calmlens::Result<()> {
/// use calmlens::testing::{MockDecision, MockOutcome};
/// let decision = MockDecision::new(MockOutcome::Blocked)
///     .flag("violence", 0.92, &[("violence-threat", 0.9)])?
///     .not_evaluated("sexual")?;
/// assert_eq!(decision.status(), MockOutcome::Blocked);
/// # Ok(()) }
/// ```
#[derive(Clone, Debug, PartialEq)]
pub struct MockDecision {
    status: MockOutcome,
    flags: Vec<Flag>,
    not_evaluated: Vec<&'static str>,
}
impl From<MockOutcome> for MockDecision {
    fn from(status: MockOutcome) -> Self {
        Self::new(status)
    }
}
impl MockDecision {
    /// A decision with `status` and no findings.
    pub fn new(status: MockOutcome) -> Self {
        Self {
            status,
            flags: Vec::new(),
            not_evaluated: Vec::new(),
        }
    }
    /// The decision's status.
    pub fn status(&self) -> MockOutcome {
        self.status
    }
    /// Records findings in `category` with its `score` (0 to 1) and, optionally, scores for its
    /// qualifiers, such as `&[("violence-threat", 0.9)]`.
    ///
    /// Fails with `sdk/invalid-argument` for an unknown category or qualifier, a score outside 0
    /// to 1, a category already in the decision, or a `pending` or `failed` decision.
    pub fn flag(mut self, category: &str, score: f64, qualifiers: &[(&str, f64)]) -> Result<Self> {
        let category = self.check_new(category)?;
        if !is_score(score) {
            return Err(invalid_argument(format!(
                "{category}'s score must be from 0 to 1"
            )));
        }
        let known = QUALIFIERS
            .iter()
            .find(|(name, _)| *name == category)
            .map_or(&[][..], |(_, qualifiers)| qualifiers);
        let mut checked: Vec<(&'static str, f64)> = Vec::with_capacity(qualifiers.len());
        for &(qualifier, qualifier_score) in qualifiers {
            let Some(&qualifier) = known.iter().find(|known| **known == qualifier) else {
                return Err(invalid_argument(format!(
                    "{qualifier} is not a qualifier of {category}"
                )));
            };
            if !is_score(qualifier_score) {
                return Err(invalid_argument(format!(
                    "{qualifier}'s score must be from 0 to 1"
                )));
            }
            if checked.iter().any(|(seen, _)| *seen == qualifier) {
                return Err(invalid_argument(format!("{qualifier} is listed twice")));
            }
            checked.push((qualifier, qualifier_score));
        }
        self.flags.push(Flag {
            category,
            score,
            qualifiers: checked,
        });
        Ok(self)
    }
    /// Marks `category` as not assessed (`not-evaluated` for reason `not-supported`).
    ///
    /// Fails with `sdk/invalid-argument` for an unknown category, a category already in the
    /// decision, or a `pending` or `failed` decision.
    pub fn not_evaluated(mut self, category: &str) -> Result<Self> {
        let category = self.check_new(category)?;
        self.not_evaluated.push(category);
        Ok(self)
    }
    /// The known category named `category`, which must not be in this decision yet.
    fn check_new(&self, category: &str) -> Result<&'static str> {
        let Some(&known) = CATEGORIES.iter().find(|known| **known == category) else {
            return Err(invalid_argument(format!(
                "{category} is not a CalmLens category"
            )));
        };
        if !self.status.is_decided() {
            return Err(invalid_argument(format!(
                "a {} decision has no findings",
                self.status
            )));
        }
        if self.not_evaluated.contains(&known) || self.flags.iter().any(|f| f.category == known) {
            return Err(invalid_argument(format!(
                "{known} is already in this decision"
            )));
        }
        Ok(known)
    }
    fn score_of(&self, category: &str) -> f64 {
        self.flags
            .iter()
            .find(|flag| flag.category == category)
            .map_or(0.0, |flag| flag.score)
    }
    /// The categories the policy acts on.
    pub(super) fn flagged_categories(&self) -> Vec<&'static str> {
        CATEGORIES
            .iter()
            .copied()
            .filter(|category| !self.not_evaluated.contains(category))
            .filter(|category| self.action(self.score_of(category)) != "allow")
            .collect()
    }
    fn action(&self, score: f64) -> &'static str {
        if self.status != MockOutcome::Allowed && score >= 0.5 {
            self.status.action()
        } else {
            "allow"
        }
    }
    /// The moderation fields a decided asset gets (`categories`, `score` and `policy`), or
    /// `None` for `pending` and `failed`.
    pub(super) fn report(&self) -> Option<Map<String, Value>> {
        if !self.status.is_decided() {
            return None;
        }
        let evaluated: Vec<(&str, f64)> = CATEGORIES
            .iter()
            .filter(|category| !self.not_evaluated.contains(category))
            .map(|category| (*category, self.score_of(category)))
            .collect();
        let mut categories = Map::new();
        for category in CATEGORIES {
            let result = if self.not_evaluated.contains(category) {
                json!({"status": "not-evaluated", "reason": "not-supported"})
            } else {
                let score = self.score_of(category);
                let flag = self.flags.iter().find(|flag| flag.category == *category);
                let mut result = json!({
                    "status": "evaluated", "likelihood": likelihood(score), "score": score,
                });
                if let Some(flag) = flag.filter(|flag| !flag.qualifiers.is_empty()) {
                    result["qualifiers"] = flag
                        .qualifiers
                        .iter()
                        .map(|(qualifier, some_score)| {
                            let result = json!({
                                "status": "evaluated", "someScore": some_score,
                                "likelihood": likelihood(*some_score),
                            });
                            ((*qualifier).to_owned(), result)
                        })
                        .collect::<Map<String, Value>>()
                        .into();
                }
                result
            };
            categories.insert((*category).to_owned(), result);
        }
        let policy_categories: Map<String, Value> = evaluated
            .iter()
            .map(|(category, score)| {
                let action = self.action(*score);
                (
                    (*category).to_owned(),
                    json!({"score": score, "action": action}),
                )
            })
            .collect();
        let mut report = Map::new();
        report.insert(
            "raw".into(),
            json!({"categories": categories, "hashMatches": []}),
        );
        report.insert("categories".into(), policy_categories.into());
        report.insert("explanationComplete".into(), true.into());
        // The mock keeps no policies, so no policy is named.
        report.insert("policy".into(), Value::Null);
        report.insert("source".into(), "policy".into());
        report.insert("rule".into(), Value::Null);
        report.insert("automatedStatus".into(), self.status.as_str().into());
        report.insert("regions".into(), json!([]));
        report.insert("linkSafety".into(), Value::Null);
        // The mock analyses no audio or video, so a decision has no findings timeline.
        report.insert("timeline".into(), Value::Null);
        report.insert("flaggedCategories".into(), json!(self.flagged_categories()));
        Some(report)
    }
}

/// Why the mock is moderating an asset, for [`MockOptions::moderate`](super::MockOptions::moderate).
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum MockModerationTrigger {
    /// An upload completed: `assets().create`, `assets().classify`, `text().classify` or a final
    /// part.
    Upload,
    /// `assets().retry` queued a failed asset again.
    Retry,
    /// `assets().explain` with `rerun` queued the asset again.
    Rerun,
    /// [`complete_processing`](super::MockServerClient::complete_processing) finished processing
    /// without a decision of its own.
    Complete,
}
impl MockModerationTrigger {
    /// `upload`, `retry`, `rerun` or `complete`.
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Upload => "upload",
            Self::Retry => "retry",
            Self::Rerun => "rerun",
            Self::Complete => "complete",
        }
    }
}

/// An asset's content as the mock moderates it.
#[derive(Clone, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct MockModerationContent {
    /// The uploaded bytes, parts joined in order; `None` without stored content, such as a
    /// website or removed content.
    pub bytes: Option<Vec<u8>>,
    /// The bytes as UTF-8 text when the asset is a `plaintext-document`.
    pub text: Option<String>,
}

/// What [`MockOptions::moderate`](super::MockOptions::moderate) receives for each asset the
/// mock moderates.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct MockModerationInput {
    /// The asset as stored, still processing.
    pub asset: Asset,
    /// Its content.
    pub content: MockModerationContent,
    /// Why it is being moderated.
    pub trigger: MockModerationTrigger,
    /// `true` when the API would queue the work rather than answer with the decision: queued
    /// uploads, retries, reruns and `complete`.
    pub queued: bool,
}
impl MockModerationInput {
    /// The decision the mock makes without a `moderate` callback: queued work other than
    /// `complete` stays `pending`, and everything else is `allowed`.
    pub fn default_decision(&self) -> MockDecision {
        if self.queued && self.trigger != MockModerationTrigger::Complete {
            MockOutcome::Pending.into()
        } else {
            MockOutcome::Allowed.into()
        }
    }
}

/// What a `moderate` callback may return: a [`MockOutcome`], a [`MockDecision`], or a
/// `calmlens::Result` of either, so a callback can use `?` on the builder. An error fails the
/// request that triggered the moderation with that error.
pub trait IntoMockDecision {
    /// The decision.
    fn into_mock_decision(self) -> Result<MockDecision>;
}
impl IntoMockDecision for MockDecision {
    fn into_mock_decision(self) -> Result<MockDecision> {
        Ok(self)
    }
}
impl IntoMockDecision for MockOutcome {
    fn into_mock_decision(self) -> Result<MockDecision> {
        Ok(self.into())
    }
}
impl<T: IntoMockDecision> IntoMockDecision for Result<T> {
    fn into_mock_decision(self) -> Result<MockDecision> {
        self?.into_mock_decision()
    }
}