paper-gap 0.3.2

Local CLI for finding research papers with missing, weak, or stale public code
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
use crate::Result;
use serde::{Deserialize, Serialize};
use std::time::Duration;

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct ProviderInfo {
    pub id: &'static str,
    pub display_name: &'static str,
    pub kind: ProviderKind,
    pub status: ProviderStatus,
    pub auth: AuthKind,
    pub base_url: &'static str,
    pub notes: &'static str,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ProviderKind {
    Paper,
    Code,
    Repo,
    Metadata,
    Both,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum ProviderStatus {
    Enabled,
    Available,
    Unavailable,
    Planned,
}

#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
#[serde(rename_all = "snake_case")]
pub enum AuthKind {
    None,
    Optional,
    Required,
    Paid,
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SourceFilter {
    All,
    Papers,
    Repos,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiscoverKind {
    Papers,
    Repos,
    All,
}

#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub struct DiscoveredSource {
    #[serde(default = "default_discovery_backend")]
    pub discovery_backend: String,
    pub name: String,
    pub url: String,
    pub kind: String,
    pub source_type: String,
    pub auth: String,
    pub api_available: String,
    pub docs_url: Option<String>,
    pub coverage_notes: String,
    pub integration_difficulty: String,
    pub confidence: String,
    pub evidence_links: Vec<String>,
    pub evidence_snippets: Vec<String>,
}

pub fn builtin() -> Vec<ProviderInfo> {
    vec![
        ProviderInfo {
            id: "arxiv",
            display_name: "arXiv",
            kind: ProviderKind::Paper,
            status: ProviderStatus::Enabled,
            auth: AuthKind::None,
            base_url: "https://export.arxiv.org/api/query",
            notes: "MVP paper provider",
        },
        ProviderInfo {
            id: "openalex",
            display_name: "OpenAlex",
            kind: ProviderKind::Paper,
            status: ProviderStatus::Enabled,
            auth: AuthKind::None,
            base_url: "https://api.openalex.org/works",
            notes: "Broad scholarly metadata provider; OPENALEX_EMAIL optional",
        },
        ProviderInfo {
            id: "papers-with-code",
            display_name: "Papers with Code",
            kind: ProviderKind::Code,
            status: ProviderStatus::Enabled,
            auth: AuthKind::None,
            base_url: "https://paperswithcode.com/api/v1",
            notes: "MVP code-link signal",
        },
        ProviderInfo {
            id: "github",
            display_name: "GitHub",
            kind: ProviderKind::Repo,
            status: ProviderStatus::Enabled,
            auth: AuthKind::Optional,
            base_url: "https://api.github.com",
            notes: "MVP repository search",
        },
        ProviderInfo {
            id: "gitlab",
            display_name: "GitLab",
            kind: ProviderKind::Repo,
            status: ProviderStatus::Enabled,
            auth: AuthKind::Optional,
            base_url: "https://gitlab.com/api/v4",
            notes: "MVP repository search",
        },
        ProviderInfo {
            id: "codeberg",
            display_name: "Codeberg",
            kind: ProviderKind::Repo,
            status: ProviderStatus::Enabled,
            auth: AuthKind::Optional,
            base_url: "https://codeberg.org/api/v1",
            notes: "Forgejo/Gitea default instance",
        },
    ]
}

pub fn filter(providers: Vec<ProviderInfo>, filter: SourceFilter) -> Vec<ProviderInfo> {
    providers
        .into_iter()
        .filter(|provider| match filter {
            SourceFilter::All => true,
            SourceFilter::Papers => matches!(
                provider.kind,
                ProviderKind::Paper | ProviderKind::Metadata | ProviderKind::Both
            ),
            SourceFilter::Repos => matches!(provider.kind, ProviderKind::Repo),
        })
        .collect()
}

pub async fn health_check(url: &str) -> &'static str {
    let client = match reqwest::Client::builder()
        .timeout(Duration::from_secs(5))
        .build()
    {
        Ok(client) => client,
        Err(_) => return "unreachable",
    };
    match client.get(url).send().await {
        Ok(response) if response.status().is_success() || response.status().is_redirection() => {
            "reachable"
        }
        Ok(_) => "unreachable",
        Err(_) => "unreachable",
    }
}

pub fn discovery_queries(kind: DiscoverKind) -> Vec<&'static str> {
    match kind {
        DiscoverKind::Papers => vec![
            "scholarly metadata APIs",
            "research paper APIs",
            "preprint APIs",
            "open-access paper indexes",
            "domain-specific literature databases",
            "conference open proceedings APIs",
        ],
        DiscoverKind::Repos => vec![
            "public code forges",
            "Git hosting APIs",
            "Forgejo Gitea instances",
            "GitLab instances",
            "institutional research code hosts",
            "package registries research software",
        ],
        DiscoverKind::All => {
            let mut queries = discovery_queries(DiscoverKind::Papers);
            queries.extend(discovery_queries(DiscoverKind::Repos));
            queries
        }
    }
}

