Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions crates/openplotva-app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4447,10 +4447,15 @@ async fn start_runtime_workers(
telegram.clone(),
)
.with_reaction_ux(Arc::clone(&generation_reactions))
.with_frames(video_jobs::TelegramVideoFrames::new(
telegram.clone(),
Arc::clone(&dialog_tool_media),
)),
.with_frames(
video_jobs::TelegramVideoFrames::new(
telegram.clone(),
Arc::clone(&dialog_tool_media),
)
.with_files(PostgresTelegramFileStore::new(
service_clients.postgres.clone(),
)),
),
}) as Arc<dyn video_jobs::VideoJobRunner>
});
let vip_image_queue = Arc::clone(&task_queue_for_updates);
Expand Down
32 changes: 30 additions & 2 deletions crates/openplotva-app/src/video_jobs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,7 @@ const VIDEO_FRAME_MAX_SIDE: u32 = 1280;
pub struct TelegramVideoFrames {
data_urls: crate::vision::TelegramClientVisionDataUrlProvider,
describer: Arc<dyn crate::dialog_tools::VisionDescriber>,
files: Option<openplotva_storage::PostgresTelegramFileStore>,
}

impl TelegramVideoFrames {
Expand All @@ -251,25 +252,52 @@ impl TelegramVideoFrames {
data_urls: crate::vision::TelegramClientVisionDataUrlProvider::new(telegram)
.with_max_side(VIDEO_FRAME_MAX_SIDE),
describer,
files: None,
}
}

/// Find the file id of a quoted picture, which the dialog knows only by its unique id.
#[must_use]
pub fn with_files(mut self, files: openplotva_storage::PostgresTelegramFileStore) -> Self {
self.files = Some(files);
self
}

async fn file_id(&self, frame: &VideoFrameRef) -> Result<String, String> {
let file_id = frame.file_id.trim();
if !file_id.is_empty() {
return Ok(file_id.to_owned());
}
let Some(files) = &self.files else {
return Err("the picture has no file id".to_owned());
};
files
.list_files_by_unique_ids(&[frame.file_unique_id.trim().to_owned()])
.await
.map_err(|error| format!("look up the picture: {error}"))?
.into_iter()
.map(|record| record.latest_file_id.trim().to_owned())
.find(|file_id| !file_id.is_empty())
.ok_or_else(|| "the picture has no known file id".to_owned())
}

async fn load(
&self,
params: &VideoGenJobParams,
frame: &VideoFrameRef,
) -> Result<LoadedVideoFrame, String> {
use crate::vision::TelegramVisionDataUrlProvider as _;

let file_id = self.file_id(frame).await?;
let data_url = self
.data_urls
.telegram_file_data_url(&frame.file_id, "photo", None)
.telegram_file_data_url(&file_id, "photo", None)
.await
.map_err(|error| format!("download the picture: {error}"))?;
let picture = openplotva_core::ChatAttachment {
kind: "image".to_owned(),
source: "message".to_owned(),
file_id: frame.file_id.clone(),
file_id,
file_unique_id: frame.file_unique_id.clone(),
..openplotva_core::ChatAttachment::default()
};
Expand Down
Loading