caretta 0.11.1

caretta agent
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
use dioxus::prelude::*;
use serde_json::{Map, Value, json};
use std::path::{Path, PathBuf};

const FACT_PREFIXES: [&str; 6] = [
    "jobs_to_be_done",
    "pains",
    "adoption_yes_if",
    "rejection_no_if",
    "anti_goals",
    "recognition_cues",
];

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct PersonaForm {
    pub original_file_name: Option<String>,
    pub file_name: String,
    pub name: String,
    pub title: String,
    pub organization: String,
    pub summary: String,
    pub communication_style: String,
    pub jobs_to_be_done: String,
    pub pains: String,
    pub adoption_yes_if: String,
    pub rejection_no_if: String,
    pub anti_goals: String,
    pub recognition_cues: String,
}

impl PersonaForm {
    fn has_content(&self) -> bool {
        [
            &self.file_name,
            &self.name,
            &self.title,
            &self.organization,
            &self.summary,
            &self.communication_style,
            &self.jobs_to_be_done,
            &self.pains,
            &self.adoption_yes_if,
            &self.rejection_no_if,
            &self.anti_goals,
            &self.recognition_cues,
        ]
        .iter()
        .any(|field| !field.trim().is_empty())
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct PersonaSummary {
    pub file_name: String,
    pub path_display: String,
    pub name: String,
    pub title: String,
    pub organization: String,
    pub summary: String,
    pub jobs_to_be_done: Vec<String>,
    pub pains: Vec<String>,
    pub recognition_cues: Vec<String>,
}

fn resolve_skill_path(root: &str, skill_path: &str) -> PathBuf {
    let path = Path::new(skill_path);
    if path.is_absolute() {
        path.to_path_buf()
    } else {
        Path::new(root).join(path)
    }
}

pub fn personas_dir(root: &str, skill_path: &str) -> PathBuf {
    resolve_skill_path(root, skill_path)
        .parent()
        .map(|path| path.join("personas"))
        .unwrap_or_else(|| Path::new(root).join("assets/skills/user-personas/personas"))
}

#[cfg(target_arch = "wasm32")]
pub fn load_personas(_root: &str, _skill_path: &str) -> Result<Vec<PersonaSummary>, String> {
    Err("Persona Studio persistence is available in the desktop app.".to_string())
}

#[cfg(not(target_arch = "wasm32"))]
pub fn load_personas(root: &str, skill_path: &str) -> Result<Vec<PersonaSummary>, String> {
    let dir = personas_dir(root, skill_path);
    if !dir.exists() {
        return Ok(Vec::new());
    }

    let mut personas = Vec::new();
    let entries =
        std::fs::read_dir(&dir).map_err(|e| format!("Failed to read {}: {e}", dir.display()))?;
    for entry in entries.flatten() {
        let path = entry.path();
        if path.extension().and_then(|ext| ext.to_str()) != Some("json") {
            continue;
        }
        match load_persona_summary_from_path(&path) {
            Ok(persona) => personas.push(persona),
            Err(err) => tracing::warn!("Skipping persona {}: {err}", path.display()),
        }
    }

    personas.sort_by(|a, b| {
        a.name
            .to_lowercase()
            .cmp(&b.name.to_lowercase())
            .then_with(|| a.file_name.cmp(&b.file_name))
    });
    Ok(personas)
}

fn load_persona_summary_from_path(path: &Path) -> Result<PersonaSummary, String> {
    let content =
        std::fs::read_to_string(path).map_err(|e| format!("Failed to read persona: {e}"))?;
    let value: Value =
        serde_json::from_str(&content).map_err(|e| format!("Invalid persona JSON: {e}"))?;
    let persona = value
        .get("persona")
        .and_then(Value::as_object)
        .ok_or_else(|| "Missing `persona` object".to_string())?;

    let file_name = path
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("persona.json")
        .to_string();
    let facts = other_facts(persona);
    let occupation = persona.get("occupation").and_then(Value::as_object);
    let summary = occupation
        .and_then(|obj| get_string(obj, "description"))
        .or_else(|| get_string(persona, "description"))
        .unwrap_or_default();

    Ok(PersonaSummary {
        file_name,
        path_display: path.to_string_lossy().to_string(),
        name: get_string(persona, "name").unwrap_or_else(|| "Untitled persona".to_string()),
        title: occupation
            .and_then(|obj| get_string(obj, "title"))
            .unwrap_or_default(),
        organization: occupation
            .and_then(|obj| get_string(obj, "organization"))
            .unwrap_or_default(),
        summary,
        jobs_to_be_done: collect_prefixed(&facts, "jobs_to_be_done"),
        pains: collect_prefixed(&facts, "pains"),
        recognition_cues: collect_prefixed(&facts, "recognition_cues"),
    })
}

#[cfg(target_arch = "wasm32")]
pub fn load_persona_form(
    _root: &str,
    _skill_path: &str,
    _file_name: &str,
) -> Result<PersonaForm, String> {
    Err("Persona Studio persistence is available in the desktop app.".to_string())
}

#[cfg(not(target_arch = "wasm32"))]
pub fn load_persona_form(
    root: &str,
    skill_path: &str,
    file_name: &str,
) -> Result<PersonaForm, String> {
    let dir = personas_dir(root, skill_path);
    let safe_file_name = safe_json_file_name(file_name, "persona");
    let path = dir.join(&safe_file_name);
    let content =
        std::fs::read_to_string(&path).map_err(|e| format!("Failed to read persona: {e}"))?;
    let value: Value =
        serde_json::from_str(&content).map_err(|e| format!("Invalid persona JSON: {e}"))?;
    let persona = value
        .get("persona")
        .and_then(Value::as_object)
        .ok_or_else(|| "Missing `persona` object".to_string())?;
    let facts = other_facts(persona);
    let occupation = persona.get("occupation").and_then(Value::as_object);

    Ok(PersonaForm {
        original_file_name: Some(safe_file_name.clone()),
        file_name: safe_file_name,
        name: get_string(persona, "name").unwrap_or_default(),
        title: occupation
            .and_then(|obj| get_string(obj, "title"))
            .unwrap_or_default(),
        organization: occupation
            .and_then(|obj| get_string(obj, "organization"))
            .unwrap_or_default(),
        summary: occupation
            .and_then(|obj| get_string(obj, "description"))
            .or_else(|| get_string(persona, "description"))
            .unwrap_or_default(),
        communication_style: get_string(persona, "communication_style").unwrap_or_default(),
        jobs_to_be_done: collect_prefixed(&facts, "jobs_to_be_done").join("\n"),
        pains: collect_prefixed(&facts, "pains").join("\n"),
        adoption_yes_if: collect_prefixed(&facts, "adoption_yes_if").join("\n"),
        rejection_no_if: collect_prefixed(&facts, "rejection_no_if").join("\n"),
        anti_goals: collect_prefixed(&facts, "anti_goals").join("\n"),
        recognition_cues: collect_prefixed(&facts, "recognition_cues").join("\n"),
    })
}

#[cfg(target_arch = "wasm32")]
pub fn save_persona(_root: &str, _skill_path: &str, _form: &PersonaForm) -> Result<String, String> {
    Err("Persona Studio persistence is available in the desktop app.".to_string())
}

#[cfg(not(target_arch = "wasm32"))]
pub fn save_persona(root: &str, skill_path: &str, form: &PersonaForm) -> Result<String, String> {
    let dir = personas_dir(root, skill_path);
    std::fs::create_dir_all(&dir)
        .map_err(|e| format!("Failed to create {}: {e}", dir.display()))?;

    let fallback_stem = if form.name.trim().is_empty() {
        "persona"
    } else {
        &form.name
    };
    let requested_file_name = safe_json_file_name(&form.file_name, fallback_stem);
    let file_name = match &form.original_file_name {
        Some(original) if original != &requested_file_name => {
            let target = dir.join(&requested_file_name);
            if target.exists() {
                return Err(format!("{} already exists", requested_file_name));
            }
            requested_file_name
        }
        Some(_) => requested_file_name,
        None => unique_json_file_name(&dir, &requested_file_name),
    };

    let existing_path = form
        .original_file_name
        .as_ref()
        .map(|name| dir.join(safe_json_file_name(name, "persona")));
    let target_path = dir.join(&file_name);

    let mut document = existing_path
        .as_ref()
        .and_then(|path| std::fs::read_to_string(path).ok())
        .and_then(|content| serde_json::from_str::<Value>(&content).ok())
        .unwrap_or_else(|| json!({ "type": "TinyPerson", "persona": {} }));

    let preserved_facts = document
        .get("persona")
        .and_then(Value::as_object)
        .map(other_facts)
        .unwrap_or_default()
        .into_iter()
        .filter(|fact| !is_known_prefixed_fact(fact))
        .collect::<Vec<_>>();
    let mut facts = preserved_facts;
    append_fact_lines(&mut facts, "jobs_to_be_done", &form.jobs_to_be_done);
    append_fact_lines(&mut facts, "pains", &form.pains);
    append_fact_lines(&mut facts, "adoption_yes_if", &form.adoption_yes_if);
    append_fact_lines(&mut facts, "rejection_no_if", &form.rejection_no_if);
    append_fact_lines(&mut facts, "anti_goals", &form.anti_goals);
    append_fact_lines(&mut facts, "recognition_cues", &form.recognition_cues);

    let document_obj = ensure_object(&mut document);
    document_obj
        .entry("type".to_string())
        .or_insert_with(|| Value::String("TinyPerson".to_string()));
    let persona_obj = ensure_child_object(document_obj, "persona");
    set_string(
        persona_obj,
        "name",
        clean_or_default(&form.name, "Untitled persona"),
    );
    set_optional_string(
        persona_obj,
        "communication_style",
        form.communication_style.trim(),
    );

    let occupation_obj = ensure_child_object(persona_obj, "occupation");
    set_optional_string(occupation_obj, "title", form.title.trim());
    set_optional_string(occupation_obj, "organization", form.organization.trim());
    set_optional_string(occupation_obj, "description", form.summary.trim());

    persona_obj.insert(
        "other_facts".to_string(),
        Value::Array(facts.into_iter().map(Value::String).collect()),
    );

    let json =
        serde_json::to_string_pretty(&document).map_err(|e| format!("Serialize failed: {e}"))?;
    std::fs::write(&target_path, json)
        .map_err(|e| format!("Failed to write {}: {e}", target_path.display()))?;

    if let Some(old_path) = existing_path
        && old_path != target_path
        && old_path.exists()
    {
        std::fs::remove_file(&old_path)
            .map_err(|e| format!("Saved new file, but failed to remove old file: {e}"))?;
    }

    Ok(file_name)
}

#[cfg(target_arch = "wasm32")]
pub fn delete_persona(_root: &str, _skill_path: &str, _file_name: &str) -> Result<(), String> {
    Err("Persona Studio persistence is available in the desktop app.".to_string())
}

#[cfg(not(target_arch = "wasm32"))]
pub fn delete_persona(root: &str, skill_path: &str, file_name: &str) -> Result<(), String> {
    let dir = personas_dir(root, skill_path);
    let path = dir.join(safe_json_file_name(file_name, "persona"));
    std::fs::remove_file(&path).map_err(|e| format!("Failed to delete {}: {e}", path.display()))
}

pub fn generate_persona_from_notes(notes: &str, sequence: usize) -> PersonaForm {
    let clean = collapse_whitespace(notes);
    let focus = if clean.is_empty() {
        "a user with an emerging workflow need".to_string()
    } else {
        trim_to_chars(&clean, 180)
    };
    let inferred_title = infer_title(&clean).unwrap_or_else(|| "Emerging User Segment".to_string());
    let inferred_org = infer_organization(&clean).unwrap_or_default();
    let inferred_name = infer_name(&clean).unwrap_or_else(|| format!("Persona {sequence}"));
    let cue_terms = keyword_cues(&clean);
    let cue_line = if cue_terms.is_empty() {
        format!(
            "Mentions workflows similar to '{}'.",
            trim_to_chars(&focus, 80)
        )
    } else {
        format!("Uses phrases such as {}.", cue_terms.join(", "))
    };

    PersonaForm {
        original_file_name: None,
        file_name: format!("{}.json", slugify(&inferred_name)),
        name: inferred_name,
        title: inferred_title,
        organization: inferred_org,
        summary: focus.clone(),
        communication_style:
            "Prefers concrete, context-rich discussion tied to their current workflow and constraints."
                .to_string(),
        jobs_to_be_done: [
            format!("Make this workflow easier to complete: {focus}"),
            "Evaluate the product quickly against their real constraints.".to_string(),
            "Move from an ad hoc workaround to a repeatable operating pattern.".to_string(),
        ]
        .join("\n"),
        pains: [
            format!("Current process is harder than it should be: {focus}"),
            "Hard to tell whether tooling will fit before investing setup time.".to_string(),
            "Switching costs feel risky without clear proof of value.".to_string(),
        ]
        .join("\n"),
        adoption_yes_if: [
            "The first run maps directly to their current workflow.".to_string(),
            "Docs and examples speak to their constraints without extra translation.".to_string(),
            "They can validate value before changing production habits.".to_string(),
        ]
        .join("\n"),
        rejection_no_if: [
            "Setup requires them to replace working tools before proving value.".to_string(),
            "The product hides tradeoffs they need to inspect.".to_string(),
            "The workflow adds review or operational overhead without a clear payoff.".to_string(),
        ]
        .join("\n"),
        anti_goals: [
            "They do not want a generic demo detached from their context.".to_string(),
            "They do not want a solution that assumes a larger team or heavier process.".to_string(),
        ]
        .join("\n"),
        recognition_cues: cue_line,
    }
}

fn get_string(map: &Map<String, Value>, key: &str) -> Option<String> {
    map.get(key)
        .and_then(Value::as_str)
        .map(str::trim)
        .filter(|value| !value.is_empty())
        .map(ToOwned::to_owned)
}

fn other_facts(persona: &Map<String, Value>) -> Vec<String> {
    persona
        .get("other_facts")
        .and_then(Value::as_array)
        .map(|facts| {
            facts
                .iter()
                .filter_map(Value::as_str)
                .map(str::trim)
                .filter(|fact| !fact.is_empty())
                .map(ToOwned::to_owned)
                .collect()
        })
        .unwrap_or_default()
}

fn collect_prefixed(facts: &[String], prefix: &str) -> Vec<String> {
    facts
        .iter()
        .filter_map(|fact| strip_fact_prefix(fact, prefix))
        .collect()
}

fn strip_fact_prefix(fact: &str, prefix: &str) -> Option<String> {
    let trimmed = fact.trim();
    let marker = format!("{prefix}:");
    if trimmed
        .to_ascii_lowercase()
        .starts_with(&marker.to_ascii_lowercase())
    {
        Some(trimmed[marker.len()..].trim().to_string())
    } else {
        None
    }
}

fn is_known_prefixed_fact(fact: &str) -> bool {
    FACT_PREFIXES
        .iter()
        .any(|prefix| strip_fact_prefix(fact, prefix).is_some())
}

fn append_fact_lines(facts: &mut Vec<String>, prefix: &str, text: &str) {
    for line in text.lines() {
        let cleaned = clean_fact_line(line);
        if cleaned.is_empty() {
            continue;
        }
        if strip_fact_prefix(&cleaned, prefix).is_some() {
            facts.push(cleaned);
        } else {
            facts.push(format!("{prefix}: {cleaned}"));
        }
    }
}

fn clean_fact_line(line: &str) -> String {
    line.trim()
        .trim_start_matches('-')
        .trim_start_matches('*')
        .trim()
        .to_string()
}

fn ensure_object(value: &mut Value) -> &mut Map<String, Value> {
    if !value.is_object() {
        *value = Value::Object(Map::new());
    }
    value.as_object_mut().expect("value is object")
}

fn ensure_child_object<'a>(
    map: &'a mut Map<String, Value>,
    key: &str,
) -> &'a mut Map<String, Value> {
    let entry = map
        .entry(key.to_string())
        .or_insert_with(|| Value::Object(Map::new()));
    if !entry.is_object() {
        *entry = Value::Object(Map::new());
    }
    entry.as_object_mut().expect("child is object")
}

fn set_string(map: &mut Map<String, Value>, key: &str, value: String) {
    map.insert(key.to_string(), Value::String(value));
}

fn set_optional_string(map: &mut Map<String, Value>, key: &str, value: &str) {
    if value.trim().is_empty() {
        map.remove(key);
    } else {
        map.insert(key.to_string(), Value::String(value.trim().to_string()));
    }
}

fn clean_or_default(value: &str, fallback: &str) -> String {
    let clean = value.trim();
    if clean.is_empty() {
        fallback.to_string()
    } else {
        clean.to_string()
    }
}

fn safe_json_file_name(input: &str, fallback_stem: &str) -> String {
    let raw = Path::new(input.trim())
        .file_name()
        .and_then(|name| name.to_str())
        .unwrap_or("");
    let stem = raw.strip_suffix(".json").unwrap_or(raw).trim().to_string();
    let safe_stem = if stem.is_empty() {
        slugify(fallback_stem)
    } else {
        slugify(&stem)
    };
    format!("{safe_stem}.json")
}

fn unique_json_file_name(dir: &Path, requested: &str) -> String {
    let requested = safe_json_file_name(requested, "persona");
    if !dir.join(&requested).exists() {
        return requested;
    }
    let stem = requested.trim_end_matches(".json");
    for i in 2..1000 {
        let candidate = format!("{stem}-{i}.json");
        if !dir.join(&candidate).exists() {
            return candidate;
        }
    }
    format!("{stem}-copy.json")
}

