Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: include universal patterns in bindings #431

Merged
merged 11 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion crates/cli/src/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ impl<'b> RichPattern<'b> {
) -> Result<CompilationResult> {
let lang = language.unwrap_or_default();
#[cfg(not(feature = "ai_builtins"))]
let injected_builtins: Option<BuiltIns> = None;
let injected_builtins: Option<BuiltIns> =
marzano_core::built_in_functions::get_ai_placeholder_functions();
#[cfg(feature = "ai_builtins")]
let injected_builtins = Some(ai_builtins::ai_builtins::get_ai_built_in_functions());

Expand Down
23 changes: 23 additions & 0 deletions crates/core/src/built_in_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,3 +494,26 @@ fn length_fn<'a>(
None => Err(anyhow!("length argument must be a list or string")),
}
}

pub fn get_ai_placeholder_functions() -> Option<BuiltIns> {
Some(
vec![
BuiltInFunction::new(
"llm_chat",
vec!["model", "messages", "pattern"],
Box::new(ai_fn_placholder),
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Spelling: Typo in function name ai_fn_placholder. Should be ai_fn_placeholder.

),
BuiltInFunction::new("embedding", vec!["target"], Box::new(ai_fn_placholder)),
]
.into(),
)
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the typo in the function name.

The function name ai_fn_placholder contains a typo. It should be ai_fn_placeholder.

-  Box::new(ai_fn_placholder),
+  Box::new(ai_fn_placeholder),
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
pub fn get_ai_placeholder_functions() -> Option<BuiltIns> {
Some(
vec![
BuiltInFunction::new(
"llm_chat",
vec!["model", "messages", "pattern"],
Box::new(ai_fn_placholder),
),
BuiltInFunction::new("embedding", vec!["target"], Box::new(ai_fn_placholder)),
]
.into(),
)
}
pub fn get_ai_placeholder_functions() -> Option<BuiltIns> {
Some(
vec![
BuiltInFunction::new(
"llm_chat",
vec!["model", "messages", "pattern"],
Box::new(ai_fn_placeholder),
),
BuiltInFunction::new("embedding", vec!["target"], Box::new(ai_fn_placeholder)),
]
.into(),
)
}


fn ai_fn_placholder<'a>(
_args: &'a [Option<Pattern<MarzanoQueryContext>>],
_context: &'a MarzanoContext<'a>,
_state: &mut State<'a, MarzanoQueryContext>,
_logs: &mut AnalysisLogs,
) -> Result<MarzanoResolvedPattern<'a>> {
bail!("AI features are not supported in your GritQL distribution. Please upgrade to the Enterprise version to use AI features.")
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the typo in the function name.

The function name ai_fn_placholder contains a typo. It should be ai_fn_placeholder.

-fn ai_fn_placholder<'a>(
+fn ai_fn_placeholder<'a>(
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fn ai_fn_placholder<'a>(
_args: &'a [Option<Pattern<MarzanoQueryContext>>],
_context: &'a MarzanoContext<'a>,
_state: &mut State<'a, MarzanoQueryContext>,
_logs: &mut AnalysisLogs,
) -> Result<MarzanoResolvedPattern<'a>> {
bail!("AI features are not supported in your GritQL distribution. Please upgrade to the Enterprise version to use AI features.")
}
fn ai_fn_placeholder<'a>(
_args: &'a [Option<Pattern<MarzanoQueryContext>>],
_context: &'a MarzanoContext<'a>,
_state: &mut State<'a, MarzanoQueryContext>,
_logs: &mut AnalysisLogs,
) -> Result<MarzanoResolvedPattern<'a>> {
bail!("AI features are not supported in your GritQL distribution. Please upgrade to the Enterprise version to use AI features.")
}

3 changes: 2 additions & 1 deletion crates/core/src/pattern_compiler/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,8 +495,8 @@ pub(crate) fn filter_libs(
foreign_functions: foreign_file,
} = defs_to_filenames(libs, parser, tree.root_node())?;
let mut filtered: BTreeMap<String, String> = BTreeMap::new();
// gross but necessary due to running these patterns befor and after each file

// gross but necessary due to running these patterns befor and after each file
let mut stack: Vec<Tree> = if will_autowrap {
let before_each_file = "before_each_file()";
let before_tree =
Expand Down Expand Up @@ -545,6 +545,7 @@ pub(crate) fn filter_libs(
}
}
}

Ok(filtered.into_iter().collect_vec())
}

Expand Down
32 changes: 20 additions & 12 deletions crates/gritmodule/src/patterns_directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl PatternsDirectory {
}
}

pub fn get_language_directory(&self, lang: PatternLanguage) -> &BTreeMap<String, String> {
fn get_language_directory(&self, lang: PatternLanguage) -> &BTreeMap<String, String> {
match lang {
PatternLanguage::JavaScript => &self.java_script,
PatternLanguage::TypeScript => &self.type_script,
Expand Down Expand Up @@ -147,6 +147,10 @@ impl PatternsDirectory {
}
}

fn get_universal(&self) -> &BTreeMap<String, String> {
self.get_language_directory(PatternLanguage::Universal)
}

#[tracing::instrument]
fn get_language_and_universal_directory(
&self,
Expand All @@ -157,9 +161,7 @@ impl PatternsDirectory {
};
let lang_library = self.get_language_directory(language);
let mut lang_library = lang_library.to_owned();
let universal = self
.get_language_directory(PatternLanguage::Universal)
.to_owned();
let universal = self.get_universal().to_owned();
let count = lang_library.len() + universal.len();
lang_library.extend(universal);
if count != lang_library.len() {
Expand All @@ -176,12 +178,6 @@ impl PatternsDirectory {
self.get_language_and_universal_directory(language)
}

fn get_language_directory_from_name(&self, name: &str) -> Option<&BTreeMap<String, String>> {
self.pattern_to_language
.get(name)
.map(|l| self.get_language_directory(*l))
}

// imo we should check if name matches [a-z][a-z0-9]*
// as currently a pattern with no language header and an invalid pattern are
// both treated as js patterns when the latter should be a not found error
Expand All @@ -195,9 +191,21 @@ impl PatternsDirectory {
Ok(LanguageLibrary::new(language, library))
}

fn get_language_directory_from_name(&self, name: &str) -> Option<&BTreeMap<String, String>> {
self.pattern_to_language
.get(name)
.map(|l| self.get_language_directory(*l))
}

pub fn get(&self, name: &str) -> Option<&String> {
self.get_language_directory_from_name(name)
.and_then(|d| d.get(name))
if let Some(dir) = self.get_language_directory_from_name(name) {
if let Some(pattern) = dir.get(name) {
return Some(pattern);
}
} else if let Some(pattern) = self.get_universal().get(name) {
return Some(pattern);
}
None
}

// do we want to do an overriding insert?
Expand Down
2 changes: 1 addition & 1 deletion crates/lsp/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ pub fn check_intersection(range1: &Range, range2: &Range) -> bool {

pub(crate) fn get_ai_built_in_functions_for_feature() -> Option<BuiltIns> {
#[cfg(not(feature = "ai_builtins"))]
return None;
return marzano_core::built_in_functions::get_ai_placeholder_functions();
#[cfg(feature = "ai_builtins")]
return Some(ai_builtins::ai_builtins::get_ai_built_in_functions());
}
Expand Down
Loading