mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-06 04:44:21 +01:00
section 8: Phase 8: Goal mode in the composer, goal status bar and goal
This commit is contained in:
1 parent
4e9f27ab0b
commit
8cd00e1a8a
19 files changed
+877
-11
No files matched your search
@@ -0,0 +1,128 @@
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import { render, screen } from '@testing-library/react'
|
||||
import userEvent from '@testing-library/user-event'
|
||||
import { SessionGoalBar } from './SessionGoalBar'
|
||||
import {
|
||||
useCancelSessionGoal,
|
||||
usePauseSessionGoal,
|
||||
useResumeSessionGoal,
|
||||
useSessionGoal,
|
||||
} from '@/hooks/useSessionGoals'
|
||||
import type { SessionGoal } from '@opencode-manager/shared/schemas'
|
||||
|
||||
vi.mock('@/hooks/useSessionGoals')
|
||||
|
||||
const baseGoal: SessionGoal = {
|
||||
id: 1,
|
||||
sessionId: 'ses_1',
|
||||
directory: '/repo',
|
||||
objective: 'Ship the feature',
|
||||
status: 'active',
|
||||
stopReason: null,
|
||||
turnState: 'running',
|
||||
continuationCount: 2,
|
||||
maxContinuations: 20,
|
||||
tokenBudget: null,
|
||||
tokensUsed: 0,
|
||||
consecutiveBlocked: 0,
|
||||
lastVerdict: null,
|
||||
lastReason: null,
|
||||
createdAt: 1,
|
||||
updatedAt: 1,
|
||||
finishedAt: null,
|
||||
}
|
||||
|
||||
function mockGoals(
|
||||
goal: SessionGoal | null,
|
||||
overrides: {
|
||||
pause?: ReturnType<typeof vi.fn>
|
||||
resume?: ReturnType<typeof vi.fn>
|
||||
cancel?: ReturnType<typeof vi.fn>
|
||||
} = {},
|
||||
) {
|
||||
vi.mocked(useSessionGoal).mockReturnValue({ data: goal } as ReturnType<typeof useSessionGoal>)
|
||||
vi.mocked(usePauseSessionGoal).mockReturnValue({
|
||||
mutate: overrides.pause ?? vi.fn(),
|
||||
isPending: false,
|
||||
} as unknown as ReturnType<typeof usePauseSessionGoal>)
|
||||
vi.mocked(useResumeSessionGoal).mockReturnValue({
|
||||
mutate: overrides.resume ?? vi.fn(),
|
||||
isPending: false,
|
||||
} as unknown as ReturnType<typeof useResumeSessionGoal>)
|
||||
vi.mocked(useCancelSessionGoal).mockReturnValue({
|
||||
mutate: overrides.cancel ?? vi.fn(),
|
||||
isPending: false,
|
||||
} as unknown as ReturnType<typeof useCancelSessionGoal>)
|
||||
}
|
||||
|
||||
describe('SessionGoalBar', () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
it('renders nothing when there is no goal', () => {
|
||||
mockGoals(null)
|
||||
const { container } = render(<SessionGoalBar sessionID="ses_1" />)
|
||||
expect(container).toBeEmptyDOMElement()
|
||||
})
|
||||
|
||||
it('shows the turn counter and pauses the active goal', async () => {
|
||||
const user = userEvent.setup()
|
||||
const pause = vi.fn()
|
||||
mockGoals({ ...baseGoal }, { pause })
|
||||
render(<SessionGoalBar sessionID="ses_1" />)
|
||||
|
||||
expect(screen.getByText('Goal active')).toBeInTheDocument()
|
||||
expect(screen.getByText('Turn 2/20')).toBeInTheDocument()
|
||||
expect(screen.getByText('Ship the feature')).toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Pause' }))
|
||||
|
||||
expect(pause).toHaveBeenCalledWith(1)
|
||||
})
|
||||
|
||||
it('shows token usage when a budget is set', () => {
|
||||
mockGoals({ ...baseGoal, tokenBudget: 1000, tokensUsed: 250 })
|
||||
render(<SessionGoalBar sessionID="ses_1" />)
|
||||
|
||||
expect(screen.getByText('250/1,000 tokens')).toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('resumes a paused goal', async () => {
|
||||
const user = userEvent.setup()
|
||||
const resume = vi.fn()
|
||||
mockGoals({ ...baseGoal, status: 'paused' }, { resume })
|
||||
render(<SessionGoalBar sessionID="ses_1" />)
|
||||
|
||||
expect(screen.getByText('Goal paused')).toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Resume' }))
|
||||
|
||||
expect(resume).toHaveBeenCalledWith(1)
|
||||
})
|
||||
|
||||
it('shows a terminal summary and can be dismissed', async () => {
|
||||
const user = userEvent.setup()
|
||||
mockGoals({ ...baseGoal, status: 'completed', stopReason: 'continuation_limit' })
|
||||
render(<SessionGoalBar sessionID="ses_1" />)
|
||||
|
||||
expect(screen.getByText('Goal completed')).toBeInTheDocument()
|
||||
expect(screen.getByText('continuation limit reached')).toBeInTheDocument()
|
||||
expect(screen.queryByRole('button', { name: 'Pause' })).not.toBeInTheDocument()
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Dismiss' }))
|
||||
|
||||
expect(screen.queryByText('Goal completed')).not.toBeInTheDocument()
|
||||
})
|
||||
|
||||
it('cancels the goal through the cancel API', async () => {
|
||||
const user = userEvent.setup()
|
||||
const cancel = vi.fn()
|
||||
mockGoals({ ...baseGoal }, { cancel })
|
||||
render(<SessionGoalBar sessionID="ses_1" />)
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Cancel' }))
|
||||
|
||||
expect(cancel).toHaveBeenCalledWith(1)
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,157 @@
|
||||
import { useState } from 'react'
|
||||
import { ChevronDown, ChevronUp, Target } from 'lucide-react'
|
||||
import { Button } from '@/components/ui/button'
|
||||
import {
|
||||
useCancelSessionGoal,
|
||||
usePauseSessionGoal,
|
||||
useResumeSessionGoal,
|
||||
useSessionGoal,
|
||||
} from '@/hooks/useSessionGoals'
|
||||
import type { SessionGoalStatus, SessionGoalStopReason } from '@opencode-manager/shared/schemas'
|
||||
|
||||
const STATUS_LABELS: Record<SessionGoalStatus, string> = {
|
||||
active: 'Goal active',
|
||||
paused: 'Goal paused',
|
||||
completed: 'Goal completed',
|
||||
blocked: 'Goal blocked',
|
||||
stopped: 'Goal stopped',
|
||||
}
|
||||
|
||||
const STOP_REASON_LABELS: Record<SessionGoalStopReason, string> = {
|
||||
cancelled: 'cancelled',
|
||||
user_paused: 'paused by you',
|
||||
continuation_limit: 'continuation limit reached',
|
||||
token_budget: 'token budget reached',
|
||||
turn_error: 'turn failed',
|
||||
interrupted: 'interrupted',
|
||||
audit_failed: 'auditor failed',
|
||||
session_deleted: 'session deleted',
|
||||
}
|
||||
|
||||
const STATUS_CHIP_CLASSES: Record<SessionGoalStatus, string> = {
|
||||
active: 'bg-highlight text-highlight-foreground border-highlight',
|
||||
paused: 'bg-warning/15 text-warning border-warning/40',
|
||||
completed: 'bg-success/15 text-success border-success/40',
|
||||
blocked: 'bg-destructive/15 text-destructive border-destructive/40',
|
||||
stopped: 'bg-muted text-muted-foreground border-border',
|
||||
}
|
||||
|
||||
const TERMINAL_STATUSES: SessionGoalStatus[] = ['completed', 'blocked', 'stopped']
|
||||
|
||||
function isTerminal(status: SessionGoalStatus): boolean {
|
||||
return TERMINAL_STATUSES.includes(status)
|
||||
}
|
||||
|
||||
export function SessionGoalBar({ sessionID }: { sessionID: string }) {
|
||||
const { data: goal } = useSessionGoal(sessionID)
|
||||
const pauseGoal = usePauseSessionGoal()
|
||||
const resumeGoal = useResumeSessionGoal()
|
||||
const cancelGoal = useCancelSessionGoal()
|
||||
const [expanded, setExpanded] = useState(false)
|
||||
const [dismissedGoalId, setDismissedGoalId] = useState<number | null>(null)
|
||||
|
||||
if (!goal) return null
|
||||
if (isTerminal(goal.status) && dismissedGoalId === goal.id) return null
|
||||
|
||||
const terminal = isTerminal(goal.status)
|
||||
const reason = goal.stopReason ? STOP_REASON_LABELS[goal.stopReason] : goal.lastReason
|
||||
|
||||
return (
|
||||
<div className="mb-1 flex flex-col gap-1 rounded-lg border border-border bg-card/70 px-2 py-1.5 text-xs">
|
||||
<div className="flex flex-wrap items-center gap-2">
|
||||
<span
|
||||
className={`inline-flex items-center gap-1 rounded-full border px-2 py-0.5 font-medium ${STATUS_CHIP_CLASSES[goal.status]}`}
|
||||
>
|
||||
<Target className="h-3 w-3" />
|
||||
{STATUS_LABELS[goal.status]}
|
||||
</span>
|
||||
|
||||
{!terminal && (
|
||||
<span className="text-muted-foreground">
|
||||
Turn {goal.continuationCount}/{goal.maxContinuations}
|
||||
</span>
|
||||
)}
|
||||
|
||||
{!terminal && goal.tokenBudget !== null && (
|
||||
<span className="text-muted-foreground">
|
||||
{goal.tokensUsed.toLocaleString()}/{goal.tokenBudget.toLocaleString()} tokens
|
||||
</span>
|
||||
)}
|
||||
|
||||
{terminal ? (
|
||||
<>
|
||||
{reason && (
|
||||
<span className="min-w-0 flex-1 truncate text-muted-foreground" title={reason}>
|
||||
{reason}
|
||||
</span>
|
||||
)}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="ml-auto h-6 px-2 text-xs"
|
||||
onClick={() => setDismissedGoalId(goal.id)}
|
||||
>
|
||||
Dismiss
|
||||
</Button>
|
||||
</>
|
||||
) : (
|
||||
<div className="ml-auto flex items-center gap-1">
|
||||
{goal.status === 'paused' ? (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs"
|
||||
disabled={resumeGoal.isPending}
|
||||
onClick={() => resumeGoal.mutate(goal.id)}
|
||||
>
|
||||
Resume
|
||||
</Button>
|
||||
) : (
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs"
|
||||
disabled={pauseGoal.isPending}
|
||||
onClick={() => pauseGoal.mutate(goal.id)}
|
||||
>
|
||||
Pause
|
||||
</Button>
|
||||
)}
|
||||
<Button
|
||||
variant="ghost"
|
||||
size="sm"
|
||||
className="h-6 px-2 text-xs text-destructive hover:text-destructive"
|
||||
disabled={cancelGoal.isPending}
|
||||
onClick={() => cancelGoal.mutate(goal.id)}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{!terminal && (
|
||||
<div className="flex items-start gap-1">
|
||||
<p className={`min-w-0 flex-1 text-muted-foreground ${expanded ? 'whitespace-pre-wrap break-words' : 'truncate'}`}>
|
||||
{goal.objective}
|
||||
</p>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setExpanded((value) => !value)}
|
||||
aria-expanded={expanded}
|
||||
aria-label={expanded ? 'Collapse goal objective' : 'Expand goal objective'}
|
||||
className="shrink-0 rounded p-0.5 text-muted-foreground hover:text-foreground"
|
||||
>
|
||||
{expanded ? <ChevronUp className="h-3 w-3" /> : <ChevronDown className="h-3 w-3" />}
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{!terminal && goal.lastReason && (
|
||||
<p className="truncate text-muted-foreground" title={goal.lastReason}>
|
||||
{goal.lastReason}
|
||||
</p>
|
||||
)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user