fn slugify(input: &str) -> String {
    let mut out = String::new();
    let mut last_dash = false;
    for ch in input.chars() {
        if ch.is_ascii_alphanumeric() {
            out.push(ch.to_ascii_lowercase());
            last_dash = false;
        } else if !last_dash {
            out.push('-');
            last_dash = true;
        }
    }
    let slug = out.trim_matches('-');
    if slug.is_empty() {
        "persona".to_string()
    } else {
        slug.to_string()
    }
}

fn collapse_whitespace(input: &str) -> String {
    input.split_whitespace().collect::<Vec<_>>().join(" ")
}

fn trim_to_chars(input: &str, max: usize) -> String {
    if input.chars().count() <= max {
        input.to_string()
    } else {
        format!("{}...", input.chars().take(max).collect::<String>())
    }
}

fn infer_name(input: &str) -> Option<String> {
    extract_after_markers(input, &["named ", "called "]).map(|name| {
        name.split([',', '.', ';'])
            .next()
            .unwrap_or(&name)
            .trim()
            .to_string()
    })
}

fn infer_title(input: &str) -> Option<String> {
    let role = extract_after_markers(
        input,
        &[
            "persona for ",
            "user is a ",
            "user is an ",
            "as a ",
            "as an ",
            "for a ",
            "for an ",
        ],
    )?;
    Some(title_case_fragment(&role))
}

