1use std::fmt;
2use std::str::FromStr;
3
4use ag_contracts::{ReasoningLevel, SpeedMode};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
8pub enum AgentKind {
9 Antigravity,
11 Gemini,
13 Claude,
15 Codex,
17}
18
19macro_rules! define_model_catalog {
23 ($($provider:ident { $(
24 $(#[$attribute:meta])*
25 $model:ident = $discriminant:literal => {
26 id: $id:literal,
27 description: $description:literal,
28 fast: $fast:literal,
29 context: $context:expr,
30 },
31 )* })*) => {
32 #[derive(Debug, Clone, Copy, PartialEq, Eq)]
38 pub enum AgentModel {
39 $($( $(#[$attribute])* $model = $discriminant, )*)*
40 }
41
42 impl AgentModel {
43 pub const ALL: &[Self] = &[$($(Self::$model,)*)*];
45
46 const fn descriptor(self) -> ModelDescriptor {
47 match self {
48 $($(Self::$model => ModelDescriptor {
49 context_limits: $context,
50 description: $description,
51 id: $id,
52 supports_fast_mode: $fast,
53 },)*)*
54 }
55 }
56 }
57
58 impl AgentKind {
59 pub fn models(self) -> &'static [AgentModel] {
61 match self {
62 Self::Antigravity => Self::Gemini.models(),
63 $(Self::$provider => &[$(AgentModel::$model,)*],)*
64 }
65 }
66 }
67 };
68}
69
70define_model_catalog! {
71 Gemini {
72 Gemini31Pro = 6 => {
74 id: "gemini-3.1-pro-preview",
75 description: "Higher-quality Gemini model for deeper reasoning.",
76 fast: false,
77 context: None,
78 },
79 Gemini38Flash = 4 => {
81 id: "gemini-3.8-flash",
82 description: "Fast Gemini model for agentic and multimodal tasks.",
83 fast: false,
84 context: None,
85 },
86 Gemini35FlashLite = 5 => {
88 id: "gemini-3.5-flash-lite",
89 description: "Lightweight Gemini model for fast, cost-conscious workloads.",
90 fast: false,
91 context: None,
92 },
93 }
94 Claude {
95 ClaudeFable51 = 10 => {
97 id: "claude-fable-5-1",
98 description: "Claude Fable model for creative, narrative-heavy tasks.",
99 fast: false,
100 context: None,
101 },
102 ClaudeOpus55 = 8 => {
104 id: "claude-opus-5-5",
105 description: "Latest Claude Opus model for complex agentic tasks.",
106 fast: true,
107 context: None,
108 },
109 ClaudeSonnet5 = 9 => {
111 id: "claude-sonnet-5",
112 description: "Balanced Claude model for quality and latency.",
113 fast: false,
114 context: None,
115 },
116 ClaudeHaiku4520251001 = 11 => {
118 id: "claude-haiku-4-5-20251001",
119 description: "Fast Claude model for lighter tasks.",
120 fast: false,
121 context: None,
122 },
123 }
124 Codex {
125 Gpt6Astra = 0 => {
127 id: "gpt-6-astra",
128 description: "Most capable Codex model for the hardest end-to-end work.",
129 fast: true,
130 context: Some(ModelContextLimits::CODEX_LARGE),
131 },
132 Gpt61Sol = 1 => {
134 id: "gpt-6.1-sol",
135 description: "Near-Astra Codex model for complex work at a lower cost.",
136 fast: true,
137 context: Some(ModelContextLimits::CODEX_LARGE),
138 },
139 Gpt6Luna = 2 => {
141 id: "gpt-6-luna",
142 description: "Efficient Codex model for focused, high-volume tasks.",
143 fast: true,
144 context: Some(ModelContextLimits::CODEX_LARGE),
145 },
146 Gpt56Terra = 3 => {
148 id: "gpt-5.6-terra",
149 description: "Current Codex model for balanced coding performance.",
150 fast: true,
151 context: Some(ModelContextLimits::CODEX_LARGE),
152 },
153 Gpt53CodexSpark = 7 => {
155 id: "gpt-5.3-codex-spark",
156 description: "Codex spark model for quick coding iterations.",
157 fast: false,
158 context: Some(ModelContextLimits::CODEX_SPARK),
159 },
160 }
161}
162
163#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165pub struct ModelContextLimits {
166 pub context_window_tokens: u64,
168 pub input_headroom_tokens: u64,
171}
172
173impl ModelContextLimits {
174 const CODEX_LARGE: Self = Self {
175 context_window_tokens: 1_050_000,
176 input_headroom_tokens: 128_000,
177 };
178 const CODEX_SPARK: Self = Self {
179 context_window_tokens: 128_000,
180 input_headroom_tokens: 8_000,
181 };
182
183 #[must_use]
185 pub const fn input_token_budget(self) -> u64 {
186 self.context_window_tokens
187 .saturating_sub(self.input_headroom_tokens)
188 }
189}
190
191struct ModelDescriptor {
192 context_limits: Option<ModelContextLimits>,
193 description: &'static str,
194 id: &'static str,
195 supports_fast_mode: bool,
196}
197
198#[derive(Debug, Clone, Copy, PartialEq, Eq)]
200pub struct AgentSelection {
201 kind: AgentKind,
202 model: AgentModel,
203}
204
205impl AgentSelection {
206 #[must_use]
211 pub fn new(kind: AgentKind, model: AgentModel) -> Self {
212 let model = if kind.supports_model(model) {
213 model
214 } else {
215 kind.default_model()
216 };
217
218 Self { kind, model }
219 }
220
221 #[must_use]
223 pub fn kind(self) -> AgentKind {
224 self.kind
225 }
226
227 #[must_use]
229 pub fn model(self) -> AgentModel {
230 self.model
231 }
232
233 #[must_use]
235 pub fn supports_fast_mode(self) -> bool {
236 self.model.descriptor().supports_fast_mode
237 }
238
239 #[must_use]
245 pub fn compatible_with_speed_mode(self, speed_mode: SpeedMode) -> Self {
246 if speed_mode == SpeedMode::Normal || self.supports_fast_mode() {
247 return self;
248 }
249
250 match self.kind {
251 AgentKind::Claude => Self::new(AgentKind::Claude, AgentModel::ClaudeOpus55),
252 AgentKind::Codex => Self::new(AgentKind::Codex, AgentKind::Codex.default_model()),
253 AgentKind::Antigravity | AgentKind::Gemini => self,
254 }
255 }
256}
257
258pub trait AgentSelectionMetadata {
260 fn name(&self) -> &'static str;
262
263 fn description(&self) -> &'static str;
265}
266
267impl AgentModel {
268 pub const fn as_str(self) -> &'static str {
271 self.descriptor().id
272 }
273
274 #[must_use]
276 pub const fn context_limits(self) -> Option<ModelContextLimits> {
277 self.descriptor().context_limits
278 }
279
280 pub fn provider_model_str(self) -> &'static str {
285 self.as_str()
286 }
287
288 pub fn parse_persisted(value: &str) -> Result<Self, String> {
301 if let Some(replacement) = Self::retired_replacement(value) {
302 return Ok(replacement);
303 }
304
305 value.parse()
306 }
307
308 #[must_use]
318 pub fn retired_replacement(value: &str) -> Option<Self> {
319 const RETIRED_MODEL_REPLACEMENTS: &[(&str, AgentModel)] = &[
320 ("gemini-3-pro-preview", AgentModel::Gemini31Pro),
321 ("gemini-3.1-pro", AgentModel::Gemini31Pro),
322 ("gemini-3-flash-preview", AgentModel::Gemini38Flash),
323 ("gemini-3.7-flash", AgentModel::Gemini38Flash),
324 ("gemini-3.6-flash", AgentModel::Gemini38Flash),
325 ("gemini-3.5-flash", AgentModel::Gemini35FlashLite),
326 (
327 "gemini-3.1-flash-lite-preview",
328 AgentModel::Gemini35FlashLite,
329 ),
330 ("claude-fable-5", AgentModel::ClaudeFable51),
331 ("claude-opus-5", AgentModel::ClaudeOpus55),
332 ("gpt-6-sol", AgentModel::Gpt61Sol),
333 ("gpt-5.6-sol", AgentModel::Gpt61Sol),
334 ("gpt-5.6-luna", AgentModel::Gpt6Luna),
335 ("claude-opus-4-8", AgentModel::ClaudeOpus55),
336 ("claude-opus-4-6", AgentModel::ClaudeOpus55),
337 ("claude-opus-4-7", AgentModel::ClaudeOpus55),
338 ("claude-sonnet-4-6", AgentModel::ClaudeSonnet5),
339 ("gpt-5.5", AgentModel::Gpt61Sol),
340 ("gpt-5.4-mini", AgentModel::Gpt6Luna),
341 ("gpt-5.4", AgentModel::Gpt61Sol),
342 ("gpt-5.3-codex", AgentModel::Gpt61Sol),
343 ("gpt-5.2-codex", AgentModel::Gpt53CodexSpark),
344 ];
345
346 RETIRED_MODEL_REPLACEMENTS
347 .iter()
348 .find(|(retired_id, _)| *retired_id == value)
349 .map(|(_, replacement)| *replacement)
350 }
351}
352
353pub fn parse_persisted_session_agent_model(
362 agent_value: Option<&str>,
363 model_value: &str,
364) -> AgentSelection {
365 let parsed_agent = agent_value
366 .filter(|value| !value.trim().is_empty())
367 .and_then(|value| value.parse::<AgentKind>().ok());
368
369 if let Some(agent_kind) = parsed_agent {
370 return parse_model_for_persisted_agent(agent_kind, model_value);
371 }
372
373 let model = AgentModel::parse_persisted(model_value)
374 .unwrap_or_else(|_| AgentKind::Antigravity.default_model());
375 let agent_kind = legacy_agent_kind_for_model_value(model_value, model);
376
377 AgentSelection::new(agent_kind, model)
378}
379
380fn parse_model_for_persisted_agent(agent_kind: AgentKind, model_value: &str) -> AgentSelection {
383 if let Some(model) = agent_kind.parse_model(model_value) {
384 return AgentSelection::new(agent_kind, model);
385 }
386
387 if let Ok(model) = AgentModel::parse_persisted(model_value)
388 && agent_kind.supports_model(model)
389 {
390 return AgentSelection::new(agent_kind, model);
391 }
392
393 AgentSelection::new(agent_kind, agent_kind.default_model())
394}
395
396fn legacy_agent_kind_for_model_value(model_value: &str, model: AgentModel) -> AgentKind {
399 if model_value.starts_with("claude-") {
400 return AgentKind::Claude;
401 }
402
403 if model_value.starts_with("gpt-") {
404 return AgentKind::Codex;
405 }
406
407 if model_value.starts_with("gemini-") {
408 return AgentKind::Antigravity;
409 }
410
411 AgentKind::ALL
412 .iter()
413 .copied()
414 .find(|agent_kind| agent_kind.supports_model(model))
415 .unwrap_or(AgentKind::Antigravity)
416}
417
418#[must_use]
421pub fn selectable_models_for_agent_kinds(agent_kinds: &[AgentKind]) -> Vec<AgentModel> {
422 let mut models = Vec::new();
423 for model in agent_kinds
424 .iter()
425 .flat_map(|agent_kind| agent_kind.models())
426 .copied()
427 {
428 if !models.contains(&model) {
429 models.push(model);
430 }
431 }
432
433 models
434}
435
436#[must_use]
443pub fn resolve_model_for_available_agent_kinds(
444 model: AgentModel,
445 agent_kinds: &[AgentKind],
446 fallback_model: AgentModel,
447) -> AgentModel {
448 if agent_kinds
449 .iter()
450 .any(|agent_kind| agent_kind.supports_model(model))
451 {
452 return model;
453 }
454
455 if agent_kinds
456 .iter()
457 .any(|agent_kind| agent_kind.supports_model(fallback_model))
458 {
459 return fallback_model;
460 }
461
462 agent_kinds
463 .first()
464 .copied()
465 .map_or(fallback_model, AgentKind::default_model)
466}
467
468#[must_use]
475pub fn resolve_agent_kind_for_model(
476 model: AgentModel,
477 agent_kinds: &[AgentKind],
478 fallback_agent_kind: AgentKind,
479) -> AgentKind {
480 agent_kinds
481 .iter()
482 .copied()
483 .find(|agent_kind| agent_kind.supports_model(model))
484 .unwrap_or(fallback_agent_kind)
485}
486
487#[must_use]
494pub fn resolve_agent_selection_for_model(
495 model: AgentModel,
496 preferred_agent_kind: AgentKind,
497 agent_kinds: &[AgentKind],
498) -> AgentSelection {
499 let agent_kind = if preferred_agent_kind.supports_model(model) {
500 preferred_agent_kind
501 } else {
502 resolve_agent_kind_for_model(model, agent_kinds, preferred_agent_kind)
503 };
504
505 AgentSelection::new(agent_kind, model)
506}
507
508#[must_use]
514pub fn resolve_prompt_model_agent_kind(
515 session_agent_kind: AgentKind,
516 agent_kinds: &[AgentKind],
517) -> Option<AgentKind> {
518 if agent_kinds.contains(&session_agent_kind) {
519 return Some(session_agent_kind);
520 }
521
522 agent_kinds.first().copied()
523}
524
525impl FromStr for AgentModel {
526 type Err = String;
527
528 fn from_str(value: &str) -> Result<Self, Self::Err> {
529 Self::ALL
530 .iter()
531 .copied()
532 .find(|model| model.as_str() == value)
533 .ok_or_else(|| format!("unknown model: {value}"))
534 }
535}
536
537impl AgentSelectionMetadata for AgentModel {
538 fn name(&self) -> &'static str {
539 (*self).provider_model_str()
540 }
541
542 fn description(&self) -> &'static str {
543 self.descriptor().description
544 }
545}
546
547impl AgentKind {
548 pub const ALL: &[AgentKind] = &[
550 AgentKind::Gemini,
551 AgentKind::Antigravity,
552 AgentKind::Claude,
553 AgentKind::Codex,
554 ];
555
556 pub fn executable_name(self) -> &'static str {
558 match self {
559 Self::Antigravity => "agy",
560 Self::Gemini => "gemini",
561 Self::Claude => "claude",
562 Self::Codex => "codex",
563 }
564 }
565
566 pub fn default_model(self) -> AgentModel {
568 match self {
569 Self::Antigravity | Self::Gemini => AgentModel::Gemini31Pro,
570 Self::Claude => AgentModel::ClaudeFable51,
571 Self::Codex => AgentModel::Gpt61Sol,
572 }
573 }
574
575 pub fn model_str(self, model: AgentModel) -> Option<&'static str> {
577 if !self.supports_model(model) {
578 return None;
579 }
580
581 Some(model.as_str())
582 }
583
584 pub fn parse_model(self, value: &str) -> Option<AgentModel> {
586 let model = value.parse::<AgentModel>().ok()?;
587 if !self.supports_model(model) {
588 return None;
589 }
590
591 Some(model)
592 }
593
594 pub fn supports_model(self, model: AgentModel) -> bool {
596 self.models().contains(&model)
597 }
598
599 pub fn supports_speed_mode(self) -> bool {
608 matches!(self, Self::Claude | Self::Codex)
609 }
610}
611
612impl AgentSelectionMetadata for AgentKind {
613 fn name(&self) -> &'static str {
614 match self {
615 Self::Antigravity => "antigravity",
616 Self::Gemini => "gemini",
617 Self::Claude => "claude",
618 Self::Codex => "codex",
619 }
620 }
621
622 fn description(&self) -> &'static str {
623 match self {
624 Self::Antigravity => "Google Antigravity CLI agent.",
625 Self::Gemini => "Google Gemini CLI agent.",
626 Self::Claude => "Anthropic Claude Code agent.",
627 Self::Codex => "OpenAI Codex CLI agent.",
628 }
629 }
630}
631
632impl AgentSelectionMetadata for ReasoningLevel {
633 fn name(&self) -> &'static str {
634 (*self).as_str()
635 }
636
637 fn description(&self) -> &'static str {
638 (*self).description()
639 }
640}
641
642impl fmt::Display for AgentKind {
643 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
644 write!(f, "{}", self.name())
645 }
646}
647
648impl FromStr for AgentKind {
649 type Err = String;
650
651 fn from_str(s: &str) -> Result<Self, Self::Err> {
652 match s.to_lowercase().as_str() {
653 "antigravity" | "agy" => Ok(Self::Antigravity),
654 "gemini" => Ok(Self::Gemini),
655 "claude" => Ok(Self::Claude),
656 "codex" => Ok(Self::Codex),
657 other => Err(format!("unknown agent kind: {other}")),
658 }
659 }
660}
661
662#[cfg(test)]
663#[path = "agent_test.rs"]
664mod tests;