From 141f0ceaede05806dcff69e95f2024c1027b21ad Mon Sep 17 00:00:00 2001 From: Lars Grammel Date: Tue, 20 Feb 2024 18:38:45 +0100 Subject: [PATCH] Add test and changeset for onFinal callback fix (#985) --- .changeset/ten-sloths-perform.md | 5 +++++ packages/core/streams/openai-stream.test.ts | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) create mode 100644 .changeset/ten-sloths-perform.md diff --git a/.changeset/ten-sloths-perform.md b/.changeset/ten-sloths-perform.md new file mode 100644 index 000000000000..95cbc078aede --- /dev/null +++ b/.changeset/ten-sloths-perform.md @@ -0,0 +1,5 @@ +--- +'ai': patch +--- + +Fix: onFinal callback is invoked with text from onToolCall when onToolCall returns string diff --git a/packages/core/streams/openai-stream.test.ts b/packages/core/streams/openai-stream.test.ts index 00290fb3e008..7a196fc7fcc4 100644 --- a/packages/core/streams/openai-stream.test.ts +++ b/packages/core/streams/openai-stream.test.ts @@ -353,6 +353,25 @@ describe('OpenAIStream', () => { ], }); }); + + it('should call onFinal with tool response when onToolCall returns string', async () => { + let finalResponse: any = undefined; + + const stream = OpenAIStream(await fetch(TOOL_CALL_TEST_URL), { + async experimental_onToolCall(payload, appendToolCallMessage) { + return 'tool-response'; + }, + + onFinal(response) { + finalResponse = response; + }, + }); + + const response = new StreamingTextResponse(stream); + await createClient(response).readAll(); // consume stream + + expect(finalResponse).toEqual('tool-response'); + }); }); describe('Azure SDK', () => {