stygian-plugin 0.14.1

Visual data extraction fallback subsystem with CSS/XPath selectors, idempotent request handling, and composable transformation pipelines.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
//! Extraction template, request, and result types

use crate::domain::idempotency::IdempotencyKey;
use crate::domain::selector::Selector;
use crate::domain::transformation::Transformation;
use crate::reliability::ReliabilityScore;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;

/// A named region within a template to extract data from
///
/// Each region represents a distinct zone on the page with its own
/// selectors and transformations.
///
/// # Example
///
/// ```
/// use stygian_plugin::domain::Region;
/// use stygian_plugin::domain::Selector;
///
/// let region = Region {
///     name: "product-title".to_string(),
///     selector: Selector::css(".product-name".to_string()),
///     schema: serde_json::json!({"type": "string"}),
///     transformations: vec![],
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Region {
    /// Region name (e.g., "product-title", "price", "rating")
    pub name: String,

    /// Primary selector (`CSS` or `XPath`) to locate the element
    pub selector: Selector,

    /// JSON schema describing the expected output shape
    pub schema: Value,

    /// Ordered transformations to apply to extracted values
    pub transformations: Vec<Transformation>,
}

impl Region {
    /// Create a new region with minimal configuration
    pub fn new(name: impl Into<String>, selector: Selector, schema: Value) -> Self {
        Self {
            name: name.into(),
            selector,
            schema,
            transformations: vec![],
        }
    }

    /// Add a transformation to the pipeline
    #[must_use]
    pub fn with_transformation(mut self, transformation: Transformation) -> Self {
        self.transformations.push(transformation);
        self
    }

