terraphim_types 1.16.34

Core types crate for Terraphim AI
Documentation
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
//! Persona definition types for agent personas with SFIA skill framework support.
//!
//! This module provides types for defining agent personas with:
//! - Core characteristics and personality traits
//! - SFIA (Skills Framework for the Information Age) skill definitions
//! - TOML serialization/deserialization for persona configuration files
//!
//! # Example TOML
//!
//! ```toml
//! agent_name = "Terraphim Architect"
//! role_name = "Systems Architect"
//! name_origin = "Greek: Terra (Earth) + phainein (to show)"
//! vibe = "Thoughtful, grounded, precise, architectural"
//! symbol = "⚡"
//! speech_style = "Technical yet accessible"
//! terraphim_nature = "Earth spirit of knowledge architecture"
//! sfia_title = "Solution Architect"
//! primary_level = 5
//! guiding_phrase = "Structure precedes function"
//! level_essence = "Enables and ensures"
//!
//! [[core_characteristics]]
//! name = "Systems Thinking"
//! description = "Views problems holistically"
//!
//! [[core_characteristics]]
//! name = "Pattern Recognition"
//! description = "Identifies recurring structures"
//!
//! [[sfia_skills]]
//! code = "ARCH"
//! name = "Solution Architecture"
//! level = 5
//! description = "Designs and communicates solution architectures"
//! ```

use serde::{Deserialize, Serialize};
use std::path::Path;

/// A complete persona definition for an AI agent.
///
/// This struct captures both the personality characteristics and
/// professional skills (via SFIA framework) of an agent persona.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct PersonaDefinition {
    /// The agent's display name
    pub agent_name: String,
    /// The role/title of the agent
    pub role_name: String,
    /// Explanation of the agent's name origin
    pub name_origin: String,
    /// The overall vibe/personality of the agent
    pub vibe: String,
    /// Symbol or emoji representing the agent
    pub symbol: String,
    /// Core personality characteristics
    #[serde(default)]
    pub core_characteristics: Vec<CharacteristicDef>,
    /// How the agent speaks (style description)
    pub speech_style: String,
    /// Description of the agent's nature/persona
    pub terraphim_nature: String,
    /// SFIA professional title
    pub sfia_title: String,
    /// Primary SFIA skill level (typically 1-7)
    pub primary_level: u8,
    /// A guiding phrase for the persona
    pub guiding_phrase: String,
    /// Description of what the level represents
    pub level_essence: String,
    /// SFIA skills possessed by this persona
    #[serde(default)]
    pub sfia_skills: Vec<SfiaSkillDef>,
}

/// Definition of a core personality characteristic.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct CharacteristicDef {
    /// Name of the characteristic
    pub name: String,
    /// Description of how this characteristic manifests
    pub description: String,
}

/// SFIA skill definition.
///
/// SFIA (Skills Framework for the Information Age) provides a common
/// reference model for skills in the IT industry.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct SfiaSkillDef {
    /// SFIA skill code (e.g., "ARCH", "DESN")
    pub code: String,
    /// Full name of the skill
    pub name: String,
    /// Skill level (typically 1-7 in SFIA framework)
    pub level: u8,
    /// Description of skill at this level
    pub description: String,
}

impl PersonaDefinition {
    /// Parse a PersonaDefinition from a TOML string.
    ///
    /// # Arguments
    ///
    /// * `toml_str` - The TOML string to parse
    ///
    /// # Returns
    ///
    /// Returns `Ok(PersonaDefinition)` on success, or `Err(toml::de::Error)`
    /// if parsing fails.
    ///
    /// # Example
    ///
    /// ```
    /// use terraphim_types::PersonaDefinition;
    ///
    /// let toml = r#"
    ///     agent_name = "Test Agent"
    ///     role_name = "Tester"
    ///     name_origin = "Test"
    ///     vibe = "Helpful"
    ///     symbol = "T"
    ///     speech_style = "Clear"
    ///     terraphim_nature = "Test nature"
    ///     sfia_title = "Test Engineer"
    ///     primary_level = 3
    ///     guiding_phrase = "Test everything"
    ///     level_essence = "Ensures quality"
    /// "#;
    ///
    /// let persona = PersonaDefinition::from_toml(toml).unwrap();
    /// assert_eq!(persona.agent_name, "Test Agent");
    /// ```
    pub fn from_toml(toml_str: &str) -> Result<Self, toml::de::Error> {
        toml::from_str(toml_str)
    }

