Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

1.2 KB
Raw Blame
import { createContext, createElement, useContext, useMemo } from 'react';
import type { Context, ReactNode } from 'react';

/** The message a part renders in, as the view rendering it reports. */
export type MessageContext = {
  messageId: string;
  nextType?: string;
  partIndex?: number;
  isExpanded: boolean;
  conversationId?: string | null;
  /** Submission state for cursor display - only true for latest message when submitting */
  isSubmitting?: boolean;
  /** Whether this is the latest message in the conversation */
  isLatestMessage?: boolean;
};

export const MessageContext: Context<MessageContext> = createContext<MessageContext>(
  {} as MessageContext,
);
export const useMessageContext = (): MessageContext => useContext(MessageContext);

/** Provides the surrounding message to one part, at that part's position in the message's content. */
export function MessagePartIndex({
  index,
  children,
}: {
  index: number;
  children?: ReactNode;
}): ReactNode {
  const message = useMessageContext();
  const value = useMemo(() => ({ ...message, partIndex: index }), [message, index]);
  return createElement(MessageContext.Provider, { value }, children);
}