import React, { createContext, PropsWithChildren, useContext, useMemo, } from 'react'; import type { ChatGptResponse, SendMessageOptions, StreamMessageParams, } from '../types'; interface ChatGptContextInterface { status: | 'initializing' | 'getting_auth_token' | 'logged-out' | 'authenticated'; login: () => void; flush: () => void; sendMessage( message: string, options?: SendMessageOptions ): Promise; sendMessage(args: StreamMessageParams): void; } const ChatGptContext = createContext( undefined as unknown as ChatGptContextInterface ); export const ChatGptProvider = ({ status, login, flush, sendMessage, children, }: PropsWithChildren) => { const contextValue = useMemo( () => ({ status, login, sendMessage, flush, }), [status, login, sendMessage, flush] ); return ( {children} ); }; export const useChatGpt = () => { const context = useContext(ChatGptContext); if (!context) { throw new Error('useChatGpt must be used within a ChatGptProvider'); } return context; };