crabtalk-core 0.0.18

Core types and traits for the Crabtalk agent runtime
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
//! Runtime — agent registry, session management, and hook orchestration.
//!
//! [`Runtime`] holds agents as immutable definitions and sessions as
//! per-session `Arc<Mutex<Session>>` containers. Tool schemas are registered
//! once at startup via `hook.on_register_tools()`. Execution methods
//! (`send_to`, `stream_to`) take a session ID, lock the session, clone the
//! agent, and run with the session's history.

use crate::{
    Agent, AgentBuilder, AgentConfig, AgentEvent, AgentResponse, AgentStopReason,
    agent::tool::{ToolRegistry, ToolSender},
    model::{Message, Model},
    runtime::hook::Hook,
};
use anyhow::{Result, bail};
use async_stream::stream;
use futures_core::Stream;
use futures_util::StreamExt;
use std::{
    collections::{BTreeMap, HashSet},
    sync::{
        Arc,
        atomic::{AtomicU64, Ordering},
    },
};
use tokio::sync::{Mutex, RwLock, mpsc};

pub mod hook;
pub mod session;

pub use session::Session;

/// 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`.
pub struct Runtime<M: Model, H: Hook> {
    pub model: M,
    pub hook: H,
    agents: BTreeMap<String, Agent<M>>,
    sessions: RwLock<BTreeMap<u64, Arc<Mutex<Session>>>>,
    next_session_id: AtomicU64,
    pub tools: ToolRegistry,
    tool_tx: Option<ToolSender>,
    active_sessions: RwLock<HashSet<u64>>,
}

impl<M: Model + Send + Sync + Clone + 'static, H: Hook + 'static> Runtime<M, H> {
    /// 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).
    pub async fn new(model: M, hook: H, tool_tx: Option<ToolSender>) -> Self {
        let mut tools = ToolRegistry::new();
        hook.on_register_tools(&mut tools).await;
        Self {
            model,
            hook,
            agents: BTreeMap::new(),
            sessions: RwLock::new(BTreeMap::new()),
            next_session_id: AtomicU64::new(1),
            tools,
            tool_tx,
            active_sessions: RwLock::new(HashSet::new()),
        }
    }

    // --- Agent registry ---

    /// 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`.
    pub fn add_agent(&mut self, config: AgentConfig) {
        let config = self.hook.on_build_agent(config);
        let name = config.name.clone();
        let tools = self.tools.filtered_snapshot(&config.tools);
        let mut builder = AgentBuilder::new(self.model.clone())
            .config(config)
            .tools(tools);
        if let Some(tx) = &self.tool_tx {
            builder = builder.tool_tx(tx.clone());
        }
        let agent = builder.build();
        self.agents.insert(name, agent);
    }

    /// Get a registered agent's config by name (cloned).
    pub fn agent(&self, name: &str) -> Option<AgentConfig> {
        self.agents.get(name).map(|a| a.config.clone())
    }

    /// Get all registered agent configs (cloned, alphabetical order).
    pub fn agents(&self) -> Vec<AgentConfig> {
        self.agents.values().map(|a| a.config.clone()).collect()
    }

    /// Get a reference to an agent by name.
    pub fn get_agent(&self, name: &str) -> Option<&Agent<M>> {
        self.agents.get(name)
    }

    // --- Session management ---

