quorum-rs 0.7.0-rc.6

Rust SDK and CLI for multi-agent deliberation systems — ships the `quorum` binary (run / status / trace / tui / init) plus the underlying agent, LLM, tool, prompt, and worker library.
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
//! LLM-based content moderation middleware.
//!
//! Uses the existing `AiModel` trait and provider infrastructure to classify
//! content against configurable categories. Leverages structured tool calling
//! (same as agent react loop) so the LLM returns a typed `ModerationResponse`
//! rather than free-form text that needs fragile parsing.
//!
//! Most expensive middleware — should run last in the pipeline, only at release stage.
//!
//! # Provider reuse
//!
//! The middleware receives an `Arc<dyn AiModel>` built from the same provider
//! config pool as deliberation agents. This gives:
//! - All providers work (Ollama, Together AI, Anthropic, OpenAI, vLLM)
//! - Rate limiting via existing `qps`/`concurrency` config
//! - Retry logic from the provider infrastructure
//! - Response parsing handles all engine quirks

use crate::agents::config::AgentConfig;
use crate::llms::{AiModel, RequestConfig};
use crate::middleware::{
    AgentMiddleware, MiddlewareContext, MiddlewareStage, MiddlewareVerdict, Verdict,
};
use async_trait::async_trait;
use serde::Deserialize;
use std::sync::Arc;

// ---------------------------------------------------------------------------
// Config
// ---------------------------------------------------------------------------

/// LLM moderation middleware configuration from YAML.
#[derive(Debug, Deserialize)]
struct LlmModerationConfig {
    /// Categories to check (e.g., ["harassment", "hate_speech", "nsfw"]).
    #[serde(default = "default_categories")]
    categories: Vec<String>,
    /// What to do when LLM returns "warn": annotate (proceed) or block.
    #[serde(default)]
    on_warning: WarningAction,
    /// What to do when LLM returns an unrecognized verdict: block (default, fail-closed) or pass.
    #[serde(default)]
    on_unknown_verdict: UnknownVerdictAction,
    /// Maximum content length to send to the moderation LLM. Content is
    /// truncated to this length before sending. Default: 10000 chars.
    #[serde(default = "default_max_moderation_length")]
    max_moderation_length: usize,
    /// Number of retries when LLM returns an unrecognized/malformed verdict.
    /// Default: 0 (no retries — apply on_unknown_verdict immediately).
    #[serde(default)]
    max_retries: u32,
    /// Model name for moderation (e.g., "meta-llama/Llama-Guard-3-8B").
    /// Defaults to the configured model if not set.
    #[serde(default)]
    model_name: Option<String>,
}

fn default_categories() -> Vec<String> {
    vec![
        "harassment".to_string(),
        "hate_speech".to_string(),
        "nsfw".to_string(),
        "prompt_injection".to_string(),
    ]
}

fn default_max_moderation_length() -> usize {
    10000
}

#[derive(Debug, Clone, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
enum WarningAction {
    /// Proceed with annotation (default).
    #[default]
    Annotate,
    /// Treat warnings as blocks.
    Block,
}

#[derive(Debug, Clone, Deserialize, Default)]
#[serde(rename_all = "lowercase")]
enum UnknownVerdictAction {
    /// Block on unrecognized verdict (default, fail-closed).
    #[default]
    Block,
    /// Pass on unrecognized verdict (fail-open — use only if you trust the model).
    Pass,
}

// ---------------------------------------------------------------------------
// LLM Response (structured via tool calling)
// ---------------------------------------------------------------------------

/// Expected JSON structure from the moderation LLM (via tool call arguments).
#[derive(Debug, Deserialize)]
struct ModerationResponse {
    results: Vec<CategoryResult>,
}

#[derive(Debug, Deserialize)]
struct CategoryResult {
    category: String,
    verdict: String, // "pass", "warn", or "block"
    #[serde(default)]
    reason: Option<String>,
}

// ---------------------------------------------------------------------------
// Tool schema for structured output
// ---------------------------------------------------------------------------

