ag_session/harness_model.rs
1//! Harness model choices offered to users and the default for new sessions.
2
3use crate::AgentModel;
4
5/// Smallest context window, in tokens, a local server may run a model with
6/// for harness turns. The system prompt, tool definitions, output schema,
7/// and response reservation leave too little room for a conversation below
8/// it, and Ollama fills a smaller window by silently dropping the start of
9/// the conversation.
10pub const MINIMUM_RUNNING_CONTEXT_WINDOW: u64 = 16_384;
11
12/// Who pays for a harness model's tokens and, for a hosted model, its price
13/// class as the `ag-harness` provider catalog declares it.
14#[derive(Clone, Copy, Debug, Eq, PartialEq)]
15pub enum ModelCost {
16 /// A local or self-hosted server runs the model at no per-token cost.
17 Local,
18 /// A hosted provider bills at most $2.50 per million output tokens,
19 /// suited to simple tasks such as titles and commit messages.
20 Cheap,
21 /// A hosted provider bills between the cheap and premium bounds.
22 Standard,
23 /// A hosted provider bills at least $15 per million output tokens.
24 Premium,
25 /// The user's subscription plan, such as `ChatGPT`'s, covers the model's
26 /// usage within its limits instead of per-token API billing.
27 Plan,
28}
29
30impl ModelCost {
31 /// Returns whether a hosted provider bills the model's tokens, per token
32 /// or through the user's plan.
33 #[must_use]
34 pub fn is_paid(self) -> bool {
35 self != Self::Local
36 }
37
38 /// Returns the price-class label of a paid model, such as `cheap`,
39 /// `plan` for one the user's plan covers, or `free` for a local one.
40 #[must_use]
41 pub fn label(self) -> &'static str {
42 match self {
43 Self::Local => "free",
44 Self::Cheap => "cheap",
45 Self::Standard => "standard",
46 Self::Premium => "premium",
47 Self::Plan => "plan",
48 }
49 }
50}
51
52/// Whether a harness model can run a turn now.
53#[derive(Clone, Copy, Debug, Eq, PartialEq)]
54pub enum HarnessModelStatus {
55 /// The provider is configured and, for a served model, offers tool calls.
56 Ready,
57 /// The named environment variable must be set before the provider can
58 /// serve requests.
59 MissingConfiguration(&'static str),
60 /// The user must sign in with the named command, such as
61 /// `ag-harness login chatgpt`, before the provider can serve requests.
62 SignInRequired(&'static str),
63 /// The server reports that the model cannot call tools, which every
64 /// harness turn needs.
65 NoToolSupport,
66}
67
68/// One harness model a user can choose, with its cost, readiness, and
69/// context window.
70#[derive(Clone, Debug, Eq, PartialEq)]
71pub struct HarnessModelOption {
72 /// Who pays for the model's tokens, and how much.
73 pub cost: ModelCost,
74 /// Context window in tokens the model supports, when the server reports
75 /// it.
76 pub context_window: Option<u64>,
77 /// Context window in tokens the server currently runs the model with,
78 /// when it reports one. It can be smaller than
79 /// [`Self::context_window`], such as Ollama's 4,096-token default.
80 pub loaded_context_window: Option<u64>,
81 /// Model sessions run.
82 pub model: AgentModel,
83 /// Provider or server display name, such as `Ollama` or `Kimi`.
84 pub provider: String,
85 /// Whether the model can run a turn now.
86 pub status: HarnessModelStatus,
87 /// Whether the provider may train its models on the prompts and
88 /// completions, which its discounted contributor tier requires.
89 pub trains_on_prompts: bool,
90}
91
92impl HarnessModelOption {
93 /// Returns whether the model can run a turn now.
94 #[must_use]
95 pub fn is_ready(&self) -> bool {
96 self.status == HarnessModelStatus::Ready
97 }
98}
99
100/// Returns the model new harness sessions start on.
101///
102/// The user's last explicit choice, `remembered`, wins while it can run, and
103/// a remembered local model wins even while its server is unreachable, so a
104/// stopped server never moves new sessions onto a paid provider. Without one,
105/// the first ready local model is preferred over the first ready paid model.
106/// When no model is ready, the first model that only needs a sign-in is
107/// returned, so the session can start and its first turn asks the user to
108/// sign in.
109#[must_use]
110pub fn default_harness_model(
111 options: &[HarnessModelOption],
112 remembered: Option<AgentModel>,
113) -> Option<AgentModel> {
114 let remembered_option =
115 remembered.and_then(|model| options.iter().find(|option| option.model == model));
116 match (remembered, remembered_option) {
117 (Some(model), Some(option)) if option.is_ready() => return Some(model),
118 (Some(model @ AgentModel::OpenAiCompatible(_)), None) => return Some(model),
119 _ => {}
120 }
121
122 let first_ready = |is_paid: bool| {
123 options
124 .iter()
125 .find(|option| option.cost.is_paid() == is_paid && option.is_ready())
126 };
127
128 first_ready(false)
129 .or_else(|| first_ready(true))
130 .or_else(|| {
131 options
132 .iter()
133 .find(|option| matches!(option.status, HarnessModelStatus::SignInRequired(_)))
134 })
135 .map(|option| option.model)
136}
137
138#[cfg(test)]
139#[path = "harness_model_test.rs"]
140mod tests;