mod chunking;
mod embedding;
pub mod fixtures;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;
use adk_artifact::{ArtifactService, DeleteRequest};
use adk_core::audit_log::{AuditRecordDraft, AuditRecordKind, AuditRecordOutcome};
use adk_core::{AdkError, Content, ErrorCategory, GenerateContentConfig, Llm, LlmRequest};
use adk_memory::graph::{CreateEntityInput, CreateRelationInput};
use adk_memory::{GraphMemoryService, MemoryEntry, MemoryService};
use adk_rag::{
Chunk, CollectionSpec, EmbeddingProvider, Field, Filter, RagError, Scope, SearchResult,
VectorQuery, VectorStore,
};
use chrono::{DateTime, NaiveDate, Utc};
use futures::StreamExt;
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
pub use chunking::Chunking;
pub use embedding::WordHashEmbedding;
use self::chunking::{ChunkCutter, PendingChunk};
use crate::audit::VISION_AUDIT_KIND;
use crate::backend::BackendDescriptor;
use crate::cloud::{LlmDescriber, extract_json};
use crate::cx::VisionCx;
use crate::digest::Digest;
use crate::error::{Result, VisionError};
use crate::manifest::VisionTask;
use crate::media::{
ArtifactLocator, Frame, FrameMeta, FramePixels, FrameSourceId, ImageRef, MediaScope,
MediaStore, Provenance, SourceImage, VideoRef, artifact_uri,
};
use crate::memory::{VisualItem, VisualMemory};
use crate::runtime::{CallOptions, CallRecord, VideoSource, VisionOutput, VisionRuntime};
use crate::sent::VisionInput;
use crate::stream::{FramePrivacy, FrameStream, NoRedaction, PrivacyHook};
use crate::traits::{Describer, Sampling, VideoRequest, VideoUsage};
use crate::video::answer::{ModelAnswer, add_usage, generate};
use crate::video::{evenly_spaced, timestamp};
pub const VISION_ANALYST_INDEXED: &str = "vision.analyst.indexed";
pub const VISION_ANALYST_ASKED: &str = "vision.analyst.asked";
pub const VISION_ANALYST_ERASED: &str = "vision.analyst.erased";
pub const CHUNK_KIND: &str = "videoChunk";
pub mod keys {
pub const KIND: &str = "kind";
pub const INDEX: &str = "index";
pub const START_MS: &str = "startMs";
pub const END_MS: &str = "endMs";
pub const LABELS: &str = "labels";
pub const KEYFRAME: &str = "keyframe";
pub const CLIP: &str = "clip";
pub const APP: &str = "app";
pub const USER: &str = "user";
pub const CAPTURED_AT: &str = "capturedAt";
pub const DAY: &str = "day";
pub const RUN: &str = "run";
pub const DESCRIBER: &str = "describer";
}
pub const FILTERABLE: [&str; 3] = [keys::KIND, keys::DAY, keys::RUN];
fn kind() -> AuditRecordKind {
AuditRecordKind::Other(VISION_AUDIT_KIND.to_string())
}
fn rag(err: RagError) -> VisionError {
VisionError::Core(AdkError::from(err))
}
#[derive(Clone, Debug, PartialEq)]
#[non_exhaustive]
pub struct ClipSource {
pub source: String,
pub scope: MediaScope,
pub clip: Option<VideoRef>,
pub started_at: Option<DateTime<Utc>>,
pub run: Option<String>,
}
impl ClipSource {
pub fn new(source: impl Into<String>, scope: MediaScope) -> Self {
Self { source: source.into(), scope, clip: None, started_at: None, run: None }
}
pub fn with_clip(mut self, clip: VideoRef) -> Self {
self.clip = Some(clip);
self
}
pub fn starting_at(mut self, at: DateTime<Utc>) -> Self {
self.started_at = Some(at);
self
}
pub fn with_run(mut self, run: impl Into<String>) -> Self {
self.run = Some(run.into());
self
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct VideoChunk {
pub id: String,
pub source: String,
pub index: u64,
pub start_ms: u64,
pub end_ms: u64,
pub caption: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub labels: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub keyframe: Option<ImageRef>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub clip: Option<VideoRef>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub captured_at: Option<DateTime<Utc>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub run: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub describer: Option<String>,
}
impl VideoChunk {
pub fn id_for(source: &str, start_ms: u64, end_ms: u64) -> String {
let key = format!("{source}\0{start_ms}\0{end_ms}");
format!("vc-{}", &Digest::of(key.as_bytes()).hex()[..32])
}
pub fn contains(&self, ms: u64) -> bool {
(self.start_ms..self.end_ms).contains(&ms)
}
pub fn time_range(&self) -> String {
format!("{}–{}", timestamp(self.start_ms), timestamp(self.end_ms))
}
pub fn artifact_uri(&self) -> Option<String> {
self.keyframe
.as_ref()
.map(|k| artifact_uri(&k.artifact))
.or_else(|| self.clip.as_ref().map(|c| artifact_uri(&c.artifact)))
}
fn embedded_text(&self) -> String {
if self.labels.is_empty() {
self.caption.clone()
} else {
format!("{} Seen: {}.", self.caption, self.labels.join(", "))
}
}
pub fn from_chunk(chunk: &Chunk) -> Result<Self> {
let meta = |key: &str| chunk.metadata.get(key);
let number = |key: &str| meta(key).and_then(|v| v.parse::<u64>().ok());
let (Some(start_ms), Some(end_ms)) = (number(keys::START_MS), number(keys::END_MS)) else {
return Err(VisionError::Parse {
backend: "video-analyst".into(),
message: format!("chunk `{}` carries no time range", chunk.id),
});
};
Ok(Self {
id: chunk.id.clone(),
source: chunk.document_id.clone(),
index: number(keys::INDEX).unwrap_or_default(),
start_ms,
end_ms,
caption: chunk.text.clone(),
labels: meta(keys::LABELS)
.map(|l| l.split('\n').filter(|s| !s.is_empty()).map(str::to_string).collect())
.unwrap_or_default(),
keyframe: meta(keys::KEYFRAME).and_then(|k| serde_json::from_str(k).ok()),
clip: meta(keys::CLIP).and_then(|c| serde_json::from_str(c).ok()),
captured_at: meta(keys::CAPTURED_AT)
.and_then(|t| DateTime::parse_from_rfc3339(t).ok())
.map(|t| t.to_utc()),
run: meta(keys::RUN).cloned(),
describer: meta(keys::DESCRIBER).cloned(),
})
}
fn to_chunk(&self, embedding: Vec<f32>, scope: &MediaScope) -> Result<Chunk> {
let mut chunk = Chunk::new(self.id.clone(), self.source.clone(), self.caption.clone())
.with_embedding(embedding)
.with_metadata(keys::KIND, CHUNK_KIND)
.with_metadata(keys::INDEX, self.index.to_string())
.with_metadata(keys::START_MS, self.start_ms.to_string())
.with_metadata(keys::END_MS, self.end_ms.to_string())
.with_metadata(keys::APP, scope.app_name.clone())
.with_metadata(keys::USER, scope.user_id.clone());
if let Some(uri) = self.artifact_uri() {
chunk = chunk.with_source_uri(uri);
}
if !self.labels.is_empty() {
chunk = chunk.with_metadata(keys::LABELS, self.labels.join("\n"));
}
if let Some(keyframe) = &self.keyframe {
chunk = chunk.with_metadata(keys::KEYFRAME, to_json(keyframe)?);
}
if let Some(clip) = &self.clip {
chunk = chunk.with_metadata(keys::CLIP, to_json(clip)?);
}
if let Some(at) = self.captured_at {
chunk = chunk
.with_metadata(
keys::CAPTURED_AT,
at.to_rfc3339_opts(chrono::SecondsFormat::Millis, true),
)
.with_metadata(keys::DAY, at.date_naive().format("%Y-%m-%d").to_string());
}
if let Some(run) = &self.run {
chunk = chunk.with_metadata(keys::RUN, run.clone());
}
if let Some(describer) = &self.describer {
chunk = chunk.with_metadata(keys::DESCRIBER, describer.clone());
}
Ok(chunk)
}
}
fn to_json<T: Serialize>(value: &T) -> Result<String> {
serde_json::to_string(value).map_err(|e| VisionError::Backend {
backend: "video-analyst".into(),
message: format!("chunk metadata could not be written: {e}"),
})
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct IndexedClip {
pub source: String,
pub chunks: Vec<VideoChunk>,
pub duration_ms: u64,
pub frames_read: u64,
pub usage: VideoUsage,
}
impl IndexedClip {
pub fn cost_per_hour_micro_usd(&self) -> Option<u64> {
let cost = self.usage.cost_micro_usd?;
if self.duration_ms == 0 {
return None;
}
let hourly = u128::from(cost) * 3_600_000 / u128::from(self.duration_ms);
Some(u64::try_from(hourly).unwrap_or(u64::MAX))
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ChunkHit {
pub chunk: VideoChunk,
pub score: f32,
}
#[derive(Clone, Debug, Default)]
#[non_exhaustive]
pub struct AnalystQuery {
pub top_k: usize,
pub source: Option<String>,
pub days: Vec<NaiveDate>,
pub filter: Option<Filter>,
}
impl AnalystQuery {
pub fn top(top_k: usize) -> Self {
Self { top_k, ..Self::default() }
}
pub fn source(mut self, source: impl Into<String>) -> Self {
self.source = Some(source.into());
self
}
pub fn on(mut self, days: impl IntoIterator<Item = NaiveDate>) -> Self {
self.days.extend(days);
self
}
pub fn filter(mut self, filter: Filter) -> Self {
self.filter = Some(filter);
self
}
fn compile(&self) -> Filter {
let mut all = vec![Filter::eq(Field::metadata(keys::KIND), CHUNK_KIND)];
if let Some(source) = &self.source {
all.push(Filter::eq(Field::DocumentId, source.clone()));
}
if !self.days.is_empty() {
all.push(Filter::is_in(
Field::metadata(keys::DAY),
self.days.iter().map(|d| d.format("%Y-%m-%d").to_string()),
));
}
if let Some(filter) = &self.filter {
all.push(filter.clone());
}
Filter::and(all)
}
}
#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct AnalystAnswer {
pub question: String,
pub answer: String,
pub citations: Vec<VideoChunk>,
#[serde(skip_serializing_if = "Option::is_none")]
pub supported: Option<bool>,
pub retrieved: Vec<ChunkHit>,
#[serde(skip_serializing_if = "Option::is_none")]
pub expanded: Option<String>,
pub usage: VideoUsage,
#[serde(skip_serializing_if = "Option::is_none")]
pub answered_by: Option<String>,
}
impl AnalystAnswer {
pub fn primary(&self) -> Option<&VideoChunk> {
self.citations.first()
}
}
#[derive(Clone, Debug, Default, PartialEq, Serialize)]
#[serde(rename_all = "camelCase")]
#[non_exhaustive]
pub struct ClipErasure {
pub source: String,
pub chunks: u64,
pub captions: u64,
pub entities: usize,
pub keyframes: Vec<ArtifactLocator>,
pub visual_chunks: u64,
}
struct Keyframes {
artifacts: Arc<dyn ArtifactService>,
media: MediaStore,
privacy: FramePrivacy,
visual: Option<Arc<VisualMemory>>,
}
struct Answerer {
llm: Arc<dyn Llm>,
descriptor: BackendDescriptor,
max_output_tokens: u32,
}
pub struct VideoAnalyst {
runtime: Arc<VisionRuntime>,
options: CallOptions,
store: Arc<dyn VectorStore>,
embedder: Arc<dyn EmbeddingProvider>,
collection: String,
chunking: Chunking,
frames_per_chunk: usize,
captions: Option<Arc<dyn MemoryService>>,
graph: Option<Arc<GraphMemoryService>>,
keyframes: Option<Keyframes>,
answerer: Option<Answerer>,
expand_questions: bool,
}
impl std::fmt::Debug for VideoAnalyst {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("VideoAnalyst")
.field("collection", &self.collection)
.field("chunking", &self.chunking)
.field("frames_per_chunk", &self.frames_per_chunk)
.field("captions", &self.captions.is_some())
.field("graph", &self.graph.is_some())
.field("keyframes", &self.keyframes.is_some())
.field("answerer", &self.answerer.as_ref().map(|a| a.descriptor.id.to_string()))
.finish_non_exhaustive()
}
}
#[must_use]
pub struct VideoAnalystBuilder {
analyst: VideoAnalyst,
visual: Option<Arc<VisualMemory>>,
keyframe_privacy: Option<Arc<dyn PrivacyHook>>,
}
impl VideoAnalystBuilder {
pub fn chunking(mut self, chunking: Chunking) -> Self {
self.analyst.chunking = chunking;
self
}
pub fn frames_per_chunk(mut self, frames: usize) -> Self {
self.analyst.frames_per_chunk = frames.max(1);
self
}
pub fn call_options(mut self, options: CallOptions) -> Self {
self.analyst.options = options;
self
}
pub fn captions(mut self, memory: Arc<dyn MemoryService>) -> Self {
self.analyst.captions = Some(memory);
self
}
pub fn graph(mut self, graph: Arc<GraphMemoryService>) -> Self {
self.analyst.graph = Some(graph);
self
}
pub fn keyframes(mut self, artifacts: Arc<dyn ArtifactService>) -> Self {
self.analyst.keyframes = Some(Keyframes {
media: MediaStore::new(Arc::clone(&artifacts)),
artifacts,
privacy: FramePrivacy::new(Arc::new(NoRedaction)),
visual: None,
});
self
}
pub fn keyframe_privacy(mut self, hook: Arc<dyn PrivacyHook>) -> Self {
self.keyframe_privacy = Some(hook);
self
}
pub fn visual_memory(mut self, memory: Arc<VisualMemory>) -> Self {
self.visual = Some(memory);
self
}
pub fn answerer<I: VisionInput>(mut self, describer: &LlmDescriber<I>) -> Self
where
LlmDescriber<I>: Describer<I>,
{
self.analyst.answerer = Some(Answerer {
llm: Arc::clone(describer.model()),
descriptor: Describer::descriptor(describer).clone(),
max_output_tokens: 1_024,
});
self
}
pub fn expand_questions(mut self, expand: bool) -> Self {
self.analyst.expand_questions = expand;
self
}
pub async fn open(mut self) -> Result<VideoAnalyst> {
self.analyst.chunking.validate()?;
self.analyst.runtime.route(VisionTask::AnalyzeVideo, &self.analyst.options)?;
if let Some(visual) = self.visual.take() {
let Some(keyframes) = &mut self.analyst.keyframes else {
return Err(VisionError::InvalidInput(
"a visual memory embeds keyframes; configure `keyframes` too".into(),
));
};
keyframes.visual = Some(visual);
}
if let Some(hook) = self.keyframe_privacy.take() {
let Some(keyframes) = &mut self.analyst.keyframes else {
return Err(VisionError::InvalidInput(
"a keyframe privacy hook redacts keyframes; configure `keyframes` too".into(),
));
};
keyframes.privacy = FramePrivacy::new(hook);
}
let analyst = self.analyst;
let spec = CollectionSpec::new(analyst.embedder.dimensions()).with_filterable(FILTERABLE);
analyst.store.create_collection(&analyst.collection, &spec).await.map_err(rag)?;
Ok(analyst)
}
}
impl VideoAnalyst {
pub fn builder(
runtime: Arc<VisionRuntime>,
store: Arc<dyn VectorStore>,
embedder: Arc<dyn EmbeddingProvider>,
collection: impl Into<String>,
) -> VideoAnalystBuilder {
VideoAnalystBuilder {
analyst: Self {
runtime,
options: CallOptions::default(),
store,
embedder,
collection: collection.into(),
chunking: Chunking::default(),
frames_per_chunk: 6,
captions: None,
graph: None,
keyframes: None,
answerer: None,
expand_questions: true,
},
visual: None,
keyframe_privacy: None,
}
}
pub fn collection(&self) -> &str {
&self.collection
}
pub fn chunking(&self) -> Chunking {
self.chunking
}
pub async fn index(
&self,
cx: &VisionCx,
scope: &Scope,
clip: &ClipSource,
mut frames: FrameStream,
sampling: Sampling,
) -> Result<VisionOutput<IndexedClip>> {
cx.check()?;
if self.count(scope, &clip.source).await? > 0 {
self.erase_source(cx, scope, &clip.source).await?;
}
let step_ms = if sampling.fps > 0.0 { (1_000.0 / sampling.fps) as u64 } else { 0 };
let mut cutter = ChunkCutter::new(self.chunking, step_ms);
let mut indexed = IndexedClip {
source: clip.source.clone(),
chunks: Vec::new(),
duration_ms: 0,
frames_read: 0,
usage: VideoUsage { cost_micro_usd: Some(0), ..VideoUsage::default() },
};
let mut calls = Vec::new();
while let Some(frame) = frames.next().await {
cx.check()?;
indexed.frames_read += 1;
if let Some(pending) = cutter.push(frame?)? {
self.index_chunk(cx, scope, clip, pending, sampling, &mut indexed, &mut calls)
.await?;
}
}
if let Some(pending) = cutter.finish() {
self.index_chunk(cx, scope, clip, pending, sampling, &mut indexed, &mut calls).await?;
}
if let Some(graph) = &self.graph {
self.record_graph(graph, clip, &indexed.chunks).await?;
}
indexed.duration_ms = match (indexed.chunks.first(), indexed.chunks.last()) {
(Some(first), Some(last)) => last.end_ms.saturating_sub(first.start_ms),
_ => 0,
};
let describers: BTreeSet<&str> =
indexed.chunks.iter().filter_map(|c| c.describer.as_deref()).collect();
let mut draft = AuditRecordDraft::new(kind(), VISION_ANALYST_INDEXED)
.outcome(AuditRecordOutcome::Succeeded)
.payload_ref(clip.source.clone())
.ext("collection", json!(self.collection))
.ext("scope", json!(scope.as_str()))
.ext("source", json!(clip.source))
.ext("chunks", json!(indexed.chunks.len()))
.ext("durationMs", json!(indexed.duration_ms))
.ext("framesRead", json!(indexed.frames_read))
.ext("framesSent", json!(indexed.usage.frames))
.ext("requests", json!(indexed.usage.requests))
.ext("costMicroUsd", json!(indexed.usage.cost_micro_usd))
.ext("costPerHourMicroUsd", json!(indexed.cost_per_hour_micro_usd()))
.ext("describers", json!(describers))
.ext("run", json!(clip.run));
if let Some(video) = &clip.clip {
draft = draft.ext("clip", json!(video.digest.to_string()));
}
cx.audit(draft).await?;
tracing::info!(
collection = %self.collection,
video.source = %clip.source,
video.chunks = indexed.chunks.len(),
video.duration_ms = indexed.duration_ms,
"video analyst indexed a clip"
);
Ok(VisionOutput { value: indexed, calls })
}
#[allow(clippy::too_many_arguments)]
async fn index_chunk(
&self,
cx: &VisionCx,
scope: &Scope,
clip: &ClipSource,
pending: PendingChunk,
sampling: Sampling,
indexed: &mut IndexedClip,
calls: &mut Vec<CallRecord>,
) -> Result<()> {
let PendingChunk { index, start_ms, end_ms, frames } = pending;
let picked = evenly_spaced(&frames, self.frames_per_chunk);
let images = picked
.iter()
.map(|(meta, pixels)| {
Ok(((*meta).clone(), SourceImage::from_pixels((**pixels).clone())?))
})
.collect::<Result<Vec<(FrameMeta, SourceImage)>>>()?;
let span = end_ms.saturating_sub(start_ms).max(1);
let fps = (images.len() as f32 * 1_000.0 / span as f32).max(f32::MIN_POSITIVE);
let rate = Sampling { fps, max_long_edge: sampling.max_long_edge };
let out = self
.runtime
.analyze_video_with(
cx,
VideoSource::frames(&images, rate),
&VideoRequest::Caption { chunk_ms: span },
&self.options,
)
.await?;
let describer = out.calls.first().map(|c| c.backend.to_string());
calls.extend(out.calls);
let analysis = out.value;
merge_usage(&mut indexed.usage, &analysis.usage);
let mut caption: Vec<&str> = analysis.events.iter().map(|e| e.text.trim()).collect();
caption.retain(|c| !c.is_empty());
let caption = match (caption.is_empty(), analysis.answer.as_deref()) {
(false, _) => caption.join(" "),
(true, Some(answer)) if !answer.trim().is_empty() => answer.trim().to_string(),
(true, _) => {
return Err(VisionError::Parse {
backend: describer.unwrap_or_default(),
message: format!(
"no caption for the chunk {}–{}",
timestamp(start_ms),
timestamp(end_ms)
),
});
}
};
let mut labels: Vec<String> = Vec::new();
for label in analysis.events.iter().flat_map(|e| e.labels.iter()) {
if !labels.iter().any(|l| l.eq_ignore_ascii_case(label)) {
labels.push(label.clone());
}
}
let captured_at = match clip.started_at {
Some(at) => Some(
at + chrono::TimeDelta::milliseconds(i64::try_from(start_ms).unwrap_or(i64::MAX)),
),
None => frames.first().map(|(m, _)| m.captured_at),
};
let mut chunk = VideoChunk {
id: VideoChunk::id_for(&clip.source, start_ms, end_ms),
source: clip.source.clone(),
index,
start_ms,
end_ms,
caption,
labels,
keyframe: None,
clip: clip.clip.clone(),
captured_at,
run: clip.run.clone(),
describer,
};
if let Some(keyframes) = &self.keyframes {
let (meta, pixels) = picked[picked.len() / 2];
let frame = Frame {
source: meta.source.clone(),
index: meta.index,
pts: std::time::Duration::from_millis(meta.pts_ms),
captured_at: meta.captured_at,
pixels: FramePixels::Ephemeral(Arc::clone(pixels)),
};
let redacted = keyframes.privacy.apply(cx, frame).await?;
let provenance = Provenance::Camera { source: FrameSourceId(clip.source.clone()) };
let (image, stored) = keyframes
.media
.put_pixels(&clip.scope, redacted.pixels().clone(), provenance)
.await?;
if let Some(visual) = &keyframes.visual {
let mut item = VisualItem::new(
image.clone(),
stored,
chunk.caption.clone(),
clip.source.clone(),
captured_at.unwrap_or(meta.captured_at),
)
.with_labels(chunk.labels.clone());
if let Some(run) = &clip.run {
item = item.with_run(run.clone());
}
calls.extend(visual.index(cx, scope, &[item]).await?.calls);
}
chunk.keyframe = Some(image);
}
let vector = self.embedder.embed(&chunk.embedded_text()).await.map_err(rag)?;
self.store
.upsert(&self.collection, scope, &[chunk.to_chunk(vector, &clip.scope)?])
.await
.map_err(rag)?;
if let Some(captions) = &self.captions {
let (app, user) = (&clip.scope.app_name, &clip.scope.user_id);
let mut entry = MemoryEntry::new(
Content::new("model").with_text(format!(
"[{}] {}",
chunk.time_range(),
chunk.caption
)),
"vision",
)
.with_id(chunk.id.clone());
if let Some(at) = chunk.captured_at {
entry = entry.with_timestamp(at);
}
if let Some(keyframe) = &chunk.keyframe {
entry = entry
.with_artifact(artifact_uri(&keyframe.artifact), keyframe.mime_type.clone());
}
if let Some(video) = &chunk.clip {
entry = entry.with_artifact(artifact_uri(&video.artifact), video.mime_type.clone());
}
captions.delete_memory(app, user, &chunk.id).await?;
captions.add_session(app, user, &chunk.id, vec![entry]).await?;
}
tracing::debug!(
collection = %self.collection,
video.source = %clip.source,
video.chunk = chunk.index,
video.start_ms = chunk.start_ms,
video.end_ms = chunk.end_ms,
"video chunk indexed"
);
indexed.chunks.push(chunk);
Ok(())
}
async fn record_graph(
&self,
graph: &GraphMemoryService,
clip: &ClipSource,
chunks: &[VideoChunk],
) -> Result<()> {
let (app, user) = (clip.scope.app_name.as_str(), clip.scope.user_id.as_str());
let observations = chunks
.iter()
.map(|c| format!("[{}] {} ({})", c.time_range(), c.caption, c.id))
.collect();
let labels: BTreeSet<String> =
chunks.iter().flat_map(|c| c.labels.iter().map(|l| l.to_lowercase())).collect();
let mut entities = vec![CreateEntityInput {
name: clip.source.clone(),
entity_type: "vision.clip".to_string(),
observations,
}];
entities.extend(labels.iter().map(|label| CreateEntityInput {
name: label.clone(),
entity_type: "vision.label".to_string(),
observations: Vec::new(),
}));
graph.create_entities(app, user, entities).await?;
if !labels.is_empty() {
let relations = labels
.into_iter()
.map(|label| CreateRelationInput {
source: clip.source.clone(),
relation_type: "observed".to_string(),
target: label,
})
.collect();
graph.create_relations(app, user, relations).await?;
}
Ok(())
}
pub async fn retrieve(
&self,
scope: &Scope,
question: &str,
query: &AnalystQuery,
) -> Result<Vec<ChunkHit>> {
let vector = self.embedder.embed(question).await.map_err(rag)?;
let filter = query.compile();
let found = self
.store
.search(
&self.collection,
scope,
&VectorQuery::new(&vector, query.top_k).with_filter(&filter),
)
.await
.map_err(rag)?;
found
.iter()
.map(|r: &SearchResult| {
Ok(ChunkHit { chunk: VideoChunk::from_chunk(&r.chunk)?, score: r.score })
})
.collect()
}
pub async fn ask(
&self,
cx: &VisionCx,
scope: &Scope,
question: &str,
query: &AnalystQuery,
) -> Result<AnalystAnswer> {
cx.check()?;
let mut usage = VideoUsage::default();
let expanded = match (&self.answerer, self.expand_questions) {
(Some(answerer), true) => {
Some(self.hypothetical(cx, answerer, question, &mut usage).await?)
}
_ => None,
};
let search = match &expanded {
Some(caption) => format!("{question} {caption}"),
None => question.to_string(),
};
let retrieved = self.retrieve(scope, &search, query).await?;
let mut answer = AnalystAnswer {
question: question.to_string(),
answer: String::new(),
citations: Vec::new(),
retrieved,
expanded,
supported: None,
usage,
answered_by: None,
};
match &self.answerer {
None => match answer.retrieved.first() {
Some(hit) => {
answer.answer = format!("{}: {}", hit.chunk.time_range(), hit.chunk.caption);
answer.citations.push(hit.chunk.clone());
}
None => {
answer.answer = "No chunk of the video is indexed for this question.".into()
}
},
Some(answerer) => self.answer(cx, answerer, &mut answer).await?,
}
let cited: Vec<&str> = answer.citations.iter().map(|c| c.id.as_str()).collect();
let sources: BTreeSet<&str> =
answer.retrieved.iter().map(|h| h.chunk.source.as_str()).collect();
cx.audit(
AuditRecordDraft::new(kind(), VISION_ANALYST_ASKED)
.outcome(AuditRecordOutcome::Succeeded)
.payload_ref(Digest::of(question.as_bytes()).to_string())
.ext("collection", json!(self.collection))
.ext("scope", json!(scope.as_str()))
.ext("sources", json!(sources))
.ext("retrieved", json!(answer.retrieved.len()))
.ext("cited", json!(cited))
.ext("answeredBy", json!(answer.answered_by))
.ext("costMicroUsd", json!(answer.usage.cost_micro_usd)),
)
.await?;
Ok(answer)
}
async fn answer(
&self,
cx: &VisionCx,
answerer: &Answerer,
answer: &mut AnalystAnswer,
) -> Result<()> {
let mut shown: Vec<&VideoChunk> = answer.retrieved.iter().map(|h| &h.chunk).collect();
shown.sort_by(|a, b| (a.source.as_str(), a.start_ms).cmp(&(b.source.as_str(), b.start_ms)));
let several = shown.iter().map(|c| c.source.as_str()).collect::<BTreeSet<_>>().len() > 1;
let lines: Vec<String> = shown
.iter()
.enumerate()
.map(|(i, c)| {
let clip = if several { format!(" of {}", c.source) } else { String::new() };
format!("[{}] {}{clip}: {}", i + 1, c.time_range(), c.caption)
})
.collect();
let prompt = format!(
"You answer questions about a video from captions of its chunks. Each line is one \
chunk: its number, its time range from the start of the video, and its caption.\n\n\
{}\n\nQuestion: {}\n\nAnswer with JSON only: {{\"answer\": <the answer in words, \
naming the time>, \"supported\": <true when the captions show what the question \
asks, false when none does>, \"chunks\": [<the numbers of the chunks that show it, \
the one where it is first seen first; when no caption shows it, the closest \
matches>]}}. Cite only chunks listed above. The captions come from a model that saw \
a few frames of each chunk and can be imprecise about details such as positions \
and colours, so a caption that describes the asked event in other words or \
approximately shows it.",
if lines.is_empty() { "(no chunks)".to_string() } else { lines.join("\n") },
answer.question
);
let schema = json!({
"type": "object",
"properties": {
"answer": {"type": "string"},
"supported": {"type": "boolean"},
"chunks": {"type": "array", "items": {"type": "integer"}}
},
"required": ["answer", "supported", "chunks"]
});
let root = Self::text_call(cx, answerer, prompt, schema, &mut answer.usage).await?;
answer.answer =
root.get("answer").and_then(Value::as_str).unwrap_or_default().trim().to_string();
answer.supported = root.get("supported").and_then(Value::as_bool);
for n in root.get("chunks").and_then(Value::as_array).into_iter().flatten() {
let Some(n) =
n.as_u64().or_else(|| n.as_str()?.trim().trim_matches(['[', ']']).parse().ok())
else {
continue;
};
let Some(chunk) =
usize::try_from(n).ok().and_then(|n| n.checked_sub(1)).and_then(|i| shown.get(i))
else {
tracing::debug!(video.cited = n, "the answer cited a chunk it was not shown");
continue;
};
if !answer.citations.iter().any(|c| c.id == chunk.id) {
answer.citations.push((*chunk).clone());
}
}
answer.answered_by = Some(answerer.descriptor.id.to_string());
Ok(())
}
async fn hypothetical(
&self,
cx: &VisionCx,
answerer: &Answerer,
question: &str,
usage: &mut VideoUsage,
) -> Result<String> {
let prompt = format!(
"A question about a video: {question}\n\nThe video is searched by captions of its \
chunks, each written by a model that watched that chunk. You do not see the \
video: write the caption that the chunk showing the answer would most likely \
have, in the same video, as one sentence naming what is seen and what happens, in \
plain words. Answer with JSON only: {{\"caption\": <the sentence>}}."
);
let schema = json!({
"type": "object",
"properties": {"caption": {"type": "string"}},
"required": ["caption"]
});
let root = Self::text_call(cx, answerer, prompt, schema, usage).await?;
Ok(root.get("caption").and_then(Value::as_str).unwrap_or_default().trim().to_string())
}
async fn text_call(
cx: &VisionCx,
answerer: &Answerer,
prompt: String,
schema: Value,
usage: &mut VideoUsage,
) -> Result<Value> {
let bound = answerer
.descriptor
.token_estimate
.map(|e| e.estimate_micro_usd(&[], prompt.len(), Some(answerer.max_output_tokens)));
let config = GenerateContentConfig {
max_output_tokens: i32::try_from(answerer.max_output_tokens).ok(),
response_schema: Some(schema),
..GenerateContentConfig::default()
};
let request = LlmRequest {
config: Some(config),
..LlmRequest::new(answerer.llm.name(), vec![Content::new("user").with_text(prompt)])
};
let backend = answerer.descriptor.id.to_string();
let ModelAnswer { text, usage: reported } =
generate(cx, &answerer.llm, bound, request, &backend).await?;
add_usage(usage, &answerer.descriptor, reported.as_ref());
extract_json(&text).ok_or_else(|| VisionError::Parse {
backend,
message: format!("the answer holds no JSON: {text}"),
})
}
pub async fn chunks(&self, scope: &Scope, source: &str) -> Result<Vec<VideoChunk>> {
let mut chunks = self
.stored(scope, source)
.await?
.iter()
.map(VideoChunk::from_chunk)
.collect::<Result<Vec<_>>>()?;
chunks.sort_by_key(|c| (c.start_ms, c.end_ms));
Ok(chunks)
}
async fn stored(&self, scope: &Scope, source: &str) -> Result<Vec<Chunk>> {
let filter = AnalystQuery::top(0).source(source).compile();
let n = self.store.count(&self.collection, scope, Some(&filter)).await.map_err(rag)?;
if n == 0 {
return Ok(Vec::new());
}
let mut axis = vec![0.0; self.embedder.dimensions()];
if let Some(first) = axis.first_mut() {
*first = 1.0;
}
let top_k = usize::try_from(n).unwrap_or(usize::MAX);
let found = self
.store
.search(&self.collection, scope, &VectorQuery::new(&axis, top_k).with_filter(&filter))
.await
.map_err(rag)?;
Ok(found.into_iter().map(|r| r.chunk).collect())
}
async fn count(&self, scope: &Scope, source: &str) -> Result<u64> {
let filter = AnalystQuery::top(0).source(source).compile();
self.store.count(&self.collection, scope, Some(&filter)).await.map_err(rag)
}
pub async fn erase_source(
&self,
cx: &VisionCx,
scope: &Scope,
source: &str,
) -> Result<ClipErasure> {
if cx.cancellation().is_cancelled() {
return Err(VisionError::Cancelled);
}
let filter = AnalystQuery::top(0).source(source).compile();
let raw = self.stored(scope, source).await?;
let stored = raw.iter().map(VideoChunk::from_chunk).collect::<Result<Vec<_>>>()?;
let found: BTreeMap<String, (String, String)> = raw
.iter()
.filter_map(|c| {
let app = c.metadata.get(keys::APP)?;
let user = c.metadata.get(keys::USER)?;
Some((c.id.clone(), (app.clone(), user.clone())))
})
.collect();
let chunks =
self.store.delete_where(&self.collection, scope, &filter).await.map_err(rag)?;
let mut erasure =
ClipErasure { source: source.to_string(), chunks, ..ClipErasure::default() };
if let Some(captions) = &self.captions {
for (id, (app, user)) in &found {
erasure.captions += captions.delete_memory(app, user, id).await?;
}
}
let users: BTreeSet<&(String, String)> = found.values().collect();
if let Some(graph) = &self.graph {
for (app, user) in &users {
erasure.entities +=
graph.delete_entities(app, user, vec![source.to_string()]).await?.len();
}
}
if let Some(keyframes) = &self.keyframes {
if let Some(visual) = &keyframes.visual {
erasure.visual_chunks = visual.erase_source(cx, scope, source).await?.chunks;
}
let mut locators: Vec<ArtifactLocator> = stored
.iter()
.filter_map(|c| c.keyframe.as_ref().map(|k| k.artifact.clone()))
.collect();
locators.sort_by_key(artifact_uri);
locators.dedup();
for locator in locators {
let deleted = keyframes
.artifacts
.delete(DeleteRequest {
app_name: locator.app_name.clone(),
user_id: locator.user_id.clone(),
session_id: locator.session_id.clone(),
file_name: locator.file_name.clone(),
version: None,
})
.await;
match deleted {
Ok(()) => {}
Err(err) if err.category == ErrorCategory::NotFound => {}
Err(err) => return Err(err.into()),
}
erasure.keyframes.push(locator);
}
}
cx.audit(
AuditRecordDraft::new(kind(), VISION_ANALYST_ERASED)
.outcome(AuditRecordOutcome::Succeeded)
.payload_ref(source.to_string())
.ext("collection", json!(self.collection))
.ext("scope", json!(scope.as_str()))
.ext("source", json!(source))
.ext("chunks", json!(erasure.chunks))
.ext("captions", json!(erasure.captions))
.ext("entities", json!(erasure.entities))
.ext("keyframes", json!(erasure.keyframes))
.ext("visualChunks", json!(erasure.visual_chunks)),
)
.await?;
tracing::info!(
collection = %self.collection,
video.source = %source,
video.chunks = erasure.chunks,
video.captions = erasure.captions,
"video analyst erased a clip"
);
Ok(erasure)
}
}
fn merge_usage(total: &mut VideoUsage, call: &VideoUsage) {
total.frames += call.frames;
total.requests += call.requests;
for (sum, add) in [
(&mut total.input_tokens, call.input_tokens),
(&mut total.output_tokens, call.output_tokens),
] {
if let Some(add) = add {
*sum.get_or_insert(0) += add;
}
}
total.cost_micro_usd = match (total.cost_micro_usd, call.cost_micro_usd) {
(Some(sum), Some(cost)) => Some(sum.saturating_add(cost)),
_ => None,
};
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chunks_round_trip_through_the_store_metadata() {
let at = DateTime::from_timestamp(1_794_981_630, 0).unwrap();
let chunk = VideoChunk {
id: VideoChunk::id_for("cam", 30_000, 60_000),
source: "cam".into(),
index: 1,
start_ms: 30_000,
end_ms: 60_000,
caption: "A forklift stops at aisle 2.".into(),
labels: vec!["forklift".into(), "aisle 2".into()],
keyframe: None,
clip: None,
captured_at: Some(at),
run: Some("run-1".into()),
describer: Some("qwen3-vl-4b-instruct".into()),
};
let stored = chunk.to_chunk(vec![1.0, 0.0], &MediaScope::new("a", "u", "s")).unwrap();
assert_eq!(stored.metadata[keys::DAY], "2026-11-18");
assert_eq!(VideoChunk::from_chunk(&stored).unwrap(), chunk);
assert!(chunk.contains(30_000) && chunk.contains(59_999) && !chunk.contains(60_000));
assert_eq!(chunk.time_range(), "0:30.0–1:00.0");
assert_ne!(chunk.id, VideoChunk::id_for("cam", 30_000, 60_001));
}
#[test]
fn costs_scale_to_an_hour_and_unknown_costs_stay_unknown() {
let mut clip = IndexedClip {
source: "cam".into(),
chunks: Vec::new(),
duration_ms: 900_000,
frames_read: 0,
usage: VideoUsage { cost_micro_usd: Some(0), ..VideoUsage::default() },
};
merge_usage(
&mut clip.usage,
&VideoUsage {
frames: 6,
requests: 1,
cost_micro_usd: Some(5_000),
..VideoUsage::default()
},
);
assert_eq!(clip.cost_per_hour_micro_usd(), Some(20_000));
merge_usage(
&mut clip.usage,
&VideoUsage { frames: 6, requests: 1, cost_micro_usd: None, ..VideoUsage::default() },
);
assert_eq!(
(clip.usage.frames, clip.usage.requests, clip.cost_per_hour_micro_usd()),
(12, 2, None)
);
}
}