pub async fn discover(kind: DiscoverKind) -> Result<Vec<DiscoveredSource>> {
    let client = reqwest::Client::builder()
        .user_agent("paper-gap")
        .timeout(Duration::from_secs(10))
        .build()?;
    let brave_key = std::env::var("BRAVE_SEARCH_API_KEY").ok();
    let mut sources = Vec::new();

    for query in discovery_queries(kind) {
        let discovered = if let Some(key) = brave_key.as_deref() {
            match discover_brave(&client, kind, query, key).await {
                Ok(found) => found,
                Err(error) => {
                    eprintln!(
                        "paper-gap: Brave discovery failed for {query}: {error}; falling back to DuckDuckGo"
                    );
                    discover_duckduckgo(&client, kind, query)
                        .await
                        .unwrap_or_else(|error| {
                            eprintln!(
                                "paper-gap: DuckDuckGo discovery failed for {query}: {error}"
                            );
                            Vec::new()
                        })
                }
            }
        } else {
            discover_duckduckgo(&client, kind, query)
                .await
                .unwrap_or_else(|error| {
                    eprintln!("paper-gap: DuckDuckGo discovery failed for {query}: {error}");
                    Vec::new()
                })
        };
        sources.extend(discovered);
    }

    Ok(deduplicate_discovered_sources(sources))
}

async fn discover_duckduckgo(
    client: &reqwest::Client,
    kind: DiscoverKind,
    query: &str,
) -> Result<Vec<DiscoveredSource>> {
    let url = format!(
        "https://duckduckgo.com/html/?q={}",
        urlencoding::encode(query)
    );
    let body = client
        .get(&url)
        .send()
        .await?
        .error_for_status()?
        .text()
        .await?;
    Ok(parse_duckduckgo_html(kind, query, &url, &body))
}

async fn discover_brave(
    client: &reqwest::Client,
    kind: DiscoverKind,
    query: &str,
    key: &str,
) -> Result<Vec<DiscoveredSource>> {
    let url = format!(
        "https://api.search.brave.com/res/v1/web/search?q={}&count=5",
        urlencoding::encode(query)
    );
    let body = client
        .get(&url)
        .header("X-Subscription-Token", key)
        .send()
        .await?
        .error_for_status()?
        .text()
        .await?;
    parse_brave_json(kind, query, &url, &body)
}

pub fn parse_brave_json(
    kind: DiscoverKind,
    query: &str,
    search_url: &str,
    json: &str,
) -> Result<Vec<DiscoveredSource>> {
    #[derive(Deserialize)]
    struct Response {
        web: Option<Web>,
    }
    #[derive(Deserialize)]
    struct Web {
        results: Vec<Item>,
    }
    #[derive(Deserialize)]
    struct Item {
        title: String,
        url: String,
        description: Option<String>,
    }

    let response: Response = serde_json::from_str(json)?;
    Ok(response
        .web
        .into_iter()
        .flat_map(|web| web.results)
        .take(5)
        .map(|item| {
            discovered_source(
                "brave_api",
                kind,
                query,
                search_url,
                item.title,
                item.url,
                item.description.unwrap_or_default(),
                "medium",
            )
        })
        .collect())
}

