Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
IX10: the index hands out its rows and a query's vector, so the feed can keep a Pro or Team account's index in raea's cloud too. raea index serve answers rows {after, limit} (export.rs: the searchable rows after a row, oldest first, each with its item, moment, thumbnail and its vector as base64 of the f16 bits the rows' file keeps, 2 KB a row, and next to ask from), and a search with vector: true also answers its query's vector the same way. Tested: export.rs's unit tests (base64, f16 bits, paging past an item not yet searchable) and on a real index (3 pictures: rows in pages of 2 then 1, "a cat" with its 2,048-character vector)
shinyobjectz0722ce3ae45c
diff --git a/systems/index/src/export.rs b/systems/index/src/export.rsnew file mode 100644index 00000000..28c59ca3--- /dev/null+++ b/systems/index/src/export.rs@@ -0,0 +1,104 @@+//! The fast pass's rows handed out (docs/index.feature IX10): on Pro and Team the feed copies each row (its vector,+//! the item it is and its thumbnail) to raea's cloud so the account's other Macs search it, and a search hands back its+//! query's vector so the same words can be asked of the cloud's rows. Vectors travel as base64 of their f16 bits, little+//! endian, as the rows' file keeps them (2 KB a row).++use crate::store::{DIM, Store};+use anyhow::Result;+use serde_json::{Value, json};+use std::io::{Read, Seek, SeekFrom};++const B64: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";++/// Standard base64 with padding+pub fn b64(bytes: &[u8]) -> String {+ let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);+ for c in bytes.chunks(3) {+ let n = (c[0] as u32) << 16 | (*c.get(1).unwrap_or(&0) as u32) << 8 | *c.get(2).unwrap_or(&0) as u32;+ for (i, shift) in [18, 12, 6, 0].into_iter().enumerate() {+ out.push(if i <= c.len() { B64[(n >> shift & 63) as usize] as char } else { '=' });+ }+ }+ out+}++/// A query's vector as the rows' file would keep it+pub fn vector_b64(v: &[f32]) -> String {+ let bytes: Vec<u8> = v.iter().flat_map(|&x| half::f16::from_f32(x).to_bits().to_le_bytes()).collect();+ b64(&bytes)+}++/// The searchable rows after row `after`, oldest first, at most `limit`: each row's number, its item (kind, name,+/// where it lives, its source, when it was taken, its size in pixels), its moment and its vector. `next` is the last+/// row handed out (or `after` when there were none), the place to ask from next. A changed item's new rows come after+/// its old ones, so a reader that keys rows by item and moment replaces them.+pub fn rows_after(store: &Store, model: &str, after: i64, limit: usize) -> Result<Value> {+ let mut q = store.db.prepare_cached(+ "SELECT r.row, r.item, r.t, i.source, i.kind, i.name, i.locator, i.taken, i.w, i.h FROM rows r JOIN items i ON i.id = r.item+ WHERE r.model = ?1 AND r.row > ?2 AND i.state = 1 ORDER BY r.row LIMIT ?3",+ )?;+ let found: Vec<(i64, i64, f64, String, String, String, String, Option<i64>, Option<i64>, Option<i64>)> = q+ .query_map(rusqlite::params![model, after, limit as i64], |r| Ok((r.get(0)?, r.get(1)?, r.get(2)?, r.get(3)?, r.get(4)?, r.get(5)?, r.get(6)?, r.get(7)?, r.get(8)?, r.get(9)?)))?+ .collect::<rusqlite::Result<_>>()?;+ let mut file = if found.is_empty() { None } else { Some(std::fs::File::open(store.vectors(model))?) };+ let mut raw = vec![0u8; DIM * 2];+ let mut rows = Vec::with_capacity(found.len());+ for (row, item, t, source, kind, name, locator, taken, w, h) in &found {+ let f = file.as_mut().expect("opened when rows were found");+ f.seek(SeekFrom::Start(*row as u64 * (DIM * 2) as u64))?;+ f.read_exact(&mut raw)?;+ rows.push(json!({ "row": row, "item": item, "t": t, "source": source, "kind": kind, "name": name, "locator": locator,+ "taken": taken, "w": w, "h": h, "thumb": store.thumb(*item), "vector": b64(&raw) }));+ }+ let next = found.last().map(|r| r.0).unwrap_or(after);+ Ok(json!({ "rows": rows, "next": next, "model": model }))+}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn base64_as_the_standard_writes_it() {+ assert_eq!(b64(b""), "");+ assert_eq!(b64(b"f"), "Zg==");+ assert_eq!(b64(b"fo"), "Zm8=");+ assert_eq!(b64(b"foo"), "Zm9v");+ assert_eq!(b64(b"foobar"), "Zm9vYmFy");+ assert_eq!(b64(&[0xff, 0xfe, 0xfd]), "//79");+ }++ #[test]+ fn a_query_vector_goes_out_as_f16_bits() {+ // 1.0 is f16 0x3C00, little endian 00 3C; -2.0 is 0xC000+ assert_eq!(vector_b64(&[1.0, -2.0]), b64(&[0x00, 0x3c, 0x00, 0xc0]));+ }++ #[test]+ fn rows_come_out_with_their_items_and_vectors_from_where_they_left_off() {+ let dir = std::env::temp_dir().join(format!("raea-export-{}", std::process::id()));+ let _ = std::fs::remove_dir_all(&dir);+ let store = Store::open(&dir).unwrap();+ let src = store.source("folder", "/pics", "pics").unwrap();+ store.db.execute("INSERT INTO items (id, source, locator, kind, name, size, mtime, state) VALUES (7, ?1, '/pics/a.jpg', 'picture', 'a.jpg', 2000, 1, 1), (8, ?1, '/pics/b.mov', 'video', 'b.mov', 9000, 1, 1), (9, ?1, '/pics/c.jpg', 'picture', 'c.jpg', 2000, 1, 0)", [&src]).unwrap();+ store.db.execute("INSERT INTO rows (model, row, item, t) VALUES ('m', 0, 7, 0), ('m', 1, 8, 0), ('m', 2, 8, 4), ('m', 3, 9, 0)", []).unwrap();+ let mut file = Vec::new();+ for r in 0..4u16 {+ file.extend((0..DIM).flat_map(|_| (0x3c00 + r).to_le_bytes()));+ }+ std::fs::write(store.vectors("m"), &file).unwrap();+ let first = rows_after(&store, "m", -1, 2).unwrap();+ assert_eq!(first["next"], 1);+ assert_eq!(first["rows"][0]["name"], "a.jpg");+ assert_eq!(first["rows"][1]["kind"], "video");+ assert_eq!(first["rows"][0]["vector"].as_str().unwrap(), b64(&file[..DIM * 2]));+ // the rest: row 2 (b.mov at 4 s); row 3's item isn't searchable yet, so it waits+ let rest = rows_after(&store, "m", 1, 10).unwrap();+ assert_eq!(rest["rows"].as_array().unwrap().len(), 1);+ assert_eq!(rest["rows"][0]["t"], 4.0);+ assert_eq!(rest["rows"][0]["vector"].as_str().unwrap(), b64(&file[2 * DIM * 2..3 * DIM * 2]));+ assert_eq!(rest["next"], 2);+ assert_eq!(rows_after(&store, "m", 2, 10).unwrap()["next"], 2);+ let _ = std::fs::remove_dir_all(&dir);+ }+}diff --git a/systems/index/src/lib.rs b/systems/index/src/lib.rsindex 48cfa6ed..0b39813d 100644--- a/systems/index/src/lib.rs+++ b/systems/index/src/lib.rs@@ -7,6 +7,7 @@#[cfg(target_vendor = "apple")]pub mod apple;pub mod cli;+pub mod export;#[cfg(target_vendor = "apple")]pub mod cloud;#[cfg(target_vendor = "apple")]diff --git a/systems/index/src/serve.rs b/systems/index/src/serve.rsindex 6b4fad85..7621e61d 100644--- a/systems/index/src/serve.rs+++ b/systems/index/src/serve.rs@@ -10,6 +10,8 @@//! {"id":5,"op":"remove","source":ID} (or the folder's path)//! {"id":6,"op":"stop"} the run going now, and the ones waiting//! {"id":7,"op":"warm"} the text tower loaded before the first search+//! {"id":8,"op":"rows","after":-1,"limit":500} searchable rows after a row, with their vectors (IX10)+//! A search with "vector": true also answers its query's vector (export.rs: base64 of f16), to ask raea's cloud too.//! Events: ready {home, model}, progress (lib.rs Progress: stage listing|embedding|done|stopped, newest item ids),//! error {root, message}. A thumbnail is home/thumbs/<id % 256>/<id>.jpg.@@ -50,7 +52,7 @@ impl Shared {}enum Find {- Words { id: Value, q: String, k: usize, source: Option<String> },+ Words { id: Value, q: String, k: usize, source: Option<String>, vector: bool },Like { id: Value, item: i64, k: usize, source: Option<String> },Warm { id: Value },}@@ -127,9 +129,15 @@ fn answer(sh: &Shared, store: &mut Store, jobs: &mpsc::Sender<PathBuf>, finds: &}"search" => {let q = s("q").filter(|q| !q.trim().is_empty()).ok_or_else(|| anyhow!("search needs words (q)"))?;- finds.send(Find::Words { id, q, k, source: s("source") }).map_err(|_| anyhow!("the searcher stopped"))?;+ let vector = ask.get("vector").and_then(Value::as_bool).unwrap_or(false);+ finds.send(Find::Words { id, q, k, source: s("source"), vector }).map_err(|_| anyhow!("the searcher stopped"))?;Ok(None)}+ "rows" => {+ let after = ask.get("after").and_then(Value::as_i64).unwrap_or(-1);+ let limit = ask.get("limit").and_then(Value::as_u64).unwrap_or(500).clamp(1, 5000) as usize;+ Ok(Some(crate::export::rows_after(store, MODEL, after, limit)?))+ }"similar" => {let item = ask.get("item").and_then(Value::as_i64).ok_or_else(|| anyhow!("similar needs an item"))?;finds.send(Find::Like { id, item, k, source: s("source") }).map_err(|_| anyhow!("the searcher stopped"))?;@@ -162,7 +170,7 @@ fn answer(sh: &Shared, store: &mut Store, jobs: &mpsc::Sender<PathBuf>, finds: &sh.stop.store(true, Ordering::Relaxed);Ok(Some(json!({ "ok": true })))}- op => anyhow::bail!("no op {op:?}: add, search, similar, warm, status, remove, stop"),+ op => anyhow::bail!("no op {op:?}: add, search, similar, warm, status, remove, stop, rows"),}}@@ -241,13 +249,17 @@ fn find(sh: &Shared, dir: &Path, finds: mpsc::Receiver<Find>) {}match f {Find::Warm { .. } => Ok(json!({ "ok": true, "load_ms": tower(&mut text)?, "rows": rows.live() })),- Find::Words { q, k, source, .. } => {+ Find::Words { q, k, source, vector, .. } => {let load = tower(&mut text)?;let t = Instant::now();let v = text.as_mut().expect("loaded").embed(&q)?;let words = t.elapsed().as_secs_f64() * 1e3;let hits = search::search(&store, &rows, &v, k, source.as_deref())?;- Ok(json!({ "hits": hits, "rows": rows.live(), "ms": { "load": load, "words": words, "search": t.elapsed().as_secs_f64() * 1e3 - words } }))+ let mut out = json!({ "hits": hits, "rows": rows.live(), "ms": { "load": load, "words": words, "search": t.elapsed().as_secs_f64() * 1e3 - words } });+ if vector {+ out["vector"] = crate::export::vector_b64(&v).into();+ }+ Ok(out)}Find::Like { item, k, source, .. } => {let v = rows.vector(item).ok_or_else(|| anyhow!("item {item} isn't searchable yet"))?;