Skip to content

Commit 0a7b396

Browse files
authored
Fix regression in ts streaming codegen (#2533)
1 parent 391e4b2 commit 0a7b396

76 files changed

Lines changed: 3476 additions & 449 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎engine/baml-lib/baml-types/src/ir_type/mod.rs‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,4 +1647,38 @@ mod tests {
16471647
);
16481648
assert_eq!(alias.to_streaming_type(&TestLookup), expected);
16491649
}
1650+
1651+
#[test]
1652+
fn partialize_mixed_done_union() {
1653+
let mut done_variant = TypeIR::class("FooDone");
1654+
done_variant.meta_mut().streaming_behavior.done = true;
1655+
1656+
let streamable_variant = TypeIR::class("MessageToUser");
1657+
let union = TypeIR::Union(
1658+
unsafe { UnionTypeGeneric::new_unsafe(vec![done_variant, streamable_variant]) },
1659+
Default::default(),
1660+
);
1661+
let streaming_type = union.to_streaming_type(&TestLookup);
1662+
let streaming_type_variants: Vec<TypeStreaming> = match streaming_type {
1663+
TypeStreaming::Union(union, _) => union.view().flatten(),
1664+
_ => panic!("Expected union"),
1665+
};
1666+
assert_eq!(streaming_type_variants.len(), 3);
1667+
1668+
let mut expected_first_variant = TypeStreaming::class("FooDone");
1669+
expected_first_variant.meta_mut().streaming_behavior.done = true;
1670+
1671+
let expected_second_variant = TypeStreaming::Class {
1672+
name: "MessageToUser".to_string(),
1673+
mode: StreamingMode::Streaming,
1674+
dynamic: false,
1675+
meta: Default::default(),
1676+
};
1677+
1678+
dbg!(&streaming_type_variants[0]);
1679+
dbg!(&streaming_type_variants[1]);
1680+
dbg!(&streaming_type_variants[2]);
1681+
assert_eq!(streaming_type_variants[0], expected_first_variant);
1682+
assert_eq!(streaming_type_variants[1], expected_second_variant);
1683+
}
16501684
}

‎engine/generators/languages/typescript/src/type.rs‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,16 @@ impl SerializeType for TypeTS {
260260
TypeTS::TypeAlias { package, name, .. } => {
261261
format!("{}{}", package.relative_from(pkg), name)
262262
}
263-
TypeTS::Union { .. } => self.default_name_within_union(),
263+
TypeTS::Union { variants, .. } => {
264+
let mut parts: Vec<String> = Vec::with_capacity(variants.len());
265+
for variant in variants.iter() {
266+
let rendered = variant.serialize_type(pkg);
267+
if !parts.contains(&rendered) {
268+
parts.push(rendered);
269+
}
270+
}
271+
parts.join(" | ")
272+
}
264273
TypeTS::Enum {
265274
package,
266275
name,
@@ -274,7 +283,7 @@ impl SerializeType for TypeTS {
274283
}
275284
}
276285
TypeTS::List(inner, _) => match &**inner {
277-
TypeTS::Union { .. } => format!("({})[]", inner.default_name_within_union()),
286+
TypeTS::Union { .. } => format!("({})[]", inner.serialize_type(pkg)),
278287
_ => {
279288
if inner.meta().is_optional() {
280289
format!("({})[]", inner.serialize_type(pkg))

‎flake.nix‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@
4646
buildInputs = (with pkgs; [
4747
cmake
4848
git
49-
go
50-
gotools
49+
go
50+
gotools
5151
mise
5252
openssl
5353
pkg-config
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class AddTodoItem {
2+
type "add_todo_item" @stream.not_null
3+
item string
4+
time string
5+
description string @description("20 word description of the item")
6+
@@stream.done
7+
}
8+
9+
class TodoMessageToUser {
10+
type "todo_message_to_user" @stream.not_null
11+
message string @description("A message to the user, about 50 words long")
12+
}
13+
14+
type TodoTool = AddTodoItem | TodoMessageToUser
15+
16+
function ChooseTodoTools(query: string) -> TodoTool[] {
17+
client GPT4
18+
prompt #"
19+
Choose tools to satisfy the user query.
20+
For example, if they ask for "5 todo items for learning chess",
21+
return a list of 5 "add_todo_item" objects and single "todo_message_to_user"
22+
object. All requests should end with a "todo_message_to_user" object.
23+
24+
{{ ctx.output_format }}
25+
"#
26+
}
27+
28+
test ChooseTodoTools {
29+
functions [ChooseTodoTools]
30+
args {
31+
query "5 todo items for learning chess"
32+
}
33+
}

‎integ-tests/go/baml_client/baml_source_map.go‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎integ-tests/go/baml_client/functions.go‎

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎integ-tests/go/baml_client/functions_parse.go‎

Lines changed: 47 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)