From 3e5a20faa4d2285d4ca138239982e1a8398d4fd8 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Mon, 28 Sep 2026 23:47:39 -0400 Subject: [PATCH] fix(navigation): show Assistant sessions in the sidebar and repo switcher --- .../navigation/DesktopSessionTree.tsx | 5 ++-- .../navigation/RepoQuickSwitchSheet.test.tsx | 5 ++-- .../navigation/RepoQuickSwitchSheet.tsx | 4 +-- .../components/navigation/RepoSessionNav.tsx | 4 +-- .../navigation/sidebar-session-tree.test.ts | 29 ++++++++++++++----- .../navigation/sidebar-session-tree.ts | 9 +++--- frontend/src/lib/navigation.test.ts | 14 ++++++++- frontend/src/lib/navigation.ts | 13 ++++++++- frontend/src/pages/AssistantRedirect.tsx | 5 ++-- frontend/src/pages/GlobalSchedules.tsx | 5 ++-- shared/src/utils/repo.ts | 2 ++ 11 files changed, 69 insertions(+), 26 deletions(-) diff --git a/frontend/src/components/navigation/DesktopSessionTree.tsx b/frontend/src/components/navigation/DesktopSessionTree.tsx index f26ff268..e4d1d369 100644 --- a/frontend/src/components/navigation/DesktopSessionTree.tsx +++ b/frontend/src/components/navigation/DesktopSessionTree.tsx @@ -13,6 +13,7 @@ import { SessionNavStatus, } from '@/components/navigation/RepoSessionNav' import { getActiveRepoId, isCurrentSessionItem, isRepoReady } from '@/components/navigation/sidebar-session-tree' +import { getRepoPath } from '@/lib/navigation' function SessionSearchInput({ hasActiveSearch, @@ -78,7 +79,7 @@ function SessionSearchResults({ repos, search }: { repos: Repo[]; search: string isWorktree={group.repo.isWorktree} isOpen isCurrent={group.repo.id === getActiveRepoId(location.pathname)} - onOpenRepo={() => navigate(`/repos/${group.repo.id}`)} + onOpenRepo={() => navigate(getRepoPath(group.repo.id))} actions={} > {group.items.map((item) => ( @@ -121,7 +122,7 @@ export function DesktopSessionTree() { repos={repos} activeRepoId={getActiveRepoId(location.pathname)} isVisible - onOpenRepo={(repoId) => navigate(`/repos/${repoId}`)} + onOpenRepo={(repoId) => navigate(getRepoPath(repoId))} onSelectSession={navigate} renderActions={(repo) => } /> diff --git a/frontend/src/components/navigation/RepoQuickSwitchSheet.test.tsx b/frontend/src/components/navigation/RepoQuickSwitchSheet.test.tsx index 3d142546..f929d3b9 100644 --- a/frontend/src/components/navigation/RepoQuickSwitchSheet.test.tsx +++ b/frontend/src/components/navigation/RepoQuickSwitchSheet.test.tsx @@ -243,7 +243,7 @@ describe('RepoQuickSwitchSheet', () => { }) }) - it('does not show assistant as a repo option', async () => { + it('shows assistant as the first repo option', async () => { vi.mocked(listRepos).mockResolvedValue([ { id: ASSISTANT_REPO_ID, @@ -276,7 +276,8 @@ describe('RepoQuickSwitchSheet', () => { expect(screen.getByText('repo1')).toBeInTheDocument() }) - expect(screen.queryByText('Assistant')).not.toBeInTheDocument() + const repoNames = screen.getAllByText(/^(Assistant|repo1)$/).map((node) => node.textContent) + expect(repoNames).toEqual(['Assistant', 'repo1']) }) it('navigates on repo click and closes sheet', async () => { diff --git a/frontend/src/components/navigation/RepoQuickSwitchSheet.tsx b/frontend/src/components/navigation/RepoQuickSwitchSheet.tsx index c3f55c1c..663c9ecf 100644 --- a/frontend/src/components/navigation/RepoQuickSwitchSheet.tsx +++ b/frontend/src/components/navigation/RepoQuickSwitchSheet.tsx @@ -8,7 +8,7 @@ import { getRepoDisplayName } from '@/lib/utils' import { AddRepoDialog } from '@/components/repo/AddRepoDialog' import { FolderGit2, Plus } from 'lucide-react' import { useUrlParams } from '@/hooks/useUrlParams' -import { getAssistantPath } from '@/lib/navigation' +import { getAssistantPath, getRepoPath } from '@/lib/navigation' import { NewSessionButton, RepoSessionNavList, SearchClearButton } from '@/components/navigation/RepoSessionNav' import { getActiveRepoId } from '@/components/navigation/sidebar-session-tree' import { useNavigableRepos } from '@/hooks/useSidebarRepoGroups' @@ -58,7 +58,7 @@ export function RepoQuickSwitchSheet({ isOpen, onClose }: RepoQuickSwitchSheetPr return } - navigateAndClose(`/repos/${id}`, { replace: true }) + navigateAndClose(getRepoPath(id), { replace: true }) } return ( diff --git a/frontend/src/components/navigation/RepoSessionNav.tsx b/frontend/src/components/navigation/RepoSessionNav.tsx index 56f88ce5..23f4364e 100644 --- a/frontend/src/components/navigation/RepoSessionNav.tsx +++ b/frontend/src/components/navigation/RepoSessionNav.tsx @@ -6,7 +6,7 @@ import { useForms, usePermissions } from '@/contexts/EventContext' import { useCreateSession } from '@/hooks/useOpenCode' import { useSidebarRepoGroups } from '@/hooks/useSidebarRepoGroups' import { cn, formatShortRelativeTime, getRepoBranchLabel, getRepoDisplayName } from '@/lib/utils' -import { getSessionPath } from '@/lib/navigation' +import { getRepoPath, getSessionPath } from '@/lib/navigation' import { isCurrentSessionItem, isRepoReady, @@ -200,7 +200,7 @@ function RepoSessionList({ repo, onSelectSession }: RepoSessionListProps) { /> ))} {hasMore && ( - onSelectSession(`/repos/${repo.id}`)}>All sessions + onSelectSession(getRepoPath(repo.id))}>All sessions )} ) diff --git a/frontend/src/components/navigation/sidebar-session-tree.test.ts b/frontend/src/components/navigation/sidebar-session-tree.test.ts index 1865fd2e..ffa235ba 100644 --- a/frontend/src/components/navigation/sidebar-session-tree.test.ts +++ b/frontend/src/components/navigation/sidebar-session-tree.test.ts @@ -44,10 +44,14 @@ describe('getActiveRepoId', () => { expect(getActiveRepoId('/repos/3/sessions/abc')).toBe(3) }) - it('returns null outside a repo and on assistant routes', () => { + it('returns null outside a repo', () => { expect(getActiveRepoId('/')).toBeNull() - expect(getActiveRepoId('/assistant')).toBeNull() - expect(getActiveRepoId('/repos/1/assistant')).toBeNull() + }) + + it('returns the assistant repo id on assistant routes', () => { + expect(getActiveRepoId('/assistant')).toBe(0) + expect(getActiveRepoId('/repos/1/assistant')).toBe(0) + expect(getActiveRepoId('/repos/0/sessions/abc')).toBe(0) }) }) @@ -76,14 +80,14 @@ describe('isRepoReady', () => { }) describe('selectNavigableRepos', () => { - it('excludes the assistant repo but keeps repos that are not ready', () => { + it('pins the assistant repo first and keeps repos that are not ready', () => { const repos = [ - createRepo({ id: 0, fullPath: '/assistant' }), - createRepo({ id: 1, fullPath: '/repos/a' }), + createRepo({ id: 1, fullPath: '/repos/a', lastAccessedAt: 20 }), createRepo({ id: 2, fullPath: '/repos/b', cloneStatus: 'cloning' }), + createRepo({ id: 0, fullPath: '/assistant', lastAccessedAt: 10 }), ] - expect(selectNavigableRepos(repos).map((repo) => repo.id)).toEqual([1, 2]) + expect(selectNavigableRepos(repos).map((repo) => repo.id)).toEqual([0, 1, 2]) }) it('sorts by last access, newest first, with never-accessed repos last', () => { @@ -173,6 +177,17 @@ describe('buildSidebarRepoGroups', () => { expect(item.path).toBe('/repos/3/sessions/w1') }) + it('labels assistant sessions and links them in assistant mode', () => { + const assistant = createRepo({ id: 0, fullPath: '/assistant', localPath: 'assistant' }) + const sessions = [createSession('s1', atStartOfDay(1000), '/assistant')] + + const [group] = buildSidebarRepoGroups({ repos: [assistant], sessions, pinnedKeys: new Set(), now: NOW }) + + expect(group.label).toBe('Assistant') + expect(group.items[0].path).toBe('/repos/0/sessions/s1?assistant=1') + expect(isCurrentSessionItem(group.items[0], '/repos/0/sessions/s1')).toBe(true) + }) + it('falls back to repo.branch when currentBranch is absent', () => { const repo = createRepo({ id: 4, fullPath: '/repos/d', branch: 'main' }) const sessions = [createSession('d1', atStartOfDay(1000), '/repos/d')] diff --git a/frontend/src/components/navigation/sidebar-session-tree.ts b/frontend/src/components/navigation/sidebar-session-tree.ts index 4efeda8e..245187e0 100644 --- a/frontend/src/components/navigation/sidebar-session-tree.ts +++ b/frontend/src/components/navigation/sidebar-session-tree.ts @@ -31,7 +31,7 @@ export function isCurrentSessionItem(item: SidebarSessionItem, pathname: string) } export function getActiveRepoId(pathname: string): number | null { - if (isAssistantPath(pathname)) return null + if (isAssistantPath(pathname)) return ASSISTANT_REPO_ID return parseRepoRoute(pathname).repoId } @@ -40,9 +40,10 @@ export function isRepoReady(repo: Repo): boolean { } export function selectNavigableRepos(repos: Repo[]): Repo[] { - return repos - .filter((repo) => repo.id !== ASSISTANT_REPO_ID) - .sort((a, b) => (b.lastAccessedAt ?? 0) - (a.lastAccessedAt ?? 0)) + const isAssistant = (repo: Repo) => Number(repo.id === ASSISTANT_REPO_ID) + return [...repos].sort( + (a, b) => isAssistant(b) - isAssistant(a) || (b.lastAccessedAt ?? 0) - (a.lastAccessedAt ?? 0), + ) } export function buildSidebarRepoGroups(input: { diff --git a/frontend/src/lib/navigation.test.ts b/frontend/src/lib/navigation.test.ts index b8e9c7a0..93f0fac6 100644 --- a/frontend/src/lib/navigation.test.ts +++ b/frontend/src/lib/navigation.test.ts @@ -1,5 +1,5 @@ import { describe, it, expect } from 'vitest'; -import { getSessionListPath, getSwipeBackTarget, getAssistantPath, getAssistantSessionListPath, isAssistantPath, getPathWithReturnTo, getReturnToPath, getSessionPath, parseRepoRoute } from './navigation'; +import { getSessionListPath, getSwipeBackTarget, getAssistantPath, getAssistantSessionListPath, isAssistantPath, getPathWithReturnTo, getReturnToPath, getRepoPath, getSessionPath, parseRepoRoute } from './navigation'; describe('getAssistantPath', () => { it('returns /assistant', () => { @@ -65,6 +65,18 @@ describe('getSessionPath', () => { expect(getSessionPath(5, 'ses_1')).toBe('/repos/5/sessions/ses_1'); expect(getSessionPath('5', 'ses_1')).toBe('/repos/5/sessions/ses_1'); }); + + it('marks assistant repo sessions with the assistant param', () => { + expect(getSessionPath(0, 'ses_1')).toBe('/repos/0/sessions/ses_1?assistant=1'); + expect(getSessionPath('0', 'ses_1')).toBe('/repos/0/sessions/ses_1?assistant=1'); + }); +}); + +describe('getRepoPath', () => { + it('routes the assistant repo to the assistant page and others to their repo page', () => { + expect(getRepoPath(0)).toBe('/assistant'); + expect(getRepoPath(5)).toBe('/repos/5'); + }); }); describe('parseRepoRoute', () => { diff --git a/frontend/src/lib/navigation.ts b/frontend/src/lib/navigation.ts index ee3bd718..2699df70 100644 --- a/frontend/src/lib/navigation.ts +++ b/frontend/src/lib/navigation.ts @@ -1,3 +1,5 @@ +import { ASSISTANT_REPO_ID } from '@opencode-manager/shared/utils'; + export function getAssistantPath(): string { return '/assistant'; } @@ -10,8 +12,17 @@ export function isAssistantPath(pathname: string): boolean { return pathname === '/assistant' || /^\/repos\/[^/]+\/assistant$/.test(pathname); } +function isAssistantRepo(repoId: number | string): boolean { + return String(repoId) === String(ASSISTANT_REPO_ID); +} + +export function getRepoPath(repoId: number | string): string { + return isAssistantRepo(repoId) ? getAssistantPath() : `/repos/${repoId}`; +} + export function getSessionPath(repoId: number | string, sessionId: string): string { - return `/repos/${repoId}/sessions/${sessionId}`; + const suffix = isAssistantRepo(repoId) ? '?assistant=1' : ''; + return `/repos/${repoId}/sessions/${sessionId}${suffix}`; } export function parseRepoRoute(pathname: string): { repoId: number | null; section: string | null; sessionId: string | null } { diff --git a/frontend/src/pages/AssistantRedirect.tsx b/frontend/src/pages/AssistantRedirect.tsx index 64423f89..c7818be9 100644 --- a/frontend/src/pages/AssistantRedirect.tsx +++ b/frontend/src/pages/AssistantRedirect.tsx @@ -5,6 +5,7 @@ import { useCreateSession } from "@/hooks/useOpenCode" import { useDialogParam } from "@/hooks/useDialogParam" import { useSidebarAction } from "@/hooks/useSidebarAction" import { useSSE } from "@/hooks/useSSE" +import { getSessionPath } from "@/lib/navigation" import { Button } from "@/components/ui/button" import { Header } from "@/components/ui/header" import { SessionList } from "@/components/session/SessionList" @@ -35,7 +36,7 @@ export function AssistantRedirect() { useSSE(assistantDirectory) const createSessionMutation = useCreateSession(assistantDirectory, (session) => { - navigate(`/repos/${repoId}/sessions/${session.id}?assistant=1`) + navigate(getSessionPath(repoId, session.id)) }) const handleCreateSession = async () => { @@ -72,7 +73,7 @@ export function AssistantRedirect() { ) : ( navigate(`/repos/${repoId}/sessions/${sessionId}?assistant=1`)} + onSelectSession={(sessionId) => navigate(getSessionPath(repoId, sessionId))} /> )} diff --git a/frontend/src/pages/GlobalSchedules.tsx b/frontend/src/pages/GlobalSchedules.tsx index 592f7b2a..e8fe2f7d 100644 --- a/frontend/src/pages/GlobalSchedules.tsx +++ b/frontend/src/pages/GlobalSchedules.tsx @@ -20,8 +20,7 @@ import { useSidebarAction } from '@/hooks/useSidebarAction' import type { ScheduleJobWithRepo, ScheduleRunWithContext } from '@/api/schedules' import { Combobox } from '@/components/ui/combobox' -import { isAssistantRepoId } from '@/lib/schedules/schedule-target' -import { getAssistantPath } from '@/lib/navigation' +import { getRepoPath } from '@/lib/navigation' type StatusFilter = 'all' | 'enabled' | 'disabled' type ScheduleModeFilter = 'all' | 'cron' | 'interval' @@ -284,7 +283,7 @@ export function GlobalSchedules() { const handleNavigateToRepo = (repoPath: string) => { const repoId = jobs.find((j) => j.repoPath === repoPath)?.repoId if (repoId === undefined) return - navigate(isAssistantRepoId(repoId) ? getAssistantPath() : `/repos/${repoId}`) + navigate(getRepoPath(repoId)) } if (isLoading) { diff --git a/shared/src/utils/repo.ts b/shared/src/utils/repo.ts index 62c282e3..f86fc25b 100644 --- a/shared/src/utils/repo.ts +++ b/shared/src/utils/repo.ts @@ -135,11 +135,13 @@ function getPathBaseName(filePath: string): string { } export function getRepoDisplayName(repo: { + id?: number name?: string | null repoUrl?: string | null localPath?: string | null sourcePath?: string | null }): string { + if (repo.id === ASSISTANT_REPO_ID) return ASSISTANT_REPO_NAME if (repo.name && repo.name.trim()) return repo.name.trim() const fromLocalPath = repo.localPath ? getPathBaseName(repo.localPath) : '' if (repo.repoUrl) return getRepoNameFromUrl(repo.repoUrl) || fromLocalPath || 'Repository'