pub struct Runtime<M: Model, H: Hook> {
pub model: M,
pub hook: H,
/* private fields */
}Expand description
The crabtalk runtime — agent registry, session store, and hook orchestration.
Agents are stored as plain immutable values. Sessions own conversation
history behind per-session Arc<Mutex<Session>>. The sessions map uses
RwLock for concurrent access without requiring &mut self.
Fields§
§model: M§hook: HImplementations§
Source§impl<M: Model + Send + Sync + Clone + 'static, H: Hook + 'static> Runtime<M, H>
impl<M: Model + Send + Sync + Clone + 'static, H: Hook + 'static> Runtime<M, H>
Sourcepub async fn new(model: M, hook: H, tool_tx: Option<ToolSender>) -> Self
pub async fn new(model: M, hook: H, tool_tx: Option<ToolSender>) -> Self
Create a new runtime with the given model and hook backend.
Calls hook.on_register_tools() to populate the schema registry.
Pass tool_tx to enable tool dispatch from agents; None means agents
have no tool dispatch (e.g. CLI without a daemon).
Sourcepub fn register_tool(&mut self, tool: Tool)
pub fn register_tool(&mut self, tool: Tool)
Register a tool schema.
Sourcepub fn unregister_tool(&mut self, name: &str) -> bool
pub fn unregister_tool(&mut self, name: &str) -> bool
Remove a tool schema by name. Returns true if it existed.
Sourcepub fn add_agent(&mut self, config: AgentConfig)
pub fn add_agent(&mut self, config: AgentConfig)
Register an agent from its configuration.
Calls hook.on_build_agent(config) to enrich the config, then builds
the agent with a filtered schema snapshot and the runtime’s tool_tx.
Sourcepub fn agent(&self, name: &str) -> Option<AgentConfig>
pub fn agent(&self, name: &str) -> Option<AgentConfig>
Get a registered agent’s config by name (cloned).
Sourcepub fn agents(&self) -> Vec<AgentConfig>
pub fn agents(&self) -> Vec<AgentConfig>
Get all registered agent configs (cloned, alphabetical order).
Sourcepub async fn get_or_create_session(
&self,
agent: &str,
created_by: &str,
) -> Result<u64>
pub async fn get_or_create_session( &self, agent: &str, created_by: &str, ) -> Result<u64>
Get or create a session for the given (agent, created_by) identity.
- Check in-memory sessions for a match → return existing ID.
- Check disk for a persisted session file → load context, return ID.
- Neither → create a new session with a fresh file.
Sourcepub async fn create_session(&self, agent: &str, created_by: &str) -> Result<u64>
pub async fn create_session(&self, agent: &str, created_by: &str) -> Result<u64>
Create a new session for the given agent. Returns the session ID.
Sourcepub async fn load_specific_session(&self, file_path: &Path) -> Result<u64>
pub async fn load_specific_session(&self, file_path: &Path) -> Result<u64>
Load a specific session from a file path. Returns the session ID.
Sourcepub async fn close_session(&self, id: u64) -> bool
pub async fn close_session(&self, id: u64) -> bool
Close (remove) a session by ID. Returns true if it existed.
Sourcepub async fn sessions(&self) -> Vec<Arc<Mutex<Session>>>
pub async fn sessions(&self) -> Vec<Arc<Mutex<Session>>>
Get all session mutexes (for iteration/listing).
Sourcepub async fn is_active(&self, id: u64) -> bool
pub async fn is_active(&self, id: u64) -> bool
Check if a session is currently active (running send_to or stream_to).
Sourcepub async fn active_session_count(&self) -> usize
pub async fn active_session_count(&self) -> usize
Number of currently active sessions.
Sourcepub async fn compact_session(&self, session_id: u64) -> Option<String>
pub async fn compact_session(&self, session_id: u64) -> Option<String>
Compact a session’s history into a concise summary.
Clones history to release the lock before the LLM call.
Returns None if session/agent not found, history empty, or LLM fails.
Sourcepub async fn transfer_sessions<M2: Model, H2: Hook>(
&self,
dest: &mut Runtime<M2, H2>,
)
pub async fn transfer_sessions<M2: Model, H2: Hook>( &self, dest: &mut Runtime<M2, H2>, )
Move all sessions from this runtime into dest.
Used during daemon reload to preserve gateway sessions. The dest
runtime must not yet be shared (call before wrapping in Arc).