/// Build the `submit_moderation` tool schema for structured LLM output.
fn moderation_tool_schema(categories: &[String]) -> async_openai::types::ChatCompletionTool {
    use async_openai::types::{ChatCompletionTool, ChatCompletionToolType, FunctionObject};

    let category_enum: Vec<serde_json::Value> =
        categories.iter().map(|c| serde_json::json!(c)).collect();

    ChatCompletionTool {
        r#type: ChatCompletionToolType::Function,
        function: FunctionObject {
            name: "submit_moderation".to_string(),
            description: Some(
                "Submit the content moderation results. For each category, provide a verdict (pass/warn/block) and reason."
                    .to_string(),
            ),
            parameters: Some(serde_json::json!({
                "type": "object",
                "properties": {
                    "results": {
                        "type": "array",
                        "items": {
                            "type": "object",
                            "properties": {
                                "category": {
                                    "type": "string",
                                    "enum": category_enum,
                                    "description": "The content category being evaluated"
                                },
                                "verdict": {
                                    "type": "string",
                                    "enum": ["pass", "warn", "block"],
                                    "description": "pass = no violation, warn = borderline/flagged, block = clear violation"
                                },
                                "reason": {
                                    "type": "string",
                                    "description": "Brief explanation for the verdict"
                                }
                            },
                            "required": ["category", "verdict", "reason"],
                            "additionalProperties": false
                        },
                        "description": "One result per category"
                    }
                },
                "required": ["results"],
                "additionalProperties": false
            })),
            strict: Some(true),
        },
    }
}

// ---------------------------------------------------------------------------
// LlmModerationMiddleware
// ---------------------------------------------------------------------------

/// LLM-based content moderation middleware.
///
/// Uses the existing `AiModel` + tool calling infrastructure to classify
/// content. The LLM receives a `submit_moderation` tool with the category
/// schema and returns structured per-category verdicts.
#[derive(Debug)]
pub struct LlmModerationMiddleware {
    categories: Vec<String>,
    on_warning: WarningAction,
    on_unknown_verdict: UnknownVerdictAction,
    max_moderation_length: usize,
    max_retries: u32,
    stages: Vec<MiddlewareStage>,
    /// The LLM model to use for moderation. None = pass-through mode.
    model: Option<Arc<dyn AiModel>>,
    /// Minimal AgentConfig for the moderation LLM call.
    agent_config: AgentConfig,
}

impl LlmModerationMiddleware {
    /// Create from YAML config JSON, active stages, and an optional LLM model.
    ///
    /// When `model` is None, the middleware operates in pass-through mode
    /// (useful for testing or when no moderation provider is configured).
    pub fn from_config(
        config: &serde_json::Value,
        stages: Vec<MiddlewareStage>,
        model: Option<Arc<dyn AiModel>>,
    ) -> Result<Self, String> {
        let cfg: LlmModerationConfig = if config.is_null() {
            LlmModerationConfig {
                categories: default_categories(),
                on_warning: WarningAction::default(),
                on_unknown_verdict: UnknownVerdictAction::default(),
                max_moderation_length: default_max_moderation_length(),
                max_retries: 0,
                model_name: None,
            }
        } else {
            serde_json::from_value(config.clone())
                .map_err(|e| format!("Invalid llm_moderation config: {e}"))?
        };

        if cfg.categories.is_empty() {
            return Err("llm_moderation requires at least one category".to_string());
        }

        // Build a minimal AgentConfig for the moderation call
        let agent_config = AgentConfig {
            name: "moderation".to_string(),
            provider_id: "moderation".to_string(),
            model_name: cfg.model_name.unwrap_or_else(|| "moderation".to_string()),
            temperature: 0.0, // Deterministic for safety classification
            max_tokens: 1000,
            json_mode: true,
            disable_native_tools: false,
            ..Default::default()
        };

        Ok(Self {
            categories: cfg.categories,
            on_warning: cfg.on_warning,
            on_unknown_verdict: cfg.on_unknown_verdict,
            max_moderation_length: cfg.max_moderation_length,
            max_retries: cfg.max_retries,
            stages,
            model,
            agent_config,
        })
    }

    /// Build the system prompt for moderation.
    fn system_prompt(&self) -> String {
        let categories = self.categories.join(", ");
        format!(
            "You are a content safety moderator. Evaluate the provided content against these categories: {categories}.\n\n\
             For each category, classify as:\n\
             - \"pass\" — content does not violate this category\n\
             - \"warn\" — borderline content, should be flagged for review\n\
             - \"block\" — clear violation, content must be rejected\n\n\
             Use the submit_moderation tool to return your results. Be concise in reasons."
        )
    }

