use std::cell::Cell;
use std::fs;
use std::io::{Read, Write};
use std::path::{Path, PathBuf};
use crate::store::{error::Error, fileio};
#[derive(Debug)]
pub struct Refusal(String);
impl std::fmt::Display for Refusal {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl std::error::Error for Refusal {}
pub fn io_refusal(message: String) -> std::io::Error {
std::io::Error::other(Refusal(message))
}
pub fn into_io(error: Error) -> std::io::Error {
match error {
Error::Value(message) => io_refusal(message),
Error::Io(error) => error,
other => std::io::Error::other(format!("{other:?}")),
}
}
pub fn setting(name: &str, default: u64, maximum: u64) -> Result<u64, Error> {
let Some(value) = std::env::var_os(name).filter(|s| !s.is_empty()) else {
return Ok(default);
};
value
.to_str()
.and_then(|s| s.parse::<u64>().ok())
.filter(|n| *n > 0 && *n <= maximum)
.ok_or_else(|| Error::Value(format!("{name} must be an integer in 1..{maximum}")))
}
pub fn check(name: &str, used: u64, maximum: u64) -> Result<(), Error> {
if used > maximum {
Err(Error::Value(format!(
"resource limit {name} exceeded: {used} > {maximum}; select a smaller batch or raise this setting"
)))
} else {
Ok(())
}
}
pub const DOCUMENTS: &str = "NEOSIAN_KNOWLEDGE_MAX_DOCUMENTS";
pub const BYTES: &str = "NEOSIAN_KNOWLEDGE_MAX_BYTES";
pub const TEMP: &str = "NEOSIAN_MAX_TEMP_BYTES";
pub const REPLICA: &str = "NEOSIAN_REPLICA_MAX_BYTES";
pub const FILE: &str = "NEOSIAN_MAX_FILE_BYTES";
pub const RESPONSE: &str = "NEOSIAN_MAX_RESPONSE_BYTES";
pub const SEARCH: &str = "NEOSIAN_SEARCH_MAX_CANDIDATES";
pub const PAGE: &str = "NEOSIAN_MAX_PAGE_BYTES";
pub const DRAFT_SECONDS: &str = "NEOSIAN_DRAFT_SECONDS";
pub const TURN_BYTES: &str = "NEOSIAN_MAX_TURN_BYTES";
pub const LEGACY_TURN_RECORDS: &str = "NEOSIAN_MAX_LEGACY_TURN_RECORDS";
pub const RECORD_TURN_BYTES: &str = "NEOSIAN_MAX_RECORD_TURN_BYTES";
pub fn ranged(name: &str, default: u64, minimum: u64, maximum: u64) -> Result<u64, Error> {
let Some(value) = std::env::var_os(name).filter(|s| !s.is_empty()) else {
return Ok(default);
};
value
.to_str()
.and_then(|s| s.parse::<u64>().ok())
.filter(|n| (minimum..=maximum).contains(n))
.ok_or_else(|| Error::Value(format!("{name} must be an integer in {minimum}..{maximum}")))
}
#[derive(Clone, Copy, Debug)]
pub struct Records {
pub draft_seconds: u64,
pub max_turn_bytes: u64,
pub max_record_turn_bytes: u64,
}
impl Records {
pub fn load() -> Result<Self, Error> {
Ok(Self {
draft_seconds: ranged(DRAFT_SECONDS, 86_400, 60, 2_592_000)?,
max_turn_bytes: ranged(TURN_BYTES, 64 << 20, 65_536, 1 << 30)?,
max_record_turn_bytes: ranged(RECORD_TURN_BYTES, 1 << 30, 65_536, 16 << 30)?,
})
}
}
#[derive(Clone, Copy)]
pub struct LegacyTurn {
pub bytes: u64,
pub records: u64,
}
impl LegacyTurn {
pub fn load() -> Result<Self, Error> {
Ok(Self {
bytes: Records::load()?.max_turn_bytes,
records: ranged(LEGACY_TURN_RECORDS, 8_192, 1, 65_536)?,
})
}
pub fn allows(self, bytes: u64, records: u64) -> bool {
bytes <= self.bytes && records <= self.records
}
pub fn check(self, turn: u64, id: &str, bytes: u64, records: u64) -> Result<(), Error> {
if bytes > self.bytes {
return Err(Error::Value(turn_too_long(turn, id, bytes, self.bytes)));
}
if records > self.records {
return Err(Error::Value(format!(
"resource limit {LEGACY_TURN_RECORDS} exceeded: turn {turn} of {id} holds {records} > {} records; a newer neosian reads this turn in parts",
self.records
)));
}
Ok(())
}
}
pub fn turn_too_long(turn: u64, conversation_id: &str, bytes: u64, maximum: u64) -> String {
format!(
"resource limit {TURN_BYTES} exceeded: turn {turn} of {conversation_id} holds {bytes} > {maximum} bytes; a newer neosian reads this turn in parts"
)
}
pub fn response_bytes() -> Result<u64, Error> {
setting(RESPONSE, 64 << 20, 1 << 30)
}
pub fn page_bytes() -> Result<u64, Error> {
setting(PAGE, 8 << 20, 1 << 30)
}
pub fn search_candidates() -> Result<usize, Error> {
Ok(setting(SEARCH, 100_000, 1_000_000)? as usize)
}
pub fn broad(maximum: usize, what: &str, narrow: &str) -> String {
format!("{BROAD}: more than {maximum} {what} hold a search term; narrow {narrow}")
}
pub const BROAD: &str = "resource limit NEOSIAN_SEARCH_MAX_CANDIDATES exceeded";
#[derive(Clone, Copy)]
pub struct Knowledge {
pub documents: u64,
pub bytes: u64,
}
impl Knowledge {
pub fn load() -> Result<Self, Error> {
Ok(Self {
documents: setting(DOCUMENTS, 10_000, 100_000)?,
bytes: setting(BYTES, 256 << 20, 2 << 30)?,
})
}
pub fn check(&self, documents: usize, bytes: usize) -> Result<(), Error> {
check(DOCUMENTS, documents as u64, self.documents)?;
check(BYTES, bytes as u64, self.bytes)
}
pub fn manifest(&self) -> usize {
(self.bytes.min(64 << 20)) as usize
}
}
pub fn file_bytes() -> Result<u64, Error> {
setting(FILE, 64 << 20, 1 << 30)
}
pub fn temp_bytes() -> Result<u64, Error> {
setting(TEMP, 512 << 20, 8 << 30)
}
pub fn read_file(path: &Path) -> Result<String, Error> {
let maximum = file_bytes()?;
check(FILE, fs::metadata(path)?.len(), maximum)?;
let mut bytes = Vec::new();
fs::File::open(path)?
.take(maximum + 1)
.read_to_end(&mut bytes)?;
check(FILE, bytes.len() as u64, maximum)?;
String::from_utf8(bytes).map_err(|_| {
std::io::Error::new(std::io::ErrorKind::InvalidData, "file is not UTF-8").into()
})
}
pub struct Scratch {
pub directory: PathBuf,
bytes: Cell<u64>,
maximum: u64,
}
impl Scratch {
pub fn new() -> Result<Self, Error> {
Self::beside(&std::env::temp_dir())
}
pub fn beside(parent: &Path) -> Result<Self, Error> {
let maximum = temp_bytes()?;
let directory = parent.join(format!(".neosian-stage-{}", uuid::Uuid::new_v4()));
fileio::private_builder().create(&directory)?;
Ok(Self {
directory,
bytes: Cell::new(0),
maximum,
})
}
pub fn remaining(&self) -> u64 {
self.maximum - self.bytes.get()
}
pub fn save(&self, text: &str) -> Result<PathBuf, Error> {
self.put(Path::new(&uuid::Uuid::new_v4().to_string()), text)
}
pub fn put(&self, relative: &Path, text: &str) -> Result<PathBuf, Error> {
let used = self
.bytes
.get()
.checked_add(text.len() as u64)
.ok_or_else(|| Error::Value("temporary byte count overflow".into()))?;
check(TEMP, used, self.maximum)?;
if relative
.components()
.any(|c| !matches!(c, std::path::Component::Normal(_)))
{
return Err(Error::Value("staged path must be relative".into()));
}
let path = self.directory.join(relative);
fileio::private_mkdir(path.parent().expect("staged parent"))?;
let mut file = fileio::private().write(true).create_new(true).open(&path)?;
file.write_all(text.as_bytes())?;
self.bytes.set(used);
Ok(path)
}
}
impl Drop for Scratch {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.directory);
}
}