use std::fmt;
use std::str::FromStr;
use std::sync::{Mutex, OnceLock, PoisonError};
use ag_contracts::{ReasoningLevel, SpeedMode};
use rustc_hash::FxHashMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AgentKind {
Antigravity,
Claude,
Codex,
Harness,
}
macro_rules! model_short_name {
($id:literal) => {
$id
};
($id:literal, $short_name:literal) => {
$short_name
};
}
macro_rules! define_model_catalog {
($($provider:ident { $(
$(#[$attribute:meta])*
$model:ident = $discriminant:literal => {
id: $id:literal,
$(short_name: $short_name:literal,)?
description: $description:literal,
fast: $fast:literal,
},
)* })*) => {
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[repr(u8)]
pub enum AgentModel {
$($( $(#[$attribute])* $model = $discriminant, )*)*
OpenAiCompatible(ServedModel) = 18,
}
impl AgentModel {
pub const ALL: &[Self] = &[$($(Self::$model,)*)*];
#[must_use]
pub const fn owner_kind(self) -> AgentKind {
match self {
$($(Self::$model => AgentKind::$provider,)*)*
Self::OpenAiCompatible(_) => AgentKind::Harness,
}
}
const fn descriptor(self) -> ModelDescriptor {
match self {
$($(Self::$model => ModelDescriptor {
description: $description,
id: $id,
short_name: model_short_name!($id $(, $short_name)?),
supports_fast_mode: $fast,
},)*)*
Self::OpenAiCompatible(served) => ModelDescriptor {
description: "Model served by a local or self-hosted \
OpenAI-compatible server, such as Ollama.",
id: served.id(),
short_name: served.id(),
supports_fast_mode: false,
},
}
}
}
impl AgentKind {
const fn declared_models(self) -> &'static [AgentModel] {
match self {
$(Self::$provider => &[$(AgentModel::$model,)*],)*
}
}
}
};
}
define_model_catalog! {
Antigravity {
Gemini4Neon = 33 => {
id: "gemini-4-neon",
description: "Gemini 4 Neon model with extra-high reasoning effort.",
fast: false,
},
Gemini4Argon = 17 => {
id: "gemini-4-argon",
description: "Gemini 4 Argon model.",
fast: false,
},
Gemini4Hydrogen = 30 => {
id: "gemini-4-hydrogen",
description: "Gemini 4 Hydrogen model with extra-high reasoning effort.",
fast: false,
},
GeminiProOrchestrator = 31 => {
id: "gemini-pro-orchestrator-g1-pro",
description: "Orchestrator (Argon) model that coordinates subagents.",
fast: false,
},
Gemini38Flash = 4 => {
id: "gemini-3.8-flash",
description: "Fast Gemini model for agentic and multimodal tasks.",
fast: false,
},
GptOss120b = 32 => {
id: "gpt-oss-120b",
description: "Open-weight GPT-OSS 120B model served through Antigravity.",
fast: false,
},
}
Claude {
ClaudeFable52 = 10 => {
id: "claude-fable-5-2",
short_name: "fable-5.2",
description: "Claude Fable model for creative, narrative-heavy tasks.",
fast: false,
},
ClaudeOpus55 = 8 => {
id: "claude-opus-5-5",
short_name: "opus-5.5",
description: "Latest Claude Opus model for complex agentic tasks.",
fast: true,
},
ClaudeSonnet55 = 9 => {
id: "claude-sonnet-5-5",
short_name: "sonnet-5.5",
description: "Balanced Claude model for quality and latency.",
fast: false,
},
ClaudeHaiku55 = 11 => {
id: "claude-haiku-5-5",
short_name: "haiku-5.5",
description: "Fast Claude model for lighter tasks.",
fast: false,
},
}
Codex {
Gpt6Astra = 0 => {
id: "gpt-6-astra",
description: "Most capable Codex model for the hardest end-to-end work.",
fast: true,
},
Gpt61Sol = 1 => {
id: "gpt-6.1-sol",
description: "Near-Astra Codex model for complex work at a lower cost.",
fast: true,
},
Gpt6Luna = 2 => {
id: "gpt-6-luna",
description: "Efficient Codex model for focused, high-volume tasks.",
fast: true,
},
Gpt56Terra = 3 => {
id: "gpt-5.6-terra",
description: "Current Codex model for balanced coding performance.",
fast: true,
},
Gpt53CodexSpark = 7 => {
id: "gpt-5.3-codex-spark",
description: "Codex spark model for quick coding iterations.",
fast: false,
},
}
Harness {
MuseSpark13 = 12 => {
id: "muse-spark-1.3",
description: "Muse Spark model served through the native harness.",
fast: false,
},
MuseSpark13Contributor = 19 => {
id: "muse-spark-1.3-contributor",
description: "Cheap Muse Spark tier; Meta may train on its prompts and completions.",
fast: false,
},
KimiK3 = 13 => {
id: "kimi-k3",
description: "Latest Kimi model served through the native harness.",
fast: false,
},
KimiK26 = 14 => {
id: "kimi-k2.6",
description: "Previous Kimi model served through the native harness.",
fast: false,
},
Qwen38Max = 15 => {
id: "qwen3.8-max",
description: "Most capable Qwen model served through the native harness.",
fast: false,
},
QwenPlus = 16 => {
id: "qwen-plus",
description: "Balanced Qwen model served through the native harness.",
fast: false,
},
Qwen38Flash = 20 => {
id: "qwen3.8-flash",
description: "Cheap Qwen model for simple tasks such as titles and commit messages.",
fast: false,
},
Qwen37Flash = 21 => {
id: "qwen3.7-flash",
description: "Cheapest Qwen model for short, simple tasks.",
fast: false,
},
ChatGptGpt61Sol = 22 => {
id: "chatgpt/gpt-6.1-sol",
description: "GPT-6.1 Sol billed to your ChatGPT plan instead of API credits.",
fast: false,
},
ChatGptGpt61Terra = 23 => {
id: "chatgpt/gpt-6.1-terra",
description: "GPT-6.1 Terra billed to your ChatGPT plan instead of API credits.",
fast: false,
},
ChatGptGpt6Astra = 24 => {
id: "chatgpt/gpt-6-astra",
description: "GPT-6 Astra billed to your ChatGPT plan instead of API credits.",
fast: false,
},
ChatGptGpt6Sol = 25 => {
id: "chatgpt/gpt-6-sol",
description: "GPT-6 Sol billed to your ChatGPT plan instead of API credits.",
fast: false,
},
ChatGptGpt6Luna = 26 => {
id: "chatgpt/gpt-6-luna",
description: "GPT-6 Luna billed to your ChatGPT plan instead of API credits.",
fast: false,
},
ChatGptGpt56Sol = 27 => {
id: "chatgpt/gpt-5.6-sol",
description: "GPT-5.6 Sol billed to your ChatGPT plan instead of API credits.",
fast: false,
},
ChatGptGpt56Terra = 28 => {
id: "chatgpt/gpt-5.6-terra",
description: "GPT-5.6 Terra billed to your ChatGPT plan instead of API credits.",
fast: false,
},
ChatGptGpt56Luna = 29 => {
id: "chatgpt/gpt-5.6-luna",
description: "GPT-5.6 Luna billed to your ChatGPT plan instead of API credits.",
fast: false,
},
}
}
const ANTIGRAVITY_MODELS: &[AgentModel] = &[
AgentModel::Gemini4Neon,
AgentModel::Gemini4Argon,
AgentModel::Gemini4Hydrogen,
AgentModel::GeminiProOrchestrator,
AgentModel::Gemini38Flash,
AgentModel::ClaudeOpus55,
AgentModel::ClaudeSonnet55,
AgentModel::GptOss120b,
];
const HARNESS_MODELS: &[AgentModel] = &[
AgentModel::MuseSpark13,
AgentModel::MuseSpark13Contributor,
AgentModel::KimiK3,
AgentModel::KimiK26,
AgentModel::Qwen38Max,
AgentModel::QwenPlus,
AgentModel::Qwen38Flash,
AgentModel::Qwen37Flash,
AgentModel::ChatGptGpt61Sol,
AgentModel::ChatGptGpt61Terra,
AgentModel::ChatGptGpt6Astra,
AgentModel::ChatGptGpt6Sol,
AgentModel::ChatGptGpt6Luna,
AgentModel::ChatGptGpt56Sol,
AgentModel::ChatGptGpt56Terra,
AgentModel::ChatGptGpt56Luna,
AgentModel::Gpt61Sol,
AgentModel::Gpt6Astra,
AgentModel::Gpt6Luna,
AgentModel::Gpt56Terra,
AgentModel::ClaudeFable52,
AgentModel::ClaudeOpus55,
AgentModel::ClaudeSonnet55,
AgentModel::ClaudeHaiku55,
];
const OPENAI_COMPATIBLE_ID: &str = "openai-compatible";
struct ModelDescriptor {
description: &'static str,
id: &'static str,
short_name: &'static str,
supports_fast_mode: bool,
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct ServedModel(&'static &'static str);
impl ServedModel {
pub const CONFIGURED: Self = Self(&OPENAI_COMPATIBLE_ID);
#[must_use]
pub fn new(name: &str) -> Option<Self> {
let name = name.trim();
if name.is_empty() {
return None;
}
Some(Self(intern(format!("{OPENAI_COMPATIBLE_ID}/{name}"))))
}
#[must_use]
pub const fn id(self) -> &'static str {
self.0
}
#[must_use]
pub fn name(self) -> Option<&'static str> {
self.id()
.strip_prefix(OPENAI_COMPATIBLE_ID)?
.strip_prefix('/')
}
fn parse(id: &str) -> Option<Self> {
if id == OPENAI_COMPATIBLE_ID {
return Some(Self::CONFIGURED);
}
Self::new(id.strip_prefix(OPENAI_COMPATIBLE_ID)?.strip_prefix('/')?)
}
}
fn intern(id: String) -> &'static &'static str {
static INTERNED: OnceLock<Mutex<FxHashMap<&str, &&str>>> = OnceLock::new();
let mut interned = INTERNED
.get_or_init(Mutex::default)
.lock()
.unwrap_or_else(PoisonError::into_inner);
if let Some(existing) = interned.get(id.as_str()) {
return existing;
}
let leaked: &'static &'static str = Box::leak(Box::new(&*Box::leak(id.into_boxed_str())));
interned.insert(leaked, leaked);
leaked
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AgentSelection {
kind: AgentKind,
model: AgentModel,
}
impl AgentSelection {
#[must_use]
pub fn new(kind: AgentKind, model: AgentModel) -> Self {
let model = if kind.supports_model(model) {
model
} else {
kind.default_model()
};
Self { kind, model }
}
#[must_use]
pub fn kind(self) -> AgentKind {
self.kind
}
#[must_use]
pub fn model(self) -> AgentModel {
self.model
}
#[must_use]
pub fn supports_fast_mode(self) -> bool {
self.kind.supports_speed_mode() && self.model.descriptor().supports_fast_mode
}
#[must_use]
pub fn compatible_with_speed_mode(self, speed_mode: SpeedMode) -> Self {
if speed_mode == SpeedMode::Normal || self.supports_fast_mode() {
return self;
}
match self.kind {
AgentKind::Claude => Self::new(AgentKind::Claude, AgentModel::ClaudeOpus55),
AgentKind::Codex => Self::new(AgentKind::Codex, AgentKind::Codex.default_model()),
AgentKind::Antigravity | AgentKind::Harness => self,
}
}
}
pub trait AgentSelectionMetadata {
fn name(&self) -> &'static str;
fn description(&self) -> &'static str;
}
impl AgentModel {
pub const fn as_str(self) -> &'static str {
self.descriptor().id
}
#[must_use]
pub fn short_name(self) -> &'static str {
match self {
Self::OpenAiCompatible(_) => self.name(),
_ => self.descriptor().short_name,
}
}
pub fn provider_model_str(self) -> &'static str {
self.as_str()
}
pub fn parse_persisted(value: &str) -> Result<Self, String> {
if let Some(replacement) = Self::retired_replacement(value) {
return Ok(replacement);
}
value.parse()
}
#[must_use]
pub fn retired_replacement(value: &str) -> Option<Self> {
const RETIRED_MODEL_REPLACEMENTS: &[(&str, AgentModel)] = &[
("gemini-3-pro-preview", AgentModel::Gemini4Argon),
("gemini-3.1-pro", AgentModel::Gemini4Argon),
("gemini-3.1-pro-preview", AgentModel::Gemini4Argon),
("gemini-3-flash-preview", AgentModel::Gemini38Flash),
("gemini-3.7-flash", AgentModel::Gemini38Flash),
("gemini-3.6-flash", AgentModel::Gemini38Flash),
("gemini-3.5-flash", AgentModel::Gemini38Flash),
("gemini-3.5-flash-lite", AgentModel::Gemini38Flash),
("gemini-3.1-flash-lite-preview", AgentModel::Gemini38Flash),
("claude-fable-5", AgentModel::ClaudeFable52),
("claude-fable-5-1", AgentModel::ClaudeFable52),
("claude-opus-5", AgentModel::ClaudeOpus55),
("claude-haiku-4-5-20251001", AgentModel::ClaudeHaiku55),
("gpt-6-sol", AgentModel::Gpt61Sol),
("gpt-5.6-sol", AgentModel::Gpt61Sol),
("gpt-5.6-luna", AgentModel::Gpt6Luna),
("claude-opus-4-8", AgentModel::ClaudeOpus55),
("claude-opus-4-6", AgentModel::ClaudeOpus55),
("claude-opus-4-7", AgentModel::ClaudeOpus55),
("claude-sonnet-4-6", AgentModel::ClaudeSonnet55),
("claude-sonnet-5", AgentModel::ClaudeSonnet55),
("gpt-5.5", AgentModel::Gpt61Sol),
("gpt-5.4-mini", AgentModel::Gpt6Luna),
("gpt-5.4", AgentModel::Gpt61Sol),
("gpt-5.3-codex", AgentModel::Gpt61Sol),
("gpt-5.2-codex", AgentModel::Gpt53CodexSpark),
];
RETIRED_MODEL_REPLACEMENTS
.iter()
.find(|(retired_id, _)| *retired_id == value)
.map(|(_, replacement)| *replacement)
}
}
pub fn parse_persisted_session_agent_model(
agent_value: Option<&str>,
model_value: &str,
) -> AgentSelection {
let parsed_agent = agent_value
.filter(|value| !value.trim().is_empty())
.and_then(|value| value.parse::<AgentKind>().ok());
if let Some(agent_kind) = parsed_agent {
return parse_model_for_persisted_agent(agent_kind, model_value);
}
let model = AgentModel::parse_persisted(model_value)
.unwrap_or_else(|_| AgentKind::Antigravity.default_model());
let agent_kind = legacy_agent_kind_for_model_value(model_value, model);
AgentSelection::new(agent_kind, model)
}
fn parse_model_for_persisted_agent(agent_kind: AgentKind, model_value: &str) -> AgentSelection {
if let Some(model) = agent_kind.parse_model(model_value) {
return AgentSelection::new(agent_kind, model);
}
if let Ok(model) = AgentModel::parse_persisted(model_value)
&& agent_kind.supports_model(model)
{
return AgentSelection::new(agent_kind, model);
}
AgentSelection::new(agent_kind, agent_kind.default_model())
}
fn legacy_agent_kind_for_model_value(model_value: &str, model: AgentModel) -> AgentKind {
if model.as_str() == model_value {
return model.owner_kind();
}
if model_value.starts_with("claude-") {
return AgentKind::Claude;
}
if model_value.starts_with("gpt-") {
return AgentKind::Codex;
}
if model_value.starts_with("gemini-") {
return AgentKind::Antigravity;
}
model.owner_kind()
}
#[must_use]
pub fn resolve_model_for_available_agent_kinds(
model: AgentModel,
agent_kinds: &[AgentKind],
fallback_model: AgentModel,
) -> AgentModel {
if agent_kinds
.iter()
.any(|agent_kind| agent_kind.supports_model(model))
{
return model;
}
if agent_kinds
.iter()
.any(|agent_kind| agent_kind.supports_model(fallback_model))
{
return fallback_model;
}
agent_kinds
.first()
.copied()
.map_or(fallback_model, AgentKind::default_model)
}
#[must_use]
pub fn resolve_agent_kind_for_model(
model: AgentModel,
agent_kinds: &[AgentKind],
fallback_agent_kind: AgentKind,
) -> AgentKind {
let owner_kind = model.owner_kind();
if agent_kinds.contains(&owner_kind) {
return owner_kind;
}
agent_kinds
.iter()
.copied()
.find(|agent_kind| agent_kind.supports_model(model))
.unwrap_or(fallback_agent_kind)
}
#[must_use]
pub fn resolve_agent_selection_for_model(
model: AgentModel,
preferred_agent_kind: AgentKind,
agent_kinds: &[AgentKind],
) -> AgentSelection {
let agent_kind = if preferred_agent_kind.supports_model(model) {
preferred_agent_kind
} else {
resolve_agent_kind_for_model(model, agent_kinds, preferred_agent_kind)
};
AgentSelection::new(agent_kind, model)
}
#[must_use]
pub fn resolve_prompt_model_agent_kind(
session_agent_kind: AgentKind,
agent_kinds: &[AgentKind],
) -> Option<AgentKind> {
if agent_kinds.contains(&session_agent_kind) {
return Some(session_agent_kind);
}
agent_kinds.first().copied()
}
impl FromStr for AgentModel {
type Err = String;
fn from_str(value: &str) -> Result<Self, Self::Err> {
Self::ALL
.iter()
.copied()
.find(|model| model.as_str() == value)
.or_else(|| ServedModel::parse(value).map(Self::OpenAiCompatible))
.ok_or_else(|| format!("unknown model: {value}"))
}
}
impl AgentSelectionMetadata for AgentModel {
fn name(&self) -> &'static str {
match self {
Self::OpenAiCompatible(served) => served.name().unwrap_or(served.id()),
_ => (*self).provider_model_str(),
}
}
fn description(&self) -> &'static str {
self.descriptor().description
}
}
impl AgentKind {
pub const ALL: &[AgentKind] = &[AgentKind::Antigravity, AgentKind::Claude, AgentKind::Codex];
pub fn executable_name(self) -> Option<&'static str> {
match self {
Self::Antigravity => Some("agy"),
Self::Claude => Some("claude"),
Self::Codex => Some("codex"),
Self::Harness => None,
}
}
pub fn models(self) -> &'static [AgentModel] {
match self {
Self::Antigravity => ANTIGRAVITY_MODELS,
Self::Harness => HARNESS_MODELS,
Self::Claude | Self::Codex => self.declared_models(),
}
}
pub fn default_model(self) -> AgentModel {
match self {
Self::Antigravity => AgentModel::Gemini4Neon,
Self::Claude => AgentModel::ClaudeFable52,
Self::Codex => AgentModel::Gpt61Sol,
Self::Harness => AgentModel::MuseSpark13,
}
}
pub fn parse_model(self, value: &str) -> Option<AgentModel> {
let model = value.parse::<AgentModel>().ok()?;
if !self.supports_model(model) {
return None;
}
Some(model)
}
pub fn supports_model(self, model: AgentModel) -> bool {
match model {
AgentModel::OpenAiCompatible(_) => self == Self::Harness,
_ => self.models().contains(&model),
}
}
pub fn supports_speed_mode(self) -> bool {
matches!(self, Self::Claude | Self::Codex)
}
}
impl AgentSelectionMetadata for AgentKind {
fn name(&self) -> &'static str {
match self {
Self::Antigravity => "antigravity",
Self::Claude => "claude",
Self::Codex => "codex",
Self::Harness => "harness",
}
}
fn description(&self) -> &'static str {
match self {
Self::Antigravity => "Google Antigravity CLI agent.",
Self::Claude => "Anthropic Claude Code agent.",
Self::Codex => "OpenAI Codex CLI agent.",
Self::Harness => "Native Agentty harness agent.",
}
}
}
impl AgentSelectionMetadata for ReasoningLevel {
fn name(&self) -> &'static str {
(*self).as_str()
}
fn description(&self) -> &'static str {
(*self).description()
}
}
impl fmt::Display for AgentKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name())
}
}
impl FromStr for AgentKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"antigravity" | "agy" | "gemini" => Ok(Self::Antigravity),
"claude" => Ok(Self::Claude),
"codex" => Ok(Self::Codex),
"harness" => Ok(Self::Harness),
other => Err(format!("unknown agent kind: {other}")),
}
}
}
#[cfg(test)]
#[path = "agent_test.rs"]
mod tests;