-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.rs
More file actions
282 lines (259 loc) · 8.05 KB
/
types.rs
File metadata and controls
282 lines (259 loc) · 8.05 KB
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
//! Types for the Web Documentation Resolver.
use crate::metrics::ResolveMetrics;
use serde::{Deserialize, Serialize};
/// Result from resolving a URL or query
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResolvedResult {
/// The URL of the result
pub url: String,
/// The content in markdown format
pub content: Option<String>,
/// Source provider
pub source: String,
/// Relevance score (0.0 - 1.0)
pub score: f64,
/// Telemetry and metrics
pub metrics: Option<ResolveMetrics>,
/// Validated links found in content
pub validated_links: Vec<String>,
/// Routing decisions made during resolution
pub routing_decisions: Vec<RoutingDecision>,
}
/// Decision details for a provider attempt
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RoutingDecision {
pub provider: String,
pub attempt_index: usize,
pub quality_score: Option<f32>,
pub accepted: bool,
pub skip_reason: Option<String>,
pub stop_reason: Option<String>,
pub negative_cache_hit: bool,
pub circuit_open: bool,
pub paid_provider: bool,
}
impl ResolvedResult {
/// Create a new resolved result
#[allow(dead_code)]
pub fn new(
url: impl Into<String>,
content: Option<String>,
source: impl Into<String>,
score: f64,
) -> Self {
Self {
url: url.into(),
content,
source: source.into(),
score,
metrics: None,
validated_links: Vec::new(),
routing_decisions: Vec::new(),
}
}
/// Check if result has valid content
pub fn is_valid(&self, min_chars: usize) -> bool {
self.content
.as_ref()
.map(|c| c.len() >= min_chars)
.unwrap_or(false)
}
}
/// Execution profiles for resource management
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, Default)]
#[serde(rename_all = "snake_case")]
pub enum Profile {
/// Use only free providers
Free,
/// Balance cost and quality (default)
#[default]
Balanced,
/// Prioritize speed, skip slow providers
Fast,
/// Prioritize quality, use best (even paid) providers
Quality,
}
impl std::str::FromStr for Profile {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"free" => Ok(Profile::Free),
"balanced" => Ok(Profile::Balanced),
"fast" => Ok(Profile::Fast),
"quality" => Ok(Profile::Quality),
_ => Err(format!("Unknown profile: {}", s)),
}
}
}
impl Profile {
/// Get allowed provider types for this profile
pub fn is_provider_allowed(&self, provider: ProviderType) -> bool {
match self {
Profile::Free => !provider.is_paid(),
Profile::Fast => provider.is_fast(),
Profile::Balanced => true,
Profile::Quality => true,
}
}
/// Get max hops/cascade depth for this profile
pub fn max_hops(&self) -> usize {
match self {
Profile::Free => 3,
Profile::Fast => 2,
Profile::Balanced => 6,
Profile::Quality => 8,
}
}
}
/// Provider types
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProviderType {
/// Exa MCP (free)
ExaMcp,
/// Exa SDK (requires API key)
Exa,
/// Tavily (requires API key)
Tavily,
/// Serper Google Search (requires API key, 2500 free credits)
Serper,
/// DuckDuckGo (free)
DuckDuckGo,
/// Mistral web search (requires API key)
MistralWebSearch,
/// llms.txt (free)
LlmsTxt,
/// Jina Reader (free)
Jina,
/// Firecrawl (requires API key)
Firecrawl,
/// Direct HTTP fetch (free)
DirectFetch,
/// Mistral browser (requires API key)
MistralBrowser,
/// Docling document parsing
Docling,
/// Image OCR
Ocr,
}
impl ProviderType {
/// Get provider name as string
pub fn name(&self) -> &'static str {
match self {
ProviderType::ExaMcp => "exa_mcp",
ProviderType::Exa => "exa",
ProviderType::Tavily => "tavily",
ProviderType::Serper => "serper",
ProviderType::DuckDuckGo => "duckduckgo",
ProviderType::MistralWebSearch => "mistral_websearch",
ProviderType::LlmsTxt => "llms_txt",
ProviderType::Jina => "jina",
ProviderType::Firecrawl => "firecrawl",
ProviderType::DirectFetch => "direct_fetch",
ProviderType::MistralBrowser => "mistral_browser",
ProviderType::Docling => "docling",
ProviderType::Ocr => "ocr",
}
}
/// Check if this is a query provider
pub fn is_query_provider(&self) -> bool {
matches!(
self,
ProviderType::ExaMcp
| ProviderType::Exa
| ProviderType::Tavily
| ProviderType::Serper
| ProviderType::DuckDuckGo
| ProviderType::MistralWebSearch
)
}
/// Check if this is a URL provider
pub fn is_url_provider(&self) -> bool {
matches!(
self,
ProviderType::LlmsTxt
| ProviderType::Jina
| ProviderType::Firecrawl
| ProviderType::DirectFetch
| ProviderType::MistralBrowser
| ProviderType::Docling
| ProviderType::Ocr
)
}
/// Check if this is a paid provider
pub fn is_paid(&self) -> bool {
matches!(
self,
ProviderType::Exa
| ProviderType::Tavily
| ProviderType::Firecrawl
| ProviderType::MistralWebSearch
| ProviderType::MistralBrowser
| ProviderType::Serper
)
}
/// Check if this is a fast provider
pub fn is_fast(&self) -> bool {
matches!(
self,
ProviderType::ExaMcp
| ProviderType::DuckDuckGo
| ProviderType::LlmsTxt
| ProviderType::Jina
| ProviderType::DirectFetch
)
}
}
impl std::fmt::Display for ProviderType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.name())
}
}
impl std::str::FromStr for ProviderType {
type Err = String;
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
match s {
"exa_mcp" => Ok(ProviderType::ExaMcp),
"exa" => Ok(ProviderType::Exa),
"tavily" => Ok(ProviderType::Tavily),
"serper" => Ok(ProviderType::Serper),
"duckduckgo" => Ok(ProviderType::DuckDuckGo),
"mistral_websearch" => Ok(ProviderType::MistralWebSearch),
"llms_txt" => Ok(ProviderType::LlmsTxt),
"jina" => Ok(ProviderType::Jina),
"firecrawl" => Ok(ProviderType::Firecrawl),
"direct_fetch" => Ok(ProviderType::DirectFetch),
"mistral_browser" => Ok(ProviderType::MistralBrowser),
"docling" => Ok(ProviderType::Docling),
"ocr" => Ok(ProviderType::Ocr),
_ => Err(format!("Unknown provider: {}", s)),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_resolved_result_valid() {
let result = ResolvedResult::new(
"https://example.com",
Some("content".to_string()),
"test",
1.0,
);
assert!(result.is_valid(5));
assert!(!result.is_valid(100));
}
#[test]
fn test_provider_type_names() {
assert_eq!(ProviderType::ExaMcp.name(), "exa_mcp");
assert_eq!(ProviderType::Tavily.name(), "tavily");
}
#[test]
fn test_provider_type_query() {
assert!(ProviderType::ExaMcp.is_query_provider());
assert!(!ProviderType::ExaMcp.is_url_provider());
assert!(ProviderType::LlmsTxt.is_url_provider());
assert!(!ProviderType::LlmsTxt.is_query_provider());
}
}