pub struct TextAccumulator { /* private fields */ }Expand description
Accumulates countable text strings from structured Gemini API objects.
This is a faithful port of the Python SDK’s _TextsAccumulator class.
It traverses Content, Tool, FunctionCall, FunctionResponse, and
Schema objects, extracting all text that Google counts when computing
token counts.
§How text is extracted
- Text parts: the text string itself
- Function calls: the function name, plus all dict keys and string values from the args
- Function responses: the function name, plus all dict keys and string values from the response
- Function declarations (tools): the name, description, plus recursive schema traversal of parameters and response
- Schemas: format, description, enum values, required field names, property keys, and recursive traversal of nested schemas
Implementations§
Source§impl TextAccumulator
impl TextAccumulator
Sourcepub fn into_texts(self) -> Vec<String>
pub fn into_texts(self) -> Vec<String>
Consumes the accumulator and returns the accumulated text segments.
Sourcepub fn add_contents(&mut self, contents: &[Content])
pub fn add_contents(&mut self, contents: &[Content])
Adds all text from multiple content objects.
Sourcepub fn add_content(&mut self, content: &Content)
pub fn add_content(&mut self, content: &Content)
Adds all countable text from a single content object.
Processes each part in the content by delegating to add_part.
Sourcepub fn add_part(&mut self, part: &Part)
pub fn add_part(&mut self, part: &Part)
Adds countable text from a single content part.
Processes the part’s fields:
- Text: appends the text directly
- Function calls: delegates to
add_function_call - Function responses: delegates to
add_function_response
Sourcepub fn add_function_call(&mut self, function_call: &FunctionCall)
pub fn add_function_call(&mut self, function_call: &FunctionCall)
Adds countable text from a function call.
Extracts the function name and traverses the args dictionary, collecting all keys and string values.
Sourcepub fn add_tool(&mut self, tool: &Tool)
pub fn add_tool(&mut self, tool: &Tool)
Adds countable text from a single tool definition.
Processes each function declaration in the tool.
Sourcepub fn add_function_responses(&mut self, responses: &[FunctionResponse])
pub fn add_function_responses(&mut self, responses: &[FunctionResponse])
Adds countable text from multiple function responses.
Sourcepub fn add_function_response(&mut self, function_response: &FunctionResponse)
pub fn add_function_response(&mut self, function_response: &FunctionResponse)
Adds countable text from a function response.
Extracts the function name and traverses the response dictionary, collecting all keys and string values.
Sourcepub fn add_schema(&mut self, schema: &Schema)
pub fn add_schema(&mut self, schema: &Schema)
Adds countable text from a schema definition.
Extracts format, description, enum values, required field names, property keys, and recursively processes nested schemas (items, properties, examples).