//! Emacs module ABI adapter for asynchronous, cached native model inference. //! //! The `emacs` crate owns the C ABI and checks opaque user-pointer types. Its //! mandated erased `emacs::Result` is confined to these exported adapters; //! worker and lifecycle code retains the explicit `NativeError` algebra. //! No `Env`, `Value` or Lisp reference is ever passed to a worker thread. #![deny(missing_docs)] use emacs::{Env, Result, defun}; use worker::{Job, Session}; mod worker; emacs::plugin_is_GPL_compatible!(); /// Register native functions without loading a model or starting a thread. #[emacs::module(name = "lispm-llm-native", mod_in_name = false)] fn init(_: &Env) -> Result<()> { Ok(()) } /// Open DIRECTORY with THREADS CPU workers and return an opaque model session. /// /// Model loading is asynchronous; load failures appear in submitted job results. /// The first session fixes LibTorch's process-wide thread count. Changing it /// requires restarting Emacs. No model-generated Lisp is evaluated. #[defun(user_ptr)] fn open(directory: String, threads: i64) -> Result { Ok(Session::open(directory, threads)?) } /// Open DIRECTORY with THREADS workers and consult EPISODIC before neural scoring. /// /// Both paths must be absolute. Episodic updates written atomically to EPISODIC /// take effect on the next submitted instruction without reloading model tensors. #[defun(user_ptr)] fn open_episodic(directory: String, threads: i64, episodic: String) -> Result { Ok(Session::open_episodic(directory, threads, episodic)?) } /// Open DIRECTORY with THREADS workers plus optional EPISODIC and CATALOG files. /// /// Every non-nil path must be absolute. Updates written atomically to EPISODIC /// or CATALOG take effect on subsequent instructions without reloading weights. #[defun(user_ptr)] fn open_workspace( directory: String, threads: i64, episodic: Option, catalog: Option, ) -> Result { Ok(Session::open_workspace(directory, threads, episodic, catalog)?) } /// Submit INSTRUCTION to SESSION and return an opaque inference job immediately. #[defun(user_ptr)] fn submit(session: &Session, instruction: String) -> Result { Ok(session.submit(instruction)?) } /// Submit INSTRUCTION scoped to JSON array MODES-JSON of active major-mode names. #[defun(user_ptr)] fn submit_modes(session: &Session, instruction: String, modes_json: String) -> Result { Ok(session.submit_modes(instruction, modes_json)?) } /// Poll JOB without waiting; return nil while pending or a terminal JSON string. /// /// The terminal status is ready, refused, error or cancelled. Ready includes /// the typed proposal object. Subsequent polls return the same terminal value. #[defun] fn poll(job: &Job) -> Result> { Ok(job.poll()?) } /// Cancel pending JOB and suppress its result without interrupting LibTorch. /// /// Cancellation is idempotent. A terminal value already observed by poll is /// immutable. Garbage collection also cancels an otherwise unobserved job. #[defun] fn cancel(job: &Job) -> Result<()> { Ok(job.cancel()?) } /// Close SESSION without joining its worker or waiting for an active prediction. /// /// New submissions fail. Pending jobs are cancelled; an active native operation /// finishes before the worker releases its model. Repeated close calls are safe. #[defun] fn close(session: &Session) -> Result<()> { session.close(); Ok(()) } /// Return the module's API version for the Lisp loader's compatibility check. #[defun] fn version() -> Result<&'static str> { Ok(env!("CARGO_PKG_VERSION")) } /// Dispatch JSON through the same workflow service exported by the C ABI. /// /// Training, benchmarking and finalization return job handles immediately. /// Polling never invokes Lisp from a worker or evaluates model-generated code. #[defun] fn request(input: String) -> Result { Ok(lispm_llm_native_api::request(&input)) }