fn infer_organization(input: &str) -> Option<String> {
    extract_after_markers(input, &[" at ", " from "]).map(|org| title_case_fragment(&org))
}

fn extract_after_markers(input: &str, markers: &[&str]) -> Option<String> {
    let lower = input.to_ascii_lowercase();
    for marker in markers {
        if let Some(idx) = lower.find(marker) {
            let start = idx + marker.len();
            let rest = input.get(start..).unwrap_or("").trim();
            let end = first_boundary(rest);
            let candidate = strip_leading_article(rest[..end].trim());
            if !candidate.is_empty() {
                return Some(candidate.to_string());
            }
        }
    }
    None
}

fn first_boundary(input: &str) -> usize {
    [
        ",", ".", ";", ":", " at ", " from ", " who ", " that ", " needs ", " wants ", " with ",
    ]
    .iter()
    .filter_map(|needle| input.find(needle))
    .min()
    .unwrap_or(input.len())
}

fn strip_leading_article(input: &str) -> &str {
    input
        .strip_prefix("a ")
        .or_else(|| input.strip_prefix("an "))
        .or_else(|| input.strip_prefix("the "))
        .unwrap_or(input)
}

fn title_case_fragment(input: &str) -> String {
    let fragment = input
        .split_whitespace()
        .take(8)
        .collect::<Vec<_>>()
        .join(" ");
    let mut out = String::new();
    for word in fragment.split_whitespace() {
        if !out.is_empty() {
            out.push(' ');
        }
        let mut chars = word.chars();
        if let Some(first) = chars.next() {
            out.push(first.to_ascii_uppercase());
            out.extend(chars);
        }
    }
    out
}

