url-preview 0.6.0

High-performance URL preview generator for messaging and social media applications
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
//! LLM provider implementations
//!
//! This module contains implementations for various LLM providers including
//! OpenAI, Anthropic, and others.

use crate::llm_extractor::{LLMProvider, LLMExtractorConfig};
use crate::PreviewError;
use async_trait::async_trait;
use serde_json::Value;
use std::collections::HashMap;

/// Mock LLM provider for testing
pub struct MockProvider {
    name: String,
    responses: HashMap<String, Value>,
}

impl MockProvider {
    pub fn new() -> Self {
        Self {
            name: "mock".to_string(),
            responses: HashMap::new(),
        }
    }
    
    pub fn with_response(mut self, key: String, response: Value) -> Self {
        self.responses.insert(key, response);
        self
    }
}

#[async_trait]
impl LLMProvider for MockProvider {
    fn name(&self) -> &str {
        &self.name
    }
    
    async fn generate(
        &self,
        _prompt: String,
        schema: Value,
        _config: &LLMExtractorConfig,
    ) -> Result<Value, PreviewError> {
        // For mock provider, return a simple response based on schema
        if let Some(properties) = schema.get("properties") {
            let mut result = serde_json::Map::new();
            
            if let Some(props) = properties.as_object() {
                for (key, prop_schema) in props {
                    let value = match prop_schema.get("type").and_then(|t| t.as_str()) {
                        Some("string") => Value::String("Mock value".to_string()),
                        Some("number") => Value::Number(serde_json::Number::from(42)),
                        Some("boolean") => Value::Bool(true),
                        Some("array") => Value::Array(vec![]),
                        Some("object") => Value::Object(serde_json::Map::new()),
                        _ => Value::Null,
                    };
                    result.insert(key.clone(), value);
                }
            }
            
            Ok(Value::Object(result))
        } else {
            Ok(Value::Object(serde_json::Map::new()))
        }
    }
}

#[cfg(feature = "async-openai")]
pub mod openai {
    use super::*;
    use async_openai::{Client, config::OpenAIConfig};
    use async_openai::types::{
        ChatCompletionRequestMessage, ChatCompletionRequestSystemMessageArgs,
        ChatCompletionRequestUserMessageArgs, CreateChatCompletionRequestArgs,
        ChatCompletionToolArgs, ChatCompletionToolType,
        FunctionObjectArgs,
    };
    
    /// OpenAI provider implementation
    pub struct OpenAIProvider {
        client: Client<OpenAIConfig>,
        model: String,
    }
    
    impl OpenAIProvider {
        pub fn new(api_key: String) -> Self {
            let config = OpenAIConfig::new().with_api_key(api_key);
            Self {
                client: Client::with_config(config),
                model: "gpt-4-turbo-preview".to_string(),
            }
        }
        
        pub fn with_model(mut self, model: String) -> Self {
            self.model = model;
            self
        }
        
        /// Create from custom client configuration
        pub fn from_config(config: OpenAIConfig, model: String) -> Self {
            Self {
                client: Client::with_config(config),
                model,
            }
        }
        
        /// Extract JSON from text content
        fn extract_json_from_text(text: &str) -> Option<String> {
            // Find the first '{' and last '}'
            let start = text.find('{')?;
            let end = text.rfind('}')?;
            
            if start <= end {
                let potential_json = &text[start..=end];
                // Basic validation - check if it looks like valid JSON
                if potential_json.contains('"') || potential_json.contains(':') {
                    return Some(potential_json.to_string());
                }
            }
            
            None
        }
    }
    
    #[async_trait]
    impl LLMProvider for OpenAIProvider {
        fn name(&self) -> &str {
            "openai"
        }
        
