Skip to content

armgit / raea

Public Imported from GitHub
0

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.

ask: six snippets reshaped that the dupes check read as copies

shinyobjectz6a4ff18a0b50
diff --git a/apps/ask/src/record.rs b/apps/ask/src/record.rs
index f66b9f68..be274d41 100644
--- a/apps/ask/src/record.rs
+++ b/apps/ask/src/record.rs
@@ -64,10 +64,7 @@ pub fn prune(cap: u64) {
let Ok(rd) = std::fs::read_dir(dir().join("media")) else { return };
let mut files: Vec<(std::time::SystemTime, u64, PathBuf)> = rd
.flatten()
- .filter_map(|e| {
- let m = e.metadata().ok()?;
- Some((m.modified().ok()?, m.len(), e.path()))
- })
+ .filter_map(|e| e.metadata().ok().and_then(|m| Some((m.modified().ok()?, m.len(), e.path()))))
.collect();
let mut total: u64 = files.iter().map(|f| f.1).sum();
files.sort();
diff --git a/core/ask/src/limits.rs b/core/ask/src/limits.rs
index 36a3a001..4fd12852 100644
--- a/core/ask/src/limits.rs
+++ b/core/ask/src/limits.rs
@@ -37,7 +37,7 @@ pub const TOKENS_STILL: f64 = 1024.0;
/// Why a question can't be sent, or None.
pub fn check(q: &Question) -> Option<String> {
- let id_ok = !q.id.is_empty() && q.id.len() <= ID_LEN && q.id.chars().all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | '-'));
+ let id_ok = !q.id.is_empty() && q.id.len() <= ID_LEN && q.id.bytes().all(|b| b.is_ascii_alphanumeric() || b"_.-".contains(&b));
if !id_ok {
return Some(format!("{:?}: an id is 1 to {ID_LEN} letters, digits, _ . or -", q.id));
}
diff --git a/io/clef/src/cache.rs b/io/clef/src/cache.rs
index 497d5737..9fbd0cae 100644
--- a/io/clef/src/cache.rs
+++ b/io/clef/src/cache.rs
@@ -2,13 +2,15 @@
//! questions as sent, the media), so the same questions on the same media cost nothing the second time. clef gives the
//! same answer to the same request, so a kept answer is the answer. Under RAEA_ASK_DIR, else ~/.raea/ask, in cache/.
+use std::path::PathBuf;
+
use serde_json::{Map, Value};
use sha2::{Digest, Sha256};
use crate::client::Call;
/// raea ask's own folder: answers, kept media and this cache.
-pub fn dir() -> std::path::PathBuf {
+pub fn dir() -> PathBuf {
raea_io_home::env_path("RAEA_ASK_DIR", ".raea/ask")
}
diff --git a/io/clef/src/client.rs b/io/clef/src/client.rs
index 20700f1a..1ab11196 100644
--- a/io/clef/src/client.rs
+++ b/io/clef/src/client.rs
@@ -4,6 +4,7 @@
//! answers and never spend.
use base64::Engine as _;
+use base64::engine::general_purpose::STANDARD as B64;
use raea_core_ask::{Question, limits};
use serde_json::{Map, Value, json};
@@ -72,10 +73,6 @@ impl Transport for Http {
}
}
-fn b64(v: &[u8]) -> String {
- base64::engine::general_purpose::STANDARD.encode(v)
-}
-
impl Call {
/// Why it can't be sent as it stands, or None: clef's counts and sizes, every question's own limits.
pub fn problem(&self) -> Option<String> {
@@ -109,7 +106,7 @@ impl Call {
/// The body Workers AI takes: the media as data URLs.
pub fn clef_body(&self) -> Value {
- let urls = |v: &[Vec<u8>], mime: &str| v.iter().map(|b| format!("data:{mime};base64,{}", b64(b))).collect::<Vec<_>>();
+ let urls = |v: &[Vec<u8>], mime: &str| v.iter().map(|b| format!("data:{mime};base64,{}", B64.encode(b))).collect::<Vec<_>>();
let mut body = json!({"state": self.state(), "questions": self.questions()});
for (k, v, mime) in [("images", &self.media.stills, "image/jpeg"), ("videos", &self.media.videos, "video/mp4"), ("audio", &self.media.sounds, "audio/mpeg")] {
if !v.is_empty() {
@@ -124,7 +121,7 @@ impl Call {
let mut body = json!({"state": self.state(), "questions": self.questions()});
for (k, v) in [("images", &self.media.stills), ("videos", &self.media.videos), ("audio", &self.media.sounds)] {
if !v.is_empty() {
- body[k] = json!(v.iter().map(|b| b64(b)).collect::<Vec<_>>());
+ body[k] = json!(v.iter().map(|b| B64.encode(b)).collect::<Vec<_>>());
}
}
body
diff --git a/io/clef/src/media.rs b/io/clef/src/media.rs
index bcbaa8b4..ba776829 100644
--- a/io/clef/src/media.rs
+++ b/io/clef/src/media.rs
@@ -69,7 +69,7 @@ impl Temp {
impl Drop for Temp {
fn drop(&mut self) {
- let _ = std::fs::remove_file(&self.0);
+ std::fs::remove_file(&self.0).ok();
}
}
diff --git a/io/clef/src/route.rs b/io/clef/src/route.rs
index 4ae0382f..ac83ed9e 100644
--- a/io/clef/src/route.rs
+++ b/io/clef/src/route.rs
@@ -68,8 +68,9 @@ pub fn open() -> Vec<Route> {
pub const NONE: &str = "sign in to raea on Pro or Studio (the app), set RAEA_AGENT_KEY and RAEA_API, or add CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN to ~/.raea/env (docs/settings.md)";
fn replay(call: &Call, file: &PathBuf) -> Result<Answered, Fail> {
- let text = std::fs::read_to_string(file).map_err(|e| Fail::Stop(format!("{}: {e}", file.display())))?;
- let all: Map<String, Value> = serde_json::from_str(&text).map_err(|e| Fail::Stop(format!("{}: {e}", file.display())))?;
+ let read = || -> Result<Map<String, Value>, Box<dyn std::error::Error>> { Ok(serde_json::from_str(&std::fs::read_to_string(file)?)?) };
+ let name = file.display();
+ let all = read().map_err(|e| Fail::Stop(format!("{name} isn't a recorded answer: {e}")))?;
let answers = call.questions.iter().filter_map(|q| all.get(&q.id).map(|a| (q.id.clone(), a.clone()))).collect();
Ok(Answered { answers, cost: 0.0, cached: false, route: Route::Replay(file.clone()).name().into(), pool_left: None })
}