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
//! Vision Port Trait
//!
//! Defines the port interface for vision-capable LLM providers.
//! This trait abstracts vision analysis operations, allowing different
//! providers (OpenAI, Anthropic) to implement their specific vision APIs.
use async_trait::async_trait;
use paladin_core::platform::container::vision::{VisionContent, VisionError};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
/// Result from vision analysis
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VisionResult {
/// The analysis/description of the image(s)
pub content: String,
/// Model used for the analysis
pub model: String,
/// Token usage information
pub token_usage: VisionTokenUsage,
/// Additional metadata from the provider
#[serde(default)]
pub metadata: HashMap<String, String>,
/// Timestamp of the response
pub timestamp: chrono::DateTime<chrono::Utc>,
}
/// Token usage for vision requests
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VisionTokenUsage {
/// Tokens used for the prompt (text + images)
pub prompt_tokens: u32,
/// Tokens used for the completion
pub completion_tokens: u32,
/// Total tokens used
pub total_tokens: u32,
}
/// Vision port trait for multi-modal image analysis
#[async_trait]
pub trait VisionPort: Send + Sync {
/// Analyze one or more images with a text prompt
///
/// # Arguments
///
/// * `prompt` - Text prompt/question about the image(s)
/// * `images` - One or more images to analyze
/// * `model` - Model to use for analysis (must be vision-capable)
/// * `max_tokens` - Maximum tokens for the response
///
/// # Returns
///
/// * `Ok(VisionResult)` - Successful analysis with content and metadata
/// * `Err(VisionError)` - Error during analysis
///
/// # Errors
///
/// - `VisionError::ModelNotSupported` - Model doesn't support vision
/// - `VisionError::InvalidImage` - Image format/data is invalid
/// - `VisionError::AuthenticationError` - Invalid API credentials
/// - `VisionError::RateLimitExceeded` - Rate limit hit
/// - `VisionError::ProviderError` - Provider-specific error
/// - `VisionError::Timeout` - Request timed out
async fn analyze_image(
&self,
prompt: &str,
images: Vec<VisionContent>,
model: &str,
max_tokens: Option<u32>,
) -> Result<VisionResult, VisionError>;
/// Check if a specific model supports vision
///
/// # Arguments
///
/// * `model` - The model name to check
///
/// # Returns
///
/// `true` if the model supports vision, `false` otherwise
fn is_vision_model(&self, model: &str) -> bool;
/// Get the provider name (e.g., "openai", "anthropic")
fn provider_name(&self) -> &str;
}