Skip to main content

adk_memory/
text.rs

1//! Shared text extraction utilities for memory backends.
2
3use adk_core::Part;
4use std::collections::HashSet;
5
6/// Extract all text parts from a [`Content`](adk_core::Content) into a single string.
7///
8/// Parts are joined with a single space. Non-text parts (images, function calls,
9/// etc.) are silently skipped.
10pub fn extract_text(content: &adk_core::Content) -> String {
11    content
12        .parts
13        .iter()
14        .filter_map(|part| match part {
15            Part::Text { text } => Some(text.as_str()),
16            _ => None,
17        })
18        .collect::<Vec<_>>()
19        .join(" ")
20}
21
22/// Tokenize text into a set of lowercase words for keyword matching.
23pub fn extract_words(text: &str) -> HashSet<String> {
24    text.split_whitespace().filter(|s| !s.is_empty()).map(|s| s.to_lowercase()).collect()
25}
26
27/// Extract and tokenize all text from a [`Content`](adk_core::Content) into word set.
28pub fn extract_words_from_content(content: &adk_core::Content) -> HashSet<String> {
29    let mut words = HashSet::new();
30    for part in &content.parts {
31        if let Part::Text { text } = part {
32            words.extend(extract_words(text));
33        }
34    }
35    words
36}