@@ -48,6 +48,30 @@ fn normalize_relay_mesh(
4848 } ) )
4949}
5050
51+ fn trim_to_optional_string ( value : & str ) -> Option < String > {
52+ let trimmed = value. trim ( ) ;
53+ if trimmed. is_empty ( ) {
54+ None
55+ } else {
56+ Some ( trimmed. to_string ( ) )
57+ }
58+ }
59+
60+ fn resolve_created_avatar_url (
61+ requested_avatar_url : Option < & str > ,
62+ persona_avatar_url : Option < String > ,
63+ agent_command : & str ,
64+ ) -> Option < String > {
65+ requested_avatar_url
66+ . and_then ( trim_to_optional_string)
67+ . or_else ( || {
68+ persona_avatar_url
69+ . as_deref ( )
70+ . and_then ( trim_to_optional_string)
71+ } )
72+ . or_else ( || managed_agent_avatar_url ( agent_command) )
73+ }
74+
5175#[ cfg( feature = "mesh-llm" ) ]
5276async fn ensure_relay_mesh_for_record (
5377 app : & AppHandle ,
@@ -474,16 +498,21 @@ pub async fn create_managed_agent(
474498 } ) ;
475499
476500 // Resolve the avatar URL once at creation and persist it on the record.
477- // This is the same logic the original publish used (user input, else
478- // command-based fallback) — storing it lets reconciliation compare
479- // against what was actually published instead of re-deriving it.
480- let resolved_avatar_url = input
481- . avatar_url
482- . as_deref ( )
483- . map ( str:: trim)
484- . filter ( |value| !value. is_empty ( ) )
485- . map ( str:: to_string)
486- . or_else ( || managed_agent_avatar_url ( & agent_command) ) ;
501+ // Explicit input wins, then the persona's own avatar, then the runtime
502+ // fallback. Storing it lets reconciliation compare against what was
503+ // actually published instead of re-deriving it.
504+ let persona_avatar_url = requested_persona_id. as_ref ( ) . and_then ( |persona_id| {
505+ load_personas ( & app)
506+ . ok ( ) ?
507+ . into_iter ( )
508+ . find ( |persona| persona. id == * persona_id) ?
509+ . avatar_url
510+ } ) ;
511+ let resolved_avatar_url = resolve_created_avatar_url (
512+ input. avatar_url . as_deref ( ) ,
513+ persona_avatar_url,
514+ & agent_command,
515+ ) ;
487516
488517 let record = crate :: managed_agents:: ManagedAgentRecord {
489518 pubkey : pubkey. clone ( ) ,
@@ -1145,149 +1174,5 @@ pub async fn probe_backend_provider(binary_path: String) -> Result<serde_json::V
11451174// No backend Tauri command needed. Presence IS the status.
11461175
11471176#[ cfg( test) ]
1148- mod tests {
1149- use super :: * ;
1150-
1151- #[ test]
1152- fn normalize_relay_mesh_rejects_empty_model_ref ( ) {
1153- let config = RelayMeshConfig {
1154- model_ref : " \t " . to_string ( ) ,
1155- } ;
1156-
1157- assert_eq ! (
1158- normalize_relay_mesh( Some ( & config) , & BackendKind :: Local ) . unwrap_err( ) ,
1159- "relay mesh modelRef is required"
1160- ) ;
1161- }
1162-
1163- #[ test]
1164- fn normalize_relay_mesh_rejects_non_local_backend ( ) {
1165- let config = RelayMeshConfig {
1166- model_ref : "Qwen3" . to_string ( ) ,
1167- } ;
1168- let backend = BackendKind :: Provider {
1169- id : "blox" . to_string ( ) ,
1170- config : serde_json:: json!( { } ) ,
1171- } ;
1172-
1173- assert_eq ! (
1174- normalize_relay_mesh( Some ( & config) , & backend) . unwrap_err( ) ,
1175- "relay mesh agents must use the local backend"
1176- ) ;
1177- }
1178-
1179- #[ test]
1180- fn normalize_relay_mesh_trims_and_preserves_valid_config ( ) {
1181- let config = RelayMeshConfig {
1182- model_ref : " Qwen3 " . to_string ( ) ,
1183- } ;
1184-
1185- assert_eq ! (
1186- normalize_relay_mesh( Some ( & config) , & BackendKind :: Local ) . unwrap( ) ,
1187- Some ( RelayMeshConfig {
1188- model_ref: "Qwen3" . to_string( ) ,
1189- } )
1190- ) ;
1191- }
1192-
1193- fn profile ( name : Option < & str > , picture : Option < & str > ) -> crate :: relay:: AgentProfileInfo {
1194- crate :: relay:: AgentProfileInfo {
1195- display_name : name. map ( str:: to_string) ,
1196- picture : picture. map ( str:: to_string) ,
1197- }
1198- }
1199-
1200- #[ test]
1201- fn profile_needs_sync_when_missing ( ) {
1202- assert ! ( profile_needs_sync( None , "Duncan" , Some ( "https://x/a.png" ) ) ) ;
1203- }
1204-
1205- #[ test]
1206- fn profile_needs_sync_when_name_diverges ( ) {
1207- let existing = profile ( Some ( "Stilgar" ) , Some ( "https://x/a.png" ) ) ;
1208- assert ! ( profile_needs_sync(
1209- Some ( & existing) ,
1210- "Duncan" ,
1211- Some ( "https://x/a.png" )
1212- ) ) ;
1213- }
1214-
1215- #[ test]
1216- fn profile_needs_sync_when_picture_diverges ( ) {
1217- let existing = profile ( Some ( "Duncan" ) , Some ( "https://x/old.png" ) ) ;
1218- assert ! ( profile_needs_sync(
1219- Some ( & existing) ,
1220- "Duncan" ,
1221- Some ( "https://x/new.png" )
1222- ) ) ;
1223- }
1224-
1225- #[ test]
1226- fn profile_in_sync_when_name_and_picture_match ( ) {
1227- let existing = profile ( Some ( "Duncan" ) , Some ( "https://x/a.png" ) ) ;
1228- assert ! ( !profile_needs_sync(
1229- Some ( & existing) ,
1230- "Duncan" ,
1231- Some ( "https://x/a.png" )
1232- ) ) ;
1233- }
1234-
1235- #[ test]
1236- fn profile_in_sync_when_both_avatars_absent ( ) {
1237- let existing = profile ( Some ( "Duncan" ) , None ) ;
1238- assert ! ( !profile_needs_sync( Some ( & existing) , "Duncan" , None ) ) ;
1239- }
1240-
1241- #[ test]
1242- fn profile_needs_sync_when_existing_name_is_none ( ) {
1243- let existing = profile ( None , Some ( "https://x/a.png" ) ) ;
1244- assert ! ( profile_needs_sync(
1245- Some ( & existing) ,
1246- "Duncan" ,
1247- Some ( "https://x/a.png" ) ,
1248- ) ) ;
1249- }
1250-
1251- #[ test]
1252- fn profile_needs_sync_when_expected_avatar_absent_but_published ( ) {
1253- let existing = profile ( Some ( "Duncan" ) , Some ( "https://x/a.png" ) ) ;
1254- assert ! ( profile_needs_sync( Some ( & existing) , "Duncan" , None ) ) ;
1255- }
1256-
1257- #[ test]
1258- fn legacy_avatar_prefers_persona_over_corrupted_relay_picture ( ) {
1259- // The regression: the relay picture was overwritten with the command
1260- // default. The persona avatar must win so the correct avatar is restored.
1261- let resolved = resolve_legacy_avatar (
1262- Some ( "https://x/persona.png" . to_string ( ) ) ,
1263- Some ( "https://x/default-icon.png" . to_string ( ) ) ,
1264- "goose" ,
1265- ) ;
1266-
1267- assert_eq ! ( resolved, "https://x/persona.png" ) ;
1268- }
1269-
1270- #[ test]
1271- fn legacy_avatar_falls_back_to_relay_picture_without_persona ( ) {
1272- let resolved =
1273- resolve_legacy_avatar ( None , Some ( "https://x/relay.png" . to_string ( ) ) , "goose" ) ;
1274-
1275- assert_eq ! ( resolved, "https://x/relay.png" ) ;
1276- }
1277-
1278- #[ test]
1279- fn legacy_avatar_falls_back_to_command_icon_when_no_persona_or_relay ( ) {
1280- use crate :: managed_agents:: managed_agent_avatar_url;
1281-
1282- let resolved = resolve_legacy_avatar ( None , None , "goose" ) ;
1283-
1284- assert_eq ! ( resolved, managed_agent_avatar_url( "goose" ) . unwrap( ) ) ;
1285- }
1286-
1287- #[ test]
1288- fn legacy_avatar_empty_when_nothing_resolves ( ) {
1289- let resolved = resolve_legacy_avatar ( None , None , "totally-unknown-command" ) ;
1290-
1291- assert ! ( resolved. is_empty( ) ) ;
1292- }
1293- }
1177+ #[ path = "agents_tests.rs" ]
1178+ mod tests;
0 commit comments