use crate::client;
use crate::config::Config;
use crate::download;
use crate::image_input::DecodedImage;
use crate::ocr::{OcrEngine, OcrError};
use crate::server::Child;
use std::sync::mpsc;
use std::sync::{Arc, Mutex, PoisonError};
use std::thread::JoinHandle;
use std::time::{Duration, Instant};
const REQUEST_TIMEOUT: Duration = Duration::from_secs(180);
const REAP_TICK: Duration = Duration::from_secs(5);
struct Reaper {
stop: mpsc::Sender<()>,
handle: Option<JoinHandle<()>>,
}
impl Reaper {
fn spawn(child: Arc<Mutex<Option<Child>>>) -> Self {
let (stop, rx) = mpsc::channel::<()>();
let handle = std::thread::spawn(move || {
loop {
match rx.recv_timeout(REAP_TICK) {
Ok(()) | Err(mpsc::RecvTimeoutError::Disconnected) => break,
Err(mpsc::RecvTimeoutError::Timeout) => {
let now = Instant::now();
let mut guard = child.lock().unwrap_or_else(PoisonError::into_inner);
if guard.as_ref().is_some_and(|c| c.should_reap(now)) {
*guard = None;
eprintln!("ocr-mcp: llama-server idle, shut down");
}
}
}
}
});
Self {
stop,
handle: Some(handle),
}
}
}
impl Drop for Reaper {
fn drop(&mut self) {
let _ = self.stop.send(());
if let Some(handle) = self.handle.take() {
let _ = handle.join();
}
}
}
pub struct LlamaOcr {
cfg: Config,
child: Arc<Mutex<Option<Child>>>,
_reaper: Reaper,
}
impl LlamaOcr {
#[must_use]
pub fn new(cfg: Config) -> Self {
let child = Arc::new(Mutex::new(None));
let reaper = Reaper::spawn(Arc::clone(&child));
Self {
cfg,
child,
_reaper: reaper,
}
}
}
impl OcrEngine for LlamaOcr {
fn describe(&mut self, image: &DecodedImage, prompt: &str) -> Result<String, OcrError> {
let base_url = {
let mut guard = self.child.lock().unwrap_or_else(PoisonError::into_inner);
if guard
.as_ref()
.is_some_and(|c| c.should_reap(Instant::now()))
{
*guard = None;
}
if guard.is_none() {
if self.cfg.auto_download {
download::ensure_weights(&self.cfg);
}
*guard = Some(Child::spawn(&self.cfg)?);
}
let child = guard.as_mut().expect("just ensured Some above");
child.touch();
child.set_busy(true);
child.base_url()
};
let url = format!("{base_url}/v1/chat/completions");
let body = client::build_request(image, prompt, self.cfg.max_tokens);
let agent: ureq::Agent = ureq::Agent::config_builder()
.timeout_global(Some(REQUEST_TIMEOUT))
.build()
.into();
let outcome = agent
.post(&url)
.send_json(&body)
.and_then(|mut r| r.body_mut().read_to_string());
let mut guard = self.child.lock().unwrap_or_else(PoisonError::into_inner);
match outcome {
Ok(text) => {
if let Some(child) = guard.as_mut() {
child.touch();
child.set_busy(false);
}
drop(guard);
client::parse_response(&text)
}
Err(e) => {
*guard = None;
Err(OcrError::RequestFailed(e.to_string()))
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn cfg() -> Config {
Config {
model: std::path::PathBuf::from("/definitely/missing/glm-ocr.gguf"),
mmproj: std::path::PathBuf::from("/definitely/missing/mmproj.gguf"),
binary: None,
idle_secs: 300,
max_tokens: 1024,
auto_download: false,
}
}
#[test]
fn missing_weights_fail_before_any_process_work() {
let mut e = LlamaOcr::new(cfg());
let img = DecodedImage {
media_type: "image/png",
bytes: vec![0x89, b'P', b'N', b'G'],
};
let err = e.describe(&img, "Transcribe.").unwrap_err();
assert!(matches!(err, OcrError::WeightsMissing { .. }));
assert!(err.to_string().contains("OCR_MCP_AUTO_DOWNLOAD"));
}
}