mirror of
https://github.com/fawney19/Aether.git
synced 2026-10-08 02:17:46 +08:00
Consolidate subscription usage policy enforcement, privacy-safe persistence, and gateway security hardening into one reviewable change. Includes bounded HTTP and execution envelopes, header and protocol guards, DNS and relay validation, authentication and secret projection hardening, secure backup/install paths, and regression coverage.
110 lines
3.9 KiB
Rust
110 lines
3.9 KiB
Rust
use std::collections::BTreeMap;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
use serde_json::{Map, Value};
|
|
|
|
use crate::{
|
|
GeminiVideoTaskSeed, LocalVideoTaskReadResponse, LocalVideoTaskRegistryMutation,
|
|
LocalVideoTaskSnapshot, LocalVideoTaskStatus, OpenAiVideoTaskSeed,
|
|
};
|
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
|
pub struct VideoTaskRegistry {
|
|
openai: BTreeMap<String, LocalVideoTaskSnapshot>,
|
|
gemini: BTreeMap<String, LocalVideoTaskSnapshot>,
|
|
}
|
|
|
|
impl VideoTaskRegistry {
|
|
pub fn insert(&mut self, mut snapshot: LocalVideoTaskSnapshot) {
|
|
snapshot.sanitize_persisted_diagnostics();
|
|
match &snapshot {
|
|
LocalVideoTaskSnapshot::OpenAi(seed) => {
|
|
self.openai.insert(seed.local_task_id.clone(), snapshot);
|
|
}
|
|
LocalVideoTaskSnapshot::Gemini(seed) => {
|
|
self.gemini.insert(seed.local_short_id.clone(), snapshot);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn read_openai(&self, task_id: &str) -> Option<LocalVideoTaskReadResponse> {
|
|
self.openai
|
|
.get(task_id)
|
|
.map(LocalVideoTaskSnapshot::read_response)
|
|
}
|
|
|
|
pub fn read_gemini(&self, short_id: &str) -> Option<LocalVideoTaskReadResponse> {
|
|
self.gemini
|
|
.get(short_id)
|
|
.map(LocalVideoTaskSnapshot::read_response)
|
|
}
|
|
|
|
pub fn clone_openai(&self, task_id: &str) -> Option<OpenAiVideoTaskSeed> {
|
|
let LocalVideoTaskSnapshot::OpenAi(seed) = self.openai.get(task_id)?.clone() else {
|
|
return None;
|
|
};
|
|
Some(seed)
|
|
}
|
|
|
|
pub fn clone_gemini(&self, short_id: &str) -> Option<GeminiVideoTaskSeed> {
|
|
let LocalVideoTaskSnapshot::Gemini(seed) = self.gemini.get(short_id)?.clone() else {
|
|
return None;
|
|
};
|
|
Some(seed)
|
|
}
|
|
|
|
pub fn list_active_snapshots(&self, limit: usize) -> Vec<LocalVideoTaskSnapshot> {
|
|
self.openai
|
|
.values()
|
|
.chain(self.gemini.values())
|
|
.filter(|snapshot| snapshot.is_active_for_refresh())
|
|
.take(limit)
|
|
.cloned()
|
|
.collect()
|
|
}
|
|
|
|
pub fn apply_mutation(&mut self, mutation: LocalVideoTaskRegistryMutation) {
|
|
match mutation {
|
|
LocalVideoTaskRegistryMutation::OpenAiCancelled { task_id } => {
|
|
if let Some(LocalVideoTaskSnapshot::OpenAi(seed)) = self.openai.get_mut(&task_id) {
|
|
seed.status = LocalVideoTaskStatus::Cancelled;
|
|
}
|
|
}
|
|
LocalVideoTaskRegistryMutation::OpenAiDeleted { task_id } => {
|
|
if let Some(LocalVideoTaskSnapshot::OpenAi(seed)) = self.openai.get_mut(&task_id) {
|
|
seed.status = LocalVideoTaskStatus::Deleted;
|
|
}
|
|
}
|
|
LocalVideoTaskRegistryMutation::GeminiCancelled { short_id } => {
|
|
if let Some(LocalVideoTaskSnapshot::Gemini(seed)) = self.gemini.get_mut(&short_id) {
|
|
seed.status = LocalVideoTaskStatus::Cancelled;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn project_openai(&mut self, task_id: &str, provider_body: &Map<String, Value>) -> bool {
|
|
let Some(LocalVideoTaskSnapshot::OpenAi(seed)) = self.openai.get_mut(task_id) else {
|
|
return false;
|
|
};
|
|
seed.apply_provider_body(provider_body);
|
|
true
|
|
}
|
|
|
|
pub fn project_gemini(&mut self, short_id: &str, provider_body: &Map<String, Value>) -> bool {
|
|
let Some(LocalVideoTaskSnapshot::Gemini(seed)) = self.gemini.get_mut(short_id) else {
|
|
return false;
|
|
};
|
|
seed.apply_provider_body(provider_body);
|
|
true
|
|
}
|
|
|
|
pub(crate) fn sanitize_persisted_diagnostics(&mut self) -> bool {
|
|
let mut changed = false;
|
|
for snapshot in self.openai.values_mut().chain(self.gemini.values_mut()) {
|
|
changed = snapshot.sanitize_persisted_diagnostics() || changed;
|
|
}
|
|
changed
|
|
}
|
|
}
|