stygian-graph 0.9.2

High-performance graph-based web scraping engine with AI extraction, multi-modal support, and anti-bot capabilities
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
//! LLM Extraction Service — orchestrator that uses AIProvider to extract structured data
//!
//! Implements `ScrapingService` by delegating to one or more `AIProvider`s.
//! Supports provider fallback: tries providers in order until one succeeds.
//!
//! # Architecture
//!
//! ```text
//! ScrapingService  ←  LlmExtractionService  →  AIProvider (Claude, GPT, Gemini, …)
//!       ↑                     ↓
//!  ServiceInput           FallbackChain
//!  { url, params }     [primary, secondary, …]
//! ```
//!
//! The `params` field of `ServiceInput` must contain:
//! - `schema`: JSON schema object defining the expected output shape
//! - `content` (optional): If present, used as-is. Otherwise `data` from a prior
//!   pipeline stage should be passed via `url`.
//!
//! # Example
//!
//! ```no_run
//! use stygian_graph::application::extraction::{LlmExtractionService, ExtractionConfig};
//! use stygian_graph::ports::{ScrapingService, ServiceInput};
//! use serde_json::json;
//!
//! # tokio::runtime::Runtime::new().unwrap().block_on(async {
//! // Provider built separately — inject via Arc<dyn AIProvider>
//! // let service = LlmExtractionService::new(providers, ExtractionConfig::default());
//! // let output = service.execute(input).await.unwrap();
//! # });
//! ```

use std::sync::Arc;

use async_trait::async_trait;
use serde_json::{Value, json};
use tracing::{debug, info, warn};

use crate::domain::error::{ProviderError, Result, StygianError};
use crate::ports::{AIProvider, ScrapingService, ServiceInput, ServiceOutput};

/// Configuration for the LLM extraction service
#[derive(Debug, Clone)]
pub struct ExtractionConfig {
    /// Maximum content length sent to providers (characters).
    /// Content is truncated at this limit to avoid token overflow.
    pub max_content_chars: usize,
    /// Whether to validate the provider output against the schema.
    /// Currently performs a structural check (is the output a JSON object?).
    pub validate_output: bool,
}

impl Default for ExtractionConfig {
    fn default() -> Self {
        Self {
            max_content_chars: 64_000,
            validate_output: true,
        }
    }
}

/// LLM-based structured data extraction service
///
/// Wraps one or more `AIProvider` instances and implements `ScrapingService`.
/// On each `execute()` call the service:
///
/// 1. Reads `schema` and optionally `content` from `ServiceInput.params`.
/// 2. Iterates through the provider list until one returns `Ok`.
/// 3. Returns extracted data in `ServiceOutput.data` (as JSON string).
/// 4. Metadata includes which provider succeeded and elapsed time.
///
/// # Provider Fallback
///
/// Providers are tried **in the order they were added**. The first success
/// short-circuits the chain. Errors from skipped providers are logged as
/// warnings, not propagated.
pub struct LlmExtractionService {
    /// Ordered fallback chain of AI providers
    providers: Vec<Arc<dyn AIProvider>>,
    config: ExtractionConfig,
}

impl LlmExtractionService {
    /// Create a new extraction service with an ordered fallback chain
    ///
    /// # Example
    ///
    /// ```no_run
    /// use stygian_graph::application::extraction::{LlmExtractionService, ExtractionConfig};
    /// use stygian_graph::adapters::ai::ollama::OllamaProvider;
    /// use std::sync::Arc;
    ///
    /// let providers: Vec<Arc<dyn stygian_graph::ports::AIProvider>> = vec![
    ///     Arc::new(OllamaProvider::new()),
    /// ];
    /// let service = LlmExtractionService::new(providers, ExtractionConfig::default());
    /// ```
    pub fn new(providers: Vec<Arc<dyn AIProvider>>, config: ExtractionConfig) -> Self {
        Self { providers, config }
    }

    /// Resolve the content to extract from.
    ///
    /// Priority:
    /// 1. `params["content"]` if present
    /// 2. `input.url` as fallback (useful when this node receives raw HTML from prior stage)
    fn resolve_content(input: &ServiceInput) -> &str {
        input
            .params
            .get("content")
            .and_then(Value::as_str)
            .unwrap_or(&input.url)
    }

    /// Truncate content to the configured character limit
    fn truncate_content<'a>(&self, content: &'a str) -> &'a str {
        if content.len() <= self.config.max_content_chars {
            content
        } else {
            warn!(
                limit = self.config.max_content_chars,
                actual = content.len(),
                "Content truncated for LLM extraction"
            );
            &content[..self.config.max_content_chars]
        }
    }

    /// Extract the `schema` from params, returning an error if missing
    fn resolve_schema(input: &ServiceInput) -> Result<Value> {
        input.params.get("schema").cloned().ok_or_else(|| {
            StygianError::Provider(ProviderError::ApiError(
                "LlmExtractionService requires 'schema' in ServiceInput.params".to_string(),
            ))
        })
    }

    /// Validate that extracted output is a JSON object (basic schema check)
    fn validate_output(output: &Value) -> Result<()> {
        if output.is_object() || output.is_array() {
            Ok(())
        } else {
            Err(StygianError::Provider(ProviderError::ApiError(format!(
                "Provider returned non-object output: {output}"
            ))))
        }
    }
}