        async fn generate(
            &self,
            prompt: String,
            schema: Value,
            _config: &LLMExtractorConfig,
        ) -> Result<Value, PreviewError> {
            // Build function definition
            let function = FunctionObjectArgs::default()
                .name("extract_data")
                .description("Extract structured data from the content")
                .parameters(schema)
                .build()
                .map_err(|e| PreviewError::ExternalServiceError {
                    service: "OpenAI".to_string(),
                    message: e.to_string(),
                })?;
            
            let tool = ChatCompletionToolArgs::default()
                .r#type(ChatCompletionToolType::Function)
                .function(function)
                .build()
                .map_err(|e| PreviewError::ExternalServiceError {
                    service: "OpenAI".to_string(),
                    message: e.to_string(),
                })?;
            
            // Build messages
            let system_message = ChatCompletionRequestSystemMessageArgs::default()
                .content("You are a helpful assistant that extracts structured data from web content.")
                .build()
                .map_err(|e| PreviewError::ExternalServiceError {
                    service: "OpenAI".to_string(),
                    message: e.to_string(),
                })?;
            
            let user_message = ChatCompletionRequestUserMessageArgs::default()
                .content(prompt)
                .build()
                .map_err(|e| PreviewError::ExternalServiceError {
                    service: "OpenAI".to_string(),
                    message: e.to_string(),
                })?;
            
            // Create request
            let request = CreateChatCompletionRequestArgs::default()
                .model(&self.model)
                .messages(vec![
                    ChatCompletionRequestMessage::System(system_message),
                    ChatCompletionRequestMessage::User(user_message),
                ])
                .tools(vec![tool])
                .tool_choice("required")
                .build()
                .map_err(|e| PreviewError::ExternalServiceError {
                    service: "OpenAI".to_string(),
                    message: e.to_string(),
                })?;
            
            // Make API call
            let response = self.client
                .chat()
                .create(request)
                .await
                .map_err(|e| PreviewError::ExternalServiceError {
                    service: "OpenAI".to_string(),
                    message: e.to_string(),
                })?;
            
            // Extract function call arguments
            if let Some(choice) = response.choices.first() {
                // First try tool calls (OpenAI format)
                if let Some(tool_calls) = &choice.message.tool_calls {
                    if let Some(tool_call) = tool_calls.first() {
                        let args_str = &tool_call.function.arguments;
                        let args: Value = serde_json::from_str(args_str)
                            .map_err(|e| PreviewError::ParseError(e.to_string()))?;
                        return Ok(args);
                    }
                }
                
                // Fallback: Try to extract JSON from content (for Claude compatibility)
                if let Some(content) = &choice.message.content {
                    // First try to parse the entire content as JSON
                    if let Ok(json) = serde_json::from_str::<Value>(content) {
                        return Ok(json);
                    }
                    
                    // Try to extract JSON from text
                    if let Some(json_str) = Self::extract_json_from_text(content) {
                        if let Ok(json) = serde_json::from_str::<Value>(&json_str) {
                            return Ok(json);
                        }
                    }
                }
            }
            
            Err(PreviewError::ExternalServiceError {
                service: "OpenAI".to_string(),
                message: "No function call or valid JSON in response".to_string(),
            })
        }
    }
}

// Anthropic provider would go here when anthropic-sdk-rust is available
// #[cfg(feature = "anthropic")]
pub mod anthropic {
    use super::*;
    
    /// Anthropic Claude provider implementation
    pub struct AnthropicProvider {
        api_key: String,
        model: String,
    }
    
    impl AnthropicProvider {
        pub fn new(api_key: String) -> Self {
            Self {
                api_key,
                model: "claude-3-opus-20240229".to_string(),
            }
        }
        
        pub fn with_model(mut self, model: String) -> Self {
            self.model = model;
            self
        }
    }
    
    #[async_trait]
    impl LLMProvider for AnthropicProvider {
        fn name(&self) -> &str {
            "anthropic"
        }
        
        async fn generate(
            &self,
            prompt: String,
            schema: Value,
            _config: &LLMExtractorConfig,
        ) -> Result<Value, PreviewError> {
            // Build the system prompt with schema instructions
            let schema_str = serde_json::to_string_pretty(&schema)
                .map_err(|e| PreviewError::ParseError(e.to_string()))?;
            
            let system_prompt = format!(
                "You are a helpful assistant that extracts structured data from web content. \
                You must respond with valid JSON that exactly matches this schema:\n\n{}\n\n\
                Only return the JSON object, no explanations or markdown.",
                schema_str
            );
            
            // Build the request payload
            let request_body = serde_json::json!({
                "model": self.model,
                "max_tokens": 1000,
                "system": system_prompt,
                "messages": [{
                    "role": "user",
                    "content": prompt
                }]
            });
            
            // Make the API call
            let client = reqwest::Client::new();
            let response = client
                .post("https://api.anthropic.com/v1/messages")
                .header("Authorization", format!("Bearer {}", self.api_key))
                .header("Content-Type", "application/json")
                .header("anthropic-version", "2023-06-01")
                .json(&request_body)
                .send()
                .await
                .map_err(|e| PreviewError::FetchError(e.to_string()))?;
            
            if !response.status().is_success() {
                let error_text = response.text().await.unwrap_or_default();
                return Err(PreviewError::ExternalServiceError {
                    service: "Anthropic".to_string(),
                    message: format!("API error: {}", error_text)
                });
            }
            
            let response_json: Value = response.json().await
                .map_err(|e| PreviewError::ParseError(e.to_string()))?;
            
            // Extract content from Claude's response
            let content = response_json["content"][0]["text"]
                .as_str()
                .ok_or_else(|| PreviewError::ExternalServiceError {
                    service: "Anthropic".to_string(),
                    message: "No content in response".to_string()
                })?;
            
            // Try to parse the content as JSON
            match serde_json::from_str::<Value>(content) {
                Ok(json) => Ok(json),
                Err(_) => {
                    // Try to extract JSON from text
                    if let Some(json_str) = AnthropicProvider::extract_json_from_text(content) {
                        serde_json::from_str(&json_str)
                            .map_err(|e| PreviewError::ParseError(e.to_string()))
                    } else {
                        Err(PreviewError::ExternalServiceError {
                            service: "Anthropic".to_string(),
                            message: "Could not extract valid JSON from response".to_string()
                        })
                    }
                }
            }
        }
    }
    
