traits.rs 2.29 KB
Newer Older
1
use crate::protocols::spec::Tool;
2
3
use crate::tool_parser::{
    errors::ToolParserResult,
4
    types::{StreamingParseResult, ToolCall},
5
6
7
8
9
10
11
};
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
12
13
    /// Returns (remaining_normal_text, tool_calls) tuple
    async fn parse_complete(&self, output: &str) -> ToolParserResult<(String, Vec<ToolCall>)>;
14
15

    /// Parse tool calls from model output (streaming)
16
17
18
19
20
    /// Parsers now maintain internal state, so self is mutable
    ///
    /// # Arguments
    /// * `chunk` - New text chunk from model output
    /// * `tools` - List of available tools for validation
21
    async fn parse_incremental(
22
        &mut self,
23
        chunk: &str,
24
25
        tools: &[Tool],
    ) -> ToolParserResult<StreamingParseResult>;
26
27

    /// Check if text contains tool calls in this parser's format
28
    fn has_tool_markers(&self, text: &str) -> bool;
29
30
31
32
33
34

    /// Optionally expose a token-aware parser implementation.
    /// Default returns `None`, meaning the parser only supports text input.
    fn as_token_parser(&self) -> Option<&dyn TokenToolParser> {
        None
    }
35
36
37
38
39
40

    /// Get unstreamed tool call arguments
    /// Returns tool call items for arguments that have been parsed but not yet streamed
    fn get_unstreamed_tool_args(&self) -> Option<Vec<crate::tool_parser::types::ToolCallItem>> {
        None
    }
41
42
43
44
45
46
47
48
49
50
51
52
53
}

/// 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;
}
54
55
56
57
58
59
60
61
62
63

#[async_trait]
pub trait TokenToolParser: ToolParser {
    /// Parse complete tool calls when provided with raw token IDs.
    async fn parse_complete_tokens(
        &self,
        tokens: &[u32],
    ) -> ToolParserResult<(String, Vec<ToolCall>)>;

    /// Streaming parser entrypoint for token chunks.
64
    /// Parsers maintain internal state, so self is mutable
65
    async fn parse_incremental_tokens(
66
        &mut self,
67
        tokens: &[u32],
68
69
        tools: &[Tool],
    ) -> ToolParserResult<StreamingParseResult>;
70
}