#[async_trait]
impl ScrapingService for LlmExtractionService {
    /// Execute structured extraction via the provider fallback chain
    ///
    /// # Example
    ///
    /// ```no_run
    /// use stygian_graph::application::extraction::{LlmExtractionService, ExtractionConfig};
    /// use stygian_graph::adapters::ai::ollama::OllamaProvider;
    /// use stygian_graph::ports::{ScrapingService, ServiceInput};
    /// use serde_json::json;
    /// use std::sync::Arc;
    ///
    /// # tokio::runtime::Runtime::new().unwrap().block_on(async {
    /// let providers: Vec<Arc<dyn stygian_graph::ports::AIProvider>> = vec![
    ///     Arc::new(OllamaProvider::new()),
    /// ];
    /// let service = LlmExtractionService::new(providers, ExtractionConfig::default());
    /// let input = ServiceInput {
    ///     url: "<h1>Hello World</h1>".to_string(),
    ///     params: json!({
    ///         "schema": {"type": "object", "properties": {"heading": {"type": "string"}}},
    ///     }),
    /// };
    /// // let output = service.execute(input).await.unwrap();
    /// # });
    /// ```
    async fn execute(&self, input: ServiceInput) -> Result<ServiceOutput> {
        if self.providers.is_empty() {
            return Err(StygianError::Provider(ProviderError::ApiError(
                "No AI providers configured in LlmExtractionService".to_string(),
            )));
        }

        let schema = Self::resolve_schema(&input)?;
        let raw_content = Self::resolve_content(&input);
        let content = self.truncate_content(raw_content).to_string();

        let start = std::time::Instant::now();
        let mut last_error: Option<StygianError> = None;

        for provider in &self.providers {
            debug!(provider = provider.name(), "Attempting LLM extraction");

            match provider.extract(content.clone(), schema.clone()).await {
                Ok(extracted) => {
                    if self.config.validate_output
                        && let Err(e) = Self::validate_output(&extracted)
                    {
                        warn!(
                            provider = provider.name(),
                            error = %e,
                            "Provider returned invalid output, trying next"
                        );
                        last_error = Some(e);
                        continue;
                    }

                    let elapsed = start.elapsed();
                    info!(
                        provider = provider.name(),
                        elapsed_ms = elapsed.as_millis(),
                        "LLM extraction succeeded"
                    );

                    return Ok(ServiceOutput {
                        data: extracted.to_string(),
                        metadata: json!({
                            "provider": provider.name(),
                            "elapsed_ms": elapsed.as_millis(),
                            "content_chars": content.len(),
                        }),
                    });
                }
                Err(e) => {
                    warn!(
                        provider = provider.name(),
                        error = %e,
                        "Provider failed, trying next in chain"
                    );
                    last_error = Some(e);
                }
            }
        }

        // All providers failed
        Err(last_error.unwrap_or_else(|| {
            StygianError::Provider(ProviderError::ApiError(
                "All AI providers in fallback chain failed".to_string(),
            ))
        }))
    }

    fn name(&self) -> &'static str {
        "llm-extraction"
    }
}

#[cfg(test)]
#[allow(
    clippy::unwrap_used,
    clippy::indexing_slicing,
    clippy::needless_pass_by_value
)]
mod tests {
    use super::*;
    use crate::ports::ProviderCapabilities;
    use futures::stream::{self, BoxStream};
    use serde_json::json;

    // --- Mock AIProvider for tests ---

    struct AlwaysSucceed {
        response: Value,
    }

    #[async_trait]
    impl AIProvider for AlwaysSucceed {
        async fn extract(&self, _content: String, _schema: Value) -> Result<Value> {
            Ok(self.response.clone())
        }

        async fn stream_extract(
            &self,
            _content: String,
            _schema: Value,
        ) -> Result<BoxStream<'static, Result<Value>>> {
            Ok(Box::pin(stream::once(async { Ok(json!({})) })))
        }

        fn capabilities(&self) -> ProviderCapabilities {
            ProviderCapabilities::default()
        }

