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

+35
View File
@@ -0,0 +1,35 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import {
getAssistantModeStatus,
initializeAssistantMode,
} from '@/api/repos'
import type { AssistantModeStatus, AssistantModeInitRequest } from '@opencode-manager/shared/types'
export function useAssistantMode(repoId: number) {
const queryClient = useQueryClient()
const statusQuery = useQuery<AssistantModeStatus>({
queryKey: ['repo', repoId, 'assistant-mode'],
queryFn: () => getAssistantModeStatus(repoId),
enabled: !!repoId,
})
const initializeMutation = useMutation({
mutationFn: (options?: AssistantModeInitRequest) =>
initializeAssistantMode(repoId, options),
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: ['repo', repoId, 'assistant-mode'],
})
},
})
return {
status: statusQuery.data,
isLoading: statusQuery.isLoading,
isError: statusQuery.isError,
error: statusQuery.error,
initialize: initializeMutation.mutateAsync,
isInitializing: initializeMutation.isPending,
}
}
@@ -0,0 +1,73 @@
import { renderHook, act } from '@testing-library/react'
import { describe, it, expect, beforeEach, vi } from 'vitest'
import { useAssistantSessionLauncher } from './useAssistantSessionLauncher'
import { OpenCodeClient } from '@/api/opencode'
import { initializeAssistantMode } from '@/api/repos'
const mocks = vi.hoisted(() => ({
listSessions: vi.fn(),
createSession: vi.fn(),
initializeAssistantMode: vi.fn(),
}))
vi.mock('@/api/repos', () => ({
initializeAssistantMode: mocks.initializeAssistantMode,
}))
vi.mock('@/api/opencode', () => ({
OpenCodeClient: vi.fn(() => ({
listSessions: mocks.listSessions,
createSession: mocks.createSession,
})),
}))
describe('useAssistantSessionLauncher', () => {
beforeEach(() => {
vi.clearAllMocks()
mocks.initializeAssistantMode.mockResolvedValue({ directory: '/assistant' })
})
it('opens the latest root session in the assistant directory', async () => {
mocks.listSessions.mockResolvedValue([
{ id: 'older', directory: '/assistant', time: { updated: 10 } },
{ id: 'newest-child', parentID: 'newest', directory: '/assistant', time: { updated: 40 } },
{ id: 'different-directory', directory: '/other', time: { updated: 50 } },
{ id: 'newest', directory: '/assistant', time: { updated: 30 } },
])
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
repoId: 123,
opcodeUrl: 'http://localhost:5551',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(initializeAssistantMode).toHaveBeenCalledWith(123)
expect(OpenCodeClient).toHaveBeenCalledWith('http://localhost:5551', '/assistant')
expect(onNavigate).toHaveBeenCalledWith('newest')
expect(mocks.createSession).not.toHaveBeenCalled()
})
it('creates a session when the assistant directory has no root sessions', async () => {
mocks.listSessions.mockResolvedValue([
{ id: 'other', directory: '/other', time: { updated: 50 } },
])
mocks.createSession.mockResolvedValue({ id: 'created' })
const onNavigate = vi.fn()
const { result } = renderHook(() => useAssistantSessionLauncher({
repoId: 123,
opcodeUrl: 'http://localhost:5551',
onNavigate,
}))
await act(async () => {
await result.current.openAssistant()
})
expect(mocks.createSession).toHaveBeenCalledWith({ title: 'Assistant' })
expect(onNavigate).toHaveBeenCalledWith('created')
})
})
@@ -0,0 +1,44 @@
import { useCallback } from 'react'
import { initializeAssistantMode } from '@/api/repos'
import { OpenCodeClient } from '@/api/opencode'
interface UseAssistantSessionLauncherOptions {
repoId: number
opcodeUrl: string
onNavigate: (sessionId: string) => void
}
export function useAssistantSessionLauncher({
repoId,
opcodeUrl,
onNavigate,
}: UseAssistantSessionLauncherOptions) {
const openAssistant = useCallback(async () => {
const assistant = await initializeAssistantMode(repoId)
const client = new OpenCodeClient(opcodeUrl, assistant.directory)
const sessions = await client.listSessions()
const assistantDirectory = assistant.directory
const rootSessions = sessions.filter(
(session) => !session.parentID
)
const assistantSessions = rootSessions.filter(
(session) => session.directory === assistantDirectory
)
const newest = assistantSessions.sort(
(a, b) => b.time.updated - a.time.updated
)[0]
if (newest) {
onNavigate(newest.id)
} else {
const session = await client.createSession({ title: 'Assistant' })
onNavigate(session.id)
}
}, [repoId, opcodeUrl, onNavigate])
return { openAssistant }
}
+3 -3
View File
@@ -25,10 +25,10 @@ describe('useSwipeBack', () => {
it('calls onClose when swipe completes and canBack is false', () => {
mockCanBack.mockReturnValue(false)
const element = document.createElement('div')
document.body.appendChild(element)
const { result } = renderHook(() =>
useSwipeBack(mockOnClose, {
enabled: true,
@@ -39,7 +39,7 @@ describe('useSwipeBack', () => {
)
const cleanup = result.current.bind(element)
const touchStart = new TouchEvent('touchstart', {
touches: [{ clientX: 10, clientY: 100 }] as any,
})