    impl AnthropicProvider {
        /// Extract JSON from text content
        fn extract_json_from_text(text: &str) -> Option<String> {
            let start = text.find('{')?;
            let end = text.rfind('}')?;
            
            if start <= end {
                let potential_json = &text[start..=end];
                if potential_json.contains('"') || potential_json.contains(':') {
                    return Some(potential_json.to_string());
                }
            }
            
            None
        }
    }
}

/// Local/Ollama provider for running models locally
pub struct LocalProvider {
    endpoint: String,
    model: String,
}

impl LocalProvider {
    pub fn new(endpoint: String, model: String) -> Self {
        Self { endpoint, model }
    }
}

// Claude-compatible provider module
pub mod claude_compat;

// Claude Code SDK provider module
#[cfg(feature = "cc-sdk")]
pub mod claude_code;

#[async_trait]
impl LLMProvider for LocalProvider {
    fn name(&self) -> &str {
        "local"
    }
    
    async fn generate(
        &self,
        prompt: String,
        schema: Value,
        _config: &LLMExtractorConfig,
    ) -> Result<Value, PreviewError> {
        // Build request for Ollama or local model server
        let schema_str = serde_json::to_string_pretty(&schema)
            .map_err(|e| PreviewError::ParseError(e.to_string()))?;
        
        let full_prompt = format!(
            "Extract structured data from the following content and return only valid JSON that matches this schema:\n\n{}\n\nContent:\n{}\n\nJSON:",
            schema_str,
            prompt
        );
        
        let request_body = serde_json::json!({
            "model": self.model,
            "prompt": full_prompt,
            "format": "json",
            "stream": false
        });
        
        // Make request to local model server (e.g., Ollama)
        let client = reqwest::Client::new();
        let response = client
            .post(format!("{}/api/generate", self.endpoint))
            .json(&request_body)
            .send()
            .await
            .map_err(|e| PreviewError::FetchError(e.to_string()))?;
        
        if !response.status().is_success() {
            return Err(PreviewError::ExternalServiceError {
                service: "Local".to_string(),
                message: format!("Local model server error: {}", response.status())
            });
        }
        
        let response_json: Value = response.json().await
            .map_err(|e| PreviewError::ParseError(e.to_string()))?;
        
        // Extract response from Ollama format
        let content = response_json["response"]
            .as_str()
            .ok_or_else(|| PreviewError::ExternalServiceError {
                service: "Local".to_string(),
                message: "No response field in local model output".to_string()
            })?;
        
        // Parse JSON response
        serde_json::from_str::<Value>(content)
            .or_else(|_| {
                // Try to extract JSON from text if direct parsing fails
                if let Some(json_str) = LocalProvider::extract_json_from_text(content) {
                    serde_json::from_str(&json_str)
                        .map_err(|e| PreviewError::ParseError(e.to_string()))
                } else {
                    Err(PreviewError::ExternalServiceError {
                        service: "Local".to_string(),
                        message: "Could not parse JSON from local model response".to_string()
                    })
                }
            })
    }
}

impl LocalProvider {
    /// Extract JSON from text content
    fn extract_json_from_text(text: &str) -> Option<String> {
        let start = text.find('{')?;
        let end = text.rfind('}')?;
        
        if start <= end {
            let potential_json = &text[start..=end];
            if potential_json.contains('"') || potential_json.contains(':') {
                return Some(potential_json.to_string());
            }
        }
        
        None
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    
    #[tokio::test]
    async fn test_mock_provider() {
        let provider = MockProvider::new();
        let schema = serde_json::json!({
            "type": "object",
            "properties": {
                "title": { "type": "string" },
                "price": { "type": "number" },
                "available": { "type": "boolean" }
            }
        });
        
        let result = provider.generate(
            "Test prompt".to_string(),
            schema,
            &LLMExtractorConfig::default()
        ).await.unwrap();
        
        assert!(result.is_object());
        let obj = result.as_object().unwrap();
        assert_eq!(obj.get("title").unwrap().as_str().unwrap(), "Mock value");
        assert_eq!(obj.get("price").unwrap().as_i64().unwrap(), 42);
        assert_eq!(obj.get("available").unwrap().as_bool().unwrap(), true);
    }
}