Skip to main content

weavatrix_memory/extraction/
provider.rs

1use super::{ExtractionInput, ExtractionOutput};
2use crate::error::MemoryError;
3use std::{error::Error, fmt};
4
5#[derive(Debug, Clone, PartialEq, Eq)]
6pub struct ExtractionError {
7    pub provider: String,
8    pub message: String,
9}
10
11impl ExtractionError {
12    #[must_use]
13    pub fn new(provider: impl Into<String>, message: impl Into<String>) -> Self {
14        Self {
15            provider: provider.into(),
16            message: message.into(),
17        }
18    }
19}
20
21impl fmt::Display for ExtractionError {
22    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
23        write!(
24            formatter,
25            "extraction provider {} failed: {}",
26            self.provider, self.message
27        )
28    }
29}
30
31impl Error for ExtractionError {}
32
33impl From<ExtractionError> for MemoryError {
34    fn from(value: ExtractionError) -> Self {
35        Self::Extraction {
36            provider: value.provider,
37            message: value.message,
38        }
39    }
40}
41
42/// Converts source material into typed mentions and relations.
43///
44/// Providers may wrap AST parsers, issue trackers, language models, or future
45/// Weavatrix search packages. The memory core owns validation, entity linking,
46/// provenance, and event creation.
47pub trait ExtractionProvider: Sync {
48    fn name(&self) -> &str;
49
50    /// Extracts candidates without mutating memory.
51    ///
52    /// # Errors
53    ///
54    /// Returns a provider-labelled failure. The engine rejects failures whose
55    /// provider identity does not match [`Self::name`].
56    fn extract(&self, input: &ExtractionInput) -> Result<ExtractionOutput, ExtractionError>;
57}