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

NextJS Codegen #1346

Open
wants to merge 12 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion .github/workflows/build-typescript-release.reusable.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ jobs:
# NOTE: we can't use mise here because it doesn't support Windows
- uses: pnpm/action-setup@v4
with:
version: 9.0.6
version: 9.12.0
run_install: false
- uses: actions/setup-node@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/primary.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
with:
version: 9.0.6
version: 9.12.0
run_install: false
- uses: actions/setup-node@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ jobs:
- name: setup pnpm
uses: pnpm/action-setup@v4
with:
version: 9.0.6
version: 9.12.0
package_json_file: engine/language_client_typescript/package.json
run_install: false

Expand Down
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
1 change: 1 addition & 0 deletions engine/baml-schema-wasm/web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "0.1.0",
"description": "The WASM package for baml-fmt",
"main": "./dist/src/baml_schema_build.js",
"packageManager": "[email protected]",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
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
Loading