Skip to main content

protocol/
pi_message.rs

1//! Reading Pi message JSON (`{role, content, ...}`), whose content is a string or a list of blocks.
2
3use serde_json::Value;
4
5/// The text blocks of a message joined (a string content as it is).
6pub fn text(message: &Value) -> String {
7    content_text(&message["content"], "")
8}
9
10/// A content's text blocks joined by `separator` (a string content as it is).
11pub fn content_text(content: &Value, separator: &str) -> String {
12    match content {
13        Value::String(text) => text.clone(),
14        Value::Array(blocks) => blocks
15            .iter()
16            .filter(|b| b["type"] == "text")
17            .map(|b| b["text"].as_str().unwrap_or_default())
18            .collect::<Vec<_>>()
19            .join(separator),
20        _ => String::new(),
21    }
22}