    /// Load a PersonaDefinition from a file.
    ///
    /// # Arguments
    ///
    /// * `path` - Path to the TOML file
    ///
    /// # Returns
    ///
    /// Returns `Ok(PersonaDefinition)` on success, or `Err(PersonaLoadError)`
    /// if the file cannot be read or parsed.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use terraphim_types::PersonaDefinition;
    ///
    /// let persona = PersonaDefinition::from_file("/path/to/persona.toml").unwrap();
    /// ```
    pub fn from_file(path: impl AsRef<Path>) -> Result<Self, PersonaLoadError> {
        let content = std::fs::read_to_string(path.as_ref()).map_err(PersonaLoadError::Io)?;
        Self::from_toml(&content).map_err(|e| PersonaLoadError::Parse(e.to_string()))
    }

    /// Serialize the persona to a TOML string.
    ///
    /// # Returns
    ///
    /// Returns `Ok(String)` containing the TOML representation, or
    /// `Err(toml::ser::Error)` if serialization fails.
    ///
    /// # Example
    ///
    /// ```
    /// use terraphim_types::PersonaDefinition;
    ///
    /// let toml = r#"
    ///     agent_name = "Test Agent"
    ///     role_name = "Tester"
    ///     name_origin = "Test"
    ///     vibe = "Helpful"
    ///     symbol = "T"
    ///     speech_style = "Clear"
    ///     terraphim_nature = "Test nature"
    ///     sfia_title = "Test Engineer"
    ///     primary_level = 3
    ///     guiding_phrase = "Test everything"
    ///     level_essence = "Ensures quality"
    /// "#;
    ///
    /// let persona = PersonaDefinition::from_toml(toml).unwrap();
    /// let output = persona.to_toml().unwrap();
    /// assert!(output.contains("agent_name = \"Test Agent\""));
    /// ```
    pub fn to_toml(&self) -> Result<String, toml::ser::Error> {
        toml::to_string_pretty(self)
    }
}

