diff --git a/packages/react/src/context/react/AssistantContext.ts b/packages/react/src/context/react/AssistantContext.ts index 956ea86b6..c1a4bdbad 100644 --- a/packages/react/src/context/react/AssistantContext.ts +++ b/packages/react/src/context/react/AssistantContext.ts @@ -16,11 +16,15 @@ export const AssistantContext = createContext( null, ); -export const useAssistantContext = (): AssistantContextValue => { +export function useAssistantContext(): AssistantContextValue; +export function useAssistantContext(options: { + optional: true; +}): AssistantContextValue | null; +export function useAssistantContext(options?: { optional: true }) { const context = useContext(AssistantContext); - if (!context) + if (!options?.optional && !context) throw new Error( "This component must be used within an AssistantRuntimeProvider.", ); return context; -}; +} diff --git a/packages/react/src/context/react/ComposerContext.ts b/packages/react/src/context/react/ComposerContext.ts index 52c69e64a..070a97dea 100644 --- a/packages/react/src/context/react/ComposerContext.ts +++ b/packages/react/src/context/react/ComposerContext.ts @@ -1,5 +1,5 @@ -import { useContext, useMemo } from "react"; -import { MessageContext } from "./MessageContext"; +import { useMemo } from "react"; +import { useMessageContext } from "./MessageContext"; import { useThreadContext } from "./ThreadContext"; import type { ComposerState } from "../stores/Composer"; import type { EditComposerState } from "../stores/EditComposer"; @@ -12,7 +12,7 @@ export type ComposerContextValue = { export const useComposerContext = (): ComposerContextValue => { const { useComposer } = useThreadContext(); - const { useEditComposer } = useContext(MessageContext) ?? {}; + const { useEditComposer } = useMessageContext({ optional: true }) ?? {}; return useMemo( () => ({ useComposer: (useEditComposer ?? useComposer) as ReadonlyStore< diff --git a/packages/react/src/context/react/ContentPartContext.ts b/packages/react/src/context/react/ContentPartContext.ts index 8e98d2c02..c6232d745 100644 --- a/packages/react/src/context/react/ContentPartContext.ts +++ b/packages/react/src/context/react/ContentPartContext.ts @@ -12,11 +12,15 @@ export const ContentPartContext = createContext( null, ); -export const useContentPartContext = (): ContentPartContextValue => { +export function useContentPartContext(): ContentPartContextValue; +export function useContentPartContext(options: { + optional: true; +}): ContentPartContextValue | null; +export function useContentPartContext(options?: { optional: true }) { const context = useContext(ContentPartContext); - if (!context) + if (!options?.optional && !context) throw new Error( "This component can only be used inside a component passed to .", ); return context; -}; +} diff --git a/packages/react/src/context/react/MessageContext.ts b/packages/react/src/context/react/MessageContext.ts index 17bb8a534..52f2b5836 100644 --- a/packages/react/src/context/react/MessageContext.ts +++ b/packages/react/src/context/react/MessageContext.ts @@ -14,11 +14,15 @@ export type MessageContextValue = { export const MessageContext = createContext(null); -export const useMessageContext = () => { +export function useMessageContext(): MessageContextValue; +export function useMessageContext(options: { + optional: true; +}): MessageContextValue | null; +export function useMessageContext(options?: { optional: true }) { const context = useContext(MessageContext); - if (!context) + if (!options?.optional && !context) throw new Error( "This component can only be used inside a component passed to .", ); return context; -}; +} diff --git a/packages/react/src/context/react/ThreadContext.ts b/packages/react/src/context/react/ThreadContext.ts index 6c9b11fd2..dad4c0f18 100644 --- a/packages/react/src/context/react/ThreadContext.ts +++ b/packages/react/src/context/react/ThreadContext.ts @@ -18,11 +18,15 @@ export type ThreadContextValue = { export const ThreadContext = createContext(null); -export const useThreadContext = (): ThreadContextValue => { +export function useThreadContext(): ThreadContextValue; +export function useThreadContext(options: { + optional: true; +}): ThreadContextValue | null; +export function useThreadContext(options?: { optional: true }) { const context = useContext(ThreadContext); - if (!context) + if (!options?.optional && !context) throw new Error( "This component must be used within an AssistantRuntimeProvider.", ); return context; -}; +}