1use std::collections::BTreeMap;
9
10use serde::{Deserialize, Serialize};
11
12pub use byob_types::profile::{
13 Effort, MAX_DESCRIPTION_CHARS, MAX_ID_CHARS, MAX_NAME_CHARS, PROTECTED_FIELDS, Params, Profile, ROLE_MAIN, ROLE_SUBAGENTS, ROLE_UTILITY, ROLE_VISION,
14 ROLES, id_ok, role_ok,
15};
16
17use crate::{ClientMsg, EndpointInfo, InferenceTest, Link};
18
19pub const CAP_PROFILES: &str = "profiles";
21
22#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24pub struct ProfileView {
25 pub profile: Profile,
26 #[serde(default)]
28 pub has_key: bool,
29 #[serde(default)]
31 pub link: Link,
32 #[serde(default, skip_serializing_if = "Option::is_none")]
34 pub info: Option<EndpointInfo>,
35}
36
37#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
39pub struct ProfileList {
40 #[serde(default)]
41 pub items: Vec<ProfileView>,
42 #[serde(default)]
43 pub roles: BTreeMap<String, String>,
44}
45
46impl ProfileList {
47 pub fn get(&self, id: &str) -> Option<&ProfileView> {
48 self.items.iter().find(|p| p.profile.id == id)
49 }
50
51 pub fn role(&self, role: &str) -> Option<&ProfileView> {
53 self.roles.get(role).and_then(|id| self.get(id))
54 }
55
56 pub fn roles_of(&self, id: &str) -> Vec<&str> {
58 self.roles.iter().filter(|(_, p)| *p == id).map(|(r, _)| r.as_str()).collect()
59 }
60}
61
62#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
64pub struct ProfileTest {
65 pub id: String,
66 pub test: InferenceTest,
67}
68
69pub(crate) fn check_sizes(msg: &ClientMsg) -> Result<(), String> {
71 let id = |s: &str| if id_ok(s) { Ok(()) } else { Err(format!("{:?} isn't a profile id", s.chars().take(80).collect::<String>())) };
72 let key = |k: &Option<String>| match k {
73 Some(k) if k.chars().count() > crate::connect::MAX_KEY_CHARS => Err("the API key is too long".to_owned()),
74 _ => Ok(()),
75 };
76 match msg {
77 ClientMsg::CreateProfile { profile, api_key, .. } | ClientMsg::UpdateProfile { profile, api_key } => {
78 key(api_key)?;
79 if matches!(msg, ClientMsg::UpdateProfile { .. }) {
80 id(&profile.id)?;
81 }
82 profile.clone().validated().map(|_| ())
83 }
84 ClientMsg::DeleteProfile { id: i } | ClientMsg::DuplicateProfile { id: i } | ClientMsg::TestProfile { id: i } => id(i),
85 ClientMsg::SetRole { role, profile } => {
86 if !role_ok(role) {
87 return Err("bad role name".into());
88 }
89 profile.as_deref().map_or(Ok(()), id)
90 }
91 ClientMsg::SetLevelProfile { profile, scope } => {
92 if let crate::ConfigScope::Workspace { id: w } = scope
93 && (w.is_empty() || w.chars().count() > crate::workspace::MAX_ID_CHARS)
94 {
95 return Err("bad id".into());
96 }
97 profile.as_deref().map_or(Ok(()), id)
98 }
99 _ => Ok(()),
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106 use crate::{ConfigScope, ServerMsg};
107
108 fn p() -> Profile {
109 Profile { id: "qwen".into(), name: "Qwen".into(), base_url: "http://h/v1".into(), model: "qwen3".into(), ..Profile::default() }
110 }
111
112 #[test]
113 fn profile_messages_round_trip_and_are_checked() {
114 let msgs = vec![
115 ClientMsg::GetProfiles,
116 ClientMsg::CreateProfile { profile: p(), api_key: Some("sk".into()), assign: Some(ConfigScope::Chat), make_main: true },
117 ClientMsg::UpdateProfile { profile: p(), api_key: None },
118 ClientMsg::DeleteProfile { id: "qwen".into() },
119 ClientMsg::DuplicateProfile { id: "qwen".into() },
120 ClientMsg::TestProfile { id: "qwen".into() },
121 ClientMsg::SetRole { role: "vision".into(), profile: Some("qwen".into()) },
122 ClientMsg::SetLevelProfile { scope: ConfigScope::Workspace { id: "w1".into() }, profile: None },
123 ];
124 for m in msgs {
125 assert_eq!(ClientMsg::from_json(&m.to_json()).unwrap(), m, "{}", m.to_json());
126 assert!(m.check_sizes().is_ok(), "{}", m.to_json());
127 }
128 let m = ClientMsg::from_json(r#"{"type":"create_profile","profile":{"base_url":"http://h/v1","model":"m"}}"#).unwrap();
130 assert!(matches!(m, ClientMsg::CreateProfile { assign: None, make_main: false, .. }));
131 assert!(ClientMsg::DeleteProfile { id: "../x".into() }.check_sizes().is_err());
132 assert!(
133 ClientMsg::CreateProfile { profile: Profile { base_url: "ftp://x".into(), ..p() }, api_key: None, assign: None, make_main: false }
134 .check_sizes()
135 .is_err()
136 );
137 assert!(ClientMsg::UpdateProfile { profile: p(), api_key: Some("k".repeat(crate::connect::MAX_KEY_CHARS + 1)) }.check_sizes().is_err());
138 assert!(ClientMsg::SetRole { role: " ".into(), profile: None }.check_sizes().is_err());
139 }
140
141 #[test]
142 fn profile_lists_carry_no_keys() {
143 let list = ProfileList {
144 items: vec![ProfileView { profile: p(), has_key: true, link: Link::Ok, info: None }],
145 roles: [("main".to_owned(), "qwen".to_owned())].into(),
146 };
147 let json = ServerMsg::Profiles(Box::new(list.clone())).to_json();
148 assert!(!json.contains("api_key") && json.contains("\"has_key\":true"), "{json}");
149 assert_eq!(ServerMsg::from_json(&json).unwrap(), ServerMsg::Profiles(Box::new(list.clone())));
150 assert_eq!(list.role("main").map(|v| v.profile.name.as_str()), Some("Qwen"));
151 assert_eq!(list.roles_of("qwen"), ["main"]);
152 let t = ServerMsg::ProfileTested(ProfileTest { id: "qwen".into(), test: InferenceTest::default() });
153 assert_eq!(ServerMsg::from_json(&t.to_json()).unwrap(), t);
154 }
155}