Skip to content

Commit

Permalink
feat: update reactjs / nextjs generated types
Browse files Browse the repository at this point in the history
  • Loading branch information
seawatts committed Jan 27, 2025
1 parent cbd5b12 commit ee273b1
Show file tree
Hide file tree
Showing 26 changed files with 6,818 additions and 7,425 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use server'

import { b } from '../index';
import { Check, Checked, RecursivePartialNull } from "../types"
import { ServerAction } from "./types"
import { Image, Audio } from "@boundaryml/baml"
import {
import type { Check, Checked, RecursivePartialNull } from "../types"
import type { ServerAction } from "./types"
import type { Image, Audio } from "@boundaryml/baml"
import type {
{%- for t in types %}{{ t }}{% if !loop.last %}, {% endif %}{% endfor -%}
} from "../types"

Expand All @@ -21,30 +21,28 @@ import {
* - Non-streaming: {{ func.return_type }}
* - Streaming: ReadableStream
*/
export const {{ func.name }}Action: ServerAction<
export const {{ func.name }}Action = async (
{%- for (name, optional, type) in func.args %}
{{ type }}{% if !loop.last %}, {% endif %}
{{ name }}{% if optional %}?{% endif %}: {{ type }},
{%- endfor %}
{%- if func.args|length > 0 %}, {% endif -%}
{{ func.return_type }}
> = async (
): Promise<{{ func.return_type }}> => {
return b.{{ func.name }}(
{%- for (name, _, _) in func.args %}
{{ name }},
{%- endfor %}
);
};

export const {{ func.name }}StreamingAction = async (
{%- for (name, optional, type) in func.args %}
{{ name }}{% if optional %}?{% endif %}: {{ type }},
{%- endfor %}
options?: { stream?: boolean }
): Promise<{{ func.return_type }} | ReadableStream<Uint8Array>> => {
if (options?.stream) {
const stream = b.stream.{{ func.name }}(
{%- for (name, _, _) in func.args %}
{{ name }},
{%- endfor %}
);
return stream.toStreamable();
}
return b.{{ func.name }}(
): Promise<ReadableStream<Uint8Array>> => {
const stream = b.stream.{{ func.name }}(
{%- for (name, _, _) in func.args %}
{{ name }},
{%- endfor %}
);
return stream.toStreamable();
};
{% endfor -%}
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,50 @@ export type FinalResponse<Output> = {
final: Output
}

export type ServerAction<Input, Output> = (...input: Input) => Promise<ReadableStream<Uint8Array> | Output>;

/**
* Configuration for streaming mode, which provides incremental updates.
* Props for streaming mode, which provides incremental updates.
* Use this when you want to show partial results as they become available.
*
* @template Output Type of the incremental response chunks
* @template Action The server action type
*/
export type StreamingInputProps<Output> = {
export type StreamingProps<Action extends ServerAction<any, any> = ServerAction<any, any>> = {
stream: true
onPartial?: (response?: RecursivePartialNull<Output>) => void
onFinal?: (response?: Output) => void
onPartial?: (response?: RecursivePartialNull<Awaited<ReturnType<Action>>>) => void
onFinal?: (response?: Awaited<ReturnType<Action>>) => void
/** Called if the operation fails */
onError?: (error: Error) => void
}

/**
* Options interface for non-streaming mode.
* @template Output The type of the final response data
* Props for non-streaming mode.
* @template Action The server action type
*/
export type NonStreamingInputProps<Output> = {
export type NonStreamingProps<Action extends ServerAction<any, any> = ServerAction<any, any>> = {
stream?: false
onPartial?: never
onFinal?: (response?: Output) => void
onFinal?: (response?: Awaited<ReturnType<Action>>) => void
/** Called if the operation fails */
onError?: (error: Error) => void
}

/**
* Union type of all possible options.
* @template Output The type of the response data
* Union type of all possible props for a BAML hook.
* @template Action The server action type
*/
export type UseLLMOptions<Output> = StreamingInputProps<Output> | NonStreamingInputProps<Output>

export type ServerAction<Input, Output> = {
(input: Input, options: { stream: true }): Promise<ReadableStream<Uint8Array>>;
(input: Input, options?: { stream?: false }): Promise<Output>;
(input: Input, options?: { stream?: boolean }): Promise<Output | ReadableStream<Uint8Array>>;
};
export type BamlHookProps<Action extends ServerAction<any, any> = ServerAction<any, any>> = StreamingProps<Action> | NonStreamingProps<Action>

export type BaseReturnType<Output> = {
/**
* Base return type for all BAML hooks
*/
export type BamlHookResult<Action extends ServerAction<any, any> = ServerAction<any, any>> = {
/**
* The complete, final result of the operation.
* Only available after successful completion (when isSuccess is true).
* Null during loading or if an error occurred.
*/
data?: Output;
data?: Awaited<ReturnType<Action>>;
/**
* Error details if the operation failed.
* Check this when isError is true to handle the failure.
Expand Down Expand Up @@ -95,42 +94,39 @@ export type BaseReturnType<Output> = {
}

/**
* Return type for streaming mode, extends base return type.
* Return type for streaming mode BAML hooks
*/
export type StreamingReturnType<Output, Input extends unknown[]> = BaseReturnType<Output> & {
export type StreamingHookResult<Action extends ServerAction<any, any> = ServerAction<any, any>> = BamlHookResult<Action> & {
/**
* The most recent partial result from the stream.
* Updates continuously while streaming, showing interim progress.
* Use this to implement real-time UI updates, typing effects,
* or progress indicators.
*/
partialData?: RecursivePartialNull<Output> | null;
partialData?: RecursivePartialNull<Awaited<ReturnType<Action>>>;

/**
* Call this function to start the operation.
* Returns a promise that resolves with the final result or null if it failed.
* If the last parameter of Input is the options object, it will be omitted.
*/
mutate: Input extends [...infer Rest, { stream?: true }]
? (...args: Rest) => Promise<ReadableStream<Uint8Array>>
: (...args: Input) => Promise<ReadableStream<Uint8Array>>;
mutate: (input: Parameters<Action>[0]) => Promise<ReadableStream<Uint8Array>>;
};

/**
* Return type for non-streaming mode, extends base return type.
* Return type for non-streaming mode BAML hooks
*/
export type NonStreamingReturnType<Output, Input extends unknown[]> = BaseReturnType<Output> & {
export type NonStreamingHookResult<Action extends ServerAction<any, any> = ServerAction<any, any>> = BamlHookResult<Action> & {
/** Not available in non-streaming mode */
partialData?: never;
mutate: Input extends [...infer Rest, { stream?: false }]
? (...args: Rest) => Promise<Output>
: (...args: Input) => Promise<Output>;
mutate: (input: Parameters<Action>[0]) => Promise<Awaited<ReturnType<Action>>>;
};

/**
* Conditional return type based on the options provided.
* Conditional return type for BAML hooks based on the provided props
*/
export type UseLLMReturnType<Output, Input extends unknown[], TOptions extends UseLLMOptions<Output>> =
TOptions extends { stream: true }
? StreamingReturnType<Output, Input>
: NonStreamingReturnType<Output, Input>;
export type BamlHookResult<
Action extends ServerAction<any, any> = ServerAction<any, any>,
Props extends BamlHookProps<Action> = BamlHookProps<Action>
> = Props extends { stream: true }
? StreamingHookResult<Action>
: NonStreamingHookResult<Action>;
16 changes: 8 additions & 8 deletions integ-tests/baml_src/generators.baml
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
generator lang_python {
output_type python/pydantic
output_dir "../python"
version "0.72.1"
version "0.73.4"
}

generator lang_typescript {
output_type typescript
output_dir "../typescript"
version "0.72.1"
version "0.73.4"
}

generator lang_ruby {
output_type ruby/sorbet
output_dir "../ruby"
version "0.72.1"
version "0.73.4"
}

generator lang_typescript_react {
output_type typescript/react
output_dir "../react"
version "0.72.1"
}
// generator lang_typescript_react {
// output_type typescript/react
// output_dir "../react"
// version "0.72.1"
// }
// generator openapi {
// output_type rest/openapi
// output_dir "../openapi"
Expand Down
53 changes: 53 additions & 0 deletions integ-tests/python/baml_client/async_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -2051,6 +2051,29 @@ async def ReturnMalformedConstraints(
)
return cast(types.MalformedConstraints, raw.cast_to(types, types))

async def ScheduleMeeting(
self,
request: types.MeetingRequest,
baml_options: BamlCallOptions = {},
) -> types.MeetingDetails:
__tb__ = baml_options.get("tb", None)
if __tb__ is not None:
tb = __tb__._tb # type: ignore (we know how to use this private attribute)
else:
tb = None
__cr__ = baml_options.get("client_registry", None)

raw = await self.__runtime.call_function(
"ScheduleMeeting",
{
"request": request,
},
self.__ctx_manager.get(),
tb,
__cr__,
)
return cast(types.MeetingDetails, raw.cast_to(types, types))

async def SchemaDescriptions(
self,
input: str,
Expand Down Expand Up @@ -5873,6 +5896,36 @@ def ReturnMalformedConstraints(
self.__ctx_manager.get(),
)

def ScheduleMeeting(
self,
request: types.MeetingRequest,
baml_options: BamlCallOptions = {},
) -> baml_py.BamlStream[partial_types.MeetingDetails, types.MeetingDetails]:
__tb__ = baml_options.get("tb", None)
if __tb__ is not None:
tb = __tb__._tb # type: ignore (we know how to use this private attribute)
else:
tb = None
__cr__ = baml_options.get("client_registry", None)

raw = self.__runtime.stream_function(
"ScheduleMeeting",
{
"request": request,
},
None,
self.__ctx_manager.get(),
tb,
__cr__,
)

return baml_py.BamlStream[partial_types.MeetingDetails, types.MeetingDetails](
raw,
lambda x: cast(partial_types.MeetingDetails, x.cast_to(types, partial_types)),
lambda x: cast(types.MeetingDetails, x.cast_to(types, types)),
self.__ctx_manager.get(),
)

def SchemaDescriptions(
self,
input: str,
Expand Down
Loading

0 comments on commit ee273b1

Please sign in to comment.