1use std::{borrow::Cow, fmt, str::FromStr};
4
5use crate::{config::Surface, error::Error};
6
7const JEV_1_13_BARE: &str = "jev-1.13";
9const JEV_1_13_NAMESPACED: &str = "typesafe/jev-1.13";
11const JEV_LATEST_BARE: &str = "jev-latest";
13const JEV_LATEST_NAMESPACED: &str = "~typesafe/jev-latest";
15
16const KNOWN_MODEL_CONTEXT_TOKENS: usize = 32_000;
18
19#[derive(Debug, Clone, PartialEq, Eq)]
38#[non_exhaustive]
39pub enum Model {
40 Jev113,
46 Latest,
51 Custom(String),
53}
54
55pub const DEFAULT_MODEL: Model = Model::Jev113;
58
59impl Model {
60 #[must_use]
75 pub fn wire_id(&self, surface: Surface) -> Cow<'_, str> {
76 match (self, surface) {
77 (Model::Jev113, Surface::Decisions) => Cow::Borrowed(JEV_1_13_NAMESPACED),
78 (Model::Jev113, Surface::SystemOne) => Cow::Borrowed(JEV_1_13_BARE),
79 (Model::Latest, Surface::Decisions) => Cow::Borrowed(JEV_LATEST_NAMESPACED),
80 (Model::Latest, Surface::SystemOne) => Cow::Borrowed(JEV_LATEST_BARE),
81 (Model::Custom(id), _) => Cow::Borrowed(id.as_str()),
82 }
83 }
84
85 #[must_use]
90 pub fn context_tokens(&self) -> Option<usize> {
91 match self {
92 Model::Jev113 | Model::Latest => Some(KNOWN_MODEL_CONTEXT_TOKENS),
93 Model::Custom(_) => None,
94 }
95 }
96}
97
98impl fmt::Display for Model {
99 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
100 match self {
101 Model::Jev113 => f.write_str(JEV_1_13_NAMESPACED),
102 Model::Latest => f.write_str(JEV_LATEST_NAMESPACED),
103 Model::Custom(id) => f.write_str(id),
104 }
105 }
106}
107
108impl FromStr for Model {
109 type Err = Error;
110
111 fn from_str(s: &str) -> Result<Self, Self::Err> {
127 if s.is_empty() {
128 return Err(Error::Invalid {
129 field: "model".to_string(),
130 reason: "model id must not be empty".to_string(),
131 });
132 }
133 Ok(match s {
134 JEV_1_13_BARE | JEV_1_13_NAMESPACED => Model::Jev113,
135 JEV_LATEST_BARE | JEV_LATEST_NAMESPACED => Model::Latest,
136 other => Model::Custom(other.to_string()),
137 })
138 }
139}
140
141impl fmt::Display for Surface {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 match self {
144 Surface::Decisions => f.write_str("decisions"),
145 Surface::SystemOne => f.write_str("systemOne"),
146 }
147 }
148}
149
150#[cfg(test)]
151mod tests {
152 use proptest::prelude::*;
153
154 use super::*;
155
156 #[test]
157 fn test_should_map_jev113_wire_id_per_surface() {
158 assert_eq!(
159 Model::Jev113.wire_id(Surface::Decisions),
160 "typesafe/jev-1.13"
161 );
162 assert_eq!(Model::Jev113.wire_id(Surface::SystemOne), "jev-1.13");
163 }
164
165 #[test]
166 fn test_should_map_latest_wire_id_per_surface() {
167 assert_eq!(
168 Model::Latest.wire_id(Surface::Decisions),
169 "~typesafe/jev-latest"
170 );
171 assert_eq!(Model::Latest.wire_id(Surface::SystemOne), "jev-latest");
172 }
173
174 #[test]
175 fn test_should_never_rewrite_custom_model() {
176 let custom = Model::Custom("acme/model-x".to_string());
177 assert_eq!(custom.wire_id(Surface::Decisions), "acme/model-x");
178 assert_eq!(custom.wire_id(Surface::SystemOne), "acme/model-x");
179 }
180
181 #[test]
182 fn test_should_default_to_pinned_jev113_not_latest() {
183 assert_eq!(DEFAULT_MODEL, Model::Jev113);
184 }
185
186 #[test]
187 fn test_should_parse_bare_and_namespaced_ids() {
188 assert_eq!("jev-1.13".parse::<Model>().unwrap(), Model::Jev113);
189 assert_eq!("typesafe/jev-1.13".parse::<Model>().unwrap(), Model::Jev113);
190 assert_eq!("jev-latest".parse::<Model>().unwrap(), Model::Latest);
191 assert_eq!(
192 "~typesafe/jev-latest".parse::<Model>().unwrap(),
193 Model::Latest
194 );
195 }
196
197 #[test]
198 fn test_should_parse_unknown_id_as_custom() {
199 assert_eq!(
200 "acme/model-x".parse::<Model>().unwrap(),
201 Model::Custom("acme/model-x".to_string())
202 );
203 }
204
205 #[test]
206 fn test_should_reject_empty_model_string() {
207 assert!("".parse::<Model>().is_err());
208 }
209
210 #[test]
211 fn test_should_report_context_tokens_for_known_models_only() {
212 assert_eq!(Model::Jev113.context_tokens(), Some(32_000));
213 assert_eq!(Model::Latest.context_tokens(), Some(32_000));
214 assert_eq!(Model::Custom("x".into()).context_tokens(), None);
215 }
216
217 proptest! {
218 #[test]
219 fn test_should_round_trip_display_and_from_str_for_custom(id in "[a-z][a-z0-9_/.~-]{1,40}") {
220 prop_assume!(!matches!(id.as_str(), "jev-1.13" | "typesafe/jev-1.13" | "jev-latest" | "~typesafe/jev-latest"));
223 let model = Model::Custom(id.clone());
224 let displayed = model.to_string();
225 let parsed: Model = displayed.parse().unwrap();
226 prop_assert_eq!(parsed, Model::Custom(id));
227 }
228
229 #[test]
230 fn test_should_round_trip_display_and_from_str_for_canonical_models(pick in 0usize..2) {
231 let model = if pick == 0 { Model::Jev113 } else { Model::Latest };
232 let displayed = model.to_string();
233 let parsed: Model = displayed.parse().unwrap();
234 prop_assert_eq!(parsed, model);
235 }
236 }
237}