//! 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};usecrate::mcp::dispatch::{Ctx, HandlerGroup, ToolResult};usecrate::mcp::handlers::common::iso_of_ms;usecrate::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,};usecrate::mcp::protocol::tool_content;pubfn 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;pubfn list_videos(ctx: &mutCtx<'_>, args: &Map<String, Value>) -> ToolResult {let args = validate_list_media(args)?; list_media(ctx, VIDEO_SPEC, &args)}pubfn update_video(ctx: &mutCtx<'_>, args: &Map<String, Value>) -> ToolResult {let args = validate_update_media(args, "video")?; update_media(ctx, VIDEO_SPEC, &args)}pubfn remove_video(ctx: &mutCtx<'_>, args: &Map<String, Value>) -> ToolResult {let id = require_media_id(args, "video")?; remove_media(ctx, VIDEO_SPEC, &id)}pubfn get_video(ctx: &mutCtx<'_>, args: &Map<String, Value>) -> 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.letmut file_url: Option<String> = None;letmut 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"); } }, }, }letmut 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.letSome(poster_key) = rtext(&row, "poster_key") else {returnOk(Value::Object(answer)); };letSome(store) = ctx.services.media_get.clone() else {returnOk(Value::Object(answer)); };let poster = match store(&poster_key) {Ok(Some(bytes)) if bytes.len() <= POSTER_MAX => bytes, _ => returnOk(Value::Object(answer)), };letmut 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"));letmut 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), ]))}