Skip to main content

infinity_agent_core/system/
model.rs

1//! The [`ModelSource`] trait: choosing the model for each completion round.
2
3use rap_protocol::ThreadId;
4use std::sync::Arc;
5
6use async_trait::async_trait;
7
8use infinity_provider_protocol::{ModelEntry, ModelProvider};
9
10type BoxError = Box<dyn std::error::Error + Send + Sync>;
11
12/// The model a thread will use for one completion round.
13#[derive(Clone)]
14pub struct ResolvedModel {
15    pub provider: Arc<dyn ModelProvider>,
16    pub model_id: String,
17    /// Context window size in tokens. Used for the auto-compaction threshold;
18    /// `0` disables it.
19    pub context_window: usize,
20    /// Whether the model accepts image inputs. When `false`, image tool
21    /// results are replaced with a text placeholder before invocation.
22    pub supports_image_input: bool,
23}
24
25/// Chooses the model for each completion round of each thread.
26///
27/// Resolution happens at the start of every round, which is what makes
28/// mid-session model switching safe: persist the new selection wherever your
29/// implementation reads it from, and the next round picks it up. An in-flight
30/// completion always finishes on the model it started with.
31#[async_trait(?Send)]
32pub trait ModelSource {
33    async fn resolve(&self, thread_id: &ThreadId<str>) -> Result<ResolvedModel, BoxError>;
34}
35
36/// A fixed model used for every thread and every round.
37pub struct StaticModel {
38    resolved: ResolvedModel,
39}
40
41impl StaticModel {
42    /// Look up `model_id` in the provider's catalog to capture its context
43    /// window and image-input support.
44    pub async fn new(provider: Arc<dyn ModelProvider>, model_id: &str) -> Result<Self, BoxError> {
45        let models = provider.list_models().await?;
46        let entry = models
47            .into_iter()
48            .find(|m| m.model_id == model_id)
49            .ok_or_else(|| format!("model '{model_id}' not found in provider catalog"))?;
50        Ok(Self::from_entry(provider, &entry))
51    }
52
53    /// Build from an already-resolved [`ModelEntry`].
54    pub fn from_entry(provider: Arc<dyn ModelProvider>, entry: &ModelEntry) -> Self {
55        Self {
56            resolved: ResolvedModel {
57                provider,
58                model_id: entry.model_id.clone(),
59                context_window: entry.context_window,
60                supports_image_input: entry.supports_image_input,
61            },
62        }
63    }
64}
65
66#[async_trait(?Send)]
67impl ModelSource for StaticModel {
68    async fn resolve(&self, _thread_id: &ThreadId<str>) -> Result<ResolvedModel, BoxError> {
69        Ok(self.resolved.clone())
70    }
71}