    /// Aggregate per-category verdicts into a single pipeline verdict.
    /// Returns (verdict, had_unknown_verdicts) so caller can decide to retry.
    fn aggregate_response(&self, response: &ModerationResponse) -> (MiddlewareVerdict, bool) {
        let mut worst_verdict = Verdict::Pass;
        let mut worst_category = String::new();
        let mut worst_reason = String::new();

        let mut has_unknown = false;
        for result in &response.results {
            let verdict = match result.verdict.to_lowercase().as_str() {
                "block" => Verdict::Block,
                "warn" => Verdict::Warn,
                "pass" | "safe" | "ok" => Verdict::Pass,
                unknown => {
                    has_unknown = true;
                    tracing::warn!(verdict = %unknown, "Unrecognized moderation verdict");
                    match self.on_unknown_verdict {
                        UnknownVerdictAction::Block => Verdict::Block,
                        UnknownVerdictAction::Pass => Verdict::Pass,
                    }
                }
            };

            let is_worse = matches!(
                (&worst_verdict, &verdict),
                (Verdict::Pass, Verdict::Warn | Verdict::Block) | (Verdict::Warn, Verdict::Block)
            );

            if is_worse {
                worst_verdict = verdict;
                worst_category = result.category.clone();
                worst_reason = result
                    .reason
                    .clone()
                    .unwrap_or_else(|| format!("{} violation detected", result.category));
            }
        }

        let verdict = match worst_verdict {
            Verdict::Block => MiddlewareVerdict::block(worst_category, worst_reason),
            Verdict::Warn => match self.on_warning {
                WarningAction::Block => MiddlewareVerdict::block(worst_category, worst_reason),
                WarningAction::Annotate => MiddlewareVerdict::warn(worst_category, worst_reason),
            },
            Verdict::Pass => MiddlewareVerdict::pass(),
        };
        (verdict, has_unknown)
    }

    /// Parse a moderation response from text (fallback when tool calling isn't used).
    pub fn parse_response(&self, response: &str) -> MiddlewareVerdict {
        let json_str = extract_json(response);
        match serde_json::from_str::<ModerationResponse>(json_str) {
            Ok(m) => self.aggregate_response(&m).0,
            Err(e) => {
                tracing::warn!(
                    error = %e,
                    response = %response,
                    "LLM moderation response is not valid JSON — fail closed"
                );
                MiddlewareVerdict::block(
                    "moderation_error",
                    "Moderation LLM returned unparsable response — blocking for safety",
                )
            }
        }
    }
}

/// Extract JSON from potentially markdown-wrapped LLM output.
fn extract_json(s: &str) -> &str {
    if let Some(start) = s.find("```json") {
        let content = &s[start + 7..];
        if let Some(end) = content.find("```") {
            return content[..end].trim();
        }
    }
    if let Some(start) = s.find("```") {
        let content = &s[start + 3..];
        if let Some(end) = content.find("```") {
            return content[..end].trim();
        }
    }
    if let Some(start) = s.find('{') {
        if let Some(end) = s.rfind('}') {
            return &s[start..=end];
        }
    }
    s.trim()
}

#[async_trait]
impl AgentMiddleware for LlmModerationMiddleware {
    async fn execute(&self, ctx: &MiddlewareContext) -> MiddlewareVerdict {
        let content = if let Some(s) = ctx.content.as_str() {
            s.to_string()
        } else {
            ctx.content.to_string()
        };

        // Truncate for moderation (no need to scan full 200KB proposals).
        // Use char_indices to find a safe UTF-8 boundary.
        let truncated = if content.len() > self.max_moderation_length {
            let boundary = content
                .char_indices()
                .take_while(|(i, _)| *i < self.max_moderation_length)
                .last()
                .map(|(i, c)| i + c.len_utf8())
                .unwrap_or(0);
            &content[..boundary]
        } else {
            &content
        };

        // If we have a model, use the full AiModel + tool calling pipeline (with retries)
        if let Some(ref model) = self.model {
            for attempt in 0..=self.max_retries {
                let verdict = self.execute_with_model(model, truncated).await;
                // Only retry if the verdict was caused by an unknown/malformed LLM response
                // and we haven't exhausted retries
                if attempt < self.max_retries
                    && verdict.verdict == crate::middleware::Verdict::Block
                    && verdict.category.as_deref() == Some("moderation_error")
                {
                    tracing::info!(
                        attempt = attempt + 1,
                        max_retries = self.max_retries,
                        "Retrying moderation LLM call due to malformed response"
                    );
                    continue;
                }
                return verdict;
            }
            // Should not reach here, but fail closed
            return MiddlewareVerdict::block("moderation_error", "Max retries exceeded");
        }

        // Fallback: check hook_state for injected response (test harness)
        if let Some(response_val) = ctx.hook_state.get("moderation_response") {
            if let Some(response_str) = response_val.as_str() {
                return self.parse_response(response_str);
            }
        }

        // No model available — pass through with warning
        tracing::debug!(
            agent_id = %ctx.agent_id,
            categories = ?self.categories,
            "LLM moderation middleware: no model configured, passing through"
        );
        MiddlewareVerdict::pass()
    }

