Readability improvements across streaming_parser, input_formatter, commands
- streaming_parser.rs: Reduced ~70 lines by removing redundant comments, consolidating doc comments, using slice syntax for TOOL_CALL_PATTERNS - input_formatter.rs: Lazy regex compilation via once_cell (performance), cleaner function structure, reduced comment noise - commands.rs: Extracted format_research_task_summary() and format_research_report_header() helpers, reduced ~40 lines of duplication - pending_research.rs: Fixed 2 unused variable warnings in tests All changes are behavior-preserving. 446 tests pass. Agent: carmack
This commit is contained in:
@@ -1,19 +1,21 @@
|
||||
//! Streaming tool parser for processing LLM response chunks.
|
||||
//!
|
||||
//! This module handles parsing of tool calls from streaming LLM responses,
|
||||
//! supporting both native tool calls and JSON-based fallback parsing.
|
||||
//! Parses tool calls from streaming LLM responses, supporting:
|
||||
//! - Native tool calls (returned directly by the provider)
|
||||
//! - JSON-based fallback parsing (for embedded models)
|
||||
//!
|
||||
//! **Important**: JSON tool calls are only recognized when they appear on their
|
||||
//! own line (preceded by a newline or at the start of the buffer). This prevents
|
||||
//! inline JSON examples in prose from being incorrectly parsed as tool calls.
|
||||
//! # JSON Tool Call Recognition
|
||||
//!
|
||||
//! To prevent false positives from JSON examples in prose, tool calls are only
|
||||
//! recognized when they appear "on their own line" - either at the start of the
|
||||
//! buffer or preceded by a newline (with optional whitespace).
|
||||
|
||||
use tracing::debug;
|
||||
|
||||
use crate::ToolCall;
|
||||
|
||||
/// Patterns used to detect JSON tool calls in text.
|
||||
/// These cover common whitespace variations in JSON formatting.
|
||||
const TOOL_CALL_PATTERNS: [&str; 4] = [
|
||||
/// JSON patterns that indicate a tool call. Covers common whitespace variations.
|
||||
const TOOL_CALL_PATTERNS: &[&str] = &[
|
||||
r#"{"tool":"#,
|
||||
r#"{ "tool":"#,
|
||||
r#"{"tool" :"#,
|
||||
@@ -24,10 +26,7 @@ const TOOL_CALL_PATTERNS: [&str; 4] = [
|
||||
// Code Fence Tracking
|
||||
// ============================================================================
|
||||
|
||||
/// Tracks whether we're inside a markdown code fence (``` block).
|
||||
///
|
||||
/// Used during streaming to avoid parsing JSON examples inside code blocks
|
||||
/// as tool calls.
|
||||
/// Tracks code fence state to avoid parsing JSON examples inside ``` blocks.
|
||||
#[derive(Debug, Default)]
|
||||
struct CodeFenceTracker {
|
||||
/// Whether we're currently inside a code fence
|
||||
@@ -53,10 +52,8 @@ impl CodeFenceTracker {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if current_line is a code fence marker and toggle state if so.
|
||||
fn check_and_toggle_fence(&mut self) {
|
||||
let trimmed = self.current_line.trim_start();
|
||||
if trimmed.starts_with("```") && trimmed.chars().take_while(|&c| c == '`').count() >= 3 {
|
||||
if self.current_line.trim_start().starts_with("```") {
|
||||
self.in_fence = !self.in_fence;
|
||||
debug!(
|
||||
"Code fence toggled: in_fence={} (line: {:?})",
|
||||
@@ -75,9 +72,7 @@ impl CodeFenceTracker {
|
||||
}
|
||||
}
|
||||
|
||||
/// Find all code fence ranges in text (for batch processing).
|
||||
///
|
||||
/// Returns a vector of (start, end) byte positions where code fences are.
|
||||
/// Find all code fence ranges in text. Returns (start, end) byte positions.
|
||||
/// Each range represents content INSIDE a fence (between ``` markers).
|
||||
fn find_code_fence_ranges(text: &str) -> Vec<(usize, usize)> {
|
||||
let mut ranges = Vec::new();
|
||||
@@ -121,8 +116,7 @@ fn is_position_in_fence_ranges(pos: usize, ranges: &[(usize, usize)]) -> bool {
|
||||
// JSON Parsing Utilities
|
||||
// ============================================================================
|
||||
|
||||
/// Find the end position (byte index) of a complete JSON object in the text.
|
||||
/// Returns None if no complete JSON object is found.
|
||||
/// Find the end byte index of a complete JSON object, or None if incomplete.
|
||||
fn find_json_object_end(text: &str) -> Option<usize> {
|
||||
let mut brace_count = 0;
|
||||
let mut in_string = false;
|
||||
@@ -155,12 +149,12 @@ fn find_json_object_end(text: &str) -> Option<usize> {
|
||||
None
|
||||
}
|
||||
|
||||
/// Check if a partial JSON tool call has been invalidated by subsequent content.
|
||||
/// Check if a partial JSON tool call has been invalidated.
|
||||
///
|
||||
/// Detects two invalidation cases:
|
||||
/// Invalidation cases:
|
||||
/// 1. Unescaped newline inside a JSON string (invalid JSON)
|
||||
/// 2. Newline followed by non-JSON prose (e.g., regular text, not `"`, `{`, `}`, etc.)
|
||||
/// 3. Newline followed by a new tool call pattern (`{"tool"`) - indicates abandoned fragment
|
||||
/// 2. Newline followed by non-JSON prose (regular text)
|
||||
/// 3. Newline followed by a new tool call pattern - indicates abandoned fragment
|
||||
fn is_json_invalidated(json_text: &str) -> bool {
|
||||
let mut in_string = false;
|
||||
let mut escape_next = false;
|
||||
@@ -187,8 +181,7 @@ fn is_json_invalidated(json_text: &str) -> bool {
|
||||
|
||||
// Check what comes after the newline
|
||||
if let Some(&(next_pos, next_ch)) = chars.peek() {
|
||||
// Check if this is the start of a NEW tool call pattern
|
||||
// This indicates the previous JSON fragment was abandoned
|
||||
// New tool call pattern = previous fragment was abandoned
|
||||
let remaining = &json_text[next_pos..];
|
||||
if remaining.starts_with("{\"tool\"")
|
||||
|| remaining.starts_with("{ \"tool\"")
|
||||
@@ -198,7 +191,6 @@ fn is_json_invalidated(json_text: &str) -> bool {
|
||||
return true; // New tool call started, previous fragment is abandoned
|
||||
}
|
||||
|
||||
// Check if next char is valid JSON continuation
|
||||
let valid_json_char = matches!(
|
||||
next_ch,
|
||||
'"' | '{' | '}' | '[' | ']' | ':' | ',' | '-' | '0'..='9' | 't' | 'f' | 'n' | '\n'
|
||||
@@ -216,11 +208,8 @@ fn is_json_invalidated(json_text: &str) -> bool {
|
||||
}
|
||||
|
||||
/// Detect malformed tool calls where LLM prose leaked into JSON keys.
|
||||
///
|
||||
/// When the LLM "stutters" or mixes formats, it sometimes emits JSON where
|
||||
/// the keys are actually fragments of conversational text rather than valid
|
||||
/// parameter names.
|
||||
fn args_contain_prose_fragments(args: &serde_json::Map<String, serde_json::Value>) -> bool {
|
||||
// When the LLM "stutters", keys may contain conversational text fragments
|
||||
const PROSE_MARKERS: &[&str] = &[
|
||||
"I'll", "Let me", "Here's", "I can", "I need", "First", "Now", "The ",
|
||||
];
|
||||
@@ -236,9 +225,7 @@ fn args_contain_prose_fragments(args: &serde_json::Map<String, serde_json::Value
|
||||
// Tool Call Pattern Matching
|
||||
// ============================================================================
|
||||
|
||||
/// Check if a position in text is "on its own line" - meaning it's either
|
||||
/// at the start of the text, or preceded by a newline with only whitespace
|
||||
/// between the newline and the position.
|
||||
/// True if position is at start of text or preceded only by whitespace after newline.
|
||||
fn is_on_own_line(text: &str, pos: usize) -> bool {
|
||||
if pos == 0 {
|
||||
return true;
|
||||
@@ -247,22 +234,19 @@ fn is_on_own_line(text: &str, pos: usize) -> bool {
|
||||
text[line_start..pos].chars().all(|c| c.is_whitespace())
|
||||
}
|
||||
|
||||
/// Find the first tool call pattern that appears on its own line.
|
||||
fn find_first_tool_call_start(text: &str) -> Option<usize> {
|
||||
find_tool_call_start(text, false)
|
||||
}
|
||||
|
||||
/// Find the last tool call pattern that appears on its own line.
|
||||
fn find_last_tool_call_start(text: &str) -> Option<usize> {
|
||||
find_tool_call_start(text, true)
|
||||
}
|
||||
|
||||
/// Find a tool call pattern in text, optionally searching backwards.
|
||||
/// Only matches patterns on their own line (at start or after newline + whitespace).
|
||||
/// Find a tool call pattern on its own line. If `find_last`, search backwards.
|
||||
fn find_tool_call_start(text: &str, find_last: bool) -> Option<usize> {
|
||||
let mut best_pos: Option<usize> = None;
|
||||
|
||||
for pattern in &TOOL_CALL_PATTERNS {
|
||||
for pattern in TOOL_CALL_PATTERNS {
|
||||
if find_last {
|
||||
// Search backwards
|
||||
let mut search_end = text.len();
|
||||
@@ -306,20 +290,16 @@ fn find_tool_call_start(text: &str, find_last: bool) -> Option<usize> {
|
||||
// StreamingToolParser
|
||||
// ============================================================================
|
||||
|
||||
/// Modern streaming tool parser that properly handles native tool calls and SSE chunks.
|
||||
/// Streaming parser for tool calls from LLM responses (native or JSON fallback).
|
||||
#[derive(Debug)]
|
||||
pub struct StreamingToolParser {
|
||||
/// Buffer for accumulating text content
|
||||
text_buffer: String,
|
||||
/// Position in text_buffer up to which tool calls have been consumed/executed.
|
||||
last_consumed_position: usize,
|
||||
/// Whether we've received a message_stop event
|
||||
message_stopped: bool,
|
||||
/// Whether we're currently in a JSON tool call (for fallback parsing)
|
||||
// JSON fallback parsing state
|
||||
in_json_tool_call: bool,
|
||||
/// Start position of JSON tool call (for fallback parsing)
|
||||
json_tool_start: Option<usize>,
|
||||
/// Tracks code fence state during streaming
|
||||
// Code fence tracking (to skip JSON examples in ``` blocks)
|
||||
fence_tracker: CodeFenceTracker,
|
||||
}
|
||||
|
||||
@@ -345,13 +325,11 @@ impl StreamingToolParser {
|
||||
pub fn process_chunk(&mut self, chunk: &g3_providers::CompletionChunk) -> Vec<ToolCall> {
|
||||
let mut completed_tools = Vec::new();
|
||||
|
||||
// Add text content to buffer and track code fence state
|
||||
if !chunk.content.is_empty() {
|
||||
self.fence_tracker.process(&chunk.content);
|
||||
self.text_buffer.push_str(&chunk.content);
|
||||
}
|
||||
|
||||
// Handle native tool calls - return them immediately when received
|
||||
if let Some(ref tool_calls) = chunk.tool_calls {
|
||||
debug!("Received native tool calls: {:?}", tool_calls);
|
||||
for tool_call in tool_calls {
|
||||
@@ -362,10 +340,8 @@ impl StreamingToolParser {
|
||||
}
|
||||
}
|
||||
|
||||
// Check if message is finished/stopped
|
||||
if chunk.finished {
|
||||
self.message_stopped = true;
|
||||
debug!("Message finished, processing accumulated tool calls");
|
||||
|
||||
// When stream finishes, find ALL JSON tool calls in the accumulated buffer
|
||||
if completed_tools.is_empty() && !self.text_buffer.is_empty() {
|
||||
@@ -380,8 +356,7 @@ impl StreamingToolParser {
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback: Try to parse JSON tool calls from current chunk content if no native tool calls.
|
||||
// Skip when inside a code fence to prevent false positives from JSON examples.
|
||||
// JSON fallback: try to parse if no native calls and not inside a code fence
|
||||
if completed_tools.is_empty()
|
||||
&& !chunk.content.is_empty()
|
||||
&& !chunk.finished
|
||||
@@ -395,14 +370,11 @@ impl StreamingToolParser {
|
||||
completed_tools
|
||||
}
|
||||
|
||||
/// Try to parse a JSON tool call from the streaming buffer.
|
||||
///
|
||||
/// Maintains state (`in_json_tool_call`, `json_tool_start`) to track
|
||||
/// partial JSON tool calls across streaming chunks.
|
||||
/// Try to parse a JSON tool call, tracking partial state across chunks.
|
||||
fn try_parse_streaming_json_tool_call(&mut self) -> Option<ToolCall> {
|
||||
let fence_ranges = find_code_fence_ranges(&self.text_buffer);
|
||||
|
||||
// If not currently in a JSON tool call, look for the start
|
||||
// Look for the start of a new tool call
|
||||
if !self.in_json_tool_call {
|
||||
let unchecked_buffer = &self.text_buffer[self.last_consumed_position..];
|
||||
if let Some(relative_pos) = find_first_tool_call_start(unchecked_buffer) {
|
||||
@@ -428,7 +400,6 @@ impl StreamingToolParser {
|
||||
if let Some(start_pos) = self.json_tool_start {
|
||||
let json_text = &self.text_buffer[start_pos..];
|
||||
|
||||
// Try to find a complete JSON object
|
||||
if let Some(end_pos) = find_json_object_end(json_text) {
|
||||
let json_str = &json_text[..=end_pos];
|
||||
debug!("Attempting to parse JSON tool call: {}", json_str);
|
||||
@@ -439,12 +410,10 @@ impl StreamingToolParser {
|
||||
return Some(tool_call);
|
||||
}
|
||||
|
||||
// Parse failed, reset and continue looking
|
||||
self.in_json_tool_call = false;
|
||||
self.json_tool_start = None;
|
||||
}
|
||||
|
||||
// Check if the partial JSON has been invalidated
|
||||
if self.in_json_tool_call && is_json_invalidated(json_text) {
|
||||
debug!("JSON tool call invalidated by subsequent content, clearing state");
|
||||
self.in_json_tool_call = false;
|
||||
@@ -458,7 +427,7 @@ impl StreamingToolParser {
|
||||
None
|
||||
}
|
||||
|
||||
/// Parse ALL JSON tool calls from the accumulated text buffer.
|
||||
/// Parse all JSON tool calls from the accumulated buffer (used at stream end).
|
||||
fn parse_all_json_tool_calls(&self) -> Vec<ToolCall> {
|
||||
let mut tool_calls = Vec::new();
|
||||
let mut search_start = 0;
|
||||
@@ -472,7 +441,6 @@ impl StreamingToolParser {
|
||||
};
|
||||
|
||||
let abs_start = search_start + relative_pos;
|
||||
let json_text = &self.text_buffer[abs_start..];
|
||||
|
||||
// Skip if inside a code fence
|
||||
if is_position_in_fence_ranges(abs_start, &fence_ranges) {
|
||||
@@ -480,7 +448,7 @@ impl StreamingToolParser {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Try to find a complete JSON object
|
||||
let json_text = &self.text_buffer[abs_start..];
|
||||
let Some(end_pos) = find_json_object_end(json_text) else {
|
||||
break; // Incomplete JSON, stop searching
|
||||
};
|
||||
@@ -497,31 +465,22 @@ impl StreamingToolParser {
|
||||
tool_calls
|
||||
}
|
||||
|
||||
/// Try to parse a JSON string as a ToolCall, validating the args.
|
||||
fn try_parse_tool_call_json(&self, json_str: &str) -> Option<ToolCall> {
|
||||
let tool_call: ToolCall = serde_json::from_str(json_str).ok()?;
|
||||
|
||||
// Validate that args is an object with reasonable keys
|
||||
let args_obj = tool_call.args.as_object()?;
|
||||
|
||||
if args_contain_prose_fragments(args_obj) {
|
||||
debug!("Detected malformed tool call with message-like keys, skipping");
|
||||
return None;
|
||||
}
|
||||
|
||||
debug!("Successfully parsed valid JSON tool call: {:?}", tool_call);
|
||||
Some(tool_call)
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// Public Accessors
|
||||
// ========================================================================
|
||||
|
||||
/// Get the accumulated text content.
|
||||
// --- Public Accessors ---
|
||||
pub fn get_text_content(&self) -> &str {
|
||||
&self.text_buffer
|
||||
}
|
||||
|
||||
/// Get content before a specific position (for display purposes).
|
||||
pub fn get_content_before_position(&self, pos: usize) -> String {
|
||||
if pos <= self.text_buffer.len() {
|
||||
self.text_buffer[..pos].to_string()
|
||||
@@ -530,12 +489,10 @@ impl StreamingToolParser {
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if the message has been stopped/finished.
|
||||
pub fn is_message_stopped(&self) -> bool {
|
||||
self.message_stopped
|
||||
}
|
||||
|
||||
/// Check if the text buffer contains an incomplete JSON tool call.
|
||||
pub fn has_incomplete_tool_call(&self) -> bool {
|
||||
let unchecked_buffer = &self.text_buffer[self.last_consumed_position..];
|
||||
let Some(start_pos) = find_last_tool_call_start(unchecked_buffer) else {
|
||||
@@ -544,7 +501,6 @@ impl StreamingToolParser {
|
||||
|
||||
let json_text = &unchecked_buffer[start_pos..];
|
||||
|
||||
// Complete or invalidated = not incomplete
|
||||
if find_json_object_end(json_text).is_some() || is_json_invalidated(json_text) {
|
||||
return false;
|
||||
}
|
||||
@@ -552,7 +508,6 @@ impl StreamingToolParser {
|
||||
true
|
||||
}
|
||||
|
||||
/// Check if the text buffer contains an unexecuted tool call.
|
||||
pub fn has_unexecuted_tool_call(&self) -> bool {
|
||||
let unchecked_buffer = &self.text_buffer[self.last_consumed_position..];
|
||||
let Some(start_pos) = find_last_tool_call_start(unchecked_buffer) else {
|
||||
@@ -568,27 +523,22 @@ impl StreamingToolParser {
|
||||
serde_json::from_str::<serde_json::Value>(json_only).is_ok()
|
||||
}
|
||||
|
||||
/// Mark all tool calls up to the current buffer position as consumed/executed.
|
||||
pub fn mark_tool_calls_consumed(&mut self) {
|
||||
self.last_consumed_position = self.text_buffer.len();
|
||||
}
|
||||
|
||||
/// Get the current text buffer length (for position tracking).
|
||||
pub fn text_buffer_len(&self) -> usize {
|
||||
self.text_buffer.len()
|
||||
}
|
||||
|
||||
/// Check if currently parsing a JSON tool call (for debugging).
|
||||
pub fn is_in_json_tool_call(&self) -> bool {
|
||||
self.in_json_tool_call
|
||||
}
|
||||
|
||||
/// Get the JSON tool start position (for debugging).
|
||||
pub fn json_tool_start_position(&self) -> Option<usize> {
|
||||
self.json_tool_start
|
||||
}
|
||||
|
||||
/// Reset the parser state for a new message.
|
||||
pub fn reset(&mut self) {
|
||||
self.text_buffer.clear();
|
||||
self.last_consumed_position = 0;
|
||||
@@ -598,34 +548,25 @@ impl StreamingToolParser {
|
||||
self.fence_tracker.reset();
|
||||
}
|
||||
|
||||
// ========================================================================
|
||||
// Static Methods (for external use)
|
||||
// ========================================================================
|
||||
|
||||
/// Find the starting position of the FIRST tool call pattern on its own line.
|
||||
// --- Static Methods (for external use) ---
|
||||
pub fn find_first_tool_call_start(text: &str) -> Option<usize> {
|
||||
find_first_tool_call_start(text)
|
||||
}
|
||||
|
||||
/// Find the starting position of the LAST tool call pattern on its own line.
|
||||
pub fn find_last_tool_call_start(text: &str) -> Option<usize> {
|
||||
find_last_tool_call_start(text)
|
||||
}
|
||||
|
||||
/// Check if a position in text is "on its own line".
|
||||
pub fn is_on_own_line(text: &str, pos: usize) -> bool {
|
||||
is_on_own_line(text, pos)
|
||||
}
|
||||
|
||||
/// Find the end position of a complete JSON object.
|
||||
pub fn find_complete_json_object_end(text: &str) -> Option<usize> {
|
||||
find_json_object_end(text)
|
||||
}
|
||||
}
|
||||
|
||||
// ============================================================================
|
||||
// Tests
|
||||
// ============================================================================
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
||||
Reference in New Issue
Block a user