Skip to content

Commit 84305ab

Browse files
committed
Use more idiomatic naming in parser module
1 parent fb064eb commit 84305ab

28 files changed

Lines changed: 192 additions & 200 deletions

sgl-router/benches/tool_parser_benchmark.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
use criterion::{black_box, criterion_group, BenchmarkId, Criterion, Throughput};
1111
use serde_json::json;
1212
use sglang_router_rs::protocols::spec::{Function, Tool};
13-
use sglang_router_rs::tool_parser::{JsonParser, ToolParser, ToolParserFactory};
13+
use sglang_router_rs::tool_parser::{JsonParser, ToolParser, ParserFactory as ToolParserFactory};
1414
use std::collections::BTreeMap;
1515
use std::sync::atomic::{AtomicBool, AtomicU64, Ordering};
1616
use std::sync::{Arc, Mutex};

sgl-router/src/reasoning_parser/factory.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -132,11 +132,11 @@ impl Default for ParserRegistry {
132132

133133
/// Factory for creating reasoning parsers based on model type.
134134
#[derive(Clone)]
135-
pub struct ReasoningParserFactory {
135+
pub struct ParserFactory {
136136
registry: ParserRegistry,
137137
}
138138

139-
impl ReasoningParserFactory {
139+
impl ParserFactory {
140140
/// Create a new factory with default parsers registered.
141141
pub fn new() -> Self {
142142
let registry = ParserRegistry::new();
@@ -241,7 +241,7 @@ impl ReasoningParserFactory {
241241
}
242242
}
243243

244-
impl Default for ReasoningParserFactory {
244+
impl Default for ParserFactory {
245245
fn default() -> Self {
246246
Self::new()
247247
}
@@ -253,35 +253,35 @@ mod tests {
253253

254254
#[test]
255255
fn test_factory_creates_deepseek_r1() {
256-
let factory = ReasoningParserFactory::new();
256+
let factory = ParserFactory::new();
257257
let parser = factory.create("deepseek-r1-distill").unwrap();
258258
assert_eq!(parser.model_type(), "deepseek_r1");
259259
}
260260

261261
#[test]
262262
fn test_factory_creates_qwen3() {
263-
let factory = ReasoningParserFactory::new();
263+
let factory = ParserFactory::new();
264264
let parser = factory.create("qwen3-7b").unwrap();
265265
assert_eq!(parser.model_type(), "qwen3");
266266
}
267267

268268
#[test]
269269
fn test_factory_creates_kimi() {
270-
let factory = ReasoningParserFactory::new();
270+
let factory = ParserFactory::new();
271271
let parser = factory.create("kimi-chat").unwrap();
272272
assert_eq!(parser.model_type(), "kimi");
273273
}
274274

275275
#[test]
276276
fn test_factory_fallback_to_passthrough() {
277-
let factory = ReasoningParserFactory::new();
277+
let factory = ParserFactory::new();
278278
let parser = factory.create("unknown-model").unwrap();
279279
assert_eq!(parser.model_type(), "passthrough");
280280
}
281281

282282
#[test]
283283
fn test_case_insensitive_matching() {
284-
let factory = ReasoningParserFactory::new();
284+
let factory = ParserFactory::new();
285285
let parser1 = factory.create("DeepSeek-R1").unwrap();
286286
let parser2 = factory.create("QWEN3").unwrap();
287287
let parser3 = factory.create("Kimi").unwrap();
@@ -293,21 +293,21 @@ mod tests {
293293

294294
#[test]
295295
fn test_step3_model() {
296-
let factory = ReasoningParserFactory::new();
296+
let factory = ParserFactory::new();
297297
let step3 = factory.create("step3-model").unwrap();
298298
assert_eq!(step3.model_type(), "step3");
299299
}
300300

301301
#[test]
302302
fn test_glm45_model() {
303-
let factory = ReasoningParserFactory::new();
303+
let factory = ParserFactory::new();
304304
let glm45 = factory.create("glm45-v2").unwrap();
305305
assert_eq!(glm45.model_type(), "glm45");
306306
}
307307

308308
#[tokio::test]
309309
async fn test_pooled_parser_reuse() {
310-
let factory = ReasoningParserFactory::new();
310+
let factory = ParserFactory::new();
311311

312312
// Get the same parser twice - should be the same instance
313313
let parser1 = factory.get_pooled("deepseek-r1");
@@ -323,7 +323,7 @@ mod tests {
323323

324324
#[tokio::test]
325325
async fn test_pooled_parser_concurrent_access() {
326-
let factory = ReasoningParserFactory::new();
326+
let factory = ParserFactory::new();
327327
let parser = factory.get_pooled("deepseek-r1");
328328

329329
// Spawn multiple async tasks that use the same parser
@@ -349,7 +349,7 @@ mod tests {
349349

350350
#[tokio::test]
351351
async fn test_pool_clearing() {
352-
let factory = ReasoningParserFactory::new();
352+
let factory = ParserFactory::new();
353353

354354
// Get a pooled parser
355355
let parser1 = factory.get_pooled("deepseek-r1");
@@ -366,7 +366,7 @@ mod tests {
366366

367367
#[tokio::test]
368368
async fn test_passthrough_parser_pooling() {
369-
let factory = ReasoningParserFactory::new();
369+
let factory = ParserFactory::new();
370370

371371
// Unknown models should get passthrough parser
372372
let parser1 = factory.get_pooled("unknown-model-1");
@@ -384,7 +384,7 @@ mod tests {
384384
use std::sync::atomic::{AtomicUsize, Ordering};
385385
use std::time::Instant;
386386

387-
let factory = ReasoningParserFactory::new();
387+
let factory = ParserFactory::new();
388388
let num_tasks = 100;
389389
let requests_per_task = 50;
390390
let models = vec!["deepseek-r1", "qwen3", "kimi", "qwen3-thinking"];
@@ -513,7 +513,7 @@ mod tests {
513513

514514
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
515515
async fn test_concurrent_pool_modifications() {
516-
let factory = ReasoningParserFactory::new();
516+
let factory = ParserFactory::new();
517517
let mut handles = vec![];
518518

519519
// Task 1: Continuously get parsers

sgl-router/src/reasoning_parser/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ pub mod factory;
22
pub mod parsers;
33
pub mod traits;
44

5-
pub use factory::{ParserRegistry, PooledParser, ReasoningParserFactory};
5+
pub use factory::{ParserFactory, PooledParser, ParserRegistry};
66
pub use parsers::{
77
BaseReasoningParser, DeepSeekR1Parser, Glm45Parser, KimiParser, Qwen3Parser,
88
QwenThinkingParser, Step3Parser,

sgl-router/src/routers/grpc/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ use crate::grpc_client::{proto, SglangSchedulerClient};
1515
use crate::protocols::spec::{
1616
ChatCompletionRequest, ChatCompletionResponse, GenerateRequest, GenerateResponse,
1717
};
18-
use crate::reasoning_parser::ReasoningParserFactory;
18+
use crate::reasoning_parser::ParserFactory as ReasoningParserFactory;
1919
use crate::tokenizer::stop::StopSequenceDecoder;
2020
use crate::tokenizer::traits::Tokenizer;
21-
use crate::tool_parser::ToolParserFactory;
21+
use crate::tool_parser::ParserFactory as ToolParserFactory;
2222

2323
// ============================================================================
2424
// Core Context Types

sgl-router/src/routers/grpc/pd_router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ use crate::protocols::spec::{
77
ChatCompletionRequest, CompletionRequest, EmbeddingRequest, GenerateRequest, RerankRequest,
88
ResponsesGetParams, ResponsesRequest,
99
};
10-
use crate::reasoning_parser::ReasoningParserFactory;
10+
use crate::reasoning_parser::ParserFactory as ReasoningParserFactory;
1111
use crate::routers::RouterTrait;
1212
use crate::server::AppContext;
1313
use crate::tokenizer::traits::Tokenizer;
14-
use crate::tool_parser::ToolParserFactory;
14+
use crate::tool_parser::ParserFactory as ToolParserFactory;
1515
use async_trait::async_trait;
1616
use axum::{
1717
body::Body,

sgl-router/src/routers/grpc/processing.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ use crate::protocols::spec::{
1313
ChatChoice, ChatCompletionMessage, ChatCompletionRequest, FunctionCallResponse, ToolCall,
1414
ToolChoice, ToolChoiceValue,
1515
};
16-
use crate::reasoning_parser::ReasoningParserFactory;
16+
use crate::reasoning_parser::ParserFactory as ReasoningParserFactory;
1717
use crate::tokenizer::stop::{SequenceDecoderOutput, StopSequenceDecoder};
1818
use crate::tokenizer::traits::Tokenizer;
19-
use crate::tool_parser::ToolParserFactory;
19+
use crate::tool_parser::ParserFactory as ToolParserFactory;
2020

2121
use super::utils;
2222

sgl-router/src/routers/grpc/router.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use crate::protocols::spec::{
1818
ChatCompletionRequest, CompletionRequest, EmbeddingRequest, GenerateRequest, RerankRequest,
1919
ResponsesGetParams, ResponsesRequest,
2020
};
21-
use crate::reasoning_parser::ReasoningParserFactory;
21+
use crate::reasoning_parser::ParserFactory as ReasoningParserFactory;
2222
use crate::routers::RouterTrait;
2323
use crate::server::AppContext;
2424
use crate::tokenizer::traits::Tokenizer;
25-
use crate::tool_parser::ToolParserFactory;
25+
use crate::tool_parser::ParserFactory as ToolParserFactory;
2626

2727
/// gRPC router implementation for SGLang
2828
#[derive(Clone)]

sgl-router/src/routers/grpc/streaming.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ use tokio::sync::mpsc;
3434
#[derive(Clone)]
3535
pub struct StreamingProcessor {
3636
tokenizer: Arc<dyn Tokenizer>,
37-
tool_parser_factory: crate::tool_parser::ToolParserFactory,
38-
reasoning_parser_factory: crate::reasoning_parser::ReasoningParserFactory,
37+
tool_parser_factory: crate::tool_parser::ParserFactory,
38+
reasoning_parser_factory: crate::reasoning_parser::ParserFactory,
3939
configured_tool_parser: Option<String>,
4040
configured_reasoning_parser: Option<String>,
4141
}
4242

4343
impl StreamingProcessor {
4444
pub fn new(
4545
tokenizer: Arc<dyn Tokenizer>,
46-
tool_parser_factory: crate::tool_parser::ToolParserFactory,
47-
reasoning_parser_factory: crate::reasoning_parser::ReasoningParserFactory,
46+
tool_parser_factory: crate::tool_parser::ParserFactory,
47+
reasoning_parser_factory: crate::reasoning_parser::ParserFactory,
4848
configured_tool_parser: Option<String>,
4949
configured_reasoning_parser: Option<String>,
5050
) -> Self {

sgl-router/src/routers/grpc/utils.rs

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -679,12 +679,10 @@ pub fn generate_tool_call_id(
679679
/// Otherwise, auto-detect based on the model name.
680680
/// Get a pooled reasoning parser (for non-streaming where state doesn't matter)
681681
pub fn get_reasoning_parser(
682-
reasoning_parser_factory: &crate::reasoning_parser::ReasoningParserFactory,
682+
reasoning_parser_factory: &crate::reasoning_parser::ParserFactory,
683683
configured_parser: Option<&String>,
684684
model: &str,
685685
) -> crate::reasoning_parser::PooledParser {
686-
use tracing::warn;
687-
688686
if let Some(parser_name) = configured_parser {
689687
// Use configured parser if specified
690688
reasoning_parser_factory
@@ -705,12 +703,10 @@ pub fn get_reasoning_parser(
705703

706704
/// Create a fresh reasoning parser instance (for streaming where state isolation is needed)
707705
pub fn create_reasoning_parser(
708-
reasoning_parser_factory: &crate::reasoning_parser::ReasoningParserFactory,
706+
reasoning_parser_factory: &crate::reasoning_parser::ParserFactory,
709707
configured_parser: Option<&String>,
710708
model: &str,
711709
) -> Option<Box<dyn crate::reasoning_parser::ReasoningParser>> {
712-
use tracing::warn;
713-
714710
if let Some(parser_name) = configured_parser {
715711
// Use configured parser if specified
716712
reasoning_parser_factory
@@ -735,12 +731,10 @@ pub fn create_reasoning_parser(
735731
/// Otherwise, auto-detect based on the model name.
736732
/// Get a pooled tool parser (for non-streaming where state doesn't matter)
737733
pub fn get_tool_parser(
738-
tool_parser_factory: &crate::tool_parser::ToolParserFactory,
734+
tool_parser_factory: &crate::tool_parser::ParserFactory,
739735
configured_parser: Option<&String>,
740736
model: &str,
741-
) -> crate::tool_parser::PooledToolParser {
742-
use tracing::warn;
743-
737+
) -> crate::tool_parser::PooledParser {
744738
if let Some(parser_name) = configured_parser {
745739
// Use configured parser if specified
746740
tool_parser_factory
@@ -761,12 +755,10 @@ pub fn get_tool_parser(
761755

762756
/// Create a fresh tool parser instance (for streaming where state isolation is needed)
763757
pub fn create_tool_parser(
764-
tool_parser_factory: &crate::tool_parser::ToolParserFactory,
758+
tool_parser_factory: &crate::tool_parser::ParserFactory,
765759
configured_parser: Option<&String>,
766760
model: &str,
767761
) -> Option<Box<dyn crate::tool_parser::ToolParser>> {
768-
use tracing::warn;
769-
770762
if let Some(parser_name) = configured_parser {
771763
// Use configured parser if specified
772764
tool_parser_factory

sgl-router/src/server.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ use crate::{
1818
},
1919
worker_spec::{WorkerApiResponse, WorkerConfigRequest, WorkerErrorResponse},
2020
},
21-
reasoning_parser::ReasoningParserFactory,
21+
reasoning_parser::ParserFactory as ReasoningParserFactory,
2222
routers::{router_manager::RouterManager, RouterTrait},
2323
service_discovery::{start_service_discovery, ServiceDiscoveryConfig},
2424
tokenizer::{factory as tokenizer_factory, traits::Tokenizer},
25-
tool_parser::ToolParserFactory,
25+
tool_parser::ParserFactory as ToolParserFactory,
2626
};
2727
use axum::{
2828
extract::{Path, Query, Request, State},
@@ -88,8 +88,8 @@ impl AppContext {
8888
tokenizer_factory::create_tokenizer(&tokenizer_path)
8989
.map_err(|e| format!("Failed to create tokenizer: {e}"))?,
9090
);
91-
let reasoning_parser_factory = Some(ReasoningParserFactory::new());
92-
let tool_parser_factory = Some(ToolParserFactory::new());
91+
let reasoning_parser_factory = Some(crate::reasoning_parser::ParserFactory::new());
92+
let tool_parser_factory = Some(crate::tool_parser::ParserFactory::new());
9393

9494
(tokenizer, reasoning_parser_factory, tool_parser_factory)
9595
} else {

0 commit comments

Comments
 (0)