Skip to main content

venice_e2ee_proxy/
openai.rs

1//! OpenAI-compatible request and response formatting.
2//!
3//! Includes the typed model-list response used by `GET /v1/models` and the
4//! shared OpenAI-style error envelope used by fail-closed validation responses.
5
6use serde::{Deserialize, Serialize};
7
8pub mod chat;
9
10/// OpenAI-compatible model-list response envelope.
11#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
12pub struct ModelListResponse {
13    pub object: String,
14    pub data: Vec<ModelObject>,
15}
16
17impl ModelListResponse {
18    /// Builds a model-list response from already-normalized model objects.
19    pub fn new(data: Vec<ModelObject>) -> Self {
20        Self {
21            object: "list".to_owned(),
22            data,
23        }
24    }
25}
26
27/// OpenAI-compatible model object with Venice metadata preserved for clients
28/// that need it.
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub struct ModelObject {
31    pub id: String,
32    pub object: String,
33    pub created: i64,
34    pub owned_by: String,
35    pub name: String,
36    pub info: ModelInfo,
37    pub venice: VeniceModelMetadata,
38}
39
40impl ModelObject {
41    /// Builds an OpenAI-compatible model object with mirrored `id` and `name` fields.
42    pub fn new(
43        id: impl Into<String>,
44        created: i64,
45        owned_by: impl Into<String>,
46        capabilities: ModelCapabilities,
47        venice: VeniceModelMetadata,
48    ) -> Self {
49        let id = id.into();
50
51        Self {
52            name: id.clone(),
53            id,
54            object: "model".to_owned(),
55            created,
56            owned_by: owned_by.into(),
57            info: ModelInfo {
58                meta: ModelMeta { capabilities },
59            },
60            venice,
61        }
62    }
63}
64
65/// OpenAI-compatible model information wrapper containing model metadata.
66#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
67pub struct ModelInfo {
68    pub meta: ModelMeta,
69}
70
71/// OpenAI-compatible model metadata containing capability flags.
72#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
73pub struct ModelMeta {
74    pub capabilities: ModelCapabilities,
75}
76
77/// Capability flags exposed in the OpenAI-compatible model metadata.
78#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Serialize, Deserialize)]
79pub struct ModelCapabilities {
80    pub function_calling: bool,
81    pub builtin_tools: bool,
82    pub web_search: bool,
83    pub code_interpreter: bool,
84    pub vision: bool,
85    pub reasoning: bool,
86    pub reasoning_effort: bool,
87}
88
89/// Venice-specific model metadata preserved alongside the OpenAI-compatible model object.
90#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
91pub struct VeniceModelMetadata {
92    pub id: String,
93    #[serde(rename = "supportsE2EE")]
94    pub supports_e2ee: bool,
95    #[serde(rename = "supportsTeeAttestation")]
96    pub supports_tee_attestation: bool,
97    #[serde(rename = "supportsReasoning")]
98    pub supports_reasoning: bool,
99    #[serde(rename = "supportsReasoningEffort")]
100    pub supports_reasoning_effort: bool,
101}
102
103impl VeniceModelMetadata {
104    /// Builds Venice metadata from the upstream model id and supported feature flags.
105    pub fn new(
106        id: impl Into<String>,
107        supports_e2ee: bool,
108        supports_tee_attestation: bool,
109        supports_reasoning: bool,
110        supports_reasoning_effort: bool,
111    ) -> Self {
112        Self {
113            id: id.into(),
114            supports_e2ee,
115            supports_tee_attestation,
116            supports_reasoning,
117            supports_reasoning_effort,
118        }
119    }
120}
121
122/// OpenAI-compatible error response envelope.
123#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
124pub struct ErrorResponse {
125    pub error: ErrorObject,
126}
127
128impl ErrorResponse {
129    /// Builds an OpenAI-compatible error response from message, type, and code strings.
130    pub fn new(
131        message: impl Into<String>,
132        error_type: impl Into<String>,
133        code: impl Into<String>,
134    ) -> Self {
135        Self {
136            error: ErrorObject {
137                message: message.into(),
138                kind: error_type.into(),
139                code: code.into(),
140            },
141        }
142    }
143}
144
145/// OpenAI-compatible error object containing the client-facing message, type, and code.
146#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
147pub struct ErrorObject {
148    pub message: String,
149    #[serde(rename = "type")]
150    pub kind: String,
151    pub code: String,
152}