Add Ralph status dialog and session management improvements (#171)

This commit is contained in:
Chris Scott authored and GitHub committed 2026-03-24 20:25:09 -04:00
1 parent 27352ea5cf
commit 86613b1ea4
12 files changed
+1132 -65

No files matched your search

+6 -2
View File
@@ -1,6 +1,6 @@
import { fetchWrapper, fetchWrapperVoid } from './fetchWrapper'
import { API_BASE_URL } from '@/config'
import type { Memory, MemoryStats, CreateMemoryRequest, UpdateMemoryRequest, PluginConfig, KvEntry, CreateKvEntryRequest, UpdateKvEntryRequest } from '@opencode-manager/shared/types'
import type { Memory, MemoryStats, CreateMemoryRequest, UpdateMemoryRequest, PluginConfig, KvEntry, CreateKvEntryRequest, UpdateKvEntryRequest, RalphState } from '@opencode-manager/shared/types'
export async function listMemories(filters?: {
projectId?: string
@@ -123,7 +123,11 @@ export async function updateKvEntry(projectId: string, key: string, data: Update
})
}
export async function cancelRalphLoop(repoId: string, sessionId: string): Promise<{ cancelled: boolean; worktreeName?: string }> {
export async function getRalphStatus(repoId: number): Promise<{ loops: RalphState[]; projectId?: string | null }> {
return fetchWrapper(`${API_BASE_URL}/api/memory/ralph/status?repoId=${repoId}`)
}
export async function cancelRalphLoop(repoId: number, sessionId: string): Promise<{ cancelled: boolean; worktreeName?: string }> {
return fetchWrapper(`${API_BASE_URL}/api/memory/ralph/cancel`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
@@ -0,0 +1,100 @@
import { Loader2, CheckCircle2, XCircle, Ban, AlertCircle } from 'lucide-react'
import { Button } from '@/components/ui/button'
import { Badge } from '@/components/ui/badge'
import type { RalphState } from '@opencode-manager/shared'
interface RepoRalphListProps {
isLoading: boolean
data: RalphState[] | undefined
error: Error | null
onCancel: (sessionId: string) => void
pendingSessionId: string | null
}
function StatusIcon({ loop }: { loop: RalphState }) {
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 RepoRalphList({ isLoading, data, error, onCancel, pendingSessionId }: RepoRalphListProps) {
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 Ralph 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 Ralph 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>
)
}
+1 -1
View File
@@ -544,7 +544,7 @@ export const useAbortSession = (
attemptAbort();
if (repoId) {
cancelRalphLoop(String(repoId), targetSessionID).catch(() => {})
cancelRalphLoop(repoId, targetSessionID).catch(() => {})
}
retryIntervalRef.current = setInterval(() => {
+48
View File
@@ -0,0 +1,48 @@
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query'
import { getRalphStatus, cancelRalphLoop } from '@/api/memory'
import { showToast } from '@/lib/toast'
export function useRalphStatus(repoId: number | undefined, open: boolean) {
const queryClient = useQueryClient()
const { data, isLoading, error } = useQuery({
queryKey: ['ralph-status', repoId],
queryFn: () => getRalphStatus(repoId!),
enabled: open && !!repoId,
staleTime: 0,
refetchInterval: ({ state }) => {
const loops = state.data?.loops ?? []
return loops.some(l => l.active) ? 5000 : false
},
})
const cancelMutation = useMutation({
mutationFn: ({ sessionId }: { sessionId: string }) =>
cancelRalphLoop(repoId!, sessionId),
onSuccess: (result) => {
if (result.cancelled) {
queryClient.invalidateQueries({ queryKey: ['ralph-status', repoId] })
} else {
showToast.error('Ralph loop is no longer active')
}
},
onError: () => {
showToast.error('Unable to cancel the Ralph loop. Please try again.')
},
})
const pendingSessionId = cancelMutation.isPending ? (cancelMutation.variables?.sessionId ?? null) : null
return { data, isLoading, error, cancelMutation, pendingSessionId }
}
export function useRalphActiveCount(repoId: number | undefined, enabled: boolean) {
const { data } = useQuery({
queryKey: ['ralph-status', repoId],
queryFn: () => getRalphStatus(repoId!),
enabled: enabled && !!repoId,
staleTime: 0,
refetchInterval: 10000,
})
return data?.loops.filter(l => l.active).length ?? 0
}
+17 -1
View File
@@ -3,16 +3,18 @@ import { useParams } from 'react-router-dom'
import { useQuery } from '@tanstack/react-query'
import { getRepo } from '@/api/repos'
import { useProjectSummary, useKvEntries } from '@/hooks/useMemories'
import { useRalphStatus } from '@/hooks/useRalphStatus'
import { Header } from '@/components/ui/header'
import { Button } from '@/components/ui/button'
import { Badge } from '@/components/ui/badge'
import { Card, CardContent } from '@/components/ui/card'
import { Tabs, TabsList, TabsTrigger, TabsContent } from '@/components/ui/tabs'
import { Loader2, Plus, Brain, Key } from 'lucide-react'
import { Loader2, Plus, Brain, Key, Bot } from 'lucide-react'
import { getRepoDisplayName } from '@/lib/utils'
import { MemoryList } from '@/components/memory/MemoryList'
import { MemoryFormDialog } from '@/components/memory/MemoryFormDialog'
import { KvList } from '@/components/memory/KvList'
import { RepoRalphList } from '@/components/repo/RepoRalphList'
export function Memories() {
const { id } = useParams<{ id: string }>()
@@ -28,6 +30,7 @@ export function Memories() {
const { data: projectSummary, isLoading: projectSummaryLoading } = useProjectSummary(repoId)
const { data: kvEntries } = useKvEntries(projectSummary?.projectId ?? undefined)
const { data: ralphData, isLoading: ralphLoading, error: ralphError, cancelMutation, pendingSessionId } = useRalphStatus(repoId, activeTab === 'ralph')
const projectId = projectSummary?.projectId ?? null
const stats = projectSummary?.stats ?? null
@@ -125,6 +128,10 @@ export function Memories() {
<Key className="w-4 h-4 mr-2" />
Key-Value
</TabsTrigger>
<TabsTrigger value="ralph">
<Bot className="w-4 h-4 mr-2" />
Ralph
</TabsTrigger>
</TabsList>
<TabsContent value="memories">
<MemoryList projectId={projectId} showFilters={true} />
@@ -132,6 +139,15 @@ export function Memories() {
<TabsContent value="kv">
<KvList projectId={projectId ?? undefined} />
</TabsContent>
<TabsContent value="ralph">
<RepoRalphList
isLoading={ralphLoading}
data={ralphData?.loops}
error={ralphError}
onCancel={(sessionId) => cancelMutation.mutate({ sessionId })}
pendingSessionId={pendingSessionId}
/>
</TabsContent>
</Tabs>
</div>