Skip to main content

systemprompt_ai/services/gateway/
spec.rs

1//! Declarative gateway-policy specification.
2//!
3//! Spec payload of `ai_gateway_policies` rows, shared with the YAML schema in
4//! `services/gateway/policies.yaml`. Carries quota windows and safety
5//! configuration.
6//!
7//! Model exposure lives on the profile's gateway catalog, not here — see
8//! `GatewayConfig::is_model_exposed`.
9//!
10//! Copyright (c) systemprompt.io — Business Source License 1.1.
11//! See <https://systemprompt.io> for licensing details.
12
13use serde::{Deserialize, Serialize};
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16#[serde(deny_unknown_fields)]
17pub struct QuotaWindow {
18    pub window_seconds: i32,
19    #[serde(default = "default_subject")]
20    pub subject: String,
21    pub max_requests: Option<i64>,
22    pub max_input_tokens: Option<i64>,
23    pub max_output_tokens: Option<i64>,
24    #[serde(default)]
25    pub max_cost_microdollars: Option<i64>,
26}
27
28impl Default for QuotaWindow {
29    fn default() -> Self {
30        Self {
31            window_seconds: 0,
32            subject: default_subject(),
33            max_requests: None,
34            max_input_tokens: None,
35            max_output_tokens: None,
36            max_cost_microdollars: None,
37        }
38    }
39}
40
41fn default_subject() -> String {
42    "user".to_owned()
43}
44
45pub const USER_QUOTA_SUBJECT: &str = "user";
46
47/// How far back into a conversation the request-phase scanners look.
48///
49/// A request carries the whole conversation, so scanning all of it re-reads
50/// every earlier turn on every turn: one finding would deny the rest of the
51/// conversation, and each turn would persist the same finding again.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
53#[serde(rename_all = "lowercase")]
54pub enum SafetyHistoryMode {
55    #[default]
56    Off,
57    Audit,
58    Block,
59}
60
61/// Phrase-list tuning for the builtin `heuristic` scanner.
62#[derive(Debug, Clone, Serialize, Deserialize, Default)]
63#[serde(deny_unknown_fields)]
64pub struct HeuristicConfig {
65    #[serde(default)]
66    pub phrases: Option<Vec<String>>,
67    #[serde(default)]
68    pub extra_phrases: Vec<String>,
69    #[serde(default)]
70    pub disable_builtin: bool,
71}
72
73#[derive(Debug, Clone, Serialize, Deserialize, Default)]
74#[serde(deny_unknown_fields)]
75pub struct SafetyConfig {
76    #[serde(default)]
77    pub scanners: Vec<String>,
78    #[serde(default)]
79    pub heuristic: HeuristicConfig,
80    #[serde(default)]
81    pub block_categories: Vec<String>,
82    #[serde(default)]
83    pub block_response_categories: Vec<String>,
84    #[serde(default)]
85    pub history: SafetyHistoryMode,
86}
87
88#[derive(Debug, Clone, Serialize, Deserialize, Default)]
89#[serde(deny_unknown_fields)]
90pub struct GatewayPolicySpec {
91    #[serde(default)]
92    pub quota_windows: Vec<QuotaWindow>,
93    #[serde(default)]
94    pub safety: SafetyConfig,
95}
96
97impl GatewayPolicySpec {
98    #[must_use]
99    pub fn permissive() -> Self {
100        Self::default()
101    }
102}