From 4d912c7ef527b1a91d87160f77bb6ba7cac84def Mon Sep 17 00:00:00 2001 From: Jeremy Chone Date: Sat, 12 Oct 2024 15:29:36 -0700 Subject: [PATCH] + rhai - add md::enclosed_block_content_or_raw (rel #11) --- Cargo.toml | 2 +- .../rhai_script/rhai_modules/rhai_md.rs | 24 +++++++++++++++++-- src/support/md/mod.rs | 2 ++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f6709e3..90d061a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ repository = "https://github.com/jeremychone/rust-devai" [lints.rust] unsafe_code = "forbid" -# unused = { level = "allow", priority = -1 } # For exploratory dev. +unused = { level = "allow", priority = -1 } # For exploratory dev. [dependencies] # -- Async diff --git a/src/script/rhai_script/rhai_modules/rhai_md.rs b/src/script/rhai_script/rhai_modules/rhai_md.rs index d9206a6..1cedb7c 100644 --- a/src/script/rhai_script/rhai_modules/rhai_md.rs +++ b/src/script/rhai_script/rhai_modules/rhai_md.rs @@ -23,7 +23,11 @@ pub fn rhai_module() -> Module { FuncRegistration::new("extract_blocks") .in_global_namespace() - .set_into_module(&mut module, extract_blocks_with_name); + .set_into_module(&mut module, extract_blocks_with_lang); + + FuncRegistration::new("outer_delimited_block_content_or_raw") + .in_global_namespace() + .set_into_module(&mut module, outer_delimited_block_content_or_raw); module } @@ -52,10 +56,26 @@ fn extract_blocks(md_content: &str) -> RhaiResult { /// returning only the blocks with a /// [language identifier](https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks#syntax-highlighting) /// that matches `lang_name`. -fn extract_blocks_with_name(md_content: &str, lang_name: &str) -> RhaiResult { +fn extract_blocks_with_lang(md_content: &str, lang_name: &str) -> RhaiResult { let blocks: Vec = md::MdBlocks::new(md_content, Some(lang_name)).collect(); let blocks: Vec = blocks.into_iter().map(MdBlock::into_dynamic).collect(); Ok(blocks.into()) } +/// ## RHAI Documentation +/// ```rhai +/// outer_delimited_block_content_or_raw(md_content: &str) -> Vec +/// ``` +/// +/// Without fully parsing the markdown, this function attempts to extract the content from the first triple backticks +/// until the last triple backticks. +/// If no start/end triple backticks are found, it will return the raw content. +/// +/// > Note: This is useful in the genai context because often LLMs return a top block (e.g., markdown, Rust) +/// > which might have other ` ``` ` in the middle but should be interpreted as nested. +/// > (GenAI does not seem to know about the 6 ticks for top level) +fn outer_delimited_block_content_or_raw(md_content: &str) -> String { + md::outer_delimited_block_content_or_raw(md_content) +} + // endregion: --- Rhai Functions diff --git a/src/support/md/mod.rs b/src/support/md/mod.rs index 8e0c61e..17be30d 100644 --- a/src/support/md/mod.rs +++ b/src/support/md/mod.rs @@ -1,7 +1,9 @@ // region: --- Modules mod md_blocks; +mod outer_block; pub use md_blocks::*; +pub use outer_block::*; // endregion: --- Modules