Skip to content

Commit

Permalink
js.t2 to ts.t2
Browse files Browse the repository at this point in the history
  • Loading branch information
sxlijin committed Jun 29, 2024
1 parent f9250cb commit 07eac56
Show file tree
Hide file tree
Showing 3 changed files with 167 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { BamlRuntime, FunctionResult, BamlCtxManager, BamlStream, Image } from "@boundaryml/baml"
import {
{%- for t in types %}{{ t }}{% if !loop.last %}, {% endif %}{% endfor -%}
} from "./types"
import TypeBuilder from "./type_builder"

export type RecursivePartialNull<T> = T extends object
? {
[P in keyof T]?: RecursivePartialNull<T[P]>;
}
: T | null;

export class BamlClient {
private runtime: BamlRuntime
private ctx_manager: BamlCtxManager
private stream_client: BamlStreamClient

constructor(runtime: BamlRuntime, ctx_manager: BamlCtxManager) {
this.runtime = runtime
this.ctx_manager = ctx_manager
this.stream_client = new BamlStreamClient(runtime, ctx_manager)
}

get stream() {
return this.stream_client
}

{% for fn in funcs %}
async {{ fn.name }}(
{% for (name, optional, type) in fn.args -%}
{{name}}{% if optional %}?{% endif %}: {{type}},
{%- endfor %}
__baml_options__?: { tb?: TypeBuilder }
): Promise<{{fn.return_type}}> {
const raw = await this.runtime.callFunction(
"{{fn.name}}",
{
{% for (name, optional, type) in fn.args -%}
"{{name}}": {{name}}{% if optional %}?? null{% endif %}{% if !loop.last %},{% endif %}
{%- endfor %}
},
this.ctx_manager.get(),
__baml_options__?.tb?.__tb(),
)
return raw.parsed() as {{fn.return_type}}
}
{% endfor %}
}

class BamlStreamClient {
constructor(private runtime: BamlRuntime, private ctx_manager: BamlCtxManager) {}

{% for fn in funcs %}
{{ fn.name }}(
{% for (name, optional, type) in fn.args -%}
{{name}}{% if optional %}?{% endif %}: {{type}},
{%- endfor %}
__baml_options__?: { tb?: TypeBuilder }
): BamlStream<RecursivePartialNull<{{ fn.return_type }}>, {{ fn.return_type }}> {
const raw = this.runtime.streamFunction(
"{{fn.name}}",
{
{% for (name, optional, type) in fn.args -%}
"{{name}}": {{name}}{% if optional %} ?? null{% endif %}{% if !loop.last %},{% endif %}
{%- endfor %}
},
undefined,
this.ctx_manager.get(),
__baml_options__?.tb?.__tb(),
)
return new BamlStream<RecursivePartialNull<{{ fn.return_type }}>, {{ fn.return_type }}>(
raw,
(a): a is RecursivePartialNull<{{ fn.return_type }}> => a,
(a): a is {{ fn.return_type }} => a,
this.ctx_manager.get(),
__baml_options__?.tb?.__tb(),
)
}
{% endfor %}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import { FieldType } from '@boundaryml/baml/native'
import { TypeBuilder as _TypeBuilder, EnumBuilder, ClassBuilder } from '@boundaryml/baml/type_builder'

export default class TypeBuilder {
private tb: _TypeBuilder;
{% for cls in classes %}{% if cls.dynamic %}
{{cls.name}}: ClassBuilder<'{{cls.name}}'
{%- for (name, _, _) in cls.fields %}{% if loop.first %}, {%endif%}"{{name}}"{% if !loop.last %} | {% endif %}{% endfor -%}
>;
{% endif %}{% endfor %}
{% for enum in enums %}{% if enum.dynamic %}
{{enum.name}}: EnumBuilder<'{{enum.name}}'{%- for value in enum.values %}{% if loop.first %}, {%endif%}"{{value}}"{% if !loop.last %} | {% endif %}{% endfor -%}>;
{% endif %}{% endfor %}

constructor() {
this.tb = new _TypeBuilder({
classes: new Set([
{% for cls in classes %}"{{cls.name}}",{% endfor %}
]),
enums: new Set([
{% for enum in enums %}"{{enum.name}}",{% endfor %}
])
});
{% for cls in classes %}{% if cls.dynamic %}
this.{{cls.name}} = this.tb.classBuilder("{{cls.name}}", [
{% for (name, _, _) in cls.fields %}"{{name}}",{% endfor %}
]);
{% endif %}{% endfor %}
{% for enum in enums %}{% if enum.dynamic %}
this.{{enum.name}} = this.tb.enumBuilder("{{enum.name}}", [
{% for value in enum.values %}"{{value}}",{% endfor %}
]);
{% endif %}{% endfor %}
}

__tb() {
return this.tb._tb();
}

string(): FieldType {
return this.tb.string()
}

int(): FieldType {
return this.tb.int()
}

float(): FieldType {
return this.tb.float()
}

bool(): FieldType {
return this.tb.bool()
}

list(type: FieldType): FieldType {
return this.tb.list(type)
}

addClass<Name extends string>(name: Name): ClassBuilder<Name> {
return this.tb.addClass(name);
}

addEnum<Name extends string>(name: Name): EnumBuilder<Name> {
return this.tb.addEnum(name);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { Image } from "@boundaryml/baml"

{%- for enum in enums %}
export enum {{enum.name}} {
{%- for value in enum.values %}
{{ value }} = "{{ value }}",
{%- endfor %}
}
{% endfor %}

{%- for cls in classes %}
export interface {{cls.name}} {
{%- for (name, optional, type) in cls.fields %}
{{name}}{% if optional %}?{% endif %}: {{type}}
{%- endfor %}
{% if cls.dynamic %}
[key: string]: any;
{%- endif %}
}
{% endfor %}

0 comments on commit 07eac56

Please sign in to comment.