Skip to content

Commit 970a099

Browse files
authoredMay 8, 2024··
fix (ai/core): streamObject fixes partial json with empty objects (#1529)
1 parent 276f22b commit 970a099

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed
 

‎.changeset/few-bobcats-mate.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'ai': patch
3+
---
4+
5+
fix (ai/core): streamObject fixes partial json with empty objects correctly

‎packages/core/core/util/fix-json.test.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -168,9 +168,13 @@ describe('object', () => {
168168
assert.strictEqual(fixJson('{"k1": 1, "k2":'), '{"k1": 1}');
169169
});
170170

171-
test('should handle trailing whitespaces', () => {
171+
test('should handle trailing whitespace', () => {
172172
assert.strictEqual(fixJson('{"key": "value" '), '{"key": "value"}');
173173
});
174+
175+
test('should handle closing after empty object', () => {
176+
assert.strictEqual(fixJson('{"a": {"b": {}'), '{"a": {"b": {}}}');
177+
});
174178
});
175179

176180
describe('nesting', () => {
@@ -265,4 +269,11 @@ describe('regression', () => {
265269
].join('\n'),
266270
);
267271
});
272+
273+
test('should handle empty objects inside nested objects and arrays', () => {
274+
assert.strictEqual(
275+
fixJson(`{"type":"div","children":[{"type":"Card","props":{}`),
276+
`{"type":"div","children":[{"type":"Card","props":{}}]}`,
277+
);
278+
});
268279
});

‎packages/core/core/util/fix-json.ts

+1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ export function fixJson(input: string): string {
140140
break;
141141
}
142142
case '}': {
143+
lastValidIndex = i;
143144
stack.pop();
144145
break;
145146
}

0 commit comments

Comments
 (0)
Please sign in to comment.