Skip to main content

robson_core/
processor.rs

1use crate::entities::conversation;
2use crate::llm::LlmMessage;
3use anyhow::Result;
4use sea_orm::DatabaseConnection;
5
6/// Sanitized, validated input from a gateway (Slack, chat, webhook, etc).
7/// Defined here so robson-core remains free of robson-slack dependencies
8/// while gateway crates (robson-slack) can produce and return this type.
9#[derive(Debug, Clone)]
10pub enum SanitizedInput {
11    Message {
12        text: String,
13        channel_id: String,
14        user_id: String,
15        thread_ts: Option<String>,
16        ts: String,
17    },
18}
19
20pub struct ProcessorOutput {
21    pub response_text: String,
22}
23
24pub struct Processor {
25    db: DatabaseConnection,
26}
27
28impl Processor {
29    pub fn new(db: DatabaseConnection) -> Self {
30        Self { db }
31    }
32
33    pub async fn dispatch(&self, input: SanitizedInput) -> Result<ProcessorOutput> {
34        let SanitizedInput::Message {
35            text,
36            channel_id: _,
37            user_id: _,
38            thread_ts,
39            ts,
40        } = input;
41        let _thread = thread_ts.as_deref().unwrap_or(&ts);
42        Ok(ProcessorOutput {
43            response_text: format!("Echo: {}", text),
44        })
45    }
46
47    /// Build LLM context from the last `max_n` conversation turns.
48    pub async fn build_context(
49        &self,
50        channel_id: &str,
51        thread_ts: &str,
52        max_n: u64,
53    ) -> Result<Vec<LlmMessage>> {
54        let all = conversation::Model::find_by_thread(&self.db, channel_id, thread_ts).await?;
55        let total = all.len();
56        let skip = if total as u64 > max_n {
57            total - max_n as usize
58        } else {
59            0
60        };
61
62        let messages = all
63            .into_iter()
64            .skip(skip)
65            .map(|m| LlmMessage {
66                role: m.role.to_string(),
67                content: m.content,
68            })
69            .collect();
70
71        Ok(messages)
72    }
73}