Skip to main content

chat_core/
lib.rs

1//! Chat orchestration state machine.
2//!
3//! Owns conversation state, token parsing, tool call detection, and the
4//! agentic loop. Used by both the TUI and web GUI — they are thin renderers
5//! over this core.
6//!
7//! This crate has no I/O, no platform deps, no async runtime. It is pure
8//! state machine logic that can be compiled to any target (native, WASI, wasm32).
9
10use serde::{Deserialize, Serialize};
11
12pub mod capnp;
13
14#[cfg(feature = "wasm")]
15use wasm_bindgen::prelude::*;
16
17// ─── Tool Call Format ────────────────────────────────────────────────
18
19/// Model-specific tool call syntax.
20///
21/// Determines which markers to scan for in streaming token output.
22/// Unified enum — replaces duplicates in chat_app.rs and api/tools.rs.
23#[derive(Clone, Copy, PartialEq, Eq, Default, Debug)]
24pub enum ToolCallFormat {
25    /// Qwen3 XML: `<tool_call>{"name":…,"arguments":…}</tool_call>`
26    Qwen3Xml,
27    /// Qwen3.5 XML parameter format:
28    /// `<tool_call><function=NAME><parameter=KEY>value</parameter>…</function></tool_call>`
29    Qwen35XmlParam,
30    /// Llama 3.1+: `<|python_tag|>` prefix + JSON
31    LlamaJson,
32    /// Mistral: `[TOOL_CALLS]` prefix + JSON array
33    MistralJson,
34    /// Model does not support tool calling.
35    #[default]
36    None,
37}
38
39impl ToolCallFormat {
40    /// Determine tool call format from model architecture string.
41    pub fn from_architecture(arch: &str) -> Self {
42        let lower = arch.to_lowercase();
43        if lower.contains("qwen3_5") || lower.contains("qwen3.5") {
44            Self::Qwen35XmlParam
45        } else if lower.contains("qwen3") || lower.contains("qwen") {
46            Self::Qwen3Xml
47        } else if lower.contains("llama") {
48            Self::LlamaJson
49        } else if lower.contains("mistral") {
50            Self::MistralJson
51        } else {
52            Self::None
53        }
54    }
55
56    /// Returns the (open, close) marker pair for this format.
57    pub fn markers(&self) -> Option<(&'static str, &'static str)> {
58        match self {
59            Self::Qwen3Xml => Some(("<tool_call>", "</tool_call>")),
60            Self::Qwen35XmlParam => Some(("<tool_call>", "</tool_call>")),
61            Self::LlamaJson => Some(("<|python_tag|>", "")),
62            Self::MistralJson => Some(("[TOOL_CALLS]", "")),
63            Self::None => None,
64        }
65    }
66}
67
68// ─── Chat History Types ──────────────────────────────────────────────
69
70/// Role of a chat history entry.
71#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
72pub enum ChatRole {
73    User,
74    Assistant,
75    /// Tool result injected back into the conversation.
76    Tool,
77}
78
79/// A recorded tool call and its result (or in-flight state).
80#[derive(Serialize, Deserialize, Clone, Debug)]
81pub struct ToolCallRecord {
82    /// Per-invocation correlation ID (UUID v4).
83    #[serde(default)]
84    pub id: String,
85    /// The UUID name used on the wire (what the model outputs as `name`).
86    pub uuid: String,
87    /// Human-readable label from `list_tools()`.
88    pub description: String,
89    /// JSON-encoded arguments string.
90    pub arguments: String,
91    /// Execution result. `None` while the call is in-flight.
92    pub result: Option<String>,
93}
94
95/// A single message in the conversation history.
96#[derive(Serialize, Deserialize, Clone, Debug)]
97pub struct ChatHistoryEntry {
98    pub role: ChatRole,
99    /// Visible response content.
100    pub content: String,
101    /// Reasoning / thinking tokens (hidden by default).
102    #[serde(default)]
103    pub thinking: String,
104    /// Tool calls made by this assistant turn.
105    #[serde(default)]
106    pub tool_calls: Vec<ToolCallRecord>,
107    /// For Tool messages: the UUID of the corresponding tool call.
108    #[serde(default)]
109    pub tool_call_id: Option<String>,
110}
111
112// ─── Events ──────────────────────────────────────────────────────────
113
114/// Events emitted by the chat state machine.
115/// These are structured — clients never see raw `<think>` or `<tool_call>` tags.
116#[derive(Debug, Clone)]
117pub enum ChatEvent {
118    /// Display text content chunk.
119    Content(String),
120    /// Thinking/reasoning block chunk.
121    Thinking(String),
122    /// A complete tool call was detected in the model output.
123    ToolCallDetected {
124        id: String,
125        uuid: String,
126        description: String,
127        arguments: String,
128    },
129    /// The model finished generating (stream complete).
130    Complete,
131    /// Stream error.
132    Error(String),
133}
134
135/// Commands sent to the chat state machine.
136#[derive(Debug, Clone)]
137pub enum ChatCommand {
138    /// User submitted a message.
139    UserMessage(String),
140    /// An inference token arrived from the backend.
141    InferenceToken(String),
142    /// Inference stream completed.
143    InferenceComplete,
144    /// Inference stream errored.
145    InferenceError(String),
146    /// Tool execution result.
147    ToolCallResult { id: String, result: String },
148    /// Cancel current generation.
149    Cancel,
150}
151
152// ─── Chat State Machine ──────────────────────────────────────────────
153
154/// Core chat orchestration state.
155///
156/// Owns conversation history, token parsing, tool call detection,
157/// and the agentic loop. No I/O — all communication is via
158/// `handle_command()` → `Vec<ChatEvent>`.
159pub struct ChatState {
160    pub history: Vec<ChatHistoryEntry>,
161    pub tool_call_format: ToolCallFormat,
162    pub tool_descriptions: std::collections::HashMap<String, String>,
163
164    // Streaming parser state
165    in_thinking: bool,
166    in_tool_call: bool,
167    tool_call_buf: String,
168
169    // Agentic loop state
170    pub pending_tool_calls: usize,
171    tool_call_counter: usize,
172    awaiting_reinvoke: bool,
173}
174
175impl ChatState {
176    pub fn new(format: ToolCallFormat) -> Self {
177        Self {
178            history: Vec::new(),
179            tool_call_format: format,
180            tool_descriptions: std::collections::HashMap::new(),
181            in_thinking: false,
182            in_tool_call: false,
183            tool_call_buf: String::new(),
184            pending_tool_calls: 0,
185            tool_call_counter: 0,
186            awaiting_reinvoke: false,
187        }
188    }
189
190    /// Process a command and return events to emit.
191    pub fn handle_command(&mut self, cmd: ChatCommand) -> Vec<ChatEvent> {
192        match cmd {
193            ChatCommand::UserMessage(text) => {
194                self.history.push(ChatHistoryEntry {
195                    role: ChatRole::User,
196                    content: text,
197                    thinking: String::new(),
198                    tool_calls: Vec::new(),
199                    tool_call_id: None,
200                });
201                // Add empty assistant entry for streaming
202                self.history.push(ChatHistoryEntry {
203                    role: ChatRole::Assistant,
204                    content: String::new(),
205                    thinking: String::new(),
206                    tool_calls: Vec::new(),
207                    tool_call_id: None,
208                });
209                self.in_thinking = false;
210                self.in_tool_call = false;
211                self.tool_call_buf.clear();
212                Vec::new() // Caller should trigger inference
213            }
214            ChatCommand::InferenceToken(token) => {
215                self.awaiting_reinvoke = false;
216                self.ingest_token(&token)
217            }
218            ChatCommand::InferenceComplete => {
219                let mut events = Vec::new();
220
221                // Flush buffered tool call for formats without closing tags (Llama/Mistral)
222                if self.in_tool_call && !self.tool_call_buf.is_empty() {
223                    self.in_tool_call = false;
224                    let buf = std::mem::take(&mut self.tool_call_buf);
225                    if let Some((uuid, arguments)) = parse_tool_call_buf(&buf) {
226                        let description = self
227                            .tool_descriptions
228                            .get(&uuid)
229                            .cloned()
230                            .unwrap_or_else(|| uuid.clone());
231                        self.tool_call_counter += 1;
232                        let id = format!("tc-{}", self.tool_call_counter);
233                        if let Some(last) = self.history.last_mut() {
234                            last.tool_calls.push(ToolCallRecord {
235                                id: id.clone(),
236                                uuid: uuid.clone(),
237                                description: description.clone(),
238                                arguments: arguments.clone(),
239                                result: None,
240                            });
241                        }
242                        self.pending_tool_calls += 1;
243                        events.push(ChatEvent::ToolCallDetected {
244                            id,
245                            uuid,
246                            description,
247                            arguments,
248                        });
249                    }
250                }
251
252                if self.pending_tool_calls == 0 {
253                    events.push(ChatEvent::Complete);
254                }
255                events
256            }
257            ChatCommand::InferenceError(msg) => {
258                vec![ChatEvent::Error(msg)]
259            }
260            ChatCommand::ToolCallResult { id, result } => {
261                // Inject tool result into history
262                if let Some(last_assistant) = self
263                    .history
264                    .iter_mut()
265                    .rev()
266                    .find(|e| e.role == ChatRole::Assistant)
267                {
268                    if let Some(tc) = last_assistant.tool_calls.iter_mut().find(|t| t.id == id) {
269                        tc.result = Some(result.clone());
270                    }
271                }
272                // Add tool message to history
273                self.history.push(ChatHistoryEntry {
274                    role: ChatRole::Tool,
275                    content: result,
276                    thinking: String::new(),
277                    tool_calls: Vec::new(),
278                    tool_call_id: Some(id),
279                });
280
281                self.pending_tool_calls = self.pending_tool_calls.saturating_sub(1);
282
283                // If all tool calls resolved, set up for re-invocation
284                if self.pending_tool_calls == 0 {
285                    self.awaiting_reinvoke = true;
286                    // Add new empty assistant entry for the next generation
287                    self.history.push(ChatHistoryEntry {
288                        role: ChatRole::Assistant,
289                        content: String::new(),
290                        thinking: String::new(),
291                        tool_calls: Vec::new(),
292                        tool_call_id: None,
293                    });
294                    self.in_thinking = false;
295                    self.in_tool_call = false;
296                    self.tool_call_buf.clear();
297                    // Caller should re-trigger inference with updated history
298                }
299                Vec::new()
300            }
301            ChatCommand::Cancel => {
302                self.in_thinking = false;
303                self.in_tool_call = false;
304                self.tool_call_buf.clear();
305                Vec::new()
306            }
307        }
308    }
309
310    /// Process a streaming token. Handles `<think>`, `<tool_call>`, and
311    /// content separation. Returns events to emit.
312    ///
313    /// Mirrors the TUI's `ChatApp::ingest_token` but returns events
314    /// instead of mutating UI state.
315    pub fn ingest_token(&mut self, token: &str) -> Vec<ChatEvent> {
316        let mut events: Vec<ChatEvent> = Vec::new();
317        let markers = self.tool_call_format.markers();
318
319        let last = match self.history.last_mut() {
320            Some(e) => e,
321            None => return events,
322        };
323
324        let mut remaining = token;
325
326        while !remaining.is_empty() {
327            if self.in_thinking {
328                if let Some(end) = remaining.find("</think>") {
329                    let chunk = &remaining[..end];
330                    last.thinking.push_str(chunk);
331                    events.push(ChatEvent::Thinking(chunk.to_owned()));
332                    self.in_thinking = false;
333                    remaining = &remaining[end + "</think>".len()..];
334                } else {
335                    last.thinking.push_str(remaining);
336                    events.push(ChatEvent::Thinking(remaining.to_owned()));
337                    break;
338                }
339            } else if self.in_tool_call {
340                let close = markers.map(|(_, c)| c).unwrap_or("");
341                if close.is_empty() {
342                    // Llama/Mistral: no closing tag — buffer everything
343                    self.tool_call_buf.push_str(remaining);
344                    break;
345                }
346                if let Some(end) = remaining.find(close) {
347                    self.tool_call_buf.push_str(&remaining[..end]);
348                    remaining = &remaining[end + close.len()..];
349                    self.in_tool_call = false;
350                    let buf = std::mem::take(&mut self.tool_call_buf);
351                    if let Some((uuid, arguments)) = parse_tool_call_buf(&buf) {
352                        let description = self
353                            .tool_descriptions
354                            .get(&uuid)
355                            .cloned()
356                            .unwrap_or_else(|| uuid.clone());
357                        self.tool_call_counter += 1;
358                        let id = format!("tc-{}", self.tool_call_counter);
359                        last.tool_calls.push(ToolCallRecord {
360                            id: id.clone(),
361                            uuid: uuid.clone(),
362                            description: description.clone(),
363                            arguments: arguments.clone(),
364                            result: None,
365                        });
366                        self.pending_tool_calls += 1;
367                        events.push(ChatEvent::ToolCallDetected {
368                            id,
369                            uuid,
370                            description,
371                            arguments,
372                        });
373                    }
374                } else {
375                    self.tool_call_buf.push_str(remaining);
376                    break;
377                }
378            } else if let Some((open, _)) = markers {
379                if let Some(start) = remaining.find(open) {
380                    let content = &remaining[..start];
381                    if !content.is_empty() {
382                        last.content.push_str(content);
383                        events.push(ChatEvent::Content(content.to_owned()));
384                    }
385                    self.in_tool_call = true;
386                    self.tool_call_buf.clear();
387                    remaining = &remaining[start + open.len()..];
388                } else if let Some(start) = remaining.find("<think>") {
389                    let content = &remaining[..start];
390                    if !content.is_empty() {
391                        last.content.push_str(content);
392                        events.push(ChatEvent::Content(content.to_owned()));
393                    }
394                    self.in_thinking = true;
395                    remaining = &remaining[start + "<think>".len()..];
396                } else {
397                    last.content.push_str(remaining);
398                    events.push(ChatEvent::Content(remaining.to_owned()));
399                    break;
400                }
401            } else if let Some(start) = remaining.find("<think>") {
402                let content = &remaining[..start];
403                if !content.is_empty() {
404                    last.content.push_str(content);
405                    events.push(ChatEvent::Content(content.to_owned()));
406                }
407                self.in_thinking = true;
408                remaining = &remaining[start + "<think>".len()..];
409            } else {
410                last.content.push_str(remaining);
411                events.push(ChatEvent::Content(remaining.to_owned()));
412                break;
413            }
414        }
415
416        events
417    }
418
419    /// Whether the state machine needs inference to be re-invoked
420    /// (e.g. after all tool results have been received).
421    /// Whether the state machine needs inference to be re-invoked
422    /// (after all tool results received and a new empty assistant entry is ready).
423    pub fn needs_reinvoke(&self) -> bool {
424        self.awaiting_reinvoke
425    }
426}
427
428// ─── Tool Call Parsers ───────────────────────────────────────────────
429
430/// Parse a tool call buffer. Tries JSON first (Qwen3/Hermes), then
431/// Qwen3.5 XML parameter format.
432pub fn parse_tool_call_buf(buf: &str) -> Option<(String, String)> {
433    let trimmed = buf.trim();
434
435    // Try JSON format: {"name": "...", "arguments": {...}}
436    if let Ok(call_data) = serde_json::from_str::<serde_json::Value>(trimmed) {
437        let name = call_data["name"].as_str()?.to_owned();
438        let arguments = serde_json::to_string(&call_data["arguments"]).ok()?;
439        return Some((name, arguments));
440    }
441
442    // Fall back to Qwen3.5 XML parameter format:
443    //   <function=get_weather>
444    //   <parameter=location>NYC</parameter>
445    //   </function>
446    if trimmed.contains("<function=") {
447        return parse_xml_param_tool_call(trimmed);
448    }
449
450    None
451}
452
453/// Parse a Qwen3.5 XML parameter-format tool call block.
454pub fn parse_xml_param_tool_call(buf: &str) -> Option<(String, String)> {
455    // Extract function name from <function=NAME>
456    let func_start = buf.find("<function=")?;
457    let name_start = func_start + "<function=".len();
458    let name_end = buf[name_start..].find('>')? + name_start;
459    let name = buf[name_start..name_end].trim().to_owned();
460    if name.is_empty() {
461        return None;
462    }
463
464    // Extract body between <function=NAME> and </function>
465    let body_start = name_end + 1;
466    let body_end = buf.find("</function>").unwrap_or(buf.len());
467    let body = &buf[body_start..body_end];
468
469    // Extract <parameter=KEY>VALUE</parameter> pairs
470    let mut args = serde_json::Map::new();
471    let mut search_from = 0;
472    while let Some(param_start) = body[search_from..].find("<parameter=") {
473        let abs_start = search_from + param_start;
474        let key_start = abs_start + "<parameter=".len();
475        let key_end = match body[key_start..].find('>') {
476            Some(p) => key_start + p,
477            None => break,
478        };
479        let key = body[key_start..key_end].trim().to_owned();
480
481        let value_start = key_end + 1;
482        let value_end = match body[value_start..].find("</parameter>") {
483            Some(p) => value_start + p,
484            None => break,
485        };
486        let value = body[value_start..value_end].trim();
487
488        if !key.is_empty() {
489            // Try JSON parse for numbers/bools/objects, fall back to string
490            let json_val = serde_json::from_str::<serde_json::Value>(value)
491                .unwrap_or_else(|_| serde_json::Value::String(value.to_owned()));
492            args.insert(key, json_val);
493        }
494
495        search_from = value_end + "</parameter>".len();
496    }
497
498    let arguments = serde_json::to_string(&serde_json::Value::Object(args)).ok()?;
499    Some((name, arguments))
500}
501
502// ─── WASM-Bindgen API ────────────────────────────────────────────────
503
504/// WASM-accessible wrapper around ChatState.
505/// All communication is via JSON strings to avoid wasm-bindgen complex type issues.
506#[cfg(feature = "wasm")]
507#[wasm_bindgen]
508pub struct WasmChatState {
509    inner: ChatState,
510}
511
512#[cfg(feature = "wasm")]
513#[wasm_bindgen]
514impl WasmChatState {
515    /// Create a new chat state with the given tool call format.
516    /// format: "qwen3", "qwen3_5", "llama", "mistral", or "" for none.
517    #[wasm_bindgen(constructor)]
518    pub fn new(format: &str) -> Self {
519        let fmt = ToolCallFormat::from_architecture(format);
520        Self {
521            inner: ChatState::new(fmt),
522        }
523    }
524
525    /// Set tool descriptions as a JSON object: {"uuid": "description", ...}
526    #[wasm_bindgen(js_name = setToolDescriptions)]
527    pub fn set_tool_descriptions(&mut self, json: &str) {
528        if let Ok(v) = serde_json::from_str::<serde_json::Value>(json) {
529            if let Some(obj) = v.as_object() {
530                for (k, v) in obj {
531                    if let Some(s) = v.as_str() {
532                        self.inner.tool_descriptions.insert(k.clone(), s.to_owned());
533                    }
534                }
535            }
536        }
537    }
538
539    /// Process a command (JSON string). Returns a JSON array of events.
540    /// Command format: {"type": "userMessage", "text": "..."} etc.
541    /// Events format: [{"type": "content", "text": "..."}, ...]
542    #[wasm_bindgen(js_name = handleCommand)]
543    pub fn handle_command(&mut self, json: &str) -> String {
544        let cmd = match json_to_command(json) {
545            Ok(c) => c,
546            Err(e) => return serde_json::json!([{"type": "error", "message": e}]).to_string(),
547        };
548        let events = self.inner.handle_command(cmd);
549        let json_events: Vec<serde_json::Value> = events.iter().map(event_to_json_value).collect();
550        serde_json::to_string(&json_events).unwrap_or_else(|_| "[]".to_owned())
551    }
552
553    /// Fast path: ingest a token and return Cap'n Proto encoded events.
554    /// Returns a length-prefixed event list: [u32 count] [u32 len] [capnp bytes] ...
555    /// No JSON serialization — binary only.
556    #[wasm_bindgen(js_name = ingestTokenBytes)]
557    pub fn ingest_token_bytes(&mut self, token: &str) -> Vec<u8> {
558        let events = self.inner.ingest_token(token);
559        let capnp_events: Vec<Vec<u8>> = events.iter().map(event_to_capnp).collect();
560        capnp::encode_event_list(&capnp_events)
561    }
562
563    /// Check if the state machine needs inference re-invocation.
564    #[wasm_bindgen(js_name = needsReinvoke)]
565    pub fn needs_reinvoke(&self) -> bool {
566        self.inner.needs_reinvoke()
567    }
568
569    /// Get the current history as JSON.
570    #[wasm_bindgen(js_name = getHistory)]
571    pub fn get_history(&self) -> String {
572        serde_json::to_string(&self.inner.history).unwrap_or_else(|_| "[]".to_owned())
573    }
574
575    /// Get the number of pending tool calls.
576    #[wasm_bindgen(js_name = pendingToolCalls)]
577    pub fn pending_tool_calls(&self) -> usize {
578        self.inner.pending_tool_calls
579    }
580}
581
582#[cfg(feature = "wasm")]
583fn json_to_command(json: &str) -> Result<ChatCommand, String> {
584    let v: serde_json::Value = serde_json::from_str(json).map_err(|e| e.to_string())?;
585    let cmd_type = v["type"].as_str().ok_or("missing type field")?;
586    match cmd_type {
587        "userMessage" => Ok(ChatCommand::UserMessage(
588            v["text"].as_str().ok_or("missing text")?.to_owned(),
589        )),
590        "inferenceToken" => Ok(ChatCommand::InferenceToken(
591            v["token"].as_str().ok_or("missing token")?.to_owned(),
592        )),
593        "inferenceComplete" => Ok(ChatCommand::InferenceComplete),
594        "inferenceError" => Ok(ChatCommand::InferenceError(
595            v["message"].as_str().unwrap_or("unknown error").to_owned(),
596        )),
597        "toolCallResult" => Ok(ChatCommand::ToolCallResult {
598            id: v["id"].as_str().ok_or("missing id")?.to_owned(),
599            result: v["result"].as_str().ok_or("missing result")?.to_owned(),
600        }),
601        "cancel" => Ok(ChatCommand::Cancel),
602        other => Err(format!("unknown command type: {other}")),
603    }
604}
605
606#[cfg(feature = "wasm")]
607fn event_to_capnp(event: &ChatEvent) -> Vec<u8> {
608    match event {
609        ChatEvent::Content(text) => capnp::build_text_event(0, text),
610        ChatEvent::Thinking(text) => capnp::build_text_event(1, text),
611        ChatEvent::ToolCallDetected {
612            id,
613            uuid,
614            description,
615            arguments,
616        } => capnp::build_tool_call_detected(id, uuid, description, arguments),
617        ChatEvent::Complete => capnp::build_complete(),
618        ChatEvent::Error(msg) => capnp::build_text_event(4, msg),
619    }
620}
621
622#[cfg(feature = "wasm")]
623fn event_to_json_value(event: &ChatEvent) -> serde_json::Value {
624    match event {
625        ChatEvent::Content(text) => serde_json::json!({"type": "content", "text": text}),
626        ChatEvent::Thinking(text) => serde_json::json!({"type": "thinking", "text": text}),
627        ChatEvent::ToolCallDetected {
628            id,
629            uuid,
630            description,
631            arguments,
632        } => {
633            serde_json::json!({"type": "toolCallDetected", "id": id, "uuid": uuid,
634                "description": description, "arguments": arguments})
635        }
636        ChatEvent::Complete => serde_json::json!({"type": "complete"}),
637        ChatEvent::Error(msg) => serde_json::json!({"type": "error", "message": msg}),
638    }
639}
640
641#[cfg(test)]
642mod tests {
643    use super::*;
644
645    #[test]
646    fn test_tool_call_format_from_architecture() {
647        assert_eq!(
648            ToolCallFormat::from_architecture("qwen3"),
649            ToolCallFormat::Qwen3Xml
650        );
651        assert_eq!(
652            ToolCallFormat::from_architecture("qwen3_5"),
653            ToolCallFormat::Qwen35XmlParam
654        );
655        assert_eq!(
656            ToolCallFormat::from_architecture("llama3"),
657            ToolCallFormat::LlamaJson
658        );
659        assert_eq!(
660            ToolCallFormat::from_architecture("mistral"),
661            ToolCallFormat::MistralJson
662        );
663        assert_eq!(
664            ToolCallFormat::from_architecture("gemma"),
665            ToolCallFormat::None
666        );
667    }
668
669    #[test]
670    fn test_parse_json_tool_call() {
671        let buf = r#"{"name": "get_weather", "arguments": {"location": "NYC"}}"#;
672        let (name, args) = parse_tool_call_buf(buf).unwrap();
673        assert_eq!(name, "get_weather");
674        assert!(args.contains("NYC"));
675    }
676
677    #[test]
678    fn test_parse_xml_tool_call() {
679        let buf = "<function=get_weather>\n<parameter=location>NYC</parameter>\n</function>";
680        let (name, args) = parse_xml_param_tool_call(buf).unwrap();
681        assert_eq!(name, "get_weather");
682        assert!(args.contains("NYC"));
683    }
684
685    #[test]
686    fn test_ingest_token_content() {
687        let mut state = ChatState::new(ToolCallFormat::Qwen3Xml);
688        state.handle_command(ChatCommand::UserMessage("hi".into()));
689        let events = state.ingest_token("Hello world");
690        assert_eq!(events.len(), 1);
691        match &events[0] {
692            ChatEvent::Content(text) => assert_eq!(text, "Hello world"),
693            _ => panic!("expected Content event"),
694        }
695    }
696
697    #[test]
698    fn test_ingest_token_thinking() {
699        let mut state = ChatState::new(ToolCallFormat::Qwen3Xml);
700        state.handle_command(ChatCommand::UserMessage("hi".into()));
701        let events = state.ingest_token("<think>reasoning here</think>answer");
702        // Should produce: Thinking("reasoning here"), Content("answer")
703        assert_eq!(events.len(), 2);
704        match &events[0] {
705            ChatEvent::Thinking(text) => assert_eq!(text, "reasoning here"),
706            _ => panic!("expected Thinking event"),
707        }
708        match &events[1] {
709            ChatEvent::Content(text) => assert_eq!(text, "answer"),
710            _ => panic!("expected Content event"),
711        }
712    }
713
714    #[test]
715    fn test_ingest_token_tool_call_json() {
716        let mut state = ChatState::new(ToolCallFormat::Qwen3Xml);
717        state.handle_command(ChatCommand::UserMessage("list repos".into()));
718        let events =
719            state.ingest_token(r#"<tool_call>{"name": "list_repos", "arguments": {}}</tool_call>"#);
720        assert_eq!(events.len(), 1);
721        match &events[0] {
722            ChatEvent::ToolCallDetected { uuid, .. } => assert_eq!(uuid, "list_repos"),
723            _ => panic!("expected ToolCallDetected event"),
724        }
725        assert_eq!(state.pending_tool_calls, 1);
726    }
727
728    #[test]
729    fn test_ingest_token_tool_call_xml() {
730        let mut state = ChatState::new(ToolCallFormat::Qwen35XmlParam);
731        state.handle_command(ChatCommand::UserMessage("weather?".into()));
732        let events = state.ingest_token(
733            "<tool_call><function=get_weather><parameter=location>NYC</parameter></function></tool_call>"
734        );
735        assert_eq!(events.len(), 1);
736        match &events[0] {
737            ChatEvent::ToolCallDetected {
738                uuid, arguments, ..
739            } => {
740                assert_eq!(uuid, "get_weather");
741                assert!(arguments.contains("NYC"));
742            }
743            _ => panic!("expected ToolCallDetected"),
744        }
745    }
746}