feat: add assistant mode, SSE streaming, and OpenCode server integration (#214)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-05-02 21:51:37 -04:00
1 parent 3cc24288a6
commit c3d6549f01
133 files changed
+7284 -6293

No files matched your search

@@ -1,100 +0,0 @@
import { Loader2, CheckCircle2, XCircle, Ban, AlertCircle } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Badge } from '@/components/ui/badge'
import type { LoopState } from '@opencode-manager/shared'
interface RepoLoopListProps {
isLoading: boolean
data: LoopState[] | undefined
error: Error | null
onCancel: (sessionId: string) => void
pendingSessionId: string | null
}
function StatusIcon({ loop }: { loop: LoopState }) {
if (loop.active) return <Loader2 className="h-4 w-4 animate-spin text-blue-400" />
if (loop.terminationReason === 'completed') return <CheckCircle2 className="h-4 w-4 text-green-400" />
if (loop.terminationReason === 'cancelled' || loop.terminationReason === 'user_aborted') return <Ban className="h-4 w-4 text-yellow-400" />
return <XCircle className="h-4 w-4 text-red-400" />
}
function formatDuration(startedAt: string, completedAt?: string): string {
const start = new Date(startedAt).getTime()
const end = completedAt ? new Date(completedAt).getTime() : Date.now()
const seconds = Math.floor((end - start) / 1000)
if (seconds < 60) return `${seconds}s`
const minutes = Math.floor(seconds / 60)
if (minutes < 60) return `${minutes}m ${seconds % 60}s`
return `${Math.floor(minutes / 60)}h ${minutes % 60}m`
}
export function RepoLoopList({ isLoading, data, error, onCancel, pendingSessionId }: RepoLoopListProps) {
if (isLoading) {
return (
<div className="flex items-center justify-center p-8">
<Loader2 className="h-6 w-6 animate-spin text-muted-foreground" />
</div>
)
}
if (error) {
return (
<div className="flex items-center gap-2 p-6 text-destructive">
<AlertCircle className="h-4 w-4" />
<span className="text-sm">Failed to load loop status</span>
</div>
)
}
if (!data?.length) {
return (
<div className="flex flex-col items-center justify-center p-8 text-muted-foreground">
<span className="text-sm">No loops found</span>
</div>
)
}
return (
<div className="flex flex-col gap-3 p-4 overflow-y-auto">
{data.map((loop) => (
<div key={loop.sessionId} className="rounded-lg border bg-card p-4 flex flex-col gap-2">
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-2 min-w-0">
<StatusIcon loop={loop} />
<span className="font-medium text-sm truncate">{loop.worktreeName}</span>
</div>
<div className="flex items-center gap-2 shrink-0">
<Badge variant="outline" className="text-xs capitalize">
{loop.phase ?? 'coding'}
</Badge>
{loop.active && (
<Button
variant="destructive"
size="sm"
onClick={() => onCancel(loop.sessionId)}
disabled={pendingSessionId === loop.sessionId}
className="h-7 text-xs"
>
Cancel
</Button>
)}
</div>
</div>
<div className="flex items-center gap-4 text-xs text-muted-foreground">
<span>Iteration {loop.iteration ?? 0}/{loop.maxIterations ?? 0}</span>
<span>{formatDuration(loop.startedAt ?? new Date().toISOString(), loop.completedAt)}</span>
{loop.worktreeBranch && <span className="truncate">{loop.worktreeBranch}</span>}
</div>
{loop.terminationReason && !loop.active && (
<span className="text-xs text-muted-foreground capitalize">
{loop.terminationReason.replace(/_/g, ' ')}
</span>
)}
{loop.lastAuditResult && (
<p className="text-xs text-muted-foreground line-clamp-2 mt-1">{loop.lastAuditResult}</p>
)}
</div>
))}
</div>
)
}
@@ -0,0 +1,268 @@
import { describe, it, expect, vi, beforeEach } from 'vitest'
import { render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { ReactNode } from 'react'
import { RepoSkillsDialog } from './RepoSkillsDialog'
import type { SkillFileInfo } from '@opencode-manager/shared'
const mocks = vi.hoisted(() => ({
listManagedSkills: vi.fn(),
sendCommand: vi.fn(),
}))
vi.mock('@/api/settings', () => ({
settingsApi: {
listManagedSkills: mocks.listManagedSkills,
},
}))
vi.mock('@/api/opencode', () => ({
OpenCodeClient: vi.fn(() => ({
sendCommand: mocks.sendCommand,
})),
}))
vi.mock('@/lib/toast', () => ({
showToast: {
error: vi.fn(),
success: vi.fn(),
},
}))
describe('RepoSkillsDialog', () => {
beforeEach(() => {
vi.clearAllMocks()
})
const createWrapper = () => {
const queryClient = new QueryClient({
defaultOptions: {
queries: { retry: false },
},
})
return ({ children }: { children: ReactNode }) => (
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
)
}
const mockSkills: SkillFileInfo[] = [
{
name: 'test-skill',
description: 'A test skill for testing',
body: 'Test skill content',
location: '/repo/.opencode/skills/test-skill/SKILL.md',
scope: 'project',
},
{
name: 'global-skill',
description: 'A global skill',
body: 'Global skill content',
location: '/home/user/.config/opencode/skills/global-skill/SKILL.md',
scope: 'global',
},
]
describe('tabs and skill filtering', () => {
it('shows both tabs when skills exist', async () => {
mocks.listManagedSkills.mockResolvedValue(mockSkills)
render(
<RepoSkillsDialog
open
onOpenChange={vi.fn()}
repoId={1}
/>,
{ wrapper: createWrapper() }
)
expect(screen.getByText('Project')).toBeInTheDocument()
expect(screen.getByText('Global')).toBeInTheDocument()
})
it('shows project skills in project tab', async () => {
mocks.listManagedSkills.mockResolvedValue(mockSkills)
render(
<RepoSkillsDialog
open
onOpenChange={vi.fn()}
repoId={1}
/>,
{ wrapper: createWrapper() }
)
await waitFor(() => {
expect(screen.getByText('Test skill')).toBeInTheDocument()
})
})
it('shows global skills in global tab', async () => {
mocks.listManagedSkills.mockResolvedValue(mockSkills)
render(
<RepoSkillsDialog
open
onOpenChange={vi.fn()}
repoId={1}
/>,
{ wrapper: createWrapper() }
)
await waitFor(() => {
expect(screen.getByText('Project')).toBeInTheDocument()
})
const globalTab = screen.getByText('Global')
await userEvent.click(globalTab)
expect(screen.getByText('Global skill')).toBeInTheDocument()
})
it('shows empty state for project tab when no project skills', async () => {
mocks.listManagedSkills.mockResolvedValue([
{
name: 'global-skill',
description: 'A global skill',
body: 'Global skill content',
location: '/home/user/.config/opencode/skills/global-skill/SKILL.md',
scope: 'global',
},
])
render(
<RepoSkillsDialog
open
onOpenChange={vi.fn()}
repoId={1}
/>,
{ wrapper: createWrapper() }
)
const projectTab = screen.getByText('Project')
await userEvent.click(projectTab)
await waitFor(() => {
expect(screen.getByText('No local skills found')).toBeInTheDocument()
})
})
})
describe('load functionality', () => {
it('shows Load button when sessionId and opcodeUrl are provided', async () => {
mocks.listManagedSkills.mockResolvedValue(mockSkills)
mocks.sendCommand.mockResolvedValue(undefined)
const onSkillLoaded = vi.fn()
const onOpenChange = vi.fn()
render(
<RepoSkillsDialog
open
onOpenChange={onOpenChange}
repoId={1}
sessionId="test-session"
opcodeUrl="http://localhost:5551"
directory="/test/repo"
onSkillLoaded={onSkillLoaded}
/>,
{ wrapper: createWrapper() }
)
await waitFor(() => {
expect(screen.getByText('Test skill')).toBeInTheDocument()
})
expect(screen.getByText('Load')).toBeInTheDocument()
})
it('does not show Load button when sessionId is not provided', async () => {
mocks.listManagedSkills.mockResolvedValue(mockSkills)
render(
<RepoSkillsDialog
open
onOpenChange={vi.fn()}
repoId={1}
/>,
{ wrapper: createWrapper() }
)
await waitFor(() => {
expect(screen.getByText('Test skill')).toBeInTheDocument()
})
expect(screen.queryByText('Load')).not.toBeInTheDocument()
})
it('calls sendCommand and closes dialog on Load click', async () => {
mocks.listManagedSkills.mockResolvedValue(mockSkills)
mocks.sendCommand.mockReturnValue(new Promise(() => {}))
const onSkillLoaded = vi.fn()
const onOpenChange = vi.fn()
const user = userEvent.setup()
render(
<RepoSkillsDialog
open
onOpenChange={onOpenChange}
repoId={1}
sessionId="test-session"
opcodeUrl="http://localhost:5551"
directory="/test/repo"
onSkillLoaded={onSkillLoaded}
/>,
{ wrapper: createWrapper() }
)
await waitFor(() => {
expect(screen.getByText('Test skill')).toBeInTheDocument()
})
const loadButton = screen.getByText('Load')
await user.click(loadButton)
expect(onOpenChange).toHaveBeenCalledWith(false)
expect(onSkillLoaded).toHaveBeenCalledWith(mockSkills[0])
expect(mocks.sendCommand).toHaveBeenCalledWith('test-session', {
command: 'test-skill',
arguments: '',
})
})
it('shows toast error on sendCommand failure', async () => {
const { showToast } = await import('@/lib/toast')
mocks.listManagedSkills.mockResolvedValue(mockSkills)
mocks.sendCommand.mockRejectedValue(new Error('Failed to load'))
const onOpenChange = vi.fn()
const user = userEvent.setup()
render(
<RepoSkillsDialog
open
onOpenChange={onOpenChange}
repoId={1}
sessionId="test-session"
opcodeUrl="http://localhost:5551"
directory="/test/repo"
/>,
{ wrapper: createWrapper() }
)
await waitFor(() => {
expect(screen.getByText('Test skill')).toBeInTheDocument()
})
const loadButton = screen.getByText('Load')
await user.click(loadButton)
await waitFor(() => {
expect(showToast.error).toHaveBeenCalledWith('Failed to load')
})
expect(onOpenChange).toHaveBeenCalledWith(false)
})
})
})
@@ -1,15 +1,32 @@
import { Dialog, DialogContent, DialogHeader, DialogTitle } from '@/components/ui/dialog'
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogDescription } from '@/components/ui/dialog'
import { RepoSkillsList } from './RepoSkillsList'
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'
import { useQuery } from '@tanstack/react-query'
import { settingsApi } from '@/api/settings'
import { useLoadSkill } from '@/hooks/useOpenCode'
import type { SkillFileInfo } from '@opencode-manager/shared'
import { useMemo, useState, useEffect } from 'react'
interface RepoSkillsDialogProps {
type RepoSkillsDialogBaseProps = {
open: boolean
onOpenChange: (open: boolean) => void
repoId: number
}
export function RepoSkillsDialog({ open, onOpenChange, repoId }: RepoSkillsDialogProps) {
type RepoSkillsDialogProps = RepoSkillsDialogBaseProps & (
| { sessionId: string; opcodeUrl: string; directory: string; onSkillLoaded?: (skill: SkillFileInfo) => void }
| { sessionId?: undefined; opcodeUrl?: undefined; directory?: undefined; onSkillLoaded?: undefined }
)
export function RepoSkillsDialog({
open,
onOpenChange,
repoId,
sessionId,
opcodeUrl,
directory,
onSkillLoaded,
}: RepoSkillsDialogProps) {
const { isLoading, data, error } = useQuery({
queryKey: ['settings', 'skills', repoId],
queryFn: () => settingsApi.listManagedSkills(repoId),
@@ -17,19 +34,75 @@ export function RepoSkillsDialog({ open, onOpenChange, repoId }: RepoSkillsDialo
staleTime: 30000,
})
const projectSkills = useMemo(
() => data?.filter((s) => s.scope === 'project') ?? [],
[data],
)
const globalSkills = useMemo(
() => data?.filter((s) => s.scope === 'global') ?? [],
[data],
)
const canLoad = !!sessionId && !!opcodeUrl
const loadSkill = useLoadSkill(opcodeUrl, sessionId, directory)
const [activeTab, setActiveTab] = useState<'project' | 'global'>('project')
useEffect(() => {
if (data && projectSkills.length === 0 && globalSkills.length > 0) {
setActiveTab('global')
}
}, [data, projectSkills.length, globalSkills.length])
const handleTabChange = (value: string) => {
setActiveTab(value as 'project' | 'global')
}
const handleLoad = (skill: SkillFileInfo) => {
loadSkill.mutate({ skillName: skill.name })
onSkillLoaded?.(skill)
onOpenChange(false)
}
if (!repoId) {
return null
}
const repoSkills = data?.filter(skill => skill.scope === 'project')
return (
<Dialog open={open} onOpenChange={onOpenChange}>
<DialogContent mobileFullscreen className="sm:max-w-2xl sm:max-h-[85vh] gap-0 flex flex-col p-0 md:p-6 pb-safe">
<DialogHeader className="p-4 sm:p-6 border-b flex flex-row items-center justify-between space-y-0 shrink-0">
<DialogHeader className="p-4 sm:p-6 border-b shrink-0">
<DialogTitle>Skills</DialogTitle>
<DialogDescription>
{canLoad ? 'Load a skill into the current session' : 'Skills available for this repository'}
</DialogDescription>
</DialogHeader>
<RepoSkillsList isLoading={isLoading} data={repoSkills} error={error} />
<Tabs value={activeTab} onValueChange={handleTabChange} className="flex-1 flex flex-col min-h-0">
<TabsList className="mx-4 sm:mx-6 mt-3">
<TabsTrigger value="project">Project</TabsTrigger>
<TabsTrigger value="global">Global</TabsTrigger>
</TabsList>
<TabsContent value="project" className="flex-1 min-h-0 flex flex-col">
<RepoSkillsList
isLoading={isLoading}
data={projectSkills}
error={error as Error | null}
emptyTitle="No local skills found"
emptyHint="Add skills to .opencode/skills/&lt;name&gt;/SKILL.md in this repository."
onLoad={canLoad ? handleLoad : undefined}
/>
</TabsContent>
<TabsContent value="global" className="flex-1 min-h-0 flex flex-col">
<RepoSkillsList
isLoading={isLoading}
data={globalSkills}
error={error as Error | null}
emptyTitle="No global skills"
emptyHint="Global skills live under ~/.config/opencode/skills/."
onLoad={canLoad ? handleLoad : undefined}
/>
</TabsContent>
</Tabs>
</DialogContent>
</Dialog>
)
+34 -10
View File
@@ -5,9 +5,19 @@ interface RepoSkillsListProps {
isLoading: boolean
data: SkillFileInfo[] | undefined
error: Error | null
emptyTitle?: string
emptyHint?: string
onLoad?: (skill: SkillFileInfo) => void
}
export function RepoSkillsList({ isLoading, data, error }: RepoSkillsListProps) {
export function RepoSkillsList({
isLoading,
data,
error,
emptyTitle,
emptyHint,
onLoad,
}: RepoSkillsListProps) {
const formatSkillName = (name: string): string => {
const formatted = name.replace(/-/g, ' ')
return formatted.charAt(0).toUpperCase() + formatted.slice(1)
@@ -35,8 +45,11 @@ export function RepoSkillsList({ isLoading, data, error }: RepoSkillsListProps)
if (!data || data.length === 0) {
return (
<div className="text-center py-6 text-muted-foreground">
<p className="text-sm">No skills available</p>
<p className="text-xs mt-1">Skills will appear here when configured in the project's .opencode/skills/ directory</p>
<p className="text-sm">{emptyTitle || 'No skills available'}</p>
<p className="text-xs mt-1">
{emptyHint ||
'Skills will appear here when configured in the project\'s .opencode/skills/ directory'}
</p>
</div>
)
}
@@ -47,15 +60,26 @@ export function RepoSkillsList({ isLoading, data, error }: RepoSkillsListProps)
{data.map((skill) => (
<div
key={skill.name}
className="p-2 rounded-lg border border-border bg-card"
className="p-2 rounded-lg border border-border bg-card flex items-center justify-between gap-2"
>
<p className="text-sm font-medium truncate">
{formatSkillName(skill.name)}
</p>
{skill.description && (
<p className="text-xs text-muted-foreground truncate mt-0.5">
{skill.description}
<div className="flex-1 min-w-0">
<p className="text-sm font-medium truncate">
{formatSkillName(skill.name)}
</p>
{skill.description && (
<p className="text-xs text-muted-foreground truncate mt-0.5">
{skill.description}
</p>
)}
</div>
{onLoad && (
<button
type="button"
onClick={() => onLoad(skill)}
className="shrink-0 text-xs font-medium text-primary hover:text-foreground transition-colors"
>
Load
</button>
)}
</div>
))}