    /// Get or create a session for the given (agent, created_by) identity.
    ///
    /// 1. Check in-memory sessions for a match → return existing ID.
    /// 2. Check disk for a persisted session file → load context, return ID.
    /// 3. Neither → create a new session with a fresh file.
    pub async fn get_or_create_session(&self, agent: &str, created_by: &str) -> Result<u64> {
        if !self.agents.contains_key(agent) {
            bail!("agent '{agent}' not registered");
        }

        // 1. In-memory lookup.
        {
            let sessions = self.sessions.read().await;
            for (id, session_mutex) in sessions.iter() {
                let s = session_mutex.lock().await;
                if s.agent == agent && s.created_by == created_by {
                    return Ok(*id);
                }
            }
        }

        // 2. Disk lookup — find latest session file for this identity.
        if let Some(path) =
            session::find_latest_session(&crate::paths::SESSIONS_DIR, agent, created_by)
            && let Ok((meta, messages)) = Session::load_context(&path)
        {
            let id = self.next_session_id.fetch_add(1, Ordering::Relaxed);
            let mut session = Session::new(id, agent, created_by);
            session.history = messages;
            session.title = meta.title;
            session.uptime_secs = meta.uptime_secs;
            session.file_path = Some(path);
            self.sessions
                .write()
                .await
                .insert(id, Arc::new(Mutex::new(session)));
            return Ok(id);
        }

        // 3. Create new.
        self.create_session(agent, created_by).await
    }

    /// Create a new session for the given agent. Returns the session ID.
    pub async fn create_session(&self, agent: &str, created_by: &str) -> Result<u64> {
        if !self.agents.contains_key(agent) {
            bail!("agent '{agent}' not registered");
        }
        let id = self.next_session_id.fetch_add(1, Ordering::Relaxed);
        let mut session = Session::new(id, agent, created_by);
        session.init_file(&crate::paths::SESSIONS_DIR);
        self.sessions
            .write()
            .await
            .insert(id, Arc::new(Mutex::new(session)));
        Ok(id)
    }

    /// Load a specific session from a file path. Returns the session ID.
    pub async fn load_specific_session(&self, file_path: &std::path::Path) -> Result<u64> {
        let (meta, messages) = Session::load_context(file_path)?;
        if !self.agents.contains_key(&meta.agent) {
            bail!("agent '{}' not registered", meta.agent);
        }
        let id = self.next_session_id.fetch_add(1, Ordering::Relaxed);
        let mut session = Session::new(id, &meta.agent, &meta.created_by);
        session.history = messages;
        session.title = meta.title;
        session.uptime_secs = meta.uptime_secs;
        session.file_path = Some(file_path.to_path_buf());
        self.sessions
            .write()
            .await
            .insert(id, Arc::new(Mutex::new(session)));
        Ok(id)
    }

    /// Close (remove) a session by ID. Returns true if it existed.
    pub async fn close_session(&self, id: u64) -> bool {
        self.sessions.write().await.remove(&id).is_some()
    }

    /// Get a session mutex by ID.
    pub async fn session(&self, id: u64) -> Option<Arc<Mutex<Session>>> {
        self.sessions.read().await.get(&id).cloned()
    }

    /// Get all session mutexes (for iteration/listing).
    pub async fn sessions(&self) -> Vec<Arc<Mutex<Session>>> {
        self.sessions.read().await.values().cloned().collect()
    }

    /// Check if a session is currently active (running send_to or stream_to).
    pub async fn is_active(&self, id: u64) -> bool {
        self.active_sessions.read().await.contains(&id)
    }

    /// Number of currently active sessions.
    pub async fn active_session_count(&self) -> usize {
        self.active_sessions.read().await.len()
    }

    /// 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.
    pub async fn compact_session(&self, session_id: u64) -> Option<String> {
        let (agent_name, history) = {
            let session_mutex = self.sessions.read().await.get(&session_id)?.clone();
            let session = session_mutex.lock().await;
            if session.history.is_empty() {
                return None;
            }
            (session.agent.clone(), session.history.clone())
        };
        self.agents.get(&agent_name)?.compact(&history).await
    }

    /// 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`).
    pub async fn transfer_sessions<M2: Model, H2: Hook>(&self, dest: &mut Runtime<M2, H2>) {
        let sessions = self.sessions.read().await;
        let dest_sessions = dest.sessions.get_mut();
        for (id, session) in sessions.iter() {
            dest_sessions.insert(*id, session.clone());
        }
        let next = self.next_session_id.load(Ordering::Relaxed);
        dest.next_session_id.store(next, Ordering::Relaxed);
    }

