, value: unknown) {
+ useEffect(() => {
+ if (textareaRef.current) resizeComposerTextarea(textareaRef.current)
+ }, [textareaRef, value])
+
+ useEffect(() => {
+ const el = textareaRef.current
+ if (!el || typeof ResizeObserver === 'undefined') return
+ let width: number | undefined
+ const observer = new ResizeObserver(([entry]) => {
+ const nextWidth = entry?.contentRect.width
+ if (nextWidth === undefined || nextWidth === width) return
+ width = nextWidth
+ resizeComposerTextarea(el)
+ })
+ observer.observe(el)
+ return () => observer.disconnect()
+ }, [textareaRef])
+}
+
function ComposerPlusMenu({
open,
diff --git a/desktop/src/components/chat/MessageList.test.tsx b/desktop/src/components/chat/MessageList.test.tsx
index 63cab834..61848293 100644
--- a/desktop/src/components/chat/MessageList.test.tsx
+++ b/desktop/src/components/chat/MessageList.test.tsx
@@ -5,6 +5,7 @@ import { relativizeWorkspacePath } from './CurrentTurnChangeCard'
import { sessionsApi } from '../../api/sessions'
import { useChatStore } from '../../stores/chatStore'
import { useWorkspaceChatContextStore } from '../../stores/workspaceChatContextStore'
+import { useWorkspacePanelStore } from '../../stores/workspacePanelStore'
import { useSettingsStore } from '../../stores/settingsStore'
import { useSessionStore } from '../../stores/sessionStore'
import { useTabStore } from '../../stores/tabStore'
@@ -1635,7 +1636,7 @@ describe('MessageList nested tool calls', () => {
await selectMessageText(userText, 'workspace selection behavior')
const floatingAddButton = screen.getByRole('button', { name: 'Reply' })
- expect(screen.getByRole('dialog', { name: 'Reply' }).parentElement?.parentElement).toBe(document.body)
+ expect(screen.getByRole('dialog', { name: 'Selected text' }).parentElement?.parentElement).toBe(document.body)
expect(userText.closest('[data-message-shell]')?.contains(floatingAddButton)).toBe(false)
fireEvent.click(floatingAddButton)
@@ -1685,6 +1686,86 @@ describe('MessageList nested tool calls', () => {
])
})
+ describe('starting a side chat from a selection', () => {
+ function showAssistantReply() {
+ useChatStore.setState({
+ sessions: {
+ [ACTIVE_TAB]: makeSessionState({
+ messages: [{ id: 'assistant-1', type: 'assistant_text', content: 'Ask about this part later.', timestamp: 1 }],
+ }),
+ },
+ })
+ }
+
+ beforeEach(() => {
+ useWorkspacePanelStore.setState(useWorkspacePanelStore.getInitialState(), true)
+ localStorage.clear()
+ })
+
+ it('offers it before Reply in the main conversation', async () => {
+ showAssistantReply()
+ render(
)
+
+ await selectMessageText(screen.getByText(/Ask about this part/), 'this part')
+
+ const actions = within(screen.getByRole('dialog', { name: 'Selected text' })).getAllByRole('button')
+ expect(actions.map((button) => button.textContent)).toEqual(['Start a side chat', 'Reply'])
+ })
+
+ it('offers only Reply inside a side chat', async () => {
+ showAssistantReply()
+ render(
)
+
+ await selectMessageText(screen.getByText(/Ask about this part/), 'this part')
+
+ expect(within(screen.getByRole('dialog', { name: 'Selected text' })).getAllByRole('button').map((button) => button.textContent)).toEqual(['Reply'])
+ })
+
+ it('opens a new side chat with the selection quoted in its composer', async () => {
+ showAssistantReply()
+ const branch = vi.spyOn(sessionsApi, 'branch').mockResolvedValue({ sessionId: 'side-1', title: 'btw', workDir: '/repo', sourceSessionId: ACTIVE_TAB })
+ render(
)
+
+ await selectMessageText(screen.getByText(/Ask about this part/), 'this part')
+ fireEvent.click(screen.getByRole('button', { name: 'Start a side chat' }))
+
+ await waitFor(() => expect(screen.queryByRole('dialog', { name: 'Selected text' })).toBeNull())
+ expect(branch).toHaveBeenCalledWith(ACTIVE_TAB, { kind: 'side' })
+ expect(useWorkspacePanelStore.getState().panelByTab[ACTIVE_TAB]?.tabs).toMatchObject([{ kind: 'sideChat', sessionId: 'side-1' }])
+ expect(useWorkspacePanelStore.getState().sideChatToFocus).toBe('side-1')
+ expect(useWorkspaceChatContextStore.getState().referencesBySession).toEqual({
+ 'side-1': [expect.objectContaining({ kind: 'chat-selection', path: 'chat://assistant/assistant-1', messageId: 'assistant-1', quote: 'this part' })],
+ })
+ expect(window.getSelection()?.toString()).toBe('')
+ })
+
+ it('blocks a second click while the side chat is being created', async () => {
+ showAssistantReply()
+ const branch = vi.spyOn(sessionsApi, 'branch').mockReturnValue(new Promise(() => {}))
+ render(
)
+
+ await selectMessageText(screen.getByText(/Ask about this part/), 'this part')
+ fireEvent.click(screen.getByRole('button', { name: 'Start a side chat' }))
+ fireEvent.click(screen.getByRole('button', { name: 'Start a side chat' }))
+
+ expect(screen.getByRole('button', { name: 'Start a side chat' })).toHaveProperty('disabled', true)
+ expect(branch).toHaveBeenCalledOnce()
+ })
+
+ it('says why a side chat could not be started', async () => {
+ showAssistantReply()
+ vi.spyOn(sessionsApi, 'branch').mockRejectedValue(new Error('busy'))
+ render(
)
+
+ await selectMessageText(screen.getByText(/Ask about this part/), 'this part')
+ fireEvent.click(screen.getByRole('button', { name: 'Start a side chat' }))
+
+ await waitFor(() => expect(toast.error).toHaveBeenCalledWith('Could not start a side chat: busy'))
+ expect(useWorkspaceChatContextStore.getState().referencesBySession).toEqual({})
+ expect(useWorkspacePanelStore.getState().sideChatToFocus).toBeNull()
+ })
+ })
+
it('closes the selected-message action but keeps the selection when pressing outside the popover', async () => {
useChatStore.setState({
sessions: {
diff --git a/desktop/src/components/chat/MessageList.tsx b/desktop/src/components/chat/MessageList.tsx
index b6ae08b8..c1ee6ceb 100644
--- a/desktop/src/components/chat/MessageList.tsx
+++ b/desktop/src/components/chat/MessageList.tsx
@@ -32,6 +32,7 @@ import { ConfirmDialog } from '../shared/ConfirmDialog'
import { getSelectionAnchor, useQuoteSelection } from '../../lib/selectionPopover'
import { ASK_USER_QUESTION_TOOL, CHOOSE_DESIGN_TEMPLATE_TOOL, isInlineQuestionTool } from '../../lib/inlineQuestionTools'
import { SelectionPopover } from '../shared/SelectionPopover'
+import { useStartSideChat } from '../../lib/sideChats'
import {
getHeightsForSession,
getMetricsForSession,
@@ -358,42 +359,60 @@ function SelectableChatMessage({
messageId,
role,
content,
+ canStartSideChat,
children,
}: {
sessionId: string
messageId: string
role: ChatMessageRole
content: string
+ canStartSideChat: boolean
children: ReactNode
}) {
const rootRef = useRef
(null)
const addReference = useWorkspaceChatContextStore((state) => state.addReference)
+ const sideChat = useStartSideChat(sessionId)
const quote = useQuoteSelection((event) => getChatSelectionFromContainer(rootRef.current, event), `${messageId}:${content}`)
const t = useTranslation()
const sourceName = role === 'assistant'
? t('chat.assistantMessageReference')
: t('chat.userMessageReference')
+ const selectionReference = () => quote.selection && {
+ kind: 'chat-selection' as const,
+ path: `chat://${role}/${messageId}`,
+ name: sourceName,
+ quote: quote.selection.text,
+ sourceRole: role,
+ messageId,
+ }
+
const addCurrentSelectionToChat = () => {
- if (!quote.selection) return
- addReference(sessionId, {
- kind: 'chat-selection',
- path: `chat://${role}/${messageId}`,
- name: sourceName,
- quote: quote.selection.text,
- sourceRole: role,
- messageId,
- })
+ const reference = selectionReference()
+ if (!reference) return
+ addReference(sessionId, reference)
quote.dismiss()
}
+ const startSideChatFromSelection = async () => {
+ const reference = selectionReference()
+ if (!reference) return
+ await sideChat.start(reference)
+ quote.dismiss()
+ }
+
+ const replyAction = { label: t('chat.replyToSelection'), onSelect: addCurrentSelectionToChat }
+ const actions = canStartSideChat
+ ? [{ label: t('chat.startSideChat'), loading: sideChat.starting, onSelect: () => void startSideChatFromSelection() }, replyAction]
+ : [replyAction]
+
return (
{children}
-
+
)
}
@@ -854,6 +873,7 @@ function MemoryEventCard({ message }: { message: MemoryEvent }) {
type MessageListProps = {
sessionId: string
compact?: boolean
+ canStartSideChat?: boolean
}
const AUTO_SCROLL_BOTTOM_THRESHOLD_PX = 48
@@ -1267,7 +1287,7 @@ const MeasuredRenderItem = memo(function MeasuredRenderItem({
)
})
-export function MessageList({ sessionId, compact = false }: MessageListProps) {
+export function MessageList({ sessionId, compact = false, canStartSideChat = false }: MessageListProps) {
const branchSession = useSessionStore((s) => s.branchSession)
const stopGeneration = useChatStore((s) => s.stopGeneration)
const reloadHistory = useChatStore((s) => s.reloadHistory)
@@ -1884,6 +1904,7 @@ export function MessageList({ sessionId, compact = false }: MessageListProps) {
}
branchAction={branchActionByMessageId.get(item.message.id)}
replyCopyText={replyCopyTextByMessageId.get(item.message.id)}
+ canStartSideChat={canStartSideChat}
/>
)}
@@ -2015,6 +2036,7 @@ export const MessageBlock = memo(function MessageBlock({
toolResult,
branchAction,
replyCopyText,
+ canStartSideChat = false,
}: {
sessionId: string
message: UIMessage
@@ -2027,6 +2049,7 @@ export const MessageBlock = memo(function MessageBlock({
onBranch: () => void
}
replyCopyText?: string
+ canStartSideChat?: boolean
}) {
const t = useTranslation()
@@ -2041,6 +2064,7 @@ export const MessageBlock = memo(function MessageBlock({
messageId={message.id}
role="user"
content={message.content}
+ canStartSideChat={canStartSideChat}
>
({
- ChatInput: ({ sessionId, compact, primary }: { sessionId: string; compact?: boolean; primary?: boolean }) => (
-
+ ChatInput: ({ sessionId, compact, primary, autoFocus }: { sessionId: string; compact?: boolean; primary?: boolean; autoFocus?: boolean }) => (
+
),
}))
@@ -65,6 +65,12 @@ describe('SessionConversation', () => {
expect(container.querySelector('[data-session-view="side"]')).not.toBeNull()
})
+ it('passes a focus request on to its composer', () => {
+ render()
+
+ expect(screen.getByTestId('chat-input').dataset.autoFocus).toBe('true')
+ })
+
it('shows the conversation, not the new-session placeholder, while a side chat’s history loads', () => {
useChatStore.setState((state) => ({ sessions: { ...state.sessions, side: { ...state.sessions.side!, messages: [] } } }))
diff --git a/desktop/src/components/chat/SessionConversation.tsx b/desktop/src/components/chat/SessionConversation.tsx
index 70bc9e46..1b219abd 100644
--- a/desktop/src/components/chat/SessionConversation.tsx
+++ b/desktop/src/components/chat/SessionConversation.tsx
@@ -112,11 +112,15 @@ export function SessionConversation({
sessionId,
compact = false,
primary = true,
+ canStartSideChat = false,
+ focusInput = false,
beforeInput,
}: {
sessionId: string
compact?: boolean
primary?: boolean
+ canStartSideChat?: boolean
+ focusInput?: boolean
beforeInput?: ReactNode
}) {
const t = useTranslation()
@@ -172,7 +176,7 @@ export function SessionConversation({
) : (
-