//! The video tools (schema v31): `list_videos`, `get_video`, `update_video`, //! `remove_video`. //! //! The machinery is [`super::media_tools`] — one implementation shared with //! photos. What lives HERE is the one difference that matters to a caller: a //! recording cannot travel inside a tool result, so there is no //! `get_video_bytes`. Half a gigabyte of base64 is not a thing to put in a //! conversation, and pretending otherwise would fail at the cap every single //! time. `get_video` answers with a short-lived download link instead — the //! `get_document` precedent, the same five minutes — plus the still frame as //! an image, when the phone has made one. //! //! The poster is the PHONE's column and `summary` is the server's: one writer //! each. So a video with no poster answers without one rather than minting a //! frame the server cannot see. use base64::engine::general_purpose::STANDARD; use base64::Engine; use serde_json::{Map, Value}; use crate::mcp::dispatch::{Ctx, HandlerGroup, ToolResult}; use crate::mcp::handlers::common::iso_of_ms; use crate::mcp::handlers::media_tools::{ get_media, list_media, media_row, remove_media, require_media_id, rtext, update_media, validate_list_media, validate_update_media, VIDEO_SPEC, }; use crate::mcp::protocol::tool_content; pub fn handlers() -> HandlerGroup { &[ ("list_videos", list_videos), ("get_video", get_video), ("update_video", update_video), ("remove_video", remove_video), ] } /// How long a recording's download link lives — `documents.ts`'s /// `FILE_URL_TTL_SECONDS`, deliberately the same five minutes: an assistant /// that asked for one is about to use it, and a link still live an hour later /// is only useful to whoever finds it in a log. Named here rather than reached /// for, the way `records_2::messages` names the attachment link's, so this /// group does not depend on another group's file for a number. const FILE_URL_TTL_SECONDS: u64 = 300; /// The most a POSTER may weigh to travel inside a tool result. /// /// A poster is a 512-px JPEG the phone made, so this ceiling is never reached /// in practice — it exists because "never reached in practice" is exactly what /// every oversized payload was before it arrived. Over it, the answer simply /// carries no image and the rest of the video's facts are unaffected. const POSTER_MAX: usize = 2 * 1024 * 1024; pub fn list_videos(ctx: &mut Ctx<'_>, args: &Map) -> ToolResult { let args = validate_list_media(args)?; list_media(ctx, VIDEO_SPEC, &args) } pub fn update_video(ctx: &mut Ctx<'_>, args: &Map) -> ToolResult { let args = validate_update_media(args, "video")?; update_media(ctx, VIDEO_SPEC, &args) } pub fn remove_video(ctx: &mut Ctx<'_>, args: &Map) -> ToolResult { let id = require_media_id(args, "video")?; remove_media(ctx, VIDEO_SPEC, &id) } pub fn get_video(ctx: &mut Ctx<'_>, args: &Map) -> ToolResult { let id = require_media_id(args, "video")?; let facts = get_media(ctx, VIDEO_SPEC, &id)?; let row = media_row(ctx, VIDEO_SPEC, &id)?; let key = rtext(&row, "key").unwrap_or_default(); // ASKED BEFORE IT IS SIGNED. A presigned GET is a signature, not a lookup — // a store mints one happily for an object that is not there, and the caller // finds out by downloading a 404. Under offline-first the row routinely // arrives before the bytes, so "the recording has not landed yet" is an // ordinary state that has to be SAID rather than handed over as a link that // fails. let mut file_url: Option = None; let mut file_url_note: Option<&str> = None; match ( ctx.services.media_head.clone(), ctx.services.presign.clone(), ) { (None, _) | (_, None) => { file_url_note = Some("this server holds no object store, so there is nothing to link to"); } (Some(head), Some(presign)) => match head(&key) { Ok(false) => { file_url_note = Some( "the recording's bytes have not reached the server yet — it is still on the \ owner's phone, waiting to upload", ); } // A store that is BROKEN is not a store that is empty, but neither // is a reason to withhold everything else the row knows. Err(_) => { file_url_note = Some("this server could not mint a download link for the recording"); } Ok(true) => match presign(&key, FILE_URL_TTL_SECONDS) { Ok(url) => file_url = Some(url), Err(_) => { file_url_note = Some("this server could not mint a download link for the recording"); } }, }, } let mut answer = facts; match &file_url { Some(url) => { answer.insert("fileUrl".to_string(), Value::from(url.clone())); answer.insert( "fileExpiresAt".to_string(), Value::from(iso_of_ms( ctx.now.timestamp_millis() + (FILE_URL_TTL_SECONDS as i64) * 1000, )), ); } None => { answer.insert("fileUrl".to_string(), Value::Null); answer.insert( "fileUrlNote".to_string(), file_url_note.map_or(Value::Null, Value::from), ); } } // The still frame, when the phone has made one and the bytes are here. An // image block rather than a link, because looking at it is the point. let Some(poster_key) = rtext(&row, "poster_key") else { return Ok(Value::Object(answer)); }; let Some(store) = ctx.services.media_get.clone() else { return Ok(Value::Object(answer)); }; let poster = match store(&poster_key) { Ok(Some(bytes)) if bytes.len() <= POSTER_MAX => bytes, _ => return Ok(Value::Object(answer)), }; let mut image = Map::new(); image.insert("type".to_string(), Value::from("image")); image.insert("data".to_string(), Value::from(STANDARD.encode(&poster))); image.insert("mimeType".to_string(), Value::from("image/jpeg")); let mut text_block = Map::new(); text_block.insert("type".to_string(), Value::from("text")); text_block.insert( "text".to_string(), Value::from(Value::Object(answer).to_string()), ); Ok(tool_content(vec![ Value::Object(image), Value::Object(text_block), ])) }