ai-sdk-next-openai/app/use-chat-shared-context/chat-context.tsx
2025-09-26 15:46:29 +00:00

49 lines
1.1 KiB
TypeScript

'use client';
import React, { createContext, useContext, ReactNode, useState } from 'react';
import { Chat } from '@ai-sdk/react';
import { DefaultChatTransport, UIMessage } from 'ai';
interface ChatContextValue {
// replace with your custom message type
chat: Chat<UIMessage>;
clearChat: () => void;
}
const ChatContext = createContext<ChatContextValue | undefined>(undefined);
function createChat() {
return new Chat<UIMessage>({
transport: new DefaultChatTransport({
api: '/api/chat-shared-context',
}),
});
}
export function ChatProvider({ children }: { children: ReactNode }) {
const [chat, setChat] = useState(() => createChat());
const clearChat = () => {
setChat(createChat());
};
return (
<ChatContext.Provider
value={{
chat,
clearChat,
}}
>
{children}
</ChatContext.Provider>
);
}
export function useSharedChatContext() {
const context = useContext(ChatContext);
if (!context) {
throw new Error('useSharedChatContext must be used within a ChatProvider');
}
return context;
}