    /// Spawn a background task to generate a conversation title from the
    /// first user+assistant exchange. Non-blocking — the main flow continues.
    fn spawn_title_generation(&self, _session_id: u64, session_mutex: Arc<Mutex<Session>>) {
        let model = self.model.clone();
        tokio::spawn(async move {
            let (user_msg, assistant_msg) = {
                let session = session_mutex.lock().await;
                let user = session
                    .history
                    .iter()
                    .find(|m| m.role == crate::model::Role::User && !m.auto_injected)
                    .map(|m| m.content.clone());
                let assistant = session
                    .history
                    .iter()
                    .find(|m| m.role == crate::model::Role::Assistant)
                    .map(|m| m.content.clone());
                (user, assistant)
            };

            let Some(user) = user_msg else { return };
            let Some(assistant) = assistant_msg else {
                return;
            };

            // Truncate to keep the title-generation request small.
            let user_snippet: String = user.chars().take(200).collect();
            let assistant_snippet: String = assistant.chars().take(200).collect();

            let prompt = format!(
                "Summarize this conversation in 3-6 words as a short title. \
                 Return ONLY the title, nothing else.\n\n\
                 User: {user_snippet}\nAssistant: {assistant_snippet}"
            );

            let request = crate::model::Request::new(model.active_model())
                .with_messages(vec![Message::user(&prompt)]);

            match model.send(&request).await {
                Ok(response) => {
                    if let Some(title) = response.content() {
                        let title = title.trim().trim_matches('"').to_string();
                        if !title.is_empty() {
                            let mut session = session_mutex.lock().await;
                            if session.title.is_empty() {
                                session.set_title(&title);
                            }
                        }
                    }
                }
                Err(e) => {
                    tracing::debug!("title generation failed: {e}");
                }
            }
        });
    }

    // --- Execution ---

    /// Push the user message, strip old auto-injected messages, and inject
    /// fresh ones via `on_before_run`. Returns the agent name.
    fn prepare_history(&self, session: &mut Session, content: &str, sender: &str) -> String {
        let content = self.hook.preprocess(&session.agent, content);
        if sender.is_empty() {
            session.history.push(Message::user(&content));
        } else {
            session
                .history
                .push(Message::user_with_sender(&content, sender));
        }

        // Strip previous auto-injected messages to avoid accumulation.
        session.history.retain(|m| !m.auto_injected);

        let agent_name = session.agent.clone();
        let recall_msgs = self
            .hook
            .on_before_run(&agent_name, session.id, &session.history);
        if !recall_msgs.is_empty() {
            let insert_pos = session.history.len().saturating_sub(1);
            for (i, msg) in recall_msgs.into_iter().enumerate() {
                session.history.insert(insert_pos + i, msg);
            }
        }
        agent_name
    }

    /// Send a message to a session and run to completion.
    pub async fn send_to(
        &self,
        session_id: u64,
        content: &str,
        sender: &str,
    ) -> Result<AgentResponse> {
        let session_mutex = self
            .sessions
            .read()
            .await
            .get(&session_id)
            .cloned()
            .ok_or_else(|| anyhow::anyhow!("session {session_id} not found"))?;

        let mut session = session_mutex.lock().await;
        let pre_run_len = session.history.len();
        let agent_name = self.prepare_history(&mut session, content, sender);
        let agent_ref = self
            .agents
            .get(&session.agent)
            .ok_or_else(|| anyhow::anyhow!("agent '{}' not registered", session.agent))?;

        let (tx, mut rx) = mpsc::unbounded_channel();
        let run_start = std::time::Instant::now();
        self.active_sessions.write().await.insert(session_id);
        let response = agent_ref.run(&mut session.history, tx, None).await;
        self.active_sessions.write().await.remove(&session_id);
        session.uptime_secs += run_start.elapsed().as_secs();

        // Drain events, stash compact summary if one occurred.
        let mut compact_summary: Option<String> = None;
        while let Ok(event) = rx.try_recv() {
            if let AgentEvent::Compact { ref summary } = event {
                compact_summary = Some(summary.clone());
            }
            self.hook.on_event(&agent_name, session_id, &event);
        }

        // Append-only persistence.
        if let Some(summary) = compact_summary {
            // Compaction happened: append compact marker + post-compact messages.
            session.append_compact(&summary);
            // history[0] is the summary-as-user-message; skip it (compact line serves that role).
            if session.history.len() > 1 {
                session.append_messages(&session.history[1..]);
            }
        } else {
            // No compaction: append new messages since pre_run.
            session.append_messages(&session.history[pre_run_len..]);
        }

        // Persist updated uptime to meta line.
        session.rewrite_meta();

        // Generate title in background if this is the first exchange.
        if session.title.is_empty() && session.history.len() >= 2 {
            self.spawn_title_generation(session_id, session_mutex.clone());
        }
        Ok(response)
    }

