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 user must paste an API key for the named provider, such as
58 /// `kimi`, before the provider can serve requests.
59 ApiKeyRequired(&'static str),
60 /// The named environment variable must be set before the provider can
61 /// serve requests.
62 MissingConfiguration(&'static str),
63 /// The user must sign in with the named command, such as
64 /// `ag-harness login chatgpt`, before the provider can serve requests.
65 SignInRequired(&'static str),
66 /// The server reports that the model cannot call tools, which every
67 /// harness turn needs.
68 NoToolSupport,
69}
70
71/// One harness model a user can choose, with its cost, readiness, and
72/// context window.
73#[derive(Clone, Debug, Eq, PartialEq)]
74pub struct HarnessModelOption {
75 /// Who pays for the model's tokens, and how much.
76 pub cost: ModelCost,
77 /// Context window in tokens the model supports, when the server reports
78 /// it.
79 pub context_window: Option<u64>,
80 /// Context window in tokens the server currently runs the model with,
81 /// when it reports one. It can be smaller than
82 /// [`Self::context_window`], such as Ollama's 4,096-token default.
83 pub loaded_context_window: Option<u64>,
84 /// Model sessions run.
85 pub model: AgentModel,
86 /// Provider or server display name, such as `Ollama` or `Kimi`.
87 pub provider: String,
88 /// Whether the model can run a turn now.
89 pub status: HarnessModelStatus,
90 /// Whether the provider may train its models on the prompts and
91 /// completions, which its discounted contributor tier requires.
92 pub trains_on_prompts: bool,
93}
94
95impl HarnessModelOption {
96 /// Returns whether the model can run a turn now.
97 #[must_use]
98 pub fn is_ready(&self) -> bool {
99 self.status == HarnessModelStatus::Ready
100 }
101}
102
103/// Returns the model new harness sessions start on.
104///
105/// The user's last explicit choice, `remembered`, wins while it can run, and
106/// a remembered local model wins even while its server is unreachable, so a
107/// stopped server never moves new sessions onto a paid provider. Without one,
108/// the first ready local model is preferred over the first ready paid model.
109/// A model that still needs a key or a sign-in is never chosen, so without a
110/// ready model this returns `None` and the harness offers no default.
111#[must_use]
112pub fn default_harness_model(
113 options: &[HarnessModelOption],
114 remembered: Option<AgentModel>,
115) -> Option<AgentModel> {
116 let remembered_option =
117 remembered.and_then(|model| options.iter().find(|option| option.model == model));
118 match (remembered, remembered_option) {
119 (Some(model), Some(option)) if option.is_ready() => return Some(model),
120 (Some(model @ AgentModel::OpenAiCompatible(_)), None) => return Some(model),
121 _ => {}
122 }
123
124 let first_ready = |is_paid: bool| {
125 options
126 .iter()
127 .find(|option| option.cost.is_paid() == is_paid && option.is_ready())
128 };
129
130 first_ready(false)
131 .or_else(|| first_ready(true))
132 .map(|option| option.model)
133}
134
135#[cfg(test)]
136#[path = "harness_model_test.rs"]
137mod tests;