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
//! Persona API — multi-persona management.
//!
//! `PersonaApi` is the public surface over `PersonaManager`. RFC-039 adds
//! `set_active_with_persist` + `persist`, which also flushes the full
//! registry via the shared `StateApi`, and `manager()` for callers that
//! need the underlying `Arc<PersonaManager>` (e.g. boot-time
//! `load_from_state_store` / `apply_config`).
use crate::persona::Persona;
use crate::persona::PersonaManager;
use std::sync::Arc;
/// Persona management system calls.
pub struct PersonaApi {
pub(crate) persona_manager: Arc<PersonaManager>,
/// RFC-039: optional callback invoked after a successful activate/persist
/// to re-seed the intent engine's system_prompt.
reseed_callback: Option<Arc<dyn Fn(Option<String>) + Send + Sync>>,
}
impl PersonaApi {
/// Create a new PersonaApi.
pub fn new(persona_manager: Arc<PersonaManager>) -> Self {
Self {
persona_manager,
reseed_callback: None,
}
}
/// Set the callback that re-seeds the intent engine's system_prompt.
/// Called automatically by `set_active_with_persist`.
pub fn set_reseed_callback(&mut self, cb: Option<Arc<dyn Fn(Option<String>) + Send + Sync>>) {
self.reseed_callback = cb;
}
/// Underlying `Arc<PersonaManager>` for boot-time wiring.
pub fn manager(&self) -> Arc<PersonaManager> {
Arc::clone(&self.persona_manager)
}
/// List all personas.
pub fn list(&self) -> Vec<Persona> {
self.persona_manager.store().list_all()
}
/// Get persona by ID.
pub fn get(&self, id: &str) -> Option<Persona> {
self.persona_manager.store().get(id)
}
/// Create a new persona (in-memory only; persistence requires an
/// explicit `persist` call after this — see `handle_persona_create`).
pub fn create(&self, persona: Persona) {
self.persona_manager.store().register(persona);
}
/// Update a persona (in-memory only; call `persist` after).
pub fn update(&self, id: &str, persona: Persona) -> anyhow::Result<()> {
self.persona_manager.store().update(id, persona)
}
/// Delete a persona (in-memory only; call `persist` after).
pub fn delete(&self, id: &str) -> anyhow::Result<()> {
self.persona_manager.store().delete(id)
}
/// Get active persona.
pub fn active(&self) -> Option<Persona> {
self.persona_manager.get_active_persona()
}
/// Get active persona ID.
pub fn active_id(&self) -> Option<String> {
self.persona_manager.active_persona_id()
}
/// Legacy in-memory `set_active` (no persistence).
/// Prefer `set_active_with_persist` for new callers.
pub fn set_active(&self, id: &str) -> anyhow::Result<()> {
self.persona_manager.set_active_persona(id)
}
/// RFC-039: set active persona + persist the full registry via `StateApi`.
/// Returns the new system_prompt so the caller can re-seed the intent
/// engine (kernel <-> ouroboros dependency direction avoidance).
pub async fn set_active_with_persist(
&self,
id: &str,
state_api: &crate::kernel_handle::StateApi,
) -> anyhow::Result<Option<String>> {
self.persona_manager.set_active(id, None).await?;
self.persist(state_api).await?;
let prompt = self
.persona_manager
.get_active_persona()
.map(|p| p.system_prompt);
// RFC-039: auto re-seed intent engine if callback is set.
if let Some(ref cb) = self.reseed_callback {
cb(prompt.clone());
}
Ok(prompt)
}
/// RFC-039: persist the full persona registry to `StateStore`.
/// Call this after every mutation (`create`/`update`/`delete`) so the
/// in-memory state matches the on-disk state. Otherwise the next
/// restart will lose the change.
pub async fn persist(&self, state_api: &crate::kernel_handle::StateApi) -> anyhow::Result<()> {
let snapshot = crate::persona::persistence::PersonaSnapshot {
schema_version: 1,
active_persona_id: self.persona_manager.active_persona_id(),
personas: self.persona_manager.store().list_all(),
};
state_api.save("personas", "index", &snapshot).await
}
/// Get persona count.
pub fn count(&self) -> usize {
self.persona_manager.store().len()
}
/// List enabled personas.
pub fn list_enabled(&self) -> Vec<Persona> {
self.persona_manager.store().list_enabled()
}
}