[router] add tool parser base structure and partial json parser (#9482)

This commit is contained in:
Chang Su
2025-08-21 22:08:56 -07:00
committed by GitHub
parent 13ec8d427e
commit 816c4c8572
11 changed files with 1260 additions and 22 deletions

View File

@@ -0,0 +1,35 @@
use crate::tool_parser::{
errors::ToolParserResult,
state::ParseState,
types::{StreamResult, ToolCall},
};
use async_trait::async_trait;
/// Core trait for all tool parsers
#[async_trait]
pub trait ToolParser: Send + Sync {
/// Parse complete tool calls from final output
async fn parse_complete(&self, output: &str) -> ToolParserResult<Vec<ToolCall>>;
/// Parse tool calls from model output (streaming)
async fn parse_incremental(
&self,
chunk: &str,
state: &mut ParseState,
) -> ToolParserResult<StreamResult>;
/// Check if text contains tool calls in this parser's format
fn detect_format(&self, text: &str) -> bool;
}
/// Trait for partial JSON parsing
pub trait PartialJsonParser: Send + Sync {
/// Parse potentially incomplete JSON
fn parse(&self, input: &str) -> ToolParserResult<(serde_json::Value, usize)>;
/// Check if JSON is complete
fn is_complete(&self, input: &str) -> bool;
/// Get the maximum parsing depth
fn max_depth(&self) -> usize;
}