    /// Validate region configuration
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::PluginError::TemplateValidationError`] when
    /// the region name is empty or the JSON schema is not an object. Returns
    /// [`crate::error::PluginError::SelectorError`] when the region's
    /// selector fails its own `validate()` call.
    pub fn validate(&self) -> crate::Result<()> {
        if self.name.is_empty() {
            return Err(crate::error::PluginError::TemplateValidationError(
                "region name cannot be empty".to_string(),
            ));
        }
        if !self.schema.is_object() {
            return Err(crate::error::PluginError::TemplateValidationError(format!(
                "region schema must be a JSON object, got {}",
                self.schema.get("type").unwrap_or(&Value::Null)
            )));
        }
        // Validate the selector syntax
        self.selector.validate()?;
        Ok(())
    }
}

/// A reusable extraction template defining how to extract data from a page
///
/// Templates combine multiple regions, each with selectors and transformations.
/// A template is the core unit of plugin configuration and is persisted for reuse.
///
/// # Example
///
/// ```
/// use stygian_plugin::domain::{ExtractionTemplate, Region, Selector};
/// use serde_json::json;
///
/// let template = ExtractionTemplate {
///     id: uuid::Uuid::new_v4(),
///     name: "Product Listing".to_string(),
///     description: Some("Extract product cards from a listing page".to_string()),
///     regions: vec![],
///     metadata: Default::default(),
/// };
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractionTemplate {
    /// Unique identifier for this template
    pub id: uuid::Uuid,

    /// User-friendly template name
    pub name: String,

    /// Optional description
    pub description: Option<String>,

    /// Regions (named extraction zones) in this template
    pub regions: Vec<Region>,

    /// Metadata (timestamps, version, etc.)
    pub metadata: TemplateMetadata,
}

/// Metadata about a template
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateMetadata {
    /// When template was created
    pub created_at: DateTime<Utc>,

    /// When template was last modified
    pub updated_at: DateTime<Utc>,

    /// When template was last used
    pub last_used_at: Option<DateTime<Utc>>,

    /// Number of times this template has been used
    pub usage_count: u64,

    /// Template version (for migration purposes)
    pub version: u32,

    /// Optional user-defined tags
    pub tags: Vec<String>,
}

impl Default for TemplateMetadata {
    fn default() -> Self {
        let now = Utc::now();
        Self {
            created_at: now,
            updated_at: now,
            last_used_at: None,
            usage_count: 0,
            version: 1,
            tags: vec![],
        }
    }
}

impl ExtractionTemplate {
    /// Create a new template with defaults
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            id: uuid::Uuid::new_v4(),
            name: name.into(),
            description: None,
            regions: vec![],
            metadata: TemplateMetadata::default(),
        }
    }

    /// Add a region to this template
    #[must_use]
    pub fn with_region(mut self, region: Region) -> Self {
        self.regions.push(region);
        self
    }

    /// Set template description
    #[must_use]
    pub fn with_description(mut self, desc: impl Into<String>) -> Self {
        self.description = Some(desc.into());
        self
    }

    /// Set template tags
    #[must_use]
    pub fn with_tags(mut self, tags: Vec<String>) -> Self {
        self.metadata.tags = tags;
        self
    }

    /// Validate the entire template
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::PluginError::TemplateValidationError`] when
    /// the template name is empty. Propagates any error returned by the
    /// per-region `validate()` call (empty name, non-object schema, or
    /// invalid selector).
    pub fn validate(&self) -> crate::Result<()> {
        if self.name.is_empty() {
            return Err(crate::error::PluginError::TemplateValidationError(
                "template name cannot be empty".to_string(),
            ));
        }
        for region in &self.regions {
            region.validate()?;
        }
        Ok(())
    }

    /// Update usage statistics
    pub fn mark_used(&mut self) {
        self.metadata.usage_count += 1;
        self.metadata.last_used_at = Some(Utc::now());
        self.metadata.updated_at = Utc::now();
    }
}

/// Request to extract data from a page using a template
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractionRequest {
    /// Template to use for extraction
    pub template: ExtractionTemplate,

    /// Target URL (for context/logging)
    pub url: String,

    /// HTML content of the page to extract from
    pub html: String,

    /// Idempotency key for safe retries
    pub idempotency_key: IdempotencyKey,

    /// Timeout in milliseconds
    pub timeout_ms: u64,

    /// Optional extraction context (arbitrary JSON)
    pub context: Option<Value>,
}

impl ExtractionRequest {
    /// Create a new extraction request
    pub fn new(
        template: ExtractionTemplate,
        url: impl Into<String>,
        html: impl Into<String>,
    ) -> Self {
        Self {
            template,
            url: url.into(),
            html: html.into(),
            idempotency_key: IdempotencyKey::new(),
            timeout_ms: 30_000,
            context: None,
        }
    }

    /// Set idempotency key
    #[must_use]
    pub const fn with_idempotency_key(mut self, key: IdempotencyKey) -> Self {
        self.idempotency_key = key;
        self
    }

    /// Set timeout
    #[must_use]
    pub const fn with_timeout(mut self, ms: u64) -> Self {
        self.timeout_ms = ms;
        self
    }

    /// Set context
    #[must_use]
    pub fn with_context(mut self, context: Value) -> Self {
        self.context = Some(context);
        self
    }

    /// Validate the request
    ///
    /// # Errors
    ///
    /// Returns [`crate::error::PluginError::TemplateValidationError`] when
    /// the embedded template is invalid. Returns
    /// [`crate::error::PluginError::ExtractionError`] when the request URL
    /// or HTML payload is empty.
    pub fn validate(&self) -> crate::Result<()> {
        self.template.validate()?;
        if self.url.is_empty() {
            return Err(crate::error::PluginError::ExtractionError(
                "URL cannot be empty".to_string(),
            ));
        }
        if self.html.is_empty() {
            return Err(crate::error::PluginError::ExtractionError(
                "HTML cannot be empty".to_string(),
            ));
        }
        Ok(())
    }
}

/// Result of a successful extraction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractionResult {
    /// Extracted data keyed by region name
    pub data: HashMap<String, Value>,

    /// Metadata about the extraction
    pub metadata: ExtractionMetadata,
}

/// Metadata about an extraction result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExtractionMetadata {
    /// Idempotency key used
    pub idempotency_key: IdempotencyKey,

    /// When extraction was completed
    pub completed_at: DateTime<Utc>,

    /// Elapsed time in milliseconds
    pub elapsed_ms: u64,

    /// Success rate for selectors (0-100)
    pub selector_success_rate: f32,

    /// Per-region extraction status
    pub region_status: HashMap<String, RegionStatus>,

    /// Optional error details
    pub errors: Vec<String>,

    /// Optional reliability score for the extraction output (T87).
    ///
    /// This field is **additive** — older consumers that don't know about
    /// reliability scoring see `None` (or the field omitted entirely when
    /// serialized with `skip_serializing_if = "Option::is_none"`).
    /// Default-on per the T87 spec; no feature gate is required.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub reliability: Option<ReliabilityScore>,
}

/// Status of extraction for a single region
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RegionStatus {
    /// Whether extraction succeeded
    pub success: bool,

    /// Number of elements matched
    pub matched_count: usize,

    /// Error message if failed
    pub error: Option<String>,
}

impl ExtractionResult {
    /// Create a new extraction result
    #[must_use]
    pub fn new(idempotency_key: IdempotencyKey) -> Self {
        Self {
            data: HashMap::new(),
            metadata: ExtractionMetadata {
                idempotency_key,
                completed_at: Utc::now(),
                elapsed_ms: 0,
                selector_success_rate: 0.0,
                region_status: HashMap::new(),
                errors: vec![],
                reliability: None,
            },
        }
    }

    /// Add extracted data for a region
    #[must_use]
    pub fn with_region_data(mut self, region_name: impl Into<String>, data: Value) -> Self {
        self.data.insert(region_name.into(), data);
        self
    }

    /// Add an error
    #[must_use]
    pub fn with_error(mut self, error: impl Into<String>) -> Self {
        self.metadata.errors.push(error.into());
        self
    }

    /// Update elapsed time
    #[must_use]
    pub const fn set_elapsed_ms(mut self, ms: u64) -> Self {
        self.metadata.elapsed_ms = ms;
        self
    }

    /// Calculate and set selector success rate
    #[expect(
        clippy::cast_precision_loss,
        reason = "region counts are small enough to be safe as f32"
    )]
    pub fn calculate_success_rate(&mut self) {
        if self.metadata.region_status.is_empty() {
            self.metadata.selector_success_rate = 100.0;
            return;
        }
        let successful = self
            .metadata
            .region_status
            .values()
            .filter(|status| status.success)
            .count();
        self.metadata.selector_success_rate =
            (successful as f32 / self.metadata.region_status.len() as f32) * 100.0;
    }

    /// Check if extraction was fully successful
    #[must_use]
    pub fn is_fully_successful(&self) -> bool {
        self.metadata.selector_success_rate >= 100.0 && self.metadata.errors.is_empty()
    }
}