Skip to content

Commit

Permalink
switch to rust thing
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronvg committed Jul 17, 2024
1 parent 69aab57 commit abb0e2a
Show file tree
Hide file tree
Showing 11 changed files with 513 additions and 143 deletions.
16 changes: 8 additions & 8 deletions engine/baml-lib/baml-core/src/validate/generator_loader/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,23 @@ pub(crate) fn parse_generator(
}
};

match parse_required_key(&args, "version", ast_generator.span()) {
Ok((version_str, version_span)) => match Version::parse(version_str) {
match parse_optional_key(&args, "version") {
Ok(Some(version_str)) => match Version::parse(version_str) {
Ok(version) => {
builder.version(version.to_string());
}
Err(_) => {
errors.push(DatamodelError::new_validation_error(
&format!("Invalid semver version string: '{}'", version_str),
version_span.clone(),
args.get("version").unwrap().span().clone(),
));
}
},
Err(_) => {
errors.push(DatamodelError::new_validation_error(
"Missing 'version' property in your generator. It must match the baml package version you have installed in your project and the VSCode extension, like 'version \"0.50.0\"'",
ast_generator.span().clone(),
));
Ok(None) => {
builder.version("0.0.0".to_string());
}
Err(err) => {
errors.push(err);
}
}

Expand Down
41 changes: 41 additions & 0 deletions engine/baml-schema-wasm/src/runtime_wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ use baml_runtime::{
};
use baml_types::{BamlMap, BamlValue};

use internal_baml_codegen::version_check::GeneratorType;
use internal_baml_codegen::version_check::{check_version, VersionCheckMode};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
Expand Down Expand Up @@ -907,6 +909,45 @@ impl WasmRuntime {
.collect()
}

#[wasm_bindgen]
pub fn check_version(
generator_version: &str,
current_version: &str,
generator_type: &str,
version_check_mode: &str,
generator_language: &str,
) -> Option<String> {
// Convert string parameters to enums
let generator_type = match generator_type {
"VSCodeCLI" => GeneratorType::VSCodeCLI,
"VSCode" => GeneratorType::VSCode,
"CLI" => GeneratorType::CLI,
_ => return Some("Invalid generator type".to_string()),
};

let version_check_mode = match version_check_mode {
"Strict" => VersionCheckMode::Strict,
"None" => VersionCheckMode::None,
_ => return Some("Invalid version check mode".to_string()),
};

let generator_language = match generator_language {
"python/pydantic" => GeneratorOutputType::PythonPydantic,
"typescript" => GeneratorOutputType::Typescript,
"ruby/sorbet" => GeneratorOutputType::RubySorbet,
_ => return Some("Invalid generator language".to_string()),
};

check_version(
generator_version,
current_version,
generator_type,
version_check_mode,
generator_language,
)
.map(|error| error.msg)
}

#[wasm_bindgen]
pub fn required_env_vars(&self) -> Vec<String> {
self.runtime
Expand Down
88 changes: 44 additions & 44 deletions engine/language-client-codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ use indexmap::IndexMap;
use internal_baml_core::{configuration::GeneratorOutputType, ir::repr::IntermediateRepr};
use semver::Version;
use std::{collections::BTreeMap, path::PathBuf};
use version_check::{check_version, GeneratorType, VersionCheckMode};

mod dir_writer;
mod python;
mod ruby;
mod typescript;
pub mod version_check;

pub struct GeneratorArgs {
/// Output directory for the generated client, relative to baml_src
Expand Down Expand Up @@ -109,64 +111,62 @@ pub trait GenerateClient {
-> Result<GenerateOutput>;
}

fn versions_equal_ignoring_patch(v1: &str, v2: &str) -> bool {
pub fn versions_equal_ignoring_patch(v1: &str, v2: &str) -> bool {
let version1 = Version::parse(v1).unwrap();
let version2 = Version::parse(v2).unwrap();

version1.major == version2.major && version1.minor == version2.minor
}

// Assume VSCode is the only one that uses WASM, and it does call this method but at a different time.
#[cfg(target_arch = "wasm32")]
fn version_check_with_error(
runtime_version: &str,
gen_version: &str,
generator_type: GeneratorType,
mode: VersionCheckMode,
client_type: GeneratorOutputType,
) -> Result<()> {
Ok(())
}

#[cfg(not(target_arch = "wasm32"))]
fn version_check_with_error(
runtime_version: &str,
gen_version: &str,
generator_type: GeneratorType,
mode: VersionCheckMode,
client_type: GeneratorOutputType,
) -> Result<()> {
let res = check_version(
runtime_version,
gen_version,
generator_type,
mode,
client_type,
);
match res {
Some(e) => Err(anyhow::anyhow!("Version mismatch: {}", e.msg)),
None => Ok(()),
}
}

impl GenerateClient for GeneratorOutputType {
fn generate_client(
&self,
ir: &IntermediateRepr,
gen: &GeneratorArgs,
) -> Result<GenerateOutput> {
let runtime_version = env!("CARGO_PKG_VERSION");
let gen_version = semver::Version::parse(&gen.version)?;
let runtime_semver = semver::Version::parse(runtime_version)?;
// with version check enabled, error if they dont match
if !gen.no_version_check {
if !versions_equal_ignoring_patch(&runtime_version, gen_version.to_string().as_str()) {
let error_message = format!(
"The generator in the BAML files (version: {}) does not match the installed baml version ({}). Major and minor version must match. Update the installed BAML package or the version in the BAML generator config. See https://docs.boundaryml.com/docs/calling-baml/generate-baml-client#best-practices",
gen.version, runtime_version
);
return Err(anyhow::anyhow!(error_message));
}
}

// Uncomment if we want to error if your generated code is newer than the runtime.
// Error if the version of the generator args is greater than this version which has been the main or only situation that has caused us issues since the generated code of a new version
// may contain types or constructs not actually available from the runtime.
// if gen_version > runtime_semver {
// let error_message = format!(
// "The generator in the BAML files (version: {}) is older than the installed baml version ({}). Try updating your installed BAML package to {}. See https://docs.boundaryml.com/docs/calling-baml/generate-baml-client#best-practices",
// gen.version, runtime_version, gen.version
// );
// return Err(anyhow::anyhow!(error_message));
// }

// log a warning everytime anyway regardless of what happens.
if gen_version != runtime_semver {
// Log a warning if BAML generated files are stale / user forgot to update the version in the config.
if gen_version < runtime_semver {
let warning_message = format!(
"The generator in the BAML files (version: {}) is older than the installed baml version ({}). Please update the generator configuration to {}. See https://docs.boundaryml.com/docs/calling-baml/generate-baml-client#best-practices",
gen.version, runtime_version, runtime_version
);
println!("{}", "BAML Warning:".yellow().bold());
println!("{}", warning_message.yellow());
log::warn!("{}", warning_message);
} else {
let warning_message = format!(
"The generator in the BAML files (version: {}) is newer than the installed baml version ({}). Please update the installed BAML package to {}. See https://docs.boundaryml.com/docs/calling-baml/generate-baml-client#best-practices",
gen.version, runtime_version, gen.version
);
println!("{}", "BAML Warning:".yellow().bold());
println!("{}", warning_message.yellow());
log::warn!("{}", warning_message);
}
if !gen.no_version_check {
version_check_with_error(
runtime_version,
&gen.version,
GeneratorType::CLI,
VersionCheckMode::Strict,
self.clone(),
)?;
}

let files = match self {
Expand Down
Loading

0 comments on commit abb0e2a

Please sign in to comment.