mirror of
https://github.com/supabase/supabase.git
synced 2026-10-08 19:05:06 +03:00
## Context Adds support for branching off from an Assistant's Response - which creates a new chat with all the previous messages including from where we're branching off from <img width="204" height="97" alt="image" src="https://github.com/user-attachments/assets/0b171ae6-f2b4-4b58-87fa-0010ad45f777" /> Branched conversations will have an indication of where it was branched off from <img width="404" height="427" alt="image" src="https://github.com/user-attachments/assets/bed8502f-3f83-4f76-bc00-feac86fa57a6" /> ## Other changes - Also added support for copying an Assistant's Response <img width="190" height="115" alt="image" src="https://github.com/user-attachments/assets/5e4aa0b8-eb6e-485f-80c0-3028b95720f7" /> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Branch conversations from assistant messages into a new chat. * View the originating conversation and navigate back to it. * Copy assistant message content with visual confirmation. * Access branching and copying actions from message controls. * **UI Updates** * Added “Branched from” indicators for branched conversations. * Updated the assistant disclaimer text to “The Assistant.” <!-- end of auto-generated comment: release notes by coderabbit.ai -->
152 lines
5.1 KiB
TypeScript
152 lines
5.1 KiB
TypeScript
import { UIMessage as VercelMessage } from '@ai-sdk/react'
|
|
import { useState } from 'react'
|
|
import { toast } from 'sonner'
|
|
import { cn, copyToClipboard } from 'ui'
|
|
|
|
import { DeleteMessageConfirmModal } from './DeleteMessageConfirmModal'
|
|
import { MessageActions } from './Message.Actions'
|
|
import type { AddToolApprovalResponse, MessageInfo } from './Message.Context'
|
|
import { MessageProvider, useMessageActionsContext, useMessageInfoContext } from './Message.Context'
|
|
import { MessageDisplay } from './Message.Display'
|
|
import { useAiAssistantStateSnapshot } from '@/state/ai-assistant-state'
|
|
|
|
function AssistantMessage({ message }: { message: VercelMessage }) {
|
|
const snap = useAiAssistantStateSnapshot()
|
|
const { onCancelEdit, onRate } = useMessageActionsContext()
|
|
const { id, variant, state, isLastMessage, readOnly, rating, isLoading } = useMessageInfoContext()
|
|
|
|
const handleRate = (newRating: 'positive' | 'negative', reason?: string) => {
|
|
onRate?.(id, newRating, reason)
|
|
}
|
|
|
|
const handleCopy = (onSuccess: () => void) => {
|
|
const response = message.parts
|
|
.filter((x) => x.type === 'text')
|
|
.map((x) => x.text)
|
|
.join('\n')
|
|
copyToClipboard(response, onSuccess)
|
|
}
|
|
|
|
return (
|
|
<MessageDisplay.Container
|
|
className={cn(
|
|
variant === 'warning' && 'bg-warning-200',
|
|
state === 'predecessor-editing' && 'opacity-50 transition-opacity cursor-pointer'
|
|
)}
|
|
onClick={state === 'predecessor-editing' ? onCancelEdit : undefined}
|
|
>
|
|
<MessageDisplay.MainArea>
|
|
<MessageDisplay.Content message={message} />
|
|
</MessageDisplay.MainArea>
|
|
{!readOnly && onRate && !isLoading && (
|
|
<MessageActions alwaysShow={isLastMessage}>
|
|
<MessageActions.Copy onClick={handleCopy} />
|
|
<MessageActions.ThumbsUp
|
|
onClick={() => handleRate('positive')}
|
|
isActive={rating === 'positive'}
|
|
disabled={!!rating}
|
|
/>
|
|
<MessageActions.ThumbsDown
|
|
onClick={(reason) => handleRate('negative', reason)}
|
|
isActive={rating === 'negative'}
|
|
disabled={!!rating}
|
|
/>
|
|
<MessageActions.Branch onClick={() => snap.branchChat(id)} />
|
|
</MessageActions>
|
|
)}
|
|
</MessageDisplay.Container>
|
|
)
|
|
}
|
|
|
|
function UserMessage({ message }: { message: VercelMessage }) {
|
|
const { id, variant, state } = useMessageInfoContext()
|
|
const { onCancelEdit, onEdit, onDelete } = useMessageActionsContext()
|
|
const [showDeleteConfirmModal, setShowDeleteConfirmModal] = useState(false)
|
|
|
|
return (
|
|
<>
|
|
<MessageDisplay.Container
|
|
className={cn(
|
|
'mt-6 text-foreground',
|
|
variant === 'warning' && 'bg-warning-200',
|
|
state === 'predecessor-editing' && 'opacity-50 transition-opacity cursor-pointer'
|
|
)}
|
|
onClick={state === 'predecessor-editing' ? onCancelEdit : undefined}
|
|
>
|
|
<MessageDisplay.MainArea>
|
|
<MessageDisplay.ProfileImage />
|
|
<MessageDisplay.Content message={message} />
|
|
</MessageDisplay.MainArea>
|
|
<MessageActions>
|
|
<MessageActions.Edit
|
|
onClick={state === 'idle' ? () => onEdit(id) : onCancelEdit}
|
|
tooltip={state === 'idle' ? 'Edit message' : 'Cancel editing'}
|
|
/>
|
|
<MessageActions.Delete onClick={() => setShowDeleteConfirmModal(true)} />
|
|
</MessageActions>
|
|
</MessageDisplay.Container>
|
|
<DeleteMessageConfirmModal
|
|
visible={showDeleteConfirmModal}
|
|
onConfirm={() => {
|
|
onDelete(id)
|
|
setShowDeleteConfirmModal(false)
|
|
toast.success('Message deleted successfully')
|
|
}}
|
|
onCancel={() => setShowDeleteConfirmModal(false)}
|
|
/>
|
|
</>
|
|
)
|
|
}
|
|
|
|
interface MessageProps {
|
|
id: string
|
|
message: VercelMessage
|
|
isLoading: boolean
|
|
readOnly?: boolean
|
|
variant?: 'default' | 'warning'
|
|
addToolApprovalResponse?: AddToolApprovalResponse
|
|
onDelete: (id: string) => void
|
|
onEdit: (id: string) => void
|
|
isAfterEditedMessage: boolean
|
|
isBeingEdited: boolean
|
|
onCancelEdit: () => void
|
|
isLastMessage?: boolean
|
|
onRate?: (id: string, rating: 'positive' | 'negative', reason?: string) => void
|
|
rating?: 'positive' | 'negative' | null
|
|
}
|
|
|
|
export function Message(props: MessageProps) {
|
|
const message = props.message
|
|
const { role } = message
|
|
const isUserMessage = role === 'user'
|
|
|
|
const messageInfo = {
|
|
id: props.id,
|
|
isLoading: props.isLoading,
|
|
readOnly: props.readOnly,
|
|
variant: props.variant,
|
|
isUserMessage,
|
|
state: props.isBeingEdited
|
|
? 'editing'
|
|
: props.isAfterEditedMessage
|
|
? 'predecessor-editing'
|
|
: 'idle',
|
|
isLastMessage: props.isLastMessage,
|
|
rating: props.rating,
|
|
} satisfies MessageInfo
|
|
|
|
const messageActions = {
|
|
addToolApprovalResponse: props.addToolApprovalResponse,
|
|
onDelete: props.onDelete,
|
|
onEdit: props.onEdit,
|
|
onCancelEdit: props.onCancelEdit,
|
|
onRate: props.onRate,
|
|
}
|
|
|
|
return (
|
|
<MessageProvider messageInfo={messageInfo} messageActions={messageActions}>
|
|
{isUserMessage ? <UserMessage message={message} /> : <AssistantMessage message={message} />}
|
|
</MessageProvider>
|
|
)
|
|
}
|