diff --git a/desktop/src/features/chats/lib/chatShared.ts b/desktop/src/features/chats/lib/chatShared.ts new file mode 100644 index 0000000000..0e2eaeda7c --- /dev/null +++ b/desktop/src/features/chats/lib/chatShared.ts @@ -0,0 +1,15 @@ +import type { ChatMetadata } from "@/shared/api/types"; +import { normalizePubkey } from "@/shared/lib/pubkey"; + +/** Chat metadata authored by someone else — a chat shared with us. */ +export function isSharedChatMetadata( + metadata: ChatMetadata | null | undefined, + identityPubkey: string | null | undefined, +) { + if (!metadata?.authorPubkey || !identityPubkey) { + return false; + } + return ( + normalizePubkey(metadata.authorPubkey) !== normalizePubkey(identityPubkey) + ); +} diff --git a/desktop/src/features/chats/ui/ChatDetail.tsx b/desktop/src/features/chats/ui/ChatDetail.tsx index 0664b0b7c0..67e06cbe71 100644 --- a/desktop/src/features/chats/ui/ChatDetail.tsx +++ b/desktop/src/features/chats/ui/ChatDetail.tsx @@ -47,7 +47,6 @@ import type { } from "@/shared/api/types"; import { KIND_SYSTEM_MESSAGE } from "@/shared/constants/kinds"; import { cn } from "@/shared/lib/cn"; -import { extractSupportedLinkPreviews } from "@/shared/lib/linkPreview"; import { normalizePubkey } from "@/shared/lib/pubkey"; import { MessageScroller, @@ -88,7 +87,11 @@ type ChatDetailProps = { profiles?: UserProfileLookup; projects: ChatProject[]; shareAction?: React.ReactNode; + /** Show the top-right work module for this PR (toggled from the header). */ + showWorkPanel?: boolean; templates: ChannelTemplate[]; + /** Latest PR link the chat's agent posted, if any. */ + workPanelHref?: string | null; }; export function ChatDetail({ @@ -106,7 +109,9 @@ export function ChatDetail({ profiles, projects, shareAction, + showWorkPanel = true, templates, + workPanelHref = null, }: ChatDetailProps) { const queryClient = useQueryClient(); const updateMetadataMutation = useUpdateChatMetadataMutation(); @@ -249,28 +254,6 @@ export function ChatDetail({ ); const hasTranscriptActivity = chatActivity.totalBlockCount > 0; - // The latest PR link the agent posted in this chat drives the top-right - // work module (branch + live PR card). - const agentPullRequestHref = React.useMemo(() => { - if (!defaultAgent?.pubkey) { - return null; - } - const agentKey = normalizePubkey(defaultAgent.pubkey); - for (let index = messages.length - 1; index >= 0; index--) { - const message = messages[index]; - if (normalizePubkey(message.pubkey) !== agentKey) { - continue; - } - const preview = extractSupportedLinkPreviews(message.content).find( - (candidate) => candidate.kind === "github-pull-request", - ); - if (preview) { - return preview.href; - } - } - return null; - }, [defaultAgent?.pubkey, messages]); - // Solo chats (you + one agent) read as a plain stream: agent rows drop // their avatar and name. Identities come back as soon as another agent or // person participates, so multi-party chats stay attributable. @@ -641,8 +624,8 @@ export function ChatDetail({ /> - {agentPullRequestHref ? ( - + {workPanelHref && showWorkPanel ? ( + ) : null} diff --git a/desktop/src/features/chats/ui/ChatHeaderActions.tsx b/desktop/src/features/chats/ui/ChatHeaderActions.tsx index 3692f40483..4f8a924b8e 100644 --- a/desktop/src/features/chats/ui/ChatHeaderActions.tsx +++ b/desktop/src/features/chats/ui/ChatHeaderActions.tsx @@ -50,6 +50,8 @@ type ChatHeaderActionsProps = { defaultAgentPubkey?: string | null; messages: RelayEvent[]; metadata: ChatMetadata | null; + /** Rendered between Share and the settings menu (work panel toggle). */ + workPanelToggle?: React.ReactNode; }; export function ChatHeaderActions({ @@ -57,6 +59,7 @@ export function ChatHeaderActions({ defaultAgentPubkey, messages, metadata, + workPanelToggle, }: ChatHeaderActionsProps) { const [isCanvasOpen, setIsCanvasOpen] = React.useState(false); const [isDefaultAgentOpen, setIsDefaultAgentOpen] = React.useState(false); @@ -81,6 +84,7 @@ export function ChatHeaderActions({ messages={messages} metadata={metadata} /> + {workPanelToggle} setIsCanvasOpen(true)} onOpenDefaultAgent={() => setIsDefaultAgentOpen(true)} diff --git a/desktop/src/features/chats/ui/ChatList.tsx b/desktop/src/features/chats/ui/ChatList.tsx new file mode 100644 index 0000000000..5c3b13a26e --- /dev/null +++ b/desktop/src/features/chats/ui/ChatList.tsx @@ -0,0 +1,321 @@ +// @ts-nocheck +import * as React from "react"; +import { + ChevronDown, + ChevronRight, + MoreVertical, + Notebook, + Plus, +} from "lucide-react"; + +import { useActiveAgentTurnsByChannel } from "@/features/agents/activeAgentTurnsStore"; +import type { buildChatProjects } from "@/features/chats/lib/chatProjects"; +import { ChatListHeader, ChatListItem } from "@/features/chats/ui/ChatListItem"; +import { ChatListSectionHeader } from "@/features/chats/ui/ChatListSectionHeader"; +import { ChatListSkeleton } from "@/features/chats/ui/ChatListSkeleton"; +import { ChatProjectDialog } from "@/features/chats/ui/ChatProjectDialog"; +import { isSharedChatMetadata } from "@/features/chats/lib/chatShared"; +import type { + Channel, + ChannelTemplate, + ChatMetadata, +} from "@/shared/api/types"; +import { Button } from "@/shared/ui/button"; +import { + DropdownMenu, + DropdownMenuContent, + DropdownMenuItem, + DropdownMenuTrigger, +} from "@/shared/ui/dropdown-menu"; + +export function ChatList({ + archivingChatId, + chats, + getChannelReadAt, + identityPubkey, + isLoading, + metadataByChatId, + onArchiveChat, + onCreateChat, + onCreateProjectChat, + onRenameChat, + onSelectChat, + onTogglePin, + onUpdateProject, + pinnedChatIds, + projects, + readStateVersion: _readStateVersion, + selectedChatId, + templates, + unreadChannelCounts, + unreadChannelIds, +}: { + archivingChatId: string | null; + chats: Channel[]; + getChannelReadAt: (channelId: string) => number | null; + identityPubkey?: string | null; + isLoading: boolean; + metadataByChatId: ReadonlyMap; + onArchiveChat: (chatId: string) => void; + onCreateChat: () => void; + onCreateProjectChat: (projectId: string) => void; + onRenameChat: (chatId: string) => void; + onSelectChat: (chatId: string) => void; + onTogglePin: (chatId: string) => void; + pinnedChatIds: ReadonlySet; + onUpdateProject: ( + project: ReturnType[number], + ) => void; + projects: ReturnType; + readStateVersion: number; + selectedChatId: string | null; + templates: ChannelTemplate[]; + unreadChannelCounts: ReadonlyMap; + unreadChannelIds: ReadonlySet; +}) { + const [collapsedProjectIds, setCollapsedProjectIds] = React.useState( + () => new Set(), + ); + const [editingProject, setEditingProject] = React.useState< + ReturnType[number] | null + >(null); + const [isCreateProjectOpen, setIsCreateProjectOpen] = React.useState(false); + const activeAgentTurnsByChannel = useActiveAgentTurnsByChannel(); + const activeChatIds = React.useMemo( + () => new Set(activeAgentTurnsByChannel.map((turn) => turn.channelId)), + [activeAgentTurnsByChannel], + ); + const chatsByProject = React.useMemo(() => { + const groups = new Map(); + const unprojected: Channel[] = []; + const shared: Channel[] = []; + const knownProjectIds = new Set(projects.map((project) => project.id)); + for (const chat of chats) { + const metadata = metadataByChatId.get(chat.id); + if (isSharedChatMetadata(metadata, identityPubkey)) { + shared.push(chat); + continue; + } + const projectId = metadata?.projectId; + if (projectId && knownProjectIds.has(projectId)) { + const group = groups.get(projectId) ?? []; + group.push(chat); + groups.set(projectId, group); + } else { + unprojected.push(chat); + } + } + const pinnedFirst = (list: Channel[]) => + [...list].sort( + (left, right) => + Number(pinnedChatIds.has(right.id)) - + Number(pinnedChatIds.has(left.id)), + ); + return { + groups: new Map( + [...groups.entries()].map(([projectId, group]) => [ + projectId, + pinnedFirst(group), + ]), + ), + shared: pinnedFirst(shared), + unprojected: pinnedFirst(unprojected), + }; + }, [chats, identityPubkey, metadataByChatId, pinnedChatIds, projects]); + + const toggleProject = React.useCallback((projectId: string) => { + setCollapsedProjectIds((current) => { + const next = new Set(current); + if (next.has(projectId)) { + next.delete(projectId); + } else { + next.add(projectId); + } + return next; + }); + }, []); + + if (isLoading) { + return ( +
+ + +
+ ); + } + + return ( +
+ +
+
+ setIsCreateProjectOpen(true)} + /> + {projects.map((project) => { + const projectChats = chatsByProject.groups.get(project.id) ?? []; + const isCollapsed = collapsedProjectIds.has(project.id); + return ( +
+
+ + + + + + + + setEditingProject(project)} + > + Project settings + + + + +
+ {!isCollapsed ? ( +
+ {projectChats.length > 0 ? ( + projectChats.map((chat) => ( + + )) + ) : ( +
+ No chats yet +
+ )} +
+ ) : null} +
+ ); + })} +
+
+ + {chatsByProject.unprojected.length > 0 ? ( + chatsByProject.unprojected.map((chat) => ( + + )) + ) : ( +
+ No chats yet +
+ )} +
+ {chatsByProject.shared.length > 0 ? ( +
+ + {chatsByProject.shared.map((chat) => ( + + ))} +
+ ) : null} +
+ { + onUpdateProject(project); + setIsCreateProjectOpen(false); + }} + open={isCreateProjectOpen} + templates={templates} + /> + { + if (!open) { + setEditingProject(null); + } + }} + onSaveProject={(project) => { + onUpdateProject(project); + setEditingProject(null); + }} + open={editingProject !== null} + project={editingProject} + templates={templates} + /> +
+ ); +} diff --git a/desktop/src/features/chats/ui/ChatWorkPanel.tsx b/desktop/src/features/chats/ui/ChatWorkPanel.tsx index bc3337a7f9..079fdeaa84 100644 --- a/desktop/src/features/chats/ui/ChatWorkPanel.tsx +++ b/desktop/src/features/chats/ui/ChatWorkPanel.tsx @@ -5,7 +5,7 @@ import { useGithubPullRequestQuery, } from "@/shared/lib/githubPullRequest"; import { parseSupportedLinkPreview } from "@/shared/lib/linkPreview"; -import { AgentPullRequestCard } from "@/shared/ui/link-preview-attachment"; +import { GithubPullRequestCard } from "@/shared/ui/link-preview-attachment"; /** * Right-hand work module for a chat whose agent produced a pull request: @@ -36,7 +36,7 @@ export function ChatWorkPanel({ prHref }: { prHref: string }) { {branch} ) : null} - + ); } diff --git a/desktop/src/features/chats/ui/ChatsScreen.tsx b/desktop/src/features/chats/ui/ChatsScreen.tsx index 250f6b65af..6446523dfa 100644 --- a/desktop/src/features/chats/ui/ChatsScreen.tsx +++ b/desktop/src/features/chats/ui/ChatsScreen.tsx @@ -2,17 +2,10 @@ import * as React from "react"; import { useQueryClient } from "@tanstack/react-query"; import { toast } from "sonner"; -import { - ChevronDown, - ChevronRight, - MoreVertical, - Notebook, - Plus, -} from "lucide-react"; +import { GitPullRequest } from "lucide-react"; import { useAppNavigation } from "@/app/navigation/useAppNavigation"; import { useAppShell } from "@/app/AppShellContext"; -import { useActiveAgentTurnsByChannel } from "@/features/agents/activeAgentTurnsStore"; import { useManagedAgentsQuery, useStartManagedAgentMutation, @@ -26,6 +19,8 @@ import { useUpdateChatMetadataMutation, } from "@/features/chats/hooks"; import { buildChatProjects } from "@/features/chats/lib/chatProjects"; +import { isSharedChatMetadata } from "@/features/chats/lib/chatShared"; +import { ChatList } from "@/features/chats/ui/ChatList"; import { toggleStoredChatPin, useStoredChatPins, @@ -42,10 +37,6 @@ import { } from "@/features/chats/lib/chatSetup"; import { ChatDetail } from "@/features/chats/ui/ChatDetail"; import { ChatHeaderActions } from "@/features/chats/ui/ChatHeaderActions"; -import { ChatListHeader, ChatListItem } from "@/features/chats/ui/ChatListItem"; -import { ChatListSectionHeader } from "@/features/chats/ui/ChatListSectionHeader"; -import { ChatListSkeleton } from "@/features/chats/ui/ChatListSkeleton"; -import { ChatProjectDialog } from "@/features/chats/ui/ChatProjectDialog"; import { ChatRenameDialog } from "@/features/chats/ui/ChatRenameDialog"; import { QuickStartChat } from "@/features/chats/ui/QuickStartChat"; import { @@ -58,19 +49,10 @@ import { useUsersBatchQuery } from "@/features/profile/hooks"; import { useWorkspaces } from "@/features/workspaces/useWorkspaces"; import { useIdentityQuery } from "@/shared/api/hooks"; import { addChannelMembers, getCanvas, setCanvas } from "@/shared/api/tauri"; -import type { - Channel, - ChannelTemplate, - ChatMetadata, -} from "@/shared/api/types"; +import { extractSupportedLinkPreviews } from "@/shared/lib/linkPreview"; +import { cn } from "@/shared/lib/cn"; import { normalizePubkey } from "@/shared/lib/pubkey"; import { Button } from "@/shared/ui/button"; -import { - DropdownMenu, - DropdownMenuContent, - DropdownMenuItem, - DropdownMenuTrigger, -} from "@/shared/ui/dropdown-menu"; type ChatsScreenProps = { initialProjectId?: string | null; @@ -100,18 +82,6 @@ async function addBotToChat(channelId: string, pubkey: string) { } } -function isSharedChatMetadata( - metadata: ChatMetadata | null | undefined, - identityPubkey: string | null | undefined, -) { - if (!metadata?.authorPubkey || !identityPubkey) { - return false; - } - return ( - normalizePubkey(metadata.authorPubkey) !== normalizePubkey(identityPubkey) - ); -} - export function ChatsScreen({ initialProjectId, selectedChatId = null, @@ -339,6 +309,36 @@ export function ChatsScreen({ identityQuery.data, ); const archiveChatMutation = useArchiveChatMutation(); + + // Latest PR link the chat's agent posted — drives the header toggle and + // the top-right work module in the conversation. + const agentPullRequestHref = React.useMemo(() => { + const agentPubkey = metadata?.defaultAgentPubkey ?? defaultAgent?.pubkey; + if (!agentPubkey) { + return null; + } + const agentKey = normalizePubkey(agentPubkey); + for (let index = messages.length - 1; index >= 0; index--) { + const message = messages[index]; + if (normalizePubkey(message.pubkey) !== agentKey) { + continue; + } + const preview = extractSupportedLinkPreviews(message.content).find( + (candidate) => candidate.kind === "github-pull-request", + ); + if (preview) { + return preview.href; + } + } + return null; + }, [defaultAgent?.pubkey, messages, metadata?.defaultAgentPubkey]); + const [isWorkPanelOpen, setIsWorkPanelOpen] = React.useState(true); + const workPanelChatRef = React.useRef(selectedChatId); + if (workPanelChatRef.current !== selectedChatId) { + // Each chat starts with its work module visible. + workPanelChatRef.current = selectedChatId; + setIsWorkPanelOpen(true); + } const startManagedAgentMutation = useStartManagedAgentMutation(); const [isEnsuringDefaultAgent, setIsEnsuringDefaultAgent] = React.useState(false); @@ -616,12 +616,36 @@ export function ChatsScreen({ onSend={handleSend} profiles={profiles} projects={chatProjects} + showWorkPanel={isWorkPanelOpen} + workPanelHref={agentPullRequestHref} shareAction={ setIsWorkPanelOpen((open) => !open)} + size="icon" + type="button" + variant="ghost" + > + + + ) : null + } /> } templates={templates} @@ -654,295 +678,3 @@ export function ChatsScreen({ ); } - -function ChatList({ - archivingChatId, - chats, - getChannelReadAt, - identityPubkey, - isLoading, - metadataByChatId, - onArchiveChat, - onCreateChat, - onCreateProjectChat, - onRenameChat, - onSelectChat, - onTogglePin, - onUpdateProject, - pinnedChatIds, - projects, - readStateVersion: _readStateVersion, - selectedChatId, - templates, - unreadChannelCounts, - unreadChannelIds, -}: { - archivingChatId: string | null; - chats: Channel[]; - getChannelReadAt: (channelId: string) => number | null; - identityPubkey?: string | null; - isLoading: boolean; - metadataByChatId: ReadonlyMap; - onArchiveChat: (chatId: string) => void; - onCreateChat: () => void; - onCreateProjectChat: (projectId: string) => void; - onRenameChat: (chatId: string) => void; - onSelectChat: (chatId: string) => void; - onTogglePin: (chatId: string) => void; - pinnedChatIds: ReadonlySet; - onUpdateProject: ( - project: ReturnType[number], - ) => void; - projects: ReturnType; - readStateVersion: number; - selectedChatId: string | null; - templates: ChannelTemplate[]; - unreadChannelCounts: ReadonlyMap; - unreadChannelIds: ReadonlySet; -}) { - const [collapsedProjectIds, setCollapsedProjectIds] = React.useState( - () => new Set(), - ); - const [editingProject, setEditingProject] = React.useState< - ReturnType[number] | null - >(null); - const [isCreateProjectOpen, setIsCreateProjectOpen] = React.useState(false); - const activeAgentTurnsByChannel = useActiveAgentTurnsByChannel(); - const activeChatIds = React.useMemo( - () => new Set(activeAgentTurnsByChannel.map((turn) => turn.channelId)), - [activeAgentTurnsByChannel], - ); - const chatsByProject = React.useMemo(() => { - const groups = new Map(); - const unprojected: Channel[] = []; - const shared: Channel[] = []; - const knownProjectIds = new Set(projects.map((project) => project.id)); - for (const chat of chats) { - const metadata = metadataByChatId.get(chat.id); - if (isSharedChatMetadata(metadata, identityPubkey)) { - shared.push(chat); - continue; - } - const projectId = metadata?.projectId; - if (projectId && knownProjectIds.has(projectId)) { - const group = groups.get(projectId) ?? []; - group.push(chat); - groups.set(projectId, group); - } else { - unprojected.push(chat); - } - } - const pinnedFirst = (list: Channel[]) => - [...list].sort( - (left, right) => - Number(pinnedChatIds.has(right.id)) - - Number(pinnedChatIds.has(left.id)), - ); - return { - groups: new Map( - [...groups.entries()].map(([projectId, group]) => [ - projectId, - pinnedFirst(group), - ]), - ), - shared: pinnedFirst(shared), - unprojected: pinnedFirst(unprojected), - }; - }, [chats, identityPubkey, metadataByChatId, pinnedChatIds, projects]); - - const toggleProject = React.useCallback((projectId: string) => { - setCollapsedProjectIds((current) => { - const next = new Set(current); - if (next.has(projectId)) { - next.delete(projectId); - } else { - next.add(projectId); - } - return next; - }); - }, []); - - if (isLoading) { - return ( -
- - -
- ); - } - - return ( -
- -
-
- setIsCreateProjectOpen(true)} - /> - {projects.map((project) => { - const projectChats = chatsByProject.groups.get(project.id) ?? []; - const isCollapsed = collapsedProjectIds.has(project.id); - return ( -
-
- - - - - - - - setEditingProject(project)} - > - Project settings - - - - -
- {!isCollapsed ? ( -
- {projectChats.length > 0 ? ( - projectChats.map((chat) => ( - - )) - ) : ( -
- No chats yet -
- )} -
- ) : null} -
- ); - })} -
-
- - {chatsByProject.unprojected.length > 0 ? ( - chatsByProject.unprojected.map((chat) => ( - - )) - ) : ( -
- No chats yet -
- )} -
- {chatsByProject.shared.length > 0 ? ( -
- - {chatsByProject.shared.map((chat) => ( - - ))} -
- ) : null} -
- { - onUpdateProject(project); - setIsCreateProjectOpen(false); - }} - open={isCreateProjectOpen} - templates={templates} - /> - { - if (!open) { - setEditingProject(null); - } - }} - onSaveProject={(project) => { - onUpdateProject(project); - setEditingProject(null); - }} - open={editingProject !== null} - project={editingProject} - templates={templates} - /> -
- ); -} diff --git a/desktop/tests/e2e/chats-first-message.spec.ts b/desktop/tests/e2e/chats-first-message.spec.ts index a0889e3246..8bba0895cf 100644 --- a/desktop/tests/e2e/chats-first-message.spec.ts +++ b/desktop/tests/e2e/chats-first-message.spec.ts @@ -141,15 +141,23 @@ test("first message in a new chat is sent and rendered", async ({ page }) => { ) .toBeGreaterThanOrEqual(-1); - // Agent-authored PR links render the prominent agent-work card variant - // twice: inline in the message and in the top-right work panel (which - // also shows the PR's source branch). + // Agent-authored PR links render the prominent agent-work card inline in + // the message; the top-right work panel shows the standard rich card plus + // the PR's source branch. await expect( page.locator("[data-link-preview='github-pull-request-agent']"), - ).toHaveCount(2, { timeout: 10_000 }); - await expect(page.getByTestId("chat-work-panel")).toBeVisible(); - await expect(page.getByTestId("chat-work-panel")).toContainText( - "kennylopez-chatmode", - ); + ).toHaveCount(1, { timeout: 10_000 }); + const workPanel = page.getByTestId("chat-work-panel"); + await expect(workPanel).toBeVisible(); + await expect(workPanel).toContainText("kennylopez-chatmode"); + await expect( + workPanel.locator("[data-link-preview='github-pull-request']"), + ).toBeVisible(); + + // The header's PR button toggles the panel. + await page.getByTestId("toggle-work-panel").click(); + await expect(workPanel).not.toBeVisible(); + await page.getByTestId("toggle-work-panel").click(); + await expect(workPanel).toBeVisible(); await page.screenshot({ path: "test-results/agent-pr-card.png" }); });