Skip to content

Commit fe0f666

Browse files
committed
chore: update example
1 parent 8c86ab7 commit fe0f666

File tree

1 file changed

+8
-1
lines changed
  • examples/next-openai/app/use-chat-data-ui-parts

1 file changed

+8
-1
lines changed

examples/next-openai/app/use-chat-data-ui-parts/page.tsx

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import ChatInput from '@/components/chat-input';
44
import { useChat } from '@ai-sdk/react';
5-
import { DefaultChatTransport, UIMessage } from 'ai';
5+
import { DefaultChatTransport, UIMessage, type FinishReason } from 'ai';
6+
import { useState } from 'react';
67

78
type MyMessage = UIMessage<
89
never,
@@ -16,6 +17,7 @@ type MyMessage = UIMessage<
1617
>;
1718

1819
export default function Chat() {
20+
const [lastFinishReason, setLastFinishReason] = useState<FinishReason | undefined>(undefined);
1921
const { error, status, sendMessage, messages, regenerate, stop } =
2022
useChat<MyMessage>({
2123
transport: new DefaultChatTransport({
@@ -24,6 +26,9 @@ export default function Chat() {
2426
onData: dataPart => {
2527
console.log('dataPart', JSON.stringify(dataPart, null, 2));
2628
},
29+
onFinish: ({ finishReason }) => {
30+
setLastFinishReason(finishReason);
31+
},
2732
});
2833

2934
return (
@@ -94,6 +99,8 @@ export default function Chat() {
9499
</div>
95100
)}
96101

102+
{messages.length > 0 && <div className="mt-4 text-gray-500">Finish reason: {String(lastFinishReason)}</div>}
103+
97104
<ChatInput status={status} onSubmit={text => sendMessage({ text })} />
98105
</div>
99106
);

0 commit comments

Comments
 (0)