Skip to content

Commit

Permalink
feat: generation for react working
Browse files Browse the repository at this point in the history
feat: added nextjs-plugin package for quick next.js config setup

feat: add codelens for react
  • Loading branch information
seawatts committed Feb 5, 2025
1 parent 9c068e0 commit f2dbc25
Show file tree
Hide file tree
Showing 152 changed files with 51,765 additions and 1,704 deletions.
6 changes: 5 additions & 1 deletion engine/baml-lib/baml-types/src/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ pub enum GeneratorOutputType {
#[strum(serialize = "typescript")]
Typescript,

#[strum(serialize = "typescript/react")]
TypescriptReact,

#[strum(serialize = "ruby/sorbet")]
RubySorbet,
}
Expand All @@ -38,6 +41,7 @@ impl GeneratorOutputType {
// DO NOT CHANGE THIS DEFAULT EVER OR YOU WILL BREAK EXISTING USERS
Self::PythonPydantic => GeneratorDefaultClientMode::Async,
Self::Typescript => GeneratorDefaultClientMode::Async,
Self::TypescriptReact => GeneratorDefaultClientMode::Async,
Self::RubySorbet => GeneratorDefaultClientMode::Sync,
}
}
Expand All @@ -48,6 +52,7 @@ impl GeneratorOutputType {
Self::OpenApi => GeneratorDefaultClientMode::Sync,
Self::PythonPydantic => GeneratorDefaultClientMode::Sync,
Self::Typescript => GeneratorDefaultClientMode::Async,
Self::TypescriptReact => GeneratorDefaultClientMode::Async,
Self::RubySorbet => GeneratorDefaultClientMode::Sync,
}
}
Expand All @@ -56,7 +61,6 @@ impl GeneratorOutputType {
impl clap::ValueEnum for GeneratorOutputType {
fn value_variants<'a>() -> &'a [Self] {
use strum::VariantArray;

Self::VARIANTS
}

Expand Down
4 changes: 4 additions & 0 deletions engine/baml-runtime/src/cli/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ impl GenerateArgs {
// this has no meaning
GeneratorDefaultClientMode::Sync
}
internal_baml_core::configuration::GeneratorOutputType::TypescriptReact => {
GeneratorDefaultClientMode::Async
}
};
// Normally `baml_client` is added via the generator, but since we're not running the generator, we need to add it manually.
let output_dir_relative_to_baml_src = PathBuf::from("..");
Expand All @@ -77,6 +80,7 @@ impl GenerateArgs {
default_client_mode,
// TODO: this should be set if user is asking for openapi
vec![],
None,
)
.context("Failed while resolving .baml paths in baml_src/")?,
)
Expand Down
4 changes: 3 additions & 1 deletion engine/baml-runtime/src/cli/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ impl InitArgs {
Some(s) => format!("{} clients via OpenAPI", s),
None => "REST clients".to_string(),
},
GeneratorOutputType::TypescriptReact => "TypeScript React clients".to_string(),
}
);
log::info!(
Expand All @@ -100,6 +101,7 @@ impl InitArgs {
GeneratorOutputType::Typescript => "typescript",
GeneratorOutputType::RubySorbet => "ruby",
GeneratorOutputType::OpenApi => "openapi",
GeneratorOutputType::TypescriptReact => "typescript-react",
}
);

Expand All @@ -114,7 +116,7 @@ fn generate_main_baml_content(
) -> String {
let default_client_mode = match output_type {
GeneratorOutputType::OpenApi | GeneratorOutputType::RubySorbet => "".to_string(),
GeneratorOutputType::PythonPydantic | GeneratorOutputType::Typescript => format!(
GeneratorOutputType::PythonPydantic | GeneratorOutputType::Typescript | GeneratorOutputType::TypescriptReact => format!(
r#"
// Valid values: "sync", "async"
// This controls what `b.FunctionName()` will be (sync or async).
Expand Down
5 changes: 3 additions & 2 deletions engine/baml-runtime/src/cli/serve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use axum_extra::{
headers::{self, authorization::Basic, Authorization, Header},
TypedHeader,
};
use baml_types::{BamlValue, GeneratorDefaultClientMode};
use baml_types::{BamlValue, GeneratorDefaultClientMode, GeneratorOutputType};
use core::pin::Pin;
use futures::Stream;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -67,7 +67,7 @@ impl ServeArgs {
if !self.preview {
log::warn!(
r#"BAML-over-HTTP API is a preview feature.
Please run with --preview, like so:
{} serve --preview
Expand Down Expand Up @@ -623,6 +623,7 @@ Tip: test that the server is up using `curl http://localhost:{}/_debug/ping`
true,
GeneratorDefaultClientMode::Sync,
Vec::new(),
None,
)
.map_err(|_| BamlError::InternalError {
message: "Failed to make placeholder generator".to_string(),
Expand Down
1 change: 1 addition & 0 deletions engine/baml-runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,7 @@ impl BamlRuntime {
no_version_check,
generator.default_client_mode(),
generator.on_generate.clone(),
Some(generator.output_type),
)?,
))
})
Expand Down
6 changes: 6 additions & 0 deletions engine/language_client_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ pub struct GeneratorArgs {
// Default call mode for functions
default_client_mode: GeneratorDefaultClientMode,
on_generate: Vec<String>,

// The type of client to generate
client_type: Option<GeneratorOutputType>,
}

fn relative_path_to_baml_src(path: &Path, baml_src: &Path) -> Result<PathBuf> {
Expand All @@ -54,6 +57,7 @@ impl GeneratorArgs {
no_version_check: bool,
default_client_mode: GeneratorDefaultClientMode,
on_generate: Vec<String>,
client_type: Option<GeneratorOutputType>,
) -> Result<Self> {
let baml_src = baml_src_dir.into();
let input_file_map: BTreeMap<PathBuf, String> = input_files
Expand All @@ -70,6 +74,7 @@ impl GeneratorArgs {
no_version_check,
default_client_mode,
on_generate,
client_type,
})
}

Expand Down Expand Up @@ -185,6 +190,7 @@ impl GenerateClient for GeneratorOutputType {
GeneratorOutputType::PythonPydantic => python::generate(ir, gen),
GeneratorOutputType::RubySorbet => ruby::generate(ir, gen),
GeneratorOutputType::Typescript => typescript::generate(ir, gen),
GeneratorOutputType::TypescriptReact => typescript::generate(ir, gen),
}?;

#[cfg(not(target_arch = "wasm32"))]
Expand Down
Loading

0 comments on commit f2dbc25

Please sign in to comment.