[model-gateway] Tighten visibility in modules and remove unused re-exports (#16524)

This commit is contained in:
Chang Su
2026-01-05 17:38:50 -08:00
committed by GitHub
parent ce694b2b9e
commit b98bd9a5fb
33 changed files with 130 additions and 383 deletions
+3 -6
View File
@@ -5,7 +5,6 @@
pub mod errors;
pub mod factory;
pub mod partial_json;
pub mod state;
pub mod traits;
pub mod types;
@@ -15,13 +14,11 @@ pub mod parsers;
#[cfg(test)]
mod tests;
// Re-export commonly used types
pub use errors::{ParserError, ParserResult};
pub use factory::{ParserFactory, ParserRegistry, PooledParser};
// Re-export parsers for convenience
// Re-export types used outside this module
pub use factory::{ParserFactory, PooledParser};
pub use parsers::{
DeepSeekParser, Glm4MoeParser, JsonParser, KimiK2Parser, LlamaParser, MinimaxM2Parser,
MistralParser, PythonicParser, QwenParser, Step3Parser,
};
pub use traits::{PartialJsonParser, ToolParser};
pub use traits::ToolParser;
pub use types::{FunctionCall, PartialToolCall, StreamingParseResult, ToolCall};
@@ -186,7 +186,7 @@ pub fn normalize_arguments_field(mut obj: Value) -> Value {
/// - `Ok(StreamingParseResult)` with any tool call items to stream
/// - `Err(ParserError)` if JSON parsing or serialization fails
#[allow(clippy::too_many_arguments)]
pub fn handle_json_tool_streaming(
pub(crate) fn handle_json_tool_streaming(
current_text: &str,
start_idx: usize,
partial_json: &mut crate::tool_parser::partial_json::PartialJson,
@@ -26,7 +26,7 @@ pub use kimik2::KimiK2Parser;
pub use llama::LlamaParser;
pub use minimax_m2::MinimaxM2Parser;
pub use mistral::MistralParser;
pub use passthrough::PassthroughParser;
pub(crate) use passthrough::PassthroughParser;
pub use pythonic::PythonicParser;
pub use qwen::QwenParser;
pub use step3::Step3Parser;
@@ -17,7 +17,7 @@ use crate::{
/// Passthrough parser that returns text unchanged with no tool calls
#[derive(Default)]
pub struct PassthroughParser;
pub(crate) struct PassthroughParser;
impl PassthroughParser {
pub fn new() -> Self {
@@ -531,23 +531,3 @@ impl<'a> Parser<'a> {
}
}
}
/// Utility function to check if a string contains complete JSON
pub fn is_complete_json(input: &str) -> bool {
serde_json::from_str::<Value>(input).is_ok()
}
/// Utility function to find common prefix between two strings
pub fn find_common_prefix(s1: &str, s2: &str) -> usize {
s1.chars()
.zip(s2.chars())
.take_while(|(a, b)| a == b)
.count()
}
/// Utility function to compute diff between old and new strings
pub fn compute_diff(old: &str, new: &str) -> String {
let common_len = find_common_prefix(old, new);
// Convert character count to byte offset
new.chars().skip(common_len).collect()
}
@@ -1,16 +0,0 @@
/// Placeholder for Harmony streaming metadata captured during token-aware parsing.
#[derive(Debug, Clone, Default)]
pub struct HarmonyStreamState {
/// All tokens observed so far for the current assistant response.
pub tokens: Vec<u32>,
/// Number of tokens that have already been processed by the Harmony parser.
pub processed_tokens: usize,
/// Number of tool calls emitted downstream.
pub emitted_calls: usize,
/// Pending analysis-channel content awaiting flush into normal text output.
pub analysis_buffer: String,
/// Whether the tool name has been surfaced for the current call.
pub emitted_name: bool,
/// Whether arguments have been surfaced for the current call.
pub emitted_args: bool,
}
+1 -36
View File
@@ -1,9 +1,5 @@
use super::*;
use crate::tool_parser::{
parsers::JsonParser,
partial_json::{compute_diff, find_common_prefix, is_complete_json, PartialJson},
traits::ToolParser,
};
use crate::tool_parser::{parsers::JsonParser, partial_json::PartialJson, traits::ToolParser};
#[tokio::test]
async fn test_tool_parser_factory() {
@@ -97,37 +93,6 @@ fn test_partial_json_depth_limit() {
assert!(result.is_err());
}
#[test]
fn test_is_complete_json() {
assert!(is_complete_json(r#"{"name": "test"}"#));
assert!(is_complete_json(r#"[1, 2, 3]"#));
assert!(is_complete_json(r#""string""#));
assert!(is_complete_json("42"));
assert!(is_complete_json("true"));
assert!(is_complete_json("null"));
assert!(!is_complete_json(r#"{"name": "#));
assert!(!is_complete_json(r#"[1, 2, "#));
assert!(!is_complete_json(r#""unclosed"#));
}
#[test]
fn test_find_common_prefix() {
assert_eq!(find_common_prefix("hello", "hello"), 5);
assert_eq!(find_common_prefix("hello", "help"), 3);
assert_eq!(find_common_prefix("hello", "world"), 0);
assert_eq!(find_common_prefix("", "hello"), 0);
assert_eq!(find_common_prefix("hello", ""), 0);
}
#[test]
fn test_compute_diff() {
assert_eq!(compute_diff("hello", "hello world"), " world");
assert_eq!(compute_diff("", "hello"), "hello");
assert_eq!(compute_diff("hello", "hello"), "");
assert_eq!(compute_diff("test", "hello"), "hello");
}
// NOTE: test_stream_result_variants removed - StreamResult enum replaced by StreamingParseResult
#[test]
@@ -16,42 +16,6 @@ pub struct FunctionCall {
pub arguments: String,
}
/// Streaming parse result
#[derive(Debug, Clone)]
pub enum StreamResult {
/// Need more data to continue parsing
Incomplete,
/// Found a tool name (for streaming)
ToolName { index: usize, name: String },
/// Found incremental arguments (for streaming)
ToolArguments { index: usize, arguments: String },
/// Completed parsing a tool
ToolComplete(ToolCall),
/// Normal text (not part of tool call)
NormalText(String),
}
/// Token configuration for parsing
#[derive(Debug, Clone)]
pub struct TokenConfig {
/// Start tokens for tool calls
pub start_tokens: Vec<String>,
/// End tokens for tool calls
pub end_tokens: Vec<String>,
/// Separator between multiple tool calls
pub separator: String,
}
impl TokenConfig {
/// Iterate over start/end token pairs
pub fn iter_pairs(&self) -> impl Iterator<Item = (&str, &str)> {
self.start_tokens
.iter()
.zip(self.end_tokens.iter())
.map(|(s, e)| (s.as_str(), e.as_str()))
}
}
/// Simple partial tool call for streaming
#[derive(Debug, Clone)]
pub struct PartialToolCall {