fix: Face processor now saves status and total_faces to face.json
This commit is contained in:
@@ -1629,6 +1629,359 @@ pub async fn exec_semantic_search(
|
||||
.to_string())
|
||||
}
|
||||
|
||||
/// Scene VLM semantic search — vector-first. Embeds the query and searches the
|
||||
/// Qdrant `_scenes` collection (768-dim, cosine). Returns scene cards with
|
||||
/// thumbnail_url, timestamp, and VLM description.
|
||||
pub async fn exec_scene_search(
|
||||
pool: &sqlx::PgPool,
|
||||
args: &serde_json::Value,
|
||||
) -> Result<String, String> {
|
||||
let query = args.get("query").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let file_uuid = args
|
||||
.get("file_uuid")
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.is_empty() && *s != "<nil>" && *s != "null");
|
||||
let limit = args
|
||||
.get("limit")
|
||||
.and_then(|v| v.as_i64())
|
||||
.unwrap_or(10)
|
||||
.min(50) as usize;
|
||||
|
||||
tracing::info!(
|
||||
"[exec_scene_search] query='{}', file_uuid={:?}, limit={}",
|
||||
query,
|
||||
file_uuid,
|
||||
limit
|
||||
);
|
||||
|
||||
if query.is_empty() {
|
||||
return Err("query is required".to_string());
|
||||
}
|
||||
|
||||
// Generate query embedding (vector-first)
|
||||
let embedder = Embedder::new("embeddinggemma-300m".to_string());
|
||||
let embedding = embedder.embed_query(query).await.map_err(|e| {
|
||||
tracing::error!("[exec_scene_search] Embedding failed: {}", e);
|
||||
format!("Embedding failed: {}", e)
|
||||
})?;
|
||||
|
||||
// Search Qdrant _scenes
|
||||
let qdrant = QdrantDb::new();
|
||||
let search_results = qdrant
|
||||
.search_scenes(&embedding, file_uuid, limit)
|
||||
.await
|
||||
.map_err(|e| {
|
||||
tracing::error!("[exec_scene_search] Qdrant search_scenes failed: {}", e);
|
||||
format!("Qdrant search failed: {}", e)
|
||||
})?;
|
||||
|
||||
tracing::info!(
|
||||
"[exec_scene_search] Qdrant returned {} hits",
|
||||
search_results.len()
|
||||
);
|
||||
|
||||
// Fetch file names for hits
|
||||
let videos = schema::table_name("videos");
|
||||
|
||||
let mut cards = Vec::new();
|
||||
for point in search_results.iter() {
|
||||
let payload = match point.get("payload") {
|
||||
Some(p) => p,
|
||||
None => continue,
|
||||
};
|
||||
|
||||
let hit_uuid = payload
|
||||
.get("file_uuid")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let scene_index = payload
|
||||
.get("scene_index")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0) as usize;
|
||||
let timestamp_sec = payload
|
||||
.get("timestamp_sec")
|
||||
.and_then(|v| v.as_f64())
|
||||
.unwrap_or(0.0);
|
||||
let vlm_description = payload
|
||||
.get("vlm_description")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let vlm_tags: Vec<String> = payload
|
||||
.get("vlm_tags")
|
||||
.and_then(|v| v.as_array())
|
||||
.map(|arr| {
|
||||
arr.iter()
|
||||
.filter_map(|v| v.as_str().map(|s| s.to_string()))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let score = point.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
|
||||
let file_name: String = sqlx::query_scalar(&format!(
|
||||
"SELECT COALESCE(v.file_name, '') FROM {} v WHERE v.file_uuid = $1",
|
||||
videos
|
||||
))
|
||||
.bind(&hit_uuid)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(|e| format!("PG query failed: {}", e))?
|
||||
.unwrap_or_default();
|
||||
|
||||
let description_preview = vlm_description.chars().take(150).collect::<String>();
|
||||
|
||||
cards.push(serde_json::json!({
|
||||
"card_type": "scene",
|
||||
"file_uuid": hit_uuid,
|
||||
"file_name": file_name,
|
||||
"scene_index": scene_index,
|
||||
"timestamp_sec": timestamp_sec,
|
||||
"thumbnail_url": format!(
|
||||
"/api/v1/file/{}/scenes/scene_{:03}.jpg",
|
||||
hit_uuid, scene_index
|
||||
),
|
||||
"score": score,
|
||||
"vlm_tags": vlm_tags,
|
||||
"vlm_description": description_preview,
|
||||
"source": "vlm_scene"
|
||||
}));
|
||||
}
|
||||
|
||||
Ok(serde_json::json!({
|
||||
"tool": "scene_search",
|
||||
"query": query,
|
||||
"count": cards.len(),
|
||||
"cards": cards
|
||||
})
|
||||
.to_string())
|
||||
}
|
||||
|
||||
/// Unified VLM search — searches BOTH scene VLM vectors (_scenes collection)
|
||||
/// and trace VLM summary vectors (main collection, chunk_type = vlm_summary)
|
||||
/// in a single call using one query embedding. Returns merged cards with a
|
||||
/// `card_type` field ("scene" | "face") so the agent can distinguish sources.
|
||||
pub async fn exec_vlm_search(
|
||||
pool: &sqlx::PgPool,
|
||||
args: &serde_json::Value,
|
||||
) -> Result<String, String> {
|
||||
let query = args.get("query").and_then(|v| v.as_str()).unwrap_or("");
|
||||
let file_uuid = args
|
||||
.get("file_uuid")
|
||||
.and_then(|v| v.as_str())
|
||||
.filter(|s| !s.is_empty() && *s != "<nil>" && *s != "null");
|
||||
let limit = args
|
||||
.get("limit")
|
||||
.and_then(|v| v.as_i64())
|
||||
.unwrap_or(10)
|
||||
.min(50) as usize;
|
||||
|
||||
tracing::info!(
|
||||
"[exec_vlm_search] query='{}', file_uuid={:?}, limit={}",
|
||||
query,
|
||||
file_uuid,
|
||||
limit
|
||||
);
|
||||
|
||||
if query.is_empty() {
|
||||
return Err("query is required".to_string());
|
||||
}
|
||||
|
||||
// Generate query embedding (vector-first, shared by scenes + traces)
|
||||
let embedder = Embedder::new("embeddinggemma-300m".to_string());
|
||||
let embedding = embedder.embed_query(query).await.map_err(|e| {
|
||||
tracing::error!("[exec_vlm_search] Embedding failed: {}", e);
|
||||
format!("Embedding failed: {}", e)
|
||||
})?;
|
||||
|
||||
// Search both VLM collections in parallel
|
||||
let qdrant = QdrantDb::new();
|
||||
let (scene_hits, trace_hits) = tokio::join!(
|
||||
qdrant.search_scenes(&embedding, file_uuid, limit),
|
||||
qdrant.search_trace_vlm(&embedding, file_uuid, limit)
|
||||
);
|
||||
|
||||
let scene_results = scene_hits.map_err(|e| {
|
||||
tracing::error!("[exec_vlm_search] Qdrant search_scenes failed: {}", e);
|
||||
format!("Qdrant scene search failed: {}", e)
|
||||
})?;
|
||||
let trace_results = trace_hits.map_err(|e| {
|
||||
tracing::error!("[exec_vlm_search] Qdrant search_trace_vlm failed: {}", e);
|
||||
format!("Qdrant trace search failed: {}", e)
|
||||
})?;
|
||||
|
||||
let videos = schema::table_name("videos");
|
||||
let mut cards = Vec::new();
|
||||
|
||||
let active_files: std::collections::HashSet<String> =
|
||||
sqlx::query_scalar(&format!("SELECT file_uuid FROM {}", videos))
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.map_err(|e| format!("PG query failed: {}", e))?
|
||||
.into_iter()
|
||||
.collect();
|
||||
|
||||
// Scene cards (_scenes collection)
|
||||
for point in scene_results.iter() {
|
||||
let payload = match point.get("payload") {
|
||||
Some(p) => p,
|
||||
None => continue,
|
||||
};
|
||||
let hit_uuid = payload
|
||||
.get("file_uuid")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
if !active_files.contains(&hit_uuid) {
|
||||
continue;
|
||||
}
|
||||
let scene_index = payload
|
||||
.get("scene_index")
|
||||
.and_then(|v| v.as_u64())
|
||||
.unwrap_or(0) as usize;
|
||||
let timestamp_sec = payload
|
||||
.get("timestamp_sec")
|
||||
.and_then(|v| v.as_f64())
|
||||
.unwrap_or(0.0);
|
||||
let vlm_description = payload
|
||||
.get("vlm_description")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
let vlm_tags: Vec<String> = payload
|
||||
.get("vlm_tags")
|
||||
.and_then(|v| v.as_array())
|
||||
.map(|arr| {
|
||||
arr.iter()
|
||||
.filter_map(|v| v.as_str().map(|s| s.to_string()))
|
||||
.collect()
|
||||
})
|
||||
.unwrap_or_default();
|
||||
let score = point.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
|
||||
let file_name: String = sqlx::query_scalar(&format!(
|
||||
"SELECT COALESCE(v.file_name, '') FROM {} v WHERE v.file_uuid = $1",
|
||||
videos
|
||||
))
|
||||
.bind(&hit_uuid)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(|e| format!("PG query failed: {}", e))?
|
||||
.unwrap_or_default();
|
||||
|
||||
cards.push(serde_json::json!({
|
||||
"card_type": "scene",
|
||||
"vlm_type": "scene",
|
||||
"file_uuid": hit_uuid,
|
||||
"file_name": file_name,
|
||||
"scene_index": scene_index,
|
||||
"timestamp_sec": timestamp_sec,
|
||||
"thumbnail_url": format!(
|
||||
"/api/v1/file/{}/scenes/scene_{:03}.jpg",
|
||||
hit_uuid, scene_index
|
||||
),
|
||||
"score": score,
|
||||
"vlm_tags": vlm_tags,
|
||||
"vlm_description": vlm_description.chars().take(150).collect::<String>(),
|
||||
"source": "vlm_scene"
|
||||
}));
|
||||
}
|
||||
|
||||
// Trace cards (main collection, chunk_type = vlm_summary)
|
||||
for point in trace_results.iter() {
|
||||
let payload = match point.get("payload") {
|
||||
Some(p) => p,
|
||||
None => continue,
|
||||
};
|
||||
let hit_uuid = payload
|
||||
.get("file_uuid")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
if !active_files.contains(&hit_uuid) {
|
||||
continue;
|
||||
}
|
||||
let trace_id = payload
|
||||
.get("trace_id")
|
||||
.and_then(|v| v.as_i64())
|
||||
.unwrap_or(0) as i32;
|
||||
let vlm_description = payload
|
||||
.get("text")
|
||||
.and_then(|v| v.as_str())
|
||||
.unwrap_or("")
|
||||
.to_string();
|
||||
|
||||
if trace_id <= 0 || hit_uuid.is_empty() {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Enrich with profile fields + identity via identity_bindings
|
||||
let (file_name, vlm_clothing, vlm_location, vlm_tags_arr, identity_name) =
|
||||
sqlx::query_as::<
|
||||
_,
|
||||
(
|
||||
String,
|
||||
Option<String>,
|
||||
Option<String>,
|
||||
Option<Vec<String>>,
|
||||
Option<String>,
|
||||
),
|
||||
>(
|
||||
"SELECT COALESCE(v.file_name, ''), tp.vlm_clothing, tp.vlm_location, tp.vlm_tags, \
|
||||
i.name \
|
||||
FROM trace_profiles tp \
|
||||
LEFT JOIN videos v ON v.file_uuid = tp.file_uuid \
|
||||
LEFT JOIN identity_bindings ib ON ib.identity_type = 'trace' \
|
||||
AND ib.identity_value = tp.trace_id::text \
|
||||
LEFT JOIN identities i ON i.id = ib.identity_id \
|
||||
WHERE tp.file_uuid = $1 AND tp.trace_id = $2",
|
||||
)
|
||||
.bind(&hit_uuid)
|
||||
.bind(trace_id)
|
||||
.fetch_optional(pool)
|
||||
.await
|
||||
.map_err(|e| format!("PG query failed: {}", e))?
|
||||
.unwrap_or_default();
|
||||
|
||||
let score = point.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
|
||||
cards.push(serde_json::json!({
|
||||
"card_type": "face",
|
||||
"vlm_type": "trace",
|
||||
"file_uuid": hit_uuid,
|
||||
"file_name": file_name,
|
||||
"trace_id": trace_id,
|
||||
"identity_name": identity_name,
|
||||
"start_frame": payload.get("start_frame").and_then(|v| v.as_i64()).unwrap_or(0),
|
||||
"end_frame": payload.get("end_frame").and_then(|v| v.as_i64()).unwrap_or(0),
|
||||
"timestamp_sec": payload.get("timestamp_sec").and_then(|v| v.as_f64()).unwrap_or(0.0),
|
||||
"thumbnail_url": format!("/api/v1/trace-thumb/{}/{}", hit_uuid, trace_id),
|
||||
"score": score,
|
||||
"vlm_clothing": vlm_clothing,
|
||||
"vlm_location": vlm_location,
|
||||
"vlm_tags": vlm_tags_arr.unwrap_or_default(),
|
||||
"vlm_description": vlm_description.chars().take(150).collect::<String>(),
|
||||
"source": "vlm_trace"
|
||||
}));
|
||||
}
|
||||
|
||||
// Sort merged cards by score desc, cap at limit
|
||||
cards.sort_by(|a, b| {
|
||||
let sa = a.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
let sb = b.get("score").and_then(|v| v.as_f64()).unwrap_or(0.0);
|
||||
sb.partial_cmp(&sa).unwrap_or(std::cmp::Ordering::Equal)
|
||||
});
|
||||
cards.truncate(limit);
|
||||
|
||||
Ok(serde_json::json!({
|
||||
"tool": "vlm_search",
|
||||
"query": query,
|
||||
"count": cards.len(),
|
||||
"cards": cards
|
||||
})
|
||||
.to_string())
|
||||
}
|
||||
|
||||
/// Search named face profiles by name — finds face_track TKG nodes whose label
|
||||
/// matches the given name, joined with trace_profiles for frame/key-image info.
|
||||
/// Returns cards with key_frame, key_face, frame_range, and time_range.
|
||||
|
||||
Reference in New Issue
Block a user