Skip to main content

lean_ctx_ext/core/knowledge_router/
manifest_builder.rs

1// SPDX-License-Identifier: Apache-2.0
2//! Provider-aware source-manifest construction.
3use std::fs;
4
5use super::source_manifest::{CostClass, SourceCapabilities, SourceManifestEntry};
6
7#[derive(Debug, Clone, Default, PartialEq, Eq)]
8pub struct ProviderConfig {
9    pub jira_configured: bool,
10    pub github_configured: bool,
11    pub gitlab_configured: bool,
12    pub postgres_configured: bool,
13    pub custom_providers: Vec<String>,
14}
15
16pub fn build_manifests_from_config(config: &ProviderConfig) -> Vec<SourceManifestEntry> {
17    let mut provider_ids = config.custom_providers.clone();
18    provider_ids.extend(
19        [
20            (config.jira_configured, "jira"),
21            (config.github_configured, "github"),
22            (config.gitlab_configured, "gitlab"),
23            (config.postgres_configured, "postgres"),
24        ]
25        .into_iter()
26        .filter_map(|(enabled, id)| enabled.then_some(id.to_string())),
27    );
28    build_manifests_for_provider_ids(&provider_ids)
29}
30
31/// Build manifests for every registered provider, including dynamic providers.
32pub fn build_manifests_for_provider_ids(provider_ids: &[String]) -> Vec<SourceManifestEntry> {
33    let mut manifests = vec![entry(
34        "local_files",
35        "LocalFiles",
36        &["file"],
37        CostClass::Negligible,
38    )];
39    let mut provider_ids = provider_ids.to_vec();
40    provider_ids.sort();
41    provider_ids.dedup();
42    manifests.extend(provider_ids.iter().map(|id| provider_entry(id)));
43    manifests
44}
45
46pub fn detect_config() -> ProviderConfig {
47    let providers = crate::core::config::Config::load().providers;
48    let file = crate::core::config::Config::path()
49        .and_then(|path| fs::read_to_string(path).ok())
50        .unwrap_or_default();
51    let configured = |env: &str, value: bool| std::env::var_os(env).is_some() || value;
52    ProviderConfig {
53        jira_configured: configured("JIRA_URL", file.contains("[providers.jira]")),
54        github_configured: configured(
55            "GITHUB_TOKEN",
56            providers.github.token.is_some() || providers.github.api_url.is_some(),
57        ),
58        gitlab_configured: configured(
59            "GITLAB_TOKEN",
60            providers.gitlab.token.is_some() || providers.gitlab.api_url.is_some(),
61        ),
62        postgres_configured: std::env::var_os("DATABASE_URL").is_some()
63            || std::env::var_os("PGDATABASE").is_some(),
64        custom_providers: providers.mcp_bridges.keys().cloned().collect(),
65    }
66}
67
68fn provider_entry(id: &str) -> SourceManifestEntry {
69    match id {
70        "jira" => entry(id, "Jira", &["issue"], CostClass::Low),
71        "github" => entry(id, "GitHub", &["issue", "pull_request"], CostClass::Low),
72        "gitlab" => entry(id, "GitLab", &["issue", "pull_request"], CostClass::Low),
73        "postgres" => entry(id, "PostgreSQL", &["schema", "table"], CostClass::Medium),
74        "emspace" => entry(id, "Emspace", &["page", "task"], CostClass::Low),
75        _ => entry(id, id, &["external"], CostClass::Medium),
76    }
77}
78
79fn entry(id: &str, name: &str, kinds: &[&str], cost_class: CostClass) -> SourceManifestEntry {
80    SourceManifestEntry {
81        source_id: id.into(),
82        display_name: name.into(),
83        kinds: kinds.iter().map(|kind| (*kind).into()).collect(),
84        capabilities: SourceCapabilities {
85            search: true,
86            exact_get: true,
87            delta_sync: true,
88            live_query: id != "local_files",
89            graph_edges: true,
90        },
91        freshness_typical_ms: 60_000,
92        cost_class,
93    }
94}