    /// Send a message to a session and stream response events.
    pub fn stream_to(
        &self,
        session_id: u64,
        content: &str,
        sender: &str,
    ) -> impl Stream<Item = AgentEvent> + '_ {
        let content = content.to_owned();
        let sender = sender.to_owned();
        stream! {
            let session_mutex = match self
                .sessions
                .read()
                .await
                .get(&session_id)
                .cloned()
            {
                Some(m) => m,
                None => {
                    let resp = AgentResponse {
                        final_response: None,
                        iterations: 0,
                        stop_reason: AgentStopReason::Error(
                            format!("session {session_id} not found"),
                        ),
                        steps: vec![],
                        // No model involved in pre-run errors.
                        model: String::new(),
                    };
                    yield AgentEvent::Done(resp);
                    return;
                }
            };

            let mut session = session_mutex.lock().await;
            let pre_run_len = session.history.len();
            let agent_name = self.prepare_history(&mut session, &content, &sender);
            let agent_ref = match self.agents.get(&session.agent) {
                Some(a) => a,
                None => {
                    let resp = AgentResponse {
                        final_response: None,
                        iterations: 0,
                        stop_reason: AgentStopReason::Error(
                            format!("agent '{}' not registered", session.agent),
                        ),
                        steps: vec![],
                        // No model involved in pre-run errors.
                        model: String::new(),
                    };
                    yield AgentEvent::Done(resp);
                    return;
                }
            };

            let run_start = std::time::Instant::now();
            self.active_sessions.write().await.insert(session_id);
            let mut compact_summary: Option<String> = None;
            let mut done_event: Option<AgentEvent> = None;
            {
                let mut event_stream = std::pin::pin!(agent_ref.run_stream(&mut session.history, Some(session_id)));
                while let Some(event) = event_stream.next().await {
                    if let AgentEvent::Compact { ref summary } = event {
                        compact_summary = Some(summary.clone());
                    }
                    self.hook.on_event(&agent_name, session_id, &event);
                    // Hold back Done — yield it after persistence.
                    if matches!(event, AgentEvent::Done(_)) {
                        done_event = Some(event);
                    } else {
                        yield event;
                    }
                }
            }
            // Borrow on session.history is released. Persist now.
            self.active_sessions.write().await.remove(&session_id);
            session.uptime_secs += run_start.elapsed().as_secs();
            if let Some(summary) = compact_summary {
                session.append_compact(&summary);
                if session.history.len() > 1 {
                    session.append_messages(&session.history[1..]);
                }
            } else {
                session.append_messages(&session.history[pre_run_len..]);
            }
            // Persist updated uptime to meta line.
            session.rewrite_meta();

            // Generate title in background if this is the first exchange.
            if session.title.is_empty() && session.history.len() >= 2 {
                self.spawn_title_generation(session_id, session_mutex.clone());
            }
            // Now yield Done.
            if let Some(event) = done_event {
                yield event;
            }
        }
    }
}