use std::str::FromStr;
use serde::{Deserialize, Serialize};
use crate::ai::message::Message;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "kebab-case")]
pub enum Api {
AnthropicMessages,
OpenAiCompletions,
}
impl FromStr for Api {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"anthropic-messages" | "anthropic" => Ok(Api::AnthropicMessages),
"openai-completions" | "openai" => Ok(Api::OpenAiCompletions),
other => Err(format!(
"unknown api '{other}'\n\n Expected one of:\n \
anthropic-messages (alias: anthropic)\n \
openai-completions (alias: openai)"
)),
}
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct Model {
pub id: String,
pub name: String,
pub api: Api,
pub provider: String,
pub base_url: String,
pub max_tokens: u32,
pub context_window: u32,
}
#[derive(Clone, Debug, PartialEq)]
pub struct Tool {
pub name: String,
pub description: String,
pub parameters: serde_json::Value,
}
#[derive(Clone, Debug, Default)]
pub struct Context {
pub system: Option<String>,
pub messages: Vec<Message>,
pub tools: Vec<Tool>,
}
impl Context {
pub fn new(system: Option<String>, messages: Vec<Message>) -> Self {
Context {
system,
messages,
tools: Vec::new(),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn api_parses_from_protocol_or_brand_name() {
assert_eq!(
Api::from_str("anthropic-messages"),
Ok(Api::AnthropicMessages)
);
assert_eq!(Api::from_str("anthropic"), Ok(Api::AnthropicMessages));
assert_eq!(
Api::from_str("openai-completions"),
Ok(Api::OpenAiCompletions)
);
assert_eq!(Api::from_str("openai"), Ok(Api::OpenAiCompletions));
let err = Api::from_str("nope").unwrap_err();
assert!(err.contains("anthropic-messages"));
assert!(err.contains("openai-completions"));
}
}