use crate::cache::hash_policy;
use crate::policy::{PolicyConfig, TablePolicy};
use crate::prompt::PromptSkill;
use crate::schema::{ColumnSchema, DialectProfile, SchemaSnapshot, TableSchema};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
pub(crate) mod filter;
pub(crate) mod prompt;
#[derive(Debug, Clone)]
pub struct PromptBuilder {
pub(crate) schema: Arc<SchemaSnapshot>,
pub(crate) dialect: DialectProfile,
pub(crate) policy: PolicyConfig,
pub(crate) policy_hash: u64,
pub(crate) include_examples: bool,
pub(crate) skill: Option<PromptSkill>,
pub(crate) reverse_fk_index: HashMap<String, Vec<String>>,
pub(crate) vector_search: bool,
#[cfg(feature = "vector-search")]
pub(crate) schema_indexer: Option<Arc<crate::prompt::schema_index::SchemaIndexer>>,
}
impl PromptBuilder {
pub fn new(schema: Arc<SchemaSnapshot>, dialect: DialectProfile, policy: PolicyConfig) -> Self {
let reverse_fk_index = build_reverse_fk_index(&schema);
Self {
schema,
dialect,
policy_hash: hash_policy(&policy),
policy,
include_examples: true,
skill: None,
reverse_fk_index,
vector_search: false,
#[cfg(feature = "vector-search")]
schema_indexer: None,
}
}
#[must_use]
pub fn with_skill(mut self, skill: PromptSkill) -> Self {
self.skill = Some(skill);
self
}
#[must_use]
pub fn with_examples(mut self, include_examples: bool) -> Self {
self.include_examples = include_examples;
self
}
#[must_use]
pub fn with_vector_search(mut self, enabled: bool) -> Self {
self.vector_search = enabled;
self
}
#[must_use]
#[cfg(feature = "vector-search")]
pub fn with_schema_indexer(
mut self,
indexer: Arc<crate::prompt::schema_index::SchemaIndexer>,
) -> Self {
self.schema_indexer = Some(indexer);
self
}
pub fn schema(&self) -> &Arc<SchemaSnapshot> {
&self.schema
}
pub fn dialect(&self) -> &DialectProfile {
&self.dialect
}
pub fn policy(&self) -> &PolicyConfig {
&self.policy
}
#[cfg(feature = "vector-search")]
pub(crate) fn ensure_indexer(&self) -> Option<Arc<crate::prompt::schema_index::SchemaIndexer>> {
if self.vector_search {
self.schema_indexer.clone()
} else {
None
}
}
pub(crate) fn is_forbidden_by_skill(&self, feature: &str) -> bool {
self.skill
.as_ref()
.is_some_and(|s| s.forbid_features.iter().any(|f| f == feature))
}
pub(crate) fn column_visible(
&self,
table: &TableSchema,
column: &ColumnSchema,
policy: Option<&TablePolicy>,
) -> bool {
if self.policy.global_denied_columns.iter().any(|denied| {
denied == &column.name || denied == &format!("{}.{}", table.name, column.name)
}) {
return false;
}
let Some(policy) = policy else {
return true;
};
if !policy.allowed || policy.denied_columns.contains(&column.name) {
return false;
}
match &policy.allowed_columns {
Some(allowed) => allowed.contains(&column.name),
None => true,
}
}
pub(crate) fn expand_foreign_key_neighbors(
&self,
matched: &HashSet<String>,
) -> HashSet<String> {
let mut expanded = matched.clone();
for table_name in matched {
if let Some(table) = self.schema.get_table(table_name) {
for column in &table.columns {
if let Some(fk) = &column.foreign_key
&& self.schema.get_table(&fk.foreign_table).is_some()
{
expanded.insert(fk.foreign_table.clone());
}
}
}
if let Some(referencing_tables) = self.reverse_fk_index.get(table_name) {
for ref_table in referencing_tables {
if self.schema.get_table(ref_table).is_some() {
expanded.insert(ref_table.clone());
}
}
}
}
expanded
}
pub(crate) fn all_table_names(&self) -> Vec<String> {
self.schema
.tables
.iter()
.map(|table| table.name.clone())
.collect()
}
}
fn build_reverse_fk_index(schema: &SchemaSnapshot) -> HashMap<String, Vec<String>> {
let mut index: HashMap<String, Vec<String>> = HashMap::new();
for table in &schema.tables {
for column in &table.columns {
if let Some(fk) = &column.foreign_key {
index
.entry(fk.foreign_table.clone())
.or_default()
.push(table.name.clone());
}
}
}
index
}