From a9ebee72c462b346d0a9d92d9c0a136ec89806d6 Mon Sep 17 00:00:00 2001 From: Chris Scott <99081550+chriswritescode-dev@users.noreply.github.com> Date: Fri, 19 Dec 2025 00:10:12 -0500 Subject: [PATCH] Fix compact command validation flow (#28) * feat: Implement /compact command with processing feedback - Add 'compact' status type to session status store - Update SessionStatusIndicator to show purple pulsing animation for compaction - Add summarizeSession() API method to OpenCode client - Modify command handler for /compact to: - Get current model from state - Show compaction toast - Set session to compacting status - Call summarize endpoint with provider/model - Update SSE handler to clear compact status on session.compacted event The user sees a purple animated indicator and 'Compacting' text during the operation, matching the TUI experience. * Fix compact command to use break instead of return for model validation --------- Co-authored-by: Chris Scott --- frontend/src/api/opencode.ts | 8 +++++ .../ui/session-status-indicator.tsx | 15 +++++++--- frontend/src/hooks/useCommandHandler.ts | 29 +++++++++++++++++-- frontend/src/hooks/useSSE.ts | 1 + frontend/src/stores/sessionStatusStore.ts | 1 + 5 files changed, 47 insertions(+), 7 deletions(-) diff --git a/frontend/src/api/opencode.ts b/frontend/src/api/opencode.ts index 844d9d33..b7e55066 100644 --- a/frontend/src/api/opencode.ts +++ b/frontend/src/api/opencode.ts @@ -82,6 +82,14 @@ export class OpenCodeClient { return response.data } + async summarizeSession(sessionID: string, providerID: string, modelID: string) { + const response = await this.client.post(`/session/${sessionID}/summarize`, { + providerID, + modelID + }) + return response.data + } + async getConfig() { const response = await this.client.get('/config') return response.data diff --git a/frontend/src/components/ui/session-status-indicator.tsx b/frontend/src/components/ui/session-status-indicator.tsx index 59c1c7de..4f9f3dc5 100644 --- a/frontend/src/components/ui/session-status-indicator.tsx +++ b/frontend/src/components/ui/session-status-indicator.tsx @@ -19,7 +19,7 @@ export const SessionStatusIndicator = memo(function SessionStatusIndicator({ const [retryCountdown, setRetryCountdown] = useState(0) useEffect(() => { - if (status.type !== 'busy' && status.type !== 'retry') { + if (status.type !== 'busy' && status.type !== 'retry' && status.type !== 'compact') { return } @@ -79,6 +79,13 @@ export const SessionStatusIndicator = memo(function SessionStatusIndicator({ return 'bg-amber-500/20' } + if (statusType === 'compact') { + if (intensity > 0.8) return 'bg-purple-500' + if (intensity > 0.5) return 'bg-purple-500/70' + if (intensity > 0.2) return 'bg-purple-500/40' + return 'bg-purple-500/20' + } + if (intensity > 0.8) return 'bg-blue-500' if (intensity > 0.5) return 'bg-blue-500/70' if (intensity > 0.2) return 'bg-blue-500/40' @@ -120,7 +127,7 @@ export const CompactStatusIndicator = memo(function CompactStatusIndicator({ const [frame, setFrame] = useState(0) useEffect(() => { - if (status.type === 'idle') return + if (status.type !== 'busy' && status.type !== 'retry' && status.type !== 'compact') return const interval = setInterval(() => { setFrame(prev => (prev + 1) % 8) @@ -134,13 +141,13 @@ export const CompactStatusIndicator = memo(function CompactStatusIndicator({ } const pulseFrames = ['●', '◐', '○', '◑', '●', '◐', '○', '◑'] - const color = status.type === 'retry' ? 'text-amber-500' : 'text-blue-500' + const color = status.type === 'retry' ? 'text-amber-500' : status.type === 'compact' ? 'text-purple-500' : 'text-blue-500' return ( {pulseFrames[frame]} - {status.type === 'retry' ? `Retry #${status.attempt}` : 'Working'} + {status.type === 'retry' ? `Retry #${status.attempt}` : status.type === 'compact' ? 'Compacting' : 'Working'} ) diff --git a/frontend/src/hooks/useCommandHandler.ts b/frontend/src/hooks/useCommandHandler.ts index cb2eb81e..1478e66d 100644 --- a/frontend/src/hooks/useCommandHandler.ts +++ b/frontend/src/hooks/useCommandHandler.ts @@ -2,8 +2,10 @@ import { useState, useCallback } from 'react' import { useNavigate } from 'react-router-dom' import { createOpenCodeClient } from '@/api/opencode' import { useCreateSession } from '@/hooks/useOpenCode' +import { useModelSelection } from '@/hooks/useModelSelection' import { showToast } from '@/lib/toast' import type { components } from '@/api/opencode-types' +import { useSessionStatus } from '@/stores/sessionStatusStore' type CommandType = components['schemas']['Command'] @@ -30,6 +32,8 @@ export function useCommandHandler({ }: CommandHandlerProps) { const navigate = useNavigate() const createSession = useCreateSession(opcodeUrl, directory) + const { model } = useModelSelection(opcodeUrl, directory) + const setSessionStatus = useSessionStatus((state) => state.setStatus) const [loading, setLoading] = useState(false) const executeCommand = useCallback(async (command: CommandType, args: string = '') => { @@ -96,11 +100,28 @@ export function useCommandHandler({ onExportSession() } break + + case 'compact': + case 'summarize': { + if (!model?.providerID || !model?.modelID) { + showToast.error('No model selected. Please select a provider and model first.') + break + } + + showToast.info('Compacting session...', { duration: 2000 }) + + setSessionStatus(sessionID, { type: 'compact' }) + + await client.summarizeSession( + sessionID, + model.providerID, + model.modelID + ) + break + } case 'share': case 'unshare': - case 'compact': - case 'summarize': case 'undo': case 'redo': case 'editor': @@ -119,10 +140,12 @@ export function useCommandHandler({ } } catch (error) { console.error('Failed to execute command:', error) + showToast.error(`Command failed: ${error instanceof Error ? error.message : 'Unknown error'}`) + setSessionStatus(sessionID, { type: 'idle' }) } finally { setLoading(false) } - }, [sessionID, opcodeUrl, directory, onShowSessionsDialog, onShowModelsDialog, onShowHelpDialog, onToggleDetails, onExportSession, createSession, navigate]) + }, [sessionID, opcodeUrl, directory, onShowSessionsDialog, onShowModelsDialog, onShowHelpDialog, onToggleDetails, onExportSession, createSession, navigate, model, setSessionStatus]) return { executeCommand, diff --git a/frontend/src/hooks/useSSE.ts b/frontend/src/hooks/useSSE.ts index e3402ba3..028e7fce 100644 --- a/frontend/src/hooks/useSSE.ts +++ b/frontend/src/hooks/useSSE.ts @@ -253,6 +253,7 @@ export const useSSE = (opcodeUrl: string | null | undefined, directory?: string) if (!('sessionID' in event.properties)) break const { sessionID } = event.properties + setSessionStatus(sessionID, { type: 'idle' }) queryClient.invalidateQueries({ queryKey: ['opencode', 'messages', opcodeUrl, sessionID, directory] }) diff --git a/frontend/src/stores/sessionStatusStore.ts b/frontend/src/stores/sessionStatusStore.ts index 75d89059..9790c225 100644 --- a/frontend/src/stores/sessionStatusStore.ts +++ b/frontend/src/stores/sessionStatusStore.ts @@ -3,6 +3,7 @@ import { create } from 'zustand' export type SessionStatusType = | { type: 'idle' } | { type: 'busy' } + | { type: 'compact' } | { type: 'retry'; attempt: number; message: string; next: number } interface SessionStatusStore {