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
//! LLM request access policy.
use HashSet;
use ;
/// Policy that decides whether a given LLM request may be sent.
///
/// Called by the `llm` module before dispatching to a
/// [`LlmProvider`](crate::llm::LlmProvider).
///
/// # Relationship to `HttpPolicy`
///
/// LLM requests are external HTTP calls, so they pass through **both**
/// [`HttpPolicy`](super::HttpPolicy) and `LlmPolicy`:
///
/// - [`HttpPolicy`](super::HttpPolicy) — network-level: "is this URL reachable?"
/// Checked first against the resolved base URL.
/// - `LlmPolicy` — AI-specific: "should data be sent to this provider?"
/// Addresses concerns that do not apply to general HTTP: data may be
/// used for model training, subject to provider-specific retention
/// policies, or expose sensitive context to a third-party AI system.
///
/// Both policies must allow the request for it to proceed.
///
/// # Built-in implementations
///
/// | Type | Behaviour |
/// |------|-----------|
/// | [`Unrestricted`] | No checks (default) |
/// | [`LlmAllowList`] | Allow only listed providers |
///
/// # Custom implementations
///
/// ```rust,no_run
/// use mlua_batteries::policy::{LlmPolicy, PolicyError};
///
/// struct OnlyLocal;
///
/// impl LlmPolicy for OnlyLocal {
/// fn check_request(&self, _provider: &str, _model: &str, base_url: &str) -> Result<(), PolicyError> {
/// if base_url.contains("localhost") || base_url.contains("127.0.0.1") {
/// Ok(())
/// } else {
/// Err(PolicyError::new(format!("LLM denied: only local endpoints allowed, got '{base_url}'")))
/// }
/// }
/// }
/// ```
/// Allow only requests to listed LLM providers.
///
/// ```rust,no_run
/// use mlua_batteries::policy::LlmAllowList;
///
/// let policy = LlmAllowList::new(["ollama", "openai"]);
/// ```