        fn name(&self) -> &'static str {
            "mock-succeed"
        }
    }

    struct AlwaysFail;

    #[async_trait]
    impl AIProvider for AlwaysFail {
        async fn extract(&self, _content: String, _schema: Value) -> Result<Value> {
            Err(StygianError::Provider(ProviderError::ApiError(
                "mock failure".to_string(),
            )))
        }

        async fn stream_extract(
            &self,
            _content: String,
            _schema: Value,
        ) -> Result<BoxStream<'static, Result<Value>>> {
            Err(StygianError::Provider(ProviderError::ApiError(
                "mock failure".to_string(),
            )))
        }

        fn capabilities(&self) -> ProviderCapabilities {
            ProviderCapabilities::default()
        }

        fn name(&self) -> &'static str {
            "mock-fail"
        }
    }

    fn make_input(schema: Value) -> ServiceInput {
        ServiceInput {
            url: "<h1>Hello</h1>".to_string(),
            params: json!({ "schema": schema }),
        }
    }

    #[tokio::test]
    async fn test_service_name() {
        let svc = LlmExtractionService::new(vec![], ExtractionConfig::default());
        assert_eq!(svc.name(), "llm-extraction");
    }

    #[tokio::test]
    async fn test_no_providers_returns_error() {
        let svc = LlmExtractionService::new(vec![], ExtractionConfig::default());
        let err = svc.execute(make_input(json!({}))).await.unwrap_err();
        assert!(err.to_string().contains("No AI providers"));
    }

    #[tokio::test]
    async fn test_missing_schema_returns_error() {
        let providers: Vec<Arc<dyn AIProvider>> = vec![Arc::new(AlwaysSucceed {
            response: json!({}),
        })];
        let svc = LlmExtractionService::new(providers, ExtractionConfig::default());
        let input = ServiceInput {
            url: "some content".to_string(),
            params: json!({}), // no schema key
        };
        let err = svc.execute(input).await.unwrap_err();
        assert!(err.to_string().contains("schema"));
    }

    #[tokio::test]
    async fn test_single_succeeding_provider() {
        let providers: Vec<Arc<dyn AIProvider>> = vec![Arc::new(AlwaysSucceed {
            response: json!({"title": "Hello"}),
        })];
        let svc = LlmExtractionService::new(providers, ExtractionConfig::default());
        let output = svc.execute(make_input(json!({}))).await.unwrap();
        assert_eq!(
            output.metadata["provider"].as_str().unwrap(),
            "mock-succeed"
        );
        let data: Value = serde_json::from_str(&output.data).unwrap();
        assert_eq!(data["title"].as_str().unwrap(), "Hello");
    }

    #[tokio::test]
    async fn test_fallback_to_second_provider() {
        let providers: Vec<Arc<dyn AIProvider>> = vec![
            Arc::new(AlwaysFail),
            Arc::new(AlwaysSucceed {
                response: json!({"score": 42}),
            }),
        ];
        let svc = LlmExtractionService::new(providers, ExtractionConfig::default());
        let output = svc.execute(make_input(json!({}))).await.unwrap();
        assert_eq!(
            output.metadata["provider"].as_str().unwrap(),
            "mock-succeed"
        );
    }

    #[tokio::test]
    async fn test_all_providers_fail() {
        let providers: Vec<Arc<dyn AIProvider>> = vec![Arc::new(AlwaysFail), Arc::new(AlwaysFail)];
        let svc = LlmExtractionService::new(providers, ExtractionConfig::default());
        let err = svc.execute(make_input(json!({}))).await.unwrap_err();
        assert!(err.to_string().contains("mock failure"));
    }

    #[tokio::test]
    async fn test_content_from_params_overrides_url() {
        let providers: Vec<Arc<dyn AIProvider>> = vec![Arc::new(AlwaysSucceed {
            response: json!({"ok": true}),
        })];
        let svc = LlmExtractionService::new(providers, ExtractionConfig::default());
        let input = ServiceInput {
            url: "should-not-be-used".to_string(),
            params: json!({
                "schema": {"type": "object"},
                "content": "actual content here"
            }),
        };
        let output = svc.execute(input).await.unwrap();
        // Metadata should reflect char count of "actual content here" (19 chars)
        assert_eq!(output.metadata["content_chars"].as_u64().unwrap(), 19);
    }

    #[test]
    fn test_truncate_content_short() {
        let svc = LlmExtractionService::new(vec![], ExtractionConfig::default());
        let s = "hello";
        assert_eq!(svc.truncate_content(s), s);
    }

    #[test]
    fn test_truncate_content_long() {
        let svc = LlmExtractionService::new(
            vec![],
            ExtractionConfig {
                max_content_chars: 5,
                ..Default::default()
            },
        );
        assert_eq!(svc.truncate_content("hello world"), "hello");
    }

    #[test]
    fn test_validate_output_object_ok() {
        assert!(LlmExtractionService::validate_output(&json!({"k": "v"})).is_ok());
    }

    #[test]
    fn test_validate_output_array_ok() {
        assert!(LlmExtractionService::validate_output(&json!([1, 2, 3])).is_ok());
    }

    #[test]
    fn test_validate_output_scalar_err() {
        assert!(LlmExtractionService::validate_output(&json!("just a string")).is_err());
    }
}