Feat/assistant session list (#198)

* feat: add assistant mode integration and fix test environment

- Add assistant-mode service for managing assistant workspace files (AGENTS.md, opencode.json)
- Add backend GET/POST routes for assistant-mode status and initialization
- Add AssistantRedirect page and useAssistantSessionLauncher hook
- Update mobile navigation to include Assistant tab (replaces Alerts)
- Fix React.act compatibility by setting NODE_ENV=test in vitest config
- Add shared Zod schemas for AssistantMode types and requests

* feat: enhance assistant mode with session list view and legacy config migration

- Add session list view for assistant mode with full workspace management
- Migrate legacy opencode.json permission format to granular permissions
- Enable assistant navigation from mobile tab bar and repo switcher
- Preserve assistant context in session routes via query params
- Auto-restart OpenCode server after assistant mode initialization
- Add comprehensive tests for legacy config detection and navigation
This commit is contained in:
Chris Scott authored and GitHub committed 2026-04-24 18:35:47 -04:00
1 parent 0a2e337efc
commit c894ff5756
22 files changed
+1347 -154

No files matched your search

@@ -1,22 +1,25 @@
import { vi } from 'vitest'
vi.mock('@/hooks/useMobile')
vi.mock('@/contexts/EventContext', () => ({
usePendingAlerts: vi.fn(),
vi.mock('@/hooks/useMobile', () => ({
useMobile: vi.fn(),
}))
import { render, screen } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { describe, it, expect, beforeEach } from 'vitest'
import { MemoryRouter } from 'react-router-dom'
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { MobileTabBar } from './MobileTabBar'
import { useMobile } from '@/hooks/useMobile'
import { usePendingAlerts } from '@/contexts/EventContext'
function LocationSpy() {
const { pathname, search } = useLocation()
return <div data-testid="location">{`${pathname}${search}`}</div>
}
describe('MobileTabBar', () => {
beforeEach(() => {
vi.clearAllMocks()
vi.mocked(usePendingAlerts).mockReturnValue(false)
})
it('renders nothing when useMobile returns false', () => {
@@ -59,6 +62,65 @@ describe('MobileTabBar', () => {
expect(screen.getByText('Schedules')).toBeInTheDocument()
})
it('renders global tabs on assistant session list path', () => {
vi.mocked(useMobile).mockReturnValue(true)
const queryClient = new QueryClient()
render(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/repos/123/assistant?view=sessions']}>
<MobileTabBar />
</MemoryRouter>
</QueryClientProvider>,
)
expect(screen.getByText('Repos')).toBeInTheDocument()
expect(screen.getByText('Assistant')).toBeInTheDocument()
expect(screen.getByText('Schedules')).toBeInTheDocument()
})
it('navigates to assistant route when repo id is present', async () => {
vi.mocked(useMobile).mockReturnValue(true)
const queryClient = new QueryClient()
const user = userEvent.setup()
render(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/repos/123']}>
<Routes>
<Route path="*" element={<>
<MobileTabBar />
<LocationSpy />
</>} />
</Routes>
</MemoryRouter>
</QueryClientProvider>,
)
await user.click(screen.getByRole('button', { name: 'Assistant' }))
expect(screen.getByTestId('location')).toHaveTextContent('/repos/123/assistant')
})
it('navigates to assistant route when assistant is clicked without repo id', async () => {
vi.mocked(useMobile).mockReturnValue(true)
const queryClient = new QueryClient()
const user = userEvent.setup()
render(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/schedules']}>
<Routes>
<Route path="*" element={<>
<MobileTabBar />
<LocationSpy />
</>} />
</Routes>
</MemoryRouter>
</QueryClientProvider>,
)
await user.click(screen.getByRole('button', { name: 'Assistant' }))
expect(screen.getByTestId('location')).toHaveTextContent('/assistant')
})
it('renders schedule tabs on /repos/:id/schedules path', () => {
vi.mocked(useMobile).mockReturnValue(true)
const queryClient = new QueryClient()
@@ -72,7 +134,7 @@ describe('MobileTabBar', () => {
expect(screen.getByText('Jobs')).toBeInTheDocument()
expect(screen.getByText('Detail')).toBeInTheDocument()
expect(screen.getByText('Runs')).toBeInTheDocument()
expect(screen.queryByText('Alerts')).not.toBeInTheDocument()
expect(screen.queryByText('Assistant')).not.toBeInTheDocument()
})
it('renders tab bar on root path', () => {
@@ -87,7 +149,7 @@ describe('MobileTabBar', () => {
)
expect(screen.getByText('Repos')).toBeInTheDocument()
expect(screen.getByText('Files')).toBeInTheDocument()
expect(screen.getByText('Alerts')).toBeInTheDocument()
expect(screen.getByText('Assistant')).toBeInTheDocument()
expect(screen.getByText('Schedules')).toBeInTheDocument()
expect(screen.getByText('More')).toBeInTheDocument()
})
@@ -106,36 +168,6 @@ describe('MobileTabBar', () => {
expect(screen.getByText('Schedules')).toBeInTheDocument()
})
it('shows badge when usePendingAlerts returns true', () => {
vi.mocked(useMobile).mockReturnValue(true)
vi.mocked(usePendingAlerts).mockReturnValue(true)
const queryClient = new QueryClient()
render(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/']}>
<MobileTabBar />
</MemoryRouter>
</QueryClientProvider>,
)
const badge = document.querySelector('.bg-orange-500')
expect(badge).toBeInTheDocument()
})
it('does not show badge when usePendingAlerts returns false', () => {
vi.mocked(useMobile).mockReturnValue(true)
vi.mocked(usePendingAlerts).mockReturnValue(false)
const queryClient = new QueryClient()
render(
<QueryClientProvider client={queryClient}>
<MemoryRouter initialEntries={['/']}>
<MobileTabBar />
</MemoryRouter>
</QueryClientProvider>,
)
const badge = document.querySelector('.bg-orange-500')
expect(badge).not.toBeInTheDocument()
})
it('Repos tab is active when pathname is / and no sheet is open', () => {
vi.mocked(useMobile).mockReturnValue(true)
const queryClient = new QueryClient()
@@ -218,7 +250,7 @@ describe('MobileTabBar', () => {
)
expect(screen.getByText('Repos')).toBeInTheDocument()
expect(screen.getByText('Files')).toBeInTheDocument()
expect(screen.getByText('Alerts')).toBeInTheDocument()
expect(screen.getByText('Assistant')).toBeInTheDocument()
expect(screen.getByText('Schedules')).toBeInTheDocument()
expect(screen.getByText('More')).toBeInTheDocument()
})
@@ -1,10 +1,9 @@
import { memo, useMemo } from 'react'
import { useLocation, useNavigate } from 'react-router-dom'
import { FolderGit2, FolderOpen, Bell, CalendarClock, Menu, Info, History } from 'lucide-react'
import { FolderGit2, FolderOpen, CalendarClock, Menu, Info, History, Bot } from 'lucide-react'
import { cn } from '@/lib/utils'
import { useMobile } from '@/hooks/useMobile'
import { useMobileTabBar, useScheduleTab, type ScheduleTabKey } from '@/hooks/useMobileTabBar'
import { usePendingAlerts } from '@/contexts/EventContext'
interface TabDef {
key: string
@@ -17,24 +16,75 @@ interface TabDef {
interface GlobalTabsArgs {
pathname: string
search: string
openSheet: ReturnType<typeof useMobileTabBar>['openSheet']
open: ReturnType<typeof useMobileTabBar>['open']
close: ReturnType<typeof useMobileTabBar>['close']
navigate: ReturnType<typeof useNavigate>
hasPending: boolean
isInsideRepo: boolean
repoId: string | null
}
function buildGlobalTabs({ pathname, openSheet, open, navigate, hasPending, isInsideRepo, repoId }: GlobalTabsArgs): TabDef[] {
type TabBarMode = 'hidden' | 'global' | 'schedule'
interface MobileTabRouteState {
mode: TabBarMode
isInsideRepo: boolean
repoId: string | null
}
function getMobileTabRouteState(pathname: string): MobileTabRouteState {
const repoMatch = pathname.match(/^\/repos\/(\d+)(?:\/([^/]+))?/)
const repoId = repoMatch?.[1] ?? null
const repoSection = repoMatch?.[2]
if (pathname === '/' || pathname === '/schedules') {
return { mode: 'global', isInsideRepo: false, repoId: null }
}
if (!repoId) {
return { mode: 'hidden', isInsideRepo: false, repoId: null }
}
switch (repoSection) {
case undefined:
case 'memories':
case 'assistant':
return { mode: 'global', isInsideRepo: true, repoId }
case 'schedules':
return { mode: 'schedule', isInsideRepo: true, repoId }
default:
return { mode: 'hidden', isInsideRepo: false, repoId }
}
}
function buildGlobalTabs({ pathname, search, openSheet, open, close, navigate, isInsideRepo, repoId }: GlobalTabsArgs): TabDef[] {
const navigateWithSearch = (params: URLSearchParams) => {
const nextSearch = params.toString()
navigate(nextSearch ? `${pathname}?${nextSearch}` : pathname, { replace: true })
}
const handleFilesClick = () => {
if (isInsideRepo && repoId) {
const newParams = new URLSearchParams({ dialog: 'files' })
navigate(`${pathname}?${newParams.toString()}`, { replace: true })
const newParams = new URLSearchParams(search)
newParams.set('dialog', 'files')
navigateWithSearch(newParams)
} else {
open('files')
}
}
const handleAssistantClick = () => {
if (repoId) {
close()
navigate(`/repos/${repoId}/assistant`)
return
}
close()
navigate('/assistant')
}
return [
{
key: 'repos',
@@ -51,12 +101,11 @@ function buildGlobalTabs({ pathname, openSheet, open, navigate, hasPending, isIn
active: openSheet === 'files',
},
{
key: 'notifications',
label: 'Alerts',
icon: Bell,
onClick: () => open('notifications'),
badge: hasPending,
active: openSheet === 'notifications',
key: 'assistant',
label: 'Assistant',
icon: Bot,
onClick: handleAssistantClick,
active: false,
},
{
key: 'schedules',
@@ -137,42 +186,41 @@ const TabBarRow = memo(function TabBarRow({ tabs }: TabBarRowProps) {
})
export const MobileTabBar = memo(function MobileTabBar() {
const { pathname } = useLocation()
const { pathname, search } = useLocation()
const navigate = useNavigate()
const { openSheet, open } = useMobileTabBar()
const { openSheet, open, close } = useMobileTabBar()
const { scheduleTab, setScheduleTab } = useScheduleTab()
const hasPending = usePendingAlerts()
const isOnRepoSchedules = /^\/repos\/\d+\/schedules$/.test(pathname)
const isRepoMemories = /^\/repos\/\d+\/memories$/.test(pathname)
const isMobile = useMobile()
const isRoot = pathname === '/'
const isGlobalSchedules = pathname === '/schedules'
const isRepoDetail = /^\/repos\/\d+$/.test(pathname)
const isInsideRepo = isRepoDetail || isOnRepoSchedules || isRepoMemories
const isRepoSession = /^\/repos\/\d+\/sessions\/\w+/.test(pathname)
const allow = isRoot || isGlobalSchedules || (isInsideRepo && !isRepoSession)
const repoIdMatch = pathname.match(/^\/repos\/(\d+)/)
const repoId = repoIdMatch ? repoIdMatch[1] : null
const routeState = useMemo(() => getMobileTabRouteState(pathname), [pathname])
const tabs = useMemo<TabDef[]>(
() => (isOnRepoSchedules
() => (routeState.mode === 'schedule'
? buildScheduleTabs(scheduleTab, setScheduleTab)
: buildGlobalTabs({
pathname,
openSheet,
open,
navigate,
hasPending,
isInsideRepo,
repoId,
})),
[isOnRepoSchedules, scheduleTab, setScheduleTab, pathname, openSheet, open, navigate, hasPending, isInsideRepo, repoId],
pathname,
search,
openSheet,
open,
close,
navigate,
isInsideRepo: routeState.isInsideRepo,
repoId: routeState.repoId,
})),
[
routeState,
scheduleTab,
setScheduleTab,
pathname,
search,
openSheet,
open,
close,
navigate,
],
)
if (!isMobile) return null
if (!allow) return null
if (routeState.mode === 'hidden') return null
return <TabBarRow tabs={tabs} />
})
@@ -1,6 +1,6 @@
import { render, screen, fireEvent, waitFor } from '@testing-library/react'
import { describe, it, expect, vi } from 'vitest'
import { MemoryRouter } from 'react-router-dom'
import { MemoryRouter, Route, Routes, useLocation } from 'react-router-dom'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { RepoQuickSwitchSheet } from './RepoQuickSwitchSheet'
import { listRepos } from '@/api/repos'
@@ -24,6 +24,11 @@ function createWrapper() {
)
}
function LocationSpy() {
const { pathname, search } = useLocation()
return <div data-testid="location">{`${pathname}${search}`}</div>
}
describe('RepoQuickSwitchSheet', () => {
beforeEach(() => {
vi.clearAllMocks()
@@ -117,4 +122,46 @@ describe('RepoQuickSwitchSheet', () => {
fireEvent.click(screen.getByText('repo1'))
expect(handleClose).not.toHaveBeenCalled()
})
it('navigates directly to assistant when mobileTabAction is assistant', async () => {
vi.mocked(listRepos).mockResolvedValue([
{
id: 1,
repoUrl: 'https://github.com/test/repo1.git',
localPath: '/path/to/repo1',
sourcePath: null,
currentBranch: 'main',
isLocal: false,
createdAt: new Date().toISOString(),
updatedAt: new Date().toISOString(),
},
])
const handleClose = vi.fn()
render(
<QueryClientProvider client={new QueryClient()}>
<MemoryRouter initialEntries={['/schedules?mobileTab=repos&mobileTabAction=assistant']}>
<Routes>
<Route
path="*"
element={
<>
<RepoQuickSwitchSheet isOpen onClose={handleClose} />
<LocationSpy />
</>
}
/>
</Routes>
</MemoryRouter>
</QueryClientProvider>,
)
await waitFor(() => {
expect(screen.getByText('repo1')).toBeInTheDocument()
})
fireEvent.click(screen.getByText('repo1'))
expect(screen.getByTestId('location')).toHaveTextContent('/repos/1/assistant')
expect(handleClose).not.toHaveBeenCalled()
})
})
@@ -43,6 +43,13 @@ export function RepoQuickSwitchSheet({ isOpen, onClose }: RepoQuickSwitchSheetPr
onClose()
return
}
const pendingAction = new URLSearchParams(location.search).get('mobileTabAction')
if (pendingAction === 'assistant') {
navigate(`/repos/${id}/assistant`)
return
}
navigate(`/repos/${id}`, { replace: true })
}