tool_parser/traits.rs
1use async_trait::async_trait;
2use openai_protocol::common::Tool;
3
4use crate::{
5 errors::ParserResult,
6 types::{StreamingParseResult, ToolCall},
7};
8
9/// Core trait for all tool parsers
10#[async_trait]
11pub trait ToolParser: Send + Sync {
12 /// Parse complete tool calls from final output
13 /// Returns (remaining_normal_text, tool_calls) tuple
14 async fn parse_complete(&self, output: &str) -> ParserResult<(String, Vec<ToolCall>)>;
15
16 /// Parse tool calls from model output (streaming)
17 /// Parsers now maintain internal state, so self is mutable
18 ///
19 /// # Arguments
20 /// * `chunk` - New text chunk from model output
21 /// * `tools` - List of available tools for validation
22 async fn parse_incremental(
23 &mut self,
24 chunk: &str,
25 tools: &[Tool],
26 ) -> ParserResult<StreamingParseResult>;
27
28 /// Check if text contains tool calls in this parser's format
29 fn has_tool_markers(&self, text: &str) -> bool;
30
31 /// Optionally expose a token-aware parser implementation.
32 /// Default returns `None`, meaning the parser only supports text input.
33 fn as_token_parser(&self) -> Option<&dyn TokenToolParser> {
34 None
35 }
36
37 /// Get unstreamed tool call arguments
38 /// Returns tool call items for arguments that have been parsed but not yet streamed
39 fn get_unstreamed_tool_args(&self) -> Option<Vec<crate::types::ToolCallItem>> {
40 None
41 }
42
43 /// Reset the parser state for reuse across requests.
44 /// This should clear all buffers and reset state to initial values.
45 fn reset(&mut self) {
46 // Default no-op implementation
47 }
48}
49
50/// Trait for partial JSON parsing
51pub trait PartialJsonParser: Send + Sync {
52 /// Parse potentially incomplete JSON
53 fn parse(&self, input: &str) -> ParserResult<(serde_json::Value, usize)>;
54
55 /// Check if JSON is complete
56 fn is_complete(&self, input: &str) -> bool;
57
58 /// Get the maximum parsing depth
59 fn max_depth(&self) -> usize;
60}
61
62#[async_trait]
63pub trait TokenToolParser: ToolParser {
64 /// Parse complete tool calls when provided with raw token IDs.
65 async fn parse_complete_tokens(&self, tokens: &[u32]) -> ParserResult<(String, Vec<ToolCall>)>;
66
67 /// Streaming parser entrypoint for token chunks.
68 /// Parsers maintain internal state, so self is mutable
69 async fn parse_incremental_tokens(
70 &mut self,
71 tokens: &[u32],
72 tools: &[Tool],
73 ) -> ParserResult<StreamingParseResult>;
74}