pub fn parse_duckduckgo_html(
    kind: DiscoverKind,
    query: &str,
    search_url: &str,
    html: &str,
) -> Vec<DiscoveredSource> {
    html.lines()
        .filter(|line| line.contains("result__a"))
        .take(5)
        .map(|line| {
            let name = strip_html(line).trim().to_string();
            let result_url = attribute(line, "href").unwrap_or_else(|| search_url.to_string());
            discovered_source(
                "duckduckgo_html",
                kind,
                query,
                search_url,
                if name.is_empty() { query.into() } else { name },
                result_url,
                strip_html(line),
                "low",
            )
        })
        .collect()
}

fn discovered_source(
    backend: &str,
    kind: DiscoverKind,
    query: &str,
    search_url: &str,
    name: String,
    url: String,
    snippet: String,
    confidence: &str,
) -> DiscoveredSource {
    let lower = format!("{} {}", name, url).to_ascii_lowercase();
    let docs_url = (lower.contains("docs") || lower.contains("api")).then(|| url.clone());
    DiscoveredSource {
        discovery_backend: backend.into(),
        name,
        url: url.clone(),
        kind: match kind {
            DiscoverKind::Papers => "papers",
            DiscoverKind::Repos => "repos",
            DiscoverKind::All => "both",
        }
        .into(),
        source_type: classify(query).into(),
        auth: "unknown".into(),
        api_available: if docs_url.is_some() { "yes" } else { "unknown" }.into(),
        docs_url,
        coverage_notes: format!("Discovered from query: {query}"),
        integration_difficulty: "unknown".into(),
        confidence: confidence.into(),
        evidence_links: vec![url, search_url.into()],
        evidence_snippets: vec![snippet],
    }
}

pub fn deduplicate_discovered_sources(sources: Vec<DiscoveredSource>) -> Vec<DiscoveredSource> {
    let mut deduplicated = Vec::new();
    for source in sources {
        let domain = normalized_domain(&source.url);
        if domain.is_empty()
            || !deduplicated
                .iter()
                .any(|existing: &DiscoveredSource| normalized_domain(&existing.url) == domain)
        {
            deduplicated.push(source);
        }
    }
    deduplicated
}

fn normalized_domain(url: &str) -> String {
    let without_scheme = url.split_once("://").map_or(url, |(_, rest)| rest);
    without_scheme
        .split('/')
        .next()
        .unwrap_or_default()
        .trim_start_matches("www.")
        .to_ascii_lowercase()
}

fn attribute(input: &str, name: &str) -> Option<String> {
    let marker = format!("{name}=\"");
    let start = input.find(&marker)? + marker.len();
    let end = input[start..].find('"')? + start;
    Some(input[start..end].replace("&amp;", "&"))
}

fn default_discovery_backend() -> String {
    "unknown".into()
}

fn classify(query: &str) -> &'static str {
    if query.contains("package") {
        "package_registry"
    } else if query.contains("forge") || query.contains("Git") || query.contains("GitLab") {
        "code_forge"
    } else if query.contains("preprint") {
        "preprint_server"
    } else if query.contains("metadata") {
        "aggregator_api"
    } else {
        "unclear"
    }
}

fn strip_html(input: &str) -> String {
    let mut out = String::new();
    let mut in_tag = false;
    for ch in input.chars() {
        match ch {
            '<' => in_tag = true,
            '>' => in_tag = false,
            _ if !in_tag => out.push(ch),
            _ => {}
        }
    }
    out.replace("&amp;", "&")
        .replace("&quot;", "\"")
        .split_whitespace()
        .collect::<Vec<_>>()
        .join(" ")
}