fn keyword_cues(input: &str) -> Vec<String> {
    let stop_words = [
        "about", "after", "against", "because", "before", "being", "their", "there", "these",
        "those", "through", "using", "wants", "needs", "without", "workflow", "person", "persona",
        "users", "user",
    ];
    let mut cues = Vec::new();
    for raw in input.split_whitespace() {
        let word = raw
            .trim_matches(|ch: char| !ch.is_ascii_alphanumeric() && ch != '-')
            .to_ascii_lowercase();
        if word.len() < 5 || stop_words.contains(&word.as_str()) {
            continue;
        }
        if !cues.iter().any(|cue| cue == &word) {
            cues.push(word);
        }
        if cues.len() == 6 {
            break;
        }
    }
    cues
}

fn update_form(mut form: Signal<PersonaForm>, update: impl FnOnce(&mut PersonaForm)) {
    let mut next = form.read().clone();
    update(&mut next);
    form.set(next);
}

fn bump_reload(mut reload_tick: Signal<u64>) {
    let next = {
        let current = *reload_tick.read();
        current + 1
    };
    reload_tick.set(next);
}

#[component]
pub fn PersonasPanel(root: Signal<String>, skill_path: Signal<String>) -> Element {
    let mut personas = use_signal(Vec::<PersonaSummary>::new);
    let mut selected = use_signal(|| None::<String>);
    let mut form = use_signal(PersonaForm::default);
    let mut notes = use_signal(String::new);
    let mut status = use_signal(|| None::<String>);
    let reload_tick = use_signal(|| 0_u64);
    let mut delete_confirm = use_signal(|| None::<String>);

    use_effect(move || {
        let _ = *reload_tick.read();
        let r = root.read().clone();
        let s = skill_path.read().clone();
        match load_personas(&r, &s) {
            Ok(list) => {
                let current = selected.read().clone();
                let next_selected = current
                    .filter(|file| list.iter().any(|persona| persona.file_name == *file))
                    .or_else(|| list.first().map(|persona| persona.file_name.clone()));
                personas.set(list);
                if selected.read().as_ref() != next_selected.as_ref() {
                    selected.set(next_selected.clone());
                    if let Some(file_name) = next_selected {
                        match load_persona_form(&r, &s, &file_name) {
                            Ok(next_form) => form.set(next_form),
                            Err(err) => status.set(Some(err)),
                        }
                    } else {
                        form.set(PersonaForm::default());
                    }
                }
            }
            Err(err) => status.set(Some(err)),
        }
    });

    let current_form = form.read().clone();
    let current_notes = notes.read().clone();
    let current_status = status.read().clone();
    let selected_file = selected.read().clone();
    let delete_target = delete_confirm.read().clone();
    let persona_count = personas.read().len();
    let personas_dir_display = personas_dir(&root.read(), &skill_path.read())
        .to_string_lossy()
        .to_string();

    rsx! {
        div { class: "persona-studio",
            div { class: "persona-nav",
                div { class: "persona-nav-header",
                    div { class: "persona-title", "Personas" }
                    div { class: "persona-count", "{persona_count}" }
                }
                div { class: "persona-generate",
                    textarea {
                        class: "persona-seed-input",
                        placeholder: "Describe a user segment...",
                        value: "{current_notes}",
                        oninput: move |evt| notes.set(evt.value()),
                    }
                    button {
                        class: "btn btn-sm btn-go",
                        disabled: notes.read().trim().is_empty(),
                        onclick: move |_| {
                            let draft = generate_persona_from_notes(&notes.read(), personas.read().len() + 1);
                            selected.set(None);
                            delete_confirm.set(None);
                            form.set(draft);
                            status.set(Some("Draft generated. Review and save.".to_string()));
                        },
                        "Generate Draft"
                    }
                }
                div { class: "persona-nav-actions",
                    button {
                        class: "btn btn-sm",
                        onclick: move |_| {
                            selected.set(None);
                            delete_confirm.set(None);
                            form.set(PersonaForm {
                                file_name: "new-persona.json".to_string(),
                                ..PersonaForm::default()
                            });
                            status.set(None);
                        },
                        "New"
                    }
                    button {
                        class: "btn btn-sm",
                        onclick: move |_| bump_reload(reload_tick),
                        "Refresh"
                    }
                }
                div { class: "persona-list",
                    if personas.read().is_empty() {
                        div { class: "persona-empty", "No personas saved." }
                    }
                    for persona in personas.read().iter() {
                        {
                            let file_name = persona.file_name.clone();
                            let active = selected_file.as_ref() == Some(&persona.file_name);
                            rsx! {
                                div {
                                    key: "{persona.file_name}",
                                    class: if active { "persona-list-item persona-list-item-active" } else { "persona-list-item" },
                                    title: "{persona.path_display}",
                                    onclick: move |_| {
                                        let r = root.read().clone();
                                        let s = skill_path.read().clone();
                                        selected.set(Some(file_name.clone()));
                                        delete_confirm.set(None);
                                        match load_persona_form(&r, &s, &file_name) {
                                            Ok(next_form) => {
                                                form.set(next_form);
                                                status.set(None);
                                            }
                                            Err(err) => status.set(Some(err)),
                                        }
                                    },
                                    div { class: "persona-list-name", "{persona.name}" }
                                    if !persona.title.is_empty() {
                                        div { class: "persona-list-role", "{persona.title}" }
                                    }
                                    if !persona.recognition_cues.is_empty() {
                                        div { class: "persona-list-cue", "{trim_to_chars(&persona.recognition_cues[0], 76)}" }
                                    }
                                }
                            }
                        }
                    }
                }
            }

            div { class: "persona-editor",
                div { class: "persona-editor-toolbar",
                    div { class: "persona-path", title: "{personas_dir_display}", "{personas_dir_display}" }
                    div { class: "persona-toolbar-actions",
                        button {
                            class: "btn btn-sm btn-go",
                            disabled: !current_form.has_content(),
                            onclick: move |_| {
                                let r = root.read().clone();
                                let s = skill_path.read().clone();
                                let current = form.read().clone();
                                match save_persona(&r, &s, &current) {
                                    Ok(file_name) => {
                                        selected.set(Some(file_name.clone()));
                                        delete_confirm.set(None);
                                        match load_persona_form(&r, &s, &file_name) {
                                            Ok(saved_form) => form.set(saved_form),
                                            Err(err) => status.set(Some(err)),
                                        }
                                        bump_reload(reload_tick);
                                        status.set(Some(format!("Saved {file_name}")));
                                    }
                                    Err(err) => status.set(Some(err)),
                                }
                            },
                            "Save"
                        }
                        button {
                            class: "btn btn-sm btn-danger",
                            disabled: current_form.original_file_name.is_none(),
                            onclick: move |_| {
                                let original_file_name = {
                                    form.read().original_file_name.clone()
                                };
                                if let Some(file_name) = original_file_name {
                                    if delete_confirm.read().as_ref() == Some(&file_name) {
                                        let r = root.read().clone();
                                        let s = skill_path.read().clone();
                                        match delete_persona(&r, &s, &file_name) {
                                            Ok(()) => {
                                                selected.set(None);
                                                form.set(PersonaForm::default());
                                                delete_confirm.set(None);
                                                bump_reload(reload_tick);
                                                status.set(Some(format!("Deleted {file_name}")));
                                            }
                                            Err(err) => status.set(Some(err)),
                                        }
                                    } else {
                                        delete_confirm.set(Some(file_name));
                                        status.set(Some("Click Delete again to confirm.".to_string()));
                                    }
                                }
                            },
                            if delete_target.as_ref() == current_form.original_file_name.as_ref() {
                                "Confirm Delete"
                            } else {
                                "Delete"
                            }
                        }
                    }
                }

                if let Some(message) = current_status {
                    div { class: "persona-status", "{message}" }
                }

                if !current_form.has_content() {
                    div { class: "persona-editor-empty", "Select a persona or generate a draft." }
                } else {
                    div { class: "persona-form",
                        div { class: "persona-form-grid",
                            label { class: "persona-field",
                                span { "File" }
                                input {
                                    class: "text-input",
                                    r#type: "text",
                                    value: "{current_form.file_name}",
                                    oninput: move |evt| update_form(form, move |draft| draft.file_name = evt.value()),
                                }
                            }
                            label { class: "persona-field",
                                span { "Name" }
                                input {
                                    class: "text-input",
                                    r#type: "text",
                                    value: "{current_form.name}",
                                    oninput: move |evt| update_form(form, move |draft| draft.name = evt.value()),
                                }
                            }
                            label { class: "persona-field",
                                span { "Role" }
                                input {
                                    class: "text-input",
                                    r#type: "text",
                                    value: "{current_form.title}",
                                    oninput: move |evt| update_form(form, move |draft| draft.title = evt.value()),
                                }
                            }
                            label { class: "persona-field",
                                span { "Organization" }
                                input {
                                    class: "text-input",
                                    r#type: "text",
                                    value: "{current_form.organization}",
                                    oninput: move |evt| update_form(form, move |draft| draft.organization = evt.value()),
                                }
                            }
                        }

                        label { class: "persona-field persona-field-wide",
                            span { "Summary" }
                            textarea {
                                class: "persona-textarea persona-textarea-sm",
                                value: "{current_form.summary}",
                                oninput: move |evt| update_form(form, move |draft| draft.summary = evt.value()),
                            }
                        }
                        label { class: "persona-field persona-field-wide",
                            span { "Communication Style" }
                            textarea {
                                class: "persona-textarea persona-textarea-sm",
                                value: "{current_form.communication_style}",
                                oninput: move |evt| update_form(form, move |draft| draft.communication_style = evt.value()),
                            }
                        }

                        div { class: "persona-section-label", "Persona Lens" }
                        div { class: "persona-form-grid persona-form-grid-facts",
                            label { class: "persona-field",
                                span { "Jobs To Be Done" }
                                textarea {
                                    class: "persona-textarea",
                                    value: "{current_form.jobs_to_be_done}",
                                    oninput: move |evt| update_form(form, move |draft| draft.jobs_to_be_done = evt.value()),
                                }
                            }
                            label { class: "persona-field",
                                span { "Pains" }
                                textarea {
                                    class: "persona-textarea",
                                    value: "{current_form.pains}",
                                    oninput: move |evt| update_form(form, move |draft| draft.pains = evt.value()),
                                }
                            }
                            label { class: "persona-field",
                                span { "Adoption Yes If" }
                                textarea {
                                    class: "persona-textarea",
                                    value: "{current_form.adoption_yes_if}",
                                    oninput: move |evt| update_form(form, move |draft| draft.adoption_yes_if = evt.value()),
                                }
                            }
                            label { class: "persona-field",
                                span { "Rejection No If" }
                                textarea {
                                    class: "persona-textarea",
                                    value: "{current_form.rejection_no_if}",
                                    oninput: move |evt| update_form(form, move |draft| draft.rejection_no_if = evt.value()),
                                }
                            }
                            label { class: "persona-field",
                                span { "Anti Goals" }
                                textarea {
                                    class: "persona-textarea",
                                    value: "{current_form.anti_goals}",
                                    oninput: move |evt| update_form(form, move |draft| draft.anti_goals = evt.value()),
                                }
                            }
                            label { class: "persona-field",
                                span { "Recognition Cues" }
                                textarea {
                                    class: "persona-textarea",
                                    value: "{current_form.recognition_cues}",
                                    oninput: move |evt| update_form(form, move |draft| draft.recognition_cues = evt.value()),
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    fn write_skill(dir: &Path) {
        let skill_dir = dir.join("assets/skills/user-personas");
        std::fs::create_dir_all(skill_dir.join("personas")).unwrap();
        std::fs::write(skill_dir.join("SKILL.md"), "# Personas\n").unwrap();
    }

    #[test]
    fn generate_persona_from_notes_populates_required_sections() {
        let form = generate_persona_from_notes(
            "Persona for a platform engineer at Acme who needs safer self-serve deploys.",
            1,
        );

        assert_eq!(form.title, "Platform Engineer");
        assert_eq!(form.organization, "Acme");
        assert!(form.jobs_to_be_done.contains("self-serve deploys"));
        assert!(form.pains.contains("Current process"));
        assert!(form.adoption_yes_if.contains("first run"));
        assert!(form.rejection_no_if.contains("replace working tools"));
        assert!(form.anti_goals.contains("generic demo"));
        assert!(form.recognition_cues.contains("platform"));
    }

    #[test]
    fn save_and_load_persona_roundtrip() {
        let dir = tempfile::tempdir().unwrap();
        write_skill(dir.path());
        let root = dir.path().to_str().unwrap();
        let skill = "assets/skills/user-personas/SKILL.md";
        let form = PersonaForm {
            file_name: "research-ops.json".to_string(),
            name: "Riley Chen".to_string(),
            title: "Research Ops Lead".to_string(),
            organization: "Northwind".to_string(),
            summary: "Coordinates research intake and synthesis.".to_string(),
            jobs_to_be_done: "Keep research findings reusable.".to_string(),
            pains: "Persona evidence gets scattered.".to_string(),
            adoption_yes_if: "Personas are durable project assets.".to_string(),
            rejection_no_if: "Requires a separate research repository.".to_string(),
            anti_goals: "Does not want marketing-only personas.".to_string(),
            recognition_cues: "Asks where evidence lives.".to_string(),
            ..PersonaForm::default()
        };

        let saved = save_persona(root, skill, &form).unwrap();
        assert_eq!(saved, "research-ops.json");

        let loaded = load_persona_form(root, skill, &saved).unwrap();
        assert_eq!(loaded.name, "Riley Chen");
        assert_eq!(loaded.title, "Research Ops Lead");
        assert_eq!(loaded.jobs_to_be_done, "Keep research findings reusable.");

        let summaries = load_personas(root, skill).unwrap();
        assert_eq!(summaries.len(), 1);
        assert_eq!(summaries[0].name, "Riley Chen");
        assert_eq!(
            summaries[0].jobs_to_be_done,
            vec!["Keep research findings reusable.".to_string()]
        );
    }

    #[test]
    fn save_preserves_unrelated_other_facts() {
        let dir = tempfile::tempdir().unwrap();
        write_skill(dir.path());
        let root = dir.path().to_str().unwrap();
        let skill = "assets/skills/user-personas/SKILL.md";
        let persona_path = dir
            .path()
            .join("assets/skills/user-personas/personas/operator.json");
        std::fs::write(
            &persona_path,
            serde_json::to_string_pretty(&json!({
                "type": "TinyPerson",
                "persona": {
                    "name": "Existing",
                    "other_facts": [
                        "current_stack: Kubernetes",
                        "jobs_to_be_done: Old job"
                    ]
                }
            }))
            .unwrap(),
        )
        .unwrap();

        let mut form = load_persona_form(root, skill, "operator.json").unwrap();
        form.jobs_to_be_done = "New job".to_string();
        save_persona(root, skill, &form).unwrap();

        let saved = std::fs::read_to_string(persona_path).unwrap();
        assert!(saved.contains("current_stack: Kubernetes"));
        assert!(saved.contains("jobs_to_be_done: New job"));
        assert!(!saved.contains("jobs_to_be_done: Old job"));
    }

    #[test]
    fn delete_persona_removes_file() {
        let dir = tempfile::tempdir().unwrap();
        write_skill(dir.path());
        let root = dir.path().to_str().unwrap();
        let skill = "assets/skills/user-personas/SKILL.md";
        let form = PersonaForm {
            file_name: "delete-me.json".to_string(),
            name: "Delete Me".to_string(),
            ..PersonaForm::default()
        };
        let saved = save_persona(root, skill, &form).unwrap();
        delete_persona(root, skill, &saved).unwrap();
        assert!(load_personas(root, skill).unwrap().is_empty());
    }
}