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 083d7db
Show file tree
Hide file tree
Showing 180 changed files with 50,707 additions and 3,796 deletions.
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
32 changes: 32 additions & 0 deletions engine/language_client_codegen/baml_src/baml_client/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
###############################################################################
#
# Welcome to Baml! To use this generated code, please run the following:
#
# $ pip install baml
#
###############################################################################

# This file was generated by BAML: please do not edit it. Instead, edit the
# BAML files and re-generate this code.
#
# ruff: noqa: E501,F401
# flake8: noqa: E501,F401
# pylint: disable=unused-import,line-too-long
# fmt: off
from . import types
from . import tracing
from . import partial_types
from .globals import reset_baml_env_vars


from .async_client import b



__all__ = [
"b",
"partial_types",
"tracing",
"types",
"reset_baml_env_vars",
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
###############################################################################
#
# Welcome to Baml! To use this generated code, please run the following:
#
# $ pip install baml
#
###############################################################################

# This file was generated by BAML: please do not edit it. Instead, edit the
# BAML files and re-generate this code.
#
# ruff: noqa: E501,F401
# flake8: noqa: E501,F401
# pylint: disable=unused-import,line-too-long
# fmt: off
from typing import Any, Dict, List, Optional, TypeVar, Union, TypedDict, Type, Literal, cast
from typing_extensions import NotRequired
import pprint

import baml_py
from pydantic import BaseModel, ValidationError, create_model

from . import partial_types, types
from .types import Checked, Check
from .type_builder import TypeBuilder
from .globals import DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME


OutputType = TypeVar('OutputType')


# Define the TypedDict with optional parameters having default values
class BamlCallOptions(TypedDict, total=False):
tb: NotRequired[TypeBuilder]
client_registry: NotRequired[baml_py.baml_py.ClientRegistry]

class BamlAsyncClient:
__runtime: baml_py.BamlRuntime
__ctx_manager: baml_py.BamlCtxManager
__stream_client: "BamlStreamClient"

def __init__(self, runtime: baml_py.BamlRuntime, ctx_manager: baml_py.BamlCtxManager):
self.__runtime = runtime
self.__ctx_manager = ctx_manager
self.__stream_client = BamlStreamClient(self.__runtime, self.__ctx_manager)

@property
def stream(self):
return self.__stream_client





class BamlStreamClient:
__runtime: baml_py.BamlRuntime
__ctx_manager: baml_py.BamlCtxManager

def __init__(self, runtime: baml_py.BamlRuntime, ctx_manager: baml_py.BamlCtxManager):
self.__runtime = runtime
self.__ctx_manager = ctx_manager



b = BamlAsyncClient(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX)

__all__ = ["b"]
41 changes: 41 additions & 0 deletions engine/language_client_codegen/baml_src/baml_client/globals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
###############################################################################
#
# Welcome to Baml! To use this generated code, please run the following:
#
# $ pip install baml
#
###############################################################################

# This file was generated by BAML: please do not edit it. Instead, edit the
# BAML files and re-generate this code.
#
# ruff: noqa: E501,F401
# flake8: noqa: E501,F401
# pylint: disable=unused-import,line-too-long
# fmt: off
import os

from baml_py import BamlCtxManager, BamlRuntime
from baml_py.baml_py import BamlError
from .inlinedbaml import get_baml_files
from typing import Dict

DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME = BamlRuntime.from_files(
"baml_src",
get_baml_files(),
os.environ.copy()
)
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX = BamlCtxManager(DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME)

def reset_baml_env_vars(env_vars: Dict[str, str]):
if DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.allow_reset():
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME.reset(
"baml_src",
get_baml_files(),
env_vars
)
DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX.reset()
else:
raise BamlError("Cannot reset BAML environment variables while there are active BAML contexts.")

__all__ = []
22 changes: 22 additions & 0 deletions engine/language_client_codegen/baml_src/baml_client/inlinedbaml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
###############################################################################
#
# Welcome to Baml! To use this generated code, please run the following:
#
# $ pip install baml
#
###############################################################################

# This file was generated by BAML: please do not edit it. Instead, edit the
# BAML files and re-generate this code.
#
# ruff: noqa: E501,F401
# flake8: noqa: E501,F401
# pylint: disable=unused-import,line-too-long
# fmt: off

file_map = {

}

def get_baml_files():
return file_map
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
###############################################################################
#
# Welcome to Baml! To use this generated code, please run the following:
#
# $ pip install baml
#
###############################################################################

# This file was generated by BAML: please do not edit it. Instead, edit the
# BAML files and re-generate this code.
#
# ruff: noqa: E501,F401
# flake8: noqa: E501,F401
# pylint: disable=unused-import,line-too-long
# fmt: off
import baml_py
from enum import Enum
from pydantic import BaseModel, ConfigDict
from typing import Dict, Generic, List, Optional, TypeVar, Union, Literal

from . import types
from .types import Checked, Check

###############################################################################
#
# These types are used for streaming, for when an instance of a type
# is still being built up and any of its fields is not yet fully available.
#
###############################################################################

T = TypeVar('T')
class StreamState(BaseModel, Generic[T]):
value: T
state: Literal["Pending", "Incomplete", "Complete"]


class Foo(BaseModel):
s: Optional[str] = None

class Greg(BaseModel):
inner: StreamState[Checked[Optional["Foo"],Literal["foo"]]]
Loading

0 comments on commit 083d7db

Please sign in to comment.