-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmodels.rs
More file actions
456 lines (405 loc) · 14 KB
/
models.rs
File metadata and controls
456 lines (405 loc) · 14 KB
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
use crate::consts::SYSTEM_PROMPT_MAX_LEN;
use crate::ApiError;
use serde::{Deserialize, Serialize};
use services::file::ports::FileData;
use services::UserId;
use utoipa::ToSchema;
/// User response DTO
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserResponse {
pub id: UserId,
pub email: String,
pub name: Option<String>,
pub avatar_url: Option<String>,
pub created_at: String,
pub updated_at: String,
}
/// Linked OAuth account response
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct LinkedAccountResponse {
pub provider: String,
pub linked_at: String,
}
/// User profile response with linked accounts
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserProfileResponse {
pub user: UserResponse,
pub linked_accounts: Vec<LinkedAccountResponse>,
}
/// Response for successful authentication
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct AuthResponse {
pub token: String,
pub expires_at: String,
}
impl From<services::user::ports::User> for UserResponse {
fn from(user: services::user::ports::User) -> Self {
Self {
id: user.id,
email: user.email,
name: user.name,
avatar_url: user.avatar_url,
created_at: user.created_at.to_rfc3339(),
updated_at: user.updated_at.to_rfc3339(),
}
}
}
impl From<services::user::ports::LinkedOAuthAccount> for LinkedAccountResponse {
fn from(account: services::user::ports::LinkedOAuthAccount) -> Self {
let provider = match account.provider {
services::user::ports::OAuthProvider::Google => "google",
services::user::ports::OAuthProvider::Github => "github",
services::user::ports::OAuthProvider::Near => "near",
};
Self {
provider: provider.to_string(),
linked_at: account.linked_at.to_rfc3339(),
}
}
}
impl From<services::user::ports::UserProfile> for UserProfileResponse {
fn from(profile: services::user::ports::UserProfile) -> Self {
Self {
user: profile.user.into(),
linked_accounts: profile
.linked_accounts
.into_iter()
.map(Into::into)
.collect(),
}
}
}
/// Cloud-API gateway attestation (forwarded from dependency)
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ApiGatewayAttestation {
/// Intel TDX quote in hex format
pub intel_quote: String,
/// Event log
#[serde(skip_serializing_if = "Option::is_none")]
pub event_log: Option<serde_json::Value>,
/// Request nonce
pub request_nonce: String,
/// Attestation info
#[serde(skip_serializing_if = "Option::is_none")]
pub info: Option<serde_json::Value>,
}
/// Model attestation from VLLM inference providers
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct ModelAttestation {
/// Cryptographic signing address (ECDSA or Ed25519)
pub signing_address: String,
/// Base64-encoded Intel TDX quote from model host
pub intel_quote: String,
/// JSON string containing NVIDIA GPU attestation
pub nvidia_payload: String,
/// TDX event log
#[serde(skip_serializing_if = "Option::is_none")]
pub event_log: Option<serde_json::Value>,
/// Additional TDX/tappd info
#[serde(skip_serializing_if = "Option::is_none")]
pub info: Option<serde_json::Value>,
}
/// Complete attestation report combining all layers
///
/// This report proves the entire trust chain:
/// 1. This chat-api service runs in a TEE (your_gateway_attestation)
/// 2. The cloud-api dependency runs in a TEE (cloud_api_gateway_attestation)
/// 3. The model inference providers run on trusted hardware (model_attestations)
#[derive(Debug, Clone, Serialize, Deserialize, ToSchema)]
pub struct CombinedAttestationReport {
/// This chat-api's own CPU attestation (proves this service runs in a TEE)
pub chat_api_gateway_attestation: ApiGatewayAttestation,
/// Cloud-API's gateway attestation (the intermediate service we depend on)
pub cloud_api_gateway_attestation: ApiGatewayAttestation,
/// Model provider attestations (can be multiple when routing to different models)
pub model_attestations: Option<Vec<ModelAttestation>>,
}
/// Attestation report structure from proxy_service
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AttestationReport {
pub gateway_attestation: ApiGatewayAttestation,
pub model_attestations: Option<Vec<ModelAttestation>>,
}
/// Appearance preference
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub enum Appearance {
Light,
Dark,
System,
}
impl From<services::user::ports::Appearance> for Appearance {
fn from(appearance: services::user::ports::Appearance) -> Self {
match appearance {
services::user::ports::Appearance::Light => Appearance::Light,
services::user::ports::Appearance::Dark => Appearance::Dark,
services::user::ports::Appearance::System => Appearance::System,
}
}
}
impl From<Appearance> for services::user::ports::Appearance {
fn from(appearance: Appearance) -> Self {
match appearance {
Appearance::Light => services::user::ports::Appearance::Light,
Appearance::Dark => services::user::ports::Appearance::Dark,
Appearance::System => services::user::ports::Appearance::System,
}
}
}
/// User settings content for API responses
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserSettingsContent {
/// Notification preference
pub notification: bool,
/// System prompt
#[serde(skip_serializing_if = "Option::is_none")]
pub system_prompt: Option<String>,
/// Web search preference
pub web_search: bool,
/// Appearance preference
pub appearance: Appearance,
}
impl From<services::user::ports::UserSettingsContent> for UserSettingsContent {
fn from(content: services::user::ports::UserSettingsContent) -> Self {
Self {
notification: content.notification,
system_prompt: content.system_prompt,
web_search: content.web_search,
appearance: content.appearance.into(),
}
}
}
/// User settings update request
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UpdateUserSettingsRequest {
/// Notification preference
pub notification: bool,
/// System prompt
pub system_prompt: Option<String>,
/// Web search preference
pub web_search: bool,
/// Appearance preference
pub appearance: Appearance,
}
impl UpdateUserSettingsRequest {
pub fn validate(&self) -> Result<(), ApiError> {
if let Some(ref system_prompt) = self.system_prompt {
if system_prompt.len() > SYSTEM_PROMPT_MAX_LEN {
return Err(ApiError::bad_request("System prompt exceeds max length"));
}
}
Ok(())
}
}
/// User settings update request (partial update)
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UpdateUserSettingsPartiallyRequest {
/// Notification preference
pub notification: Option<bool>,
/// System prompt
pub system_prompt: Option<String>,
/// Web search preference
pub web_search: Option<bool>,
/// Appearance preference
pub appearance: Option<Appearance>,
}
impl UpdateUserSettingsPartiallyRequest {
pub fn validate(&self) -> Result<(), ApiError> {
if let Some(ref system_prompt) = self.system_prompt {
if system_prompt.len() > SYSTEM_PROMPT_MAX_LEN {
return Err(ApiError::bad_request("System prompt exceeds max length"));
}
}
Ok(())
}
}
/// User settings response
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserSettingsResponse {
/// User ID
pub user_id: UserId,
/// Settings content (serialized as "settings")
#[serde(rename = "settings")]
pub content: UserSettingsContent,
}
/// Model settings content for API responses
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ModelSettings {
/// Whether models are public (visible/usable in responses)
pub public: bool,
/// Optional system-level system prompt for this model
#[serde(skip_serializing_if = "Option::is_none")]
pub system_prompt: Option<String>,
}
impl From<services::model::ports::ModelSettings> for ModelSettings {
fn from(content: services::model::ports::ModelSettings) -> Self {
Self {
public: content.public,
system_prompt: content.system_prompt,
}
}
}
impl From<ModelSettings> for services::model::ports::ModelSettings {
fn from(content: ModelSettings) -> Self {
Self {
public: content.public,
system_prompt: content.system_prompt,
}
}
}
/// Partial model settings for API requests
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct PartialModelSettings {
/// Whether models are public (visible/usable in responses)
pub public: Option<bool>,
/// Optional system-level system prompt for this model
pub system_prompt: Option<String>,
}
impl From<services::model::ports::PartialModelSettings> for PartialModelSettings {
fn from(content: services::model::ports::PartialModelSettings) -> Self {
Self {
public: content.public,
system_prompt: content.system_prompt,
}
}
}
impl From<PartialModelSettings> for services::model::ports::PartialModelSettings {
fn from(content: PartialModelSettings) -> Self {
Self {
public: content.public,
system_prompt: content.system_prompt,
}
}
}
/// Complete model response
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ModelResponse {
/// External model identifier (e.g. "gpt-4.1")
pub model_id: String,
/// Settings stored for this model
pub settings: ModelSettings,
}
impl From<services::model::ports::Model> for ModelResponse {
fn from(model: services::model::ports::Model) -> Self {
Self {
model_id: model.model_id,
settings: model.settings.into(),
}
}
}
/// Model upsert request
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UpsertModelsRequest {
pub settings: ModelSettings,
}
/// Model settings update request (partial update)
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UpdateModelRequest {
pub settings: Option<PartialModelSettings>,
}
/// Batch model upsert request
///
/// Maps model_id to partial settings to update.
/// Example: { "gpt-4": { "public": true }, "gpt-3.5": { "public": false, "system_prompt": "..." } }
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct BatchUpsertModelsRequest {
#[serde(flatten)]
pub models: std::collections::HashMap<String, PartialModelSettings>,
}
/// Model list response with pagination
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct ModelListResponse {
/// List of models
pub models: Vec<ModelResponse>,
/// Maximum number of items returned
pub limit: i64,
/// Number of items skipped
pub offset: i64,
/// Total number of models
pub total: i64,
}
/// Paginated user list response
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UserListResponse {
/// List of users
pub users: Vec<UserResponse>,
/// Maximum number of items returned
pub limit: i64,
/// Number of items skipped
pub offset: i64,
/// Total number of users
pub total: u64,
}
/// System configs response
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct SystemConfigsResponse {
/// Default model identifier to use when not specified
#[serde(skip_serializing_if = "Option::is_none")]
pub default_model: Option<String>,
}
impl From<services::system_configs::ports::SystemConfigs> for SystemConfigsResponse {
fn from(config: services::system_configs::ports::SystemConfigs) -> Self {
Self {
default_model: config.default_model,
}
}
}
/// System configs upsert request (full replace)
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UpsertSystemConfigsRequest {
/// Default model identifier to use when not specified
#[serde(skip_serializing_if = "Option::is_none")]
pub default_model: Option<String>,
}
impl From<UpsertSystemConfigsRequest> for services::system_configs::ports::SystemConfigs {
fn from(req: UpsertSystemConfigsRequest) -> Self {
services::system_configs::ports::SystemConfigs {
default_model: req.default_model,
}
}
}
/// System configs update request (partial)
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct UpdateSystemConfigsRequest {
/// Default model identifier to use when not specified
#[serde(skip_serializing_if = "Option::is_none")]
pub default_model: Option<String>,
}
impl From<UpdateSystemConfigsRequest> for services::system_configs::ports::PartialSystemConfigs {
fn from(req: UpdateSystemConfigsRequest) -> Self {
services::system_configs::ports::PartialSystemConfigs {
default_model: req.default_model,
}
}
}
/// File list response with pagination
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct FileListResponse {
/// Always "list"
pub object: String,
/// List of files (without `object` field per item)
pub data: Vec<FileGetResponse>,
/// First file ID in the list
#[serde(skip_serializing_if = "Option::is_none")]
pub first_id: Option<String>,
/// Last file ID in the list
#[serde(skip_serializing_if = "Option::is_none")]
pub last_id: Option<String>,
/// Whether there are more files available
pub has_more: bool,
}
/// File get response with `object` field
#[derive(Debug, Serialize, Deserialize, ToSchema)]
pub struct FileGetResponse {
/// Always "file"
pub object: String,
#[serde(flatten)]
pub file: FileData,
}
impl From<FileData> for FileGetResponse {
fn from(file: FileData) -> Self {
Self {
object: "file".to_string(),
file,
}
}
}