    fn stages(&self) -> Vec<MiddlewareStage> {
        self.stages.clone()
    }

    fn name(&self) -> &str {
        "llm_moderation"
    }
}

impl LlmModerationMiddleware {
    /// Execute moderation using the AiModel + structured tool calling.
    async fn execute_with_model(
        &self,
        model: &Arc<dyn AiModel>,
        content: &str,
    ) -> MiddlewareVerdict {
        use async_openai::types::{
            ChatCompletionRequestMessage, ChatCompletionRequestSystemMessageArgs,
            ChatCompletionRequestUserMessageArgs,
        };

        let system_msg = ChatCompletionRequestSystemMessageArgs::default()
            .content(self.system_prompt())
            .build()
            .map(ChatCompletionRequestMessage::System);

        let user_msg = ChatCompletionRequestUserMessageArgs::default()
            .content(format!("Moderate this content:\n\n{content}"))
            .build()
            .map(ChatCompletionRequestMessage::User);

        let (system_msg, user_msg) = match (system_msg, user_msg) {
            (Ok(s), Ok(u)) => (s, u),
            _ => {
                tracing::error!("Failed to build moderation messages");
                return MiddlewareVerdict::block(
                    "moderation_error",
                    "Failed to construct moderation request",
                );
            }
        };

        let tool = moderation_tool_schema(&self.categories);
        let request = RequestConfig {
            messages: vec![system_msg, user_msg],
            tools: Some(vec![tool]),
            tool_choice: None,
            presence_penalty: None,
        };

        match model.chat_completion(&self.agent_config, request).await {
            Ok(result) => {
                // Extract tool call arguments from the response
                if let Some(choice) = result.response.choices.first() {
                    // Check for tool calls (structured output path)
                    if let Some(ref tool_calls) = choice.message.tool_calls {
                        if let Some(tc) = tool_calls.first() {
                            match serde_json::from_str::<ModerationResponse>(&tc.function.arguments)
                            {
                                Ok(moderation) => return self.aggregate_response(&moderation).0,
                                Err(e) => {
                                    tracing::warn!(
                                        error = %e,
                                        args_len = tc.function.arguments.len(),
                                        "Failed to parse moderation tool call — fail closed"
                                    );
                                    return MiddlewareVerdict::block(
                                        "moderation_error",
                                        "Moderation tool call returned invalid arguments",
                                    );
                                }
                            }
                        }
                    }

                    // Fallback: parse content as text (some models don't use tool calling)
                    if let Some(ref content) = choice.message.content {
                        return self.parse_response(content);
                    }
                }

                tracing::warn!("Moderation LLM returned empty response — fail closed");
                MiddlewareVerdict::block(
                    "moderation_error",
                    "Moderation LLM returned empty response",
                )
            }
            Err(e) => {
                tracing::error!(error = %e, "Moderation LLM call failed — fail closed");
                MiddlewareVerdict::block(
                    "moderation_error",
                    format!("Moderation LLM call failed: {e}"),
                )
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    fn make_ctx(content: &str) -> MiddlewareContext {
        MiddlewareContext {
            content: serde_json::json!(content),
            action: "propose".to_string(),
            agent_id: "test-agent".to_string(),
            job_id: "test-job".to_string(),
            round: 1,
            stage: MiddlewareStage::Release,
            metadata: serde_json::json!(null),
            hook_state: HashMap::new(),
        }
    }

    fn default_mw() -> LlmModerationMiddleware {
        LlmModerationMiddleware::from_config(
            &serde_json::json!(null),
            vec![MiddlewareStage::Release],
            None, // no model — test/pass-through mode
        )
        .unwrap()
    }

    #[test]
    fn parse_all_pass() {
        let mw = default_mw();
        let response = r#"{"results": [
            {"category": "harassment", "verdict": "pass"},
            {"category": "nsfw", "verdict": "pass"}
        ]}"#;
        let verdict = mw.parse_response(response);
        assert_eq!(verdict.verdict, Verdict::Pass);
    }

    #[test]
    fn parse_block_verdict() {
        let mw = default_mw();
        let response = r#"{"results": [
            {"category": "harassment", "verdict": "block", "reason": "Contains targeted harassment"},
            {"category": "nsfw", "verdict": "pass"}
        ]}"#;
        let verdict = mw.parse_response(response);
        assert_eq!(verdict.verdict, Verdict::Block);
        assert_eq!(verdict.category.as_deref(), Some("harassment"));
        assert!(verdict.reason.as_deref().unwrap().contains("harassment"));
    }

    #[test]
    fn parse_warn_with_annotate() {
        let mw = LlmModerationMiddleware::from_config(
            &serde_json::json!({"on_warning": "annotate"}),
            vec![MiddlewareStage::Release],
            None,
        )
        .unwrap();
        let response = r#"{"results": [
            {"category": "nsfw", "verdict": "warn", "reason": "Borderline content"}
        ]}"#;
        let verdict = mw.parse_response(response);
        assert_eq!(verdict.verdict, Verdict::Warn);
    }

    #[test]
    fn parse_warn_with_block_action() {
        let mw = LlmModerationMiddleware::from_config(
            &serde_json::json!({"on_warning": "block"}),
            vec![MiddlewareStage::Release],
            None,
        )
        .unwrap();
        let response = r#"{"results": [
            {"category": "nsfw", "verdict": "warn", "reason": "Borderline"}
        ]}"#;
        let verdict = mw.parse_response(response);
        assert_eq!(verdict.verdict, Verdict::Block);
    }

    #[test]
    fn parse_invalid_json_blocks() {
        let mw = default_mw();
        let verdict = mw.parse_response("not json at all");
        assert_eq!(verdict.verdict, Verdict::Block);
        assert_eq!(verdict.category.as_deref(), Some("moderation_error"));
    }

    #[test]
    fn parse_markdown_wrapped_json() {
        let mw = default_mw();
        let response = r#"Here's the analysis:
```json
{"results": [{"category": "harassment", "verdict": "pass"}]}
```"#;
        let verdict = mw.parse_response(response);
        assert_eq!(verdict.verdict, Verdict::Pass);
    }

    #[test]
    fn block_beats_warn() {
        let mw = default_mw();
        let response = r#"{"results": [
            {"category": "nsfw", "verdict": "warn", "reason": "Mild"},
            {"category": "harassment", "verdict": "block", "reason": "Severe"}
        ]}"#;
        let verdict = mw.parse_response(response);
        assert_eq!(verdict.verdict, Verdict::Block);
        assert_eq!(verdict.category.as_deref(), Some("harassment"));
    }

    #[tokio::test]
    async fn execute_with_hook_state_response() {
        let mw = default_mw();
        let mut ctx = make_ctx("Some content to moderate");
        ctx.hook_state.insert(
            "moderation_response".to_string(),
            serde_json::json!(r#"{"results": [{"category": "harassment", "verdict": "block", "reason": "Test block"}]}"#),
        );
        let verdict = mw.execute(&ctx).await;
        assert_eq!(verdict.verdict, Verdict::Block);
    }

    #[tokio::test]
    async fn execute_without_model_passes_through() {
        let mw = default_mw();
        let ctx = make_ctx("Some content");
        let verdict = mw.execute(&ctx).await;
        // No model configured → pass through
        assert_eq!(verdict.verdict, Verdict::Pass);
    }

    #[test]
    fn empty_categories_rejected() {
        let result = LlmModerationMiddleware::from_config(
            &serde_json::json!({"categories": []}),
            vec![MiddlewareStage::Release],
            None,
        );
        assert!(result.is_err());
    }

    #[test]
    fn extract_json_from_markdown() {
        assert_eq!(extract_json("```json\n{\"a\":1}\n```"), "{\"a\":1}");
        assert_eq!(extract_json("```\n{\"a\":1}\n```"), "{\"a\":1}");
        assert_eq!(extract_json("text {\"a\":1} text"), "{\"a\":1}");
        assert_eq!(extract_json("{\"a\":1}"), "{\"a\":1}");
    }

    #[test]
    fn tool_schema_has_correct_categories() {
        let tool = moderation_tool_schema(&["harassment".to_string(), "nsfw".to_string()]);
        assert_eq!(tool.function.name, "submit_moderation");
        let params = tool.function.parameters.unwrap();
        let items = &params["properties"]["results"]["items"]["properties"]["category"]["enum"];
        assert_eq!(items, &serde_json::json!(["harassment", "nsfw"]));
    }

    #[test]
    fn config_with_model_name() {
        let mw = LlmModerationMiddleware::from_config(
            &serde_json::json!({
                "model_name": "meta-llama/Llama-Guard-3-8B",
                "categories": ["harassment"]
            }),
            vec![MiddlewareStage::Release],
            None,
        )
        .unwrap();
        assert_eq!(mw.agent_config.model_name, "meta-llama/Llama-Guard-3-8B");
    }
}