Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
change
sglang
Commits
dc01313d
Unverified
Commit
dc01313d
authored
Oct 16, 2025
by
Chang Su
Committed by
GitHub
Oct 16, 2025
Browse files
[router] Add rustfmt and set group imports by default (#11732)
parent
7a7f99be
Changes
126
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
147 additions
and
108 deletions
+147
-108
sgl-router/src/tokenizer/sequence.rs
sgl-router/src/tokenizer/sequence.rs
+4
-2
sgl-router/src/tokenizer/stop.rs
sgl-router/src/tokenizer/stop.rs
+7
-4
sgl-router/src/tokenizer/stream.rs
sgl-router/src/tokenizer/stream.rs
+4
-2
sgl-router/src/tokenizer/tests.rs
sgl-router/src/tokenizer/tests.rs
+3
-2
sgl-router/src/tokenizer/tiktoken.rs
sgl-router/src/tokenizer/tiktoken.rs
+3
-2
sgl-router/src/tokenizer/traits.rs
sgl-router/src/tokenizer/traits.rs
+5
-2
sgl-router/src/tool_parser/factory.rs
sgl-router/src/tool_parser/factory.rs
+11
-6
sgl-router/src/tool_parser/mod.rs
sgl-router/src/tool_parser/mod.rs
+2
-3
sgl-router/src/tool_parser/parsers/deepseek_parser.rs
sgl-router/src/tool_parser/parsers/deepseek_parser.rs
+8
-7
sgl-router/src/tool_parser/parsers/glm4_moe_parser.rs
sgl-router/src/tool_parser/parsers/glm4_moe_parser.rs
+8
-7
sgl-router/src/tool_parser/parsers/gpt_oss_harmony_parser.rs
sgl-router/src/tool_parser/parsers/gpt_oss_harmony_parser.rs
+7
-6
sgl-router/src/tool_parser/parsers/gpt_oss_parser.rs
sgl-router/src/tool_parser/parsers/gpt_oss_parser.rs
+9
-8
sgl-router/src/tool_parser/parsers/helpers.rs
sgl-router/src/tool_parser/parsers/helpers.rs
+9
-4
sgl-router/src/tool_parser/parsers/json_parser.rs
sgl-router/src/tool_parser/parsers/json_parser.rs
+9
-8
sgl-router/src/tool_parser/parsers/kimik2_parser.rs
sgl-router/src/tool_parser/parsers/kimik2_parser.rs
+8
-7
sgl-router/src/tool_parser/parsers/llama_parser.rs
sgl-router/src/tool_parser/parsers/llama_parser.rs
+9
-8
sgl-router/src/tool_parser/parsers/mistral_parser.rs
sgl-router/src/tool_parser/parsers/mistral_parser.rs
+9
-8
sgl-router/src/tool_parser/parsers/passthrough_parser.rs
sgl-router/src/tool_parser/parsers/passthrough_parser.rs
+9
-4
sgl-router/src/tool_parser/parsers/pythonic_parser.rs
sgl-router/src/tool_parser/parsers/pythonic_parser.rs
+14
-10
sgl-router/src/tool_parser/parsers/qwen_parser.rs
sgl-router/src/tool_parser/parsers/qwen_parser.rs
+9
-8
No files found.
sgl-router/src/tokenizer/sequence.rs
View file @
dc01313d
use
super
::
traits
::{
TokenIdType
,
Tokenizer
as
TokenizerTrait
};
use
anyhow
::
Result
;
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Arc
;
use
anyhow
::
Result
;
use
super
::
traits
::{
TokenIdType
,
Tokenizer
as
TokenizerTrait
};
/// Maintains state for an ongoing sequence of tokens and their decoded text
/// Maintains state for an ongoing sequence of tokens and their decoded text
/// This provides a cleaner abstraction for managing token sequences
/// This provides a cleaner abstraction for managing token sequences
pub
struct
Sequence
{
pub
struct
Sequence
{
...
...
sgl-router/src/tokenizer/stop.rs
View file @
dc01313d
use
s
uper
::
seque
nc
e
::
Sequence
;
use
s
td
::{
collections
::
HashSet
,
sy
nc
::
Arc
}
;
use
super
::
traits
::{
self
,
TokenIdType
};
use
anyhow
::
Result
;
use
anyhow
::
Result
;
use
std
::
collections
::
HashSet
;
use
std
::
sync
::
Arc
;
use
super
::{
sequence
::
Sequence
,
traits
::{
self
,
TokenIdType
},
};
/// Output from the sequence decoder
/// Output from the sequence decoder
#[derive(Debug,
Clone,
PartialEq)]
#[derive(Debug,
Clone,
PartialEq)]
...
...
sgl-router/src/tokenizer/stream.rs
View file @
dc01313d
// src/tokenizer/stream.rs
// src/tokenizer/stream.rs
use
super
::
traits
::{
self
,
TokenIdType
};
use
anyhow
::
Result
;
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Arc
;
use
anyhow
::
Result
;
use
super
::
traits
::{
self
,
TokenIdType
};
const
INITIAL_INCREMENTAL_DETOKENIZATION_OFFSET
:
usize
=
5
;
const
INITIAL_INCREMENTAL_DETOKENIZATION_OFFSET
:
usize
=
5
;
/// DecodeStream will keep the state necessary to produce individual chunks of
/// DecodeStream will keep the state necessary to produce individual chunks of
...
...
sgl-router/src/tokenizer/tests.rs
View file @
dc01313d
#[cfg(test)]
#[cfg(test)]
use
super
::
*
;
#[cfg(test)]
use
std
::
sync
::
Arc
;
use
std
::
sync
::
Arc
;
#[cfg(test)]
use
super
::
*
;
#[test]
#[test]
fn
test_mock_tokenizer_encode
()
{
fn
test_mock_tokenizer_encode
()
{
let
tokenizer
=
mock
::
MockTokenizer
::
new
();
let
tokenizer
=
mock
::
MockTokenizer
::
new
();
...
...
sgl-router/src/tokenizer/tiktoken.rs
View file @
dc01313d
use
anyhow
::{
Error
,
Result
};
use
tiktoken_rs
::{
cl100k_base
,
p50k_base
,
p50k_edit
,
r50k_base
,
CoreBPE
};
use
super
::
traits
::{
use
super
::
traits
::{
Decoder
,
Encoder
,
Encoding
,
SpecialTokens
,
TokenIdType
,
Tokenizer
as
TokenizerTrait
,
Decoder
,
Encoder
,
Encoding
,
SpecialTokens
,
TokenIdType
,
Tokenizer
as
TokenizerTrait
,
};
};
use
anyhow
::{
Error
,
Result
};
use
tiktoken_rs
::{
cl100k_base
,
p50k_base
,
p50k_edit
,
r50k_base
,
CoreBPE
};
/// Tiktoken tokenizer wrapper for OpenAI GPT models
/// Tiktoken tokenizer wrapper for OpenAI GPT models
pub
struct
TiktokenTokenizer
{
pub
struct
TiktokenTokenizer
{
...
...
sgl-router/src/tokenizer/traits.rs
View file @
dc01313d
use
std
::{
collections
::
hash_map
::
DefaultHasher
,
hash
::{
Hash
,
Hasher
},
};
use
anyhow
::
Result
;
use
anyhow
::
Result
;
use
std
::
collections
::
hash_map
::
DefaultHasher
;
use
std
::
hash
::{
Hash
,
Hasher
};
/// Type alias for token IDs
/// Type alias for token IDs
pub
type
TokenIdType
=
u32
;
pub
type
TokenIdType
=
u32
;
...
...
sgl-router/src/tool_parser/factory.rs
View file @
dc01313d
// Factory and pool for creating model-specific tool parsers with pooling support.
// Factory and pool for creating model-specific tool parsers with pooling support.
use
std
::
collections
::
HashMap
;
use
std
::{
use
std
::
sync
::{
Arc
,
RwLock
};
collections
::
HashMap
,
sync
::{
Arc
,
RwLock
},
};
use
tokio
::
sync
::
Mutex
;
use
tokio
::
sync
::
Mutex
;
use
crate
::
tool_parser
::
parsers
::{
use
crate
::
tool_parser
::{
DeepSeekParser
,
Glm4MoeParser
,
GptOssHarmonyParser
,
GptOssParser
,
JsonParser
,
KimiK2Parser
,
parsers
::{
LlamaParser
,
MistralParser
,
PassthroughParser
,
PythonicParser
,
QwenParser
,
Step3Parser
,
DeepSeekParser
,
Glm4MoeParser
,
GptOssHarmonyParser
,
GptOssParser
,
JsonParser
,
KimiK2Parser
,
LlamaParser
,
MistralParser
,
PassthroughParser
,
PythonicParser
,
QwenParser
,
Step3Parser
,
},
traits
::
ToolParser
,
};
};
use
crate
::
tool_parser
::
traits
::
ToolParser
;
/// Type alias for pooled parser instances.
/// Type alias for pooled parser instances.
pub
type
PooledParser
=
Arc
<
Mutex
<
Box
<
dyn
ToolParser
>>>
;
pub
type
PooledParser
=
Arc
<
Mutex
<
Box
<
dyn
ToolParser
>>>
;
...
...
sgl-router/src/tool_parser/mod.rs
View file @
dc01313d
...
@@ -18,11 +18,10 @@ mod tests;
...
@@ -18,11 +18,10 @@ mod tests;
// Re-export commonly used types
// Re-export commonly used types
pub
use
errors
::{
ParserError
,
ParserResult
};
pub
use
errors
::{
ParserError
,
ParserResult
};
pub
use
factory
::{
ParserFactory
,
ParserRegistry
,
PooledParser
};
pub
use
factory
::{
ParserFactory
,
ParserRegistry
,
PooledParser
};
pub
use
traits
::{
PartialJsonParser
,
ToolParser
};
pub
use
types
::{
FunctionCall
,
PartialToolCall
,
StreamingParseResult
,
ToolCall
};
// Re-export parsers for convenience
// Re-export parsers for convenience
pub
use
parsers
::{
pub
use
parsers
::{
DeepSeekParser
,
Glm4MoeParser
,
GptOssParser
,
JsonParser
,
KimiK2Parser
,
LlamaParser
,
DeepSeekParser
,
Glm4MoeParser
,
GptOssParser
,
JsonParser
,
KimiK2Parser
,
LlamaParser
,
MistralParser
,
PythonicParser
,
QwenParser
,
Step3Parser
,
MistralParser
,
PythonicParser
,
QwenParser
,
Step3Parser
,
};
};
pub
use
traits
::{
PartialJsonParser
,
ToolParser
};
pub
use
types
::{
FunctionCall
,
PartialToolCall
,
StreamingParseResult
,
ToolCall
};
sgl-router/src/tool_parser/parsers/deepseek_parser.rs
View file @
dc01313d
...
@@ -2,13 +2,14 @@ use async_trait::async_trait;
...
@@ -2,13 +2,14 @@ use async_trait::async_trait;
use
regex
::
Regex
;
use
regex
::
Regex
;
use
serde_json
::
Value
;
use
serde_json
::
Value
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::{
ParserError
,
ParserResult
},
errors
::{
ParserError
,
ParserResult
},
parsers
::
helpers
,
parsers
::
helpers
,
traits
::
ToolParser
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
},
};
};
/// DeepSeek V3 format parser for tool calls
/// DeepSeek V3 format parser for tool calls
...
...
sgl-router/src/tool_parser/parsers/glm4_moe_parser.rs
View file @
dc01313d
...
@@ -2,13 +2,14 @@ use async_trait::async_trait;
...
@@ -2,13 +2,14 @@ use async_trait::async_trait;
use
regex
::
Regex
;
use
regex
::
Regex
;
use
serde_json
::
Value
;
use
serde_json
::
Value
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::{
ParserError
,
ParserResult
},
errors
::{
ParserError
,
ParserResult
},
parsers
::
helpers
,
parsers
::
helpers
,
traits
::
ToolParser
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
},
};
};
/// GLM-4 MoE format parser for tool calls
/// GLM-4 MoE format parser for tool calls
...
...
sgl-router/src/tool_parser/parsers/gpt_oss_harmony_parser.rs
View file @
dc01313d
use
async_trait
::
async_trait
;
use
async_trait
::
async_trait
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::
ParserResult
,
errors
::
ParserResult
,
traits
::{
TokenToolParser
,
ToolParser
},
traits
::{
TokenToolParser
,
ToolParser
},
types
::{
StreamingParseResult
,
ToolCall
},
types
::{
StreamingParseResult
,
ToolCall
},
},
};
};
/// Placeholder for the Harmony-backed GPT-OSS parser.
/// Placeholder for the Harmony-backed GPT-OSS parser.
...
...
sgl-router/src/tool_parser/parsers/gpt_oss_parser.rs
View file @
dc01313d
...
@@ -2,14 +2,15 @@ use async_trait::async_trait;
...
@@ -2,14 +2,15 @@ use async_trait::async_trait;
use
regex
::
Regex
;
use
regex
::
Regex
;
use
serde_json
::
Value
;
use
serde_json
::
Value
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::{
ParserError
,
ParserResult
},
errors
::{
ParserError
,
ParserResult
},
parsers
::
helpers
,
parsers
::
helpers
,
partial_json
::
PartialJson
,
partial_json
::
PartialJson
,
traits
::
ToolParser
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
},
};
};
/// GPT-OSS format parser for tool calls
/// GPT-OSS format parser for tool calls
...
...
sgl-router/src/tool_parser/parsers/helpers.rs
View file @
dc01313d
use
crate
::
protocols
::
common
::
Tool
;
use
serde_json
::
Value
;
use
std
::
collections
::
HashMap
;
use
std
::
collections
::
HashMap
;
use
crate
::
tool_parser
::
errors
::{
ParserError
,
ParserResult
};
use
serde_json
::
Value
;
use
crate
::
tool_parser
::
types
::{
StreamingParseResult
,
ToolCallItem
};
use
crate
::{
protocols
::
common
::
Tool
,
tool_parser
::{
errors
::{
ParserError
,
ParserResult
},
types
::{
StreamingParseResult
,
ToolCallItem
},
},
};
/// Get a mapping of tool names to their indices
/// Get a mapping of tool names to their indices
pub
fn
get_tool_indices
(
tools
:
&
[
Tool
])
->
HashMap
<
String
,
usize
>
{
pub
fn
get_tool_indices
(
tools
:
&
[
Tool
])
->
HashMap
<
String
,
usize
>
{
...
...
sgl-router/src/tool_parser/parsers/json_parser.rs
View file @
dc01313d
use
async_trait
::
async_trait
;
use
async_trait
::
async_trait
;
use
serde_json
::
Value
;
use
serde_json
::
Value
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::{
ParserError
,
ParserResult
},
errors
::{
ParserError
,
ParserResult
},
parsers
::
helpers
,
parsers
::
helpers
,
partial_json
::
PartialJson
,
partial_json
::
PartialJson
,
traits
::
ToolParser
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
},
};
};
/// JSON format parser for tool calls
/// JSON format parser for tool calls
...
...
sgl-router/src/tool_parser/parsers/kimik2_parser.rs
View file @
dc01313d
...
@@ -2,13 +2,14 @@ use async_trait::async_trait;
...
@@ -2,13 +2,14 @@ use async_trait::async_trait;
use
regex
::
Regex
;
use
regex
::
Regex
;
use
serde_json
::
Value
;
use
serde_json
::
Value
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::
ParserResult
,
errors
::
ParserResult
,
parsers
::
helpers
,
parsers
::
helpers
,
traits
::
ToolParser
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
},
};
};
/// Kimi K2 format parser for tool calls
/// Kimi K2 format parser for tool calls
...
...
sgl-router/src/tool_parser/parsers/llama_parser.rs
View file @
dc01313d
use
async_trait
::
async_trait
;
use
async_trait
::
async_trait
;
use
serde_json
::
Value
;
use
serde_json
::
Value
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::{
ParserError
,
ParserResult
},
errors
::{
ParserError
,
ParserResult
},
parsers
::
helpers
,
parsers
::
helpers
,
partial_json
::
PartialJson
,
partial_json
::
PartialJson
,
traits
::
ToolParser
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
},
},
};
};
/// Llama 3.2 format parser for tool calls
/// Llama 3.2 format parser for tool calls
...
...
sgl-router/src/tool_parser/parsers/mistral_parser.rs
View file @
dc01313d
use
async_trait
::
async_trait
;
use
async_trait
::
async_trait
;
use
serde_json
::
Value
;
use
serde_json
::
Value
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::{
ParserError
,
ParserResult
},
errors
::{
ParserError
,
ParserResult
},
parsers
::
helpers
,
parsers
::
helpers
,
partial_json
::
PartialJson
,
partial_json
::
PartialJson
,
traits
::
ToolParser
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
},
},
};
};
/// Mistral format parser for tool calls
/// Mistral format parser for tool calls
...
...
sgl-router/src/tool_parser/parsers/passthrough_parser.rs
View file @
dc01313d
...
@@ -4,12 +4,17 @@
...
@@ -4,12 +4,17 @@
//! tool call parsing should be performed. It simply returns the input text
//! tool call parsing should be performed. It simply returns the input text
//! with no tool calls detected.
//! with no tool calls detected.
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::
tool_parser
::
errors
::
ParserResult
;
use
crate
::
tool_parser
::
traits
::
ToolParser
;
use
crate
::
tool_parser
::
types
::{
StreamingParseResult
,
ToolCall
,
ToolCallItem
};
use
async_trait
::
async_trait
;
use
async_trait
::
async_trait
;
use
crate
::{
protocols
::
common
::
Tool
,
tool_parser
::{
errors
::
ParserResult
,
traits
::
ToolParser
,
types
::{
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
},
};
/// Passthrough parser that returns text unchanged with no tool calls
/// Passthrough parser that returns text unchanged with no tool calls
#[derive(Default)]
#[derive(Default)]
pub
struct
PassthroughParser
;
pub
struct
PassthroughParser
;
...
...
sgl-router/src/tool_parser/parsers/pythonic_parser.rs
View file @
dc01313d
use
std
::
sync
::
OnceLock
;
/// Pythonic format parser for tool calls
/// Pythonic format parser for tool calls
///
///
/// Handles Python function call syntax within square brackets:
/// Handles Python function call syntax within square brackets:
...
@@ -10,18 +12,20 @@
...
@@ -10,18 +12,20 @@
use
async_trait
::
async_trait
;
use
async_trait
::
async_trait
;
use
num_traits
::
ToPrimitive
;
use
num_traits
::
ToPrimitive
;
use
regex
::
Regex
;
use
regex
::
Regex
;
use
rustpython_parser
::
ast
::{
Constant
,
Expr
,
Mod
,
UnaryOp
};
use
rustpython_parser
::{
use
rustpython_parser
::{
parse
,
Mode
};
ast
::{
Constant
,
Expr
,
Mod
,
UnaryOp
},
parse
,
Mode
,
};
use
serde_json
::{
Map
,
Number
,
Value
};
use
serde_json
::{
Map
,
Number
,
Value
};
use
std
::
sync
::
OnceLock
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::
tool_parser
::{
use
crate
::{
errors
::{
ParserError
,
ParserResult
},
protocols
::
common
::
Tool
,
parsers
::
helpers
,
tool_parser
::{
traits
::
ToolParser
,
errors
::{
ParserError
,
ParserResult
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
parsers
::
helpers
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
,
ToolCallItem
},
},
};
};
static
PYTHONIC_BLOCK_REGEX
:
OnceLock
<
Regex
>
=
OnceLock
::
new
();
static
PYTHONIC_BLOCK_REGEX
:
OnceLock
<
Regex
>
=
OnceLock
::
new
();
...
...
sgl-router/src/tool_parser/parsers/qwen_parser.rs
View file @
dc01313d
...
@@ -2,14 +2,15 @@ use async_trait::async_trait;
...
@@ -2,14 +2,15 @@ use async_trait::async_trait;
use
regex
::
Regex
;
use
regex
::
Regex
;
use
serde_json
::
Value
;
use
serde_json
::
Value
;
use
crate
::
protocols
::
common
::
Tool
;
use
crate
::{
protocols
::
common
::
Tool
,
use
crate
::
tool_parser
::{
tool_parser
::{
errors
::{
ParserError
,
ParserResult
},
errors
::{
ParserError
,
ParserResult
},
parsers
::
helpers
,
parsers
::
helpers
,
partial_json
::
PartialJson
,
partial_json
::
PartialJson
,
traits
::
ToolParser
,
traits
::
ToolParser
,
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
},
types
::{
FunctionCall
,
StreamingParseResult
,
ToolCall
},
},
};
};
/// Qwen format parser for tool calls
/// Qwen format parser for tool calls
...
...
Prev
1
2
3
4
5
6
7
Next
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment