feat: add cluster-agent endpoint, fix OCR labeling, fix ingestion blocking

- Add POST /api/v1/file/:file_uuid/cluster-agent endpoint for on-demand face clustering
- Fix OCR chunks being labeled as ASRX: use ChunkType.as_str() instead of {:?}
- Rule 1 now deletes old chunks before re-inserting to avoid stale data
- Add fallback face_traced.json when store_traced_faces.py fails
- ingestion_complete now handles status='error' to unblock jobs
- VLM describe tool now uses trace_id with pre-extracted face crops
This commit is contained in:
Accusys
2026-07-20 21:48:27 +08:00
parent 244af51edf
commit 7dc910e2de
8 changed files with 654 additions and 57 deletions
+25 -47
View File
@@ -824,7 +824,7 @@ pub struct PostgresCache {
#[derive(Debug, serde::Serialize, sqlx::FromRow)]
pub struct SemanticSearchResult {
pub id: i32,
pub file_uuid: Option<String>, // Added for global search
pub file_uuid: Option<String>,
pub scene_order: i32,
pub start_frame: i64,
pub end_frame: i64,
@@ -836,6 +836,7 @@ pub struct SemanticSearchResult {
pub metadata: Option<serde_json::Value>,
pub similarity: Option<f64>,
pub content: Option<serde_json::Value>,
pub chunk_type: String,
}
/// Result structure for child chunks
@@ -2519,7 +2520,8 @@ impl PostgresDb {
text_content, \
metadata, \
(1 - (embedding <=> $1::vector)) as similarity, \
content \
content, \
COALESCE(chunk_type, 'sentence') as chunk_type \
FROM {} \
WHERE file_uuid = $2 AND chunk_type IN ('sentence', 'story_parent', 'llm_parent') AND embedding IS NOT NULL \
ORDER BY embedding <=> $1::vector \
@@ -2557,7 +2559,8 @@ impl PostgresDb {
text_content, \
metadata, \
(1 - (embedding <=> $1::vector)) as similarity, \
content \
content, \
COALESCE(chunk_type, 'sentence') as chunk_type \
FROM {} \
WHERE chunk_type IN ('sentence', 'story_parent', 'llm_parent') AND embedding IS NOT NULL \
ORDER BY embedding <=> $1::vector \
@@ -2590,7 +2593,8 @@ impl PostgresDb {
text_content as text_content, \
metadata, \
1.0::float8 as similarity, \
content \
content, \
COALESCE(chunk_type, 'sentence') as chunk_type \
FROM {} \
WHERE file_uuid = $1 AND chunk_id = $2 AND embedding IS NOT NULL \
LIMIT 1",
@@ -2622,7 +2626,8 @@ impl PostgresDb {
text_content as text_content, \
metadata, \
1.0::float8 as similarity, \
content \
content, \
COALESCE(chunk_type, 'sentence') as chunk_type \
FROM {} \
WHERE file_uuid = $1 AND chunk_id = $2 \
LIMIT 1",
@@ -2885,7 +2890,7 @@ impl PostgresDb {
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
) -> Result<()> {
let table = schema::table_name("chunk");
let ct_str = format!("{:?}", chunk.chunk_type).to_lowercase();
let ct_str = chunk.chunk_type.as_str();
let fps = chunk.fps;
let start_time = chunk.start_frame as f64 / chunk.fps;
let end_time = chunk.end_frame as f64 / chunk.fps;
@@ -3419,45 +3424,18 @@ impl PostgresDb {
let like = format!("%{}%", query.replace('%', "%%"));
use sqlx::Row;
// Check if query contains CJK characters
let has_cjk = query.chars().any(|c| {
('\u{4E00}'..='\u{9FFF}').contains(&c)
|| ('\u{3040}'..='\u{309F}').contains(&c)
|| ('\u{30A0}'..='\u{30FF}').contains(&c)
|| ('\u{AC00}'..='\u{D7AF}').contains(&c)
});
let sql = if has_cjk {
// CJK/Korean: use ILIKE position-based ranking
format!(
"SELECT chunk_id, file_uuid, chunk_type, text_content, start_time, end_time, \
(1.0 - (POSITION(LOWER($1) IN LOWER(text_content))::float8 / NULLIF(LENGTH(text_content), 0)::float8))::float8 as score \
FROM {} \
WHERE text_content ILIKE $2 AND text_content != '' \
{}\
ORDER BY score DESC \
LIMIT $3",
table,
if file_uuid.is_some() { "AND file_uuid = $4 " } else { "" }
)
} else {
// English: use PostgreSQL full-text search
format!(
"SELECT chunk_id, file_uuid, chunk_type, text_content, start_time, end_time, \
CASE \
WHEN to_tsvector('english', text_content) @@ plainto_tsquery('english', $1) \
THEN ts_rank(to_tsvector('english', text_content), plainto_tsquery('english', $1))::float8 \
ELSE 0.1::float8 \
END as score \
FROM {} \
WHERE text_content ILIKE $2 AND text_content != '' \
{}\
ORDER BY score DESC \
LIMIT $3",
table,
if file_uuid.is_some() { "AND file_uuid = $4 " } else { "" }
)
};
// Simple LIKE-based matching (no FTS stemming)
let sql = format!(
"SELECT chunk_id, file_uuid, chunk_type, text_content, start_time, end_time, \
(1.0 - (POSITION(LOWER($1) IN LOWER(text_content))::float8 / NULLIF(LENGTH(text_content), 0)::float8))::float8 as score \
FROM {} \
WHERE text_content ILIKE $2 AND text_content != '' \
{}\
ORDER BY score DESC \
LIMIT $3",
table,
if file_uuid.is_some() { "AND file_uuid = $4 " } else { "" }
);
let rows = if let Some(u) = file_uuid {
sqlx::query(&sql)
@@ -4309,7 +4287,7 @@ impl PostgresDb {
pub async fn store_chunk(&self, chunk: &crate::core::chunk::types::Chunk) -> Result<()> {
let table = schema::table_name("chunk");
let ct_str = format!("{:?}", chunk.chunk_type).to_lowercase();
let ct_str = chunk.chunk_type.as_str();
let start_time = chunk.start_frame as f64 / chunk.fps;
let end_time = chunk.end_frame as f64 / chunk.fps;
sqlx::query(&format!(
@@ -4550,7 +4528,7 @@ impl Database for PostgresDb {
impl crate::core::db::ChunkStore for PostgresDb {
async fn store_chunk(&self, chunk: &crate::core::chunk::types::Chunk) -> Result<()> {
let table = schema::table_name("chunk");
let ct_str = format!("{:?}", chunk.chunk_type).to_lowercase();
let ct_str = chunk.chunk_type.as_str();
let start_time = chunk.start_frame as f64 / chunk.fps;
let end_time = chunk.end_frame as f64 / chunk.fps;
sqlx::query(&format!(