Skip to main content

rigspark_runtime/
chat_service.rs

1use crate::{
2    context::{ContextBundle, ContextRef, DisclosureStore},
3    harness::{ChatHarness, DeltaSink, HarnessError, HarnessMessage, HarnessRequest},
4    library::Library,
5    memory::{CaptureOptions, MemoryStore},
6    sessions::{SessionDoc, SessionError, SessionRepository, StoredMessage},
7    workspace::{WorkspaceError, WorkspaceService},
8};
9use tokio_util::sync::CancellationToken;
10
11#[derive(Debug, thiserror::Error)]
12pub enum ChatError {
13    #[error("invalid chat request")]
14    Invalid,
15    #[error("context disclosure requires approval")]
16    Disclosure,
17    #[error(transparent)]
18    Harness(#[from] HarnessError),
19    #[error(transparent)]
20    Session(#[from] SessionError),
21    #[error(transparent)]
22    Workspace(#[from] WorkspaceError),
23    #[error("library composition failed")]
24    Library,
25}
26pub struct ChatOptions<'request> {
27    pub session_id: &'request str,
28    pub revision: u64,
29    pub provider: &'request str,
30    pub model: &'request str,
31    pub message: &'request str,
32    pub context: &'request [ContextRef],
33    pub agent: Option<&'request str>,
34    pub skills: &'request [String],
35    pub temperature: Option<f64>,
36}
37pub struct PreparedChat {
38    session_id: String,
39    revision: u64,
40    provider: String,
41    message: String,
42    request: HarnessRequest,
43    context: ContextBundle,
44}
45impl PreparedChat {
46    pub fn context(&self) -> &ContextBundle {
47        &self.context
48    }
49    pub fn provider(&self) -> &str {
50        &self.provider
51    }
52}
53pub struct ChatOutcome {
54    pub text: String,
55    pub session: SessionDoc,
56    pub memory_captured: Option<bool>,
57}
58pub struct AgentRuntime<'runtime> {
59    pub chat: &'runtime dyn crate::agent::AgentChat,
60    pub manager: &'runtime mut crate::mcp::Manager,
61    pub grants: &'runtime mut crate::tool_policy::SessionGrants,
62    pub approver: Option<&'runtime dyn crate::agent::ToolApprover>,
63    pub workspace_id: Option<&'runtime str>,
64    pub max_steps: usize,
65}
66pub struct ChatService<'service> {
67    pub sessions: &'service SessionRepository,
68    pub library: &'service Library,
69    pub workspace: &'service WorkspaceService,
70    pub disclosures: &'service mut DisclosureStore,
71}
72impl ChatService<'_> {
73    pub fn prepare(&self, options: ChatOptions<'_>) -> Result<PreparedChat, ChatError> {
74        self.prepare_with_prompt(options, None)
75    }
76    pub fn prepare_with_prompt(
77        &self,
78        options: ChatOptions<'_>,
79        system_prompt: Option<&str>,
80    ) -> Result<PreparedChat, ChatError> {
81        if system_prompt.is_some_and(|text| text.encode_utf16().count() > 48 * 1024) {
82            return Err(ChatError::Invalid);
83        }
84        if options.message.trim().is_empty()
85            || options.message.encode_utf16().count() > 32768
86            || !["local", "claude", "openai", "openai-compatible", "opencode"]
87                .contains(&options.provider)
88        {
89            return Err(ChatError::Invalid);
90        }
91        let session = self
92            .sessions
93            .get(options.session_id)?
94            .ok_or(ChatError::Invalid)?;
95        if session.archived || session.revision != options.revision {
96            return Err(SessionError::Conflict.into());
97        }
98        let context = ContextBundle::resolve(self.workspace, options.context)?;
99        let mut messages = Vec::new();
100        if let Some(prompt) = system_prompt.filter(|text| !text.trim().is_empty()) {
101            messages.push(HarnessMessage {
102                role: "system".into(),
103                content: crate::sessions::gui_text(prompt),
104            });
105        }
106        if let Some(instructions) = self
107            .library
108            .compose(options.agent, options.skills)
109            .map_err(|_| ChatError::Library)?
110        {
111            messages.push(HarnessMessage {
112                role: "system".into(),
113                content: instructions,
114            });
115        }
116        if !context.text.is_empty() {
117            messages.push(HarnessMessage {
118                role: "system".into(),
119                content: context.text.clone(),
120            });
121        }
122        messages.extend(
123            session
124                .messages
125                .iter()
126                .skip(session.messages.len().saturating_sub(20))
127                .map(|message| HarnessMessage {
128                    role: message.role.clone(),
129                    content: message.content.clone(),
130                }),
131        );
132        let message = crate::sessions::gui_text(options.message);
133        messages.push(HarnessMessage {
134            role: "user".into(),
135            content: message.clone(),
136        });
137        let request = HarnessRequest {
138            model: options.model.into(),
139            messages,
140            temperature: options.temperature,
141        };
142        request.validate()?;
143        Ok(PreparedChat {
144            session_id: session.id,
145            revision: session.revision,
146            provider: options.provider.into(),
147            message,
148            request,
149            context,
150        })
151    }
152    pub fn approve_disclosure(&mut self, prepared: &PreparedChat) {
153        self.disclosures
154            .approve(&prepared.session_id, &prepared.provider, &prepared.context);
155    }
156    pub async fn run(
157        &self,
158        prepared: PreparedChat,
159        harness: &dyn ChatHarness,
160        timestamp: &str,
161        memory: Option<(&MemoryStore, CaptureOptions<'_>)>,
162        cancel: &CancellationToken,
163        sink: &mut DeltaSink<'_>,
164    ) -> Result<ChatOutcome, ChatError> {
165        if harness.name() != prepared.provider || timestamp.is_empty() || timestamp.len() > 40 {
166            return Err(ChatError::Invalid);
167        }
168        self.authorize(&prepared, timestamp, cancel)?;
169        let run = self.sessions.runs.begin(&prepared.session_id, cancel)?;
170        let text = tokio::select! { biased; _ = run.token().cancelled() => return Err(HarnessError::Cancelled.into()), result = harness.chat(&prepared.request, run.token(), sink) => result? };
171        self.finish(prepared, text, timestamp, memory, &run).await
172    }
173    pub async fn run_agent(
174        &self,
175        prepared: PreparedChat,
176        runtime: AgentRuntime<'_>,
177        timestamp: &str,
178        memory: Option<(&MemoryStore, CaptureOptions<'_>)>,
179        cancel: &CancellationToken,
180        sink: &mut crate::agent::EventSink<'_>,
181    ) -> Result<ChatOutcome, ChatError> {
182        if prepared.provider != "local" {
183            return Err(ChatError::Invalid);
184        }
185        self.authorize(&prepared, timestamp, cancel)?;
186        let run = self.sessions.runs.begin(&prepared.session_id, cancel)?;
187        let options = crate::agent::AgentOptions {
188            session_id: &prepared.session_id,
189            workspace_id: runtime.workspace_id,
190            model: &prepared.request.model,
191            temperature: prepared.request.temperature,
192            max_steps: runtime.max_steps,
193            messages: prepared
194                .request
195                .messages
196                .iter()
197                .map(|message| crate::ollama_inference::ChatMessage {
198                    role: message.role.clone(),
199                    content: message.content.clone(),
200                    tool_calls: Vec::new(),
201                    tool_name: None,
202                })
203                .collect(),
204        };
205        let text = crate::agent::run_turn(
206            runtime.chat,
207            runtime.manager,
208            runtime.grants,
209            runtime.approver,
210            options,
211            run.token(),
212            sink,
213        )
214        .await?;
215        self.finish(prepared, text, timestamp, memory, &run).await
216    }
217    fn authorize(
218        &self,
219        prepared: &PreparedChat,
220        timestamp: &str,
221        cancel: &CancellationToken,
222    ) -> Result<(), ChatError> {
223        if timestamp.is_empty() || timestamp.len() > 40 {
224            return Err(ChatError::Invalid);
225        }
226        if !self
227            .disclosures
228            .allowed(&prepared.session_id, &prepared.provider, &prepared.context)
229        {
230            return Err(ChatError::Disclosure);
231        }
232        let current = self
233            .sessions
234            .get(&prepared.session_id)?
235            .ok_or(ChatError::Invalid)?;
236        if current.archived || current.revision != prepared.revision {
237            return Err(SessionError::Conflict.into());
238        }
239        if cancel.is_cancelled() {
240            return Err(HarnessError::Cancelled.into());
241        }
242        Ok(())
243    }
244    async fn finish(
245        &self,
246        prepared: PreparedChat,
247        text: String,
248        timestamp: &str,
249        memory: Option<(&MemoryStore, CaptureOptions<'_>)>,
250        run: &crate::runs::RunLease,
251    ) -> Result<ChatOutcome, ChatError> {
252        let cancel = run.token();
253        if cancel.is_cancelled() {
254            return Err(HarnessError::Cancelled.into());
255        }
256        let session = run.commit(|| {
257            self.sessions.append_exchange(
258                &prepared.session_id,
259                StoredMessage {
260                    role: "user".into(),
261                    content: prepared.message.clone(),
262                    at: timestamp.into(),
263                    attachments: (!prepared.context.manifest.is_empty())
264                        .then_some(prepared.context.manifest),
265                },
266                StoredMessage {
267                    role: "assistant".into(),
268                    content: text.clone(),
269                    at: timestamp.into(),
270                    attachments: None,
271                },
272                prepared.revision,
273            )
274        })?;
275        let memory_captured = match memory {
276            Some((store, options)) => Some(
277                store
278                    .capture(&prepared.message, &text, options, cancel)
279                    .await
280                    .is_ok(),
281            ),
282            None => None,
283        };
284        Ok(ChatOutcome {
285            text,
286            session,
287            memory_captured,
288        })
289    }
290}