Skip to content

fix: should trigger request again when throttle is not undefined #5528

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions packages/react/src/use-chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,15 +317,19 @@ By default, it's set to 1, which means that only a single LLM call is made.
onUpdate({ message, data, replaceLastMessage }) {
mutateStatus('streaming');

const newMessages = [
...(replaceLastMessage
? chatMessages.slice(0, chatMessages.length - 1)
: chatMessages),
message,
];

throttledMutate(
[
...(replaceLastMessage
? chatMessages.slice(0, chatMessages.length - 1)
: chatMessages),
message,
],
newMessages,
false,
);

messagesRef.current = newMessages;

if (data?.length) {
throttledMutateStreamData(
Expand Down
60 changes: 60 additions & 0 deletions packages/react/src/use-chat.ui.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,66 @@ describe('maxSteps', () => {
expect(screen.getByTestId('message-1')).toHaveTextContent('final result');
});
});

describe('should trigger request again when experimental_throttle is not undefined', () => {
let onToolCallInvoked = false;

setupTestComponent(() => {
const { append } = useChat({
async onToolCall({ toolCall }) {
onToolCallInvoked = true;

return `test-tool-response: ${toolCall.toolName} ${
toolCall.toolCallId
} ${JSON.stringify(toolCall.args)}`;
},
maxSteps: 5,
experimental_throttle: 100
});

return (
<div>
<button
data-testid="do-append"
onClick={() => {
append({ role: 'user', content: 'hi' });
}}
/>
</div>
);
});

beforeEach(() => {
onToolCallInvoked = false;
});

it('should trigger request again', async () => {
server.urls['/api/chat'].response = [
{
type: 'stream-chunks',
chunks: [
formatDataStreamPart('tool_call', {
toolCallId: 'tool-call-0',
toolName: 'test-tool',
args: { testArg: 'test-value' },
}),
],
},
{
type: 'stream-chunks',
chunks: [formatDataStreamPart('text', 'final result')],
},
];

await userEvent.click(screen.getByTestId('do-append'));

expect(onToolCallInvoked).toBe(true);

await waitFor(async () => {
expect(await server.calls.length).toBe(2);
});
});
});

describe('two steps with error response', () => {
let onToolCallCounter = 0;
Expand Down