Skip to main content

hyprstream_core/tui/
rpc_transport.rs

1//! RPC-backed StreamSpawner and ToolCaller for ChatApp inference.
2//!
3//! Deduplicated from `shell_handlers.rs` and `service.rs` — both callers now
4//! use `make_chat_spawner` and (optionally) `make_tool_caller` from this module.
5
6use ed25519_dalek::SigningKey;
7use parking_lot::RwLock;
8use std::collections::HashMap;
9use std::sync::Arc;
10
11type ServiceTokenProvider = Arc<dyn Fn() -> anyhow::Result<String> + Send + Sync>;
12
13/// Build a `StreamSpawner` that drives inference via `ModelClient`.
14///
15/// Spawns a dedicated OS thread with a single-threaded Tokio runtime per
16/// user message.  Applies the chat template, starts an authenticated inference
17/// stream, and sends `ChatEvent` variants back through the provided channel.
18///
19/// `available_tools` is closure-captured and passed to `apply_chat_template`
20/// so the model's chat template can inject the tool list into the prompt.
21pub fn make_chat_spawner(
22    signing_key: &SigningKey,
23    model_ref: &str,
24    available_tools: Option<Vec<serde_json::Value>>,
25    gen_config: Arc<RwLock<hyprstream_tui::chat_app::ChatGenConfig>>,
26) -> hyprstream_tui::chat_app::StreamSpawner {
27    use hyprstream_rpc::streaming::StreamPayload;
28
29    use hyprstream_rpc_std::inference_client::GenerationRequest;
30    use hyprstream_rpc_std::inference_client::{ChatMessage, ToolCall, ToolCallFunction};
31    use hyprstream_tui::chat_app::{ChatEvent, ChatHistoryEntry, ChatRole};
32
33    let sk = signing_key.clone();
34    let mr = model_ref.to_owned();
35    let tools_json = available_tools.map(serde_json::Value::Array);
36    let gen_config_arc = gen_config;
37
38    Box::new(move |history: Vec<ChatHistoryEntry>, event_tx| {
39        use hyprstream_tui::chat_app::CancelHandle;
40        use tokio::sync::oneshot;
41
42        let sk_inner = sk.clone();
43        let mr_inner = mr.clone();
44        let tx = event_tx.clone();
45        let tools_inner = tools_json.clone();
46        let gen_cfg = gen_config_arc.read().clone();
47
48        let (cancel_tx, cancel_rx) = oneshot::channel::<()>();
49
50        std::thread::spawn(move || {
51            let rt = match tokio::runtime::Builder::new_current_thread()
52                .enable_all()
53                .build()
54            {
55                Ok(r) => r,
56                Err(e) => {
57                    let _ = tx.send(ChatEvent::StreamError(e.to_string()));
58                    return;
59                }
60            };
61
62            rt.block_on(async move {
63                let model_client =
64                    match crate::tui::service::production_model_client(sk_inner.clone()) {
65                        Ok(c) => c,
66                        Err(e) => {
67                            let _ = tx.send(ChatEvent::StreamError(format!(
68                                "Failed to create ModelClient: {e}"
69                            )));
70                            return;
71                        }
72                    };
73
74                // Map ChatHistoryEntry → ChatMessage, handling all roles.
75                // Skip the trailing empty assistant placeholder — it's only a
76                // generation marker for the TUI; `add_generation_prompt: true`
77                // already injects the assistant-start token into the prompt.
78                // Including it causes a double `<|im_start|>assistant` prefix.
79                let history_slice = match history.last() {
80                    Some(e)
81                        if matches!(e.role, ChatRole::Assistant)
82                            && e.content.is_empty()
83                            && e.tool_calls.is_empty() =>
84                    {
85                        &history[..history.len() - 1]
86                    }
87                    _ => &history[..],
88                };
89
90                let messages: Vec<ChatMessage> = history_slice
91                    .iter()
92                    .map(|e| match e.role {
93                        ChatRole::Tool => ChatMessage {
94                            role: "tool".to_owned(),
95                            content: e.content.clone(),
96                            tool_calls: vec![],
97                            tool_call_id: e.tool_call_id.clone().unwrap_or_default(),
98                        },
99                        ChatRole::Assistant if !e.tool_calls.is_empty() => ChatMessage {
100                            role: "assistant".to_owned(),
101                            content: e.content.clone(),
102                            tool_calls: e
103                                .tool_calls
104                                .iter()
105                                .map(|tc| ToolCall {
106                                    // Use the per-invocation correlation ID (not the
107                                    // function-name UUID) so two calls to the same
108                                    // tool have distinct tool_call_id values.
109                                    id: tc.id.clone(),
110                                    tool_type: "function".to_owned(),
111                                    function: ToolCallFunction {
112                                        name: tc.uuid.clone(),
113                                        arguments: tc.arguments.clone(),
114                                    },
115                                })
116                                .collect(),
117                            tool_call_id: String::new(),
118                        },
119                        ChatRole::User | ChatRole::Assistant => ChatMessage {
120                            role: match e.role {
121                                ChatRole::User => "user",
122                                _ => "assistant",
123                            }
124                            .to_owned(),
125                            content: e.content.clone(),
126                            tool_calls: vec![],
127                            tool_call_id: String::new(),
128                        },
129                    })
130                    .collect();
131
132                let tools_json = tools_inner
133                    .as_ref()
134                    .map(|t| serde_json::to_string(t).unwrap_or_default())
135                    .unwrap_or_default();
136                let template_result = tokio::time::timeout(
137                    std::time::Duration::from_secs(15),
138                    model_client.infer(&mr_inner).apply_chat_template(
139                        &hyprstream_rpc_std::model_client::ChatTemplateRequest {
140                            messages: messages.clone(),
141                            add_generation_prompt: true,
142                            tools_json: Some(tools_json.clone()).filter(|s| !s.is_empty()),
143                            max_tokens: Some(gen_cfg.max_tokens as u32),
144                            enable_thinking: None,
145                            template_vars_json: None,
146                        },
147                    ),
148                )
149                .await;
150                let prompt = match template_result {
151                    Ok(Ok(p)) => crate::config::TemplatedPrompt::new(p),
152                    Ok(Err(e)) => {
153                        let _ = tx.send(ChatEvent::TemplateError(e.to_string()));
154                        return;
155                    }
156                    Err(_elapsed) => {
157                        let _ = tx.send(ChatEvent::TemplateError(
158                            "chat template timed out after 15s — model service may be busy"
159                                .to_owned(),
160                        ));
161                        return;
162                    }
163                };
164
165                let req = GenerationRequest {
166                    prompt: prompt.into_inner(),
167                    max_tokens: Some(gen_cfg.max_tokens as u32),
168                    temperature: Some(gen_cfg.temperature),
169                    top_p: Some(gen_cfg.top_p),
170                    top_k: gen_cfg.top_k.map(|v| v as u32),
171                    ..Default::default()
172                };
173
174                use hyprstream_rpc_std::model_client::InferRpc;
175                let mut handle =
176                    match InferRpc::generate_stream(&model_client.infer(&mr_inner), &req).await {
177                        Ok(h) => h,
178                        Err(e) => {
179                            let _ = tx.send(ChatEvent::StreamError(e.to_string()));
180                            return;
181                        }
182                    };
183
184                let mut cancel_rx = cancel_rx;
185                loop {
186                    tokio::select! {
187                        biased;
188                        _ = &mut cancel_rx => {
189                            let _ = tx.send(ChatEvent::StreamCancelled);
190                            break;
191                        }
192                        result = handle.recv_next() => {
193                            match result {
194                                Ok(Some(StreamPayload::Data(b))) => {
195                                    let _ = tx.send(ChatEvent::Token(
196                                        String::from_utf8_lossy(&b).into_owned(),
197                                    ));
198                                }
199                                Ok(Some(StreamPayload::Complete(_))) | Ok(None) => {
200                                    let _ = tx.send(ChatEvent::StreamComplete);
201                                    break;
202                                }
203                                Ok(Some(StreamPayload::Error(m))) => {
204                                    let _ = tx.send(ChatEvent::StreamError(m));
205                                    break;
206                                }
207                                Ok(Some(StreamPayload::Tagged { .. })) => {
208                                    // encrypted event payload, skip
209                                    continue;
210                                }
211                                Err(e) => {
212                                    let _ = tx.send(ChatEvent::StreamError(e.to_string()));
213                                    break;
214                                }
215                            }
216                        }
217                    }
218                }
219            });
220        });
221
222        let handle: CancelHandle = Box::new(move || {
223            let _ = cancel_tx.send(());
224        });
225        handle
226    })
227}
228
229/// Build a `ToolCaller` backed by the MCP ZMQ service.
230///
231/// Eagerly fetches the tool list so that `tool_descriptions` and `openai_tools`
232/// can be returned alongside the caller.  If the MCP service is unreachable,
233/// returns empty collections and a caller that immediately sends an error result.
234/// A missing or blank TUI service credential is rejected before either operation
235/// is constructed, so neither production MCP path can fall back to tokenless RPC.
236///
237/// Returns `(caller, descriptions, openai_tools)` where:
238/// - `descriptions` maps UUID → human label (for TUI display)
239/// - `openai_tools` is the full OpenAI-format tool list for `apply_chat_template`
240pub fn make_tool_caller(
241    signing_key: &SigningKey,
242) -> anyhow::Result<(
243    hyprstream_tui::chat_app::ToolCaller,
244    HashMap<String, String>,
245    Vec<serde_json::Value>,
246)> {
247    let sk = signing_key.clone();
248    let token_key = signing_key.clone();
249    let token_provider: ServiceTokenProvider =
250        Arc::new(move || super::service::required_service_token(&token_key));
251    make_tool_caller_with_token_provider(sk, token_provider)
252}
253
254fn make_tool_caller_with_token_provider(
255    signing_key: SigningKey,
256    token_provider: ServiceTokenProvider,
257) -> anyhow::Result<(
258    hyprstream_tui::chat_app::ToolCaller,
259    HashMap<String, String>,
260    Vec<serde_json::Value>,
261)> {
262    use hyprstream_rpc_std::mcp_client::McpClient as GenMcpClient;
263    use hyprstream_tui::chat_app::ChatEvent;
264
265    let sk = signing_key;
266
267    // ── Eagerly fetch tool list ───────────────────────────────────────────────
268    // Spawned on a dedicated OS thread so `block_on` is safe regardless of
269    // whether the caller is inside an existing Tokio runtime.
270    let sk_fetch = sk.clone();
271    let token_fetch = current_service_token(&token_provider)?;
272    let (descriptions, openai_tools): (HashMap<String, String>, Vec<serde_json::Value>) =
273        std::thread::spawn(move || {
274            let rt = tokio::runtime::Builder::new_current_thread()
275                .enable_all()
276                .build()
277                .ok()?;
278            rt.block_on(async move {
279                let gen: GenMcpClient = GenMcpClient::from_provider(
280                    &hyprstream_discovery::ProductionRpcClientProvider,
281                    sk_fetch,
282                    Some(token_fetch),
283                )
284                .ok()?;
285                let tool_list = gen.list_tools().await.ok()?;
286                let mut descs = HashMap::new();
287                let mut tools = Vec::new();
288                for t in tool_list.tools {
289                    descs.insert(t.name.clone(), t.description.clone());
290                    // Build OpenAI-format tool definition for apply_chat_template.
291                    // Omit `parameters` (full JSON Schema) to keep the tool list
292                    // compact — the schema can add hundreds of tokens per tool and
293                    // quickly exhaust the context window.  Name + description is
294                    // sufficient for the model to know when and how to call a tool.
295                    tools.push(serde_json::json!({
296                        "type": "function",
297                        "function": {
298                            "name": t.name,
299                            "description": t.description,
300                        }
301                    }));
302                }
303                Some((descs, tools))
304            })
305        })
306        .join()
307        .unwrap_or(None)
308        .unwrap_or_default();
309
310    // ── Build the caller closure ──────────────────────────────────────────────
311    let caller: hyprstream_tui::chat_app::ToolCaller = std::sync::Arc::new(
312        move |id: String,
313              uuid: String,
314              arguments: String,
315              event_tx: std::sync::mpsc::SyncSender<ChatEvent>| {
316            let sk_c = sk.clone();
317            let token_provider_c = Arc::clone(&token_provider);
318            let uuid_c = uuid.clone();
319            std::thread::spawn(move || {
320                let result_str: String = (move || {
321                    // Resolve at invocation time: the service JWT can renew
322                    // while a chat stays open, so a captured eager-list token
323                    // would eventually fail valid tool calls.
324                    let token_c = match current_service_token(&token_provider_c) {
325                        Ok(token) => token,
326                        Err(e) => {
327                            return format!("error: TUI MCP service credential unavailable: {e}")
328                        }
329                    };
330                    let rt = match tokio::runtime::Builder::new_current_thread()
331                        .enable_all()
332                        .build()
333                    {
334                        Ok(r) => r,
335                        Err(e) => return format!("error: {e}"),
336                    };
337                    rt.block_on(async move {
338                        let mcp_client = match GenMcpClient::from_provider(
339                            &hyprstream_discovery::ProductionRpcClientProvider,
340                            sk_c,
341                            Some(token_c),
342                        ) {
343                            Ok(c) => c,
344                            Err(e) => return format!("error: failed to create McpClient: {e}"),
345                        };
346                        match mcp_client
347                            .call_tool(&hyprstream_rpc_std::mcp_client::CallTool {
348                                tool_name: uuid_c,
349                                arguments,
350                                caller_identity: hyprstream_rpc::identity::Did::new(String::new()),
351                            })
352                            .await
353                        {
354                            Ok(res) => {
355                                if res.success {
356                                    res.result
357                                } else {
358                                    format!("error: {}", res.error_message)
359                                }
360                            }
361                            Err(e) => format!("error: {e}"),
362                        }
363                    })
364                })();
365                let _ = event_tx.send(ChatEvent::ToolCallResult {
366                    id,
367                    uuid,
368                    result: result_str,
369                });
370            });
371        },
372    );
373
374    Ok((caller, descriptions, openai_tools))
375}
376
377fn current_service_token(token_provider: &ServiceTokenProvider) -> anyhow::Result<String> {
378    let token = token_provider()?;
379    anyhow::ensure!(
380        !token.trim().is_empty(),
381        "TUI MCP service credential is unavailable"
382    );
383    Ok(token)
384}
385
386#[cfg(test)]
387mod tests {
388    use super::*;
389
390    #[test]
391    fn mcp_tool_caller_rejects_missing_or_blank_service_credentials() {
392        for token in [String::new(), " \t".to_owned()] {
393            let provider: ServiceTokenProvider = Arc::new(move || Ok(token.clone()));
394            let error = match current_service_token(&provider) {
395                Ok(_) => panic!("blank credential must not create a TUI MCP caller"),
396                Err(error) => error.to_string(),
397            };
398            assert!(error.contains("service credential is unavailable"));
399        }
400    }
401
402    #[test]
403    fn current_service_token_is_refreshed_each_time() -> anyhow::Result<()> {
404        let tokens = Arc::new(parking_lot::Mutex::new(vec![
405            "renewed".to_owned(),
406            "initial".to_owned(),
407        ]));
408        let tokens_for_provider = Arc::clone(&tokens);
409        let provider: ServiceTokenProvider = Arc::new(move || {
410            tokens_for_provider
411                .lock()
412                .pop()
413                .ok_or_else(|| anyhow::anyhow!("credential no longer available"))
414        });
415        assert_eq!(current_service_token(&provider)?, "initial");
416        assert_eq!(current_service_token(&provider)?, "renewed");
417        Ok(())
418    }
419}