Skip to content

Commit

Permalink
Checked in baml client typescript (#1135)
Browse files Browse the repository at this point in the history
<!-- ELLIPSIS_HIDDEN -->


> [!IMPORTANT]
> Introduces `Checked` and `Check` interfaces to TypeScript BAML client,
consolidating type definitions and updating imports, with mostly
successful test results.
> 
>   - **TypeScript Client**:
>     - Added `Checked` and `Check` interfaces to `types.ts.j2`.
>     - Removed `Checked` and `Check` from `checked.ts`.
> - Updated imports in `async_client.ts.j2` and `sync_client.ts.j2` to
use `Checked` and `Check` from `types`.
>   - **Files Removed**:
>     - Removed contents of `checked.d.ts` and `checked.js`.
>   - **Testing**:
> - Test report shows 60 passed tests and 1 failed test, unrelated to
`Checked` functionality.
> 
> <sup>This description was created by </sup>[<img alt="Ellipsis"
src="https://img.shields.io/badge/Ellipsis-blue?color=175173">](https://www.ellipsis.dev?ref=BoundaryML%2Fbaml&utm_source=github&utm_medium=referral)<sup>
for de87455. It will automatically
update as commits are pushed.</sup>


<!-- ELLIPSIS_HIDDEN -->
  • Loading branch information
imalsogreg authored Nov 4, 2024
1 parent 823f74c commit ad759cd
Show file tree
Hide file tree
Showing 15 changed files with 48 additions and 82 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BamlRuntime, FunctionResult, BamlCtxManager, BamlStream, Image, ClientRegistry, BamlValidationError, createBamlValidationError } from "@boundaryml/baml"
import { Checked, Check } from "./types"
import {
{%- for t in types %}{{ t }}{% if !loop.last %}, {% endif %}{% endfor -%}
} from "./types"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BamlRuntime, FunctionResult, BamlCtxManager, BamlSyncStream, Image, ClientRegistry, createBamlValidationError, BamlValidationError } from "@boundaryml/baml"
import {
import { Checked, Check } from "./types"
import {
{%- for t in types %}{{ t }}{% if !loop.last %}, {% endif %}{% endfor -%}
} from "./types"
import TypeBuilder from "./type_builder"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,24 @@
import { Image } from "@boundaryml/baml"

export interface Checked<T,CheckName extends string = string> {
value: T,
checks: Record<CheckName, Check>,
}

export interface Check {
name: string,
expr: string
status: "succeeded" | "failed"
}

export function all_succeeded<CheckName extends string>(checks: Record<CheckName, Check>): boolean {
return get_checks(checks).every(check => check.status === "succeeded")
}

export function get_checks<CheckName extends string>(checks: Record<CheckName, Check>): Check[] {
return Object.values(checks)
}

{%- for enum in enums %}
export enum {{enum.name}} {
{%- for value in enum.values %}
Expand Down
12 changes: 0 additions & 12 deletions engine/language_client_typescript/checked.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion engine/language_client_typescript/checked.d.ts.map

This file was deleted.

11 changes: 0 additions & 11 deletions engine/language_client_typescript/checked.js

This file was deleted.

1 change: 0 additions & 1 deletion engine/language_client_typescript/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export { BamlRuntime, FunctionResult, FunctionResultStream, BamlImage as Image, ClientBuilder, BamlAudio as Audio, invoke_runtime_cli, ClientRegistry, BamlLogEvent, } from './native';
export { BamlStream } from './stream';
export { BamlCtxManager } from './async_context_vars';
export { Checked } from './checked';
export declare class BamlValidationError extends Error {
prompt: string;
raw_output: string;
Expand Down
2 changes: 1 addition & 1 deletion engine/language_client_typescript/index.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 0 additions & 18 deletions engine/language_client_typescript/typescript_src/checked.ts

This file was deleted.

1 change: 0 additions & 1 deletion engine/language_client_typescript/typescript_src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ export {
} from './native'
export { BamlStream } from './stream'
export { BamlCtxManager } from './async_context_vars'
export { Checked } from './checked'

export class BamlValidationError extends Error {
prompt: string
Expand Down
3 changes: 1 addition & 2 deletions fern/01-guide/05-baml-advanced/validations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,8 @@ def main():
```

```typescript Typescript
import { b } from './baml_client'
import { b, get_checks } from './baml_client'
import { Citation } from './baml_client/types'
import { get_checks } from '@boundaryml/baml/checked'

const main = () => {
const citation = b.GetCitation("SpaceX, is an American spacecraft manufacturer, launch service provider...")
Expand Down
1 change: 1 addition & 0 deletions integ-tests/typescript/baml_client/async_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $ pnpm add @boundaryml/baml
// @ts-nocheck
// biome-ignore format: autogenerated code
import { BamlRuntime, FunctionResult, BamlCtxManager, BamlStream, Image, ClientRegistry, BamlValidationError, createBamlValidationError } from "@boundaryml/baml"
import { Checked, Check } from "./types"
import {BigNumbers, Blah, BlockConstraint, BlockConstraintForParam, BookOrder, ClassOptionalOutput, ClassOptionalOutput2, ClassWithImage, CompoundBigNumbers, ContactInfo, CustomTaskResult, DummyOutput, DynInputOutput, DynamicClassOne, DynamicClassTwo, DynamicOutput, Earthling, Education, Email, EmailAddress, Event, FakeImage, FlightConfirmation, FooAny, GroceryReceipt, InnerClass, InnerClass2, InputClass, InputClassNested, LiteralClassHello, LiteralClassOne, LiteralClassTwo, MalformedConstraints, MalformedConstraints2, Martian, NamedArgsSingleClass, Nested, Nested2, NestedBlockConstraint, NestedBlockConstraintForParam, OptionalTest_Prop1, OptionalTest_ReturnType, OrderInfo, OriginalA, OriginalB, Person, PhoneNumber, Quantity, RaysData, ReceiptInfo, ReceiptItem, Recipe, Resume, Schema, SearchParams, SomeClassNestedDynamic, StringToClassEntry, TestClassAlias, TestClassNested, TestClassWithEnum, TestOutputClass, TwoStoriesOneTitle, UnionTest_ReturnType, WithReasoning, AliasedEnum, Category, Category2, Category3, Color, DataType, DynEnumOne, DynEnumTwo, EnumInClass, EnumOutput, Hobby, NamedArgsSingleEnum, NamedArgsSingleEnumList, OptionalTest_CategoryType, OrderStatus, Tag, TestEnum} from "./types"
import TypeBuilder from "./type_builder"
import { DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME } from "./globals"
Expand Down
1 change: 1 addition & 0 deletions integ-tests/typescript/baml_client/sync_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ $ pnpm add @boundaryml/baml
// @ts-nocheck
// biome-ignore format: autogenerated code
import { BamlRuntime, FunctionResult, BamlCtxManager, BamlSyncStream, Image, ClientRegistry, createBamlValidationError, BamlValidationError } from "@boundaryml/baml"
import { Checked, Check } from "./types"
import {BigNumbers, Blah, BlockConstraint, BlockConstraintForParam, BookOrder, ClassOptionalOutput, ClassOptionalOutput2, ClassWithImage, CompoundBigNumbers, ContactInfo, CustomTaskResult, DummyOutput, DynInputOutput, DynamicClassOne, DynamicClassTwo, DynamicOutput, Earthling, Education, Email, EmailAddress, Event, FakeImage, FlightConfirmation, FooAny, GroceryReceipt, InnerClass, InnerClass2, InputClass, InputClassNested, LiteralClassHello, LiteralClassOne, LiteralClassTwo, MalformedConstraints, MalformedConstraints2, Martian, NamedArgsSingleClass, Nested, Nested2, NestedBlockConstraint, NestedBlockConstraintForParam, OptionalTest_Prop1, OptionalTest_ReturnType, OrderInfo, OriginalA, OriginalB, Person, PhoneNumber, Quantity, RaysData, ReceiptInfo, ReceiptItem, Recipe, Resume, Schema, SearchParams, SomeClassNestedDynamic, StringToClassEntry, TestClassAlias, TestClassNested, TestClassWithEnum, TestOutputClass, TwoStoriesOneTitle, UnionTest_ReturnType, WithReasoning, AliasedEnum, Category, Category2, Category3, Color, DataType, DynEnumOne, DynEnumTwo, EnumInClass, EnumOutput, Hobby, NamedArgsSingleEnum, NamedArgsSingleEnumList, OptionalTest_CategoryType, OrderStatus, Tag, TestEnum} from "./types"
import TypeBuilder from "./type_builder"
import { DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_CTX, DO_NOT_USE_DIRECTLY_UNLESS_YOU_KNOW_WHAT_YOURE_DOING_RUNTIME } from "./globals"
Expand Down
19 changes: 19 additions & 0 deletions integ-tests/typescript/baml_client/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,25 @@ $ pnpm add @boundaryml/baml
// @ts-nocheck
// biome-ignore format: autogenerated code
import { Image } from "@boundaryml/baml"

export interface Checked<T,CheckName extends string = string> {
value: T,
checks: Record<CheckName, Check>,
}

export interface Check {
name: string,
expr: string
status: "succeeded" | "failed"
}

export function all_succeeded<CheckName extends string>(checks: Record<CheckName, Check>): boolean {
return get_checks(checks).every(check => check.status === "succeeded")
}

export function get_checks<CheckName extends string>(checks: Record<CheckName, Check>): Check[] {
return Object.values(checks)
}
export enum AliasedEnum {
KEY_ONE = "KEY_ONE",
KEY_TWO = "KEY_TWO",
Expand Down
Loading

0 comments on commit ad759cd

Please sign in to comment.