Files
Saxon Fletcher 2f8181b8bb refactor: decouple assistant chat surfaces (#48973)
## Summary

- extract the reusable assistant conversation body from the sidebar
interface
- drive shared chat rendering and mutations with an explicit chat ID
- keep sidebar selection, shortcuts, SQL-editor context, and header
controls in the sidebar wrapper
- add selection-free chat creation and branching primitives while
preserving existing sidebar helpers
- add store tests for cross-surface selection and mutation isolation

This is PR 1 of 3. Review this foundation first, then #49031 for the
routed Explorer chat lifecycle, followed by #49032 for discovery and
cross-surface entry points.

Supersedes #48970 after renaming the branch to use the repository's
`chore/` convention.

## Test plan

- `mise exec node@22 -- pnpm --dir apps/studio exec tsc --noEmit`
- `mise exec node@22 -- pnpm --dir apps/studio exec vitest run
state/ai-assistant-state.test.ts`
- ESLint on changed files
- Prettier on changed files
2026-08-13 16:00:39 +10:00

152 lines
5.0 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'
function AssistantMessage({ message }: { message: VercelMessage }) {
const { onBranch, 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={() => onBranch(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
onBranch: (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,
onBranch: props.onBranch,
onCancelEdit: props.onCancelEdit,
onRate: props.onRate,
}
return (
<MessageProvider messageInfo={messageInfo} messageActions={messageActions}>
{isUserMessage ? <UserMessage message={message} /> : <AssistantMessage message={message} />}
</MessageProvider>
)
}