Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

1.7 KB
Raw Blame
import { createContext, useContext, type PropsWithChildren } from 'react'

export type AddToolApprovalResponse = (args: {
  id: string
  approved: boolean
  reason?: string
}) => void | PromiseLike<void>

export interface MessageInfo {
  id: string

  variant?: 'default' | 'warning'

  isLoading: boolean
  readOnly?: boolean

  isUserMessage?: boolean
  isLastMessage?: boolean

  state: 'idle' | 'editing' | 'predecessor-editing'
  rating?: 'positive' | 'negative' | null
}

export interface MessageActions {
  addToolApprovalResponse?: AddToolApprovalResponse

  onDelete: (id: string) => void
  onEdit: (id: string) => void
  onBranch: (id: string) => void
  onCancelEdit: () => void
  onRate?: (id: string, rating: 'positive' | 'negative', reason?: string) => void
}

const MessageInfoContext = createContext<MessageInfo | null>(null)
const MessageActionsContext = createContext<MessageActions | null>(null)

export function useMessageInfoContext() {
  const ctx = useContext(MessageInfoContext)
  if (!ctx) {
    throw Error('useMessageInfoContext must be used within a MessageProvider')
  }
  return ctx
}

export function useMessageActionsContext() {
  const ctx = useContext(MessageActionsContext)
  if (!ctx) {
    throw Error('useMessageActionsContext must be used within a MessageProvider')
  }
  return ctx
}

export function MessageProvider({
  messageInfo,
  messageActions,
  children,
}: PropsWithChildren<{ messageInfo: MessageInfo; messageActions: MessageActions }>) {
  return (
    <MessageInfoContext.Provider value={messageInfo}>
      <MessageActionsContext.Provider value={messageActions}>
        {children}
      </MessageActionsContext.Provider>
    </MessageInfoContext.Provider>
  )
}