ailoy 0.3.1

Agents that drive a language model through tool-augmented turns, in a virtx console.
Documentation
use futures::{future::BoxFuture, stream::BoxStream};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};

use crate::{
    datatype::Value,
    message::{Message, MessageDeltaOutput, MessageOutput},
    tool::ToolDesc,
};

/// How much a model thinks before it answers.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
#[serde(rename_all = "lowercase")]
pub enum ThinkingEffort {
    Low,
    Medium,
    High,
}

/// A request the model's API refused as sent: a 4xx saying the request itself is wrong,
/// not that the server is busy or the caller unknown. Sending it again as is gains nothing;
/// what is in it has to change.
#[derive(Clone, Debug)]
pub struct RequestRefused {
    pub status: u16,
    pub message: String,
}

impl std::fmt::Display for RequestRefused {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(&self.message)
    }
}

impl std::error::Error for RequestRefused {}

/// The error for a request the API answered with `status`: a [`RequestRefused`] when the
/// status says the request itself was wrong, else a plain error with the same message.
pub(crate) fn api_error(status: u16, message: String) -> anyhow::Error {
    if matches!(status, 400 | 413 | 422) {
        RequestRefused { status, message }.into()
    } else {
        anyhow::anyhow!(message)
    }
}

/// Settings every [`InferLangModel`] can apply, whichever way it runs the model.
#[derive(Clone, Debug, Default, Serialize, Deserialize, JsonSchema)]
pub struct LangModelOptions {
    /// How much the model thinks; `None` keeps the model's or backend's default.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub thinking_effort: Option<ThinkingEffort>,

    /// JSON schema the output must match.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub output_schema: Option<Value>,
}

/// A language model that can be run on a conversation.
pub trait InferLangModel: Send + Sync {
    /// Run the model to completion and return the whole output.
    fn infer(
        &self,
        messages: &[Message],
        tools: &[ToolDesc],
        options: &LangModelOptions,
    ) -> BoxFuture<'static, anyhow::Result<MessageOutput>>;

    /// Run the model and yield its output as deltas while it is generated.
    fn infer_stream(
        &self,
        messages: &[Message],
        tools: &[ToolDesc],
        options: &LangModelOptions,
    ) -> BoxStream<'static, anyhow::Result<MessageDeltaOutput>>;
}