Skip to main content

floopy/
options.rs

1//! Gateway behaviour toggles ([`FloopyOptions`]) and per-call overrides
2//! ([`RequestOptions`]). Field names map 1:1 to the other Floopy SDKs.
3
4use std::collections::HashMap;
5use std::time::Duration;
6
7use crate::constants::HEADER_FLOOPY_PROVIDER;
8
9/// Cache controls. Maps to the `Floopy-Cache-*` headers. A `None` field is
10/// omitted (the gateway default applies).
11#[derive(Debug, Clone, Default)]
12pub struct CacheOptions {
13    /// Toggle the exact + semantic cache for the request.
14    pub enabled: Option<bool>,
15    /// Maximum number of entries per semantic cache bucket.
16    pub bucket_max_size: Option<u32>,
17}
18
19/// Gateway behaviour toggles, mapped to `Floopy-*` headers and forwarded to
20/// **every** request (both OpenAI-compatible and Floopy-only). Empty / `None`
21/// fields are omitted.
22#[derive(Debug, Clone, Default)]
23pub struct FloopyOptions {
24    /// Cache controls. Maps to `Floopy-Cache-*` headers.
25    pub cache: Option<CacheOptions>,
26    /// Stored prompt id; the gateway resolves it to the active prompt
27    /// content. Maps to `Floopy-Prompt-Id`.
28    pub prompt_id: Option<String>,
29    /// Pinned prompt version. Use with `prompt_id`. Maps to
30    /// `Floopy-Prompt-Version`.
31    pub prompt_version: Option<String>,
32    /// Toggle the LLM firewall pre-check. Maps to
33    /// `floopy-llm-security-enabled`.
34    pub llm_security_enabled: Option<bool>,
35}
36
37/// Per-call overrides, merged on top of the client defaults. Construct with
38/// [`RequestOptions::new`] and the builder-style setters.
39#[derive(Debug, Clone, Default)]
40pub struct RequestOptions {
41    pub(crate) headers: HashMap<String, String>,
42    pub(crate) timeout: Option<Duration>,
43    pub(crate) options: Option<FloopyOptions>,
44}
45
46impl RequestOptions {
47    /// An empty set of per-call overrides.
48    #[must_use]
49    pub fn new() -> Self {
50        Self::default()
51    }
52
53    /// Add (or replace) a single per-call header. Highest precedence.
54    #[must_use]
55    pub fn header(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
56        self.headers.insert(key.into(), value.into());
57        self
58    }
59
60    /// Select the upstream the gateway forwards a Batch/Files request to
61    /// (the `floopy-provider` header). A batch carries no model up front so
62    /// the provider cannot be inferred — set this, or rely on the key's
63    /// single configured provider. No-op for other resources.
64    #[must_use]
65    pub fn provider(self, provider: impl Into<String>) -> Self {
66        self.header(HEADER_FLOOPY_PROVIDER, provider)
67    }
68
69    /// Override the client timeout for this call.
70    #[must_use]
71    pub fn timeout(mut self, timeout: Duration) -> Self {
72        self.timeout = Some(timeout);
73        self
74    }
75
76    /// Override the Floopy options for this call.
77    #[must_use]
78    pub fn options(mut self, options: FloopyOptions) -> Self {
79        self.options = Some(options);
80        self
81    }
82}