Skip to main content

autoagents_core/
document.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3
4use crate::embeddings::{Embed, EmbedError, TextEmbedder};
5
6/// Represents a piece of content along with optional metadata.
7/// Mirrors the LangChain document abstraction (`page_content` + `metadata`).
8#[derive(Debug, Clone, Serialize, Deserialize, Default)]
9pub struct Document {
10    pub page_content: String,
11    #[serde(default)]
12    pub metadata: Value,
13}
14
15impl Document {
16    pub fn new(page_content: impl Into<String>) -> Self {
17        Self {
18            page_content: page_content.into(),
19            metadata: Value::Object(Default::default()),
20        }
21    }
22
23    pub fn with_metadata(page_content: impl Into<String>, metadata: Value) -> Self {
24        Self {
25            page_content: page_content.into(),
26            metadata,
27        }
28    }
29}
30
31impl Embed for Document {
32    fn embed(&self, embedder: &mut TextEmbedder) -> Result<(), EmbedError> {
33        embedder.embed(self.page_content.clone());
34        Ok(())
35    }
36}