2026-04-04 01:40:24 +08:00
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use serde_json::{Map, Value};
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
use crate::{
|
2026-04-04 01:40:24 +08:00
|
|
|
GeminiVideoTaskSeed, LocalVideoTaskReadResponse, LocalVideoTaskRegistryMutation,
|
|
|
|
|
LocalVideoTaskSnapshot, LocalVideoTaskStatus, OpenAiVideoTaskSeed,
|
|
|
|
|
};
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
2026-04-05 20:23:16 +08:00
|
|
|
pub struct VideoTaskRegistry {
|
2026-03-31 19:19:04 +08:00
|
|
|
openai: BTreeMap<String, LocalVideoTaskSnapshot>,
|
|
|
|
|
gemini: BTreeMap<String, LocalVideoTaskSnapshot>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl VideoTaskRegistry {
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn insert(&mut self, snapshot: LocalVideoTaskSnapshot) {
|
2026-03-31 19:19:04 +08:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn read_openai(&self, task_id: &str) -> Option<LocalVideoTaskReadResponse> {
|
2026-03-31 19:19:04 +08:00
|
|
|
self.openai
|
|
|
|
|
.get(task_id)
|
|
|
|
|
.map(LocalVideoTaskSnapshot::read_response)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn read_gemini(&self, short_id: &str) -> Option<LocalVideoTaskReadResponse> {
|
2026-03-31 19:19:04 +08:00
|
|
|
self.gemini
|
|
|
|
|
.get(short_id)
|
|
|
|
|
.map(LocalVideoTaskSnapshot::read_response)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn clone_openai(&self, task_id: &str) -> Option<OpenAiVideoTaskSeed> {
|
2026-03-31 19:19:04 +08:00
|
|
|
let LocalVideoTaskSnapshot::OpenAi(seed) = self.openai.get(task_id)?.clone() else {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
Some(seed)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn clone_gemini(&self, short_id: &str) -> Option<GeminiVideoTaskSeed> {
|
2026-03-31 19:19:04 +08:00
|
|
|
let LocalVideoTaskSnapshot::Gemini(seed) = self.gemini.get(short_id)?.clone() else {
|
|
|
|
|
return None;
|
|
|
|
|
};
|
|
|
|
|
Some(seed)
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn list_active_snapshots(&self, limit: usize) -> Vec<LocalVideoTaskSnapshot> {
|
2026-03-31 19:19:04 +08:00
|
|
|
self.openai
|
|
|
|
|
.values()
|
|
|
|
|
.chain(self.gemini.values())
|
|
|
|
|
.filter(|snapshot| snapshot.is_active_for_refresh())
|
|
|
|
|
.take(limit)
|
|
|
|
|
.cloned()
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn apply_mutation(&mut self, mutation: LocalVideoTaskRegistryMutation) {
|
2026-03-31 19:19:04 +08:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn project_openai(&mut self, task_id: &str, provider_body: &Map<String, Value>) -> bool {
|
2026-03-31 19:19:04 +08:00
|
|
|
let Some(LocalVideoTaskSnapshot::OpenAi(seed)) = self.openai.get_mut(task_id) else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
seed.apply_provider_body(provider_body);
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-05 20:23:16 +08:00
|
|
|
pub fn project_gemini(&mut self, short_id: &str, provider_body: &Map<String, Value>) -> bool {
|
2026-03-31 19:19:04 +08:00
|
|
|
let Some(LocalVideoTaskSnapshot::Gemini(seed)) = self.gemini.get_mut(short_id) else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
|
|
|
|
seed.apply_provider_body(provider_body);
|
|
|
|
|
true
|
|
|
|
|
}
|
|
|
|
|
}
|