mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 12:24:30 +01:00
Fix pnpm resources (#29)
* Add message undo/refresh features and stop audio floating button * Exclude workspace directory from pnpm workspace detection --------- Co-authored-by: Chris Scott <chris@cstech.dev>
This commit is contained in:
1 parent
fb430359ce
commit
0cbfe2e358
8 files changed
+338
-19
No files matched your search
@@ -5,8 +5,10 @@ import { Repos } from './pages/Repos'
|
||||
import { RepoDetail } from './pages/RepoDetail'
|
||||
import { SessionDetail } from './pages/SessionDetail'
|
||||
import { SettingsDialog } from './components/settings/SettingsDialog'
|
||||
import { FloatingActionButton } from './components/ui/floating-action-button'
|
||||
import { useSettingsDialog } from './hooks/useSettingsDialog'
|
||||
import { useTheme } from './hooks/useTheme'
|
||||
import { useTTS } from './hooks/useTTS'
|
||||
import { TTSProvider } from './contexts/TTSContext'
|
||||
|
||||
const queryClient = new QueryClient({
|
||||
@@ -20,6 +22,7 @@ const queryClient = new QueryClient({
|
||||
|
||||
function AppContent() {
|
||||
const { isOpen, close } = useSettingsDialog()
|
||||
const { isPlaying, stop } = useTTS()
|
||||
useTheme()
|
||||
|
||||
return (
|
||||
@@ -31,6 +34,12 @@ function AppContent() {
|
||||
|
||||
</Routes>
|
||||
<SettingsDialog open={isOpen} onOpenChange={close} />
|
||||
<FloatingActionButton
|
||||
variant="stop-audio"
|
||||
visible={isPlaying}
|
||||
onClick={stop}
|
||||
position="top-left"
|
||||
/>
|
||||
<Toaster
|
||||
position="bottom-right"
|
||||
expand={false}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { memo, useMemo, useState, useCallback } from 'react'
|
||||
import { MessagePart } from './MessagePart'
|
||||
import { MessageActionButtons } from './MessageActionButtons'
|
||||
import { UserMessageActionButtons } from './UserMessageActionButtons'
|
||||
import { EditableUserMessage, ClickableUserMessage } from './EditableUserMessage'
|
||||
import type { MessageWithParts } from '@/api/types'
|
||||
|
||||
@@ -19,6 +20,7 @@ interface MessageThreadProps {
|
||||
messages?: MessageWithParts[]
|
||||
onFileClick?: (filePath: string, lineNumber?: number) => void
|
||||
onChildSessionClick?: (sessionId: string) => void
|
||||
onUndoMessage?: (restoredPrompt: string) => void
|
||||
model?: string
|
||||
agent?: string
|
||||
}
|
||||
@@ -28,25 +30,20 @@ const isMessageStreaming = (msg: MessageWithParts): boolean => {
|
||||
return !('completed' in msg.info.time && msg.info.time.completed)
|
||||
}
|
||||
|
||||
const findPendingAssistantMessageId = (messages: MessageWithParts[]): string | undefined => {
|
||||
const findLastMessageByRole = (
|
||||
messages: MessageWithParts[],
|
||||
role: 'user' | 'assistant',
|
||||
predicate?: (msg: MessageWithParts) => boolean
|
||||
): string | undefined => {
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
const msg = messages[i]
|
||||
if (msg.info.role === 'assistant' && isMessageStreaming(msg)) {
|
||||
if (msg.info.role === role && (!predicate || predicate(msg))) {
|
||||
return msg.info.id
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
const findLastAssistantMessageId = (messages: MessageWithParts[]): string | undefined => {
|
||||
for (let i = messages.length - 1; i >= 0; i--) {
|
||||
if (messages[i].info.role === 'assistant') {
|
||||
return messages[i].info.id
|
||||
}
|
||||
}
|
||||
return undefined
|
||||
}
|
||||
|
||||
const findUserMessageBeforeAssistant = (
|
||||
messages: MessageWithParts[],
|
||||
assistantMessageId: string
|
||||
@@ -69,6 +66,7 @@ export const MessageThread = memo(function MessageThread({
|
||||
messages,
|
||||
onFileClick,
|
||||
onChildSessionClick,
|
||||
onUndoMessage,
|
||||
model,
|
||||
agent
|
||||
}: MessageThreadProps) {
|
||||
@@ -77,12 +75,17 @@ export const MessageThread = memo(function MessageThread({
|
||||
|
||||
const pendingAssistantId = useMemo(() => {
|
||||
if (!messages) return undefined
|
||||
return findPendingAssistantMessageId(messages)
|
||||
return findLastMessageByRole(messages, 'assistant', isMessageStreaming)
|
||||
}, [messages])
|
||||
|
||||
const lastAssistantId = useMemo(() => {
|
||||
if (!messages) return undefined
|
||||
return findLastAssistantMessageId(messages)
|
||||
return findLastMessageByRole(messages, 'assistant')
|
||||
}, [messages])
|
||||
|
||||
const lastUserMessageId = useMemo(() => {
|
||||
if (!messages) return undefined
|
||||
return findLastMessageByRole(messages, 'user')
|
||||
}, [messages])
|
||||
|
||||
const isSessionBusy = !!pendingAssistantId
|
||||
@@ -111,11 +114,13 @@ export const MessageThread = memo(function MessageThread({
|
||||
const streaming = isMessageStreaming(msg)
|
||||
const isQueued = msg.info.role === 'user' && pendingAssistantId && msg.info.id > pendingAssistantId
|
||||
const isLastAssistant = msg.info.id === lastAssistantId
|
||||
const isLastUserMessage = msg.info.role === 'user' && msg.info.id === lastUserMessageId
|
||||
const messageTextContent = getMessageTextContent(msg)
|
||||
|
||||
const nextAssistantMessage = messages.slice(index + 1).find(m => m.info.role === 'assistant')
|
||||
const isUserBeforeAssistant = msg.info.role === 'user' && nextAssistantMessage
|
||||
const canEditUserMessage = isUserBeforeAssistant && nextAssistantMessage?.info.id === lastAssistantId && !isSessionBusy
|
||||
const canUndoUserMessage = isLastUserMessage && nextAssistantMessage && !isSessionBusy && onUndoMessage
|
||||
|
||||
const userMessageContent = msg.info.role === 'assistant'
|
||||
? findUserMessageBeforeAssistant(messages, msg.info.id)
|
||||
@@ -174,6 +179,20 @@ export const MessageThread = memo(function MessageThread({
|
||||
agent={agent}
|
||||
/>
|
||||
)}
|
||||
|
||||
{canUndoUserMessage && (
|
||||
<UserMessageActionButtons
|
||||
opcodeUrl={opcodeUrl}
|
||||
sessionId={sessionID}
|
||||
directory={directory}
|
||||
userMessageId={msg.info.id}
|
||||
userMessageContent={messageTextContent}
|
||||
assistantMessageId={nextAssistantMessage.info.id}
|
||||
onUndo={onUndoMessage}
|
||||
model={model}
|
||||
agent={agent}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { useState, useRef, useEffect, useMemo, type KeyboardEvent } from 'react'
|
||||
import { useState, useRef, useEffect, useMemo, useImperativeHandle, forwardRef, type KeyboardEvent } from 'react'
|
||||
import { useSendPrompt, useAbortSession, useMessages, useSendShell, useAgents } from '@/hooks/useOpenCode'
|
||||
import { useSettings } from '@/hooks/useSettings'
|
||||
import { useCommands } from '@/hooks/useCommands'
|
||||
@@ -23,6 +23,11 @@ import type { MessageWithParts, FileInfo } from '@/api/types'
|
||||
|
||||
type CommandType = components['schemas']['Command']
|
||||
|
||||
export interface PromptInputHandle {
|
||||
setPromptValue: (value: string) => void
|
||||
clearPrompt: () => void
|
||||
}
|
||||
|
||||
interface PromptInputProps {
|
||||
opcodeUrl: string
|
||||
directory?: string
|
||||
@@ -35,9 +40,10 @@ interface PromptInputProps {
|
||||
onShowHelpDialog?: () => void
|
||||
onToggleDetails?: () => boolean
|
||||
onExportSession?: () => void
|
||||
onPromptChange?: (hasContent: boolean) => void
|
||||
}
|
||||
|
||||
export function PromptInput({
|
||||
export const PromptInput = forwardRef<PromptInputHandle, PromptInputProps>(function PromptInput({
|
||||
opcodeUrl,
|
||||
directory,
|
||||
sessionID,
|
||||
@@ -48,10 +54,30 @@ export function PromptInput({
|
||||
onShowModelsDialog,
|
||||
onShowHelpDialog,
|
||||
onToggleDetails,
|
||||
onExportSession
|
||||
}: PromptInputProps) {
|
||||
onExportSession,
|
||||
onPromptChange
|
||||
}, ref) {
|
||||
const [prompt, setPrompt] = useState('')
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
setPromptValue: (value: string) => {
|
||||
setPrompt(value)
|
||||
if (textareaRef.current) {
|
||||
textareaRef.current.style.height = 'auto'
|
||||
textareaRef.current.style.height = `${textareaRef.current.scrollHeight}px`
|
||||
textareaRef.current.focus()
|
||||
}
|
||||
},
|
||||
clearPrompt: () => {
|
||||
setPrompt('')
|
||||
setAttachedFiles(new Map())
|
||||
setSelectedAgent(null)
|
||||
if (textareaRef.current) {
|
||||
textareaRef.current.style.height = 'auto'
|
||||
}
|
||||
}
|
||||
}), [])
|
||||
|
||||
const [isBashMode, setIsBashMode] = useState(false)
|
||||
const [showSuggestions, setShowSuggestions] = useState(false)
|
||||
const [suggestionQuery, setSuggestionQuery] = useState('')
|
||||
@@ -465,6 +491,10 @@ export function PromptInput({
|
||||
}
|
||||
}, [disabled])
|
||||
|
||||
useEffect(() => {
|
||||
onPromptChange?.(prompt.trim().length > 0)
|
||||
}, [prompt, onPromptChange])
|
||||
|
||||
|
||||
|
||||
return (
|
||||
@@ -573,4 +603,4 @@ export function PromptInput({
|
||||
/>
|
||||
</div>
|
||||
)
|
||||
}
|
||||
})
|
||||
@@ -0,0 +1,86 @@
|
||||
import { memo } from 'react'
|
||||
import { X, RefreshCw, Loader2 } from 'lucide-react'
|
||||
import { useUndoMessage } from '@/hooks/useUndoMessage'
|
||||
import { useRefreshMessage } from '@/hooks/useRemoveMessage'
|
||||
|
||||
interface UserMessageActionButtonsProps {
|
||||
opcodeUrl: string
|
||||
sessionId: string
|
||||
directory?: string
|
||||
userMessageId: string
|
||||
userMessageContent: string
|
||||
assistantMessageId: string
|
||||
onUndo: (restoredPrompt: string) => void
|
||||
model?: string
|
||||
agent?: string
|
||||
}
|
||||
|
||||
export const UserMessageActionButtons = memo(function UserMessageActionButtons({
|
||||
opcodeUrl,
|
||||
sessionId,
|
||||
directory,
|
||||
userMessageId,
|
||||
userMessageContent,
|
||||
assistantMessageId,
|
||||
onUndo,
|
||||
model,
|
||||
agent
|
||||
}: UserMessageActionButtonsProps) {
|
||||
const undoMessage = useUndoMessage({
|
||||
opcodeUrl,
|
||||
sessionId,
|
||||
directory,
|
||||
onSuccess: onUndo
|
||||
})
|
||||
const refreshMessage = useRefreshMessage({ opcodeUrl, sessionId, directory })
|
||||
|
||||
const isLoading = undoMessage.isPending || refreshMessage.isPending
|
||||
|
||||
const handleUndo = () => {
|
||||
if (isLoading) return
|
||||
undoMessage.mutate({
|
||||
messageID: userMessageId,
|
||||
messageContent: userMessageContent
|
||||
})
|
||||
}
|
||||
|
||||
const handleRefresh = () => {
|
||||
if (isLoading || !userMessageContent) return
|
||||
refreshMessage.mutate({
|
||||
assistantMessageID: assistantMessageId,
|
||||
userMessageContent,
|
||||
model,
|
||||
agent
|
||||
})
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="flex items-center gap-1 opacity-0 group-hover:opacity-100 transition-opacity">
|
||||
<button
|
||||
onClick={handleRefresh}
|
||||
disabled={isLoading}
|
||||
className="p-1 rounded hover:bg-primary/20 text-muted-foreground hover:text-primary transition-colors disabled:opacity-50"
|
||||
title="Try again for different response"
|
||||
>
|
||||
{refreshMessage.isPending ? (
|
||||
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
||||
) : (
|
||||
<RefreshCw className="w-3.5 h-3.5" />
|
||||
)}
|
||||
</button>
|
||||
|
||||
<button
|
||||
onClick={handleUndo}
|
||||
disabled={isLoading}
|
||||
className="p-1 rounded hover:bg-destructive/20 text-muted-foreground hover:text-destructive transition-colors disabled:opacity-50"
|
||||
title="Undo this message"
|
||||
>
|
||||
{undoMessage.isPending ? (
|
||||
<Loader2 className="w-3.5 h-3.5 animate-spin" />
|
||||
) : (
|
||||
<X className="w-3.5 h-3.5" />
|
||||
)}
|
||||
</button>
|
||||
</div>
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,84 @@
|
||||
import { memo, type ReactNode } from 'react'
|
||||
import { X, VolumeX, Loader2 } from 'lucide-react'
|
||||
import { cn } from '@/lib/utils'
|
||||
|
||||
type FloatingActionVariant = 'clear' | 'stop-audio' | 'custom'
|
||||
|
||||
interface FloatingActionButtonProps {
|
||||
variant: FloatingActionVariant
|
||||
onClick: () => void
|
||||
visible: boolean
|
||||
loading?: boolean
|
||||
icon?: ReactNode
|
||||
label?: string
|
||||
position?: 'top-right' | 'top-left' | 'bottom-right' | 'bottom-left'
|
||||
className?: string
|
||||
}
|
||||
|
||||
const variantConfig: Record<FloatingActionVariant, { icon: ReactNode; label: string; colors: string }> = {
|
||||
'clear': {
|
||||
icon: <X className="w-5 h-5" />,
|
||||
label: 'Clear',
|
||||
colors: 'bg-muted hover:bg-destructive/20 text-muted-foreground hover:text-destructive border-border hover:border-destructive/50'
|
||||
},
|
||||
'stop-audio': {
|
||||
icon: <VolumeX className="w-5 h-5" />,
|
||||
label: 'Stop Audio',
|
||||
colors: 'bg-destructive/90 hover:bg-destructive text-destructive-foreground border-destructive'
|
||||
},
|
||||
'custom': {
|
||||
icon: null,
|
||||
label: '',
|
||||
colors: 'bg-muted hover:bg-muted-foreground/20 text-muted-foreground border-border'
|
||||
}
|
||||
}
|
||||
|
||||
const positionClasses: Record<NonNullable<FloatingActionButtonProps['position']>, string> = {
|
||||
'top-right': 'top-4 right-4',
|
||||
'top-left': 'top-4 left-4',
|
||||
'bottom-right': 'bottom-4 right-4',
|
||||
'bottom-left': 'bottom-4 left-4'
|
||||
}
|
||||
|
||||
export const FloatingActionButton = memo(function FloatingActionButton({
|
||||
variant,
|
||||
onClick,
|
||||
visible,
|
||||
loading = false,
|
||||
icon,
|
||||
label,
|
||||
position = 'top-right',
|
||||
className
|
||||
}: FloatingActionButtonProps) {
|
||||
const config = variantConfig[variant]
|
||||
const displayIcon = icon ?? config.icon
|
||||
const displayLabel = label ?? config.label
|
||||
|
||||
if (!visible) return null
|
||||
|
||||
return (
|
||||
<button
|
||||
onClick={onClick}
|
||||
disabled={loading}
|
||||
className={cn(
|
||||
'fixed z-50 flex items-center gap-2 px-4 py-3 rounded-full border shadow-lg backdrop-blur-sm transition-all duration-200',
|
||||
'min-w-[48px] min-h-[48px]',
|
||||
'active:scale-95',
|
||||
config.colors,
|
||||
positionClasses[position],
|
||||
loading && 'opacity-70 cursor-not-allowed',
|
||||
className
|
||||
)}
|
||||
aria-label={displayLabel}
|
||||
>
|
||||
{loading ? (
|
||||
<Loader2 className="w-5 h-5 animate-spin" />
|
||||
) : (
|
||||
displayIcon
|
||||
)}
|
||||
{displayLabel && (
|
||||
<span className="text-sm font-medium hidden sm:inline">{displayLabel}</span>
|
||||
)}
|
||||
</button>
|
||||
)
|
||||
})
|
||||
@@ -0,0 +1,68 @@
|
||||
import { useMutation, useQueryClient } from '@tanstack/react-query'
|
||||
import { createOpenCodeClient } from '@/api/opencode'
|
||||
import { showToast } from '@/lib/toast'
|
||||
import type { MessageListResponse } from '@/api/types'
|
||||
|
||||
interface UseUndoMessageOptions {
|
||||
opcodeUrl: string | null
|
||||
sessionId: string
|
||||
directory?: string
|
||||
onSuccess?: (restoredPrompt: string) => void
|
||||
}
|
||||
|
||||
export function useUndoMessage({
|
||||
opcodeUrl,
|
||||
sessionId,
|
||||
directory,
|
||||
onSuccess
|
||||
}: UseUndoMessageOptions) {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ messageID, messageContent }: { messageID: string, messageContent: string }) => {
|
||||
if (!opcodeUrl) throw new Error('OpenCode URL not available')
|
||||
|
||||
const client = createOpenCodeClient(opcodeUrl, directory)
|
||||
await client.revertMessage(sessionId, { messageID })
|
||||
return messageContent
|
||||
},
|
||||
onMutate: async ({ messageID }) => {
|
||||
const queryKey = ['opencode', 'messages', opcodeUrl, sessionId, directory]
|
||||
|
||||
await queryClient.cancelQueries({ queryKey })
|
||||
|
||||
const previousMessages = queryClient.getQueryData<MessageListResponse>(queryKey)
|
||||
|
||||
if (previousMessages) {
|
||||
const messageIndex = previousMessages.findIndex(m => m.info.id === messageID)
|
||||
if (messageIndex !== -1) {
|
||||
const newMessages = previousMessages.slice(0, messageIndex)
|
||||
queryClient.setQueryData(queryKey, newMessages)
|
||||
}
|
||||
}
|
||||
|
||||
return { previousMessages }
|
||||
},
|
||||
onError: (error, _, context) => {
|
||||
console.error('Failed to undo message:', error)
|
||||
|
||||
if (context?.previousMessages) {
|
||||
queryClient.setQueryData(
|
||||
['opencode', 'messages', opcodeUrl, sessionId, directory],
|
||||
context.previousMessages
|
||||
)
|
||||
}
|
||||
|
||||
showToast.error('Failed to undo message')
|
||||
},
|
||||
onSuccess: (restoredPrompt) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['opencode', 'messages', opcodeUrl, sessionId, directory]
|
||||
})
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ['opencode', 'session', opcodeUrl, sessionId, directory]
|
||||
})
|
||||
onSuccess?.(restoredPrompt)
|
||||
}
|
||||
})
|
||||
}
|
||||
@@ -3,7 +3,8 @@ import { useParams, useNavigate } from "react-router-dom";
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { getRepo } from "@/api/repos";
|
||||
import { MessageThread } from "@/components/message/MessageThread";
|
||||
import { PromptInput } from "@/components/message/PromptInput";
|
||||
import { PromptInput, type PromptInputHandle } from "@/components/message/PromptInput";
|
||||
import { FloatingActionButton } from "@/components/ui/floating-action-button";
|
||||
import { ModelSelectDialog } from "@/components/model/ModelSelectDialog";
|
||||
import { SessionDetailHeader } from "@/components/session/SessionDetailHeader";
|
||||
import { SessionList } from "@/components/session/SessionList";
|
||||
@@ -33,11 +34,13 @@ export function SessionDetail() {
|
||||
const { preferences, updateSettings } = useSettings();
|
||||
const messageContainerRef = useRef<HTMLDivElement>(null);
|
||||
const pageRef = useRef<HTMLDivElement>(null);
|
||||
const promptInputRef = useRef<PromptInputHandle>(null);
|
||||
const [modelDialogOpen, setModelDialogOpen] = useState(false);
|
||||
const [sessionsDialogOpen, setSessionsDialogOpen] = useState(false);
|
||||
const [fileBrowserOpen, setFileBrowserOpen] = useState(false);
|
||||
const [selectedFilePath, setSelectedFilePath] = useState<string | undefined>();
|
||||
const [showScrollButton, setShowScrollButton] = useState(false);
|
||||
const [hasPromptContent, setHasPromptContent] = useState(false);
|
||||
|
||||
const handleSwipeBack = useCallback(() => {
|
||||
navigate(`/repos/${repoId}`);
|
||||
@@ -176,6 +179,14 @@ export function SessionDetail() {
|
||||
showToast.success(`Exported to ${filename}`)
|
||||
}, [messages, session]);
|
||||
|
||||
const handleUndoMessage = useCallback((restoredPrompt: string) => {
|
||||
promptInputRef.current?.setPromptValue(restoredPrompt)
|
||||
}, []);
|
||||
|
||||
const handleClearPrompt = useCallback(() => {
|
||||
promptInputRef.current?.clearPrompt()
|
||||
}, []);
|
||||
|
||||
|
||||
|
||||
if (!sessionId) {
|
||||
@@ -231,6 +242,7 @@ export function SessionDetail() {
|
||||
messages={messages}
|
||||
onFileClick={handleFileClick}
|
||||
onChildSessionClick={handleChildSessionClick}
|
||||
onUndoMessage={handleUndoMessage}
|
||||
model={modelString || undefined}
|
||||
/>
|
||||
) : null}
|
||||
@@ -238,6 +250,7 @@ export function SessionDetail() {
|
||||
{opcodeUrl && repoDirectory && (
|
||||
<div className="absolute bottom-0 left-0 right-0 flex justify-center">
|
||||
<PromptInput
|
||||
ref={promptInputRef}
|
||||
opcodeUrl={opcodeUrl}
|
||||
directory={repoDirectory}
|
||||
sessionID={sessionId}
|
||||
@@ -251,9 +264,18 @@ export function SessionDetail() {
|
||||
}}
|
||||
onToggleDetails={handleToggleDetails}
|
||||
onExportSession={handleExportSession}
|
||||
onPromptChange={setHasPromptContent}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
|
||||
<FloatingActionButton
|
||||
variant="clear"
|
||||
visible={hasPromptContent}
|
||||
onClick={handleClearPrompt}
|
||||
position="top-right"
|
||||
label="Clear"
|
||||
/>
|
||||
</div>
|
||||
|
||||
<ModelSelectDialog
|
||||
|
||||
@@ -2,3 +2,4 @@ packages:
|
||||
- 'shared'
|
||||
- 'backend'
|
||||
- 'frontend'
|
||||
- '!workspace/**'
|
||||
Reference in new issue
Block a user