/// Errors that can occur when loading a persona definition.
#[derive(Debug, thiserror::Error)]
pub enum PersonaLoadError {
    /// IO error when reading the persona file.
    #[error("IO error reading persona file: {0}")]
    Io(#[from] std::io::Error),
    /// TOML parsing error.
    #[error("TOML parse error: {0}")]
    Parse(String),
    /// Persona not found at the specified path.
    #[error("Persona not found: {0}")]
    NotFound(String),
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::env::temp_dir;
    use std::fs;

    /// Minimal valid TOML parses into PersonaDefinition
    #[test]
    fn test_persona_from_toml_minimal() {
        let toml = r#"
            agent_name = "Test Agent"
            role_name = "Tester"
            name_origin = "Test"
            vibe = "Helpful"
            symbol = "T"
            speech_style = "Clear"
            terraphim_nature = "Test nature"
            sfia_title = "Test Engineer"
            primary_level = 3
            guiding_phrase = "Test everything"
            level_essence = "Ensures quality"
        "#;

        let persona = PersonaDefinition::from_toml(toml).unwrap();
        assert_eq!(persona.agent_name, "Test Agent");
        assert_eq!(persona.role_name, "Tester");
        assert_eq!(persona.primary_level, 3);
        assert!(persona.core_characteristics.is_empty());
        assert!(persona.sfia_skills.is_empty());
    }

    /// Full persona TOML with all fields parses correctly
    #[test]
    fn test_persona_from_toml_full() {
        let toml = r#"
            agent_name = "Terraphim Architect"
            role_name = "Systems Architect"
            name_origin = "Greek: Terra (Earth) + phainein (to show)"
            vibe = "Thoughtful, grounded, precise, architectural"
            symbol = "⚡"
            speech_style = "Technical yet accessible"
            terraphim_nature = "Earth spirit of knowledge architecture"
            sfia_title = "Solution Architect"
            primary_level = 5
            guiding_phrase = "Structure precedes function"
            level_essence = "Enables and ensures"

            [[core_characteristics]]
            name = "Systems Thinking"
            description = "Views problems holistically"

            [[core_characteristics]]
            name = "Pattern Recognition"
            description = "Identifies recurring structures"

            [[sfia_skills]]
            code = "ARCH"
            name = "Solution Architecture"
            level = 5
            description = "Designs and communicates solution architectures"

            [[sfia_skills]]
            code = "DESN"
            name = "Systems Design"
            level = 5
            description = "Specifies and designs large-scale systems"
        "#;

        let persona = PersonaDefinition::from_toml(toml).unwrap();
        assert_eq!(persona.agent_name, "Terraphim Architect");
        assert_eq!(persona.symbol, "");
        assert_eq!(persona.primary_level, 5);
        assert_eq!(persona.core_characteristics.len(), 2);
        assert_eq!(persona.core_characteristics[0].name, "Systems Thinking");
        assert_eq!(persona.sfia_skills.len(), 2);
        assert_eq!(persona.sfia_skills[0].code, "ARCH");
        assert_eq!(persona.sfia_skills[1].name, "Systems Design");
    }

    /// from_toml(to_toml(def)) produces identical struct
    #[test]
    fn test_persona_roundtrip() {
        let toml = r#"
            agent_name = "Test Agent"
            role_name = "Tester"
            name_origin = "Test"
            vibe = "Helpful"
            symbol = "T"
            speech_style = "Clear"
            terraphim_nature = "Test nature"
            sfia_title = "Test Engineer"
            primary_level = 3
            guiding_phrase = "Test everything"
            level_essence = "Ensures quality"

            [[core_characteristics]]
            name = "Test Char"
            description = "A test characteristic"

            [[sfia_skills]]
            code = "TEST"
            name = "Testing"
            level = 3
            description = "Tests things"
        "#;

        let persona = PersonaDefinition::from_toml(toml).unwrap();
        let output = persona.to_toml().unwrap();
        let reparsed = PersonaDefinition::from_toml(&output).unwrap();

        assert_eq!(persona, reparsed);
    }

    /// Missing agent_name returns parse error
    #[test]
    fn test_persona_missing_required_field() {
        let toml = r#"
            role_name = "Tester"
            name_origin = "Test"
            vibe = "Helpful"
            symbol = "T"
            speech_style = "Clear"
            terraphim_nature = "Test nature"
            sfia_title = "Test Engineer"
            primary_level = 3
            guiding_phrase = "Test everything"
            level_essence = "Ensures quality"
        "#;

        let result = PersonaDefinition::from_toml(toml);
        assert!(result.is_err());
    }

    /// Array of {name, description} objects parses
    #[test]
    fn test_persona_characteristic_parsing() {
        let toml = r#"
            agent_name = "Test"
            role_name = "Tester"
            name_origin = "Test"
            vibe = "Helpful"
            symbol = "T"
            speech_style = "Clear"
            terraphim_nature = "Test"
            sfia_title = "Tester"
            primary_level = 3
            guiding_phrase = "Test"
            level_essence = "Test"

            [[core_characteristics]]
            name = "First"
            description = "First characteristic"

            [[core_characteristics]]
            name = "Second"
            description = "Second characteristic"

            [[core_characteristics]]
            name = "Third"
            description = "Third characteristic"
        "#;

        let persona = PersonaDefinition::from_toml(toml).unwrap();
        assert_eq!(persona.core_characteristics.len(), 3);
        assert_eq!(persona.core_characteristics[1].name, "Second");
        assert_eq!(
            persona.core_characteristics[1].description,
            "Second characteristic"
        );
    }

    /// Array of {code, name, level, description} objects parses
    #[test]
    fn test_persona_sfia_skill_parsing() {
        let toml = r#"
            agent_name = "Test"
            role_name = "Tester"
            name_origin = "Test"
            vibe = "Helpful"
            symbol = "T"
            speech_style = "Clear"
            terraphim_nature = "Test"
            sfia_title = "Tester"
            primary_level = 3
            guiding_phrase = "Test"
            level_essence = "Test"

            [[sfia_skills]]
            code = "CODE1"
            name = "Skill One"
            level = 2
            description = "First skill"

            [[sfia_skills]]
            code = "CODE2"
            name = "Skill Two"
            level = 4
            description = "Second skill"
        "#;

        let persona = PersonaDefinition::from_toml(toml).unwrap();
        assert_eq!(persona.sfia_skills.len(), 2);
        assert_eq!(persona.sfia_skills[0].code, "CODE1");
        assert_eq!(persona.sfia_skills[0].level, 2);
        assert_eq!(persona.sfia_skills[1].name, "Skill Two");
        assert_eq!(persona.sfia_skills[1].level, 4);
    }

    /// Level 0 and level 8 are accepted (no range enforcement at type level)
    #[test]
    fn test_persona_sfia_level_bounds() {
        let toml = r#"
            agent_name = "Test"
            role_name = "Tester"
            name_origin = "Test"
            vibe = "Helpful"
            symbol = "T"
            speech_style = "Clear"
            terraphim_nature = "Test"
            sfia_title = "Tester"
            primary_level = 0
            guiding_phrase = "Test"
            level_essence = "Test"

            [[sfia_skills]]
            code = "ZERO"
            name = "Zero Level"
            level = 0
            description = "Level zero"

            [[sfia_skills]]
            code = "EIGHT"
            name = "Eight Level"
            level = 8
            description = "Level eight"
        "#;

        let persona = PersonaDefinition::from_toml(toml).unwrap();
        assert_eq!(persona.primary_level, 0);
        assert_eq!(persona.sfia_skills[0].level, 0);
        assert_eq!(persona.sfia_skills[1].level, 8);
    }

    /// Missing file returns PersonaLoadError::Io
    #[test]
    fn test_persona_from_file_not_found() {
        let path = temp_dir().join("nonexistent_persona_12345.toml");
        let result = PersonaDefinition::from_file(&path);

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("IO error"));
    }

    /// Invalid TOML returns PersonaLoadError::Parse
    #[test]
    fn test_persona_from_file_invalid_toml() {
        let temp_file = temp_dir().join("invalid_persona_test.toml");
        fs::write(&temp_file, "this is not valid toml = [").unwrap();

        let result = PersonaDefinition::from_file(&temp_file);
        fs::remove_file(&temp_file).unwrap();

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.to_string().contains("TOML parse error"));
    }

    /// Clone and PartialEq derive work correctly
    #[test]
    fn test_persona_definition_clone_eq() {
        let toml = r#"
            agent_name = "Test Agent"
            role_name = "Tester"
            name_origin = "Test"
            vibe = "Helpful"
            symbol = "T"
            speech_style = "Clear"
            terraphim_nature = "Test nature"
            sfia_title = "Test Engineer"
            primary_level = 3
            guiding_phrase = "Test everything"
            level_essence = "Ensures quality"
        "#;

        let persona = PersonaDefinition::from_toml(toml).unwrap();
        let cloned = persona.clone();

        assert_eq!(persona, cloned);
        assert!(persona.agent_name == cloned.agent_name);
    }
}