Skip to content

Commit

Permalink
feat: set up auto-format
Browse files Browse the repository at this point in the history
  • Loading branch information
sxlijin committed Dec 14, 2024
1 parent d3b9596 commit c537d65
Show file tree
Hide file tree
Showing 21 changed files with 204 additions and 77 deletions.
22 changes: 22 additions & 0 deletions engine/baml-schema-wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#[cfg(target_arch = "wasm32")]
pub mod runtime_wasm;

use internal_baml_core::internal_baml_schema_ast::{format_schema, FormatOptions};
use std::env;
use wasm_bindgen::prelude::*;

Expand All @@ -9,3 +10,24 @@ pub fn version() -> String {
// register_panic_hook();
env!("CARGO_PKG_VERSION").to_string()
}

#[wasm_bindgen]
pub fn format_document(path: String, text: String) -> Option<String> {
log::info!("Trying to format document (rust): {}", path);
match format_schema(
&text,
FormatOptions {
indent_width: 2,
fail_on_unhandled_rule: false,
},
) {
Ok(formatted) => {
log::info!("Formatted document: {}", formatted);
Some(formatted)
}
Err(e) => {
log::error!("Failed to format document: {} {:?}", path, e);
None
}
}
}
3 changes: 2 additions & 1 deletion engine/baml-schema-wasm/src/runtime_wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ pub fn on_wasm_init() {
if #[cfg(debug_assertions)] {
const LOG_LEVEL: log::Level = log::Level::Debug;
} else {
const LOG_LEVEL: log::Level = log::Level::Warn;
// const LOG_LEVEL: log::Level = log::Level::Warn;
const LOG_LEVEL: log::Level = log::Level::Debug;
}
};
// I dont think we need this line anymore -- seems to break logging if you add it.
Expand Down
7 changes: 6 additions & 1 deletion engine/cli/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,12 @@ impl RuntimeCli {
args.from = BamlRuntime::parse_baml_src_path(&args.from)?;
t.block_on(async { args.run_async().await })
}
Commands::Format(args) => args.run(),
Commands::Format(args) => {
// We deliberately don't apply parse_baml_src_path here
// see format.rs for more details
// args.from = BamlRuntime::parse_baml_src_path(&args.from)?;
args.run()
}
}
}
}
61 changes: 47 additions & 14 deletions engine/cli/src/format.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,64 @@
use std::{fs, path::PathBuf};

use anyhow::Result;
use baml_runtime::{baml_src_files, BamlRuntime};
use clap::Args;
use internal_baml_core::internal_baml_schema_ast::{format_schema, FormatOptions};

#[derive(Args, Debug)]
pub struct FormatArgs {
#[arg(long, help = "path/to/baml_src", default_value = "./baml_src")]
pub from: PathBuf,

#[arg(
help = "Specific files to format. If none provided, formats all files in the baml_src directory"
)]
pub paths: Vec<PathBuf>,

#[arg(
short = 'n',
long = "dry-run",
help = "Write formatter changes to stdout instead of files",
default_value = "false"
)]
pub dry_run: bool,
}

impl FormatArgs {
pub fn run(&self) -> Result<()> {
let source = fs::read_to_string(&self.from)?;
let formatted = format_schema(
&source,
FormatOptions {
indent_width: 4,
fail_on_unhandled_rule: false,
},
)?;

let mut to = self.from.clone();
to.set_extension("formatted.baml");
fs::write(&to, formatted)?;

log::info!("Formatted {} to {}", self.from.display(), to.display());
let paths = if self.paths.is_empty() {
// Usually this is done in commands.rs, but fmt is a special case
// because it doesn't need to actually load the BAML runtime to parse
// BAML files.
let from = BamlRuntime::parse_baml_src_path(&self.from)?;
baml_src_files(&from)?
} else {
self.paths.clone()
};

for path in paths.iter() {
let source = fs::read_to_string(&path)?;
match format_schema(
&source,
FormatOptions {
indent_width: 2,
fail_on_unhandled_rule: false,
},
) {
Ok(formatted) => {
if self.dry_run {
println!("{}", formatted);
} else {
fs::write(&path, formatted)?;
}
}
Err(e) => {
log::error!("Failed to format {}: {}", path.display(), e);
}
}
}

log::info!("Formatted {} files", paths.len());

Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions integ-tests/baml_src/clients.baml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ client<llm> GPT4 {
model gpt-4o
api_key env.OPENAI_API_KEY
}
}
}


client<llm> GPT4o {
Expand All @@ -28,7 +28,7 @@ client<llm> GPT4o {
model gpt-4o
api_key env.OPENAI_API_KEY
}
}
}


client<llm> GPT4Turbo {
Expand All @@ -38,7 +38,7 @@ client<llm> GPT4Turbo {
model gpt-4-turbo
api_key env.OPENAI_API_KEY
}
}
}

client<llm> GPT35 {
provider openai
Expand Down Expand Up @@ -164,7 +164,7 @@ client<llm> Resilient_SimpleSyntax {
Lottery_SimpleSyntax
]
}
}
}

client<llm> Lottery_SimpleSyntax {
provider baml-round-robin
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ class ReceiptItem {
}

class ReceiptInfo {
items ReceiptItem[]
total_cost float?
venue "barisa" | "ox_burger"
items ReceiptItem[]
total_cost float?
venue "barisa" | "ox_burger"
}

function ExtractReceiptInfo(email: string, reason: "curiosity" | "personal_finance") -> ReceiptInfo {
Expand Down
1 change: 0 additions & 1 deletion integ-tests/baml_src/test-files/dynamic/dynamic.baml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ enum DynEnumTwo {
class SomeClassNestedDynamic {
hi string
@@dynamic

}

class DynamicClassTwo {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
class NamedArgsSingleClass {
key string
key_two bool
key_three int
// TODO: doesn't work with keys with numbers
// key2 bool
// key3 int
key_three int // TODO: doesn't work with keys with numbers // key2 bool // key3 int
}

function TestFnNamedArgsSingleClass(myArg: NamedArgsSingleClass) -> string {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


// string[]
// string[]
function FnNamedArgsSingleStringOptional(myString: string?) -> string {
client GPT35
prompt #"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,3 @@ test TestAudioInput {
}



Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ function FnEnumListOutput(input: string) -> EnumOutput[] {

Answer:
"#
}
}

test FnEnumListOutput {
functions [FnEnumListOutput]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
class Recipe {
// what do comments
ingredients map<string, Quantity>
recipe_type "breakfast" | "dinner"
recipe_type "breakfast" | "dinner"
}

class Quantity {
amount int | float
unit string?
amount int | float
unit string?
}

function AaaSamOutputFormat(recipe: string) -> Recipe {
client GPT35
client GPT35
prompt #"
Return this value back to me: {{recipe}}

Expand Down
22 changes: 11 additions & 11 deletions integ-tests/baml_src/test-files/testing_pipeline/resume.baml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
class Resume {
name string
email string
phone string
experience Education[]
education string[]
skills string[]
name string
email string
phone string
experience Education[]
education string[]
skills string[]
}

class Education {
institution string
location string
degree string
major string[]
graduation_date string?
institution string
location string
degree string
major string[]
graduation_date string?
}

template_string AddRole(foo: string) #"
Expand Down
Loading

0 comments on commit c537d65

Please sign in to comment.