spawningpool 0.2.0

Create hyper-specific, 0-waste agents
Documentation
//! Constructing models for a local LM Studio server.
//!
//! spawningpool deliberately does **not** embed a catalog of hosted models or
//! their limits — those facts go stale and being their arbiter is a liability
//! (the same reasoning keeps pricing out of [`crate::ai::Usage`]). Models you
//! call are defined in your own registry. The one thing that must live in-tree
//! is constructing a [`Model`] for a local LM Studio server, whose ids are
//! discovered at runtime via [`crate::ai::Client::list_models`].

use crate::ai::model::{Api, Model};

const LMSTUDIO_DEFAULT_BASE_URL: &str = "http://localhost:1234";

/// Default output cap and context window for a discovered/constructed local
/// model, which reports neither over the OpenAI API.
const LMSTUDIO_DEFAULT_MAX_TOKENS: u32 = 4096;
const LMSTUDIO_DEFAULT_CONTEXT_WINDOW: u32 = 8192;

pub(crate) fn lmstudio_base_url() -> String {
    std::env::var("LMSTUDIO_BASE_URL").unwrap_or_else(|_| LMSTUDIO_DEFAULT_BASE_URL.to_string())
}

/// Construct a model for a local LM Studio id, pointing at the configured
/// base URL with conservative defaults.
pub(crate) fn lmstudio_model(id: &str) -> Model {
    Model {
        id: id.to_string(),
        name: id.to_string(),
        api: Api::OpenAiCompletions,
        provider: "lmstudio".to_string(),
        base_url: lmstudio_base_url(),
        max_tokens: LMSTUDIO_DEFAULT_MAX_TOKENS,
        context_window: LMSTUDIO_DEFAULT_CONTEXT_WINDOW,
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn lmstudio_model_is_constructed_on_trust() {
        let model = lmstudio_model("qwen2.5-coder-7b");
        assert_eq!(model.api, Api::OpenAiCompletions);
        assert_eq!(model.provider, "lmstudio");
        assert_eq!(model.id, "qwen2.5-coder-7b");
    }

    #[test]
    fn lmstudio_base_url_defaults_when_env_unset() {
        // Only assert the default when the env var is unset, to avoid depending
        // on the ambient environment.
        if std::env::var_os("LMSTUDIO_BASE_URL").is_none() {
            assert_eq!(lmstudio_base_url(), "http://localhost:1234");
        }
    }
}